Skip to content

feat(binding-kafka): connect an api request stream to the broker named by its affinity - #3198

Merged
jfallows merged 2 commits into
fix/3183-kafka-cache-api-passthroughfrom
fix/3195-kafka-api-broker-affinity
Nov 13, 2026
Merged

jfallows merged 2 commits into
fix/3183-kafka-cache-api-passthroughfrom
fix/3195-kafka-api-broker-affinity

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

KafkaClientApiFactory served every raw API request stream (KIND_API_REQUEST) by connecting to the first configured bootstrap server, whatever the stream's affinity; the affinity only chose which client instance was used. A raw API stream therefore could not reach a specific broker, so an API that must be sent to a particular broker got an error such as NOT_COORDINATOR (16) from any other broker.

The affinity of a raw API request stream is now the broker id, as it already is for fetch and produce streams. The client resolves it against the route's broker table (KafkaClientRoute.servers, which the meta stream rewrites from the Metadata Brokers[]) every time it opens the network stream:

Affinity Broker table Result
N > 0 knows N connect to broker N's host and port
N > 0 does not know N reset the stream before any client is created, no fallback
0 knows broker 0 connect to broker 0
0 does not know broker 0 first bootstrap server, as before

Because the id is resolved at each connect, a reconnect follows a changed broker address, and the pending streams are reset when the broker has left the table. There is one client (one connection, one learned API version table) per affinity, as before.

One behaviour change, accepted in the design: an affinity-0 stream used to always connect to the first bootstrap server; when the route's meta stream has already learned broker 0 it now connects to broker 0's advertised address. Routes with no table, or no broker 0 in it, behave as before. This relies on what fetch and produce already rely on, that the addresses in the broker table are reachable by the binding.

Design: #3195 (comment) (revised for broker id 0 in #3195 (comment)).

Stacked: this PR's base is fix/3183-kafka-cache-api-passthrough (head b3bc4396062a60d6b9313b5f0c79b2c887ff8b80, #3185). The cache bindings already forward the caller's affinity to the exit of an API request stream, so they need no change here.

Mirrored pattern

KafkaClientFetchFactory and KafkaClientProduceFactory: the affinity is the broker id, resolved with clientRoute.servers.get(affinity), and the resolved server becomes the proxy begin extension of the network stream. The factory takes supplyClientRoute like the meta, fetch and produce factories. No broker address is carried in a begin extension and no new table or refresh mechanism is added.

Protocol text read

Read with curl from the Apache Kafka 4.0.0 tag, clients/src/main/resources/common/message/: OffsetDeleteRequest.json and OffsetDeleteResponse.json (apiKey 47, valid version 0, no broker identity), FindCoordinatorResponse.json (apiKey 10, NodeId/Host/Port in v0-3, Coordinators[] from v4) and MetadataResponse.json (apiKey 3, Brokers[] with NodeId as the map key, Host, Port). Only the API key, valid versions and those field names were checked.

Fixes #3195

Test evidence

Test-first: commit f9b27977 holds only the scripts and IT methods; commit 2 holds the implementation.

New scripts: network pairs create.topics.v7.broker, .broker.bootstrap, .broker.zero, .broker.unknown, .broker.negotiated under streams/network/api (each plays the metadata connection to the bootstrap server, then the API connection to the broker, asserting the proxy destination), and application clients create.topics.v7.broker, .broker.zero, .broker.unknown under streams/application/api (a meta stream, then the API stream with the affinity, a script property affinity overridable with @ScriptProperty). New IT methods, all in existing classes: five in the spec network/KafkaCreateTopicsIT and five in the runtime KafkaCreateTopicsIT.

On the base (b3bc4396) with unchanged production code:

  • The 5 spec network pairs pass (they only pair scripts with each other), 11 of 11 in the class.
  • Runtime KafkaCreateTopicsIT, test commit only: 13 run, 4 failed. shouldCreateTopicsV3ToBrokerByAffinity, ...ToBrokerZeroWhenKnown and ...ToBrokerByAffinityNegotiated fail because the API connection arrives with the proxy destination localhost (the bootstrap server) where the script expects broker2.example.com or broker0.example.com; shouldRejectCreateTopicsV3ToUnknownBroker fails because the stream is accepted where connect aborted is expected. Each then times out on the rerun. shouldCreateTopicsV3ToBootstrapWhenBrokerZeroUnknown passes before and after: it guards today's behaviour.
  • With the implementation: 13 of 13 pass.

Verification of the head: clean verify of specs/binding-kafka.spec and runtime/binding-kafka together, with checkstyle, license and notice checks on and the build cache skipped (-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 472 run, 0 failures, 12 skipped.
  • runtime/binding-kafka: unit tests 229 run, 0 failures; ITs 447 run, 0 failures, 67 skipped.

What I did not run

  • The full reactor, and runtime/engine (not touched).
  • The examples tests that CI runs with Docker Compose.
  • Anything against a real multi-broker cluster.
  • A check that the skipped ITs were already skipped on the base.

How it was built: the mvnw wrapper could not download its jar (HTTP 429 from the egress proxy), so I used the system mvn 3.9.11 with -Dmaven.artifact.threads=1; two runs failed first only because the proxy refused downloads of the surefire provider, and passed unchanged when rerun with -U.

Not delivered / follow-ups

  • The new application scripts are clients only. A paired application server would have to sit beside the meta stream on the same accept address, so the application level has no spec IT of its own for these three; the pairing is covered at the network level and the application clients are exercised by the runtime ITs.
  • An affinity of 0 cannot select the first bootstrap server once the table knows broker 0; that is the accepted behaviour change above.
  • The table is populated by a meta stream on the route, as fetch and produce already assume; there is no on-demand Metadata fetch. A non-zero id that is not yet in the table resets the stream.
  • clientsByAffinity still never evicts entries; with the unknown-id check before a client is created, it is bounded by the size of the broker table.

Generated by Claude Code

… request stream

Learn the broker table from a metadata stream on the route, then send a
raw api request whose affinity is a broker id, expecting the network
stream to be opened to that broker's host and port: a non-zero id, id 0
when the table knows broker 0, the same with api version negotiation,
and an id the table does not know, which must reset the stream. An
affinity of 0 with no broker 0 in the table keeps connecting to the first
bootstrap server.

The spec scripts pair each network client with its server. The runtime
tests fail while every api request stream connects to the first
bootstrap server whatever its affinity.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV39g7wrgVnLJ7j2nV
…d by its affinity

Resolve the broker of a raw api request stream from its affinity as the
fetch and produce clients do: look the affinity up as a broker id in the
route's broker table, which the meta stream refreshes from the metadata
brokers. An affinity of 0 resolves to broker 0 when the table knows it,
and otherwise to the first bootstrap server as before. A non-zero
affinity the table does not know resets the stream before a client is
created, with no fallback to another broker.

The address is resolved at every connect, so a reconnect follows a
changed broker address and resets the pending streams when the broker
has left the table.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoV39g7wrgVnLJ7j2nV
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: address a raw API request stream to a specific broker by broker id

2 participants