| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | namespace AsyncResponse.Channels.MongoDB; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// MongoDB implementation of <see cref="IRecoveryStateStore"/> and |
| | | 9 | | /// <see cref="IRecoveryStateScanner"/>. Entries live in a TTL-indexed collection, so MongoDB itself |
| | | 10 | | /// reaps expired registrations. |
| | | 11 | | /// </summary> |
| | 3 | 12 | | internal sealed class MongoDbRecoveryStateStore( |
| | 3 | 13 | | MongoDbChannelStore _store, |
| | 3 | 14 | | ILogger<MongoDbRecoveryStateStore> _logger) : IRecoveryStateStore, IRecoveryStateScanner |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public async Task SaveAsync(string correlationId, RecoveryState state, TimeSpan ttl, CancellationToken cancellationT |
| | | 18 | | { |
| | 3 | 19 | | ArgumentException.ThrowIfNullOrWhiteSpace(correlationId); |
| | 3 | 20 | | ArgumentNullException.ThrowIfNull(state); |
| | 3 | 21 | | if (ttl <= TimeSpan.Zero) |
| | 3 | 22 | | throw new ArgumentOutOfRangeException(nameof(ttl), "TTL must be greater than zero."); |
| | 3 | 23 | | if (!string.Equals(state.CorrelationId, correlationId, StringComparison.Ordinal)) |
| | 3 | 24 | | throw new ArgumentException("The recovery-state correlation id must match the store key.", nameof(state)); |
| | 3 | 25 | | if (state.SchemaVersion != RecoveryStateSchema.Current) |
| | 3 | 26 | | throw new ArgumentException("The recovery state must use the current schema version.", nameof(state)); |
| | | 27 | | |
| | 1 | 28 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 1 | 29 | | if (state.RegistrationId == Guid.Empty) |
| | 1 | 30 | | state.RegistrationId = Guid.NewGuid(); |
| | | 31 | | |
| | 1 | 32 | | await _store.SaveRecoveryStateAsync(correlationId, state, ttl, cancellationToken).ConfigureAwait(false); |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public async Task<IReadOnlyList<RecoveryState>> GetAllAsync(string correlationId, CancellationToken cancellationToke |
| | | 37 | | { |
| | 1 | 38 | | ArgumentException.ThrowIfNullOrWhiteSpace(correlationId); |
| | 1 | 39 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 40 | | |
| | 1 | 41 | | var jsonStates = await _store.LoadRecoveryStatesAsync(correlationId, cancellationToken).ConfigureAwait(false); |
| | 1 | 42 | | return DeserializeStates(jsonStates, correlationId); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public Task<bool> TryDeleteAsync(string correlationId, Guid registrationId, CancellationToken cancellationToken = de |
| | | 47 | | { |
| | 3 | 48 | | ArgumentException.ThrowIfNullOrWhiteSpace(correlationId); |
| | 3 | 49 | | if (registrationId == Guid.Empty) |
| | 3 | 50 | | throw new ArgumentException("Registration id cannot be empty.", nameof(registrationId)); |
| | 1 | 51 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 1 | 52 | | return _store.DeleteRecoveryStateAsync(correlationId, registrationId, cancellationToken); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public async IAsyncEnumerable<RecoveryState> ScanAsync([EnumeratorCancellation] CancellationToken cancellationToken |
| | | 57 | | { |
| | 1 | 58 | | await foreach (var json in _store.ScanRecoveryStateJsonAsync(cancellationToken).ConfigureAwait(false)) |
| | | 59 | | { |
| | 1 | 60 | | var state = DeserializeState(json, correlationId: null); |
| | 1 | 61 | | if (state is not null) |
| | 1 | 62 | | yield return state; |
| | | 63 | | } |
| | 1 | 64 | | } |
| | | 65 | | |
| | | 66 | | private IReadOnlyList<RecoveryState> DeserializeStates(IReadOnlyList<string> jsonStates, string correlationId) |
| | | 67 | | { |
| | 1 | 68 | | if (jsonStates.Count == 0) |
| | 1 | 69 | | return []; |
| | | 70 | | |
| | 1 | 71 | | var states = new List<RecoveryState>(jsonStates.Count); |
| | 1 | 72 | | foreach (var json in jsonStates) |
| | | 73 | | { |
| | 1 | 74 | | var state = DeserializeState(json, correlationId); |
| | 1 | 75 | | if (state is not null) |
| | 1 | 76 | | states.Add(state); |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | return states; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | private RecoveryState? DeserializeState(string json, string? correlationId) |
| | | 83 | | { |
| | | 84 | | try |
| | | 85 | | { |
| | 3 | 86 | | var state = AsyncResponseJson.Deserialize<RecoveryState>(json); |
| | 3 | 87 | | if (state is null) |
| | 3 | 88 | | return null; |
| | | 89 | | |
| | 3 | 90 | | if (state.RegistrationId == Guid.Empty || string.IsNullOrWhiteSpace(state.CorrelationId)) |
| | | 91 | | { |
| | 3 | 92 | | _logger.LogWarning( |
| | 3 | 93 | | "MongoDB recovery state for correlationId {CorrelationId} has an incomplete identity; rejecting it." |
| | 3 | 94 | | correlationId ?? state.CorrelationId); |
| | 3 | 95 | | return null; |
| | | 96 | | } |
| | | 97 | | |
| | 3 | 98 | | if (!RecoveryStateSchema.IsReadable(state.SchemaVersion)) |
| | | 99 | | { |
| | 1 | 100 | | _logger.LogWarning( |
| | 1 | 101 | | "MongoDB recovery state for correlationId {CorrelationId} has unsupported schema version {SchemaVers |
| | 1 | 102 | | correlationId ?? state.CorrelationId, |
| | 1 | 103 | | state.SchemaVersion, |
| | 1 | 104 | | RecoveryStateSchema.Current); |
| | 1 | 105 | | return null; |
| | | 106 | | } |
| | | 107 | | |
| | 3 | 108 | | if (!string.IsNullOrWhiteSpace(correlationId) |
| | 3 | 109 | | && !string.Equals(state.CorrelationId, correlationId, StringComparison.Ordinal)) |
| | | 110 | | { |
| | 3 | 111 | | _logger.LogWarning( |
| | 3 | 112 | | "MongoDB recovery state has correlationId {StoredCorrelationId}, expected {CorrelationId}; rejecting |
| | 3 | 113 | | state.CorrelationId, correlationId); |
| | 3 | 114 | | return null; |
| | | 115 | | } |
| | | 116 | | |
| | 3 | 117 | | return state; |
| | | 118 | | } |
| | 3 | 119 | | catch (JsonException ex) |
| | | 120 | | { |
| | 3 | 121 | | _logger.LogWarning(ex, "Unreadable MongoDB recovery state for correlationId {CorrelationId}; skipping.", cor |
| | 3 | 122 | | return null; |
| | | 123 | | } |
| | 3 | 124 | | } |
| | | 125 | | } |