Search in sources :

Example 1 with ServerSelector

use of com.mongodb.selector.ServerSelector 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 ServerSelector

use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.

the class SingleServerClusterTest method shouldGetServerWithOkDescription.

@Test
@SuppressWarnings("deprecation")
public void shouldGetServerWithOkDescription() throws InterruptedException {
    Server server = cluster.selectServer(new ServerSelector() {

        @Override
        public List<ServerDescription> select(final ClusterDescription clusterDescription) {
            return clusterDescription.getPrimaries();
        }
    });
    assertTrue(server.getDescription().isOk());
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) List(java.util.List) ClusterFixture.getCredentialList(com.mongodb.ClusterFixture.getCredentialList) Test(org.junit.Test)

Example 3 with ServerSelector

use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.

the class SingleServerClusterTest method shouldGetServerWithOkDescription.

@Test
public void shouldGetServerWithOkDescription() {
    // given
    setUpCluster(getPrimary());
    // when
    ServerTuple serverTuple = cluster.selectServer(new ServerSelector() {

        @Override
        public List<ServerDescription> select(final ClusterDescription clusterDescription) {
            return getPrimaries(clusterDescription);
        }
    });
    // then
    assertTrue(serverTuple.getServerDescription().isOk());
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 4 with ServerSelector

use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.

the class ServerSessionPool method endClosedSessions.

private void endClosedSessions(final List<BsonDocument> identifiers) {
    if (identifiers.isEmpty()) {
        return;
    }
    final List<ServerDescription> primaryPreferred = new ReadPreferenceServerSelector(ReadPreference.primaryPreferred()).select(cluster.getCurrentDescription());
    if (primaryPreferred.isEmpty()) {
        return;
    }
    Connection connection = null;
    try {
        connection = cluster.selectServer(new ServerSelector() {

            @Override
            public List<ServerDescription> select(final ClusterDescription clusterDescription) {
                for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
                    if (cur.getAddress().equals(primaryPreferred.get(0).getAddress())) {
                        return Collections.singletonList(cur);
                    }
                }
                return Collections.emptyList();
            }
        }).getServer().getConnection();
        connection.command("admin", new BsonDocument("endSessions", new BsonArray(identifiers)), new NoOpFieldNameValidator(), ReadPreference.primaryPreferred(), new BsonDocumentCodec(), NoOpSessionContext.INSTANCE, serverApi, IgnorableRequestContext.INSTANCE);
    } catch (MongoException e) {
    // ignore exceptions
    } finally {
        if (connection != null) {
            connection.release();
        }
    }
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) ServerDescription(com.mongodb.connection.ServerDescription) BsonArray(org.bson.BsonArray) Connection(com.mongodb.internal.connection.Connection) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) ClusterDescription(com.mongodb.connection.ClusterDescription) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 5 with ServerSelector

use of com.mongodb.selector.ServerSelector 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)

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