Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CosmosClientBuilder: Adds Warmup Option to Create/Initialize Containers #3257

Merged
merged 7 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Microsoft.Azure.Cosmos.Fluent
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Documents;
Expand Down Expand Up @@ -130,6 +132,21 @@ public CosmosClient Build()
new CosmosClient(this.accountEndpoint, this.tokenCredential, this.clientOptions);
}

/// <summary>
/// A method to create the cosmos client and initialize the provided containers.
/// </summary>
/// <param name="containers">Containers to be initialized identified by it's database name and container name.</param>
/// <param name="cancellationToken">(Optional) Cancellation Token</param>
/// <returns>
/// A CosmosClient object.
/// </returns>
public async Task<CosmosClient> BuildAndCreateInitializeAsync(IReadOnlyList<(string databaseId, string containerId)> containers, CancellationToken cancellationToken = default)
imanvt marked this conversation as resolved.
Show resolved Hide resolved
{
return this.tokenCredential == null ?
imanvt marked this conversation as resolved.
Show resolved Hide resolved
await CosmosClient.CreateAndInitializeAsync(this.accountEndpoint, this.accountKey, containers, this.clientOptions, cancellationToken) :
await CosmosClient.CreateAndInitializeAsync(this.accountEndpoint, this.tokenCredential, containers, this.clientOptions, cancellationToken);
}

/// <summary>
/// A method to create the cosmos client
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Documents;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -80,6 +81,34 @@ public async Task CreateAndInitializeTest()
cosmosClient.Dispose();
}

[TestMethod]
public async Task CreateAndInitializeWithCosmosClientBuilderTest()
{
int httpCallsMade = 0;
HttpClientHandlerHelper httpClientHandlerHelper = new HttpClientHandlerHelper
{
RequestCallBack = (request, cancellationToken) =>
{
httpCallsMade++;
return null;
}
};

(string endpoint, string authKey) = TestCommon.GetAccountInfo();
List<(string, string)> containers = new List<(string, string)>
{ ("ClientCreateAndInitializeDatabase", "ClientCreateAndInitializeContainer")};

CosmosClientBuilder builder = new CosmosClientBuilder(endpoint, authKey).WithHttpClientFactory(() => new HttpClient(httpClientHandlerHelper));
CosmosClient cosmosClient = await builder.BuildAndCreateInitializeAsync(containers);
Assert.IsNotNull(cosmosClient);
int httpCallsMadeAfterCreation = httpCallsMade;

ContainerInternal container = (ContainerInternal)cosmosClient.GetContainer("ClientCreateAndInitializeDatabase", "ClientCreateAndInitializeContainer");
ItemResponse<ToDoActivity> readResponse = await container.ReadItemAsync<ToDoActivity>("1", new Cosmos.PartitionKey("Status1"));
Assert.AreEqual(httpCallsMade, httpCallsMadeAfterCreation);
cosmosClient.Dispose();
}

[TestMethod]
[ExpectedException(typeof(HttpRequestException))]
public async Task AuthIncorrectTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4356,6 +4356,13 @@
"Attributes": [],
"MethodInfo": "Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder WithThrottlingRetryOptions(System.TimeSpan, Int32);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.CosmosClient] BuildAndCreateInitializeAsync(System.Collections.Generic.IReadOnlyList`1[System.ValueTuple`2[System.String,System.String]], System.Threading.CancellationToken)[System.Runtime.CompilerServices.AsyncStateMachineAttribute(typeof(Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder+<BuildAndCreateInitializeAsync>))]": {
"Type": "Method",
"Attributes": [
"AsyncStateMachineAttribute"
],
"MethodInfo": "System.Threading.Tasks.Task`1[Microsoft.Azure.Cosmos.CosmosClient] BuildAndCreateInitializeAsync(System.Collections.Generic.IReadOnlyList`1[System.ValueTuple`2[System.String,System.String]], System.Threading.CancellationToken);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void .ctor(System.String, Azure.Core.TokenCredential)": {
"Type": "Constructor",
"Attributes": [],
Expand Down