Skip to content

[.NET] Add an ack callback for streams - #1238

Draft
dimitrije-djordjevic-db wants to merge 12 commits into
mainfrom
effort/dotnet-ack-callback
Draft

dimitrije-djordjevic-db wants to merge 12 commits into
mainfrom
effort/dotnet-ack-callback

Conversation

@dimitrije-djordjevic-db

Copy link
Copy Markdown
Contributor

What changes are proposed in this pull request?

Adds an ack callback to .NET streams (Beta). Closes #1121.

  • StreamConfigurationOptions.AckCallback takes an IAckCallback (OnAck(long offset), OnError(long offset, string message)). Callbacks arrive in offset order on an SDK background thread.
  • StreamConfigurationOptions.CallbackMaxWaitTimeMs sets how long Close/Dispose wait for pending callbacks: null uses the SDK default (5000 ms), and CallbackWaitForever waits without a limit.
  • An exception thrown by a callback is caught and written to System.Diagnostics.Trace with the offset, instead of terminating the process. A callback that arrives after disposal is ignored safely. RecreateStream keeps the same callback.
  • The docs state the limits: when Close returns, all callbacks have run, unless it waited longer than CallbackMaxWaitTimeMs (default 5 s); after a stream fails, some OnError calls can be lost if Close runs before they arrive (a core fix is in [Rust] Drain ack callbacks when closing a failed stream #1193).
  • README section, a JSON example (built in CI), and a NEXT_CHANGELOG entry.

Why: today .NET callers learn about acks only through WaitForOffset[Async]() or Flush[Async](). The C FFI already supports an ack callback, and Python, Java and C++ expose it.

How is this tested?

  • Unit tests: option-to-C mapping (including the wait setting), the callback registry and its reference counts, calls through the native trampolines, exception containment, and failed creates.
  • Mock-server integration tests: offsets in order on both create paths, and RecreateStream sharing 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.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[.NET] Add ack callback

1 participant