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