Search in sources :

Example 1 with ClusterListener

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

use of com.mongodb.event.ClusterListener in project spring-data-mongodb by spring-projects.

the class MongoClientFactoryBean method computeClientSetting.

/**
 * Create {@link MongoClientSettings} based on configuration and priority (lower is better).
 * <ol>
 * <li>{@link MongoClientFactoryBean#mongoClientSettings}</li>
 * <li>{@link MongoClientFactoryBean#connectionString}</li>
 * <li>default {@link MongoClientSettings}</li>
 * </ol>
 *
 * @since 3.0
 */
protected MongoClientSettings computeClientSetting() {
    if (connectionString != null && (StringUtils.hasText(host) || port != null)) {
        throw new IllegalStateException("ConnectionString and host/port configuration exclude one another!");
    }
    ConnectionString connectionString = this.connectionString != null ? this.connectionString : new ConnectionString(String.format("mongodb://%s:%s", getOrDefault(host, ServerAddress.defaultHost()), getOrDefault(port, "" + ServerAddress.defaultPort())));
    Builder builder = MongoClientSettings.builder().applyConnectionString(connectionString);
    builder.uuidRepresentation(UuidRepresentation.JAVA_LEGACY);
    if (mongoClientSettings != null) {
        MongoClientSettings defaultSettings = MongoClientSettings.builder().build();
        SslSettings sslSettings = mongoClientSettings.getSslSettings();
        ClusterSettings clusterSettings = mongoClientSettings.getClusterSettings();
        ConnectionPoolSettings connectionPoolSettings = mongoClientSettings.getConnectionPoolSettings();
        SocketSettings socketSettings = mongoClientSettings.getSocketSettings();
        ServerSettings serverSettings = mongoClientSettings.getServerSettings();
        builder = // 
        builder.applicationName(computeSettingsValue(defaultSettings.getApplicationName(), mongoClientSettings.getApplicationName(), // 
        connectionString.getApplicationName())).applyToSslSettings(settings -> {
            applySettings(settings::enabled, computeSettingsValue(SslSettings::isEnabled, defaultSettings.getSslSettings(), sslSettings, connectionString.getSslEnabled()));
            applySettings(settings::invalidHostNameAllowed, (computeSettingsValue(SslSettings::isInvalidHostNameAllowed, defaultSettings.getSslSettings(), sslSettings, connectionString.getSslInvalidHostnameAllowed())));
            settings.context(sslSettings.getContext());
        }).applyToClusterSettings(settings -> {
            applySettings(settings::hosts, computeSettingsValue(ClusterSettings::getHosts, defaultSettings.getClusterSettings(), clusterSettings, connectionString.getHosts().stream().map(ServerAddress::new).collect(Collectors.toList())));
            applySettings(settings::requiredReplicaSetName, computeSettingsValue(ClusterSettings::getRequiredReplicaSetName, defaultSettings.getClusterSettings(), clusterSettings, connectionString.getRequiredReplicaSetName()));
            applySettings(settings::srvHost, computeSettingsValue(ClusterSettings::getSrvHost, defaultSettings.getClusterSettings(), clusterSettings, null));
            applySettings(settings::mode, computeSettingsValue(ClusterSettings::getMode, defaultSettings.getClusterSettings(), clusterSettings, null));
            applySettings(it -> settings.localThreshold(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ClusterSettings it) -> it.getLocalThreshold(TimeUnit.MILLISECONDS), defaultSettings.getClusterSettings(), clusterSettings, connectionString.getLocalThreshold()));
            applySettings(settings::requiredClusterType, computeSettingsValue(ClusterSettings::getRequiredClusterType, defaultSettings.getClusterSettings(), clusterSettings, null));
            applySettings(it -> settings.serverSelectionTimeout(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ClusterSettings it) -> it.getServerSelectionTimeout(TimeUnit.MILLISECONDS), defaultSettings.getClusterSettings(), clusterSettings, connectionString.getServerSelectionTimeout()));
            applySettings(settings::serverSelector, computeSettingsValue(ClusterSettings::getServerSelector, defaultSettings.getClusterSettings(), clusterSettings, null));
            List<ClusterListener> clusterListeners = computeSettingsValue(ClusterSettings::getClusterListeners, defaultSettings.getClusterSettings(), clusterSettings, null);
            if (clusterListeners != null) {
                clusterListeners.forEach(settings::addClusterListener);
            }
        }).applyToConnectionPoolSettings(settings -> {
            applySettings(it -> settings.maintenanceFrequency(it, TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaintenanceFrequency(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, null));
            applySettings(it -> settings.maxConnectionIdleTime(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaxConnectionIdleTime(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxConnectionIdleTime()));
            applySettings(it -> settings.maxConnectionLifeTime(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaxConnectionLifeTime(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxConnectionLifeTime()));
            applySettings(it -> settings.maxWaitTime(it.longValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaxWaitTime(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxWaitTime()));
            applySettings(it -> settings.maintenanceInitialDelay(it, TimeUnit.MILLISECONDS), computeSettingsValue((ConnectionPoolSettings it) -> it.getMaintenanceInitialDelay(TimeUnit.MILLISECONDS), defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, null));
            applySettings(settings::minSize, computeSettingsValue(ConnectionPoolSettings::getMinSize, defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMinConnectionPoolSize()));
            applySettings(settings::maxSize, computeSettingsValue(ConnectionPoolSettings::getMaxSize, defaultSettings.getConnectionPoolSettings(), connectionPoolSettings, connectionString.getMaxConnectionPoolSize()));
        }).applyToSocketSettings(settings -> {
            applySettings(it -> settings.connectTimeout(it, TimeUnit.MILLISECONDS), computeSettingsValue((SocketSettings it) -> it.getConnectTimeout(TimeUnit.MILLISECONDS), defaultSettings.getSocketSettings(), socketSettings, connectionString.getConnectTimeout()));
            applySettings(it -> settings.readTimeout(it, TimeUnit.MILLISECONDS), computeSettingsValue((SocketSettings it) -> it.getReadTimeout(TimeUnit.MILLISECONDS), defaultSettings.getSocketSettings(), socketSettings, connectionString.getSocketTimeout()));
            applySettings(settings::receiveBufferSize, computeSettingsValue(SocketSettings::getReceiveBufferSize, defaultSettings.getSocketSettings(), socketSettings, null));
            applySettings(settings::sendBufferSize, computeSettingsValue(SocketSettings::getSendBufferSize, defaultSettings.getSocketSettings(), socketSettings, null));
        }).applyToServerSettings(settings -> {
            applySettings(it -> settings.minHeartbeatFrequency(it.intValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ServerSettings it) -> it.getMinHeartbeatFrequency(TimeUnit.MILLISECONDS), defaultSettings.getServerSettings(), serverSettings, null));
            applySettings(it -> settings.heartbeatFrequency(it.intValue(), TimeUnit.MILLISECONDS), computeSettingsValue((ServerSettings it) -> it.getHeartbeatFrequency(TimeUnit.MILLISECONDS), defaultSettings.getServerSettings(), serverSettings, connectionString.getHeartbeatFrequency()));
            settings.applySettings(serverSettings);
        }).autoEncryptionSettings(// 
        mongoClientSettings.getAutoEncryptionSettings()).codecRegistry(// 
        mongoClientSettings.getCodecRegistry());
        applySettings(builder::readConcern, computeSettingsValue(defaultSettings.getReadConcern(), mongoClientSettings.getReadConcern(), connectionString.getReadConcern()));
        applySettings(builder::writeConcern, computeSettingsValue(defaultSettings.getWriteConcern(), mongoClientSettings.getWriteConcern(), connectionString.getWriteConcern()));
        applySettings(builder::readPreference, computeSettingsValue(defaultSettings.getReadPreference(), mongoClientSettings.getReadPreference(), connectionString.getReadPreference()));
        applySettings(builder::retryReads, computeSettingsValue(defaultSettings.getRetryReads(), mongoClientSettings.getRetryReads(), connectionString.getRetryReads()));
        applySettings(builder::retryWrites, computeSettingsValue(defaultSettings.getRetryWrites(), mongoClientSettings.getRetryWrites(), connectionString.getRetryWritesValue()));
        applySettings(builder::uuidRepresentation, computeSettingsValue(null, mongoClientSettings.getUuidRepresentation(), UuidRepresentation.JAVA_LEGACY));
    }
    if (!CollectionUtils.isEmpty(credential)) {
        builder = builder.credential(credential.iterator().next());
    }
    if (StringUtils.hasText(replicaSet)) {
        builder.applyToClusterSettings((settings) -> {
            settings.requiredReplicaSetName(replicaSet);
        });
    }
    return builder.build();
}
Also used : ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) Arrays(java.util.Arrays) DataAccessException(org.springframework.dao.DataAccessException) MongoCredential(com.mongodb.MongoCredential) MongoClient(com.mongodb.client.MongoClient) ClusterSettings(com.mongodb.connection.ClusterSettings) Function(java.util.function.Function) ServerSettings(com.mongodb.connection.ServerSettings) AbstractFactoryBean(org.springframework.beans.factory.config.AbstractFactoryBean) PersistenceExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator) SocketSettings(com.mongodb.connection.SocketSettings) Nullable(org.springframework.lang.Nullable) ServerAddress(com.mongodb.ServerAddress) SslSettings(com.mongodb.connection.SslSettings) ObjectUtils(org.springframework.util.ObjectUtils) MongoClients(com.mongodb.client.MongoClients) ClusterListener(com.mongodb.event.ClusterListener) UuidRepresentation(org.bson.UuidRepresentation) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Builder(com.mongodb.MongoClientSettings.Builder) List(java.util.List) ConnectionString(com.mongodb.ConnectionString) CollectionUtils(org.springframework.util.CollectionUtils) MongoClientSettings(com.mongodb.MongoClientSettings) SpringDataMongoDB(org.springframework.data.mongodb.SpringDataMongoDB) StringUtils(org.springframework.util.StringUtils) ClusterSettings(com.mongodb.connection.ClusterSettings) Builder(com.mongodb.MongoClientSettings.Builder) MongoClientSettings(com.mongodb.MongoClientSettings) SocketSettings(com.mongodb.connection.SocketSettings) SslSettings(com.mongodb.connection.SslSettings) ServerSettings(com.mongodb.connection.ServerSettings) ClusterListener(com.mongodb.event.ClusterListener) ConnectionString(com.mongodb.ConnectionString) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings)

Example 3 with ClusterListener

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

Aggregations

ConnectionString (com.mongodb.ConnectionString)3 MongoClientSettings (com.mongodb.MongoClientSettings)3 ClusterSettings (com.mongodb.connection.ClusterSettings)3 SslSettings (com.mongodb.connection.SslSettings)3 ClusterListener (com.mongodb.event.ClusterListener)3 Block (com.mongodb.Block)2 ClusterFixture.getSslSettings (com.mongodb.ClusterFixture.getSslSettings)2 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)2 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 BsonDocument (org.bson.BsonDocument)2 Document (org.bson.Document)2 Test (org.junit.Test)2 MongoClientException (com.mongodb.MongoClientException)1 Builder (com.mongodb.MongoClientSettings.Builder)1 MongoCredential (com.mongodb.MongoCredential)1 MongoException (com.mongodb.MongoException)1 ServerAddress (com.mongodb.ServerAddress)1 MongoClient (com.mongodb.client.MongoClient)1 MongoClients (com.mongodb.client.MongoClients)1