use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.
the class MongoClientDelegate method getConnectedClusterDescription.
private ClusterDescription getConnectedClusterDescription() {
ClusterDescription clusterDescription = cluster.getDescription();
if (getServerDescriptionListToConsiderForSessionSupport(clusterDescription).isEmpty()) {
cluster.selectServer(new ServerSelector() {
@Override
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
return getServerDescriptionListToConsiderForSessionSupport(clusterDescription);
}
});
clusterDescription = cluster.getDescription();
}
return clusterDescription;
}
use of com.mongodb.selector.ServerSelector 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);
}
}
use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.
the class ServerSelectionWithinLatencyWindowTest method shouldPassAllOutcomes.
@Test
public void shouldPassAllOutcomes() {
ServerSelector selector = new ReadPreferenceServerSelector(ReadPreference.nearest());
Map<ServerAddress, List<ServerTuple>> selectionResultsGroupedByServerAddress = IntStream.range(0, iterations).mapToObj(i -> BaseCluster.selectServer(selector, clusterDescription, address -> Assertions.assertNotNull(serverCatalog.get(address)))).collect(groupingBy(serverTuple -> serverTuple.getServerDescription().getAddress()));
Map<ServerAddress, BigDecimal> selectionFrequencies = selectionResultsGroupedByServerAddress.entrySet().stream().collect(toMap(Map.Entry::getKey, entry -> BigDecimal.valueOf(entry.getValue().size()).setScale(2, RoundingMode.UNNECESSARY).divide(BigDecimal.valueOf(iterations), RoundingMode.HALF_UP)));
outcome.assertMatches(selectionFrequencies);
}
Aggregations