powdb-sync 0.13.0

Retained replication-unit substrate for PowDB embedded replicas and read replicas
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use std::fs;
use std::io;
use std::path::Path;

use crate::metadata::{
    now_unix_secs, read_replica_cursors_unlocked, replace_replica_cursors_unlocked,
    with_cursor_metadata_lock,
};
use crate::segment::list_segment_files;
use crate::{read_identity, retained_segments_dir, retained_tail_progress, ReplicaCursor};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SyncRepairAction {
    None,
    Pull,
    AwaitArchive,
    Rebootstrap,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicaSyncStatus {
    pub replica_id: String,
    pub active: bool,
    pub last_applied_lsn: Option<u64>,
    pub remote_lsn: u64,
    /// Largest contiguous retained-unit LSN currently servable after the
    /// replica cursor. This can lag behind `remote_lsn` while the primary has
    /// progressed but its latest WAL records have not yet been published into
    /// retained segments.
    pub servable_lsn: Option<u64>,
    /// Primary progress that is not yet represented in contiguous retained
    /// history for this replica.
    pub unarchived_lsn: Option<u64>,
    pub lag_lsn: Option<u64>,
    /// Estimated retained segment bytes that overlap the unapplied LSN range.
    pub lag_bytes: Option<u64>,
    /// Milliseconds since the primary-side cursor was last acknowledged while stale.
    pub lag_ms: Option<u64>,
    pub stale: bool,
    pub repair_action: SyncRepairAction,
    pub last_sync_error: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplicaApplyAckSummary {
    pub replica_id: String,
    pub previous_applied_lsn: u64,
    pub applied_lsn: u64,
    pub remote_lsn: u64,
    pub advanced: bool,
    pub status: ReplicaSyncStatus,
}

/// Acknowledge that a replica successfully applied retained history through an
/// LSN observed on the primary.
///
/// This is the primary-side half of the future pull protocol: after a replica
/// applies a tail locally, the server should call this to advance the retained
/// history floor for that replica. The update is monotonic and fail-closed:
/// stale acknowledgements, inactive cursors, and acknowledgements beyond the
/// advertised primary LSN are rejected.
pub fn acknowledge_replica_apply(
    data_dir: &Path,
    replica_id: &str,
    applied_lsn: u64,
    remote_lsn: u64,
) -> io::Result<ReplicaApplyAckSummary> {
    acknowledge_replica_apply_at(
        data_dir,
        replica_id,
        applied_lsn,
        remote_lsn,
        now_unix_secs(),
    )
}

/// Return primary-side sync status for one replica cursor.
pub fn replica_sync_status(
    data_dir: &Path,
    replica_id: &str,
    remote_lsn: u64,
) -> io::Result<ReplicaSyncStatus> {
    replica_sync_status_at(data_dir, replica_id, remote_lsn, now_unix_secs())
}

fn acknowledge_replica_apply_at(
    data_dir: &Path,
    replica_id: &str,
    applied_lsn: u64,
    remote_lsn: u64,
    now: u64,
) -> io::Result<ReplicaApplyAckSummary> {
    if applied_lsn > remote_lsn {
        return Err(invalid_input(format!(
            "replica acknowledgement LSN {applied_lsn} is ahead of remote LSN {remote_lsn}"
        )));
    }

    with_cursor_metadata_lock(data_dir, || {
        let mut cursors = read_replica_cursors_unlocked(data_dir)?;
        let Some(cursor) = cursors
            .iter_mut()
            .find(|cursor| cursor.replica_id == replica_id)
        else {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "replica cursor not found; rebootstrap required",
            ));
        };
        if !cursor.active {
            return Err(invalid_input(
                "replica cursor is inactive; rebootstrap required",
            ));
        }
        if applied_lsn < cursor.applied_lsn {
            return Err(invalid_input(format!(
                "replica acknowledgement LSN {applied_lsn} is behind primary cursor LSN {}",
                cursor.applied_lsn
            )));
        }

        let previous_applied_lsn = cursor.applied_lsn;
        let mut acknowledged = cursor.clone();
        acknowledged.applied_lsn = applied_lsn;
        acknowledged.updated_unix_secs = now;
        let status = status_for_cursor(data_dir, replica_id, Some(&acknowledged), remote_lsn, now)?;
        *cursor = acknowledged;
        replace_replica_cursors_unlocked(data_dir, cursors)?;

        Ok(ReplicaApplyAckSummary {
            replica_id: replica_id.to_string(),
            previous_applied_lsn,
            applied_lsn,
            remote_lsn,
            advanced: applied_lsn > previous_applied_lsn,
            status,
        })
    })
}

fn replica_sync_status_at(
    data_dir: &Path,
    replica_id: &str,
    remote_lsn: u64,
    now: u64,
) -> io::Result<ReplicaSyncStatus> {
    let cursors = read_replica_cursors_unlocked(data_dir)?;
    let cursor = cursors
        .iter()
        .find(|cursor| cursor.replica_id == replica_id);
    status_for_cursor(data_dir, replica_id, cursor, remote_lsn, now)
}

fn status_for_cursor(
    data_dir: &Path,
    replica_id: &str,
    cursor: Option<&ReplicaCursor>,
    remote_lsn: u64,
    now: u64,
) -> io::Result<ReplicaSyncStatus> {
    let Some(cursor) = cursor else {
        return Ok(ReplicaSyncStatus {
            replica_id: replica_id.to_string(),
            active: false,
            last_applied_lsn: None,
            remote_lsn,
            servable_lsn: None,
            unarchived_lsn: None,
            lag_lsn: None,
            lag_bytes: None,
            lag_ms: None,
            stale: true,
            repair_action: SyncRepairAction::Rebootstrap,
            last_sync_error: Some("replica cursor not found; rebootstrap required".to_string()),
        });
    };

    let lag_lsn = remote_lsn.checked_sub(cursor.applied_lsn);
    if !cursor.active {
        return Ok(ReplicaSyncStatus {
            replica_id: replica_id.to_string(),
            active: false,
            last_applied_lsn: Some(cursor.applied_lsn),
            remote_lsn,
            servable_lsn: None,
            unarchived_lsn: None,
            lag_lsn,
            lag_bytes: None,
            lag_ms: None,
            stale: true,
            repair_action: SyncRepairAction::Rebootstrap,
            last_sync_error: Some("replica cursor is inactive; rebootstrap required".to_string()),
        });
    }
    if cursor.applied_lsn > remote_lsn {
        return Ok(ReplicaSyncStatus {
            replica_id: replica_id.to_string(),
            active: true,
            last_applied_lsn: Some(cursor.applied_lsn),
            remote_lsn,
            servable_lsn: None,
            unarchived_lsn: None,
            lag_lsn: None,
            lag_bytes: None,
            lag_ms: None,
            stale: true,
            repair_action: SyncRepairAction::Rebootstrap,
            last_sync_error: Some(format!(
                "replica cursor LSN {} is ahead of remote LSN {remote_lsn}",
                cursor.applied_lsn
            )),
        });
    }
    if cursor.applied_lsn == remote_lsn {
        return Ok(ReplicaSyncStatus {
            replica_id: replica_id.to_string(),
            active: true,
            last_applied_lsn: Some(cursor.applied_lsn),
            remote_lsn,
            servable_lsn: Some(remote_lsn),
            unarchived_lsn: Some(0),
            lag_lsn: Some(0),
            lag_bytes: Some(0),
            lag_ms: Some(0),
            stale: false,
            repair_action: SyncRepairAction::None,
            last_sync_error: None,
        });
    }

    let lag_ms = now
        .saturating_sub(cursor.updated_unix_secs)
        .saturating_mul(1000);
    let identity = read_identity(data_dir)?;
    let segment_dir = retained_segments_dir(data_dir);
    let progress = retained_tail_progress(
        &segment_dir,
        identity.segment_identity(),
        cursor.applied_lsn,
        remote_lsn,
    );

    match progress {
        Ok(progress) => {
            let servable_lsn = progress.contiguous_through_lsn;
            let unarchived_lsn = remote_lsn.saturating_sub(servable_lsn);
            let (repair_action, last_sync_error) = if servable_lsn > cursor.applied_lsn {
                (SyncRepairAction::Pull, None)
            } else {
                (
                    SyncRepairAction::AwaitArchive,
                    Some("primary has advanced, but retained history is not yet archived; retry after archive".to_string()),
                )
            };
            Ok(ReplicaSyncStatus {
                replica_id: replica_id.to_string(),
                active: true,
                last_applied_lsn: Some(cursor.applied_lsn),
                remote_lsn,
                servable_lsn: Some(servable_lsn),
                unarchived_lsn: Some(unarchived_lsn),
                lag_lsn,
                lag_bytes: Some(retained_lag_bytes(
                    &segment_dir,
                    cursor.applied_lsn,
                    servable_lsn,
                )?),
                lag_ms: Some(lag_ms),
                stale: true,
                repair_action,
                last_sync_error,
            })
        }
        Err(err)
            if err.kind() == io::ErrorKind::InvalidData
                && (err.to_string().contains("gap") || err.to_string().contains("missing")) =>
        {
            Ok(ReplicaSyncStatus {
                replica_id: replica_id.to_string(),
                active: true,
                last_applied_lsn: Some(cursor.applied_lsn),
                remote_lsn,
                servable_lsn: None,
                unarchived_lsn: None,
                lag_lsn,
                lag_bytes: None,
                lag_ms: Some(lag_ms),
                stale: true,
                repair_action: SyncRepairAction::Rebootstrap,
                last_sync_error: Some(format!(
                    "retained history is unavailable; rebootstrap required: {err}"
                )),
            })
        }
        Err(err) => Err(err),
    }
}

fn retained_lag_bytes(dir: &Path, applied_lsn: u64, remote_lsn: u64) -> io::Result<u64> {
    let mut total = 0u64;
    for file in list_segment_files(dir)? {
        if file.end_lsn <= applied_lsn || file.start_lsn > remote_lsn {
            continue;
        }
        let len = fs::metadata(file.path)?.len();
        total = total
            .checked_add(len)
            .ok_or_else(|| io::Error::other("retained lag byte count overflow"))?;
    }
    Ok(total)
}

fn invalid_input(message: impl Into<String>) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidInput, message.into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        minimum_retained_lsn, read_replica_cursors, write_identity_snapshot, write_segment_atomic,
        DatabaseIdentity, IdentitySnapshot, RetainedSegment, RetainedUnit,
    };

    fn identity() -> DatabaseIdentity {
        DatabaseIdentity {
            database_id: *b"replica-status!!",
            primary_generation: 1,
        }
    }

    fn unit(lsn: u64) -> RetainedUnit {
        RetainedUnit {
            tx_id: 1,
            record_type: 4,
            lsn,
            data: lsn.to_le_bytes().to_vec(),
        }
    }

    fn write_identity(data_dir: &Path) {
        let snapshot = IdentitySnapshot::from_identity(identity(), 1);
        write_identity_snapshot(data_dir, &snapshot).unwrap();
    }

    fn write_segment(data_dir: &Path, start_lsn: u64, end_lsn: u64) {
        let units = (start_lsn..=end_lsn).map(unit).collect();
        let segment = RetainedSegment::new(identity().segment_identity(), units).unwrap();
        write_segment_atomic(&retained_segments_dir(data_dir), &segment).unwrap();
    }

    fn active_cursor(replica_id: &str, applied_lsn: u64, updated_unix_secs: u64) -> ReplicaCursor {
        ReplicaCursor {
            replica_id: replica_id.to_string(),
            applied_lsn,
            updated_unix_secs,
            active: true,
        }
    }

    #[test]
    fn acknowledgement_advances_cursor_and_clears_lag() {
        let dir = tempfile::tempdir().unwrap();
        write_identity(dir.path());
        write_segment(dir.path(), 1, 5);
        write_segment(dir.path(), 6, 10);
        crate::upsert_replica_cursor(dir.path(), ReplicaCursor::active("replica-a", 0)).unwrap();

        let ack = acknowledge_replica_apply_at(dir.path(), "replica-a", 10, 10, 200).unwrap();

        assert_eq!(ack.previous_applied_lsn, 0);
        assert_eq!(ack.applied_lsn, 10);
        assert!(ack.advanced);
        assert_eq!(ack.status.repair_action, SyncRepairAction::None);
        assert!(!ack.status.stale);
        assert_eq!(ack.status.lag_lsn, Some(0));
        assert_eq!(ack.status.lag_bytes, Some(0));
        assert_eq!(minimum_retained_lsn(dir.path()).unwrap(), Some(11));

        let cursor = read_replica_cursors(dir.path())
            .unwrap()
            .into_iter()
            .find(|cursor| cursor.replica_id == "replica-a")
            .unwrap();
        assert_eq!(cursor.applied_lsn, 10);
        assert_eq!(cursor.updated_unix_secs, 200);
    }

    #[test]
    fn stale_status_reports_pull_with_lag_bytes() {
        let dir = tempfile::tempdir().unwrap();
        write_identity(dir.path());
        write_segment(dir.path(), 1, 5);
        write_segment(dir.path(), 6, 10);
        crate::upsert_replica_cursor(dir.path(), active_cursor("replica-a", 5, 100)).unwrap();

        let status = replica_sync_status_at(dir.path(), "replica-a", 10, 500).unwrap();

        assert!(status.stale);
        assert_eq!(status.repair_action, SyncRepairAction::Pull);
        assert_eq!(status.servable_lsn, Some(10));
        assert_eq!(status.unarchived_lsn, Some(0));
        assert_eq!(status.lag_lsn, Some(5));
        assert!(status.lag_bytes.unwrap() > 0);
        assert!(status.lag_ms.unwrap() > 0);
        assert_eq!(status.last_sync_error, None);
    }

    #[test]
    fn stale_status_reports_pull_for_contiguous_archived_prefix() {
        let dir = tempfile::tempdir().unwrap();
        write_identity(dir.path());
        write_segment(dir.path(), 1, 8);
        crate::upsert_replica_cursor(dir.path(), active_cursor("replica-a", 5, 100)).unwrap();

        let status = replica_sync_status_at(dir.path(), "replica-a", 10, 500).unwrap();

        assert!(status.stale);
        assert_eq!(status.repair_action, SyncRepairAction::Pull);
        assert_eq!(status.servable_lsn, Some(8));
        assert_eq!(status.unarchived_lsn, Some(2));
        assert_eq!(status.lag_lsn, Some(5));
        assert!(status.lag_bytes.unwrap() > 0);
        assert_eq!(status.last_sync_error, None);
    }

    #[test]
    fn stale_status_reports_await_archive_when_primary_is_ahead_of_retained_tail() {
        let dir = tempfile::tempdir().unwrap();
        write_identity(dir.path());
        crate::upsert_replica_cursor(dir.path(), active_cursor("replica-a", 5, 100)).unwrap();

        let status = replica_sync_status_at(dir.path(), "replica-a", 10, 500).unwrap();

        assert!(status.stale);
        assert_eq!(status.repair_action, SyncRepairAction::AwaitArchive);
        assert_eq!(status.servable_lsn, Some(5));
        assert_eq!(status.unarchived_lsn, Some(5));
        assert_eq!(status.lag_lsn, Some(5));
        assert_eq!(status.lag_bytes, Some(0));
        assert!(status.last_sync_error.unwrap().contains("not yet archived"));
    }

    #[test]
    fn missing_retained_tail_reports_rebootstrap() {
        let dir = tempfile::tempdir().unwrap();
        write_identity(dir.path());
        crate::upsert_replica_cursor(dir.path(), active_cursor("replica-a", 5, 100)).unwrap();
        write_segment(dir.path(), 8, 10);

        let status = replica_sync_status_at(dir.path(), "replica-a", 10, 500).unwrap();

        assert!(status.stale);
        assert_eq!(status.repair_action, SyncRepairAction::Rebootstrap);
        assert_eq!(status.servable_lsn, None);
        assert_eq!(status.unarchived_lsn, None);
        assert_eq!(status.lag_bytes, None);
        assert!(status
            .last_sync_error
            .unwrap()
            .contains("rebootstrap required"));
    }

    #[test]
    fn acknowledgement_rejects_stale_and_inactive_cursors() {
        let dir = tempfile::tempdir().unwrap();
        write_identity(dir.path());
        write_segment(dir.path(), 1, 10);
        crate::upsert_replica_cursor(dir.path(), ReplicaCursor::active("replica-a", 5)).unwrap();

        let err = acknowledge_replica_apply_at(dir.path(), "replica-a", 4, 10, 100).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
        assert!(err.to_string().contains("behind primary cursor"));

        crate::retire_replica_cursor(dir.path(), "replica-a", 101).unwrap();
        let err = acknowledge_replica_apply_at(dir.path(), "replica-a", 6, 10, 102).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
        assert!(err.to_string().contains("rebootstrap required"));
    }

    #[test]
    fn acknowledgement_status_error_leaves_cursor_unchanged() {
        let dir = tempfile::tempdir().unwrap();
        write_identity(dir.path());
        write_segment(dir.path(), 1, 10);
        crate::upsert_replica_cursor(dir.path(), active_cursor("replica-a", 5, 100)).unwrap();
        fs::remove_file(crate::sync_state_dir(dir.path()).join(crate::IDENTITY_FILE)).unwrap();

        let err = acknowledge_replica_apply_at(dir.path(), "replica-a", 6, 10, 200).unwrap_err();

        assert_eq!(err.kind(), io::ErrorKind::NotFound);
        let cursor = read_replica_cursors(dir.path())
            .unwrap()
            .into_iter()
            .find(|cursor| cursor.replica_id == "replica-a")
            .unwrap();
        assert_eq!(cursor.applied_lsn, 5);
        assert_eq!(cursor.updated_unix_secs, 100);
    }
}