-
-
Notifications
You must be signed in to change notification settings - Fork 1
Introduce mechanism to collect and retry completing successfully produced messages #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a1f70e
core implementation
joaofbantunes df23430
MongoDB implementation of ICompleteRetrier
joaofbantunes d400d6a
MySQL implementation of ICompleteRetrier
joaofbantunes 39c96ce
PostgreSQL implementation of ICompleteRetrier
joaofbantunes cc3079e
Small tweaks and naming adjustments
joaofbantunes 21fafd1
adding docs
joaofbantunes 7221ef1
cleaning up and simplifying names
joaofbantunes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,4 @@ outline: deep | |
|
|
||
| # Push | ||
|
|
||
| 🚧 coming soon | ||
| 🚧 coming soon (maybe 😂) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.Metrics; | ||
|
|
||
| namespace YakShaveFx.OutboxKit.Core.OpenTelemetry; | ||
|
|
||
| internal sealed class CompletionRetrierMetrics : IDisposable | ||
| { | ||
| private readonly Meter _meter; | ||
| private readonly Counter<long> _completionRetryAttemptsCounter; | ||
| private readonly Counter<long> _completionRetriedMessagesCounter; | ||
|
|
||
| public CompletionRetrierMetrics(IMeterFactory meterFactory) | ||
| { | ||
| _meter = meterFactory.Create(Constants.MeterName); | ||
|
|
||
| _completionRetryAttemptsCounter = _meter.CreateCounter<long>( | ||
| "outbox.completion_retry_attempts", | ||
| unit: "{attempt}", | ||
| description: "The number of attempts to retry completion of produced messages"); | ||
|
|
||
| _completionRetriedMessagesCounter = _meter.CreateCounter<long>( | ||
| "outbox.completion_retried_messages", | ||
| unit: "{message}", | ||
| description: "The number of messages for which completion was retried"); | ||
|
|
||
| } | ||
|
|
||
| public void CompletionRetryAttempted(OutboxKey key, int count) | ||
| { | ||
| if (_completionRetryAttemptsCounter.Enabled && count > 0) | ||
| { | ||
| var tags = new TagList | ||
| { | ||
| { "provider_key", key.ProviderKey }, | ||
| { "client_key", key.ClientKey } | ||
| }; | ||
| _completionRetryAttemptsCounter.Add(1, tags); | ||
| } | ||
|
|
||
| if (_completionRetriedMessagesCounter.Enabled && count > 0) | ||
| { | ||
| var tags = new TagList | ||
| { | ||
| { "provider_key", key.ProviderKey }, | ||
| { "client_key", key.ClientKey } | ||
| }; | ||
| _completionRetriedMessagesCounter.Add(count, tags); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() => _meter.Dispose(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| using System.Diagnostics; | ||
| using YakShaveFx.OutboxKit.Core.OpenTelemetry; | ||
|
|
||
| namespace YakShaveFx.OutboxKit.Core.Polling; | ||
|
|
||
| internal interface ICompletionRetryCollector | ||
| { | ||
| void Collect(IReadOnlyCollection<IMessage> messages); | ||
| } | ||
|
|
||
| internal interface ICompletionRetrier | ||
| { | ||
| ValueTask RetryAsync(CancellationToken ct); | ||
| } | ||
|
|
||
| // not thread safe, as it is only used in the context of a producing flow, which has no concurrency | ||
| internal sealed class CompletionRetrier( | ||
| OutboxKey key, | ||
| IBatchCompleteRetrier providerCompletionRetrier, | ||
| RetrierBuilderFactory retrierBuilderFactory, | ||
| CompletionRetrierMetrics metrics) | ||
| : ICompletionRetryCollector, ICompletionRetrier | ||
| { | ||
| private readonly Retrier _retrier = retrierBuilderFactory.Create() | ||
| .WithMaxRetries(int.MaxValue) | ||
| .WithShouldRetryDecider(ex => | ||
| { | ||
| // retry on all exceptions except cancellation | ||
| if (ex is OperationCanceledException oce) return oce.CancellationToken == CancellationToken.None; | ||
| return true; | ||
| }) | ||
| .Build(); | ||
|
|
||
| private List<IMessage> _messages = []; | ||
|
|
||
| public void Collect(IReadOnlyCollection<IMessage> messages) => _messages.AddRange(messages); | ||
|
|
||
| public ValueTask RetryAsync(CancellationToken ct) | ||
| => _messages.Count == 0 | ||
| ? ValueTask.CompletedTask | ||
| : new(InnerRetryCompleteAsync(ct)); | ||
|
|
||
| private async Task InnerRetryCompleteAsync(CancellationToken ct) | ||
| { | ||
| await _retrier.ExecuteWithRetryAsync( | ||
| async () => | ||
| { | ||
| metrics.CompletionRetryAttempted(key, _messages.Count); | ||
| using var activity = ActivityHelpers.StartActivity( | ||
| "retrying produced messages completion", | ||
| key, | ||
| [new(ActivityConstants.OutboxProducedMessagesToCompleteTag, _messages.Count)]); | ||
|
|
||
| try | ||
| { | ||
| await providerCompletionRetrier.RetryAsync(_messages, ct); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| activity?.SetStatus(ActivityStatusCode.Error); | ||
| throw; | ||
| } | ||
| }, | ||
| ct); | ||
|
|
||
| // since most of the time there are no messages to retry, we clear messages by creating a new list, | ||
| // so the old one can be garbage collected, avoiding the underlying array to be kept in memory | ||
| _messages = []; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| namespace YakShaveFx.OutboxKit.Core.Polling; | ||
|
|
||
| /// <summary> | ||
| /// Interface to be implemented by library users, to make it possible to retry completing messages already produced. | ||
| /// </summary> | ||
| public interface IBatchCompleteRetrier | ||
| { | ||
| /// <summary> | ||
| /// Retries completing the given collection of messages. | ||
| /// </summary> | ||
| /// <param name="messages">The messages that were previously successfully produced.</param> | ||
| /// <param name="ct">The async cancellation token.</param> | ||
| /// <returns>The task representing the asynchronous operation</returns> | ||
| Task RetryAsync(IReadOnlyCollection<IMessage> messages, CancellationToken ct); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.