-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathReplicaFailoverSession.cs
More file actions
389 lines (342 loc) · 16.2 KB
/
ReplicaFailoverSession.cs
File metadata and controls
389 lines (342 loc) · 16.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Garnet.client;
using Garnet.common;
using Garnet.server;
using Microsoft.Extensions.Logging;
namespace Garnet.cluster
{
internal sealed partial class FailoverSession : IDisposable
{
/// <summary>
/// Connection to primary if reachable
/// </summary>
GarnetClient primaryClient = null;
/// <summary>
/// Send page size for GarnetClient
/// </summary>
const int sendPageSize = 1 << 17;
/// <summary>
/// Helper method to establish connection towards remote node
/// </summary>
/// <param name="nodeId">Id of node to create connection for</param>
/// <returns></returns>
private async Task<GarnetClient> CreateConnectionAsync(string nodeId)
{
var endpoint = oldConfig.GetEndpointFromNodeId(nodeId);
var client = new GarnetClient(
endpoint,
clusterProvider.serverOptions.TlsOptions?.TlsClientOptions,
sendPageSize: sendPageSize,
maxOutstandingTasks: 8,
authUsername: clusterProvider.ClusterUsername,
authPassword: clusterProvider.ClusterPassword, epoch: epoch, logger: logger);
try
{
if (!client.IsConnected)
await client.ReconnectAsync().WaitAsync(failoverTimeout, cts.Token).ConfigureAwait(false);
return client;
}
catch (Exception ex)
{
client?.Dispose();
logger?.LogError(ex, "ReplicaFailoverSession.CreateConnection");
return null;
}
}
/// <summary>
/// Acquire a connection to the node identified by given node-id.
/// </summary>
/// <param name="nodeId"></param>
/// <returns></returns>
private Task<GarnetClient> GetConnectionAsync(string nodeId)
=> CreateConnectionAsync(nodeId);
/// <summary>
/// Send stop writes message to PRIMARY
/// </summary>
/// <returns>True on success, false otherwise</returns>
private async Task<bool> PauseWritesAndWaitForSyncAsync()
{
var primaryId = oldConfig.LocalNodePrimaryId;
var client = await GetConnectionAsync(primaryId).ConfigureAwait(false);
try
{
if (client == null)
{
logger?.LogError("Failed to initialize connection to primary {primaryId}", primaryId);
return false;
}
// Cache connection for use with next operations
primaryClient = client;
// Issue stop writes to the primary
status = FailoverStatus.ISSUING_PAUSE_WRITES;
var localIdBytes = Encoding.ASCII.GetBytes(oldConfig.LocalNodeId);
var resp = await client.ExecuteClusterFailStopWritesAsync(localIdBytes).WaitAsync(failoverTimeout, cts.Token).ConfigureAwait(false);
var primaryReplicationOffset = AofAddress.FromString(resp);
// Wait for replica to catch up
status = FailoverStatus.WAITING_FOR_SYNC;
while (primaryReplicationOffset.AnyGreater(clusterProvider.replicationManager.ReplicationOffset))
{
// Fail if upper bound time for failover has been reached
if (FailoverTimeout)
{
logger?.LogError("AwaitReplicationSync timed out failoverStart");
return false;
}
await Task.Yield();
}
return true;
}
catch (Exception ex)
{
logger?.LogError(ex, "PauseWritesAndWaitForSync Error");
return false;
}
}
/// <summary>
/// Perform series of steps to update local config and take ownership of primary slots.
/// </summary>
private async Task<bool> TakeOverAsPrimaryAsync()
{
// Take over as primary and inform old primary
status = FailoverStatus.TAKING_OVER_AS_PRIMARY;
var acquiredLock = false;
try
{
// Exception injection point for testing: simulates TakeOverAsPrimary failure
// after PauseWritesAndWaitForSync has already sent failstopwrites to the primary.
ExceptionInjectionHelper.TriggerException(ExceptionInjectionType.Failover_Fail_TakeOverAsPrimary);
// Make replica syncing unavailable by setting recovery flag
if (!clusterProvider.replicationManager.BeginRecovery(RecoveryStatus.ClusterFailover, upgradeLock: false))
{
logger?.LogWarning($"{nameof(TakeOverAsPrimaryAsync)}: {{logMessage}}", Encoding.ASCII.GetString(CmdStrings.RESP_ERR_GENERIC_CANNOT_ACQUIRE_RECOVERY_LOCK));
return false;
}
acquiredLock = true;
_ = await clusterProvider.BumpAndWaitForEpochTransitionAsync().ConfigureAwait(false);
// Take over slots from old primary
if (!clusterProvider.clusterManager.TryTakeOverForPrimary())
{
logger?.LogWarning($"{nameof(TakeOverAsPrimaryAsync)}: {{logMessage}}", Encoding.ASCII.GetString(CmdStrings.RESP_ERR_GENERIC_CANNOT_TAKEOVER_FROM_PRIMARY));
return false;
}
// Update replicationIds and replicationOffset2
clusterProvider.replicationManager.TryUpdateForFailover();
// Cancel active replication tasks
clusterProvider.replicationManager.ResetReplicaReplayDriverStore();
// Update sequence number generator for sharded log if needed
if (clusterProvider.serverOptions.AofPhysicalSublogCount > 1)
{
clusterProvider.storeWrapper.appendOnlyFile.ResetSequenceNumberGenerator();
await clusterProvider.storeWrapper.TaskManager.CancelAsync(TaskType.AdvanceTimeReplicaTask).ConfigureAwait(false);
}
// Initialize checkpoint history
if (!clusterProvider.replicationManager.InitializeCheckpointStore())
logger?.LogWarning("Failed acquiring latest memory checkpoint metadata at {method}", nameof(TakeOverAsPrimaryAsync));
_ = clusterProvider.BumpAndWaitForEpochTransitionAsync().ConfigureAwait(false);
// Stop advance time task when reconfiguring node to be replica
if (clusterProvider.storeWrapper.serverOptions.AofPhysicalSublogCount > 1)
await clusterProvider.storeWrapper.TaskManager.CancelAsync(TaskType.AdvanceTimeReplicaTask).ConfigureAwait(false);
// Resume all background maintenance that were possibly shutdown when this node became a replica
clusterProvider.storeWrapper.StartPrimaryTasks();
}
catch (Exception ex)
{
logger?.LogError(ex, "{method}", nameof(TakeOverAsPrimaryAsync));
throw;
}
finally
{
// Disable recovering as now this node has become a primary or failed in its attempt earlier
if (acquiredLock)
clusterProvider.replicationManager.EndRecovery(RecoveryStatus.NoRecovery, downgradeLock: false);
}
return true;
}
/// <summary>
/// Issue gossip and attach request to replica
/// </summary>
/// <param name="replicaId">Replica-id to issue gossip and attache request</param>
/// <param name="configByteArray">Serialized local cluster config data</param>
/// <returns></returns>
private async Task BroadcastConfigAndRequestAttachAsync(string replicaId, byte[] configByteArray)
{
// Force async
await Task.Yield();
var oldPrimaryId = oldConfig.LocalNodePrimaryId;
var newConfig = clusterProvider.clusterManager.CurrentConfig;
var client = oldPrimaryId.Equals(replicaId) ? primaryClient : await GetConnectionAsync(replicaId).ConfigureAwait(false);
try
{
if (client == null)
{
logger?.LogError("Failed to initialize connection to replica {replicaId}", replicaId);
return;
}
// Force send updated config to replica
var resp = await client.GossipAsync(configByteArray).WaitAsync(failoverTimeout, cts.Token).ConfigureAwait(false);
try
{
var current = clusterProvider.clusterManager.CurrentConfig;
if (resp.Length > 0)
{
clusterProvider.clusterManager.gossipStats.UpdateGossipBytesRecv(resp.Length);
var returnedConfigArray = resp.Span.ToArray();
// Validate config version before full deserialization
if (!ClusterConfig.TryPeekVersion(returnedConfigArray, out var version) || version != ClusterConfig.ClusterConfigVersion)
{
logger?.LogWarning("Received failover gossip response with incompatible config version: {version}", version);
}
else
{
var other = ClusterConfig.FromByteArray(returnedConfigArray);
// Check if gossip is from a node that is known and trusted before merging
if (current.IsKnown(other.LocalNodeId))
_ = clusterProvider.clusterManager.TryMerge(other);
else
logger?.LogWarning("Received gossip from unknown node: {node-id}", other.LocalNodeId);
}
}
}
catch (Exception ex)
{
logger?.LogCritical(ex, "IssueAttachReplicas faulted");
}
finally
{
resp.Dispose();
}
var localAddress = oldConfig.LocalNodeIp;
var localPort = oldConfig.LocalNodePort;
// Ask replica to attach and sync
var replicaOfResp = await client.ReplicaOf(localAddress, localPort).WaitAsync(failoverTimeout, cts.Token).ConfigureAwait(false);
// Check if response for attach succeeded
if (!replicaOfResp.Equals("OK"))
logger?.LogWarning("IssueAttachReplicas Error: {replicaId} {replicaOfResp}", replicaId, replicaOfResp);
}
finally
{
client?.Dispose();
}
}
/// <summary>
/// Issue attach message to remote replicas
/// </summary>
/// <returns></returns>
private async Task IssueAttachReplicasAsync()
{
// Get information of local node from newConfig
var newConfig = clusterProvider.clusterManager.CurrentConfig;
// Get replica ids for old primary from old configuration
var oldPrimaryId = oldConfig.LocalNodePrimaryId;
var replicaIds = newConfig.GetReplicaIds(oldPrimaryId);
var configByteArray = newConfig.ToByteArray();
var attachReplicaTasks = new List<Task>();
// If DEFAULT failover try to make old primary replica of this new primary
if (option is FailoverOption.DEFAULT)
{
replicaIds.Add(oldPrimaryId);
}
// Issue gossip and attach request to replicas
foreach (var replicaId in replicaIds)
{
try
{
attachReplicaTasks.Add(BroadcastConfigAndRequestAttachAsync(replicaId, configByteArray));
}
catch (Exception ex)
{
logger?.LogError(ex, "IssueAttachReplicas Error");
}
}
// Wait for tasks to complete
if (attachReplicaTasks.Count > 0)
{
try
{
await Task.WhenAll(attachReplicaTasks).ConfigureAwait(false);
}
catch (Exception ex)
{
logger?.LogWarning(ex, "WaitingForAttachToComplete Error");
}
}
}
/// <summary>
/// Returns true if failstopwrites was confirmed by the primary and the primary's
/// config was modified (slots given up, role changed to replica). Used to determine
/// whether the primary needs to be reset on failover failure.
/// </summary>
private bool PrimaryNeedsReset()
=> status is FailoverStatus.WAITING_FOR_SYNC or FailoverStatus.TAKING_OVER_AS_PRIMARY;
/// <summary>
/// REPLICA main failover task
/// </summary>
/// <returns></returns>
public async Task<bool> BeginAsyncReplicaFailoverAsync()
{
// CLUSTER FAILOVER OPTIONS
// FORCE: Do not await for the primary since it might be unreachable
// TAKEOVER: Same as force but also do not await for voting from other primaries
var failoverSucceeded = false;
try
{
// Issue stop writes and on ack wait for replica to catch up
if (option is FailoverOption.DEFAULT && !await PauseWritesAndWaitForSyncAsync().ConfigureAwait(false))
{
return false;
}
// If TAKEOVER option is set skip voting
if (option is FailoverOption.DEFAULT or FailoverOption.FORCE)
{
//TODO: implement voting
}
// Transition to primary role
if (!await TakeOverAsPrimaryAsync().ConfigureAwait(false))
{
return false;
}
failoverSucceeded = true;
// Attach to old replicas, and old primary if DEFAULT option
await IssueAttachReplicasAsync().ConfigureAwait(false);
await clusterProvider.storeWrapper.SuspendReplicaOnlyTasksAsync();
clusterProvider.storeWrapper.StartPrimaryTasks();
return true;
}
catch (Exception ex)
{
logger?.LogWarning(ex, "BeginAsyncReplicaFailover Error");
return false;
}
finally
{
// If failstopwrites was confirmed by the primary (status reached WAITING_FOR_SYNC
// or beyond) but the failover did not succeed, reset the primary back to its
// original state. Without this, the primary has already given up its slots
// (via TryStopWrites) but the replica never claimed them, leaving the cluster
// in an incoherent state where no node owns the slots.
if (PrimaryNeedsReset() && !failoverSucceeded)
{
try
{
logger?.LogWarning("Attempting to reset primary after failed failover");
if (primaryClient != null)
{
_ = await primaryClient.ExecuteClusterFailStopWritesAsync(Array.Empty<byte>()).WaitAsync(failoverTimeout, cts.Token).ConfigureAwait(false);
}
}
catch (Exception ex)
{
logger?.LogError(ex, "Failed to reset primary after failed failover — cluster may be in an incoherent state");
}
}
primaryClient?.Dispose();
status = FailoverStatus.NO_FAILOVER;
}
}
}
}