-
Notifications
You must be signed in to change notification settings - Fork 491
/
GlobalPartitionEndpointManagerCore.cs
225 lines (190 loc) · 8.53 KB
/
GlobalPartitionEndpointManagerCore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
#nullable enable
namespace Microsoft.Azure.Cosmos.Routing
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Documents;
/// <summary>
/// This class is used to failover single partitions to different regions.
/// The client retry policy will mark a partition as down. The PartitionKeyRangeToLocation
/// will add an override to the next read region. When the request is retried it will
/// override the default location with the new region from the PartitionKeyRangeToLocation.
/// </summary>
internal sealed class GlobalPartitionEndpointManagerCore : GlobalPartitionEndpointManager
{
private readonly IGlobalEndpointManager globalEndpointManager;
private readonly Lazy<ConcurrentDictionary<PartitionKeyRange, PartitionKeyRangeFailoverInfo>> PartitionKeyRangeToLocation = new Lazy<ConcurrentDictionary<PartitionKeyRange, PartitionKeyRangeFailoverInfo>>(
() => new ConcurrentDictionary<PartitionKeyRange, PartitionKeyRangeFailoverInfo>());
public GlobalPartitionEndpointManagerCore(
IGlobalEndpointManager globalEndpointManager)
{
this.globalEndpointManager = globalEndpointManager ?? throw new ArgumentNullException(nameof(globalEndpointManager));
}
private bool CanUsePartitionLevelFailoverLocations(DocumentServiceRequest request)
{
if (this.globalEndpointManager.ReadEndpoints.Count <= 1)
{
return false;
}
if (request.ResourceType == ResourceType.Document ||
(request.ResourceType == ResourceType.StoredProcedure && request.OperationType == Documents.OperationType.ExecuteJavaScript))
{
// Disable for multimaster because it currently
// depends on 403.3 to signal the primary region is backup
// and to fail back over
if (!this.globalEndpointManager.CanUseMultipleWriteLocations(request))
{
return true;
}
}
return false;
}
public override bool TryAddPartitionLevelLocationOverride(
DocumentServiceRequest request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
if (request.RequestContext == null)
{
return false;
}
if (!this.CanUsePartitionLevelFailoverLocations(request))
{
return false;
}
PartitionKeyRange? partitionKeyRange = request.RequestContext.ResolvedPartitionKeyRange;
if (partitionKeyRange == null)
{
return false;
}
if (this.PartitionKeyRangeToLocation.IsValueCreated
&& this.PartitionKeyRangeToLocation.Value.TryGetValue(
partitionKeyRange,
out PartitionKeyRangeFailoverInfo partitionKeyRangeFailover))
{
DefaultTrace.TraceVerbose("Partition level override. URI: {0}, PartitionKeyRange: {1}",
partitionKeyRangeFailover.Current,
partitionKeyRange.Id);
request.RequestContext.RouteToLocation(partitionKeyRangeFailover.Current);
return true;
}
return false;
}
/// <summary>
/// Marks the current location unavailable for write
/// </summary>
public override bool TryMarkEndpointUnavailableForPartitionKeyRange(
DocumentServiceRequest request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
// Only do partition level failover if it is a write operation.
// Write operation will throw a write forbidden if it is not the primary
// region.
if (request.IsReadOnlyRequest)
{
return false;
}
if (request.RequestContext == null)
{
return false;
}
if (!this.CanUsePartitionLevelFailoverLocations(request))
{
return false;
}
PartitionKeyRange? partitionKeyRange = request.RequestContext.ResolvedPartitionKeyRange;
if (partitionKeyRange == null)
{
return false;
}
Uri? failedLocation = request.RequestContext.LocationEndpointToRoute;
if (failedLocation == null)
{
return false;
}
PartitionKeyRangeFailoverInfo partionFailover = this.PartitionKeyRangeToLocation.Value.GetOrAdd(
partitionKeyRange,
(_) => new PartitionKeyRangeFailoverInfo(failedLocation));
// For any single master write accounts, the next locations to fail over will be the read regions configured at the account level.
// For multi master write accounts, since all the regions are treated as write regions, the next locations to fail over
// will be the preferred read regions that are configured in the application preferred regions in the CosmosClientOptions.
bool isSingleMasterWriteAccount = !this.globalEndpointManager.CanUseMultipleWriteLocations(request);
ReadOnlyCollection<Uri> nextLocations = isSingleMasterWriteAccount
? this.globalEndpointManager.AccountReadEndpoints
: this.globalEndpointManager.ReadEndpoints;
// Will return true if it was able to update to a new region
if (partionFailover.TryMoveNextLocation(
locations: nextLocations,
failedLocation: failedLocation))
{
DefaultTrace.TraceInformation("Partition level override added to new location. PartitionKeyRange: {0}, failedLocation: {1}, new location: {2}",
partitionKeyRange,
failedLocation,
partionFailover.Current);
return true;
}
// All the locations have been tried. Remove the override information
DefaultTrace.TraceInformation("Partition level override removed. PartitionKeyRange: {0}, failedLocation: {1}",
partitionKeyRange,
failedLocation);
this.PartitionKeyRangeToLocation.Value.TryRemove(partitionKeyRange, out PartitionKeyRangeFailoverInfo _);
return false;
}
internal sealed class PartitionKeyRangeFailoverInfo
{
// HashSet is not thread safe and should only accessed in the lock
private readonly HashSet<Uri> FailedLocations;
public PartitionKeyRangeFailoverInfo(
Uri currentLocation)
{
this.Current = currentLocation;
this.FailedLocations = new HashSet<Uri>();
}
public Uri Current { get; private set; }
public bool TryMoveNextLocation(
IReadOnlyCollection<Uri> locations,
Uri failedLocation)
{
// Another thread already updated it
if (failedLocation != this.Current)
{
return true;
}
lock (this.FailedLocations)
{
// Another thread already updated it
if (failedLocation != this.Current)
{
return true;
}
foreach (Uri? location in locations)
{
if (this.Current == location)
{
continue;
}
if (this.FailedLocations.Contains(location))
{
continue;
}
this.FailedLocations.Add(failedLocation);
this.Current = location;
return true;
}
}
return false;
}
}
}
}