diff options
author | snaury <[email protected]> | 2022-12-05 18:14:42 +0300 |
---|---|---|
committer | snaury <[email protected]> | 2022-12-05 18:14:42 +0300 |
commit | 3eec43dd0e58c78c87f23c930b9ecf977a4d8fc0 (patch) | |
tree | 3e8da03c0646ca43d37f7cbc646cfe6f5d872720 | |
parent | d4195ed627058ff09181afee4edf6e9b7d7bf5dd (diff) |
Rename table snapshots when renaming a table
-rw-r--r-- | ydb/core/tx/datashard/datashard.cpp | 1 | ||||
-rw-r--r-- | ydb/core/tx/datashard/datashard_snapshots.cpp | 26 | ||||
-rw-r--r-- | ydb/core/tx/datashard/datashard_snapshots.h | 2 | ||||
-rw-r--r-- | ydb/core/tx/datashard/datashard_ut_snapshot.cpp | 51 |
4 files changed, 80 insertions, 0 deletions
diff --git a/ydb/core/tx/datashard/datashard.cpp b/ydb/core/tx/datashard/datashard.cpp index 798d255c883..3ad5b986bb3 100644 --- a/ydb/core/tx/datashard/datashard.cpp +++ b/ydb/core/tx/datashard/datashard.cpp @@ -1350,6 +1350,7 @@ TUserTable::TPtr TDataShard::MoveUserTable(TOperation::TPtr op, const NKikimrTxD } } + SnapshotManager.RenameSnapshots(txc.DB, prevId, newId); SchemaSnapshotManager.RenameSnapshots(txc.DB, prevId, newId); if (newTableInfo->NeedSchemaSnapshots()) { AddSchemaSnapshot(newId, version, op->GetStep(), op->GetTxId(), txc, ctx); diff --git a/ydb/core/tx/datashard/datashard_snapshots.cpp b/ydb/core/tx/datashard/datashard_snapshots.cpp index f0a36804358..b40a9c8057c 100644 --- a/ydb/core/tx/datashard/datashard_snapshots.cpp +++ b/ydb/core/tx/datashard/datashard_snapshots.cpp @@ -817,6 +817,32 @@ void TSnapshotManager::EnsureRemovedRowVersions(NTable::TDatabase& db, const TRo } } +void TSnapshotManager::RenameSnapshots(NTable::TDatabase& db, const TPathId& prevTableId, const TPathId& newTableId) { + TSnapshotTableKey prevTableKey(prevTableId.OwnerId, prevTableId.LocalPathId); + TSnapshotTableKey newTableKey(newTableId.OwnerId, newTableId.LocalPathId); + + NIceDb::TNiceDb nicedb(db); + + auto it = Snapshots.lower_bound(prevTableKey); + while (it != Snapshots.end() && it->first == prevTableKey) { + TSnapshotKey oldKey = it->first; + TSnapshotKey newKey(newTableKey.OwnerId, newTableKey.PathId, oldKey.Step, oldKey.TxId); + + Y_VERIFY_DEBUG(!References.contains(oldKey), "Unexpected reference to snapshot during rename"); + + PersistAddSnapshot(nicedb, newKey, it->second.Name, it->second.Flags, it->second.Timeout); + + if (ExpireQueue.Has(&it->second)) { + auto& newSnapshot = Snapshots.at(newKey); + newSnapshot.ExpireTime = it->second.ExpireTime; + ExpireQueue.Add(&newSnapshot); + } + + ++it; + PersistRemoveSnapshot(nicedb, oldKey); + } +} + } // namespace NDataShard } // namespace NKikimr diff --git a/ydb/core/tx/datashard/datashard_snapshots.h b/ydb/core/tx/datashard/datashard_snapshots.h index dd147b4a457..eb868fa4dbc 100644 --- a/ydb/core/tx/datashard/datashard_snapshots.h +++ b/ydb/core/tx/datashard/datashard_snapshots.h @@ -207,6 +207,8 @@ public: void Fix_KIKIMR_14259(NTable::TDatabase& db); void EnsureRemovedRowVersions(NTable::TDatabase& db, const TRowVersion& from, const TRowVersion& to); + void RenameSnapshots(NTable::TDatabase& db, const TPathId& prevTableId, const TPathId& newTableId); + private: void DoRemoveSnapshot(NTable::TDatabase& db, const TSnapshotKey& key); diff --git a/ydb/core/tx/datashard/datashard_ut_snapshot.cpp b/ydb/core/tx/datashard/datashard_ut_snapshot.cpp index a1dfa154089..a8366d36bba 100644 --- a/ydb/core/tx/datashard/datashard_ut_snapshot.cpp +++ b/ydb/core/tx/datashard/datashard_ut_snapshot.cpp @@ -3607,6 +3607,57 @@ Y_UNIT_TEST_SUITE(DataShardSnapshots) { "{ items { uint32_value: 3 } items { uint32_value: 31 } }"); } + Y_UNIT_TEST_WITH_MVCC(VolatileSnapshotRenameTimeout) { + TPortManager pm; + TServerSettings serverSettings(pm.GetPort(2134)); + serverSettings.SetDomainName("Root") + .SetEnableMvcc(WithMvcc) + .SetUseRealThreads(false) + .SetDomainPlanResolution(1000); + + Tests::TServer::TPtr server = new TServer(serverSettings); + auto &runtime = *server->GetRuntime(); + auto sender = runtime.AllocateEdgeActor(); + + runtime.SetLogPriority(NKikimrServices::TX_DATASHARD, NLog::PRI_TRACE); + runtime.SetLogPriority(NKikimrServices::TX_PROXY, NLog::PRI_DEBUG); + runtime.GetAppData().AllowReadTableImmediate = true; + + InitRoot(server, sender); + + CreateShardedTable(server, sender, "/Root", "table-1", 2); + CreateShardedTable(server, sender, "/Root", "table-2", 2); + + ExecSQL(server, sender, "UPSERT INTO `/Root/table-1` (key, value) VALUES (1, 1), (2, 2), (3, 3);"); + ExecSQL(server, sender, "UPSERT INTO `/Root/table-2` (key, value) VALUES (10, 10), (20, 20), (30, 30);"); + + auto snapshot = CreateVolatileSnapshot(server, { "/Root/table-1", "/Root/table-2" }, TDuration::MilliSeconds(10000)); + + ExecSQL(server, sender, "UPSERT INTO `/Root/table-1` (key, value) VALUES (1, 11), (2, 22), (3, 33), (4, 44);"); + ExecSQL(server, sender, "UPSERT INTO `/Root/table-2` (key, value) VALUES (10, 11), (20, 22), (30, 33), (40, 44);"); + + auto table1snapshot1 = ReadShardedTable(server, "/Root/table-1", snapshot); + UNIT_ASSERT_VALUES_EQUAL(table1snapshot1, + "key = 1, value = 1\n" + "key = 2, value = 2\n" + "key = 3, value = 3\n"); + + WaitTxNotification(server, sender, AsyncMoveTable(server, "/Root/table-1", "/Root/table-1-moved")); + + auto table1snapshot2 = ReadShardedTable(server, "/Root/table-1-moved", snapshot); + UNIT_ASSERT_VALUES_EQUAL(table1snapshot2, + "key = 1, value = 1\n" + "key = 2, value = 2\n" + "key = 3, value = 3\n"); + + Cerr << "---- Sleeping ----" << Endl; + SimulateSleep(server, TDuration::Seconds(60)); + + auto table1snapshot3 = ReadShardedTable(server, "/Root/table-1-moved", snapshot); + UNIT_ASSERT_VALUES_EQUAL(table1snapshot3, + "ERROR: WrongRequest\n"); + } + } } // namespace NKikimr |