Skip to content

Commit

Permalink
Initial commit to add API to retrieve min throughput for a CosmosCont…
Browse files Browse the repository at this point in the history
…ainer (#7259)

* Initial commit to add API to retrieve min throughput for a CosmosContainer

* Adding tests and using SqlQuerySpec to fetch the offer throughput for a container
  • Loading branch information
abinav2307 authored and weshaggard committed Jan 13, 2020
1 parent 14c8c1d commit 10e4bd5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
package com.azure.data.cosmos;

import com.azure.data.cosmos.internal.DatabaseForTest;
import com.azure.data.cosmos.internal.HttpConstants;
import com.azure.data.cosmos.internal.Offer;
import com.azure.data.cosmos.internal.Paths;
Expand Down Expand Up @@ -427,6 +428,26 @@ public Mono<Integer> readProvisionedThroughput() {
}).map(cosmosOfferResponse -> cosmosOfferResponse.getResource().getThroughput());
}

/**
* Gets the min throughput to which this container can be scaled down to
*
* @return a {@link Mono} containing min throughput or an error.
*/
public Mono<Integer> readMinThroughput() {
return this.read().flatMap(cosmosContainerResponse -> database.getDocClientWrapper()
.queryOffers(
new SqlQuerySpec("select * from c where c.offerResourceId = @OFFER_RESOURCE_ID",
new SqlParameterList(new SqlParameter("@OFFER_RESOURCE_ID", cosmosContainerResponse.resourceSettings().resourceId()))), new FeedOptions())
.single()).flatMap(offerFeedResponse -> {
if (offerFeedResponse.results().isEmpty()) {
return Mono.error(BridgeInternal.createCosmosClientException(HttpConstants.StatusCodes.BADREQUEST,
"No offers found for the resource"));
}
return database.getDocClientWrapper().readOffer(offerFeedResponse.results().get(0).selfLink())
.single();
}).map(cosmosOfferResponse -> Integer.parseInt(cosmosOfferResponse.getResponseHeaders().get(HttpConstants.HttpHeaders.OFFER_MIN_THROUGHPUT)));
}

/**
* Sets throughput provisioned for a container in measurement of
* Requests-per-Unit in the Azure Cosmos service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public static class HttpHeaders {
// Offer header
public static final String OFFER_TYPE = "x-ms-offer-type";
public static final String OFFER_THROUGHPUT = "x-ms-offer-throughput";
public static final String OFFER_MIN_THROUGHPUT = "x-ms-cosmos-min-throughput";
public static final String OFFER_IS_RU_PER_MINUTE_THROUGHPUT_ENABLED = "x-ms-offer-is-ru-per-minute-throughput-enabled";

// Upsert header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,34 @@ public void readCollection_DoesntExist(String collectionName) throws Exception {
validateFailure(readObservable, validator);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT, dataProvider = "collectionCrudArgProvider")
public void readCollectionThroughput(String collectionName) throws InterruptedException {
CosmosContainerProperties collectionDefinition = getCollectionDefinition(collectionName);

Mono<CosmosContainerResponse> createObservable = database.createContainer(collectionDefinition, 400);
CosmosContainer collection = createObservable.block().container();

Mono<CosmosContainerResponse> readObservable = collection.read();
CosmosContainer readCollection = readObservable.block().container();

int provisionedThroughputForCollection = readCollection.readProvisionedThroughput().block();
assertThat(provisionedThroughputForCollection).isEqualTo(400);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT, dataProvider = "collectionCrudArgProvider")
public void readCollectionMinThroughput(String collectionName) throws InterruptedException {
CosmosContainerProperties collectionDefinition = getCollectionDefinition(collectionName);

Mono<CosmosContainerResponse> createObservable = database.createContainer(collectionDefinition, 400);
CosmosContainer collection = createObservable.block().container();

Mono<CosmosContainerResponse> readObservable = collection.read();
CosmosContainer readCollection = readObservable.block().container();

int minThroughputForCollection = readCollection.readMinThroughput().block();
assertThat(minThroughputForCollection).isGreaterThan(0);
}

@Test(groups = { "emulator" }, timeOut = TIMEOUT, dataProvider = "collectionCrudArgProvider")
public void deleteCollection(String collectionName) throws InterruptedException {
CosmosContainerProperties collectionDefinition = getCollectionDefinition(collectionName);
Expand Down

0 comments on commit 10e4bd5

Please sign in to comment.