|
| 1 | +#include "ydb/core/persqueue/partition_scale_manager.h" |
| 2 | + |
| 3 | +namespace NKikimr { |
| 4 | +namespace NPQ { |
| 5 | + |
| 6 | + |
| 7 | +TPartitionScaleManager::TPartitionScaleManager( |
| 8 | + const TString& topicName, |
| 9 | + const TString& databasePath, |
| 10 | + NKikimrPQ::TUpdateBalancerConfig& balancerConfig |
| 11 | +) |
| 12 | + : TopicName(topicName) |
| 13 | + , DatabasePath(databasePath) |
| 14 | + , BalancerConfig(balancerConfig) { |
| 15 | + |
| 16 | + } |
| 17 | + |
| 18 | +void TPartitionScaleManager::HandleScaleStatusChange(const TPartitionInfo& partition, NKikimrPQ::EScaleStatus scaleStatus, const TActorContext& ctx) { |
| 19 | + if (scaleStatus == NKikimrPQ::EScaleStatus::NEED_SPLIT) { |
| 20 | + PartitionsToSplit.emplace(partition.Id, partition); |
| 21 | + TrySendScaleRequest(ctx); |
| 22 | + } else { |
| 23 | + PartitionsToSplit.erase(partition.Id); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +void TPartitionScaleManager::TrySendScaleRequest(const TActorContext& ctx) { |
| 28 | + TInstant delayDeadline = LastResponseTime + RequestTimeout; |
| 29 | + if (!DatabasePath || RequestInflight || delayDeadline > ctx.Now()) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + auto splitMergePair = BuildScaleRequest(); |
| 34 | + if (splitMergePair.first.empty() && splitMergePair.second.empty()) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + RequestInflight = true; |
| 39 | + CurrentScaleRequest = ctx.Register(new TPartitionScaleRequest( |
| 40 | + TopicName, |
| 41 | + DatabasePath, |
| 42 | + BalancerConfig.PathId, |
| 43 | + BalancerConfig.PathVersion, |
| 44 | + splitMergePair.first, |
| 45 | + splitMergePair.second, |
| 46 | + ctx.SelfID |
| 47 | + )); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +using TPartitionSplit = NKikimrSchemeOp::TPersQueueGroupDescription_TPartitionSplit; |
| 52 | +using TPartitionMerge = NKikimrSchemeOp::TPersQueueGroupDescription_TPartitionMerge; |
| 53 | + |
| 54 | +std::pair<std::vector<TPartitionSplit>, std::vector<TPartitionMerge>> TPartitionScaleManager::BuildScaleRequest() { |
| 55 | + std::vector<TPartitionSplit> splitsToApply; |
| 56 | + std::vector<TPartitionMerge> mergesToApply; |
| 57 | + |
| 58 | + size_t allowedSplitsCount = BalancerConfig.PartitionCountLimit > BalancerConfig.CurPartitions ? BalancerConfig.PartitionCountLimit - BalancerConfig.CurPartitions : 0; |
| 59 | + auto itSplit = PartitionsToSplit.begin(); |
| 60 | + while (allowedSplitsCount > 0 && itSplit != PartitionsToSplit.end()) { |
| 61 | + const auto partitionId = itSplit->first; |
| 62 | + const auto& partition = itSplit->second; |
| 63 | + |
| 64 | + if (BalancerConfig.PartitionGraph.GetPartition(partitionId)->Children.empty()) { |
| 65 | + auto mid = GetRangeMid(partition.KeyRange.FromBound ? *partition.KeyRange.FromBound : "", partition.KeyRange.ToBound ?*partition.KeyRange.ToBound : ""); |
| 66 | + if (mid.empty()) { |
| 67 | + itSplit = PartitionsToSplit.erase(itSplit); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + TPartitionSplit split; |
| 72 | + split.set_partition(partition.Id); |
| 73 | + split.set_splitboundary(mid); |
| 74 | + splitsToApply.push_back(split); |
| 75 | + |
| 76 | + allowedSplitsCount--; |
| 77 | + itSplit++; |
| 78 | + } else { |
| 79 | + itSplit = PartitionsToSplit.erase(itSplit); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return {splitsToApply, mergesToApply}; |
| 84 | +} |
| 85 | + |
| 86 | +void TPartitionScaleManager::HandleScaleRequestResult(TPartitionScaleRequest::TEvPartitionScaleRequestDone::TPtr& ev, const TActorContext& ctx) { |
| 87 | + RequestInflight = false; |
| 88 | + LastResponseTime = ctx.Now(); |
| 89 | + auto result = ev->Get(); |
| 90 | + if (result->Status == TEvTxUserProxy::TResultStatus::ExecComplete) { |
| 91 | + TrySendScaleRequest(ctx); |
| 92 | + } else { |
| 93 | + ui64 newTimeout = RequestTimeout.MilliSeconds() == 0 ? MIN_SCALE_REQUEST_REPEAT_SECONDS_TIMEOUT + RandomNumber<ui64>(50) : RequestTimeout.MilliSeconds() * 2; |
| 94 | + RequestTimeout = TDuration::MilliSeconds(std::min(newTimeout, static_cast<ui64>(MAX_SCALE_REQUEST_REPEAT_SECONDS_TIMEOUT))); |
| 95 | + ctx.Schedule(RequestTimeout, new TEvents::TEvWakeup(TRY_SCALE_REQUEST_WAKE_UP_TAG)); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void TPartitionScaleManager::Die(const TActorContext& ctx) { |
| 100 | + if (CurrentScaleRequest) { |
| 101 | + ctx.Send(CurrentScaleRequest, new TEvents::TEvPoisonPill()); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void TPartitionScaleManager::UpdateBalancerConfig(NKikimrPQ::TUpdateBalancerConfig& config) { |
| 106 | + BalancerConfig = TBalancerConfig(config); |
| 107 | +} |
| 108 | + |
| 109 | +void TPartitionScaleManager::UpdateDatabasePath(const TString& dbPath) { |
| 110 | + DatabasePath = dbPath; |
| 111 | +} |
| 112 | + |
| 113 | +TString TPartitionScaleManager::GetRangeMid(const TString& from, const TString& to) { |
| 114 | + if (from > to) { |
| 115 | + return ""; |
| 116 | + } |
| 117 | + |
| 118 | + TStringBuilder result; |
| 119 | + |
| 120 | + unsigned char fromPadding = 0; |
| 121 | + unsigned char toPadding = 255; |
| 122 | + |
| 123 | + size_t maxSize = std::max(from.size(), to.size()); |
| 124 | + for (size_t i = 0; i < maxSize; ++i) { |
| 125 | + ui16 fromChar = i < from.size() ? static_cast<ui16>(from[i]) : fromPadding; |
| 126 | + unsigned char toChar = i < to.size() ? static_cast<unsigned char>(to[i]) : toPadding; |
| 127 | + |
| 128 | + ui16 sum = fromChar + toChar; |
| 129 | + |
| 130 | + result += static_cast<unsigned char>(sum / 2); |
| 131 | + } |
| 132 | + |
| 133 | + if (result == from) { |
| 134 | + result += static_cast<unsigned char>(127); |
| 135 | + } |
| 136 | + |
| 137 | + return result; |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace NPQ |
| 141 | +} // namespace NKikimr |
0 commit comments