-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathGossip.cs
More file actions
521 lines (454 loc) · 21.4 KB
/
Copy pathGossip.cs
File metadata and controls
521 lines (454 loc) · 21.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Garnet.common;
using Garnet.server;
using Microsoft.Extensions.Logging;
namespace Garnet.cluster
{
internal sealed partial class ClusterManager : IDisposable
{
const int maxRandomNodesToPoll = 3;
public readonly TimeSpan gossipDelay;
public readonly TimeSpan clusterTimeout;
private volatile int numActiveTasks = 0;
private readonly common.ReaderWriterLock activeMergeLock;
public readonly GarnetClusterConnectionStore clusterConnectionStore;
public GossipStats gossipStats;
readonly int GossipSamplePercent;
readonly ConcurrentDictionary<string, long> workerBanList = new();
public readonly CancellationTokenSource ctsGossip = new();
/// <summary>
/// Return worker ban list
/// </summary>
/// <returns></returns>
public List<string> GetBanList()
{
var banlist = new List<string>();
foreach (var w in workerBanList)
{
var nodeId = w.Key;
var expiry = w.Value;
var diff = expiry - DateTimeOffset.UtcNow.Ticks;
var str = $"{nodeId} : {(int)TimeSpan.FromTicks(diff).TotalSeconds}";
banlist.Add(str);
}
return banlist;
}
/// <summary>
/// Get connection info to populate CLUSTER NODES
/// </summary>
/// <param name="nodeId"></param>
/// <param name="info"></param>
/// <returns></returns>
public bool GetConnectionInfo(string nodeId, out ConnectionInfo info)
=> clusterConnectionStore.GetConnectionInfo(nodeId, out info);
/// <summary>
/// Get link status info for primary of this node.
/// </summary>
/// <param name="config">Snapshot of config to use for retrieving that information.</param>
/// <returns>MetricsItem array of all the associated info.</returns>
public MetricsItem[] GetPrimaryLinkStatus(ClusterConfig config)
{
ConnectionInfo info = new();
var primaryId = config.LocalNodePrimaryId;
if (primaryId != null)
_ = clusterConnectionStore.GetConnectionInfo(primaryId, out info);
var primaryLinkStatus = new MetricsItem[2]
{
new("master_link_status", info.connected ? "up" : "down"),
new("master_last_io_seconds_ago", info.lastIO.ToString())
};
return primaryLinkStatus;
}
/// <summary>
/// Pause merge config ops by setting numActiveMerge to MinValue.
/// Called when FORGET op executes and waits until ongoing merge operations complete before executing FORGET
/// Multiple FORGET ops can execute at the same time.
/// </summary>
public void SuspendConfigMerge() => activeMergeLock.WriteLock();
/// <summary>
/// Resume config merge
/// </summary>
public void ResumeConfigMerge() => activeMergeLock.WriteUnlock();
/// <summary>
/// Initiate meet and main gossip tasks
/// </summary>
void TryStartGossipTasks()
{
// Run one round of meet at start-up
for (var i = 2; i <= CurrentConfig.NumWorkers; i++)
{
var (address, port) = CurrentConfig.GetWorkerAddress((ushort)i);
RunMeetTask(address, port);
}
// Startup gossip background task
_ = Interlocked.Increment(ref numActiveTasks);
_ = Task.Run(GossipMainAsync);
}
/// <summary>
/// Merge incoming config to evolve local version
/// </summary>
public bool TryMerge(ClusterConfig senderConfig, bool acquireLock = true)
{
try
{
if (acquireLock) activeMergeLock.ReadLock();
if (workerBanList.ContainsKey(senderConfig.LocalNodeId))
{
logger?.LogTrace("Cannot merge node <{nodeid}> because still in ban list", senderConfig.LocalNodeId);
return false;
}
while (true)
{
var current = currentConfig;
var currentCopy = current.Copy();
var next = currentCopy.Merge(senderConfig, workerBanList, logger).HandleConfigEpochCollision(senderConfig, logger);
if (currentCopy == next) return false;
if (Interlocked.CompareExchange(ref currentConfig, next, current) == current)
break;
}
FlushConfig();
return true;
}
finally
{
if (acquireLock) activeMergeLock.ReadUnlock();
}
}
/// <summary>
/// Run meet background task
/// </summary>
/// <param name="address"></param>
/// <param name="port"></param>
public void RunMeetTask(string address, int port)
=> _ = Task.Run(() => TryMeetAsync(address, port));
/// <summary>
/// This task will immediately communicate with the new node and try to merge the retrieve configuration to its own.
/// If node to meet was previous in the ban list then it will not be added to the cluster.
/// </summary>
/// <param name="address">Address of node to issue meet to</param>
/// <param name="port"> Port of node to issue meet to</param>
/// <param name="acquireLock">Whether to acquire lock for merging. Default true</param>
public async Task TryMeetAsync(string address, int port, bool acquireLock = true)
{
GarnetServerNode gsn = null;
var conf = CurrentConfig;
var nodeId = conf.GetWorkerNodeIdFromAddress(address, port);
MemoryResult<byte> resp = default;
var created = false;
gossipStats.UpdateMeetRequestsRecv();
try
{
if (nodeId != null)
clusterConnectionStore.GetConnection(nodeId, out gsn);
if (gsn == null)
{
var endpoints = await Format.TryCreateEndpointAsync(address, port, tryConnect: true, logger: logger).ConfigureAwait(false);
if (endpoints == null)
{
logger?.LogError("Invalid CLUSTER MEET endpoint!");
}
gsn = new GarnetServerNode(clusterProvider, endpoints[0], tlsOptions?.TlsClientOptions, clusterConnectionStore.Epoch, logger: logger);
created = true;
}
// Initialize GarnetServerNode
// Thread-Safe initialization executes only once
await gsn.InitializeAsync().ConfigureAwait(false);
// Send full config in Gossip
resp = await gsn.TryMeetAsync(conf.ToByteArray()).ConfigureAwait(false);
if (resp.Length > 0)
{
var respArray = resp.Span.ToArray();
// Validate config version before full deserialization
if (!ClusterConfig.TryPeekVersion(respArray, out var version) || version != ClusterConfig.ClusterConfigVersion)
{
logger?.LogWarning("MEET response has incompatible config version: {version}", version);
if (created) gsn?.Dispose();
gossipStats.UpdateMeetRequestsFailed();
}
else
{
var other = ClusterConfig.FromByteArray(respArray);
nodeId = other.LocalNodeId;
gsn.NodeId = nodeId;
logger?.LogInformation("MEET {nodeId} {address} {port}", nodeId, address, port);
// Merge without a check because node is trusted as meet was issued by admin
_ = TryMerge(other, acquireLock);
gossipStats.UpdateMeetRequestsSucceed();
// If failed to add newly created connection dispose of it to reclaim resources
// Dispose only connections that this meet task has created to avoid conflicts with existing connections from gossip main thread
// After connection is added we are no longer the owner. Background gossip task will be owner
if (created && !await clusterConnectionStore.AddConnectionAsync(gsn).ConfigureAwait(false))
gsn.Dispose();
}
}
}
catch (Exception ex)
{
logger?.LogError(ex, "Meet terminated with error");
if (created) gsn?.Dispose();
gossipStats.UpdateMeetRequestsFailed();
}
finally
{
resp.Dispose();
}
}
/// <summary>
/// Forward message by issuing CLUSTER PUBLISH|SPUBLISH
/// </summary>
/// <param name="cmd"></param>
/// <param name="channel"></param>
/// <param name="message"></param>
public ValueTask TryClusterPublishAsync(RespCommand cmd, Span<byte> channel, Span<byte> message)
{
var conf = CurrentConfig;
List<(string NodeId, IPEndPoint Endpoint)> nodeEntries = null;
if (cmd == RespCommand.PUBLISH)
{
conf.GetAllNodeIds(out nodeEntries);
}
else
{
conf.GetNodeIdsForShard(out nodeEntries);
}
for (var entryIx = 0; entryIx < nodeEntries.Count; entryIx++)
{
var (nodeId, endpoint) = nodeEntries[entryIx];
var getOrAddTask = clusterConnectionStore.GetOrAddAsync(clusterProvider, endpoint, tlsOptions, nodeId, logger: logger);
GarnetServerNode gsn;
if (getOrAddTask.IsCompletedSuccessfully)
{
// Cannot remove .GetResult here, but it's gated by IsCompletedSuccessfully so safe
(_, gsn) = AsyncUtils.BlockingWait(getOrAddTask);
}
else
{
// Otherwise copy channel & message and go async
return new(GoAsyncHelperAsync(getOrAddTask, default, entryIx, null, cmd, channel.ToArray(), message.ToArray()));
}
if (gsn == null)
continue;
// Initialize GarnetServerNode
// Thread-Safe initialization executes only once
var initTask = gsn.InitializeAsync();
if (initTask.IsCompletedSuccessfully)
{
// Can stay sync, so proceed
gsn.TryClusterPublish(cmd, channel, message);
}
else
{
// Copy channel & message and go async
return new(GoAsyncHelperAsync(default, initTask, entryIx, gsn, cmd, channel.ToArray(), message.ToArray()));
}
}
// Completed synchronously
return default;
async Task GoAsyncHelperAsync(ValueTask<(bool Success, GarnetServerNode Node)> getOrAddTask, ValueTask initTask, int lastEntryIx, GarnetServerNode lastGsn, RespCommand cmd, Memory<byte> channel, Memory<byte> message)
{
// Finish the task which caused us to go async
if (lastGsn == null)
{
(_, lastGsn) = await getOrAddTask.ConfigureAwait(false);
if (lastGsn != null)
{
await lastGsn.InitializeAsync().ConfigureAwait(false);
}
}
else
{
await initTask.ConfigureAwait(false);
}
if (lastGsn != null)
{
lastGsn.TryClusterPublish(cmd, channel.Span, message.Span);
}
// Process remainder of entries, staying async
for (var entryIx = lastEntryIx + 1; entryIx < nodeEntries.Count; entryIx++)
{
var (nodeId, endpoint) = nodeEntries[entryIx];
var (_, gsn) = await clusterConnectionStore.GetOrAddAsync(clusterProvider, endpoint, tlsOptions, nodeId, logger: logger).ConfigureAwait(false);
if (gsn == null)
continue;
// Initialize GarnetServerNode
// Thread-Safe initialization executes only once
await gsn.InitializeAsync().ConfigureAwait(false);
// Publish to remote nodes
gsn.TryClusterPublish(cmd, channel.Span, message.Span);
}
}
}
/// <summary>
/// Main gossip async task
/// </summary>
async Task GossipMainAsync()
{
// Main gossip loop
try
{
while (true)
{
ctsGossip.Token.ThrowIfCancellationRequested();
await InitConnectionsAsync().ConfigureAwait(false);
// Choose between full broadcast or sample gossip to few nodes
if (GossipSamplePercent == 100)
await BroadcastGossipSendAsync().ConfigureAwait(false);
else
await GossipSampleSendAsync().ConfigureAwait(false);
await Task.Delay(gossipDelay, ctsGossip.Token).ConfigureAwait(false);
}
}
catch (TaskCanceledException) when (ctsGossip.Token.IsCancellationRequested)
{
// Suppress the exception if the task was cancelled because of store wrapper disposal
}
catch (Exception ex)
{
logger?.LogWarning("GossipMain terminated with error {msg}", ex.Message);
}
finally
{
try
{
clusterConnectionStore.Dispose();
}
catch (Exception ex)
{
logger?.LogWarning("Error disposing closing gossip connections {msg}", ex.Message);
}
_ = Interlocked.Decrement(ref numActiveTasks);
}
// Initialize connections for nodes that have either been dispose due to banlist (after expiry) or timeout
async Task InitConnectionsAsync()
{
await DisposeBannedWorkerConnectionsAsync().ConfigureAwait(false);
var current = currentConfig;
var addresses = current.GetWorkerInfoForGossip();
foreach (var a in addresses)
{
ctsGossip.Token.ThrowIfCancellationRequested();
var nodeId = a.Item1;
var address = a.Item2;
var port = a.Item3;
// Establish new connection only if it is not in banlist and not in dictionary
if (!workerBanList.ContainsKey(nodeId) && !clusterConnectionStore.GetConnection(nodeId, out var _))
{
try
{
var (success, gsn) = await clusterConnectionStore.GetOrAddAsync(clusterProvider, new IPEndPoint(IPAddress.Parse(address), port), tlsOptions, nodeId, logger: logger).ConfigureAwait(false);
if (gsn == null)
{
logger?.LogWarning("InitConnections: Could not establish connection to remote node [{nodeId} {address}:{port}] failed", nodeId, address, port);
_ = await clusterConnectionStore.TryRemoveConnectionAsync(nodeId).ConfigureAwait(false);
continue;
}
await gsn.InitializeAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
logger?.LogWarning(ex, "InitConnections: Could not establish connection to remote node [{nodeId} {address}:{port}] failed", nodeId, address, port);
_ = await clusterConnectionStore.TryRemoveConnectionAsync(nodeId).ConfigureAwait(false);
}
}
}
async Task DisposeBannedWorkerConnectionsAsync()
{
foreach (var w in workerBanList)
{
ctsGossip.Token.ThrowIfCancellationRequested();
var nodeId = w.Key;
var expiry = w.Value;
// Check if ban on worker has expired or not
if (!Expired(expiry))
{
// Remove connection for banned worker
_ = await clusterConnectionStore.TryRemoveConnectionAsync(nodeId).ConfigureAwait(false);
}
else // Remove worker from ban list
_ = workerBanList.TryRemove(nodeId, out var _);
}
static bool Expired(long expiry) => expiry < DateTimeOffset.UtcNow.Ticks;
}
}
// Initiate a full broadcast gossip transmission
async Task BroadcastGossipSendAsync()
{
// Issue async gossip tasks to all nodes
uint offset = 0;
while (clusterConnectionStore.GetConnectionAtOffset(offset, out var currNode))
{
try
{
ctsGossip.Token.ThrowIfCancellationRequested();
// Issue gossip message to node and truck success metrics
if (currNode.TryGossip())
{
gossipStats.gossip_success_count++;
offset++;
continue;
}
gossipStats.gossip_timeout_count++;
logger?.LogWarning("GOSSIP to remote node [{nodeId} {endpoint}] timeout!", currNode.NodeId, currNode.EndPoint);
_ = await clusterConnectionStore.TryRemoveConnectionAsync(currNode.NodeId).ConfigureAwait(false);
}
catch (Exception ex)
{
logger?.LogWarning(ex, "GOSSIP to remote node [{nodeId} {endpoint}] failed!", currNode.NodeId, currNode.EndPoint);
_ = await clusterConnectionStore.TryRemoveConnectionAsync(currNode.NodeId).ConfigureAwait(false);
gossipStats.gossip_failed_count++;
}
}
}
// Initiate sampling gossip transmission
async Task GossipSampleSendAsync()
{
var nodeCount = clusterConnectionStore.Count;
var fraction = (int)(Math.Ceiling(nodeCount * (GossipSamplePercent / 100.0f)));
var count = Math.Max(Math.Min(1, nodeCount), fraction);
var startTime = DateTimeOffset.UtcNow.Ticks;
while (count > 0)
{
var minSend = startTime;
GarnetServerNode currNode = null;
for (var i = 0; i < maxRandomNodesToPoll; i++)
{
// Pick the node with earliest send timestamp
if (clusterConnectionStore.GetRandomConnection(out var c) && c.GossipSend < minSend)
{
minSend = c.GossipSend;
currNode = c;
}
}
if (currNode == null) break;
try
{
ctsGossip.Token.ThrowIfCancellationRequested();
// Issue gossip message to node and truck success metrics
if (currNode.TryGossip())
{
gossipStats.gossip_success_count++;
continue;
}
gossipStats.gossip_timeout_count++;
logger?.LogWarning("GOSSIP to remote node [{nodeId} {endpoint}] timeout!", currNode.NodeId, currNode.EndPoint);
_ = await clusterConnectionStore.TryRemoveConnectionAsync(currNode.NodeId).ConfigureAwait(false);
}
catch (Exception ex)
{
logger?.LogError(ex, "GOSSIP to remote node [{nodeId} {endpoint}] failed!", currNode.NodeId, currNode.EndPoint);
_ = await clusterConnectionStore.TryRemoveConnectionAsync(currNode.NodeId).ConfigureAwait(false);
gossipStats.gossip_failed_count++;
}
count--;
}
}
}
}
}