|
| 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 | +} |
0 commit comments