-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTableCopyEnqueuer.cs
153 lines (140 loc) · 6.23 KB
/
TableCopyEnqueuer.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Table;
namespace Knapcode.ExplorePackages.Worker.TableCopy
{
public class TableCopyEnqueuer<T> where T : ITableEntity, new()
{
private readonly MessageEnqueuer _enqueuer;
private readonly SchemaSerializer _serializer;
private readonly ILogger<TableCopyEnqueuer<T>> _logger;
public TableCopyEnqueuer(
MessageEnqueuer enqueuer,
SchemaSerializer serializer,
ILogger<TableCopyEnqueuer<T>> logger)
{
_enqueuer = enqueuer;
_serializer = serializer;
_logger = logger;
}
public async Task StartSerialAsync(string sourceTableName, string destinationTableName)
{
await _enqueuer.EnqueueAsync(new[]
{
new TableCopyMessage<T>
{
SourceTableName = sourceTableName,
DestinationTableName = destinationTableName,
Strategy = TableCopyStrategy.Serial,
},
});
}
public async Task StartPrefixScanAsync(string sourceTableName, string destinationTableName, string partitionKeyPrefix, int takeCount)
{
await _enqueuer.EnqueueAsync(new[]
{
GetPrefixScanMessage(sourceTableName, destinationTableName, new TablePrefixScanStartParameters
{
PartitionKeyPrefix = partitionKeyPrefix,
TakeCount = takeCount,
}),
});
}
public async Task EnqueuePrefixScanStepsAsync(string destinationTableName, List<TablePrefixScanStep> nextSteps)
{
// Two types of messages can be enqueued here:
// 1. Table row copy messages (the actual work to be done)
// 2. Table copy messages (recursion)
var entities = new List<TableEntity>();
var tableCopyMessages = new List<TableCopyMessage<T>>();
foreach (var nextStep in nextSteps)
{
switch (nextStep)
{
case TablePrefixScanEntitySegment<TableEntity> segment:
entities.AddRange(segment.Entities);
break;
case TablePrefixScanPartitionKeyQuery partitionKeyQuery:
tableCopyMessages.Add(GetPrefixScanMessage(partitionKeyQuery.Parameters.Table.Name, destinationTableName, new TablePrefixScanPartitionKeyQueryParameters
{
TakeCount = partitionKeyQuery.Parameters.TakeCount,
Depth = partitionKeyQuery.Depth,
PartitionKey = partitionKeyQuery.PartitionKey,
RowKeySkip = partitionKeyQuery.RowKeySkip,
}));
break;
case TablePrefixScanPrefixQuery prefixQuery:
tableCopyMessages.Add(GetPrefixScanMessage(prefixQuery.Parameters.Table.Name, destinationTableName, new TablePrefixScanPrefixQueryParameters
{
TakeCount = prefixQuery.Parameters.TakeCount,
Depth = prefixQuery.Depth,
PartitionKeyPrefix = prefixQuery.PartitionKeyPrefix,
PartitionKeyLowerBound = prefixQuery.PartitionKeyLowerBound,
}));
break;
default:
throw new NotImplementedException();
}
}
if (entities.Any())
{
var sourceTableName = nextSteps.First().Parameters.Table.Name;
await EnqueueRowCopyAsync(sourceTableName, destinationTableName, entities);
}
await _enqueuer.EnqueueAsync(tableCopyMessages);
}
public async Task EnqueueRowCopyAsync(string sourceTableName, string destinationTableName, IEnumerable<TableEntity> entities)
{
await _enqueuer.EnqueueAsync(
entities
.GroupBy(x => x.PartitionKey)
.Select(x => new TableRowCopyMessage<T>
{
SourceTableName = sourceTableName,
DestinationTableName = destinationTableName,
PartitionKey = x.Key,
RowKeys = x.Select(x => x.RowKey).ToList(),
})
.ToList(),
split: m =>
{
if (m.RowKeys.Count <= 2)
{
return null;
}
var firstHalf = m.RowKeys.Take(m.RowKeys.Count / 2).ToList();
var secondHalf = m.RowKeys.Skip(firstHalf.Count).ToList();
return new[]
{
new TableRowCopyMessage<T>
{
SourceTableName = m.SourceTableName,
DestinationTableName = m.DestinationTableName,
PartitionKey = m.PartitionKey,
RowKeys = firstHalf,
},
new TableRowCopyMessage<T>
{
SourceTableName = m.SourceTableName,
DestinationTableName = m.DestinationTableName,
PartitionKey = m.PartitionKey,
RowKeys = secondHalf,
},
};
});
}
private TableCopyMessage<T> GetPrefixScanMessage<TParameters>(string sourceTableName, string destinationTableName, TParameters parameters)
{
return new TableCopyMessage<T>
{
SourceTableName = sourceTableName,
DestinationTableName = destinationTableName,
Strategy = TableCopyStrategy.PrefixScan,
Parameters = _serializer.Serialize(parameters).AsJToken(),
};
}
}
}