Skip to content

Use connectTimeoutMS for connection establishment in maintenance TimeoutContext. #1777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ public long getWriteTimeoutMS() {

public int getConnectTimeoutMs() {
final long connectTimeoutMS = getTimeoutSettings().getConnectTimeoutMS();
if (isMaintenanceContext) {
return (int) connectTimeoutMS;
}

return Math.toIntExact(Timeout.nullAsInfinite(timeout).call(MILLISECONDS,
() -> connectTimeoutMS,
(ms) -> connectTimeoutMS == 0 ? ms : Math.min(ms, connectTimeoutMS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ static void initialize(final OperationContext operationContext, final Socket soc
static void configureSocket(final Socket socket, final OperationContext operationContext, final SocketSettings settings) throws SocketException {
socket.setTcpNoDelay(true);
socket.setKeepAlive(true);
int readTimeoutMS = (int) operationContext.getTimeoutContext().getReadTimeoutMS();
if (readTimeoutMS > 0) {
socket.setSoTimeout(readTimeoutMS);
}

// Adding keep alive options for users of Java 11+. These options will be ignored for older Java versions.
setExtendedSocketOptions(socket);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.mongodb.event.ConnectionClosedEvent;
import com.mongodb.event.ConnectionCreatedEvent;
import com.mongodb.event.ConnectionReadyEvent;
import com.mongodb.internal.connection.InternalStreamConnection;
import com.mongodb.internal.connection.ServerHelper;
import com.mongodb.internal.connection.TestCommandListener;
import com.mongodb.internal.connection.TestConnectionPoolListener;
Expand Down Expand Up @@ -908,6 +909,45 @@ public void shouldThrowTimeoutExceptionForSubsequentCommitTransaction() {
assertEquals(1, failedEvents.size());
}

/**
* Not a prose spec test. However, it is additional test case for better coverage.
* <p>
* From the spec:
* - When doing `minPoolSize` maintenance, `connectTimeoutMS` is used as the timeout for socket establishment.
*/
@Test
@DisplayName("Should use connectTimeoutMS when establishing connection in background")
public void shouldUseConnectTimeoutMsWhenEstablishingConnectionInBackground() {
assumeTrue(serverVersionAtLeast(4, 4));

collectionHelper.runAdminCommand("{"
+ " configureFailPoint: \"failCommand\","
+ " mode: \"alwaysOn\","
+ " data: {"
+ " failCommands: [\"hello\", \"isMaster\"],"
+ " blockConnection: true,"
+ " blockTimeMS: " + 500
+ " }"
+ "}");

try (MongoClient ignored = createMongoClient(getMongoClientSettingsBuilder()
.applyToConnectionPoolSettings(builder -> builder.minSize(1))
// Use a very short timeout to ensure that the connection establishment will fail on the first handshake command.
.timeout(1, TimeUnit.MILLISECONDS))) {
InternalStreamConnection.setRecordEverything(true);

// Wait for the connection to start establishment in the background.
sleep(1000);
}

List<CommandStartedEvent> commandStartedEvents = commandListener.getCommandStartedEvents("isMaster");
assertEquals(1, commandStartedEvents.size());

List<CommandFailedEvent> commandFailedEvents = commandListener.getCommandFailedEvents("isMaster");
assertEquals(1, commandFailedEvents.size());
assertInstanceOf(MongoOperationTimeoutException.class, commandFailedEvents.get(0).getThrowable());
}

private static Stream<Arguments> test8ServerSelectionArguments() {
return Stream.of(
Arguments.of(Named.of("serverSelectionTimeoutMS honored if timeoutMS is not set",
Expand Down