This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Copy pathbanking_trace.rs
609 lines (531 loc) · 19.8 KB
/
banking_trace.rs
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use {
crate::sigverify::SigverifyTracerPacketStats,
bincode::serialize_into,
chrono::{DateTime, Local},
crossbeam_channel::{unbounded, Receiver, SendError, Sender, TryRecvError},
rolling_file::{RollingCondition, RollingConditionBasic, RollingFileAppender},
solana_perf::{
packet::{to_packet_batches, PacketBatch},
test_tx::test_tx,
},
solana_sdk::{hash::Hash, slot_history::Slot},
std::{
fs::{create_dir_all, remove_dir_all},
io::{self, Write},
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread::{self, sleep, JoinHandle},
time::{Duration, SystemTime},
},
tempfile::TempDir,
thiserror::Error,
};
pub type BankingPacketBatch = Arc<(Vec<PacketBatch>, Option<SigverifyTracerPacketStats>)>;
pub type BankingPacketSender = TracedSender;
pub type BankingPacketReceiver = Receiver<BankingPacketBatch>;
pub type TracerThreadResult = Result<(), TraceError>;
pub type TracerThread = Option<JoinHandle<TracerThreadResult>>;
pub type DirByteLimit = u64;
#[derive(Error, Debug)]
pub enum TraceError {
#[error("IO Error: {0}")]
IoError(#[from] std::io::Error),
#[error("Serialization Error: {0}")]
SerializeError(#[from] bincode::Error),
#[error("Integer Cast Error: {0}")]
IntegerCastError(#[from] std::num::TryFromIntError),
#[error("Trace directory's byte limit is too small (must be larger than {1}): {0}")]
TooSmallDirByteLimit(DirByteLimit, DirByteLimit),
}
const BASENAME: &str = "events";
const TRACE_FILE_ROTATE_COUNT: u64 = 14; // target 2 weeks retention under normal load
const TRACE_FILE_WRITE_INTERVAL_MS: u64 = 100;
const BUF_WRITER_CAPACITY: usize = 10 * 1024 * 1024;
pub const TRACE_FILE_DEFAULT_ROTATE_BYTE_THRESHOLD: u64 = 1024 * 1024 * 1024;
pub const DISABLED_BAKING_TRACE_DIR: DirByteLimit = 0;
pub const BANKING_TRACE_DIR_DEFAULT_BYTE_LIMIT: DirByteLimit =
TRACE_FILE_DEFAULT_ROTATE_BYTE_THRESHOLD * TRACE_FILE_ROTATE_COUNT;
#[derive(Clone, Debug)]
struct ActiveTracer {
trace_sender: Sender<TimedTracedEvent>,
exit: Arc<AtomicBool>,
}
#[derive(Debug)]
pub struct BankingTracer {
active_tracer: Option<ActiveTracer>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct TimedTracedEvent(std::time::SystemTime, TracedEvent);
#[derive(Serialize, Deserialize, Debug)]
enum TracedEvent {
PacketBatch(ChannelLabel, BankingPacketBatch),
BlockAndBankHash(Slot, Hash, Hash),
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub enum ChannelLabel {
NonVote,
TpuVote,
GossipVote,
Dummy,
}
struct RollingConditionGrouped {
basic: RollingConditionBasic,
tried_rollover_after_opened: bool,
is_checked: bool,
}
impl RollingConditionGrouped {
fn new(basic: RollingConditionBasic) -> Self {
Self {
basic,
tried_rollover_after_opened: bool::default(),
is_checked: bool::default(),
}
}
fn reset(&mut self) {
self.is_checked = false;
}
}
struct GroupedWriter<'a> {
now: DateTime<Local>,
underlying: &'a mut RollingFileAppender<RollingConditionGrouped>,
}
impl<'a> GroupedWriter<'a> {
fn new(underlying: &'a mut RollingFileAppender<RollingConditionGrouped>) -> Self {
Self {
now: Local::now(),
underlying,
}
}
}
impl RollingCondition for RollingConditionGrouped {
fn should_rollover(&mut self, now: &DateTime<Local>, current_filesize: u64) -> bool {
if !self.tried_rollover_after_opened {
self.tried_rollover_after_opened = true;
// rollover normally if empty to reuse it if possible
if current_filesize > 0 {
// forcibly rollover anew, so that we always avoid to append
// to a possibly-damaged tracing file even after unclean
// restarts
return true;
}
}
if !self.is_checked {
self.is_checked = true;
self.basic.should_rollover(now, current_filesize)
} else {
false
}
}
}
impl<'a> Write for GroupedWriter<'a> {
fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, io::Error> {
self.underlying.write_with_datetime(buf, &self.now)
}
fn flush(&mut self) -> std::result::Result<(), io::Error> {
self.underlying.flush()
}
}
pub fn receiving_loop_with_minimized_sender_overhead<T, E, const SLEEP_MS: u64>(
exit: Arc<AtomicBool>,
receiver: Receiver<T>,
mut on_recv: impl FnMut(T) -> Result<(), E>,
) -> Result<(), E> {
'outer: while !exit.load(Ordering::Relaxed) {
'inner: loop {
// avoid futex-based blocking here, otherwise a sender would have to
// wake me up at a syscall cost...
match receiver.try_recv() {
Ok(message) => on_recv(message)?,
Err(TryRecvError::Empty) => break 'inner,
Err(TryRecvError::Disconnected) => {
break 'outer;
}
};
if exit.load(Ordering::Relaxed) {
break 'outer;
}
}
sleep(Duration::from_millis(SLEEP_MS));
}
Ok(())
}
impl BankingTracer {
pub fn new(
maybe_config: Option<(&PathBuf, Arc<AtomicBool>, DirByteLimit)>,
) -> Result<(Arc<Self>, TracerThread), TraceError> {
match maybe_config {
None => Ok((Self::new_disabled(), None)),
Some((path, exit, dir_byte_limit)) => {
let rotate_threshold_size = dir_byte_limit / TRACE_FILE_ROTATE_COUNT;
if rotate_threshold_size == 0 {
return Err(TraceError::TooSmallDirByteLimit(
dir_byte_limit,
TRACE_FILE_ROTATE_COUNT,
));
}
let (trace_sender, trace_receiver) = unbounded();
let file_appender = Self::create_file_appender(path, rotate_threshold_size)?;
let tracer_thread =
Self::spawn_background_thread(trace_receiver, file_appender, exit.clone())?;
Ok((
Arc::new(Self {
active_tracer: Some(ActiveTracer { trace_sender, exit }),
}),
Some(tracer_thread),
))
}
}
}
pub fn new_disabled() -> Arc<Self> {
Arc::new(Self {
active_tracer: None,
})
}
pub fn is_enabled(&self) -> bool {
self.active_tracer.is_some()
}
fn create_channel(&self, label: ChannelLabel) -> (BankingPacketSender, BankingPacketReceiver) {
Self::channel(label, self.active_tracer.as_ref().cloned())
}
pub fn create_channel_non_vote(&self) -> (BankingPacketSender, BankingPacketReceiver) {
self.create_channel(ChannelLabel::NonVote)
}
pub fn create_channel_tpu_vote(&self) -> (BankingPacketSender, BankingPacketReceiver) {
self.create_channel(ChannelLabel::TpuVote)
}
pub fn create_channel_gossip_vote(&self) -> (BankingPacketSender, BankingPacketReceiver) {
self.create_channel(ChannelLabel::GossipVote)
}
pub fn hash_event(&self, slot: Slot, blockhash: &Hash, bank_hash: &Hash) {
self.trace_event(|| {
TimedTracedEvent(
SystemTime::now(),
TracedEvent::BlockAndBankHash(slot, *blockhash, *bank_hash),
)
})
}
fn trace_event(&self, on_trace: impl Fn() -> TimedTracedEvent) {
if let Some(ActiveTracer { trace_sender, exit }) = &self.active_tracer {
if !exit.load(Ordering::Relaxed) {
trace_sender
.send(on_trace())
.expect("active tracer thread unless exited");
}
}
}
pub fn channel_for_test() -> (TracedSender, Receiver<BankingPacketBatch>) {
Self::channel(ChannelLabel::Dummy, None)
}
fn channel(
label: ChannelLabel,
active_tracer: Option<ActiveTracer>,
) -> (TracedSender, Receiver<BankingPacketBatch>) {
let (sender, receiver) = unbounded();
(TracedSender::new(label, sender, active_tracer), receiver)
}
pub fn ensure_cleanup_path(path: &PathBuf) -> Result<(), io::Error> {
remove_dir_all(path).or_else(|err| {
if err.kind() == io::ErrorKind::NotFound {
Ok(())
} else {
Err(err)
}
})
}
fn create_file_appender(
path: &PathBuf,
rotate_threshold_size: u64,
) -> Result<RollingFileAppender<RollingConditionGrouped>, TraceError> {
create_dir_all(path)?;
let grouped = RollingConditionGrouped::new(
RollingConditionBasic::new()
.daily()
.max_size(rotate_threshold_size),
);
let appender = RollingFileAppender::new_with_buffer_capacity(
path.join(BASENAME),
grouped,
(TRACE_FILE_ROTATE_COUNT - 1).try_into()?,
BUF_WRITER_CAPACITY,
)?;
Ok(appender)
}
fn spawn_background_thread(
trace_receiver: Receiver<TimedTracedEvent>,
mut file_appender: RollingFileAppender<RollingConditionGrouped>,
exit: Arc<AtomicBool>,
) -> Result<JoinHandle<TracerThreadResult>, TraceError> {
let thread = thread::Builder::new().name("solBanknTracer".into()).spawn(
move || -> TracerThreadResult {
receiving_loop_with_minimized_sender_overhead::<_, _, TRACE_FILE_WRITE_INTERVAL_MS>(
exit,
trace_receiver,
|event| -> Result<(), TraceError> {
file_appender.condition_mut().reset();
serialize_into(&mut GroupedWriter::new(&mut file_appender), &event)?;
Ok(())
},
)?;
file_appender.flush()?;
Ok(())
},
)?;
Ok(thread)
}
}
pub struct TracedSender {
label: ChannelLabel,
sender: Sender<BankingPacketBatch>,
active_tracer: Option<ActiveTracer>,
}
impl TracedSender {
fn new(
label: ChannelLabel,
sender: Sender<BankingPacketBatch>,
active_tracer: Option<ActiveTracer>,
) -> Self {
Self {
label,
sender,
active_tracer,
}
}
pub fn send(&self, batch: BankingPacketBatch) -> Result<(), SendError<BankingPacketBatch>> {
if let Some(ActiveTracer { trace_sender, exit }) = &self.active_tracer {
if !exit.load(Ordering::Relaxed) {
trace_sender
.send(TimedTracedEvent(
SystemTime::now(),
TracedEvent::PacketBatch(self.label, BankingPacketBatch::clone(&batch)),
))
.map_err(|err| {
error!(
"unexpected error when tracing a banking event...: {:?}",
err
);
SendError(BankingPacketBatch::clone(&batch))
})?;
}
}
self.sender.send(batch)
}
}
pub mod for_test {
use super::*;
pub fn sample_packet_batch() -> BankingPacketBatch {
BankingPacketBatch::new((to_packet_batches(&vec![test_tx(); 4], 10), None))
}
pub fn drop_and_clean_temp_dir_unless_suppressed(temp_dir: TempDir) {
std::env::var("BANKING_TRACE_LEAVE_FILES").is_ok().then(|| {
warn!("prevented to remove {:?}", temp_dir.path());
drop(temp_dir.into_path());
});
}
pub fn terminate_tracer(
tracer: Arc<BankingTracer>,
tracer_thread: TracerThread,
main_thread: JoinHandle<TracerThreadResult>,
sender: TracedSender,
exit: Option<Arc<AtomicBool>>,
) {
if let Some(exit) = exit {
exit.store(true, Ordering::Relaxed);
}
drop((sender, tracer));
main_thread.join().unwrap().unwrap();
if let Some(tracer_thread) = tracer_thread {
tracer_thread.join().unwrap().unwrap();
}
}
}
#[cfg(test)]
mod tests {
use {
super::*,
bincode::ErrorKind::Io as BincodeIoError,
std::{
fs::File,
io::{BufReader, ErrorKind::UnexpectedEof},
str::FromStr,
},
};
#[test]
fn test_new_disabled() {
let exit = Arc::<AtomicBool>::default();
let tracer = BankingTracer::new_disabled();
let (non_vote_sender, non_vote_receiver) = tracer.create_channel_non_vote();
let dummy_main_thread = thread::spawn(move || {
receiving_loop_with_minimized_sender_overhead::<_, TraceError, 0>(
exit,
non_vote_receiver,
|_packet_batch| Ok(()),
)
});
non_vote_sender
.send(BankingPacketBatch::new((vec![], None)))
.unwrap();
for_test::terminate_tracer(tracer, None, dummy_main_thread, non_vote_sender, None);
}
#[test]
fn test_send_after_exited() {
let temp_dir = TempDir::new().unwrap();
let path = temp_dir.path().join("banking-trace");
let exit = Arc::<AtomicBool>::default();
let (tracer, tracer_thread) =
BankingTracer::new(Some((&path, exit.clone(), DirByteLimit::max_value()))).unwrap();
let (non_vote_sender, non_vote_receiver) = tracer.create_channel_non_vote();
let exit_for_dummy_thread = Arc::<AtomicBool>::default();
let exit_for_dummy_thread2 = exit_for_dummy_thread.clone();
let dummy_main_thread = thread::spawn(move || {
receiving_loop_with_minimized_sender_overhead::<_, TraceError, 0>(
exit_for_dummy_thread,
non_vote_receiver,
|_packet_batch| Ok(()),
)
});
// kill and join the tracer thread
exit.store(true, Ordering::Relaxed);
tracer_thread.unwrap().join().unwrap().unwrap();
// .hash_event() must succeed even after exit is already set to true
let blockhash = Hash::from_str("B1ockhash1111111111111111111111111111111111").unwrap();
let bank_hash = Hash::from_str("BankHash11111111111111111111111111111111111").unwrap();
tracer.hash_event(4, &blockhash, &bank_hash);
drop(tracer);
// .send() must succeed even after exit is already set to true and further tracer is
// already dropped
non_vote_sender
.send(for_test::sample_packet_batch())
.unwrap();
// finally terminate and join the main thread
exit_for_dummy_thread2.store(true, Ordering::Relaxed);
dummy_main_thread.join().unwrap().unwrap();
}
#[test]
fn test_record_and_restore() {
let temp_dir = TempDir::new().unwrap();
let path = temp_dir.path().join("banking-trace");
let exit = Arc::<AtomicBool>::default();
let (tracer, tracer_thread) =
BankingTracer::new(Some((&path, exit.clone(), DirByteLimit::max_value()))).unwrap();
let (non_vote_sender, non_vote_receiver) = tracer.create_channel_non_vote();
let dummy_main_thread = thread::spawn(move || {
receiving_loop_with_minimized_sender_overhead::<_, TraceError, 0>(
exit,
non_vote_receiver,
|_packet_batch| Ok(()),
)
});
non_vote_sender
.send(for_test::sample_packet_batch())
.unwrap();
let blockhash = Hash::from_str("B1ockhash1111111111111111111111111111111111").unwrap();
let bank_hash = Hash::from_str("BankHash11111111111111111111111111111111111").unwrap();
tracer.hash_event(4, &blockhash, &bank_hash);
for_test::terminate_tracer(
tracer,
tracer_thread,
dummy_main_thread,
non_vote_sender,
None,
);
let mut stream = BufReader::new(File::open(path.join(BASENAME)).unwrap());
let results = (0..=3)
.map(|_| bincode::deserialize_from::<_, TimedTracedEvent>(&mut stream))
.collect::<Vec<_>>();
let mut i = 0;
assert_matches!(
results[i],
Ok(TimedTracedEvent(
_,
TracedEvent::PacketBatch(ChannelLabel::NonVote, _)
))
);
i += 1;
assert_matches!(
results[i],
Ok(TimedTracedEvent(
_,
TracedEvent::BlockAndBankHash(4, actual_blockhash, actual_bank_hash)
)) if actual_blockhash == blockhash && actual_bank_hash == bank_hash
);
i += 1;
assert_matches!(
results[i],
Err(ref err) if matches!(
**err,
BincodeIoError(ref error) if error.kind() == UnexpectedEof
)
);
for_test::drop_and_clean_temp_dir_unless_suppressed(temp_dir);
}
#[test]
fn test_spill_over_at_rotation() {
let temp_dir = TempDir::new().unwrap();
let path = temp_dir.path().join("banking-trace");
const REALLY_SMALL_ROTATION_THRESHOLD: u64 = 1;
let mut file_appender =
BankingTracer::create_file_appender(&path, REALLY_SMALL_ROTATION_THRESHOLD).unwrap();
file_appender.write_all(b"foo").unwrap();
file_appender.condition_mut().reset();
file_appender.write_all(b"bar").unwrap();
file_appender.condition_mut().reset();
file_appender.flush().unwrap();
assert_eq!(
[
std::fs::read_to_string(path.join("events")).ok(),
std::fs::read_to_string(path.join("events.1")).ok(),
std::fs::read_to_string(path.join("events.2")).ok(),
],
[Some("bar".into()), Some("foo".into()), None]
);
for_test::drop_and_clean_temp_dir_unless_suppressed(temp_dir);
}
#[test]
fn test_reopen_with_blank_file() {
let temp_dir = TempDir::new().unwrap();
let path = temp_dir.path().join("banking-trace");
let mut file_appender =
BankingTracer::create_file_appender(&path, TRACE_FILE_DEFAULT_ROTATE_BYTE_THRESHOLD)
.unwrap();
// assume this is unclean write
file_appender.write_all(b"f").unwrap();
file_appender.flush().unwrap();
// reopen while shadow-dropping the old tracer
let mut file_appender =
BankingTracer::create_file_appender(&path, TRACE_FILE_DEFAULT_ROTATE_BYTE_THRESHOLD)
.unwrap();
// new file won't be created as appender is lazy
assert_eq!(
[
std::fs::read_to_string(path.join("events")).ok(),
std::fs::read_to_string(path.join("events.1")).ok(),
std::fs::read_to_string(path.join("events.2")).ok(),
],
[Some("f".into()), None, None]
);
// initial write actually creates the new blank file
file_appender.write_all(b"bar").unwrap();
assert_eq!(
[
std::fs::read_to_string(path.join("events")).ok(),
std::fs::read_to_string(path.join("events.1")).ok(),
std::fs::read_to_string(path.join("events.2")).ok(),
],
[Some("".into()), Some("f".into()), None]
);
// flush actually write the actual data
file_appender.flush().unwrap();
assert_eq!(
[
std::fs::read_to_string(path.join("events")).ok(),
std::fs::read_to_string(path.join("events.1")).ok(),
std::fs::read_to_string(path.join("events.2")).ok(),
],
[Some("bar".into()), Some("f".into()), None]
);
for_test::drop_and_clean_temp_dir_unless_suppressed(temp_dir);
}
}