-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathNatsKVStore.cs
502 lines (420 loc) · 18 KB
/
NatsKVStore.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
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using NATS.Client.Core;
using NATS.Client.JetStream;
using NATS.Client.JetStream.Models;
using NATS.Client.KeyValueStore.Internal;
namespace NATS.Client.KeyValueStore;
/// <summary>
/// Key Value Store entry operation
/// </summary>
public enum NatsKVOperation
{
/// <summary>
/// A value was put into the bucket
/// </summary>
Put,
/// <summary>
/// A value was deleted from a bucket
/// </summary>
Del,
/// <summary>
/// A value was purged from a bucket
/// </summary>
Purge,
}
/// <summary>
/// Key Value Store
/// </summary>
public class NatsKVStore : INatsKVStore
{
private const string NatsExpectedLastSubjectSequence = "Nats-Expected-Last-Subject-Sequence";
private const string KVOperation = "KV-Operation";
private const string NatsRollup = "Nats-Rollup";
private const string OperationPurge = "PURGE";
private const string RollupSub = "sub";
private const string OperationDel = "DEL";
private const string NatsSubject = "Nats-Subject";
private const string NatsSequence = "Nats-Sequence";
private const string NatsTimeStamp = "Nats-Time-Stamp";
private static readonly Regex ValidKeyRegex = new(pattern: @"\A[-/_=\.a-zA-Z0-9]+\z", RegexOptions.Compiled);
private static readonly NatsKVException MissingSequenceHeaderException = new("Missing sequence header");
private static readonly NatsKVException MissingTimestampHeaderException = new("Missing timestamp header");
private static readonly NatsKVException MissingHeadersException = new("Missing headers");
private static readonly NatsKVException UnexpectedSubjectException = new("Unexpected subject");
private static readonly NatsKVException UnexpectedNumberOfOperationHeadersException = new("Unexpected number of operation headers");
private static readonly NatsKVException InvalidSequenceException = new("Can't parse sequence header");
private static readonly NatsKVException InvalidTimestampException = new("Can't parse timestamp header");
private static readonly NatsKVException InvalidOperationException = new("Can't parse operation header");
private readonly INatsJSStream _stream;
private readonly string _kvBucket;
internal NatsKVStore(string bucket, INatsJSContext context, INatsJSStream stream)
{
Bucket = bucket;
JetStreamContext = context;
_stream = stream;
_kvBucket = $"$KV.{Bucket}.";
}
/// <inheritdoc />
public INatsJSContext JetStreamContext { get; }
/// <inheritdoc />
public string Bucket { get; }
/// <inheritdoc />
public async ValueTask<ulong> PutAsync<T>(string key, T value, INatsSerialize<T>? serializer = default, CancellationToken cancellationToken = default)
{
ValidateKey(key);
var ack = await JetStreamContext.PublishAsync($"$KV.{Bucket}.{key}", value, serializer: serializer, cancellationToken: cancellationToken);
ack.EnsureSuccess();
return ack.Seq;
}
/// <inheritdoc />
public async ValueTask<ulong> CreateAsync<T>(string key, T value, INatsSerialize<T>? serializer = default, CancellationToken cancellationToken = default)
{
ValidateKey(key);
// First try to create a new entry
try
{
return await UpdateAsync(key, value, revision: 0, serializer, cancellationToken);
}
catch (NatsKVWrongLastRevisionException)
{
}
// If that fails, try to update an existing entry which may have been deleted
try
{
await GetEntryAsync<T>(key, cancellationToken: cancellationToken);
}
catch (NatsKVKeyDeletedException e)
{
return await UpdateAsync(key, value, e.Revision, serializer, cancellationToken);
}
throw new NatsKVCreateException();
}
/// <inheritdoc />
public async ValueTask<ulong> UpdateAsync<T>(string key, T value, ulong revision, INatsSerialize<T>? serializer = default, CancellationToken cancellationToken = default)
{
ValidateKey(key);
var headers = new NatsHeaders { { NatsExpectedLastSubjectSequence, revision.ToString() } };
try
{
var ack = await JetStreamContext.PublishAsync($"$KV.{Bucket}.{key}", value, headers: headers, serializer: serializer, cancellationToken: cancellationToken);
ack.EnsureSuccess();
return ack.Seq;
}
catch (NatsJSApiException e)
{
if (e.Error is { ErrCode: 10071, Code: 400, Description: not null } && e.Error.Description.StartsWith("wrong last sequence", StringComparison.OrdinalIgnoreCase))
{
throw new NatsKVWrongLastRevisionException();
}
throw;
}
}
/// <inheritdoc />
public async ValueTask DeleteAsync(string key, NatsKVDeleteOpts? opts = default, CancellationToken cancellationToken = default)
{
ValidateKey(key);
opts ??= new NatsKVDeleteOpts();
var headers = new NatsHeaders();
if (opts.Purge)
{
headers.Add(KVOperation, OperationPurge);
headers.Add(NatsRollup, RollupSub);
}
else
{
headers.Add(KVOperation, OperationDel);
}
if (opts.Revision != default)
{
headers.Add(NatsExpectedLastSubjectSequence, opts.Revision.ToString());
}
var subject = $"$KV.{Bucket}.{key}";
try
{
var ack = await JetStreamContext.PublishAsync<object?>(subject, null, headers: headers, cancellationToken: cancellationToken);
ack.EnsureSuccess();
}
catch (NatsJSApiException e)
{
if (e.Error is { ErrCode: 10071, Code: 400, Description: not null } && e.Error.Description.StartsWith("wrong last sequence", StringComparison.OrdinalIgnoreCase))
{
throw new NatsKVWrongLastRevisionException();
}
throw;
}
}
/// <inheritdoc />
public ValueTask PurgeAsync(string key, NatsKVDeleteOpts? opts = default, CancellationToken cancellationToken = default) =>
DeleteAsync(key, (opts ?? new NatsKVDeleteOpts()) with { Purge = true }, cancellationToken);
/// <inheritdoc />
public async ValueTask<NatsKVEntry<T>> GetEntryAsync<T>(string key, ulong revision = default, INatsDeserialize<T>? serializer = default, CancellationToken cancellationToken = default)
{
var result = await TryGetEntryAsync(key, revision, serializer, cancellationToken);
if (!result.Success)
{
ThrowException(result.Error);
}
return result.Value;
}
/// <inheritdoc />
#if !NETSTANDARD
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
#endif
public async ValueTask<NatsResult<NatsKVEntry<T>>> TryGetEntryAsync<T>(string key, ulong revision = default, INatsDeserialize<T>? serializer = default, CancellationToken cancellationToken = default)
{
ValidateKey(key);
serializer ??= JetStreamContext.Connection.Opts.SerializerRegistry.GetDeserializer<T>();
var keySubject = _kvBucket + key;
var request = new StreamMsgGetRequest();
if (revision == default)
{
request.LastBySubj = keySubject;
}
else
{
request.Seq = revision;
request.NextBySubj = keySubject;
}
if (_stream.Info.Config.AllowDirect)
{
var direct = await _stream.GetDirectAsync<T>(request, serializer, cancellationToken);
if (direct is { Headers: { } headers } msg)
{
if (headers.Code == 404)
return NatsKVKeyNotFoundException.Default;
if (!headers.TryGetLastValue(NatsSubject, out var subject))
return MissingSequenceHeaderException;
if (revision != default)
{
if (!string.Equals(subject, keySubject, StringComparison.Ordinal))
{
return UnexpectedSubjectException;
}
}
if (!headers.TryGetLastValue(NatsSequence, out var sequenceValue))
return MissingSequenceHeaderException;
if (!ulong.TryParse(sequenceValue, out var sequence))
return InvalidSequenceException;
if (!headers.TryGetLastValue(NatsTimeStamp, out var timestampValue))
return MissingTimestampHeaderException;
if (!DateTimeOffset.TryParse(timestampValue, out var timestamp))
return InvalidTimestampException;
var operation = NatsKVOperation.Put;
if (headers.TryGetValue(KVOperation, out var operationValues))
{
if (operationValues.Count != 1)
return UnexpectedNumberOfOperationHeadersException;
if (!Enum.TryParse(operationValues[0], ignoreCase: true, out operation))
return InvalidOperationException;
}
if (operation is NatsKVOperation.Del or NatsKVOperation.Purge)
{
return new NatsKVKeyDeletedException(sequence);
}
return new NatsKVEntry<T>(Bucket, key)
{
Bucket = Bucket,
Key = key,
Created = timestamp,
Revision = sequence,
Operation = operation,
Value = msg.Data,
Delta = 0,
UsedDirectGet = true,
Error = msg.Error,
};
}
else
{
return MissingHeadersException;
}
}
else
{
var response = await _stream.GetAsync(request, cancellationToken);
if (revision != default)
{
if (string.Equals(response.Message.Subject, keySubject, StringComparison.Ordinal))
{
return UnexpectedSubjectException;
}
}
T? data;
NatsDeserializeException? deserializeException = null;
if (response.Message.Data.Length > 0)
{
var buffer = new ReadOnlySequence<byte>(response.Message.Data);
try
{
data = serializer.Deserialize(buffer);
}
catch (Exception e)
{
deserializeException = new NatsDeserializeException(buffer.ToArray(), e);
data = default;
}
}
else
{
data = default;
}
return new NatsKVEntry<T>(Bucket, key)
{
Created = response.Message.Time,
Revision = response.Message.Seq,
Value = data,
UsedDirectGet = false,
Error = deserializeException,
};
}
}
/// <inheritdoc />
public IAsyncEnumerable<NatsKVEntry<T>> WatchAsync<T>(string key, INatsDeserialize<T>? serializer = default, NatsKVWatchOpts? opts = default, CancellationToken cancellationToken = default)
=> WatchAsync<T>([key], serializer, opts, cancellationToken);
/// <inheritdoc />
public async IAsyncEnumerable<NatsKVEntry<T>> WatchAsync<T>(IEnumerable<string> keys, INatsDeserialize<T>? serializer = default, NatsKVWatchOpts? opts = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await using var watcher = await WatchInternalAsync<T>(keys, serializer, opts, cancellationToken);
if (watcher.InitialConsumer.Info.NumPending == 0 && opts?.OnNoData != null)
{
if (await opts.OnNoData(cancellationToken))
{
yield break;
}
}
await foreach (var entry in watcher.Entries.ReadAllAsync(cancellationToken).ConfigureAwait(false))
{
yield return entry;
}
}
/// <inheritdoc />
public async IAsyncEnumerable<NatsKVEntry<T>> HistoryAsync<T>(string key, INatsDeserialize<T>? serializer = default, NatsKVWatchOpts? opts = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
try
{
await GetEntryAsync<T>(key, cancellationToken: cancellationToken);
}
catch (NatsKVKeyNotFoundException)
{
yield break;
}
catch (NatsKVKeyDeletedException)
{
}
opts ??= NatsKVWatchOpts.Default;
opts = opts with { IncludeHistory = true };
await using var watcher = await WatchInternalAsync<T>([key], serializer, opts, cancellationToken);
await foreach (var entry in watcher.Entries.ReadAllAsync(cancellationToken).ConfigureAwait(false))
{
yield return entry;
if (entry.Delta == 0)
yield break;
}
}
/// <inheritdoc />
public async ValueTask<NatsKVStatus> GetStatusAsync(CancellationToken cancellationToken = default)
{
await _stream.RefreshAsync(cancellationToken);
var isCompressed = _stream.Info.Config.Compression != StreamConfigCompression.None;
return new NatsKVStatus(Bucket, isCompressed, _stream.Info);
}
/// <inheritdoc />
public IAsyncEnumerable<NatsKVEntry<T>> WatchAsync<T>(INatsDeserialize<T>? serializer = default, NatsKVWatchOpts? opts = default, CancellationToken cancellationToken = default) =>
WatchAsync<T>([">"], serializer, opts, cancellationToken);
/// <inheritdoc />
public async ValueTask PurgeDeletesAsync(NatsKVPurgeOpts? opts = default, CancellationToken cancellationToken = default)
{
opts ??= NatsKVPurgeOpts.Default;
var limit = DateTimeOffset.UtcNow - opts.DeleteMarkersThreshold;
var timeLimited = opts.DeleteMarkersThreshold > TimeSpan.Zero;
var deleted = new List<NatsKVEntry<int>>();
// Type doesn't matter here, we're just using the watcher to get the keys
await using (var watcher = await WatchInternalAsync<int>([">"], opts: new NatsKVWatchOpts { MetaOnly = true }, cancellationToken: cancellationToken))
{
if (watcher.InitialConsumer.Info.NumPending == 0)
return;
await foreach (var entry in watcher.Entries.ReadAllAsync(cancellationToken).ConfigureAwait(false))
{
if (entry.Operation is NatsKVOperation.Purge or NatsKVOperation.Del)
deleted.Add(entry);
if (entry.Delta == 0)
goto PURGE_LOOP_DONE;
}
}
PURGE_LOOP_DONE:
foreach (var entry in deleted)
{
var request = new StreamPurgeRequest { Filter = $"$KV.{Bucket}.{entry.Key}" };
if (timeLimited && entry.Created > limit)
{
request.Keep = 1;
}
var response = await _stream.PurgeAsync(request, cancellationToken);
if (!response.Success)
{
throw new NatsKVException("Purge failed");
}
}
}
/// <inheritdoc />
public IAsyncEnumerable<string> GetKeysAsync(NatsKVWatchOpts? opts = default, CancellationToken cancellationToken = default)
=> GetKeysAsync([">"], opts, cancellationToken);
/// <inheritdoc />
public async IAsyncEnumerable<string> GetKeysAsync(IEnumerable<string> filters, NatsKVWatchOpts? opts = default, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
opts ??= NatsKVWatchOpts.Default;
opts = opts with { IgnoreDeletes = false, MetaOnly = true, UpdatesOnly = false, };
// Type doesn't matter here, we're just using the watcher to get the keys
await using var watcher = await WatchInternalAsync<int>(filters, serializer: default, opts, cancellationToken);
if (watcher.InitialConsumer.Info.NumPending == 0)
yield break;
await foreach (var entry in watcher.Entries.ReadAllAsync(cancellationToken).ConfigureAwait(false))
{
if (entry.Operation is NatsKVOperation.Put)
yield return entry.Key;
if (entry.Delta == 0)
yield break;
}
}
internal async ValueTask<NatsKVWatcher<T>> WatchInternalAsync<T>(IEnumerable<string> keys, INatsDeserialize<T>? serializer = default, NatsKVWatchOpts? opts = default, CancellationToken cancellationToken = default)
{
opts ??= NatsKVWatchOpts.Default;
serializer ??= JetStreamContext.Connection.Opts.SerializerRegistry.GetDeserializer<T>();
opts.ThrowIfInvalid();
var watcher = new NatsKVWatcher<T>(
context: JetStreamContext,
bucket: Bucket,
keys: keys,
opts: opts,
serializer: serializer,
subOpts: default,
cancellationToken: cancellationToken);
await watcher.InitAsync();
return watcher;
}
/// <summary>
/// Valid keys are \A[-/_=\.a-zA-Z0-9]+\z, additionally they may not start or end in .
/// </summary>
private static void ValidateKey(string key)
{
if (string.IsNullOrWhiteSpace(key) || key.Length == 0)
{
ThrowNatsKVException("Key cannot be empty");
}
if (key[0] == '.' || key[^1] == '.')
{
ThrowNatsKVException("Key cannot start or end with a period");
}
if (!ValidKeyRegex.IsMatch(key))
{
ThrowNatsKVException("Key contains invalid characters");
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowNatsKVException(string message) => throw new NatsKVException(message);
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowException(Exception exception) => throw exception;
}
public record NatsKVStatus(string Bucket, bool IsCompressed, StreamInfo Info);