use of com.microsoft.azure.cosmosdb.Document in project ambry by linkedin.
the class AzureTestUtils method getMockedObservableForSingleResource.
/**
* Utility to mock the call chain to get mocked {@link Observable} for single resource from {@link AsyncDocumentClient}.
* @param metadata the {@link CloudBlobMetadata} to return as a document.
* @return {@link Observable< ResourceResponse <Document>>} object.
*/
static Observable<ResourceResponse<Document>> getMockedObservableForSingleResource(CloudBlobMetadata metadata) throws IOException {
Observable<ResourceResponse<Document>> mockResponse = mock(Observable.class);
BlockingObservable<ResourceResponse<Document>> mockBlockingObservable = mock(BlockingObservable.class);
when(mockResponse.toBlocking()).thenReturn(mockBlockingObservable);
ResourceResponse<Document> mockResourceResponse = mock(ResourceResponse.class);
when(mockBlockingObservable.single()).thenReturn(mockResourceResponse);
Document metadataDoc = createDocumentFromCloudBlobMetadata(metadata);
when(mockResourceResponse.getResource()).thenReturn(metadataDoc);
return mockResponse;
}
use of com.microsoft.azure.cosmosdb.Document in project ambry by linkedin.
the class AzureTestUtils method createDocumentFromCloudBlobMetadata.
/**
* Create {@link Document} object from {@link CloudBlobMetadata} object.
* @param cloudBlobMetadata {@link CloudBlobMetadata} object.
* @return {@link Document} object.
*/
static Document createDocumentFromCloudBlobMetadata(CloudBlobMetadata cloudBlobMetadata) throws IOException {
Document document = new Document(objectMapper.writeValueAsString(cloudBlobMetadata));
document.set(CosmosDataAccessor.COSMOS_LAST_UPDATED_COLUMN, System.currentTimeMillis());
return document;
}
use of com.microsoft.azure.cosmosdb.Document in project ambry by linkedin.
the class AzureTestUtils method createDocumentFromCloudBlobMetadata.
/**
* Create {@link Document} object from {@link CloudBlobMetadata} object with specified updateTime.
* @param cloudBlobMetadata {@link CloudBlobMetadata} object.
* @param uploadTime specified upload time.
* @return {@link Document} object.
*/
static Document createDocumentFromCloudBlobMetadata(CloudBlobMetadata cloudBlobMetadata, long uploadTime) throws JsonProcessingException {
Document document = new Document(objectMapper.writeValueAsString(cloudBlobMetadata));
document.set(CosmosDataAccessor.COSMOS_LAST_UPDATED_COLUMN, uploadTime);
return document;
}
use of com.microsoft.azure.cosmosdb.Document in project ambry by linkedin.
the class CosmosDataAccessor method getContainerBlobs.
/**
* Get the list of blobs in the specified partition that belong to the specified container.
* @param partitionPath the partition to query.
* @param accountId account id of the container.
* @param containerId container id of the container.
* @param queryLimit max number of blobs to return.
* @return a List of {@link CloudBlobMetadata} referencing the blobs belonging to the deprecated containers.
* @throws DocumentClientException in case of any error.
*/
List<CloudBlobMetadata> getContainerBlobs(String partitionPath, short accountId, short containerId, int queryLimit) throws DocumentClientException {
SqlQuerySpec querySpec = new SqlQuerySpec(CONTAINER_BLOBS_QUERY, new SqlParameterCollection(new SqlParameter(LIMIT_PARAM, queryLimit), new SqlParameter(CONTAINER_ID_PARAM, containerId), new SqlParameter(ACCOUNT_ID_PARAM, accountId)));
FeedOptions feedOptions = new FeedOptions();
feedOptions.setMaxItemCount(queryLimit);
feedOptions.setResponseContinuationTokenLimitInKb(continuationTokenLimitKb);
feedOptions.setPartitionKey(new PartitionKey(partitionPath));
try {
Iterator<FeedResponse<Document>> iterator = executeCosmosQuery(partitionPath, querySpec, feedOptions, azureMetrics.deletedContainerBlobsQueryTime).getIterator();
List<CloudBlobMetadata> containerBlobsList = new ArrayList<>();
double requestCharge = 0.0;
while (iterator.hasNext()) {
FeedResponse<Document> response = iterator.next();
requestCharge += response.getRequestCharge();
response.getResults().iterator().forEachRemaining(doc -> containerBlobsList.add(createMetadataFromDocument(doc)));
}
if (requestCharge >= requestChargeThreshold) {
logger.info("Deleted container blobs query partition {} containerId {} accountId {} request charge {} for {} records", partitionPath, containerId, accountId, requestCharge, containerBlobsList.size());
}
return containerBlobsList;
} catch (RuntimeException rex) {
if (rex.getCause() instanceof DocumentClientException) {
logger.warn("Dead blobs query {} partition {} got {}", querySpec.getQueryText(), partitionPath, ((DocumentClientException) rex.getCause()).getStatusCode());
throw (DocumentClientException) rex.getCause();
}
throw rex;
}
}
use of com.microsoft.azure.cosmosdb.Document in project ambry by linkedin.
the class CosmosDataAccessor method bulkDeleteMetadata.
/**
* Delete the blob metadata documents from CosmosDB using the BulkDelete stored procedure.
* @param blobMetadataList the list of blob metadata documents to delete.
* @return the number of documents deleted.
* @throws DocumentClientException if the operation failed.
*/
private int bulkDeleteMetadata(List<CloudBlobMetadata> blobMetadataList) throws DocumentClientException {
String partitionPath = blobMetadataList.get(0).getPartitionId();
RequestOptions options = getRequestOptions(partitionPath);
// stored proc link provided in config. Test for it at startup and use if available.
String quotedBlobIds = blobMetadataList.stream().map(metadata -> '"' + metadata.getId() + '"').collect(Collectors.joining(","));
String query = String.format(BULK_DELETE_QUERY, quotedBlobIds);
String sprocLink = cosmosCollectionLink + BULK_DELETE_SPROC;
boolean more = true;
int deleteCount = 0;
double requestCharge = 0;
try {
while (more) {
StoredProcedureResponse response = asyncDocumentClient.executeStoredProcedure(sprocLink, options, new String[] { query }).toBlocking().single();
requestCharge += response.getRequestCharge();
Document responseDoc = response.getResponseAsDocument();
more = responseDoc.getBoolean(PROPERTY_CONTINUATION);
deleteCount += responseDoc.getInt(PROPERTY_DELETED);
}
if (requestCharge >= requestChargeThreshold) {
logger.info("Bulk delete partition {} request charge {} for {} records", partitionPath, requestCharge, deleteCount);
}
return deleteCount;
} catch (RuntimeException rex) {
if (rex.getCause() instanceof DocumentClientException) {
throw (DocumentClientException) rex.getCause();
} else {
throw rex;
}
}
}
Aggregations