Created
January 16, 2022 21:50
-
-
Save RachidAZ/6ce523df96117dceece42a12ad4c2447 to your computer and use it in GitHub Desktop.
Scale up/down Cosmos DB Throughput (manual/auto - shared(DB)/collection level )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.Cosmos; | |
// DB Level Throughput (Shared among collections) , returns both Manual and Auto Scale Throughput | |
public static async Task<int?[]> GetDBThroughput(string connection, string key, string db) | |
{ | |
using (var clientcos = new CosmosClient(connection, key)) | |
{ | |
Microsoft.Azure.Cosmos.RequestOptions requestOptions = new Microsoft.Azure.Cosmos.RequestOptions(); | |
ThroughputResponse response= await clientcos.GetDatabase(db).ReadThroughputAsync(requestOptions); | |
Console.WriteLine($"Current DB Throughput: {response.Resource?.Throughput}"); | |
Console.WriteLine($"Current DB AutoscaleMaxThroughput: {response.Resource?.AutoscaleMaxThroughput}"); | |
Console.WriteLine($"DB MinThroughput: {response.MinThroughput}"); | |
// Console.WriteLine($"IsReplacePending: {response.IsReplacePending}"); | |
return new int?[] { response.Resource.Throughput, response.Resource.AutoscaleMaxThroughput is null ? 0 : response.Resource.AutoscaleMaxThroughput }; | |
} | |
} | |
// Collection Level Throughput , returns both Manual and Auto Scale Throughput | |
public static async Task<int?[]> GetThroughput(string connection, string key, string db, string collectionName) | |
{ | |
using (var clientcos = new CosmosClient(connection, key)) | |
{ | |
var cont = clientcos.GetContainer(db, collectionName); | |
Microsoft.Azure.Cosmos.RequestOptions requestOptions = new Microsoft.Azure.Cosmos.RequestOptions(); | |
/* | |
cont.ReadThroughputAsync(requestOptions); | |
Console.WriteLine($"Throughput: {throughputProperties?.Throughput}"); | |
*/ | |
ThroughputResponse response = await cont.ReadThroughputAsync(requestOptions); | |
Console.WriteLine($"Current Throughput: {response.Resource?.Throughput}"); | |
Console.WriteLine($"Current AutoscaleMaxThroughput: {response.Resource?.AutoscaleMaxThroughput}"); | |
Console.WriteLine($"MinThroughput: {response.MinThroughput}"); | |
// Console.WriteLine($"IsReplacePending: {response.IsReplacePending}"); | |
return new int?[] { response.Resource.Throughput, response.Resource.AutoscaleMaxThroughput is null ? 0 : response.Resource.AutoscaleMaxThroughput }; | |
} | |
} | |
// set manual DB level (shared ) Throughput | |
public static async Task SetManualDBThroughput(int throughputValue, string connection, string key, string db) | |
{ | |
using (var clientcos = new CosmosClient(connection, key)) | |
{ | |
Microsoft.Azure.Cosmos.RequestOptions requestOptions = new Microsoft.Azure.Cosmos.RequestOptions(); | |
ThroughputResponse throughput = await clientcos.GetDatabase(db).ReplaceThroughputAsync( | |
ThroughputProperties.CreateManualThroughput(throughputValue)); | |
} | |
} | |
// set autoscale DB level (shared ) Throughput | |
public static async Task SetAutoScaleDBThroughput(int throughputValue, string connection, string key, string db) | |
{ | |
using (var clientcos = new CosmosClient(connection, key)) | |
{ | |
Microsoft.Azure.Cosmos.RequestOptions requestOptions = new Microsoft.Azure.Cosmos.RequestOptions(); | |
ThroughputResponse throughput = await clientcos.GetDatabase(db).ReplaceThroughputAsync( | |
ThroughputProperties.CreateAutoscaleThroughput(throughputValue)); | |
} | |
} | |
// set manual collection Throughput | |
public static async Task SetManualThroughput(int throughputValue, string connection, string key, string db, string collectionName) | |
{ | |
using (var clientcos = new CosmosClient(connection, key)) | |
{ | |
var cont = clientcos.GetContainer(db, collectionName); | |
Microsoft.Azure.Cosmos.RequestOptions requestOptions = new Microsoft.Azure.Cosmos.RequestOptions(); | |
// change it: | |
ThroughputResponse throughput = await cont.ReplaceThroughputAsync( | |
ThroughputProperties.CreateManualThroughput(throughputValue)); | |
} | |
} | |
// set autoscale collection Throughput | |
public static async Task SetAutoScaleThroughput(int throughputValue, string connection, string key, string db, string collectionName) | |
{ | |
using (var clientcos = new CosmosClient(connection, key)) | |
{ | |
var cont = clientcos.GetContainer(db, collectionName); | |
Microsoft.Azure.Cosmos.RequestOptions requestOptions = new Microsoft.Azure.Cosmos.RequestOptions(); | |
ThroughputResponse throughput = await cont.ReplaceThroughputAsync( | |
ThroughputProperties.CreateAutoscaleThroughput(throughputValue)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment