From ce7b1666d9b3beee165acd76c41047746c49bd6c Mon Sep 17 00:00:00 2001 From: sabith Date: Wed, 18 Nov 2026 12:25:35 -0800 Subject: [PATCH] fix(cas,mergetree): volume-aware CAS single-replica merges and whitespace-tolerant ServerUUID::load 1. In ReplicatedMergeTreeMergeStrategyPicker, populate remote_fs_execute_merges_on_single_replica_time_threshold when the table's storage policy includes a content-addressed (CAS) disk, and use it in shouldMergeOnSingleReplica() when all source parts of the merge reside on a content-addressed or zero-copy remote disk. On multi-volume local-hot / CAS-cold storage policies, this keeps local disk merges independent of remote_fs_execute_merges_on_single_replica_time_threshold while coordinating cold CAS merges on a single replica for CA_RELINK_COOKIE relinking. 2. In loadServerUUID (ServerUUID.cpp), skip trailing whitespace before assertEOF() so newline-terminated /var/lib/clickhouse/uuid files do not fail with CANNOT_PARSE_INPUT_ASSERTION_FAILED and get overwritten with a newly generated random UUIDv4. 3. Add unit test coverage in src/Core/tests/gtest_server_uuid.cpp and document single-replica merge coordination for mixed local/CAS policies in docs/en/antalya/cas/architecture/replication.md. Signed-off-by: sabith --- .../antalya/cas/architecture/replication.md | 12 ++++ src/Core/ServerUUID.cpp | 1 + src/Core/tests/gtest_server_uuid.cpp | 68 +++++++++++++++++++ ...ReplicatedMergeTreeMergeStrategyPicker.cpp | 35 +++++++++- 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 src/Core/tests/gtest_server_uuid.cpp diff --git a/docs/en/antalya/cas/architecture/replication.md b/docs/en/antalya/cas/architecture/replication.md index 43ec9947d5028..8619adc3d304b 100644 --- a/docs/en/antalya/cas/architecture/replication.md +++ b/docs/en/antalya/cas/architecture/replication.md @@ -163,3 +163,15 @@ different entries throws. Manifests are therefore per-ref and never moved: a detach creates a new manifest for `detached/` and retires the old one, and the blobs' net in-degree is unchanged. + +## Single-replica merge coordination {#single-replica-merges} + +When `remote_fs_execute_merges_on_single_replica_time_threshold` is positive on a table whose +storage policy includes a content-addressed disk, `ReplicatedMergeTreeMergeStrategyPicker` applies +that threshold only to `MERGE_PARTS` entries whose source parts all reside on a content-addressed +(or zero-copy) disk. The designated replica executes the merge and publishes the resulting manifest +to the shared pool; peer replicas fetch the merged part by relink (`CA_RELINK_COOKIE`) without +re-reading or re-merging the source blobs. On a multi-volume policy pairing a local hot disk with a +content-addressed cold disk, merges whose source parts still reside on the local hot volume continue +to follow `execute_merges_on_single_replica_time_threshold` (`0` by default), so local hot merges +are not delayed waiting on a remote peer. diff --git a/src/Core/ServerUUID.cpp b/src/Core/ServerUUID.cpp index 7851bd96f0c3e..b49d5f0ffaa9a 100644 --- a/src/Core/ServerUUID.cpp +++ b/src/Core/ServerUUID.cpp @@ -44,6 +44,7 @@ UUID loadServerUUID(const fs::path & server_uuid_file, Poco::Logger * log) UUID uuid; ReadBufferFromFile in(server_uuid_file); readUUIDText(uuid, in); + skipWhitespaceIfAny(in); assertEOF(in); return uuid; } diff --git a/src/Core/tests/gtest_server_uuid.cpp b/src/Core/tests/gtest_server_uuid.cpp new file mode 100644 index 0000000000000..f92a9d9dc84cf --- /dev/null +++ b/src/Core/tests/gtest_server_uuid.cpp @@ -0,0 +1,68 @@ +#include + +#include +#include +#include +#include + +#include +#include + +namespace fs = std::filesystem; +using namespace DB; + +namespace +{ +fs::path tempUUIDPath(const std::string & suffix) +{ + return fs::temp_directory_path() / ("clickhouse_gtest_server_uuid_" + suffix); +} +} + +TEST(ServerUUID, LoadsExact36ByteAndNewlineTerminatedUUID) +{ + const std::string expected_str = "12345678-1234-5234-8234-123456789012"; + const UUID expected = parse(expected_str); + auto * log = &Poco::Logger::root(); + + for (const auto & [suffix, payload] : std::vector>{ + {"exact", expected_str}, + {"lf", expected_str + "\n"}, + {"crlf", expected_str + "\r\n"}, + }) + { + fs::path path = tempUUIDPath(suffix); + { + std::ofstream out(path, std::ios::binary | std::ios::trunc); + out << payload; + } + SCOPE_EXIT({ + std::error_code ec; + fs::remove(path, ec); + }); + + const UUID loaded = loadServerUUID(path, log); + EXPECT_EQ(loaded, expected) << "failed for suffix: " << suffix; + } +} + +TEST(ServerUUID, OverwritesCorruptedTrailingPayloadWithFreshUUID) +{ + const std::string base_str = "12345678-1234-5234-8234-123456789012"; + const UUID base_uuid = parse(base_str); + auto * log = &Poco::Logger::root(); + + fs::path path = tempUUIDPath("corrupt"); + { + std::ofstream out(path, std::ios::binary | std::ios::trunc); + out << base_str << " trailing-garbage"; + } + SCOPE_EXIT({ + std::error_code ec; + fs::remove(path, ec); + }); + + const UUID loaded = loadServerUUID(path, log); + EXPECT_NE(loaded, UUIDHelpers::Nil); + EXPECT_NE(loaded, base_uuid); +} diff --git a/src/Storages/MergeTree/ReplicatedMergeTreeMergeStrategyPicker.cpp b/src/Storages/MergeTree/ReplicatedMergeTreeMergeStrategyPicker.cpp index 1b7fc2f21fc95..2b731ceac806e 100644 --- a/src/Storages/MergeTree/ReplicatedMergeTreeMergeStrategyPicker.cpp +++ b/src/Storages/MergeTree/ReplicatedMergeTreeMergeStrategyPicker.cpp @@ -56,10 +56,32 @@ bool ReplicatedMergeTreeMergeStrategyPicker::isMergeFinishedByReplica(const Stri bool ReplicatedMergeTreeMergeStrategyPicker::shouldMergeOnSingleReplica(const ReplicatedMergeTreeLogEntryData & entry) const { + if (entry.type != ReplicatedMergeTreeLogEntry::MERGE_PARTS) + return false; + time_t threshold = execute_merges_on_single_replica_time_threshold; + time_t remote_threshold = remote_fs_execute_merges_on_single_replica_time_threshold; + + if (remote_threshold > threshold && !entry.source_parts.empty()) + { + bool all_parts_remote_or_cas = true; + for (const auto & name : entry.source_parts) + { + auto part = storage.getPartIfExists( + name, + {MergeTreeDataPartState::PreActive, MergeTreeDataPartState::Active, MergeTreeDataPartState::Outdated}); + if (!part || (!part->getDataPartStorage().isContentAddressed() && !part->getDataPartStorage().supportZeroCopyReplication())) + { + all_parts_remote_or_cas = false; + break; + } + } + if (all_parts_remote_or_cas) + threshold = remote_threshold; + } + return ( threshold > 0 /// feature turned on - && entry.type == ReplicatedMergeTreeLogEntry::MERGE_PARTS /// it is a merge log entry && entry.create_time + threshold > time(nullptr) /// not too much time waited ); } @@ -101,7 +123,16 @@ void ReplicatedMergeTreeMergeStrategyPicker::refreshState() const auto settings = storage.getSettings(); time_t threshold = (*settings)[MergeTreeSetting::execute_merges_on_single_replica_time_threshold].totalSeconds(); time_t threshold_init = 0; - if ((*settings)[MergeTreeSetting::allow_remote_fs_zero_copy_replication]) + bool has_cas_disk = false; + for (const auto & disk : storage.getDisks()) + { + if (disk && disk->isContentAddressed()) + { + has_cas_disk = true; + break; + } + } + if ((*settings)[MergeTreeSetting::allow_remote_fs_zero_copy_replication] || has_cas_disk) threshold_init = (*settings)[MergeTreeSetting::remote_fs_execute_merges_on_single_replica_time_threshold].totalSeconds(); if (threshold == 0)