| 1 | package lease |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // CheckpointReceipt describes a completed sync and close under a healthy |
| 10 | // lease. It does not prove restored database semantics or authorize a command. |
| 11 | // SafeUntil is useful only in-process, with its monotonic clock component. |
| 12 | type CheckpointReceipt struct { |
| 13 | CompletedAt time.Time |
| 14 | SafeUntil time.Time |
| 15 | } |
| 16 | |
| 17 | // Checkpoint keeps the same renewal running while one temporary replica store |
| 18 | // opens, syncs and closes. Failure poisons the handle but does not release it; |
| 19 | // the caller must retain its fence, record the failure and explicitly Release. |
| 20 | func (h *Held) Checkpoint(ctx context.Context, dbPath string) (receipt CheckpointReceipt, err error) { |
| 21 | if err = h.begin(false); err != nil { |
| 22 | return receipt, err |
| 23 | } |
| 24 | defer func() { h.finish(false, err) }() |
| 25 | opCtx, cancel := context.WithCancel(ctx) |
| 26 | stop := context.AfterFunc(h.rn.ctx, cancel) |
| 27 | defer stop() |
| 28 | defer cancel() |
| 29 | store, db, err := h.newStore(dbPath, 0) |
| 30 | if err != nil { |
| 31 | return receipt, err |
| 32 | } |
| 33 | // Short checkpoints need only the explicit L0 sync. Background snapshots |
| 34 | // are unnecessary and v0.5.14 closes the DB before joining those monitors. |
| 35 | db.MonitorInterval = 0 |
| 36 | db.Replica.MonitorEnabled = false |
| 37 | store.CompactionMonitorEnabled = false |
| 38 | store.L0RetentionCheckInterval = 0 |
| 39 | store.SetRetentionEnabled(false) |
| 40 | openErr := store.Open(opCtx) |
| 41 | var syncErr error |
| 42 | if openErr == nil { |
| 43 | syncErr = db.SyncAndWait(opCtx) |
| 44 | } |
| 45 | if openErr != nil || syncErr != nil { |
| 46 | cancel() |
| 47 | } |
| 48 | closeErr := store.Close(opCtx) |
| 49 | until, healthErr := h.rn.SafeUntil() |
| 50 | err = errors.Join(openErr, syncErr, closeErr, healthErr, opCtx.Err()) |
| 51 | if err != nil { |
| 52 | return CheckpointReceipt{}, err |
| 53 | } |
| 54 | completed := time.Now() |
| 55 | if !completed.Before(until) { |
| 56 | return CheckpointReceipt{}, ErrLost |
| 57 | } |
| 58 | return CheckpointReceipt{CompletedAt: completed, SafeUntil: until}, nil |
| 59 | } |
| 60 | |