-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathDynamoDBMembershipTable.cs
664 lines (593 loc) · 29.4 KB
/
DynamoDBMembershipTable.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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Runtime;
using Orleans.Runtime.MembershipService;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Orleans.Clustering.DynamoDB
{
internal class DynamoDBMembershipTable : IMembershipTable
{
private static readonly TableVersion NotFoundTableVersion = new TableVersion(0, "0");
private const string CURRENT_ETAG_ALIAS = ":currentETag";
private const int MAX_BATCH_SIZE = 25;
private readonly ILogger logger;
private DynamoDBStorage storage;
private readonly DynamoDBClusteringOptions options;
private readonly string clusterId;
public DynamoDBMembershipTable(
ILoggerFactory loggerFactory,
IOptions<DynamoDBClusteringOptions> clusteringOptions,
IOptions<ClusterOptions> clusterOptions)
{
logger = loggerFactory.CreateLogger<DynamoDBMembershipTable>();
this.options = clusteringOptions.Value;
this.clusterId = clusterOptions.Value.ClusterId;
}
public Task InitializeMembershipTable(bool tryInitTableVersion) => InitializeMembershipTable(tryInitTableVersion, CancellationToken.None);
public Task DeleteMembershipTableEntries(string clusterId) => DeleteMembershipTableEntries(clusterId, CancellationToken.None);
public Task CleanupDefunctSiloEntries(DateTimeOffset beforeDate) => CleanupDefunctSiloEntries(beforeDate, CancellationToken.None);
public Task<MembershipTableData> ReadRow(SiloAddress key) => ReadRow(key, CancellationToken.None);
public Task<MembershipTableData> ReadAll() => ReadAll(CancellationToken.None);
public Task<bool> InsertRow(MembershipEntry entry, TableVersion tableVersion) => InsertRow(entry, tableVersion, CancellationToken.None);
public Task<bool> UpdateRow(MembershipEntry entry, string etag, TableVersion tableVersion) => UpdateRow(entry, etag, tableVersion, CancellationToken.None);
public Task UpdateIAmAlive(MembershipEntry entry) => UpdateIAmAlive(entry, CancellationToken.None);
public async Task InitializeMembershipTable(bool tryInitTableVersion, CancellationToken cancellationToken)
{
this.storage = new DynamoDBStorage(
this.logger,
this.options.Service,
this.options.AccessKey,
this.options.SecretKey,
this.options.Token,
this.options.ProfileName,
this.options.ReadCapacityUnits,
this.options.WriteCapacityUnits,
this.options.UseProvisionedThroughput,
this.options.CreateIfNotExists,
this.options.UpdateIfExists);
logger.LogInformation((int)ErrorCode.MembershipBase, "Initializing AWS DynamoDB Membership Table");
await storage.InitializeTable(
this.options.TableName,
new List<KeySchemaElement>
{
new KeySchemaElement { AttributeName = SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME, KeyType = KeyType.HASH },
new KeySchemaElement { AttributeName = SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME, KeyType = KeyType.RANGE }
},
new List<AttributeDefinition>
{
new AttributeDefinition { AttributeName = SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S },
new AttributeDefinition { AttributeName = SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME, AttributeType = ScalarAttributeType.S }
},
cancellationToken: cancellationToken);
// even if I am not the one who created the table,
// try to insert an initial table version if it is not already there,
// so we always have a first table version row, before this silo starts working.
if (tryInitTableVersion)
{
// ignore return value, since we don't care if I inserted it or not, as long as it is in there.
bool created = await TryCreateTableVersionEntryAsync(cancellationToken);
if(created) logger.LogInformation("Created new table version row.");
}
}
private async Task<bool> TryCreateTableVersionEntryAsync(CancellationToken cancellationToken)
{
var keys = new Dictionary<string, AttributeValue>
{
{ $"{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}", new AttributeValue(this.clusterId) },
{ $"{SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME}", new AttributeValue(SiloInstanceRecord.TABLE_VERSION_ROW) }
};
var versionRow = await storage.ReadSingleEntryAsync(this.options.TableName, keys, fields => new SiloInstanceRecord(fields), cancellationToken);
if (versionRow != null)
{
return false;
}
if (!TryCreateTableVersionRecord(0, null, out var entry))
{
return false;
}
var notExistConditionExpression =
$"attribute_not_exists({SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}) AND attribute_not_exists({SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME})";
try
{
await storage.PutEntryAsync(this.options.TableName, entry.GetFields(true), notExistConditionExpression, cancellationToken: cancellationToken);
}
catch (ConditionalCheckFailedException)
{
return false;
}
return true;
}
private bool TryCreateTableVersionRecord(int version, string etag, out SiloInstanceRecord entry)
{
int etagInt;
if (etag is null)
{
etagInt = 0;
}
else
{
if (!int.TryParse(etag, out etagInt))
{
entry = default;
return false;
}
}
entry = new SiloInstanceRecord
{
DeploymentId = clusterId,
SiloIdentity = SiloInstanceRecord.TABLE_VERSION_ROW,
MembershipVersion = version,
ETag = etagInt
};
return true;
}
public async Task DeleteMembershipTableEntries(string clusterId, CancellationToken cancellationToken)
{
try
{
var keys = new Dictionary<string, AttributeValue> { { $":{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}", new AttributeValue(clusterId) } };
var records = await storage.QueryAsync(
this.options.TableName,
keys,
$"{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME} = :{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}",
item => new SiloInstanceRecord(item),
cancellationToken: cancellationToken);
var toDelete = new List<Dictionary<string, AttributeValue>>();
foreach (var record in records.results)
{
toDelete.Add(record.GetKeys());
}
List<Task> tasks = new List<Task>();
foreach (var batch in toDelete.BatchIEnumerable(MAX_BATCH_SIZE))
{
tasks.Add(storage.DeleteEntriesAsync(this.options.TableName, batch, cancellationToken));
}
await Task.WhenAll(tasks);
}
catch (Exception exc)
{
this.logger.LogError(
(int)ErrorCode.MembershipBase,
exc,
"Unable to delete membership records on table {TableName} for ClusterId {ClusterId}",
this.options.TableName,
clusterId);
throw;
}
}
public async Task<MembershipTableData> ReadRow(SiloAddress siloAddress, CancellationToken cancellationToken)
{
try
{
var siloEntryKeys = new Dictionary<string, AttributeValue>
{
{ $"{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}", new AttributeValue(this.clusterId) },
{ $"{SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME}", new AttributeValue(SiloInstanceRecord.ConstructSiloIdentity(siloAddress)) }
};
var versionEntryKeys = new Dictionary<string, AttributeValue>
{
{ $"{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}", new AttributeValue(this.clusterId) },
{ $"{SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME}", new AttributeValue(SiloInstanceRecord.TABLE_VERSION_ROW) }
};
var entries = await storage.GetEntriesTxAsync(
this.options.TableName,
new[] { siloEntryKeys, versionEntryKeys },
fields => new SiloInstanceRecord(fields),
cancellationToken);
MembershipTableData data = Convert(entries.ToList());
if (this.logger.IsEnabled(LogLevel.Trace)) this.logger.LogTrace("Read my entry {SiloAddress} Table: {TableData}", siloAddress.ToString(), data.ToString());
return data;
}
catch (Exception exc)
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
exc,
"Intermediate error reading silo entry for key {SiloAddress} from the table {TableName}",
siloAddress.ToString(),
this.options.TableName);
throw;
}
}
public async Task<MembershipTableData> ReadAll(CancellationToken cancellationToken)
{
try
{
//first read just the version row so that we can check for version consistency
var versionEntryKeys = new Dictionary<string, AttributeValue>
{
{ $"{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}", new AttributeValue(this.clusterId) },
{ $"{SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME}", new AttributeValue(SiloInstanceRecord.TABLE_VERSION_ROW) }
};
var versionRow = await this.storage.ReadSingleEntryAsync(
this.options.TableName,
versionEntryKeys,
fields => new SiloInstanceRecord(fields),
cancellationToken);
if (versionRow == null)
{
throw new KeyNotFoundException("No version row found for membership table");
}
var keys = new Dictionary<string, AttributeValue> { { $":{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}", new AttributeValue(this.clusterId) } };
var records = await this.storage.QueryAllAsync(
this.options.TableName,
keys,
$"{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME} = :{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}",
item => new SiloInstanceRecord(item),
cancellationToken: cancellationToken);
if (records.Any(record => record.MembershipVersion > versionRow.MembershipVersion))
{
this.logger.LogWarning((int)ErrorCode.MembershipBase, "Found an inconsistency while reading all silo entries");
//not expecting this to hit often, but if it does, should put in a limit
return await this.ReadAll(cancellationToken);
}
MembershipTableData data = Convert(records);
if (this.logger.IsEnabled(LogLevel.Trace)) this.logger.LogTrace("ReadAll Table {Table}", data.ToString());
return data;
}
catch (Exception exc)
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
exc,
"Intermediate error reading all silo entries {TableName}.",
options.TableName);
throw;
}
}
public async Task<bool> InsertRow(MembershipEntry entry, TableVersion tableVersion, CancellationToken cancellationToken)
{
try
{
if (this.logger.IsEnabled(LogLevel.Debug)) this.logger.LogDebug("InsertRow entry = {Entry}", entry.ToString());
var tableEntry = Convert(entry, tableVersion);
if (!TryCreateTableVersionRecord(tableVersion.Version, tableVersion.VersionEtag, out var versionEntry))
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
"Insert failed. Invalid ETag value. Will retry. Entry {Entry}, eTag {ETag}",
entry.ToString(),
tableVersion.VersionEtag);
return false;
}
versionEntry.ETag++;
bool result;
try
{
var notExistConditionExpression =
$"attribute_not_exists({SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}) AND attribute_not_exists({SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME})";
var tableEntryInsert = new Put
{
Item = tableEntry.GetFields(true),
ConditionExpression = notExistConditionExpression,
TableName = this.options.TableName
};
var conditionalValues = new Dictionary<string, AttributeValue> { { CURRENT_ETAG_ALIAS, new AttributeValue { N = tableVersion.VersionEtag } } };
var etagConditionalExpression = $"{SiloInstanceRecord.ETAG_PROPERTY_NAME} = {CURRENT_ETAG_ALIAS}";
var versionEntryUpdate = new Update
{
TableName = this.options.TableName,
Key = versionEntry.GetKeys(),
ConditionExpression = etagConditionalExpression
};
(versionEntryUpdate.UpdateExpression, versionEntryUpdate.ExpressionAttributeValues) =
this.storage.ConvertUpdate(versionEntry.GetFields(), conditionalValues);
await this.storage.WriteTxAsync(new[] {tableEntryInsert}, new[] {versionEntryUpdate}, cancellationToken: cancellationToken);
result = true;
}
catch (TransactionCanceledException canceledException)
{
if (canceledException.Message.Contains("ConditionalCheckFailed")) //not a good way to check for this currently
{
result = false;
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
"Insert failed due to contention on the table. Will retry. Entry {Entry}",
entry.ToString());
}
else
{
throw;
}
}
return result;
}
catch (Exception exc)
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
exc,
"Intermediate error inserting entry {Entry} to the table {TableName}.",
entry.ToString(),
this.options.TableName);
throw;
}
}
public async Task<bool> UpdateRow(MembershipEntry entry, string etag, TableVersion tableVersion, CancellationToken cancellationToken)
{
try
{
if (this.logger.IsEnabled(LogLevel.Debug)) this.logger.LogDebug("UpdateRow entry = {Entry}, etag = {}", entry.ToString(), etag);
var siloEntry = Convert(entry, tableVersion);
if (!int.TryParse(etag, out var currentEtag))
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
"Update failed. Invalid ETag value. Will retry. Entry {Entry}, eTag {ETag}",
entry.ToString(),
etag);
return false;
}
siloEntry.ETag = currentEtag + 1;
if (!TryCreateTableVersionRecord(tableVersion.Version, tableVersion.VersionEtag, out var versionEntry))
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
"Update failed. Invalid ETag value. Will retry. Entry {Entry}, eTag {ETag}",
entry.ToString(),
tableVersion.VersionEtag);
return false;
}
versionEntry.ETag++;
bool result;
try
{
var etagConditionalExpression = $"{SiloInstanceRecord.ETAG_PROPERTY_NAME} = {CURRENT_ETAG_ALIAS}";
var siloConditionalValues = new Dictionary<string, AttributeValue> { { CURRENT_ETAG_ALIAS, new AttributeValue { N = etag } } };
var siloEntryUpdate = new Update
{
TableName = this.options.TableName,
Key = siloEntry.GetKeys(),
ConditionExpression = etagConditionalExpression
};
(siloEntryUpdate.UpdateExpression, siloEntryUpdate.ExpressionAttributeValues) =
this.storage.ConvertUpdate(siloEntry.GetFields(), siloConditionalValues);
var versionConditionalValues = new Dictionary<string, AttributeValue> { { CURRENT_ETAG_ALIAS, new AttributeValue { N = tableVersion.VersionEtag } } };
var versionEntryUpdate = new Update
{
TableName = this.options.TableName,
Key = versionEntry.GetKeys(),
ConditionExpression = etagConditionalExpression
};
(versionEntryUpdate.UpdateExpression, versionEntryUpdate.ExpressionAttributeValues) =
this.storage.ConvertUpdate(versionEntry.GetFields(), versionConditionalValues);
await this.storage.WriteTxAsync(updates: new[] {siloEntryUpdate, versionEntryUpdate}, cancellationToken: cancellationToken);
result = true;
}
catch (TransactionCanceledException canceledException)
{
if (canceledException.Message.Contains("ConditionalCheckFailed")) //not a good way to check for this currently
{
result = false;
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
canceledException,
"Update failed due to contention on the table. Will retry. Entry {Entry}, eTag {ETag}",
entry.ToString(),
etag);
}
else
{
throw;
}
}
return result;
}
catch (Exception exc)
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
exc,
"Intermediate error updating entry {Entry} to the table {TableName}.",
entry.ToString(),
this.options.TableName);
throw;
}
}
public async Task UpdateIAmAlive(MembershipEntry entry, CancellationToken cancellationToken)
{
try
{
if (this.logger.IsEnabled(LogLevel.Debug)) this.logger.LogDebug("Merge entry = {Entry}", entry.ToString());
var siloEntry = ConvertPartial(entry);
var fields = new Dictionary<string, AttributeValue> { { SiloInstanceRecord.I_AM_ALIVE_TIME_PROPERTY_NAME, new AttributeValue(siloEntry.IAmAliveTime) } };
var expression = $"attribute_exists({SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}) AND attribute_exists({SiloInstanceRecord.SILO_IDENTITY_PROPERTY_NAME})";
await this.storage.UpsertEntryAsync(this.options.TableName, siloEntry.GetKeys(),fields, expression, cancellationToken: cancellationToken);
}
catch (Exception exc)
{
this.logger.LogWarning(
(int)ErrorCode.MembershipBase,
exc,
"Intermediate error updating IAmAlive field for entry {Entry} to the table {TableName}.",
entry.ToString(),
this.options.TableName);
throw;
}
}
private MembershipTableData Convert(List<SiloInstanceRecord> entries)
{
try
{
var memEntries = new List<Tuple<MembershipEntry, string>>();
var tableVersion = NotFoundTableVersion;
foreach (var tableEntry in entries)
{
if (tableEntry.SiloIdentity == SiloInstanceRecord.TABLE_VERSION_ROW)
{
tableVersion = new TableVersion(tableEntry.MembershipVersion, tableEntry.ETag.ToString(CultureInfo.InvariantCulture));
}
else
{
try
{
MembershipEntry membershipEntry = Parse(tableEntry);
memEntries.Add(new Tuple<MembershipEntry, string>(membershipEntry,
tableEntry.ETag.ToString(CultureInfo.InvariantCulture)));
}
catch (Exception exc)
{
this.logger.LogError(
(int)ErrorCode.MembershipBase,
exc,
"Intermediate error parsing SiloInstanceTableEntry to MembershipTableData: {TableEntry}. Ignoring this entry.",
tableEntry);
}
}
}
var data = new MembershipTableData(memEntries, tableVersion);
return data;
}
catch (Exception exc)
{
this.logger.LogError(
(int)ErrorCode.MembershipBase,
exc,
"Intermediate error parsing SiloInstanceTableEntry to MembershipTableData: {Entries}.",
Utils.EnumerableToString(entries));
throw;
}
}
private MembershipEntry Parse(SiloInstanceRecord tableEntry)
{
var parse = new MembershipEntry
{
HostName = tableEntry.HostName,
Status = (SiloStatus)tableEntry.Status
};
parse.ProxyPort = tableEntry.ProxyPort;
parse.SiloAddress = SiloAddress.New(IPAddress.Parse(tableEntry.Address), tableEntry.Port, tableEntry.Generation);
if (!string.IsNullOrEmpty(tableEntry.SiloName))
{
parse.SiloName = tableEntry.SiloName;
}
parse.StartTime = !string.IsNullOrEmpty(tableEntry.StartTime) ?
LogFormatter.ParseDate(tableEntry.StartTime) : default(DateTime);
parse.IAmAliveTime = !string.IsNullOrEmpty(tableEntry.IAmAliveTime) ?
LogFormatter.ParseDate(tableEntry.IAmAliveTime) : default(DateTime);
var suspectingSilos = new List<SiloAddress>();
var suspectingTimes = new List<DateTime>();
if (!string.IsNullOrEmpty(tableEntry.SuspectingSilos))
{
string[] silos = tableEntry.SuspectingSilos.Split('|');
foreach (string silo in silos)
{
suspectingSilos.Add(SiloAddress.FromParsableString(silo));
}
}
if (!string.IsNullOrEmpty(tableEntry.SuspectingTimes))
{
string[] times = tableEntry.SuspectingTimes.Split('|');
foreach (string time in times)
suspectingTimes.Add(LogFormatter.ParseDate(time));
}
if (suspectingSilos.Count != suspectingTimes.Count)
throw new OrleansException($"SuspectingSilos.Length of {suspectingSilos.Count} as read from Azure table is not equal to SuspectingTimes.Length of {suspectingTimes.Count}");
for (int i = 0; i < suspectingSilos.Count; i++)
parse.AddSuspector(suspectingSilos[i], suspectingTimes[i]);
return parse;
}
private SiloInstanceRecord Convert(MembershipEntry memEntry, TableVersion tableVersion)
{
var tableEntry = new SiloInstanceRecord
{
DeploymentId = this.clusterId,
Address = memEntry.SiloAddress.Endpoint.Address.ToString(),
Port = memEntry.SiloAddress.Endpoint.Port,
Generation = memEntry.SiloAddress.Generation,
HostName = memEntry.HostName,
Status = (int)memEntry.Status,
ProxyPort = memEntry.ProxyPort,
SiloName = memEntry.SiloName,
StartTime = LogFormatter.PrintDate(memEntry.StartTime),
IAmAliveTime = LogFormatter.PrintDate(memEntry.IAmAliveTime),
SiloIdentity = SiloInstanceRecord.ConstructSiloIdentity(memEntry.SiloAddress),
MembershipVersion = tableVersion.Version
};
if (memEntry.SuspectTimes != null)
{
var siloList = new StringBuilder();
var timeList = new StringBuilder();
bool first = true;
foreach (var tuple in memEntry.SuspectTimes)
{
if (!first)
{
siloList.Append('|');
timeList.Append('|');
}
siloList.Append(tuple.Item1.ToParsableString());
timeList.Append(LogFormatter.PrintDate(tuple.Item2));
first = false;
}
tableEntry.SuspectingSilos = siloList.ToString();
tableEntry.SuspectingTimes = timeList.ToString();
}
else
{
tableEntry.SuspectingSilos = string.Empty;
tableEntry.SuspectingTimes = string.Empty;
}
return tableEntry;
}
private SiloInstanceRecord ConvertPartial(MembershipEntry memEntry)
{
return new SiloInstanceRecord
{
DeploymentId = this.clusterId,
IAmAliveTime = LogFormatter.PrintDate(memEntry.IAmAliveTime),
SiloIdentity = SiloInstanceRecord.ConstructSiloIdentity(memEntry.SiloAddress)
};
}
public async Task CleanupDefunctSiloEntries(DateTimeOffset beforeDate, CancellationToken cancellationToken)
{
try
{
var keys = new Dictionary<string, AttributeValue>
{
{ $":{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}", new AttributeValue(this.clusterId) },
};
var filter = $"{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME} = :{SiloInstanceRecord.DEPLOYMENT_ID_PROPERTY_NAME}";
var records = await this.storage.QueryAllAsync(
this.options.TableName,
keys,
filter,
item => new SiloInstanceRecord(item),
cancellationToken: cancellationToken);
var defunctRecordKeys = records.Where(r => SiloIsDefunct(r, beforeDate)).Select(r => r.GetKeys());
var tasks = new List<Task>();
foreach (var batch in defunctRecordKeys.BatchIEnumerable(MAX_BATCH_SIZE))
{
tasks.Add(this.storage.DeleteEntriesAsync(this.options.TableName, batch, cancellationToken));
}
await Task.WhenAll(tasks);
}
catch (Exception exc)
{
this.logger.LogError(
(int)ErrorCode.MembershipBase,
exc,
"Unable to clean up defunct membership records on table {TableName} for ClusterId {ClusterId}",
this.options.TableName,
this.clusterId);
throw;
}
}
private static bool SiloIsDefunct(SiloInstanceRecord silo, DateTimeOffset beforeDate)
{
return DateTimeOffset.TryParse(silo.IAmAliveTime, out var iAmAliveTime)
&& iAmAliveTime < beforeDate
&& silo.Status != (int)SiloStatus.Active;
}
}
}