Search in sources :

Example 41 with ServerAddress

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

the class ServerSelectionSelectionTest method buildServerDescription.

private ServerDescription buildServerDescription(final BsonDocument serverDescription) {
    ServerDescription.Builder builder = ServerDescription.builder();
    builder.address(new ServerAddress(serverDescription.getString("address").getValue()));
    ServerType serverType = getServerType(serverDescription.getString("type").getValue());
    builder.ok(serverType != ServerType.UNKNOWN);
    builder.type(serverType);
    if (serverDescription.containsKey("tags")) {
        builder.tagSet(buildTagSet(serverDescription.getDocument("tags")));
    }
    if (serverDescription.containsKey("avg_rtt_ms")) {
        builder.roundTripTime(serverDescription.getNumber("avg_rtt_ms").asInt32().getValue(), TimeUnit.MILLISECONDS);
    }
    builder.state(ServerConnectionState.CONNECTED);
    if (serverDescription.containsKey("lastWrite")) {
        builder.lastWriteDate(new Date(serverDescription.getDocument("lastWrite").getNumber("lastWriteDate").longValue()));
    }
    if (serverDescription.containsKey("lastUpdateTime")) {
        // convert to nanos
        builder.lastUpdateTimeNanos(serverDescription.getNumber("lastUpdateTime").longValue() * 1000000);
    } else {
        builder.lastUpdateTimeNanos(42L);
    }
    if (serverDescription.containsKey("maxWireVersion")) {
        builder.maxWireVersion(serverDescription.getNumber("maxWireVersion").intValue());
    }
    return builder.build();
}
Also used : ServerAddress(com.mongodb.ServerAddress) Date(java.util.Date)

Example 42 with ServerAddress

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

the class TestClusterableServerFactory method getBuilder.

private ServerDescription.Builder getBuilder(final ServerAddress serverAddress, final ServerType serverType, final List<ServerAddress> hosts, final List<ServerAddress> passives, final boolean ok, final String setName, final ObjectId electionId, final ServerAddress trueAddress) {
    Set<String> hostsSet = new HashSet<String>();
    for (ServerAddress cur : hosts) {
        hostsSet.add(cur.toString());
    }
    Set<String> passivesSet = new HashSet<String>();
    for (ServerAddress cur : passives) {
        passivesSet.add(cur.toString());
    }
    return ServerDescription.builder().address(serverAddress).type(serverType).ok(ok).state(CONNECTED).canonicalAddress(trueAddress == null ? serverAddress.toString() : trueAddress.toString()).hosts(hostsSet).passives(passivesSet).setName(setName).electionId(electionId).setVersion(1);
}
Also used : ServerAddress(com.mongodb.ServerAddress) HashSet(java.util.HashSet)

Example 43 with ServerAddress

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

the class CompositeServerSelectorTest method shouldPassOnClusterDescriptionWithCorrectServersAndSettings.

@Test
public void shouldPassOnClusterDescriptionWithCorrectServersAndSettings() {
    TestServerSelector firstSelector = new TestServerSelector();
    TestServerSelector secondSelector = new TestServerSelector();
    CompositeServerSelector composedSelector = new CompositeServerSelector(asList(firstSelector, secondSelector));
    composedSelector.select(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(first, second, third), ClusterSettings.builder().hosts(asList(new ServerAddress())).build(), ServerSettings.builder().build()));
    assertTrue(secondSelector.clusterDescription.getServerDescriptions().isEmpty());
    assertNotNull(secondSelector.clusterDescription.getClusterSettings());
    assertNotNull(secondSelector.clusterDescription.getServerSettings());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 44 with ServerAddress

use of com.mongodb.ServerAddress 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 45 with ServerAddress

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

Aggregations

ServerAddress (com.mongodb.ServerAddress)58 Test (org.junit.Test)22 MongoClient (com.mongodb.MongoClient)13 MongoCredential (com.mongodb.MongoCredential)9 ArrayList (java.util.ArrayList)9 Before (org.junit.Before)9 TagSet (com.mongodb.TagSet)4 ClusterDescription (com.mongodb.connection.ClusterDescription)4 ClusterSettings (com.mongodb.connection.ClusterSettings)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 Tag (com.mongodb.Tag)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 ServerDescription (com.mongodb.connection.ServerDescription)3 UnknownHostException (java.net.UnknownHostException)3 Date (java.util.Date)3 ChunkInfo (org.apache.drill.exec.store.mongo.common.ChunkInfo)3 BsonDocument (org.bson.BsonDocument)3 DataXException (com.alibaba.datax.common.exception.DataXException)2 ConnectionString (com.mongodb.ConnectionString)2 MongoClientURI (com.mongodb.MongoClientURI)2