-
Notifications
You must be signed in to change notification settings - Fork 7
/
TablePrefixScanner.cs
465 lines (407 loc) · 19.2 KB
/
TablePrefixScanner.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Azure;
using Azure.Data.Tables;
using NuGet.Insights.StorageNoOpRetry;
using static NuGet.Insights.StorageUtility;
namespace NuGet.Insights.TablePrefixScan
{
public class TablePrefixScanner
{
private readonly ITelemetryClient _telemetryClient;
private readonly ILogger<TablePrefixScanner> _logger;
public TablePrefixScanner(
ITelemetryClient telemetryClient,
ILogger<TablePrefixScanner> logger)
{
_telemetryClient = telemetryClient;
_logger = logger;
}
public async Task<List<T>> ListAsync<T>(
TableClientWithRetryContext table,
string partitionKeyPrefix)
where T : class, ITableEntity, new()
{
return await ListAsync<T>(
table,
partitionKeyPrefix,
selectColumns: null,
takeCount: MaxTakeCount);
}
public async Task<List<T>> ListAsync<T>(
TableClientWithRetryContext table,
string partitionKeyPrefix,
IList<string> selectColumns,
int takeCount)
where T : class, ITableEntity, new()
{
return await ListAsync<T>(
table,
partitionKeyPrefix,
selectColumns,
takeCount,
expandPartitionKeys: true,
segmentsPerFirstPrefix: 1,
segmentsPerSubsequentPrefix: 1);
}
public async Task<List<T>> ListAsync<T>(
TableClientWithRetryContext table,
string partitionKeyPrefix,
string partitionKeyLowerBound,
string partitionKeyUpperBound,
IList<string> selectColumns,
int takeCount)
where T : class, ITableEntity, new()
{
return await ListAsync<T>(
table,
partitionKeyPrefix,
partitionKeyLowerBound,
partitionKeyUpperBound,
selectColumns,
takeCount,
expandPartitionKeys: true,
segmentsPerFirstPrefix: 1,
segmentsPerSubsequentPrefix: 1);
}
public async Task<List<T>> ListAsync<T>(
TableClientWithRetryContext table,
string partitionKeyPrefix,
IList<string> selectColumns,
int takeCount,
bool expandPartitionKeys,
int segmentsPerFirstPrefix,
int segmentsPerSubsequentPrefix)
where T : class, ITableEntity, new()
{
var output = await ListAsync<T, T>(
table,
partitionKeyPrefix,
partitionKeyLowerBound: null,
partitionKeyUpperBound: null,
selectColumns,
takeCount,
expandPartitionKeys,
segmentsPerFirstPrefix,
segmentsPerSubsequentPrefix,
addSegment: (x, o) => o.AddRange(x));
_logger.LogInformation("Completed prefix scan. Found {Count} entities.", output.Count);
return output;
}
public async Task<List<T>> ListAsync<T>(
TableClientWithRetryContext table,
string partitionKeyPrefix,
string partitionKeyLowerBound,
string partitionKeyUpperBound,
IList<string> selectColumns,
int takeCount,
bool expandPartitionKeys,
int segmentsPerFirstPrefix,
int segmentsPerSubsequentPrefix)
where T : class, ITableEntity, new()
{
var output = await ListAsync<T, T>(
table,
partitionKeyPrefix,
partitionKeyLowerBound,
partitionKeyUpperBound,
selectColumns,
takeCount,
expandPartitionKeys,
segmentsPerFirstPrefix,
segmentsPerSubsequentPrefix,
addSegment: (x, o) => o.AddRange(x));
_logger.LogInformation("Completed prefix scan. Found {Count} entities.", output.Count);
return output;
}
public async Task<List<IReadOnlyList<T>>> ListSegmentsAsync<T>(
TableClientWithRetryContext table,
string partitionKeyPrefix,
IList<string> selectColumns,
int takeCount,
bool expandPartitionKeys,
int segmentsPerFirstPrefix,
int segmentsPerSubsequentPrefix)
where T : class, ITableEntity, new()
{
var output = await ListAsync<T, IReadOnlyList<T>>(
table,
partitionKeyPrefix,
partitionKeyLowerBound: null,
partitionKeyUpperBound: null,
selectColumns,
takeCount,
expandPartitionKeys,
segmentsPerFirstPrefix,
segmentsPerSubsequentPrefix,
addSegment: (x, o) => o.Add(x));
_logger.LogInformation("Completed prefix scan. Found {Count} segments.", output.Count);
return output;
}
private async Task<List<TOutput>> ListAsync<T, TOutput>(
TableClientWithRetryContext table,
string partitionKeyPrefix,
string partitionKeyLowerBound,
string partitionKeyUpperBound,
IList<string> selectColumns,
int takeCount,
bool expandPartitionKeys,
int segmentsPerFirstPrefix,
int segmentsPerSubsequentPrefix,
Action<IReadOnlyList<T>, List<TOutput>> addSegment) where T : class, ITableEntity, new()
{
if (selectColumns != null && (!selectColumns.Contains(PartitionKey) || !selectColumns.Contains(RowKey)))
{
throw new ArgumentException($"The {PartitionKey} and {RowKey} columns must be queried.", nameof(selectColumns));
}
_logger.LogInformation(
"Starting prefix scan with " +
"partition key prefix '{Prefix}', " +
"partition key lower bound '{LowerBound}', " +
"partition key upper bound '{UpperBound}', " +
"select columns '{SelectColumns}', " +
"take count {TakeCount}, " +
"segments per first prefix {SegmentsPerFirstPrefix}, " +
"segments per subsequent prefix {SegmentsPerSubsequentPrefix}.",
partitionKeyPrefix,
partitionKeyLowerBound,
partitionKeyUpperBound,
selectColumns,
takeCount,
segmentsPerFirstPrefix,
segmentsPerSubsequentPrefix);
var output = new List<TOutput>();
var parameters = new TableQueryParameters(table, selectColumns, takeCount, expandPartitionKeys);
var start = new TablePrefixScanStart(parameters, partitionKeyPrefix, partitionKeyLowerBound, partitionKeyUpperBound);
var initialSteps = Start(start);
initialSteps.Reverse();
var remainingSteps = new Stack<TablePrefixScanStep>(initialSteps);
while (remainingSteps.Any())
{
var currentStep = remainingSteps.Pop();
_logger.LogInformation("At depth {Depth}, processing prefix scan step: {Step}", currentStep.Depth, currentStep);
IReadOnlyList<TablePrefixScanStep> newSteps;
switch (currentStep)
{
case TablePrefixScanEntitySegment<T> segment:
newSteps = Array.Empty<TablePrefixScanEntitySegment<T>>();
addSegment(segment.Entities, output);
break;
case TablePrefixScanPartitionKeyQuery partitionKeyQuery:
newSteps = await ExecutePartitionKeyQueryAsync<T>(partitionKeyQuery);
break;
case TablePrefixScanPrefixQuery prefixQuery:
newSteps = await ExecutePrefixQueryAsync<T>(prefixQuery, segmentsPerFirstPrefix, segmentsPerSubsequentPrefix);
break;
default:
throw new NotImplementedException();
}
foreach (var newRange in newSteps.Reverse())
{
remainingSteps.Push(newRange);
}
}
return output;
}
public List<TablePrefixScanStep> Start(TablePrefixScanStart start)
{
var steps = new List<TablePrefixScanStep>();
var nextDepth = start.Depth + 1;
// Originally I have a NULL character '\0' for both the row key and partition key prefix lower bound but
// the Azure Storage Emulator behaved different for this. It looked like it completely ignored the '\0'
// included in the query. Real Azure Table Storage does not ignore the NULL byte.
if (start.Parameters.ExpandPartitionKeys
&& (start.PartitionKeyLowerBound is null || string.CompareOrdinal(start.PartitionKeyPrefix, start.PartitionKeyLowerBound) > 0)
&& (start.PartitionKeyUpperBound is null || string.CompareOrdinal(start.PartitionKeyPrefix, start.PartitionKeyUpperBound) < 0))
{
steps.Add(new TablePrefixScanPartitionKeyQuery(start.Parameters, nextDepth, start.PartitionKeyPrefix, rowKeySkip: null));
}
var defaultLowerBound = start.PartitionKeyPrefix;
var defaultUpperBound = start.PartitionKeyPrefix + char.MaxValue;
steps.Add(new TablePrefixScanPrefixQuery(
start.Parameters,
nextDepth,
partitionKeyPrefix: start.PartitionKeyPrefix,
partitionKeyLowerBound: Max(defaultLowerBound, start.PartitionKeyLowerBound ?? defaultLowerBound),
partitionKeyUpperBound: Min(defaultUpperBound, start.PartitionKeyUpperBound ?? defaultUpperBound)));
return steps;
}
public async Task<List<TablePrefixScanStep>> ExecutePartitionKeyQueryAsync<T>(TablePrefixScanPartitionKeyQuery query) where T : class, ITableEntity, new()
{
using var metrics = _telemetryClient.StartQueryLoopMetrics();
Expression<Func<T, bool>> filter;
if (query.RowKeySkip is not null)
{
// Match the provided partition key, skip past the provided row key
filter = x =>
x.PartitionKey == query.PartitionKey
&& string.Compare(x.RowKey, query.RowKeySkip, StringComparison.Ordinal) > 0;
}
else
{
// Match the provided partition key
filter = x =>
x.PartitionKey == query.PartitionKey;
}
var tablePageQuery = query.Parameters.Table
.QueryAsync(
filter,
maxPerPage: query.Parameters.TakeCount,
select: query.Parameters.SelectColumns)
.AsPages();
var output = new List<TablePrefixScanStep>();
await using var enumerator = tablePageQuery.GetAsyncEnumerator();
while (await enumerator.MoveNextAsync(metrics))
{
if (enumerator.Current.Values.Any())
{
output.Add(new TablePrefixScanEntitySegment<T>(query.Parameters, query.Depth + 1, enumerator.Current.Values));
}
}
return output;
}
public async Task<List<TablePrefixScanStep>> ExecutePrefixQueryAsync<T>(TablePrefixScanPrefixQuery query) where T : class, ITableEntity, new()
{
return await ExecutePrefixQueryAsync<T>(query, segmentsPerFirstPrefix: 1, segmentsPerSubsequentPrefix: 1);
}
public async Task<List<TablePrefixScanStep>> ExecutePrefixQueryAsync<T>(
TablePrefixScanPrefixQuery query,
int segmentsPerFirstPrefix,
int segmentsPerSubsequentPrefix)
where T : class, ITableEntity, new()
{
if (segmentsPerFirstPrefix < 1)
{
throw new ArgumentOutOfRangeException(nameof(segmentsPerFirstPrefix), segmentsPerFirstPrefix, "The number of segments per first prefix must be at least 1.");
}
if (segmentsPerSubsequentPrefix < 1)
{
throw new ArgumentOutOfRangeException(nameof(segmentsPerSubsequentPrefix), segmentsPerSubsequentPrefix, "The number of segments per subsequent prefix must be at least 1.");
}
using var metrics = _telemetryClient.StartQueryLoopMetrics();
//
// Consider the following query, where we're enumerating all partition keys starting with '$'.
//
// QUERY = get 3 entities where PK > '$\0' and PK < '$\uffff'
//
// +- PK --- RK -+
// | $1_0 | 10.0 |
// RESULT = | $1_0 | 11.0 | and a non-null continuation token
// | $2_A | 12.0 |
// +-------------+
//
// From the result set, we can deduce the following facts:
// 1. The '$1' prefix is totally discovered. No more work in this space is necessary.
// 2. The '$2_A' partition key exists but we don't know how many row keys are in it.
// 3. The '$2' prefix exists but we don't know how many partition keys are in it.
//
// Therefore we yield three types of results from each fact.
// 1. A terminal result, containing all three rows.
// 2. A result to enumerate all of the row keys for partition key '$2_A', starting after row key '12.0'.
// 3. A result to expand the '$2' prefix further, starting after partition key '$2_A'
//
// This method that we're in will also see that we've reached '$2_A' and will continue expanding the '$'
// prefix to find partition keys '$3_A' and on with a query like this:
//
// QUERY = get 3 entities where PK > '$2\uffff' and PK < '$\uffff'
//
var output = new List<TablePrefixScanStep>();
var upperBound = query.PartitionKeyUpperBound;
string lastPartitionKey = null;
var segmentsPerPrefix = segmentsPerFirstPrefix;
while (true)
{
var lowerBound = lastPartitionKey == null ? query.PartitionKeyLowerBound : IncrementPrefix(query.PartitionKeyPrefix, lastPartitionKey) + char.MaxValue;
Expression<Func<T, bool>> filter = x =>
string.Compare(x.PartitionKey, lowerBound, StringComparison.Ordinal) > 0 // Skip past the first character of last partition key seen.
&& string.Compare(x.PartitionKey, upperBound, StringComparison.Ordinal) < 0; // Don't go outside of the provided prefix
var tablePageQuery = query.Parameters.Table
.QueryAsync(
filter,
maxPerPage: query.Parameters.TakeCount,
select: query.Parameters.SelectColumns)
.AsPages();
// Find N segments with at least one entity. I'm not entirely sure if it's possible to get zero
// entities but have a continuation token, so let's protect against that.
var results = new List<T>();
var segmentCount = 0;
Page<T> segment = null;
await using var enumerator = tablePageQuery.GetAsyncEnumerator();
while (segmentCount < segmentsPerPrefix && await enumerator.MoveNextAsync(metrics))
{
segment = enumerator.Current;
if (segment.Values.Any())
{
results.AddRange(segment.Values);
segmentCount++;
}
}
segmentsPerPrefix = segmentsPerSubsequentPrefix;
if (!results.Any())
{
break;
}
lastPartitionKey = results.Last().PartitionKey;
var hasMore = segment.ContinuationToken != null;
output.AddRange(MakeResults(query, results, hasMore));
if (!hasMore)
{
break;
}
}
return output;
}
private static string IncrementPrefix(string partitionKeyPrefix, string partitionKey)
{
string nextChar;
if (char.IsHighSurrogate(partitionKey[partitionKeyPrefix.Length]))
{
nextChar = partitionKey.Substring(partitionKeyPrefix.Length, 2);
}
else
{
nextChar = partitionKey.Substring(partitionKeyPrefix.Length, 1);
}
var prefix = partitionKeyPrefix + nextChar;
return prefix;
}
private static IEnumerable<TablePrefixScanStep> MakeResults<T>(TablePrefixScanPrefixQuery step, List<T> results, bool hasMore) where T : ITableEntity, new()
{
if (!results.Any())
{
throw new ArgumentException("The segment must have at least one entity.");
}
var nextDepth = step.Depth + 1;
// Produce a terminal node for the discovered results.
yield return new TablePrefixScanEntitySegment<T>(step.Parameters, nextDepth, results);
if (hasMore)
{
var last = results.Last();
if (step.Parameters.ExpandPartitionKeys)
{
// Find the remaining row keys for the partition key that straddles the current and subsequent page.
// It's possible that this partition key has very few rows or even only has a single row meaning that this
// yield query may not result in very many records. That's OK. We don't want to read too far into the
// prefix in a serial fashion.
yield return new TablePrefixScanPartitionKeyQuery(step.Parameters, nextDepth, last.PartitionKey, last.RowKey);
}
// Expand the next prefix of the last partition key.
var nextPrefix = IncrementPrefix(step.PartitionKeyPrefix, last.PartitionKey);
yield return new TablePrefixScanPrefixQuery(
step.Parameters,
nextDepth,
partitionKeyPrefix: nextPrefix,
partitionKeyLowerBound: Max(step.PartitionKeyLowerBound, last.PartitionKey),
partitionKeyUpperBound: Min(step.PartitionKeyUpperBound, nextPrefix + char.MaxValue));
}
}
private static string Min(string a, string b)
{
return string.CompareOrdinal(a, b) < 0 ? a : b;
}
private static string Max(string a, string b)
{
return string.CompareOrdinal(a, b) > 0 ? a : b;
}
}
}