From 45585f0e7f1b97c27ca9790a1cfc2f508caaf843 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Fri, 13 May 2022 15:39:05 +0000 Subject: [PATCH 1/2] Fix possible overflow in OvercommitRatio --- src/Common/OvercommitTracker.h | 8 +++-- .../02294_overcommit_overflow.reference | 0 .../0_stateless/02294_overcommit_overflow.sh | 31 +++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/02294_overcommit_overflow.reference create mode 100755 tests/queries/0_stateless/02294_overcommit_overflow.sh diff --git a/src/Common/OvercommitTracker.h b/src/Common/OvercommitTracker.h index b4de6074bbb..fec52b261a7 100644 --- a/src/Common/OvercommitTracker.h +++ b/src/Common/OvercommitTracker.h @@ -20,10 +20,12 @@ struct OvercommitRatio friend bool operator<(OvercommitRatio const & lhs, OvercommitRatio const & rhs) noexcept { + Int128 lhs_committed = lhs.committed, lhs_soft_limit = lhs.soft_limit; + Int128 rhs_committed = rhs.committed, rhs_soft_limit = rhs.soft_limit; // (a / b < c / d) <=> (a * d < c * b) - return (lhs.committed * rhs.soft_limit) < (rhs.committed * lhs.soft_limit) - || (lhs.soft_limit == 0 && rhs.soft_limit > 0) - || (lhs.committed == 0 && rhs.committed == 0 && lhs.soft_limit > rhs.soft_limit); + return (lhs_committed * rhs_soft_limit) < (rhs_committed * lhs_soft_limit) + || (lhs_soft_limit == 0 && rhs_soft_limit > 0) + || (lhs_committed == 0 && rhs_committed == 0 && lhs_soft_limit > rhs_soft_limit); } // actual query memory usage diff --git a/tests/queries/0_stateless/02294_overcommit_overflow.reference b/tests/queries/0_stateless/02294_overcommit_overflow.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/02294_overcommit_overflow.sh b/tests/queries/0_stateless/02294_overcommit_overflow.sh new file mode 100755 index 00000000000..4f49afdf9af --- /dev/null +++ b/tests/queries/0_stateless/02294_overcommit_overflow.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# Tags: no-parallel + +CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +# shellcheck source=../shell_config.sh +. "$CURDIR"/../shell_config.sh + +$CLICKHOUSE_CLIENT -q 'DROP USER IF EXISTS u02294' +$CLICKHOUSE_CLIENT -q 'CREATE USER IF NOT EXISTS u02294 IDENTIFIED WITH no_password' +$CLICKHOUSE_CLIENT -q 'GRANT ALL ON *.* TO u02294' + +function query() +{ + $CLICKHOUSE_CLIENT -u u02294 -q 'SELECT number FROM numbers(130000) GROUP BY number SETTINGS max_memory_usage_for_user=5000000,memory_overcommit_ratio_denominator=2000000000000000000,memory_usage_overcommit_max_wait_microseconds=500' >/dev/null 2>/dev/null +} + +export -f query + +function user_test() +{ + for _ in {1..10}; + do + clickhouse_client_loop_timeout 10 query & + done + + wait +} + +output=$(user_test) + +$CLICKHOUSE_CLIENT -q 'DROP USER IF EXISTS u02294' From 75c5ae31f792afc46f395a28f5c8801a2bcbeb67 Mon Sep 17 00:00:00 2001 From: Dmitry Novik Date: Fri, 13 May 2022 16:27:52 +0000 Subject: [PATCH 2/2] Fix shellcheck --- .../0_stateless/02294_overcommit_overflow.sh | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/queries/0_stateless/02294_overcommit_overflow.sh b/tests/queries/0_stateless/02294_overcommit_overflow.sh index 4f49afdf9af..64a4dddc67f 100755 --- a/tests/queries/0_stateless/02294_overcommit_overflow.sh +++ b/tests/queries/0_stateless/02294_overcommit_overflow.sh @@ -16,16 +16,11 @@ function query() export -f query -function user_test() -{ - for _ in {1..10}; - do - clickhouse_client_loop_timeout 10 query & - done +for _ in {1..10}; +do + clickhouse_client_loop_timeout 10 query & +done - wait -} - -output=$(user_test) +wait $CLICKHOUSE_CLIENT -q 'DROP USER IF EXISTS u02294'