Search in sources :

Example 16 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 17 with ClusterDescription

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

the class ServerAddressSelectorTest method testAll.

@Test
public void testAll() throws UnknownHostException {
    ServerAddressSelector selector = new ServerAddressSelector(new ServerAddress("localhost:27018"));
    assertTrue(selector.toString().startsWith("ServerAddressSelector"));
    assertEquals(selector.getServerAddress(), selector.getServerAddress());
    ServerDescription primary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress()).ok(true).type(ServerType.REPLICA_SET_PRIMARY).build();
    ServerDescription secondary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress("localhost:27018")).ok(true).type(ServerType.REPLICA_SET_SECONDARY).build();
    assertEquals(Arrays.asList(secondary), selector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, Arrays.asList(primary, secondary))));
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 18 with ClusterDescription

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

the class CompositeServerSelector method select.

@Override
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
    ClusterDescription curClusterDescription = clusterDescription;
    List<ServerDescription> choices = null;
    for (ServerSelector cur : serverSelectors) {
        choices = cur.select(curClusterDescription);
        curClusterDescription = new ClusterDescription(clusterDescription.getConnectionMode(), clusterDescription.getType(), choices, clusterDescription.getClusterSettings(), clusterDescription.getServerSettings());
    }
    return choices;
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ClusterDescription(com.mongodb.connection.ClusterDescription)

Example 19 with ClusterDescription

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

the class LoadBalancedCluster method init.

private void init(final ClusterId clusterId, final ClusterableServerFactory serverFactory, final ServerAddress host) {
    clusterListener.clusterOpening(new ClusterOpeningEvent(clusterId));
    ClusterDescription initialDescription = new ClusterDescription(settings.getMode(), ClusterType.LOAD_BALANCED, singletonList(ServerDescription.builder().address(settings.getHosts().get(0)).state(CONNECTING).build()), settings, serverFactory.getSettings());
    clusterListener.clusterDescriptionChanged(new ClusterDescriptionChangedEvent(clusterId, initialDescription, description));
    description = new ClusterDescription(ClusterConnectionMode.LOAD_BALANCED, ClusterType.LOAD_BALANCED, singletonList(ServerDescription.builder().ok(true).state(ServerConnectionState.CONNECTED).type(ServerType.LOAD_BALANCER).address(host).build()), settings, serverFactory.getSettings());
    server = serverFactory.create(this, host);
    clusterListener.clusterDescriptionChanged(new ClusterDescriptionChangedEvent(clusterId, description, initialDescription));
}
Also used : ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) ClusterDescription(com.mongodb.connection.ClusterDescription) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent)

Example 20 with ClusterDescription

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

the class MongoClientDelegate method createClientSession.

@Nullable
public ClientSession createClientSession(final ClientSessionOptions options, final ReadConcern readConcern, final WriteConcern writeConcern, final ReadPreference readPreference) {
    notNull("readConcern", readConcern);
    notNull("writeConcern", writeConcern);
    notNull("readPreference", readPreference);
    ClusterDescription connectedClusterDescription = getConnectedClusterDescription();
    if (connectedClusterDescription.getLogicalSessionTimeoutMinutes() == null && connectedClusterDescription.getConnectionMode() != ClusterConnectionMode.LOAD_BALANCED) {
        return null;
    } else {
        ClientSessionOptions mergedOptions = ClientSessionOptions.builder(options).defaultTransactionOptions(TransactionOptions.merge(options.getDefaultTransactionOptions(), TransactionOptions.builder().readConcern(readConcern).writeConcern(writeConcern).readPreference(readPreference).build())).build();
        return new ClientSessionImpl(serverSessionPool, originator, mergedOptions, this);
    }
}
Also used : ClusterDescription(com.mongodb.connection.ClusterDescription) ClientSessionOptions(com.mongodb.ClientSessionOptions) Nullable(com.mongodb.lang.Nullable)

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