Skip to content

Apply timeout to server selection #1226

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

Merged
merged 2 commits into from
Oct 30, 2023
Merged
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
@@ -156,6 +156,7 @@ private static Timeout calculateTimeout(@Nullable final Long timeoutMS) {

public Timeout startServerSelectionTimeout() {
long ms = getTimeoutSettings().getServerSelectionTimeoutMS();
return StartTime.now().timeoutAfterOrInfiniteIfNegative(ms, MILLISECONDS);
Timeout serverSelectionTimeout = StartTime.now().timeoutAfterOrInfiniteIfNegative(ms, MILLISECONDS);
return serverSelectionTimeout.orEarlier(timeout);
}
}
Original file line number Diff line number Diff line change
@@ -105,26 +105,26 @@ public ServerTuple selectServer(final ServerSelector serverSelector, final Opera

ServerSelector compositeServerSelector = getCompositeServerSelector(serverSelector);
boolean selectionFailureLogged = false;
Timeout timeout = operationContext.getTimeoutContext().startServerSelectionTimeout();
Timeout serverSelectionTimeout = operationContext.getTimeoutContext().startServerSelectionTimeout();

while (true) {
CountDownLatch currentPhaseLatch = phase.get();
ClusterDescription currentDescription = description;
ServerTuple serverTuple = selectServer(compositeServerSelector, currentDescription, timeout);
ServerTuple serverTuple = selectServer(compositeServerSelector, currentDescription, serverSelectionTimeout);

throwIfIncompatible(currentDescription);
if (serverTuple != null) {
return serverTuple;
}
if (timeout.hasExpired()) {
if (serverSelectionTimeout.hasExpired()) {
throw createTimeoutException(serverSelector, currentDescription);
}
if (!selectionFailureLogged) {
logServerSelectionFailure(serverSelector, currentDescription, timeout);
logServerSelectionFailure(serverSelector, currentDescription, serverSelectionTimeout);
selectionFailureLogged = true;
}
connect();
Timeout heartbeatLimitedTimeout = timeout.orEarlier(startMinWaitHeartbeatTimeout());
Timeout heartbeatLimitedTimeout = serverSelectionTimeout.orEarlier(startMinWaitHeartbeatTimeout());
heartbeatLimitedTimeout.awaitOn(currentPhaseLatch,
() -> format("waiting for a server that matches %s", serverSelector));
}
4 changes: 4 additions & 0 deletions driver-core/src/main/com/mongodb/internal/time/TimePoint.java
Original file line number Diff line number Diff line change
@@ -219,8 +219,12 @@ public int hashCode() {

@Override
public String toString() {
long remainingMs = nanos == null
? -1
: TimeUnit.MILLISECONDS.convert(currentNanos() - nanos, NANOSECONDS);
return "TimePoint{"
+ "nanos=" + nanos
+ "remainingMs=" + remainingMs
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ static Timeout infinite() {
* @return a timeout that expires in the specified duration after now.
*/
static Timeout expiresIn(final long duration, final TimeUnit unit) {
// TODO (CSOT) confirm that all usages in final PR are non-negative
// TODO (CSOT) confirm that all usages in final PR always supply a non-negative duration
if (duration < 0) {
throw new AssertionError("Timeouts must not be in the past");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.internal;

import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.syncadapter.SyncMongoClient;

public class ClientSideOperationsTimeoutAsyncProseTests extends ClientSideOperationsTimeoutProseTests {

@Override
protected MongoClient createMongoClient(final MongoClientSettings settings) {
return new SyncMongoClient(MongoClients.create(settings));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.internal;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoTimeoutException;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* See
* <a href="https://github.com/mongodb/specifications/blob/master/source/client-side-operations-timeout/tests/README.rst">Prose Tests</a>.
*/
public class ClientSideOperationsTimeoutProseTests {

protected MongoClient createMongoClient(final MongoClientSettings settings) {
return MongoClients.create(settings);
}

private long msElapsedSince(final long t1) {
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t1);
}

@NotNull
private MongoClientSettings createMongoClientSettings(final String connectionString) {
// All MongoClient instances created for tests MUST be configured
// with read/write concern majority, read preference primary, and
// TODO (CSOT): command monitoring enabled to listen for command_started events.
ConnectionString cs = new ConnectionString(connectionString);
MongoClientSettings.Builder builder = MongoClientSettings.builder()
.readConcern(ReadConcern.MAJORITY)
.writeConcern(WriteConcern.MAJORITY)
.readPreference(ReadPreference.primary())
.applyConnectionString(cs);
return builder.build();
}

@ParameterizedTest
@ValueSource(strings = {
"mongodb://invalid/?serverSelectionTimeoutMS=10",
"mongodb://invalid/?timeoutMS=10&serverSelectionTimeoutMS=200",
"mongodb://invalid/?timeoutMS=200&serverSelectionTimeoutMS=10",
"mongodb://invalid/?timeoutMS=0&serverSelectionTimeoutMS=10",
})
public void test8ServerSelectionPart1(final String connectionString) {
int timeoutBuffer = 100; // 5 in spec, Java is slower
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is increased, and the MS=200 (20 in spec). I used 20 for timeoutBuffer initially, but in the async test, this needs at least ~80.

// 1. Create a MongoClient
try (MongoClient mongoClient = createMongoClient(createMongoClientSettings(connectionString))) {
long start = System.nanoTime();
// 2. Using client, execute:
Throwable throwable = assertThrows(MongoTimeoutException.class, () -> {
mongoClient.getDatabase("admin").runCommand(new BsonDocument("ping", new BsonInt32(1)));
});
// Expect this to fail with a server selection timeout error after no more than 15ms [this is increased]
long elapsed = msElapsedSince(start);
assertTrue(throwable.getMessage().contains("while waiting for a server"));
assertTrue(elapsed < 10 + timeoutBuffer, "Took too long to time out, elapsedMS: " + elapsed);
}
}
}