diff --git a/ydb/core/tx/schemeshard/index/operation_alter_index.cpp b/ydb/core/tx/schemeshard/index/operation_alter_index.cpp index f01b32ab57786..e8e94e85fa2ab 100644 --- a/ydb/core/tx/schemeshard/index/operation_alter_index.cpp +++ b/ydb/core/tx/schemeshard/index/operation_alter_index.cpp @@ -193,10 +193,7 @@ class TAlterTableIndex: public TSubOperation { context.DbChanges.PersistAlterIndex(indexPath->PathId); context.DbChanges.PersistTxState(OperationId); - auto indexData = context.SS->Indexes.Update(indexPath->PathId); - context.MemChanges.RecordUndo([indexData, previous = indexData->AlterData]() { - indexData->AlterData = previous; - }); + auto& indexData = context.SS->Indexes.Update(indexPath->PathId, context.MemChanges); TTableIndexInfo::TPtr newIndexData = indexData->CreateNextVersion(); Y_ABORT_UNLESS(newIndexData); newIndexData->State = tableIndexAlter.GetState(); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_bsv.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_bsv.cpp index 80de4fd6081e8..4c2934dce4f07 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_bsv.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_bsv.cpp @@ -186,9 +186,6 @@ class TAlterBlockStoreVolume: public TSubOperation { TBlockStorePartitionInfo::TPtr part = new TBlockStorePartitionInfo(); part->PartitionId = i + volume->DefaultPartitionCount; part->AlterVersion = volume->AlterData->AlterVersion; - context.MemChanges.RecordUndo([volume, shardIdx]() { - volume->Shards.erase(shardIdx); - }); volume->Shards[shardIdx] = std::move(part); } @@ -442,7 +439,7 @@ class TAlterBlockStoreVolume: public TSubOperation { } } - auto volume = context.SS->BlockStoreVolumes.Update(path.Base()->PathId); + auto& volume = context.SS->BlockStoreVolumes.Update(path.Base()->PathId, context.MemChanges); Y_ABORT_UNLESS(volume); const auto* alterVolumeConfig = ParseParams(volume->VolumeConfig, alter, errStr); @@ -600,9 +597,6 @@ class TAlterBlockStoreVolume: public TSubOperation { alterData->DefaultPartitionCount = TBlockStoreVolumeInfo::CalculateDefaultPartitionCount(alterData->VolumeConfig); alterData->ExplicitChannelProfileCount = alterData->VolumeConfig.ExplicitChannelProfilesSize(); - context.MemChanges.RecordUndo([volume, previous = volume->AlterData]() { - volume->AlterData = previous; - }); volume->PrepareAlter(alterData); auto newVolumeSpace = volume->GetVolumeSpace(); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_cdc_stream.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_cdc_stream.cpp index f77adf52e77c8..4be3bb0666b5e 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_cdc_stream.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_cdc_stream.cpp @@ -161,7 +161,7 @@ class TAlterCdcStream: public TSubOperation { } Y_ABORT_UNLESS(context.SS->CdcStreams.contains(streamPath.Base()->PathId)); - auto stream = context.SS->CdcStreams.Update(streamPath.Base()->PathId); + auto& stream = context.SS->CdcStreams.Update(streamPath.Base()->PathId, context.MemChanges); TCdcStreamInfo::EState requiredState = TCdcStreamInfo::EState::ECdcStreamStateInvalid; TCdcStreamInfo::EState newState = TCdcStreamInfo::EState::ECdcStreamStateInvalid; @@ -201,9 +201,6 @@ class TAlterCdcStream: public TSubOperation { context.DbChanges.PersistAlterCdcStream(streamPath.Base()->PathId); context.DbChanges.PersistTxState(OperationId); - context.MemChanges.RecordUndo([stream, previous = stream->AlterData]() { - stream->AlterData = previous; - }); auto streamAlter = stream->CreateNextVersion(); Y_ABORT_UNLESS(streamAlter); streamAlter->State = newState; diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_extsubdomain.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_extsubdomain.cpp index 782ae922f896b..56707b89bceb8 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_extsubdomain.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_extsubdomain.cpp @@ -47,7 +47,7 @@ struct TParamsDelta { }; std::tuple -VerifyParams(TParamsDelta* delta, const TPathId pathId, const TIntrusiveConstPtr& current, +VerifyParams(TParamsDelta* delta, const TPathId pathId, const TSubDomainInfo::TPtr& current, const NKikimrSubDomains::TSubDomainSettings& input, const bool isServerlessExclusiveDynamicNodesEnabled) { auto paramError = [](const TStringBuf& msg) { return std::make_tuple(NKikimrScheme::EStatus::StatusInvalidParameter, @@ -342,7 +342,7 @@ VerifyParams(TParamsDelta* delta, const TPathId pathId, const TIntrusiveConstPtr } void VerifyParams(TProposeResponse* result, TParamsDelta* delta, const TPathId pathId, - const TIntrusiveConstPtr& current, const NKikimrSubDomains::TSubDomainSettings& input, + const TSubDomainInfo::TPtr& current, const NKikimrSubDomains::TSubDomainSettings& input, const bool isServerlessExclusiveDynamicNodesEnabled) { // TProposeRespose should come in assuming positive outcome (status NKikimrScheme::StatusAccepted, no errors) Y_ABORT_UNLESS(result->IsAccepted()); @@ -354,7 +354,6 @@ void RegisterChanges(const TTxState& txState, const TTxId operationTxId, TOperat const auto& basenameId = path.Base()->PathId; context.MemChanges.GrabPath(context.SS, basenameId); - context.MemChanges.GrabDomain(context.SS, basenameId); // Registering shards is a bit complicated as every shard should be registered // in many places: @@ -621,7 +620,7 @@ class TAlterExtSubDomainCreateHive: public TSubOperation { // Get existing extsubdomain Y_ABORT_UNLESS(context.SS->SubDomains.contains(basenameId)); - auto subdomainInfo = context.SS->SubDomains.Update(basenameId); + auto& subdomainInfo = context.SS->SubDomains.Update(basenameId, context.MemChanges); Y_ABORT_UNLESS(subdomainInfo); auto result = MakeHolder(NKikimrScheme::StatusAccepted, ui64(OperationId.GetTxId()), ui64(schemeshardTabletId)); @@ -873,7 +872,7 @@ class TAlterExtSubDomain: public TSubOperation { // Get existing extsubdomain Y_ABORT_UNLESS(context.SS->SubDomains.contains(basenameId)); - auto subdomainInfo = context.SS->SubDomains.Update(basenameId); + auto& subdomainInfo = context.SS->SubDomains.Update(basenameId, context.MemChanges); Y_ABORT_UNLESS(subdomainInfo); auto result = MakeHolder(NKikimrScheme::StatusAccepted, ui64(OperationId.GetTxId()), ui64(schemeshardTabletId)); @@ -1134,7 +1133,7 @@ TVector CreateCompatibleAlterExtSubDomain(TOperationId id, const auto& basenameId = path.Base()->PathId; Y_ABORT_UNLESS(context.SS->SubDomains.contains(basenameId)); - auto subdomainInfo = context.SS->SubDomains.at(basenameId); + auto& subdomainInfo = context.SS->SubDomains.Update(basenameId, context.MemChanges); Y_ABORT_UNLESS(subdomainInfo); if (subdomainInfo->GetAlter()) { diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_fs.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_fs.cpp index 8bba8ff056264..dd71087b0bd1f 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_fs.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_fs.cpp @@ -345,7 +345,7 @@ THolder TAlterFileStore::Propose( Y_ABORT_UNLESS(path.Base()->IsCreateFinished()); - auto fs = context.SS->FileStoreInfos.Update(path.Base()->PathId); + auto& fs = context.SS->FileStoreInfos.Update(path.Base()->PathId, context.MemChanges); Y_VERIFY_S(fs, "FileStore info is null. PathId: " << path.Base()->PathId); if (fs->AlterConfig) { @@ -397,10 +397,6 @@ THolder TAlterFileStore::Propose( const auto oldFileStoreSpace = fs->GetFileStoreSpace(); - context.MemChanges.RecordUndo([fs, version = fs->AlterVersion]() { - fs->AlterConfig.Reset(); - fs->AlterVersion = version; - }); fs->PrepareAlter(*alterConfig); const auto newFileStoreSpace = fs->GetFileStoreSpace(); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_kesus.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_kesus.cpp index 8cb78d92ba09d..f22812f95c31d 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_kesus.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_kesus.cpp @@ -315,7 +315,7 @@ class TAlterKesus: public TSubOperation { return result; } - auto kesus = context.SS->KesusInfos.Update(path.Base()->PathId); + auto& kesus = context.SS->KesusInfos.Update(path.Base()->PathId, context.MemChanges); Y_ABORT_UNLESS(kesus); Y_ABORT_UNLESS(path.Base()->IsCreateFinished()); // checks.NotUnderOperation checks that path not under creation @@ -331,10 +331,6 @@ class TAlterKesus: public TSubOperation { return result; } - context.MemChanges.RecordUndo([kesus, version = kesus->AlterVersion]() { - kesus->AlterConfig.Reset(); - kesus->AlterVersion = version; - }); kesus->AlterConfig.Reset(new Ydb::Coordination::Config); kesus->AlterConfig->CopyFrom(kesus->Config); kesus->AlterConfig->MergeFrom(*alterConfig); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_pq.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_pq.cpp index 628ce1f1de9ad..415edb9d90319 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_pq.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_pq.cpp @@ -400,9 +400,6 @@ class TAlterPQ: public TSubOperation { TString prevBound; for (size_t i = 0; i < partitions.size(); ++i) { auto* partitionInfo = partitions[i].second; - context.MemChanges.RecordUndo([pqGroup, id = partitionInfo->PqId, previous = partitionInfo->KeyRange]() { - pqGroup->Partitions.at(id)->KeyRange = previous; - }); if (i) { partitionInfo->KeyRange.ConstructInPlace(); partitionInfo->KeyRange->FromBound = prevBound; @@ -422,24 +419,12 @@ class TAlterPQ: public TSubOperation { for (auto& [shardIdx, tabletInfo] : pqGroup->Shards) { for (const auto& partitionInfo : tabletInfo->Partitions) { if (splitMergeWasDisabled) { - context.MemChanges.RecordUndo([pqGroup, id = partitionInfo->PqId, - status = partitionInfo->Status, keyRange = partitionInfo->KeyRange, - parents = partitionInfo->ParentPartitionIds, children = partitionInfo->ChildPartitionIds]() { - auto* partition = pqGroup->Partitions.at(id); - partition->Status = status; - partition->KeyRange = keyRange; - partition->ParentPartitionIds = parents; - partition->ChildPartitionIds = children; - }); // clear all splitmerge fields partitionInfo->Status = NKikimrPQ::ETopicPartitionStatus::Active; partitionInfo->KeyRange.Clear(); partitionInfo->ParentPartitionIds.clear(); partitionInfo->ChildPartitionIds.clear(); } else if (const auto* range = pqGroup->AlterData->KeyRangesToChange.FindPtr(partitionInfo->PqId)) { - context.MemChanges.RecordUndo([pqGroup, id = partitionInfo->PqId, previous = partitionInfo->KeyRange]() { - pqGroup->Partitions.at(id)->KeyRange = previous; - }); partitionInfo->KeyRange = *range; } context.SS->PersistPersQueue(db, item->PathId, shardIdx, *partitionInfo.Get()); @@ -556,17 +541,11 @@ class TAlterPQ: public TSubOperation { txState.Shards.emplace_back(idx, ETabletType::PersQueue, TTxState::CreateParts); context.SS->RegisterShardInfo(idx, defaultShardInfo); - context.MemChanges.RecordUndo([pqGroup, idx]() { - pqGroup->Shards.erase(idx); - }); pqGroup->Shards[idx] = new TTopicTabletInfo(); } if (!hasBalancer) { const auto idx = context.SS->NextShardIdx(startShardIdx, pqShardsToCreate); - context.MemChanges.RecordUndo([pqGroup, previous = pqGroup->BalancerShardIdx]() { - pqGroup->BalancerShardIdx = previous; - }); pqGroup->BalancerShardIdx = idx; txState.Shards.emplace_back(idx, ETabletType::PersQueueReadBalancer, TTxState::CreateParts); context.SS->RegisterShardInfo(idx, @@ -585,12 +564,12 @@ class TAlterPQ: public TSubOperation { << " hasBalancer " << hasBalancer); if (!pqGroup->AlterData->PartitionsToAdd.empty()) { - ReassignIds(pqGroup, context); + ReassignIds(pqGroup); } return shardsToCreate > 0; } - void ReassignIds(TTopicInfo::TPtr pqGroup, TOperationContext& context) { + void ReassignIds(TTopicInfo::TPtr pqGroup) { Y_ABORT_UNLESS(pqGroup->TotalPartitionCount >= pqGroup->TotalGroupCount); ui32 numOld = pqGroup->TotalPartitionCount; ui32 numNew = pqGroup->AlterData->PartitionsToAdd.size() + numOld; @@ -620,34 +599,12 @@ class TAlterPQ: public TSubOperation { for (const auto parentId : partition->ParentPartitionIds) { auto* parent = pqGroup->Partitions[parentId]; - context.MemChanges.RecordUndo([pqGroup, parentId, status = parent->Status, version = parent->AlterVersion]() { - auto* parent = pqGroup->Partitions.at(parentId); - parent->Status = status; - parent->AlterVersion = version; - }); parent->Status = NKikimrPQ::ETopicPartitionStatus::Inactive; parent->AlterVersion = alterVersion; } - context.MemChanges.RecordUndo([pqGroup, tablet = it->second, - id = partition->PqId, size = it->second->Partitions.size()]() { - pqGroup->Partitions.erase(id); - tablet->Partitions.resize(size); - }); pqGroup->AddPartition(it->first, partition.Release()); } - // InitSplitMergeGraph adds reverse edges, including edges on existing - // partition objects shared by the topic's lookup structures. - for (const auto& [id, partition] : pqGroup->Partitions) { - for (const auto parentId : partition->ParentPartitionIds) { - auto* parent = pqGroup->Partitions.at(parentId); - if (!parent->ChildPartitionIds.contains(id)) { - context.MemChanges.RecordUndo([pqGroup, parentId, id]() { - pqGroup->Partitions.at(parentId)->ChildPartitionIds.erase(id); - }); - } - } - } pqGroup->InitSplitMergeGraph(); } @@ -703,7 +660,7 @@ class TAlterPQ: public TSubOperation { } } - auto topic = context.SS->Topics.Update(path.Base()->PathId); + auto& topic = context.SS->Topics.Update(path.Base()->PathId, context.MemChanges); Y_ABORT_UNLESS(topic); if (topic->AlterVersion == 0) { @@ -1258,9 +1215,6 @@ class TAlterPQ: public TSubOperation { pqChannelsBinding = tabletChannelsBinding; } - context.MemChanges.RecordUndo([topic, previous = topic->AlterData]() { - topic->AlterData = previous; - }); topic->PrepareAlter(alterData); const TTxState& txState = PrepareChanges(OperationId, path, topic, shardsToCreate, tabletChannelsBinding, pqChannelsBinding, context, tabletConfig, newTabletConfig); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_replication.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_replication.cpp index 80f1fdaa1485a..d75bfbce0e06b 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_replication.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_replication.cpp @@ -407,7 +407,7 @@ class TAlterReplication: public TSubOperation { } Y_ABORT_UNLESS(context.SS->Replications.contains(path.Base()->PathId)); - auto replication = context.SS->Replications.Update(path.Base()->PathId); + auto& replication = context.SS->Replications.Update(path.Base()->PathId, context.MemChanges); if (replication->AlterVersion == 0) { result->SetError(NKikimrScheme::StatusMultipleModifications, "Replication is not created yet"); @@ -442,9 +442,6 @@ class TAlterReplication: public TSubOperation { return result; } - context.MemChanges.RecordUndo([replication, previous = replication->AlterData]() { - replication->AlterData = previous; - }); auto alterData = replication->CreateNextVersion(); if (op.HasState()) { diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_secret.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_secret.cpp index ecc83158244fa..6bd90f1645966 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_secret.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_secret.cpp @@ -152,7 +152,7 @@ class TAlterSecret: public TSubOperation { } Y_ABORT_UNLESS(context.SS->Secrets.contains(secretPath.Base()->PathId)); - auto secretInfo = context.SS->Secrets.Update(secretPath.Base()->PathId); + auto& secretInfo = context.SS->Secrets.Update(secretPath.Base()->PathId, context.MemChanges); if (secretInfo->AlterVersion == 0) { result->SetError(NKikimrScheme::StatusMultipleModifications, "Secret is not created yet"); @@ -195,9 +195,6 @@ class TAlterSecret: public TSubOperation { } } - context.MemChanges.RecordUndo([secretInfo, previous = secretInfo->AlterData]() { - secretInfo->AlterData = previous; - }); auto alterData = secretInfo->CreateNextVersion(); alterData->Description.SetValue(alterSecretProto.GetValue()); alterData->Description.SetVersion(secretInfo->AlterVersion); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_sequence.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_sequence.cpp index 0ecae9679d25b..f2cd708fcc227 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_sequence.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_sequence.cpp @@ -491,7 +491,7 @@ class TAlterSequence: public TSubOperation { } Y_ABORT_UNLESS(context.SS->Sequences.contains(dstPath->PathId)); - auto sequenceInfo = context.SS->Sequences.Update(dstPath->PathId); + auto& sequenceInfo = context.SS->Sequences.Update(dstPath->PathId, context.MemChanges); Y_ABORT_UNLESS(!sequenceInfo->AlterData); const NScheme::TTypeRegistry* typeRegistry = AppData()->TypeRegistry; @@ -503,9 +503,6 @@ class TAlterSequence: public TSubOperation { return result; } - context.MemChanges.RecordUndo([sequenceInfo, previous = sequenceInfo->AlterData]() { - sequenceInfo->AlterData = previous; - }); TSequenceInfo::TPtr alterData = sequenceInfo->CreateNextVersion(); Y_ABORT_UNLESS(alterData); alterData->Description = *description; diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_solomon.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_solomon.cpp index e674a391159e3..62472630b724f 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_solomon.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_solomon.cpp @@ -225,7 +225,7 @@ class TAlterSolomon: public TSubOperation { } } - auto solomon = context.SS->SolomonVolumes.Update(path.Base()->PathId); + auto& solomon = context.SS->SolomonVolumes.Update(path.Base()->PathId, context.MemChanges); if (!alter.HasPartitionCount() && !alter.GetUpdateChannelsBinding()) { result->SetError(NKikimrScheme::StatusInvalidParameter, "Empty alter"); @@ -322,9 +322,6 @@ class TAlterSolomon: public TSubOperation { path.Base()->IncShardsInside(shardsToCreate); } - context.MemChanges.RecordUndo([solomon, previous = solomon->AlterData]() { - solomon->AlterData = previous; - }); solomon->AlterData = alterSolomon; context.SS->PersistAlterSolomonVolume(db, path.Base()->PathId, solomon); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_subdomain.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_subdomain.cpp index 402bb07aed44d..cb221341ce9b6 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_subdomain.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_subdomain.cpp @@ -127,7 +127,7 @@ class TAlterSubDomain: public TSubOperation { TPathElement::TPtr subDomain = path.Base(); Y_ABORT_UNLESS(context.SS->SubDomains.contains(subDomain->PathId)); - auto subDomainInfo = context.SS->SubDomains.Update(subDomain->PathId); + auto& subDomainInfo = context.SS->SubDomains.Update(subDomain->PathId, context.MemChanges); Y_ABORT_UNLESS(subDomainInfo); if (subDomainInfo->GetAlter()) { @@ -340,7 +340,6 @@ class TAlterSubDomain: public TSubOperation { PersistShards(db, txState, shardsToCreate, context.SS); context.SS->PersistUpdateNextShardIdx(db); } - context.MemChanges.GrabDomain(context.SS, subDomain->PathId); subDomainInfo->SetAlter(alterData); context.SS->PersistSubDomainAlter(db, subDomain->PathId, *alterData); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_alter_table.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_alter_table.cpp index 4123f423d208a..52231c9e332b7 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_alter_table.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_alter_table.cpp @@ -701,7 +701,7 @@ class TAlterTable: public TSubOperation { } Y_ABORT_UNLESS(context.SS->Tables.contains(path.Base()->PathId)); - auto table = context.SS->Tables.Update(path.Base()->PathId); + auto& table = context.SS->Tables.Update(path.Base()->PathId, context.MemChanges); if (context.SS->IsTableInBackupCollection(path.Base()->PathId)) { result->SetError(NKikimrScheme::StatusPreconditionFailed, @@ -774,11 +774,6 @@ class TAlterTable: public TSubOperation { } } - // The schema change is staged in AlterData; undo only its attachment, - // without copying the table's partitioning or per-shard statistics. - context.MemChanges.RecordUndo([table, previousAlterData = table->AlterData]() { - table->AlterData = previousAlterData; - }); table->PrepareAlter(alterData); PrepareChanges(OperationId, path.Base(), table, bindingChanges, context); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_assign_bsv.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_assign_bsv.cpp index 14d80cd2c7626..f72efbbdff7ed 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_assign_bsv.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_assign_bsv.cpp @@ -50,7 +50,7 @@ class TAssignBlockStoreVolume: public TSubOperationBase { return result; } - auto volume = context.SS->BlockStoreVolumes.Update(path.Base()->PathId); + auto& volume = context.SS->BlockStoreVolumes.Update(path.Base()->PathId, context.MemChanges); if (volume->AlterVersion == 0) { result->SetError(NKikimrScheme::StatusMultipleModifications, "Block store volume is not created yet"); return result; @@ -74,10 +74,6 @@ class TAssignBlockStoreVolume: public TSubOperationBase { NIceDb::TNiceDb db(context.GetDB()); - context.MemChanges.RecordUndo([volume, token = volume->MountToken, version = volume->TokenVersion]() { - volume->MountToken = token; - volume->TokenVersion = version; - }); volume->MountToken = mountToken; ++volume->TokenVersion; context.SS->PersistBlockStoreVolumeMountToken(db, path.Base()->PathId, volume); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_common.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_common.cpp index cacf40cdad3d0..3669b7547c68a 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_common.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_common.cpp @@ -1044,7 +1044,7 @@ void UpdatePartitioningForCopyTable(TOperationId operationId, TTxState &txState, txState.TxShardsListFinalized = true; } -TVector ApplyPartitioningCopyTable(const TShardInfo &templateDatashardInfo, TTableInfo::TCPtr srcTableInfo, TTxState &txState, TSchemeShard *ss) { +TVector ApplyPartitioningCopyTable(const TShardInfo &templateDatashardInfo, TTableInfo::TPtr srcTableInfo, TTxState &txState, TSchemeShard *ss) { // Build a mutable copy of src partitions for the dst table. TVector dstPartitions; { diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_common.h b/ydb/core/tx/schemeshard/schemeshard__operation_common.h index 9d64cff14368a..b81d855583657 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_common.h +++ b/ydb/core/tx/schemeshard/schemeshard__operation_common.h @@ -83,7 +83,7 @@ void AckAllSchemaChanges(const TOperationId& operationId, TTxState& txState, TOp bool CheckPartitioningChangedForTableModification(TTxState& txState, TOperationContext& context); void UpdatePartitioningForTableModification(TOperationId txId, TTxState& txState, TOperationContext& context); -TVector ApplyPartitioningCopyTable(const TShardInfo& templateDatashardInfo, TTableInfo::TCPtr srcTableInfo, TTxState& txState, TSchemeShard* ss); +TVector ApplyPartitioningCopyTable(const TShardInfo& templateDatashardInfo, TTableInfo::TPtr srcTableInfo, TTxState& txState, TSchemeShard* ss); bool SourceTablePartitioningChangedForCopyTable(const TTxState& txState, TOperationContext& context); void UpdatePartitioningForCopyTable(TOperationId operationId, TTxState& txState, TOperationContext& context); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp index 4525591782c12..c8fddfca3a3d1 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_copy_table.cpp @@ -14,7 +14,7 @@ namespace { using namespace NKikimr; using namespace NSchemeShard; -void PrepareScheme(NKikimrSchemeOp::TTableDescription* schema, const TString& name, const TTableInfo::TCPtr srcTableInfo, TOperationContext &context) { +void PrepareScheme(NKikimrSchemeOp::TTableDescription* schema, const TString& name, const TTableInfo::TPtr srcTableInfo, TOperationContext &context) { const NScheme::TTypeRegistry* typeRegistry = AppData(context.Ctx)->TypeRegistry; NKikimrSchemeOp::TTableDescription completedSchema; @@ -141,6 +141,7 @@ class TConfigureParts: public TSubOperationState { FillSrcSnapshot(txState, ui64(dstDatashardId), *combined.MutableSendSnapshot()); // Get coordinated version from source table's AlterData (shared across both drop and create) + // GrabTable is needed for proper rollback if operation fails auto& srcTable = context.SS->Tables.UpdateUntracked(txState->SourcePathId); srcTable->InitAlterData(OperationId); ui64 coordVersion = srcTable->AlterData->CoordinatedSchemaVersion.GetOrElse(srcTable->AlterVersion + 1); @@ -671,7 +672,8 @@ class TCopyTable: public TSubOperation { } Y_ABORT_UNLESS(context.SS->Tables.contains(srcPath.Base()->PathId)); - auto srcTableInfo = context.SS->Tables.at(srcPath.Base()->PathId); + // Copy, not a reference: Tables.Set() below can rehash and invalidate a slot ref. + auto srcTableInfo = context.SS->Tables.Update(srcPath.Base()->PathId, context.MemChanges); { const NKikimrSchemeOp::TPartitionConfig &srcPartitionConfig = srcTableInfo->PartitionConfig(); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_create_bsv.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_create_bsv.cpp index 27d54bea9b4a7..d9e797e4619fa 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_create_bsv.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_create_bsv.cpp @@ -139,8 +139,8 @@ TTxState& PrepareChanges(TOperationId operationId, TPathElement::TPtr parentDir, TBlockStoreVolumeInfo::TPtr emptyVolume = new TBlockStoreVolumeInfo(); emptyVolume->Shards.swap(volume->Shards); - emptyVolume->AlterData = volume; context.SS->BlockStoreVolumes.Set({.Path = pathId, .Value = emptyVolume, .Changes = context.MemChanges}); + context.SS->BlockStoreVolumes.Update(pathId, context.MemChanges)->AlterData = volume; context.SS->PersistBlockStoreVolume(db, pathId, emptyVolume); context.SS->PersistAddBlockStoreVolumeAlter(db, pathId, volume); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_create_pq.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_create_pq.cpp index 91507a7828dc6..b9bb355fd9d08 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_create_pq.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_create_pq.cpp @@ -483,8 +483,8 @@ class TCreatePQ: public TSubOperation { TTopicInfo::TPtr emptyGroup = new TTopicInfo; emptyGroup->Shards.swap(pqGroup->Shards); - emptyGroup->AlterData = pqGroup; context.SS->Topics.Set({.Path = pathId, .Value = emptyGroup, .Changes = context.MemChanges}); + context.SS->Topics.Update(pathId, context.MemChanges)->AlterData = pqGroup; context.DbChanges.PersistPersQueueGroup(pathId, emptyGroup); context.DbChanges.PersistAddPersQueueGroupAlter(pathId, pqGroup); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_drop_sequence.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_drop_sequence.cpp index e62846bc1284d..906869d3edd99 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_drop_sequence.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_drop_sequence.cpp @@ -324,7 +324,7 @@ class TDropSequence: public TSubOperation { if (parent->IsTable() && !parent.IsUnderDeleting()) { // TODO: we probably want some kind of usage refcount - auto tableInfo = context.SS->Tables.at(parent->PathId); + auto& tableInfo = context.SS->Tables.Update(parent->PathId, context.MemChanges); if (tableInfo->IsUsingSequence(path->Name)) { TString explain = TStringBuilder() << "cannot delete sequence " << path->Name << " used by table " << parent.PathString(); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.cpp index ca0091c0aa315..0397c08a7f682 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.cpp @@ -158,11 +158,10 @@ void TMemoryChanges::UnDo(TSchemeShard* ss) { Paths.pop(); } - // Restore membership and explicitly recorded fields in reverse mutation order. - // Paths above already restored the counters, so membership undo does not release. - while (UndoActions) { - UndoActions.top()(); - UndoActions.pop(); + // Self-ref map rollbacks recorded by TDbRefMap::Set, undone LIFO. + while (DbRefUndos) { + DbRefUndos.top()(); + DbRefUndos.pop(); } while (TablesWithSnapshots) { diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.h b/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.h index 4291ff1e58eab..5bc2f1876851a 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.h +++ b/ydb/core/tx/schemeshard/schemeshard__operation_memory_changes.h @@ -7,10 +7,12 @@ #include #include +#include #include #include #include + #include namespace NKikimr::NSchemeShard { @@ -33,15 +35,19 @@ class TMemoryChanges: public TSimpleRefCount { }; TPathStack Paths; + + using TTableSnapshotState = std::pair; TStack TablesWithSnapshots; using TLockState = std::pair; TStack LockedPaths; + using TColumnTableState = std::pair; TStack ColumnTables; + using TShardState = std::pair>; TStack Shards; @@ -55,6 +61,12 @@ class TMemoryChanges: public TSimpleRefCount { using TTxState = std::pair>; TStack TxStates; + + + + + + using TLongIncrementalRestoreOpState = std::pair>; TStack LongIncrementalRestoreOps; @@ -69,11 +81,17 @@ class TMemoryChanges: public TSimpleRefCount { using TBCPathToFullBackupState = std::pair>; TStack BCPathToFullBackup; + + using TSharedShardEntry = std::tuple>; TStack SharedShardEntries; - // Common LIFO stack for map membership changes and explicit field undo. - TStack> UndoActions; + + TStack> DbRefUndos; + + // Dedup: at most one value snapshot per (self-ref map, path) per tx, so + // repeated Update() on the same object doesn't re-copy it. + THashMap> UpdateSnapshotted; // Only the propose tx can roll back (UnDo runs only from AbortOperationPropose), // so only it records undos; other txs would just accumulate dead weight. @@ -102,11 +120,17 @@ class TMemoryChanges: public TSimpleRefCount { // that acquires a ref on an ungrabbed path can't fully roll back. bool IsPathTracked(const TPathId& id) const { return Paths.Contains(id); } + // True the first time this (map, path) needs a snapshot; false when disarmed. + bool NeedsUpdateSnapshot(const void* map, const TPathId& id) { + return Armed && UpdateSnapshotted[map].insert(id).second; + } + void GrabNewTxState(TSchemeShard* ss, const TOperationId& op); void GrabNewPath(TSchemeShard* ss, const TPathId& pathId); void GrabPath(TSchemeShard* ss, const TPathId& pathId); + void GrabNewColumnTable(TSchemeShard* ss, const TPathId& pathId); void GrabColumnTable(TSchemeShard* ss, const TPathId& pathId); @@ -115,11 +139,20 @@ class TMemoryChanges: public TSimpleRefCount { void GrabDomain(TSchemeShard* ss, const TPathId& pathId); + + + void GrabNewTableSnapshot(TSchemeShard* ss, const TPathId& pathId, TTxId snapshotTxId); void GrabNewLongLock(TSchemeShard* ss, const TPathId& pathId); void GrabLongLock(TSchemeShard* ss, const TPathId& pathId, TTxId lockTxId); + + + + + + void GrabNewLongIncrementalRestoreOp(TSchemeShard* ss, const TOperationId& opId); void GrabLongIncrementalRestoreOp(TSchemeShard* ss, const TOperationId& opId); @@ -128,14 +161,17 @@ class TMemoryChanges: public TSimpleRefCount { void GrabNewFullBackupOp(TSchemeShard* ss, ui64 id); void GrabNewBCPathToFullBackup(TSchemeShard* ss, const TPathId& bcPathId); + + void GrabNewSharedShard(TSchemeShard* ss, const TShardIdx& shardIdx, const TPathId& pathId); void GrabSharedShard(TSchemeShard* ss, const TShardIdx& shardIdx, const TPathId& pathId); - // Record map membership changes and explicit field undo on one LIFO stack, - // so mutations are undone before an earlier insertion/replacement is undone. - void RecordUndo(std::function undo) { + + // TDbRefMap::Set records its own rollback closure here (undone LIFO), + // replacing the per-map GrabNew*/Grab* + UnDo branches. + void RecordDbRefUndo(std::function undo) { if (Armed) { - UndoActions.push(std::move(undo)); + DbRefUndos.push(std::move(undo)); } } diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_move_sequence.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_move_sequence.cpp index 51ed1f09ff0e2..d04d47322ea6a 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_move_sequence.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_move_sequence.cpp @@ -978,16 +978,13 @@ class TMoveSequence: public TSubOperation { txState.State = TTxState::CreateParts; Y_ABORT_UNLESS(context.SS->Sequences.contains(srcPath.Base()->PathId)); - auto srcSequence = context.SS->Sequences.Update(srcPath.Base()->PathId); + auto& srcSequence = context.SS->Sequences.Update(srcPath.Base()->PathId, context.MemChanges); Y_ABORT_UNLESS(!srcSequence->Sharding.GetSequenceShards().empty()); const auto& protoSequenceShard = *srcSequence->Sharding.GetSequenceShards().rbegin(); TShardIdx sequenceShard = FromProto(protoSequenceShard); TSequenceInfo::TPtr sequenceInfo = new TSequenceInfo(0); - context.MemChanges.RecordUndo([srcSequence, previous = srcSequence->AlterData]() { - srcSequence->AlterData = previous; - }); sequenceInfo->AlterData = srcSequence->CreateNextVersion(); txState.Shards.emplace_back(sequenceShard, ETabletType::SequenceShard, TTxState::ConfigureParts); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_rotate_cdc_stream.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_rotate_cdc_stream.cpp index d8c1d09809777..e520970f8faca 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_rotate_cdc_stream.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_rotate_cdc_stream.cpp @@ -221,7 +221,7 @@ class TRotateCdcStream: public TSubOperation { Y_ABORT_UNLESS(context.SS->CdcStreams.contains(oldStreamPath.Base()->PathId)); - auto oldStream = context.SS->CdcStreams.Update(oldStreamPath.Base()->PathId); + auto& oldStream = context.SS->CdcStreams.Update(oldStreamPath.Base()->PathId, context.MemChanges); TCdcStreamInfo::EState requiredState = TCdcStreamInfo::EState::ECdcStreamStateDisabled; TCdcStreamInfo::EState newState = TCdcStreamInfo::EState::ECdcStreamStateInvalid; @@ -372,9 +372,6 @@ class TRotateCdcStream: public TSubOperation { context.DbChanges.PersistAlterCdcStream(pathId); context.DbChanges.PersistTxState(OperationId); - context.MemChanges.RecordUndo([oldStream, previous = oldStream->AlterData]() { - oldStream->AlterData = previous; - }); auto streamAlter = oldStream->CreateNextVersion(); Y_ABORT_UNLESS(streamAlter); streamAlter->State = newState; diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_split_merge.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_split_merge.cpp index 394cd10e61394..43110692684c7 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_split_merge.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_split_merge.cpp @@ -1083,7 +1083,7 @@ class TSplitMerge: public TSubOperation { context.DbChanges.PersistShard(shard.Idx); } - auto& mutableTableInfo = context.SS->Tables.Update(path->PathId); + auto& mutableTableInfo = context.SS->Tables.Update(path->PathId, context.MemChanges); mutableTableInfo->RegisterSplitMergeOp(OperationId, op); diff --git a/ydb/core/tx/schemeshard/schemeshard__operation_upgrade_subdomain.cpp b/ydb/core/tx/schemeshard/schemeshard__operation_upgrade_subdomain.cpp index a1c20d60f02ea..f306fc165525b 100644 --- a/ydb/core/tx/schemeshard/schemeshard__operation_upgrade_subdomain.cpp +++ b/ydb/core/tx/schemeshard/schemeshard__operation_upgrade_subdomain.cpp @@ -1164,7 +1164,7 @@ class TUpgradeSubDomain: public TSubOperation { auto pathId = path.Base()->PathId; Y_ABORT_UNLESS(context.SS->SubDomains.contains(pathId)); - auto subDomain = context.SS->SubDomains.Update(pathId); + auto subDomain = context.SS->SubDomains.Update(pathId, context.MemChanges); if (!subDomain->IsSupportTransactions()) { result->SetError(NKikimrScheme::StatusSchemeError, "There are no sense to upgrade subdomain with out transactions support (NBS?)."); return result; @@ -1232,7 +1232,6 @@ class TUpgradeSubDomain: public TSubOperation { const TShardInfo& shardInfo = context.SS->ShardInfos.at(shardIdx); txState.Shards.emplace_back(shardIdx, TTabletTypes::SchemeShard, TTxState::CreateParts); alterData->AddPrivateShard(shardIdx); - context.MemChanges.GrabDomain(context.SS, pathId); subDomain->SetAlter(alterData); diff --git a/ydb/core/tx/schemeshard/schemeshard_db_ref_map.h b/ydb/core/tx/schemeshard/schemeshard_db_ref_map.h index 4ddaaaf1596a0..5e26bb7487a9f 100644 --- a/ydb/core/tx/schemeshard/schemeshard_db_ref_map.h +++ b/ydb/core/tx/schemeshard/schemeshard_db_ref_map.h @@ -24,6 +24,36 @@ namespace NDbRefDetail { template struct TConstView> { using type = std::shared_ptr; }; } +// Clone a value for an undo snapshot; TTableInfo uses DeepCopy (COW-shares its +// partitioning), other types copy-construct. +template +TIntrusivePtr DbRefUndoClone(const TIntrusivePtr& p) { + return p ? TIntrusivePtr(new T(*p)) : TIntrusivePtr(); +} +template +std::shared_ptr DbRefUndoClone(const std::shared_ptr& p) { + return p ? std::make_shared(*p) : std::shared_ptr(); +} +inline TIntrusivePtr DbRefUndoClone(const TIntrusivePtr& p) { + return p ? TTableInfo::DeepCopy(*p) : TIntrusivePtr(); +} + +// Restore an Update() snapshot on abort. Default: swap the pointer back. +// TTableInfo restores contents into the still-live object instead, because +// TTLEnabledTables aliases that object by raw pointer and must stay in sync; +// operator= on the TSimpleRefCount base is a no-op, so the live refcount is kept. +template +void DbRefUndoRestoreSlot(V& slot, const V& snap) { + slot = snap; +} +inline void DbRefUndoRestoreSlot(TIntrusivePtr& slot, const TIntrusivePtr& snap) { + if (slot && snap) { + *slot = *snap; + } else { + slot = snap; + } +} + // Teardown interface: maps self-register so Clear() iterates one registry. class IDbRefMap { public: @@ -39,9 +69,8 @@ class IDbRefMap { // THashMap holding a DbRefCount self-ref per entry: insert acquires, // erase releases. No operator[], so a missing-key read can't silently acquire. -// Set records membership undo; Update leaves mutation undo to its caller where -// proposal rollback is supported. Both belong to the armed propose phase. -// SetUntracked/UpdateUntracked/erase are for other phases (init, plan-step, progress, stats). +// Set/Update record undo and belong to the armed propose phase; SetUntracked/ +// UpdateUntracked/erase are for other phases (init, plan-step, progress, stats). template class TDbRefMap : public IDbRefMap { using TInner = THashMap; @@ -90,11 +119,11 @@ class TDbRefMap : public IDbRefMap { Y_VERIFY_DEBUG_S(args.Changes.IsPathTracked(args.Path), "Set(" << Reason.c_str() << ") acquires a ref on " << args.Path << " but the path was not grabbed in this tx; GrabNewPath/GrabPath it first"); - args.Changes.RecordUndo([this, id = args.Path]() { UndoErase(id); }); + args.Changes.RecordDbRefUndo([this, id = args.Path]() { UndoErase(id); }); it = Map.emplace(args.Path, std::move(args.Value)).first; AcquirePathDbRef(SS, args.Path, Reason); } else { - args.Changes.RecordUndo([this, id = args.Path, old = it->second]() { UndoRestore(id, old); }); + args.Changes.RecordDbRefUndo([this, id = args.Path, old = it->second]() { UndoRestore(id, old); }); it->second = std::move(args.Value); } return it->second; @@ -122,19 +151,23 @@ class TDbRefMap : public IDbRefMap { return it->second; } - // Mutable access during propose. This does NOT snapshot the object: callers - // supporting proposal rollback must record undo for the fields they change. - // Existing operations that prohibit proposal rollback keep that contract. - // Returns const V& so the pointee stays mutable (->Field), but replacing the - // slot still requires Set(). - const V& Update(const TPathId& id) { - Y_VERIFY_DEBUG_S(IsProposeArmed(SS), - "Update on " << Reason.c_str() << " outside an armed propose; use UpdateUntracked"); - return Map.at(id); + // Mutable access that records a deduped undo snapshot. The only tracked way to mutate. + // Returns const V&: the pointee stays mutable (->Field), but the slot can't be + // reseated (Update(id,mc) = newPtr won't compile), which would desync the undo. + const V& Update(const TPathId& id, TMemoryChanges& changes) { + Y_VERIFY_DEBUG_S(changes.IsArmed(), + "tracked Update on " << Reason.c_str() << " outside an armed propose; use UpdateUntracked"); + V& slot = Map.at(id); + if (changes.NeedsUpdateSnapshot(this, id)) { + changes.RecordDbRefUndo([this, id, snap = DbRefUndoClone(slot)]() { + UndoRestoreInPlace(id, snap); + }); + } + return slot; } // Pointee-mutable access without undo, for non-transactional callers (init, stats). - // Const V& (no slot reseat); propose operations use Update(). + // Const V& (no slot reseat); operations use Update() so their mutation is undoable. const V& UpdateUntracked(const TPathId& id) { return Map.at(id); } @@ -196,6 +229,16 @@ class TDbRefMap : public IDbRefMap { return slot; } + // Update-undo: restore pre-mutation contents into the SAME live object rather than + // swapping the pointer, so secondary aliases of it (e.g. TTLEnabledTables) don't + // desync. Falls back to a pointer restore if either side is null. + void UndoRestoreInPlace(const TPathId& id, const V& snap) { + auto it = Map.find(id); + if (it != Map.end()) { + DbRefUndoRestoreSlot(it->second, snap); + } + } + // Drop a tx-created entry without releasing (Paths owns the counter). void UndoErase(const TPathId& id) { Map.erase(id); diff --git a/ydb/core/tx/schemeshard/schemeshard_info_types.cpp b/ydb/core/tx/schemeshard/schemeshard_info_types.cpp index 6e0a1929c5724..a1fbc2f5ac8fd 100644 --- a/ydb/core/tx/schemeshard/schemeshard_info_types.cpp +++ b/ydb/core/tx/schemeshard/schemeshard_info_types.cpp @@ -2143,22 +2143,25 @@ void TTableInfo::DeserializeAlterExtraData(const TString& str) { #endif void TTableInfo::SetPartitioning(TVector&& newPartitioning) { - Y_ENSURE(PartitionStore.empty()); - Y_ENSURE(Partitions.empty()); + Y_ENSURE(Partitioning->Store.empty()); + Y_ENSURE(Partitioning->Order.empty()); Y_ENSURE(SplitOpsInFlight.empty()); + // Build into a fresh, unique partitioning — never into one shared with a snapshot. + Partitioning = std::make_shared(); + for (const auto& p : newPartitioning) { Stats.PartitionStats.emplace(p.ShardIdx, TPartitionStats{}); } Stats.Aggregated.PartCount = newPartitioning.size(); ExpectedPartitionCount = newPartitioning.size(); - PartitionStore.reserve(newPartitioning.size()); - Partitions.reserve(newPartitioning.size()); + Partitioning->Store.reserve(newPartitioning.size()); + Partitioning->Order.reserve(newPartitioning.size()); for (auto& p : newPartitioning) { const TShardIdx idx = p.ShardIdx; - auto [it, _] = PartitionStore.emplace(idx, std::move(p)); - Partitions.push_back(&it->second); + auto [it, _] = Partitioning->Store.emplace(idx, std::move(p)); + Partitioning->Order.push_back(&it->second); } newPartitioning.clear(); @@ -2169,14 +2172,14 @@ void TTableInfo::SetPartitioning(TVector&& newPartitioning) { // Only schedule TTL sweeps when TTL is actually enabled — non-TTL tables // don't need entries in CondEraseSchedule at all. if (IsTTLEnabled()) { - for (auto* p : Partitions) { + for (auto* p : Partitioning->Order) { CondEraseSchedule.PushRaw(p->NextCondErase, p->ShardIdx); } CondEraseSchedule.BuildHeap(); } - for (ui64 i = 0; i < Partitions.size(); ++i) { - Partitions[i]->Position = i; + for (ui64 i = 0; i < Partitioning->Order.size(); ++i) { + Partitioning->Order[i]->Position = i; } VerifyConsistency(); @@ -2184,7 +2187,7 @@ void TTableInfo::SetPartitioning(TVector&& newPartitioning) { void TTableInfo::MovePartitioning(TVector&& newPartitioning) { // Same shard set as before — Stats are already correct (caller is a DeepCopy). - Y_ABORT_UNLESS(newPartitioning.size() == PartitionStore.size()); + Y_ABORT_UNLESS(newPartitioning.size() == Partitioning->Store.size()); THashMap newStore; newStore.reserve(newPartitioning.size()); @@ -2196,8 +2199,12 @@ void TTableInfo::MovePartitioning(TVector&& newPartitioning) { newOrder.push_back(&it->second); } newPartitioning.clear(); - PartitionStore = std::move(newStore); - Partitions = std::move(newOrder); + { + auto next = std::make_shared(); + next->Store = std::move(newStore); + next->Order = std::move(newOrder); + Partitioning = std::move(next); + } // Discard any in-flight TTL state and reschedule all shards. if (IsTTLEnabled()) { @@ -2205,8 +2212,8 @@ void TTableInfo::MovePartitioning(TVector&& newPartitioning) { ScheduleAllCondErase(); } - for (ui64 i = 0; i < Partitions.size(); ++i) { - Partitions[i]->Position = i; + for (ui64 i = 0; i < Partitioning->Order.size(); ++i) { + Partitioning->Order[i]->Position = i; } PreserializedTablePartitions.clear(); @@ -2238,16 +2245,20 @@ void TTableInfo::CopyPartitioning(TVector&& newPartitioning) { Stats.PartitionStats.emplace(idx, TPartitionStats{}); } newPartitioning.clear(); - PartitionStore = std::move(newStore); - Partitions = std::move(newOrder); + { + auto next = std::make_shared(); + next->Store = std::move(newStore); + next->Order = std::move(newOrder); + Partitioning = std::move(next); + } if (IsTTLEnabled()) { ClearCondEraseSchedule(); ScheduleAllCondErase(); } - for (ui64 i = 0; i < Partitions.size(); ++i) { - Partitions[i]->Position = i; + for (ui64 i = 0; i < Partitioning->Order.size(); ++i) { + Partitioning->Order[i]->Position = i; } PreserializedTablePartitions.clear(); @@ -2266,12 +2277,15 @@ void TTableInfo::ApplySplitMerge( const ui64 kRemoved = removedShards.size(); const ui64 kAdded = dstPartitions.size(); + // In-place splice below — detach from any shared snapshot first. + EnsureUniquePartitioning(); + // Src shards are contiguous at [splitFirstIdx, splitFirstIdx + kRemoved). - Y_ABORT_UNLESS(splitFirstIdx < Partitions.size()); + Y_ABORT_UNLESS(splitFirstIdx < Partitioning->Order.size()); const ui64 splitEndIdx = splitFirstIdx + kRemoved; - Y_ABORT_UNLESS(splitEndIdx <= Partitions.size()); + Y_ABORT_UNLESS(splitEndIdx <= Partitioning->Order.size()); for (const TShardIdx& s : removedShards) { - const auto* p = PartitionStore.FindPtr(s); + const auto* p = Partitioning->Store.FindPtr(s); Y_ABORT_UNLESS(p && p->Position >= splitFirstIdx && p->Position < splitEndIdx); } @@ -2280,21 +2294,20 @@ void TTableInfo::ApplySplitMerge( for (const auto& dst : dstPartitions) { Stats.PartitionStats.emplace(dst.ShardIdx, TPartitionStats{}); } - const ui64 newPartitionCount = Partitions.size() - kRemoved + kAdded; + const ui64 newPartitionCount = Partitioning->Order.size() - kRemoved + kAdded; Stats.Aggregated.PartCount = newPartitionCount; if (SplitOpsInFlight.empty()) { ExpectedPartitionCount = newPartitionCount; } - // PartitionStore: erase src, insert dst for (const TShardIdx& s : removedShards) { - PartitionStore.erase(s); + Partitioning->Store.erase(s); } TVector dstPtrs; dstPtrs.reserve(kAdded); for (auto& dst : dstPartitions) { const TShardIdx idx = dst.ShardIdx; - auto [it, _] = PartitionStore.emplace(idx, std::move(dst)); + auto [it, _] = Partitioning->Store.emplace(idx, std::move(dst)); dstPtrs.push_back(&it->second); } dstPartitions.clear(); @@ -2310,13 +2323,12 @@ void TTableInfo::ApplySplitMerge( } } - // Partitions (pointer vector): erase src ptrs, insert dst ptrs // Moving raw pointers is cheap and does not affect the pointed-to objects. // Update Position for newly-inserted dst shards and all right-shifted survivors. - Partitions.erase(Partitions.begin() + splitFirstIdx, Partitions.begin() + splitEndIdx); - Partitions.insert(Partitions.begin() + splitFirstIdx, dstPtrs.begin(), dstPtrs.end()); - for (ui64 i = splitFirstIdx; i < Partitions.size(); ++i) { - Partitions[i]->Position = i; + Partitioning->Order.erase(Partitioning->Order.begin() + splitFirstIdx, Partitioning->Order.begin() + splitEndIdx); + Partitioning->Order.insert(Partitioning->Order.begin() + splitFirstIdx, dstPtrs.begin(), dstPtrs.end()); + for (ui64 i = splitFirstIdx; i < Partitioning->Order.size(); ++i) { + Partitioning->Order[i]->Position = i; } PreserializedTablePartitions.clear(); @@ -2335,20 +2347,20 @@ void TTableInfo::VerifyConsistency() const { THPTimer cpuTimer; - Y_ABORT_UNLESS(PartitionStore.size() == Partitions.size()); - Y_ABORT_UNLESS(Stats.PartitionStats.size() == Partitions.size()); - Y_ABORT_UNLESS(Stats.Aggregated.PartCount == Partitions.size()); + Y_ABORT_UNLESS(Partitioning->Store.size() == Partitioning->Order.size()); + Y_ABORT_UNLESS(Stats.PartitionStats.size() == Partitioning->Order.size()); + Y_ABORT_UNLESS(Stats.Aggregated.PartCount == Partitioning->Order.size()); - for (size_t i = 0; i < Partitions.size(); ++i) { - const TTableShardInfo* p = Partitions[i]; + for (size_t i = 0; i < Partitioning->Order.size(); ++i) { + const TTableShardInfo* p = Partitioning->Order[i]; Y_ABORT_UNLESS(p); - // Pointer must come from PartitionStore (same address). - Y_ABORT_UNLESS(PartitionStore.FindPtr(p->ShardIdx) == p); + // Pointer must come from Partitioning->Store (same address). + Y_ABORT_UNLESS(Partitioning->Store.FindPtr(p->ShardIdx) == p); Y_ABORT_UNLESS(Stats.PartitionStats.contains(p->ShardIdx)); - if (i + 1 < Partitions.size()) { + if (i + 1 < Partitioning->Order.size()) { Y_ABORT_UNLESS(!p->EndOfRange.empty()); } - // Position must reflect actual index in Partitions. + // Position must reflect actual index in Partitioning->Order. Y_ABORT_UNLESS(p->Position == i); } @@ -2357,8 +2369,8 @@ void TTableInfo::VerifyConsistency() const { if (IsTTLEnabled()) { // Each live shard is either waiting in CondEraseSchedule or currently // being erased (popped into InFlightCondErase by AddInFlightCondErase). - Y_ABORT_UNLESS(CondEraseSchedule.Container().size() + InFlightCondErase.size() == Partitions.size()); - for (const auto* p : Partitions) { + Y_ABORT_UNLESS(CondEraseSchedule.Container().size() + InFlightCondErase.size() == Partitioning->Order.size()); + for (const auto* p : Partitioning->Order) { Y_ABORT_UNLESS(CondEraseSchedule.Contains(p->ShardIdx) || InFlightCondErase.contains(p->ShardIdx)); } } else { @@ -2599,10 +2611,10 @@ void TTableInfo::RegisterSplitMergeOp(TOperationId opId, const TTxState& txState Y_ENSURE(txState.SplitDescription); if (SplitOpsInFlight.empty()) { - Y_ENSURE(Partitions.size() == ExpectedPartitionCount, + Y_ENSURE(Partitioning->Order.size() == ExpectedPartitionCount, "info " << "ExpectedPartitionCount: " << ExpectedPartitionCount - << " Partitions.size(): " << Partitions.size()); + << " Partitions.size(): " << Partitioning->Order.size()); } if (txState.State < TTxState::NotifyPartitioningChanged) { @@ -2646,10 +2658,10 @@ void TTableInfo::FinishSplitMergeOp(TOperationId opId) { AbortSplitMergeOp(opId); if (SplitOpsInFlight.empty()) { - Y_ENSURE(Partitions.size() == ExpectedPartitionCount, + Y_ENSURE(Partitioning->Order.size() == ExpectedPartitionCount, "info " << "ExpectedPartitionCount: " << ExpectedPartitionCount - << " Partitions.size(): " << Partitions.size()); + << " Partitions.size(): " << Partitioning->Order.size()); } } @@ -2766,11 +2778,11 @@ bool TTableInfo::CheckCanMergePartitions(const TSplitSettings& splitSettings, return false; } - if (Partitions.size() <= GetMinPartitionsCount()) { + if (Partitioning->Order.size() <= GetMinPartitionsCount()) { return false; } - const TTableShardInfo* partitionInfo = PartitionStore.FindPtr(shardIdx); + const TTableShardInfo* partitionInfo = Partitioning->Store.FindPtr(shardIdx); if (!partitionInfo) { return false; } diff --git a/ydb/core/tx/schemeshard/schemeshard_info_types.h b/ydb/core/tx/schemeshard/schemeshard_info_types.h index c3d777e43b006..ebc26ebd9452d 100644 --- a/ydb/core/tx/schemeshard/schemeshard_info_types.h +++ b/ydb/core/tx/schemeshard/schemeshard_info_types.h @@ -964,10 +964,32 @@ struct TTableInfo : public TSimpleRefCount { using TPartitionsVec = TVector; void CalculateColumnIdByName() const; - // Stable-address store: THashMap uses separate chaining, so element references - // survive insert. Also serves as the O(1) ShardIdx lookup index. - THashMap PartitionStore; - TPartitionsVec Partitions; // ordered by EndOfRange; raw ptrs into PartitionStore + // Partition set, shared copy-on-write. Store is a stable-address map, Order + // holds raw ptrs into it. DeepCopy shares this O(1); mutations build a fresh + // one (or EnsureUniquePartitioning for the in-place cond-erase update). + struct TPartitioning { + THashMap Store; + TPartitionsVec Order; // ordered by EndOfRange; raw ptrs into Store + }; + std::shared_ptr Partitioning = std::make_shared(); + + static std::shared_ptr ClonePartitioning(const TPartitioning& src) { + auto copy = std::make_shared(); + copy->Store = src.Store; + copy->Order.resize(src.Order.size()); + for (ui64 i = 0; i < src.Order.size(); ++i) { + copy->Order[i] = copy->Store.FindPtr(src.Order[i]->ShardIdx); + } + return copy; + } + // Detach from any shared (snapshot) partitioning before an in-place mutation. + TPartitioning& EnsureUniquePartitioning() { + if (Partitioning.use_count() != 1) { + Partitioning = ClonePartitioning(*Partitioning); + } + return *Partitioning; + } + TCondEraseSchedule CondEraseSchedule; THashMap InFlightCondErase; // shard to pipe client mutable TMaybe TTLColumnId; @@ -978,8 +1000,9 @@ struct TTableInfo : public TSimpleRefCount { TAggregatedStats Stats; bool ShardsStatsDetached = false; + // Ptr into the (possibly shared) store; read-only unless EnsureUniquePartitioning was called. TTableShardInfo* FindPartition(const TShardIdx& shardIdx) { - return PartitionStore.FindPtr(shardIdx); + return Partitioning->Store.FindPtr(shardIdx); } public: @@ -998,14 +1021,10 @@ struct TTableInfo : public TSimpleRefCount { } static TTableInfo::TPtr DeepCopy(const TTableInfo& other) { + // Shares other's Partitioning in O(1); the next structural change + // copies-on-write, so Order's raw ptrs never dangle. The copy is still + // O(partitions) via Stats.PartitionStats and VerifyConsistency below. TTableInfo::TPtr copy(new TTableInfo(other)); - // Partitions holds raw pointers into PartitionStore; after the value copy - // they point into other's store — rebuild them to point into the copy's. - copy->Partitions.resize(other.Partitions.size()); - for (ui64 i = 0; i < other.Partitions.size(); ++i) { - copy->Partitions[i] = copy->PartitionStore.FindPtr(other.Partitions[i]->ShardIdx); - Y_ABORT_UNLESS(copy->Partitions[i]); - } copy->VerifyConsistency(); @@ -1117,21 +1136,20 @@ struct TTableInfo : public TSimpleRefCount { #endif void SetPartitioning(TVector&& newPartitioning); - // Rebuild PartitionStore/Partitions from newPartitioning; Stats are already correct - // (caller is a DeepCopy of a table with the same physical shard set). + // Keeps existing Stats (caller is a DeepCopy of a table with the same physical shard set). void MovePartitioning(TVector&& newPartitioning); - // Rebuild PartitionStore/Partitions and Stats from scratch for all-new shard IDs + // Rebuilds Stats from scratch for all-new shard IDs // (caller is a fresh dst table whose old placeholder shards had zero stats). void CopyPartitioning(TVector&& newPartitioning); - // O(N) consistency check across Partitions, PartitionStore, Stats, and CondEraseSchedule. + // O(N) consistency check across Partitioning->Order, Partitioning->Store, Stats, and CondEraseSchedule. void VerifyConsistency() const; // In-place split/merge: replaces the contiguous src shard range with dst shards. void ApplySplitMerge(TVector&& dstPartitions, const TVector& removedShards, ui64 splitFirstIdx, TInstant now); const TVector& GetPartitions() const { - return Partitions; + return Partitioning->Order; } const TAggregatedStats& GetStats() const { @@ -1172,7 +1190,7 @@ struct TTableInfo : public TSimpleRefCount { } const THashMap& GetPartitionStore() const { - return PartitionStore; + return Partitioning->Store; } ui64 GetExpectedPartitionCount() const { @@ -1233,7 +1251,7 @@ struct TTableInfo : public TSimpleRefCount { // We also want auto merge enabled when table has more shards than the // specified maximum number of partitions. This way when something // splits by size over the limit we merge some smaller partitions. - return Partitions.size() > GetMaxPartitionsCount() && !params.DisableForceShardSplit; + return Partitioning->Order.size() > GetMaxPartitionsCount() && !params.DisableForceShardSplit; } NKikimrSchemeOp::TSplitByLoadSettings GetEffectiveSplitByLoadSettings( @@ -1348,9 +1366,9 @@ struct TTableInfo : public TSimpleRefCount { return true; } // Otherwise we split when we may add one more partition - if (Partitions.size() < GetMaxPartitionsCount() && dataSize >= GetShardSizeToSplit(params)) { + if (Partitioning->Order.size() < GetMaxPartitionsCount() && dataSize >= GetShardSizeToSplit(params)) { reason = TStringBuilder() << "split by size (" - << "shardCount: " << Partitions.size() << ", " + << "shardCount: " << Partitioning->Order.size() << ", " << "maxShardCount: " << GetMaxPartitionsCount() << ", " << "shardSize: " << dataSize << ", " << "maxShardSize: " << GetShardSizeToSplit(params) << ")"; @@ -1406,14 +1424,14 @@ struct TTableInfo : public TSimpleRefCount { return nullptr; } const TShardIdx& shardIdx = CondEraseSchedule.Top().second; - const auto* p = PartitionStore.FindPtr(shardIdx); + const auto* p = Partitioning->Store.FindPtr(shardIdx); Y_ABORT_UNLESS(p); return p; } // Schedule any partition not already in the schedule or in-flight. void ScheduleAllCondErase() { - for (const auto* p : Partitions) { + for (const auto* p : Partitioning->Order) { if (!CondEraseSchedule.Contains(p->ShardIdx) && !InFlightCondErase.contains(p->ShardIdx)) { CondEraseSchedule.Push(p->NextCondErase, p->ShardIdx); } @@ -1452,6 +1470,7 @@ struct TTableInfo : public TSimpleRefCount { } void UpdateNextCondErase(const TShardIdx& shardIdx, const TInstant& now, const TDuration& next) { + EnsureUniquePartitioning(); // in-place mutation: detach from any shared snapshot auto* p = FindPartition(shardIdx); Y_ENSURE(p); @@ -1460,7 +1479,7 @@ struct TTableInfo : public TSimpleRefCount { p->LastCondEraseLag = TDuration::Zero(); } - bool IsUsingSequence(const TString& name) const { + bool IsUsingSequence(const TString& name) { for (const auto& pr : Columns) { if (pr.second.DefaultKind == ETableColumnDefaultKind::FromSequence && pr.second.DefaultValue == name) @@ -2999,6 +3018,18 @@ struct TFileStoreInfo : public TSimpleRefCount { THolder AlterConfig; ui64 AlterVersion = 0; + TFileStoreInfo() = default; + // Deep-copies the owned AlterConfig so the info can be snapshotted for undo. + TFileStoreInfo(const TFileStoreInfo& other) + : IndexShardIdx(other.IndexShardIdx) + , IndexTabletId(other.IndexTabletId) + , Config(other.Config) + , Version(other.Version) + , AlterConfig(other.AlterConfig ? MakeHolder(*other.AlterConfig) : nullptr) + , AlterVersion(other.AlterVersion) + {} + TFileStoreInfo& operator=(const TFileStoreInfo&) = delete; + void PrepareAlter(const NKikimrFileStore::TConfig& alterConfig) { Y_ENSURE(!AlterConfig); Y_ENSURE(!AlterVersion); @@ -3090,6 +3121,18 @@ struct TKesusInfo : public TSimpleRefCount { THolder AlterConfig; ui64 AlterVersion = 0; + TKesusInfo() = default; + // Deep-copies the owned AlterConfig so the info can be snapshotted for undo. + TKesusInfo(const TKesusInfo& other) + : KesusShardIdx(other.KesusShardIdx) + , KesusTabletId(other.KesusTabletId) + , Config(other.Config) + , Version(other.Version) + , AlterConfig(other.AlterConfig ? MakeHolder(*other.AlterConfig) : nullptr) + , AlterVersion(other.AlterVersion) + {} + TKesusInfo& operator=(const TKesusInfo&) = delete; + void FinishAlter() { Y_ENSURE(AlterConfig, "No alter config at Alter completion"); Y_ENSURE(AlterVersion, "No alter version at Alter completion"); diff --git a/ydb/core/tx/schemeshard/ut_base/ut_commit_redo_limit.cpp b/ydb/core/tx/schemeshard/ut_base/ut_commit_redo_limit.cpp index b9048829df09c..9d458a553427a 100644 --- a/ydb/core/tx/schemeshard/ut_base/ut_commit_redo_limit.cpp +++ b/ydb/core/tx/schemeshard/ut_base/ut_commit_redo_limit.cpp @@ -147,9 +147,9 @@ Y_UNIT_TEST_SUITE(TSchemeShardCheckProposeSize) { TestDescribeResult(DescribePath(runtime, "/MyRoot/pqgroup"), {NLs::PathExist}); } - // A redo-overflow abort of a copy must preserve the source table's partitions, - // then an alter walks them. Targeted undo must keep the live source intact; - // replacing it with a shallow snapshot used to dangle its partition pointers. + // A redo-overflow abort of a copy restores the source table's undo snapshot, + // then an alter walks its partitions. Regression: a shallow snapshot dangled + // the source's partition ptrs (crash at ShardInfos.contains). Y_UNIT_TEST(CopyTableAbortRollbackPreservesSourcePartitions) { TTestBasicRuntime runtime; TTestEnv env(runtime); @@ -171,7 +171,7 @@ Y_UNIT_TEST_SUITE(TSchemeShardCheckProposeSize) { )"); env.TestWaitNotification(runtime, txId); - // Abort the copy's propose after it has changed source-table bookkeeping. + // Abort the copy's propose after it has snapshotted the source table. MaxCommitRedoMB = 1; AsyncCopyTable(runtime, ++txId, "/MyRoot", "table-copy", "/MyRoot/table"); TestModificationResults(runtime, txId, diff --git a/ydb/core/tx/schemeshard/ut_base/ut_db_ref_map.cpp b/ydb/core/tx/schemeshard/ut_base/ut_db_ref_map.cpp index 80e5d1034ba9b..bdc25cef414e3 100644 --- a/ydb/core/tx/schemeshard/ut_base/ut_db_ref_map.cpp +++ b/ydb/core/tx/schemeshard/ut_base/ut_db_ref_map.cpp @@ -1,14 +1,11 @@ #include #include -#include #include -#include #include using namespace NKikimr; using namespace NSchemeShard; -using namespace NSchemeShardUT_Private; namespace { @@ -22,27 +19,6 @@ TVector MakeShards(ui32 n, ui64 ownerId = 1) { return v; } -template -void WithSchemeShard(TTest test) { - TSchemeShard* ss = nullptr; - auto factory = [&ss](const TActorId& tablet, TTabletStorageInfo* info) { - ss = new TSchemeShard(tablet, info); - return ss; - }; - TTestBasicRuntime runtime; - TTestEnv env(runtime, TTestEnvOptions(), factory); - runtime.RunCall([&]() { - const TPathId pathId = TPath::Resolve("/MyRoot", ss).Base()->PathId; - // Use a real registered map and path for reference reconciliation. The - // temporary table entry is removed before returning to the event loop. - UNIT_ASSERT(!ss->Tables.contains(pathId)); - test(*ss, pathId); - UNIT_ASSERT(!ss->Tables.contains(pathId)); - ss->DebugCheckDbRefIntegrity(); - return true; - }); -} - } // namespace Y_UNIT_TEST_SUITE(TDbRefMapTest) { @@ -61,144 +37,46 @@ Y_UNIT_TEST_SUITE(TDbRefMapTest) { std::shared_ptr>); } - Y_UNIT_TEST(MembershipOwnsExactlyOnePathReference) { - WithSchemeShard([](TSchemeShard& ss, const TPathId& pathId) { - const auto initialRefs = ss.PathsById.at(pathId)->DbRefCount; - auto first = MakeIntrusive(); - auto second = MakeIntrusive(); - - ss.Tables.SetUntracked(pathId, first); - UNIT_ASSERT_VALUES_EQUAL(ss.PathsById.at(pathId)->DbRefCount, initialRefs + 1); - ss.Tables.SetUntracked(pathId, second); - UNIT_ASSERT_EQUAL(ss.Tables.at(pathId).Get(), second.Get()); - UNIT_ASSERT_VALUES_EQUAL(ss.PathsById.at(pathId)->DbRefCount, initialRefs + 1); - UNIT_ASSERT_VALUES_EQUAL(ss.Tables.erase(pathId), 1); - UNIT_ASSERT_VALUES_EQUAL(ss.PathsById.at(pathId)->DbRefCount, initialRefs); - UNIT_ASSERT_VALUES_EQUAL(ss.Tables.erase(pathId), 0); - UNIT_ASSERT_VALUES_EQUAL(ss.PathsById.at(pathId)->DbRefCount, initialRefs); - }); - } + // The undo clone shares the immutable partitioning copy-on-write (O(1), no + // fixup); a shallow copy would dangle Order's raw ptrs (the ReplicationAttribute crash). + Y_UNIT_TEST(UndoCloneSharesPartitioning) { + TTableInfo::TPtr orig(new TTableInfo()); + orig->SetPartitioning(MakeShards(3)); - Y_UNIT_TEST(InsertAndReplaceUndoAfterPathCounterRestoration) { - WithSchemeShard([](TSchemeShard& ss, const TPathId& pathId) { - const auto initialRefs = ss.PathsById.at(pathId)->DbRefCount; - auto first = MakeIntrusive(); - auto second = MakeIntrusive(); - first->AlterVersion = 10; - second->AlterVersion = 20; - - TMemoryChanges changes; - changes.Arm(&ss); - changes.GrabPath(&ss, pathId); - ss.Tables.Set({.Path = pathId, .Value = first, .Changes = changes}); - changes.RecordUndo([first]() { first->AlterVersion = 10; }); - first->AlterVersion = 11; - ss.Tables.Set({.Path = pathId, .Value = second, .Changes = changes}); - changes.RecordUndo([second]() { second->AlterVersion = 20; }); - second->AlterVersion = 21; - UNIT_ASSERT_VALUES_EQUAL(ss.PathsById.at(pathId)->DbRefCount, initialRefs + 1); - - changes.UnDo(&ss); - changes.Disarm(); - // Paths restore the count first. Undoing the insertion must not - // decrement it again; mutation callbacks restore their own objects. - UNIT_ASSERT(!ss.Tables.contains(pathId)); - UNIT_ASSERT_VALUES_EQUAL(ss.PathsById.at(pathId)->DbRefCount, initialRefs); - UNIT_ASSERT_VALUES_EQUAL(first->AlterVersion, 10); - UNIT_ASSERT_VALUES_EQUAL(second->AlterVersion, 20); - }); - } + TTableInfo::TPtr clone = DbRefUndoClone(orig); + + UNIT_ASSERT(clone); + UNIT_ASSERT_UNEQUAL(clone.Get(), orig.Get()); + UNIT_ASSERT_VALUES_EQUAL(clone->GetPartitions().size(), 3u); - Y_UNIT_TEST(ReplacementAndFieldUndoShareReverseOrder) { - WithSchemeShard([](TSchemeShard& ss, const TPathId& pathId) { - auto first = MakeIntrusive(); - auto second = MakeIntrusive(); - first->AlterVersion = 10; - second->AlterVersion = 20; - ss.Tables.SetUntracked(pathId, first); - const auto initialRefs = ss.PathsById.at(pathId)->DbRefCount; - - TMemoryChanges changes; - changes.Arm(&ss); - changes.GrabPath(&ss, pathId); - changes.RecordUndo([first]() { first->AlterVersion = 10; }); - ss.Tables.Update(pathId)->AlterVersion = 11; - ss.Tables.Set({.Path = pathId, .Value = second, .Changes = changes}); - changes.RecordUndo([second]() { second->AlterVersion = 20; }); - ss.Tables.Update(pathId)->AlterVersion = 21; - - changes.UnDo(&ss); - changes.Disarm(); - UNIT_ASSERT_EQUAL(ss.Tables.at(pathId).Get(), first.Get()); - UNIT_ASSERT_VALUES_EQUAL(first->AlterVersion, 10); - UNIT_ASSERT_VALUES_EQUAL(second->AlterVersion, 20); - UNIT_ASSERT_VALUES_EQUAL(ss.PathsById.at(pathId)->DbRefCount, initialRefs); - ss.Tables.erase(pathId); - }); + // Shared: same store and the very same partition objects (no deep copy). + UNIT_ASSERT_EQUAL(&clone->GetPartitionStore(), &orig->GetPartitionStore()); + UNIT_ASSERT_EQUAL(clone->GetPartitions()[0], orig->GetPartitions()[0]); + + clone->VerifyConsistency(); } - Y_UNIT_TEST(AlterDataUndoPreservesTableIdentity) { - WithSchemeShard([](TSchemeShard& ss, const TPathId& pathId) { - auto table = MakeIntrusive(); - table->SetPartitioning(MakeShards(2)); - auto previous = MakeIntrusive(); - table->AlterData = previous; - ss.Tables.SetUntracked(pathId, table); - const auto* alias = table.Get(); - const auto* partition = table->GetPartitions().front(); - const auto* stats = &table->GetStats().PartitionStats.at(partition->ShardIdx); - - TMemoryChanges changes; - changes.Arm(&ss); - auto writable = ss.Tables.Update(pathId); - changes.RecordUndo([writable, previous = writable->AlterData]() { - writable->AlterData = previous; - }); - auto candidate = MakeIntrusive(); - candidate->AlterVersion = table->AlterVersion + 1; - writable->PrepareAlter(candidate); - UNIT_ASSERT_EQUAL(table->AlterData.Get(), candidate.Get()); - - changes.UnDo(&ss); - changes.Disarm(); - UNIT_ASSERT_EQUAL(ss.Tables.at(pathId).Get(), alias); - UNIT_ASSERT_EQUAL(alias->AlterData.Get(), previous.Get()); - UNIT_ASSERT_EQUAL(alias->GetPartitions().front(), partition); - UNIT_ASSERT_EQUAL(&alias->GetStats().PartitionStats.at(partition->ShardIdx), stats); - ss.Tables.erase(pathId); - }); + // An in-place mutation on one side detaches it (copy-on-write): the two tables + // then own separate stores and neither sees the other's change. + Y_UNIT_TEST(UndoCloneCopiesOnWrite) { + TTableInfo::TPtr orig(new TTableInfo()); + orig->SetPartitioning(MakeShards(2)); + + TTableInfo::TPtr clone = DbRefUndoClone(orig); + UNIT_ASSERT_EQUAL(&clone->GetPartitionStore(), &orig->GetPartitionStore()); // shared + + // Mutate orig's cond-erase in place — must copy-on-write away from the clone. + const TShardIdx shardIdx = orig->GetPartitions()[0]->ShardIdx; + orig->UpdateNextCondErase(shardIdx, TInstant::Seconds(100), TDuration::Seconds(10)); + + UNIT_ASSERT_UNEQUAL(&clone->GetPartitionStore(), &orig->GetPartitionStore()); + UNIT_ASSERT_VALUES_EQUAL(clone->GetPartitions().size(), 2u); + clone->VerifyConsistency(); + orig->VerifyConsistency(); } - Y_UNIT_TEST(UpdateDoesNotSnapshotTwoHundredThousandShards) { - WithSchemeShard([](TSchemeShard& ss, const TPathId& pathId) { - constexpr ui32 shardCount = 200000; - auto table = MakeIntrusive(); - table->SetPartitioning(MakeShards(shardCount)); - table->AlterData = MakeIntrusive(); - ss.Tables.SetUntracked(pathId, table); - UNIT_ASSERT_VALUES_EQUAL(table->GetStats().PartitionStats.size(), shardCount); - const auto* partition = table->GetPartitions().front(); - const auto* stats = &table->GetStats().PartitionStats.at(partition->ShardIdx); - const auto alterOwners = table->AlterData.RefCount(); - - TMemoryChanges changes; - changes.Arm(&ss); - for (ui32 i = 0; i < 256; ++i) { - const auto& writable = ss.Tables.Update(pathId); - UNIT_ASSERT_EQUAL(writable.Get(), table.Get()); - // A retained whole-table snapshot would copy the AlterData - // smart pointer too, increasing its owner count even though - // the live table's partition/statistics addresses stay unchanged. - UNIT_ASSERT_VALUES_EQUAL(writable->AlterData.RefCount(), alterOwners); - } - changes.UnDo(&ss); - changes.Disarm(); - UNIT_ASSERT_EQUAL(ss.Tables.at(pathId).Get(), table.Get()); - UNIT_ASSERT_EQUAL(table->GetPartitions().front(), partition); - UNIT_ASSERT_EQUAL(&table->GetStats().PartitionStats.at(partition->ShardIdx), stats); - UNIT_ASSERT_VALUES_EQUAL(table->AlterData.RefCount(), alterOwners); - table->VerifyConsistency(); - ss.Tables.erase(pathId); - }); + Y_UNIT_TEST(UndoCloneNullIsNull) { + TTableInfo::TPtr nul; + UNIT_ASSERT(!DbRefUndoClone(nul)); } } diff --git a/ydb/core/tx/schemeshard/ut_ttl/ut_ttl.cpp b/ydb/core/tx/schemeshard/ut_ttl/ut_ttl.cpp index d855a5ede7d12..e9d9e3991f635 100644 --- a/ydb/core/tx/schemeshard/ut_ttl/ut_ttl.cpp +++ b/ydb/core/tx/schemeshard/ut_ttl/ut_ttl.cpp @@ -897,10 +897,11 @@ Y_UNIT_TEST_SUITE(TSchemeShardTTLTests) { } Y_UNIT_TEST(RejectedAlterKeepsTTLTableConsistent) { - // Regression (issue #33764): a rejected AlterTable must preserve the - // live TTableInfo object shared with TTLEnabledTables. Update() does not - // snapshot or replace it; staged mutations have explicit undo. Replacing - // it on abort would desync the alias checked by TTxRunConditionalErase. + // Regression (issue #33764): a rejected AlterTable grabs the table via + // Tables.Update() during propose, so the abort (memory-changes UnDo) must + // restore the pre-mutation contents into the SAME TTableInfo object. + // Swapping in a fresh clone desyncs the TTLEnabledTables alias, and the + // next TTxRunConditionalErase asserts *checkedTable == tableInfo. TTestBasicRuntime runtime; TTestEnv env(runtime); ui64 txId = 100; @@ -915,7 +916,7 @@ Y_UNIT_TEST_SUITE(TSchemeShardTTLTests) { env.TestWaitNotification(runtime, txId); // Reaches Tables.Update() in propose, then ParseParams rejects the - // key-column drop -> the abort must leave the live table unchanged. + // key-column drop -> the operation aborts and rolls back the table. TestAlterTable(runtime, ++txId, "/MyRoot", R"( Name: "TTLEnabledTable" DropColumns { Name: "key" }