|
1 | 1 | #include "balancing_actor.h" |
2 | 2 | #include "defs.h" |
| 3 | +#include "utils.h" |
| 4 | + |
| 5 | +#include <ydb/core/blobstorage/vdisk/common/vdisk_queues.h> |
| 6 | +#include <ydb/core/blobstorage/vdisk/repl/blobstorage_replbroker.h> |
3 | 7 |
|
4 | 8 |
|
5 | 9 | namespace NKikimr { |
6 | 10 |
|
7 | 11 | class TBalancingActor : public TActorBootstrapped<TBalancingActor> { |
8 | 12 | private: |
9 | 13 | std::shared_ptr<TBalancingCtx> Ctx; |
10 | | - public: |
11 | | - void Bootstrap() { |
12 | | - Become(&TThis::StateFunc); |
| 14 | + TLogoBlobsSnapshot::TForwardIterator It; |
| 15 | + TQueueActorMapPtr QueueActorMapPtr; |
| 16 | + TActiveActors ActiveActors; |
| 17 | + THashSet<TVDiskID> ConnectedVDisks; |
| 18 | + |
| 19 | + TActorId SenderId; |
| 20 | + TActorId DeleterId; |
| 21 | + |
| 22 | + struct TStats { |
| 23 | + bool SendCompleted = false; |
| 24 | + bool DeleteCompleted = false; |
| 25 | + }; |
| 26 | + |
| 27 | + TStats Stats; |
| 28 | + |
| 29 | + void CreateVDisksQueues() { |
| 30 | + QueueActorMapPtr = std::make_shared<TQueueActorMap>(); |
| 31 | + auto interconnectChannel = TInterconnectChannels::EInterconnectChannels::IC_BLOBSTORAGE_ASYNC_DATA; |
| 32 | + const TBlobStorageGroupInfo::TTopology& topology = Ctx->GInfo->GetTopology(); |
| 33 | + NBackpressure::TQueueClientId queueClientId( |
| 34 | + NBackpressure::EQueueClientType::Balancing, topology.GetOrderNumber(Ctx->VCtx->ShortSelfVDisk)); |
| 35 | + |
| 36 | + CreateQueuesForVDisks(*QueueActorMapPtr, SelfId(), Ctx->GInfo, Ctx->VCtx, |
| 37 | + Ctx->GInfo->GetVDisks(), Ctx->MonGroup.GetGroup(), |
| 38 | + queueClientId, NKikimrBlobStorage::EVDiskQueueId::GetAsyncRead, |
| 39 | + "DisksBalancing", interconnectChannel); |
| 40 | + } |
| 41 | + |
| 42 | + std::pair<TQueue<TPartInfo>, TQueue<TPartInfo>> CollectKeys(const TActorContext &ctx) { |
| 43 | + TQueue<TPartInfo> sendOnMainParts, tryDeleteParts; |
| 44 | + |
| 45 | + for (It.SeekToFirst(); It.Valid(); It.Next()) { |
| 46 | + TPartsCollectorMerger merger(Ctx->GInfo->GetTopology().GType); |
| 47 | + It.PutToMerger(&merger); |
| 48 | + |
| 49 | + for (ui8 partIdx: PartsToSendOnMain(Ctx->GInfo->GetTopology(), Ctx->VCtx->ShortSelfVDisk, It.GetCurKey().LogoBlobID(), merger.Ingress)) { |
| 50 | + if (!merger.Parts[partIdx - 1].has_value()) { |
| 51 | + LOG_WARN_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 52 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " |
| 53 | + << "not found part " << (ui32)partIdx << " for " << It.GetCurKey().LogoBlobID().ToString()); |
| 54 | + continue; // something strange |
| 55 | + } |
| 56 | + sendOnMainParts.push(TPartInfo{ |
| 57 | + .Key=TLogoBlobID(It.GetCurKey().LogoBlobID(), partIdx), |
| 58 | + .Ingress=merger.Ingress, |
| 59 | + .PartData=*merger.Parts[partIdx - 1] |
| 60 | + }); |
| 61 | + LOG_DEBUG_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 62 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " |
| 63 | + << "Send on main: " << sendOnMainParts.back().Key.ToString() << " " << sendOnMainParts.back().Ingress.ToString(&Ctx->GInfo->GetTopology(), Ctx->VCtx->ShortSelfVDisk, sendOnMainParts.back().Key)); |
| 64 | + } |
| 65 | + |
| 66 | + for (ui8 partIdx: PartsToDelete(Ctx->GInfo->GetTopology(), Ctx->VCtx->ShortSelfVDisk, It.GetCurKey().LogoBlobID(), merger.Ingress)) { |
| 67 | + tryDeleteParts.push(TPartInfo{ |
| 68 | + .Key=TLogoBlobID(It.GetCurKey().LogoBlobID(), partIdx), |
| 69 | + .Ingress=merger.Ingress, |
| 70 | + }); |
| 71 | + LOG_DEBUG_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 72 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " |
| 73 | + << "Delete: " << tryDeleteParts.back().Key.ToString() << " " << tryDeleteParts.back().Ingress.ToString(&Ctx->GInfo->GetTopology(), Ctx->VCtx->ShortSelfVDisk, tryDeleteParts.back().Key)); |
| 74 | + } |
| 75 | + merger.Clear(); |
| 76 | + } |
| 77 | + return {sendOnMainParts, tryDeleteParts}; |
| 78 | + } |
| 79 | + |
| 80 | + void StartBalancing(const TActorContext &ctx) { |
| 81 | + LOG_DEBUG_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 82 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " << "Ask repl token"); |
| 83 | + if (!Send(MakeBlobStorageReplBrokerID(), new TEvQueryReplToken(Ctx->VDiskCfg->BaseInfo.PDiskId))) { |
| 84 | + HandleReplToken(ctx); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void HandleReplToken(const TActorContext &ctx) { |
| 89 | + LOG_DEBUG_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 90 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " << "Repl token acquired"); |
| 91 | + DoJobQuant(ctx); |
| 92 | + Send(MakeBlobStorageReplBrokerID(), new TEvReleaseReplToken); |
| 93 | + Schedule(TDuration::Seconds(1), new NActors::TEvents::TEvWakeup()); |
| 94 | + } |
| 95 | + |
| 96 | + void Handle(NActors::TEvents::TEvCompleted::TPtr ev, const TActorContext &ctx) { |
| 97 | + switch (ev->Get()->Id) { |
| 98 | + case SENDER_ID: { |
| 99 | + Stats.SendCompleted = true; |
| 100 | + break; |
| 101 | + } |
| 102 | + case DELETER_ID: { |
| 103 | + Stats.DeleteCompleted = true; |
| 104 | + break; |
| 105 | + } |
| 106 | + default: |
| 107 | + Y_ABORT("Unexpected id"); |
| 108 | + } |
| 109 | + if (Stats.SendCompleted && Stats.DeleteCompleted) { |
| 110 | + LOG_DEBUG_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 111 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " << "Balancing completed"); |
| 112 | + Send(Ctx->SkeletonId, new TEvStartBalancing()); |
| 113 | + Send(SelfId(), new NActors::TEvents::TEvPoison); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + void Handle(TEvProxyQueueState::TPtr ev) { |
| 118 | + const TVDiskID& vdiskId = ev->Get()->VDiskId; |
| 119 | + if (ev->Get()->IsConnected) { |
| 120 | + ConnectedVDisks.insert(vdiskId); |
| 121 | + } else { |
| 122 | + ConnectedVDisks.erase(vdiskId); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + void Handle(TEvVGenerationChange::TPtr ev) { |
| 127 | + Cerr << "TEvVGenerationChange" << Endl; |
| 128 | + // forward message to queue actors |
| 129 | + TEvVGenerationChange *msg = ev->Get(); |
| 130 | + for (const auto& kv : *QueueActorMapPtr) { |
| 131 | + Send(kv.second, msg->Clone()); |
| 132 | + } |
| 133 | + Ctx->GInfo = msg->NewInfo; |
| 134 | + } |
| 135 | + |
| 136 | + void DoJobQuant(const TActorContext& ctx) { |
| 137 | + LOG_DEBUG_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 138 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " << "Connected vdisks " << ConnectedVDisks.size() << "/" << Ctx->GInfo->GetTotalVDisksNum() - 1); |
| 139 | + Send(SenderId, new NActors::TEvents::TEvWakeup()); |
| 140 | + Send(DeleterId, new NActors::TEvents::TEvWakeup()); |
| 141 | + } |
| 142 | + |
| 143 | + void PassAway() override { |
| 144 | + for (const auto& kv : *QueueActorMapPtr) { |
| 145 | + Send(kv.second, new TEvents::TEvPoison); |
| 146 | + } |
| 147 | + TActorBootstrapped::PassAway(); |
13 | 148 | } |
14 | 149 |
|
15 | 150 | STRICT_STFUNC(StateFunc, |
16 | | - CFunc(NActors::TEvents::TEvPoison::EventType, Die) |
| 151 | + CFunc(TEvReplToken::EventType, HandleReplToken) |
| 152 | + HFunc(NActors::TEvents::TEvCompleted, Handle) |
| 153 | + hFunc(TEvProxyQueueState, Handle) |
| 154 | + hFunc(TEvVGenerationChange, Handle) |
| 155 | + CFunc(NActors::TEvents::TEvWakeup::EventType, StartBalancing) |
| 156 | + cFunc(NActors::TEvents::TEvPoison::EventType, PassAway) |
17 | 157 | ); |
18 | 158 |
|
| 159 | + public: |
19 | 160 | TBalancingActor(std::shared_ptr<TBalancingCtx> &ctx) |
20 | 161 | : TActorBootstrapped<TBalancingActor>() |
21 | 162 | , Ctx(ctx) |
| 163 | + , It(Ctx->Snap.HullCtx, &Ctx->Snap.LogoBlobsSnap) |
22 | 164 | { |
23 | 165 | } |
| 166 | + |
| 167 | + void Bootstrap(const TActorContext &ctx) { |
| 168 | + CreateVDisksQueues(); |
| 169 | + auto [sendOnMainParts, tryDeleteParts] = CollectKeys(ctx); |
| 170 | + LOG_DEBUG_S(ctx, NKikimrServices::BS_VDISK_BALANCING, |
| 171 | + Ctx->GInfo->GetTopology().GetOrderNumber(Ctx->VCtx->ShortSelfVDisk) << "$ " |
| 172 | + << ctx.Now().MilliSeconds() << " Bootstrap" << ": " |
| 173 | + << "sendOnMainParts size = " << sendOnMainParts.size() << "; tryDeleteParts size = " << tryDeleteParts.size()); |
| 174 | + |
| 175 | + SenderId = ctx.Register(CreateSenderActor(SelfId(), std::move(sendOnMainParts), QueueActorMapPtr, Ctx)); |
| 176 | + ActiveActors.Insert(SenderId, __FILE__, __LINE__, ctx, NKikimrServices::BLOBSTORAGE); |
| 177 | + |
| 178 | + DeleterId = ctx.Register(CreateDeleterActor(SelfId(), std::move(tryDeleteParts), QueueActorMapPtr, Ctx)); |
| 179 | + ActiveActors.Insert(DeleterId, __FILE__, __LINE__, ctx, NKikimrServices::BLOBSTORAGE); |
| 180 | + |
| 181 | + Become(&TThis::StateFunc); |
| 182 | + Schedule(TDuration::Seconds(1), new NActors::TEvents::TEvWakeup()); |
| 183 | + } |
24 | 184 | }; |
25 | 185 |
|
26 | 186 | IActor* CreateBalancingActor(std::shared_ptr<TBalancingCtx> ctx) { |
27 | 187 | return new TBalancingActor(ctx); |
28 | 188 | } |
29 | | -} |
| 189 | +} // NKikimr |
0 commit comments