Search in sources :

Example 1 with ClusterOpeningEvent

use of com.mongodb.event.ClusterOpeningEvent in project mongo-java-driver by mongodb.

the class InitialDnsSeedlistDiscoveryTest method shouldDiscoverSrvRecord.

@Test
public void shouldDiscoverSrvRecord() throws InterruptedException {
    assumeFalse(isServerlessTest());
    assumeFalse(isError);
    final CountDownLatch seedsLatch = new CountDownLatch(1);
    final CountDownLatch hostsLatch = new CountDownLatch(1);
    final ConnectionString connectionString = new ConnectionString(uri);
    final SslSettings sslSettings = getSslSettings(connectionString);
    assumeTrue("SSL settings don't match", getSslSettings().isEnabled() == sslSettings.isEnabled());
    MongoClientSettings settings = MongoClientSettings.builder().applyToClusterSettings(new Block<ClusterSettings.Builder>() {

        @Override
        public void apply(final ClusterSettings.Builder builder) {
            builder.applyConnectionString(connectionString).addClusterListener(new ClusterListener() {

                @Override
                public void clusterOpening(final ClusterOpeningEvent event) {
                }

                @Override
                public void clusterClosed(final ClusterClosedEvent event) {
                }

                @Override
                public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
                    List<String> seedsList = event.getNewDescription().getServerDescriptions().stream().map(ServerDescription::getAddress).map(ServerAddress::toString).collect(Collectors.toList());
                    List<String> okHostsList = event.getNewDescription().getServerDescriptions().stream().filter(ServerDescription::isOk).map(ServerDescription::getAddress).map(ServerAddress::toString).collect(Collectors.toList());
                    hostsCheck(seedsList, seeds, numSeeds, seedsLatch);
                    hostsCheck(okHostsList, hosts, numHosts, hostsLatch);
                }
            });
        }

        private void hostsCheck(final List<String> actual, @Nullable final List<String> expected, @Nullable final Integer expectedSize, final CountDownLatch latch) {
            if (expected == null && expectedSize == null) {
                latch.countDown();
            } else if (expected != null && actual.size() == expected.size() && actual.containsAll(expected)) {
                latch.countDown();
            } else if (expectedSize != null && actual.size() == expectedSize) {
                latch.countDown();
            }
        }
    }).applyToSslSettings(new Block<SslSettings.Builder>() {

        @Override
        public void apply(final SslSettings.Builder builder) {
            builder.applySettings(sslSettings);
            builder.invalidHostNameAllowed(true);
        }
    }).build();
    try (MongoClient client = createMongoClient(settings)) {
        assertTrue(seedsLatch.await(ClusterFixture.TIMEOUT, TimeUnit.SECONDS));
        assertTrue(hostsLatch.await(ClusterFixture.TIMEOUT, TimeUnit.SECONDS));
        assertTrue(client.getDatabase("admin").runCommand(new Document("ping", 1)).containsKey("ok"));
    }
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ServerDescription(com.mongodb.connection.ServerDescription) MongoClientSettings(com.mongodb.MongoClientSettings) ConnectionString(com.mongodb.ConnectionString) CountDownLatch(java.util.concurrent.CountDownLatch) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) ClusterFixture.getSslSettings(com.mongodb.ClusterFixture.getSslSettings) SslSettings(com.mongodb.connection.SslSettings) ClusterClosedEvent(com.mongodb.event.ClusterClosedEvent) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) ClusterListener(com.mongodb.event.ClusterListener) Block(com.mongodb.Block) ConnectionString(com.mongodb.ConnectionString) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Example 2 with ClusterOpeningEvent

use of com.mongodb.event.ClusterOpeningEvent 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 3 with ClusterOpeningEvent

use of com.mongodb.event.ClusterOpeningEvent in project mongo-java-driver by mongodb.

the class ServerDiscoveryAndMonitoringMonitoringTest method assertEvents.

private void assertEvents(final BsonArray events) {
    Iterator<ClusterDescriptionChangedEvent> clusterDescriptionChangedEventIterator = clusterListener.getClusterDescriptionChangedEvents().iterator();
    for (BsonValue eventValue : events) {
        BsonDocument eventDocument = eventValue.asDocument();
        if (eventDocument.containsKey("topology_opening_event")) {
            ClusterOpeningEvent event = clusterListener.getClusterOpeningEvent();
            assertNotNull("event", event);
            assertEquals("clusterId", getCluster().getClusterId(), event.getClusterId());
        } else if (eventDocument.containsKey("topology_description_changed_event")) {
            ClusterDescriptionChangedEvent event = clusterDescriptionChangedEventIterator.next();
            assertNotNull("event", event);
            assertEquals(getCluster().getClusterId(), event.getClusterId());
            BsonDocument topologyDescriptionChangedEventDocument = eventDocument.getDocument("topology_description_changed_event");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(topologyDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            BsonDocument newDescription = topologyDescriptionChangedEventDocument.getDocument("newDescription");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(newDescription), event.getNewDescription());
            if (newDescription.getString("topologyType").getValue().equals("Single")) {
                assertEquals(SingleServerCluster.class, getCluster().getClass());
            } else {
                assertEquals(MultiServerCluster.class, getCluster().getClass());
            }
        } else if (eventDocument.containsKey("server_opening_event")) {
            BsonDocument serverOpeningEventDocument = eventDocument.getDocument("server_opening_event");
            ServerAddress serverAddress = new ServerAddress(serverOpeningEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerOpeningEvent event = serverListener.getServerOpeningEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_closed_event")) {
            BsonDocument serverClosedEventDocument = eventDocument.getDocument("server_closed_event");
            ServerAddress serverAddress = new ServerAddress(serverClosedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerClosedEvent event = serverListener.getServerClosedEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_description_changed_event")) {
            BsonDocument serverDescriptionChangedEventDocument = eventDocument.getDocument("server_description_changed_event");
            ServerAddress serverAddress = new ServerAddress(serverDescriptionChangedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            assertEquals("serverDescriptionChangedEvents size", 1, serverListener.getServerDescriptionChangedEvents().size());
            ServerDescriptionChangedEvent event = serverListener.getServerDescriptionChangedEvents().get(0);
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("newDescription")), event.getNewDescription());
        } else {
            throw new IllegalArgumentException("Unsupported event type: " + eventDocument.keySet().iterator().next());
        }
    }
    assertFalse(clusterDescriptionChangedEventIterator.hasNext());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ServerClosedEvent(com.mongodb.event.ServerClosedEvent) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) BsonDocument(org.bson.BsonDocument) ServerOpeningEvent(com.mongodb.event.ServerOpeningEvent) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) BsonValue(org.bson.BsonValue)

Example 4 with ClusterOpeningEvent

use of com.mongodb.event.ClusterOpeningEvent in project mongo-java-driver by mongodb.

the class LoadBalancedCluster method init.

private void init(final ClusterId clusterId, final ClusterableServerFactory serverFactory, final ServerAddress host) {
    clusterListener.clusterOpening(new ClusterOpeningEvent(clusterId));
    ClusterDescription initialDescription = new ClusterDescription(settings.getMode(), ClusterType.LOAD_BALANCED, singletonList(ServerDescription.builder().address(settings.getHosts().get(0)).state(CONNECTING).build()), settings, serverFactory.getSettings());
    clusterListener.clusterDescriptionChanged(new ClusterDescriptionChangedEvent(clusterId, initialDescription, description));
    description = new ClusterDescription(ClusterConnectionMode.LOAD_BALANCED, ClusterType.LOAD_BALANCED, singletonList(ServerDescription.builder().ok(true).state(ServerConnectionState.CONNECTED).type(ServerType.LOAD_BALANCER).address(host).build()), settings, serverFactory.getSettings());
    server = serverFactory.create(this, host);
    clusterListener.clusterDescriptionChanged(new ClusterDescriptionChangedEvent(clusterId, description, initialDescription));
}
Also used : ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) ClusterDescription(com.mongodb.connection.ClusterDescription) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent)

Example 5 with ClusterOpeningEvent

use of com.mongodb.event.ClusterOpeningEvent in project mongo-java-driver by mongodb.

the class ServerDiscoveryAndMonitoringMonitoringTest method assertEvents.

private void assertEvents(final BsonArray events) {
    Iterator<ClusterDescriptionChangedEvent> clusterDescriptionChangedEventIterator = clusterListener.getClusterDescriptionChangedEvents().iterator();
    for (BsonValue eventValue : events) {
        BsonDocument eventDocument = eventValue.asDocument();
        if (eventDocument.containsKey("topology_opening_event")) {
            ClusterOpeningEvent event = clusterListener.getClusterOpeningEvent();
            assertNotNull("event", event);
            assertEquals("clusterId", getCluster().getClusterId(), event.getClusterId());
        } else if (eventDocument.containsKey("topology_description_changed_event")) {
            ClusterDescriptionChangedEvent event = clusterDescriptionChangedEventIterator.next();
            assertNotNull("event", event);
            assertEquals(getCluster().getClusterId(), event.getClusterId());
            BsonDocument topologyDescriptionChangedEventDocument = eventDocument.getDocument("topology_description_changed_event");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(topologyDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            BsonDocument newDescription = topologyDescriptionChangedEventDocument.getDocument("newDescription");
            assertEqualClusterDescriptions(createClusterDescriptionFromClusterDescriptionDocument(newDescription), event.getNewDescription());
            if (newDescription.getString("topologyType").getValue().equals("Single") && isSingleServerClusterExpected()) {
                assertEquals(SingleServerCluster.class, getCluster().getClass());
            } else if (newDescription.getString("topologyType").getValue().equals("LoadBalanced")) {
                assertEquals(LoadBalancedCluster.class, getCluster().getClass());
            } else {
                assertEquals(MultiServerCluster.class, getCluster().getClass());
            }
        } else if (eventDocument.containsKey("server_opening_event")) {
            BsonDocument serverOpeningEventDocument = eventDocument.getDocument("server_opening_event");
            ServerAddress serverAddress = new ServerAddress(serverOpeningEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerOpeningEvent event = serverListener.getServerOpeningEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_closed_event")) {
            BsonDocument serverClosedEventDocument = eventDocument.getDocument("server_closed_event");
            ServerAddress serverAddress = new ServerAddress(serverClosedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            ServerClosedEvent event = serverListener.getServerClosedEvent();
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
        } else if (eventDocument.containsKey("server_description_changed_event")) {
            BsonDocument serverDescriptionChangedEventDocument = eventDocument.getDocument("server_description_changed_event");
            ServerAddress serverAddress = new ServerAddress(serverDescriptionChangedEventDocument.getString("address").getValue());
            TestServerListener serverListener = serverListenerFactory.getListener(serverAddress);
            assertNotNull("serverListener", serverListener);
            assertEquals("serverDescriptionChangedEvents size", 1, serverListener.getServerDescriptionChangedEvents().size());
            ServerDescriptionChangedEvent event = serverListener.getServerDescriptionChangedEvents().get(0);
            assertNotNull("event", event);
            assertEquals("serverId", new ServerId(getCluster().getClusterId(), serverAddress), event.getServerId());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("previousDescription")), event.getPreviousDescription());
            assertEqualServerDescriptions(createServerDescriptionFromServerDescriptionDocument(serverDescriptionChangedEventDocument.getDocument("newDescription")), event.getNewDescription());
        } else {
            throw new IllegalArgumentException("Unsupported event type: " + eventDocument.keySet().iterator().next());
        }
    }
    assertFalse(clusterDescriptionChangedEventIterator.hasNext());
}
Also used : ServerAddress(com.mongodb.ServerAddress) ServerClosedEvent(com.mongodb.event.ServerClosedEvent) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) ServerId(com.mongodb.connection.ServerId) BsonDocument(org.bson.BsonDocument) ServerOpeningEvent(com.mongodb.event.ServerOpeningEvent) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) BsonValue(org.bson.BsonValue)

Aggregations

ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)5 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)5 BsonDocument (org.bson.BsonDocument)4 ServerAddress (com.mongodb.ServerAddress)3 ServerClosedEvent (com.mongodb.event.ServerClosedEvent)3 ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)3 ServerOpeningEvent (com.mongodb.event.ServerOpeningEvent)3 ClusterDescription (com.mongodb.connection.ClusterDescription)2 ServerId (com.mongodb.connection.ServerId)2 ClusterClosedEvent (com.mongodb.event.ClusterClosedEvent)2 BsonValue (org.bson.BsonValue)2 Block (com.mongodb.Block)1 ClusterFixture.getSslSettings (com.mongodb.ClusterFixture.getSslSettings)1 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)1 ConnectionString (com.mongodb.ConnectionString)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 ClusterId (com.mongodb.connection.ClusterId)1 ClusterSettings (com.mongodb.connection.ClusterSettings)1 ConnectionId (com.mongodb.connection.ConnectionId)1 ServerDescription (com.mongodb.connection.ServerDescription)1