Skip to content

Commit 4c6d266

Browse files
committed
Revert "PartitionKey: Fixes EnsureIdGetAppendedToPartitionKeyIfNeededAsync to use in-memory cache only"
This reverts commit 2f8b90b.
1 parent 2f8b90b commit 4c6d266

7 files changed

Lines changed: 21 additions & 84 deletions

File tree

Microsoft.Azure.Cosmos/src/DocumentClient.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,6 @@ internal virtual async Task<ClientCollectionCache> GetCollectionCacheAsync(ITrac
616616
}
617617
}
618618

619-
/// <summary>
620-
/// Returns the collection cache if the client is already initialized.
621-
/// Does not trigger initialization. Returns null if the client has not been initialized yet.
622-
/// </summary>
623-
internal ClientCollectionCache CollectionCacheIfInitialized => this.collectionCache;
624-
625619
internal virtual async Task<PartitionKeyRangeCache> GetPartitionKeyRangeCacheAsync(ITrace trace)
626620
{
627621
using (ITrace childTrace = trace.StartChild("Get Partition Key Range Cache", TraceComponent.Routing, Tracing.TraceLevel.Info))

Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -441,21 +441,6 @@ public override async Task<ContainerProperties> GetCachedContainerPropertiesAsyn
441441
}
442442
}
443443

444-
internal override bool TryGetCachedContainerPropertiesFromMemory(out ContainerProperties containerProperties)
445-
{
446-
containerProperties = null;
447-
ClientCollectionCache cache = this.ClientContext.DocumentClient.CollectionCacheIfInitialized;
448-
if (cache == null)
449-
{
450-
return false;
451-
}
452-
453-
return cache.TryGetCachedContainerProperties(
454-
HttpConstants.Versions.CurrentVersion,
455-
this.LinkUri,
456-
out containerProperties);
457-
}
458-
459444
// Name based look-up, needs re-computation and can't be cached
460445
public override async Task<string> GetCachedRIDAsync(
461446
bool forceRefresh,

Microsoft.Azure.Cosmos/src/Resource/Container/ContainerInternal.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,6 @@ public abstract Task<ContainerProperties> GetCachedContainerPropertiesAsync(
5555
ITrace trace,
5656
CancellationToken cancellationToken);
5757

58-
/// <summary>
59-
/// Attempts to retrieve container properties from in-memory cache only.
60-
/// Does not trigger client initialization or make HTTP calls.
61-
/// Returns false if the cache is not populated.
62-
/// </summary>
63-
internal virtual bool TryGetCachedContainerPropertiesFromMemory(out ContainerProperties containerProperties)
64-
{
65-
containerProperties = null;
66-
return false;
67-
}
68-
6958
public abstract Task<IReadOnlyList<IReadOnlyList<string>>> GetPartitionKeyPathTokensAsync(
7059
ITrace trace,
7160
CancellationToken cancellationToken = default);

Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerPropertiesExtensions.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,35 @@ internal static class ContainerPropertiesExtensions
1919
{
2020
private const int DefaultStreamBufferSize = 4096;
2121

22-
internal static Task<PartitionKey?> EnsureIdGetAppendedToPartitionKeyIfNeededAsync(
22+
internal static async Task<PartitionKey?> EnsureIdGetAppendedToPartitionKeyIfNeededAsync(
2323
this ContainerInternal container,
2424
PartitionKey? partitionKey,
2525
string itemId,
2626
CancellationToken cancellationToken)
2727
{
2828
if (container == null)
2929
{
30-
return Task.FromResult(partitionKey);
30+
return partitionKey;
3131
}
3232

33-
// Only use container properties that are already cached in memory.
34-
// This avoids triggering client initialization (EnsureValidClientAsync)
35-
// or making HTTP calls (ResolveByNameAsync) before the handler pipeline
36-
// has had a chance to initialize the client.
37-
// On subsequent requests, the cache will be populated by the normal
38-
// request pipeline and this optimization will take effect.
39-
if (!container.TryGetCachedContainerPropertiesFromMemory(out ContainerProperties containerProperties)
40-
|| containerProperties == null
41-
|| !containerProperties.IsLastPartitionKeyPathId)
33+
ContainerProperties containerProperties;
34+
try
35+
{
36+
containerProperties = await container.GetCachedContainerPropertiesAsync(
37+
forceRefresh: false,
38+
trace: NoOpTrace.Singleton,
39+
cancellationToken: cancellationToken);
40+
}
41+
catch (Exception)
42+
{
43+
// Container may not exist yet (e.g. operations on non-existent containers).
44+
// Swallow the exception and let the actual operation surface the proper error.
45+
return partitionKey;
46+
}
47+
48+
if (containerProperties == null || !containerProperties.IsLastPartitionKeyPathId)
4249
{
43-
return Task.FromResult(partitionKey);
50+
return partitionKey;
4451
}
4552

4653
if (string.IsNullOrEmpty(itemId))
@@ -56,7 +63,7 @@ internal static class ContainerPropertiesExtensions
5663

5764
if (existingComponents.Count != partitionKeyPaths.Count - 1)
5865
{
59-
return Task.FromResult(partitionKey);
66+
return partitionKey;
6067
}
6168
}
6269

@@ -86,7 +93,7 @@ internal static class ContainerPropertiesExtensions
8693

8794
Documents.Routing.PartitionKeyInternal partitionKeyInternal = new Documents.Routing.PartitionKeyInternal(allComponentsList);
8895

89-
return Task.FromResult<PartitionKey?>(new PartitionKey(partitionKeyInternal));
96+
return new PartitionKey(partitionKeyInternal);
9097
}
9198

9299
internal static async Task<(string, Stream)> GetItemIdFromStreamIfRequiredAsync(

Microsoft.Azure.Cosmos/src/Routing/AsyncCache.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -198,27 +198,6 @@ public void Remove(TKey key)
198198
}
199199
}
200200

201-
/// <summary>
202-
/// Attempts to get a value from the cache without triggering any async operations.
203-
/// Returns true only if the value is already cached and completed successfully.
204-
/// </summary>
205-
public bool TryGetCachedValue(TKey key, out TValue value)
206-
{
207-
value = default;
208-
if (this.values.TryGetValue(key, out AsyncLazy<TValue> lazyValue)
209-
&& lazyValue.IsValueCreated
210-
&& lazyValue.Value.IsCompleted
211-
&& lazyValue.Value.Status == TaskStatus.RanToCompletion)
212-
{
213-
#pragma warning disable VSTHRD002 // Safe: task is already completed successfully
214-
value = lazyValue.Value.Result;
215-
#pragma warning restore VSTHRD002
216-
return true;
217-
}
218-
219-
return false;
220-
}
221-
222201
public bool TryRemoveIfCompleted(TKey key)
223202
{
224203
AsyncLazy<TValue> initialLazyValue;

Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,6 @@ private Task<ContainerProperties> ResolveByRidAsync(
271271
cancellationToken);
272272
}
273273

274-
/// <summary>
275-
/// Attempts to retrieve cached container properties from in-memory cache only.
276-
/// Does not trigger any HTTP calls or async initialization.
277-
/// Returns true if the properties are already cached and available.
278-
/// </summary>
279-
internal bool TryGetCachedContainerProperties(
280-
string apiVersion,
281-
string resourceAddress,
282-
out ContainerProperties containerProperties)
283-
{
284-
string resourceFullName = PathsHelper.GetCollectionPath(resourceAddress);
285-
InternalCache cache = this.GetCache(apiVersion);
286-
return cache.collectionInfoByName.TryGetCachedValue(resourceFullName, out containerProperties);
287-
}
288-
289274
internal virtual async Task<ContainerProperties> ResolveByNameAsync(
290275
string apiVersion,
291276
string resourceAddress,

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosItemUnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,6 @@ private static Mock<ContainerInternal> CreateMockContainerWithProperties(Contain
12081208
It.IsAny<ITrace>(),
12091209
It.IsAny<CancellationToken>()))
12101210
.ReturnsAsync(containerProperties);
1211-
mock.Setup(x => x.TryGetCachedContainerPropertiesFromMemory(out containerProperties))
1212-
.Returns(true);
12131211
return mock;
12141212
}
12151213

0 commit comments

Comments
 (0)