Search in sources :

Example 6 with ClusterDescriptionChangedEvent

use of com.mongodb.event.ClusterDescriptionChangedEvent 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)

Example 7 with ClusterDescriptionChangedEvent

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

the class InitialDnsSeedlistDiscoveryTest method shouldResolveTxtRecord.

@Test
public void shouldResolveTxtRecord() throws InterruptedException {
    if (isError) {
        MongoClient client = null;
        try {
            final AtomicReference<MongoException> exceptionReference = new AtomicReference<MongoException>();
            final CountDownLatch latch = new CountDownLatch(1);
            ConnectionString connectionString;
            MongoClientSettings settings;
            try {
                connectionString = new ConnectionString(uri);
                final SslSettings sslSettings = getSslSettings(connectionString);
                assumeTrue("SSL settings don't match", getSslSettings().isEnabled() == sslSettings.isEnabled());
                settings = MongoClientSettings.builder().applyConnectionString(connectionString).applyToSslSettings(new Block<SslSettings.Builder>() {

                    @Override
                    public void apply(final SslSettings.Builder builder) {
                        builder.applySettings(sslSettings);
                        builder.invalidHostNameAllowed(true);
                    }
                }).applyToClusterSettings(new Block<ClusterSettings.Builder>() {

                    @Override
                    public void apply(final ClusterSettings.Builder builder) {
                        builder.serverSelectionTimeout(5, TimeUnit.SECONDS);
                        builder.addClusterListener(new ClusterListener() {

                            @Override
                            public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) {
                                if (event.getNewDescription().getSrvResolutionException() != null) {
                                    exceptionReference.set(event.getNewDescription().getSrvResolutionException());
                                    latch.countDown();
                                }
                            }
                        });
                    }
                }).build();
            } catch (MongoClientException | IllegalArgumentException e) {
                // all good
                return;
            }
            client = createMongoClient(settings);
            // Instead we just try to execute an operation and assert that it throws
            if (settings.getClusterSettings().getMode() == ClusterConnectionMode.LOAD_BALANCED) {
                try {
                    client.getDatabase("admin").runCommand(new Document("ping", 1));
                } catch (MongoClientException e) {
                // all good
                }
            } else {
                if (!latch.await(5, TimeUnit.SECONDS)) {
                    fail("Failed to capture SRV resolution exception");
                }
                try {
                    throw exceptionReference.get();
                } catch (MongoClientException e) {
                // all good
                }
            }
        } finally {
            if (client != null) {
                client.close();
            }
        }
    } else {
        ConnectionString connectionString = new ConnectionString(this.uri);
        for (Map.Entry<String, BsonValue> entry : options.entrySet()) {
            if (entry.getKey().equals("replicaSet")) {
                assertEquals(entry.getValue().asString().getValue(), connectionString.getRequiredReplicaSetName());
            } else if (entry.getKey().equals("ssl")) {
                assertEquals(entry.getValue().asBoolean().getValue(), connectionString.getSslEnabled());
            } else if (entry.getKey().equals("authSource")) {
                // ignoring authSource for now, because without at least a userName also in the connection string,
                // the authSource is ignored.  If the test gets this far, at least we know that a TXT record
                // containing in authSource doesn't blow up.  We just don't test that it's actually used.
                assertTrue(true);
            } else if (entry.getKey().equals("directConnection")) {
                assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isDirectConnection());
            } else if (entry.getKey().equals("loadBalanced")) {
                assertEquals(entry.getValue().asBoolean().getValue(), connectionString.isLoadBalanced());
            } else if (entry.getKey().equals("srvMaxHosts")) {
                assertEquals(Integer.valueOf(entry.getValue().asInt32().getValue()), connectionString.getSrvMaxHosts());
            } else if (entry.getKey().equals("srvServiceName")) {
                assertEquals(entry.getValue().asString().getValue(), connectionString.getSrvServiceName());
            } else {
                throw new UnsupportedOperationException("No support configured yet for " + entry.getKey());
            }
        }
    }
}
Also used : ClusterSettings(com.mongodb.connection.ClusterSettings) ConnectionString(com.mongodb.ConnectionString) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoException(com.mongodb.MongoException) MongoClientException(com.mongodb.MongoClientException) AtomicReference(java.util.concurrent.atomic.AtomicReference) MongoClientSettings(com.mongodb.MongoClientSettings) CountDownLatch(java.util.concurrent.CountDownLatch) ClusterFixture.getSslSettings(com.mongodb.ClusterFixture.getSslSettings) SslSettings(com.mongodb.connection.SslSettings) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) ClusterListener(com.mongodb.event.ClusterListener) Block(com.mongodb.Block) ConnectionString(com.mongodb.ConnectionString) Map(java.util.Map) BsonValue(org.bson.BsonValue) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Example 8 with ClusterDescriptionChangedEvent

use of com.mongodb.event.ClusterDescriptionChangedEvent 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)

Aggregations

ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)8 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)5 BsonDocument (org.bson.BsonDocument)5 ServerAddress (com.mongodb.ServerAddress)3 ServerClosedEvent (com.mongodb.event.ServerClosedEvent)3 ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)3 ServerOpeningEvent (com.mongodb.event.ServerOpeningEvent)3 BsonValue (org.bson.BsonValue)3 Block (com.mongodb.Block)2 ClusterFixture.getSslSettings (com.mongodb.ClusterFixture.getSslSettings)2 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)2 ConnectionString (com.mongodb.ConnectionString)2 MongoClientSettings (com.mongodb.MongoClientSettings)2 ClusterDescription (com.mongodb.connection.ClusterDescription)2 ClusterSettings (com.mongodb.connection.ClusterSettings)2 ServerId (com.mongodb.connection.ServerId)2 SslSettings (com.mongodb.connection.SslSettings)2 ClusterClosedEvent (com.mongodb.event.ClusterClosedEvent)2 ClusterListener (com.mongodb.event.ClusterListener)2 CountDownLatch (java.util.concurrent.CountDownLatch)2