From d00af73f9d611492085e04e0e2a0b25523a00672 Mon Sep 17 00:00:00 2001 From: Almas Abdrazak Date: Tue, 2 Dec 2025 15:12:48 -0800 Subject: [PATCH 1/4] JAVA-4320 fix server selection flaky test --- .../mongodb/client/AbstractServerSelectionProseTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java index 506a40d8bd6..e021137b9a8 100644 --- a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java +++ b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java @@ -72,7 +72,7 @@ void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedExcepti String appName = "loadBalancingTest"; int timeoutSeconds = 60; int tasks = 10; - int opsPerTask = 100; + int opsPerTask = 1000; // it used to be 100 but NodeJs driver has 1000 TestCommandListener commandListener = new TestCommandListener(singletonList("commandStartedEvent"), singletonList("drop")); MongoClientSettings clientSettings = getMongoClientSettingsBuilder() .applicationName(appName) @@ -109,7 +109,10 @@ void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedExcepti commandListener.reset(); Map selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds); - selectionRates.values().forEach(rate -> assertEquals(0.5, rate, 0.1, selectionRates::toString)); + // assert that each mongos was selected roughly 50% of the time (within +/- 15%). + // the specification says within 10% but in practice + // the deviation can be higher , Nodejs driver uses 15% and has not seen failures with that threshold + selectionRates.values().forEach(rate -> assertEquals(0.5, rate, 0.15, selectionRates::toString)); } finally { executor.shutdownNow(); assertTrue(executor.awaitTermination(timeoutSeconds, SECONDS)); From 1b68542b8d235a6fbc6792e311067a9881c83a3c Mon Sep 17 00:00:00 2001 From: Almas Abdrazak Date: Tue, 9 Dec 2025 14:01:44 -0800 Subject: [PATCH 2/4] set server selection threshold to 3seconds --- .../client/AbstractServerSelectionProseTest.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java index e021137b9a8..76a75dfbda3 100644 --- a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java +++ b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java @@ -20,6 +20,9 @@ import com.mongodb.ServerAddress; import com.mongodb.event.CommandStartedEvent; import com.mongodb.internal.connection.TestCommandListener; + +import java.util.concurrent.TimeUnit; + import org.bson.BsonArray; import org.bson.BsonBoolean; import org.bson.BsonDocument; @@ -72,11 +75,13 @@ void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedExcepti String appName = "loadBalancingTest"; int timeoutSeconds = 60; int tasks = 10; - int opsPerTask = 1000; // it used to be 100 but NodeJs driver has 1000 + int opsPerTask = 100; TestCommandListener commandListener = new TestCommandListener(singletonList("commandStartedEvent"), singletonList("drop")); MongoClientSettings clientSettings = getMongoClientSettingsBuilder() .applicationName(appName) .applyConnectionString(multiMongosConnectionString) + // set it to 3000 ms according to specification + .applyToClusterSettings(builder -> builder.localThreshold(3000L, TimeUnit.MILLISECONDS)) .applyToConnectionPoolSettings(builder -> builder .minSize(tasks)) .addCommandListener(commandListener) @@ -109,10 +114,7 @@ void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedExcepti commandListener.reset(); Map selectionRates = doSelections(collection, commandListener, executor, tasks, opsPerTask, timeoutSeconds); - // assert that each mongos was selected roughly 50% of the time (within +/- 15%). - // the specification says within 10% but in practice - // the deviation can be higher , Nodejs driver uses 15% and has not seen failures with that threshold - selectionRates.values().forEach(rate -> assertEquals(0.5, rate, 0.15, selectionRates::toString)); + selectionRates.values().forEach(rate -> assertEquals(0.5, rate, 0.1, selectionRates::toString)); } finally { executor.shutdownNow(); assertTrue(executor.awaitTermination(timeoutSeconds, SECONDS)); From 5ab863eb3c2e17d9121f88f9a51036264f667b77 Mon Sep 17 00:00:00 2001 From: Almas Abdrazak Date: Wed, 10 Dec 2025 22:30:00 -0800 Subject: [PATCH 3/4] serverSelection flaky test, update comments --- .../mongodb/client/AbstractServerSelectionProseTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java index 76a75dfbda3..0f74142ec4a 100644 --- a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java +++ b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java @@ -80,7 +80,11 @@ void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedExcepti MongoClientSettings clientSettings = getMongoClientSettingsBuilder() .applicationName(appName) .applyConnectionString(multiMongosConnectionString) - // set it to 3000 ms according to specification + // our default `localThreshold` value is 15 ms, + // which deviates from the default value 3000 ms required by the specification + // such a small localThreshold disrupts server + // selection in this probabilistic test enough to make it fail noticeably often. + // with 3000 ms the test passes reliably. .applyToClusterSettings(builder -> builder.localThreshold(3000L, TimeUnit.MILLISECONDS)) .applyToConnectionPoolSettings(builder -> builder .minSize(tasks)) From e66d05e7f6f462d5236be4fa0cc68ad306cc0ff6 Mon Sep 17 00:00:00 2001 From: Almas Abdrazak Date: Fri, 12 Dec 2025 16:20:12 -0800 Subject: [PATCH 4/4] server selection , update threshold to 30_000 MS --- .../mongodb/client/AbstractServerSelectionProseTest.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java index 0f74142ec4a..45cc8c28aa2 100644 --- a/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java +++ b/driver-sync/src/test/functional/com/mongodb/client/AbstractServerSelectionProseTest.java @@ -80,12 +80,7 @@ void operationCountBasedSelectionWithinLatencyWindow() throws InterruptedExcepti MongoClientSettings clientSettings = getMongoClientSettingsBuilder() .applicationName(appName) .applyConnectionString(multiMongosConnectionString) - // our default `localThreshold` value is 15 ms, - // which deviates from the default value 3000 ms required by the specification - // such a small localThreshold disrupts server - // selection in this probabilistic test enough to make it fail noticeably often. - // with 3000 ms the test passes reliably. - .applyToClusterSettings(builder -> builder.localThreshold(3000L, TimeUnit.MILLISECONDS)) + .applyToClusterSettings(builder -> builder.localThreshold(30_000L, TimeUnit.MILLISECONDS)) .applyToConnectionPoolSettings(builder -> builder .minSize(tasks)) .addCommandListener(commandListener)