Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/en/antalya/cas/architecture/replication.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,15 @@ different entries throws.

Manifests are therefore per-ref and never moved: a detach creates a new manifest for
`detached/<part>` 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.
1 change: 1 addition & 0 deletions src/Core/ServerUUID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
68 changes: 68 additions & 0 deletions src/Core/tests/gtest_server_uuid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <gtest/gtest.h>

#include <Core/ServerUUID.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <Common/Logger.h>

#include <filesystem>
#include <fstream>

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<UUID>(expected_str);
auto * log = &Poco::Logger::root();

for (const auto & [suffix, payload] : std::vector<std::pair<std::string, std::string>>{
{"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<UUID>(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);
}
35 changes: 33 additions & 2 deletions src/Storages/MergeTree/ReplicatedMergeTreeMergeStrategyPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down Expand Up @@ -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)
Expand Down