Search in sources :

Example 1 with ConnectionPolicy

use of com.microsoft.azure.cosmosdb.ConnectionPolicy in project ambry by linkedin.

the class AzureContainerCompactorIntegrationTest method cleanup.

/**
 * Cleanup entries in the cosmos db container deletion table.
 * @throws DocumentClientException in case of any exception.
 */
private void cleanup() throws DocumentClientException {
    ConnectionPolicy connectionPolicy = new ConnectionPolicy();
    connectionPolicy.setRequestTimeoutInMillis(cloudConfig.cloudQueryRequestTimeout);
    // Note: retry decisions are made at CloudBlobStore level.  Configure Cosmos with no retries.
    RetryOptions noRetries = new RetryOptions();
    noRetries.setMaxRetryAttemptsOnThrottledRequests(0);
    connectionPolicy.setRetryOptions(noRetries);
    if (azureCloudConfig.cosmosDirectHttps) {
        logger.info("Using CosmosDB DirectHttps connection mode");
        connectionPolicy.setConnectionMode(ConnectionMode.Direct);
    }
    AsyncDocumentClient asyncDocumentClient = new AsyncDocumentClient.Builder().withServiceEndpoint(azureCloudConfig.cosmosEndpoint).withMasterKeyOrResourceToken(azureCloudConfig.cosmosKey).withConnectionPolicy(connectionPolicy).withConsistencyLevel(ConsistencyLevel.Session).build();
    Set<CosmosContainerDeletionEntry> entries = cloudDestination.getCosmosDataAccessor().getDeprecatedContainers(100);
    AtomicBoolean error = new AtomicBoolean(false);
    while (!entries.isEmpty() && !error.get()) {
        entries.stream().forEach(entry -> {
            try {
                RequestOptions requestOptions = new RequestOptions();
                requestOptions.setPartitionKey(new PartitionKey(entry.getId()));
                cloudRequestAgent.doWithRetries(() -> CosmosDataAccessor.executeCosmosAction(() -> asyncDocumentClient.deleteDocument(azureCloudConfig.cosmosDeletedContainerCollectionLink + "/docs/" + entry.getId(), requestOptions).toBlocking().single(), null), "Test Cleanup", entry.getId());
            } catch (CloudStorageException ex) {
                logger.warn("Failed to delete container deprecation entry {}. Unable to cleanup", entry);
                error.set(true);
            }
        });
        entries = cloudDestination.getCosmosDataAccessor().getDeprecatedContainers(100);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestOptions(com.microsoft.azure.cosmosdb.RequestOptions) CloudStorageException(com.github.ambry.cloud.CloudStorageException) RetryOptions(com.microsoft.azure.cosmosdb.RetryOptions) PartitionKey(com.microsoft.azure.cosmosdb.PartitionKey) ConnectionPolicy(com.microsoft.azure.cosmosdb.ConnectionPolicy) AsyncDocumentClient(com.microsoft.azure.cosmosdb.rx.AsyncDocumentClient)

Example 2 with ConnectionPolicy

use of com.microsoft.azure.cosmosdb.ConnectionPolicy in project ambry by linkedin.

the class CloudTestUtil method cleanupPartition.

/**
 * Cleanup the specified partition in azure by deleting all the blobs of the partition.
 * @param azureCloudConfig Properties containing the credentials needed for connection to azure.
 * @param partitionId partition to be deleted.
 */
static void cleanupPartition(AzureCloudConfig azureCloudConfig, PartitionId partitionId) {
    ConnectionPolicy connectionPolicy = new ConnectionPolicy();
    AsyncDocumentClient asyncDocumentClient = new AsyncDocumentClient.Builder().withServiceEndpoint(azureCloudConfig.cosmosEndpoint).withMasterKeyOrResourceToken(azureCloudConfig.cosmosKey).withConnectionPolicy(connectionPolicy).withConsistencyLevel(ConsistencyLevel.Session).build();
    SqlQuerySpec sqlQuerySpec = new SqlQuerySpec("select * from c where c.partitionId=\"" + partitionId.toPathString() + "\"");
    FeedOptions feedOptions = new FeedOptions();
    feedOptions.setPartitionKey(new PartitionKey(partitionId.toPathString()));
    Iterator<FeedResponse<Document>> iterator = asyncDocumentClient.queryDocuments(azureCloudConfig.cosmosCollectionLink, sqlQuerySpec, feedOptions).toBlocking().getIterator();
    RequestOptions requestOptions = new RequestOptions();
    requestOptions.setPartitionKey(new PartitionKey(partitionId.toPathString()));
    while (iterator.hasNext()) {
        FeedResponse<Document> response = iterator.next();
        response.getResults().forEach(document -> asyncDocumentClient.deleteDocument(azureCloudConfig.cosmosCollectionLink + "/docs/" + document.getId(), requestOptions).toBlocking().single());
    }
}
Also used : SqlQuerySpec(com.microsoft.azure.cosmosdb.SqlQuerySpec) RequestOptions(com.microsoft.azure.cosmosdb.RequestOptions) FeedOptions(com.microsoft.azure.cosmosdb.FeedOptions) PartitionKey(com.microsoft.azure.cosmosdb.PartitionKey) FeedResponse(com.microsoft.azure.cosmosdb.FeedResponse) ConnectionPolicy(com.microsoft.azure.cosmosdb.ConnectionPolicy) Document(com.microsoft.azure.cosmosdb.Document) AsyncDocumentClient(com.microsoft.azure.cosmosdb.rx.AsyncDocumentClient)

Aggregations

ConnectionPolicy (com.microsoft.azure.cosmosdb.ConnectionPolicy)2 PartitionKey (com.microsoft.azure.cosmosdb.PartitionKey)2 RequestOptions (com.microsoft.azure.cosmosdb.RequestOptions)2 AsyncDocumentClient (com.microsoft.azure.cosmosdb.rx.AsyncDocumentClient)2 CloudStorageException (com.github.ambry.cloud.CloudStorageException)1 Document (com.microsoft.azure.cosmosdb.Document)1 FeedOptions (com.microsoft.azure.cosmosdb.FeedOptions)1 FeedResponse (com.microsoft.azure.cosmosdb.FeedResponse)1 RetryOptions (com.microsoft.azure.cosmosdb.RetryOptions)1 SqlQuerySpec (com.microsoft.azure.cosmosdb.SqlQuerySpec)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1