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

Performance: Fix CPU consumption with EnableTcpConnectionEndpointRediscovery #1682

Merged
merged 3 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<ClientOfficialVersion>3.10.1</ClientOfficialVersion>
<ClientPreviewVersion>3.9.1</ClientPreviewVersion>
<DirectVersion>3.11.1</DirectVersion>
<DirectVersion>3.11.2</DirectVersion>
<EncryptionVersion>1.0.0-preview4</EncryptionVersion>
<HybridRowVersion>1.0.0-preview</HybridRowVersion>
<AboveDirBuildProps>$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))</AboveDirBuildProps>
Expand Down
12 changes: 12 additions & 0 deletions Microsoft.Azure.Cosmos/src/Routing/AddressResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
Expand All @@ -13,6 +14,7 @@ namespace Microsoft.Azure.Cosmos
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Cosmos.Routing;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Rntbd;
using Microsoft.Azure.Documents.Routing;
using Newtonsoft.Json;

Expand Down Expand Up @@ -709,6 +711,16 @@ private PartitionKeyRange TryResolveServerPartitionByPartitionKey(
return null;
}

public Task UpdateAsync(ServerKey serverKey, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

public Task UpdateAsync(IReadOnlyList<AddressCacheToken> addressCacheTokens, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

private class ResolutionResult
{
public PartitionKeyRange TargetPartitionKeyRange { get; private set; }
Expand Down
73 changes: 68 additions & 5 deletions Microsoft.Azure.Cosmos/src/Routing/GatewayAddressCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Microsoft.Azure.Cosmos.Routing
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Collections;
using Microsoft.Azure.Documents.Rntbd;
using Microsoft.Azure.Documents.Routing;

internal class GatewayAddressCache : IAddressCache, IDisposable
Expand All @@ -33,12 +34,14 @@ internal class GatewayAddressCache : IAddressCache, IDisposable

private readonly AsyncCache<PartitionKeyRangeIdentity, PartitionAddressInformation> serverPartitionAddressCache;
private readonly ConcurrentDictionary<PartitionKeyRangeIdentity, DateTime> suboptimalServerPartitionTimestamps;
private readonly ConcurrentDictionary<ServerKey, HashSet<PartitionKeyRangeIdentity>> serverPartitionAddressToPkRangeIdMap;
private readonly IServiceConfigurationReader serviceConfigReader;
private readonly long suboptimalPartitionForceRefreshIntervalInSeconds;

private readonly Protocol protocol;
private readonly string protocolFilter;
private readonly IAuthorizationTokenProvider tokenProvider;
private readonly bool enableTcpConnectionEndpointRediscovery;

private HttpClient httpClient;

Expand All @@ -54,7 +57,8 @@ public GatewayAddressCache(
TimeSpan requestTimeout,
long suboptimalPartitionForceRefreshIntervalInSeconds = 600,
HttpMessageHandler messageHandler = null,
ApiType apiType = ApiType.None)
ApiType apiType = ApiType.None,
bool enableTcpConnectionEndpointRediscovery = false)
{
this.addressEndpoint = new Uri(serviceEndpoint + "/" + Paths.AddressPathSegment);
this.protocol = protocol;
Expand All @@ -63,7 +67,9 @@ public GatewayAddressCache(
this.serviceConfigReader = serviceConfigReader;
this.serverPartitionAddressCache = new AsyncCache<PartitionKeyRangeIdentity, PartitionAddressInformation>();
this.suboptimalServerPartitionTimestamps = new ConcurrentDictionary<PartitionKeyRangeIdentity, DateTime>();
this.serverPartitionAddressToPkRangeIdMap = new ConcurrentDictionary<ServerKey, HashSet<PartitionKeyRangeIdentity>>();
this.suboptimalMasterPartitionTimestamp = DateTime.MaxValue;
this.enableTcpConnectionEndpointRediscovery = enableTcpConnectionEndpointRediscovery;

this.suboptimalPartitionForceRefreshIntervalInSeconds = suboptimalPartitionForceRefreshIntervalInSeconds;

Expand Down Expand Up @@ -255,6 +261,46 @@ public async Task<PartitionAddressInformation> TryGetAddressesAsync(
}
}

public async Task TryUpdateAddressAsync(
ServerKey serverKey,
CancellationToken cancellationToken)
{
if (serverKey == null)
{
throw new ArgumentNullException(nameof(serverKey));
}

List<Task> tasks = new List<Task>();
HashSet<PartitionKeyRangeIdentity> pkRangeIds;
if (this.serverPartitionAddressToPkRangeIdMap.TryGetValue(serverKey, out pkRangeIds))
{
foreach (PartitionKeyRangeIdentity pkRangeId in pkRangeIds)
{
DefaultTrace.TraceInformation("Refresh addresses for collectionRid :{0}, pkRangeId: {1}, serviceEndpoint: {2}",
pkRangeId.CollectionRid,
pkRangeId.PartitionKeyRangeId,
this.serviceEndpoint);

tasks.Add(this.serverPartitionAddressCache.GetAsync(
pkRangeId,
null,
() => this.GetAddressesForRangeIdAsync(
null,
pkRangeId.CollectionRid,
pkRangeId.PartitionKeyRangeId,
forceRefresh: true),
cancellationToken,
forceRefresh: true));
}

// remove the server key from the map since we are updating the addresses
HashSet<PartitionKeyRangeIdentity> ignorePkRanges;
this.serverPartitionAddressToPkRangeIdMap.TryRemove(serverKey, out ignorePkRanges);
}

await Task.WhenAll(tasks);
}

public async Task<PartitionAddressInformation> UpdateAsync(
PartitionKeyRangeIdentity partitionKeyRangeIdentity,
CancellationToken cancellationToken)
Expand Down Expand Up @@ -528,12 +574,29 @@ internal Tuple<PartitionKeyRangeIdentity, PartitionAddressInformation> ToPartiti

PartitionKeyRangeIdentity partitionKeyRangeIdentity = new PartitionKeyRangeIdentity(collectionRid, address.PartitionKeyRangeId);

if (this.enableTcpConnectionEndpointRediscovery && partitionKeyRangeIdentity.PartitionKeyRangeId != PartitionKeyRange.MasterPartitionKeyRangeId)
{
// add serverKey-pkRangeIdentity mapping only for addresses retrieved from gateway
foreach (AddressInformation addressInfo in addressInfos)
{
DefaultTrace.TraceInformation("Added address to serverPartitionAddressToPkRangeIdMap, collectionRid :{0}, pkRangeId: {1}, address: {2}",
partitionKeyRangeIdentity.CollectionRid,
partitionKeyRangeIdentity.PartitionKeyRangeId,
addressInfo.PhysicalUri);

this.serverPartitionAddressToPkRangeIdMap.AddOrUpdate(
new ServerKey(new Uri(addressInfo.PhysicalUri)), new HashSet<PartitionKeyRangeIdentity>() { partitionKeyRangeIdentity },
(serverKey, pkRangeIds) =>
{
pkRangeIds.Add(partitionKeyRangeIdentity);
return pkRangeIds;
});
}
}

return Tuple.Create(
partitionKeyRangeIdentity,
new PartitionAddressInformation(
addressInfos,
partitionKeyRangeIdentity.PartitionKeyRangeId == PartitionKeyRange.MasterPartitionKeyRangeId ? null : partitionKeyRangeIdentity,
this.serviceEndpoint));
new PartitionAddressInformation(addressInfos));
}

private static string LogAddressResolutionStart(DocumentServiceRequest request, Uri targetEndpoint)
Expand Down
24 changes: 22 additions & 2 deletions Microsoft.Azure.Cosmos/src/Routing/GlobalAddressResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ namespace Microsoft.Azure.Cosmos.Routing
using Microsoft.Azure.Cosmos.Common;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Rntbd;

/// <summary>
/// AddressCache implementation for client SDK. Supports cross region address routing based on
/// avaialbility and preference list.
/// </summary>
internal sealed class GlobalAddressResolver : IAddressResolverExtension, IDisposable
internal sealed class GlobalAddressResolver : IAddressResolver, IDisposable
{
private const int MaxBackupReadRegions = 3;

Expand All @@ -35,6 +36,7 @@ internal sealed class GlobalAddressResolver : IAddressResolverExtension, IDispos
private readonly ConcurrentDictionary<Uri, EndpointCache> addressCacheByEndpoint;
private readonly TimeSpan requestTimeout;
private readonly ApiType apiType;
private readonly bool enableTcpConnectionEndpointRediscovery;

public GlobalAddressResolver(
GlobalEndpointManager endpointManager,
Expand Down Expand Up @@ -63,6 +65,8 @@ public GlobalAddressResolver(
!connectionPolicy.EnableReadRequestsFallback.HasValue || connectionPolicy.EnableReadRequestsFallback.Value
? GlobalAddressResolver.MaxBackupReadRegions : 0;

this.enableTcpConnectionEndpointRediscovery = connectionPolicy.EnableTcpConnectionEndpointRediscovery;

this.maxEndpoints = maxBackupReadEndpoints + 2; // for write and alternate write endpoint (during failover)

this.addressCacheByEndpoint = new ConcurrentDictionary<Uri, EndpointCache>();
Expand Down Expand Up @@ -131,6 +135,21 @@ public async Task UpdateAsync(
await Task.WhenAll(tasks);
}

public async Task UpdateAsync(
ServerKey serverKey,
CancellationToken cancellationToken)
{
List<Task> tasks = new List<Task>();

foreach (KeyValuePair<Uri, EndpointCache> addressCache in this.addressCacheByEndpoint)
{
// since we don't know which address cache contains the pkRanges mapped to this node, we do a tryUpdate on all AddressCaches of all regions
tasks.Add(addressCache.Value.AddressCache.TryUpdateAddressAsync(serverKey, cancellationToken));
}

await Task.WhenAll(tasks);
}

/// <summary>
/// ReplicatedResourceClient will use this API to get the direct connectivity AddressCache for given request.
/// </summary>
Expand Down Expand Up @@ -164,7 +183,8 @@ private EndpointCache GetOrAddEndpoint(Uri endpoint)
this.serviceConfigReader,
this.requestTimeout,
messageHandler: this.messageHandler,
apiType: this.apiType);
apiType: this.apiType,
enableTcpConnectionEndpointRediscovery: this.enableTcpConnectionEndpointRediscovery);

string location = this.endpointManager.GetLocation(endpoint);
AddressResolver addressResolver = new AddressResolver(null, new NullRequestSigner(), location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ private StoreClient GetMockStoreClient()
AddressSelector addressSelector = new AddressSelector(mockAddressCache.Object, Protocol.Tcp);
TransportClient mockTransportClient = this.GetMockTransportClient();
ISessionContainer sessionContainer = new SessionContainer(string.Empty);
var connectionStateListener = new ConnectionStateListener(null);

StoreReader storeReader = new StoreReader(mockTransportClient, addressSelector, sessionContainer, connectionStateListener);
StoreReader storeReader = new StoreReader(mockTransportClient, addressSelector, sessionContainer);

Mock<IAuthorizationTokenProvider> mockAuthorizationTokenProvider = new Mock<IAuthorizationTokenProvider>();
mockAuthorizationTokenProvider.Setup(provider => provider.AddSystemAuthorizationHeaderAsync(
Expand Down Expand Up @@ -149,7 +148,7 @@ private Mock<IAddressResolver> GetMockAddressCache()
It.IsAny<DocumentServiceRequest>(),
It.IsAny<bool>(),
new CancellationToken()))
.ReturnsAsync(new PartitionAddressInformation(addressInformation, null, null));
.ReturnsAsync(new PartitionAddressInformation(addressInformation));

return mockAddressCache;
}
Expand Down
Loading