Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] LocalQuorum: Adds Quorum reads on Consistent Prefix Accounts #3680

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public Func<HttpClient> HttpClientFactory
internal bool EnablePartitionLevelFailover { get; set; } = false;

/// <summary>
/// Quorum Read allowed with eventual consistency account
/// Quorum Read allowed with eventual consistency account or consistent prefix account.
/// </summary>
internal bool EnableUpgradeConsistencyToLocalQuorum { get; set; } = false;

Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ internal CosmosClientBuilder WithPartitionLevelFailoverEnabled()
}

/// <summary>
/// To enable LocalQuorum Consistency, i.e. Allow Quorum read with Eventual Consistency Account
/// To enable LocalQuorum Consistency, i.e. Allows Quorum read with Eventual Consistency Account or with Consistent Prefix Account.
/// Use By Compute Only
/// </summary>
internal CosmosClientBuilder AllowUpgradeConsistencyToLocalQuorum()
Expand Down
15 changes: 8 additions & 7 deletions Microsoft.Azure.Cosmos/src/ValidationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace Microsoft.Azure.Cosmos
internal static class ValidationHelpers
{
/// <summary>
/// If isLocalQuorumConsistency flag is true, it allows only "Quorum Read with Eventual Consistency Account".
/// If isLocalQuorumConsistency flag is true, it allows only "Quorum Read with either an Eventual Consistency Account or a Consistent Prefix Account".
/// It goes through a validation where it doesn't allow strong consistency over weaker consistency.
/// </summary>
/// <param name="backendConsistency"> Account Level Consistency </param>
/// <param name="desiredConsistency"> Request/Client Level Consistency</param>
/// <param name="isLocalQuorumConsistency"> Allows Quorum Read with Eventual Account</param>
/// <param name="isLocalQuorumConsistency"> Allows Quorum Read with Eventual or Consistent Prefix Account</param>
/// <param name="operationType"> <see cref="OperationType"/> </param>
/// <param name="resourceType"> <see cref="ResourceType"/> </param>
/// <returns>true/false</returns>
Expand All @@ -36,12 +36,12 @@ public static bool IsValidConsistencyLevelOverwrite(
}

/// <summary>
/// If isLocalQuorumConsistency flag is true, it allows only "Quorum Read with Eventual Consistency Account".
/// If isLocalQuorumConsistency flag is true, it allows only "Quorum Read with either an Eventual Consistency Account or a Consistent Prefix Account".
/// It goes through a validation where it doesn't allow strong consistency over weaker consistency.
/// </summary>
/// <param name="backendConsistency"> Account Level Consistency </param>
/// <param name="desiredConsistency"> Request/Client Level Consistency</param>
/// <param name="isLocalQuorumConsistency"> Allows Quorum Read with Eventual Account</param>
/// <param name="isLocalQuorumConsistency"> Allows Quorum Read with Eventual or Consistent Prefix Account</param>
/// <param name="operationType"> <see cref="OperationType"/> </param>
/// <param name="resourceType"> <see cref="ResourceType"/> </param>
/// <returns>true/false</returns>
Expand Down Expand Up @@ -107,7 +107,7 @@ private static bool IsValidConsistencyLevelOverwrite(
}

/// <summary>
/// Condition to check Quorum(i.e. Strong) read with eventual account
/// Condition to check Quorum(i.e. Strong) read with either an eventual consistency account or a consistent prefix account.
/// </summary>
/// <param name="backendConsistency"></param>
/// <param name="desiredConsistency"></param>
Expand All @@ -119,7 +119,7 @@ private static bool IsLocalQuorumConsistency(Documents.ConsistencyLevel backendC
OperationType? operationType,
ResourceType? resourceType)
{
if (backendConsistency != Documents.ConsistencyLevel.Eventual)
if (backendConsistency != Documents.ConsistencyLevel.Eventual && backendConsistency != Documents.ConsistencyLevel.ConsistentPrefix)
{
return false;
}
Expand All @@ -136,7 +136,8 @@ private static bool IsLocalQuorumConsistency(Documents.ConsistencyLevel backendC
}

if (!operationType.HasValue ||
(operationType.HasValue && !(operationType == OperationType.Read || operationType == OperationType.ReadFeed)))
(operationType.HasValue &&
!(operationType == OperationType.Read || operationType == OperationType.ReadFeed || operationType == OperationType.SqlQuery || operationType == OperationType.Query)))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Tests
{
using System.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class ValidationHelpersTest
{
[TestMethod]
[DataRow(true, ConsistencyLevel.Eventual, ConsistencyLevel.Strong)]
[DataRow(true, ConsistencyLevel.ConsistentPrefix, ConsistencyLevel.Strong)]
[DataRow(false, ConsistencyLevel.Eventual, ConsistencyLevel.BoundedStaleness)]
[DataRow(false, ConsistencyLevel.ConsistentPrefix, ConsistencyLevel.BoundedStaleness)]
[DataRow(false, ConsistencyLevel.Session, ConsistencyLevel.Strong)]
[DataRow(false, ConsistencyLevel.BoundedStaleness, ConsistencyLevel.Strong)]
public void TestIsValidConsistencyLevelOverwrite(bool isValidConsistencyLevelOverwrite,
ConsistencyLevel backendConsistencyLevel,
ConsistencyLevel desiredConsistencyLevel)
{
bool result = ValidationHelpers.IsValidConsistencyLevelOverwrite(backendConsistencyLevel,
desiredConsistencyLevel,
true,
Documents.OperationType.Read,
Documents.ResourceType.Document);

Assert.AreEqual(isValidConsistencyLevelOverwrite, result);
}

[TestMethod]
[DataRow(false, ConsistencyLevel.Eventual, ConsistencyLevel.Strong)]
[DataRow(false, ConsistencyLevel.ConsistentPrefix, ConsistencyLevel.Strong)]
public void TestIsValidConsistencyLevelOverwrite_OnlyWhenSpecifyingExplicitOverwrite(bool isValidConsistencyLevelOverwrite,
ConsistencyLevel backendConsistencyLevel,
ConsistencyLevel desiredConsistencyLevel)
{
bool result = ValidationHelpers.IsValidConsistencyLevelOverwrite(backendConsistencyLevel,
desiredConsistencyLevel,
false,
Documents.OperationType.Read,
Documents.ResourceType.Document);

Assert.AreEqual(isValidConsistencyLevelOverwrite, result);
}

[TestMethod]
[DataRow(true, ConsistencyLevel.Eventual, ConsistencyLevel.Strong, Documents.OperationType.Read)]
[DataRow(true, ConsistencyLevel.Eventual, ConsistencyLevel.Strong, Documents.OperationType.ReadFeed)]
[DataRow(true, ConsistencyLevel.ConsistentPrefix, ConsistencyLevel.Strong, Documents.OperationType.Query)]
[DataRow(true, ConsistencyLevel.ConsistentPrefix, ConsistencyLevel.Strong, Documents.OperationType.SqlQuery)]
[DataRow(false, ConsistencyLevel.Eventual, ConsistencyLevel.Strong, Documents.OperationType.QueryPlan)]
[DataRow(false, ConsistencyLevel.ConsistentPrefix, ConsistencyLevel.Strong, Documents.OperationType.Create)]
[DataRow(false, ConsistencyLevel.ConsistentPrefix, ConsistencyLevel.Strong, Documents.OperationType.Batch)]
public void TestIsValidConsistencyLevelOverwrite_OnlyAllowedForCertainOperationTypes(
bool isValidConsistencyLevelOverwrite,
ConsistencyLevel backendConsistencyLevel,
ConsistencyLevel desiredConsistencyLevel,
dynamic operationType)
{
bool result = ValidationHelpers.IsValidConsistencyLevelOverwrite(backendConsistencyLevel,
desiredConsistencyLevel,
true,
(Documents.OperationType) operationType,
Documents.ResourceType.Document);

Assert.AreEqual(isValidConsistencyLevelOverwrite, result);
}
}
}