This repository was archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathCosmosDBMembershipTable.cs
More file actions
executable file
·475 lines (405 loc) · 19.4 KB
/
CosmosDBMembershipTable.cs
File metadata and controls
executable file
·475 lines (405 loc) · 19.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
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.Clustering.CosmosDB.Models;
using Orleans.Configuration;
using Orleans.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace Orleans.Clustering.CosmosDB
{
internal class CosmosDBMembershipTable : IMembershipTable
{
private const string READ_ALL_QUERY = "SELECT * FROM c";
private const string READ_ENTRY_QUERY = "SELECT * FROM c WHERE c.id = @siloId OR c.id = 'ClusterVersion'";
private const string CLUSTER_VERSION_ID = "ClusterVersion";
private const string PARTITION_KEY = "/ClusterId";
private readonly ILoggerFactory _loggerFactory;
private readonly ILogger _logger;
private readonly CosmosDBClusteringOptions _options;
private readonly ClusterOptions _clusterOptions;
private CosmosClient _cosmos;
private Container _container;
private ItemResponse<SiloEntity> _selfRow;
public CosmosDBMembershipTable(ILoggerFactory loggerFactory, IOptions<ClusterOptions> clusterOptions, IOptions<CosmosDBClusteringOptions> clusteringOptions)
{
this._clusterOptions = clusterOptions.Value;
this._loggerFactory = loggerFactory;
this._logger = loggerFactory?.CreateLogger<CosmosDBMembershipTable>();
this._options = clusteringOptions.Value;
}
public async Task InitializeMembershipTable(bool tryInitTableVersion)
{
if (this._options.Client != null)
{
this._cosmos = this._options.Client;
}
else
{
this._cosmos = new CosmosClient(this._options.AccountEndpoint, this._options.AccountKey,
new CosmosClientOptions
{
ConnectionMode = this._options.ConnectionMode
}
);
}
this._container = this._cosmos.GetDatabase(this._options.DB).GetContainer(this._options.Collection);
if (this._options.CanCreateResources)
{
if (this._options.DropDatabaseOnInit)
{
await this.TryDeleteDatabase();
}
await this.TryCreateCosmosDBResources();
}
ClusterVersionEntity versionEntity = null;
try
{
versionEntity = (await this._container.ReadItemAsync<ClusterVersionEntity>(
CLUSTER_VERSION_ID,
new PartitionKey(this._clusterOptions.ClusterId))).Resource;
}
catch (CosmosException ce) when (ce.StatusCode == HttpStatusCode.NotFound)
{
if (versionEntity == null)
{
versionEntity = new ClusterVersionEntity
{
ClusterId = this._clusterOptions.ClusterId,
ClusterVersion = 0,
Id = CLUSTER_VERSION_ID
};
var response = await this._container.CreateItemAsync(
versionEntity,
new PartitionKey(versionEntity.ClusterId)
);
if (response.StatusCode == HttpStatusCode.Created)
this._logger?.Info("Created new Cluster Version entity.");
}
}
}
public async Task<MembershipTableData> ReadAll()
{
try
{
(ClusterVersionEntity Version, List<SiloEntity> Silos) response = await this.ReadRecords(this._clusterOptions.ClusterId);
ClusterVersionEntity versionEntity = response.Version;
List<SiloEntity> entryEntities = response.Silos;
TableVersion version = null;
if (versionEntity != null)
{
version = new TableVersion(versionEntity.ClusterVersion, versionEntity.ETag);
}
else
{
this._logger.LogError("Initial ClusterVersionEntity entity doesn't exist.");
}
var memEntries = new List<Tuple<MembershipEntry, string>>();
foreach (var entity in entryEntities)
{
try
{
MembershipEntry membershipEntry = ParseEntity(entity);
memEntries.Add(new Tuple<MembershipEntry, string>(membershipEntry, entity.ETag));
}
catch (Exception exc)
{
this._logger.LogError(exc, "Failure reading all membership records.");
throw;
}
}
var data = new MembershipTableData(memEntries, version);
return data;
}
catch (Exception exc)
{
this._logger.LogWarning($"Failure reading all silo entries for cluster id {this._clusterOptions.ClusterId}: {exc}");
throw;
}
}
public async Task DeleteMembershipTableEntries(string clusterId)
{
var all = await this.ReadRecords(clusterId);
var batch = this._container.CreateTransactionalBatch(new PartitionKey(clusterId));
foreach (var silo in all.Silos)
{
batch = batch.DeleteItem(silo.Id);
}
batch = batch.DeleteItem(all.Version.Id);
await batch.ExecuteAsync();
}
public async Task<bool> InsertRow(MembershipEntry entry, TableVersion tableVersion)
{
try
{
var siloEntity = ConvertToEntity(entry, this._clusterOptions.ClusterId);
var versionEntity = this.BuildVersionEntity(tableVersion);
var response = await this._container.CreateTransactionalBatch(new PartitionKey(this._clusterOptions.ClusterId))
.ReplaceItem(versionEntity.Id, versionEntity, new TransactionalBatchItemRequestOptions { IfMatchEtag = tableVersion.VersionEtag })
.CreateItem(siloEntity).ExecuteAsync();
return response.IsSuccessStatusCode;
}
catch (CosmosException exc)
{
if (exc.StatusCode == HttpStatusCode.PreconditionFailed) return false;
throw;
}
}
public async Task<MembershipTableData> ReadRow(SiloAddress key)
{
var id = ConstructSiloEntityId(key);
try
{
var query = this._container
.GetItemQueryIterator<dynamic>(
new QueryDefinition(READ_ENTRY_QUERY).WithParameter("@siloId", id),
requestOptions: new QueryRequestOptions
{
PartitionKey = new PartitionKey(this._clusterOptions.ClusterId)
}
);
var docs = await query.ReadNextAsync();
var versionDoc = docs.Where(i => i.EntityType == nameof(ClusterVersionEntity)).SingleOrDefault();
ClusterVersionEntity clusterVersionEntity = versionDoc != null ? ClusterVersionEntity.FromDocument(versionDoc) : default;
var siloEntities = docs.Where(i => i.EntityType == nameof(SiloEntity)).Select<dynamic, SiloEntity>(d => SiloEntity.FromDocument(d));
TableVersion version = null;
if (clusterVersionEntity != null)
{
version = new TableVersion(clusterVersionEntity.ClusterVersion, clusterVersionEntity.ETag);
}
else
{
this._logger.LogError("Initial ClusterVersionEntity entity doesn't exist.");
}
var memEntries = new List<Tuple<MembershipEntry, string>>();
foreach (var entity in siloEntities)
{
try
{
MembershipEntry membershipEntry = ParseEntity(entity);
memEntries.Add(new Tuple<MembershipEntry, string>(membershipEntry, entity.ETag));
}
catch (Exception exc)
{
this._logger.LogError(exc, "Failure reading membership row.");
throw;
}
}
var data = new MembershipTableData(memEntries, version);
return data;
}
catch (Exception exc)
{
this._logger.LogWarning($"Failure reading silo entry {id} for cluster id {this._clusterOptions.ClusterId}: {exc}");
throw;
}
}
public async Task UpdateIAmAlive(MembershipEntry entry)
{
var siloEntityId = ConstructSiloEntityId(entry.SiloAddress);
if (this._selfRow is not { } selfRow)
{
var response = await this._container.ReadItemAsync<SiloEntity>(siloEntityId, new PartitionKey(this._clusterOptions.ClusterId));
if (response.StatusCode != HttpStatusCode.OK)
{
var message = $"Unable to query for SiloEntity {entry.ToFullString()}";
this._logger.LogWarning((int)ErrorCode.MembershipBase, message);
throw new OrleansException(message);
}
this._selfRow = selfRow = response;
}
var siloEntity = selfRow.Resource;
siloEntity.IAmAliveTime = entry.IAmAliveTime;
try
{
var replaceResponse = await this._container.ReplaceItemAsync(
siloEntity,
siloEntityId,
new PartitionKey(this._clusterOptions.ClusterId),
new ItemRequestOptions { IfMatchEtag = selfRow.ETag });
this._selfRow = replaceResponse;
}
catch
{
this._selfRow = null;
throw;
}
}
public async Task<bool> UpdateRow(MembershipEntry entry, string etag, TableVersion tableVersion)
{
try
{
var siloEntity = ConvertToEntity(entry, this._clusterOptions.ClusterId);
siloEntity.ETag = etag;
var versionEntity = this.BuildVersionEntity(tableVersion);
var response = await this._container.CreateTransactionalBatch(new PartitionKey(this._clusterOptions.ClusterId))
.ReplaceItem(versionEntity.Id, versionEntity, new TransactionalBatchItemRequestOptions { IfMatchEtag = tableVersion.VersionEtag })
.ReplaceItem(siloEntity.Id, siloEntity, new TransactionalBatchItemRequestOptions { IfMatchEtag = siloEntity.ETag }).ExecuteAsync();
return response.IsSuccessStatusCode;
}
catch (CosmosException exc)
{
if (exc.StatusCode == HttpStatusCode.PreconditionFailed) return false;
throw;
}
}
private static MembershipEntry ParseEntity(SiloEntity entity)
{
var entry = new MembershipEntry
{
HostName = entity.Hostname,
Status = entity.Status
};
if (entity.ProxyPort.HasValue)
entry.ProxyPort = entity.ProxyPort.Value;
entry.SiloAddress = SiloAddress.New(new IPEndPoint(IPAddress.Parse(entity.Address), entity.Port), entity.Generation);
entry.SiloName = entity.SiloName;
entry.StartTime = entity.StartTime.UtcDateTime;
entry.IAmAliveTime = entity.IAmAliveTime.UtcDateTime;
var suspectingSilos = new List<SiloAddress>();
var suspectingTimes = new List<DateTime>();
foreach (var silo in entity.SuspectingSilos)
{
suspectingSilos.Add(SiloAddress.FromParsableString(silo));
}
foreach (var time in entity.SuspectingTimes)
{
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 eqaul to SuspectingTimes.Length of {suspectingTimes.Count}");
for (int i = 0; i < suspectingSilos.Count; i++)
entry.AddSuspector(suspectingSilos[i], suspectingTimes[i]);
return entry;
}
private static SiloEntity ConvertToEntity(MembershipEntry memEntry, string clusterId)
{
var tableEntry = new SiloEntity
{
Id = ConstructSiloEntityId(memEntry.SiloAddress),
ClusterId = clusterId,
Address = memEntry.SiloAddress.Endpoint.Address.ToString(),
Port = memEntry.SiloAddress.Endpoint.Port,
Generation = memEntry.SiloAddress.Generation,
Hostname = memEntry.HostName,
Status = memEntry.Status,
ProxyPort = memEntry.ProxyPort,
SiloName = memEntry.SiloName,
StartTime = memEntry.StartTime,
IAmAliveTime = memEntry.IAmAliveTime
};
if (memEntry.SuspectTimes != null)
{
foreach (var tuple in memEntry.SuspectTimes)
{
tableEntry.SuspectingSilos.Add(tuple.Item1.ToParsableString());
tableEntry.SuspectingTimes.Add(LogFormatter.PrintDate(tuple.Item2));
}
}
return tableEntry;
}
private static string ConstructSiloEntityId(SiloAddress silo)
{
return $"{silo.Endpoint.Address}-{silo.Endpoint.Port}-{silo.Generation}";
}
private ClusterVersionEntity BuildVersionEntity(TableVersion tableVersion)
{
return new ClusterVersionEntity
{
ClusterId = this._clusterOptions.ClusterId,
ClusterVersion = tableVersion.Version,
Id = CLUSTER_VERSION_ID,
ETag = tableVersion.VersionEtag
};
}
private async Task TryDeleteDatabase()
{
try
{
await this._cosmos.GetDatabase(this._options.DB).DeleteAsync();
}
catch (CosmosException dce) when (dce.StatusCode == System.Net.HttpStatusCode.NotFound)
{
return;
}
}
private async Task TryCreateCosmosDBResources()
{
var dbResponse = (await this._cosmos.CreateDatabaseIfNotExistsAsync(this._options.DB)).Database;
var containerProperties = new ContainerProperties(this._options.Collection, PARTITION_KEY);
containerProperties.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
containerProperties.IndexingPolicy.IncludedPaths.Add(new IncludedPath { Path = "/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/Address/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/Port/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/Generation/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/Hostname/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/SiloName/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/\"SuspectingSilos\"/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/\"SuspectingTimes\"/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/StartTime/*" });
containerProperties.IndexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/IAmAliveTime/*" });
//var consistency = this._options.GetConsistencyLevel();
//if (consistency.HasValue)
//{
// containerProperties.IndexingPolicy.IndexingMode = consistency.Value;
//}
containerProperties.IndexingPolicy.IndexingMode = IndexingMode.Consistent;
if (this._options.UseDedicatedThroughput)
{
ThroughputProperties throughputProperties = this._options.UseAutoscaleThroughput
? ThroughputProperties.CreateAutoscaleThroughput(this._options.AutoscaleThroughputMax)
: ThroughputProperties.CreateManualThroughput(this._options.CollectionThroughput);
await dbResponse.CreateContainerIfNotExistsAsync(containerProperties, throughputProperties);
}
else
{
await dbResponse.CreateContainerIfNotExistsAsync(containerProperties);
}
}
public async Task CleanupDefunctSiloEntries(DateTimeOffset beforeDate)
{
var allSilos = (await this.ReadRecords(this._clusterOptions.ClusterId)).Silos;
if (allSilos.Count == 0) return;
var toDelete = allSilos.Where(s => s.Status == SiloStatus.Dead && s.IAmAliveTime < beforeDate);
var tasks = new List<Task>();
var pk = new PartitionKey(this._clusterOptions.ClusterId);
foreach (var deadSilo in toDelete)
{
tasks.Add(
this._container.DeleteItemAsync<SiloEntity>(
deadSilo.Id,
pk
)
);
}
await Task.WhenAll(tasks);
}
private async Task<(ClusterVersionEntity Version, List<SiloEntity> Silos)> ReadRecords(string clusterId)
{
var query = this._container
.GetItemQueryIterator<dynamic>(
READ_ALL_QUERY,
requestOptions: new QueryRequestOptions
{
PartitionKey = new PartitionKey(clusterId)
}
);
var silos = new List<SiloEntity>();
ClusterVersionEntity clusterVersion = null;
do
{
var items = await query.ReadNextAsync();
var version = items.Where(i => i.EntityType == nameof(ClusterVersionEntity)).SingleOrDefault();
if (version != null)
{
clusterVersion = ClusterVersionEntity.FromDocument(version);
}
silos.AddRange(items.Where(i => i.EntityType == nameof(SiloEntity)).Select(d => (SiloEntity)SiloEntity.FromDocument(d)));
} while (query.HasMoreResults);
return (clusterVersion, silos);
}
}
}