Search in sources :

Example 1 with ClusterConnectionMode

use of com.mongodb.connection.ClusterConnectionMode in project mongo-java-driver by mongodb.

the class DefaultClusterableServerFactory method create.

@Override
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress) {
    ServerId serverId = new ServerId(cluster.getClusterId(), serverAddress);
    ClusterConnectionMode clusterMode = cluster.getSettings().getMode();
    SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
    ServerMonitor serverMonitor = new DefaultServerMonitor(serverId, serverSettings, cluster.getClock(), // no credentials, compressor list, or command listener for the server monitor factory
    new InternalStreamConnectionFactory(clusterMode, heartbeatStreamFactory, null, applicationName, mongoDriverInformation, emptyList(), null, serverApi), clusterMode, serverApi, sdamProvider);
    ConnectionPool connectionPool = new DefaultConnectionPool(serverId, new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName, mongoDriverInformation, compressorList, commandListener, serverApi), connectionPoolSettings, internalConnectionPoolSettings, sdamProvider);
    ServerListener serverListener = singleServerListener(serverSettings);
    SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor, connectionPool, clusterMode);
    sdamProvider.initialize(sdam);
    serverMonitor.start();
    return new DefaultServer(serverId, clusterMode, connectionPool, new DefaultConnectionFactory(), serverMonitor, sdam, serverListener, commandListener, cluster.getClock(), true);
}
Also used : ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) ServerId(com.mongodb.connection.ServerId) EventListenerHelper.singleServerListener(com.mongodb.internal.event.EventListenerHelper.singleServerListener) ServerListener(com.mongodb.event.ServerListener)

Example 2 with ClusterConnectionMode

use of com.mongodb.connection.ClusterConnectionMode in project mongo-java-driver by mongodb.

the class AbstractConnectionPoolTest method setUp.

@Before
public void setUp() {
    assumeFalse(skipTest);
    ConnectionPoolSettings.Builder settingsBuilder = ConnectionPoolSettings.builder();
    BsonDocument poolOptions = definition.getDocument("poolOptions", new BsonDocument());
    if (poolOptions.containsKey("maxPoolSize")) {
        settingsBuilder.maxSize(poolOptions.getNumber("maxPoolSize").intValue());
    }
    if (poolOptions.containsKey("minPoolSize")) {
        settingsBuilder.minSize(poolOptions.getNumber("minPoolSize").intValue());
    }
    if (poolOptions.containsKey("maxIdleTimeMS")) {
        settingsBuilder.maxConnectionIdleTime(poolOptions.getNumber("maxIdleTimeMS").intValue(), TimeUnit.MILLISECONDS);
    }
    if (poolOptions.containsKey("waitQueueTimeoutMS")) {
        settingsBuilder.maxWaitTime(poolOptions.getNumber("waitQueueTimeoutMS").intValue(), TimeUnit.MILLISECONDS);
    }
    if (poolOptions.containsKey("backgroundThreadIntervalMS")) {
        long intervalMillis = poolOptions.getNumber("backgroundThreadIntervalMS").longValue();
        assertFalse(intervalMillis == 0);
        if (intervalMillis < 0) {
            settingsBuilder.maintenanceInitialDelay(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
        } else {
            /* Using frequency/period instead of an interval as required by the specification is incorrect, for example,
                 * because it opens up a possibility to run the background thread non-stop if runs are as long as or longer than the period.
                 * Nevertheless, I am reusing what we already have in the driver instead of clogging up the implementation. */
            settingsBuilder.maintenanceFrequency(poolOptions.getNumber("backgroundThreadIntervalMS").longValue(), TimeUnit.MILLISECONDS);
        }
    }
    if (poolOptions.containsKey("maxConnecting")) {
        settingsBuilder.maxConnecting(poolOptions.getInt32("maxConnecting").intValue());
    }
    listener = new TestConnectionPoolListener();
    settingsBuilder.addConnectionPoolListener(listener);
    settings = settingsBuilder.build();
    InternalConnectionPoolSettings internalSettings = InternalConnectionPoolSettings.builder().prestartAsyncWorkManager(PRESTART_POOL_ASYNC_WORK_MANAGER_FILE_NAMES.contains(fileName)).build();
    Style style = Style.of(definition.getString("style").getValue());
    switch(style) {
        case UNIT:
            {
                ServerId serverId = new ServerId(new ClusterId(), new ServerAddress("host1"));
                pool = new DefaultConnectionPool(serverId, new TestInternalConnectionFactory(), settings, internalSettings, SameObjectProvider.initialized(mock(SdamServerDescriptionManager.class)));
                break;
            }
        case INTEGRATION:
            {
                ServerId serverId = new ServerId(new ClusterId(), ClusterFixture.getPrimary());
                ClusterConnectionMode connectionMode = ClusterConnectionMode.MULTIPLE;
                SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
                pool = new ConnectionIdAdjustingConnectionPool(new DefaultConnectionPool(serverId, new InternalStreamConnectionFactory(connectionMode, createStreamFactory(SocketSettings.builder().build(), ClusterFixture.getSslSettings()), ClusterFixture.getCredentialWithCache(), poolOptions.getString("appName", new BsonString(fileName + ": " + description)).getValue(), MongoDriverInformation.builder().build(), Collections.emptyList(), new TestCommandListener(), ClusterFixture.getServerApi()), settings, internalSettings, sdamProvider));
                sdamProvider.initialize(new DefaultSdamServerDescriptionManager(mockedCluster(), serverId, mock(ServerListener.class), mock(ServerMonitor.class), pool, connectionMode));
                setFailPoint();
                break;
            }
        default:
            {
                throw new AssertionError(format("Style %s is not implemented", style));
            }
    }
    if (internalSettings.isPrestartAsyncWorkManager()) {
        waitForPoolAsyncWorkManagerStart();
    }
}
Also used : ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) SameObjectProvider(com.mongodb.internal.inject.SameObjectProvider) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) ServerId(com.mongodb.connection.ServerId) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ConnectionPoolSettings(com.mongodb.connection.ConnectionPoolSettings) ServerListener(com.mongodb.event.ServerListener) Before(org.junit.Before)

Example 3 with ClusterConnectionMode

use of com.mongodb.connection.ClusterConnectionMode in project spring-data-mongodb by spring-projects.

the class MongoClientSettingsFactoryBean method createInstance.

@Override
protected MongoClientSettings createInstance() {
    Builder builder = // 
    MongoClientSettings.builder().readPreference(// 
    readPreference).writeConcern(// 
    writeConcern).readConcern(// 
    readConcern).codecRegistry(// 
    codecRegistry).applicationName(// 
    applicationName).autoEncryptionSettings(// 
    autoEncryptionSettings).applyToClusterSettings((settings) -> {
        settings.serverSelectionTimeout(clusterServerSelectionTimeoutMS, TimeUnit.MILLISECONDS);
        if (clusterConnectionMode != null) {
            settings.mode(clusterConnectionMode);
        }
        settings.requiredReplicaSetName(clusterRequiredReplicaSetName);
        if (!CollectionUtils.isEmpty(clusterHosts)) {
            settings.hosts(clusterHosts);
        }
        settings.localThreshold(clusterLocalThresholdMS, TimeUnit.MILLISECONDS);
        // settings.maxWaitQueueSize(clusterMaxWaitQueueSize);
        settings.requiredClusterType(custerRequiredClusterType);
        if (StringUtils.hasText(clusterSrvHost)) {
            settings.srvHost(clusterSrvHost);
        }
    }).applyToConnectionPoolSettings((settings) -> {
        settings.minSize(poolMinSize);
        settings.maxSize(poolMaxSize);
        settings.maxConnectionIdleTime(poolMaxConnectionIdleTimeMS, TimeUnit.MILLISECONDS);
        settings.maxWaitTime(poolMaxWaitTimeMS, TimeUnit.MILLISECONDS);
        settings.maxConnectionLifeTime(poolMaxConnectionLifeTimeMS, TimeUnit.MILLISECONDS);
        // settings.maxWaitQueueSize(poolMaxWaitQueueSize);
        settings.maintenanceFrequency(poolMaintenanceFrequencyMS, TimeUnit.MILLISECONDS);
        settings.maintenanceInitialDelay(poolMaintenanceInitialDelayMS, TimeUnit.MILLISECONDS);
    }).applyToServerSettings((settings) -> {
        settings.minHeartbeatFrequency(serverMinHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
        settings.heartbeatFrequency(serverHeartbeatFrequencyMS, TimeUnit.MILLISECONDS);
    }).applyToSocketSettings((settings) -> {
        settings.connectTimeout(socketConnectTimeoutMS, TimeUnit.MILLISECONDS);
        settings.readTimeout(socketReadTimeoutMS, TimeUnit.MILLISECONDS);
        settings.receiveBufferSize(socketReceiveBufferSize);
        settings.sendBufferSize(socketSendBufferSize);
    }).applyToSslSettings((settings) -> {
        settings.enabled(sslEnabled);
        if (sslEnabled) {
            settings.invalidHostNameAllowed(sslInvalidHostNameAllowed);
            try {
                settings.context(StringUtils.hasText(sslProvider) ? SSLContext.getInstance(sslProvider) : SSLContext.getDefault());
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalArgumentException(e.getMessage(), e);
            }
        }
    });
    if (streamFactoryFactory != null) {
        builder = builder.streamFactoryFactory(streamFactoryFactory);
    }
    if (retryReads != null) {
        builder = builder.retryReads(retryReads);
    }
    if (retryWrites != null) {
        builder = builder.retryWrites(retryWrites);
    }
    if (uUidRepresentation != null) {
        builder = builder.uuidRepresentation(uUidRepresentation);
    }
    if (serverApi != null) {
        builder = builder.serverApi(serverApi);
    }
    return builder.build();
}
Also used : ReadPreference(com.mongodb.ReadPreference) ServerAddress(com.mongodb.ServerAddress) Arrays(java.util.Arrays) SSLContext(javax.net.ssl.SSLContext) ServerApi(com.mongodb.ServerApi) ClusterType(com.mongodb.connection.ClusterType) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) AutoEncryptionSettings(com.mongodb.AutoEncryptionSettings) UuidRepresentation(org.bson.UuidRepresentation) AbstractFactoryBean(org.springframework.beans.factory.config.AbstractFactoryBean) TimeUnit(java.util.concurrent.TimeUnit) Builder(com.mongodb.MongoClientSettings.Builder) StreamFactoryFactory(com.mongodb.connection.StreamFactoryFactory) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) ClusterConnectionMode(com.mongodb.connection.ClusterConnectionMode) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Nullable(org.springframework.lang.Nullable) MongoClientSettings(com.mongodb.MongoClientSettings) ReadConcern(com.mongodb.ReadConcern) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) StringUtils(org.springframework.util.StringUtils) Builder(com.mongodb.MongoClientSettings.Builder) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

ClusterConnectionMode (com.mongodb.connection.ClusterConnectionMode)3 ServerAddress (com.mongodb.ServerAddress)2 ServerId (com.mongodb.connection.ServerId)2 ServerListener (com.mongodb.event.ServerListener)2 AutoEncryptionSettings (com.mongodb.AutoEncryptionSettings)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 Builder (com.mongodb.MongoClientSettings.Builder)1 ReadConcern (com.mongodb.ReadConcern)1 ReadPreference (com.mongodb.ReadPreference)1 ServerApi (com.mongodb.ServerApi)1 WriteConcern (com.mongodb.WriteConcern)1 ClusterId (com.mongodb.connection.ClusterId)1 ClusterType (com.mongodb.connection.ClusterType)1 ConnectionPoolSettings (com.mongodb.connection.ConnectionPoolSettings)1 StreamFactoryFactory (com.mongodb.connection.StreamFactoryFactory)1 EventListenerHelper.singleServerListener (com.mongodb.internal.event.EventListenerHelper.singleServerListener)1 SameObjectProvider (com.mongodb.internal.inject.SameObjectProvider)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1