Search in sources :

Example 1 with ReadPreferenceServerSelector

use of com.mongodb.internal.selector.ReadPreferenceServerSelector in project mongo-java-driver by mongodb.

the class ServerSessionPool method endClosedSessions.

private void endClosedSessions(final List<BsonDocument> identifiers) {
    if (identifiers.isEmpty()) {
        return;
    }
    final List<ServerDescription> primaryPreferred = new ReadPreferenceServerSelector(ReadPreference.primaryPreferred()).select(cluster.getCurrentDescription());
    if (primaryPreferred.isEmpty()) {
        return;
    }
    Connection connection = null;
    try {
        connection = cluster.selectServer(new ServerSelector() {

            @Override
            public List<ServerDescription> select(final ClusterDescription clusterDescription) {
                for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
                    if (cur.getAddress().equals(primaryPreferred.get(0).getAddress())) {
                        return Collections.singletonList(cur);
                    }
                }
                return Collections.emptyList();
            }
        }).getServer().getConnection();
        connection.command("admin", new BsonDocument("endSessions", new BsonArray(identifiers)), new NoOpFieldNameValidator(), ReadPreference.primaryPreferred(), new BsonDocumentCodec(), NoOpSessionContext.INSTANCE, serverApi, IgnorableRequestContext.INSTANCE);
    } catch (MongoException e) {
    // ignore exceptions
    } finally {
        if (connection != null) {
            connection.release();
        }
    }
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) ServerDescription(com.mongodb.connection.ServerDescription) BsonArray(org.bson.BsonArray) Connection(com.mongodb.internal.connection.Connection) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) ClusterDescription(com.mongodb.connection.ClusterDescription) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 2 with ReadPreferenceServerSelector

use of com.mongodb.internal.selector.ReadPreferenceServerSelector in project mongo-java-driver by mongodb.

the class ServerSelectionSelectionTest method getServerSelector.

private ServerSelector getServerSelector() {
    if (definition.getString("operation", new BsonString("read")).getValue().equals("write")) {
        return new WritableServerSelector();
    } else {
        BsonDocument readPreferenceDefinition = definition.getDocument("read_preference");
        ReadPreference readPreference;
        if (readPreferenceDefinition.getString("mode").getValue().equals("Primary")) {
            readPreference = ReadPreference.valueOf("Primary");
        } else if (readPreferenceDefinition.containsKey("maxStalenessSeconds")) {
            readPreference = ReadPreference.valueOf(readPreferenceDefinition.getString("mode", new BsonString("Primary")).getValue(), buildTagSets(readPreferenceDefinition.getArray("tag_sets", new BsonArray())), Math.round(readPreferenceDefinition.getNumber("maxStalenessSeconds").doubleValue() * 1000), TimeUnit.MILLISECONDS);
        } else {
            readPreference = ReadPreference.valueOf(readPreferenceDefinition.getString("mode", new BsonString("Primary")).getValue(), buildTagSets(readPreferenceDefinition.getArray("tag_sets", new BsonArray())));
        }
        return new ReadPreferenceServerSelector(readPreference);
    }
}
Also used : ReadPreference(com.mongodb.ReadPreference) WritableServerSelector(com.mongodb.internal.selector.WritableServerSelector) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) BsonArray(org.bson.BsonArray) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector)

Example 3 with ReadPreferenceServerSelector

use of com.mongodb.internal.selector.ReadPreferenceServerSelector 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 4 with ReadPreferenceServerSelector

use of com.mongodb.internal.selector.ReadPreferenceServerSelector in project mongo-java-driver by mongodb.

the class ServerSelectionWithinLatencyWindowTest method shouldPassAllOutcomes.

@Test
public void shouldPassAllOutcomes() {
    ServerSelector selector = new ReadPreferenceServerSelector(ReadPreference.nearest());
    Map<ServerAddress, List<ServerTuple>> selectionResultsGroupedByServerAddress = IntStream.range(0, iterations).mapToObj(i -> BaseCluster.selectServer(selector, clusterDescription, address -> Assertions.assertNotNull(serverCatalog.get(address)))).collect(groupingBy(serverTuple -> serverTuple.getServerDescription().getAddress()));
    Map<ServerAddress, BigDecimal> selectionFrequencies = selectionResultsGroupedByServerAddress.entrySet().stream().collect(toMap(Map.Entry::getKey, entry -> BigDecimal.valueOf(entry.getValue().size()).setScale(2, RoundingMode.UNNECESSARY).divide(BigDecimal.valueOf(iterations), RoundingMode.HALF_UP)));
    outcome.assertMatches(selectionFrequencies);
}
Also used : ReadPreference(com.mongodb.ReadPreference) IntStream(java.util.stream.IntStream) RunWith(org.junit.runner.RunWith) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) JsonPoweredTestHelper.testDir(util.JsonPoweredTestHelper.testDir) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) BsonNumber(org.bson.BsonNumber) BigDecimal(java.math.BigDecimal) Collectors.toMap(java.util.stream.Collectors.toMap) Assertions(com.mongodb.assertions.Assertions) ServerSelector(com.mongodb.selector.ServerSelector) BsonArray(org.bson.BsonArray) Map(java.util.Map) ClusterDescription(com.mongodb.connection.ClusterDescription) Path(java.nio.file.Path) Parameterized(org.junit.runners.Parameterized) RoundingMode(java.math.RoundingMode) JsonPoweredTestHelper.testDocs(util.JsonPoweredTestHelper.testDocs) ServerAddress(com.mongodb.ServerAddress) Collection(java.util.Collection) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ServerSelectionSelectionTest.buildClusterDescription(com.mongodb.connection.ServerSelectionSelectionTest.buildClusterDescription) Mockito(org.mockito.Mockito) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) Assert.assertEquals(org.junit.Assert.assertEquals) ServerSelector(com.mongodb.selector.ServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) ServerAddress(com.mongodb.ServerAddress) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

ReadPreferenceServerSelector (com.mongodb.internal.selector.ReadPreferenceServerSelector)4 ClusterDescription (com.mongodb.connection.ClusterDescription)3 BsonArray (org.bson.BsonArray)3 BsonDocument (org.bson.BsonDocument)3 ReadPreference (com.mongodb.ReadPreference)2 ServerSelector (com.mongodb.selector.ServerSelector)2 Test (org.junit.Test)2 MongoException (com.mongodb.MongoException)1 ServerAddress (com.mongodb.ServerAddress)1 Assertions (com.mongodb.assertions.Assertions)1 ServerDescription (com.mongodb.connection.ServerDescription)1 ServerSelectionSelectionTest.buildClusterDescription (com.mongodb.connection.ServerSelectionSelectionTest.buildClusterDescription)1 Connection (com.mongodb.internal.connection.Connection)1 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)1 WritableServerSelector (com.mongodb.internal.selector.WritableServerSelector)1 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)1 BigDecimal (java.math.BigDecimal)1 RoundingMode (java.math.RoundingMode)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1