-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* factory * As client cache * tests * unrelated contract change * Check for no-op * Removing dispose * Using AsyncCache * Text * Reintroducing cache to avoid async locks * cleanup
- Loading branch information
1 parent
8cf4234
commit efc5c78
Showing
8 changed files
with
194 additions
and
12 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos | ||
{ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using Microsoft.Azure.Documents; | ||
|
||
/// <summary> | ||
/// Cache to create and share Executor instances across the client's lifetime. | ||
/// </summary> | ||
internal class BatchAsyncContainerExecutorCache : IDisposable | ||
{ | ||
private ConcurrentDictionary<string, BatchAsyncContainerExecutor> executorsPerContainer = new ConcurrentDictionary<string, BatchAsyncContainerExecutor>(); | ||
|
||
public BatchAsyncContainerExecutor GetExecutorForContainer( | ||
ContainerCore container, | ||
CosmosClientContext cosmosClientContext) | ||
{ | ||
if (!cosmosClientContext.ClientOptions.AllowBulkExecution) | ||
{ | ||
throw new InvalidOperationException("AllowBulkExecution is not currently enabled."); | ||
} | ||
|
||
string containerLink = container.LinkUri.ToString(); | ||
if (this.executorsPerContainer.TryGetValue(containerLink, out BatchAsyncContainerExecutor executor)) | ||
{ | ||
return executor; | ||
} | ||
|
||
BatchAsyncContainerExecutor newExecutor = new BatchAsyncContainerExecutor( | ||
container, | ||
cosmosClientContext, | ||
Constants.MaxOperationsInDirectModeBatchRequest, | ||
Constants.MaxDirectModeBatchRequestBodySizeInBytes); | ||
if (!this.executorsPerContainer.TryAdd(containerLink, newExecutor)) | ||
{ | ||
newExecutor.Dispose(); | ||
} | ||
|
||
return this.executorsPerContainer[containerLink]; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (KeyValuePair<string, BatchAsyncContainerExecutor> cacheEntry in this.executorsPerContainer) | ||
{ | ||
cacheEntry.Value.Dispose(); | ||
} | ||
} | ||
} | ||
} |
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
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
115 changes: 115 additions & 0 deletions
115
....Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.Tests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Documents; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
|
||
[TestClass] | ||
public class BatchAsyncContainerExecutorCacheTests | ||
{ | ||
[TestMethod] | ||
public async Task ConcurrentGet_ReturnsSameExecutorInstance() | ||
{ | ||
Mock<CosmosClient> mockClient = new Mock<CosmosClient>(); | ||
mockClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost")); | ||
|
||
CosmosClientContext context = new ClientContextCore( | ||
client: mockClient.Object, | ||
clientOptions: new CosmosClientOptions() { AllowBulkExecution = true }, | ||
userJsonSerializer: null, | ||
defaultJsonSerializer: null, | ||
sqlQuerySpecSerializer: null, | ||
cosmosResponseFactory: null, | ||
requestHandler: null, | ||
documentClient: null); | ||
|
||
DatabaseCore db = new DatabaseCore(context, "test"); | ||
|
||
List<Task<ContainerCore>> tasks = new List<Task<ContainerCore>>(); | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
tasks.Add(Task.Run(() => Task.FromResult((ContainerCore)db.GetContainer("test")))); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
|
||
BatchAsyncContainerExecutor firstExecutor = tasks[0].Result.BatchExecutor; | ||
Assert.IsNotNull(firstExecutor); | ||
for (int i = 1; i < 20; i++) | ||
{ | ||
BatchAsyncContainerExecutor otherExecutor = tasks[i].Result.BatchExecutor; | ||
Assert.AreEqual(firstExecutor, otherExecutor); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
[Timeout(60000)] | ||
public async Task SingleTaskScheduler_ExecutorTest() | ||
{ | ||
Mock<CosmosClient> mockClient = new Mock<CosmosClient>(); | ||
mockClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost")); | ||
|
||
CosmosClientContext context = new ClientContextCore( | ||
client: mockClient.Object, | ||
clientOptions: new CosmosClientOptions() { AllowBulkExecution = true }, | ||
userJsonSerializer: null, | ||
defaultJsonSerializer: null, | ||
sqlQuerySpecSerializer: null, | ||
cosmosResponseFactory: null, | ||
requestHandler: null, | ||
documentClient: null); | ||
|
||
DatabaseCore db = new DatabaseCore(context, "test"); | ||
|
||
List<Task<ContainerCore>> tasks = new List<Task<ContainerCore>>(); | ||
for (int i = 0; i < 20; i++) | ||
{ | ||
tasks.Add( | ||
Task.Factory.StartNew(() => (ContainerCore)db.GetContainer("test"), | ||
CancellationToken.None, | ||
TaskCreationOptions.None, | ||
new SingleTaskScheduler())); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
|
||
BatchAsyncContainerExecutor firstExecutor = tasks[0].Result.BatchExecutor; | ||
Assert.IsNotNull(firstExecutor); | ||
for (int i = 1; i < 20; i++) | ||
{ | ||
BatchAsyncContainerExecutor otherExecutor = tasks[i].Result.BatchExecutor; | ||
Assert.AreEqual(firstExecutor, otherExecutor); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void Null_When_OptionsOff() | ||
{ | ||
Mock<CosmosClient> mockClient = new Mock<CosmosClient>(); | ||
mockClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost")); | ||
|
||
CosmosClientContext context = new ClientContextCore( | ||
client: mockClient.Object, | ||
clientOptions: new CosmosClientOptions() { }, | ||
userJsonSerializer: null, | ||
defaultJsonSerializer: null, | ||
sqlQuerySpecSerializer: null, | ||
cosmosResponseFactory: null, | ||
requestHandler: null, | ||
documentClient: null); | ||
|
||
DatabaseCore db = new DatabaseCore(context, "test"); | ||
ContainerCore container = (ContainerCore)db.GetContainer("test"); | ||
Assert.IsNull(container.BatchExecutor); | ||
} | ||
} | ||
} |
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
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
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
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