ClickHouse/tests/queries/0_stateless/01019_alter_materialized_view_consistent.sh

84 lines
2.4 KiB
Bash
Raw Normal View History

2020-01-31 17:12:18 +00:00
#!/usr/bin/env bash
set -e
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
2020-01-31 17:12:18 +00:00
$CLICKHOUSE_CLIENT --multiquery <<EOF
DROP TABLE IF EXISTS src_a;
DROP TABLE IF EXISTS src_b;
DROP TABLE IF EXISTS mv;
2020-05-12 23:55:16 +00:00
CREATE TABLE src_a (v UInt64) ENGINE = Null;
CREATE TABLE src_b (v UInt64) ENGINE = Null;
2020-01-31 17:12:18 +00:00
2020-05-12 23:55:16 +00:00
CREATE MATERIALIZED VIEW mv (test UInt8, case UInt8)
2020-01-31 17:12:18 +00:00
Engine = MergeTree()
ORDER BY test AS
SELECT v == 1 as test, v as case FROM src_a;
EOF
# The purpose of this test is to ensure that MV query A is always used against source table A. Same for query/table B.
# Also, helps detect data races.
function insert_thread() {
2020-05-12 23:55:16 +00:00
# Always wait for all background INSERTs to finish to catch stuck queries.
trap 'wait; exit;' INT
INSERT[0]="INSERT INTO TABLE src_a VALUES (1);"
INSERT[1]="INSERT INTO TABLE src_b VALUES (2);"
while true; do
2020-05-14 12:45:42 +00:00
# trigger 50 concurrent inserts at a time
2020-08-01 00:40:56 +00:00
for _ in {0..50}; do
2020-05-12 23:55:16 +00:00
# ignore `Possible deadlock avoided. Client should retry`
$CLICKHOUSE_CLIENT -q "${INSERT[$RANDOM % 2]}" 2>/dev/null &
done
wait
2020-05-14 12:45:42 +00:00
is_done=$($CLICKHOUSE_CLIENT -q "SELECT countIf(case = 1) > 0 AND countIf(case = 2) > 0 FROM mv;")
if [ "$is_done" -eq "1" ]; then
break
fi
2020-01-31 17:12:18 +00:00
done
}
function alter_thread() {
2020-05-12 23:55:16 +00:00
trap 'exit' INT
2020-01-31 17:12:18 +00:00
2020-05-12 23:55:16 +00:00
ALTER[0]="ALTER TABLE mv MODIFY QUERY SELECT v == 1 as test, v as case FROM src_a;"
ALTER[1]="ALTER TABLE mv MODIFY QUERY SELECT v == 2 as test, v as case FROM src_b;"
2020-01-31 17:12:18 +00:00
2020-05-12 23:55:16 +00:00
while true; do
$CLICKHOUSE_CLIENT --allow_experimental_alter_materialized_view_structure=1 \
-q "${ALTER[$RANDOM % 2]}"
sleep "0.0$RANDOM"
2020-05-14 12:45:42 +00:00
is_done=$($CLICKHOUSE_CLIENT -q "SELECT countIf(case = 1) > 0 AND countIf(case = 2) > 0 FROM mv;")
if [ "$is_done" -eq "1" ]; then
break
fi
2020-05-12 23:55:16 +00:00
done
2020-01-31 17:12:18 +00:00
}
2020-05-12 23:55:16 +00:00
export -f insert_thread;
export -f alter_thread;
2020-01-31 17:12:18 +00:00
2020-05-14 12:45:42 +00:00
# finishes much faster with all builds, except debug with coverage
timeout 120 bash -c insert_thread &
timeout 120 bash -c alter_thread &
2020-01-31 17:12:18 +00:00
wait
2020-05-14 12:45:42 +00:00
$CLICKHOUSE_CLIENT -q "SELECT countIf(case = 1) > 0 AND countIf(case = 2) > 0 FROM mv LIMIT 1;"
2020-01-31 17:12:18 +00:00
$CLICKHOUSE_CLIENT -q "SELECT 'inconsistencies', count() FROM mv WHERE test == 0;"
$CLICKHOUSE_CLIENT -q "DROP VIEW mv"
$CLICKHOUSE_CLIENT -q "DROP TABLE src_a"
$CLICKHOUSE_CLIENT -q "DROP TABLE src_b"