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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
#pragma once
#include <ydb/library/actors/core/actor.h>
#include <ydb/library/actors/core/actor_bootstrapped.h>
#include <ydb/library/actors/core/hfunc.h>
#include <ydb/core/base/appdata.h>
#include <ydb/core/base/counters.h>
#include <ydb/core/base/feature_flags.h>
#include <ydb/core/base/domain.h>
#include <ydb/core/base/statestorage.h>
#include <ydb/core/health_check/health_check.h>
#include <ydb/core/protos/db_metadata_cache.pb.h>
#include <algorithm>
namespace NKikimr {
inline TString MakeDatabaseMetadataCacheBoardPath(const TString& database) {
return "metadatacache+" + database;
}
class TDatabaseMetadataCache : public TActorBootstrapped<TDatabaseMetadataCache> {
public:
enum EEv {
EvRefreshCache = EventSpaceBegin(TKikimrEvents::ES_DB_METADATA_CACHE),
EvEnd
};
static_assert(EvEnd < EventSpaceEnd(TKikimrEvents::ES_DB_METADATA_CACHE),
"expect EvEnd < EventSpaceEnd(TKikimrEvents::ES_DB_METADATA_CACHE)");
struct TEvRefreshCache : TEventLocal<TEvRefreshCache, EvRefreshCache> {};
using TBoardInfoEntries = TMap<TActorId, TEvStateStorage::TBoardInfoEntry>;
private:
static constexpr TDuration TIMEOUT = TDuration::Seconds(15);
const TString Path;
const TString BoardPath;
ui32 ActiveNode;
std::optional<Ydb::Monitoring::SelfCheckResult> Result;
TInstant LastResultUpdate;
std::vector<TActorId> Clients;
TBoardInfoEntries BoardInfo;
TActorId PublishActor;
TActorId SubscribeActor;
bool RequestInProgress = false;
::NMonitoring::TDynamicCounterPtr Counters;
static const inline TString HEALTHCHECK_REQUESTS_MADE_COUNTER = "DbMetadataCache/HealthCheckRequestsMade";
static const inline TString HEALTHCHECK_REQUESTS_ANSWERED_COUNTER = "DbMetadataCache/HealthCheckRequestsAnswered";
void SendRequest() {
if (RequestInProgress) {
return;
}
RequestInProgress = true;
auto request = std::make_unique<NHealthCheck::TEvSelfCheckRequest>();
request->Database = Path;
request->Request.set_return_verbose_status(true);
Send(NHealthCheck::MakeHealthCheckID(), request.release());
Counters->GetCounter(HEALTHCHECK_REQUESTS_MADE_COUNTER, true)->Inc();
}
void Reply(TActorId client) {
auto response = std::make_unique<NHealthCheck::TEvSelfCheckResultProto>();
response->Record = *Result;
Send(client, response.release());
Counters->GetCounter(HEALTHCHECK_REQUESTS_ANSWERED_COUNTER, true)->Inc();
}
void RefreshCache() {
SendRequest();
Schedule(TIMEOUT, new TEvRefreshCache());
}
void UpdateActiveNode() {
ActiveNode = PickActiveNode(BoardInfo);
LOG_DEBUG_S(TActivationContext::AsActorContext(), NKikimrServices::DB_METADATA_CACHE, "Active node is " << ActiveNode);
bool areWeActive = (ActiveNode == SelfId().NodeId());
if (areWeActive && CurrentStateFunc() != &TThis::StateActive) {
RefreshCache();
Become(&TThis::StateActive);
} else if (!areWeActive && CurrentStateFunc() != &TThis::StateInactive) {
Become(&TThis::StateInactive);
}
}
void SubscribeToBoard() {
auto domainInfo = AppData()->DomainsInfo->Domains.begin()->second;
SubscribeActor = RegisterWithSameMailbox(CreateBoardLookupActor(BoardPath,
SelfId(),
domainInfo->DefaultStateStorageGroup,
EBoardLookupMode::Subscription));
}
void Handle(TEvStateStorage::TEvBoardInfo::TPtr& ev) {
if (ev->Get()->Status != TEvStateStorage::TEvBoardInfo::EStatus::Ok) {
SubscribeToBoard();
return;
}
BoardInfo = std::move(ev->Get()->InfoEntries);
UpdateActiveNode();
}
void Handle(TEvStateStorage::TEvBoardInfoUpdate::TPtr& ev) {
if (ev->Get()->Status != TEvStateStorage::TEvBoardInfo::EStatus::Ok) {
SubscribeToBoard();
return;
}
auto& updates = ev->Get()->Updates;
for (auto& [actor, update] : updates) {
if (update.Dropped) {
BoardInfo.erase(actor);
} else {
BoardInfo.insert_or_assign(actor, std::move(update));
}
}
UpdateActiveNode();
}
void Handle(NHealthCheck::TEvSelfCheckResult::TPtr& ev) {
RequestInProgress = false;
Result = ev->Get()->Result;
LastResultUpdate = TActivationContext::Now();
for (const auto& client : Clients) {
Reply(client);
}
Clients.clear();
}
void Handle(NHealthCheck::TEvSelfCheckRequestProto::TPtr& ev) {
LOG_DEBUG_S(TActivationContext::AsActorContext(), NKikimrServices::DB_METADATA_CACHE, "Got request");
TInstant now = TActivationContext::Now();
if (Result && now - LastResultUpdate <= 2 * TIMEOUT) {
LOG_DEBUG_S(TActivationContext::AsActorContext(), NKikimrServices::DB_METADATA_CACHE, "Replying now");
Reply(ev->Sender);
} else {
LOG_DEBUG_S(TActivationContext::AsActorContext(), NKikimrServices::DB_METADATA_CACHE, "Answer not ready, waiting");
SendRequest();
Clients.push_back(ev->Sender);
}
}
public:
TDatabaseMetadataCache(const TString& path,
const ::NMonitoring::TDynamicCounterPtr& counters) : Path(path)
, BoardPath(MakeDatabaseMetadataCacheBoardPath(Path))
{
Counters = GetServiceCounters(counters, "utils");
}
static ui32 PickActiveNode(const TBoardInfoEntries& infoEntries) {
ui32 result = 0;
TInstant minStartTime = TInstant::Max();
for (const auto& [actor, entry] : infoEntries) {
if (entry.Dropped) {
continue;
}
NKikimrMetadataCache::TDatabaseMetadataCacheInfo info;
if (!info.ParseFromString(entry.Payload) || !info.HasStartTimestamp()) {
continue;
}
TInstant startTime = TInstant::MicroSeconds(info.GetStartTimestamp());
if (startTime < minStartTime) {
minStartTime = startTime;
result = actor.NodeId();
}
}
return result;
}
void Bootstrap() {
LOG_DEBUG_S(TActivationContext::AsActorContext(), NKikimrServices::DB_METADATA_CACHE, "Starting db metadata cache actor");
auto domainInfo = AppData()->DomainsInfo->Domains.begin()->second;
TInstant now = TActivationContext::Now();
NKikimrMetadataCache::TDatabaseMetadataCacheInfo info;
info.SetStartTimestamp(now.MicroSeconds());
PublishActor = RegisterWithSameMailbox(CreateBoardPublishActor(BoardPath,
info.SerializeAsString(),
SelfId(),
domainInfo->DefaultStateStorageGroup,
0,
true));
SubscribeToBoard();
Become(&TThis::StateWait);
}
void PassAway() override {
Send(PublishActor, new TEvents::TEvPoison);
Send(SubscribeActor, new TEvents::TEvPoison);
return TActor::PassAway();
}
STATEFN(StateWait) {
switch (ev->GetTypeRewrite()) {
IgnoreFunc(TEvRefreshCache);
hFunc(TEvStateStorage::TEvBoardInfo, Handle);
cFunc(TEvents::TEvPoison::EventType, PassAway);
hFunc(NHealthCheck::TEvSelfCheckResult, Handle);
hFunc(NHealthCheck::TEvSelfCheckRequestProto, Handle);
default: Y_ABORT("Unexpected event: %s", ev->ToString().c_str());
}
}
STATEFN(StateActive) {
switch (ev->GetTypeRewrite()) {
cFunc(EvRefreshCache, RefreshCache);
hFunc(NHealthCheck::TEvSelfCheckResult, Handle);
hFunc(NHealthCheck::TEvSelfCheckRequestProto, Handle);
hFunc(TEvStateStorage::TEvBoardInfo, Handle);
hFunc(TEvStateStorage::TEvBoardInfoUpdate, Handle);
cFunc(TEvents::TEvPoison::EventType, PassAway);
default: Y_ABORT("Unexpected event: %s", ev->ToString().c_str());
}
}
STATEFN(StateInactive) {
switch (ev->GetTypeRewrite()) {
IgnoreFunc(TEvRefreshCache);
hFunc(NHealthCheck::TEvSelfCheckResult, Handle);
hFunc(NHealthCheck::TEvSelfCheckRequestProto, Handle);
hFunc(TEvStateStorage::TEvBoardInfo, Handle);
hFunc(TEvStateStorage::TEvBoardInfoUpdate, Handle);
cFunc(TEvents::TEvPoison::EventType, PassAway);
default: Y_ABORT("Unexpected event: %s", ev->ToString().c_str());
}
}
};
inline TActorId MakeDatabaseMetadataCacheId(ui32 nodeId) {
return TActorId(nodeId, "METACACHE");
}
inline std::unique_ptr<IActor> CreateDatabaseMetadataCache(const TString& path, const ::NMonitoring::TDynamicCounterPtr& counters) {
return std::unique_ptr<IActor>(new TDatabaseMetadataCache(path, counters));
}
inline std::optional<TActorId> ResolveActiveDatabaseMetadataCache(const TMap<TActorId, TEvStateStorage::TBoardInfoEntry>& infoEntries) {
auto activeNode = TDatabaseMetadataCache::PickActiveNode(infoEntries);
if (activeNode == 0) {
return std::nullopt;
}
return MakeDatabaseMetadataCacheId(activeNode);
}
} // NKikimr
|