-
-
Notifications
You must be signed in to change notification settings - Fork 54
Support FlushAsync in Producer #334
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ type internal ProducerMessage<'T> = | |
| | Close of TaskCompletionSource<ResultOrException<unit>> | ||
| | Tick of ProducerTickType | ||
| | GetStats of TaskCompletionSource<ProducerStats> | ||
| | Flush of TaskCompletionSource<unit> | ||
|
|
||
| type internal ProducerImpl<'T> private (producerConfig: ProducerConfiguration, clientConfig: PulsarClientConfiguration, connectionPool: ConnectionPool, | ||
| partitionIndex: int, lookup: ILookupService, schema: ISchema<'T>, | ||
|
|
@@ -754,6 +755,42 @@ type internal ProducerImpl<'T> private (producerConfig: ProducerConfiguration, c | |
| | ProducerMessage.GetStats channel -> | ||
| channel.SetResult <| stats.GetStats() | ||
|
|
||
| | ProducerMessage.Flush channel -> | ||
| Log.Logger.LogDebug("{0} Flush requested, pendingMessages count: {1}", prefix, pendingMessages.Count) | ||
| // First, send all batched messages | ||
| batchMessageAndSend() | ||
|
|
||
| // If pendingMessages queue is empty, return immediately | ||
| if pendingMessages.Count = 0 then | ||
| Log.Logger.LogDebug("{0} Flush completed immediately, no pending messages", prefix) | ||
| channel.SetResult() | ||
| else | ||
| // Get the last element from the queue by iterating through it | ||
| let mutable lastMessage = Unchecked.defaultof<PendingMessage<'T>> | ||
| for msg in pendingMessages do | ||
|
||
| lastMessage <- msg | ||
|
|
||
| Log.Logger.LogDebug("{0} Flush waiting for last message callback, sequenceId: {1}", prefix, %lastMessage.SequenceId) | ||
| // Wait for the last message's callback to complete asynchronously | ||
| backgroundTask { | ||
| match lastMessage.Callback with | ||
| | SingleCallback (_, _, tcsOption) -> | ||
| match tcsOption with | ||
| | Some tcs -> | ||
| let! _ = tcs.Task | ||
RobertIndie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| () | ||
| | None -> () | ||
| | BatchCallbacks batchCallbacks -> | ||
| // Wait for all TaskCompletionSource in the batch | ||
| let tasks = | ||
| batchCallbacks | ||
| |> Array.choose (fun struct(_, _, tcsOption) -> | ||
| tcsOption |> Option.map (fun tcs -> tcs.Task :> Task)) | ||
| if tasks.Length > 0 then | ||
| do! Task.WhenAll(tasks) | ||
RobertIndie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| channel.SetResult() | ||
RobertIndie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } |> ignore | ||
|
|
||
| | ProducerMessage.Close channel -> | ||
|
|
||
| match connectionHandler.ConnectionState with | ||
|
|
@@ -941,6 +978,10 @@ type internal ProducerImpl<'T> private (producerConfig: ProducerConfiguration, c | |
| | Ready _ -> trueTask | ||
| | _ -> falseTask | ||
|
|
||
| member this.FlushAsync() = | ||
| connectionHandler.CheckIfActive() |> throwIfNotNull | ||
| postAndAsyncReply mb (fun channel -> ProducerMessage.Flush channel) | ||
RobertIndie marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| interface IAsyncDisposable with | ||
|
|
||
| member this.DisposeAsync() = | ||
|
|
||
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,116 @@ | ||
| module Pulsar.Client.IntegrationTests.Flush | ||
|
|
||
| open System | ||
| open System.Collections.Generic | ||
| open System.Text | ||
| open System.Threading | ||
| open Expecto | ||
| open Pulsar.Client.Api | ||
| open Pulsar.Client.Common | ||
| open Serilog | ||
| open Pulsar.Client.IntegrationTests.Common | ||
|
|
||
|
|
||
| let testMessageOrderAndDuplicates (messageSet: HashSet<string>) (receivedMessage: string) (expectedMessage: string) = | ||
| if messageSet.Contains(receivedMessage) then | ||
| failwith $"Duplicate message received: {receivedMessage}" | ||
| messageSet.Add(receivedMessage) |> ignore | ||
| if receivedMessage <> expectedMessage then | ||
| failwith $"Incorrect message order. Expected: {expectedMessage}, Received: {receivedMessage}" | ||
|
|
||
| [<Tests>] | ||
| let tests = | ||
|
|
||
| testList "Flush" [ | ||
|
|
||
| testTask "Flush with batch enabled" { | ||
| Log.Debug("Started Flush with batch enabled") | ||
| let client = getClient() | ||
| let topicName = "persistent://public/default/test-flush-batch-enabled-" + Guid.NewGuid().ToString("N") | ||
|
|
||
| let! (consumer : IConsumer<byte[]>) = | ||
| client.NewConsumer() | ||
| .Topic(topicName) | ||
| .SubscriptionName("my-subscriber-name") | ||
| .SubscribeAsync() | ||
|
|
||
| let! (producer : IProducer<byte[]>) = | ||
| client.NewProducer() | ||
| .Topic(topicName) | ||
| .EnableBatching(true) | ||
| .BatchingMaxPublishDelay(TimeSpan.FromHours(1.0)) | ||
| .BatchingMaxMessages(10000) | ||
| .CreateAsync() | ||
|
|
||
| // Send 10 messages asynchronously without waiting | ||
| for i in 0..9 do | ||
| let message = $"my-message-{i}" | ||
| producer.SendAsync(Encoding.UTF8.GetBytes(message)) |> ignore | ||
|
|
||
| // Flush to ensure all messages are sent and acknowledged | ||
| do! producer.FlushAsync() | ||
|
|
||
| // Dispose producer | ||
| do! (producer :> IAsyncDisposable).DisposeAsync().AsTask() | ||
|
|
||
| // Receive and verify messages | ||
| let messageSet = HashSet<string>() | ||
| let cts = new CancellationTokenSource(TimeSpan.FromSeconds(5.0)) | ||
|
|
||
| for i in 0..9 do | ||
| let! (msg : Message<byte[]>) = consumer.ReceiveAsync(cts.Token) | ||
| let receivedMessage = Encoding.UTF8.GetString(msg.GetValue()) | ||
| Log.Debug("Received message: [{0}]", receivedMessage) | ||
| let expectedMessage = $"my-message-{i}" | ||
| testMessageOrderAndDuplicates messageSet receivedMessage expectedMessage | ||
|
|
||
| do! (consumer :> IAsyncDisposable).DisposeAsync().AsTask() | ||
|
|
||
| Log.Debug("Finished Started Flush with batch enabled") | ||
| } | ||
|
|
||
| testTask "Flush with batch disabled" { | ||
| Log.Debug("Started Flush with batch disabled") | ||
| let client = getClient() | ||
| let topicName = "persistent://public/default/test-flush-batch-disabled-" + Guid.NewGuid().ToString("N") | ||
|
|
||
| let! (consumer : IConsumer<byte[]>) = | ||
| client.NewConsumer() | ||
| .Topic(topicName) | ||
| .SubscriptionName("my-subscriber-name") | ||
| .SubscribeAsync() | ||
|
|
||
| let! (producer : IProducer<byte[]>) = | ||
| client.NewProducer() | ||
| .Topic(topicName) | ||
| .EnableBatching(false) | ||
| .CreateAsync() | ||
|
|
||
| // Send 10 messages asynchronously without waiting | ||
| for i in 0..9 do | ||
| let message = $"my-message-{i}" | ||
| producer.SendAsync(Encoding.UTF8.GetBytes(message)) |> ignore | ||
|
|
||
| // Flush to ensure all messages are sent and acknowledged | ||
| do! producer.FlushAsync() | ||
|
|
||
| // Dispose producer | ||
| do! (producer :> IAsyncDisposable).DisposeAsync().AsTask() | ||
|
|
||
| // Receive and verify messages | ||
| let messageSet = HashSet<string>() | ||
| let cts = new CancellationTokenSource(TimeSpan.FromSeconds(5.0)) | ||
|
|
||
| for i in 0..9 do | ||
| let! (msg : Message<byte[]>) = consumer.ReceiveAsync(cts.Token) | ||
| let receivedMessage = Encoding.UTF8.GetString(msg.GetValue()) | ||
| Log.Debug("Received message: [{0}]", receivedMessage) | ||
| let expectedMessage = $"my-message-{i}" | ||
| testMessageOrderAndDuplicates messageSet receivedMessage expectedMessage | ||
|
|
||
| do! (consumer :> IAsyncDisposable).DisposeAsync().AsTask() | ||
|
|
||
| Log.Debug("Finished Flush with batch disabled") | ||
| } | ||
| ] | ||
|
|
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.