2020//! This is the lowest-latency option and is correct for use cases that
2121//! accept CRDT-style monotonic convergence (e.g. mobile/edge workloads).
2222
23- use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
24-
2523use nodedb_types:: error:: sqlstate;
2624use nodedb_types:: { DatabaseId , MirrorOrigin , MirrorStatus } ;
2725
@@ -52,11 +50,17 @@ pub enum MirrorReadOutcome {
5250/// `promoted` is true when the mirror has been promoted and the database
5351/// is now a writable primary — in that case all consistency levels are
5452/// served locally (no longer a mirror).
53+ /// `now_ms` is the current wall-clock time (ms since epoch), supplied by the
54+ /// caller rather than read internally so the staleness comparison is a pure
55+ /// function of its inputs — this keeps `BoundedStaleness` boundary tests
56+ /// deterministic instead of racing the wall clock between lag injection and
57+ /// the check.
5558pub fn check_mirror_read_consistency (
5659 catalog : & SystemCatalog ,
5760 mirror_db_id : DatabaseId ,
5861 mirror_origin : & MirrorOrigin ,
5962 consistency : ReadConsistency ,
63+ now_ms : u64 ,
6064) -> MirrorReadOutcome {
6165 // A promoted mirror is a normal writable database — serve all reads locally.
6266 if matches ! ( mirror_origin. status, MirrorStatus :: Promoted ) {
@@ -106,11 +110,6 @@ pub fn check_mirror_read_consistency(
106110 }
107111 } ;
108112
109- let now_ms = SystemTime :: now ( )
110- . duration_since ( UNIX_EPOCH )
111- . unwrap_or ( Duration :: ZERO )
112- . as_millis ( ) as u64 ;
113-
114113 let actual_lag_ms = now_ms. saturating_sub ( last_apply_ms) ;
115114 let max_lag_ms = max_lag. as_millis ( ) as u64 ;
116115
@@ -176,7 +175,13 @@ mod tests {
176175 let db_id = DatabaseId :: new ( 1024 ) ;
177176 let origin = sample_origin ( MirrorStatus :: Following ) ;
178177
179- match check_mirror_read_consistency ( & catalog, db_id, & origin, ReadConsistency :: Strong ) {
178+ match check_mirror_read_consistency (
179+ & catalog,
180+ db_id,
181+ & origin,
182+ ReadConsistency :: Strong ,
183+ now_ms ( ) ,
184+ ) {
180185 MirrorReadOutcome :: Reject {
181186 sqlstate_code,
182187 message,
@@ -198,7 +203,13 @@ mod tests {
198203 let db_id = DatabaseId :: new ( 1025 ) ;
199204 let origin = sample_origin ( MirrorStatus :: Following ) ;
200205
201- match check_mirror_read_consistency ( & catalog, db_id, & origin, ReadConsistency :: Eventual ) {
206+ match check_mirror_read_consistency (
207+ & catalog,
208+ db_id,
209+ & origin,
210+ ReadConsistency :: Eventual ,
211+ now_ms ( ) ,
212+ ) {
202213 MirrorReadOutcome :: ServeLocally => { }
203214 MirrorReadOutcome :: Reject { message, .. } => {
204215 panic ! ( "Eventual should serve locally, got reject: {message}" )
@@ -226,7 +237,7 @@ mod tests {
226237 . unwrap ( ) ;
227238
228239 let bound = ReadConsistency :: BoundedStaleness ( Duration :: from_secs ( 10 ) ) ;
229- match check_mirror_read_consistency ( & catalog, db_id, & origin, bound) {
240+ match check_mirror_read_consistency ( & catalog, db_id, & origin, bound, now_ms ( ) ) {
230241 MirrorReadOutcome :: ServeLocally => { }
231242 MirrorReadOutcome :: Reject { message, .. } => {
232243 panic ! ( "Fresh mirror should serve locally, got reject: {message}" )
@@ -255,7 +266,7 @@ mod tests {
255266
256267 // Bound = 5 seconds; actual lag ≈ 60 seconds.
257268 let bound = ReadConsistency :: BoundedStaleness ( Duration :: from_secs ( 5 ) ) ;
258- match check_mirror_read_consistency ( & catalog, db_id, & origin, bound) {
269+ match check_mirror_read_consistency ( & catalog, db_id, & origin, bound, now_ms ( ) ) {
259270 MirrorReadOutcome :: Reject {
260271 sqlstate_code,
261272 message,
@@ -282,7 +293,7 @@ mod tests {
282293 ReadConsistency :: Eventual ,
283294 ReadConsistency :: BoundedStaleness ( Duration :: from_secs ( 1 ) ) ,
284295 ] {
285- match check_mirror_read_consistency ( & catalog, db_id, & origin, consistency) {
296+ match check_mirror_read_consistency ( & catalog, db_id, & origin, consistency, now_ms ( ) ) {
286297 MirrorReadOutcome :: ServeLocally => { }
287298 MirrorReadOutcome :: Reject { message, .. } => {
288299 panic ! ( "Promoted mirror should always serve locally, got: {message}" )
0 commit comments