-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internal PartitionKeyRangeCache: Adds RID refresh on NotFound in GetR…
…outingMapAsync (#2096) In GetRoutingMapAsync, we read the RID from cache & then look up the associated collection routing map. In the case where the RID is stale & the collection routing map is not found, the stale RID should be invalidated & the operation retried. I am also switching a method written using ContinueWith to be async instead as I found during testing that this style can cause AggregateException to be thrown which callers do not typically expect (e.g. a NotFound would end up as a 500 because of that).
- Loading branch information
Showing
2 changed files
with
83 additions
and
24 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
...ft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/PartitionKeyRangeCacheTests.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,53 @@ | ||
// unset | ||
|
||
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests | ||
{ | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Cosmos.Routing; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class PartitionKeyRangeCacheTests | ||
{ | ||
[TestMethod] | ||
[Owner("flnarenj")] | ||
public async Task TestRidRefreshOnNotFoundAsync() | ||
{ | ||
CosmosClient resourceClient = TestCommon.CreateCosmosClient(); | ||
|
||
string dbName = Guid.NewGuid().ToString(); | ||
string containerName = Guid.NewGuid().ToString(); | ||
|
||
Database db = await resourceClient.CreateDatabaseAsync(dbName); | ||
Container container = await db.CreateContainerAsync(containerName, "/_id"); | ||
|
||
CosmosClient testClient = TestCommon.CreateCosmosClient(); | ||
ContainerInternal testContainer = (ContainerInlineCore)testClient.GetContainer(dbName, containerName); | ||
|
||
// Populate the RID cache. | ||
string cachedRidAsync = await testContainer.GetCachedRIDAsync(forceRefresh: false); | ||
|
||
// Delete the container (using resource client). | ||
await container.DeleteContainerAsync(); | ||
|
||
// Because the RID is cached, this will now try to resolve the collection routing map. | ||
Assert.AreEqual(cachedRidAsync, await testContainer.GetCachedRIDAsync(forceRefresh: false)); | ||
CosmosException notFoundException = await Assert.ThrowsExceptionAsync<CosmosException>(() => testContainer.GetRoutingMapAsync(cancellationToken: default)); | ||
Assert.AreEqual(HttpStatusCode.NotFound, notFoundException.StatusCode); | ||
|
||
await db.CreateContainerAsync(containerName, "/_id"); | ||
|
||
CollectionRoutingMap collectionRoutingMap = await testContainer.GetRoutingMapAsync(cancellationToken: default); | ||
Assert.IsNotNull(collectionRoutingMap); | ||
Assert.AreNotEqual(cachedRidAsync, await testContainer.GetCachedRIDAsync(forceRefresh: false)); | ||
|
||
// Delete the container (using resource client). | ||
await container.DeleteContainerAsync(); | ||
|
||
CollectionRoutingMap collectionRoutingMapFromCache = await testContainer.GetRoutingMapAsync(cancellationToken: default); | ||
Assert.AreEqual(collectionRoutingMap, collectionRoutingMapFromCache); | ||
} | ||
} | ||
} |