use of com.mongodb.selector.CompositeServerSelector in project mongo-java-driver by mongodb.
the class BaseCluster method selectServer.
@Override
public Server selectServer(final ServerSelector serverSelector) {
isTrue("open", !isClosed());
try {
CountDownLatch currentPhase = phase.get();
ClusterDescription curDescription = description;
ServerSelector compositeServerSelector = getCompositeServerSelector(serverSelector);
Server server = selectRandomServer(compositeServerSelector, curDescription);
boolean selectionFailureLogged = false;
long startTimeNanos = System.nanoTime();
long curTimeNanos = startTimeNanos;
long maxWaitTimeNanos = getMaxWaitTimeNanos();
while (true) {
throwIfIncompatible(curDescription);
if (server != null) {
return server;
}
if (curTimeNanos - startTimeNanos > maxWaitTimeNanos) {
throw createTimeoutException(serverSelector, curDescription);
}
if (!selectionFailureLogged) {
logServerSelectionFailure(serverSelector, curDescription);
selectionFailureLogged = true;
}
connect();
currentPhase.await(Math.min(maxWaitTimeNanos - (curTimeNanos - startTimeNanos), getMinWaitTimeNanos()), NANOSECONDS);
curTimeNanos = System.nanoTime();
currentPhase = phase.get();
curDescription = description;
server = selectRandomServer(compositeServerSelector, curDescription);
}
} catch (InterruptedException e) {
throw new MongoInterruptedException(format("Interrupted while waiting for a server that matches %s", serverSelector), e);
}
}
use of com.mongodb.selector.CompositeServerSelector in project mongo-java-driver by mongodb.
the class ServerSelectionSelectionTest method shouldPassAllOutcomes.
@Test
public void shouldPassAllOutcomes() {
// skip this test because the driver prohibits maxStaleness or tagSets with mode of primary at a much lower level
assumeTrue(!description.equals("max-staleness/ReplicaSetWithPrimary/MaxStalenessWithModePrimary.json"));
ServerSelector serverSelector = null;
List<ServerDescription> suitableServers = buildServerDescriptions(definition.getArray("suitable_servers", new BsonArray()));
List<ServerDescription> selectedServers = null;
try {
serverSelector = getServerSelector();
selectedServers = serverSelector.select(clusterDescription);
if (error) {
fail("Should have thrown exception");
}
} catch (MongoConfigurationException e) {
if (!error) {
fail("Should not have thrown exception: " + e);
}
return;
}
assertServers(selectedServers, suitableServers);
ServerSelector latencyBasedServerSelector = new CompositeServerSelector(asList(serverSelector, new LatencyMinimizingServerSelector(15, TimeUnit.MILLISECONDS)));
List<ServerDescription> inLatencyWindowServers = buildServerDescriptions(definition.getArray("in_latency_window"));
List<ServerDescription> latencyBasedSelectedServers = latencyBasedServerSelector.select(clusterDescription);
assertServers(latencyBasedSelectedServers, inLatencyWindowServers);
}
use of com.mongodb.selector.CompositeServerSelector in project mongo-java-driver by mongodb.
the class BaseCluster method selectServer.
@Override
public ServerTuple selectServer(final ServerSelector serverSelector) {
isTrue("open", !isClosed());
try {
CountDownLatch currentPhase = phase.get();
ClusterDescription curDescription = description;
ServerSelector compositeServerSelector = getCompositeServerSelector(serverSelector);
ServerTuple serverTuple = selectServer(compositeServerSelector, curDescription);
boolean selectionFailureLogged = false;
long startTimeNanos = System.nanoTime();
long curTimeNanos = startTimeNanos;
long maxWaitTimeNanos = getMaxWaitTimeNanos();
while (true) {
throwIfIncompatible(curDescription);
if (serverTuple != null) {
return serverTuple;
}
if (curTimeNanos - startTimeNanos > maxWaitTimeNanos) {
throw createTimeoutException(serverSelector, curDescription);
}
if (!selectionFailureLogged) {
logServerSelectionFailure(serverSelector, curDescription);
selectionFailureLogged = true;
}
connect();
currentPhase.await(Math.min(maxWaitTimeNanos - (curTimeNanos - startTimeNanos), getMinWaitTimeNanos()), NANOSECONDS);
curTimeNanos = System.nanoTime();
currentPhase = phase.get();
curDescription = description;
serverTuple = selectServer(compositeServerSelector, curDescription);
}
} catch (InterruptedException e) {
throw new MongoInterruptedException(format("Interrupted while waiting for a server that matches %s", serverSelector), e);
}
}
Aggregations