Skip to content

Commit

Permalink
Using broadcast channels instead of notify
Browse files Browse the repository at this point in the history
  • Loading branch information
godmodegalactus committed Mar 28, 2024
1 parent df095e1 commit 303576d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
31 changes: 14 additions & 17 deletions cluster-endpoints/src/grpc_multiplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ use solana_lite_rpc_core::AnyhowJoinHandle;
use solana_sdk::clock::Slot;
use solana_sdk::commitment_config::CommitmentConfig;
use std::collections::{BTreeSet, HashMap, HashSet};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast::Receiver;
use tokio::sync::Notify;
use tokio::sync::broadcast::{self, Receiver};
use yellowstone_grpc_proto::geyser::subscribe_update::UpdateOneof;
use yellowstone_grpc_proto::geyser::SubscribeUpdate;

Expand Down Expand Up @@ -56,8 +53,7 @@ impl FromYellowstoneExtractor for BlockMetaHashExtractor {
fn create_grpc_multiplex_processed_block_stream(
grpc_sources: &Vec<GrpcSourceConfig>,
processed_block_sender: async_channel::Sender<ProducedBlock>,
exit_notfier: Arc<Notify>,
do_exit: Arc<AtomicBool>,
mut exit_notify: broadcast::Receiver<()>,
) -> Vec<AnyhowJoinHandle> {
let commitment_config = CommitmentConfig::processed();

Expand All @@ -70,20 +66,20 @@ fn create_grpc_multiplex_processed_block_stream(
grpc_source.grpc_x_token.clone(),
block_sender,
yellowstone_grpc_proto::geyser::CommitmentLevel::Processed,
exit_notfier.clone(),
exit_notify.resubscribe(),
));
streams.push(block_reciever)
}
let merging_streams: AnyhowJoinHandle = tokio::task::spawn(async move {
const MAX_SIZE: usize = 1024;
let mut slots_processed = BTreeSet::<u64>::new();
while !do_exit.load(std::sync::atomic::Ordering::Relaxed) {
loop {
let mut select_all = futures::stream::select_all(streams.clone());
let block_message = tokio::select! {
message = select_all.next() => {
message
},
_ = exit_notfier.notified() => {
_ = exit_notify.recv() => {
break;
}
};
Expand All @@ -104,6 +100,9 @@ fn create_grpc_multiplex_processed_block_stream(
slots_processed.pop_first();
}
}
} else {
// so that the process does not use all the CPU.
tokio::time::sleep(Duration::from_millis(1)).await;
}
}
Ok(())
Expand Down Expand Up @@ -150,13 +149,11 @@ pub fn create_grpc_multiplex_blocks_subscription(
let (processed_block_sender, processed_block_reciever) =
async_channel::unbounded::<ProducedBlock>();

let exit_notify = Arc::new(Notify::new());
let do_exit = Arc::new(AtomicBool::new(false));
let (exit_notifier, exit_notify) = broadcast::channel::<()>(1);
let processed_blocks_tasks = create_grpc_multiplex_processed_block_stream(
&grpc_sources,
processed_block_sender,
exit_notify.clone(),
do_exit.clone(),
exit_notify,
);

let confirmed_blockmeta_stream = create_grpc_multiplex_block_meta_stream(
Expand Down Expand Up @@ -258,10 +255,10 @@ pub fn create_grpc_multiplex_blocks_subscription(
}
}
}
exit_notify.notify_waiters();
exit_notify.notify_one();
do_exit.store(true, std::sync::atomic::Ordering::Relaxed);
futures::future::join_all(processed_blocks_tasks).await;
let _ = exit_notifier.send(());
if tokio::time::timeout(Duration::from_millis(500), futures::future::join_all(processed_blocks_tasks)).await.is_err() {
log::error!("Cannot join the block processing threads in time");
}
}
})
};
Expand Down
18 changes: 12 additions & 6 deletions cluster-endpoints/src/grpc_subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use solana_sdk::{
};
use solana_transaction_status::{Reward, RewardType};
use std::{collections::HashMap, sync::Arc};
use tokio::sync::Notify;
use tokio::sync::broadcast;
use yellowstone_grpc_client::GeyserGrpcClient;
use yellowstone_grpc_proto::geyser::{SubscribeRequestFilterSlots, SubscribeUpdateSlot};

Expand Down Expand Up @@ -298,7 +298,7 @@ pub fn create_block_processing_task(
grpc_x_token: Option<String>,
block_sx: async_channel::Sender<SubscribeUpdateBlock>,
commitment_level: CommitmentLevel,
exit_notfier: Arc<Notify>,
mut exit_notify: broadcast::Receiver<()>,
) -> AnyhowJoinHandle {
tokio::spawn(async move {
'main_loop: loop {
Expand All @@ -316,7 +316,8 @@ pub fn create_block_processing_task(
// connect to grpc
let mut client =
connect_with_timeout_hacked(grpc_addr.clone(), grpc_x_token.clone()).await?;
let mut stream = client
let mut stream = tokio::select! {
res = client
.subscribe_once(
HashMap::new(),
Default::default(),
Expand All @@ -327,8 +328,13 @@ pub fn create_block_processing_task(
Some(commitment_level),
Default::default(),
None,
)
.await?;
) => {
res?
},
_ = exit_notify.recv() => {
break;
}
};

loop {
tokio::select! {
Expand Down Expand Up @@ -361,7 +367,7 @@ pub fn create_block_processing_task(
}
};
},
_ = exit_notfier.notified() => {
_ = exit_notify.recv() => {
break 'main_loop;
}
}
Expand Down

0 comments on commit 303576d

Please sign in to comment.