Skip to content

ExcludeRegions on ItemRequestOptions is ignored when thin client mode is enabled #5789

@jeet1995

Description

@jeet1995

Describe the bug

When thin client mode is enabled (AZURE_COSMOS_THIN_CLIENT_ENABLED=true), per-operation ExcludeRegions on ItemRequestOptions is ignored. All requests route to the first available thin client endpoint (typically the first write region) regardless of the excluded regions list.

Without thin client enabled, ExcludeRegions works correctly — each operation routes to the expected region.

To reproduce

Multi-writer account with 3 write regions (East US 2, West Central US, South Central US). Three CreateItemAsync calls, each excluding 2 regions to pin to a specific target region.

Environment.SetEnvironmentVariable("AZURE_COSMOS_THIN_CLIENT_ENABLED", "true");

using var client = new CosmosClient(endpoint, key, new CosmosClientOptions
{
    ConnectionMode = ConnectionMode.Gateway,
    ApplicationPreferredRegions = new List<string> { "East US 2", "West Central US", "South Central US" }
});

// Pin to West Central US by excluding the other two
var opts = new ItemRequestOptions
{
    ExcludeRegions = new List<string> { "East US 2", "South Central US" }
};
var resp = await container.CreateItemAsync(item, pk, opts);
// Diagnostics show request went to eastus2:10250, NOT westcentralus:10250

Expected: Request routes to westcentralus.documents.azure.com:10250
Actual: Request routes to eastus2.documents.azure.com:10250

Root cause analysis

RequestInvokerHandler.SendAsync() (line ~125-146) branches for thin client operations and calls endpointManager.ResolveThinClientEndpoint(request) instead of ResolveServiceEndpoint(request).

LocationCache.ResolveThinClientEndpoint() (line ~933-949) selects from precomputed ThinClientWriteEndpoints/ThinClientReadEndpoints using index-based selection. It does not check request.RequestContext.ExcludeRegions.

The ExcludeRegions filtering only exists in the normal gateway path: LocationCache.ResolveServiceEndpoint()GetApplicableEndpoints() (line ~416-448).

Suggested fix

ResolveThinClientEndpoint() should filter ThinClientWriteEndpoints/ThinClientReadEndpoints by request.RequestContext.ExcludeRegions before selecting the endpoint, mirroring the logic in GetApplicableEndpoints().

Environment

  • SDK: Microsoft.Azure.Cosmos 3.58.0
  • OS: Windows
  • .NET: 10.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions