Skip to content

Re-subscribe NATS JetStream pull consumers after a reconnect - #103557

Open
Krishnachaitanyakc wants to merge 1 commit into
ClickHouse:masterfrom
Krishnachaitanyakc:fix-96651-pr
Open

Krishnachaitanyakc wants to merge 1 commit into
ClickHouse:masterfrom
Krishnachaitanyakc:fix-96651-pr

Conversation

@Krishnachaitanyakc

Copy link
Copy Markdown
Contributor

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 plain Subscribe and QueueSubscribe subscriptions, but it does not restore JetStream pull subscriptions (js_PullSubscribeAsync), which are driven by application-level PUB $JS.API.CONSUMER.MSG.NEXT round-trips.

Result: after a NATS failover, JetStream consumers in StorageNATS are silently dead — the underlying connection reports as healthy and streamingToViewsFunc keeps looping, but no messages are ever delivered. The only workaround is DETACH TABLE followed by ATTACH TABLE, which re-runs subscribe and re-issues the JetStream pull.

Approach

  • 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.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixes silent message loss in ENGINE = NATS JetStream consumers after a NATS cluster failover. Previously, when the underlying connection reconnected to a new server, the JetStream subscription was silently dead and required DETACH TABLE / ATTACH TABLE to 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

  • Documentation is written (mandatory for new features)

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
Krishnachaitanyakc marked this pull request as ready for review April 27, 2026 18:07
@Krishnachaitanyakc

Krishnachaitanyakc commented May 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@Michicosun @yariks5s can you please review this?

@abashkeev abashkeev added the comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). label Sep 4, 2026
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 14, 2026
@clickhouse-gh clickhouse-gh Bot closed this Nov 14, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Nov 14, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). external Author is not a member of the ClickHouse organization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NATS JetStream pull consumer does not recover after failover (or change server)

4 participants