Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 17, 2025

This PR adds the ability to customize the MaxOperationsInDirectModeBatchRequest used by BatchAsyncContainerExecutor through environment variable configuration, as requested in the issue.

Changes Made

🆕 New Features

  • Environment Variable Support: Added COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST environment variable
  • Backward Compatibility: Falls back to existing Constants.MaxOperationsInDirectModeBatchRequest when env var is not set
  • Input Validation: Validates positive integer values and provides clear error messages for invalid inputs

📁 Files Added/Modified

  1. BatchConfiguration.cs (New) - Configuration helper class with environment variable support
  2. BatchAsyncContainerExecutorCache.cs - Updated to use configurable value (minimal 1-line change)
  3. BatchConfigurationTests.cs (New) - Comprehensive unit tests
  4. BatchAsyncContainerExecutorCacheTests.cs - Added integration test

🔧 Usage Example

# Set custom batch operation limit
export COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST=200

# Cosmos DB client will now use 200 operations per batch instead of default
CosmosClient client = new CosmosClient(connectionString, new CosmosClientOptions 
{ 
    AllowBulkExecution = true 
});

✅ Implementation Details

  • Environment Variable Name: COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST
  • Expected Values: Positive integers (e.g., "50", "200", "500")
  • Default Behavior: Uses existing constant value when env var not set
  • Error Handling: Throws ArgumentException with descriptive messages for invalid values

🧪 Testing

Added comprehensive test coverage including:

  • Default behavior (no environment variable)
  • Custom values from environment variable
  • Input validation (negative numbers, invalid strings, zero)
  • Edge cases (empty strings, large values)
  • Integration testing with BatchAsyncContainerExecutorCache

🛡️ Quality Assurance

  • Minimal Changes: Only 1 line modified in existing production code
  • No Breaking Changes: Existing behavior preserved when env var not set
  • Build Verified: Project builds successfully
  • Backward Compatible: Existing tests and functionality unaffected

This implementation provides enterprise-ready configurability for batch operations while maintaining the robustness and reliability of the existing codebase.

Fixes #5294.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] BatchAsyncContainerExecutor customize MaxOperationsInDirectModeBatchRequest Add environment variable configuration for BatchAsyncContainerExecutor MaxOperationsInDirectModeBatchRequest Jul 17, 2025
Copilot AI requested a review from kirankumarkolli July 17, 2025 00:42
Copilot finished work on behalf of kirankumarkolli July 17, 2025 00:42
/// If the environment variable is not set, the default value from Constants.MaxOperationsInDirectModeBatchRequest is used.
/// </summary>
/// <returns>The maximum number of operations allowed in a direct mode batch request.</returns>
public static int GetMaxOperationsInDirectModeBatchRequest()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to ConfigurationManager as method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the logic to ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest() method as requested. The BatchConfiguration class now delegates to this centralized configuration method. (8abde21)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove BatchConfiguration class, its unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unnecessary BatchConfiguration class as requested. The logic is now handled directly by ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest() and the wrapper class has been eliminated. Tests have been updated to test ConfigurationManager directly. (00ea126)

return Constants.MaxOperationsInDirectModeBatchRequest;
}

if (int.TryParse(environmentValue, out int parsedValue))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid only if value is less than Constants.MaxOperationsInDirectModeBatchRequest and positive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added validation to ensure the value is positive and less than or equal to Constants.MaxOperationsInDirectModeBatchRequest. Updated tests accordingly. (8abde21)

Copilot finished work on behalf of kirankumarkolli July 17, 2025 01:13
container,
cosmosClientContext,
Constants.MaxOperationsInDirectModeBatchRequest,
ConfigurationManager.GetMaxOperationsInDirectModeBatchRequest(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its reading everytime and checking ENVIRONMENT variables which is expensive.
Please read it once and use afterewards

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented caching to read environment variable only once using Lazy<T>. The value is now cached on first access and reused for subsequent calls, eliminating the performance overhead. Added test-friendly controls to disable caching during unit tests. (d515dd8)

/// A read-only string containing the environment variable name for configuring the maximum number of operations
/// allowed in a direct mode batch request.
/// </summary>
internal static readonly string MaxOperationsInDirectModeBatchRequest = "COSMOS_MAX_OPERATIONS_IN_DIRECT_MODE_BATCH_REQUEST";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated environment variable name to "AZURE_COSMOS_MAX_OPERATIONS_IN_BATCH_REQUEST" as suggested and updated corresponding tests. (d515dd8)

Copilot AI requested a review from kirankumarkolli July 17, 2025 19:56
Copilot finished work on behalf of kirankumarkolli July 17, 2025 19:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BatchAsyncContainerExecutor customize MaxOperationsInDirectModeBatchRequest

2 participants