ClickHouse/tests/queries/0_stateless/transactions.lib

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.6 KiB
Plaintext
Raw Normal View History

2021-11-17 18:14:14 +00:00
#!/usr/bin/env bash
2021-12-22 16:34:02 +00:00
# shellcheck disable=SC2015
# Useful to run queries in parallel sessions
2021-11-17 18:14:14 +00:00
function tx()
{
tx_num=$1
query=$2
2021-12-20 18:53:05 +00:00
session="${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}_tx$tx_num"
query_id="${session}_${RANDOM}"
2021-11-17 18:14:14 +00:00
url_without_session="https://${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT_HTTPS}/?"
2021-12-20 18:53:05 +00:00
url="${url_without_session}session_id=$session&query_id=$query_id&database=$CLICKHOUSE_DATABASE"
2021-11-17 18:14:14 +00:00
${CLICKHOUSE_CURL} -m 90 -sSk "$url" --data "$query" | sed "s/^/tx$tx_num\t/"
2021-11-17 18:14:14 +00:00
}
2021-12-22 16:34:02 +00:00
# Waits for the last query in session to finish
2021-12-20 18:53:05 +00:00
function tx_wait() {
tx_num=$1
session="${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}_tx$tx_num"
2021-12-22 16:34:02 +00:00
# try get pid of previous query
query_pid=""
tmp_file_name="${CLICKHOUSE_TMP}/tmp_tx_${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}"
query_id_and_pid=$(grep -F "$session" "$tmp_file_name" 2>/dev/null | tail -1) ||:
read -r query_id query_pid <<< "$query_id_and_pid" ||:
2021-12-20 18:53:05 +00:00
# wait for previous query in transaction
2021-12-22 16:34:02 +00:00
if [ -n "$query_pid" ]; then
timeout 5 tail --pid=$query_pid -f /dev/null && return ||:
fi
# there is no pid (or maybe we got wrong one), so wait using system.processes (it's less reliable)
2021-12-20 18:53:05 +00:00
count=0
while [[ $($CLICKHOUSE_CLIENT -q "SELECT count() FROM system.processes WHERE query_id LIKE '$session%'") -gt 0 ]]; do
sleep 0.5
count=$((count+1))
if [ "$count" -gt 120 ]; then
echo "timeout while waiting for $tx_num"
break
fi
done;
}
2021-12-22 16:34:02 +00:00
# Wait for previous query in session to finish, starts new one asynchronously
2021-12-20 18:53:05 +00:00
function tx_async()
{
tx_num=$1
query=$2
2021-12-24 13:14:17 +00:00
tx_wait "$tx_num"
2021-12-20 18:53:05 +00:00
session="${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}_tx$tx_num"
query_id="${session}_${RANDOM}"
url_without_session="https://${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT_HTTPS}/?"
url="${url_without_session}session_id=$session&query_id=$query_id&database=$CLICKHOUSE_DATABASE"
2021-12-22 16:34:02 +00:00
# We cannot be sure that query will actually start execution and appear in system.processes before the next call to tx_wait
# Also we cannot use global map in bash to store last query_id for each tx_num, so we use tmp file...
tmp_file_name="${CLICKHOUSE_TMP}/tmp_tx_${CLICKHOUSE_TEST_ZOOKEEPER_PREFIX}"
2021-12-20 18:53:05 +00:00
# run query asynchronously
${CLICKHOUSE_CURL} -m 90 -sSk "$url" --data "$query" | sed "s/^/tx$tx_num\t/" &
2021-12-22 16:34:02 +00:00
query_pid=$!
echo -e "$query_id\t$query_pid" >> "$tmp_file_name"
2021-12-20 18:53:05 +00:00
}
2021-12-23 19:02:27 +00:00
# Wait for previous query in session to finish, execute the next one synchronously
function tx_sync()
{
tx_num=$1
query=$2
2021-12-24 13:14:17 +00:00
tx_wait "$tx_num"
tx "$tx_num" "$query"
2021-12-23 19:02:27 +00:00
}
2023-08-18 09:17:32 +00:00
# vi: ft=bash