Repository navigation
Fix DROP TABLE hang on Kafka tables due to unbounded rd_kafka_assign wait - #101158
alexey-milovidov wants to merge 33 commits into
Conversation
…gn wait When a Kafka consumer experiences a heartbeat error followed by a rebalance (e.g. after broker becomes temporarily unreachable), DROP TABLE can hang indefinitely. The root cause is inside librdkafka: rd_kafka_assign(), called from the rebalance callback during poll_batch(), sends an op to the consumer-group thread and waits with RD_POLL_INFINITE. If the cgrp thread is blocked waiting for the coordinator, poll_batch() never returns, making pipeline cancellation impossible regardless of the cancel callback. This updates both contrib/librdkafka and contrib/cppkafka: - librdkafka: replace RD_POLL_INFINITE with 30s timeout in rd_kafka_assign0 (ClickHouse/librdkafka#18) - cppkafka: catch HandleException from assign/unassign in handle_rebalance to prevent throwing from a C callback (ClickHouse/cppkafka#7) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [a428f19] Summary: ⏳
AI ReviewSummaryThis PR updates only Findings❌ Blockers
ClickHouse Rules
Performance & Safety
Final Verdict
|
Address review feedback: catch `std::exception` and `...` in addition to `HandleException` in `handle_rebalance`, since this function runs from a C callback where any escaping exception is undefined behavior. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
56239a9 to
4a70e1c
Compare
|
The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
This is interesting. The code in cppkafka is good. The changes in librdkafka are a little bit risky (30 sec timeout), but we can see how it goes.
|
Make the PR in ClickHouse/librdkafka#18 based on the branch, that we currently use. It should be one of |
|
Retargeted ClickHouse/librdkafka#18 to The companion cppkafka change is already merged: ClickHouse/cppkafka#7. |
|
After we merged both PRs for submodules and one more, update the submodules in this PR. |
Bump `contrib/cppkafka` and `contrib/librdkafka` to the heads of their ClickHouse release branches now that the dependent fork PRs are merged: - cppkafka: bump to merge of ClickHouse/cppkafka#7 (Catch exceptions from `assign`/`unassign` in `handle_rebalance`). - librdkafka: bump to merge of ClickHouse/librdkafka#18 (Use bounded timeout in `rd_kafka_assign0` instead of `RD_POLL_INFINITE`) and ClickHouse/librdkafka#17 (Fix use-after-free in consumer lag timer). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Workaround: gh token lacks workflow scope so changes to .github/workflows/* are rejected on push. Revert these to the pre-merge content; they will be picked up again when master is re-merged through a path that has workflow scope. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
CI bisect of the persistent kafka-test failures on this PR: Symptom: every recent run has these timing out at 600 s on
On master vs PR (CIDB, last 30 days):
Bisect via timestamps:
The original 30 s Just refreshed the master merge — next CI run will be against today's master, but the underlying fix is in |
Restore `.github/workflows/create_release.yml` and `.github/workflows/vectorsearchstress.yml` to byte-identical content of `origin/master` so the `Check Workflows` step in `Config Workflow` no longer reports them as outdated. The previous restore-to-PR-tip workaround left the YAMLs stale relative to master, which made the check fail. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
| @@ -1 +1 @@ | |||
| Subproject commit cd9a80837602fe0f97c146beff752e705f40cb0a | |||
| Subproject commit ae43f47aa7a3bb82bbc4ab3a336d542923fa6ae8 | |||
There was a problem hiding this comment.
The librdkafka pointer here includes e6ba87c7 and 61aa9022. On this PR branch, CI bisect shows those two commits correlate with persistent Kafka integration timeouts after 2026-04-29, while the earlier timeout fix (54df05ab) was green for weeks.
Please avoid advancing to ae43f47 until the follow-up in ClickHouse/librdkafka resolves that regression; otherwise this PR carries a known blocker for Kafka tests.
`LimitTransform::makeChunkWithPreviousRow` asserts `row < chunk.getNumRows()` and is called from `preparePair` with `data.current_chunk.getNumRows() - 1` whenever `with_ties && rows_read == offset + limit`. This silently assumes the current chunk has at least one row. The read-in-order pipeline violates that assumption. With settings `optimize_read_in_order = 1`, `read_in_order_use_virtual_row = 1` and `read_in_order_use_virtual_row_per_block = 1`, `VirtualRowTransform` emits zero-row chunks carrying `MergeTreeReadInfo` as sort-key markers for `MergingSortedTransform`. In the single-stream case there is no merging, so `RemoveVirtualRowTransform` is inserted to clean up — but it only stripped the `ChunkInfo` and left the empty chunk on the pipeline. When such a chunk arrived at `LimitTransform` after the previous data chunk had pushed `rows_read` to exactly `offset + limit`, the call became `makeChunkWithPreviousRow(empty_chunk, 0 - 1)` and the `UInt64` underflow tripped the assertion (server abort in debug builds; surfaces as a `LOGICAL_ERROR` in release builds). The AST fuzzer hit this on master across multiple unrelated PRs (#99495, #104268, #101158) over the past 30 days. Minimal reproducer: CREATE TABLE t (id UInt64, m Map(String, String), INDEX idx_mk mapKeys(m) TYPE bloom_filter GRANULARITY 1) ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 1; INSERT INTO t VALUES (1, {'1':'1'}); INSERT INTO t VALUES (2, {'2':'2'}); INSERT INTO t VALUES (3, {'3':'3'}); SELECT id FROM t PREWHERE mapContains(m, toFixedString('1', 1)) ORDER BY id ASC LIMIT 1 WITH TIES SETTINGS optimize_read_in_order = 1, read_in_order_use_virtual_row = 1, read_in_order_use_virtual_row_per_block = 1; Two fixes: 1. `LimitTransform.cpp`: skip the `makeChunkWithPreviousRow` call when the current chunk has zero rows. There is no row to remember and the `previous_row_chunk` saved from the prior non-empty chunk is still the correct tie key for the boundary. 2. `RemoveVirtualRowTransform` (`SortingStep.cpp`): actually consume the virtual-row chunk — `chunk.clear()` it and enable `skip_empty_chunks` on the base `ISimpleTransform` so it is not re-emitted. This matches the existing comment ("we need to remove virtual row before output") and protects other downstream transforms from receiving these pointless markers. Regression test `04247_99495_limit_with_ties_virtual_row_empty_chunk` aborts master in debug builds and passes after this change. CI report: https://s3.amazonaws.com/clickhouse-test-reports/PRs/99495/b699481fd40feb67d515e47725de159f20dd1ff0/ast_fuzzer_amd_debug/ Related: #99495 (sighting PR)
`LimitTransform::makeChunkWithPreviousRow` asserts `row < chunk.getNumRows()` and is called from `preparePair` with `data.current_chunk.getNumRows() - 1` whenever `with_ties && rows_read == offset + limit`. This silently assumes the current chunk has at least one row. The read-in-order pipeline violates that assumption. With settings `optimize_read_in_order = 1`, `read_in_order_use_virtual_row = 1` and `read_in_order_use_virtual_row_per_block = 1`, `VirtualRowTransform` emits zero-row chunks carrying `MergeTreeReadInfo` as sort-key markers for `MergingSortedTransform`. In the single-stream case there is no merging, so `RemoveVirtualRowTransform` is inserted to clean up — but it only stripped the `ChunkInfo` and left the empty chunk on the pipeline. When such a chunk arrived at `LimitTransform` after the previous data chunk had pushed `rows_read` to exactly `offset + limit`, the call became `makeChunkWithPreviousRow(empty_chunk, 0 - 1)` and the `UInt64` underflow tripped the assertion (server abort in debug builds; surfaces as a `LOGICAL_ERROR` in release builds). The AST fuzzer hit this on master across multiple unrelated PRs (ClickHouse#99495, ClickHouse#104268, ClickHouse#101158) over the past 30 days. Minimal reproducer: CREATE TABLE t (id UInt64, m Map(String, String), INDEX idx_mk mapKeys(m) TYPE bloom_filter GRANULARITY 1) ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 1; INSERT INTO t VALUES (1, {'1':'1'}); INSERT INTO t VALUES (2, {'2':'2'}); INSERT INTO t VALUES (3, {'3':'3'}); SELECT id FROM t PREWHERE mapContains(m, toFixedString('1', 1)) ORDER BY id ASC LIMIT 1 WITH TIES SETTINGS optimize_read_in_order = 1, read_in_order_use_virtual_row = 1, read_in_order_use_virtual_row_per_block = 1; Two fixes: 1. `LimitTransform.cpp`: skip the `makeChunkWithPreviousRow` call when the current chunk has zero rows. There is no row to remember and the `previous_row_chunk` saved from the prior non-empty chunk is still the correct tie key for the boundary. 2. `RemoveVirtualRowTransform` (`SortingStep.cpp`): actually consume the virtual-row chunk — `chunk.clear()` it and enable `skip_empty_chunks` on the base `ISimpleTransform` so it is not re-emitted. This matches the existing comment ("we need to remove virtual row before output") and protects other downstream transforms from receiving these pointless markers. Regression test `04247_99495_limit_with_ties_virtual_row_empty_chunk` aborts master in debug builds and passes after this change. CI report: https://s3.amazonaws.com/clickhouse-test-reports/PRs/99495/b699481fd40feb67d515e47725de159f20dd1ff0/ast_fuzzer_amd_debug/ Related: ClickHouse#99495 (sighting PR)
Fix
DROP TABLEhang on Kafka tables when a rebalance is in progress and the broker is temporarily unreachable.The root cause is inside librdkafka:
rd_kafka_assign, called from the rebalance callback duringpoll_batch, sends anRD_KAFKA_OP_ASSIGNop to the consumer-group background thread and waits withRD_POLL_INFINITEviard_kafka_op_req→rd_kafka_q_pop. If the cgrp thread is blocked (e.g. waiting for a coordinator while the broker is unreachable after a network interruption),poll_batchnever returns — regardless of its own timeout — because the block happens inside a callback dispatched during queue serving. This makes pipeline cancellation impossible, causingDROP TABLEto hang for 600+ seconds until the client times out.Updates both
contrib/librdkafkaandcontrib/cppkafka:RD_POLL_INFINITEwith 30s timeout inrd_kafka_assign0HandleException,std::exception, and...fromassign/unassigninhandle_rebalanceto prevent any exception from escaping the C callback; route errors through existingrebalance_error_callback_This was identified as the cause of frequent Kafka integration test failures in CI (cascade failures in
test_batch_fastdue totest_kafka_consumer_hangtiming out).Same hang stack trace as in the issue closed by #100604, which only addressed the consumer-destructor path; the broader fix here also handles the unbounded wait during normal
poll_batch.Refs:
Existing test coverage:
test_kafka_consumer_hangintests/integration/test_storage_kafka/test_batch_fast.pyexercises this exact code path (heartbeat error →REBALANCE_IN_PROGRESS→DROP TABLE).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
DROP TABLEhang on Kafka tables when a rebalance coincides with broker unavailability, caused by an unbounded wait inside librdkafka'srd_kafka_assign.Documentation entry for user-facing changes