use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.
the class BaseCluster method selectServer.
@Override
public Server selectServer(final ServerSelector serverSelector) {
isTrue("open", !isClosed());
try {
CountDownLatch currentPhase = phase.get();
ClusterDescription curDescription = description;
ServerSelector compositeServerSelector = getCompositeServerSelector(serverSelector);
Server server = selectRandomServer(compositeServerSelector, curDescription);
boolean selectionFailureLogged = false;
long startTimeNanos = System.nanoTime();
long curTimeNanos = startTimeNanos;
long maxWaitTimeNanos = getMaxWaitTimeNanos();
while (true) {
throwIfIncompatible(curDescription);
if (server != null) {
return server;
}
if (curTimeNanos - startTimeNanos > maxWaitTimeNanos) {
throw createTimeoutException(serverSelector, curDescription);
}
if (!selectionFailureLogged) {
logServerSelectionFailure(serverSelector, curDescription);
selectionFailureLogged = true;
}
connect();
currentPhase.await(Math.min(maxWaitTimeNanos - (curTimeNanos - startTimeNanos), getMinWaitTimeNanos()), NANOSECONDS);
curTimeNanos = System.nanoTime();
currentPhase = phase.get();
curDescription = description;
server = selectRandomServer(compositeServerSelector, curDescription);
}
} catch (InterruptedException e) {
throw new MongoInterruptedException(format("Interrupted while waiting for a server that matches %s", serverSelector), e);
}
}
use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.
the class SingleServerClusterTest method shouldGetServerWithOkDescription.
@Test
@SuppressWarnings("deprecation")
public void shouldGetServerWithOkDescription() throws InterruptedException {
Server server = cluster.selectServer(new ServerSelector() {
@Override
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
return clusterDescription.getPrimaries();
}
});
assertTrue(server.getDescription().isOk());
}
use of com.mongodb.selector.ServerSelector in project mongo-java-driver by mongodb.
the class SingleServerClusterTest method shouldGetServerWithOkDescription.
@Test
public void shouldGetServerWithOkDescription() {
// given
setUpCluster(getPrimary());
// when
ServerTuple serverTuple = cluster.selectServer(new ServerSelector() {
@Override
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
return getPrimaries(clusterDescription);
}
});
// then
assertTrue(serverTuple.getServerDescription().isOk());
}
use of com.mongodb.selector.ServerSelector 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.selector.ServerSelector in project mongo-java-driver by mongodb.
the class ServerSelectionSelectionTest method shouldPassAllOutcomes.
@Test
public void shouldPassAllOutcomes() {
// skip this test because the driver prohibits maxStaleness or tagSets with mode of primary at a much lower level
assumeTrue(!description.equals("max-staleness/ReplicaSetWithPrimary/MaxStalenessWithModePrimary.json"));
ServerSelector serverSelector = null;
List<ServerDescription> suitableServers = buildServerDescriptions(definition.getArray("suitable_servers", new BsonArray()));
List<ServerDescription> selectedServers = null;
try {
serverSelector = getServerSelector();
selectedServers = serverSelector.select(clusterDescription);
if (error) {
fail("Should have thrown exception");
}
} catch (MongoConfigurationException e) {
if (!error) {
fail("Should not have thrown exception: " + e);
}
return;
}
assertServers(selectedServers, suitableServers);
ServerSelector latencyBasedServerSelector = new CompositeServerSelector(asList(serverSelector, new LatencyMinimizingServerSelector(15, TimeUnit.MILLISECONDS)));
List<ServerDescription> inLatencyWindowServers = buildServerDescriptions(definition.getArray("in_latency_window"));
List<ServerDescription> latencyBasedSelectedServers = latencyBasedServerSelector.select(clusterDescription);
assertServers(latencyBasedSelectedServers, inLatencyWindowServers);
}
Aggregations