Search in sources :

Example 1 with ServerDescription

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

the class TaggableReadPreference method selectFreshServers.

protected List<ServerDescription> selectFreshServers(final ClusterDescription clusterDescription, final List<ServerDescription> servers) {
    if (getMaxStaleness(MILLISECONDS) == null) {
        return servers;
    }
    if (clusterDescription.getServerSettings() == null) {
        throw new MongoConfigurationException("heartbeat frequency must be provided in cluster description");
    }
    if (!serversAreAllThreeDotFour(clusterDescription)) {
        throw new MongoConfigurationException("Servers must all be at least version 3.4 when max staleness is configured");
    }
    if (clusterDescription.getType() != ClusterType.REPLICA_SET) {
        return servers;
    }
    long heartbeatFrequencyMS = clusterDescription.getServerSettings().getHeartbeatFrequency(MILLISECONDS);
    if (getMaxStaleness(MILLISECONDS) < Math.max(SMALLEST_MAX_STALENESS_MS, heartbeatFrequencyMS + IDLE_WRITE_PERIOD_MS)) {
        if (SMALLEST_MAX_STALENESS_MS > heartbeatFrequencyMS + IDLE_WRITE_PERIOD_MS) {
            throw new MongoConfigurationException(format("Max staleness (%d sec) must be at least 90 seconds", getMaxStaleness(SECONDS)));
        } else {
            throw new MongoConfigurationException(format("Max staleness (%d ms) must be at least the heartbeat period (%d ms) " + "plus the idle write period (%d ms)", getMaxStaleness(MILLISECONDS), heartbeatFrequencyMS, IDLE_WRITE_PERIOD_MS));
        }
    }
    List<ServerDescription> freshServers = new ArrayList<ServerDescription>(servers.size());
    ServerDescription primary = findPrimary(clusterDescription);
    if (primary != null) {
        for (ServerDescription cur : servers) {
            if (cur.isPrimary()) {
                freshServers.add(cur);
            } else {
                if (getStalenessOfSecondaryRelativeToPrimary(primary, cur, heartbeatFrequencyMS) <= getMaxStaleness(MILLISECONDS)) {
                    freshServers.add(cur);
                }
            }
        }
    } else {
        ServerDescription mostUpdateToDateSecondary = findMostUpToDateSecondary(clusterDescription);
        for (ServerDescription cur : servers) {
            if (mostUpdateToDateSecondary.getLastWriteDate().getTime() - cur.getLastWriteDate().getTime() + heartbeatFrequencyMS <= getMaxStaleness(MILLISECONDS)) {
                freshServers.add(cur);
            }
        }
    }
    return freshServers;
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ArrayList(java.util.ArrayList)

Example 2 with ServerDescription

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

the class ReadPreferenceChooseServersTest method setUp.

@Before
public void setUp() throws IOException {
    final TagSet tags1 = new TagSet(asList(new Tag("foo", "1"), new Tag("bar", "2"), new Tag("baz", "1")));
    final TagSet tags2 = new TagSet(asList(new Tag("foo", "1"), new Tag("bar", "2"), new Tag("baz", "2")));
    final TagSet tags3 = new TagSet(asList(new Tag("foo", "1"), new Tag("bar", "2"), new Tag("baz", "3")));
    long acceptableLatencyMS = 15;
    long bestRoundTripTime = 50;
    long acceptableRoundTripTime = bestRoundTripTime + (acceptableLatencyMS / 2);
    long unacceptableRoundTripTime = bestRoundTripTime + acceptableLatencyMS + 1;
    primary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27017)).roundTripTime(acceptableRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(ServerType.REPLICA_SET_PRIMARY).tagSet(tags1).maxDocumentSize(FOUR_MEG).build();
    secondary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27018)).roundTripTime(bestRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(ServerType.REPLICA_SET_SECONDARY).tagSet(tags2).maxDocumentSize(FOUR_MEG).build();
    otherSecondary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27019)).roundTripTime(unacceptableRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(ServerType.REPLICA_SET_SECONDARY).tagSet(tags3).maxDocumentSize(FOUR_MEG).build();
    ServerDescription uninitiatedMember = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27020)).roundTripTime(unacceptableRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(REPLICA_SET_OTHER).maxDocumentSize(FOUR_MEG).build();
    List<ServerDescription> nodeList = new ArrayList<ServerDescription>();
    nodeList.add(primary);
    nodeList.add(secondary);
    nodeList.add(otherSecondary);
    nodeList.add(uninitiatedMember);
    set = new ClusterDescription(MULTIPLE, REPLICA_SET, nodeList);
    setNoPrimary = new ClusterDescription(MULTIPLE, REPLICA_SET, asList(secondary, otherSecondary));
    setNoSecondary = new ClusterDescription(MULTIPLE, REPLICA_SET, asList(primary, uninitiatedMember));
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ArrayList(java.util.ArrayList) ClusterDescription(com.mongodb.connection.ClusterDescription) Before(org.junit.Before)

Example 3 with ServerDescription

use of com.mongodb.connection.ServerDescription 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 4 with ServerDescription

use of com.mongodb.connection.ServerDescription 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 5 with ServerDescription

use of com.mongodb.connection.ServerDescription 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

ServerDescription (com.mongodb.connection.ServerDescription)6 ClusterDescription (com.mongodb.connection.ClusterDescription)5 ServerAddress (com.mongodb.ServerAddress)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)1