Search in sources :

Example 6 with ServerSelector

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;
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) List(java.util.List) ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 7 with ServerSelector

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);
    }
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) CompositeServerSelector(com.mongodb.selector.CompositeServerSelector) LatencyMinimizingServerSelector(com.mongodb.internal.selector.LatencyMinimizingServerSelector) MongoInterruptedException(com.mongodb.MongoInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterDescription(com.mongodb.connection.ClusterDescription) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 8 with ServerSelector

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);
}
Also used : ReadPreference(com.mongodb.ReadPreference) IntStream(java.util.stream.IntStream) RunWith(org.junit.runner.RunWith) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) JsonPoweredTestHelper.testDir(util.JsonPoweredTestHelper.testDir) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) BsonNumber(org.bson.BsonNumber) BigDecimal(java.math.BigDecimal) Collectors.toMap(java.util.stream.Collectors.toMap) Assertions(com.mongodb.assertions.Assertions) ServerSelector(com.mongodb.selector.ServerSelector) BsonArray(org.bson.BsonArray) Map(java.util.Map) ClusterDescription(com.mongodb.connection.ClusterDescription) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) RoundingMode(java.math.RoundingMode) JsonPoweredTestHelper.testDocs(util.JsonPoweredTestHelper.testDocs) ServerAddress(com.mongodb.ServerAddress) Collection(java.util.Collection) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ServerSelectionSelectionTest.buildClusterDescription(com.mongodb.connection.ServerSelectionSelectionTest.buildClusterDescription) Mockito(org.mockito.Mockito) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) Assert.assertEquals(org.junit.Assert.assertEquals) ServerSelector(com.mongodb.selector.ServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) ServerAddress(com.mongodb.ServerAddress) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

ServerSelector (com.mongodb.selector.ServerSelector)8 ClusterDescription (com.mongodb.connection.ClusterDescription)5 List (java.util.List)4 Test (org.junit.Test)4 ReadPreferenceServerSelector (com.mongodb.internal.selector.ReadPreferenceServerSelector)3 CompositeServerSelector (com.mongodb.selector.CompositeServerSelector)3 BsonArray (org.bson.BsonArray)3 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 BsonDocument (org.bson.BsonDocument)2 ClusterFixture.getCredentialList (com.mongodb.ClusterFixture.getCredentialList)1 MongoConfigurationException (com.mongodb.MongoConfigurationException)1 MongoException (com.mongodb.MongoException)1 ReadPreference (com.mongodb.ReadPreference)1 ServerAddress (com.mongodb.ServerAddress)1 Assertions (com.mongodb.assertions.Assertions)1 ServerDescription (com.mongodb.connection.ServerDescription)1 ServerSelectionSelectionTest.buildClusterDescription (com.mongodb.connection.ServerSelectionSelectionTest.buildClusterDescription)1 Connection (com.mongodb.internal.connection.Connection)1