Search in sources :

Example 1 with ServerListener

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");
    }
}
Also used : ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ServerSettings(com.mongodb.connection.ServerSettings) Fixture.getMongoClientSettingsBuilder(com.mongodb.client.Fixture.getMongoClientSettingsBuilder) Block(com.mongodb.Block) MongoClientSettings(com.mongodb.MongoClientSettings) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) ServerListener(com.mongodb.event.ServerListener) ClusterFixture.isServerlessTest(com.mongodb.ClusterFixture.isServerlessTest) Test(org.junit.Test)

Example 2 with ServerListener

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);
}
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 3 with ServerListener

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);
    }
}
Also used : ServerId(com.mongodb.connection.ServerId) ServerListener(com.mongodb.event.ServerListener)

Aggregations

ServerListener (com.mongodb.event.ServerListener)3 ServerId (com.mongodb.connection.ServerId)2 Block (com.mongodb.Block)1 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)1 MongoClientSettings (com.mongodb.MongoClientSettings)1 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)1 ClusterConnectionMode (com.mongodb.connection.ClusterConnectionMode)1 ServerSettings (com.mongodb.connection.ServerSettings)1 ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)1 EventListenerHelper.singleServerListener (com.mongodb.internal.event.EventListenerHelper.singleServerListener)1 BsonDocument (org.bson.BsonDocument)1 Document (org.bson.Document)1 Test (org.junit.Test)1