forked from ydb-platform/nbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathban.cpp
192 lines (155 loc) · 4.52 KB
/
ban.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "ban.h"
#include "config.h"
#include <cloud/storage/core/libs/diagnostics/logging.h>
#include <cloud/storage/core/libs/diagnostics/monitoring.h>
#include <library/cpp/monlib/dynamic_counters/counters.h>
#include <util/generic/algorithm.h>
#include <util/generic/hash_set.h>
#include <util/stream/file.h>
#include <util/string/builder.h>
#include <util/string/cast.h>
#include <util/system/spinlock.h>
using namespace NMonitoring;
namespace NCloud::NBlockStore::NDiscovery {
namespace {
////////////////////////////////////////////////////////////////////////////////
struct TBanListCounters
{
TDynamicCounters::TCounterPtr UpdateCount;
TDynamicCounters::TCounterPtr UpdateErrors;
TDynamicCounters::TCounterPtr BannedCount;
void Register(TDynamicCounters& counters)
{
UpdateCount = counters.GetCounter("UpdateCount", true);
UpdateErrors = counters.GetCounter("UpdateErrors", true);
BannedCount = counters.GetCounter("BannedCount");
}
void OnUpdate()
{
UpdateCount->Inc();
}
void OnUpdateError()
{
UpdateErrors->Inc();
}
void SetBannedCount(int count)
{
*BannedCount = count;
}
};
////////////////////////////////////////////////////////////////////////////////
class TBanList final
: public IBanList
{
private:
TDiscoveryConfigPtr Config;
ILoggingServicePtr Logging;
IMonitoringServicePtr Monitoring;
TLog Log;
TBanListCounters Counters;
THashSet<std::pair<TString, ui16>> Instances;
TAdaptiveLock Lock;
public:
TBanList(
TDiscoveryConfigPtr config,
ILoggingServicePtr logging,
IMonitoringServicePtr monitoring)
: Config(std::move(config))
, Logging(std::move(logging))
, Monitoring(std::move(monitoring))
{
}
public:
void Start() override
{
Log = Logging->CreateLog("BLOCKSTORE_DISCOVERY");
auto counters = Monitoring->GetCounters();
auto rootGroup = counters->GetSubgroup("counters", "blockstore");
auto discoveryCounters = rootGroup->GetSubgroup("component", "discovery");
auto banListCounters = rootGroup->GetSubgroup("subcomponent", "banlist");
Counters.Register(*banListCounters);
}
void Stop() override
{
}
void Update() override
{
Counters.OnUpdate();
THashSet<std::pair<TString, ui16>> instances;
TIFStream is(Config->GetBannedInstanceListFile());
TString line;
while (is.ReadLine(line)) {
TStringBuf host, portStr;
TStringBuf(line).Split('\t', host, portStr);
ui16 port;
if (TryFromString(portStr, port)) {
instances.insert(std::make_pair(TString{host}, port));
} else {
Counters.OnUpdateError();
STORAGE_ERROR(TStringBuilder()
<< "broken banlist entry: " << line.Quote());
}
}
with_lock (Lock) {
Instances.swap(instances);
Counters.SetBannedCount(Instances.size());
}
}
bool IsBanned(const TString& host, ui16 port) const override
{
with_lock (Lock) {
return Instances.contains(std::make_pair(host, port));
}
}
};
////////////////////////////////////////////////////////////////////////////////
class TBanListStub final
: public IBanList
{
private:
TDeque<std::pair<TString, ui16>> Instances;
public:
TBanListStub(TDeque<std::pair<TString, ui16>> instances)
: Instances(std::move(instances))
{
}
public:
void Start() override
{
}
void Stop() override
{
}
void Update() override
{
}
bool IsBanned(const TString& host, ui16 port) const override
{
return FindIf(
Instances.begin(),
Instances.end(),
[&] (const auto& i) {
return host == i.first && port == i.second;
}
) != Instances.end();
}
};
} // namespace
////////////////////////////////////////////////////////////////////////////////
IBanListPtr CreateBanList(
TDiscoveryConfigPtr config,
ILoggingServicePtr logging,
IMonitoringServicePtr monitoring)
{
return std::make_shared<TBanList>(
std::move(config),
std::move(logging),
std::move(monitoring)
);
}
IBanListPtr CreateBanListStub(
TDeque<std::pair<TString, ui16>> instances)
{
return std::make_shared<TBanListStub>(std::move(instances));
}
} // namespace NCloud::NBlockStore::NDiscovery