Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 5 additions & 73 deletions crates/dbsp/src/circuit/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::operator::communication::Exchange;
use crate::storage::backend::StorageBackend;
use crate::storage::file::format::Compression;
use crate::storage::file::writer::Parameters;
use crate::trace::aligned_deserialize;
use crate::utils::process_rss_bytes;
use crate::{
DetailedError,
Expand All @@ -34,6 +33,7 @@ use feldera_types::memory_pressure::{
use indexmap::IndexSet;
use once_cell::sync::Lazy;
use serde::Serialize;
use std::convert::identity;
use std::iter::repeat;
use std::ops::Range;
use std::path::Path;
Expand Down Expand Up @@ -1282,48 +1282,11 @@ impl Runtime {

/// A synchronization primitive that allows multiple threads within a runtime to agree
/// when a condition is satisfied.
pub(crate) enum Consensus {
SingleThreaded,
MultiThreaded {
notify_sender: Arc<Notify>,
notify_receiver: Arc<Notify>,
exchange: Arc<Exchange<bool>>,
},
}
pub(crate) struct Consensus(Broadcast<bool>);

impl Consensus {
pub fn new() -> Self {
match Runtime::runtime() {
Some(runtime) if Runtime::num_workers() > 1 => {
let worker_index = Runtime::worker_index();
let exchange_id = runtime.sequence_next().try_into().unwrap();
let exchange = Exchange::with_runtime(
&runtime,
exchange_id,
Box::new(|data| aligned_deserialize(&data[..])),
);

let notify_sender = Arc::new(Notify::new());
let notify_sender_clone = notify_sender.clone();
let notify_receiver = Arc::new(Notify::new());
let notify_receiver_clone = notify_receiver.clone();

exchange.register_sender_callback(worker_index, move || {
notify_sender_clone.notify_one()
});

exchange.register_receiver_callback(worker_index, move || {
notify_receiver_clone.notify_one()
});

Self::MultiThreaded {
notify_sender,
notify_receiver,
exchange,
}
}
_ => Self::SingleThreaded,
}
Self(Broadcast::new())
}

/// Returns `true` if all workers vote `true`.
Expand All @@ -1332,37 +1295,7 @@ impl Consensus {
///
/// * `local` - Local vote by the current worker.
pub async fn check(&self, local: bool) -> Result<bool, SchedulerError> {
match self {
Self::SingleThreaded => Ok(local),
Self::MultiThreaded {
notify_sender,
notify_receiver,
exchange,
} => {
while !exchange.try_send_all_with_serializer(
Runtime::worker_index(),
repeat(local),
|local| vec![local as u8],
) {
if Runtime::kill_in_progress() {
return Err(SchedulerError::Killed);
}
notify_sender.notified().await;
}
// Receive the status of each peer, compute global result
// as a logical and of all peer statuses.
let mut global = true;
while !exchange.try_receive_all(Runtime::worker_index(), |status| global &= status)
{
if Runtime::kill_in_progress() {
return Err(SchedulerError::Killed);
}
// Sleep if other threads are still working.
notify_receiver.notified().await;
}
Ok(global)
}
}
Ok(self.0.collect(local).await?.into_iter().all(identity))
}
}

Expand Down Expand Up @@ -1439,8 +1372,7 @@ where
}
notify_sender.notified().await;
}
// Receive the status of each peer, compute global result
// as a logical and of all peer statuses.
// Receive and collect the status of each peer.
let mut result = Vec::with_capacity(Runtime::num_workers());
while !exchange
.try_receive_all(Runtime::worker_index(), |status| result.push(status))
Expand Down
Loading