|
| 1 | +#include "node_broker_impl.h" |
| 2 | + |
| 3 | +#include <ydb/core/protos/counters_node_broker.pb.h> |
| 4 | + |
| 5 | +namespace NKikimr::NNodeBroker { |
| 6 | + |
| 7 | +constexpr size_t MAX_NODES_BATCH_SIZE = 1000; |
| 8 | + |
| 9 | +class TNodeBroker::TTxMigrateState : public TTransactionBase<TNodeBroker> { |
| 10 | +public: |
| 11 | + TTxMigrateState(TNodeBroker *self, TDbChanges&& dbChanges) |
| 12 | + : TBase(self) |
| 13 | + , DbChanges(std::move(dbChanges)) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + TTxType GetTxType() const override { return TXTYPE_MIGRATE_STATE; } |
| 18 | + |
| 19 | + void FinalizeMigration(TTransactionContext &txc, const TActorContext &ctx) |
| 20 | + { |
| 21 | + LOG_DEBUG(ctx, NKikimrServices::NODE_BROKER, "TTxMigrateState FinalizeMigration"); |
| 22 | + |
| 23 | + if (DbChanges.UpdateEpoch) { |
| 24 | + Self->Dirty.DbUpdateEpoch(Self->Dirty.Epoch, txc); |
| 25 | + } |
| 26 | + |
| 27 | + if (DbChanges.UpdateApproxEpochStart) { |
| 28 | + Self->Dirty.DbUpdateApproxEpochStart(Self->Dirty.ApproxEpochStart, txc); |
| 29 | + } |
| 30 | + |
| 31 | + if (DbChanges.UpdateMainNodesTable) { |
| 32 | + Self->Dirty.DbUpdateMainNodesTable(txc); |
| 33 | + } |
| 34 | + |
| 35 | + // Move epoch if required. |
| 36 | + auto now = ctx.Now(); |
| 37 | + while (now > Self->Dirty.Epoch.End) { |
| 38 | + TStateDiff diff; |
| 39 | + Self->Dirty.ComputeNextEpochDiff(diff); |
| 40 | + Self->Dirty.ApplyStateDiff(diff); |
| 41 | + Self->Dirty.DbApplyStateDiff(diff, txc); |
| 42 | + } |
| 43 | + |
| 44 | + Finalized = true; |
| 45 | + } |
| 46 | + |
| 47 | + void ProcessMigrationBatch(TTransactionContext &txc, const TActorContext &ctx) |
| 48 | + { |
| 49 | + LOG_DEBUG(ctx, NKikimrServices::NODE_BROKER, TStringBuilder() |
| 50 | + << "TTxMigrateState ProcessMigrationBatch" |
| 51 | + << " UpdateNodes left " << DbChanges.UpdateNodes.size() |
| 52 | + << ", NewVersionUpdateNodes left " << DbChanges.NewVersionUpdateNodes.size()); |
| 53 | + |
| 54 | + size_t nodesBatchSize = 0; |
| 55 | + while (nodesBatchSize < MAX_NODES_BATCH_SIZE && !DbChanges.UpdateNodes.empty()) { |
| 56 | + Self->Dirty.DbUpdateNode(DbChanges.UpdateNodes.back(), txc); |
| 57 | + DbChanges.UpdateNodes.pop_back(); |
| 58 | + ++nodesBatchSize; |
| 59 | + } |
| 60 | + |
| 61 | + const bool newVersionInBatch = nodesBatchSize < MAX_NODES_BATCH_SIZE |
| 62 | + && !DbChanges.NewVersionUpdateNodes.empty() |
| 63 | + && DbChanges.UpdateEpoch; |
| 64 | + |
| 65 | + if (newVersionInBatch) { |
| 66 | + Self->Dirty.DbUpdateEpoch(Self->Dirty.Epoch, txc); |
| 67 | + DbChanges.UpdateEpoch = false; |
| 68 | + // Changing version may affect uncommitted approximate epoch start |
| 69 | + if (DbChanges.UpdateApproxEpochStart) { |
| 70 | + Self->Dirty.DbUpdateApproxEpochStart(Self->Dirty.ApproxEpochStart, txc); |
| 71 | + DbChanges.UpdateApproxEpochStart = false; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + while (nodesBatchSize < MAX_NODES_BATCH_SIZE && !DbChanges.NewVersionUpdateNodes.empty()) { |
| 76 | + Self->Dirty.DbUpdateNode(DbChanges.NewVersionUpdateNodes.back(), txc); |
| 77 | + DbChanges.NewVersionUpdateNodes.pop_back(); |
| 78 | + ++nodesBatchSize; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + bool Execute(TTransactionContext &txc, const TActorContext &ctx) override |
| 83 | + { |
| 84 | + LOG_DEBUG(ctx, NKikimrServices::NODE_BROKER, "TTxMigrateState Execute"); |
| 85 | + |
| 86 | + ProcessMigrationBatch(txc, ctx); |
| 87 | + if (!DbChanges.HasNodeUpdates()) { |
| 88 | + FinalizeMigration(txc, ctx); |
| 89 | + } |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + void Complete(const TActorContext &ctx) override |
| 94 | + { |
| 95 | + LOG_DEBUG(ctx, NKikimrServices::NODE_BROKER, "TTxMigrateState Complete"); |
| 96 | + |
| 97 | + if (Finalized) { |
| 98 | + Self->Committed = Self->Dirty; |
| 99 | + Self->Become(&TNodeBroker::StateWork); |
| 100 | + Self->SubscribeForConfigUpdates(ctx); |
| 101 | + Self->ScheduleEpochUpdate(ctx); |
| 102 | + Self->PrepareEpochCache(); |
| 103 | + Self->SignalTabletActive(ctx); |
| 104 | + } else { |
| 105 | + Self->Execute(Self->CreateTxMigrateState(std::move(DbChanges))); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +private: |
| 110 | + TDbChanges DbChanges; |
| 111 | + bool Finalized = false; |
| 112 | +}; |
| 113 | + |
| 114 | +ITransaction *TNodeBroker::CreateTxMigrateState(TDbChanges&& dbChanges) |
| 115 | +{ |
| 116 | + return new TTxMigrateState(this, std::move(dbChanges)); |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace NKikimr::NNodeBroker |
0 commit comments