Search in sources :

Example 6 with ClusterDescription

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());
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Example 7 with ClusterDescription

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));
}
Also used : ServerDescription(com.mongodb.connection.ServerDescription) ArrayList(java.util.ArrayList) ClusterDescription(com.mongodb.connection.ClusterDescription) Before(org.junit.Before)

Example 8 with ClusterDescription

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();
        }
    }
}
Also used : ServerSelector(com.mongodb.selector.ServerSelector) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) ServerDescription(com.mongodb.connection.ServerDescription) BsonArray(org.bson.BsonArray) Connection(com.mongodb.internal.connection.Connection) ReadPreferenceServerSelector(com.mongodb.internal.selector.ReadPreferenceServerSelector) ClusterDescription(com.mongodb.connection.ClusterDescription) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 9 with ClusterDescription

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);
}
Also used : ClusterId(com.mongodb.connection.ClusterId) ServerHeartbeatFailedEvent(com.mongodb.event.ServerHeartbeatFailedEvent) ServerAddress(com.mongodb.ServerAddress) ServerHeartbeatStartedEvent(com.mongodb.event.ServerHeartbeatStartedEvent) IOException(java.io.IOException) ServerHeartbeatSucceededEvent(com.mongodb.event.ServerHeartbeatSucceededEvent) ServerClosedEvent(com.mongodb.event.ServerClosedEvent) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ConnectionId(com.mongodb.connection.ConnectionId) ClusterClosedEvent(com.mongodb.event.ClusterClosedEvent) ServerId(com.mongodb.connection.ServerId) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) BsonDocument(org.bson.BsonDocument) ServerOpeningEvent(com.mongodb.event.ServerOpeningEvent) ClusterDescription(com.mongodb.connection.ClusterDescription) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) Test(org.junit.jupiter.api.Test)

Example 10 with ClusterDescription

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)));
}
Also used : MongoException(com.mongodb.MongoException) MongoClientException(com.mongodb.MongoClientException) ClusterDescription(com.mongodb.connection.ClusterDescription) Test(org.junit.Test)

Aggregations

ClusterDescription (com.mongodb.connection.ClusterDescription)31 ServerAddress (com.mongodb.ServerAddress)15 ServerDescription (com.mongodb.connection.ServerDescription)12 Test (org.junit.Test)11 ServerSelector (com.mongodb.selector.ServerSelector)5 Test (org.junit.jupiter.api.Test)5 ReadPreferenceServerSelector (com.mongodb.internal.selector.ReadPreferenceServerSelector)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 BsonDocument (org.bson.BsonDocument)3 ClientSessionOptions (com.mongodb.ClientSessionOptions)2 MongoException (com.mongodb.MongoException)2 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)2 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)2 LatencyMinimizingServerSelector (com.mongodb.internal.selector.LatencyMinimizingServerSelector)2 BsonArray (org.bson.BsonArray)2 MongoClientException (com.mongodb.MongoClientException)1 MongoTimeoutException (com.mongodb.MongoTimeoutException)1 ReadPreference (com.mongodb.ReadPreference)1