Merge pull request #37197 from ClickHouse/overcommit-ratio-overflow

Fix possible overflow in OvercommitRatio
This commit is contained in:
Dmitry Novik 2022-05-15 15:09:19 +02:00 committed by GitHub
commit 2259add1fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -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

View File

@ -0,0 +1,26 @@
#!/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
for _ in {1..10};
do
clickhouse_client_loop_timeout 10 query &
done
wait
$CLICKHOUSE_CLIENT -q 'DROP USER IF EXISTS u02294'