Skip to content

Commit af438aa

Browse files
authored
Merge 8c005ad into 571cfe1
2 parents 571cfe1 + 8c005ad commit af438aa

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed

ydb/core/tx/schemeshard/common/validation.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "validation.h"
22

3+
#include <util/string/builder.h>
34
#include <yql/essentials/parser/pg_wrapper/interface/type_desc.h>
45

56
extern "C" {
@@ -59,10 +60,29 @@ bool TTTLValidator::ValidateUnit(const NScheme::TTypeInfo columnType, NKikimrSch
5960

6061
bool TTTLValidator::ValidateTiers(const NKikimrSchemeOp::TTTLSettings::TEnabled ttlSettings, TString& errStr) {
6162
for (ui64 i = 0; i < ttlSettings.TiersSize(); ++i) {
62-
if (ttlSettings.GetTiers(i).HasDelete() && i + 1 != ttlSettings.TiersSize()) {
63-
errStr = "Only the last tier in TTL settings can have Delete action";
63+
const auto& tier = ttlSettings.GetTiers(i);
64+
if (!tier.HasApplyAfterSeconds()) {
65+
errStr = "Missing ApplyAfterSeconds in a tier";
6466
return false;
6567
}
68+
if (i != 0 && tier.GetApplyAfterSeconds() <= ttlSettings.GetTiers(i - 1).GetApplyAfterSeconds()) {
69+
errStr = TStringBuilder() << "Tiers in the sequence must have increasing ApplyAfterSeconds: "
70+
<< ttlSettings.GetTiers(i - 1).GetApplyAfterSeconds() << " >= " << tier.GetApplyAfterSeconds();
71+
return false;
72+
}
73+
switch (tier.GetActionCase()) {
74+
case NKikimrSchemeOp::TTTLSettings_TTier::kDelete:
75+
if (i + 1 != ttlSettings.TiersSize()) {
76+
errStr = "Only the last tier in TTL settings can have Delete action";
77+
return false;
78+
}
79+
break;
80+
case NKikimrSchemeOp::TTTLSettings_TTier::kEvictToExternalStorage:
81+
break;
82+
case NKikimrSchemeOp::TTTLSettings_TTier::ACTION_NOT_SET:
83+
errStr = "Unset tier action";
84+
return false;
85+
}
6686
}
6787
return true;
6888
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <ydb/core/tx/schemeshard/common/validation.h>
2+
#include <ydb/core/tx/schemeshard/schemeshard_info_types.h>
3+
4+
#include <library/cpp/testing/unittest/registar.h>
5+
6+
using namespace NKikimr;
7+
using namespace NSchemeShard;
8+
9+
Y_UNIT_TEST_SUITE(TSchemeShardTTLUtility) {
10+
void TestValidateTiers(const std::vector<NKikimrSchemeOp::TTTLSettings::TTier>& tiers, const TConclusionStatus& expectedResult) {
11+
NKikimrSchemeOp::TTTLSettings::TEnabled input;
12+
for (const auto& tier : tiers) {
13+
*input.AddTiers() = tier;
14+
}
15+
16+
TString error;
17+
UNIT_ASSERT_VALUES_EQUAL(NValidation::TTTLValidator::ValidateTiers(input, error), expectedResult.IsSuccess());
18+
if (expectedResult.IsFail()) {
19+
UNIT_ASSERT_STRING_CONTAINS(error, expectedResult.GetErrorMessage());
20+
}
21+
}
22+
23+
Y_UNIT_TEST(ValidateTiers) {
24+
NKikimrSchemeOp::TTTLSettings::TTier tierNoAction;
25+
tierNoAction.SetApplyAfterSeconds(60);
26+
NKikimrSchemeOp::TTTLSettings::TTier tierNoDuration;
27+
tierNoDuration.MutableDelete();
28+
auto makeDeleteTier = [](const ui32 seconds) {
29+
NKikimrSchemeOp::TTTLSettings::TTier tier;
30+
tier.MutableDelete();
31+
tier.SetApplyAfterSeconds(seconds);
32+
return tier;
33+
};
34+
auto makeEvictTier = [](const ui32 seconds) {
35+
NKikimrSchemeOp::TTTLSettings::TTier tier;
36+
tier.MutableEvictToExternalStorage()->SetStorageName("/Root/abc");
37+
tier.SetApplyAfterSeconds(seconds);
38+
return tier;
39+
};
40+
41+
TestValidateTiers({ tierNoAction }, TConclusionStatus::Fail("Unset tier action"));
42+
TestValidateTiers({ tierNoDuration }, TConclusionStatus::Fail("Missing ApplyAfterSeconds in a tier"));
43+
TestValidateTiers({ makeDeleteTier(1) }, TConclusionStatus::Success());
44+
TestValidateTiers({ makeEvictTier(1) }, TConclusionStatus::Success());
45+
TestValidateTiers({ makeEvictTier(1), makeDeleteTier(2) }, TConclusionStatus::Success());
46+
TestValidateTiers({ makeEvictTier(1), makeEvictTier(2), makeDeleteTier(3) }, TConclusionStatus::Success());
47+
TestValidateTiers({ makeEvictTier(1), makeEvictTier(2) }, TConclusionStatus::Success());
48+
TestValidateTiers({ makeEvictTier(2), makeEvictTier(1) }, TConclusionStatus::Fail("Tiers in the sequence must have increasing ApplyAfterSeconds"));
49+
TestValidateTiers({ makeDeleteTier(1), makeEvictTier(2) }, TConclusionStatus::Fail("Only the last tier in TTL settings can have Delete action"));
50+
TestValidateTiers({ makeDeleteTier(1), makeDeleteTier(2) }, TConclusionStatus::Fail("Only the last tier in TTL settings can have Delete action"));
51+
}
52+
53+
void ValidateGetExpireAfter(const NKikimrSchemeOp::TTTLSettings::TEnabled& ttlSettings, const bool allowNonDeleteTiers, const TConclusion<TDuration>& expectedResult) {
54+
auto result = GetExpireAfter(ttlSettings, allowNonDeleteTiers);
55+
UNIT_ASSERT_VALUES_EQUAL(result.IsSuccess(), expectedResult.IsSuccess());
56+
if (expectedResult.IsFail()) {
57+
UNIT_ASSERT_STRING_CONTAINS(result.GetErrorMessage(), expectedResult.GetErrorMessage());
58+
}
59+
}
60+
61+
Y_UNIT_TEST(GetExpireAfter) {
62+
NKikimrSchemeOp::TTTLSettings::TTier evictTier;
63+
evictTier.MutableEvictToExternalStorage()->SetStorageName("/Root/abc");
64+
evictTier.SetApplyAfterSeconds(1800);
65+
NKikimrSchemeOp::TTTLSettings::TTier deleteTier;
66+
deleteTier.MutableDelete();
67+
deleteTier.SetApplyAfterSeconds(3600);
68+
69+
{
70+
NKikimrSchemeOp::TTTLSettings::TEnabled input;
71+
ValidateGetExpireAfter(input, true, TDuration::Zero());
72+
ValidateGetExpireAfter(input, false, TDuration::Zero());
73+
}
74+
{
75+
NKikimrSchemeOp::TTTLSettings::TEnabled input;
76+
input.SetExpireAfterSeconds(60);
77+
ValidateGetExpireAfter(input, true, TDuration::Seconds(60));
78+
ValidateGetExpireAfter(input, false, TDuration::Seconds(60));
79+
}
80+
{
81+
NKikimrSchemeOp::TTTLSettings::TEnabled input;
82+
*input.AddTiers() = deleteTier;
83+
ValidateGetExpireAfter(input, true, TDuration::Seconds(3600));
84+
ValidateGetExpireAfter(input, false, TDuration::Seconds(3600));
85+
}
86+
{
87+
NKikimrSchemeOp::TTTLSettings::TEnabled input;
88+
*input.AddTiers() = evictTier;
89+
*input.AddTiers() = deleteTier;
90+
ValidateGetExpireAfter(input, true, TDuration::Seconds(3600));
91+
ValidateGetExpireAfter(input, false, TConclusionStatus::Fail("Only DELETE via TTL is allowed for row-oriented tables"));
92+
}
93+
{
94+
NKikimrSchemeOp::TTTLSettings::TEnabled input;
95+
*input.AddTiers() = evictTier;
96+
ValidateGetExpireAfter(input, true, TConclusionStatus::Fail("TTL settings does not contain DELETE action"));
97+
ValidateGetExpireAfter(input, false, TConclusionStatus::Fail("Only DELETE via TTL is allowed for row-oriented tables"));
98+
}
99+
}
100+
}

ydb/core/tx/schemeshard/ut_ttl/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PEERDIR(
2222

2323
SRCS(
2424
ut_ttl.cpp
25+
ut_ttl_utility.cpp
2526
)
2627

2728
YQL_LAST_ABI_VERSION()

0 commit comments

Comments
 (0)