Skip to content

DATAMONGO-1451 - Translate write concern timeouts to TransientDataAccessResourceException #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1451-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1451-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1451-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1451-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1451-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.11.0.BUILD-SNAPSHOT</version>
<version>1.11.0.DATAMONGO-1451-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 the original author or authors.
* Copyright 2010-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.dao.PermissionDeniedDataAccessException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.mongodb.BulkOperationException;
import org.springframework.data.mongodb.UncategorizedMongoDbException;
Expand All @@ -34,6 +35,7 @@

import com.mongodb.BulkWriteException;
import com.mongodb.MongoException;
import com.mongodb.WriteConcernException;

/**
* Simple {@link PersistenceExceptionTranslator} for Mongo. Convert the given runtime exception to an appropriate
Expand All @@ -43,6 +45,7 @@
* @author Oliver Gierke
* @author Michal Vich
* @author Christoph Strobl
* @author Mark Paluch
*/
public class MongoExceptionTranslator implements PersistenceExceptionTranslator {

Expand All @@ -65,6 +68,12 @@ public class MongoExceptionTranslator implements PersistenceExceptionTranslator
*/
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {

// Check for a timeout exception

if (ex instanceof WriteConcernException && ReflectiveWriteResultInvoker.wasTimeout((WriteConcernException) ex)) {
return new TransientDataAccessResourceException(ex.getMessage(), ex);
}

// Check for well-known MongoException subclasses.

String exception = ClassUtils.getShortName(ClassUtils.getUserClass(ex.getClass()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 the original author or authors.
* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,8 +19,10 @@
import static org.springframework.util.ReflectionUtils.*;

import java.lang.reflect.Method;
import java.util.Map;

import com.mongodb.MongoException;
import com.mongodb.WriteConcernException;
import com.mongodb.WriteResult;

/**
Expand All @@ -29,19 +31,24 @@
*
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
* @since 1.7
*/
final class ReflectiveWriteResultInvoker {

private static final Method GET_ERROR_METHOD;
private static final Method WAS_ACKNOWLEDGED_METHOD;
private static final Method GET_RESPONSE;
private static final Method GET_COMMAND_RESULT;

private ReflectiveWriteResultInvoker() {}

static {

GET_ERROR_METHOD = findMethod(WriteResult.class, "getError");
WAS_ACKNOWLEDGED_METHOD = findMethod(WriteResult.class, "wasAcknowledged");
GET_RESPONSE = findMethod(WriteConcernException.class, "getResponse");
GET_COMMAND_RESULT = findMethod(WriteConcernException.class, "getCommandResult");
}

/**
Expand All @@ -64,4 +71,29 @@ public static String getError(WriteResult writeResult) {
public static boolean wasAcknowledged(WriteResult writeResult) {
return isMongo3Driver() ? ((Boolean) invokeMethod(WAS_ACKNOWLEDGED_METHOD, writeResult)).booleanValue() : true;
}

/**
* @param writeConcernException
* @return return {@literal true} if the {@link WriteConcernException} indicates a write concern timeout as reason
* @since 1.10
*/
@SuppressWarnings("unchecked")
public static boolean wasTimeout(WriteConcernException writeConcernException) {

Map<Object, Object> response;
if (isMongo3Driver()) {
response = (Map<Object, Object>) invokeMethod(GET_RESPONSE, writeConcernException);
} else {
response = (Map<Object, Object>) invokeMethod(GET_COMMAND_RESULT, writeConcernException);
}

if (response != null && response.containsKey("wtimeout")) {
Object wtimeout = response.get("wtimeout");
if (wtimeout != null && wtimeout.toString().contains("true")) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.UnknownHostException;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -33,20 +38,25 @@
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.data.mongodb.UncategorizedMongoDbException;
import org.springframework.data.mongodb.util.MongoClientVersion;
import org.springframework.test.util.ReflectionTestUtils;

import com.mongodb.MongoCursorNotFoundException;
import com.mongodb.MongoException;
import com.mongodb.MongoInternalException;
import com.mongodb.MongoSocketException;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcernException;

/**
* Unit tests for {@link MongoExceptionTranslator}.
*
*
* @author Michal Vich
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class MongoExceptionTranslatorUnitTests {
Expand Down Expand Up @@ -133,6 +143,56 @@ public void translateMongoInternalException() {
expectExceptionWithCauseMessage(translatedException, InvalidDataAccessResourceUsageException.class);
}

@Test // DATAMONGO-1451
@SuppressWarnings("unchecked")
public void translateTimeoutToTransientDataAccessResourceExceptionWith2xDriver() throws Exception {

assumeThat(MongoClientVersion.isMongo3Driver(), is(false));

Constructor<?> constructor = Class.forName("com.mongodb.CommandResult").getDeclaredConstructor(ServerAddress.class);
constructor.setAccessible(true);

Map<String, Object> commandResult = (Map<String, Object>) constructor.newInstance(new ServerAddress("localhost"));
commandResult.put("wtimeout", true);
commandResult.put("ok", 1);
commandResult.put("n", 0);
commandResult.put("err", "waiting for replication timed out");
commandResult.put("code", 64);

DataAccessException translatedException = translator.translateExceptionIfPossible(
(RuntimeException) ReflectionTestUtils.invokeMethod(commandResult, "getException"));

expectExceptionWithCauseMessage(translatedException, TransientDataAccessResourceException.class);
}

@Test // DATAMONGO-1451
public void translateTimeoutToTransientDataAccessResourceExceptionWith3xDriver() throws Exception {

assumeThat(MongoClientVersion.isMongo3Driver(), is(true));

Class<?> bsonDocumentClass = Class.forName("org.bson.BsonDocument");

Method getWriteResult = Class.forName("com.mongodb.connection.ProtocolHelper").getDeclaredMethod("getWriteResult",
bsonDocumentClass, ServerAddress.class);

String response = "{ \"serverUsed\" : \"10.10.17.35:27017\" , \"ok\" : 1 , \"n\" : 0 , \"wtimeout\" : true , \"err\" : \"waiting for replication timed out\" , \"code\" : 64}";
Object bsonDocument = bsonDocumentClass.getDeclaredMethod("parse", String.class).invoke(null, response);

try {
getWriteResult.setAccessible(true);
getWriteResult.invoke(null, bsonDocument, new ServerAddress("localhost"));
fail("Missing Exception");
} catch (InvocationTargetException e) {

assertThat(e.getTargetException(), is(instanceOf(WriteConcernException.class)));

DataAccessException translatedException = translator
.translateExceptionIfPossible((RuntimeException) e.getTargetException());
expectExceptionWithCauseMessage(translatedException, TransientDataAccessResourceException.class);
}

}

@Test
public void translateUnsupportedException() {

Expand Down