-
Notifications
You must be signed in to change notification settings - Fork 42
Reliable Producer #104
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
Reliable Producer #104
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
086cd0c
Work in progress
Gsantomaggio ec5d193
First implementation
Gsantomaggio 860cb4a
Merge branch 'main' into rproducer
Gsantomaggio 1f8942b
First implementation
Gsantomaggio 032a196
Add tests
Gsantomaggio 121b66b
Formatting
Gsantomaggio c1f6593
Approve
Gsantomaggio 9503e10
Logs
Gsantomaggio 9e017f3
Handle metadata update
Gsantomaggio 014a509
Approve
Gsantomaggio 35b082e
Be sure that the publisher is removed even if there is an error
Gsantomaggio 9853fce
Be sure that the publisher is removed even if there is an error
Gsantomaggio 66eef94
Add the last sequence
Gsantomaggio 3582bb4
Formatting
Gsantomaggio 8112a0c
Verified
Gsantomaggio bfade22
Resolve dead lock during the reconnection
Gsantomaggio f3c961e
Rename to ReliableProducer
Gsantomaggio ec87443
Change InitPublishingId
Gsantomaggio 82b51cd
Rename file
lukebakken a18af21
Update APIApproval
lukebakken 906bc1c
Remove commented-out code
lukebakken 54674f4
Remove the class and use onlu ulong
Gsantomaggio 31b930d
Merge branch 'rproducer' of github.com:rabbitmq/rabbitmq-stream-dotne…
Gsantomaggio 5af24bb
Merge branch 'main' into rproducer
lukebakken 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
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,127 @@ | ||
// This source code is dual-licensed under the Apache License, version | ||
// 2.0, and the Mozilla Public License, version 2.0. | ||
// Copyright (c) 2007-2020 VMware, Inc. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Threading.Tasks.Dataflow; | ||
using System.Timers; | ||
using Timer = System.Timers.Timer; | ||
|
||
namespace RabbitMQ.Stream.Client.Reliable; | ||
|
||
/// <summary> | ||
/// ConfirmationStatus can be: | ||
/// </summary> | ||
public enum ConfirmationStatus : ushort | ||
{ | ||
WaitForConfirmation = 0, | ||
Confirmed = 1, | ||
TimeoutError = 2, | ||
StreamNotAvailable = 6, | ||
InternalError = 15, | ||
AccessRefused = 16, | ||
PreconditionFailed = 17, | ||
PublisherDoesNotExist = 18, | ||
UndefinedError = 200, | ||
} | ||
|
||
/// <summary> | ||
/// MessagesConfirmation is a wrapper around the message/s | ||
/// This class is returned to the user to understand | ||
/// the message status. | ||
/// </summary> | ||
public class MessagesConfirmation | ||
{ | ||
public ulong PublishingId { get; internal set; } | ||
public List<Message> Messages { get; internal init; } | ||
public DateTime InsertDateTime { get; init; } | ||
public ConfirmationStatus Status { get; internal set; } | ||
} | ||
|
||
/// <summary> | ||
/// ConfirmationPipe maintains the status for the sent and received messages. | ||
/// TPL Action block sends the confirmation to the user in async way | ||
/// So the send/1 is not blocking. | ||
/// </summary> | ||
public class ConfirmationPipe | ||
{ | ||
private ActionBlock<Tuple<ConfirmationStatus, ulong>> _waitForConfirmationActionBlock; | ||
private readonly ConcurrentDictionary<ulong, MessagesConfirmation> _waitForConfirmation = new(); | ||
private readonly Timer _invalidateTimer = new(); | ||
private Func<MessagesConfirmation, Task> ConfirmHandler { get; } | ||
|
||
public ConfirmationPipe(Func<MessagesConfirmation, Task> confirmHandler) | ||
{ | ||
ConfirmHandler = confirmHandler; | ||
} | ||
|
||
public void Start() | ||
{ | ||
_waitForConfirmationActionBlock = new ActionBlock<Tuple<ConfirmationStatus, ulong>>( | ||
request => | ||
{ | ||
var (confirmationStatus, publishingId) = request; | ||
|
||
_waitForConfirmation.TryRemove(publishingId, out var message); | ||
if (message == null) | ||
{ | ||
return; | ||
} | ||
|
||
message.Status = confirmationStatus; | ||
ConfirmHandler?.Invoke(message); | ||
}, new ExecutionDataflowBlockOptions | ||
{ | ||
MaxDegreeOfParallelism = 1, | ||
// throttling | ||
BoundedCapacity = 50_000 | ||
}); | ||
|
||
_invalidateTimer.Elapsed += OnTimedEvent; | ||
_invalidateTimer.Interval = 2000; | ||
_invalidateTimer.Enabled = true; | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_invalidateTimer.Enabled = false; | ||
_waitForConfirmationActionBlock.Complete(); | ||
} | ||
|
||
private async void OnTimedEvent(object? sender, ElapsedEventArgs e) | ||
{ | ||
{ | ||
foreach (var pair in _waitForConfirmation.Where(pair => | ||
(DateTime.Now - pair.Value.InsertDateTime).Seconds > 2)) | ||
{ | ||
await RemoveUnConfirmedMessage(pair.Value.PublishingId, ConfirmationStatus.TimeoutError); | ||
} | ||
} | ||
} | ||
|
||
public void AddUnConfirmedMessage(ulong publishingId, Message message) | ||
{ | ||
AddUnConfirmedMessage(publishingId, new List<Message>() { message }); | ||
} | ||
|
||
public void AddUnConfirmedMessage(ulong publishingId, List<Message> messages) | ||
{ | ||
_waitForConfirmation.TryAdd(publishingId, | ||
new MessagesConfirmation() | ||
{ | ||
Messages = messages, | ||
PublishingId = publishingId, | ||
InsertDateTime = DateTime.Now | ||
}); | ||
} | ||
|
||
public Task RemoveUnConfirmedMessage(ulong publishingId, ConfirmationStatus confirmationStatus) | ||
{ | ||
return _waitForConfirmationActionBlock.SendAsync( | ||
Tuple.Create(confirmationStatus, publishingId)); | ||
} | ||
} |
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,11 @@ | ||
// This source code is dual-licensed under the Apache License, version | ||
// 2.0, and the Mozilla Public License, version 2.0. | ||
// Copyright (c) 2007-2020 VMware, Inc. | ||
|
||
namespace RabbitMQ.Stream.Client.Reliable; | ||
|
||
public interface IReconnectStrategy | ||
{ | ||
void WhenDisconnected(out bool reconnect); | ||
void WhenConnected(); | ||
} |
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,18 @@ | ||
// This source code is dual-licensed under the Apache License, version | ||
// 2.0, and the Mozilla Public License, version 2.0. | ||
// Copyright (c) 2007-2020 VMware, Inc. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace RabbitMQ.Stream.Client.Reliable; | ||
|
||
/// <summary> | ||
/// Define PublishingId Strategy. | ||
/// Can be automatic, so the ReliableProducer will provide | ||
/// the PublishingId | ||
/// </summary> | ||
public interface IPublishingIdStrategy | ||
{ | ||
ulong GetPublishingId(); | ||
Task InitPublishingId(); | ||
} |
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.