Search in sources :

Example 1 with MongoSocketException

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

the class NettyStream method initializeChannel.

private void initializeChannel(final AsyncCompletionHandler<Void> handler, final Queue<SocketAddress> socketAddressQueue) {
    if (socketAddressQueue.isEmpty()) {
        handler.failed(new MongoSocketException("Exception opening socket", getAddress()));
    } else {
        SocketAddress nextAddress = socketAddressQueue.poll();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workerGroup);
        bootstrap.channel(socketChannelClass);
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, settings.getConnectTimeout(MILLISECONDS));
        bootstrap.option(ChannelOption.TCP_NODELAY, true);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        if (settings.getReceiveBufferSize() > 0) {
            bootstrap.option(ChannelOption.SO_RCVBUF, settings.getReceiveBufferSize());
        }
        if (settings.getSendBufferSize() > 0) {
            bootstrap.option(ChannelOption.SO_SNDBUF, settings.getSendBufferSize());
        }
        bootstrap.option(ChannelOption.ALLOCATOR, allocator);
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(final SocketChannel ch) {
                ChannelPipeline pipeline = ch.pipeline();
                if (sslSettings.isEnabled()) {
                    addSslHandler(ch);
                }
                int readTimeout = settings.getReadTimeout(MILLISECONDS);
                if (readTimeout > NO_SCHEDULE_TIME) {
                    readTimeoutMillis = readTimeout;
                    /* We need at least one handler before (in the inbound evaluation order) the InboundBufferHandler,
                         * so that we can fire exception events (they are inbound events) using its context and the InboundBufferHandler
                         * receives them. SslHandler is not always present, so adding a NOOP handler.*/
                    pipeline.addLast(new ChannelInboundHandlerAdapter());
                    readTimeoutTask = new ReadTimeoutTask(pipeline.lastContext());
                }
                pipeline.addLast(new InboundBufferHandler());
            }
        });
        final ChannelFuture channelFuture = bootstrap.connect(nextAddress);
        channelFuture.addListener(new OpenChannelFutureListener(socketAddressQueue, channelFuture, handler));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) MongoSocketException(com.mongodb.MongoSocketException) Bootstrap(io.netty.bootstrap.Bootstrap) SocketAddress(java.net.SocketAddress) ChannelPipeline(io.netty.channel.ChannelPipeline) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 2 with MongoSocketException

use of com.mongodb.MongoSocketException in project jackrabbit-oak by apache.

the class MongoUtilsTest method documentStoreExceptionType.

@Test
public void documentStoreExceptionType() {
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new IOException()));
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new MongoException("message")));
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(newMongoCommandException(42)));
    assertEquals(GENERIC, getDocumentStoreExceptionTypeFor(new DuplicateKeyException(response(11000), new ServerAddress(), null)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11600)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11601)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newWriteConcernException(11602)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11600)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11601)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(newMongoCommandException(11602)));
    assertEquals(TRANSIENT, getDocumentStoreExceptionTypeFor(new MongoSocketException("message", new ServerAddress())));
}
Also used : MongoException(com.mongodb.MongoException) ServerAddress(com.mongodb.ServerAddress) MongoSocketException(com.mongodb.MongoSocketException) IOException(java.io.IOException) DuplicateKeyException(com.mongodb.DuplicateKeyException) Test(org.junit.Test)

Example 3 with MongoSocketException

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

the class AsynchronousSocketChannelStream method initializeSocketChannel.

private void initializeSocketChannel(final AsyncCompletionHandler<Void> handler, final Queue<SocketAddress> socketAddressQueue) {
    if (socketAddressQueue.isEmpty()) {
        handler.failed(new MongoSocketException("Exception opening socket", serverAddress));
    } else {
        SocketAddress socketAddress = socketAddressQueue.poll();
        try {
            AsynchronousSocketChannel attemptConnectionChannel = AsynchronousSocketChannel.open(group);
            attemptConnectionChannel.setOption(StandardSocketOptions.TCP_NODELAY, true);
            attemptConnectionChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
            if (settings.getReceiveBufferSize() > 0) {
                attemptConnectionChannel.setOption(StandardSocketOptions.SO_RCVBUF, settings.getReceiveBufferSize());
            }
            if (settings.getSendBufferSize() > 0) {
                attemptConnectionChannel.setOption(StandardSocketOptions.SO_SNDBUF, settings.getSendBufferSize());
            }
            attemptConnectionChannel.connect(socketAddress, null, new OpenCompletionHandler(handler, socketAddressQueue, attemptConnectionChannel));
        } catch (IOException e) {
            handler.failed(new MongoSocketOpenException("Exception opening socket", serverAddress, e));
        } catch (Throwable t) {
            handler.failed(t);
        }
    }
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) MongoSocketException(com.mongodb.MongoSocketException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) MongoSocketOpenException(com.mongodb.MongoSocketOpenException)

Example 4 with MongoSocketException

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

the class ErrorMatcher method assertErrorsMatch.

void assertErrorsMatch(final BsonDocument expectedError, final Exception e) {
    context.push(ContextElement.ofError(expectedError, e));
    assertTrue(context.getMessage("Unexpected field in expectError.  One of " + expectedError.keySet()), EXPECTED_ERROR_FIELDS.containsAll(expectedError.keySet()));
    if (expectedError.containsKey("isError")) {
        assertTrue(context.getMessage("isError must be true"), expectedError.getBoolean("isError").getValue());
    }
    if (expectedError.containsKey("isClientError")) {
        assertEquals(context.getMessage("Exception must be of type MongoClientException or IllegalArgumentException" + " or IllegalStateException or MongoSocketException"), expectedError.getBoolean("isClientError").getValue(), e instanceof MongoClientException || e instanceof IllegalArgumentException || e instanceof IllegalStateException || e instanceof MongoSocketException);
    }
    if (expectedError.containsKey("errorContains")) {
        String errorContains = expectedError.getString("errorContains").getValue();
        assertTrue(context.getMessage("Error message does not contain expected string: " + errorContains), e.getMessage().contains(errorContains));
    }
    if (expectedError.containsKey("errorCode")) {
        assertTrue(context.getMessage("Exception must be of type MongoCommandException or MongoQueryException when checking" + " for error codes"), e instanceof MongoCommandException || e instanceof MongoQueryException);
        int errorCode = (e instanceof MongoCommandException) ? ((MongoCommandException) e).getErrorCode() : ((MongoQueryException) e).getErrorCode();
        assertEquals(context.getMessage("Error codes must match"), expectedError.getNumber("errorCode").intValue(), errorCode);
    }
    if (expectedError.containsKey("errorCodeName")) {
        assertTrue(context.getMessage("Exception must be of type MongoCommandException when checking for error codes"), e instanceof MongoCommandException);
        MongoCommandException mongoCommandException = (MongoCommandException) e;
        assertEquals(context.getMessage("Error code names must match"), expectedError.getString("errorCodeName").getValue(), mongoCommandException.getErrorCodeName());
    }
    if (expectedError.containsKey("errorLabelsOmit")) {
        assertTrue(context.getMessage("Exception must be of type MongoException when checking for error labels"), e instanceof MongoException);
        MongoException mongoException = (MongoException) e;
        for (BsonValue cur : expectedError.getArray("errorLabelsOmit")) {
            assertFalse(context.getMessage("Expected error label to be omitted but it is not: " + cur.asString().getValue()), mongoException.hasErrorLabel(cur.asString().getValue()));
        }
    }
    if (expectedError.containsKey("errorLabelsContain")) {
        assertTrue(context.getMessage("Exception must be of type MongoException when checking for error labels"), e instanceof MongoException);
        MongoException mongoException = (MongoException) e;
        for (BsonValue cur : expectedError.getArray("errorLabelsContain")) {
            assertTrue(context.getMessage("Expected error label: " + cur.asString().getValue()), mongoException.hasErrorLabel(cur.asString().getValue()));
        }
    }
    if (expectedError.containsKey("expectResult")) {
        // MongoBulkWriteException does not include information about the successful writes, so this is the only check
        // that can currently be done
        assertTrue(context.getMessage("Exception must be of type MongoBulkWriteException when checking for results"), e instanceof MongoBulkWriteException);
    }
    context.pop();
}
Also used : MongoException(com.mongodb.MongoException) MongoClientException(com.mongodb.MongoClientException) MongoCommandException(com.mongodb.MongoCommandException) MongoSocketException(com.mongodb.MongoSocketException) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) MongoQueryException(com.mongodb.MongoQueryException) BsonValue(org.bson.BsonValue)

Example 5 with MongoSocketException

use of com.mongodb.MongoSocketException in project brave by openzipkin.

the class TraceMongoCommandListener method commandStarted.

/**
 * Uses {@link ThreadLocalSpan} as there's no attribute namespace shared between callbacks, but
 * all callbacks happen on the same thread.
 */
@Override
public void commandStarted(CommandStartedEvent event) {
    String databaseName = event.getDatabaseName();
    // don't trace commands like "endSessions"
    if ("admin".equals(databaseName))
        return;
    Span span = threadLocalSpan.next();
    if (span == null || span.isNoop())
        return;
    String commandName = event.getCommandName();
    BsonDocument command = event.getCommand();
    String collectionName = getCollectionName(command, commandName);
    span.name(getSpanName(commandName, collectionName)).kind(CLIENT).remoteServiceName("mongodb-" + databaseName).tag("mongodb.command", commandName);
    if (collectionName != null) {
        span.tag("mongodb.collection", collectionName);
    }
    ConnectionDescription connectionDescription = event.getConnectionDescription();
    if (connectionDescription != null) {
        ConnectionId connectionId = connectionDescription.getConnectionId();
        if (connectionId != null) {
            span.tag("mongodb.cluster_id", connectionId.getServerId().getClusterId().getValue());
        }
        try {
            InetSocketAddress socketAddress = connectionDescription.getServerAddress().getSocketAddress();
            span.remoteIpAndPort(socketAddress.getAddress().getHostAddress(), socketAddress.getPort());
        } catch (MongoSocketException ignored) {
        }
    }
    span.start();
}
Also used : ConnectionDescription(com.mongodb.connection.ConnectionDescription) ConnectionId(com.mongodb.connection.ConnectionId) BsonDocument(org.bson.BsonDocument) InetSocketAddress(java.net.InetSocketAddress) MongoSocketException(com.mongodb.MongoSocketException) Span(brave.Span) ThreadLocalSpan(brave.propagation.ThreadLocalSpan)

Aggregations

MongoSocketException (com.mongodb.MongoSocketException)5 MongoException (com.mongodb.MongoException)2 IOException (java.io.IOException)2 SocketAddress (java.net.SocketAddress)2 Span (brave.Span)1 ThreadLocalSpan (brave.propagation.ThreadLocalSpan)1 DuplicateKeyException (com.mongodb.DuplicateKeyException)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 MongoClientException (com.mongodb.MongoClientException)1 MongoCommandException (com.mongodb.MongoCommandException)1 MongoQueryException (com.mongodb.MongoQueryException)1 MongoSocketOpenException (com.mongodb.MongoSocketOpenException)1 ServerAddress (com.mongodb.ServerAddress)1 ConnectionDescription (com.mongodb.connection.ConnectionDescription)1 ConnectionId (com.mongodb.connection.ConnectionId)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 SocketChannel (io.netty.channel.socket.SocketChannel)1