Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CosmosClientOptions: Adds Private Custom Account Endpoints #4265

Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
83cee90
Code changes to add regional endpoints for account metadata calls.
kundadebdatta Jan 17, 2024
1e12015
Code changes to refactor some codes.
kundadebdatta Jan 19, 2024
7f68b28
Code changes to add unit tests.
kundadebdatta Jan 22, 2024
7aa43f8
Code changes to make minor code clean-up.
kundadebdatta Jan 23, 2024
58517ed
Merge branch 'master' into users/kundadebdatta/4236_add_custom_domain…
kundadebdatta Jan 23, 2024
60a0803
Code changes to fix tests. Refactored API.
kundadebdatta Jan 23, 2024
a3b4c78
Merge branch 'users/kundadebdatta/4236_add_custom_domain_names' of ht…
kundadebdatta Jan 23, 2024
6efe507
Code changes to refactor the enumeration logic inside global endpoint…
kundadebdatta Jan 25, 2024
fb5af02
Code changes to address review comments.
kundadebdatta Jan 25, 2024
55f1ace
Code changes to fix minor API parameter.
kundadebdatta Jan 25, 2024
94b0058
Code changes to update the API naming.
kundadebdatta Jan 26, 2024
9cc57ad
Code changes to update some attribute names.
kundadebdatta Jan 26, 2024
3c4498f
Merge branch 'master' into users/kundadebdatta/4236_add_custom_domain…
kundadebdatta Jan 26, 2024
348e4a7
Code changes to refactor service endpoint creation logic.
kundadebdatta Jan 29, 2024
ea51744
Code changes to address review comments.
kundadebdatta Jan 30, 2024
b8788d4
Merge branch 'master' into users/kundadebdatta/4236_add_custom_domain…
kundadebdatta Jan 30, 2024
f177371
Code changes to address review comments.
kundadebdatta Feb 6, 2024
35d1c9a
Merge branch 'master' into users/kundadebdatta/4236_add_custom_domain…
kundadebdatta Feb 6, 2024
d4c3526
Code changes to update the API contract.
kundadebdatta Feb 6, 2024
5a39ba3
Merge branch 'master' into users/kundadebdatta/4236_add_custom_domain…
kundadebdatta Feb 6, 2024
65b9b44
Cosmetic code changes.
kundadebdatta Feb 6, 2024
be9b5d7
Merge branch 'master' into users/kundadebdatta/4236_add_custom_domain…
kundadebdatta Feb 7, 2024
3a9d168
Code changes to address review comments.
kundadebdatta Feb 8, 2024
581c6af
Merge branch 'master' into users/kundadebdatta/4236_add_custom_domain…
kundadebdatta Feb 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Microsoft.Azure.Cosmos/src/ConnectionPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal sealed class ConnectionPolicy

private Protocol connectionProtocol;
private ObservableCollection<string> preferredLocations;
private ObservableCollection<Uri> accountInitializationCustomEndpoints;

/// <summary>
/// Initializes a new instance of the <see cref="ConnectionPolicy"/> class to connect to the Azure Cosmos DB service.
Expand All @@ -43,6 +44,7 @@ public ConnectionPolicy()
this.MediaReadMode = MediaReadMode.Buffered;
this.UserAgentContainer = new UserAgentContainer(clientId: 0);
this.preferredLocations = new ObservableCollection<string>();
this.accountInitializationCustomEndpoints = new ObservableCollection<Uri>();
this.EnableEndpointDiscovery = true;
this.MaxConnectionLimit = defaultMaxConcurrentConnectionLimit;
this.RetryOptions = new RetryOptions();
Expand Down Expand Up @@ -90,6 +92,27 @@ public void SetPreferredLocations(IReadOnlyList<string> regions)
}
}

/// <summary>
/// Sets the custom private endpoints required to fetch account information from
/// private domain names.
/// </summary>
/// <param name="customEndpoints">An instance of <see cref="IEnumerable{T}"/> containing the custom DNS endpoints
/// provided by the customer.</param>
public void SetAccountInitializationCustomEndpoints(
IEnumerable<Uri> customEndpoints)
{
if (customEndpoints == null)
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentNullException(nameof(customEndpoints));
}

this.accountInitializationCustomEndpoints.Clear();
foreach (Uri endpoint in customEndpoints)
{
this.accountInitializationCustomEndpoints.Add(endpoint);
}
}

/// <summary>
/// Gets or sets the maximum number of concurrent fanout requests sent to the Azure Cosmos DB service.
/// </summary>
Expand Down Expand Up @@ -270,6 +293,24 @@ public Collection<string> PreferredLocations
}
}

/// <summary>
/// Gets the custom private endpoints for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// <para>
/// During the CosmosClient initialization the account information, including the available regions, is obtained from the <see cref="CosmosClient.Endpoint"/>.
/// Should the global endpoint become inaccessible, the CosmosClient will attempt to obtain the account information issuing requests to the custom endpoints
/// provided in the customAccountEndpoints list.
/// </para>
/// </remarks>
public Collection<Uri> AccountInitializationCustomEndpoints
{
get
{
return this.accountInitializationCustomEndpoints;
}
}

/// <summary>
/// Gets or sets the flag to enable endpoint discovery for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
Expand Down
41 changes: 41 additions & 0 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,42 @@ public string ApplicationName
/// </example>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/high-availability#high-availability-with-cosmos-db-in-the-event-of-regional-outages">High availability on regional outages</seealso>
public IReadOnlyList<string> ApplicationPreferredRegions { get; set; }

/// <summary>
/// Gets and sets the custom endpoints to use for account initialization for geo-replicated database accounts in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// <para>
/// During the CosmosClient initialization the account information, including the available regions, is obtained from the <see cref="CosmosClient.Endpoint"/>.
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
/// Should the global endpoint become inaccessible, the CosmosClient will attempt to obtain the account information issuing requests to the custom endpoints provided in <see cref="AccountInitializationCustomEndpoints"/>.
/// </para>
/// <para>
/// Nevertheless, this parameter remains optional and is recommended for implementation when a customer has configured an endpoint with a custom DNS hostname
/// (instead of accountname-region.documents.azure.com) etc. for their Cosmos DB account.
/// </para>
/// <para>
/// See also <seealso href="https://docs.microsoft.com/azure/cosmos-db/sql/troubleshoot-sdk-availability">Diagnose
/// and troubleshoot the availability of Cosmos SDKs</seealso> for more details.
/// </para>
/// </remarks>
/// <example>
/// <code language="c#">
/// <![CDATA[
/// CosmosClientOptions clientOptions = new CosmosClientOptions()
/// {
/// AccountInitializationCustomEndpoints = new HashSet<Uri>()
/// {
/// new Uri("custom.p-1.documents.azure.com"),
/// new Uri("custom.p-2.documents.azure.com")
/// }
/// };
///
/// CosmosClient client = new CosmosClient("endpoint", "key", clientOptions);
/// ]]>
/// </code>
/// </example>
/// <seealso href="https://docs.microsoft.com/azure/cosmos-db/high-availability#high-availability-with-cosmos-db-in-the-event-of-regional-outages">High availability on regional outages</seealso>
public IEnumerable<Uri> AccountInitializationCustomEndpoints { get; set; }

/// <summary>
/// Get or set the maximum number of concurrent connections allowed for the target
Expand Down Expand Up @@ -847,6 +883,11 @@ internal virtual ConnectionPolicy GetConnectionPolicy(int clientId)
List<string> mappedRegions = this.ApplicationPreferredRegions.Select(s => mapper.GetCosmosDBRegionName(s)).ToList();

connectionPolicy.SetPreferredLocations(mappedRegions);
}

if (this.AccountInitializationCustomEndpoints != null)
{
connectionPolicy.SetAccountInitializationCustomEndpoints(this.AccountInitializationCustomEndpoints);
}

if (this.MaxRetryAttemptsOnRateLimitedRequests != null)
Expand Down
35 changes: 35 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,41 @@ public CosmosClientBuilder WithApplicationPreferredRegions(IReadOnlyList<string>
return this;
}

/// <summary>
kundadebdatta marked this conversation as resolved.
Show resolved Hide resolved
/// Sets the custom endpoints to use for account initialization for geo-replicated database accounts in the Azure Cosmos DB service.
/// During the CosmosClient initialization the account information, including the available regions, is obtained from the <see cref="CosmosClient.Endpoint"/>.
/// Should the global endpoint become inaccessible, the CosmosClient will attempt to obtain the account information issuing requests to the custom endpoints
/// provided in the customAccountEndpoints list.
/// </summary>
/// <param name="customAccountEndpoints">An instance of <see cref="IEnumerable{T}"/> of Uri containing the custom private endpoints for the cosmos db account.</param>
/// <remarks>
/// This function is optional and is recommended for implementation when a customer has configured one or more endpoints with a custom DNS
/// hostname (instead of accountname-region.documents.azure.com) etc. for their Cosmos DB account.
/// </remarks>
/// <example>
/// The example below creates a new instance of <see cref="CosmosClientBuilder"/> with the regional endpoints.
/// <code language="c#">
/// <![CDATA[
/// CosmosClientBuilder cosmosClientBuilder = new CosmosClientBuilder(
/// accountEndpoint: "https://testcosmos.documents.azure.com:443/",
/// authKeyOrResourceToken: "SuperSecretKey")
/// .WithCustomAccountEndpoints(new HashSet<Uri>()
/// {
/// new Uri("https://region-1.documents-test.windows-int.net:443/"),
/// new Uri("https://region-2.documents-test.windows-int.net:443/")
/// });
/// CosmosClient client = cosmosClientBuilder.Build();
/// ]]>
/// </code>
/// </example>
/// <returns>The current <see cref="CosmosClientBuilder"/>.</returns>
/// <seealso cref="CosmosClientOptions.AccountInitializationCustomEndpoints"/>
public CosmosClientBuilder WithCustomAccountEndpoints(IEnumerable<Uri> customAccountEndpoints)
{
this.clientOptions.AccountInitializationCustomEndpoints = customAccountEndpoints;
return this;
}

/// <summary>
/// Limits the operations to the provided endpoint on the CosmosClientBuilder constructor.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/GatewayAccountReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Globalization;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -89,6 +88,7 @@ public async Task<AccountProperties> InitializeReaderAsync()
AccountProperties databaseAccount = await GlobalEndpointManager.GetDatabaseAccountFromAnyLocationsAsync(
defaultEndpoint: this.serviceEndpoint,
locations: this.connectionPolicy.PreferredLocations,
accountInitializationCustomEndpoints: this.connectionPolicy.AccountInitializationCustomEndpoints,
getDatabaseAccountFn: this.GetDatabaseAccountAsync,
cancellationToken: this.cancellationToken);

Expand Down
Loading