Repository navigation
[.NET] Add an ack callback for streams - #1238
Draft
dimitrije-djordjevic-db wants to merge 12 commits into
Draft
dimitrije-djordjevic-db wants to merge 12 commits into
dimitrije-djordjevic-db wants to merge 12 commits into
Conversation
StreamConfigurationOptions.AckCallback (Beta) takes an IAckCallback that receives OnAck and OnError calls for ordinary streams, in offset order, on an SDK background thread. CallbackMaxWaitTimeMs sets how long Close and Dispose wait for pending callbacks: null keeps the SDK default (5000 ms), and CallbackWaitForever waits without a limit. The FFI's ack_user_data carries an integer key into a reference- counted registry, not a GCHandle: the ack ABI has no free hook, so a late callback must find nothing rather than a freed handle. RecreateStream shares the key, since the core gives the new stream the same callback. Static UnmanagedCallersOnly trampolines catch every exception. With a callback set, Close and Dispose call the native close even on a failed stream, so its OnError calls arrive. Adds a JsonAckCallback example, built in CI. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
An exception thrown by IAckCallback.OnAck or OnError is still caught so it cannot unwind into the native caller, but is now reported with Trace.TraceError, including the method and offset. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Unit tests: the C options get both trampolines and the key only when a callback is set, and CallbackMaxWaitTimeMs maps its null, 0, value and CallbackWaitForever states; the registry hands out distinct keys, keeps a shared key until its last release and ignores calls after it; the pointers Apply writes reach the callback, with a UTF-8 message and "" for a null one; callback exceptions stay in the dispatch; and a failed create leaves the registry unchanged on all four create paths. Mock tests: acks reach the callback once each, in order, on the sync and async create paths; a recreated stream keeps the shared key after the old stream is disposed, still gets acks, and its Dispose or DisposeAsync releases the key. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
State when Close returns relative to the ack callbacks, and that some OnError calls can be lost when a failed stream closes before they arrive. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
On a failed stream, Close() can return before pending callbacks run, and they may still run until Dispose(); say so instead of promising delivery. Document what the callback sees after RecreateStream, and that ZerobusSdk methods must not be called from a callback. The JsonAckCallback example now fails when records fail or are rejected. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The callback-exception test now checks that each swallowed exception is reported through Trace with its method, offset and exception text. A new test checks that CloseAsync waits for callbacks still pending when it starts, and that the callback key stays registered until the stream is disposed. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Do the managed work of the headers-provider creates inside the cleanup try, as the OAuth creates do. Document that Close, CloseAsync and Dispose wait for callbacks, that Dispose is required with a callback, what can run after Dispose, and that the wait limit covers only the callback wait. Fix the README snippet and project tree, and make the example report the last acknowledged offset and keep a flush error. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
A stream that is never disposed, and that its callback doesn't reference, releases its callback key when the finalizer runs. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The JsonAckCallback example and the README snippet now dispose the stream after any flush failure and rethrow the flush error, so a Dispose error cannot hide it. The stream creates free the native stream if the managed wrapper cannot be built. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The finalizer test compared the process-wide registry count, which other tests' undisposed streams also lower when the forced collections finalize them. It now checks that its own callback is no longer registered, through a new internal IsRegistered test hook. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
RecreateStream now frees the new native stream if its wrapper cannot be built, as the creates do. The bridge-handle comments in the headers-provider creates now say exactly what can still leak it, and the README ack snippet uses a using block like the other snippets. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What changes are proposed in this pull request?
Adds an ack callback to .NET streams (Beta). Closes #1121.
StreamConfigurationOptions.AckCallbacktakes anIAckCallback(OnAck(long offset),OnError(long offset, string message)). Callbacks arrive in offset order on an SDK background thread.StreamConfigurationOptions.CallbackMaxWaitTimeMssets how longClose/Disposewait for pending callbacks: null uses the SDK default (5000 ms), andCallbackWaitForeverwaits without a limit.System.Diagnostics.Tracewith the offset, instead of terminating the process. A callback that arrives after disposal is ignored safely.RecreateStreamkeeps the same callback.Closereturns, all callbacks have run, unless it waited longer thanCallbackMaxWaitTimeMs(default 5 s); after a stream fails, someOnErrorcalls can be lost ifCloseruns before they arrive (a core fix is in [Rust] Drain ack callbacks when closing a failed stream #1193).Why: today .NET callers learn about acks only through
WaitForOffset[Async]()orFlush[Async](). The C FFI already supports an ack callback, and Python, Java and C++ expose it.How is this tested?
RecreateStreamsharing the callback until both streams are disposed.dotnet build,dotnet format --verify-no-changes, the examples, and the unit and integration tests pass under net8.0 and net10.0 on repeated runs.This pull request and its description were written by Isaac.