Skip to content

fix(binding-kafka): refresh the broker table before answering an unknown broker id - #3232

Merged
jfallows merged 4 commits into
test/3212-kafka-api-broker-application-serversfrom
fix/3213-kafka-api-stale-broker-id
Nov 15, 2026
Merged

jfallows merged 4 commits into
test/3212-kafka-api-broker-application-serversfrom
fix/3213-kafka-api-stale-broker-id

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

A raw API request stream carries the id of the broker to connect to as its affinity. A non-zero id that the route's broker table does not know used to be reset at once by newStream, and nothing refreshed the table, so the caller's next request could meet the same stale table.

The stream is now held while the route's Metadata refresh runs, and is reset with the same reset as before only after that refresh has completed or failed.

  • The refresh is clientRoute.metaFlush, which KafkaClientFetchFactory and KafkaClientProduceFactory already call when a broker leaves or changes.
  • There is no retry. Even if the refreshed table now contains the id, the answer to that attempt is the reset.
  • A failed refresh (meta stream aborted or reset) still ends the stream, so the caller is not left waiting.
  • A route with no meta stream resets at once, since nothing can refresh it.
  • Streams that hit the same unknown id share one flush and are reset together. A caller that aborts or resets its own stream while waiting leaves the queue.
  • A reconnect that finds the id gone from the table gets the same treatment, instead of the immediate reset.
  • An unknown id creates no entry in clientsByAffinity, as before, so the map stays bounded by the broker table.
  • Affinity 0 and known ids behave as before. onNetClosed gets no metaFlush: in fetch and produce it serves a connected broker that went away, which this issue does not cover.

Design: #3213 (comment) (revised design), following the decision recorded on the issue.

New mechanism

KafkaClientRoute gains a completion registry: registerMetaRefreshed / unregisterMetaRefreshed and a metaRefreshed fan-out, the reverse of registerMetaFlush / metaFlush, plus hasMeta(). The API factory registers a one-shot callback before it flushes. KafkaClientMetaFactory fires metaRefreshed after a Metadata response has rewritten the table, and from the meta stream's cleanupApplication when it fails. The first completion or failure releases the waiting streams.

No existing mechanism fits because metaFlush is fire-and-forget. Nothing in the module reported a refresh finishing or failing: KafkaClientRoute held only the table and the flush list, and a failure was visible only inside the meta client.

Alternatives rejected:

  1. Reset right after firing metaFlush. This does not answer "only after the refresh".
  2. A timer on the held stream. It is new and arbitrary, and it either waits too long or answers before a slow refresh.
  3. Retry inside the binding until the table has the id. The issue's decision puts recovery with the caller and rules out a hidden retry.

Mirrored pattern

KafkaClientFetchFactory / KafkaClientProduceFactory call clientRoute.metaFlush.accept(traceId). The registry mirrors KafkaClientRoute's existing ArrayUtil list of LongConsumer flushes. The waiting queue mirrors KafkaApiClient.pending.

Fixes #3213

Test evidence

Test-first: the first commit holds only the scripts and IT methods, the second the production change.

New and changed scripts, all under specs/binding-kafka.spec:

  • streams/network/api/create.topics.v7.broker.unknown: now plays a second Metadata exchange before the reset (the id is still absent from the refreshed table).
  • create.topics.v7.broker.unknown.refreshed (network and application pairs): the refreshed table lists broker 9 and moves partition 1 to it. The stream is still reset and no connection is made to broker 9.
  • create.topics.v7.broker.unknown.refresh.failed (network and application pairs): the second Metadata request is answered with an abort of the meta connection. The stream is reset.
  • New IT methods in existing classes only: the network and application spec KafkaCreateTopicsIT and the runtime KafkaCreateTopicsIT.
  • The unchanged broker, broker.zero, broker.bootstrap and broker.negotiated scripts are untouched. They run in the same class and pass.

On the first commit alone (f62c7415a), with unchanged production code:

  • Spec network KafkaCreateTopicsIT: 13 of 13. Spec application KafkaCreateTopicsIT: 6 of 6. These only pair scripts with each other.
  • Runtime KafkaCreateTopicsIT: 3 scenarios fail, shouldRejectCreateTopicsV3ToUnknownBroker, ...ToUnknownBrokerRefreshed and ...ToUnknownBrokerWhenRefreshFails. Each expected the second Metadata request (read 26 ... 3s # metadata) and saw none, because the binding reset the stream without refreshing; each then times out at 5 s on the rerun. The other methods in the class pass. Messages kept as commit1-failing-runtime-IT.log in the session scratchpad.

With the production commit: runtime KafkaCreateTopicsIT passes 15 of 15.

Order of the reset after the refresh was observed, not scripted. With temporary trace output (removed before the commit), each scenario printed: initial meta response, then metaFlush fired, then (second response decoded, or meta failure), then refresh released waiting=1. The release came only after the refresh finished or failed.

Flow control: a held stream gets no WINDOW and sends no data. It is answered with RESET on the initial side and ABORT on the reply side, the same cleanupApp the reconnect path already used. There is no new credit path.

Verification of the head (clean verify -pl specs/binding-kafka.spec,runtime/binding-kafka, checkstyle, license and notice checks on, -Dmaven.build.cache.skipCache=true, after installing upstream with -am -DskipTests -DskipITs -Dskip.checks=true): BUILD SUCCESS.

  • specs/binding-kafka.spec: unit tests 226 run, 0 failures; ITs 476 run, 0 failures, 12 skipped.
  • runtime/binding-kafka: unit tests 229 run, 0 failures; ITs 449 run, 0 failures, 67 skipped. This includes the module's jacoco coverage check.

Not delivered

  • The application refresh.failed pair does not assert what the meta stream sees. A read aborted after a flushed meta data extension was never delivered to the client script, including for an existing meta script with an abort appended. I did not find the cause. The client script asserts only the reset of the API stream, and the runtime IT does not assert the meta stream's fate either.
  • Several streams on one unknown id (one shared flush) has no IT. A two-stream script races the first release against the second stream's arrival.
  • The first-completion-or-failure rule means a failure of any meta stream on the route releases the waiting streams, even one that was not carrying the flush.
  • reconnectAttempt in KafkaClientApiFactory is still never reset (found while reading, unrelated here).
  • The full reactor, runtime/engine, the examples tests that CI runs with Docker Compose, and anything against a real multi-broker cluster.
  • A check that the skipped ITs were already skipped on develop.
  • Rebase note: test(binding-kafka): pair the broker-addressed api request application clients with servers #3215 adds application servers for the broker, broker.zero and broker.unknown scripts and methods to the same application KafkaCreateTopicsIT. The .unknown application client is unchanged here. A rebase after test(binding-kafka): pair the broker-addressed api request application clients with servers #3215 should only conflict textually in that IT class.

Generated by Claude Code

jfallows and others added 4 commits November 13, 2026 10:35
* feat(common-json): mark and rewind the generator

A generator can mark its position between writes and rewind to it, which discards the bytes written since and
restores the structural context, so a unit of output can be generated, measured and then kept or taken back and
generated again. This is the output-side counterpart of the parser mark and rewind.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUTvQwZM7nY1gjgnZJ

* refactor(common-json): require every generator to support mark and rewind

The generator mark and rewind are declared without a default, as JsonGeneratorImpl is the one implementation
of JsonGeneratorEx and supports both.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUTvQwZM7nY1gjgnZJ

* test(common-json): rewind the generator that escapes its output

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoUTvQwZM7nY1gjgnZJ

---------

Co-authored-by: Claude <[email protected]>
…s answered

A raw API request stream addressed to a non-zero broker id that the route's
table does not know triggers an immediate Metadata refresh, and is answered
with the existing reset only after that refresh has completed or failed.
There is no retry, even when the refreshed table now contains the id.

The network scripts for the unknown id now play a second Metadata exchange
before the reset. New paired scripts cover a refresh whose response lists the
id (still reset, no connection to that broker) and a refresh that fails
(the meta connection is aborted). The application pairs for the same two
scenarios carry the refreshed partitions on the meta stream.

The spec network and application ITs and the runtime IT gain one method per
scenario. The runtime scenarios fail against the current binding because it
never sends the second Metadata request.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVZT5Lb5aAUG57ioRa
…own broker id

A raw API request stream addressed to a non-zero broker id that the route's
table does not know is no longer reset at once. It is held, the route's
Metadata refresh is triggered (the same metaFlush the fetch and produce
clients use), and the stream is reset with the existing reset only after
that refresh has completed or failed. There is no retry, so the answer to
that attempt is the reset even if the refreshed table now has the id, and
the caller's next request meets a fresh table.

KafkaClientRoute gains the reverse of its meta flush registry: a one-shot
completion callback that the meta client fires after a Metadata response has
rewritten the table and when its stream fails. Streams hitting the same
unknown id share one flush and are reset together; a route with no meta
stream resets at once. The same holds when a reconnect finds the id gone.
An unknown id gets no entry in clientsByAffinity.

Fixes #3213

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoVZT5Lb5aAUG57ioRa
…cation-servers' into fix/3213-kafka-api-stale-broker-id
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.

binding-kafka: refresh the broker table before answering a raw API stream addressed to an unknown broker id

2 participants