Skip to content

store all versions of TTL in memory #14677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions ydb/core/tx/columnshard/columnshard_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1617,11 +1617,7 @@ void TColumnShard::OnTieringModified(const std::optional<ui64> pathId) {
StoragesManager->OnTieringModified(Tiers);
if (TablesManager.HasPrimaryIndex()) {
if (pathId) {
std::optional<NOlap::TTiering> tableTtl;
if (auto* findTtl = TablesManager.GetTtl().FindPtr(*pathId)) {
tableTtl = *findTtl;
}
TablesManager.MutablePrimaryIndex().OnTieringModified(tableTtl, *pathId);
TablesManager.MutablePrimaryIndex().OnTieringModified(TablesManager.GetTableTtl(*pathId), *pathId);
} else {
TablesManager.MutablePrimaryIndex().OnTieringModified(TablesManager.GetTtl());
}
Expand Down
39 changes: 6 additions & 33 deletions ydb/core/tx/columnshard/tables_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ bool TTablesManager::InitFromDB(NIceDb::TNiceDb& db) {
return false;
}

THashMap<ui64, NOlap::TSnapshot> lastVersion;
while (!rowset.EndOfSet()) {
const ui64 pathId = rowset.GetValue<Schema::TableVersionInfo::PathId>();
Y_ABORT_UNLESS(Tables.contains(pathId));
Expand All @@ -126,22 +125,8 @@ bool TTablesManager::InitFromDB(NIceDb::TNiceDb& db) {
AFL_VERIFY(preset);
AFL_VERIFY(preset->Id == versionInfo.GetSchemaPresetId())("preset", preset->Id)("table", versionInfo.GetSchemaPresetId());

if (!table.IsDropped() && versionInfo.HasTtlSettings()) {
auto& ttlSettings = versionInfo.GetTtlSettings();
auto vIt = lastVersion.find(pathId);
if (vIt == lastVersion.end()) {
vIt = lastVersion.emplace(pathId, version).first;
}
if (vIt->second <= version) {
if (ttlSettings.HasEnabled()) {
NOlap::TTiering deserializedTtl;
AFL_VERIFY(deserializedTtl.DeserializeFromProto(ttlSettings.GetEnabled()).IsSuccess());
Ttl[pathId] = std::move(deserializedTtl);
} else {
Ttl.erase(pathId);
}
vIt->second = version;
}
if (versionInfo.HasTtlSettings()) {
Ttl.AddVersionFromProto(pathId, version, versionInfo.GetTtlSettings());
}
table.AddVersion(version);
if (!rowset.Next()) {
Expand Down Expand Up @@ -230,7 +215,7 @@ const TTableInfo& TTablesManager::GetTable(const ui64 pathId) const {
}

ui64 TTablesManager::GetMemoryUsage() const {
ui64 memory = Tables.size() * sizeof(TTableInfo) + PathsToDrop.size() * sizeof(ui64) + Ttl.size() * sizeof(NOlap::TTiering);
ui64 memory = Tables.size() * sizeof(TTableInfo) + PathsToDrop.size() * sizeof(ui64) + Ttl.GetMemoryUsage();
if (PrimaryIndex) {
memory += PrimaryIndex->MemoryUsage();
}
Expand All @@ -242,7 +227,6 @@ void TTablesManager::DropTable(const ui64 pathId, const NOlap::TSnapshot& versio
auto& table = Tables[pathId];
table.SetDropVersion(version);
AFL_VERIFY(PathsToDrop[version].emplace(pathId).second);
Ttl.erase(pathId);
Schema::SaveTableDropVersion(db, pathId, version.GetPlanStep(), version.GetTxId());
}

Expand Down Expand Up @@ -299,7 +283,7 @@ void TTablesManager::AddSchemaVersion(
for (auto&& i : Tables) {
PrimaryIndex->RegisterTable(i.first);
}
PrimaryIndex->OnTieringModified(Ttl);
PrimaryIndex->OnTieringModified(GetTtl());
} else {
PrimaryIndex->RegisterSchemaVersion(version, presetId, NOlap::IColumnEngine::TSchemaInitializationData(versionInfo));
}
Expand All @@ -319,14 +303,7 @@ void TTablesManager::AddTableVersion(const ui64 pathId, const NOlap::TSnapshot&
bool isTtlModified = false;
if (versionInfo.HasTtlSettings()) {
isTtlModified = true;
const auto& ttlSettings = versionInfo.GetTtlSettings();
if (ttlSettings.HasEnabled()) {
NOlap::TTiering deserializedTtl;
AFL_VERIFY(deserializedTtl.DeserializeFromProto(ttlSettings.GetEnabled()).IsSuccess());
Ttl[pathId] = std::move(deserializedTtl);
} else {
Ttl.erase(pathId);
}
Ttl.AddVersionFromProto(pathId, version, versionInfo.GetTtlSettings());
}

if (versionInfo.HasSchemaPresetId()) {
Expand All @@ -344,11 +321,7 @@ void TTablesManager::AddTableVersion(const ui64 pathId, const NOlap::TSnapshot&

if (isTtlModified) {
if (PrimaryIndex) {
if (auto findTtl = Ttl.FindPtr(pathId)) {
PrimaryIndex->OnTieringModified(*findTtl, pathId);
} else {
PrimaryIndex->OnTieringModified({}, pathId);
}
PrimaryIndex->OnTieringModified(GetTableTtl(pathId), pathId);
}
}
Schema::SaveTableVersionInfo(db, pathId, version, versionInfo);
Expand Down
59 changes: 56 additions & 3 deletions ydb/core/tx/columnshard/tables_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,53 @@ class TTableInfo {
}
};

class TTtlVersions {
private:
THashMap<ui64, std::map<NOlap::TSnapshot, std::optional<NOlap::TTiering>>> Ttl;

void AddVersion(const ui64 pathId, const NOlap::TSnapshot& snapshot, std::optional<NOlap::TTiering> ttl) {
AFL_VERIFY(Ttl[pathId].emplace(snapshot, ttl).second)("snapshot", snapshot);
}

public:
void AddVersionFromProto(const ui64 pathId, const NOlap::TSnapshot& snapshot, const NKikimrSchemeOp::TColumnDataLifeCycle& ttlSettings) {
std::optional<NOlap::TTiering> ttlVersion;
if (ttlSettings.HasEnabled()) {
NOlap::TTiering deserializedTtl;
AFL_VERIFY(deserializedTtl.DeserializeFromProto(ttlSettings.GetEnabled()).IsSuccess());
ttlVersion.emplace(std::move(deserializedTtl));
}
AddVersion(pathId, snapshot, ttlVersion);
}

std::optional<NOlap::TTiering> GetTableTtl(const ui64 pathId, const NOlap::TSnapshot& snapshot = NOlap::TSnapshot::Max()) const {
auto findTable = Ttl.FindPtr(pathId);
if (!findTable) {
return std::nullopt;
}
const auto findTtl = findTable->upper_bound(snapshot);
if (findTtl == findTable->begin()) {
return std::nullopt;
}
return std::prev(findTtl)->second;
}

ui64 GetMemoryUsage() const {
ui64 memory = 0;
for (const auto& [_, ttlVersions] : Ttl) {
memory += ttlVersions.size() * sizeof(NOlap::TTiering);
}
return memory;
}
};

class TTablesManager {
private:
THashMap<ui64, TTableInfo> Tables;
THashSet<ui32> SchemaPresetsIds;
THashMap<ui32, NKikimrSchemeOp::TColumnTableSchema> ActualSchemaForPreset;
std::map<NOlap::TSnapshot, THashSet<ui64>> PathsToDrop;
THashMap<ui64, NOlap::TTiering> Ttl;
TTtlVersions Ttl;
std::unique_ptr<NOlap::IColumnEngine> PrimaryIndex;
std::shared_ptr<NOlap::IStoragesManager> StoragesManager;
std::shared_ptr<NOlap::NDataAccessorControl::IDataAccessorsManager> DataAccessorsManager;
Expand All @@ -177,8 +217,21 @@ class TTablesManager {
bool TryFinalizeDropPathOnExecute(NTable::TDatabase& dbTable, const ui64 pathId) const;
bool TryFinalizeDropPathOnComplete(const ui64 pathId);

const THashMap<ui64, NOlap::TTiering>& GetTtl() const {
return Ttl;
THashMap<ui64, NOlap::TTiering> GetTtl(const NOlap::TSnapshot& snapshot = NOlap::TSnapshot::Max()) const {
THashMap<ui64, NOlap::TTiering> ttl;
for (const auto& [pathId, info] : Tables) {
if (info.IsDropped(snapshot)) {
continue;
}
if (auto tableTtl = Ttl.GetTableTtl(pathId, snapshot)) {
ttl.emplace(pathId, std::move(*tableTtl));
}
}
return ttl;
}

std::optional<NOlap::TTiering> GetTableTtl(const ui64 pathId, const NOlap::TSnapshot& snapshot = NOlap::TSnapshot::Max()) const {
return Ttl.GetTableTtl(pathId, snapshot);
}

const std::map<NOlap::TSnapshot, THashSet<ui64>>& GetPathsToDrop() const {
Expand Down
Loading