Search in sources :

Example 21 with ClusterDescription

use of com.mongodb.connection.ClusterDescription 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 22 with ClusterDescription

use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.

the class BaseCluster method getDescription.

@Override
public ClusterDescription getDescription() {
    isTrue("open", !isClosed());
    try {
        CountDownLatch currentPhase = phase.get();
        ClusterDescription curDescription = description;
        boolean selectionFailureLogged = false;
        long startTimeNanos = System.nanoTime();
        long curTimeNanos = startTimeNanos;
        long maxWaitTimeNanos = getMaxWaitTimeNanos();
        while (curDescription.getType() == ClusterType.UNKNOWN) {
            if (curTimeNanos - startTimeNanos > maxWaitTimeNanos) {
                throw new MongoTimeoutException(format("Timed out after %d ms while waiting to connect. Client view of cluster state " + "is %s", settings.getServerSelectionTimeout(MILLISECONDS), curDescription.getShortDescription()));
            }
            if (!selectionFailureLogged) {
                if (LOGGER.isInfoEnabled()) {
                    if (settings.getServerSelectionTimeout(MILLISECONDS) < 0) {
                        LOGGER.info("Cluster description not yet available. Waiting indefinitely.");
                    } else {
                        LOGGER.info(format("Cluster description not yet available. Waiting for %d ms before timing out", settings.getServerSelectionTimeout(MILLISECONDS)));
                    }
                }
                selectionFailureLogged = true;
            }
            connect();
            currentPhase.await(Math.min(maxWaitTimeNanos - (curTimeNanos - startTimeNanos), getMinWaitTimeNanos()), NANOSECONDS);
            curTimeNanos = System.nanoTime();
            currentPhase = phase.get();
            curDescription = description;
        }
        return curDescription;
    } catch (InterruptedException e) {
        throw new MongoInterruptedException("Interrupted while waiting to connect", e);
    }
}
Also used : MongoInterruptedException(com.mongodb.MongoInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterDescription(com.mongodb.connection.ClusterDescription) MongoTimeoutException(com.mongodb.MongoTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 23 with ClusterDescription

use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.

the class AbstractMultiServerCluster method updateDescription.

private ClusterDescription updateDescription() {
    ClusterDescription newDescription = new ClusterDescription(MULTIPLE, clusterType, getSrvResolutionException(), getNewServerDescriptionList(), getSettings(), getServerFactory().getSettings());
    updateDescription(newDescription);
    return newDescription;
}
Also used : ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 24 with ClusterDescription

use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.

the class AbstractMultiServerCluster method onChange.

void onChange(final Collection<ServerAddress> newHosts) {
    withLock(() -> {
        if (isClosed()) {
            return;
        }
        for (ServerAddress cur : newHosts) {
            addServer(cur);
        }
        for (Iterator<ServerTuple> iterator = addressToServerTupleMap.values().iterator(); iterator.hasNext(); ) {
            ServerTuple cur = iterator.next();
            if (!newHosts.contains(cur.description.getAddress())) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(format("Removing %s from client view of cluster.", cur.description.getAddress()));
                }
                iterator.remove();
                cur.server.close();
            }
        }
        ClusterDescription oldClusterDescription = getCurrentDescription();
        ClusterDescription newClusterDescription = updateDescription();
        fireChangeEvent(newClusterDescription, oldClusterDescription);
    });
}
Also used : ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 25 with ClusterDescription

use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.

the class ReadPreferenceWithFallbackServerSelectorTest method shouldSelectCorrectServersWhenNoServersHaveBeenDiscovered.

@Test
public void shouldSelectCorrectServersWhenNoServersHaveBeenDiscovered() {
    ReadPreferenceWithFallbackServerSelector selector = new ReadPreferenceWithFallbackServerSelector(ReadPreference.secondary(), FIVE_DOT_ZERO_WIRE_VERSION, ReadPreference.primary());
    ClusterDescription clusterDescription = new ClusterDescription(ClusterConnectionMode.MULTIPLE, ClusterType.REPLICA_SET, asList(builder().ok(false).state(CONNECTING).address(new ServerAddress("localhost:27017")).build(), builder().ok(false).state(CONNECTING).address(new ServerAddress("localhost:27018")).build()));
    assertEquals(emptyList(), selector.select(clusterDescription));
    assertEquals(ReadPreference.secondary(), selector.getAppliedReadPreference());
    // when there is one connecting server, and a primary and secondary with maxWireVersion >= minWireVersion, apply read preference
    clusterDescription = new ClusterDescription(ClusterConnectionMode.MULTIPLE, ClusterType.REPLICA_SET, asList(builder().ok(false).state(CONNECTING).address(new ServerAddress("localhost:27017")).build(), builder().ok(true).state(CONNECTED).type(REPLICA_SET_PRIMARY).address(new ServerAddress("localhost:27018")).maxWireVersion(FIVE_DOT_ZERO_WIRE_VERSION).build(), builder().ok(true).state(CONNECTED).type(REPLICA_SET_SECONDARY).address(new ServerAddress("localhost:27019")).maxWireVersion(FIVE_DOT_ZERO_WIRE_VERSION).build()));
    List<ServerDescription> serverDescriptionList = selector.select(clusterDescription);
    assertEquals(clusterDescription.getServerDescriptions().stream().filter(serverDescription -> serverDescription.getType() == REPLICA_SET_SECONDARY).collect(toList()), serverDescriptionList);
    assertEquals(ReadPreference.secondary(), selector.getAppliedReadPreference());
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.jupiter.api.Test)

Aggregations

ClusterDescription (com.mongodb.connection.ClusterDescription)31 ServerAddress (com.mongodb.ServerAddress)15 ServerDescription (com.mongodb.connection.ServerDescription)12 Test (org.junit.Test)11 ServerSelector (com.mongodb.selector.ServerSelector)5 Test (org.junit.jupiter.api.Test)5 ReadPreferenceServerSelector (com.mongodb.internal.selector.ReadPreferenceServerSelector)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 BsonDocument (org.bson.BsonDocument)3 ClientSessionOptions (com.mongodb.ClientSessionOptions)2 MongoException (com.mongodb.MongoException)2 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)2 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)2 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)2 BsonArray (org.bson.BsonArray)2 MongoClientException (com.mongodb.MongoClientException)1 MongoTimeoutException (com.mongodb.MongoTimeoutException)1 ReadPreference (com.mongodb.ReadPreference)1