Skip to content
Merged
Prev Previous commit
Next Next commit
Some more changes
  • Loading branch information
kirankumarkolli committed Mar 20, 2025
commit 7905e13ba90ceee950e63f5110c74ad92be45701
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ internal DocumentClient(Uri serviceEndpoint,
RemoteCertificateValidationCallback remoteCertificateValidationCallback = null,
CosmosClientTelemetryOptions cosmosClientTelemetryOptions = null,
IChaosInterceptorFactory chaosInterceptorFactory = null,
bool enableAsyncCacheExceptionNoSharing = false)
bool enableAsyncCacheExceptionNoSharing = true)
{
if (sendingRequestEventArgs != null)
{
Expand Down
10 changes: 5 additions & 5 deletions Microsoft.Azure.Cosmos/src/Routing/AsyncCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.Azure.Cosmos.Common
/// <typeparam name="TValue">Type of values.</typeparam>
internal sealed class AsyncCache<TKey, TValue>
{
private readonly bool isStackTraceOptimizationEnabled;
private readonly bool enableAsyncCacheExceptionNoSharing;
private readonly IEqualityComparer<TValue> valueEqualityComparer;
private readonly IEqualityComparer<TKey> keyEqualityComparer;

Expand All @@ -29,17 +29,17 @@ internal sealed class AsyncCache<TKey, TValue>
public AsyncCache(
IEqualityComparer<TValue> valueEqualityComparer,
IEqualityComparer<TKey> keyEqualityComparer = null,
bool enableStackTraceOptimization = false)
bool enableAsyncCacheExceptionNoSharing = true)
{
this.keyEqualityComparer = keyEqualityComparer ?? EqualityComparer<TKey>.Default;
this.values = new ConcurrentDictionary<TKey, AsyncLazy<TValue>>(this.keyEqualityComparer);
this.valueEqualityComparer = valueEqualityComparer;
this.isStackTraceOptimizationEnabled = enableStackTraceOptimization;
this.enableAsyncCacheExceptionNoSharing = enableAsyncCacheExceptionNoSharing;
}

public AsyncCache(bool enableStackTraceOptimization = false)
public AsyncCache(bool enableStackTraceOptimization = true)
: this(valueEqualityComparer: EqualityComparer<TValue>.Default,
enableStackTraceOptimization: enableStackTraceOptimization)
enableAsyncCacheExceptionNoSharing: enableStackTraceOptimization)
{
}

Expand Down
10 changes: 5 additions & 5 deletions Microsoft.Azure.Cosmos/src/Routing/AsyncCacheNonBlocking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.Azure.Cosmos
/// </summary>
internal sealed class AsyncCacheNonBlocking<TKey, TValue> : IDisposable
{
private readonly bool isStackTraceOptimizationEnabled;
private readonly bool enableAsyncCacheExceptionNoSharing;
private readonly CancellationTokenSource cancellationTokenSource;
private readonly ConcurrentDictionary<TKey, AsyncLazyWithRefreshTask<TValue>> values;
private readonly Func<Exception, bool> removeFromCacheOnBackgroundRefreshException;
Expand All @@ -32,21 +32,21 @@ public AsyncCacheNonBlocking(
Func<Exception, bool> removeFromCacheOnBackgroundRefreshException = null,
IEqualityComparer<TKey> keyEqualityComparer = null,
CancellationToken cancellationToken = default,
bool enableAsyncCacheExceptionNoSharing = false)
bool enableAsyncCacheExceptionNoSharing = true)
{
this.keyEqualityComparer = keyEqualityComparer ?? EqualityComparer<TKey>.Default;
this.values = new ConcurrentDictionary<TKey, AsyncLazyWithRefreshTask<TValue>>(this.keyEqualityComparer);
this.removeFromCacheOnBackgroundRefreshException = removeFromCacheOnBackgroundRefreshException ?? AsyncCacheNonBlocking<TKey, TValue>.RemoveNotFoundFromCacheOnException;
this.cancellationTokenSource = cancellationToken == default
? new CancellationTokenSource()
: CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
this.isStackTraceOptimizationEnabled = enableAsyncCacheExceptionNoSharing;
this.enableAsyncCacheExceptionNoSharing = enableAsyncCacheExceptionNoSharing;
}

public AsyncCacheNonBlocking(bool enableStackTraceOptimization = false)
public AsyncCacheNonBlocking(bool enableAsyncCacheExceptionNoSharing = true)
: this(removeFromCacheOnBackgroundRefreshException: null,
keyEqualityComparer: null,
enableAsyncCacheExceptionNoSharing: enableStackTraceOptimization)
enableAsyncCacheExceptionNoSharing: enableAsyncCacheExceptionNoSharing)
{
}

Expand Down
12 changes: 6 additions & 6 deletions Microsoft.Azure.Cosmos/src/Routing/CollectionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ internal abstract class CollectionCache
protected class InternalCache
{
internal InternalCache(
bool enableStackTraceOptimization)
bool enableAsyncCacheExceptionNoSharing)
{
this.collectionInfoByName = new AsyncCache<string, ContainerProperties>(
new CollectionRidComparer(),
enableStackTraceOptimization: enableStackTraceOptimization);
enableAsyncCacheExceptionNoSharing: enableAsyncCacheExceptionNoSharing);

this.collectionInfoById = new AsyncCache<string, ContainerProperties>(
new CollectionRidComparer(),
enableStackTraceOptimization: enableStackTraceOptimization);
enableAsyncCacheExceptionNoSharing: enableAsyncCacheExceptionNoSharing);

this.collectionInfoByNameLastRefreshTime = new ConcurrentDictionary<string, DateTime>();
this.collectionInfoByIdLastRefreshTime = new ConcurrentDictionary<string, DateTime>();
Expand All @@ -56,11 +56,11 @@ internal InternalCache(
protected readonly InternalCache[] cacheByApiList;

protected CollectionCache(
bool enableStackTraceOptimization)
bool enableAsyncCacheExceptionNoSharing)
{
this.cacheByApiList = new InternalCache[2];
this.cacheByApiList[0] = new InternalCache(enableStackTraceOptimization); // for API version < 2018-12-31
this.cacheByApiList[1] = new InternalCache(enableStackTraceOptimization); // for API version >= 2018-12-31
this.cacheByApiList[0] = new InternalCache(enableAsyncCacheExceptionNoSharing); // for API version < 2018-12-31
this.cacheByApiList[1] = new InternalCache(enableAsyncCacheExceptionNoSharing); // for API version >= 2018-12-31
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Routing/GatewayAddressCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ public GatewayAddressCache(
long suboptimalPartitionForceRefreshIntervalInSeconds = 600,
bool enableTcpConnectionEndpointRediscovery = false,
bool replicaAddressValidationEnabled = false,
bool enableStackTraceOptimization = false)
bool enableAsyncCacheExceptionNoSharing = true)
{
this.addressEndpoint = new Uri(serviceEndpoint + "/" + Paths.AddressPathSegment);
this.protocol = protocol;
this.tokenProvider = tokenProvider;
this.serviceEndpoint = serviceEndpoint;
this.serviceConfigReader = serviceConfigReader;
this.serverPartitionAddressCache = new AsyncCacheNonBlocking<PartitionKeyRangeIdentity, PartitionAddressInformation>(enableStackTraceOptimization);
this.serverPartitionAddressCache = new AsyncCacheNonBlocking<PartitionKeyRangeIdentity, PartitionAddressInformation>(enableAsyncCacheExceptionNoSharing);
this.suboptimalServerPartitionTimestamps = new ConcurrentDictionary<PartitionKeyRangeIdentity, DateTime>();
this.serverPartitionAddressToPkRangeIdMap = new ConcurrentDictionary<ServerKey, HashSet<PartitionKeyRangeIdentity>>();
this.suboptimalMasterPartitionTimestamp = DateTime.MaxValue;
Expand Down
8 changes: 4 additions & 4 deletions Microsoft.Azure.Cosmos/src/Routing/GlobalAddressResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal sealed class GlobalAddressResolver : IAddressResolverExtension, IDispos
private readonly ConcurrentDictionary<Uri, EndpointCache> addressCacheByEndpoint;
private readonly bool enableTcpConnectionEndpointRediscovery;
private readonly bool isReplicaAddressValidationEnabled;
private readonly bool enableStackTraceOptimization;
private readonly bool enableAsyncCacheExceptionNoSharing;
private readonly IConnectionStateListener connectionStateListener;
private IOpenConnectionsHandler openConnectionsHandler;

Expand All @@ -54,7 +54,7 @@ public GlobalAddressResolver(
ConnectionPolicy connectionPolicy,
CosmosHttpClient httpClient,
IConnectionStateListener connectionStateListener,
bool enableStackTraceOptimization = false)
bool enableAsyncCacheExceptionNoSharing = true)
{
this.endpointManager = endpointManager;
this.partitionKeyRangeLocationCache = partitionKeyRangeLocationCache;
Expand All @@ -74,7 +74,7 @@ public GlobalAddressResolver(

this.isReplicaAddressValidationEnabled = ConfigurationManager.IsReplicaAddressValidationEnabled(connectionPolicy);

this.enableStackTraceOptimization = enableStackTraceOptimization;
this.enableAsyncCacheExceptionNoSharing = enableAsyncCacheExceptionNoSharing;

this.maxEndpoints = maxBackupReadEndpoints + 2; // for write and alternate write endpoint (during failover)

Expand Down Expand Up @@ -349,7 +349,7 @@ private EndpointCache GetOrAddEndpoint(Uri endpoint)
this.connectionStateListener,
enableTcpConnectionEndpointRediscovery: this.enableTcpConnectionEndpointRediscovery,
replicaAddressValidationEnabled: this.isReplicaAddressValidationEnabled,
enableStackTraceOptimization: this.enableStackTraceOptimization);
enableAsyncCacheExceptionNoSharing: this.enableAsyncCacheExceptionNoSharing);

string location = this.endpointManager.GetLocation(endpoint);
AddressResolver addressResolver = new AddressResolver(null, new NullRequestSigner(), location);
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Routing/GlobalEndpointManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class GlobalEndpointManager : IGlobalEndpointManager
public GlobalEndpointManager(
IDocumentClientInternal owner,
ConnectionPolicy connectionPolicy,
bool enableStackTraceOptimization = false)
bool enableAsyncCacheExceptionNoSharing = true)
{
this.locationCache = new LocationCache(
new ReadOnlyCollection<string>(connectionPolicy.PreferredLocations),
Expand All @@ -59,7 +59,7 @@ public GlobalEndpointManager(
this.connectionPolicy = connectionPolicy;

this.connectionPolicy.PreferenceChanged += this.OnPreferenceChanged;
this.databaseAccountCache = new AsyncCache<string, AccountProperties>(enableStackTraceOptimization);
this.databaseAccountCache = new AsyncCache<string, AccountProperties>(enableAsyncCacheExceptionNoSharing);

#if !(NETSTANDARD15 || NETSTANDARD16)
#if NETSTANDARD20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class AsyncCacheNonBlockingTests
[ExpectedException(typeof(NotFoundException))]
public async Task ValidateNegativeScenario(bool forceRefresh)
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
await asyncCache.GetAsync(
"test",
async (_) =>
Expand All @@ -42,7 +42,7 @@ await asyncCache.GetAsync(
[TestMethod]
public async Task ValidateMultipleBackgroundRefreshesScenario()
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);

string expectedValue = "ResponseValue";
string response = await asyncCache.GetAsync(
Expand Down Expand Up @@ -75,7 +75,7 @@ public async Task ValidateMultipleBackgroundRefreshesScenario()
[ExpectedException(typeof(NotFoundException))]
public async Task ValidateNegativeNotAwaitedScenario()
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
Task task1 = asyncCache.GetAsync(
"test",
async (_) =>
Expand Down Expand Up @@ -105,7 +105,7 @@ await asyncCache.GetAsync(
public async Task ValidateNotFoundOnBackgroundRefreshRemovesFromCacheScenario()
{
string value1 = "Response1Value";
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
string response1 = await asyncCache.GetAsync(
"test",
async (_) =>
Expand Down Expand Up @@ -171,7 +171,7 @@ await asyncCache.GetAsync(
[Owner("jawilley")]
public async Task ValidateAsyncCacheNonBlocking()
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
string result = await asyncCache.GetAsync(
"test",
(_) => Task.FromResult("test2"),
Expand Down Expand Up @@ -212,7 +212,7 @@ public async Task ValidateAsyncCacheNonBlocking()
[Owner("jawilley")]
public async Task ValidateCacheValueIsRemovedAfterException()
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
string result = await asyncCache.GetAsync(
key: "test",
singleValueInitFunc: (_) => Task.FromResult("test2"),
Expand Down Expand Up @@ -268,7 +268,7 @@ await asyncCache.GetAsync(
[Owner("jawilley")]
public async Task ValidateConcurrentCreateAsyncCacheNonBlocking()
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
int totalLazyCalls = 0;

List<Task> tasks = new List<Task>();
Expand All @@ -292,7 +292,7 @@ public async Task ValidateConcurrentCreateAsyncCacheNonBlocking()
[Owner("jawilley")]
public async Task ValidateConcurrentCreateWithFailureAsyncCacheNonBlocking()
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
int totalLazyCalls = 0;

Random random = new Random();
Expand Down Expand Up @@ -331,7 +331,7 @@ await asyncCache.GetAsync(
[Owner("jawilley")]
public async Task ValidateExceptionScenariosCacheNonBlocking()
{
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new AsyncCacheNonBlocking<string, string>(enableAsyncCacheExceptionNoSharing: false);
int totalLazyCalls = 0;

try
Expand Down Expand Up @@ -402,7 +402,7 @@ await asyncCache.GetAsync(
public async Task Refresh_WhenRefreshRequestedForAnExistingKey_ShouldRefreshTheCache()
{
// Arrange.
AsyncCacheNonBlocking<string, string> asyncCache = new (enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new (enableAsyncCacheExceptionNoSharing: false);

// Act and Assert.
string result = await asyncCache.GetAsync(
Expand Down Expand Up @@ -437,7 +437,7 @@ await Task.Delay(
public async Task Refresh_WhenThrowsDocumentClientException_ShouldRemoveKeyFromTheCache()
{
// Arrange.
AsyncCacheNonBlocking<string, string> asyncCache = new (enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new (enableAsyncCacheExceptionNoSharing: false);

// Act and Assert.
string result = await asyncCache.GetAsync(
Expand Down Expand Up @@ -491,7 +491,7 @@ await Task.Delay(
public async Task Refresh_WhenThrowsOtherException_ShouldNotRemoveKeyFromTheCache()
{
// Arrange.
AsyncCacheNonBlocking<string, string> asyncCache = new (enableStackTraceOptimization: false);
AsyncCacheNonBlocking<string, string> asyncCache = new (enableAsyncCacheExceptionNoSharing: false);

// Act and Assert.
string result = await asyncCache.GetAsync(
Expand Down
Loading