Search in sources :

Example 11 with ClusterDescription

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

the class LatencyMinimizingServerSelectorTest method testLatencyDifferentialMinimization.

@Test
public void testLatencyDifferentialMinimization() throws UnknownHostException {
    LatencyMinimizingServerSelector selector = new LatencyMinimizingServerSelector(20, TimeUnit.MILLISECONDS);
    ServerDescription primary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress()).ok(true).type(ServerType.REPLICA_SET_PRIMARY).roundTripTime(10, TimeUnit.MILLISECONDS).build();
    ServerDescription secondaryOne = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27018")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(15, TimeUnit.MILLISECONDS).build();
    ServerDescription secondaryTwo = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27019")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(31, TimeUnit.MILLISECONDS).build();
    ServerDescription secondaryThree = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27020")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).roundTripTime(30, TimeUnit.MILLISECONDS).build();
    assertEquals(Arrays.asList(primary, secondaryOne, secondaryThree), selector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, Arrays.asList(primary, secondaryOne, secondaryTwo, secondaryThree))));
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 12 with ClusterDescription

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

the class CompositeServerSelectorTest method shouldApplyServerSelectorsInOrder.

@Test
public void shouldApplyServerSelectorsInOrder() {
    selector = new CompositeServerSelector(asList(new ReadPreferenceServerSelector(secondary()), new LatencyMinimizingServerSelector(15, MILLISECONDS)));
    assertEquals(selector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(first, second, third))), asList(second, third));
}
Also used : LatencyMinimizingServerSelector(com.mongodb.internal.selector.LatencyMinimizingServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 13 with ClusterDescription

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

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

the class SingleServerCluster method publishDescription.

private void publishDescription(final ClusterType clusterType, final ServerDescription serverDescription) {
    ClusterDescription currentDescription = getCurrentDescription();
    ClusterDescription description = new ClusterDescription(ClusterConnectionMode.SINGLE, clusterType, serverDescription == null ? emptyList() : singletonList(serverDescription), getSettings(), getServerFactory().getSettings());
    updateDescription(description);
    fireChangeEvent(description, currentDescription);
}
Also used : ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 15 with ClusterDescription

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

the class MongoClientImplTest method testStartSession.

@Test
void testStartSession() {
    ServerDescription serverDescription = ServerDescription.builder().address(new ServerAddress()).state(ServerConnectionState.CONNECTED).maxWireVersion(8).build();
    MongoClientImpl mongoClient = createMongoClient();
    Cluster cluster = mongoClient.getCluster();
    when(cluster.getCurrentDescription()).thenReturn(new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, singletonList(serverDescription)));
    ServerSessionPool serverSessionPool = mock(ServerSessionPool.class);
    ClientSessionHelper clientSessionHelper = new ClientSessionHelper(mongoClient, serverSessionPool);
    assertAll("Start Session Tests", () -> assertAll("check validation", () -> assertThrows(IllegalArgumentException.class, () -> mongoClient.startSession(null))), () -> {
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(ClientSessionOptions.builder().build(), OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(), "Default");
    }, () -> {
        ClientSessionOptions options = ClientSessionOptions.builder().causallyConsistent(true).defaultTransactionOptions(TransactionOptions.builder().readConcern(ReadConcern.LINEARIZABLE).build()).build();
        Mono<ClientSession> expected = clientSessionHelper.createClientSessionMono(options, OPERATION_EXECUTOR);
        assertPublisherIsTheSameAs(expected, mongoClient.startSession(options), "with options");
    });
}
Also used : ServerSessionPool(com.mongodb.internal.session.ServerSessionPool) ServerDescription(com.mongodb.connection.ServerDescription) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ServerAddress(com.mongodb.ServerAddress) Cluster(com.mongodb.internal.connection.Cluster) ClusterDescription(com.mongodb.connection.ClusterDescription) ClientSessionOptions(com.mongodb.ClientSessionOptions) 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