Skip to content

Commit 225bab2

Browse files
authored
add tiering info to TTL in public api (#11390)
1 parent f54668a commit 225bab2

File tree

22 files changed

+700
-161
lines changed

22 files changed

+700
-161
lines changed

ydb/core/grpc_services/rpc_log_store.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -509,29 +509,8 @@ class TDescribeLogTableRPC : public TRpcSchemeRequestActor<TDescribeLogTableRPC,
509509
}
510510

511511
if (tableDescription.HasTtlSettings() && tableDescription.GetTtlSettings().HasEnabled()) {
512-
const auto& inTTL = tableDescription.GetTtlSettings().GetEnabled();
513-
514-
switch (inTTL.GetColumnUnit()) {
515-
case NKikimrSchemeOp::TTTLSettings::UNIT_AUTO: {
516-
auto& outTTL = *describeLogTableResult.mutable_ttl_settings()->mutable_date_type_column();
517-
outTTL.set_column_name(inTTL.GetColumnName());
518-
outTTL.set_expire_after_seconds(inTTL.GetExpireAfterSeconds());
519-
break;
520-
}
521-
522-
case NKikimrSchemeOp::TTTLSettings::UNIT_SECONDS:
523-
case NKikimrSchemeOp::TTTLSettings::UNIT_MILLISECONDS:
524-
case NKikimrSchemeOp::TTTLSettings::UNIT_MICROSECONDS:
525-
case NKikimrSchemeOp::TTTLSettings::UNIT_NANOSECONDS: {
526-
auto& outTTL = *describeLogTableResult.mutable_ttl_settings()->mutable_value_since_unix_epoch();
527-
outTTL.set_column_name(inTTL.GetColumnName());
528-
outTTL.set_column_unit(static_cast<Ydb::Table::ValueSinceUnixEpochModeSettings::Unit>(inTTL.GetColumnUnit()));
529-
outTTL.set_expire_after_seconds(inTTL.GetExpireAfterSeconds());
530-
break;
531-
}
532-
533-
default:
534-
break;
512+
if (!FillTtlSettings(*describeLogTableResult.mutable_ttl_settings(), tableDescription.GetTtlSettings().GetEnabled(), status, error)) {
513+
return Reply(status, error, NKikimrIssues::TIssuesIds::DEFAULT_ERROR, ctx);
535514
}
536515
}
537516

ydb/core/kqp/provider/yql_kikimr_gateway.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,16 @@ bool ConvertReadReplicasSettingsToProto(const TString settings, Ydb::Table::Read
311311

312312
void ConvertTtlSettingsToProto(const NYql::TTtlSettings& settings, Ydb::Table::TtlSettings& proto) {
313313
if (!settings.ColumnUnit) {
314-
auto& opts = *proto.mutable_date_type_column();
314+
auto& opts = *proto.mutable_date_type_column_v1();
315315
opts.set_column_name(settings.ColumnName);
316-
opts.set_expire_after_seconds(settings.ExpireAfter.Seconds());
317316
} else {
318-
auto& opts = *proto.mutable_value_since_unix_epoch();
317+
auto& opts = *proto.mutable_value_since_unix_epoch_v1();
319318
opts.set_column_name(settings.ColumnName);
320319
opts.set_column_unit(static_cast<Ydb::Table::ValueSinceUnixEpochModeSettings::Unit>(*settings.ColumnUnit));
321-
opts.set_expire_after_seconds(settings.ExpireAfter.Seconds());
322320
}
321+
auto* deleteTier = proto.add_tiers();
322+
deleteTier->set_apply_after_seconds(settings.ExpireAfter.Seconds());
323+
deleteTier->mutable_delete_();
323324
}
324325

325326
Ydb::FeatureFlag::Status GetFlagValue(const TMaybe<bool>& value) {

ydb/core/kqp/proxy_service/kqp_script_executions_ut.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ NKikimrSchemeOp::TColumnDescription Col(const TString& columnName, NScheme::TTyp
3535

3636
[[maybe_unused]] NKikimrSchemeOp::TTTLSettings TtlCol(const TString& columnName) {
3737
NKikimrSchemeOp::TTTLSettings settings;
38+
auto* deleteTier = settings.MutableEnabled()->AddTiers();
39+
deleteTier->MutableDelete();
40+
deleteTier->SetApplyAfterSeconds(TDuration::Minutes(20).Seconds());
3841
settings.MutableEnabled()->SetExpireAfterSeconds(TDuration::Minutes(20).Seconds());
3942
settings.MutableEnabled()->SetColumnName(columnName);
4043
settings.MutableEnabled()->MutableSysSettings()->SetRunInterval(TDuration::Minutes(60).MicroSeconds());

ydb/core/protos/flat_scheme_op.proto

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,24 @@ message TTTLSettings {
222222
optional uint32 MaxShardsInFlight = 6 [default = 0]; // zero means no limit
223223
}
224224

225+
message TEvictionToExternalStorageSettings {
226+
optional string StorageName = 1;
227+
}
228+
229+
message TTier {
230+
optional uint32 ApplyAfterSeconds = 1;
231+
oneof Action {
232+
google.protobuf.Empty Delete = 2;
233+
TEvictionToExternalStorageSettings EvictToExternalStorage = 3;
234+
}
235+
}
236+
225237
message TEnabled {
226238
optional string ColumnName = 1;
227-
optional uint32 ExpireAfterSeconds = 2;
239+
optional uint32 ExpireAfterSeconds = 2 [deprecated = true];
228240
optional EUnit ColumnUnit = 3;
229241
optional TSysSettings SysSettings = 4;
242+
repeated TTier Tiers = 5;
230243
}
231244

232245
message TDisabled {
@@ -581,10 +594,11 @@ message TColumnDataLifeCycle {
581594
message TTtl {
582595
optional string ColumnName = 1;
583596
oneof Expire {
584-
uint32 ExpireAfterSeconds = 2;
597+
uint32 ExpireAfterSeconds = 2 [deprecated = true]; // ignored if Tiers are not empty
585598
uint64 ExpireAfterBytes = 4;
586599
}
587600
optional TTTLSettings.EUnit ColumnUnit = 3;
601+
repeated TTTLSettings.TTier Tiers = 5;
588602
}
589603

590604
message TDisabled {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,14 @@ bool TTTLValidator::ValidateUnit(const NScheme::TTypeInfo columnType, NKikimrSch
5757
return true;
5858
}
5959

60-
}
60+
bool TTTLValidator::ValidateTiers(const NKikimrSchemeOp::TTTLSettings::TEnabled ttlSettings, TString& errStr) {
61+
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";
64+
return false;
65+
}
66+
}
67+
return true;
68+
}
69+
70+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ namespace NKikimr::NSchemeShard::NValidation {
99
class TTTLValidator {
1010
public:
1111
static bool ValidateUnit(const NScheme::TTypeInfo columnType, NKikimrSchemeOp::TTTLSettings::EUnit unit, TString& errStr);
12+
static bool ValidateTiers(const NKikimrSchemeOp::TTTLSettings::TEnabled ttlSettings, TString& errStr);
1213
};
13-
}
14+
}

ydb/core/tx/schemeshard/olap/operations/alter/abstract/converter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class TConverterModifyToAlter {
2626
if (enabled.HasColumnUnit()) {
2727
alterEnabled->SetColumnUnit(enabled.GetColumnUnit());
2828
}
29+
for (const auto& tier : enabled.GetTiers()) {
30+
alterEnabled->AddTiers()->CopyFrom(tier);
31+
}
2932
} else if (tableTtl.HasDisabled()) {
3033
alterTtl->MutableDisabled();
3134
}

ydb/core/tx/schemeshard/schemeshard__conditional_erase.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,16 @@ struct TSchemeShard::TTxRunConditionalErase: public TSchemeShard::TRwTxBase {
141141
}
142142

143143
const auto& settings = tableInfo->TTLSettings().GetEnabled();
144-
const TDuration expireAfter = TDuration::Seconds(settings.GetExpireAfterSeconds());
145-
const TInstant wallClock = ctx.Now() - expireAfter;
144+
145+
auto expireAfter = GetExpireAfter(settings, true);
146+
if (expireAfter.IsFail()) {
147+
LOG_WARN_S(ctx, NKikimrServices::FLAT_TX_SCHEMESHARD,
148+
"Invalid TTL settings: " << expireAfter.GetErrorMessage()
149+
<< ": shardIdx: " << tableShardInfo.ShardIdx << ": pathId: " << shardInfo.PathId
150+
<< ", at schemeshard: " << Self->TabletID());
151+
return false;
152+
}
153+
const TInstant wallClock = ctx.Now() - *expireAfter;
146154

147155
NKikimrTxDataShard::TEvConditionalEraseRowsRequest request;
148156
request.SetTableId(shardInfo.PathId.LocalPathId);

ydb/core/tx/schemeshard/schemeshard_info_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3623,6 +3623,8 @@ bool ValidateTtlSettings(const NKikimrSchemeOp::TTTLSettings& ttl,
36233623
const THashMap<TString, ui32>& colName2Id,
36243624
const TSubDomainInfo& subDomain, TString& errStr);
36253625

3626+
TConclusion<TDuration> GetExpireAfter(const NKikimrSchemeOp::TTTLSettings::TEnabled& settings, const bool allowNonDeleteTiers);
3627+
36263628
std::optional<std::pair<i64, i64>> ValidateSequenceType(const TString& sequenceName, const TString& dataType,
36273629
const NKikimr::NScheme::TTypeRegistry& typeRegistry, bool pgTypesEnabled, TString& errStr);
36283630

ydb/core/tx/schemeshard/schemeshard_validate_ttl.cpp

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

33
#include "common/validation.h"
4-
#include "olap/columns/schema.h"
54

65
#include <ydb/core/protos/flat_scheme_op.pb.h>
76

@@ -58,8 +57,18 @@ bool ValidateTtlSettings(const NKikimrSchemeOp::TTTLSettings& ttl,
5857
return false;
5958
}
6059

60+
if (!NValidation::TTTLValidator::ValidateTiers(enabled, errStr)) {
61+
return false;
62+
}
63+
64+
const auto expireAfter = GetExpireAfter(enabled, false);
65+
if (expireAfter.IsFail()) {
66+
errStr = expireAfter.GetErrorMessage();
67+
return false;
68+
}
69+
6170
const TInstant now = TInstant::Now();
62-
if (enabled.GetExpireAfterSeconds() > now.Seconds()) {
71+
if (expireAfter->Seconds() > now.Seconds()) {
6372
errStr = Sprintf("TTL should be less than %" PRIu64 " seconds (%" PRIu64 " days, %" PRIu64 " years). The ttl behaviour is undefined before 1970.", now.Seconds(), now.Days(), now.Days() / 365);
6473
return false;
6574
}
@@ -85,4 +94,20 @@ bool ValidateTtlSettings(const NKikimrSchemeOp::TTTLSettings& ttl,
8594
return true;
8695
}
8796

97+
TConclusion<TDuration> GetExpireAfter(const NKikimrSchemeOp::TTTLSettings::TEnabled& settings, const bool allowNonDeleteTiers) {
98+
if (settings.TiersSize()) {
99+
for (const auto& tier : settings.GetTiers()) {
100+
if (tier.HasDelete()) {
101+
return TDuration::Seconds(tier.GetApplyAfterSeconds());
102+
} else if (!allowNonDeleteTiers) {
103+
return TConclusionStatus::Fail("Only DELETE via TTL is allowed for row-oriented tables");
104+
}
105+
}
106+
return TConclusionStatus::Fail("TTL settings does not contain DELETE action");
107+
} else {
108+
// legacy format
109+
return TDuration::Seconds(settings.GetExpireAfterSeconds());
110+
}
111+
}
112+
88113
}}

0 commit comments

Comments
 (0)