1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::{GlobalTime, PeerID, ServerWatermarksLE, ServerWatermarksLEExt, Srw, Svw};
use async_std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{
collections::BTreeSet,
sync::atomic::{AtomicU64, Ordering},
time::Duration,
};
pub trait Clock: Send + Sync {
fn current_time(&self) -> SystemTime;
}
pub struct RealClock;
impl Clock for RealClock {
fn current_time(&self) -> SystemTime {
std::time::SystemTime::now()
}
}
pub struct TestClock(Arc<AtomicU64>);
impl TestClock {
pub fn new() -> Self {
TestClock(Arc::new(AtomicU64::from(0)))
}
}
impl Clock for TestClock {
fn current_time(&self) -> SystemTime {
SystemTime::UNIX_EPOCH + Duration::from_millis(self.0.fetch_add(1, Ordering::SeqCst))
}
}
pub struct TidMgr {
clock: Box<dyn Clock>,
prev: GlobalTime,
pub(crate) wq_set: BTreeSet<GlobalTime>,
wa_set: BTreeSet<GlobalTime>,
}
impl TidMgr {
pub(crate) fn new(self_id: PeerID, clock: Box<dyn Clock>) -> Self {
TidMgr {
clock,
prev: GlobalTime::time_zero_for(self_id),
wq_set: Default::default(),
wa_set: Default::default(),
}
}
pub(crate) fn current_time(&self) -> SystemTime {
self.clock.current_time()
}
pub(crate) fn self_id(&self) -> PeerID {
self.prev.peer
}
pub(crate) fn create_timestamp(&mut self) -> GlobalTime {
let now = self.clock.current_time();
let next_millis = match now.duration_since(UNIX_EPOCH) {
Err(_) => None,
Ok(dur) => {
let secs: u64 = dur.as_secs();
let millis: u32 = dur.subsec_millis();
let mut ms = secs
.checked_mul(1000)
.expect("create_timestamp sec-to-ms overflow");
ms = ms
.checked_add(millis as u64)
.expect("create_timestamp ms-addition overflow");
Some(ms)
}
};
self.prev = match next_millis {
Some(millis) if millis > self.prev.milli_secs => {
self.prev.with_milli_sec(millis).with_event(1)
}
_ => self.prev.next_event(),
};
self.prev
}
pub(crate) fn create_watermark_and_start_s_phase(&mut self) -> GlobalTime {
let ts = self.create_timestamp();
self.wq_set.insert(ts);
self.wa_set.insert(ts);
ts
}
pub(crate) fn stored(&mut self, ts: GlobalTime) {
assert!(self.wq_set.remove(&ts));
}
pub(crate) fn fully_replicated(&mut self, ts: GlobalTime) {
assert!(!self.wq_set.contains(&ts));
assert!(self.wa_set.remove(&ts));
}
pub(crate) fn svw(&mut self) -> Svw {
match self.wq_set.iter().next() {
None => Svw(self.create_timestamp()),
Some(ts) => Svw(ts.clone()),
}
}
pub(crate) fn srw(&mut self) -> Srw {
match self.wa_set.iter().next() {
None => Srw(self.create_timestamp()),
Some(ts) => Srw(ts.clone()),
}
}
pub(crate) fn server_watermarks(&mut self) -> ServerWatermarksLE {
ServerWatermarksLE::new(self.svw(), self.srw())
}
}