Skip to content

Commit e669eda

Browse files
committed
JAVA-2859: Don't block selecting server when closing server session pool
1 parent 420da30 commit e669eda

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

driver-core/src/main/com/mongodb/internal/session/ServerSessionPool.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
import com.mongodb.MongoException;
2020
import com.mongodb.ReadPreference;
2121
import com.mongodb.connection.Cluster;
22+
import com.mongodb.connection.ClusterDescription;
2223
import com.mongodb.connection.Connection;
24+
import com.mongodb.connection.ServerDescription;
2325
import com.mongodb.internal.connection.ConcurrentPool;
2426
import com.mongodb.internal.connection.ConcurrentPool.Prune;
2527
import com.mongodb.internal.connection.NoOpSessionContext;
2628
import com.mongodb.internal.validator.NoOpFieldNameValidator;
2729
import com.mongodb.selector.ReadPreferenceServerSelector;
30+
import com.mongodb.selector.ServerSelector;
2831
import com.mongodb.session.ServerSession;
2932
import org.bson.BsonArray;
3033
import org.bson.BsonBinary;
@@ -36,6 +39,7 @@
3639
import org.bson.codecs.UuidCodec;
3740

3841
import java.util.ArrayList;
42+
import java.util.Collections;
3943
import java.util.List;
4044
import java.util.UUID;
4145

@@ -118,7 +122,23 @@ private void endClosedSessions() {
118122
return;
119123
}
120124

121-
Connection connection = cluster.selectServer(new ReadPreferenceServerSelector(ReadPreference.primaryPreferred())).getConnection();
125+
final List<ServerDescription> primaryPreferred = new ReadPreferenceServerSelector(ReadPreference.primaryPreferred())
126+
.select(cluster.getCurrentDescription());
127+
if (primaryPreferred.isEmpty()) {
128+
return;
129+
}
130+
131+
Connection connection = cluster.selectServer(new ServerSelector() {
132+
@Override
133+
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
134+
for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
135+
if (cur.getAddress().equals(primaryPreferred.get(0).getAddress())) {
136+
return Collections.singletonList(cur);
137+
}
138+
}
139+
return Collections.emptyList();
140+
}
141+
}).getConnection();
122142
try {
123143
connection.command("admin",
124144
new BsonDocument("endSessions", new BsonArray(closedSessionIdentifiers)), new NoOpFieldNameValidator(),

driver-core/src/test/unit/com/mongodb/internal/session/ServerSessionPoolSpecification.groovy

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import com.mongodb.connection.ServerDescription
2626
import com.mongodb.connection.ServerSettings
2727
import com.mongodb.internal.connection.NoOpSessionContext
2828
import com.mongodb.internal.validator.NoOpFieldNameValidator
29-
import com.mongodb.selector.ReadPreferenceServerSelector
3029
import org.bson.BsonArray
3130
import org.bson.BsonBinarySubType
3231
import org.bson.BsonDocument
@@ -252,14 +251,14 @@ class ServerSessionPoolSpecification extends Specification {
252251

253252
then:
254253
// first batch is the first 10K sessions, final batch is the last one
255-
1 * cluster.selectServer { (it as ReadPreferenceServerSelector).readPreference == primaryPreferred() } >> server
254+
1 * cluster.selectServer(_) >> server
256255
1 * connection.command('admin',
257256
new BsonDocument('endSessions', new BsonArray(sessions*.getIdentifier())),
258257
{ it instanceof NoOpFieldNameValidator }, primaryPreferred(),
259258
{ it instanceof BsonDocumentCodec }, NoOpSessionContext.INSTANCE) >> new BsonDocument()
260259
1 * connection.release()
261260

262-
1 * cluster.selectServer { (it as ReadPreferenceServerSelector).readPreference == primaryPreferred() } >> server
261+
1 * cluster.selectServer(_) >> server
263262
1 * connection.command('admin',
264263
new BsonDocument('endSessions', new BsonArray([oneOverBatchSizeSession.getIdentifier()])),
265264
{ it instanceof NoOpFieldNameValidator }, primaryPreferred(),

0 commit comments

Comments
 (0)