2018-08-09 14:41:21 -06:00
|
|
|
//! The `broadcast_stage` broadcasts data from a leader node to validators
|
2018-08-09 12:54:23 -06:00
|
|
|
//!
|
|
|
|
use counter::Counter;
|
2018-09-12 13:59:19 -07:00
|
|
|
use crdt::{Crdt, CrdtError, NodeInfo, LEADER_ROTATION_INTERVAL};
|
2018-09-18 13:49:10 -07:00
|
|
|
use entry::Entry;
|
2018-08-09 12:54:23 -06:00
|
|
|
#[cfg(feature = "erasure")]
|
|
|
|
use erasure;
|
2018-09-18 13:49:10 -07:00
|
|
|
use ledger::Block;
|
2018-08-09 12:54:23 -06:00
|
|
|
use log::Level;
|
2018-09-18 21:45:49 -07:00
|
|
|
use packet::{BlobRecycler, SharedBlobs};
|
|
|
|
use rayon::prelude::*;
|
2018-08-09 12:54:23 -06:00
|
|
|
use result::{Error, Result};
|
2018-08-09 15:17:50 -06:00
|
|
|
use service::Service;
|
2018-08-09 12:54:23 -06:00
|
|
|
use std::net::UdpSocket;
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
2018-09-18 13:49:10 -07:00
|
|
|
use std::sync::mpsc::{Receiver, RecvTimeoutError};
|
2018-08-09 12:54:23 -06:00
|
|
|
use std::sync::{Arc, RwLock};
|
2018-08-09 15:17:50 -06:00
|
|
|
use std::thread::{self, Builder, JoinHandle};
|
2018-09-18 21:45:49 -07:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use timing::duration_as_ms;
|
2018-09-07 13:38:48 -06:00
|
|
|
use window::{self, SharedWindow, WindowIndex, WindowUtil, WINDOW_SIZE};
|
2018-08-09 12:54:23 -06:00
|
|
|
|
|
|
|
fn broadcast(
|
|
|
|
node_info: &NodeInfo,
|
|
|
|
broadcast_table: &[NodeInfo],
|
|
|
|
window: &SharedWindow,
|
|
|
|
recycler: &BlobRecycler,
|
2018-09-18 13:49:10 -07:00
|
|
|
receiver: &Receiver<Vec<Entry>>,
|
2018-08-09 12:54:23 -06:00
|
|
|
sock: &UdpSocket,
|
|
|
|
transmit_index: &mut WindowIndex,
|
|
|
|
receive_index: &mut u64,
|
|
|
|
) -> Result<()> {
|
2018-09-05 22:36:59 -06:00
|
|
|
let id = node_info.id;
|
2018-08-09 12:54:23 -06:00
|
|
|
let timer = Duration::new(1, 0);
|
2018-09-18 13:49:10 -07:00
|
|
|
let entries = receiver.recv_timeout(timer)?;
|
2018-09-18 21:45:49 -07:00
|
|
|
let mut num_entries = entries.len();
|
|
|
|
let mut ventries = Vec::new();
|
|
|
|
ventries.push(entries);
|
2018-09-18 13:49:10 -07:00
|
|
|
while let Ok(entries) = receiver.try_recv() {
|
2018-09-18 21:45:49 -07:00
|
|
|
num_entries += entries.len();
|
|
|
|
ventries.push(entries);
|
2018-08-09 12:54:23 -06:00
|
|
|
}
|
|
|
|
|
2018-09-18 21:45:49 -07:00
|
|
|
let to_blobs_start = Instant::now();
|
|
|
|
let dq: SharedBlobs = ventries
|
|
|
|
.into_par_iter()
|
|
|
|
.flat_map(|p| p.to_blobs(recycler))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let to_blobs_elapsed = duration_as_ms(&to_blobs_start.elapsed());
|
|
|
|
|
2018-08-09 12:54:23 -06:00
|
|
|
// flatten deque to vec
|
|
|
|
let blobs_vec: Vec<_> = dq.into_iter().collect();
|
|
|
|
|
2018-09-18 21:45:49 -07:00
|
|
|
let blobs_chunking = Instant::now();
|
2018-08-09 12:54:23 -06:00
|
|
|
// We could receive more blobs than window slots so
|
|
|
|
// break them up into window-sized chunks to process
|
|
|
|
let blobs_chunked = blobs_vec.chunks(WINDOW_SIZE as usize).map(|x| x.to_vec());
|
2018-09-18 21:45:49 -07:00
|
|
|
let chunking_elapsed = duration_as_ms(&blobs_chunking.elapsed());
|
2018-08-09 12:54:23 -06:00
|
|
|
|
2018-09-07 13:38:48 -06:00
|
|
|
trace!("{}", window.read().unwrap().print(&id, *receive_index));
|
2018-08-09 12:54:23 -06:00
|
|
|
|
2018-09-18 21:45:49 -07:00
|
|
|
let broadcast_start = Instant::now();
|
2018-08-09 12:54:23 -06:00
|
|
|
for mut blobs in blobs_chunked {
|
|
|
|
let blobs_len = blobs.len();
|
2018-09-05 22:36:59 -06:00
|
|
|
trace!("{}: broadcast blobs.len: {}", id, blobs_len);
|
2018-08-09 12:54:23 -06:00
|
|
|
|
|
|
|
// Index the blobs
|
|
|
|
window::index_blobs(node_info, &blobs, receive_index)
|
|
|
|
.expect("index blobs for initial window");
|
|
|
|
|
|
|
|
// keep the cache of blobs that are broadcast
|
|
|
|
inc_new_counter_info!("streamer-broadcast-sent", blobs.len());
|
|
|
|
{
|
|
|
|
let mut win = window.write().unwrap();
|
|
|
|
assert!(blobs.len() <= win.len());
|
|
|
|
for b in &blobs {
|
|
|
|
let ix = b.read().unwrap().get_index().expect("blob index");
|
|
|
|
let pos = (ix % WINDOW_SIZE) as usize;
|
2018-09-14 13:13:36 -07:00
|
|
|
if let Some(x) = win[pos].data.take() {
|
2018-08-09 12:54:23 -06:00
|
|
|
trace!(
|
2018-09-05 22:36:59 -06:00
|
|
|
"{} popped {} at {}",
|
|
|
|
id,
|
2018-08-09 12:54:23 -06:00
|
|
|
x.read().unwrap().get_index().unwrap(),
|
|
|
|
pos
|
|
|
|
);
|
2018-09-05 05:07:02 +09:00
|
|
|
recycler.recycle(x, "broadcast-data");
|
2018-08-09 12:54:23 -06:00
|
|
|
}
|
2018-09-14 13:13:36 -07:00
|
|
|
if let Some(x) = win[pos].coding.take() {
|
2018-08-09 12:54:23 -06:00
|
|
|
trace!(
|
2018-09-05 22:36:59 -06:00
|
|
|
"{} popped {} at {}",
|
|
|
|
id,
|
2018-08-09 12:54:23 -06:00
|
|
|
x.read().unwrap().get_index().unwrap(),
|
|
|
|
pos
|
|
|
|
);
|
2018-09-05 05:07:02 +09:00
|
|
|
recycler.recycle(x, "broadcast-coding");
|
2018-08-09 12:54:23 -06:00
|
|
|
}
|
|
|
|
|
2018-09-05 22:36:59 -06:00
|
|
|
trace!("{} null {}", id, pos);
|
2018-08-09 12:54:23 -06:00
|
|
|
}
|
|
|
|
while let Some(b) = blobs.pop() {
|
|
|
|
let ix = b.read().unwrap().get_index().expect("blob index");
|
|
|
|
let pos = (ix % WINDOW_SIZE) as usize;
|
2018-09-05 22:36:59 -06:00
|
|
|
trace!("{} caching {} at {}", id, ix, pos);
|
2018-08-09 12:54:23 -06:00
|
|
|
assert!(win[pos].data.is_none());
|
|
|
|
win[pos].data = Some(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the coding blob data from the window data blobs
|
|
|
|
#[cfg(feature = "erasure")]
|
|
|
|
{
|
|
|
|
erasure::generate_coding(
|
2018-09-05 22:36:59 -06:00
|
|
|
&id,
|
2018-08-09 12:54:23 -06:00
|
|
|
&mut window.write().unwrap(),
|
|
|
|
recycler,
|
|
|
|
*receive_index,
|
|
|
|
blobs_len,
|
|
|
|
&mut transmit_index.coding,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
*receive_index += blobs_len as u64;
|
|
|
|
|
|
|
|
// Send blobs out from the window
|
|
|
|
Crdt::broadcast(
|
|
|
|
&node_info,
|
|
|
|
&broadcast_table,
|
|
|
|
&window,
|
|
|
|
&sock,
|
|
|
|
transmit_index,
|
|
|
|
*receive_index,
|
|
|
|
)?;
|
|
|
|
}
|
2018-09-18 21:45:49 -07:00
|
|
|
let broadcast_elapsed = duration_as_ms(&broadcast_start.elapsed());
|
|
|
|
|
|
|
|
info!(
|
|
|
|
"broadcast: {} entries, blob time {} chunking time {} broadcast time {}",
|
|
|
|
num_entries, to_blobs_elapsed, chunking_elapsed, broadcast_elapsed
|
|
|
|
);
|
|
|
|
|
2018-08-09 12:54:23 -06:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:17:50 -06:00
|
|
|
pub struct BroadcastStage {
|
|
|
|
thread_hdl: JoinHandle<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BroadcastStage {
|
|
|
|
fn run(
|
2018-08-09 16:20:13 -06:00
|
|
|
sock: &UdpSocket,
|
|
|
|
crdt: &Arc<RwLock<Crdt>>,
|
|
|
|
window: &SharedWindow,
|
2018-08-09 15:17:50 -06:00
|
|
|
entry_height: u64,
|
2018-08-09 16:20:13 -06:00
|
|
|
recycler: &BlobRecycler,
|
2018-09-18 13:49:10 -07:00
|
|
|
receiver: &Receiver<Vec<Entry>>,
|
2018-08-09 15:17:50 -06:00
|
|
|
) {
|
|
|
|
let mut transmit_index = WindowIndex {
|
|
|
|
data: entry_height,
|
|
|
|
coding: entry_height,
|
|
|
|
};
|
|
|
|
let mut receive_index = entry_height;
|
|
|
|
let me = crdt.read().unwrap().my_data().clone();
|
|
|
|
loop {
|
2018-09-12 13:59:19 -07:00
|
|
|
if transmit_index.data % (LEADER_ROTATION_INTERVAL as u64) == 0 {
|
|
|
|
let rcrdt = crdt.read().unwrap();
|
|
|
|
let my_id = rcrdt.my_data().id;
|
|
|
|
match rcrdt.get_scheduled_leader(transmit_index.data) {
|
|
|
|
Some(id) if id == my_id => (),
|
|
|
|
// If the leader stays in power for the next
|
|
|
|
// round as well, then we don't exit. Otherwise, exit.
|
|
|
|
_ => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:17:50 -06:00
|
|
|
let broadcast_table = crdt.read().unwrap().compute_broadcast_table();
|
|
|
|
if let Err(e) = broadcast(
|
|
|
|
&me,
|
|
|
|
&broadcast_table,
|
|
|
|
&window,
|
|
|
|
&recycler,
|
|
|
|
&receiver,
|
|
|
|
&sock,
|
|
|
|
&mut transmit_index,
|
|
|
|
&mut receive_index,
|
|
|
|
) {
|
|
|
|
match e {
|
|
|
|
Error::RecvTimeoutError(RecvTimeoutError::Disconnected) => break,
|
|
|
|
Error::RecvTimeoutError(RecvTimeoutError::Timeout) => (),
|
|
|
|
Error::CrdtError(CrdtError::NoPeers) => (), // TODO: Why are the unit-tests throwing hundreds of these?
|
|
|
|
_ => {
|
|
|
|
inc_new_counter_info!("streamer-broadcaster-error", 1, 1);
|
|
|
|
error!("broadcaster error: {:?}", e);
|
2018-08-09 12:54:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-09 15:17:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Service to broadcast messages from the leader to layer 1 nodes.
|
|
|
|
/// See `crdt` for network layer definitions.
|
|
|
|
/// # Arguments
|
|
|
|
/// * `sock` - Socket to send from.
|
|
|
|
/// * `exit` - Boolean to signal system exit.
|
|
|
|
/// * `crdt` - CRDT structure
|
|
|
|
/// * `window` - Cache of blobs that we have broadcast
|
|
|
|
/// * `recycler` - Blob recycler.
|
|
|
|
/// * `receiver` - Receive channel for blobs to be retransmitted to all the layer 1 nodes.
|
|
|
|
pub fn new(
|
|
|
|
sock: UdpSocket,
|
|
|
|
crdt: Arc<RwLock<Crdt>>,
|
|
|
|
window: SharedWindow,
|
|
|
|
entry_height: u64,
|
|
|
|
recycler: BlobRecycler,
|
2018-09-18 13:49:10 -07:00
|
|
|
receiver: Receiver<Vec<Entry>>,
|
2018-08-09 15:17:50 -06:00
|
|
|
) -> Self {
|
|
|
|
let thread_hdl = Builder::new()
|
|
|
|
.name("solana-broadcaster".to_string())
|
|
|
|
.spawn(move || {
|
2018-08-09 16:20:13 -06:00
|
|
|
Self::run(&sock, &crdt, &window, entry_height, &recycler, &receiver);
|
2018-09-12 13:59:19 -07:00
|
|
|
})
|
|
|
|
.unwrap();
|
2018-08-09 15:17:50 -06:00
|
|
|
|
|
|
|
BroadcastStage { thread_hdl }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Service for BroadcastStage {
|
2018-09-13 14:00:17 -07:00
|
|
|
type JoinReturnType = ();
|
2018-08-09 15:17:50 -06:00
|
|
|
|
|
|
|
fn join(self) -> thread::Result<()> {
|
2018-09-12 15:05:22 -07:00
|
|
|
self.thread_hdl.join()?;
|
|
|
|
Ok(())
|
2018-08-09 15:17:50 -06:00
|
|
|
}
|
2018-08-09 12:54:23 -06:00
|
|
|
}
|