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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
#include "downtime.h"
#include "cluster_info.h"
#include "scheme.h"
#include <util/generic/algorithm.h>
#include <util/string/join.h>
namespace NKikimr {
namespace NCms {
TDowntime::TDowntime(TDuration ignoredDowntimeGap)
: IgnoredDowntimeGap(ignoredDowntimeGap)
{
}
void TDowntime::AddDowntime(TInstant start, TInstant end, const TString &reason)
{
AddDowntime({start, end, reason});
}
void TDowntime::AddDowntime(TInstant start, TDuration duration, const TString &reason)
{
AddDowntime({start, duration, reason});
}
void TDowntime::AddDowntime(const TSegment segment)
{
if (!segment.Duration())
return;
auto it = DowntimeSegments.insert(segment);
while (it.first != DowntimeSegments.begin()) {
it = CollapseWithPrev(it.first);
if (!it.second)
break;
}
while (++it.first != DowntimeSegments.end()) {
it = CollapseWithPrev(it.first);
if (!it.second)
break;
}
}
void TDowntime::AddDowntime(const TDowntime &downtime)
{
for (auto &segment : downtime.GetDowntimeSegments())
AddDowntime(segment);
}
void TDowntime::AddDowntime(const TLockableItem &item, TInstant now)
{
AddDowntime(item.Downtime);
if (item.State != NKikimrCms::UP)
AddDowntime(item.Timestamp, now, "known downtime");
if (item.Lock.Defined()) {
auto end = Min(now + TDuration::MicroSeconds(item.Lock->Action.GetDuration()),
item.Lock->ActionDeadline);
AddDowntime(now, end, item.Lock->PermissionId);
}
for (auto &lock : item.ExternalLocks) {
auto start = Max(lock.LockStart, now);
AddDowntime(start, lock.LockDeadline, lock.NotificationId);
}
}
bool TDowntime::HasUpcomingDowntime(TInstant now, TDuration distance, TDuration duration) const
{
for (auto &segment : DowntimeSegments) {
if (segment.Start > now + distance)
break;
if (segment.End >= now && segment.Duration() >= duration)
return true;
}
return false;
}
TDuration TDowntime::GetIgnoredDowntimeGap() const
{
return IgnoredDowntimeGap;
}
void TDowntime::SetIgnoredDowntimeGap(TDuration gap)
{
bool merge = IgnoredDowntimeGap < gap;
IgnoredDowntimeGap = gap;
if (merge && !DowntimeSegments.empty()) {
for (auto it = ++DowntimeSegments.begin(); it != DowntimeSegments.end(); ++it)
it = CollapseWithPrev(it).first;
}
}
void TDowntime::CleanupOldSegments(TInstant now)
{
while (!DowntimeSegments.empty()) {
auto it = DowntimeSegments.begin();
if (it->End + IgnoredDowntimeGap < now)
DowntimeSegments.erase(it);
else
break;
}
}
void TDowntime::Serialize(NKikimrCms::TAvailabilityStats *rec) const
{
for (auto &segment : DowntimeSegments) {
auto &entry = *rec->AddDowntimes();
entry.SetStart(segment.Start.GetValue());
entry.SetEnd(segment.End.GetValue());
entry.SetExplanation(segment.Reason);
}
rec->SetIgnoredDowntimeGap(IgnoredDowntimeGap.GetValue());
}
void TDowntime::Deserialize(const NKikimrCms::TAvailabilityStats &rec)
{
SetIgnoredDowntimeGap(TDuration::FromValue(rec.GetIgnoredDowntimeGap()));
for (auto &entry : rec.GetDowntimes()) {
AddDowntime(TInstant::FromValue(entry.GetStart()),
TInstant::FromValue(entry.GetEnd()),
entry.GetExplanation());
}
}
std::pair<TDowntime::TSegments::iterator, bool>
TDowntime::CollapseWithPrev(TSegments::iterator it)
{
auto prev = it;
--prev;
if (it->Start - prev->End <= IgnoredDowntimeGap) {
TString newReason;
if (!prev->Reason || prev->Reason == it->Reason)
newReason = it->Reason;
else if (!it->Reason)
newReason = prev->Reason;
else
newReason = Join(", ", prev->Reason, it->Reason);
TSegment collapsed = {prev->Start, Max(prev->End, it->End), newReason};
DowntimeSegments.erase(prev);
DowntimeSegments.erase(it);
return {DowntimeSegments.insert(collapsed).first, true};
}
return {it, false};
}
TDowntimes::TDowntimes()
{
}
void TDowntimes::SetIgnoredDowntimeGap(TDuration gap)
{
if (IgnoredDowntimeGap == gap)
return;
for (auto &pr : NodeDowntimes)
pr.second.SetIgnoredDowntimeGap(gap);
for (auto &pr : PDiskDowntimes)
pr.second.SetIgnoredDowntimeGap(gap);
}
void TDowntimes::CleanupOld(TInstant now)
{
for (auto &pr : NodeDowntimes)
pr.second.CleanupOldSegments(now);
for (auto &pr : PDiskDowntimes)
pr.second.CleanupOldSegments(now);
}
void TDowntimes::CleanupEmpty()
{
for (auto it = NodeDowntimes.begin(); it != NodeDowntimes.end(); ) {
auto next = it;
++next;
if (it->second.Empty())
NodeDowntimes.erase(it);
it = next;
}
for (auto it = PDiskDowntimes.begin(); it != PDiskDowntimes.end(); ) {
auto next = it;
++next;
if (it->second.Empty())
PDiskDowntimes.erase(it);
it = next;
}
}
bool TDowntimes::DbLoadState(TTransactionContext& txc,
const TActorContext& ctx)
{
Y_UNUSED(ctx);
NIceDb::TNiceDb db(txc.DB);
auto nodeRowset = db.Table<Schema::NodeDowntimes>().Range().Select<Schema::NodeDowntimes::TColumns>();
auto pdiskRowset = db.Table<Schema::PDiskDowntimes>().Range().Select<Schema::PDiskDowntimes::TColumns>();
if (!nodeRowset.IsReady() || !pdiskRowset.IsReady())
return false;
NodeDowntimes.clear();
PDiskDowntimes.clear();
while (!nodeRowset.EndOfSet()) {
ui32 nodeId = nodeRowset.GetValue<Schema::NodeDowntimes::NodeId>();
NKikimrCms::TAvailabilityStats rec = nodeRowset.GetValue<Schema::NodeDowntimes::Downtime>();
NodeDowntimes[nodeId].Deserialize(rec);
if (!nodeRowset.Next())
return false;
}
while (!pdiskRowset.EndOfSet()) {
ui32 nodeId = pdiskRowset.GetValue<Schema::PDiskDowntimes::NodeId>();
ui32 diskId = pdiskRowset.GetValue<Schema::PDiskDowntimes::DiskId>();
NKikimrCms::TAvailabilityStats rec = pdiskRowset.GetValue<Schema::PDiskDowntimes::Downtime>();
PDiskDowntimes[TPDiskID(nodeId, diskId)].Deserialize(rec);
if (!pdiskRowset.Next())
return false;
}
return true;
}
void TDowntimes::DbStoreState(TTransactionContext& txc,
const TActorContext& ctx)
{
NIceDb::TNiceDb db(txc.DB);
for (auto &pr : NodeDowntimes) {
if (pr.second.Empty()) {
db.Table<Schema::NodeDowntimes>().Key(pr.first).Delete();
LOG_TRACE_S(ctx, NKikimrServices::CMS,
"Removed downtime for node " << pr.first << " from local DB");
} else {
NKikimrCms::TAvailabilityStats rec;
pr.second.Serialize(&rec);
db.Table<Schema::NodeDowntimes>().Key(pr.first)
.Update<Schema::NodeDowntimes::Downtime>(rec);
LOG_TRACE_S(ctx, NKikimrServices::CMS,
"Updated downtime for node " << pr.first
<< " in local DB downtime=" << pr.second);
}
}
for (auto &pr : PDiskDowntimes) {
if (pr.second.Empty()) {
db.Table<Schema::PDiskDowntimes>().Key(pr.first.NodeId, pr.first.DiskId)
.Delete();
LOG_TRACE_S(ctx, NKikimrServices::CMS, "Removed downtime for pdisk " << pr.first.ToString()
<< " from local DB");
} else {
NKikimrCms::TAvailabilityStats rec;
pr.second.Serialize(&rec);
db.Table<Schema::PDiskDowntimes>().Key(pr.first.NodeId, pr.first.DiskId)
.Update<Schema::PDiskDowntimes::Downtime>(rec);
LOG_TRACE_S(ctx, NKikimrServices::CMS,
"Updated downtime for pdisk " << pr.first.ToString()
<< " in local DB downtime=" << pr.second);
}
}
}
} // NCms
} // NKikimr
Y_DECLARE_OUT_SPEC(, NKikimr::NCms::TDowntime::TSegment, stream, value) {
stream << "[" << value.Start.ToStringLocalUpToSeconds() << "-"
<< value.End.ToStringLocalUpToSeconds() << "]("
<< value.Reason << ")";
}
Y_DECLARE_OUT_SPEC(, NKikimr::NCms::TDowntime, stream, value) {
stream << JoinSeq(", ", value.GetDowntimeSegments());
}
|