From 3ba3133ffc39d7d44b8b471ed5d48def19baea33 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 17 Jul 2025 00:24:00 +0000 Subject: [PATCH 1/6] Initial plan From b521198003a2e8e0535147f4402df6fb9f914005 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 17 Jul 2025 00:39:14 +0000 Subject: [PATCH 2/6] Add environment variable configuration for MaxOperationsInDirectModeBatchRequest Co-authored-by: kirankumarkolli <6880899+kirankumarkolli@users.noreply.github.com> --- .../Batch/BatchAsyncContainerExecutorCache.cs | 2 +- .../src/Batch/BatchConfiguration.cs | 47 +++++++ .../BatchAsyncContainerExecutorCacheTests.cs | 34 +++++ .../Batch/BatchConfigurationTests.cs | 130 ++++++++++++++++++ 4 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs create mode 100644 Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs diff --git a/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs b/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs index 6312a542e2..ee837afd1f 100644 --- a/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs +++ b/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs @@ -36,7 +36,7 @@ public BatchAsyncContainerExecutor GetExecutorForContainer( BatchAsyncContainerExecutor newExecutor = new BatchAsyncContainerExecutor( container, cosmosClientContext, - Constants.MaxOperationsInDirectModeBatchRequest, + BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(), DefaultMaxBulkRequestBodySizeInBytes); if (!this.executorsPerContainer.TryAdd(containerLink, newExecutor)) { diff --git a/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs b/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs new file mode 100644 index 0000000000..c6c232debe --- /dev/null +++ b/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs @@ -0,0 +1,47 @@ +//------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------ + +namespace Microsoft.Azure.Cosmos +{ + using System; + using Microsoft.Azure.Documents; + + /// + /// Configuration settings for batch operations. + /// + internal static class BatchConfiguration + { + private const string MaxOperationsEnvironmentVariableName = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; + + /// + /// Gets the maximum number of operations allowed in a direct mode batch request. + /// This value can be customized using the COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST environment variable. + /// If the environment variable is not set, the default value from Constants.MaxOperationsInDirectModeBatchRequest is used. + /// + /// The maximum number of operations allowed in a direct mode batch request. + public static int GetMaxOperationsInDirectModeBatchRequest() + { + string environmentValue = Environment.GetEnvironmentVariable(MaxOperationsEnvironmentVariableName); + + if (string.IsNullOrEmpty(environmentValue)) + { + return Constants.MaxOperationsInDirectModeBatchRequest; + } + + if (int.TryParse(environmentValue, out int parsedValue)) + { + if (parsedValue <= 0) + { + throw new ArgumentException( + $"Environment variable {MaxOperationsEnvironmentVariableName} must be a positive integer. Current value: {environmentValue}"); + } + + return parsedValue; + } + + throw new ArgumentException( + $"Environment variable {MaxOperationsEnvironmentVariableName} must be a valid integer. Current value: {environmentValue}"); + } + } +} \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs index 548f203d3e..327fad95e0 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs @@ -79,6 +79,40 @@ public void Null_When_OptionsOff() Assert.IsNull(container.BatchExecutor); } + [TestMethod] + public void GetExecutorForContainer_UsesCustomMaxOperationsFromEnvironment() + { + // Arrange + const string environmentVariableName = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; + const int customMaxOperations = 150; + + // Store original value to restore later + string originalValue = Environment.GetEnvironmentVariable(environmentVariableName); + + try + { + Environment.SetEnvironmentVariable(environmentVariableName, customMaxOperations.ToString()); + + CosmosClientContext context = this.MockClientContext(); + DatabaseInternal db = new DatabaseInlineCore(context, "test"); + ContainerInternal container = new ContainerInlineCore(context, db, "test"); + + // Act + BatchAsyncContainerExecutor executor = container.BatchExecutor; + + // Assert + Assert.IsNotNull(executor); + // The executor should be created with the custom max operations value + // We verify this indirectly by ensuring the executor was created successfully + // The actual value is verified in the BatchConfiguration tests + } + finally + { + // Restore original environment variable value + Environment.SetEnvironmentVariable(environmentVariableName, originalValue); + } + } + private CosmosClientContext MockClientContext(bool allowBulkExecution = true) { Mock mockClient = new Mock(); diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs new file mode 100644 index 0000000000..7fd895dc7d --- /dev/null +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs @@ -0,0 +1,130 @@ +//------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------ + +namespace Microsoft.Azure.Cosmos.Tests +{ + using System; + using Microsoft.Azure.Documents; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class BatchConfigurationTests + { + private const string EnvironmentVariableName = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; + + [TestCleanup] + public void TestCleanup() + { + // Clean up environment variable after each test + Environment.SetEnvironmentVariable(EnvironmentVariableName, null); + } + + [TestMethod] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableNotSet_ReturnsDefault() + { + // Arrange + Environment.SetEnvironmentVariable(EnvironmentVariableName, null); + + // Act + int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert + Assert.AreEqual(Constants.MaxOperationsInDirectModeBatchRequest, result); + } + + [TestMethod] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToValidValue_ReturnsValue() + { + // Arrange + const int expectedValue = 50; + Environment.SetEnvironmentVariable(EnvironmentVariableName, expectedValue.ToString()); + + // Act + int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert + Assert.AreEqual(expectedValue, result); + } + + [TestMethod] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToLargeValue_ReturnsValue() + { + // Arrange + const int expectedValue = 500; + Environment.SetEnvironmentVariable(EnvironmentVariableName, expectedValue.ToString()); + + // Act + int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert + Assert.AreEqual(expectedValue, result); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToZero_ThrowsArgumentException() + { + // Arrange + Environment.SetEnvironmentVariable(EnvironmentVariableName, "0"); + + // Act + BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert - ExpectedException + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToNegativeValue_ThrowsArgumentException() + { + // Arrange + Environment.SetEnvironmentVariable(EnvironmentVariableName, "-1"); + + // Act + BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert - ExpectedException + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToInvalidString_ThrowsArgumentException() + { + // Arrange + Environment.SetEnvironmentVariable(EnvironmentVariableName, "invalid"); + + // Act + BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert - ExpectedException + } + + [TestMethod] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToEmptyString_ReturnsDefault() + { + // Arrange + Environment.SetEnvironmentVariable(EnvironmentVariableName, ""); + + // Act + int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert + Assert.AreEqual(Constants.MaxOperationsInDirectModeBatchRequest, result); + } + + [TestMethod] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToOne_ReturnsOne() + { + // Arrange + const int expectedValue = 1; + Environment.SetEnvironmentVariable(EnvironmentVariableName, expectedValue.ToString()); + + // Act + int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert + Assert.AreEqual(expectedValue, result); + } + } +} \ No newline at end of file From 8abde21db51c24d25e38f89390683cae1545c146 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 17 Jul 2025 00:58:59 +0000 Subject: [PATCH 3/6] Move BatchConfiguration logic to ConfigurationManager and add upper bound validation Co-authored-by: kirankumarkolli <6880899+kirankumarkolli@users.noreply.github.com> --- .../src/Batch/BatchConfiguration.cs | 26 +---------- .../src/Util/ConfigurationManager.cs | 43 +++++++++++++++++++ .../Batch/BatchConfigurationTests.cs | 17 +++++++- 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs b/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs index c6c232debe..758f44cd01 100644 --- a/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs +++ b/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs @@ -4,16 +4,11 @@ namespace Microsoft.Azure.Cosmos { - using System; - using Microsoft.Azure.Documents; - /// /// Configuration settings for batch operations. /// internal static class BatchConfiguration { - private const string MaxOperationsEnvironmentVariableName = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; - /// /// Gets the maximum number of operations allowed in a direct mode batch request. /// This value can be customized using the COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST environment variable. @@ -22,26 +17,7 @@ internal static class BatchConfiguration /// The maximum number of operations allowed in a direct mode batch request. public static int GetMaxOperationsInDirectModeBatchRequest() { - string environmentValue = Environment.GetEnvironmentVariable(MaxOperationsEnvironmentVariableName); - - if (string.IsNullOrEmpty(environmentValue)) - { - return Constants.MaxOperationsInDirectModeBatchRequest; - } - - if (int.TryParse(environmentValue, out int parsedValue)) - { - if (parsedValue <= 0) - { - throw new ArgumentException( - $"Environment variable {MaxOperationsEnvironmentVariableName} must be a positive integer. Current value: {environmentValue}"); - } - - return parsedValue; - } - - throw new ArgumentException( - $"Environment variable {MaxOperationsEnvironmentVariableName} must be a valid integer. Current value: {environmentValue}"); + return ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); } } } \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs b/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs index 6199651ebb..921bab8995 100644 --- a/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs +++ b/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs @@ -111,6 +111,12 @@ internal static class ConfigurationManager /// internal static readonly string TcpChannelMultiplexingEnabled = "AZURE_COSMOS_TCP_CHANNEL_MULTIPLEX_ENABLED"; + /// + /// A read-only string containing the environment variable name for configuring the maximum number of operations + /// allowed in a direct mode batch request. + /// + internal static readonly string MaxOperationsInDirectModeBatchRequest = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; + public static T GetEnvironmentVariable(string variable, T defaultValue) { string value = Environment.GetEnvironmentVariable(variable); @@ -357,5 +363,42 @@ public static bool IsTcpChannelMultiplexingEnabled() variable: ConfigurationManager.TcpChannelMultiplexingEnabled, defaultValue: false); } + + /// + /// Gets the maximum number of operations allowed in a direct mode batch request. + /// This value can be customized using the COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST environment variable. + /// If the environment variable is not set, the default value from Constants.MaxOperationsInDirectModeBatchRequest is used. + /// The configured value must be positive and less than or equal to the default constant value. + /// + /// The maximum number of operations allowed in a direct mode batch request. + public static int GetMaxOperationsInDirectModeBatchRequest() + { + string environmentValue = Environment.GetEnvironmentVariable(ConfigurationManager.MaxOperationsInDirectModeBatchRequest); + + if (string.IsNullOrEmpty(environmentValue)) + { + return Documents.Constants.MaxOperationsInDirectModeBatchRequest; + } + + if (int.TryParse(environmentValue, out int parsedValue)) + { + if (parsedValue <= 0) + { + throw new ArgumentException( + $"Environment variable {ConfigurationManager.MaxOperationsInDirectModeBatchRequest} must be a positive integer. Current value: {environmentValue}"); + } + + if (parsedValue > Documents.Constants.MaxOperationsInDirectModeBatchRequest) + { + throw new ArgumentException( + $"Environment variable {ConfigurationManager.MaxOperationsInDirectModeBatchRequest} must be less than or equal to {Documents.Constants.MaxOperationsInDirectModeBatchRequest}. Current value: {environmentValue}"); + } + + return parsedValue; + } + + throw new ArgumentException( + $"Environment variable {ConfigurationManager.MaxOperationsInDirectModeBatchRequest} must be a valid integer. Current value: {environmentValue}"); + } } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs index 7fd895dc7d..81f5647974 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs @@ -51,7 +51,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToLargeValue_ReturnsValue() { // Arrange - const int expectedValue = 500; + const int expectedValue = 50; // Changed to a smaller value that should be within bounds Environment.SetEnvironmentVariable(EnvironmentVariableName, expectedValue.ToString()); // Act @@ -61,6 +61,21 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Assert.AreEqual(expectedValue, result); } + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToValueGreaterThanMax_ThrowsArgumentException() + { + // Arrange + // Set to a value that's likely greater than the default constant + const int valueGreaterThanMax = 10000; + Environment.SetEnvironmentVariable(EnvironmentVariableName, valueGreaterThanMax.ToString()); + + // Act + BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + + // Assert - ExpectedException + } + [TestMethod] [ExpectedException(typeof(ArgumentException))] public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetToZero_ThrowsArgumentException() From 00ea12631f460ba93f506f3f32edadc3541dc37d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 17 Jul 2025 01:12:10 +0000 Subject: [PATCH 4/6] Remove unnecessary BatchConfiguration class and use ConfigurationManager directly Co-authored-by: kirankumarkolli <6880899+kirankumarkolli@users.noreply.github.com> --- .../Batch/BatchAsyncContainerExecutorCache.cs | 2 +- .../src/Batch/BatchConfiguration.cs | 23 ------------------- .../BatchAsyncContainerExecutorCacheTests.cs | 2 +- ...s.cs => ConfigurationManagerBatchTests.cs} | 20 ++++++++-------- 4 files changed, 12 insertions(+), 35 deletions(-) delete mode 100644 Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs rename Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/{BatchConfigurationTests.cs => ConfigurationManagerBatchTests.cs} (84%) diff --git a/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs b/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs index ee837afd1f..fab9820e7b 100644 --- a/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs +++ b/Microsoft.Azure.Cosmos/src/Batch/BatchAsyncContainerExecutorCache.cs @@ -36,7 +36,7 @@ public BatchAsyncContainerExecutor GetExecutorForContainer( BatchAsyncContainerExecutor newExecutor = new BatchAsyncContainerExecutor( container, cosmosClientContext, - BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(), + ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(), DefaultMaxBulkRequestBodySizeInBytes); if (!this.executorsPerContainer.TryAdd(containerLink, newExecutor)) { diff --git a/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs b/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs deleted file mode 100644 index 758f44cd01..0000000000 --- a/Microsoft.Azure.Cosmos/src/Batch/BatchConfiguration.cs +++ /dev/null @@ -1,23 +0,0 @@ -//------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. All rights reserved. -//------------------------------------------------------------ - -namespace Microsoft.Azure.Cosmos -{ - /// - /// Configuration settings for batch operations. - /// - internal static class BatchConfiguration - { - /// - /// Gets the maximum number of operations allowed in a direct mode batch request. - /// This value can be customized using the COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST environment variable. - /// If the environment variable is not set, the default value from Constants.MaxOperationsInDirectModeBatchRequest is used. - /// - /// The maximum number of operations allowed in a direct mode batch request. - public static int GetMaxOperationsInDirectModeBatchRequest() - { - return ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); - } - } -} \ No newline at end of file diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs index 327fad95e0..26c4e281f5 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchAsyncContainerExecutorCacheTests.cs @@ -104,7 +104,7 @@ public void GetExecutorForContainer_UsesCustomMaxOperationsFromEnvironment() Assert.IsNotNull(executor); // The executor should be created with the custom max operations value // We verify this indirectly by ensuring the executor was created successfully - // The actual value is verified in the BatchConfiguration tests + // The actual value is verified in the ConfigurationManager tests } finally { diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/ConfigurationManagerBatchTests.cs similarity index 84% rename from Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs rename to Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/ConfigurationManagerBatchTests.cs index 81f5647974..b0a17890ca 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/BatchConfigurationTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/ConfigurationManagerBatchTests.cs @@ -9,7 +9,7 @@ namespace Microsoft.Azure.Cosmos.Tests using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] - public class BatchConfigurationTests + public class ConfigurationManagerTests { private const string EnvironmentVariableName = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; @@ -27,7 +27,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableNotS Environment.SetEnvironmentVariable(EnvironmentVariableName, null); // Act - int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + int result = ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert Assert.AreEqual(Constants.MaxOperationsInDirectModeBatchRequest, result); @@ -41,7 +41,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, expectedValue.ToString()); // Act - int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + int result = ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert Assert.AreEqual(expectedValue, result); @@ -55,7 +55,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, expectedValue.ToString()); // Act - int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + int result = ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert Assert.AreEqual(expectedValue, result); @@ -71,7 +71,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, valueGreaterThanMax.ToString()); // Act - BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert - ExpectedException } @@ -84,7 +84,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, "0"); // Act - BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert - ExpectedException } @@ -97,7 +97,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, "-1"); // Act - BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert - ExpectedException } @@ -110,7 +110,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, "invalid"); // Act - BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert - ExpectedException } @@ -122,7 +122,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, ""); // Act - int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + int result = ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert Assert.AreEqual(Constants.MaxOperationsInDirectModeBatchRequest, result); @@ -136,7 +136,7 @@ public void GetMaxOperationsInDirectModeBatchRequest_WhenEnvironmentVariableSetT Environment.SetEnvironmentVariable(EnvironmentVariableName, expectedValue.ToString()); // Act - int result = BatchConfiguration.GetMaxOperationsInDirectModeBatchRequest(); + int result = ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(); // Assert Assert.AreEqual(expectedValue, result); From e1e0458e722b7c4f7f012c594f1e076d7808015f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 17 Jul 2025 19:52:17 +0000 Subject: [PATCH 5/6] Update env var name and implement performance optimization with caching Co-authored-by: kirankumarkolli <6880899+kirankumarkolli@users.noreply.github.com> --- .../src/Util/ConfigurationManager.cs | 50 ++++++++++++++++++- .../Batch/ConfigurationManagerBatchTests.cs | 16 +++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs b/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs index 921bab8995..448de08170 100644 --- a/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs +++ b/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs @@ -115,7 +115,18 @@ internal static class ConfigurationManager /// A read-only string containing the environment variable name for configuring the maximum number of operations /// allowed in a direct mode batch request. /// - internal static readonly string MaxOperationsInDirectModeBatchRequest = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; + internal static readonly string MaxOperationsInDirectModeBatchRequest = "AZURE_COSMOS_MAX_OPERATIONS_IN_BATCH_REQUEST"; + + /// + /// Cached value for the maximum number of operations in a direct mode batch request. + /// This is initialized once and reused to avoid repeatedly reading the environment variable. + /// + private static readonly Lazy maxOperationsInDirectModeBatchRequestCached = new Lazy(GetMaxOperationsInDirectModeBatchRequestInternal); + + /// + /// Internal field to track if caching is disabled (used for testing). + /// + private static bool isCachingDisabled = false; public static T GetEnvironmentVariable(string variable, T defaultValue) { @@ -366,12 +377,29 @@ public static bool IsTcpChannelMultiplexingEnabled() /// /// Gets the maximum number of operations allowed in a direct mode batch request. - /// This value can be customized using the COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST environment variable. + /// This value can be customized using the AZURE_COSMOS_MAX_OPERATIONS_IN_BATCH_REQUEST environment variable. /// If the environment variable is not set, the default value from Constants.MaxOperationsInDirectModeBatchRequest is used. /// The configured value must be positive and less than or equal to the default constant value. + /// This method uses caching to avoid repeatedly reading the environment variable. /// /// The maximum number of operations allowed in a direct mode batch request. public static int GetMaxOperationsInDirectModeBatchRequest() + { + // If caching is disabled (for testing), always read fresh + if (isCachingDisabled) + { + return GetMaxOperationsInDirectModeBatchRequestInternal(); + } + + return maxOperationsInDirectModeBatchRequestCached.Value; + } + + /// + /// Internal method that performs the actual environment variable reading and validation. + /// This is called only once and cached by the Lazy of int field. + /// + /// The maximum number of operations allowed in a direct mode batch request. + private static int GetMaxOperationsInDirectModeBatchRequestInternal() { string environmentValue = Environment.GetEnvironmentVariable(ConfigurationManager.MaxOperationsInDirectModeBatchRequest); @@ -400,5 +428,23 @@ public static int GetMaxOperationsInDirectModeBatchRequest() throw new ArgumentException( $"Environment variable {ConfigurationManager.MaxOperationsInDirectModeBatchRequest} must be a valid integer. Current value: {environmentValue}"); } + + /// + /// Disables caching for the maximum operations in direct mode batch request. + /// This method is intended for testing purposes only. + /// + internal static void DisableBatchRequestCaching() + { + isCachingDisabled = true; + } + + /// + /// Enables caching for the maximum operations in direct mode batch request. + /// This method is intended for testing purposes only. + /// + internal static void EnableBatchRequestCaching() + { + isCachingDisabled = false; + } } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/ConfigurationManagerBatchTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/ConfigurationManagerBatchTests.cs index b0a17890ca..b2bf5a4a49 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/ConfigurationManagerBatchTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Batch/ConfigurationManagerBatchTests.cs @@ -11,7 +11,21 @@ namespace Microsoft.Azure.Cosmos.Tests [TestClass] public class ConfigurationManagerTests { - private const string EnvironmentVariableName = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST"; + private const string EnvironmentVariableName = "AZURE_COSMOS_MAX_OPERATIONS_IN_BATCH_REQUEST"; + + [ClassInitialize] + public static void ClassInitialize(TestContext context) + { + // Disable caching for tests to allow environment variable changes to take effect + ConfigurationManager.DisableBatchRequestCaching(); + } + + [ClassCleanup] + public static void ClassCleanup() + { + // Re-enable caching after tests + ConfigurationManager.EnableBatchRequestCaching(); + } [TestCleanup] public void TestCleanup() From d515dd8ee43441b5729bfb1d60b94c23b4382d7a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 17 Jul 2025 19:55:19 +0000 Subject: [PATCH 6/6] Fix caching implementation to properly reset cache for tests Co-authored-by: kirankumarkolli <6880899+kirankumarkolli@users.noreply.github.com> --- Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs b/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs index 448de08170..782038ee92 100644 --- a/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs +++ b/Microsoft.Azure.Cosmos/src/Util/ConfigurationManager.cs @@ -121,7 +121,7 @@ internal static class ConfigurationManager /// Cached value for the maximum number of operations in a direct mode batch request. /// This is initialized once and reused to avoid repeatedly reading the environment variable. /// - private static readonly Lazy maxOperationsInDirectModeBatchRequestCached = new Lazy(GetMaxOperationsInDirectModeBatchRequestInternal); + private static Lazy maxOperationsInDirectModeBatchRequestCached = new Lazy(GetMaxOperationsInDirectModeBatchRequestInternal); /// /// Internal field to track if caching is disabled (used for testing). @@ -440,11 +440,13 @@ internal static void DisableBatchRequestCaching() /// /// Enables caching for the maximum operations in direct mode batch request. - /// This method is intended for testing purposes only. + /// This method is intended for testing purposes only and resets the cache. /// internal static void EnableBatchRequestCaching() { isCachingDisabled = false; + // Reset the cache to ensure fresh value is read when caching is re-enabled + maxOperationsInDirectModeBatchRequestCached = new Lazy(GetMaxOperationsInDirectModeBatchRequestInternal); } } }