use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method shouldSelectServerAsynchronouslyWhenThereIsSRVLookup.
@Test
public void shouldSelectServerAsynchronouslyWhenThereIsSRVLookup() {
// given
String srvHostName = "foo.bar.com";
ServerAddress resolvedServerAddress = new ServerAddress("host1");
ClusterableServer expectedServer = mock(ClusterableServer.class);
ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
ClusterableServerFactory serverFactory = mockServerFactory(resolvedServerAddress, expectedServer);
DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)));
cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
// when
FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
cluster.selectServerAsync(mock(ServerSelector.class), callback);
ServerTuple serverTuple = callback.get();
// then
assertServerTupleExpectations(resolvedServerAddress, expectedServer, serverTuple);
}
use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method shouldTimeoutSelectServerWhenThereIsSRVLookupException.
@Test
public void shouldTimeoutSelectServerWhenThereIsSRVLookupException() {
// given
String srvHostName = "foo.bar.com";
ServerAddress resolvedServerAddress = new ServerAddress("host1");
ClusterableServer expectedServer = mock(ClusterableServer.class);
ClusterSettings clusterSettings = ClusterSettings.builder().serverSelectionTimeout(10, MILLISECONDS).mode(ClusterConnectionMode.LOAD_BALANCED).srvHost(srvHostName).build();
ClusterableServerFactory serverFactory = mockServerFactory(resolvedServerAddress, expectedServer);
DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
when(dnsSrvRecordMonitorFactory.create(eq(srvHostName), eq(clusterSettings.getSrvServiceName()), any())).thenAnswer(invocation -> new TestDnsSrvRecordMonitor(invocation.getArgument(2)).sleepTime(Duration.ofMillis(1)).exception(new MongoConfigurationException("Unable to resolve SRV record")));
cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
MongoTimeoutException exception = assertThrows(MongoTimeoutException.class, () -> cluster.selectServer(mock(ServerSelector.class)));
assertEquals("Timed out after 10 ms while waiting to resolve SRV records for foo.bar.com. " + "Resolution exception was 'com.mongodb.MongoConfigurationException: Unable to resolve SRV record'", exception.getMessage());
}
use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method shouldSelectServerWhenThereIsNoSRVLookup.
@Test
public void shouldSelectServerWhenThereIsNoSRVLookup() {
// given
ServerAddress serverAddress = new ServerAddress("host1");
ClusterableServer expectedServer = mock(ClusterableServer.class);
ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).hosts(Collections.singletonList(serverAddress)).build();
ClusterableServerFactory serverFactory = mockServerFactory(serverAddress, expectedServer);
cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, mock(DnsSrvRecordMonitorFactory.class));
// when
ServerTuple serverTuple = cluster.selectServer(mock(ServerSelector.class));
// then
assertServerTupleExpectations(serverAddress, expectedServer, serverTuple);
// when
FutureResultCallback<ServerTuple> callback = new FutureResultCallback<>();
cluster.selectServerAsync(mock(ServerSelector.class), callback);
serverTuple = callback.get();
// then
assertServerTupleExpectations(serverAddress, expectedServer, serverTuple);
}
use of com.mongodb.connection.ClusterSettings in project mongo-java-driver by mongodb.
the class LoadBalancedClusterTest method shouldNotInitServerAfterClosing.
@Test
void shouldNotInitServerAfterClosing() {
// prepare mocks
ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.LOAD_BALANCED).srvHost("foo.bar.com").build();
ClusterableServerFactory serverFactory = mock(ClusterableServerFactory.class);
when(serverFactory.getSettings()).thenReturn(mock(ServerSettings.class));
DnsSrvRecordMonitorFactory srvRecordMonitorFactory = mock(DnsSrvRecordMonitorFactory.class);
when(srvRecordMonitorFactory.create(any(), eq(clusterSettings.getSrvServiceName()), any(DnsSrvRecordInitializer.class))).thenReturn(mock(DnsSrvRecordMonitor.class));
ArgumentCaptor<DnsSrvRecordInitializer> serverInitializerCaptor = ArgumentCaptor.forClass(DnsSrvRecordInitializer.class);
// create `cluster` and capture its `DnsSrvRecordInitializer` (server initializer)
LoadBalancedCluster cluster = new LoadBalancedCluster(new ClusterId(), clusterSettings, serverFactory, srvRecordMonitorFactory);
verify(srvRecordMonitorFactory, times(1)).create(any(), eq(clusterSettings.getSrvServiceName()), serverInitializerCaptor.capture());
// close `cluster`, call `DnsSrvRecordInitializer.initialize` and check that it does not result in creating a `ClusterableServer`
cluster.close();
serverInitializerCaptor.getValue().initialize(Collections.singleton(new ServerAddress()));
verify(serverFactory, never()).create(any(), any());
}
use of com.mongodb.connection.ClusterSettings 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();
}
Aggregations