sof 0.18.2

Solana Observer Framework for low-latency shred ingestion and plugin-driven transaction observation
Documentation
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
#[cfg(feature = "gossip-bootstrap")]
use super::*;
#[cfg(feature = "gossip-bootstrap")]
use crate::repair::{GossipRepairClient, RepairPeerSnapshot, ServedRepairRequest};
#[cfg(feature = "gossip-bootstrap")]
use tokio::sync::oneshot;

#[cfg(feature = "gossip-bootstrap")]
#[derive(Debug)]
pub(crate) enum RepairCommand {
    Request {
        request: MissingShredRequest,
    },
    NoteShredSources {
        sources: Vec<(SocketAddr, u16)>,
    },
    HandleResponsePing {
        packet: Arc<[u8]>,
        from_addr: SocketAddr,
    },
    HandleServeRequest {
        packet: Arc<[u8]>,
        from_addr: SocketAddr,
    },
}

#[cfg(feature = "gossip-bootstrap")]
#[derive(Debug)]
pub(crate) enum RepairOutcome {
    RequestSent {
        peer_addr: SocketAddr,
    },
    RequestNoPeer {
        request: MissingShredRequest,
    },
    RequestError {
        request: MissingShredRequest,
        error: String,
    },
    ResponsePingHandledFrom {
        source: SocketAddr,
    },
    ResponsePingError {
        source: SocketAddr,
        error: String,
    },
    ServeRequestHandled {
        source: SocketAddr,
        request: ServedRepairRequest,
    },
    ServeRequestError {
        source: SocketAddr,
        error: String,
    },
}

#[cfg(feature = "gossip-bootstrap")]
#[derive(Debug, thiserror::Error)]
pub(crate) enum RepairDriverStartError {
    #[error("failed to spawn dedicated repair thread: {source}")]
    SpawnThread { source: std::io::Error },
}

#[cfg(feature = "gossip-bootstrap")]
type RepairDriverParts = (
    mpsc::Sender<RepairCommand>,
    mpsc::Receiver<RepairOutcome>,
    ArcShift<RepairPeerSnapshot>,
    RepairDriverHandle,
);

#[cfg(feature = "gossip-bootstrap")]
#[derive(Debug)]
pub(crate) struct RepairDriverHandle {
    shutdown_tx: Option<oneshot::Sender<()>>,
    join_handle: Option<std::thread::JoinHandle<()>>,
}

#[cfg(feature = "gossip-bootstrap")]
impl RepairDriverHandle {
    #[expect(
        clippy::missing_const_for_fn,
        reason = "contains non-const std thread handle storage"
    )]
    fn new(shutdown_tx: oneshot::Sender<()>, join_handle: std::thread::JoinHandle<()>) -> Self {
        Self {
            shutdown_tx: Some(shutdown_tx),
            join_handle: Some(join_handle),
        }
    }

    fn abort(mut self) {
        if let Some(shutdown_tx) = self.shutdown_tx.take()
            && shutdown_tx.send(()).is_err()
        {
            // Driver thread already exited.
        }
        drop(self.join_handle.take());
    }

    async fn shutdown(mut self) {
        if let Some(shutdown_tx) = self.shutdown_tx.take()
            && shutdown_tx.send(()).is_err()
        {
            // Driver thread already exited.
        }
        if let Some(join_handle) = self.join_handle.take() {
            drop(
                tokio::task::spawn_blocking(move || {
                    drop(join_handle.join());
                })
                .await,
            );
        }
    }
}

#[cfg(feature = "gossip-bootstrap")]
#[derive(Debug)]
pub(crate) struct RepairSourceHintBuffer {
    counts: HashMap<SocketAddr, u16>,
    capacity: usize,
}

#[cfg(feature = "gossip-bootstrap")]
impl RepairSourceHintBuffer {
    pub(crate) fn new(capacity: usize) -> Self {
        let capacity = capacity.max(1);
        Self {
            counts: HashMap::with_capacity(capacity),
            capacity,
        }
    }

    #[cfg(test)]
    pub(crate) fn new_baseline(capacity: usize) -> Self {
        Self {
            counts: HashMap::new(),
            capacity: capacity.max(1),
        }
    }

    pub(crate) fn len(&self) -> usize {
        self.counts.len()
    }

    pub(crate) fn record(&mut self, source_addr: SocketAddr) -> Result<(), ()> {
        if let Some(entry) = self.counts.get_mut(&source_addr) {
            *entry = entry.saturating_add(1);
            return Ok(());
        }
        if self.counts.len() >= self.capacity {
            return Err(());
        }
        let _ = self.counts.insert(source_addr, 1);
        Ok(())
    }

    fn drain_batch(&mut self, max_batch_size: usize) -> Vec<(SocketAddr, u16)> {
        if self.counts.is_empty() {
            return Vec::new();
        }
        let mut entries: Vec<_> = self.counts.drain().collect();
        entries.sort_unstable_by(|(_, left_hits), (_, right_hits)| right_hits.cmp(left_hits));
        let max_batch_size = max_batch_size.max(1);
        if entries.len() <= max_batch_size {
            return entries;
        }
        let remaining = entries.split_off(max_batch_size);
        for (addr, hits) in remaining {
            let _ = self.counts.insert(addr, hits);
        }
        entries
    }
}

#[cfg(feature = "gossip-bootstrap")]
pub(crate) fn flush_repair_source_hints(
    source_hints: &mut RepairSourceHintBuffer,
    command_tx: Option<&mpsc::Sender<RepairCommand>>,
    batch_size: usize,
    source_hint_drops: &mut u64,
    source_hint_enqueued: &mut u64,
) {
    if source_hints.len() == 0 {
        return;
    }
    let batch = source_hints.drain_batch(batch_size);
    if batch.is_empty() {
        return;
    }
    if let Some(command_tx) = command_tx {
        let batch_len = u64::try_from(batch.len()).unwrap_or(u64::MAX);
        match command_tx.try_send(RepairCommand::NoteShredSources { sources: batch }) {
            Ok(()) => {
                *source_hint_enqueued = source_hint_enqueued.saturating_add(batch_len);
            }
            Err(_) => {
                *source_hint_drops = source_hint_drops.saturating_add(batch_len);
            }
        }
    } else {
        let batch_len = u64::try_from(batch.len()).unwrap_or(u64::MAX);
        *source_hint_drops = source_hint_drops.saturating_add(batch_len);
    }
}

#[cfg(feature = "gossip-bootstrap")]
pub(crate) fn spawn_repair_driver(
    mut repair_client: GossipRepairClient,
    relay_cache: Option<SharedRelayCache>,
) -> Result<RepairDriverParts, RepairDriverStartError> {
    let (command_tx, mut command_rx) =
        mpsc::channel::<RepairCommand>(read_repair_command_queue_capacity());
    let (result_tx, result_rx) =
        mpsc::channel::<RepairOutcome>(read_repair_result_queue_capacity());
    let peer_snapshot = repair_client.peer_snapshot_handle();
    let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();
    let driver_thread = std::thread::Builder::new()
        .name(String::from("sof-repair-driver"))
        .spawn(move || {
            let runtime = match tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
            {
                Ok(runtime) => runtime,
                Err(source) => {
                    tracing::error!(
                        ?source,
                        "failed to build dedicated repair runtime"
                    );
                    return;
                }
            };
            runtime.block_on(async move {
                let mut slot_hint = 0_u64;
                let mut refresh_tick = interval(Duration::from_millis(read_repair_peer_refresh_ms()));
                refresh_tick.tick().await;
                let _ = repair_client.refresh_peer_snapshot(slot_hint);
                loop {
                    tokio::select! {
                        _ = &mut shutdown_rx => {
                            break;
                        }
                        maybe_command = command_rx.recv() => {
                            let Some(command) = maybe_command else {
                                break;
                            };
                            match command {
                                RepairCommand::Request { request } => {
                                    slot_hint = slot_hint.max(request.slot);
                                    let outcome = match repair_client
                                        .request_missing_shred(request.slot, request.index, request.kind)
                                        .await
                                    {
                                        Ok(Some(peer_addr)) => RepairOutcome::RequestSent { peer_addr },
                                        Ok(None) => RepairOutcome::RequestNoPeer { request },
                                        Err(error) => RepairOutcome::RequestError {
                                            request,
                                            error: error.to_string(),
                                        },
                                    };
                                    if result_tx.try_send(outcome).is_err() {
                                        // Receiver dropped; drop outcome.
                                    }
                                }
                                RepairCommand::NoteShredSources { sources } => {
                                    let _ = repair_client.note_shred_sources(&sources);
                                }
                                RepairCommand::HandleResponsePing { packet, from_addr } => {
                                    match repair_client
                                        .maybe_handle_response_ping(packet.as_ref(), from_addr)
                                        .await
                                    {
                                        Ok(true) => {
                                            if result_tx
                                                .try_send(RepairOutcome::ResponsePingHandledFrom {
                                                    source: from_addr,
                                                })
                                                .is_err()
                                            {
                                                // Receiver dropped; drop outcome.
                                            }
                                        }
                                        Ok(false) => {}
                                        Err(error) => {
                                            if result_tx
                                                .try_send(RepairOutcome::ResponsePingError {
                                                    source: from_addr,
                                                    error: error.to_string(),
                                                })
                                                .is_err()
                                            {
                                                // Receiver dropped; drop outcome.
                                            }
                                        }
                                    }
                                }
                                RepairCommand::HandleServeRequest { packet, from_addr } => {
                                    match repair_client
                                        .maybe_serve_repair_request(
                                            packet.as_ref(),
                                            from_addr,
                                            relay_cache.as_ref(),
                                        )
                                        .await
                                    {
                                        Ok(Some(request)) => {
                                            if result_tx
                                                .try_send(RepairOutcome::ServeRequestHandled {
                                                    source: from_addr,
                                                    request,
                                                })
                                                .is_err()
                                            {
                                                // Receiver dropped; drop outcome.
                                            }
                                        }
                                        Ok(None) => {}
                                        Err(error) => {
                                            if result_tx
                                                .try_send(RepairOutcome::ServeRequestError {
                                                    source: from_addr,
                                                    error: error.to_string(),
                                                })
                                                .is_err()
                                            {
                                                // Receiver dropped; drop outcome.
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        _ = refresh_tick.tick() => {
                            let _ = repair_client.refresh_peer_snapshot(slot_hint);
                        }
                    }
                }
            });
        })
        .map_err(|source| RepairDriverStartError::SpawnThread { source })?;
    let driver_handle = RepairDriverHandle::new(shutdown_tx, driver_thread);
    Ok((command_tx, result_rx, peer_snapshot, driver_handle))
}

#[cfg(all(test, feature = "gossip-bootstrap"))]
mod tests {
    use std::{
        env,
        net::{IpAddr, Ipv4Addr, SocketAddr},
        time::Instant,
    };

    use super::RepairSourceHintBuffer;

    #[test]
    #[ignore = "profiling fixture for repair source hint buffer allocation"]
    fn repair_source_hint_buffer_profile_fixture() {
        let iterations = env::var("SOF_REPAIR_SOURCE_HINT_PROFILE_ITERS")
            .ok()
            .and_then(|raw| raw.parse::<usize>().ok())
            .filter(|value| *value > 0)
            .unwrap_or(20_000);
        let capacity = env::var("SOF_REPAIR_SOURCE_HINT_PROFILE_CAPACITY")
            .ok()
            .and_then(|raw| raw.parse::<usize>().ok())
            .filter(|value| *value > 0)
            .unwrap_or(256);
        let batch_size = env::var("SOF_REPAIR_SOURCE_HINT_PROFILE_BATCH")
            .ok()
            .and_then(|raw| raw.parse::<usize>().ok())
            .filter(|value| *value > 0)
            .unwrap_or(capacity / 2);
        let addresses = (0..capacity)
            .map(|index| {
                SocketAddr::new(
                    IpAddr::V4(Ipv4Addr::new(
                        127,
                        0,
                        u8::try_from((index / 255) % 255).unwrap_or(0),
                        u8::try_from((index % 255) + 1).unwrap_or(u8::MAX),
                    )),
                    u16::try_from((10_000 + index) % usize::from(u16::MAX)).unwrap_or(u16::MAX),
                )
            })
            .collect::<Vec<_>>();
        assert!(!addresses.is_empty());

        let baseline_started_at = Instant::now();
        for _ in 0..iterations {
            let mut buffer = RepairSourceHintBuffer::new_baseline(capacity);
            for addr in addresses.iter().copied() {
                assert!(buffer.record(addr).is_ok());
            }
            let drained = buffer.drain_batch(batch_size);
            assert!(!drained.is_empty());
        }
        let baseline_elapsed = baseline_started_at.elapsed();

        let optimized_started_at = Instant::now();
        for _ in 0..iterations {
            let mut buffer = RepairSourceHintBuffer::new(capacity);
            for addr in addresses.iter().copied() {
                assert!(buffer.record(addr).is_ok());
            }
            let drained = buffer.drain_batch(batch_size);
            assert!(!drained.is_empty());
        }
        let optimized_elapsed = optimized_started_at.elapsed();

        let baseline_avg_ns =
            baseline_elapsed.as_nanos() / u128::try_from(iterations).unwrap_or(u128::MAX);
        let optimized_avg_ns =
            optimized_elapsed.as_nanos() / u128::try_from(iterations).unwrap_or(u128::MAX);
        let baseline_avg_us = baseline_avg_ns as f64 / 1_000.0;
        let optimized_avg_us = optimized_avg_ns as f64 / 1_000.0;
        println!(
            "repair_source_hint_buffer_profile_fixture iterations={} baseline_us={} optimized_us={} baseline_avg_ns_per_iteration={} optimized_avg_ns_per_iteration={} baseline_avg_us_per_iteration={:.6} optimized_avg_us_per_iteration={:.6}",
            iterations,
            baseline_elapsed.as_micros(),
            optimized_elapsed.as_micros(),
            baseline_avg_ns,
            optimized_avg_ns,
            baseline_avg_us,
            optimized_avg_us
        );
    }
}

#[cfg(feature = "gossip-bootstrap")]
pub(crate) fn replace_repair_driver(
    repair_client: crate::repair::GossipRepairClient,
    relay_cache: Option<SharedRelayCache>,
    repair_command_tx: &mut Option<mpsc::Sender<RepairCommand>>,
    repair_result_rx: &mut Option<mpsc::Receiver<RepairOutcome>>,
    repair_peer_snapshot: &mut Option<ArcShift<crate::repair::RepairPeerSnapshot>>,
    repair_driver_handle: &mut Option<RepairDriverHandle>,
) {
    *repair_command_tx = None;
    *repair_result_rx = None;
    *repair_peer_snapshot = None;
    if let Some(handle) = repair_driver_handle.take() {
        handle.abort();
    }
    match spawn_repair_driver(repair_client, relay_cache) {
        Ok((command_tx, result_rx, peer_snapshot, driver_handle)) => {
            *repair_command_tx = Some(command_tx);
            *repair_result_rx = Some(result_rx);
            *repair_peer_snapshot = Some(peer_snapshot);
            *repair_driver_handle = Some(driver_handle);
        }
        Err(error) => {
            tracing::error!(?error, "failed to replace repair driver");
        }
    }
}

#[cfg(feature = "gossip-bootstrap")]
pub(crate) async fn stop_repair_driver(
    repair_command_tx: &mut Option<mpsc::Sender<RepairCommand>>,
    repair_result_rx: &mut Option<mpsc::Receiver<RepairOutcome>>,
    repair_peer_snapshot: &mut Option<ArcShift<crate::repair::RepairPeerSnapshot>>,
    repair_driver_handle: &mut Option<RepairDriverHandle>,
) {
    *repair_command_tx = None;
    *repair_result_rx = None;
    *repair_peer_snapshot = None;
    if let Some(handle) = repair_driver_handle.take() {
        handle.shutdown().await;
    }
}