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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
#include "lease_holder.h"
#include "node_broker.h"
#include <library/cpp/actors/core/actor_bootstrapped.h>
#include <library/cpp/actors/core/hfunc.h>
#include <ydb/core/mon/mon.h>
#include <library/cpp/actors/core/mon.h>
#include <ydb/core/base/appdata.h>
#include <ydb/core/base/tablet_pipe.h>
#include <library/cpp/monlib/service/pages/templates.h>
#include <util/random/random.h>
namespace NKikimr {
namespace NNodeBroker {
using namespace NMon;
class TLeaseHolder : public TActorBootstrapped<TLeaseHolder> {
private:
struct TEvPrivate {
enum EEv {
EvExpire = EventSpaceBegin(TEvents::ES_PRIVATE),
EvEnd
};
static_assert(EvEnd < EventSpaceEnd(TEvents::ES_PRIVATE), "expect EvEnd < EventSpaceEnd(TEvents::ES_PRIVATE)");
struct TEvExpire : TEventLocal<TEvExpire, EvExpire> {};
};
public:
using TBase = TActorBootstrapped<TLeaseHolder>;
static constexpr NKikimrServices::TActivity::EType ActorActivityType() {
return NKikimrServices::TActivity::NODE_BROKER_LEASE_HOLDER;
}
TLeaseHolder(TInstant expire)
: LastPingEpoch(0)
, Expire(expire)
{
}
void Bootstrap(const TActorContext &ctx)
{
NActors::TMon* mon = AppData(ctx)->Mon;
if (mon) {
NMonitoring::TIndexMonPage *actorsMonPage = mon->RegisterIndexPage("actors", "Actors");
mon->RegisterActorPage(actorsMonPage, "lease", "Dynamic node lease status",
false, ctx.ExecutorThread.ActorSystem, ctx.SelfID);
}
Become(&TThis::StatePing);
ScheduleExpire(ctx);
Ping(ctx);
}
private:
STFUNC(StateIdle)
{
switch (ev->GetTypeRewrite()) {
HFunc(TEvents::TEvWakeup, HandleIdle);
HFunc(TEvHttpInfo, Handle);
HFunc(TEvPrivate::TEvExpire, Handle);
HFunc(TEvTabletPipe::TEvClientDestroyed, HandleIdle);
HFunc(TEvTabletPipe::TEvClientConnected, HandleIdle);
IgnoreFunc(TEvNodeBroker::TEvExtendLeaseResponse);
default:
Y_FAIL("TLeaseHolder::StateIdle unexpected event type: %" PRIx32 " event: %s",
ev->GetTypeRewrite(), ev->HasEvent() ? ev->GetBase()->ToString().data() : "serialized?");
}
}
STFUNC(StatePing)
{
switch (ev->GetTypeRewrite()) {
HFunc(TEvHttpInfo, Handle);
HFunc(TEvNodeBroker::TEvExtendLeaseResponse, Handle);
HFunc(TEvPrivate::TEvExpire, Handle);
HFunc(TEvTabletPipe::TEvClientDestroyed, Handle);
HFunc(TEvTabletPipe::TEvClientConnected, Handle);
default:
Y_FAIL("TLeaseHolder::StatePing unexpected event type: %" PRIx32 " event: %s",
ev->GetTypeRewrite(), ev->HasEvent() ? ev->GetBase()->ToString().data() : "serialized?");
}
}
void Die(const TActorContext &ctx)
{
if (NodeBrokerPipe)
NTabletPipe::CloseClient(ctx, NodeBrokerPipe);
TBase::Die(ctx);
}
void OnPipeDestroyedIdle(const TActorContext &ctx)
{
NTabletPipe::CloseClient(ctx, NodeBrokerPipe);
NodeBrokerPipe = TActorId();
}
void HandleIdle(TEvTabletPipe::TEvClientConnected::TPtr &ev, const TActorContext &ctx) {
if (ev->Get()->ClientId == NodeBrokerPipe && ev->Get()->Status != NKikimrProto::OK)
OnPipeDestroyedIdle(ctx);
}
void HandleIdle(TEvTabletPipe::TEvClientDestroyed::TPtr &ev, const TActorContext &ctx) {
if (ev->Get()->ClientId == NodeBrokerPipe)
OnPipeDestroyedIdle(ctx);
}
void HandleIdle(TEvents::TEvWakeup::TPtr &, const TActorContext &ctx) {
Become(&TThis::StatePing);
Ping(ctx);
}
void OnPipeDestroyed(const TActorContext &ctx)
{
NTabletPipe::CloseClient(ctx, NodeBrokerPipe);
NodeBrokerPipe = TActorId();
Ping(ctx);
}
void Handle(TEvTabletPipe::TEvClientConnected::TPtr &ev, const TActorContext &ctx) {
if (ev->Get()->ClientId == NodeBrokerPipe && ev->Get()->Status != NKikimrProto::OK)
OnPipeDestroyed(ctx);
}
void Handle(TEvTabletPipe::TEvClientDestroyed::TPtr &ev, const TActorContext &ctx) {
if (ev->Get()->ClientId == NodeBrokerPipe)
OnPipeDestroyed(ctx);
}
void Connect(const TActorContext &ctx)
{
auto dinfo = AppData(ctx)->DomainsInfo;
auto &domain = dinfo->GetDomain(NodeIdToDomain(ctx.SelfID.NodeId(), *dinfo));
ui32 group = domain.DefaultStateStorageGroup;
NTabletPipe::TClientConfig config;
config.RetryPolicy = NTabletPipe::TClientRetryPolicy::WithRetries();
auto pipe = NTabletPipe::CreateClient(ctx.SelfID, MakeNodeBrokerID(group), config);
NodeBrokerPipe = ctx.Register(pipe);
}
void Handle(TEvNodeBroker::TEvExtendLeaseResponse::TPtr &ev, const TActorContext &ctx) noexcept
{
auto &rec = ev->Get()->Record;
// Error means Node Broker doesn't know about this node.
// Node is either already expired or its ID is banned.
Y_VERIFY(rec.GetNodeId() == ctx.SelfID.NodeId());
if (rec.GetStatus().GetCode() != NKikimrNodeBroker::TStatus::OK) {
LOG_ERROR(ctx, NKikimrServices::NODE_BROKER, "Cannot extend lease: %s",
rec.GetStatus().GetReason().data());
return;
}
Expire = TInstant::MicroSeconds(rec.GetExpire());
LastResponse = ctx.Now();
LOG_DEBUG_S(ctx, NKikimrServices::NODE_BROKER,
"Node has now extended lease expiring " << ToString(Expire));
if (rec.HasEpoch()) {
LastPingEpoch = rec.GetEpoch().GetId();
EpochEnd = TInstant::FromValue(rec.GetEpoch().GetEnd());
if (Expire != TInstant::Max()) {
Y_VERIFY(Expire > EpochEnd);
Y_VERIFY(rec.GetExpire() == rec.GetEpoch().GetNextEnd());
ui64 window = (Expire - EpochEnd).GetValue() / 2;
Y_VERIFY(window);
NextPing = EpochEnd + TDuration::FromValue(RandomNumber<ui64>(window));
}
} else {
NextPing = ctx.Now() + (Expire - ctx.Now()) / 2;
}
if (Expire != TInstant::Max())
ctx.Schedule(NextPing - ctx.Now(), new TEvents::TEvWakeup());
else
NextPing = TInstant::Max();
Become(&TThis::StateIdle);
}
void Ping(const TActorContext &ctx)
{
if (!NodeBrokerPipe)
Connect(ctx);
auto request = MakeHolder<TEvNodeBroker::TEvExtendLeaseRequest>();
request->Record.SetNodeId(ctx.SelfID.NodeId());
NTabletPipe::SendData(ctx, NodeBrokerPipe, request.Release());
}
void Handle(TEvHttpInfo::TPtr& ev, const TActorContext& ctx) {
TStringStream str;
HTML(str) {
PRE() {
str << "Lease expires: " << ToString(Expire) << Endl
<< "Last lease extension: " << ToString(LastResponse) << Endl
<< "Last ping epoch: " << LastPingEpoch << Endl
<< "Epoch end: " << ToString(EpochEnd) << Endl
<< "Next ping at: " << ToString(NextPing) << Endl;
}
}
ctx.Send(ev->Sender, new TEvHttpInfoRes(str.Str()));
}
void ScheduleExpire(const TActorContext &ctx)
{
if (Expire != TInstant::Max())
ctx.Schedule(Expire - ctx.Now(), new TEvPrivate::TEvExpire);
}
void Handle(TEvPrivate::TEvExpire::TPtr &,const TActorContext &ctx)
{
if (Expire <= ctx.Now())
StopNode(ctx);
else
ScheduleExpire(ctx);
}
void StopNode(const TActorContext &ctx)
{
LOG_ERROR_S(ctx, NKikimrServices::NODE_BROKER, "Stop node upon lease expiration (exit code 2)");
AppData(ctx)->KikimrShouldContinue->ShouldStop(2);
}
TString ToString(TInstant t) const
{
if (t == TInstant::Max())
return "NEVER";
return t.ToRfc822StringLocal();
}
private:
TActorId NodeBrokerPipe;
ui64 LastPingEpoch;
TInstant EpochEnd;
TInstant Expire;
TInstant LastResponse;
TInstant NextPing;
};
IActor *CreateLeaseHolder(TInstant expire)
{
return new TLeaseHolder(expire);
}
} // NNodeBroker
} // NKikimr
|