Search in sources :

Example 1 with ClusterDescription

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

the class ClusterFixture method getConnectedServerVersion.

@SuppressWarnings("deprecation")
public static ServerVersion getConnectedServerVersion() {
    ClusterDescription clusterDescription = getCluster().getDescription();
    int retries = 0;
    while (clusterDescription.getAny().isEmpty() && retries <= 3) {
        try {
            Thread.sleep(1000);
            retries++;
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted", e);
        }
        clusterDescription = getCluster().getDescription();
    }
    if (clusterDescription.getAny().isEmpty()) {
        throw new RuntimeException("There are no servers available in " + clusterDescription);
    }
    return clusterDescription.getAny().get(0).getVersion();
}
Also used : ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 2 with ClusterDescription

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

the class LatencyMinimizingServerSelectorTest method testZeroLatencyDifferentialTolerance.

@Test
public void testZeroLatencyDifferentialTolerance() throws UnknownHostException {
    LatencyMinimizingServerSelector selector = new LatencyMinimizingServerSelector(0, TimeUnit.NANOSECONDS);
    ServerDescription primary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress()).ok(true).type(ServerType.REPLICA_SET_PRIMARY).roundTripTime(10, TimeUnit.NANOSECONDS).build();
    ServerDescription secondaryOne = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27018")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(11, TimeUnit.NANOSECONDS).build();
    assertEquals(Arrays.asList(primary), selector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, Arrays.asList(primary, secondaryOne))));
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 3 with ClusterDescription

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

the class BaseCluster method selectServerAsync.

@Override
public void selectServerAsync(final ServerSelector serverSelector, final SingleResultCallback<ServerTuple> callback) {
    isTrue("open", !isClosed());
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("Asynchronously selecting server with selector %s", serverSelector));
    }
    ServerSelectionRequest request = new ServerSelectionRequest(serverSelector, getCompositeServerSelector(serverSelector), getMaxWaitTimeNanos(), callback);
    CountDownLatch currentPhase = phase.get();
    ClusterDescription currentDescription = description;
    if (!handleServerSelectionRequest(request, currentPhase, currentDescription)) {
        notifyWaitQueueHandler(request);
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 4 with ClusterDescription

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

the class AbstractMultiServerCluster method onChange.

@Override
public void onChange(final ServerDescriptionChangedEvent event) {
    withLock(() -> {
        if (isClosed()) {
            return;
        }
        ServerDescription newDescription = event.getNewDescription();
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace(format("Handling description changed event for server %s with description %s", newDescription.getAddress(), newDescription));
        }
        ServerTuple serverTuple = addressToServerTupleMap.get(newDescription.getAddress());
        if (serverTuple == null) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace(format("Ignoring description changed event for removed server %s", newDescription.getAddress()));
            }
            return;
        }
        boolean shouldUpdateDescription = true;
        if (newDescription.isOk()) {
            if (clusterType == UNKNOWN && newDescription.getType() != REPLICA_SET_GHOST) {
                clusterType = newDescription.getClusterType();
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(format("Discovered cluster type of %s", clusterType));
                }
            }
            switch(clusterType) {
                case REPLICA_SET:
                    shouldUpdateDescription = handleReplicaSetMemberChanged(newDescription);
                    break;
                case SHARDED:
                    shouldUpdateDescription = handleShardRouterChanged(newDescription);
                    break;
                case STANDALONE:
                    shouldUpdateDescription = handleStandAloneChanged(newDescription);
                    break;
                default:
                    break;
            }
        }
        ClusterDescription oldClusterDescription = null;
        ClusterDescription newClusterDescription = null;
        if (shouldUpdateDescription) {
            serverTuple.description = newDescription;
            oldClusterDescription = getCurrentDescription();
            newClusterDescription = updateDescription();
        }
        if (shouldUpdateDescription) {
            fireChangeEvent(newClusterDescription, oldClusterDescription);
        }
    });
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 5 with ClusterDescription

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

the class AbstractMultiServerCluster method initialize.

protected void initialize(final Collection<ServerAddress> serverAddresses) {
    ClusterDescription currentDescription = getCurrentDescription();
    // synchronizing this code because addServer registers a callback which is re-entrant to this instance.
    // In other words, we are leaking a reference to "this" from the constructor.
    withLock(() -> {
        for (final ServerAddress serverAddress : serverAddresses) {
            addServer(serverAddress);
        }
        ClusterDescription newDescription = updateDescription();
        fireChangeEvent(newDescription, currentDescription);
    });
}
Also used : ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription)

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