|
| 1 | +#include "cpu_quota_manager.h" |
| 2 | + |
| 3 | +#include <util/string/builder.h> |
| 4 | + |
| 5 | + |
| 6 | +namespace NFq { |
| 7 | + |
| 8 | +//// TCpuQuotaManager::TCounters |
| 9 | + |
| 10 | +TCpuQuotaManager::TCounters::TCounters(const ::NMonitoring::TDynamicCounterPtr& subComponent) |
| 11 | + : SubComponent(subComponent) |
| 12 | +{ |
| 13 | + Register(); |
| 14 | +} |
| 15 | + |
| 16 | +void TCpuQuotaManager::TCounters::Register() { |
| 17 | + RegisterCommonMetrics(CpuLoadRequest); |
| 18 | + InstantLoadPercentage = SubComponent->GetCounter("InstantLoadPercentage", false); |
| 19 | + AverageLoadPercentage = SubComponent->GetCounter("AverageLoadPercentage", false); |
| 20 | + QuotedLoadPercentage = SubComponent->GetCounter("QuotedLoadPercentage", false); |
| 21 | +} |
| 22 | + |
| 23 | +void TCpuQuotaManager::TCounters::RegisterCommonMetrics(TCommonMetrics& metrics) const { |
| 24 | + metrics.Ok = SubComponent->GetCounter("Ok", true); |
| 25 | + metrics.Error = SubComponent->GetCounter("Error", true); |
| 26 | +} |
| 27 | + |
| 28 | +//// TCpuQuotaManager::TCpuQuotaResponse |
| 29 | + |
| 30 | +TCpuQuotaManager::TCpuQuotaResponse::TCpuQuotaResponse(int32_t currentLoad, NYdb::EStatus status, NYql::TIssues issues) |
| 31 | + : CurrentLoad(currentLoad) |
| 32 | + , Status(status) |
| 33 | + , Issues(std::move(issues)) |
| 34 | +{} |
| 35 | + |
| 36 | +//// TCpuQuotaManager |
| 37 | + |
| 38 | +TCpuQuotaManager::TCpuQuotaManager(TDuration monitoringRequestDelay, TDuration averageLoadInterval, TDuration idleTimeout, double defaultQueryLoad, bool strict, ui32 cpuNumber, const ::NMonitoring::TDynamicCounterPtr& subComponent) |
| 39 | + : Counters(subComponent) |
| 40 | + , MonitoringRequestDelay(monitoringRequestDelay) |
| 41 | + , AverageLoadInterval(averageLoadInterval) |
| 42 | + , IdleTimeout(idleTimeout) |
| 43 | + , DefaultQueryLoad(defaultQueryLoad) |
| 44 | + , Strict(strict) |
| 45 | + , CpuNumber(cpuNumber) |
| 46 | +{} |
| 47 | + |
| 48 | +double TCpuQuotaManager::GetInstantLoad() const { |
| 49 | + return InstantLoad; |
| 50 | +} |
| 51 | + |
| 52 | +double TCpuQuotaManager::GetAverageLoad() const { |
| 53 | + return AverageLoad; |
| 54 | +} |
| 55 | + |
| 56 | +TDuration TCpuQuotaManager::GetMonitoringRequestDelay() const { |
| 57 | + TDuration delay = MonitoringRequestDelay; |
| 58 | + if (IdleTimeout && TInstant::Now() - LastRequestCpuQuota > IdleTimeout) { |
| 59 | + delay = AverageLoadInterval / 2; |
| 60 | + } |
| 61 | + |
| 62 | + return LastUpdateCpuLoad ? (LastUpdateCpuLoad + delay) - TInstant::Now() : TDuration::Zero(); |
| 63 | +} |
| 64 | + |
| 65 | +void TCpuQuotaManager::UpdateCpuLoad(double instantLoad, ui32 cpuNumber, bool success) { |
| 66 | + auto now = TInstant::Now(); |
| 67 | + LastUpdateCpuLoad = now; |
| 68 | + |
| 69 | + if (!success) { |
| 70 | + Counters.CpuLoadRequest.Error->Inc(); |
| 71 | + CheckLoadIsOutdated(); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + auto delta = now - LastCpuLoad; |
| 76 | + LastCpuLoad = now; |
| 77 | + |
| 78 | + if (cpuNumber) { |
| 79 | + CpuNumber = cpuNumber; |
| 80 | + } |
| 81 | + |
| 82 | + InstantLoad = instantLoad; |
| 83 | + // exponential moving average |
| 84 | + if (!Ready || delta >= AverageLoadInterval) { |
| 85 | + AverageLoad = InstantLoad; |
| 86 | + QuotedLoad = InstantLoad; |
| 87 | + } else { |
| 88 | + auto ratio = static_cast<double>(delta.GetValue()) / AverageLoadInterval.GetValue(); |
| 89 | + AverageLoad = (1 - ratio) * AverageLoad + ratio * InstantLoad; |
| 90 | + QuotedLoad = (1 - ratio) * QuotedLoad + ratio * InstantLoad; |
| 91 | + } |
| 92 | + Ready = true; |
| 93 | + Counters.CpuLoadRequest.Ok->Inc(); |
| 94 | + Counters.InstantLoadPercentage->Set(static_cast<ui64>(InstantLoad * 100)); |
| 95 | + Counters.AverageLoadPercentage->Set(static_cast<ui64>(AverageLoad * 100)); |
| 96 | + Counters.QuotedLoadPercentage->Set(static_cast<ui64>(QuotedLoad * 100)); |
| 97 | +} |
| 98 | + |
| 99 | +bool TCpuQuotaManager::CheckLoadIsOutdated() { |
| 100 | + if (TInstant::Now() - LastCpuLoad > AverageLoadInterval) { |
| 101 | + Ready = false; |
| 102 | + QuotedLoad = 0.0; |
| 103 | + Counters.QuotedLoadPercentage->Set(0); |
| 104 | + } |
| 105 | + return Ready; |
| 106 | +} |
| 107 | + |
| 108 | +bool TCpuQuotaManager::HasCpuQuota(double maxClusterLoad) { |
| 109 | + LastRequestCpuQuota = TInstant::Now(); |
| 110 | + return maxClusterLoad == 0.0 || ((Ready || !Strict) && QuotedLoad < maxClusterLoad); |
| 111 | +} |
| 112 | + |
| 113 | +TCpuQuotaManager::TCpuQuotaResponse TCpuQuotaManager::RequestCpuQuota(double quota, double maxClusterLoad) { |
| 114 | + if (quota < 0.0 || quota > 1.0) { |
| 115 | + return TCpuQuotaResponse(-1, NYdb::EStatus::OVERLOADED, {NYql::TIssue(TStringBuilder() << "Incorrect quota value (exceeds 1.0 or less than 0.0) " << quota)}); |
| 116 | + } |
| 117 | + quota = quota ? quota : DefaultQueryLoad; |
| 118 | + |
| 119 | + CheckLoadIsOutdated(); |
| 120 | + if (!HasCpuQuota(maxClusterLoad)) { |
| 121 | + return TCpuQuotaResponse(-1, NYdb::EStatus::OVERLOADED, {NYql::TIssue(TStringBuilder() |
| 122 | + << "Cluster is overloaded, current quoted load " << static_cast<ui64>(QuotedLoad * 100) |
| 123 | + << "%, average load " << static_cast<ui64>(AverageLoad * 100) << "%" |
| 124 | + )}); |
| 125 | + } |
| 126 | + |
| 127 | + QuotedLoad += quota; |
| 128 | + Counters.QuotedLoadPercentage->Set(static_cast<ui64>(QuotedLoad * 100)); |
| 129 | + return TCpuQuotaResponse(QuotedLoad * 100); |
| 130 | +} |
| 131 | + |
| 132 | +void TCpuQuotaManager::AdjustCpuQuota(double quota, TDuration duration, double cpuSecondsConsumed) { |
| 133 | + if (!CpuNumber) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (duration && duration < AverageLoadInterval / 2 && quota <= 1.0) { |
| 138 | + quota = quota ? quota : DefaultQueryLoad; |
| 139 | + auto load = (cpuSecondsConsumed * 1000.0 / duration.MilliSeconds()) / CpuNumber; |
| 140 | + if (quota > load) { |
| 141 | + auto adjustment = (quota - load) / 2; |
| 142 | + if (QuotedLoad > adjustment) { |
| 143 | + QuotedLoad -= adjustment; |
| 144 | + } else { |
| 145 | + QuotedLoad = 0.0; |
| 146 | + } |
| 147 | + Counters.QuotedLoadPercentage->Set(static_cast<ui64>(QuotedLoad * 100)); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +} // namespace NFq |
0 commit comments