Add check for growing age AND precision in rollup config

This commit is contained in:
Mikhail f. Shiryaev 2021-03-06 13:28:21 +01:00
parent 6a6bc3924d
commit efc04b29a6
No known key found for this signature in database
GPG Key ID: 39CD5753BD5D856F
3 changed files with 61 additions and 2 deletions

View File

@ -60,6 +60,27 @@ static Names extractColumnNames(const ASTPtr & node)
} }
} }
/** Is used to order Graphite::Retentions by age and precision descending.
* Throws exception if not both age and precision are less or greater then another.
*/
static bool compareRetentions(const Graphite::Retention & a, const Graphite::Retention & b)
{
if (a.age > b.age && a.precision > b.precision)
{
return true;
}
else if (a.age < b.age && a.precision < b.precision)
{
return false;
}
String error_msg = "age and precision should only grow up: "
+ std::to_string(a.age) + ":" + std::to_string(a.precision) + " vs "
+ std::to_string(b.age) + ":" + std::to_string(b.precision);
throw Exception(
error_msg,
ErrorCodes::BAD_ARGUMENTS);
}
/** Read the settings for Graphite rollup from config. /** Read the settings for Graphite rollup from config.
* Example * Example
* *
@ -157,8 +178,7 @@ appendGraphitePattern(const Poco::Util::AbstractConfiguration & config, const St
/// retention should be in descending order of age. /// retention should be in descending order of age.
if (pattern.type & pattern.TypeRetention) /// TypeRetention or TypeAll if (pattern.type & pattern.TypeRetention) /// TypeRetention or TypeAll
std::sort(pattern.retentions.begin(), pattern.retentions.end(), std::sort(pattern.retentions.begin(), pattern.retentions.end(), compareRetentions);
[] (const Graphite::Retention & a, const Graphite::Retention & b) { return a.age > b.age; });
patterns.emplace_back(pattern); patterns.emplace_back(pattern);
} }

View File

@ -94,4 +94,25 @@
</retention> </retention>
</default> </default>
</graphite_rollup_broken> </graphite_rollup_broken>
<graphite_rollup_wrong_age_precision>
<path_column_name>metric</path_column_name>
<time_column_name>timestamp</time_column_name>
<value_column_name>value</value_column_name>
<version_column_name>updated</version_column_name>
<default>
<function>avg</function>
<retention>
<age>0</age>
<precision>60</precision>
</retention>
<retention>
<age>36000</age>
<precision>600</precision>
</retention>
<retention>
<age>72000</age>
<precision>300</precision>
</retention>
</default>
</graphite_rollup_wrong_age_precision>
</yandex> </yandex>

View File

@ -3,6 +3,7 @@ import os.path as p
import time import time
import pytest import pytest
from helpers.client import QueryRuntimeException
from helpers.cluster import ClickHouseCluster from helpers.cluster import ClickHouseCluster
from helpers.test_tools import TSV from helpers.test_tools import TSV
@ -442,3 +443,20 @@ SELECT * FROM test.graphite;
''') ''')
assert TSV(result) == TSV(expected) assert TSV(result) == TSV(expected)
def test_wrong_rollup_config(graphite_table):
with pytest.raises(QueryRuntimeException) as exc:
q('''
CREATE TABLE test.graphite_not_created
(metric String, value Float64, timestamp UInt32, date Date, updated UInt32)
ENGINE = GraphiteMergeTree('graphite_rollup_wrong_age_precision')
PARTITION BY toYYYYMM(date)
ORDER BY (metric, timestamp)
SETTINGS index_granularity=1;
''')
# The order of retentions is not guaranteed
assert ("age and precision should only grow up: " in str(exc.value))
assert ("36000:600" in str(exc.value))
assert ("72000:300" in str(exc.value))