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));
}
}
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");
}
}
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);
}
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);
}
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);
}
}
Aggregations