Repository navigation
fix(binding-kafka): don't reject cache fetch when partition leader isn't known yet (backport #2129) - #2147
Merged
Conversation
…n't known yet (#2129) * 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. --------- Co-authored-by: Claude <[email protected]> (cherry picked from commit e943a33)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Backport of #2129 (cherry-pick of develop commit e943a33) to
support/1.x.KafkaCacheClientFetchFactoryandKafkaCacheServerFetchFactoryrejected a new fetch stream withERROR_NOT_LEADER_FOR_PARTITIONwhenever the affinity/member leader id didn't match the fanout's currentleaderId— including the case where the fanout had never resolved a leader at all yet (leadersByPartitionIdreturns itsmissingValue()sentinel before the first successfulmetaresponse). 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 leader-known state via
KafkaCacheRoute.LEADER_UNKNOWN. The leader-mismatch checks only reject the stream when a leader was actually known and it disagrees — a genuinely unknown leader no longer short-circuits the stream. A real mismatch (e.g. partition reassignment) is still rejected exactly as before.Added:
CacheFetchIT#shouldServeClientFetchThatRacesAheadOfLeaderplusspecs/binding-kafka.spec/.../fetch/partition.leader.unknown/{client,server}.rptCacheBootstrapIT#shouldReconnectBootstrapAfterDescribeError/#shouldReconnectBootstrapAfterFetchErrorplus matching.rptscripts, confirming the one-shot startup bootstrap's internal describe/fetch streams recover via the existing fanout reconnect/backoff machinery after a transient broker failure.Backport adaptation
support/1.xpins an olderk3po(3.4.0) that doesn't haveK3poRule#deferStartable(added later, used ondevelopto avoid a fixedThread.sleep(500)race guard inCacheBootstrapIT). Restored the originalThread.sleep(500)+outerRule(k3po).around(engine)ordering for this branch instead of bumping the k3po dependency.Verified:
./mvnw clean verify -pl runtime/binding-kafkaand./mvnw clean verify -pl specs/binding-kafka.specboth green (172 tests inbinding-kafka, matching the original PR's count).Fixes #2116
Generated by Claude Code