2021-08-10 13:34:40 +00:00
#!/usr/bin/env bash
# shellcheck source=./mergetree_mutations.lib
. "$CURDIR"/mergetree_mutations.lib
2021-08-11 15:24:47 +00:00
function try_sync_replicas()
2021-08-10 13:34:40 +00:00
{
readarray -t tables_arr < <(${CLICKHOUSE_CLIENT} --query="SELECT name FROM system.tables WHERE database=currentDatabase() AND name like '$1%' AND engine like '%Replicated%'")
for t in "${tables_arr[@]}"
do
# The size of log may be big, so increase timeout.
2021-08-12 10:39:06 +00:00
$CLICKHOUSE_CLIENT --receive_timeout 400 -q "SYSTEM SYNC REPLICA $t" || $CLICKHOUSE_CLIENT -q \
"select 'sync failed, queue:', * from system.replication_queue where database=currentDatabase() and table='$t'" &
2021-08-10 13:34:40 +00:00
done
wait
echo "Replication did not hang: synced all replicas of $1"
}
function check_replication_consistency()
{
2021-08-11 15:24:47 +00:00
# Forcefully cancel mutations to avoid waiting for them to finish
${CLICKHOUSE_CLIENT} --query="KILL MUTATION WHERE database=currentDatabase() AND table like '$1%'" > /dev/null
2021-08-10 13:34:40 +00:00
# SYNC REPLICA is not enough if some MUTATE_PARTs are not assigned yet
wait_for_all_mutations "$1%"
2021-08-11 15:24:47 +00:00
try_sync_replicas "$1"
2021-08-12 10:39:06 +00:00
res=$($CLICKHOUSE_CLIENT -q \
2021-08-10 13:34:40 +00:00
"SELECT
2021-08-12 15:58:49 +00:00
if((countDistinct(data) as c) == 0, 1, c)
2021-08-10 13:34:40 +00:00
FROM
(
SELECT _table, ($2) AS data
FROM merge(currentDatabase(), '$1') GROUP BY _table
2021-08-12 10:39:06 +00:00
)")
echo "Consistency: $res"
if [ $res -ne 1 ]; then
echo "Replicas have diverged:"
$CLICKHOUSE_CLIENT -q "select 'data', _table, $2, arraySort(groupArrayDistinct(_part)) from merge(currentDatabase(), '$1') group by _table"
$CLICKHOUSE_CLIENT -q "select 'queue', * from system.replication_queue where database=currentDatabase() and table like '$1%'"
$CLICKHOUSE_CLIENT -q "select 'mutations', * from system.mutations where database=currentDatabase() and table like '$1%'"
$CLICKHOUSE_CLIENT -q "select 'parts', * from system.parts where database=currentDatabase() and table like '$1%'"
echo "Good luck with debugging..."
2021-08-11 15:24:47 +00:00
fi
2021-08-10 13:34:40 +00:00
}