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();
}
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);
}
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());
}
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))));
}
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))));
}
Aggregations