use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class BaseCluster method selectServer.
@Override
public ServerTuple selectServer(final ServerSelector serverSelector) {
isTrue("open", !isClosed());
try {
CountDownLatch currentPhase = phase.get();
ClusterDescription curDescription = description;
ServerSelector compositeServerSelector = getCompositeServerSelector(serverSelector);
ServerTuple serverTuple = selectServer(compositeServerSelector, curDescription);
boolean selectionFailureLogged = false;
long startTimeNanos = System.nanoTime();
long curTimeNanos = startTimeNanos;
long maxWaitTimeNanos = getMaxWaitTimeNanos();
while (true) {
throwIfIncompatible(curDescription);
if (serverTuple != null) {
return serverTuple;
}
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;
serverTuple = selectServer(compositeServerSelector, curDescription);
}
} catch (InterruptedException e) {
throw new MongoInterruptedException(format("Interrupted while waiting for a server that matches %s", serverSelector), e);
}
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class BaseCluster method getDescription.
@Override
public ClusterDescription getDescription() {
isTrue("open", !isClosed());
try {
CountDownLatch currentPhase = phase.get();
ClusterDescription curDescription = description;
boolean selectionFailureLogged = false;
long startTimeNanos = System.nanoTime();
long curTimeNanos = startTimeNanos;
long maxWaitTimeNanos = getMaxWaitTimeNanos();
while (curDescription.getType() == ClusterType.UNKNOWN) {
if (curTimeNanos - startTimeNanos > maxWaitTimeNanos) {
throw new MongoTimeoutException(format("Timed out after %d ms while waiting to connect. Client view of cluster state " + "is %s", settings.getServerSelectionTimeout(MILLISECONDS), curDescription.getShortDescription()));
}
if (!selectionFailureLogged) {
if (LOGGER.isInfoEnabled()) {
if (settings.getServerSelectionTimeout(MILLISECONDS) < 0) {
LOGGER.info("Cluster description not yet available. Waiting indefinitely.");
} else {
LOGGER.info(format("Cluster description not yet available. Waiting for %d ms before timing out", settings.getServerSelectionTimeout(MILLISECONDS)));
}
}
selectionFailureLogged = true;
}
connect();
currentPhase.await(Math.min(maxWaitTimeNanos - (curTimeNanos - startTimeNanos), getMinWaitTimeNanos()), NANOSECONDS);
curTimeNanos = System.nanoTime();
currentPhase = phase.get();
curDescription = description;
}
return curDescription;
} catch (InterruptedException e) {
throw new MongoInterruptedException("Interrupted while waiting to connect", e);
}
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class AbstractMultiServerCluster method updateDescription.
private ClusterDescription updateDescription() {
ClusterDescription newDescription = new ClusterDescription(MULTIPLE, clusterType, getSrvResolutionException(), getNewServerDescriptionList(), getSettings(), getServerFactory().getSettings());
updateDescription(newDescription);
return newDescription;
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class AbstractMultiServerCluster method onChange.
void onChange(final Collection<ServerAddress> newHosts) {
withLock(() -> {
if (isClosed()) {
return;
}
for (ServerAddress cur : newHosts) {
addServer(cur);
}
for (Iterator<ServerTuple> iterator = addressToServerTupleMap.values().iterator(); iterator.hasNext(); ) {
ServerTuple cur = iterator.next();
if (!newHosts.contains(cur.description.getAddress())) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info(format("Removing %s from client view of cluster.", cur.description.getAddress()));
}
iterator.remove();
cur.server.close();
}
}
ClusterDescription oldClusterDescription = getCurrentDescription();
ClusterDescription newClusterDescription = updateDescription();
fireChangeEvent(newClusterDescription, oldClusterDescription);
});
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class ReadPreferenceWithFallbackServerSelectorTest method shouldSelectCorrectServersWhenNoServersHaveBeenDiscovered.
@Test
public void shouldSelectCorrectServersWhenNoServersHaveBeenDiscovered() {
ReadPreferenceWithFallbackServerSelector selector = new ReadPreferenceWithFallbackServerSelector(ReadPreference.secondary(), FIVE_DOT_ZERO_WIRE_VERSION, ReadPreference.primary());
ClusterDescription clusterDescription = new ClusterDescription(ClusterConnectionMode.MULTIPLE, ClusterType.REPLICA_SET, asList(builder().ok(false).state(CONNECTING).address(new ServerAddress("localhost:27017")).build(), builder().ok(false).state(CONNECTING).address(new ServerAddress("localhost:27018")).build()));
assertEquals(emptyList(), selector.select(clusterDescription));
assertEquals(ReadPreference.secondary(), selector.getAppliedReadPreference());
// when there is one connecting server, and a primary and secondary with maxWireVersion >= minWireVersion, apply read preference
clusterDescription = new ClusterDescription(ClusterConnectionMode.MULTIPLE, ClusterType.REPLICA_SET, asList(builder().ok(false).state(CONNECTING).address(new ServerAddress("localhost:27017")).build(), builder().ok(true).state(CONNECTED).type(REPLICA_SET_PRIMARY).address(new ServerAddress("localhost:27018")).maxWireVersion(FIVE_DOT_ZERO_WIRE_VERSION).build(), builder().ok(true).state(CONNECTED).type(REPLICA_SET_SECONDARY).address(new ServerAddress("localhost:27019")).maxWireVersion(FIVE_DOT_ZERO_WIRE_VERSION).build()));
List<ServerDescription> serverDescriptionList = selector.select(clusterDescription);
assertEquals(clusterDescription.getServerDescriptions().stream().filter(serverDescription -> serverDescription.getType() == REPLICA_SET_SECONDARY).collect(toList()), serverDescriptionList);
assertEquals(ReadPreference.secondary(), selector.getAppliedReadPreference());
}
Aggregations