This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
audit.cpp
468 lines (403 loc) · 16.3 KB
/
audit.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (C) 2018 Bluzelle
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <audit/audit.hpp>
#include <boost/beast/core/detail/base64.hpp>
#include <boost/asio/ip/udp.hpp>
#include <boost/format.hpp>
using namespace bzn;
audit::audit(std::shared_ptr<bzn::asio::io_context_base> io_context
, std::shared_ptr<bzn::node_base> node
, bzn::optional<boost::asio::ip::udp::endpoint> monitor_endpoint
, bzn::uuid_t uuid
, size_t mem_size
, bool use_pbft)
: uuid(std::move(uuid))
, node(std::move(node))
, io_context(std::move(io_context))
, leader_alive_timer(this->io_context->make_unique_steady_timer())
, leader_progress_timer(this->io_context->make_unique_steady_timer())
, monitor_endpoint(std::move(monitor_endpoint))
, socket(this->io_context->make_unique_udp_socket())
, statsd_namespace_prefix("com.bluzelle.swarm.singleton.node." + this->uuid + ".")
, mem_size(mem_size)
, use_pbft(use_pbft)
{
}
const std::list<std::string>&
audit::error_strings() const
{
return this->recorded_errors;
}
size_t
audit::error_count() const
{
return this->recorded_errors.size() + this->forgotten_error_count;
}
void
audit::start()
{
std::call_once(this->start_once, [this]()
{
this->node->register_for_message("audit", std::bind(&audit::handle
, shared_from_this()
, std::placeholders::_1
, std::placeholders::_2));
if (this->monitor_endpoint)
{
LOG(info) << boost::format("Audit module running, will send stats to %1%:%2%")
% this->monitor_endpoint->address().to_string()
% this->monitor_endpoint->port();
}
else
{
LOG(info) "Audit module running, but not sending stats anywhere because no monitor configured";
}
LOG(info) << "Audit module running";
if (this->use_pbft)
{
this->pbft_specific_init();
}
else
{
this->raft_specific_init();
}
});
}
void
audit::raft_specific_init()
{
this->reset_leader_alive_timer();
}
void
audit::pbft_specific_init()
{
this->reset_primary_alive_timer();
}
void
audit::reset_primary_alive_timer()
{
LOG(debug) << "starting primary alive timer";
this->primary_dead_count = 0;
// We use the leader_alive_timer here because introducing a new timer would make the tests annoyingly complex
this->leader_alive_timer->cancel();
this->leader_alive_timer->expires_from_now(this->primary_timeout);
this->leader_alive_timer->async_wait(std::bind(&audit::handle_primary_alive_timeout, shared_from_this(), std::placeholders::_1));
}
void
audit::reset_leader_alive_timer()
{
LOG(debug) << "starting leader alive timer";
this->leader_dead_count = 0;
this->leader_alive_timer->cancel();
this->leader_alive_timer->expires_from_now(this->leader_timeout);
this->leader_alive_timer->async_wait(std::bind(&audit::handle_leader_alive_timeout, shared_from_this(), std::placeholders::_1));
}
void
audit::handle_leader_alive_timeout(const boost::system::error_code& ec)
{
if (ec)
{
LOG(debug) << "Leader alive timeout canceled " << ec.message();
return;
}
this->report_error(bzn::NO_LEADER_METRIC_NAME, str(boost::format("No leader alive [%1%]") % ++(this->leader_dead_count)));
this->clear_leader_progress_timer();
this->leader_has_uncommitted_entries = false;
this->leader_alive_timer->expires_from_now(this->leader_timeout);
this->leader_alive_timer->async_wait(std::bind(&audit::handle_leader_alive_timeout, shared_from_this(), std::placeholders::_1));
}
void
audit::handle_primary_alive_timeout(const boost::system::error_code& ec)
{
if (ec)
{
LOG(debug) << "Primary alive timeout canceled " << ec.message();
return;
}
this->report_error(bzn::NO_PRIMARY_METRIC_NAME, str(boost::format("No primary alive [%1%]") % ++(this->primary_dead_count)));
this->leader_alive_timer->expires_from_now(this->primary_timeout);
this->leader_alive_timer->async_wait(std::bind(&audit::handle_primary_alive_timeout, shared_from_this(), std::placeholders::_1));
}
void
audit::reset_leader_progress_timer()
{
LOG(debug) << "(re)starting leader progress timer";
this->leader_stuck_count = 0;
this->leader_progress_timer->cancel();
this->leader_progress_timer->expires_from_now(this->leader_timeout);
this->leader_progress_timer->async_wait(std::bind(&audit::handle_leader_progress_timeout, shared_from_this(), std::placeholders::_1));
}
void
audit::clear_leader_progress_timer()
{
this->leader_stuck_count = 0;
this->leader_progress_timer->cancel();
}
void audit::handle_leader_progress_timeout(const boost::system::error_code& ec)
{
if (ec)
{
LOG(debug) << "Leader progress timeout canceled " << ec.message();
return;
}
this->report_error(bzn::LEADER_STUCK_METRIC_NAME, str(boost::format("Leader alive but not making progress [%1%]") % ++(this->leader_stuck_count)));
this->leader_progress_timer->expires_from_now(this->leader_timeout);
this->leader_progress_timer->async_wait(std::bind(&audit::handle_leader_progress_timeout, shared_from_this(), std::placeholders::_1));
}
void
audit::report_error(const std::string& metric_name, const std::string& description)
{
this->recorded_errors.push_back(description);
std::string metric = this->statsd_namespace_prefix + metric_name;
LOG(fatal) << boost::format("[%1%]: %2%") % metric % description;
this->send_to_monitor(metric + bzn::STATSD_COUNTER_FORMAT);
this->trim();
}
void
audit::send_to_monitor(const std::string& stat)
{
if (!this->monitor_endpoint)
{
return;
}
LOG(debug) << boost::format("Sending stat '%1%' to monitor at %2%:%3%")
% stat
% this->monitor_endpoint->address().to_string()
% this->monitor_endpoint->port();
std::shared_ptr<boost::asio::const_buffer> buffer = std::make_shared<boost::asio::const_buffer>(stat.c_str(), stat.size());
this->socket->async_send_to(*buffer, *(this->monitor_endpoint),
[buffer](const boost::system::error_code& error, std::size_t bytes)
{
LOG(error) << boost::format("UDP send failed, sent %1% bytes, '%2'") % error.message() % bytes;
}
);
}
void
audit::handle(const bzn::message& json, std::shared_ptr<bzn::session_base> session)
{
audit_message message;
message.ParseFromString(boost::beast::detail::base64_decode(json["audit-data"].asString()));
LOG(debug) << "Got audit message" << message.DebugString();
if (message.has_raft_commit())
{
this->handle_raft_commit(message.raft_commit());
}
else if (message.has_leader_status())
{
this->handle_leader_status(message.leader_status());
}
else if (message.has_pbft_commit())
{
this->handle_pbft_commit(message.pbft_commit());
}
else if (message.has_primary_status())
{
this->handle_primary_status(message.primary_status());
}
else if (message.has_failure_detected())
{
this->handle_failure_detected(message.failure_detected());
}
else
{
LOG(error) << "Got an unknown audit message? " << message.ShortDebugString();
}
session->close();
}
void audit::handle_primary_status(const primary_status& primary_status)
{
if (!this->use_pbft)
{
LOG(debug) << "audit ignoring primary status message because we are in raft mode";
return;
}
std::lock_guard<std::mutex> lock(this->audit_lock);
if (this->recorded_primaries.count(primary_status.view()) == 0)
{
LOG(info) << "audit recording that primary of view " << primary_status.view() << " is '" << primary_status.primary() << "'";
this->send_to_monitor(bzn::PRIMARY_HEARD_METRIC_NAME+bzn::STATSD_COUNTER_FORMAT);
this->recorded_primaries[primary_status.view()] = primary_status.primary();
this->trim();
}
else if (this->recorded_primaries[primary_status.view()] != primary_status.primary())
{
std::string err = str(boost::format(
"Conflicting primary elected! '%1%' is the recorded primary of view %2%, but '%3%' claims to be the primary of the same view.")
% this->recorded_primaries[primary_status.view()]
% primary_status.view()
% primary_status.primary());
this->report_error(bzn::PRIMARY_CONFLICT_METRIC_NAME, err);
}
this->reset_primary_alive_timer();
}
void
audit::handle_leader_status(const leader_status& leader_status)
{
if (this->use_pbft)
{
LOG(debug) << "audit ignoring leader status message because we are in pbft mode";
return;
}
std::lock_guard<std::mutex> lock(this->audit_lock);
if (this->recorded_leaders.count(leader_status.term()) == 0)
{
LOG(info) << "audit recording that leader of term " << leader_status.term() << " is '" << leader_status.leader() << "'";
this->send_to_monitor(bzn::NEW_LEADER_METRIC_NAME+bzn::STATSD_COUNTER_FORMAT);
this->recorded_leaders[leader_status.term()] = leader_status.leader();
this->trim();
}
else if (this->recorded_leaders[leader_status.term()] != leader_status.leader())
{
std::string err = str(boost::format(
"Conflicting leader elected! '%1%' is the recorded leader of term %2%, but '%3%' claims to be the leader of the same term.")
% this->recorded_leaders[leader_status.term()]
% leader_status.term()
% leader_status.leader());
this->report_error(bzn::LEADER_CONFLICT_METRIC_NAME, err);
}
this->reset_leader_alive_timer();
this->handle_leader_data(leader_status);
}
void
audit::handle_leader_data(const leader_status& leader_status)
{
// The goal here is to implement a timer based on the leader making "progress" -
// committing the uncommitted entries in its log. Specifically:
// - The timer must be reset whenever the leader changes
// - The timer must be halted when the leader has no uncommitted entries
// - The timer must be reset, but not halted, when the leader commits some but not all of the uncommitted entries
// - The timer must be restarted from full when the leader gets an uncommitted entry where it previously had none
if (leader_status.leader() != this->last_leader)
{
// Leader has changed - halt or restart timer
this->last_leader = leader_status.leader();
this->handle_leader_made_progress(leader_status);
}
else if (leader_status.current_commit_index() > this->last_leader_commit_index)
{
// Leader has made progress - halt or restart timer
this->handle_leader_made_progress(leader_status);
}
else if (leader_status.current_log_index() > leader_status.current_commit_index() && !this->leader_has_uncommitted_entries)
{
// Leader didn't have uncommitted entries, now it does - restart timer
this->reset_leader_progress_timer();
this->leader_has_uncommitted_entries = true;
}
this->last_leader_commit_index = leader_status.current_commit_index();
}
void audit::handle_leader_made_progress(const leader_status& leader_status)
{
// Extracted common logic in the case where we know that the leader has made some progress,
// but may or may not still have more messages to commit
if (leader_status.current_commit_index() == leader_status.current_log_index())
{
this->clear_leader_progress_timer();
this->leader_has_uncommitted_entries = false;
}
else
{
this->reset_leader_progress_timer();
this->leader_has_uncommitted_entries = true;
}
}
void
audit::handle_raft_commit(const raft_commit_notification& commit)
{
std::lock_guard<std::mutex> lock(this->audit_lock);
if (this->use_pbft)
{
LOG(debug) << "audit ignoring raft commit message because we are in pbft mode";
return;
}
this->send_to_monitor(bzn::RAFT_COMMIT_METRIC_NAME + bzn::STATSD_COUNTER_FORMAT);
if (this->recorded_raft_commits.count(commit.log_index()) == 0)
{
LOG(info) << "audit recording that message '" << commit.operation() << "' is committed at index " << commit.log_index();
this->recorded_raft_commits[commit.log_index()] = commit.operation();
this->trim();
}
else if (this->recorded_raft_commits[commit.log_index()] != commit.operation())
{
std::string err = str(boost::format(
"Conflicting commit detected! '%1%' is the recorded entry at index %2%, but '%3%' has been committed with the same index.")
% this->recorded_raft_commits[commit.log_index()]
% commit.log_index()
% commit.operation());
this->report_error(bzn::RAFT_COMMIT_CONFLICT_METRIC_NAME, err);
}
}
void
audit::handle_pbft_commit(const pbft_commit_notification& commit)
{
std::lock_guard<std::mutex> lock(this->audit_lock);
if (!this->use_pbft)
{
LOG(debug) << "audit ignoring pbft commit message because we are in raft mode";
return;
}
this->send_to_monitor(bzn::PBFT_COMMIT_METRIC_NAME + bzn::STATSD_COUNTER_FORMAT);
if (this->recorded_pbft_commits.count(commit.sequence_number()) == 0)
{
LOG(info) << "audit recording that message '" << commit.operation() << "' is committed at sequence " << commit.sequence_number();
this->recorded_pbft_commits[commit.sequence_number()] = commit.operation();
this->trim();
}
else if (this->recorded_pbft_commits[commit.sequence_number()] != commit.operation())
{
std::string err = str(boost::format(
"Conflicting commit detected! '%1%' is the recorded entry at sequence %2%, but '%3%' has been committed with the same sequence.")
% this->recorded_pbft_commits[commit.sequence_number()]
% commit.sequence_number()
% commit.operation());
this->report_error(bzn::PBFT_COMMIT_CONFLICT_METRIC_NAME, err);
}
}
void
audit::handle_failure_detected(const failure_detected& /*failure*/)
{
// TODO KEP-539: more info in this message
std::lock_guard<std::mutex> lock(this->audit_lock);
if (!this->use_pbft)
{
LOG(debug) << "audit ignoring pbft failure detected message because we are in raft mode";
return;
}
this->send_to_monitor(bzn::FAILURE_DETECTED_METRIC_NAME + bzn::STATSD_COUNTER_FORMAT);
}
size_t
audit::current_memory_size()
{
return this->recorded_raft_commits.size() + this->recorded_errors.size() + this->recorded_leaders.size();
}
void
audit::trim()
{
while(this->recorded_errors.size() > this->mem_size)
{
this->recorded_errors.pop_front();
this->forgotten_error_count++;
}
// Here we're removing the lowest term/log index entries, which is sort of like the oldest entries. I'd rather
// remove entries at random, but that's not straightforward to do with STL containers without making some onerous
// performance compromise.
while(this->recorded_leaders.size() > this->mem_size)
{
this->recorded_leaders.erase(this->recorded_leaders.begin());
}
while(this->recorded_raft_commits.size() > this->mem_size)
{
this->recorded_raft_commits.erase(this->recorded_raft_commits.begin());
}
}