forked from ydb-platform/nbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquota_metrics.cpp
126 lines (103 loc) · 3.11 KB
/
quota_metrics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "quota_metrics.h"
#include <cloud/storage/core/libs/diagnostics/max_calculator.h>
#include <library/cpp/monlib/dynamic_counters/counters.h>
#include <library/cpp/deprecated/atomic/atomic.h>
#include <util/system/mutex.h>
namespace NCloud::NBlockStore {
////////////////////////////////////////////////////////////////////////////////
struct TUsedQuotaMetrics::TImpl
{
NMonitoring::TDynamicCountersPtr Group;
NMonitoring::TDynamicCounters::TCounterPtr UsedQuota;
NMonitoring::TDynamicCounters::TCounterPtr MaxUsedQuota;
TMaxCalculator<DEFAULT_BUCKET_COUNT> MaxUsedQuotaCalc;
TAtomic IsInitialized = 0;
TRWMutex Mutex;
explicit TImpl(ITimerPtr timer)
: MaxUsedQuotaCalc(std::move(timer))
{}
void Register(NMonitoring::TDynamicCountersPtr counters)
{
auto guard = TWriteGuard(Mutex);
if (!AtomicGet(IsInitialized) && counters) {
Group = std::move(counters);
UsedQuota = Group->GetCounter("UsedQuota");
MaxUsedQuota = Group->GetCounter("MaxUsedQuota");
AtomicSet(IsInitialized, 1);
}
}
void Unregister()
{
auto guard = TWriteGuard(Mutex);
if (AtomicGet(IsInitialized)) {
Group->RemoveCounter("UsedQuota");
Group->RemoveCounter("MaxUsedQuota");
UsedQuota.Drop();
MaxUsedQuota.Drop();
for (ui32 i = 0; i < DEFAULT_BUCKET_COUNT + 1; ++i) {
MaxUsedQuotaCalc.Add(0);
MaxUsedQuotaCalc.NextValue();
}
AtomicSet(IsInitialized, 0);
}
}
void UpdateQuota(ui64 quota)
{
auto guard = TReadGuard(Mutex);
MaxUsedQuotaCalc.Add(quota);
if (UsedQuota) {
UsedQuota->Set(quota);
}
}
void UpdateMaxQuota()
{
auto guard = TWriteGuard(Mutex);
if (MaxUsedQuota) {
MaxUsedQuota->Set(MaxUsedQuotaCalc.NextValue());
}
}
void UpdateMaxQuota(ui64 maxQuota)
{
auto guard = TWriteGuard(Mutex);
MaxUsedQuotaCalc.NextValue();
if (MaxUsedQuota) {
MaxUsedQuota->Set(maxQuota);
}
}
};
////////////////////////////////////////////////////////////////////////////////
TUsedQuotaMetrics::TUsedQuotaMetrics(ITimerPtr timer)
: Impl(std::make_unique<TImpl>(std::move(timer)))
{}
TUsedQuotaMetrics::~TUsedQuotaMetrics() = default;
void TUsedQuotaMetrics::Register(NMonitoring::TDynamicCountersPtr counters)
{
if (!AtomicGet(Impl->IsInitialized)) {
Impl->Register(std::move(counters));
}
}
void TUsedQuotaMetrics::Unregister()
{
if (AtomicGet(Impl->IsInitialized)) {
Impl->Unregister();
}
}
void TUsedQuotaMetrics::UpdateQuota(ui64 quota)
{
if (AtomicGet(Impl->IsInitialized)) {
Impl->UpdateQuota(quota);
}
}
void TUsedQuotaMetrics::UpdateMaxQuota()
{
if (AtomicGet(Impl->IsInitialized)) {
Impl->UpdateMaxQuota();
}
}
void TUsedQuotaMetrics::UpdateMaxQuota(ui64 maxQuota)
{
if (AtomicGet(Impl->IsInitialized)) {
Impl->UpdateMaxQuota(maxQuota);
}
}
} // namespace NCloud::NBlockStore