Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ static Throwable chooseRetryableWriteException(
return mostRecentAttemptException.getCause();
}
return mostRecentAttemptException;
} else if (mostRecentAttemptException instanceof ResourceSupplierInternalException) {
} else if (mostRecentAttemptException instanceof ResourceSupplierInternalException
|| (mostRecentAttemptException instanceof MongoException
&& ((MongoException)mostRecentAttemptException).hasErrorLabel(NO_WRITES_PERFORMED_ERROR_LABEL))) {
return previouslyChosenException;
} else {
return mostRecentAttemptException;
Expand Down Expand Up @@ -571,6 +573,7 @@ private static boolean isRetryWritesEnabled(@Nullable final BsonDocument command
}

static final String RETRYABLE_WRITE_ERROR_LABEL = "RetryableWriteError";
private static final String NO_WRITES_PERFORMED_ERROR_LABEL = "NoWritesPerformed";

private static boolean decideRetryableAndAddRetryableWriteErrorLabel(final Throwable t, @Nullable final Integer maxWireVersion) {
if (!(t instanceof MongoException)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ public void poolClearedExceptionMustBeRetryable() throws InterruptedException, E
mongoCollection -> mongoCollection.insertOne(new Document()), "insert", true);
}

/**
* Prose test #3.
*/
@Test
public void originalErrorMustBePropagatedIfNoWritesPerformed() throws InterruptedException {
com.mongodb.client.RetryableWritesProseTest.originalErrorMustBePropagatedIfNoWritesPerformed(
mongoClientSettings -> new SyncMongoClient(MongoClients.create(mongoClientSettings)));
}

private boolean canRunTests() {
Document storageEngine = (Document) getServerStatus().get("storageEngine");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import com.mongodb.Function;
import com.mongodb.MongoClientException;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoException;
import com.mongodb.ServerAddress;
import com.mongodb.assertions.Assertions;
import com.mongodb.event.CommandFailedEvent;
import com.mongodb.event.CommandListener;
import com.mongodb.event.ConnectionCheckOutFailedEvent;
import com.mongodb.event.ConnectionCheckedOutEvent;
import com.mongodb.event.ConnectionPoolClearedEvent;
Expand All @@ -34,12 +39,17 @@
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import static com.mongodb.ClusterFixture.getServerStatus;
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet;
Expand All @@ -55,6 +65,7 @@
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

Expand Down Expand Up @@ -138,7 +149,6 @@ public static <R> void poolClearedExceptionMustBeRetryable(
* As a result, the client has to wait for at least its heartbeat delay until it hears back from a server
* (while it waits for a response, calling `ServerMonitor.connect` has no effect).
* Thus, we want to use small heartbeat delay to reduce delays in the test. */
.minHeartbeatFrequency(50, TimeUnit.MILLISECONDS)
.heartbeatFrequency(50, TimeUnit.MILLISECONDS))
.retryReads(true)
.retryWrites(true)
Expand Down Expand Up @@ -179,6 +189,64 @@ public static <R> void poolClearedExceptionMustBeRetryable(
}
}

/**
* Prose test #3.
*/
@Test
public void originalErrorMustBePropagatedIfNoWritesPerformed() throws InterruptedException {
originalErrorMustBePropagatedIfNoWritesPerformed(MongoClients::create);
}

public static void originalErrorMustBePropagatedIfNoWritesPerformed(
final Function<MongoClientSettings, MongoClient> clientCreator) throws InterruptedException {
assumeTrue(serverVersionAtLeast(6, 0) && isDiscoverableReplicaSet());
BiFunction<Integer, List<String>, BsonDocument> configureFailPointDocCreator = (errorCode, errorLabels) ->
new BsonDocument()
.append("configureFailPoint", new BsonString("failCommand"))
.append("mode", new BsonDocument()
.append("times", new BsonInt32(1)))
.append("data", new BsonDocument()
.append("failCommands", new BsonArray(singletonList(new BsonString("insert"))))
.append("errorCode", new BsonInt32(errorCode))
.append("errorLabels", new BsonArray(errorLabels.stream().map(BsonString::new).collect(Collectors.toList()))));
ServerAddress primaryServerAddress = Fixture.getPrimary();
CompletableFuture<FailPoint> futureFailPoint = new CompletableFuture<>();
CommandListener commandListener = new CommandListener() {
private final AtomicBoolean configureFailPoint = new AtomicBoolean(true);

@Override
public void commandFailed(final CommandFailedEvent event) {
if (event.getCommandName().equals("insert") && configureFailPoint.compareAndSet(true, false)) {
Assertions.assertTrue(futureFailPoint.complete(FailPoint.enable(
configureFailPointDocCreator.apply(10107, asList("RetryableWriteError", "NoWritesPerformed")),
primaryServerAddress
)));
}
}
};
try (MongoClient client = clientCreator.apply(getMongoClientSettingsBuilder()
.retryWrites(true)
.addCommandListener(commandListener)
.applyToServerSettings(builder ->
// see `poolClearedExceptionMustBeRetryable` for the explanation
builder.heartbeatFrequency(50, TimeUnit.MILLISECONDS))
.build());
FailPoint ignored = FailPoint.enable(configureFailPointDocCreator.apply(91, singletonList("RetryableWriteError")), client)) {
MongoCollection<Document> collection = client.getDatabase(getDefaultDatabaseName())
.getCollection("originalErrorMustBePropagatedIfErrorWithRetryableWriteErrorLabelHappens");
collection.drop();
try {
collection.insertOne(new Document());
} catch (MongoCommandException e) {
assertEquals(e.getErrorCode(), 91);
return;
}
fail("must not reach");
} finally {
futureFailPoint.thenAccept(FailPoint::close);
}
}

private boolean canRunTests() {
Document storageEngine = (Document) getServerStatus().get("storageEngine");

Expand Down