Skip to content

Commit

Permalink
Per Partition Automatic Failover: Adds requirement to enable applicat…
Browse files Browse the repository at this point in the history
…ion region or application preferred regions for PPAF (#4987)

# Pull Request Template

## Description

ClientOptions now validates that either ApplicationRegion or
ApplicationPreferredRegion is mandatory when PartitionLevelFailover is
enabled.

Live testing was also done independently to validate this change and
that PPAF works for gateway interactions, with PPAF, SDK will now
failover to secondary regions for gateway interactions.

## Type of change

Please delete options that are not relevant.

- [] New feature (non-breaking change which adds functionality)

## Closing issues

To automatically close an issue: closes #4974
  • Loading branch information
NaluTripician authored Jan 30, 2025
1 parent 14fb3a1 commit 0958198
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
11 changes: 6 additions & 5 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,11 +1136,12 @@ private void ValidateLimitToEndpointSettings()
}

private void ValidatePartitionLevelFailoverSettings()
{
if (this.EnablePartitionLevelFailover
&& (this.ApplicationPreferredRegions == null || this.ApplicationPreferredRegions.Count == 0))
{
throw new ArgumentException($"{nameof(this.ApplicationPreferredRegions)} is required when {nameof(this.EnablePartitionLevelFailover)} is enabled.");
{
if (this.EnablePartitionLevelFailover
&& string.IsNullOrEmpty(this.ApplicationRegion)
&& (this.ApplicationPreferredRegions is null || this.ApplicationPreferredRegions.Count == 0))
{
throw new ArgumentException($"{nameof(this.ApplicationPreferredRegions)} or {nameof(this.ApplicationRegion)} is required when {nameof(this.EnablePartitionLevelFailover)} is enabled.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public void CosmosClientOptions_WhenPartitionLevelFailoverEnabledAndPreferredReg
ArgumentException exception = Assert.ThrowsException<ArgumentException>(() => cosmosClientBuilder.Build());

Assert.AreEqual(
expected: "ApplicationPreferredRegions is required when EnablePartitionLevelFailover is enabled.",
expected: "ApplicationPreferredRegions or ApplicationRegion is required when EnablePartitionLevelFailover is enabled.",
actual: exception.Message);
}
finally
Expand Down Expand Up @@ -1134,6 +1134,61 @@ public void TestServerCertificatesValidationWithDisableSSLFlagTrue(string connSt
#nullable disable
}

[TestMethod]
public void PPAFClientApplicationRegionCreationTest()
{
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
{
ApplicationRegion = Regions.WestUS2,
EnablePartitionLevelFailover = true
};

CosmosClient cosmosClient = new CosmosClient(ConnectionString, cosmosClientOptions);
Assert.AreEqual(Regions.WestUS2, cosmosClient.ClientOptions.ApplicationRegion);
Assert.IsTrue(cosmosClient.ClientOptions.EnablePartitionLevelFailover);
}

[TestMethod]
public void PPAFClientApplicationPreferredRegionCreationTest()
{
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
{
ApplicationPreferredRegions = new List<string> { Regions.WestUS2, Regions.EastUS2 },
EnablePartitionLevelFailover = true
};

CosmosClient cosmosClient = new CosmosClient(ConnectionString, cosmosClientOptions);
Assert.AreEqual(Regions.WestUS2, cosmosClient.ClientOptions.ApplicationPreferredRegions[0]);
Assert.AreEqual(Regions.EastUS2, cosmosClient.ClientOptions.ApplicationPreferredRegions[1]);
Assert.IsTrue(cosmosClient.ClientOptions.EnablePartitionLevelFailover);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void PPAFClientAppRegionAndAppPreferredRegionTest()
{
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
{
EnablePartitionLevelFailover = true,
ApplicationPreferredRegions = new List<string> { Regions.WestUS2, Regions.EastUS2 },
ApplicationRegion = Regions.AustraliaCentral
};

_ = new CosmosClient(ConnectionString, cosmosClientOptions);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void PPAFClientNoRegionsTest()
{
CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
{
EnablePartitionLevelFailover = true
};

_ = new CosmosClient(ConnectionString, cosmosClientOptions);
}

private class TestWebProxy : IWebProxy
{
public ICredentials Credentials { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void CreateItemAsync_WithNoPreferredRegionsAndServiceUnavailable_ShouldTh
cosmosClientOptions));

Assert.AreEqual(
expected: "ApplicationPreferredRegions is required when EnablePartitionLevelFailover is enabled.",
expected: "ApplicationPreferredRegions or ApplicationRegion is required when EnablePartitionLevelFailover is enabled.",
actual: exception.Message);
}

Expand Down

0 comments on commit 0958198

Please sign in to comment.