Search in sources :

Example 1 with ServerDescriptionChangedEvent

use of com.mongodb.event.ServerDescriptionChangedEvent in project mongo-java-driver by mongodb.

the class TestServer method sendNotification.

public void sendNotification(final ServerDescription newDescription) {
    ServerDescription currentDescription = description;
    description = newDescription;
    if (serverListener != null) {
        serverListener.serverDescriptionChanged(new ServerDescriptionChangedEvent(serverId, newDescription, currentDescription));
    }
}
Also used : ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent)

Example 2 with ServerDescriptionChangedEvent

use of com.mongodb.event.ServerDescriptionChangedEvent 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 3 with ServerDescriptionChangedEvent

use of com.mongodb.event.ServerDescriptionChangedEvent in project mongo-java-driver by mongodb.

the class DefaultSdamServerDescriptionManager method updateDescription.

private void updateDescription(final ServerDescription newDescription) {
    ServerDescription previousDescription = description;
    description = newDescription;
    ServerDescriptionChangedEvent serverDescriptionChangedEvent = new ServerDescriptionChangedEvent(serverId, newDescription, previousDescription);
    if (!wouldDescriptionsGenerateEquivalentEvents(newDescription, previousDescription)) {
        serverListener.serverDescriptionChanged(serverDescriptionChangedEvent);
    }
    cluster.onChange(serverDescriptionChangedEvent);
}
Also used : ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ServerDescription(com.mongodb.connection.ServerDescription) ServerDescriptionHelper.unknownConnectingServerDescription(com.mongodb.internal.connection.ServerDescriptionHelper.unknownConnectingServerDescription)

Example 4 with ServerDescriptionChangedEvent

use of com.mongodb.event.ServerDescriptionChangedEvent 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);
}
Also used : ClusterId(com.mongodb.connection.ClusterId) ServerHeartbeatFailedEvent(com.mongodb.event.ServerHeartbeatFailedEvent) ServerAddress(com.mongodb.ServerAddress) ServerHeartbeatStartedEvent(com.mongodb.event.ServerHeartbeatStartedEvent) IOException(java.io.IOException) ServerHeartbeatSucceededEvent(com.mongodb.event.ServerHeartbeatSucceededEvent) ServerClosedEvent(com.mongodb.event.ServerClosedEvent) ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ConnectionId(com.mongodb.connection.ConnectionId) ClusterClosedEvent(com.mongodb.event.ClusterClosedEvent) ServerId(com.mongodb.connection.ServerId) ClusterDescriptionChangedEvent(com.mongodb.event.ClusterDescriptionChangedEvent) BsonDocument(org.bson.BsonDocument) ServerOpeningEvent(com.mongodb.event.ServerOpeningEvent) ClusterDescription(com.mongodb.connection.ClusterDescription) ClusterOpeningEvent(com.mongodb.event.ClusterOpeningEvent) Test(org.junit.jupiter.api.Test)

Example 5 with ServerDescriptionChangedEvent

use of com.mongodb.event.ServerDescriptionChangedEvent in project mongo-java-driver by mongodb.

the class TestServer method sendNotification.

public void sendNotification(final ServerDescription newDescription) {
    ServerDescription currentDescription = description;
    description = newDescription;
    ServerDescriptionChangedEvent event = new ServerDescriptionChangedEvent(serverId, newDescription, currentDescription);
    if (cluster != null) {
        cluster.onChange(event);
    }
    if (serverListener != null) {
        serverListener.serverDescriptionChanged(event);
    }
}
Also used : ServerDescriptionChangedEvent(com.mongodb.event.ServerDescriptionChangedEvent) ServerDescription(com.mongodb.connection.ServerDescription)

Aggregations

ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)7 BsonDocument (org.bson.BsonDocument)4 ServerAddress (com.mongodb.ServerAddress)3 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)3 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)3 ServerClosedEvent (com.mongodb.event.ServerClosedEvent)3 ServerOpeningEvent (com.mongodb.event.ServerOpeningEvent)3 ServerDescription (com.mongodb.connection.ServerDescription)2 ServerId (com.mongodb.connection.ServerId)2 BsonValue (org.bson.BsonValue)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 ClusterDescription (com.mongodb.connection.ClusterDescription)1 ClusterId (com.mongodb.connection.ClusterId)1 ConnectionId (com.mongodb.connection.ConnectionId)1 ServerSettings (com.mongodb.connection.ServerSettings)1 ClusterClosedEvent (com.mongodb.event.ClusterClosedEvent)1 ServerHeartbeatFailedEvent (com.mongodb.event.ServerHeartbeatFailedEvent)1