Search in sources :

Example 1 with CompositeServerSelector

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

Example 2 with CompositeServerSelector

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);
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) CompositeServerSelector(com.mongodb.selector.CompositeServerSelector) WritableServerSelector(com.mongodb.internal.selector.WritableServerSelector) LatencyMinimizingServerSelector(com.mongodb.internal.selector.LatencyMinimizingServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) MongoConfigurationException(com.mongodb.MongoConfigurationException) BsonArray(org.bson.BsonArray) LatencyMinimizingServerSelector(com.mongodb.internal.selector.LatencyMinimizingServerSelector) CompositeServerSelector(com.mongodb.selector.CompositeServerSelector) Test(org.junit.Test)

Example 3 with CompositeServerSelector

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);
    }
}
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)

Aggregations

CompositeServerSelector (com.mongodb.selector.CompositeServerSelector)3 ServerSelector (com.mongodb.selector.ServerSelector)3 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 MongoConfigurationException (com.mongodb.MongoConfigurationException)1 ClusterDescription (com.mongodb.connection.ClusterDescription)1 ReadPreferenceServerSelector (com.mongodb.internal.selector.ReadPreferenceServerSelector)1 WritableServerSelector (com.mongodb.internal.selector.WritableServerSelector)1 BsonArray (org.bson.BsonArray)1 Test (org.junit.Test)1