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