use of com.mongodb.event.ServerListener in project mongo-java-driver by mongodb.
the class ServerDiscoveryAndMonitoringProseTests method testRTTUpdates.
@Test
public void testRTTUpdates() throws InterruptedException {
assumeTrue(isStandalone());
assumeTrue(serverVersionAtLeast(4, 4));
List<ServerDescriptionChangedEvent> events = synchronizedList(new ArrayList<>());
MongoClientSettings settings = getMongoClientSettingsBuilder().applicationName("streamingRttTest").applyToServerSettings(new Block<ServerSettings.Builder>() {
@Override
public void apply(final ServerSettings.Builder builder) {
builder.heartbeatFrequency(50, TimeUnit.MILLISECONDS);
builder.addServerListener(new ServerListener() {
@Override
public void serverDescriptionChanged(final ServerDescriptionChangedEvent event) {
events.add(event);
}
});
}
}).build();
try (MongoClient client = MongoClients.create(settings)) {
client.getDatabase("admin").runCommand(new Document("ping", 1));
Thread.sleep(250);
assertTrue(events.size() >= 1);
events.forEach(event -> assertTrue(event.getNewDescription().getRoundTripTimeNanos() > 0));
configureFailPoint(parse(format("{" + "configureFailPoint: \"failCommand\"," + "mode: {times: 1000}," + " data: {" + " failCommands: [\"%s\", \"%s\"]," + " blockConnection: true," + " blockTimeMS: 100," + " appName: \"streamingRttTest\"" + " }" + "}", LEGACY_HELLO, HELLO)));
long startTime = System.currentTimeMillis();
while (true) {
long rttMillis = NANOSECONDS.toMillis(client.getClusterDescription().getServerDescriptions().get(0).getRoundTripTimeNanos());
if (rttMillis > 50) {
break;
}
assertFalse(System.currentTimeMillis() - startTime > 1000);
// noinspection BusyWait
Thread.sleep(50);
}
} finally {
disableFailPoint("failCommand");
}
}
use of com.mongodb.event.ServerListener 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);
}
use of com.mongodb.event.ServerListener in project mongo-java-driver by mongodb.
the class DefaultTestClusterableServerFactory method create.
@Override
public ClusterableServer create(final Cluster cluster, final ServerAddress serverAddress) {
ServerId serverId = new ServerId(cluster.getClusterId(), serverAddress);
if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
return new LoadBalancedServer(serverId, new TestConnectionPool(), new TestConnectionFactory(), serverListenerFactory.create(serverAddress), cluster.getClock());
} else {
SameObjectProvider<SdamServerDescriptionManager> sdamProvider = SameObjectProvider.uninitialized();
TestServerMonitor serverMonitor = new TestServerMonitor(sdamProvider);
serverAddressToServerMonitorMap.put(serverAddress, serverMonitor);
ConnectionPool connectionPool = new TestConnectionPool();
ServerListener serverListener = serverListenerFactory.create(serverAddress);
SdamServerDescriptionManager sdam = new DefaultSdamServerDescriptionManager(cluster, serverId, serverListener, serverMonitor, connectionPool, clusterConnectionMode);
sdamProvider.initialize(sdam);
serverMonitor.start();
return new DefaultServer(serverId, clusterConnectionMode, connectionPool, new TestConnectionFactory(), serverMonitor, sdam, serverListener, null, cluster.getClock(), true);
}
}
Aggregations