Skip to content

fix(binding-kafka): don't reject cache fetch when partition leader isn't known yet - #2129

Merged
jfallows merged 14 commits into
developfrom
claude/github-issue-2116-f2i4b4
Jul 13, 2026
Merged

jfallows merged 14 commits into
developfrom
claude/github-issue-2116-f2i4b4

Conversation

@jfallows

Copy link
Copy Markdown
Contributor

Description

KafkaCacheClientFetchFactory and KafkaCacheServerFetchFactory rejected a new fetch stream with ERROR_NOT_LEADER_FOR_PARTITION whenever the affinity/member leader id didn't match the fanout's current leaderId — including the case where the fanout had never resolved a leader at all yet (leadersByPartitionId returns its missingValue() sentinel before the first successful meta response). That meant a fetch stream racing ahead of the one-shot startup bootstrap for a partition was permanently rejected instead of being served once the real leader was known, since nothing ever retried the fetch.

Both factories now track a leaderKnown boolean per stream, computed from whether the leader lookup returned the sentinel value at stream-open time. The leader-mismatch checks in onClientFanoutMemberOpening/onServerFanoutMemberOpening and onClientInitialBegin/onServerInitialBegin only reject the stream when a leader was actually known and it disagrees — a genuinely unknown leader no longer short-circuits the stream. This does not change behavior for the normal case: once a leader is known, a real mismatch (e.g. partition reassignment) is still rejected exactly as before, and the full existing regression suite (CacheFetchIT, CacheMergedIT, CacheBootstrapIT, CacheDescribeIT, CacheMetaIT, CacheProduceIT) passes unchanged.

Added:

  • CacheFetchIT#shouldServeClientFetchThatRacesAheadOfLeader plus specs/binding-kafka.spec/.../fetch/client.races.ahead.of.leader/{client,server}.rpt — a regression test proving a bare fetch stream (no prior describe/meta) is now served instead of rejected.
  • CacheBootstrapIT#shouldReconnectBootstrapAfterDescribeError / #shouldReconnectBootstrapAfterFetchError plus matching .rpt scripts — coverage confirming the one-shot startup bootstrap's internal describe/fetch streams recover via the existing fanout reconnect/backoff machinery after a transient broker failure.

Verified test-first: confirmed the new client.races.ahead.of.leader test fails against the pre-fix code (git stash) before restoring the fix.

Fixes #2116


Generated by Claude Code

claude added 2 commits July 11, 2026 01:28
…after broker failure

Adds CacheBootstrapIT scenarios simulating a broker that rejects the
bootstrap's describe/fetch connection once (e.g. not ready yet at Zilla
startup) then accepts on retry. Both already pass against unmodified
code: the bootstrap's internal describe/meta/fetch streams route through
the same per-topic KafkaCacheServerDescribeFactory/MetaFactory/FetchFactory
fanouts used by regular cache_client reads, and inherit their existing
SIGNAL_RECONNECT exponential-backoff retry for free, since the bootstrap
stream stays registered as a fanout member. Locks in this previously
untested resilience behavior referenced in #2116.
…n't known yet

KafkaCacheClientFetchFactory and KafkaCacheServerFetchFactory each reject
an incoming fetch stream with ERROR_NOT_LEADER_FOR_PARTITION whenever the
caller's declared affinity doesn't match cacheRoute.supplyLeadersByPartitionId().
That map is populated by the meta fanout and starts empty until describe/meta
has resolved the topic at least once. A fetch request that arrives before that
happens - the exact startup race in #2116, e.g. Kafka not yet ready when Zilla
starts - reads a "missing" sentinel and gets torn down immediately, since a
default/unset affinity can never equal it.

Distinguish "leader genuinely unknown yet" from "leader known but stale caller
belief" by tracking whether the lookup actually resolved an entry. Only reject
the stream in the latter case (a real partition reassignment); when the leader
is unknown, let the stream join the fanout and wait, the same way it already
would once the leader resolves later. Verified this doesn't affect any of the
existing leader-mismatch/reconnect coverage across CacheFetchIT, CacheMergedIT,
CacheProduceIT, CacheDescribeIT, CacheMetaIT, and CacheBootstrapIT (172 tests,
all still green).

Also adds two CacheBootstrapIT regression tests proving the bootstrap's
internal describe/meta/fetch streams already inherit exponential-backoff
reconnect from the shared per-topic server-side fanouts (no code change
needed there) - the initial hypothesis in #2116 about a missing retry timer
did not hold up under test.
claude added 12 commits July 11, 2026 16:16
…BootstrapIT

Thread.sleep(500) before k3po.start() was a guess at how long the one-shot
bootstrap connection takes to fail against a not-yet-listening k3po script.
EngineRule.aroundStart(k3po::deferStartable) (already used elsewhere, e.g.
MqttKafkaSessionProxyIT, AsyncapiProxyIT, and binding-smux in zilla-plus)
defers engine.start() until k3po's script reaches PREPARED, guaranteeing the
mock server is listening before the engine's first connection attempt.
…oute.LEADER_UNKNOWN constant

leaderKnown was a derived boolean, redundant with the leaderId field it was
always passed and stored alongside. Publish the missing-value sentinel as a
named constant on KafkaCacheRoute (the actual owner of leadersByPartitionId)
instead, and compare leaderId against it directly at each use site. Same
behavior, one fewer field/parameter per stream class, and the sentinel is
now defined in exactly one place instead of being re-derived per stream.
Use the bare constant name at each comparison site instead of the
KafkaCacheRoute-qualified form, matching the existing static-import
convention already used for sibling constants in these two files.
…WN guards

Order each && so the common steady-state case (leader known, ids agree)
short-circuits on the first, cheaper-to-fail comparison without ever
touching LEADER_UNKNOWN.
…known

Previously, KafkaCacheServerFetchFanout.doServerFanoutInitialBeginIfNecessary
connected upstream unconditionally, even when the partition leader was still
LEADER_UNKNOWN. That connect used the unresolved leaderId as the stream
affinity, which KafkaClientFetchFactory can't map to a real broker
(clientRoute.servers.get(affinity) misses), so the connection silently fell
back to the exit route's default/bootstrap connection instead of the actual
leader's own server stream - working only by coincidence when that fallback
happened to reach the leader, and otherwise waiting on a real broker's
NOT_LEADER_FOR_PARTITION response to unwind and retry.

Gate the connect on leaderId != LEADER_UNKNOWN. When still unknown, reuse the
existing SIGNAL_RECONNECT backoff (the same one used for reconnect-after-
failure) to re-check cacheRoute's leadersByPartitionId until the topic's own
meta resolution (independent of this fetch) populates it, then connect with
the real, resolved leader affinity. No more speculative connects through the
bootstrap fallback.

Reworked the CacheFetchIT races-ahead-of-leader regression test to drive a
real meta resolution after the fetch is sent (via a within-script notify/
await barrier) instead of relying on an immediately-successful bogus-affinity
connect, since that fallback behavior no longer occurs. Confirmed the
reworked test fails (assertion error from the fanout connecting out of
sequence) against the pre-fix code before restoring the fix. Full existing
regression suite (172 tests across CacheFetchIT, CacheMergedIT,
CacheBootstrapIT, CacheDescribeIT, CacheMetaIT, CacheProduceIT) passes
unchanged.
…ario dir

No sibling scenario directory under fetch/ uses a client./server. role
prefix (they name the entity/condition under test instead, e.g.
partition.not.leader, filter.sync). Rename client.races.ahead.of.leader
to races.ahead.of.leader to match.
…r.unknown

Matches the noun-first convention used by fetch/ siblings (partition.not.leader,
partition.leader.distinct, filter.sync, message.key) instead of a verb-phrase
scenario name.
…ged.fetch.reconnect.*

Matches the merged.<kind>.* / unmerged.<kind>.* prefix convention used by
every other scenario directory in streams/application/merged/, and lines up
with the existing unmerged.fetch.* scenarios CacheBootstrapIT already uses
(unmerged.fetch.message.values, unmerged.fetch.message.values.live).
…rios

unmerged.fetch.reconnect.after.describe.error and
unmerged.fetch.reconnect.after.fetch.error only had server.rpt (used by
CacheBootstrapIT, where the engine plays the client role). Add the missing
client.rpt peer for each, and cover both scenarios in MergedIT so the two
scripts are verified as self-consistent per the spec IT convention.

partition.leader.unknown is left without spec-level peer IT coverage,
matching the existing precedent set by produce/partition.not.leader.reconnect:
the scenario exercises Zilla's own defer-connect/reconnect mediation between
fetch and meta streams, which a direct client-server run (no Zilla in the
loop) cannot faithfully reproduce.
@jfallows
jfallows merged commit e943a33 into develop Jul 13, 2026
42 checks passed
jfallows added a commit that referenced this pull request Jul 13, 2026
…n't known yet (#2129) (#2147)

* test(binding-kafka): add regression coverage for bootstrap reconnect after broker failure

Adds CacheBootstrapIT scenarios simulating a broker that rejects the
bootstrap's describe/fetch connection once (e.g. not ready yet at Zilla
startup) then accepts on retry. Both already pass against unmodified
code: the bootstrap's internal describe/meta/fetch streams route through
the same per-topic KafkaCacheServerDescribeFactory/MetaFactory/FetchFactory
fanouts used by regular cache_client reads, and inherit their existing
SIGNAL_RECONNECT exponential-backoff retry for free, since the bootstrap
stream stays registered as a fanout member. Locks in this previously
untested resilience behavior referenced in #2116.

* fix(binding-kafka): don't reject cache fetch when partition leader isn't known yet

KafkaCacheClientFetchFactory and KafkaCacheServerFetchFactory each reject
an incoming fetch stream with ERROR_NOT_LEADER_FOR_PARTITION whenever the
caller's declared affinity doesn't match cacheRoute.supplyLeadersByPartitionId().
That map is populated by the meta fanout and starts empty until describe/meta
has resolved the topic at least once. A fetch request that arrives before that
happens - the exact startup race in #2116, e.g. Kafka not yet ready when Zilla
starts - reads a "missing" sentinel and gets torn down immediately, since a
default/unset affinity can never equal it.

Distinguish "leader genuinely unknown yet" from "leader known but stale caller
belief" by tracking whether the lookup actually resolved an entry. Only reject
the stream in the latter case (a real partition reassignment); when the leader
is unknown, let the stream join the fanout and wait, the same way it already
would once the leader resolves later. Verified this doesn't affect any of the
existing leader-mismatch/reconnect coverage across CacheFetchIT, CacheMergedIT,
CacheProduceIT, CacheDescribeIT, CacheMetaIT, and CacheBootstrapIT (172 tests,
all still green).

Also adds two CacheBootstrapIT regression tests proving the bootstrap's
internal describe/meta/fetch streams already inherit exponential-backoff
reconnect from the shared per-topic server-side fanouts (no code change
needed there) - the initial hypothesis in #2116 about a missing retry timer
did not hold up under test.

* test(binding-kafka): replace fixed sleep with deferStartable in CacheBootstrapIT

Thread.sleep(500) before k3po.start() was a guess at how long the one-shot
bootstrap connection takes to fail against a not-yet-listening k3po script.
EngineRule.aroundStart(k3po::deferStartable) (already used elsewhere, e.g.
MqttKafkaSessionProxyIT, AsyncapiProxyIT, and binding-smux in zilla-plus)
defers engine.start() until k3po's script reaches PREPARED, guaranteeing the
mock server is listening before the engine's first connection attempt.

* refactor(binding-kafka): replace leaderKnown boolean with KafkaCacheRoute.LEADER_UNKNOWN constant

leaderKnown was a derived boolean, redundant with the leaderId field it was
always passed and stored alongside. Publish the missing-value sentinel as a
named constant on KafkaCacheRoute (the actual owner of leadersByPartitionId)
instead, and compare leaderId against it directly at each use site. Same
behavior, one fewer field/parameter per stream class, and the sentinel is
now defined in exactly one place instead of being re-derived per stream.

* style(binding-kafka): static-import KafkaCacheRoute.LEADER_UNKNOWN

Use the bare constant name at each comparison site instead of the
KafkaCacheRoute-qualified form, matching the existing static-import
convention already used for sibling constants in these two files.

* style(binding-kafka): put leader-mismatch check first in LEADER_UNKNOWN guards

Order each && so the common steady-state case (leader known, ids agree)
short-circuits on the first, cheaper-to-fail comparison without ever
touching LEADER_UNKNOWN.

* fix(binding-kafka): defer cache-server fetch connect until leader is known

Previously, KafkaCacheServerFetchFanout.doServerFanoutInitialBeginIfNecessary
connected upstream unconditionally, even when the partition leader was still
LEADER_UNKNOWN. That connect used the unresolved leaderId as the stream
affinity, which KafkaClientFetchFactory can't map to a real broker
(clientRoute.servers.get(affinity) misses), so the connection silently fell
back to the exit route's default/bootstrap connection instead of the actual
leader's own server stream - working only by coincidence when that fallback
happened to reach the leader, and otherwise waiting on a real broker's
NOT_LEADER_FOR_PARTITION response to unwind and retry.

Gate the connect on leaderId != LEADER_UNKNOWN. When still unknown, reuse the
existing SIGNAL_RECONNECT backoff (the same one used for reconnect-after-
failure) to re-check cacheRoute's leadersByPartitionId until the topic's own
meta resolution (independent of this fetch) populates it, then connect with
the real, resolved leader affinity. No more speculative connects through the
bootstrap fallback.

Reworked the CacheFetchIT races-ahead-of-leader regression test to drive a
real meta resolution after the fetch is sent (via a within-script notify/
await barrier) instead of relying on an immediately-successful bogus-affinity
connect, since that fallback behavior no longer occurs. Confirmed the
reworked test fails (assertion error from the fanout connecting out of
sequence) against the pre-fix code before restoring the fix. Full existing
regression suite (172 tests across CacheFetchIT, CacheMergedIT,
CacheBootstrapIT, CacheDescribeIT, CacheMetaIT, CacheProduceIT) passes
unchanged.

* style(binding-kafka): drop stray client. prefix from races-ahead scenario dir

No sibling scenario directory under fetch/ uses a client./server. role
prefix (they name the entity/condition under test instead, e.g.
partition.not.leader, filter.sync). Rename client.races.ahead.of.leader
to races.ahead.of.leader to match.

* style(binding-kafka): rename races.ahead.of.leader to partition.leader.unknown

Matches the noun-first convention used by fetch/ siblings (partition.not.leader,
partition.leader.distinct, filter.sync, message.key) instead of a verb-phrase
scenario name.

* style(binding-kafka): rename bootstrap.reconnect.* scenarios to unmerged.fetch.reconnect.*

Matches the merged.<kind>.* / unmerged.<kind>.* prefix convention used by
every other scenario directory in streams/application/merged/, and lines up
with the existing unmerged.fetch.* scenarios CacheBootstrapIT already uses
(unmerged.fetch.message.values, unmerged.fetch.message.values.live).

* test(binding-kafka): add missing client.rpt peers for reconnect scenarios

unmerged.fetch.reconnect.after.describe.error and
unmerged.fetch.reconnect.after.fetch.error only had server.rpt (used by
CacheBootstrapIT, where the engine plays the client role). Add the missing
client.rpt peer for each, and cover both scenarios in MergedIT so the two
scripts are verified as self-consistent per the spec IT convention.

partition.leader.unknown is left without spec-level peer IT coverage,
matching the existing precedent set by produce/partition.not.leader.reconnect:
the scenario exercises Zilla's own defer-connect/reconnect mediation between
fetch and meta streams, which a direct client-server run (no Zilla in the
loop) cannot faithfully reproduce.

---------


(cherry picked from commit e943a33)

Co-authored-by: Claude <[email protected]>
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: cache-client fetch never recovers if the one-shot startup bootstrap fails

2 participants