2018-07-02 19:34:15 +00:00
#!/usr/bin/env bash
2021-09-12 12:35:27 +00:00
# Tags: replica, no-replicated-database, no-parallel, no-fasttest
# Tag no-replicated-database: Old syntax is not allowed
2018-07-02 19:34:15 +00:00
CURDIR = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
2020-12-28 11:46:53 +00:00
# shellcheck source=../shell_config.sh
2020-08-01 00:51:12 +00:00
. " $CURDIR " /../shell_config.sh
2018-07-02 19:34:15 +00:00
2020-12-28 11:46:53 +00:00
# shellcheck source=./mergetree_mutations.lib
2020-08-01 00:52:41 +00:00
. " $CURDIR " /mergetree_mutations.lib
2018-08-01 19:01:58 +00:00
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE IF EXISTS mutations_r1"
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE IF EXISTS mutations_r2"
2018-07-02 19:34:15 +00:00
2021-03-15 04:51:28 +00:00
${ CLICKHOUSE_CLIENT } --query= " CREATE TABLE mutations_r1(d Date, x UInt32, s String, m MATERIALIZED x + 2) ENGINE ReplicatedMergeTree('/clickhouse/tables/ $CLICKHOUSE_TEST_ZOOKEEPER_PREFIX /mutations', 'r1', d, intDiv(x, 10), 8192) "
${ CLICKHOUSE_CLIENT } --query= " CREATE TABLE mutations_r2(d Date, x UInt32, s String, m MATERIALIZED x + 2) ENGINE ReplicatedMergeTree('/clickhouse/tables/ $CLICKHOUSE_TEST_ZOOKEEPER_PREFIX /mutations', 'r2', d, intDiv(x, 10), 8192) "
2018-07-02 19:34:15 +00:00
# Test a mutation on empty table
2020-09-28 11:17:07 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_r1 DELETE WHERE x = 1 SETTINGS mutations_sync = 2"
2018-07-02 19:34:15 +00:00
# Insert some data
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= " INSERT INTO mutations_r1(d, x, s) VALUES \
2018-07-05 14:19:33 +00:00
( '2000-01-01' , 1, 'a' ) "
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= " INSERT INTO mutations_r1(d, x, s) VALUES \
2018-07-02 19:34:15 +00:00
( '2000-01-01' , 2, 'b' ) , ( '2000-01-01' , 3, 'c' ) , ( '2000-01-01' , 4, 'd' ) \
( '2000-02-01' , 2, 'b' ) , ( '2000-02-01' , 3, 'c' ) , ( '2000-02-01' , 4, 'd' ) "
2018-07-05 14:19:33 +00:00
# Try some malformed queries that should fail validation.
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_r1 DELETE WHERE nonexistent = 0" 2>/dev/null || echo "Query should fail 1"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_r1 DELETE WHERE d = '11'" 2>/dev/null || echo "Query should fail 2"
2018-07-05 14:19:33 +00:00
2018-07-02 19:34:15 +00:00
# Delete some values
2020-10-21 09:32:12 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_r1 DELETE WHERE x % 2 = 1 SETTINGS mutations_sync = 2"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_r1 DELETE WHERE s = 'd' SETTINGS mutations_sync = 2"
2020-09-15 15:16:29 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_r1 DELETE WHERE m = 3 SETTINGS mutations_sync = 2"
2018-07-02 19:34:15 +00:00
# Insert more data
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= " INSERT INTO mutations_r1(d, x, s) VALUES \
2018-07-02 19:34:15 +00:00
( '2000-01-01' , 5, 'e' ) , ( '2000-02-01' , 5, 'e' ) "
2020-09-28 11:17:07 +00:00
${ CLICKHOUSE_CLIENT } --query "SYSTEM SYNC REPLICA mutations_r2"
2018-07-02 19:34:15 +00:00
# Check that the table contains only the data that should not be deleted.
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "SELECT d, x, s, m FROM mutations_r2 ORDER BY d, x"
2018-07-02 19:34:15 +00:00
# Check the contents of the system.mutations table.
${ CLICKHOUSE_CLIENT } --query= " SELECT mutation_id, command, block_numbers.partition_id, block_numbers.number, parts_to_do, is_done \
2021-08-07 10:02:30 +00:00
FROM system.mutations WHERE database = '$CLICKHOUSE_DATABASE' and table = 'mutations_r2' ORDER BY mutation_id"
2018-07-02 19:34:15 +00:00
2018-08-01 19:01:58 +00:00
${ CLICKHOUSE_CLIENT } --query= "SELECT '*** Test mutations cleaner ***'"
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE IF EXISTS mutations_cleaner_r1"
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE IF EXISTS mutations_cleaner_r2"
2018-08-01 19:01:58 +00:00
# Create 2 replicas with finished_mutations_to_keep = 2
2021-03-15 04:51:28 +00:00
${ CLICKHOUSE_CLIENT } --query= " CREATE TABLE mutations_cleaner_r1(x UInt32) ENGINE ReplicatedMergeTree('/clickhouse/tables/ $CLICKHOUSE_TEST_ZOOKEEPER_PREFIX /mutations_cleaner', 'r1') ORDER BY x SETTINGS \
2018-08-01 19:01:58 +00:00
finished_mutations_to_keep = 2,
cleanup_delay_period = 1,
cleanup_delay_period_random_add = 0"
2021-03-15 04:51:28 +00:00
${ CLICKHOUSE_CLIENT } --query= " CREATE TABLE mutations_cleaner_r2(x UInt32) ENGINE ReplicatedMergeTree('/clickhouse/tables/ $CLICKHOUSE_TEST_ZOOKEEPER_PREFIX /mutations_cleaner', 'r2') ORDER BY x SETTINGS \
2018-08-01 19:01:58 +00:00
finished_mutations_to_keep = 2,
cleanup_delay_period = 1,
cleanup_delay_period_random_add = 0"
# Insert some data
2021-10-26 11:06:09 +00:00
${ CLICKHOUSE_CLIENT } --query= "INSERT INTO mutations_cleaner_r1(x) VALUES (1), (2), (3), (4), (5)"
2018-08-01 19:01:58 +00:00
# Add some mutations and wait for their execution
2020-10-21 09:32:12 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_cleaner_r1 DELETE WHERE x = 1 SETTINGS mutations_sync = 2"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_cleaner_r1 DELETE WHERE x = 2 SETTINGS mutations_sync = 2"
2020-09-15 15:16:29 +00:00
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_cleaner_r1 DELETE WHERE x = 3 SETTINGS mutations_sync = 2"
2018-08-01 19:01:58 +00:00
# Add another mutation and prevent its execution on the second replica
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "SYSTEM STOP REPLICATION QUEUES mutations_cleaner_r2"
${ CLICKHOUSE_CLIENT } --query= "ALTER TABLE mutations_cleaner_r1 DELETE WHERE x = 4"
2018-08-01 19:01:58 +00:00
# Sleep for more than cleanup_delay_period
sleep 1.5
# Check that the first mutation is cleaned
2021-08-07 10:02:30 +00:00
${ CLICKHOUSE_CLIENT } --query= " SELECT mutation_id, command, is_done FROM system.mutations WHERE database = ' $CLICKHOUSE_DATABASE ' and table = 'mutations_cleaner_r2' ORDER BY mutation_id "
2018-08-01 19:01:58 +00:00
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE mutations_r1"
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE mutations_r2"
2018-08-01 19:01:58 +00:00
2019-04-16 14:13:13 +00:00
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE mutations_cleaner_r1"
${ CLICKHOUSE_CLIENT } --query= "DROP TABLE mutations_cleaner_r2"