Skip to content
6 changes: 6 additions & 0 deletions Microsoft.Azure.Cosmos/src/ConnectionPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ public bool EnablePartitionLevelCircuitBreaker
set;
}

internal bool DisablePartitionLevelFailoverClientLevelOverride
{
get;
set;
}

/// <summary>
/// Gets or sets the certificate validation callback.
/// </summary>
Expand Down
13 changes: 11 additions & 2 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,15 @@ bool EnableRemoteRegionPreferredForSessionRetry
/// If compute gateway chooses to enable PPAF, then the .NET SDK will enable PPCB by default, which will improve the read availability and latency. This would mean
/// when PPAF is enabled, the SDK will automatically enable PPCB as well.
/// </summary>
internal bool EnablePartitionLevelCircuitBreaker { get; set; } = ConfigurationManager.IsPartitionLevelCircuitBreakerEnabled(defaultValue: false);
internal bool EnablePartitionLevelCircuitBreaker { get; set; } = ConfigurationManager.IsPartitionLevelCircuitBreakerEnabled(defaultValue: false);

/// <summary>
/// Flag from gateway to disable partition level failover. Normally, the SDK will enable partition level failover based on the account settings.
/// This flag will be used internally by the compute gateway as by default it will disable partition level failover.
///
/// The default value for this parameter is 'false'.
/// </summary>
internal bool DisablePartitionLevelFailover { get; set; } = false;

/// <summary>
/// Quorum Read allowed with eventual consistency account or consistent prefix account.
Expand Down Expand Up @@ -1029,7 +1037,8 @@ internal virtual ConnectionPolicy GetConnectionPolicy(int clientId)
MaxRequestsPerTcpConnection = this.MaxRequestsPerTcpConnection,
MaxTcpConnectionsPerEndpoint = this.MaxTcpConnectionsPerEndpoint,
EnableEndpointDiscovery = !this.LimitToEndpoint,
EnablePartitionLevelCircuitBreaker = this.EnablePartitionLevelCircuitBreaker,
EnablePartitionLevelCircuitBreaker = this.EnablePartitionLevelCircuitBreaker,
DisablePartitionLevelFailoverClientLevelOverride = this.DisablePartitionLevelFailover,
PortReuseMode = this.portReuseMode,
EnableTcpConnectionEndpointRediscovery = this.EnableTcpConnectionEndpointRediscovery,
EnableAdvancedReplicaSelectionForTcp = this.EnableAdvancedReplicaSelectionForTcp,
Expand Down
7 changes: 3 additions & 4 deletions Microsoft.Azure.Cosmos/src/DocumentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,13 +1056,12 @@ private async Task<bool> GetInitializationTaskAsync(IStoreClientFactory storeCli
this.EnsureValidOverwrite(this.desiredConsistencyLevel.Value);
}

bool isPPafEnabled = ConfigurationManager.IsPartitionLevelFailoverEnabled(defaultValue: false);
if (this.accountServiceConfiguration != null && this.accountServiceConfiguration.AccountProperties.EnablePartitionLevelFailover.HasValue)
if (!this.ConnectionPolicy.DisablePartitionLevelFailoverClientLevelOverride
&& this.accountServiceConfiguration != null && this.accountServiceConfiguration.AccountProperties.EnablePartitionLevelFailover.HasValue)
{
isPPafEnabled = this.accountServiceConfiguration.AccountProperties.EnablePartitionLevelFailover.Value;
this.ConnectionPolicy.EnablePartitionLevelFailover = this.accountServiceConfiguration.AccountProperties.EnablePartitionLevelFailover.Value;
}

this.ConnectionPolicy.EnablePartitionLevelFailover = isPPafEnabled;
this.ConnectionPolicy.EnablePartitionLevelCircuitBreaker |= this.ConnectionPolicy.EnablePartitionLevelFailover;
this.ConnectionPolicy.UserAgentContainer.AppendFeatures(this.GetUserAgentFeatures());
this.InitializePartitionLevelFailoverWithDefaultHedging();
Expand Down
6 changes: 2 additions & 4 deletions Microsoft.Azure.Cosmos/src/Routing/GlobalEndpointManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,11 @@ public virtual void InitializeAccountPropertiesAndStartBackgroundRefresh(Account
return;
}

bool isPPafEnabled = ConfigurationManager.IsPartitionLevelFailoverEnabled(defaultValue: false);
if (databaseAccount.EnablePartitionLevelFailover.HasValue)
if (!this.connectionPolicy.DisablePartitionLevelFailoverClientLevelOverride && databaseAccount.EnablePartitionLevelFailover.HasValue)
{
isPPafEnabled = databaseAccount.EnablePartitionLevelFailover.Value;
this.connectionPolicy.EnablePartitionLevelFailover = databaseAccount.EnablePartitionLevelFailover.Value;
}

this.connectionPolicy.EnablePartitionLevelFailover = isPPafEnabled;
GlobalEndpointManager.ParseThinClientLocationsFromAdditionalProperties(databaseAccount);

this.locationCache.OnDatabaseAccountRead(databaseAccount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public PartitionKeyRangeFailoverInfo(
this.ConsecutiveWriteRequestFailureCount = 0;
this.ReadRequestFailureCounterThreshold = ConfigurationManager.GetCircuitBreakerConsecutiveFailureCountForReads(10);
this.WriteRequestFailureCounterThreshold = ConfigurationManager.GetCircuitBreakerConsecutiveFailureCountForWrites(5);
this.TimeoutCounterResetWindowInMinutes = TimeSpan.FromMinutes(1);
this.TimeoutCounterResetWindowInMinutes = TimeSpan.FromMinutes(ConfigurationManager.GetCircuitBreakerTimeoutCounterResetWindowInMinutes(5));
this.FirstRequestFailureTime = DateTime.UtcNow;
this.LastRequestFailureTime = DateTime.UtcNow;
}
Expand Down
43 changes: 24 additions & 19 deletions Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ internal static class ConfigurationManager
/// A read-only string containing the environment variable name for capturing the consecutive failure count for writes, before triggering per partition
/// circuit breaker flow. The default value for this interval is 10 consecutive requests within 1 min window.
/// </summary>
internal static readonly string CircuitBreakerConsecutiveFailureCountForWrites = "AZURE_COSMOS_PPCB_CONSECUTIVE_FAILURE_COUNT_FOR_WRITES";
internal static readonly string CircuitBreakerConsecutiveFailureCountForWrites = "AZURE_COSMOS_PPCB_CONSECUTIVE_FAILURE_COUNT_FOR_WRITES";

/// <summary>
/// A read-only string containing the environment variable name for capturing the consecutive failure count for writes, before triggering per partition
/// circuit breaker flow. The default value for this interval is 5 consecutive requests within 1 min window.
/// </summary>
internal static readonly string CircuitBreakerTimeoutCounterResetWindowInMinutes = "AZURE_COSMOS_PPCB_TIMEOUT_COUNTER_RESET_WINDOW_IN_MINUTES";

/// <summary>
/// Environment variable name for overriding optimistic direct execution of queries.
Expand Down Expand Up @@ -159,23 +165,6 @@ public static bool IsReplicaAddressValidationEnabled(
defaultValue: true);
}

/// <summary>
/// Gets the boolean value of the partition level failover environment variable. Note that, partition level failover
/// is disabled by default for both preview and GA releases. The user can set the respective environment variable
/// 'AZURE_COSMOS_PARTITION_LEVEL_FAILOVER_ENABLED' to override the value for both preview and GA. The method will
/// eventually be removed, once partition level failover is enabled by default for both preview and GA.
/// </summary>
/// <param name="defaultValue">A boolean field containing the default value for partition level failover.</param>
/// <returns>A boolean flag indicating if partition level failover is enabled.</returns>
public static bool IsPartitionLevelFailoverEnabled(
bool defaultValue)
{
return ConfigurationManager
.GetEnvironmentVariable(
variable: ConfigurationManager.PartitionLevelFailoverEnabled,
defaultValue: defaultValue);
}

/// <summary>
/// Gets the boolean value indicating whether the thin client mode is enabled based on the environment variable override.
/// </summary>
Expand Down Expand Up @@ -284,7 +273,23 @@ public static int GetCircuitBreakerConsecutiveFailureCountForWrites(
.GetEnvironmentVariable(
variable: ConfigurationManager.CircuitBreakerConsecutiveFailureCountForWrites,
defaultValue: defaultValue);
}
}

/// <summary>
/// Gets the consecutive faulure count for writes (applicable for multi master accounts) before triggering
/// the per partition circuit breaker flow. The default value for this interval is 5 consecutive requests within a 1-minute window.
/// The user can set the respective environment variable 'AZURE_COSMOS_PPCB_TIMEOUT_COUNTER_RESET_WINDOW_IN_MINUTES' to override the value.
/// </summary>
/// <param name="defaultValue">An integer containing the default value for the consecutive failure count.</param>
/// <returns>An double representing the timeout counter reset window in minutes.</returns>
public static double GetCircuitBreakerTimeoutCounterResetWindowInMinutes(
double defaultValue)
{
return ConfigurationManager
.GetEnvironmentVariable(
variable: ConfigurationManager.CircuitBreakerTimeoutCounterResetWindowInMinutes,
defaultValue: defaultValue);
}

/// <summary>
/// Gets the boolean value indicating whether optimistic direct execution is enabled based on the environment variable override.
Expand Down
Loading
Loading