Skip to content

Commit

Permalink
Use tokio RwLock instead of parking_lot
Browse files Browse the repository at this point in the history
  • Loading branch information
macladson committed Jun 15, 2023
1 parent 53d3a53 commit 2226ed9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
3 changes: 1 addition & 2 deletions validator_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,4 @@ url = "2.2.2"
malloc_utils = { path = "../common/malloc_utils" }
sysinfo = "0.26.5"
system_health = { path = "../common/system_health" }
logging = { path = "../common/logging" }

logging = { path = "../common/logging" }
30 changes: 16 additions & 14 deletions validator_client/src/beacon_node_fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::http_metrics::metrics::{inc_counter_vec, ENDPOINT_ERRORS, ENDPOINT_RE
use environment::RuntimeContext;
use eth2::BeaconNodeHttpClient;
use futures::future;
use parking_lot::RwLock as PLRwLock;
use serde_derive::{Deserialize, Serialize};
use slog::{debug, error, warn, Logger};
use slot_clock::SlotClock;
Expand Down Expand Up @@ -141,7 +140,7 @@ pub enum CandidateError {
pub struct CandidateBeaconNode<E> {
id: usize,
beacon_node: BeaconNodeHttpClient,
health: PLRwLock<Option<BeaconNodeHealth>>,
health: RwLock<Option<BeaconNodeHealth>>,
status: RwLock<Result<(), CandidateError>>,
_phantom: PhantomData<E>,
}
Expand All @@ -156,7 +155,10 @@ impl<E: EthSpec> Eq for CandidateBeaconNode<E> {}

impl<E: EthSpec> Ord for CandidateBeaconNode<E> {
fn cmp(&self, other: &Self) -> Ordering {
match (&(*self.health.read()), &(*other.health.read())) {
match (
&(*self.health.blocking_read()),
&(*other.health.blocking_read()),
) {
(None, None) => Ordering::Equal,
(None, _) => Ordering::Greater,
(_, None) => Ordering::Less,
Expand All @@ -177,7 +179,7 @@ impl<E: EthSpec> CandidateBeaconNode<E> {
Self {
id,
beacon_node,
health: PLRwLock::new(None),
health: RwLock::new(None),
status: RwLock::new(Err(CandidateError::Uninitialized)),
_phantom: PhantomData,
}
Expand Down Expand Up @@ -232,14 +234,14 @@ impl<E: EthSpec> CandidateBeaconNode<E> {
new_health.get_health_tier()
);

*self.health.write() = Some(new_health);
*self.health.write().await = Some(new_health);
*self.status.write().await = Ok(());

Ok(())
}
Err(status) => {
// Set the health as None which is sorted last in the list.
*self.health.write() = None;
*self.health.write().await = None;
*self.status.write().await = Err(status);
Ok(())
}
Expand Down Expand Up @@ -367,7 +369,7 @@ impl<T: SlotClock, E: EthSpec> BeaconNodeFallback<T, E> {
pub async fn num_synced(&self) -> usize {
let mut n = 0;
for candidate in self.candidates.read().await.iter() {
if let Some(cand) = candidate.health.read().as_ref() {
if let Some(cand) = candidate.health.read().await.as_ref() {
if self
.distance_tiers
.distance_tier(cand.health_tier.sync_distance)
Expand All @@ -384,7 +386,7 @@ impl<T: SlotClock, E: EthSpec> BeaconNodeFallback<T, E> {
pub async fn num_synced_fallback(&self) -> usize {
let mut n = 0;
for candidate in self.candidates.read().await.iter().skip(1) {
if let Some(cand) = candidate.health.read().as_ref() {
if let Some(cand) = candidate.health.read().await.as_ref() {
if self
.distance_tiers
.distance_tier(cand.health_tier.sync_distance)
Expand Down Expand Up @@ -708,12 +710,12 @@ mod tests {
health_tier: BeaconNodeHealthTier::new(4, Slot::new(9), small),
};

*candidate_1.health.write() = Some(health_1);
*candidate_2.health.write() = Some(health_2);
*candidate_3.health.write() = Some(health_3);
*candidate_4.health.write() = Some(health_4);
*candidate_5.health.write() = Some(health_5);
*candidate_6.health.write() = Some(health_6);
*candidate_1.health.blocking_write() = Some(health_1);
*candidate_2.health.blocking_write() = Some(health_2);
*candidate_3.health.blocking_write() = Some(health_3);
*candidate_4.health.blocking_write() = Some(health_4);
*candidate_5.health.blocking_write() = Some(health_5);
*candidate_6.health.blocking_write() = Some(health_6);

let mut candidates = vec![
candidate_3,
Expand Down

0 comments on commit 2226ed9

Please sign in to comment.