Skip to content
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

Making CosmosPatchOperations thread-safe #29143

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,13 @@ private <T> Mono<CosmosItemResponse<T>> patchItemHelper(String itemId,
Class<T> itemType) {
this.setRequestHeaders(options);
List<Mono<PatchOperation>> monoList = new ArrayList<>();
for (PatchOperation patchOperation : cosmosPatchOperationsAccessor.getPatchOperations(cosmosPatchOperations)) {
List<PatchOperation> operations = cosmosPatchOperationsAccessor.getPatchOperations(cosmosPatchOperations);
List<PatchOperation> operationsSnapshot;
synchronized (operations) {
operationsSnapshot = new ArrayList<>(operations);
}

for (PatchOperation patchOperation : operationsSnapshot) {
Mono<PatchOperation> itemPatchOperationMono = null;
if (patchOperation.getOperationType() == PatchOperationType.REMOVE) {
itemPatchOperationMono = Mono.just(patchOperation);
Expand Down Expand Up @@ -596,8 +602,15 @@ else if (patchOperation instanceof PatchOperationCore) {
CosmosPatchOperations encryptedCosmosPatchOperations = CosmosPatchOperations.create();

return encryptedPatchOperationsListMono.flatMap(patchOperations -> {
cosmosPatchOperationsAccessor.getPatchOperations(encryptedCosmosPatchOperations).addAll(patchOperations);
return patchItemInternalHelper(itemId, partitionKey, encryptedCosmosPatchOperations, finalRequestOptions,itemType, false);
List<PatchOperation> snapshot =
cosmosPatchOperationsAccessor.getPatchOperations(encryptedCosmosPatchOperations);

synchronized(snapshot) {
snapshot.addAll(patchOperations);
}

return patchItemInternalHelper(
itemId, partitionKey, encryptedCosmosPatchOperations, finalRequestOptions,itemType, false);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import com.azure.cosmos.implementation.patch.PatchOperationType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkArgument;
import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull;

/**
* Grammar is a super set of this RFC: https://tools.ietf.org/html/rfc6902#section-4.1
Expand Down Expand Up @@ -49,7 +49,7 @@ public final class CosmosPatchOperations {
private final List<PatchOperation> patchOperations;

private CosmosPatchOperations() {
this.patchOperations = new ArrayList<>();
this.patchOperations = Collections.synchronizedList(new ArrayList<>());
}

/**
Expand Down Expand Up @@ -255,8 +255,12 @@ public CosmosPatchOperations increment(String path, double value) {
return this;
}

// NOTE returning this patchOperations means any
// modifications - like adding new entries is still
// thread-safe - but enumerating over the collection is not
// unless synchronized
List<PatchOperation> getPatchOperations() {
return patchOperations;
return this.patchOperations;
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down