diff --git a/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs b/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs index fec7f4ac2f..c826bb90d1 100644 --- a/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs +++ b/Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs @@ -600,7 +600,7 @@ public Func HttpClientFactory internal bool EnablePartitionLevelFailover { get; set; } = false; /// - /// Quorum Read allowed with eventual consistency account + /// Quorum Read allowed with eventual consistency account or consistent prefix account. /// internal bool EnableUpgradeConsistencyToLocalQuorum { get; set; } = false; diff --git a/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs b/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs index 44c38ec51b..692d5b498a 100644 --- a/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs +++ b/Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs @@ -698,7 +698,7 @@ internal CosmosClientBuilder WithPartitionLevelFailoverEnabled() } /// - /// 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 /// internal CosmosClientBuilder AllowUpgradeConsistencyToLocalQuorum() diff --git a/Microsoft.Azure.Cosmos/src/ValidationHelpers.cs b/Microsoft.Azure.Cosmos/src/ValidationHelpers.cs index 14256919c6..58a7a55ddb 100644 --- a/Microsoft.Azure.Cosmos/src/ValidationHelpers.cs +++ b/Microsoft.Azure.Cosmos/src/ValidationHelpers.cs @@ -10,12 +10,12 @@ namespace Microsoft.Azure.Cosmos internal static class ValidationHelpers { /// - /// 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. /// /// Account Level Consistency /// Request/Client Level Consistency - /// Allows Quorum Read with Eventual Account + /// Allows Quorum Read with Eventual or Consistent Prefix Account /// /// /// true/false @@ -36,12 +36,12 @@ public static bool IsValidConsistencyLevelOverwrite( } /// - /// 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. /// /// Account Level Consistency /// Request/Client Level Consistency - /// Allows Quorum Read with Eventual Account + /// Allows Quorum Read with Eventual or Consistent Prefix Account /// /// /// true/false @@ -107,7 +107,7 @@ private static bool IsValidConsistencyLevelOverwrite( } /// - /// 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. /// /// /// @@ -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; } @@ -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; } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ValidationHelpersTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ValidationHelpersTests.cs new file mode 100644 index 0000000000..2c9db33cec --- /dev/null +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/ValidationHelpersTests.cs @@ -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); + } + } +} \ No newline at end of file