Skip to content

Revert "Resume a NATS JetStream subscription after a reconnect" - #119270

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
Algunenano:revert-nats-jetstream-reconnect-resume
Sep 10, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
Algunenano:revert-nats-jetstream-reconnect-resume

Conversation

@Algunenano

@Algunenano Algunenano commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Reverts #115343.

The revert also undoes the three follow-up commits that only adjust the tests that pull request added (2abc75f9c49, 8551a53f2d5, 9eb7ad13b55).

The NATS TLS certificate work that landed on top of the same files is kept. src/Storages/NATS/NATSConnection.cpp keeps its ErrorCodes block and #include <Common/Exception.h>, which #115343 introduced and loadNATSCertificates/validateNATSCertificates now need.

Related: #115343

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/119270]

Version info

  • Merged into: 26.9.1.1156 (included in 26.9 and later)

This reverts the changes of ClickHouse#115343 (merge commit
5564667), together with the three
follow-up commits that only adjust the tests it added:
2abc75f, 8551a53 and 9eb7ad1.

The NATS TLS certificate work that landed on top of the same files is
kept: `src/Storages/NATS/NATSConnection.cpp` keeps its `ErrorCodes`
block and `#include <Common/Exception.h>`, which the reverted commit had
introduced and the certificate code now needs.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [16ecb3c]


AI Review

Summary

This PR reverts the JetStream reconnect-resume work from #115343, but the revert is broader than the reconnect-count logic itself. It also drops two follow-up protections that kept JetStream's at-least-once behavior intact around view teardown and buffered backlog handling, so I do not think it is safe to merge as-is.

Findings

❌ Blockers

  • [src/Storages/NATS/StorageNATS.cpp:843] streamToViews no longer re-checks checkDependencies() after InterpreterInsertQuery::execute(). InsertDependenciesBuilder::createChainWithDependencies() falls back to NullSinkToStorage when skip_destination_table is true and the last ready dependent view disappears in that gap, but the code below still creates NATSSources and ackConsumed() afterwards. A DROP VIEW or DETACH TABLE that lands there can therefore acknowledge a whole nats_max_block_size batch that reached no table. Suggested fix: restore the post-execute() readiness check before any source is created.

⚠️ Majors

  • [src/Storages/NATS/StorageNATS.cpp:413] unsubscribeConsumers() now always clears the JetStream queue after draining. A closed subscription can still have a large local backlog, and dropBuffered() destroys the only natsMsg handles for those deliveries without natsMsg_Nak. The broker keeps them in AckPending until AckWait expires, so a graceful restart, SYSTEM STOP, or last-view drop can leave the view short for minutes even though the rows were already buffered locally. Suggested fix: restore the old "wait for the backlog to drain or return it before destroying the subscription" behavior; dropBuffered() is only safe once the subscription is already gone.
Final Verdict

Changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 10, 2026
/// view is gone, and a materialized view whose target is `Null` legitimately ends in the same
/// discarding sink while being a view the table streams to. The check can only err towards
/// skipping a cycle whose pipeline would have inserted somewhere, which consumes nothing.
if (!checkDependencies(table_id))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InterpreterInsertQuery::execute() can still build a NullSinkToStorage chain here when skip_destination_table is true and the last ready dependent view disappears in the gap between threadFunc()'s pre-check and this lookup (see InsertDependenciesBuilder::createChainWithDependencies()). The code below then still creates NATSSources and unconditionally ackConsumed() at the end of streamToViews(), so a DROP VIEW or DETACH TABLE in that window can acknowledge a full nats_max_block_size batch that reached no table.

We still need the post-execute() checkDependencies(table_id) guard that this revert removed, or some equivalent check before any source is created.

consumer->finishAndReturnUnprocessed(INATSConsumer::SkippedMessages::Acknowledge);
consumer->unsubscribe();
consumer->unsubscribe(/*finish_queue=*/true);
consumer->dropBuffered();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This puts the JetStream backlog-loss path back on every unsubscribe/recovery. A closed pull subscription does not imply an empty local queue: if the broker restarts while the consumer is behind, onMsg may already have a large backlog of MessageData::msg handles buffered here. Clearing the queue after unsubscribe(/*finish_queue=*/true) destroys those handles without natsMsg_Nak, so the broker keeps the rows in AckPending until AckWait expires.

That means a graceful restart, SYSTEM STOP, or last-view drop can leave the view short for minutes even though the rows were already buffered locally. dropBuffered() is only safe once the subscription is already gone; this path still needs the old "wait for the backlog to drain or return it before destroying the subscription" behavior.

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

Labels

comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants