2021-05-18 09:31:40 +00:00
|
|
|
use smallvec::SmallVec;
|
2021-06-03 18:23:25 +00:00
|
|
|
use std::{
|
|
|
|
cmp::{self, Ordering},
|
2021-11-22 16:20:43 +00:00
|
|
|
fmt, iter,
|
2021-06-03 18:23:25 +00:00
|
|
|
ops::{Add, AddAssign},
|
|
|
|
};
|
2021-04-09 09:02:46 +00:00
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub type ReplicaId = u16;
|
2021-05-18 09:31:40 +00:00
|
|
|
pub type Seq = u32;
|
2021-04-07 17:56:15 +00:00
|
|
|
|
2021-06-03 18:23:25 +00:00
|
|
|
#[derive(Clone, Copy, Default, Eq, Hash, PartialEq, Ord, PartialOrd)]
|
2021-03-10 04:00:51 +00:00
|
|
|
pub struct Local {
|
|
|
|
pub replica_id: ReplicaId,
|
2021-04-07 17:56:15 +00:00
|
|
|
pub value: Seq,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 22:33:43 +00:00
|
|
|
#[derive(Clone, Copy, Default, Eq, Hash, PartialEq)]
|
2021-03-10 04:00:51 +00:00
|
|
|
pub struct Lamport {
|
|
|
|
pub replica_id: ReplicaId,
|
2021-06-03 18:23:25 +00:00
|
|
|
pub value: Seq,
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Local {
|
2021-12-09 11:00:51 +00:00
|
|
|
pub const MIN: Self = Self {
|
|
|
|
replica_id: ReplicaId::MIN,
|
|
|
|
value: Seq::MIN,
|
|
|
|
};
|
|
|
|
pub const MAX: Self = Self {
|
|
|
|
replica_id: ReplicaId::MAX,
|
|
|
|
value: Seq::MAX,
|
|
|
|
};
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub fn new(replica_id: ReplicaId) -> Self {
|
|
|
|
Self {
|
|
|
|
replica_id,
|
|
|
|
value: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) -> Self {
|
|
|
|
let timestamp = *self;
|
|
|
|
self.value += 1;
|
|
|
|
timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observe(&mut self, timestamp: Self) {
|
|
|
|
if timestamp.replica_id == self.replica_id {
|
|
|
|
self.value = cmp::max(self.value, timestamp.value + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Add<&'a Self> for Local {
|
|
|
|
type Output = Local;
|
|
|
|
|
|
|
|
fn add(self, other: &'a Self) -> Self::Output {
|
2022-08-10 21:39:24 +00:00
|
|
|
*cmp::max(&self, other)
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AddAssign<&'a Local> for Local {
|
|
|
|
fn add_assign(&mut self, other: &Self) {
|
|
|
|
if *self < *other {
|
2022-08-10 21:39:24 +00:00
|
|
|
*self = *other;
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 18:23:25 +00:00
|
|
|
#[derive(Clone, Default, Hash, Eq, PartialEq)]
|
2021-11-22 16:20:43 +00:00
|
|
|
pub struct Global(SmallVec<[u32; 8]>);
|
2021-04-09 09:02:46 +00:00
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
impl Global {
|
|
|
|
pub fn new() -> Self {
|
2021-04-09 09:02:46 +00:00
|
|
|
Self::default()
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:56:15 +00:00
|
|
|
pub fn get(&self, replica_id: ReplicaId) -> Seq {
|
2021-11-22 16:20:43 +00:00
|
|
|
self.0.get(replica_id as usize).copied().unwrap_or(0) as Seq
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observe(&mut self, timestamp: Local) {
|
2021-11-22 16:20:43 +00:00
|
|
|
if timestamp.value > 0 {
|
|
|
|
let new_len = timestamp.replica_id as usize + 1;
|
|
|
|
if new_len > self.0.len() {
|
|
|
|
self.0.resize(new_len, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry = &mut self.0[timestamp.replica_id as usize];
|
|
|
|
*entry = cmp::max(*entry, timestamp.value);
|
2021-05-18 09:31:40 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 13:28:20 +00:00
|
|
|
pub fn join(&mut self, other: &Self) {
|
2021-11-22 16:20:43 +00:00
|
|
|
if other.0.len() > self.0.len() {
|
|
|
|
self.0.resize(other.0.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (left, right) in self.0.iter_mut().zip(&other.0) {
|
|
|
|
*left = cmp::max(*left, *right);
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 13:28:20 +00:00
|
|
|
pub fn meet(&mut self, other: &Self) {
|
2021-11-22 16:20:43 +00:00
|
|
|
if other.0.len() > self.0.len() {
|
|
|
|
self.0.resize(other.0.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_len = 0;
|
|
|
|
for (ix, (left, right)) in self
|
|
|
|
.0
|
|
|
|
.iter_mut()
|
|
|
|
.zip(other.0.iter().chain(iter::repeat(&0)))
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
if *left == 0 {
|
|
|
|
*left = *right;
|
|
|
|
} else if *right > 0 {
|
|
|
|
*left = cmp::min(*left, *right);
|
|
|
|
}
|
|
|
|
|
|
|
|
if *left != 0 {
|
|
|
|
new_len = ix + 1;
|
2021-06-01 13:28:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 16:20:43 +00:00
|
|
|
self.0.resize(new_len, 0);
|
2021-06-01 13:28:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
pub fn observed(&self, timestamp: Local) -> bool {
|
|
|
|
self.get(timestamp.replica_id) >= timestamp.value
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:20:43 +00:00
|
|
|
pub fn observed_any(&self, other: &Self) -> bool {
|
|
|
|
let mut lhs = self.0.iter();
|
|
|
|
let mut rhs = other.0.iter();
|
|
|
|
loop {
|
|
|
|
if let Some(left) = lhs.next() {
|
|
|
|
if let Some(right) = rhs.next() {
|
|
|
|
if *right > 0 && left >= right {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
2021-06-01 14:54:02 +00:00
|
|
|
|
2022-01-06 01:36:12 +00:00
|
|
|
pub fn observed_all(&self, other: &Self) -> bool {
|
2021-11-22 16:20:43 +00:00
|
|
|
let mut lhs = self.0.iter();
|
|
|
|
let mut rhs = other.0.iter();
|
|
|
|
loop {
|
|
|
|
if let Some(left) = lhs.next() {
|
|
|
|
if let Some(right) = rhs.next() {
|
|
|
|
if left < right {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return rhs.next().is_none();
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 14:54:02 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2022-01-06 01:36:12 +00:00
|
|
|
pub fn changed_since(&self, other: &Self) -> bool {
|
|
|
|
if self.0.len() > other.0.len() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (left, right) in self.0.iter().zip(other.0.iter()) {
|
|
|
|
if left > right {
|
|
|
|
return true;
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-06 01:36:12 +00:00
|
|
|
false
|
2021-11-22 16:20:43 +00:00
|
|
|
}
|
2021-03-10 04:00:51 +00:00
|
|
|
|
2022-08-10 21:39:24 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = Local> + '_ {
|
2021-11-22 16:20:43 +00:00
|
|
|
self.0.iter().enumerate().map(|(replica_id, seq)| Local {
|
|
|
|
replica_id: replica_id as ReplicaId,
|
|
|
|
value: *seq,
|
|
|
|
})
|
2021-03-10 04:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 02:05:24 +00:00
|
|
|
impl FromIterator<Local> for Global {
|
|
|
|
fn from_iter<T: IntoIterator<Item = Local>>(locals: T) -> Self {
|
|
|
|
let mut result = Self::new();
|
|
|
|
for local in locals {
|
|
|
|
result.observe(local);
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 22:33:43 +00:00
|
|
|
impl Ord for Lamport {
|
|
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
|
|
// Use the replica id to break ties between concurrent events.
|
|
|
|
self.value
|
|
|
|
.cmp(&other.value)
|
|
|
|
.then_with(|| self.replica_id.cmp(&other.replica_id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Lamport {
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 04:00:51 +00:00
|
|
|
impl Lamport {
|
|
|
|
pub fn new(replica_id: ReplicaId) -> Self {
|
|
|
|
Self {
|
|
|
|
value: 1,
|
|
|
|
replica_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self) -> Self {
|
|
|
|
let timestamp = *self;
|
|
|
|
self.value += 1;
|
|
|
|
timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn observe(&mut self, timestamp: Self) {
|
|
|
|
self.value = cmp::max(self.value, timestamp.value) + 1;
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 18:23:25 +00:00
|
|
|
|
|
|
|
impl fmt::Debug for Local {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "Local {{{}: {}}}", self.replica_id, self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Lamport {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "Lamport {{{}: {}}}", self.replica_id, self.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Global {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "Global {{")?;
|
2021-11-22 16:20:43 +00:00
|
|
|
for timestamp in self.iter() {
|
|
|
|
if timestamp.replica_id > 0 {
|
2021-06-03 18:23:25 +00:00
|
|
|
write!(f, ", ")?;
|
|
|
|
}
|
2021-11-22 16:20:43 +00:00
|
|
|
write!(f, "{}: {}", timestamp.replica_id, timestamp.value)?;
|
2021-06-03 18:23:25 +00:00
|
|
|
}
|
|
|
|
write!(f, "}}")
|
|
|
|
}
|
|
|
|
}
|