Skip to content

LockGuard that can measure time - #88898

Merged
nikitamikhaylov merged 5 commits into
masterfrom
traceable_locks
Nov 4, 2025
Merged

nikitamikhaylov merged 5 commits into
masterfrom
traceable_locks

Conversation

@nikitamikhaylov

@nikitamikhaylov nikitamikhaylov commented Oct 22, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Introduced a LockGuardWithStopWatch class and used it in the background pool for executing merges. In case a mutex was held for a second or some thread was struggling to get it within a second a warning message will be printed.
Moved the heavy code from the destructor of MergeMutateSelectedEntry to finalize() method to avoid holding the lock in MergeTreeBackground executor for too long.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [90fdca8]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan, old analyzer, 4/6) failure
test_replicated_database_recover_digest_mismatch/test.py::test_recover_digest_mismatch FAIL cidb
Upgrade check (amd_tsan) failure
Killed by signal (in clickhouse-server.log) FAIL cidb
Fatal message in clickhouse-server.log (see fatal_messages.txt) FAIL cidb
Killed by signal (output files) FAIL cidb
Found signal in gdb.log FAIL cidb
Upgrade check (amd_debug) failure
Killed by signal (in clickhouse-server.log) FAIL cidb
Fatal message in clickhouse-server.log (see fatal_messages.txt) FAIL cidb
Killed by signal (output files) FAIL cidb
Found signal in gdb.log FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 22, 2025
@thevar1able

This comment has been minimized.

@nikitamikhaylov
nikitamikhaylov force-pushed the traceable_locks branch 2 times, most recently from e798e0d to 622ace4 Compare October 24, 2025 13:00
@nikitamikhaylov

Copy link
Copy Markdown
Member Author

That's too much:

2025-10-25 01:33:28 Reason: having stderror:  
2025-10-25 01:33:28 [6ee9df80e0f4] 2025.10.24 06:33:20.522512 [ 730070 ] {5c4b034c-d568-474d-9edb-4bceb4050cbe} <Warning> MergeTreeBackgroundExecutor: Lock acquisition took 2391 ms for thread 730070 in [void DB::MergeTreeBackgroundExecutor<DB::DynamicRuntimeQueue>::removeTasksCorrespondingToStorage(StorageID) [Queue = DB::DynamicRuntimeQueue]], Stack trace (when copying this message, always include the lines below): 
2025-10-25 01:33:28  0. ./ci/tmp/build/./src/Common/StackTrace.cpp:395: StackTrace::StackTrace() @ 0x000000000ecc93d4
2025-10-25 01:33:28 1. ./src/Common/LockGuardWithStopWatch.h:39: DB::LockGuardWithStopWatch::~LockGuardWithStopWatch() @ 0x00000000169c5354
2025-10-25 01:33:28 2. ./ci/tmp/build/./src/Storages/MergeTree/MergeTreeBackgroundExecutor.cpp:220: DB::MergeTreeBackgroundExecutor<DB::DynamicRuntimeQueue>::removeTasksCorrespondingToStorage(DB::StorageID) @ 0x00000000169ca380
2025-10-25 01:33:28 3. ./ci/tmp/build/./src/Storages/MergeTree/BackgroundJobsAssignee.cpp:130: DB::BackgroundJobsAssignee::finish() @ 0x00000000168e82a4
2025-10-25 01:33:28 4. ./ci/tmp/build/./src/Storages/StorageMergeTree.cpp:254: DB::StorageMergeTree::shutdown(bool) @ 0x00000000169d0734
2025-10-25 01:33:28 5. ./src/Storages/IStorage.h:589: DB::InterpreterDropQuery::executeToTableImpl(std::shared_ptr<DB::Context const> const&, DB::ASTDropQuery&, std::shared_ptr<DB::IDatabase>&, StrongTypedef<wide::integer<128ul, unsigned int>, DB::UUIDTag>&) @ 0x0000000013ef412c
2025-10-25 01:33:28 6. ./ci/tmp/build/./src/Interpreters/InterpreterDropQuery.cpp:122: DB::InterpreterDropQuery::executeToTable(DB::ASTDropQuery&) @ 0x0000000013ef2e98
2025-10-25 01:33:28 7. ./ci/tmp/build/./src/Interpreters/InterpreterDropQuery.cpp:76: DB::InterpreterDropQuery::execute() @ 0x0000000013ef21e0
2025-10-25 01:33:28 8. ./ci/tmp/build/./src/Interpreters/executeQuery.cpp:1603: DB::executeQueryImpl(char const*, char const*, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum, std::unique_ptr<DB::ReadBuffer, std::default_delete<DB::ReadBuffer>>&, std::shared_ptr<DB::IAST>&, std::shared_ptr<DB::ImplicitTransactionControlExecutor>, std::function<void ()>) @ 0x0000000014234e90
2025-10-25 01:33:28 9. ./ci/tmp/build/./src/Interpreters/executeQuery.cpp:1812: DB::executeQuery(String const&, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum) @ 0x0000000014230340
2025-10-25 01:33:28 10. ./ci/tmp/build/./src/Server/TCPHandler.cpp:744: DB::TCPHandler::runImpl() @ 0x0000000017060c54
2025-10-25 01:33:28 11. ./ci/tmp/build/./src/Server/TCPHandler.cpp:2818: DB::TCPHandler::run() @ 0x0000000017079a64
2025-10-25 01:33:28 12. ./ci/tmp/build/./base/poco/Net/src/TCPServerConnection.cpp:40: Poco::Net::TCPServerConnection::start() @ 0x000000001b87a018
2025-10-25 01:33:28 13. ./ci/tmp/build/./base/poco/Net/src/TCPServerDispatcher.cpp:115: Poco::Net::TCPServerDispatcher::run() @ 0x000000001b87a638
2025-10-25 01:33:28 14. ./ci/tmp/build/./base/poco/Foundation/src/ThreadPool.cpp:205: Poco::PooledThread::run() @ 0x000000001b8169b8
2025-10-25 01:33:28 15. ./base/poco/Foundation/src/Thread_POSIX.cpp:341: Poco::ThreadImpl::runnableEntry(void*) @ 0x000000001b814014
2025-10-25 01:33:28 16. ? @ 0x000000000007d5b8
2025-10-25 01:33:28 17. ? @ 0x00000000000e5edc

@nikitamikhaylov

nikitamikhaylov commented Oct 26, 2025 •

Copy link
Copy Markdown
Member Author

Now it's clear what takes time -- MergeMutateSelectedEntry

2025.10.26 18:58:45.075598 [ 2377 ] {} <Debug> DeleteMarker: future_part marker destroyed
2025.10.26 18:58:45.075600 [ 2377 ] {} <Debug> DeleteMarker: table_lock_holder marker destroyed
2025.10.26 18:58:45.075614 [ 2377 ] {} <Debug> DeleteMarker: merge_mutate_entry marker destroyed
2025.10.26 18:58:47.048866 [ 2377 ] {} <Debug> DeleteMarker: metadata_snapshot marker destroyed
2025.10.26 18:58:47.048879 [ 2377 ] {} <Debug> DeleteMarker: storage marker destroyed
2025.10.26 18:58:47.048899 [ 2377 ] {} <Debug> DeleteMarker: state marker destroyed
2025.10.26 18:58:47.048914 [ 2377 ] {} <Warning> MergeTreeBackgroundExecutor: Releasing background task runtime data took 1.973 seconds (> 1s), executor=MergeMutate, storage=test_3el2gxr5.dedup_test (22f293e6-4ad0-47bd-a2ad-a18699a4dd7b), query_id=22f293e6-4ad0-47bd-a2ad-a18699a4dd7b::1_445_501_1, deleting=false
2025.10.26 18:58:47.051675 [ 2377 ] {} <Warning> MergeTreeBackgroundExecutor: Lock was held for 1973 ms by thread 2377 in [auto DB::MergeTreeBackgroundExecutor<DB::DynamicRuntimeQueue>::routine(TaskRuntimeDataPtr)::(anonymous class)::operator()(TaskRuntimeDataPtr &&) const [Queue = DB::DynamicRuntimeQueue]], Stack trace (when copying this message, always include the lines below):

@nikitamikhaylov

Copy link
Copy Markdown
Member Author

More precisely, it is CurrentlyMergingPartsTagger::~CurrentlyMergingPartsTagger() where the acquisition of currently_processing_in_background_mutex takes too long.

Comment thread src/Common/LockGuardWithStopWatch.h Resolved
@nikitamikhaylov

Copy link
Copy Markdown
Member Author

I think we can merge this PR right away.

Comment thread src/Common/LockGuardWithStopWatch.h Outdated
Comment on lines +12 to +16
#ifndef NDEBUG
static constexpr auto THRESHOLD_MILLISECONDS = 1000;
#else
static constexpr auto THRESHOLD_MILLISECONDS = 10000;
#endif

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.

It means 1s for debug build and 10s for other builds

@nikitamikhaylov

Copy link
Copy Markdown
Member Author

I removed the backport labels, let it live in master. Not sure about how good the checks are.

@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue Nov 4, 2025
Merged via the queue into master with commit 5915b51 Nov 4, 2025
@nikitamikhaylov
nikitamikhaylov deleted the traceable_locks branch November 4, 2025 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

4 participants