-
Notifications
You must be signed in to change notification settings - Fork 541
Expand file tree
/
Copy pathCollectionRoutingMap.cs
More file actions
290 lines (240 loc) · 12.4 KB
/
Copy pathCollectionRoutingMap.cs
File metadata and controls
290 lines (240 loc) · 12.4 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Routing
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Routing;
/// <summary>
/// Stored partition key ranges in an efficient way with some additional information and provides
/// convenience methods for working with set of ranges.
/// </summary>
internal sealed class CollectionRoutingMap
{
/// <summary>
/// Partition key range id to partition address and range.
/// </summary>
private readonly Dictionary<string, Tuple<PartitionKeyRange, ServiceIdentity>> rangeById;
private readonly List<PartitionKeyRange> orderedPartitionKeyRanges;
private readonly List<Range<string>> orderedRanges;
private readonly HashSet<string> goneRanges;
private readonly static int InvalidPkRangeId = -1;
internal int HighestNonOfflinePkRangeId { get; private set; }
private readonly (IComparer<Range<string>> MinComparer, IComparer<Range<string>> MaxComparer) comparers;
private CollectionRoutingMap(
Dictionary<string, Tuple<PartitionKeyRange, ServiceIdentity>> rangeById,
List<PartitionKeyRange> orderedPartitionKeyRanges,
string collectionUniqueId,
string changeFeedNextIfNoneMatch)
{
this.rangeById = rangeById;
this.orderedPartitionKeyRanges = orderedPartitionKeyRanges;
this.orderedRanges = orderedPartitionKeyRanges.Select(
range =>
new Range<string>(
range.MinInclusive,
range.MaxExclusive,
true,
false)).ToList();
this.CollectionUniqueId = collectionUniqueId;
this.ChangeFeedNextIfNoneMatch = changeFeedNextIfNoneMatch;
this.goneRanges = new HashSet<string>(orderedPartitionKeyRanges.SelectMany(r => r.Parents ?? Enumerable.Empty<string>()));
this.HighestNonOfflinePkRangeId = orderedPartitionKeyRanges.Max(range =>
{
int pkId = CollectionRoutingMap.InvalidPkRangeId;
if (!int.TryParse(range.Id, NumberStyles.Integer, CultureInfo.InvariantCulture, out pkId))
{
DefaultTrace.TraceCritical(
"Could not parse partition key range Id as int {0} for collectionRid {1}",
range.Id,
this.CollectionUniqueId);
throw new ArgumentException(string.Format(
CultureInfo.InvariantCulture,
"Could not parse partition key range Id as int {0} for collectionRid {1}",
range.Id,
this.CollectionUniqueId));
}
return range.Status == PartitionKeyRangeStatus.Offline ? CollectionRoutingMap.InvalidPkRangeId : pkId;
});
//LengthAwareComparer is the default Range comparer and flag <see cref="ConfigurationManager.UseLengthAwareRangeComparator"/> is used to ovverride the default comparer to legacy Min/Max comparer.
bool useLengthAwareComparer = ConfigurationManager.IsLengthAwareRangeComparatorEnabled();
this.comparers = useLengthAwareComparer
? (Range<string>.LengthAwareMinComparer.Instance, Range<string>.LengthAwareMaxComparer.Instance)
: (Range<string>.MinComparer.Instance, Range<string>.MaxComparer.Instance);
}
public static CollectionRoutingMap TryCreateCompleteRoutingMap(
IEnumerable<Tuple<PartitionKeyRange, ServiceIdentity>> ranges,
string collectionUniqueId,
string changeFeedNextIfNoneMatch = null)
{
Dictionary<string, Tuple<PartitionKeyRange, ServiceIdentity>> rangeById =
new Dictionary<string, Tuple<PartitionKeyRange, ServiceIdentity>>(StringComparer.Ordinal);
foreach (Tuple<PartitionKeyRange, ServiceIdentity> range in ranges)
{
rangeById[range.Item1.Id] = range;
}
List<Tuple<PartitionKeyRange, ServiceIdentity>> sortedRanges = rangeById.Values.ToList();
sortedRanges.Sort(new MinPartitionKeyTupleComparer());
List<PartitionKeyRange> orderedRanges = sortedRanges.Select(range => range.Item1).ToList();
if (!IsCompleteSetOfRanges(orderedRanges))
{
return null;
}
return new CollectionRoutingMap(rangeById, orderedRanges, collectionUniqueId, changeFeedNextIfNoneMatch);
}
public string CollectionUniqueId { get; private set; }
public string ChangeFeedNextIfNoneMatch { get; private set; }
/// <summary>
/// Ranges in increasing order.
/// </summary>
public IReadOnlyList<PartitionKeyRange> OrderedPartitionKeyRanges
{
get
{
return this.orderedPartitionKeyRanges;
}
}
public IReadOnlyList<PartitionKeyRange> GetOverlappingRanges(Range<string> range)
{
return this.GetOverlappingRanges(new[] { range });
}
public IReadOnlyList<PartitionKeyRange> GetOverlappingRanges(IReadOnlyList<Range<string>> providedPartitionKeyRanges)
{
if (providedPartitionKeyRanges == null)
{
throw new ArgumentNullException("providedPartitionKeyRanges");
}
SortedList<string, PartitionKeyRange> partitionRanges = new SortedList<string, PartitionKeyRange>();
// Algorithm: Use binary search to find the positions of the min key and max key in the routing map
// Then within that two positions, check for overlapping partition key ranges
foreach (Range<string> providedRange in providedPartitionKeyRanges)
{
int minIndex = this.orderedRanges.BinarySearch(providedRange, this.comparers.MinComparer);
if (minIndex < 0)
{
minIndex = Math.Max(0, (~minIndex) - 1);
}
int maxIndex = this.orderedRanges.BinarySearch(providedRange, this.comparers.MaxComparer);
if (maxIndex < 0)
{
maxIndex = Math.Min(this.OrderedPartitionKeyRanges.Count - 1, ~maxIndex);
}
for (int i = minIndex; i <= maxIndex; ++i)
{
if (Range<string>.CheckOverlapping(this.orderedRanges[i], providedRange))
{
partitionRanges[this.OrderedPartitionKeyRanges[i].MinInclusive] = this.OrderedPartitionKeyRanges[i];
}
}
}
return new ReadOnlyCollection<PartitionKeyRange>(partitionRanges.Values);
}
public PartitionKeyRange GetRangeByEffectivePartitionKey(string effectivePartitionKeyValue)
{
if (string.CompareOrdinal(effectivePartitionKeyValue, PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey) >= 0)
{
throw new ArgumentException("effectivePartitionKeyValue");
}
if (string.CompareOrdinal(PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey, effectivePartitionKeyValue) == 0)
{
return this.orderedPartitionKeyRanges[0];
}
int index = this.orderedRanges.BinarySearch(
new Range<string>(effectivePartitionKeyValue, effectivePartitionKeyValue, true, true),
Range<string>.MinComparer.Instance);
if (index < 0)
{
index = ~index - 1;
Debug.Assert(index >= 0);
Debug.Assert(this.orderedRanges[index].Contains(effectivePartitionKeyValue));
}
return this.orderedPartitionKeyRanges[index];
}
public PartitionKeyRange TryGetRangeByPartitionKeyRangeId(string partitionKeyRangeId)
{
Tuple<PartitionKeyRange, ServiceIdentity> addresses;
if (this.rangeById.TryGetValue(partitionKeyRangeId, out addresses))
{
return addresses.Item1;
}
return null;
}
public ServiceIdentity TryGetInfoByPartitionKeyRangeId(string partitionKeyRangeId)
{
Tuple<PartitionKeyRange, ServiceIdentity> addresses;
if (this.rangeById.TryGetValue(partitionKeyRangeId, out addresses))
{
return addresses.Item2;
}
return null;
}
public CollectionRoutingMap TryCombine(
IEnumerable<Tuple<PartitionKeyRange, ServiceIdentity>> ranges,
string changeFeedNextIfNoneMatch)
{
HashSet<string> newGoneRanges = new HashSet<string>(ranges.SelectMany(tuple => tuple.Item1.Parents ?? Enumerable.Empty<string>()));
newGoneRanges.UnionWith(this.goneRanges);
Dictionary<string, Tuple<PartitionKeyRange, ServiceIdentity>> newRangeById =
this.rangeById.Values.Where(tuple => !newGoneRanges.Contains(tuple.Item1.Id)).ToDictionary(tuple => tuple.Item1.Id, StringComparer.Ordinal);
foreach (Tuple<PartitionKeyRange, ServiceIdentity> tuple in ranges.Where(tuple => !newGoneRanges.Contains(tuple.Item1.Id)))
{
newRangeById[tuple.Item1.Id] = tuple;
DefaultTrace.TraceInformation(
"CollectionRoutingMap.TryCombine newRangeById[{0}] = {1}",
tuple.Item1.Id, tuple);
}
List<Tuple<PartitionKeyRange, ServiceIdentity>> sortedRanges = newRangeById.Values.ToList();
sortedRanges.Sort(new MinPartitionKeyTupleComparer());
List<PartitionKeyRange> newOrderedRanges = sortedRanges.Select(range => range.Item1).ToList();
if (!IsCompleteSetOfRanges(newOrderedRanges))
{
return null;
}
return new CollectionRoutingMap(newRangeById, newOrderedRanges, this.CollectionUniqueId, changeFeedNextIfNoneMatch);
}
private class MinPartitionKeyTupleComparer : IComparer<Tuple<PartitionKeyRange, ServiceIdentity>>
{
public int Compare(Tuple<PartitionKeyRange, ServiceIdentity> left, Tuple<PartitionKeyRange, ServiceIdentity> right)
{
return string.CompareOrdinal(left.Item1.MinInclusive, right.Item1.MinInclusive);
}
}
private static bool IsCompleteSetOfRanges(IList<PartitionKeyRange> orderedRanges)
{
bool isComplete = false;
if (orderedRanges.Count > 0)
{
PartitionKeyRange firstRange = orderedRanges[0];
PartitionKeyRange lastRange = orderedRanges[orderedRanges.Count - 1];
isComplete = string.CompareOrdinal(firstRange.MinInclusive, PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey) == 0;
isComplete &= string.CompareOrdinal(lastRange.MaxExclusive, PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey) == 0;
for (int i = 1; i < orderedRanges.Count; i++)
{
PartitionKeyRange previousRange = orderedRanges[i - 1];
PartitionKeyRange currentRange = orderedRanges[i];
isComplete &= previousRange.MaxExclusive.Equals(currentRange.MinInclusive);
if (!isComplete)
{
if (string.CompareOrdinal(previousRange.MaxExclusive, currentRange.MinInclusive) > 0)
{
throw new InvalidOperationException("Ranges overlap");
}
break;
}
}
}
return isComplete;
}
public bool IsGone(string partitionKeyRangeId)
{
return this.goneRanges.Contains(partitionKeyRangeId);
}
}
}