Skip to content

Fix "Part <...> does not contain in snapshot of previous virtual parts. (PART_IS_TEMPORARILY_LOCKED)" during DETACH PART - #76039

Merged
tavplubix merged 5 commits into
ClickHouse:masterfrom
aalexfvk:fix_drop_detached_failure
Apr 3, 2025
Merged

tavplubix merged 5 commits into
ClickHouse:masterfrom
aalexfvk:fix_drop_detached_failure

Conversation

@aalexfvk

@aalexfvk aalexfvk commented Feb 13, 2025 •

Copy link
Copy Markdown
Contributor

The race condition leading to the error below can be easily reproduced for the test lines:

INSERT INTO test1 SELECT toDate('2023-01-01') + toIntervalDay(number), number + 1000 from system.numbers limit 20
ALTER TABLE test1 DETACH PART 'all_0_0_0'"

By adding sleep in any place of ReplicatedMergeTreeQueue::pullLogsToQueue() before this line.

Error:

2025.02.11 11:50:40.000593 [ 9 ] {061d749d-1ae0-4dca-8b71-50f2ce366ddf} <Debug> executeQuery: (from 172.16.1.1:62162) (query 1, line 1) ALTER TABLE test1 DETACH PART 'all_0_0_0' (stage: Complete)
2025.02.11 11:50:40.000742 [ 9 ] {061d749d-1ae0-4dca-8b71-50f2ce366ddf} <Test> CancellationChecker: Did not add the task because the timeout is 0. Query: ALTER TABLE test1 DETACH PART 'all_0_0_0'
2025.02.11 11:50:40.001213 [ 9 ] {061d749d-1ae0-4dca-8b71-50f2ce366ddf} <Trace> default.test1 (240a67b9-3ffc-44e0-8c18-1e9ad21281f8): Will try to insert a log entry to DROP_PART for part all_0_0_0
2025.02.11 11:50:40.234026 [ 27 ] {} <Trace> default.test1 (ReplicatedMergeTreeQueue): Insert entry queue-0000000000 to queue with type GET_PART with virtual parts [all_0_0_0]
2025.02.11 11:50:40.234112 [ 27 ] {} <Test> default.test1 (ReplicatedMergeTreeQueue): Adding part all_0_0_0 to mutations
2025.02.11 11:50:40.234152 [ 27 ] {} <Debug> default.test1 (ReplicatedMergeTreeQueue): Pulled 1 entries to queue.
2025.02.11 11:50:40.234186 [ 27 ] {} <Debug> default.test1 (240a67b9-3ffc-44e0-8c18-1e9ad21281f8): Updating strategy picker state
2025.02.11 11:50:40.235400 [ 27 ] {} <Trace> default.test1 (StorageReplicatedMergeTree::queueUpdatingTask): Execution took 507 ms.
2025.02.11 11:50:40.236458 [ 603 ] {} <Test> default.test1 (ReplicatedMergeTreeQueue): Removing successful entry queue-0000000000 from queue with type GET_PART with virtual parts [all_0_0_0]
2025.02.11 11:50:40.236580 [ 603 ] {} <Test> default.test1 (ReplicatedMergeTreeQueue): Adding parts [all_0_0_0] to current parts
2025.02.11 11:50:40.236664 [ 603 ] {} <Test> default.test1 (ReplicatedMergeTreeQueue): Removing part all_0_0_0 from mutations (remove_part: false, remove_covered_parts: true)
2025.02.11 11:50:40.351288 [ 9 ] {061d749d-1ae0-4dca-8b71-50f2ce366ddf} <Error> executeQuery: Code: 384. DB::Exception: Part all_0_0_0 does not contain in snapshot of previous virtual parts. (PART_IS_TEMPORARILY_LOCKED) (version 25.2.1.1812 (official build)) (from 172.16.1.1:62162) (query 1, line 1) (in query: ALTER TABLE test1 DETACH PART 'all_0_0_0'), Stack trace (when copying this message, always include the lines below):

0. ./contrib/llvm-project/libcxx/include/__exception/exception.h:106: Poco::Exception::Exception(String const&, int) @ 0x0000000021eae400
1. ./build_docker/./src/Common/Exception.cpp:106: DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x00000000103f8e18
2. ./src/Common/Exception.h:104: DB::Exception::Exception(PreformattedMessage const&, int) @ 0x00000000135d543e
3. ./build_docker/./src/Storages/StorageReplicatedMergeTree.cpp:9248: DB::StorageReplicatedMergeTree::dropPartImpl(std::shared_ptr<zkutil::ZooKeeper>&, String, DB::ReplicatedMergeTreeLogEntry&, bool, bool) @ 0x000000001c0cb589
4. ./build_docker/./src/Storages/StorageReplicatedMergeTree.cpp:6863: DB::StorageReplicatedMergeTree::dropPart(String const&, bool, std::shared_ptr<DB::Context const>) @ 0x000000001c0cc0ee
5. ./build_docker/./src/Storages/MergeTree/MergeTreeData.cpp:5630: DB::MergeTreeData::alterPartition(std::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::vector<DB::PartitionCommand, std::allocator<DB::PartitionCommand>> const&, std::shared_ptr<DB::Context const>) @ 0x000000001c7026da
6. ./build_docker/./src/Interpreters/InterpreterAlterQuery.cpp:245: DB::InterpreterAlterQuery::executeToTable(DB::ASTAlterQuery const&) @ 0x00000000197afea3
7. ./build_docker/./src/Interpreters/InterpreterAlterQuery.cpp:80: DB::InterpreterAlterQuery::execute() @ 0x00000000197ad0a3
8. ./build_docker/./src/Interpreters/executeQuery.cpp:1455: DB::executeQueryImpl(char const*, char const*, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum, DB::ReadBuffer*, std::shared_ptr<DB::IAST>&) @ 0x000000001a17e8a9
9. ./build_docker/./src/Interpreters/executeQuery.cpp:1622: DB::executeQuery(String const&, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum) @ 0x000000001a1795ce
10. ./build_docker/./src/Server/TCPHandler.cpp:662: DB::TCPHandler::runImpl() @ 0x000000001cf7bf05
11. ./build_docker/./src/Server/TCPHandler.cpp:2625: DB::TCPHandler::run() @ 0x000000001cfa2c88
12. ./build_docker/./base/poco/Net/src/TCPServerConnection.cpp:40: Poco::Net::TCPServerConnection::start() @ 0x0000000021fc4503
13. ./build_docker/./base/poco/Net/src/TCPServerDispatcher.cpp:115: Poco::Net::TCPServerDispatcher::run() @ 0x0000000021fc4d72
14. ./build_docker/./base/poco/Foundation/src/ThreadPool.cpp:205: Poco::PooledThread::run() @ 0x0000000021f3dc43
15. ./build_docker/./base/poco/Foundation/src/Thread.cpp:45: Poco::(anonymous namespace)::RunnableHolder::run() @ 0x0000000021f3bf50
16. ./base/poco/Foundation/src/Thread_POSIX.cpp:335: Poco::ThreadImpl::runnableEntry(void*) @ 0x0000000021f3a30a
17. __tsan_thread_start_func @ 0x0000000007f43428
18. ? @ 0x00007f17b8a53ac3
19. ? @ 0x00007f17b8ae5850

Failure report

Race condition takes place between creating of merge predicate (actually saving prev_virtual_parts) and the pulling of new log entries to queue with subsequent updating queue.virtual_parts.

I implemented the simplest fix without conditional logic for now.
Alternatives would be pulling preliminary only for some checking (for example, for dropPart) or use virtual_parts instead of prev_virtual_parts while working DistributedMergePredicate::CanUsePartInMerges() -> checkCanMergePartsPreconditions() link

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 to CHANGELOG.md):

Fix "Part <...> does not contain in snapshot of previous virtual parts. (PART_IS_TEMPORARILY_LOCKED)" during DETACH PART

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Feb 14, 2025
@clickhouse-gh

clickhouse-gh Bot commented Feb 14, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f364900]

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Feb 14, 2025
@filimonov filimonov mentioned this pull request Mar 12, 2025
1 task
@SmitaRKulkarni SmitaRKulkarni self-assigned this Mar 18, 2025
@SmitaRKulkarni

Copy link
Copy Markdown
Member

Unrelated failures :
Stateless tests (release, old analyzer, s3, DatabaseReplicated, 2/2) : 02125_many_mutations - fails in master link

Stateless tests (tsan, 3/3) - 00156_max_execution_speed_sample_merge - Fixed in #77346

@azat

azat commented Mar 22, 2025 •

Copy link
Copy Markdown
Member

It can be easily covered with a test

create table test1 (p Date, k Int) engine=ReplicatedMergeTree('/tables/t1', 'r1') order by (p) partition by k;
system stop pulling replication log test1;
INSERT INTO test1 SELECT toDate('2023-01-01') + toIntervalDay(number), number + 1000 from system.numbers limit 20;
system start pulling replication log test1;
ALTER TABLE test1 DETACH PART '1019_0_0_0';
-- Code: 384. DB::Exception: Received from localhost:9000. DB::Exception: Part 1019_0_0_0 does not contain in snapshot of previous virtual parts. (PART_IS_TEMPORARILY_LOCKED)
ALTER TABLE test1 DETACH PART '1019_0_0_0';
-- Ok

@aalexfvk let's commit this test (maybe with some cleanup)

@azat azat self-assigned this Mar 22, 2025
/// in the list of virtual parts during pulling queue.
/// The second call to pullLogsToQueue() below is used for preserving the invariant,
/// see DistributedMergePredicate
queue_.pullLogsToQueue(zookeeper, {}, ReplicatedMergeTreeQueue::MERGE_PREDICATE);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like pulling queue 2x time for every merge should be OK (since the queue should not be big), but maybe @tavplubix see some problems here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We want to ensure that the predicate uses the most recent state of the queue

Actually we do not really want this and it's fine to use a slightly outdated set of virtual parts here, at least for merges, which is the main usecase for merge predicate. And for DROP PART, the error is retriable, so it's a minor issue. If we still want to avoid this error, it would be better to pull logs before constructing merge pred in dropPartImpl, so we will not have to think about potential overhead for merges. Yes, the overhead is not big, but looks like we can easily avoid it for merges

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks reasonable, upload the new version concerning only dropPartImpl()

auto-merge was automatically disabled March 24, 2025 20:46

Head branch was pushed to by a user without write access

@azat

azat commented Mar 26, 2025

Copy link
Copy Markdown
Member

Stress test (asan) — Possible deadlock on shutdown (see gdb.log)
Stress test (msan) — Possible deadlock on shutdown (see gdb.log)

Stress test (debug) — Killed by signal (in clickhouse-server.log)

Stress test (ubsan) — Possible deadlock on shutdown (see gdb.log)

@azat
azat added this pull request to the merge queue Mar 26, 2025
@azat
azat removed this pull request from the merge queue due to a manual request Mar 26, 2025
@aalexfvk

Copy link
Copy Markdown
Contributor Author

@azat Are we waiting for solving issues you linked to ? Or should I focus on some of them

@azat

azat commented Mar 31, 2025

Copy link
Copy Markdown
Member

Fixing something that failed on CI is always welcome! But, it is not a requirement for this PR to be merged.

From my side looks good now (after the pulling has been removed from generic code).
@tavplubix @SmitaRKulkarni any objections on this one?

@tavplubix
tavplubix added this pull request to the merge queue Apr 3, 2025
Merged via the queue into ClickHouse:master with commit a6014b5 Apr 3, 2025
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 3, 2025
jh0x pushed a commit to jh0x/ClickHouse that referenced this pull request Nov 6, 2026
…redicate

A part inserted on a replica enters that replica's `queue.virtual_parts`
only when the replica pulls the part's own GET_PART log entry, which the
background `queueUpdatingTask` does asynchronously. `movePartitionToShard`
builds a `ReplicatedMergeTreeZooKeeperMergePredicate`, whose constructor
snapshots `virtual_parts` into `prev_virtual_parts` before it pulls the log,
and `canUsePartInMerges` rejects a part missing from that snapshot. So
`ALTER TABLE ... MOVE PART ... TO SHARD` issued right after an INSERT on the
same replica fails with "Part ... does not contain in snapshot of previous
virtual parts. (PART_IS_TEMPORARILY_LOCKED)" while the background pull is
still in flight. On the macOS Fast test runner the client round trips are
short enough to hit this window, which makes
05495_part_moves_between_shards_step_retry time out.

`dropPartImpl` got the same fix in ClickHouse#76039 (pull the log with
MERGE_PREDICATE before `getMergePredicate`); `movePartitionToShard`, whose
check block is a copy of `dropPart`'s, never did. Port that call.

The new test stops log pulling, inserts, then restarts pulling and moves
the part in one client call, so the insert's entry is not in the queue when
the MOVE starts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 pr-bugfix Pull request with bugfix, not backported by default 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.

6 participants