Search in sources :

Example 1 with ConnectionId

use of com.mongodb.connection.ConnectionId in project mongo-java-driver by mongodb.

the class InternalStreamConnectionInitializer method applyGetLastErrorResult.

private InternalConnectionInitializationDescription applyGetLastErrorResult(final BsonDocument getLastErrorResult, final InternalConnectionInitializationDescription description) {
    ConnectionDescription connectionDescription = description.getConnectionDescription();
    ConnectionId connectionId;
    if (getLastErrorResult.containsKey("connectionId")) {
        connectionId = connectionDescription.getConnectionId().withServerValue(getLastErrorResult.getNumber("connectionId").intValue());
    } else {
        connectionId = connectionDescription.getConnectionId();
    }
    return description.withConnectionDescription(connectionDescription.withConnectionId(connectionId));
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) DescriptionHelper.createConnectionDescription(com.mongodb.internal.connection.DescriptionHelper.createConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId)

Example 2 with ConnectionId

use of com.mongodb.connection.ConnectionId 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 3 with ConnectionId

use of com.mongodb.connection.ConnectionId in project mongo-java-driver by mongodb.

the class InternalStreamConnectionInitializer method createInitializationDescription.

private InternalConnectionInitializationDescription createInitializationDescription(final BsonDocument helloResult, final InternalConnection internalConnection, final long startTime) {
    ConnectionId connectionId = internalConnection.getDescription().getConnectionId();
    ConnectionDescription connectionDescription = createConnectionDescription(clusterConnectionMode, connectionId, helloResult);
    ServerDescription serverDescription = createServerDescription(internalConnection.getDescription().getServerAddress(), helloResult, System.nanoTime() - startTime);
    return new InternalConnectionInitializationDescription(connectionDescription, serverDescription);
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) DescriptionHelper.createConnectionDescription(com.mongodb.internal.connection.DescriptionHelper.createConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) ServerDescription(com.mongodb.connection.ServerDescription) DescriptionHelper.createServerDescription(com.mongodb.internal.connection.DescriptionHelper.createServerDescription)

Example 4 with ConnectionId

use of com.mongodb.connection.ConnectionId in project mongo-java-driver by mongodb.

the class DescriptionHelper method createConnectionDescription.

static ConnectionDescription createConnectionDescription(final ClusterConnectionMode clusterConnectionMode, final ConnectionId connectionId, final BsonDocument helloResult) {
    ConnectionDescription connectionDescription = new ConnectionDescription(connectionId, getMaxWireVersion(helloResult), getServerType(helloResult), getMaxWriteBatchSize(helloResult), getMaxBsonObjectSize(helloResult), getMaxMessageSizeBytes(helloResult), getCompressors(helloResult), helloResult.getArray("saslSupportedMechs", null));
    if (helloResult.containsKey("connectionId")) {
        ConnectionId newConnectionId = connectionDescription.getConnectionId().withServerValue(helloResult.getNumber("connectionId").intValue());
        connectionDescription = connectionDescription.withConnectionId(newConnectionId);
    }
    if (clusterConnectionMode == ClusterConnectionMode.LOAD_BALANCED) {
        ObjectId serviceId = getServiceId(helloResult);
        if (serviceId != null) {
            connectionDescription = connectionDescription.withServiceId(serviceId);
        } else {
            throw new MongoClientException("Driver attempted to initialize in load balancing mode, but the server does not support " + "this mode");
        }
    }
    return connectionDescription;
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) MongoClientException(com.mongodb.MongoClientException) ObjectId(org.bson.types.ObjectId)

Example 5 with ConnectionId

use of com.mongodb.connection.ConnectionId in project mongo-java-driver by mongodb.

the class X509AuthenticatorNoUserNameTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescriptionThreeTwo = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 4, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
    connectionDescriptionThreeFour = new ConnectionDescription(new ConnectionId(new ServerId(new ClusterId(), new ServerAddress())), 5, ServerType.STANDALONE, 1000, 16000, 48000, Collections.<String>emptyList());
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) ServerId(com.mongodb.connection.ServerId) ClusterId(com.mongodb.connection.ClusterId) ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Aggregations

ConnectionId (com.mongodb.connection.ConnectionId)6 ConnectionDescription (com.mongodb.connection.ConnectionDescription)5 ServerAddress (com.mongodb.ServerAddress)2 ClusterId (com.mongodb.connection.ClusterId)2 ServerId (com.mongodb.connection.ServerId)2 DescriptionHelper.createConnectionDescription (com.mongodb.internal.connection.DescriptionHelper.createConnectionDescription)2 BsonDocument (org.bson.BsonDocument)2 Span (brave.Span)1 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)1 MongoClientException (com.mongodb.MongoClientException)1 MongoSocketException (com.mongodb.MongoSocketException)1 ClusterDescription (com.mongodb.connection.ClusterDescription)1 ServerDescription (com.mongodb.connection.ServerDescription)1 ClusterClosedEvent (com.mongodb.event.ClusterClosedEvent)1 ClusterDescriptionChangedEvent (com.mongodb.event.ClusterDescriptionChangedEvent)1 ClusterOpeningEvent (com.mongodb.event.ClusterOpeningEvent)1 ServerClosedEvent (com.mongodb.event.ServerClosedEvent)1 ServerDescriptionChangedEvent (com.mongodb.event.ServerDescriptionChangedEvent)1 ServerHeartbeatFailedEvent (com.mongodb.event.ServerHeartbeatFailedEvent)1 ServerHeartbeatStartedEvent (com.mongodb.event.ServerHeartbeatStartedEvent)1