Skip to content

Commit 91c76ab

Browse files
authored
Merge pull request ClickHouse#78405 from jaymebrd/fail-fast-system-sync-replica-lightweight-from
fix: fail `system sync replica lightweight from foo` if replica foo doesn't exist
2 parents f17dd06 + 54a8629 commit 91c76ab

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/Storages/MergeTree/ReplicatedMergeTreeQueue.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace ErrorCodes
4242
extern const int LOGICAL_ERROR;
4343
extern const int UNEXPECTED_NODE_IN_ZOOKEEPER;
4444
extern const int ABORTED;
45+
extern const int BAD_ARGUMENTS;
4546
}
4647

4748

@@ -2424,10 +2425,26 @@ ReplicatedMergeTreeQueue::addSubscriber(ReplicatedMergeTreeQueue::SubscriberCall
24242425
std::unordered_set<String> existing_replicas;
24252426
if (!src_replicas.empty())
24262427
{
2427-
Strings unfiltered_hosts;
2428-
unfiltered_hosts = storage.getZooKeeper()->getChildren(zookeeper_path + "/replicas");
2428+
Strings unfiltered_hosts = storage.getZooKeeper()->getChildren(zookeeper_path + "/replicas");
2429+
existing_replicas.reserve(unfiltered_hosts.size());
2430+
24292431
for (const auto & host : unfiltered_hosts)
2430-
existing_replicas.insert(host);
2432+
existing_replicas.emplace(host);
2433+
2434+
for (const auto & requested_replica : src_replicas)
2435+
{
2436+
if (!existing_replicas.contains(requested_replica))
2437+
{
2438+
throw Exception(
2439+
ErrorCodes::BAD_ARGUMENTS,
2440+
"SYSTEM SYNC REPLICA FROM '{}' failed: replica does not exist. "
2441+
"Available replicas at '{}/replicas': {}",
2442+
requested_replica,
2443+
zookeeper_path,
2444+
fmt::join(unfiltered_hosts, ", ")
2445+
);
2446+
}
2447+
}
24312448
}
24322449

24332450
out_entry_names.reserve(queue.size());

tests/queries/0_stateless/02962_system_sync_replica_lightweight_from_modifier.reference

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Testing sync from non-existent replica...
2+
Error correctly detected for non-existent replica
13
Replication did not hang: synced all replicas of test_table_
24
Consistency: 1
35
Test completed

tests/queries/0_stateless/02962_system_sync_replica_lightweight_from_modifier.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# Tags: zookeeper, no-parallel, no-fasttest
2+
# Tags: zookeeper, no-parallel, no-fasttest, no-shared-merge-tree
33

44
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
55
# shellcheck source=../shell_config.sh
@@ -57,13 +57,33 @@ function mutations_thread() {
5757
done
5858
}
5959

60+
function consistency_table_sync_non_existent_replica() {
61+
echo "Testing sync from non-existent replica..."
62+
local NON_EXISTENT_REPLICA="non_existent_replica_$RANDOM"
63+
64+
SYNC_OUTPUT=$($CLICKHOUSE_CLIENT --query "SYSTEM SYNC REPLICA test_table_1 LIGHTWEIGHT FROM '$NON_EXISTENT_REPLICA'" 2>&1)
65+
EXIT_CODE=$?
66+
67+
if ! echo "$SYNC_OUTPUT" | grep -q "failed: replica does not exist"; then
68+
echo "FAILED: Error was not detected properly for non-existent replica"
69+
echo
70+
echo "Full output from SYSTEM SYNC command (exit code $EXIT_CODE):"
71+
echo
72+
echo "$SYNC_OUTPUT"
73+
exit 1
74+
fi
75+
76+
echo "Error correctly detected for non-existent replica"
77+
}
78+
6079
export -f insert_thread
6180
export -f sync_and_drop_replicas
6281
export -f optimize_thread
6382
export -f mutations_thread
6483

6584
TIMEOUT=30
6685

86+
consistency_table_sync_non_existent_replica
6787
timeout $TIMEOUT bash -c insert_thread 2> /dev/null &
6888
timeout $TIMEOUT bash -c sync_and_drop_replicas 2> /dev/null &
6989
timeout $TIMEOUT bash -c optimize_thread 2> /dev/null &

0 commit comments

Comments
 (0)