Repository navigation
Re-subscribe NATS JetStream pull consumers after a reconnect - #103557
Open
Krishnachaitanyakc wants to merge 1 commit into
Open
Krishnachaitanyakc wants to merge 1 commit into
Krishnachaitanyakc wants to merge 1 commit into
Conversation
When a NATS server in the cluster fails over, the libnats C library
reconnects to the next server (TCP, CONNECT, PING/PONG). The library
automatically restores plain `Subscribe` and `QueueSubscribe`
subscriptions, but it does NOT restore JetStream pull subscriptions
(`js_PullSubscribeAsync`), which require application-driven
`PUB $JS.API.CONSUMER.MSG.NEXT` round-trips.
Result: after a NATS failover, JetStream consumers in `StorageNATS`
were silently dead — the underlying connection reported as healthy and
the streaming task kept looping, but no messages were ever delivered.
The only workaround was to `DETACH TABLE` and `ATTACH TABLE`, which
re-ran `subscribe` and re-issued the JetStream pull.
Fix:
* Add a monotonic `reconnect_count` to `NATSConnection`, incremented
from the libnats reconnect callback. Safe to keep as a member
atomic because `natsConnection_Destroy` synchronously joins the
libnats reconnect thread before returning, so the callback cannot
fire after the `NATSConnection` is destroyed (the same property
the existing log-only callback already relies on).
* Add `INATSConsumer::needsResubscribeOnReconnect`. Plain core
subscriptions return false (auto-restored by libnats), JetStream
pull subscriptions return true.
* In `StorageNATS::streamingToViewsFunc`, snapshot the reconnect
counter at entry. When it increments, run a new
`resubscribeConsumersOnReconnect` that selectively
`unsubscribe + subscribe` only for consumers that need it
(JetStream), leaving working core subscriptions untouched.
* Read the counter BEFORE re-subscribing and store it AFTER. If a
reconnect happens during re-subscribe, the next iteration sees a
fresh mismatch and re-subscribes again, guaranteeing eventual
recovery.
* Initialize the snapshot after the first successful subscribe in
`initializeConsumersFunc` so a reconnect that happens during init
is not treated as a new event by the streaming task.
A C++ unit test verifies the counter primitive (initial zero, stable
across reads). The end-to-end re-subscribe path requires a live NATS
cluster and is documented as the manual repro from the issue; per
ClickHouse's history of flaky messaging integration tests (e.g. the
RabbitMQ reconnect tests), no new flaky integration test is added.
Closes ClickHouse#96651
Krishnachaitanyakc
marked this pull request as ready for review
April 27, 2026 18:07
Contributor
Author
|
@Michicosun @yariks5s can you please review this? |
This branch has not been deployed
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.
Linked issues
Fixes #96651
Summary
When a NATS server in the cluster fails over, the libnats C library reconnects to the next server (TCP,
CONNECT,PING/PONG). The library automatically restores plainSubscribeandQueueSubscribesubscriptions, but it does not restore JetStream pull subscriptions (js_PullSubscribeAsync), which are driven by application-levelPUB $JS.API.CONSUMER.MSG.NEXTround-trips.Result: after a NATS failover, JetStream consumers in
StorageNATSare silently dead — the underlying connection reports as healthy andstreamingToViewsFunckeeps looping, but no messages are ever delivered. The only workaround isDETACH TABLEfollowed byATTACH TABLE, which re-runssubscribeand re-issues the JetStream pull.Approach
Add a monotonic
reconnect_counttoNATSConnection, incremented from the libnats reconnect callback. Safe to keep as a member atomic becausenatsConnection_Destroysynchronously joins the libnats reconnect thread before returning, so the callback cannot fire after theNATSConnectionis destroyed (the same property the existing log-only callback already relies on).Add
INATSConsumer::needsResubscribeOnReconnect. Plain core subscriptions returnfalse(auto-restored by libnats), JetStream pull subscriptions returntrue.In
StorageNATS::streamingToViewsFunc, snapshot the reconnect counter at entry. When it increments, run a newresubscribeConsumersOnReconnectthat selectivelyunsubscribe + subscribeonly for consumers that need it (JetStream), leaving working core subscriptions untouched.Read the counter before re-subscribing and store it after. If a reconnect happens during re-subscribe, the next iteration sees a fresh mismatch and re-subscribes again, guaranteeing eventual recovery.
Initialize the snapshot after the first successful subscribe in
initializeConsumersFuncso a reconnect that happens during init is not treated as a new event by the streaming task.A C++ unit test verifies the counter primitive (initial zero, stable across reads). The end-to-end re-subscribe path requires a live NATS cluster and is documented as the manual repro from the issue; per ClickHouse's history of flaky messaging integration tests (e.g. the RabbitMQ reconnect tests), no new flaky integration test is added.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes silent message loss in
ENGINE = NATSJetStream consumers after a NATS cluster failover. Previously, when the underlying connection reconnected to a new server, the JetStream subscription was silently dead and requiredDETACH TABLE/ATTACH TABLEto recover. The consumer now re-establishes its JetStream subscription automatically. Plain (non-JetStream) NATS subscriptions were already restored by the libnats client and are not affected.Documentation entry for user-facing changes