Skip to content

Commit

Permalink
add tiering info to TTL in public api (#11390)
Browse files Browse the repository at this point in the history
  • Loading branch information
swalrus1 authored Nov 27, 2024
1 parent f54668a commit 225bab2
Show file tree
Hide file tree
Showing 22 changed files with 700 additions and 161 deletions.
25 changes: 2 additions & 23 deletions ydb/core/grpc_services/rpc_log_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,29 +509,8 @@ class TDescribeLogTableRPC : public TRpcSchemeRequestActor<TDescribeLogTableRPC,
}

if (tableDescription.HasTtlSettings() && tableDescription.GetTtlSettings().HasEnabled()) {
const auto& inTTL = tableDescription.GetTtlSettings().GetEnabled();

switch (inTTL.GetColumnUnit()) {
case NKikimrSchemeOp::TTTLSettings::UNIT_AUTO: {
auto& outTTL = *describeLogTableResult.mutable_ttl_settings()->mutable_date_type_column();
outTTL.set_column_name(inTTL.GetColumnName());
outTTL.set_expire_after_seconds(inTTL.GetExpireAfterSeconds());
break;
}

case NKikimrSchemeOp::TTTLSettings::UNIT_SECONDS:
case NKikimrSchemeOp::TTTLSettings::UNIT_MILLISECONDS:
case NKikimrSchemeOp::TTTLSettings::UNIT_MICROSECONDS:
case NKikimrSchemeOp::TTTLSettings::UNIT_NANOSECONDS: {
auto& outTTL = *describeLogTableResult.mutable_ttl_settings()->mutable_value_since_unix_epoch();
outTTL.set_column_name(inTTL.GetColumnName());
outTTL.set_column_unit(static_cast<Ydb::Table::ValueSinceUnixEpochModeSettings::Unit>(inTTL.GetColumnUnit()));
outTTL.set_expire_after_seconds(inTTL.GetExpireAfterSeconds());
break;
}

default:
break;
if (!FillTtlSettings(*describeLogTableResult.mutable_ttl_settings(), tableDescription.GetTtlSettings().GetEnabled(), status, error)) {
return Reply(status, error, NKikimrIssues::TIssuesIds::DEFAULT_ERROR, ctx);
}
}

Expand Down
9 changes: 5 additions & 4 deletions ydb/core/kqp/provider/yql_kikimr_gateway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,16 @@ bool ConvertReadReplicasSettingsToProto(const TString settings, Ydb::Table::Read

void ConvertTtlSettingsToProto(const NYql::TTtlSettings& settings, Ydb::Table::TtlSettings& proto) {
if (!settings.ColumnUnit) {
auto& opts = *proto.mutable_date_type_column();
auto& opts = *proto.mutable_date_type_column_v1();
opts.set_column_name(settings.ColumnName);
opts.set_expire_after_seconds(settings.ExpireAfter.Seconds());
} else {
auto& opts = *proto.mutable_value_since_unix_epoch();
auto& opts = *proto.mutable_value_since_unix_epoch_v1();
opts.set_column_name(settings.ColumnName);
opts.set_column_unit(static_cast<Ydb::Table::ValueSinceUnixEpochModeSettings::Unit>(*settings.ColumnUnit));
opts.set_expire_after_seconds(settings.ExpireAfter.Seconds());
}
auto* deleteTier = proto.add_tiers();
deleteTier->set_apply_after_seconds(settings.ExpireAfter.Seconds());
deleteTier->mutable_delete_();
}

Ydb::FeatureFlag::Status GetFlagValue(const TMaybe<bool>& value) {
Expand Down
3 changes: 3 additions & 0 deletions ydb/core/kqp/proxy_service/kqp_script_executions_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ NKikimrSchemeOp::TColumnDescription Col(const TString& columnName, NScheme::TTyp

[[maybe_unused]] NKikimrSchemeOp::TTTLSettings TtlCol(const TString& columnName) {
NKikimrSchemeOp::TTTLSettings settings;
auto* deleteTier = settings.MutableEnabled()->AddTiers();
deleteTier->MutableDelete();
deleteTier->SetApplyAfterSeconds(TDuration::Minutes(20).Seconds());
settings.MutableEnabled()->SetExpireAfterSeconds(TDuration::Minutes(20).Seconds());
settings.MutableEnabled()->SetColumnName(columnName);
settings.MutableEnabled()->MutableSysSettings()->SetRunInterval(TDuration::Minutes(60).MicroSeconds());
Expand Down
18 changes: 16 additions & 2 deletions ydb/core/protos/flat_scheme_op.proto
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,24 @@ message TTTLSettings {
optional uint32 MaxShardsInFlight = 6 [default = 0]; // zero means no limit
}

message TEvictionToExternalStorageSettings {
optional string StorageName = 1;
}

message TTier {
optional uint32 ApplyAfterSeconds = 1;
oneof Action {
google.protobuf.Empty Delete = 2;
TEvictionToExternalStorageSettings EvictToExternalStorage = 3;
}
}

message TEnabled {
optional string ColumnName = 1;
optional uint32 ExpireAfterSeconds = 2;
optional uint32 ExpireAfterSeconds = 2 [deprecated = true];
optional EUnit ColumnUnit = 3;
optional TSysSettings SysSettings = 4;
repeated TTier Tiers = 5;
}

message TDisabled {
Expand Down Expand Up @@ -581,10 +594,11 @@ message TColumnDataLifeCycle {
message TTtl {
optional string ColumnName = 1;
oneof Expire {
uint32 ExpireAfterSeconds = 2;
uint32 ExpireAfterSeconds = 2 [deprecated = true]; // ignored if Tiers are not empty
uint64 ExpireAfterBytes = 4;
}
optional TTTLSettings.EUnit ColumnUnit = 3;
repeated TTTLSettings.TTier Tiers = 5;
}

message TDisabled {
Expand Down
12 changes: 11 additions & 1 deletion ydb/core/tx/schemeshard/common/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ bool TTTLValidator::ValidateUnit(const NScheme::TTypeInfo columnType, NKikimrSch
return true;
}

}
bool TTTLValidator::ValidateTiers(const NKikimrSchemeOp::TTTLSettings::TEnabled ttlSettings, TString& errStr) {
for (ui64 i = 0; i < ttlSettings.TiersSize(); ++i) {
if (ttlSettings.GetTiers(i).HasDelete() && i + 1 != ttlSettings.TiersSize()) {
errStr = "Only the last tier in TTL settings can have Delete action";
return false;
}
}
return true;
}

}
3 changes: 2 additions & 1 deletion ydb/core/tx/schemeshard/common/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace NKikimr::NSchemeShard::NValidation {
class TTTLValidator {
public:
static bool ValidateUnit(const NScheme::TTypeInfo columnType, NKikimrSchemeOp::TTTLSettings::EUnit unit, TString& errStr);
static bool ValidateTiers(const NKikimrSchemeOp::TTTLSettings::TEnabled ttlSettings, TString& errStr);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class TConverterModifyToAlter {
if (enabled.HasColumnUnit()) {
alterEnabled->SetColumnUnit(enabled.GetColumnUnit());
}
for (const auto& tier : enabled.GetTiers()) {
alterEnabled->AddTiers()->CopyFrom(tier);
}
} else if (tableTtl.HasDisabled()) {
alterTtl->MutableDisabled();
}
Expand Down
12 changes: 10 additions & 2 deletions ydb/core/tx/schemeshard/schemeshard__conditional_erase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ struct TSchemeShard::TTxRunConditionalErase: public TSchemeShard::TRwTxBase {
}

const auto& settings = tableInfo->TTLSettings().GetEnabled();
const TDuration expireAfter = TDuration::Seconds(settings.GetExpireAfterSeconds());
const TInstant wallClock = ctx.Now() - expireAfter;

auto expireAfter = GetExpireAfter(settings, true);
if (expireAfter.IsFail()) {
LOG_WARN_S(ctx, NKikimrServices::FLAT_TX_SCHEMESHARD,
"Invalid TTL settings: " << expireAfter.GetErrorMessage()
<< ": shardIdx: " << tableShardInfo.ShardIdx << ": pathId: " << shardInfo.PathId
<< ", at schemeshard: " << Self->TabletID());
return false;
}
const TInstant wallClock = ctx.Now() - *expireAfter;

NKikimrTxDataShard::TEvConditionalEraseRowsRequest request;
request.SetTableId(shardInfo.PathId.LocalPathId);
Expand Down
2 changes: 2 additions & 0 deletions ydb/core/tx/schemeshard/schemeshard_info_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3623,6 +3623,8 @@ bool ValidateTtlSettings(const NKikimrSchemeOp::TTTLSettings& ttl,
const THashMap<TString, ui32>& colName2Id,
const TSubDomainInfo& subDomain, TString& errStr);

TConclusion<TDuration> GetExpireAfter(const NKikimrSchemeOp::TTTLSettings::TEnabled& settings, const bool allowNonDeleteTiers);

std::optional<std::pair<i64, i64>> ValidateSequenceType(const TString& sequenceName, const TString& dataType,
const NKikimr::NScheme::TTypeRegistry& typeRegistry, bool pgTypesEnabled, TString& errStr);

Expand Down
29 changes: 27 additions & 2 deletions ydb/core/tx/schemeshard/schemeshard_validate_ttl.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "schemeshard_info_types.h"

#include "common/validation.h"
#include "olap/columns/schema.h"

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

Expand Down Expand Up @@ -58,8 +57,18 @@ bool ValidateTtlSettings(const NKikimrSchemeOp::TTTLSettings& ttl,
return false;
}

if (!NValidation::TTTLValidator::ValidateTiers(enabled, errStr)) {
return false;
}

const auto expireAfter = GetExpireAfter(enabled, false);
if (expireAfter.IsFail()) {
errStr = expireAfter.GetErrorMessage();
return false;
}

const TInstant now = TInstant::Now();
if (enabled.GetExpireAfterSeconds() > now.Seconds()) {
if (expireAfter->Seconds() > now.Seconds()) {
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);
return false;
}
Expand All @@ -85,4 +94,20 @@ bool ValidateTtlSettings(const NKikimrSchemeOp::TTTLSettings& ttl,
return true;
}

TConclusion<TDuration> GetExpireAfter(const NKikimrSchemeOp::TTTLSettings::TEnabled& settings, const bool allowNonDeleteTiers) {
if (settings.TiersSize()) {
for (const auto& tier : settings.GetTiers()) {
if (tier.HasDelete()) {
return TDuration::Seconds(tier.GetApplyAfterSeconds());
} else if (!allowNonDeleteTiers) {
return TConclusionStatus::Fail("Only DELETE via TTL is allowed for row-oriented tables");
}
}
return TConclusionStatus::Fail("TTL settings does not contain DELETE action");
} else {
// legacy format
return TDuration::Seconds(settings.GetExpireAfterSeconds());
}
}

}}
3 changes: 3 additions & 0 deletions ydb/core/tx/schemeshard/ut_helpers/ls_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,9 @@ TCheckFunc HasTtlEnabled(const TString& columnName, const TDuration& expireAfter
UNIT_ASSERT_VALUES_EQUAL(ttl.GetEnabled().GetColumnName(), columnName);
UNIT_ASSERT_VALUES_EQUAL(ttl.GetEnabled().GetColumnUnit(), columnUnit);
UNIT_ASSERT_VALUES_EQUAL(ttl.GetEnabled().GetExpireAfterSeconds(), expireAfter.Seconds());
UNIT_ASSERT_VALUES_EQUAL(ttl.GetEnabled().TiersSize(), 1);
UNIT_ASSERT(ttl.GetEnabled().GetTiers(0).HasDelete());
UNIT_ASSERT_VALUES_EQUAL(ttl.GetEnabled().GetTiers(0).GetApplyAfterSeconds(), expireAfter.Seconds());
};
}

Expand Down
Loading

0 comments on commit 225bab2

Please sign in to comment.