Skip to content

[Python] Add validate() and federated_auth() - #1118

Closed
dimitrije-djordjevic-db wants to merge 22 commits into
effort/mux-streams-pyo3-muxfrom
effort/mux-streams-pyo3
Closed

dimitrije-djordjevic-db wants to merge 22 commits into
effort/mux-streams-pyo3-muxfrom
effort/mux-streams-pyo3

Conversation

@dimitrije-djordjevic-db

@dimitrije-djordjevic-db dimitrije-djordjevic-db commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Stack

  1. [Python] Add stream_builder() #1116
  2. [Python] Add multiplexed streams (Beta) #1117
  3. [Python] Add validate() and federated_auth() #1118 ← this PR

What changes are proposed in this pull request?

This PR adds two methods to the stream builder from the previous PRs in this stack, for ordinary and multiplexed streams, in the sync and async SDKs.

validate() mirrors the Rust SDK's validate(). It runs the checks build() makes before connecting, without opening anything. It calls the core's own validate() through the native layer, so the core stays the only source for its limits and defaults, such as the in-flight budget of a multiplexed stream. As in Rust, it does not resolve authentication or parse the Avro writer schema, so build() can still fail. It is available after multiplexed(n) too, and it is a plain method in the async SDK, as in Rust.

federated_auth(token) brings external-IdP federation to the stream builder. Until now it was only available through create_stream(auth=...). It takes the same FederatedToken, and like the other auth setters, the last one called wins.

  • build() and validate() create a fresh native token supplier per call, as create_stream(auth=...) does, so an async stream never keeps the event loop of an earlier build.
  • The returned streams keep the supplier's callback alive for recoveries and let Python's garbage collector see it, so a reference cycle through the stream is still collected.
  • The auth setters of the shared builder now go through common hooks, so a new authentication method does not touch the others. The persistent builder's behavior and error messages are unchanged.
  • [Python] Add PersonalAccessToken auth for create_stream #1072 and [Python] Add personal_access_token to the persistent stream builder #1073 add personal access token auth to create_stream() and the persistent stream builder, and also change zerobus/sdk/shared/auth.py. Whichever lands second adds personal_access_token() to stream_builder() through the same hooks.

It also completes the multiplexed stream docs from the previous PR: an ingest call waits at most 30 seconds for a full sub-stream and then raises a retriable ZerobusException without queuing the record; after a sub-stream fails, recovery means re-ingesting get_unacked_records() and any records not yet queued into a new stream (the README shows how); each sub-stream gets max_inflight_requests // n; and a sync stream is for one thread per instance, while an async stream is safe across tasks.

Related: #163

How is this tested?

Unless noted, the tests run in CI without a server.

  • tests/test_stream_builder.py, validate(): it opens nothing and resolves no authentication, runs the Python checks before the native call, surfaces the core's checks (the in-flight budget, the ordinary-only callback), decodes the descriptor but not the Avro schema, and leaves the builder usable for build().
  • tests/test_stream_builder.py, federated_auth(): a fresh supplier per build() and validate() call with the token's settings, replacement by and of the other auth setters, the auth kept after multiplexed(n), input checks, a federated build reaching the network and calling the token callback once, and failed builds releasing the callback's owner.
  • tests/test_stream_builder.py: recovery_retries and recovery_backoff_ms reach the core, checked by timing a failed open.
  • tests/test_signatures.py and tests/test_persistent_stream.py: native argument defaults and the persistent builder's error messages, pinned in full.
  • tests/test_multiplexed_integration.py (real server, never in CI) now also checks that unacknowledged batches keep each ingest call's grouping, and that an async stream keeps working after a cancelled flush.
  • examples/sync_example_federated.py now also opens a multiplexed stream with federated_auth(). A federated stream was not run against a real server.

This pull request and its description were written by Isaac.

The Rust SDK lets a caller check a stream_builder() configuration with
validate() before build(). Add the same method to the Python stream
builder and the multiplexed stream builder, sync and async.

validate() runs the Python checks, then a new native validate_stream()
that resolves the settings and calls the core's StreamBuilder::validate()
or MultiplexedStreamBuilder::validate(), so the core stays the only
source for its own limits and defaults (such as the in-flight budget of
a multiplexed stream). Like the core, it opens nothing, does not resolve
authentication, and does not parse the Avro schema.

It is a plain method in the async SDK too, as in Rust. It only takes a
read lock on the SDK, which nothing ever write-locks, so it needs no
runtime.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The multiplexed() docstring and the README still said the multiplexed
builder only has ack_callback() and build(); it now has validate() too.
Also group the tokio import with the other external crates.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Cover that validate() opens nothing and resolves no authentication, runs
the Python checks before the native call, surfaces the core's own checks
(the in-flight budget, the ordinary-only callback), decodes the
descriptor but not the Avro schema, extracts the callback, and leaves the
builder usable for build(). Also check the native validate_stream
defaults, and that ack_callback(None) before multiplexed(n) validates and
reaches the network.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Show that build() on the same builder does call the headers provider, and drop a check that repeated the validate() surface tests.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Each auth setter cleared the others by hand, and the "exactly one
authentication method" check named oauth() and headers_provider()
directly. Route both through hooks (_clear_auth(), _auth_methods_set(),
and an _AUTH_METHODS class attribute) so a builder can add an
authentication method without touching the others. Behavior and error
messages are unchanged.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The persistent builder has no federated_auth(), so its message must keep naming only oauth() and headers_provider().

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
External-IdP federation was only available through
create_stream(auth=...). Add federated_auth(token) to the stream builder,
taking the same FederatedToken, for ordinary and multiplexed streams and
for validate(). Like the other auth setters, the last one called wins.

build() and validate() make a fresh native IdpSupplier per call and pass
it, with the token's databricks_client_id, as separate arguments of the
native open and validate calls, as create_stream_federated does. Keeping
it off StreamSettings keeps the handle a short-lived local; a cached one
would freeze the first event loop in the async SDK.

The returned ZerobusStream and MultiplexedStream hold the supplier's
callback holder strongly and expose it to the cyclic GC, so an
owner/stream/callback cycle stays collectable. Every future of the async
multiplexed stream carries the holder too, since it can outlive the
stream object and a recovery may need a fresh token.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The README, the multiplexed examples, and the stream_builder() changelog
entry still listed only OAuth and a custom headers provider for stream
builder authentication. Add federated_auth(token).

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Cover that build() and validate() pass a fresh native supplier per call
with the sync or async policy and the token's cache identity, forward
the Databricks client ID, replace and are replaced by the other auth
setters, and keep the auth in force when multiplexed(n) was called.
Also check the setter's input, the native one-auth-method rule, that a
federated build reaches the network and calls the token callback once,
and that failed builds and validates release the callback's owner.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Match the three-method native auth message, the federated_auth() setter message, and the native databricks_client_id check in full.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Rewrap lines that earlier doc edits left long, list the stream builder
and multiplexed stream builder methods under API Changes, and mention
federated_auth() next to the other auth setters in the README snippet.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Check that unacknowledged batches keep each ingest call's grouping and
match the unacknowledged records, that the async stream takes and
flushes another record after a cancelled flush, and that
recovery_retries and recovery_backoff_ms reach the core. Pin the
persistent builder's table and format messages in full.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Spell out what callers of a multiplexed stream need to handle:
- An ingest call waits at most 30 seconds for a full sub-stream, then
  raises a retriable ZerobusException; the record was not queued.
- After a sub-stream fails, every call re-raises its error, which may be
  retriable, so recovery means get_unacked_records() into a new stream.
  The README shows how, including decoding JSON records first.
- Each sub-stream gets max_inflight_requests // n.
- A sync stream is for one thread at a time; an async one is safe
  across tasks.

Also list NonRetriableException and ZerobusException under the ordinary
StreamBuilder.build() Raises section, as the multiplexed build() does.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The stream is marked closed only once a call observes a sub-stream
failure, and wait_for_message_id() checks only the sub-stream that owns
the message ID, so it can still succeed after another one failed. Say
so instead of "every call raises". Also note the 30 second capacity wait
on ingest_records().

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>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The README recovery snippet dropped every record after the call that
failed: those were never queued, and get_unacked_records() does not
return them. Track how many records were queued and re-ingest the rest
too.

Also say "one thread per instance" for sync streams, and state the
integer division of max_inflight_requests in the multiplexed()
docstring, as the README does.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
A refused connection takes about 2 s on Windows, so absolute bounds failed there. Compare two opens that differ in one setting instead.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
An async open can release the callback holder after asyncio.run() returns, so a single gc.collect() raced on Windows. Retry for a bounded time; a real leak still fails.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Pin the full no-auth message in the existing auth test, check the
validate() order with one missing setting, check the validate() callback
type on the multiplexed builder only, and fold the "consumes nothing"
check into the first validate() test. Keep one multiplexed snapshot case
for federation and drop the builder cycle test that F1, F2 and F10
already cover.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
The test checks the multiplexed builder only; say why the ordinary case is not here.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
Say why each new clippy::too_many_arguments allow is needed: the native
methods mirror their Python keyword signatures one argument per
parameter, and the shared validate_stream helper takes all of them.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Dimitrije Đorđević <dimitrije.djordjevic@databricks.com>
@dimitrije-djordjevic-db

Copy link
Copy Markdown
Contributor Author

Superseded: Python opens multiplexed streams with create_multiplexed_stream instead of a stream builder. See #1224 and #1117.

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.

1 participant