use of com.microsoft.azure.cosmosdb.AccessCondition in project ambry by linkedin.
the class CosmosDataAccessor method updateContainerDeletionEntry.
/**
* Update the container deletion entry document in the CosmosDB collection.
* @param containerId the container id for which document is replaced.
* @param accountId the account id for which document is replaced.
* @param updateFields {@link BiConsumer} object to use as callback to update the required fields.
* @return the {@link ResourceResponse} returned by the operation, if successful.
* Returns {@Null} if the field already has the specified value.
* @throws DocumentClientException if the record was not found or if the operation failed.
*/
ResourceResponse<Document> updateContainerDeletionEntry(short containerId, short accountId, BiConsumer<Document, AtomicBoolean> updateFields) throws DocumentClientException {
// Read the existing record
String id = CosmosContainerDeletionEntry.generateContainerDeletionEntryId(accountId, containerId);
String docLink = getContainerDeletionEntryDocumentLink(id);
RequestOptions options = getRequestOptions(id);
ResourceResponse<Document> readResponse = executeCosmosAction(() -> asyncDocumentClient.readDocument(docLink, options).toBlocking().single(), azureMetrics.continerDeletionEntryReadTime);
Document doc = readResponse.getResource();
AtomicBoolean fieldsChanged = new AtomicBoolean(false);
updateFields.accept(doc, fieldsChanged);
if (!fieldsChanged.get()) {
logger.debug("No change in value for container deletion entry {}", doc.toJson());
return null;
}
// For testing conflict handling
if (updateCallback != null) {
try {
updateCallback.call();
} catch (Exception ex) {
logger.error("Error in update callback", ex);
}
}
// Set condition to ensure we don't clobber a concurrent update
AccessCondition accessCondition = new AccessCondition();
accessCondition.setCondition(doc.getETag());
options.setAccessCondition(accessCondition);
try {
return executeCosmosAction(() -> asyncDocumentClient.replaceDocument(doc, options).toBlocking().single(), azureMetrics.documentUpdateTime);
} catch (DocumentClientException e) {
if (e.getStatusCode() == HttpConstants.StatusCodes.PRECONDITION_FAILED) {
azureMetrics.blobUpdateConflictCount.inc();
}
throw e;
}
}
use of com.microsoft.azure.cosmosdb.AccessCondition in project ambry by linkedin.
the class CosmosDataAccessor method updateMetadata.
/**
* Update the blob metadata document in the CosmosDB collection.
* @param blobId the {@link BlobId} for which metadata is replaced.
* @param updateFields Map of field names and new values to update.
* @return the {@link ResourceResponse} returned by the operation, if successful.
* Returns {@Null} if the field already has the specified value.
* @throws DocumentClientException if the record was not found or if the operation failed.
*/
ResourceResponse<Document> updateMetadata(BlobId blobId, Map<String, String> updateFields) throws DocumentClientException {
// Read the existing record
String docLink = getDocumentLink(blobId.getID());
RequestOptions options = getRequestOptions(blobId.getPartition().toPathString());
ResourceResponse<Document> readResponse = executeCosmosAction(() -> asyncDocumentClient.readDocument(docLink, options).toBlocking().single(), azureMetrics.documentReadTime);
Document doc = readResponse.getResource();
// Update only if value has changed
Map<String, String> fieldsToUpdate = updateFields.entrySet().stream().filter(map -> !String.valueOf(updateFields.get(map.getKey())).equals(doc.get(map.getKey()))).collect(Collectors.toMap(Map.Entry::getKey, map -> String.valueOf(map.getValue())));
if (fieldsToUpdate.size() == 0) {
logger.debug("No change in value for {} in blob {}", updateFields.keySet(), blobId.getID());
return null;
}
// For testing conflict handling
if (updateCallback != null) {
try {
updateCallback.call();
} catch (Exception ex) {
logger.error("Error in update callback", ex);
}
}
// Perform the update
fieldsToUpdate.forEach((key, value) -> doc.set(key, value));
// Set condition to ensure we don't clobber a concurrent update
AccessCondition accessCondition = new AccessCondition();
accessCondition.setCondition(doc.getETag());
options.setAccessCondition(accessCondition);
try {
return executeCosmosAction(() -> asyncDocumentClient.replaceDocument(doc, options).toBlocking().single(), azureMetrics.documentUpdateTime);
} catch (DocumentClientException e) {
if (e.getStatusCode() == HttpConstants.StatusCodes.PRECONDITION_FAILED) {
azureMetrics.blobUpdateConflictCount.inc();
}
throw e;
}
}
Aggregations