-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathAzureTableStorage.cs
550 lines (496 loc) · 23.6 KB
/
AzureTableStorage.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.Data.Tables;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
using Orleans.Configuration.Overrides;
using Orleans.Persistence.AzureStorage;
using Orleans.Providers.Azure;
using Orleans.Runtime;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace Orleans.Storage
{
/// <summary>
/// Simple storage for writing grain state data to Azure table storage.
/// </summary>
public class AzureTableGrainStorage : IGrainStorage, IRestExceptionDecoder, ILifecycleParticipant<ISiloLifecycle>
{
private readonly AzureTableStorageOptions options;
private readonly ClusterOptions clusterOptions;
private readonly IGrainStorageSerializer storageSerializer;
private readonly ILogger logger;
private GrainStateTableDataManager tableDataManager;
// each property can hold 64KB of data and each entity can take 1MB in total, so 15 full properties take
// 15 * 64 = 960 KB leaving room for the primary key, timestamp etc
private const int MAX_DATA_CHUNK_SIZE = 64 * 1024;
private const int MAX_STRING_PROPERTY_LENGTH = 32 * 1024;
private const int MAX_DATA_CHUNKS_COUNT = 15;
private const string BINARY_DATA_PROPERTY_NAME = "Data";
private const string STRING_DATA_PROPERTY_NAME = "StringData";
private readonly string name;
/// <summary> Default constructor </summary>
public AzureTableGrainStorage(
string name,
AzureTableStorageOptions options,
IOptions<ClusterOptions> clusterOptions,
IServiceProvider services,
ILogger<AzureTableGrainStorage> logger)
{
this.options = options;
this.clusterOptions = clusterOptions.Value;
this.name = name;
this.storageSerializer = options.GrainStorageSerializer;
this.logger = logger;
}
/// <summary> Read state data function for this storage provider. </summary>
/// <see cref="IGrainStorage.ReadStateAsync{T}"/>
public async Task ReadStateAsync<T>(string grainType, GrainId grainId, IGrainState<T> grainState)
{
if (tableDataManager == null) throw new ArgumentException("GrainState-Table property not initialized");
string pk = GetKeyString(grainId);
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_ReadingData,
"Reading: GrainType={GrainType} Pk={PartitionKey} Grainid={GrainId} from Table={TableName}",
grainType,
pk,
grainId,
this.options.TableName);
string partitionKey = pk;
string rowKey = AzureTableUtils.SanitizeTableProperty(grainType);
var entity = await tableDataManager.Read(partitionKey, rowKey).ConfigureAwait(false);
if (entity is not null)
{
var loadedState = ConvertFromStorageFormat<T>(entity);
grainState.RecordExists = loadedState != null;
grainState.State = loadedState ?? Activator.CreateInstance<T>();
grainState.ETag = entity.ETag.ToString();
}
// Else leave grainState in previous default condition
}
/// <summary> Write state data function for this storage provider. </summary>
/// <see cref="IGrainStorage.WriteStateAsync{T}"/>
public async Task WriteStateAsync<T>(string grainType, GrainId grainId, IGrainState<T> grainState)
{
if (tableDataManager == null) throw new ArgumentException("GrainState-Table property not initialized");
string pk = GetKeyString(grainId);
if (logger.IsEnabled(LogLevel.Trace))
logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_WritingData,
"Writing: GrainType={GrainType} Pk={PartitionKey} Grainid={GrainId} ETag={ETag} to Table={TableName}",
grainType,
pk,
grainId,
grainState.ETag,
this.options.TableName);
var rowKey = AzureTableUtils.SanitizeTableProperty(grainType);
var entity = new TableEntity(pk, rowKey)
{
ETag = new ETag(grainState.ETag)
};
ConvertToStorageFormat(grainState.State, entity);
try
{
await DoOptimisticUpdate(() => tableDataManager.Write(entity), grainType, grainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);
grainState.ETag = entity.ETag.ToString();
grainState.RecordExists = true;
}
catch (Exception exc)
{
logger.LogError((int)AzureProviderErrorCode.AzureTableProvider_WriteError, exc,
"Error Writing: GrainType={GrainType} GrainId={GrainId} ETag={ETag} to Table={TableName}",
grainType,
grainId,
grainState.ETag,
this.options.TableName);
throw;
}
}
/// <summary> Clear / Delete state data function for this storage provider. </summary>
/// <remarks>
/// If the <c>DeleteStateOnClear</c> is set to <c>true</c> then the table row
/// for this grain will be deleted / removed, otherwise the table row will be
/// cleared by overwriting with default / null values.
/// </remarks>
/// <see cref="IGrainStorage.ClearStateAsync{T}"/>
public async Task ClearStateAsync<T>(string grainType, GrainId grainId, IGrainState<T> grainState)
{
if (tableDataManager == null) throw new ArgumentException("GrainState-Table property not initialized");
string pk = GetKeyString(grainId);
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_WritingData,
"Clearing: GrainType={GrainType} Pk={PartitionKey} Grainid={GrainId} ETag={ETag} DeleteStateOnClear={DeleteStateOnClear} from Table={TableName}",
grainType,
pk,
grainId,
grainState.ETag,
this.options.DeleteStateOnClear,
this.options.TableName);
var rowKey = AzureTableUtils.SanitizeTableProperty(grainType);
var entity = new TableEntity(pk, rowKey)
{
ETag = new ETag(grainState.ETag)
};
string operation = "Clearing";
try
{
if (this.options.DeleteStateOnClear)
{
operation = "Deleting";
await DoOptimisticUpdate(() => tableDataManager.Delete(entity), grainType, grainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);
}
else
{
await DoOptimisticUpdate(() => tableDataManager.Write(entity), grainType, grainId, this.options.TableName, grainState.ETag).ConfigureAwait(false);
}
grainState.ETag = entity.ETag.ToString(); // Update in-memory data to the new ETag
grainState.RecordExists = false;
}
catch (Exception exc)
{
logger.LogError((int)AzureProviderErrorCode.AzureTableProvider_DeleteError,
exc,
"Error {Operation}: GrainType={GrainType} Grainid={GrainId} ETag={ETag} from Table={TableName}",
operation,
grainType,
grainId,
grainState.ETag,
this.options.TableName);
throw;
}
}
private static async Task DoOptimisticUpdate(Func<Task> updateOperation, string grainType, GrainId grainId, string tableName, string currentETag)
{
try
{
await updateOperation.Invoke().ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.IsPreconditionFailed() || ex.IsConflict() || ex.IsNotFound())
{
throw new TableStorageUpdateConditionNotSatisfiedException(grainType, grainId.ToString(), tableName, "Unknown", currentETag, ex);
}
}
/// <summary>
/// Serialize to Azure storage format in either binary or JSON format.
/// </summary>
/// <param name="grainState">The grain state data to be serialized</param>
/// <param name="entity">The Azure table entity the data should be stored in</param>
/// <remarks>
/// See:
/// http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
/// for more on the JSON serializer.
/// </remarks>
internal void ConvertToStorageFormat<T>(T grainState, TableEntity entity)
{
var binaryData = storageSerializer.Serialize<T>(grainState);
CheckMaxDataSize(binaryData.ToMemory().Length, MAX_DATA_CHUNK_SIZE * MAX_DATA_CHUNKS_COUNT);
if (options.UseStringFormat)
{
var properties = SplitStringData(binaryData.ToString().AsMemory());
foreach (var keyValuePair in properties.Zip(GetPropertyNames(STRING_DATA_PROPERTY_NAME),
(property, name) => new KeyValuePair<string, object>(name, property.ToString())))
{
entity[keyValuePair.Key] = keyValuePair.Value;
}
}
else
{
var properties = SplitBinaryData(binaryData);
foreach (var keyValuePair in properties.Zip(GetPropertyNames(BINARY_DATA_PROPERTY_NAME),
(property, name) => new KeyValuePair<string, object>(name, property.ToArray())))
{
entity[keyValuePair.Key] = keyValuePair.Value;
}
}
}
private void CheckMaxDataSize(int dataSize, int maxDataSize)
{
if (dataSize > maxDataSize)
{
var msg = string.Format("Data too large to write to Azure table. Size={0} MaxSize={1}", dataSize, maxDataSize);
logger.LogError(0, "Data too large to write to Azure table. Size={Size} MaxSize={MaxSize}", dataSize, maxDataSize);
throw new ArgumentOutOfRangeException("GrainState.Size", msg);
}
}
private static IEnumerable<ReadOnlyMemory<char>> SplitStringData(ReadOnlyMemory<char> stringData)
{
var startIndex = 0;
while (startIndex < stringData.Length)
{
var chunkSize = Math.Min(MAX_STRING_PROPERTY_LENGTH, stringData.Length - startIndex);
yield return stringData.Slice(startIndex, chunkSize);
startIndex += chunkSize;
}
}
private static IEnumerable<ReadOnlyMemory<byte>> SplitBinaryData(ReadOnlyMemory<byte> binaryData)
{
var startIndex = 0;
while (startIndex < binaryData.Length)
{
var chunkSize = Math.Min(MAX_DATA_CHUNK_SIZE, binaryData.Length - startIndex);
yield return binaryData.Slice(startIndex, chunkSize);
startIndex += chunkSize;
}
}
private static IEnumerable<string> GetPropertyNames(string basePropertyName)
{
yield return basePropertyName;
for (var i = 1; i < MAX_DATA_CHUNKS_COUNT; ++i)
{
yield return basePropertyName + i;
}
}
private static IEnumerable<byte[]> ReadBinaryDataChunks(TableEntity entity)
{
foreach (var binaryDataPropertyName in GetPropertyNames(BINARY_DATA_PROPERTY_NAME))
{
if (entity.TryGetValue(binaryDataPropertyName, out var dataProperty))
{
switch (dataProperty)
{
// if TablePayloadFormat.JsonNoMetadata is used
case string stringValue:
if (!string.IsNullOrEmpty(stringValue))
{
yield return Convert.FromBase64String(stringValue);
}
break;
// if any payload type providing metadata is used
case byte[] binaryValue:
if (binaryValue != null && binaryValue.Length > 0)
{
yield return binaryValue;
}
break;
}
}
}
}
private static byte[] ReadBinaryData(TableEntity entity)
{
var dataChunks = ReadBinaryDataChunks(entity).ToArray();
var dataSize = dataChunks.Select(d => d.Length).Sum();
var result = new byte[dataSize];
var startIndex = 0;
foreach (var dataChunk in dataChunks)
{
Array.Copy(dataChunk, 0, result, startIndex, dataChunk.Length);
startIndex += dataChunk.Length;
}
return result;
}
private static IEnumerable<string> ReadStringDataChunks(TableEntity entity)
{
foreach (var stringDataPropertyName in GetPropertyNames(STRING_DATA_PROPERTY_NAME))
{
if (entity.TryGetValue(stringDataPropertyName, out var dataProperty))
{
if (dataProperty is string {Length: > 0 } data)
{
yield return data;
}
}
}
}
private static string ReadStringData(TableEntity entity)
{
return string.Join(string.Empty, ReadStringDataChunks(entity));
}
/// <summary>
/// Deserialize from Azure storage format
/// </summary>
/// <param name="entity">The Azure table entity the stored data</param>
internal T ConvertFromStorageFormat<T>(TableEntity entity)
{
// Read from both column type for backward compatibility
var binaryData = ReadBinaryData(entity);
var stringData = ReadStringData(entity);
T dataValue = default;
try
{
var input = binaryData.Length > 0
? new BinaryData(binaryData)
: new BinaryData(stringData);
dataValue = this.storageSerializer.Deserialize<T>(input);
}
catch (Exception exc)
{
var sb = new StringBuilder();
if (binaryData.Length > 0)
{
sb.AppendFormat("Unable to convert from storage format GrainStateEntity.Data={0}", binaryData);
}
else if (!string.IsNullOrEmpty(stringData))
{
sb.AppendFormat("Unable to convert from storage format GrainStateEntity.StringData={0}", stringData);
}
if (dataValue != null)
{
sb.AppendFormat("Data Value={0} Type={1}", dataValue, dataValue.GetType());
}
logger.LogError(exc, "{Message}", sb.ToString());
throw new AggregateException(sb.ToString(), exc);
}
return dataValue;
}
private string GetKeyString(GrainId grainId)
{
var key = $"{clusterOptions.ServiceId}_{grainId}";
return AzureTableUtils.SanitizeTableProperty(key);
}
private class GrainStateTableDataManager
{
public string TableName { get; private set; }
private readonly AzureTableDataManager<TableEntity> tableManager;
private readonly ILogger logger;
public GrainStateTableDataManager(AzureStorageOperationOptions options, ILogger logger)
{
this.logger = logger;
TableName = options.TableName;
tableManager = new AzureTableDataManager<TableEntity>(options, logger);
}
public Task InitTableAsync()
{
return tableManager.InitTableAsync();
}
public async Task<TableEntity> Read(string partitionKey, string rowKey)
{
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_Storage_Reading,
"Reading: PartitionKey={PartitionKey} RowKey={RowKey} from Table={TableName}",
partitionKey,
rowKey,
TableName);
try
{
var data = await tableManager.ReadSingleTableEntryAsync(partitionKey, rowKey).ConfigureAwait(false);
if (data.Entity == null)
{
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_DataNotFound,
"DataNotFound reading: PartitionKey={PartitionKey} RowKey={RowKey} from Table={TableName}",
partitionKey,
rowKey,
TableName);
return default;
}
TableEntity stateEntity = data.Entity;
var record = stateEntity;
record.ETag = new ETag(data.ETag);
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_Storage_DataRead,
"Read: PartitionKey={PartitionKey} RowKey={RowKey} from Table={TableName} with ETag={ETag}",
stateEntity.PartitionKey,
stateEntity.RowKey,
TableName,
record.ETag);
return record;
}
catch (Exception exc)
{
if (AzureTableUtils.TableStorageDataNotFound(exc))
{
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_DataNotFound,
exc,
"DataNotFound reading (exception): PartitionKey={PartitionKey} RowKey={RowKey} from Table={TableName}",
partitionKey,
rowKey,
TableName);
return default; // No data
}
throw;
}
}
public async Task Write(TableEntity entity)
{
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_Storage_Writing,
"Writing: PartitionKey={PartitionKey} RowKey={RowKey} to Table={TableName} with ETag={ETag}",
entity.PartitionKey,
entity.RowKey,
TableName,
entity.ETag);
string eTag = string.IsNullOrEmpty(entity.ETag.ToString()) ?
await tableManager.CreateTableEntryAsync(entity).ConfigureAwait(false) :
await tableManager.UpdateTableEntryAsync(entity, entity.ETag).ConfigureAwait(false);
entity.ETag = new ETag(eTag);
}
public async Task Delete(TableEntity entity)
{
if (string.IsNullOrWhiteSpace(entity.ETag.ToString()))
{
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_DataNotFound,
"Not attempting to delete non-existent persistent state: PartitionKey={PartitionKey} RowKey={RowKey} from Table={TableName} with ETag={ETag}",
entity.PartitionKey,
entity.RowKey,
TableName,
entity.ETag);
return;
}
if (logger.IsEnabled(LogLevel.Trace)) logger.LogTrace((int)AzureProviderErrorCode.AzureTableProvider_Storage_Writing,
"Deleting: PartitionKey={PartitionKey} RowKey={RowKey} from Table={TableName} with ETag={ETag}",
entity.PartitionKey,
entity.RowKey,
TableName,
entity.ETag);
await tableManager.DeleteTableEntryAsync(entity, entity.ETag).ConfigureAwait(false);
entity.ETag = default;
}
}
/// <summary> Decodes Storage exceptions.</summary>
public bool DecodeException(Exception e, out HttpStatusCode httpStatusCode, out string restStatus, bool getRESTErrors = false)
{
return AzureTableUtils.EvaluateException(e, out httpStatusCode, out restStatus, getRESTErrors);
}
private async Task Init(CancellationToken ct)
{
var stopWatch = Stopwatch.StartNew();
try
{
this.logger.LogInformation((int)AzureProviderErrorCode.AzureTableProvider_InitProvider,
"AzureTableGrainStorage {ProviderName} initializing: {Options}",
name,
this.options.ToString());
this.tableDataManager = new GrainStateTableDataManager(this.options, this.logger);
await this.tableDataManager.InitTableAsync();
stopWatch.Stop();
this.logger.LogInformation((int)AzureProviderErrorCode.AzureTableProvider_InitProvider,
"Initializing provider {ProviderName} of type {ProviderType} in stage {Stage} took {ElapsedMilliseconds} Milliseconds.",
this.name,
this.GetType().Name,
this.options.InitStage,
stopWatch.ElapsedMilliseconds);
}
catch (Exception ex)
{
stopWatch.Stop();
this.logger.LogError((int)ErrorCode.Provider_ErrorFromInit, ex,
"Initialization failed for provider {ProviderName} of type {ProviderType} in stage {Stage} in {ElapsedMilliseconds} Milliseconds.",
this.name,
this.GetType().Name,
this.options.InitStage,
stopWatch.ElapsedMilliseconds);
throw;
}
}
private Task Close(CancellationToken ct)
{
this.tableDataManager = null;
return Task.CompletedTask;
}
public void Participate(ISiloLifecycle lifecycle)
{
lifecycle.Subscribe(OptionFormattingUtilities.Name<AzureTableGrainStorage>(this.name), this.options.InitStage, Init, Close);
}
}
public static class AzureTableGrainStorageFactory
{
public static AzureTableGrainStorage Create(IServiceProvider services, string name)
{
var optionsSnapshot = services.GetRequiredService<IOptionsMonitor<AzureTableStorageOptions>>();
var clusterOptions = services.GetProviderClusterOptions(name);
return ActivatorUtilities.CreateInstance<AzureTableGrainStorage>(services, name, optionsSnapshot.Get(name), clusterOptions);
}
}
}