Skip to content

Commit 5371fa7

Browse files
committed
Remove old schema snapshot upon adding new one
1 parent b8eb829 commit 5371fa7

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

ydb/core/tx/datashard/datashard.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,9 +1495,14 @@ void TDataShard::ScheduleRemoveAbandonedSchemaSnapshots() {
14951495
Y_DEBUG_ABORT_UNLESS(State == TShardState::PreOffline);
14961496
break;
14971497
}
1498-
if (snapshot.Schema->GetTableSchemaVersion() < it->second->GetTableSchemaVersion()) {
1499-
PendingSchemaSnapshotsToRemove.push_back(key);
1498+
if (SchemaSnapshotManager.HasReference(key)) {
1499+
continue;
1500+
}
1501+
if (snapshot.Schema->GetTableSchemaVersion() >= it->second->GetTableSchemaVersion()) {
1502+
continue;
15001503
}
1504+
1505+
PendingSchemaSnapshotsToRemove.push_back(key);
15011506
}
15021507

15031508
if (wasEmpty && !PendingSchemaSnapshotsToRemove.empty()) {
@@ -1733,8 +1738,18 @@ void TDataShard::AddSchemaSnapshot(const TPathId& pathId, ui64 tableSchemaVersio
17331738
Y_ABORT_UNLESS(TableInfos.contains(pathId.LocalPathId));
17341739
auto tableInfo = TableInfos[pathId.LocalPathId];
17351740

1736-
const auto key = TSchemaSnapshotKey(pathId.OwnerId, pathId.LocalPathId, tableSchemaVersion);
1741+
const auto key = TSchemaSnapshotKey(pathId, tableSchemaVersion);
17371742
SchemaSnapshotManager.AddSnapshot(txc.DB, key, TSchemaSnapshot(tableInfo, step, txId));
1743+
1744+
const auto& snapshots = SchemaSnapshotManager.GetSnapshots();
1745+
for (auto it = snapshots.lower_bound(TSchemaSnapshotKey(pathId, 1)); it != snapshots.end(); ++it) {
1746+
if (it->first == key) {
1747+
break;
1748+
}
1749+
if (!SchemaSnapshotManager.HasReference(it->first)) {
1750+
ScheduleRemoveSchemaSnapshot(it->first);
1751+
}
1752+
}
17381753
}
17391754

17401755
void TDataShard::PersistLastLoanTableTid(NIceDb::TNiceDb& db, ui32 localTid) {

ydb/core/tx/datashard/datashard_schema_snapshots.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ const TSchemaSnapshot* TSchemaSnapshotManager::FindSnapshot(const TSchemaSnapsho
8080
return Snapshots.FindPtr(key);
8181
}
8282

83-
void TSchemaSnapshotManager::RemoveShapshot(NIceDb::TNiceDb& db, const TSchemaSnapshotKey& key) {
83+
void TSchemaSnapshotManager::RemoveShapshot(NTable::TDatabase& db, const TSchemaSnapshotKey& key) {
8484
auto it = Snapshots.find(key);
8585
if (it == Snapshots.end()) {
8686
return;
8787
}
8888

8989
Snapshots.erase(it);
90-
PersistRemoveSnapshot(db, key);
90+
91+
NIceDb::TNiceDb nicedb(db);
92+
PersistRemoveSnapshot(nicedb, key);
9193
}
9294

9395
void TSchemaSnapshotManager::RenameSnapshots(NTable::TDatabase& db,

ydb/core/tx/datashard/datashard_schema_snapshots.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TSchemaSnapshotManager {
3333

3434
bool AddSnapshot(NTable::TDatabase& db, const TSchemaSnapshotKey& key, const TSchemaSnapshot& snapshot);
3535
const TSchemaSnapshot* FindSnapshot(const TSchemaSnapshotKey& key) const;
36-
void RemoveShapshot(NIceDb::TNiceDb& db, const TSchemaSnapshotKey& key);
36+
void RemoveShapshot(NTable::TDatabase& db, const TSchemaSnapshotKey& key);
3737
void RenameSnapshots(NTable::TDatabase& db, const TPathId& prevTableId, const TPathId& newTableId);
3838
const TSnapshots& GetSnapshots() const;
3939

ydb/core/tx/datashard/remove_schema_snapshots.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ class TDataShard::TTxRemoveSchemaSnapshots: public NTabletFlatExecutor::TTransac
1111
TTxType GetTxType() const override { return TXTYPE_REMOVE_SCHEMA_SNAPSHOTS; }
1212

1313
bool Execute(TTransactionContext& txc, const TActorContext&) override {
14-
NIceDb::TNiceDb db(txc.DB);
15-
1614
while (!Self->PendingSchemaSnapshotsToRemove.empty()) {
1715
const auto key = Self->PendingSchemaSnapshotsToRemove.back();
1816
const auto* snapshot = Self->GetSchemaSnapshotManager().FindSnapshot(key);
@@ -38,7 +36,7 @@ class TDataShard::TTxRemoveSchemaSnapshots: public NTabletFlatExecutor::TTransac
3836
continue;
3937
}
4038

41-
Self->GetSchemaSnapshotManager().RemoveShapshot(db, key);
39+
Self->GetSchemaSnapshotManager().RemoveShapshot(txc.DB, key);
4240
Self->PendingSchemaSnapshotsToRemove.pop_back();
4341
}
4442

0 commit comments

Comments
 (0)