-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathMigrateOperation.cs
More file actions
302 lines (253 loc) · 13.2 KB
/
MigrateOperation.cs
File metadata and controls
302 lines (253 loc) · 13.2 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
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Garnet.client;
using Garnet.server;
using Microsoft.Extensions.Logging;
using Tsavorite.core;
namespace Garnet.cluster
{
internal sealed partial class MigrateSession : IDisposable
{
internal sealed partial class MigrateOperation
{
public readonly Sketch sketch;
public readonly List<byte[]> keysToDelete;
public StoreScan storeScan;
private readonly ConcurrentDictionary<byte[], byte[]> vectorSetsIndexKeysToMigrate;
private readonly ConcurrentDictionary<byte[], byte> rangeIndexKeysToMigrate;
readonly MigrateSession session;
readonly GarnetClientSession gcs;
readonly LocalServerSession localServerSession;
public GarnetClientSession Client => gcs;
public LocalServerSession LocalSession => localServerSession;
public IEnumerable<KeyValuePair<byte[], byte[]>> VectorSets => vectorSetsIndexKeysToMigrate;
public IEnumerable<byte[]> RangeIndexKeys => rangeIndexKeysToMigrate.Keys;
public void ThrowIfCancelled() => session._cts.Token.ThrowIfCancellationRequested();
public bool Contains(int slot) => session._sslots.Contains(slot);
public bool ContainsNamespace(ReadOnlySpan<byte> namespaceBytes)
{
Debug.Assert(namespaceBytes.Length == 1, "Longer namespaces note supported");
var ns = (ulong)namespaceBytes[0];
return session._namespaces?.Contains(ns) ?? false;
}
public void EncounteredVectorSet(byte[] key, byte[] value)
=> vectorSetsIndexKeysToMigrate.TryAdd(key, value);
public void EncounteredRangeIndex(byte[] key) => rangeIndexKeysToMigrate.TryAdd(key, 0);
public MigrateOperation(MigrateSession session, Sketch sketch = null, int batchSize = 1 << 18)
{
this.session = session;
gcs = session.GetGarnetClient();
localServerSession = session.GetLocalSession();
this.sketch = sketch ?? new(keyCount: batchSize << 2);
storeScan = new StoreScan(this);
keysToDelete = [];
vectorSetsIndexKeysToMigrate = new(ByteArrayComparer.Instance);
rangeIndexKeysToMigrate = new(ByteArrayComparer.Instance);
}
public async ValueTask<bool> InitializeAsync()
{
if (!await session.CheckConnectionAsync(gcs).ConfigureAwait(false))
return false;
gcs.InitializeIterationBuffer(session.clusterProvider.storeWrapper.loggingFrequency);
return true;
}
public void Dispose()
{
gcs.Dispose();
localServerSession.Dispose();
}
/// <summary>
/// Perform scan to gather keys and build sketch
/// </summary>
/// <param name="currentAddress"></param>
/// <param name="endAddress"></param>
public void Scan(ref long currentAddress, long endAddress)
=> localServerSession.BasicGarnetApi.IterateStore(ref storeScan, ref currentAddress, endAddress, endAddress,
includeTombstones: true);
/// <summary>
/// Transmit gathered keys
/// </summary>
/// <returns></returns>
public async Task<bool> TransmitSlotsAsync()
{
var output = new UnifiedOutput(); // TODO: initialize this based on gcs curr and end; make sure it has the initial part of the "send" set
var vectorOutput = new VectorOutput(); // TODO: initialize this based on gcs curr and end; make sure it has the initial part of the "send" set
try
{
var input = new UnifiedInput(RespCommand.MIGRATE);
input.arg1 = session.NetworkBufferSettings.sendBufferSize - common.NetworkBufferSettings.SendBufferOverheadReserve;
VectorInput vectorInput = new();
vectorInput.AlignmentExpected = true; // We're moving DiskANN sourced data, so alignment is expected
vectorInput.MaxMigrationHeapAllocationSize = session.NetworkBufferSettings.sendBufferSize - common.NetworkBufferSettings.SendBufferOverheadReserve;
foreach (var (ns, key, hasNs) in sketch.argSliceVector)
{
if (hasNs)
{
// Migrating Vector Set element data
if (!await session.WriteOrSendRecordAsync(gcs, localServerSession, ns, key, ref vectorInput, ref vectorOutput, out _).ConfigureAwait(false))
return false;
}
else
{
// Migrating everything else
if (!await session.WriteOrSendRecordAsync(gcs, localServerSession, key, ref input, ref output, out _).ConfigureAwait(false))
return false;
}
}
// Flush final data in client buffer
if (!await session.HandleMigrateTaskResponseAsync(gcs.SendAndResetIterationBuffer()).ConfigureAwait(false))
return false;
}
finally
{
output.SpanByteAndMemory.Dispose();
vectorOutput.SpanByteAndMemory.Dispose();
}
return true;
}
public async Task<bool> TransmitKeysAsync(Func<PinnedSpanByte, bool> shouldSkipKey)
{
// Use this for both stores; main store will just use the SpanByteAndMemory directly. We want it to be outside iterations
// so we can reuse the SpanByteAndMemory.Memory across iterations.
// TODO: initialize 'output' based on gcs curr and end; make sure it has the initial part of the "send" set, and call gcs.IncrementRecordDirect().
// This will still allow SBAM.Memory to be reused.
var output = new UnifiedOutput();
try
{
var keys = sketch.Keys;
var input = new UnifiedInput(RespCommand.MIGRATE)
{
arg1 = session.NetworkBufferSettings.sendBufferSize - 1024 // Reserve some space for overhead
};
for (var i = 0; i < keys.Count; i++)
{
if (keys[i].Item2)
continue;
// Skip keys that require special handling
if (shouldSkipKey(keys[i].Item1))
continue;
if (!await session.WriteOrSendRecordAsync(gcs, localServerSession, keys[i].Item1, ref input, ref output, out var status).ConfigureAwait(false))
return false;
// If key was FOUND, mark it for deletion
if (status != GarnetStatus.NOTFOUND)
keys[i] = (keys[i].Item1, true);
}
// Flush final data in client buffer
if (!await session.HandleMigrateTaskResponseAsync(gcs.SendAndResetIterationBuffer()).ConfigureAwait(false))
return false;
}
finally
{
output.SpanByteAndMemory.Dispose();
}
return true;
}
/// <summary>
/// Transmit data in namespaces during a MIGRATE ... KEYS operation.
///
/// Doesn't delete anything, just scans and transmits.
/// </summary>
public async ValueTask<bool> TransmitKeysNamespacesAsync(ILogger logger)
{
var migrateOperation = this;
if (!await migrateOperation.InitializeAsync().ConfigureAwait(false))
return false;
var workerStartAddress = migrateOperation.session.clusterProvider.storeWrapper.store.Log.BeginAddress;
var workerEndAddress = migrateOperation.session.clusterProvider.storeWrapper.store.Log.TailAddress;
var cursor = workerStartAddress;
logger?.LogWarning("<MainStore> migrate keys (namespaces) scan range [{workerStartAddress}, {workerEndAddress}]", workerStartAddress, workerEndAddress);
while (true)
{
var current = cursor;
// Build Sketch
migrateOperation.sketch.SetStatus(SketchStatus.INITIALIZING);
migrateOperation.Scan(ref current, workerEndAddress);
// Stop if no keys have been found
if (migrateOperation.sketch.argSliceVector.IsEmpty) break;
logger?.LogWarning("Scan from {cursor} to {current} and discovered {count} keys", cursor, current, migrateOperation.sketch.argSliceVector.Count);
// Transition EPSM to MIGRATING
migrateOperation.sketch.SetStatus(SketchStatus.TRANSMITTING);
await migrateOperation.session.WaitForConfigPropagationAsync().ConfigureAwait(false);
// Transmit all keys gathered
if (!await migrateOperation.TransmitSlotsAsync().ConfigureAwait(false))
{
logger?.LogWarning("TransmitSlots failed for {cursor} to {current} (with {count} keys)", cursor, current, migrateOperation.sketch.argSliceVector.Count);
return false;
}
// Transition EPSM to DELETING
migrateOperation.sketch.SetStatus(SketchStatus.DELETING);
await migrateOperation.session.WaitForConfigPropagationAsync().ConfigureAwait(false);
// Clear keys from buffer
migrateOperation.sketch.Clear();
cursor = current;
}
return true;
}
/// <summary>
/// Delete keys after migration if copyOption is not set
/// </summary>
public void DeleteKeys()
{
if (session._copyOption)
return;
if (session.transferOption == TransferOption.SLOTS)
{
foreach (var (ns, key, hasNs) in sketch.argSliceVector)
{
if (hasNs)
{
// Namespace'd keys are deleted as part after migration completes
continue;
}
else
{
_ = localServerSession.BasicGarnetApi.DELETE(key);
}
}
}
else
{
var keys = sketch.Keys;
for (var i = 0; i < keys.Count; i++)
{
// Do not delete the key if it is not marked for deletion because it has not been transmitted to the target node
if (keys[i].Item2)
_ = localServerSession.BasicGarnetApi.DELETE(keys[i].Item1);
}
}
}
/// <summary>
/// Delete a Vector Set after migration if _copyOption is not set.
/// </summary>
public void DeleteVectorSet(PinnedSpanByte key)
{
if (session._copyOption)
return;
var delRes = localServerSession.BasicGarnetApi.DELETE(key);
session.logger?.LogDebug("Deleting Vector Set {key} after migration: {delRes}", Encoding.UTF8.GetString(key), delRes);
}
/// <summary>
/// Delete a RangeIndex after migration. COPY option is not yet supported for RangeIndex keys.
/// </summary>
public void DeleteRangeIndex(PinnedSpanByte key)
{
// COPY option is not yet supported for RangeIndex keys. Supporting it would require
// an atomic swap or transactional approach for replacing BfTree data files when the
// stub already exists at the destination, with proper recovery semantics in case the
// process crashes mid-swap. For now, we always delete the source key.
if (session._copyOption)
{
session.logger?.LogWarning("COPY option ignored for RangeIndex key {key}", Encoding.UTF8.GetString(key));
}
var delRes = localServerSession.BasicGarnetApi.DELETE(key);
session.logger?.LogDebug("Deleted RangeIndex key {key} after migration: {delRes}", Encoding.UTF8.GetString(key), delRes);
}
}
}
}