Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
removes RwLock on ClusterInfo.instance
Browse files Browse the repository at this point in the history
  • Loading branch information
behzadnouri authored and mvines committed Dec 9, 2020
1 parent 5421981 commit 895d7d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
14 changes: 6 additions & 8 deletions core/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub struct ClusterInfo {
socket: UdpSocket,
local_message_pending_push_queue: RwLock<Vec<(CrdsValue, u64)>>,
contact_debug_interval: u64,
instance: RwLock<NodeInstance>,
instance: NodeInstance,
}

impl Default for ClusterInfo {
Expand Down Expand Up @@ -557,7 +557,7 @@ impl ClusterInfo {
socket: UdpSocket::bind("0.0.0.0:0").unwrap(),
local_message_pending_push_queue: RwLock::new(vec![]),
contact_debug_interval: DEFAULT_CONTACT_DEBUG_INTERVAL,
instance: RwLock::new(NodeInstance::new(id, timestamp())),
instance: NodeInstance::new(id, timestamp()),
};
{
let mut gossip = me.gossip.write().unwrap();
Expand Down Expand Up @@ -592,7 +592,7 @@ impl ClusterInfo {
.clone(),
),
contact_debug_interval: self.contact_debug_interval,
instance: RwLock::new(NodeInstance::new(*new_id, timestamp())),
instance: NodeInstance::new(*new_id, timestamp()),
}
}

Expand All @@ -617,10 +617,9 @@ impl ClusterInfo {
) {
let now = timestamp();
self.my_contact_info.write().unwrap().wallclock = now;
self.instance.write().unwrap().update_wallclock(now);
let entries: Vec<_> = vec![
CrdsData::ContactInfo(self.my_contact_info()),
CrdsData::NodeInstance(self.instance.read().unwrap().clone()),
CrdsData::NodeInstance(self.instance.with_wallclock(now)),
]
.into_iter()
.map(|v| CrdsValue::new_signed(v, &self.keypair))
Expand Down Expand Up @@ -1811,7 +1810,7 @@ impl ClusterInfo {
let recycler = PacketsRecycler::default();
let crds_data = vec![
CrdsData::Version(Version::new(self.id())),
CrdsData::NodeInstance(self.instance.read().unwrap().clone()),
CrdsData::NodeInstance(self.instance.with_wallclock(timestamp())),
];
for value in crds_data {
let value = CrdsValue::new_signed(value, &self.keypair);
Expand Down Expand Up @@ -2530,10 +2529,9 @@ impl ClusterInfo {
});
// Check if there is a duplicate instance of
// this node with more recent timestamp.
let self_instance = self.instance.read().unwrap().clone();
let check_duplicate_instance = |values: &[CrdsValue]| {
for value in values {
if self_instance.check_duplicate(value) {
if self.instance.check_duplicate(value) {
return Err(Error::DuplicateNodeInstance);
}
}
Expand Down
20 changes: 17 additions & 3 deletions core/src/crds_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,11 @@ impl NodeInstance {
}
}

pub fn update_wallclock(&mut self, now: u64) {
if self.wallclock < now {
self.wallclock = now;
// Clones the value with an updated wallclock.
pub fn with_wallclock(&self, now: u64) -> Self {
Self {
wallclock: now,
..*self
}
}

Expand Down Expand Up @@ -896,6 +898,18 @@ mod test {
timestamp: now - 1,
token: rng.gen(),
})));
// Updated wallclock is not a duplicate.
let other = node.with_wallclock(now + 8);
assert_eq!(
other,
NodeInstance {
from: pubkey,
wallclock: now + 8,
timestamp: now,
token: node.token,
}
);
assert!(!node.check_duplicate(&make_crds_value(other)));
// Duplicate instance.
assert!(node.check_duplicate(&make_crds_value(NodeInstance {
from: pubkey,
Expand Down

0 comments on commit 895d7d6

Please sign in to comment.