Skip to content

Commit 11f55c6

Browse files
committed
Implement
JAVA-5528
1 parent 6b77f78 commit 11f55c6

13 files changed

+1152
-19
lines changed

driver-core/src/main/com/mongodb/ClientBulkWriteException.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Optional;
2626

2727
import static com.mongodb.assertions.Assertions.isTrueArgument;
28+
import static com.mongodb.assertions.Assertions.notNull;
29+
import static com.mongodb.internal.operation.ClientBulkWriteOperation.Exceptions.serverAddressFromException;
2830
import static java.util.Collections.emptyList;
2931
import static java.util.Collections.emptyMap;
3032
import static java.util.Collections.unmodifiableList;
@@ -56,16 +58,23 @@ public final class ClientBulkWriteException extends MongoServerException {
5658
* @param writeErrors The {@linkplain #getWriteErrors() write errors}.
5759
* @param partialResult The {@linkplain #getPartialResult() partial result}.
5860
* @param serverAddress The {@linkplain MongoServerException#getServerAddress() server address}.
61+
* If {@code error} is a {@link MongoServerException} or a {@link MongoSocketException}, then {@code serverAddress}
62+
* must be equal to the {@link ServerAddress} they bear.
5963
*/
6064
public ClientBulkWriteException(
6165
@Nullable final MongoException error,
6266
@Nullable final List<WriteConcernError> writeConcernErrors,
6367
@Nullable final Map<Long, WriteError> writeErrors,
6468
@Nullable final ClientBulkWriteResult partialResult,
6569
final ServerAddress serverAddress) {
66-
super(message(error, writeConcernErrors, writeErrors, partialResult, serverAddress), serverAddress);
70+
super(
71+
message(
72+
error, writeConcernErrors, writeErrors, partialResult,
73+
notNull("serverAddress", serverAddress)),
74+
validateServerAddress(error, serverAddress));
6775
// BULK-TODO Should ClientBulkWriteException.getCode be the same as error.getCode,
6876
// and getErrorLabels/hasErrorLabel contain the same labels as error.getErrorLabels?
77+
// TRANSIENT_TRANSACTION_ERROR_LABEL, UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL, RETRYABLE_WRITE_ERROR_LABEL, NO_WRITES_PERFORMED_ERROR_LABEL
6978
isTrueArgument("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty",
7079
!(writeConcernErrors == null || writeConcernErrors.isEmpty())
7180
|| !(writeErrors == null || writeErrors.isEmpty())
@@ -89,6 +98,14 @@ private static String message(
8998
+ (partialResult == null ? "" : " Partial result: " + partialResult + ".");
9099
}
91100

101+
private static ServerAddress validateServerAddress(@Nullable final MongoException error, final ServerAddress serverAddress) {
102+
serverAddressFromException(error).ifPresent(serverAddressFromError ->
103+
isTrueArgument("`serverAddress` must be equal to that of the `error`", serverAddressFromError.equals(serverAddress)));
104+
return error instanceof MongoServerException
105+
? ((MongoServerException) error).getServerAddress()
106+
: serverAddress;
107+
}
108+
92109
/**
93110
* The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error},
94111
* nor is an {@linkplain #getWriteErrors() error of an individual write operation}.

driver-core/src/main/com/mongodb/internal/operation/AsyncOperationHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ static <R> AsyncCallbackSupplier<R> decorateReadWithRetriesAsync(final RetryStat
322322
static <R> AsyncCallbackSupplier<R> decorateWriteWithRetriesAsync(final RetryState retryState, final OperationContext operationContext,
323323
final AsyncCallbackSupplier<R> asyncWriteFunction) {
324324
return new RetryingAsyncCallbackSupplier<>(retryState, onRetryableWriteAttemptFailure(operationContext),
325-
CommandOperationHelper::shouldAttemptToRetryWrite, callback -> {
325+
CommandOperationHelper::shouldAttemptToRetryWriteAndAddRetryableLabel, callback -> {
326326
logRetryExecute(retryState, operationContext);
327327
asyncWriteFunction.get(callback);
328328
});

driver-core/src/main/com/mongodb/internal/operation/BsonDocumentWrapperHelper.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.mongodb.internal.operation;
1818

19+
import org.bson.BsonArray;
1920
import org.bson.BsonDocument;
2021
import org.bson.BsonDocumentWrapper;
2122

@@ -25,7 +26,12 @@ final class BsonDocumentWrapperHelper {
2526

2627
@SuppressWarnings("unchecked")
2728
static <T> List<T> toList(final BsonDocument result, final String fieldContainingWrappedArray) {
28-
return ((BsonArrayWrapper<T>) result.getArray(fieldContainingWrappedArray)).getWrappedArray();
29+
// BULK-TODO why does the expectation of this code fails?
30+
BsonArray array = result.getArray(fieldContainingWrappedArray);
31+
if (array instanceof BsonArrayWrapper) {
32+
return ((BsonArrayWrapper<T>) array).getWrappedArray();
33+
}
34+
return (List<T>) array.getValues();
2935
}
3036

3137
@SuppressWarnings("unchecked")

driver-core/src/main/com/mongodb/internal/operation/BulkWriteBatch.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static BulkWriteBatch createBulkWriteBatch(final MongoNamespace namespace,
111111
}
112112
if (canRetryWrites && !writeRequestsAreRetryable) {
113113
canRetryWrites = false;
114-
LOGGER.debug("retryWrites set but one or more writeRequests do not support retryable writes");
114+
logWriteModelDoesNotSupportRetries();
115115
}
116116
return new BulkWriteBatch(namespace, connectionDescription, ordered, writeConcern, bypassDocumentValidation,
117117
canRetryWrites, new BulkWriteBatchCombiner(connectionDescription.getServerAddress(), ordered, writeConcern),
@@ -385,4 +385,8 @@ private static boolean isRetryable(final WriteRequest writeRequest) {
385385
}
386386
return true;
387387
}
388+
389+
static void logWriteModelDoesNotSupportRetries() {
390+
LOGGER.debug("retryWrites set but one or more writeRequests do not support retryable writes");
391+
}
388392
}

0 commit comments

Comments
 (0)