| | | 1 | | using System.Diagnostics.Metrics; |
| | | 2 | | using AsiBackbone.Core.Emissions; |
| | | 3 | | using AsiBackbone.Core.Outbox; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Logging.Abstractions; |
| | | 7 | | |
| | | 8 | | namespace AsiBackbone.EntityFrameworkCore.Outbox; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// EF Core governance outbox store that exposes explicit caller-owned claim transition outcomes. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// This store preserves the existing convenience API while adding an outcome-aware contract that distinguishes applied |
| | | 15 | | /// transitions from stale claims, terminal no-ops, concurrency losses, and missing rows. It delegates persistence to |
| | | 16 | | /// <see cref="EfCoreGovernanceOutboxStore" /> and observes the scoped <see cref="DbContext" /> save boundary so callers |
| | | 17 | | /// infer write ownership from the returned durable status alone. |
| | | 18 | | /// </remarks> |
| | | 19 | | public sealed class EfCoreGovernanceOutboxOutcomeStore : IAsiBackboneGovernanceOutboxClaimOutcomeStore |
| | | 20 | | { |
| | 2 | 21 | | private static readonly Meter Meter = new("AsiBackbone.EntityFrameworkCore.Outbox", "1.0.0"); |
| | 2 | 22 | | private static readonly Counter<long> ClaimTransitionCounter = Meter.CreateCounter<long>( |
| | 2 | 23 | | "asibackbone.outbox.claim_transition_attempts", |
| | 2 | 24 | | description: "Counts claimed outbox transition attempts by caller-visible outcome."); |
| | | 25 | | |
| | 2 | 26 | | private static readonly Action<ILogger, string, string, string, string, Exception?> LogClaimTransitionNotApplied = |
| | 2 | 27 | | LoggerMessage.Define<string, string, string, string>( |
| | 2 | 28 | | LogLevel.Warning, |
| | 2 | 29 | | new EventId(19801, nameof(LogClaimTransitionNotApplied)), |
| | 2 | 30 | | "Claimed governance outbox transition was not applied by worker {WorkerId} for entry {OutboxEntryId}. Outcom |
| | | 31 | | |
| | | 32 | | private readonly DbContext dbContext; |
| | | 33 | | private readonly EfCoreGovernanceOutboxStore innerStore; |
| | | 34 | | private readonly ILogger<EfCoreGovernanceOutboxOutcomeStore> logger; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="EfCoreGovernanceOutboxOutcomeStore" /> class. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="dbContext">The host-owned scoped database context.</param> |
| | | 40 | | /// <param name="logger">The logger used for non-applied claimed-transition diagnostics.</param> |
| | 14 | 41 | | public EfCoreGovernanceOutboxOutcomeStore( |
| | 14 | 42 | | DbContext dbContext, |
| | 14 | 43 | | ILogger<EfCoreGovernanceOutboxOutcomeStore>? logger = null) |
| | | 44 | | { |
| | 14 | 45 | | ArgumentNullException.ThrowIfNull(dbContext); |
| | | 46 | | |
| | 14 | 47 | | this.dbContext = dbContext; |
| | 14 | 48 | | innerStore = new EfCoreGovernanceOutboxStore(dbContext); |
| | 14 | 49 | | this.logger = logger ?? NullLogger<EfCoreGovernanceOutboxOutcomeStore>.Instance; |
| | 14 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public ValueTask<GovernanceOutboxEntry> EnqueueAsync( |
| | | 54 | | GovernanceEmissionEnvelope envelope, |
| | | 55 | | CancellationToken cancellationToken = default) |
| | | 56 | | { |
| | 0 | 57 | | return innerStore.EnqueueAsync(envelope, cancellationToken); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public ValueTask<GovernanceOutboxEntry> SaveAsync( |
| | | 62 | | GovernanceOutboxEntry entry, |
| | | 63 | | CancellationToken cancellationToken = default) |
| | | 64 | | { |
| | 12 | 65 | | return innerStore.SaveAsync(entry, cancellationToken); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | public ValueTask<GovernanceOutboxEntry?> FindByOutboxEntryIdAsync( |
| | | 70 | | string outboxEntryId, |
| | | 71 | | CancellationToken cancellationToken = default) |
| | | 72 | | { |
| | 0 | 73 | | return innerStore.FindByOutboxEntryIdAsync(outboxEntryId, cancellationToken); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public ValueTask<IReadOnlyList<GovernanceOutboxEntry>> FindPendingAsync( |
| | | 78 | | int maxCount = 100, |
| | | 79 | | CancellationToken cancellationToken = default) |
| | | 80 | | { |
| | 0 | 81 | | return innerStore.FindPendingAsync(maxCount, cancellationToken); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc /> |
| | | 85 | | public ValueTask<IReadOnlyList<GovernanceOutboxEntry>> FindRetryReadyAsync( |
| | | 86 | | DateTimeOffset utcNow, |
| | | 87 | | int maxCount = 100, |
| | | 88 | | CancellationToken cancellationToken = default) |
| | | 89 | | { |
| | 0 | 90 | | return innerStore.FindRetryReadyAsync(utcNow, maxCount, cancellationToken); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc /> |
| | | 94 | | public ValueTask<IReadOnlyList<GovernanceOutboxClaim>> ClaimPendingAsync( |
| | | 95 | | GovernanceOutboxClaimRequest request, |
| | | 96 | | CancellationToken cancellationToken = default) |
| | | 97 | | { |
| | 12 | 98 | | return innerStore.ClaimPendingAsync(request, cancellationToken); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <inheritdoc /> |
| | | 102 | | public ValueTask<IReadOnlyList<GovernanceOutboxClaim>> ClaimRetryReadyAsync( |
| | | 103 | | GovernanceOutboxClaimRequest request, |
| | | 104 | | CancellationToken cancellationToken = default) |
| | | 105 | | { |
| | 0 | 106 | | return innerStore.ClaimRetryReadyAsync(request, cancellationToken); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <inheritdoc /> |
| | | 110 | | public async ValueTask<GovernanceOutboxEntry> MarkClaimDeliveredAsync( |
| | | 111 | | GovernanceOutboxClaim claim, |
| | | 112 | | GovernanceEmissionResult result, |
| | | 113 | | CancellationToken cancellationToken = default) |
| | | 114 | | { |
| | 0 | 115 | | return (await TryMarkClaimDeliveredAsync(claim, result, cancellationToken).ConfigureAwait(false)).Entry; |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | public async ValueTask<GovernanceOutboxEntry> MarkClaimFailedAsync( |
| | | 120 | | GovernanceOutboxClaim claim, |
| | | 121 | | GovernanceEmissionError governanceEmissionError, |
| | | 122 | | DateTimeOffset? nextRetryUtc = null, |
| | | 123 | | CancellationToken cancellationToken = default) |
| | | 124 | | { |
| | 0 | 125 | | return (await TryMarkClaimFailedAsync(claim, governanceEmissionError, nextRetryUtc, cancellationToken).Configure |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | | 129 | | public async ValueTask<GovernanceOutboxEntry> MarkClaimDeadLetteredAsync( |
| | | 130 | | GovernanceOutboxClaim claim, |
| | | 131 | | GovernanceEmissionError governanceEmissionError, |
| | | 132 | | string? deadLetterReason = null, |
| | | 133 | | CancellationToken cancellationToken = default) |
| | | 134 | | { |
| | 0 | 135 | | return (await TryMarkClaimDeadLetteredAsync( |
| | 0 | 136 | | claim, |
| | 0 | 137 | | governanceEmissionError, |
| | 0 | 138 | | deadLetterReason, |
| | 0 | 139 | | cancellationToken).ConfigureAwait(false)).Entry; |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <inheritdoc /> |
| | | 143 | | public async ValueTask<GovernanceOutboxEntry> SaveClaimAsync( |
| | | 144 | | GovernanceOutboxClaim claim, |
| | | 145 | | GovernanceOutboxEntry entry, |
| | | 146 | | CancellationToken cancellationToken = default) |
| | | 147 | | { |
| | 0 | 148 | | return (await TrySaveClaimAsync(claim, entry, cancellationToken).ConfigureAwait(false)).Entry; |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <inheritdoc /> |
| | | 152 | | public ValueTask<GovernanceOutboxEntry?> ReleaseClaimAsync( |
| | | 153 | | GovernanceOutboxClaim claim, |
| | | 154 | | string? reason = null, |
| | | 155 | | CancellationToken cancellationToken = default) |
| | | 156 | | { |
| | 2 | 157 | | return innerStore.ReleaseClaimAsync(claim, reason, cancellationToken); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <inheritdoc /> |
| | | 161 | | public ValueTask<GovernanceOutboxEntry> MarkDeliveredAsync( |
| | | 162 | | string outboxEntryId, |
| | | 163 | | GovernanceEmissionResult result, |
| | | 164 | | CancellationToken cancellationToken = default) |
| | | 165 | | { |
| | 2 | 166 | | return innerStore.MarkDeliveredAsync(outboxEntryId, result, cancellationToken); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <inheritdoc /> |
| | | 170 | | public ValueTask<GovernanceOutboxEntry> MarkFailedAsync( |
| | | 171 | | string outboxEntryId, |
| | | 172 | | GovernanceEmissionError governanceEmissionError, |
| | | 173 | | DateTimeOffset? nextRetryUtc = null, |
| | | 174 | | CancellationToken cancellationToken = default) |
| | | 175 | | { |
| | 0 | 176 | | return innerStore.MarkFailedAsync(outboxEntryId, governanceEmissionError, nextRetryUtc, cancellationToken); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <inheritdoc /> |
| | | 180 | | public ValueTask<GovernanceOutboxEntry> MarkDeadLetteredAsync( |
| | | 181 | | string outboxEntryId, |
| | | 182 | | GovernanceEmissionError governanceEmissionError, |
| | | 183 | | string? deadLetterReason = null, |
| | | 184 | | CancellationToken cancellationToken = default) |
| | | 185 | | { |
| | 0 | 186 | | return innerStore.MarkDeadLetteredAsync(outboxEntryId, governanceEmissionError, deadLetterReason, cancellationTo |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <inheritdoc /> |
| | | 190 | | public ValueTask<GovernanceOutboxClaimTransitionResult> TryMarkClaimDeliveredAsync( |
| | | 191 | | GovernanceOutboxClaim claim, |
| | | 192 | | GovernanceEmissionResult result, |
| | | 193 | | CancellationToken cancellationToken = default) |
| | | 194 | | { |
| | 8 | 195 | | ArgumentNullException.ThrowIfNull(result); |
| | 8 | 196 | | return TryUpdateClaimAsync( |
| | 8 | 197 | | claim, |
| | 6 | 198 | | () => innerStore.MarkClaimDeliveredAsync(claim, result, cancellationToken), |
| | 8 | 199 | | cancellationToken); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | /// <inheritdoc /> |
| | | 203 | | public ValueTask<GovernanceOutboxClaimTransitionResult> TryMarkClaimFailedAsync( |
| | | 204 | | GovernanceOutboxClaim claim, |
| | | 205 | | GovernanceEmissionError governanceEmissionError, |
| | | 206 | | DateTimeOffset? nextRetryUtc = null, |
| | | 207 | | CancellationToken cancellationToken = default) |
| | | 208 | | { |
| | 4 | 209 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | 4 | 210 | | return TryUpdateClaimAsync( |
| | 4 | 211 | | claim, |
| | 2 | 212 | | () => innerStore.MarkClaimFailedAsync(claim, governanceEmissionError, nextRetryUtc, cancellationToken), |
| | 4 | 213 | | cancellationToken); |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | /// <inheritdoc /> |
| | | 217 | | public ValueTask<GovernanceOutboxClaimTransitionResult> TryMarkClaimDeadLetteredAsync( |
| | | 218 | | GovernanceOutboxClaim claim, |
| | | 219 | | GovernanceEmissionError governanceEmissionError, |
| | | 220 | | string? deadLetterReason = null, |
| | | 221 | | CancellationToken cancellationToken = default) |
| | | 222 | | { |
| | 0 | 223 | | ArgumentNullException.ThrowIfNull(governanceEmissionError); |
| | 0 | 224 | | return TryUpdateClaimAsync( |
| | 0 | 225 | | claim, |
| | 0 | 226 | | () => innerStore.MarkClaimDeadLetteredAsync(claim, governanceEmissionError, deadLetterReason, cancellationTo |
| | 0 | 227 | | cancellationToken); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <inheritdoc /> |
| | | 231 | | public ValueTask<GovernanceOutboxClaimTransitionResult> TrySaveClaimAsync( |
| | | 232 | | GovernanceOutboxClaim claim, |
| | | 233 | | GovernanceOutboxEntry entry, |
| | | 234 | | CancellationToken cancellationToken = default) |
| | | 235 | | { |
| | 0 | 236 | | ArgumentNullException.ThrowIfNull(entry); |
| | | 237 | | |
| | 0 | 238 | | return !string.Equals(claim.OutboxEntryId, entry.OutboxEntryId, StringComparison.Ordinal) |
| | 0 | 239 | | ? throw new ArgumentException("Claim and entry must reference the same outbox entry ID.", nameof(entry)) |
| | 0 | 240 | | : TryUpdateClaimAsync( |
| | 0 | 241 | | claim, |
| | 0 | 242 | | () => innerStore.SaveClaimAsync(claim, entry, cancellationToken), |
| | 0 | 243 | | cancellationToken); |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | private async ValueTask<GovernanceOutboxClaimTransitionResult> TryUpdateClaimAsync( |
| | | 247 | | GovernanceOutboxClaim claim, |
| | | 248 | | Func<ValueTask<GovernanceOutboxEntry>> update, |
| | | 249 | | CancellationToken cancellationToken) |
| | | 250 | | { |
| | 12 | 251 | | ArgumentNullException.ThrowIfNull(claim); |
| | 12 | 252 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 253 | | |
| | 12 | 254 | | GovernanceOutboxEntry? currentEntry = await innerStore |
| | 12 | 255 | | .FindByOutboxEntryIdAsync(claim.OutboxEntryId, cancellationToken) |
| | 12 | 256 | | .ConfigureAwait(false); |
| | | 257 | | |
| | 12 | 258 | | if (currentEntry is null) |
| | | 259 | | { |
| | 0 | 260 | | return Complete(claim, claim.Entry, GovernanceOutboxClaimTransitionOutcome.Missing); |
| | | 261 | | } |
| | | 262 | | |
| | 12 | 263 | | if (currentEntry.IsDelivered || currentEntry.IsDeadLettered) |
| | | 264 | | { |
| | 2 | 265 | | return Complete(claim, currentEntry, GovernanceOutboxClaimTransitionOutcome.Terminal); |
| | | 266 | | } |
| | | 267 | | |
| | 10 | 268 | | if (!currentEntry.IsClaimedBy(claim)) |
| | | 269 | | { |
| | 2 | 270 | | return Complete(claim, currentEntry, GovernanceOutboxClaimTransitionOutcome.StaleClaim); |
| | | 271 | | } |
| | | 272 | | |
| | 8 | 273 | | bool saveCompleted = false; |
| | | 274 | | void savedChangesHandler(object? sender, SavedChangesEventArgs eventArgs) |
| | | 275 | | { |
| | | 276 | | _ = sender; |
| | | 277 | | _ = eventArgs; |
| | 2 | 278 | | saveCompleted = true; |
| | 2 | 279 | | } |
| | | 280 | | |
| | 8 | 281 | | dbContext.SavedChanges += savedChangesHandler; |
| | | 282 | | |
| | | 283 | | GovernanceOutboxEntry returnedEntry; |
| | | 284 | | try |
| | | 285 | | { |
| | 8 | 286 | | returnedEntry = await update().ConfigureAwait(false); |
| | 8 | 287 | | } |
| | 0 | 288 | | catch (InvalidOperationException) |
| | | 289 | | { |
| | 0 | 290 | | GovernanceOutboxEntry? missingEntry = await innerStore |
| | 0 | 291 | | .FindByOutboxEntryIdAsync(claim.OutboxEntryId, cancellationToken) |
| | 0 | 292 | | .ConfigureAwait(false); |
| | | 293 | | |
| | 0 | 294 | | if (missingEntry is null) |
| | | 295 | | { |
| | 0 | 296 | | return Complete(claim, currentEntry, GovernanceOutboxClaimTransitionOutcome.Missing); |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | throw; |
| | | 300 | | } |
| | | 301 | | finally |
| | | 302 | | { |
| | 8 | 303 | | dbContext.SavedChanges -= savedChangesHandler; |
| | | 304 | | } |
| | | 305 | | |
| | 8 | 306 | | if (saveCompleted) |
| | | 307 | | { |
| | 2 | 308 | | return Complete(claim, returnedEntry, GovernanceOutboxClaimTransitionOutcome.Applied); |
| | | 309 | | } |
| | | 310 | | |
| | 6 | 311 | | GovernanceOutboxEntry? refreshedEntry = await innerStore |
| | 6 | 312 | | .FindByOutboxEntryIdAsync(claim.OutboxEntryId, cancellationToken) |
| | 6 | 313 | | .ConfigureAwait(false); |
| | | 314 | | |
| | 6 | 315 | | return refreshedEntry is null |
| | 6 | 316 | | ? Complete(claim, currentEntry, GovernanceOutboxClaimTransitionOutcome.Missing) |
| | 6 | 317 | | : Complete(claim, refreshedEntry, GovernanceOutboxClaimTransitionOutcome.ConcurrencyLost); |
| | 12 | 318 | | } |
| | | 319 | | |
| | | 320 | | private GovernanceOutboxClaimTransitionResult Complete( |
| | | 321 | | GovernanceOutboxClaim claim, |
| | | 322 | | GovernanceOutboxEntry entry, |
| | | 323 | | GovernanceOutboxClaimTransitionOutcome outcome) |
| | | 324 | | { |
| | 12 | 325 | | ClaimTransitionCounter.Add( |
| | 12 | 326 | | 1, |
| | 12 | 327 | | new KeyValuePair<string, object?>("outcome", outcome.ToString()), |
| | 12 | 328 | | new KeyValuePair<string, object?>("durable_status", entry.Status.ToString())); |
| | | 329 | | |
| | 12 | 330 | | if (outcome is not GovernanceOutboxClaimTransitionOutcome.Applied) |
| | | 331 | | { |
| | 10 | 332 | | LogClaimTransitionNotApplied( |
| | 10 | 333 | | logger, |
| | 10 | 334 | | claim.WorkerId, |
| | 10 | 335 | | claim.OutboxEntryId, |
| | 10 | 336 | | outcome.ToString(), |
| | 10 | 337 | | entry.Status.ToString(), |
| | 10 | 338 | | null); |
| | | 339 | | } |
| | | 340 | | |
| | 12 | 341 | | return GovernanceOutboxClaimTransitionResult.Create(entry, outcome); |
| | | 342 | | } |
| | | 343 | | } |