use of com.mongodb.connection.ClusterId in project mongo-java-driver by mongodb.
the class SingleServerClusterTest method setUpCluster.
private void setUpCluster(final ServerAddress serverAddress) {
SocketStreamFactory streamFactory = new SocketStreamFactory(SocketSettings.builder().build(), getSslSettings());
ClusterId clusterId = new ClusterId();
ClusterSettings clusterSettings = ClusterSettings.builder().mode(ClusterConnectionMode.SINGLE).hosts(singletonList(serverAddress)).build();
cluster = new SingleServerCluster(clusterId, clusterSettings, new DefaultClusterableServerFactory(ServerSettings.builder().build(), ConnectionPoolSettings.builder().maxSize(1).build(), InternalConnectionPoolSettings.builder().build(), streamFactory, streamFactory, getCredential(), null, null, null, Collections.<MongoCompressor>emptyList(), getServerApi()));
}
use of com.mongodb.connection.ClusterId in project mongo-java-driver by mongodb.
the class DefaultClusterFactory method createCluster.
/**
* Creates a cluster with the given settings. The cluster mode will be based on the mode from the settings.
*
* @param originalClusterSettings the cluster settings
* @param originalServerSettings the server settings
* @param connectionPoolSettings the connection pool settings
* @param internalConnectionPoolSettings the internal connection pool settings
* @param streamFactory the stream factory
* @param heartbeatStreamFactory the heartbeat stream factory
* @param credential the credential, which may be null
* @param commandListener an optional listener for command-related events
* @param applicationName an optional application name to associate with connections to the servers in this cluster
* @param mongoDriverInformation the optional driver information associate with connections to the servers in this cluster
* @param compressorList the list of compressors to request, in priority order
* @param serverApi the server api, which may be null
* @return the cluster
*
* @since 3.6
*/
public Cluster createCluster(final ClusterSettings originalClusterSettings, final ServerSettings originalServerSettings, final ConnectionPoolSettings connectionPoolSettings, final InternalConnectionPoolSettings internalConnectionPoolSettings, final StreamFactory streamFactory, final StreamFactory heartbeatStreamFactory, @Nullable final MongoCredential credential, final CommandListener commandListener, final String applicationName, final MongoDriverInformation mongoDriverInformation, final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi) {
ClusterId clusterId = new ClusterId();
ClusterSettings clusterSettings;
ServerSettings serverSettings;
if (noClusterEventListeners(originalClusterSettings, originalServerSettings)) {
clusterSettings = ClusterSettings.builder(originalClusterSettings).clusterListenerList(singletonList(NO_OP_CLUSTER_LISTENER)).build();
serverSettings = ServerSettings.builder(originalServerSettings).serverListenerList(singletonList(NO_OP_SERVER_LISTENER)).serverMonitorListenerList(singletonList(NO_OP_SERVER_MONITOR_LISTENER)).build();
} else {
AsynchronousClusterEventListener clusterEventListener = AsynchronousClusterEventListener.startNew(clusterId, getClusterListener(originalClusterSettings), getServerListener(originalServerSettings), getServerMonitorListener(originalServerSettings));
clusterSettings = ClusterSettings.builder(originalClusterSettings).clusterListenerList(singletonList(clusterEventListener)).build();
serverSettings = ServerSettings.builder(originalServerSettings).serverListenerList(singletonList(clusterEventListener)).serverMonitorListenerList(singletonList(clusterEventListener)).build();
}
DnsSrvRecordMonitorFactory dnsSrvRecordMonitorFactory = new DefaultDnsSrvRecordMonitorFactory(clusterId, serverSettings);
if (clusterSettings.getMode() == ClusterConnectionMode.LOAD_BALANCED) {
ClusterableServerFactory serverFactory = new LoadBalancedClusterableServerFactory(serverSettings, connectionPoolSettings, internalConnectionPoolSettings, streamFactory, credential, commandListener, applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList, serverApi);
return new LoadBalancedCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
} else {
ClusterableServerFactory serverFactory = new DefaultClusterableServerFactory(serverSettings, connectionPoolSettings, internalConnectionPoolSettings, streamFactory, heartbeatStreamFactory, credential, commandListener, applicationName, mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList, serverApi);
if (clusterSettings.getMode() == ClusterConnectionMode.SINGLE) {
return new SingleServerCluster(clusterId, clusterSettings, serverFactory);
} else if (clusterSettings.getMode() == ClusterConnectionMode.MULTIPLE) {
if (clusterSettings.getSrvHost() == null) {
return new MultiServerCluster(clusterId, clusterSettings, serverFactory);
} else {
return new DnsMultiServerCluster(clusterId, clusterSettings, serverFactory, dnsSrvRecordMonitorFactory);
}
} else {
throw new UnsupportedOperationException("Unsupported cluster mode: " + clusterSettings.getMode());
}
}
}
use of com.mongodb.connection.ClusterId in project mongo-java-driver by mongodb.
the class AbstractServerDiscoveryAndMonitoringTest method init.
protected void init(final ServerListenerFactory serverListenerFactory, final ClusterListener clusterListener) {
ConnectionString connectionString = new ConnectionString(definition.getString("uri").getValue());
ClusterSettings settings = ClusterSettings.builder().applyConnectionString(connectionString).serverSelectionTimeout(1, TimeUnit.SECONDS).build();
ClusterId clusterId = new ClusterId();
factory = new DefaultTestClusterableServerFactory(settings.getMode(), serverListenerFactory);
ClusterSettings clusterSettings = settings.getClusterListeners().contains(clusterListener) ? settings : ClusterSettings.builder(settings).addClusterListener(clusterListener).build();
if (settings.getMode() == ClusterConnectionMode.SINGLE) {
cluster = new SingleServerCluster(clusterId, clusterSettings, factory);
} else if (settings.getMode() == ClusterConnectionMode.MULTIPLE) {
cluster = new MultiServerCluster(clusterId, clusterSettings, factory);
} else {
cluster = new LoadBalancedCluster(clusterId, clusterSettings, factory, null);
}
}
use of com.mongodb.connection.ClusterId 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();
}
}
use of com.mongodb.connection.ClusterId 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);
}
Aggregations