Skip to content

fix(binding-kafka): restart api client reconnect back-off after a connection opens - #3268

Merged
jfallows merged 3 commits into
developfrom
fix/3256-kafka-api-reconnect-attempt
Nov 17, 2026
Merged

jfallows merged 3 commits into
developfrom
fix/3256-kafka-api-reconnect-attempt

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

The reconnectAttempt counter of the API client in KafkaClientApiFactory is incremented on every reconnect (50 << reconnectAttempt++, capped by client.reconnect) and was never reset. The client is long-lived (cached per affinity), so after an outage the back-off of a later reconnect carried on from where the earlier one ended instead of starting again.

The counter is now reset when the initial side of a connection first opens (first window in onNetWindow). This mirrors how the cache client fetch stream (KafkaCacheClientFetchFactory, onClientFanoutInitialWindow) and the cache client produce stream (KafkaCacheClientProduceFactory, onClientFanInitialOpened) reset their own reconnectAttempt; the cache server fetch, meta, describe and produce streams do the same.

Scenario create.topics.v7.reconnect.backoff.reset (paired app and net scripts, in the existing KafkaCreateTopicsIT classes): eight requests are queued on one API client with the default client.reconnect of 5 s; the first seven connections are aborted by the server while their request is in flight, the eighth answers.

Test evidence

The evidence is timing-based. The engine signaler fires on a 512 ms timer wheel (EngineWorker: new DeadlineTimerWheel(MILLISECONDS, ..., 512, 1024)), so a back-off of 50 ms costs one tick (about 0.5 s), and a restarted and a growing back-off only separate once the growing one passes a tick. The scenario is sized for that, against the existing 5 s Timeout of the IT class (which covers the test body, not engine and k3po start-up):

Code Result Test body time
before the fix fails, times out at the 5 s limit (2 runs); with the limit raised to 30 s it completes about 8.5 s (3.5 s over the limit)
with the fix passes (3 runs at 5 s; 2 runs also pass at a 4 s limit) about 3.4 s (margin at least 1 s, about 1.6 s estimated)

The body times are the reported test time less about 1.7 s of start-up overhead (measured with a 3 s limit), so they are estimates; the pass/fail results are measured. With a debug print of the computed back-off (not committed) the fixed code scheduled every reconnect with reconnectAttempt = 0. The same scenario with client.reconnect=0 (which skips the back-off) passes on the unfixed code, so the scripts themselves are sound. The first commit's test (1 s cap, 12 streams) could not separate the two within the limit because of the tick, so the second commit re-sizes it; commit 1 failed with and without the fix and is superseded.

Verified with checks on and the build cache skipped:

  • specs/binding-kafka.spec: clean install, 226 unit tests, 24 stream ITs (network and application KafkaCreateTopicsIT including the new method), build success
  • runtime/binding-kafka: clean verify, 229 unit tests and 462 ITs, 0 failures, 67 skipped (existing skips), build success; KafkaCreateTopicsIT 16 tests, 0 failures
  • license:format and checkstyle:check clean

Kafka protocol text

Read myself: the Apache Kafka protocol documentation page (kafka.apache.org/44/design/protocol), the opening of the Network section and the error-code table; I searched the whole page text for "back-off" and "reconnect" and found neither. I did not read the rest of the page (API key tables and message schemas) in full; the request and response frames in the scripts are copied from the existing create.topics.v7 scripts, not newly derived.

  • "No handshake is required on connection or disconnection."
  • "The server guarantees that on a single TCP connection, requests will be processed in the order they are sent and responses will return in that order as well."

The protocol text says nothing about reconnect back-off; this is a client-side policy, and the change only makes it consistent with the other reconnecting streams of this binding.

Not delivered

  • No change to the clock source or the timer resolution; the timing-based test is the consequence.
  • No unit test: the behaviour lives in the stream state machine, covered by the IT scenario.
  • No change to other factories, which already reset their counters.

Fixes #3256


Generated by Claude Code

…a connection opens

Twelve requests are queued on one api client with client.reconnect of 1s.
Each of the first eleven connections is aborted by the server while its
request is in flight, so the client reconnects with a back-off each time.
The twelfth connection answers. With the back-off restarting after each
opened connection every reconnect waits 50ms; with the counter never reset
the delays grow 50, 100, 200, 400, 800 then 1000ms and the scenario exceeds
the five second limit of the existing IT class.

Refs #3256

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

Run the scenario with the default client.reconnect of 5s and eight queued
requests, so seven connections are aborted while a request is in flight and
the eighth answers. The engine signaler fires on a 512ms timer wheel, so a
cap of 1s could not separate a restarted back-off from a growing one within
the five second limit of the IT class.

Refs #3256

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

KafkaApiClient incremented reconnectAttempt on every reconnect and never
reset it, so after an outage the back-off of a later reconnect carried on
from where the earlier one ended. Reset it when the initial side of a
connection first opens, as the cache client fetch and produce streams do.

Fixes #3256

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SiHVoYEX7orynKcJ3dSiXi
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: reconnectAttempt in KafkaClientApiFactory is never reset

2 participants