use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class ClusterFixture method getConnectedServerVersion.
@SuppressWarnings("deprecation")
public static ServerVersion getConnectedServerVersion() {
ClusterDescription clusterDescription = getCluster().getDescription();
int retries = 0;
while (clusterDescription.getAny().isEmpty() && retries <= 3) {
try {
Thread.sleep(1000);
retries++;
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
clusterDescription = getCluster().getDescription();
}
if (clusterDescription.getAny().isEmpty()) {
throw new RuntimeException("There are no servers available in " + clusterDescription);
}
return clusterDescription.getAny().get(0).getVersion();
}
use of com.mongodb.connection.ClusterDescription 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))));
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class BaseCluster method selectServerAsync.
@Override
public void selectServerAsync(final ServerSelector serverSelector, final SingleResultCallback<ServerTuple> callback) {
isTrue("open", !isClosed());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Asynchronously selecting server with selector %s", serverSelector));
}
ServerSelectionRequest request = new ServerSelectionRequest(serverSelector, getCompositeServerSelector(serverSelector), getMaxWaitTimeNanos(), callback);
CountDownLatch currentPhase = phase.get();
ClusterDescription currentDescription = description;
if (!handleServerSelectionRequest(request, currentPhase, currentDescription)) {
notifyWaitQueueHandler(request);
}
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class AbstractMultiServerCluster method onChange.
@Override
public void onChange(final ServerDescriptionChangedEvent event) {
withLock(() -> {
if (isClosed()) {
return;
}
ServerDescription newDescription = event.getNewDescription();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Handling description changed event for server %s with description %s", newDescription.getAddress(), newDescription));
}
ServerTuple serverTuple = addressToServerTupleMap.get(newDescription.getAddress());
if (serverTuple == null) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(format("Ignoring description changed event for removed server %s", newDescription.getAddress()));
}
return;
}
boolean shouldUpdateDescription = true;
if (newDescription.isOk()) {
if (clusterType == UNKNOWN && newDescription.getType() != REPLICA_SET_GHOST) {
clusterType = newDescription.getClusterType();
if (LOGGER.isInfoEnabled()) {
LOGGER.info(format("Discovered cluster type of %s", clusterType));
}
}
switch(clusterType) {
case REPLICA_SET:
shouldUpdateDescription = handleReplicaSetMemberChanged(newDescription);
break;
case SHARDED:
shouldUpdateDescription = handleShardRouterChanged(newDescription);
break;
case STANDALONE:
shouldUpdateDescription = handleStandAloneChanged(newDescription);
break;
default:
break;
}
}
ClusterDescription oldClusterDescription = null;
ClusterDescription newClusterDescription = null;
if (shouldUpdateDescription) {
serverTuple.description = newDescription;
oldClusterDescription = getCurrentDescription();
newClusterDescription = updateDescription();
}
if (shouldUpdateDescription) {
fireChangeEvent(newClusterDescription, oldClusterDescription);
}
});
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class AbstractMultiServerCluster method initialize.
protected void initialize(final Collection<ServerAddress> serverAddresses) {
ClusterDescription currentDescription = getCurrentDescription();
// synchronizing this code because addServer registers a callback which is re-entrant to this instance.
// In other words, we are leaking a reference to "this" from the constructor.
withLock(() -> {
for (final ServerAddress serverAddress : serverAddresses) {
addServer(serverAddress);
}
ClusterDescription newDescription = updateDescription();
fireChangeEvent(newDescription, currentDescription);
});
}
Aggregations