Skip to content

Commit 8b5e415

Browse files
committed
A pooled DBPort instance now determines its own server version
JAVA-1635
1 parent 982d14d commit 8b5e415

File tree

8 files changed

+45
-19
lines changed

8 files changed

+45
-19
lines changed

src/main/com/mongodb/DBApiLayer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ CommandResult doAuthenticate(MongoCredential credentials) {
250250
boolean isServerVersionAtLeast(final List<Integer> versionList) {
251251
DBPort primaryPort = getConnector().getPrimaryPort();
252252
try {
253-
return getConnector().getServerDescription(primaryPort.getAddress()).getVersion()
254-
.compareTo(new ServerVersion(versionList)) >= 0;
253+
return primaryPort.getServerVersion().compareTo(new ServerVersion(versionList)) >= 0;
255254
} finally {
256255
_connector.releasePort(primaryPort);
257256
}

src/main/com/mongodb/DBCollectionImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public void createIndex(final DBObject keys, final DBObject options, DBEncoder e
382382
index.putAll(options);
383383
index.put("key", keys);
384384

385-
if (connector.getServerDescription(port.getAddress()).getVersion().compareTo(new ServerVersion(2, 6)) >= 0) {
385+
if (port.getServerVersion().compareTo(new ServerVersion(2, 6)) >= 0) {
386386
final BasicDBObject createIndexes = new BasicDBObject("createIndexes", getName());
387387

388388
BasicDBList list = new BasicDBList();
@@ -493,7 +493,7 @@ public BulkWriteResult execute() throws IOException {
493493

494494
private boolean useWriteCommands(final WriteConcern concern, final DBPort port) {
495495
return concern.callGetLastError() &&
496-
db.getConnector().getServerDescription(port.getAddress()).getVersion().compareTo(new ServerVersion(2, 6)) >= 0;
496+
port.getServerVersion().compareTo(new ServerVersion(2, 6)) >= 0;
497497
}
498498

499499
private MessageSettings getMessageSettings(final DBPort port) {

src/main/com/mongodb/DBPort.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,20 @@ public class DBPort implements Connection {
8686
*/
8787
@SuppressWarnings("deprecation")
8888
public DBPort( ServerAddress addr ){
89-
this( addr , null , new MongoOptions(), 0 );
89+
this( addr , new MongoOptions());
9090
}
91-
92-
DBPort( ServerAddress addr, PooledConnectionProvider pool, MongoOptions options, int generation ) {
91+
92+
// Normal usage
93+
DBPort( ServerAddress addr, PooledConnectionProvider pool, Mongo mongo, int generation ) {
94+
this(addr, pool, mongo, mongo.getMongoOptions(), generation);
95+
}
96+
97+
// Server monitor usage
98+
DBPort( ServerAddress addr, MongoOptions options ) {
99+
this(addr, null, null, options, 0);
100+
}
101+
102+
private DBPort( ServerAddress addr, PooledConnectionProvider pool, Mongo mongo, MongoOptions options, int generation ) {
93103
_options = options;
94104
_sa = addr;
95105
_addr = addr;
@@ -98,8 +108,8 @@ public DBPort( ServerAddress addr ){
98108

99109
_logger = Logger.getLogger( _rootLogger.getName() + "." + addr.toString() );
100110
try {
101-
ensureOpen();
102111
_decoder = _options.dbDecoderFactory.create();
112+
ensureOpen(mongo);
103113
openedAt = System.currentTimeMillis();
104114
lastUsedAt = openedAt;
105115
} catch (IOException e) {
@@ -248,6 +258,15 @@ InputStream getInputStream() throws IOException {
248258
* @throws IOException
249259
*/
250260
public synchronized void ensureOpen() throws IOException {
261+
ensureOpen(null);
262+
}
263+
264+
/**
265+
* makes sure that a connection to the server has been opened
266+
* @throws IOException
267+
* @param mongo
268+
*/
269+
private synchronized void ensureOpen(final Mongo mongo) throws IOException {
251270

252271
if ( _socket != null )
253272
return;
@@ -271,6 +290,9 @@ public synchronized void ensureOpen() throws IOException {
271290
_socket.setSoTimeout( _options.socketTimeout );
272291
_in = new BufferedInputStream( _socket.getInputStream() );
273292
_out = _socket.getOutputStream();
293+
if (mongo != null) {
294+
_serverVersion = ServerMonitor.getVersion(runCommand(mongo.getDB("admin"), new BasicDBObject("buildinfo", 1)));
295+
}
274296
successfullyConnected = true;
275297
}
276298
catch ( IOException e ){
@@ -364,7 +386,7 @@ CommandResult authenticate(Mongo mongo, final MongoCredential credentials) {
364386
Authenticator authenticator;
365387
MongoCredential actualCredentials;
366388
if (credentials.getMechanism() == null) {
367-
if (mongo.getConnector().getServerDescription(getAddress()).getVersion().compareTo(new ServerVersion(3, 0)) >= 0) {
389+
if (_serverVersion.compareTo(new ServerVersion(3, 0)) >= 0) {
368390
actualCredentials = MongoCredential.createScramSha1Credential(credentials.getUserName(), credentials.getSource(),
369391
credentials.getPassword());
370392
} else {
@@ -415,6 +437,10 @@ public long getUsageCount() {
415437
return usageCount;
416438
}
417439

440+
ServerVersion getServerVersion() {
441+
return _serverVersion;
442+
}
443+
418444
PooledConnectionProvider getProvider() {
419445
return provider;
420446
}
@@ -440,6 +466,7 @@ Set<String> getAuthenticatedDatabases() {
440466
private volatile Socket _socket;
441467
private volatile InputStream _in;
442468
private volatile OutputStream _out;
469+
private volatile ServerVersion _serverVersion;
443470

444471
// needs synchronization to ensure that modifications are published.
445472
private final Set<String> authenticatedDatabases = Collections.synchronizedSet(new HashSet<String>());

src/main/com/mongodb/DBPortFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
package com.mongodb;
1818

1919
class DBPortFactory implements ConnectionFactory {
20-
private final MongoOptions options;
20+
private final Mongo mongo;
2121

22-
DBPortFactory(MongoOptions options) {
23-
this.options = options;
22+
DBPortFactory(Mongo mongo) {
23+
this.mongo = mongo;
2424
}
2525

2626
@Override
2727
public Connection create(ServerAddress serverAddress, PooledConnectionProvider provider, int generation) {
28-
return new DBPort(serverAddress, provider, options, generation);
28+
return new DBPort(serverAddress, provider, mongo, generation);
2929
}
3030
}

src/main/com/mongodb/DefaultClusterableServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ClusterableServer create(final ServerAddress serverAddress) {
4242
.maxWaitTime(options.getMaxWaitTime(), MILLISECONDS)
4343
.build();
4444
return new DefaultServer(serverAddress, settings, clusterId,
45-
new PooledConnectionProvider(clusterId, serverAddress, new DBPortFactory(options), connectionPoolSettings,
45+
new PooledConnectionProvider(clusterId, serverAddress, new DBPortFactory(mongo), connectionPoolSettings,
4646
new JMXConnectionPoolListener(mongo.getMongoOptions().getDescription())),
4747
mongo);
4848
}

src/main/com/mongodb/ServerMonitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void run() {
8585
Throwable previousException = currentException;
8686
try {
8787
if (connection == null) {
88-
connection = new DBPort(serverAddress, null, getOptions(), 0);
88+
connection = new DBPort(serverAddress, getOptions());
8989
}
9090
try {
9191
currentServerDescription = lookupServerDescription(connection);
@@ -99,7 +99,7 @@ public void run() {
9999
connection = null;
100100
connectionProvider.invalidate();
101101
}
102-
connection = new DBPort(serverAddress, null, getOptions(), 0);
102+
connection = new DBPort(serverAddress, getOptions());
103103
try {
104104
currentServerDescription = lookupServerDescription(connection);
105105
} catch (IOException e1) {
@@ -263,7 +263,7 @@ private ServerDescription createDescription(final CommandResult commandResult, f
263263
}
264264

265265
@SuppressWarnings("unchecked")
266-
private static ServerVersion getVersion(final CommandResult buildInfoResult) {
266+
static ServerVersion getVersion(final CommandResult buildInfoResult) {
267267
return new ServerVersion(((List<Integer>) buildInfoResult.get("versionArray")).subList(0, 3));
268268
}
269269

src/test/com/mongodb/DBPortTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void testAuthentication() throws IOException {
4242
db1.addUser("u1", "e".toCharArray());
4343
db2.addUser("u2", "e".toCharArray());
4444

45-
DBPort port = new DBPort(m.getAddress());
45+
DBPort port = new DBPort(m.getAddress(), null, m, 0);
4646
port.checkAuth(m);
4747

4848
Set<String> expected = new HashSet<String>();

src/test/com/mongodb/ServerMonitorSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class ServerMonitorSpecification extends FunctionalSpecification {
135135
def initializeServerMonitor(ServerAddress address) {
136136
def options = new MongoOptions()
137137
options.connectTimeout = 1000
138-
def connectionProvider = new PooledConnectionProvider('cluster-1', address, new DBPortFactory(options),
138+
def connectionProvider = new PooledConnectionProvider('cluster-1', address, new DBPortFactory(getMongoClient()),
139139
ConnectionPoolSettings.builder().maxSize(1).build(),
140140
new JMXConnectionPoolListener());
141141
serverMonitor = new ServerMonitor(address,

0 commit comments

Comments
 (0)