use of com.mongodb.connection.ClusterDescription 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.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class ReadPreferenceChooseServersTest method setUp.
@Before
public void setUp() throws IOException {
final TagSet tags1 = new TagSet(asList(new Tag("foo", "1"), new Tag("bar", "2"), new Tag("baz", "1")));
final TagSet tags2 = new TagSet(asList(new Tag("foo", "1"), new Tag("bar", "2"), new Tag("baz", "2")));
final TagSet tags3 = new TagSet(asList(new Tag("foo", "1"), new Tag("bar", "2"), new Tag("baz", "3")));
long acceptableLatencyMS = 15;
long bestRoundTripTime = 50;
long acceptableRoundTripTime = bestRoundTripTime + (acceptableLatencyMS / 2);
long unacceptableRoundTripTime = bestRoundTripTime + acceptableLatencyMS + 1;
primary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27017)).roundTripTime(acceptableRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(ServerType.REPLICA_SET_PRIMARY).tagSet(tags1).maxDocumentSize(FOUR_MEG).build();
secondary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27018)).roundTripTime(bestRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(ServerType.REPLICA_SET_SECONDARY).tagSet(tags2).maxDocumentSize(FOUR_MEG).build();
otherSecondary = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27019)).roundTripTime(unacceptableRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(ServerType.REPLICA_SET_SECONDARY).tagSet(tags3).maxDocumentSize(FOUR_MEG).build();
ServerDescription uninitiatedMember = ServerDescription.builder().state(CONNECTED).address(new ServerAddress(HOST, 27020)).roundTripTime(unacceptableRoundTripTime * 1000000L, NANOSECONDS).ok(true).type(REPLICA_SET_OTHER).maxDocumentSize(FOUR_MEG).build();
List<ServerDescription> nodeList = new ArrayList<ServerDescription>();
nodeList.add(primary);
nodeList.add(secondary);
nodeList.add(otherSecondary);
nodeList.add(uninitiatedMember);
set = new ClusterDescription(MULTIPLE, REPLICA_SET, nodeList);
setNoPrimary = new ClusterDescription(MULTIPLE, REPLICA_SET, asList(secondary, otherSecondary));
setNoSecondary = new ClusterDescription(MULTIPLE, REPLICA_SET, asList(primary, uninitiatedMember));
}
use of com.mongodb.connection.ClusterDescription 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.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class AsynchronousClusterEventListenerTest method testEventsPublished.
@Test
public void testEventsPublished() throws InterruptedException {
AllClusterEventListener targetListener = new AllClusterEventListener();
ClusterId clusterId = new ClusterId();
ServerId serverId = new ServerId(clusterId, new ServerAddress());
ConnectionId connectionId = new ConnectionId(serverId);
AsynchronousClusterEventListener listener = AsynchronousClusterEventListener.startNew(clusterId, targetListener, targetListener, targetListener);
ClusterOpeningEvent clusterOpeningEvent = new ClusterOpeningEvent(clusterId);
listener.clusterOpening(clusterOpeningEvent);
assertEquals(clusterOpeningEvent, targetListener.take());
ClusterDescriptionChangedEvent clusterDescriptionChangedEvent = new ClusterDescriptionChangedEvent(clusterId, new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, Collections.emptyList()), new ClusterDescription(ClusterConnectionMode.SINGLE, ClusterType.STANDALONE, Collections.emptyList()));
listener.clusterDescriptionChanged(clusterDescriptionChangedEvent);
assertEquals(clusterDescriptionChangedEvent, targetListener.take());
ServerHeartbeatStartedEvent serverHeartbeatStartedEvent = new ServerHeartbeatStartedEvent(connectionId);
listener.serverHearbeatStarted(serverHeartbeatStartedEvent);
assertEquals(serverHeartbeatStartedEvent, targetListener.take());
ServerHeartbeatSucceededEvent serverHeartbeatSucceededEvent = new ServerHeartbeatSucceededEvent(connectionId, new BsonDocument(), 1, true);
listener.serverHeartbeatSucceeded(serverHeartbeatSucceededEvent);
assertEquals(serverHeartbeatSucceededEvent, targetListener.take());
ServerHeartbeatFailedEvent serverHeartbeatFailedEvent = new ServerHeartbeatFailedEvent(connectionId, 1, true, new IOException());
listener.serverHeartbeatFailed(serverHeartbeatFailedEvent);
assertEquals(serverHeartbeatFailedEvent, targetListener.take());
ServerOpeningEvent serverOpeningEvent = new ServerOpeningEvent(serverId);
listener.serverOpening(serverOpeningEvent);
assertEquals(serverOpeningEvent, targetListener.take());
ServerDescriptionChangedEvent serverDescriptionChangedEvent = new ServerDescriptionChangedEvent(serverId, ServerDescription.builder().address(new ServerAddress()).type(UNKNOWN).state(CONNECTING).build(), ServerDescription.builder().address(new ServerAddress()).type(STANDALONE).state(CONNECTED).build());
listener.serverDescriptionChanged(serverDescriptionChangedEvent);
assertEquals(serverDescriptionChangedEvent, targetListener.take());
ServerClosedEvent serverClosedEvent = new ServerClosedEvent(serverId);
listener.serverClosed(serverClosedEvent);
assertEquals(serverClosedEvent, targetListener.take());
ClusterClosedEvent clusterClosedEvent = new ClusterClosedEvent(clusterId);
listener.clusterClosed(clusterClosedEvent);
assertEquals(clusterClosedEvent, targetListener.take());
// The thread should die after publishing the ClusterClosedEvent
listener.getPublishingThread().join(5000);
}
use of com.mongodb.connection.ClusterDescription in project mongo-java-driver by mongodb.
the class EventHelperTest method testClusterDescriptionEquivalence.
@Test
public void testClusterDescriptionEquivalence() {
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().build())), new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().build()))));
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build()))));
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27018").build(), createBuilder("localhost:27017").build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().build())), new ClusterDescription(SINGLE, STANDALONE, singletonList(createBuilder().maxWireVersion(4).build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, singletonList(createBuilder("localhost:27017").build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").maxWireVersion(4).build()))));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27017").build(), createBuilder("localhost:27018").build())), new ClusterDescription(MULTIPLE, REPLICA_SET, asList(createBuilder("localhost:27018").build(), createBuilder("localhost:27017").maxWireVersion(4).build()))));
assertTrue(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null)));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, null, emptyList(), null, null)));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg2"), emptyList(), null, null)));
assertFalse(wouldDescriptionsGenerateEquivalentEvents(new ClusterDescription(SINGLE, STANDALONE, new MongoException("msg1"), emptyList(), null, null), new ClusterDescription(SINGLE, STANDALONE, new MongoClientException("msg1"), emptyList(), null, null)));
}
Aggregations