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