region HA P1a Task 10: lease helpers for the clean release
TruncateWAL checkpoints a stopped database's WAL so the local release record can show it empty (the keep decision needs it). The replicator reports its monitor interval, so the stream cadence is testable through the wake, and RestartObligationsForTest lets other packages restart the obligation store from disk.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+45
−0
internal/lease/check.go+29 −0internal/lease/owed.go+11 −0internal/lease/release.go+5 −0
diff --git a/internal/lease/check.go b/internal/lease/check.go| index 39aa91735..fb4d00e74 100644 |
| --- a/internal/lease/check.go |
| +++ b/internal/lease/check.go |
| @@ -189,3 +189,32 @@ func hasReplicaIn(ctx context.Context, client ltxLister) (bool, error) { |
| } |
| return false, nil |
| } |
| + |
| +// TruncateWAL checkpoints path's WAL into the database and truncates it |
| +// (PRAGMA wal_checkpoint(TRUNCATE)), so it is empty, or gone once this, |
| +// the last connection, closes. Region HA's clean release calls it after |
| +// the replica's final sync and close, with every writer proven stopped: |
| +// the local record can then show the WAL empty, which the keep decision |
| +// requires (spec §5.2). The database's content is unchanged; litestream |
| +// resumes over a WAL truncated after a sync to its end incrementally. |
| +// A checkpoint that could not complete (a reader or writer still busy) is |
| +// an error. |
| +func TruncateWAL(ctx context.Context, path string) error { |
| + db, err := sql.Open("sqlite", "file:"+path) |
| + if err != nil { |
| + return err |
| + } |
| + defer db.Close() |
| + db.SetMaxOpenConns(1) |
| + if _, err := db.ExecContext(ctx, "PRAGMA busy_timeout=5000"); err != nil { |
| + return fmt.Errorf("open: %w", err) |
| + } |
| + var busy, logFrames, checkpointed int |
| + if err := db.QueryRowContext(ctx, "PRAGMA wal_checkpoint(TRUNCATE)").Scan(&busy, &logFrames, &checkpointed); err != nil { |
| + return fmt.Errorf("wal_checkpoint: %w", err) |
| + } |
| + if busy != 0 { |
| + return errors.New("wal_checkpoint: the database is busy") |
| + } |
| + return db.Close() |
| +} |
diff --git a/internal/lease/owed.go b/internal/lease/owed.go| index d4d722c6f..d95d741aa 100644 |
| --- a/internal/lease/owed.go |
| +++ b/internal/lease/owed.go |
| @@ -143,6 +143,17 @@ func SetObligationDir(dir string) error { |
| return nil |
| } |
| +// RestartObligationsForTest is an agent restart as the obligation store |
| +// sees it, for tests outside this package: the pending set in memory is |
| +// dropped and dir is loaded again (SetObligationDir), so only what was |
| +// persisted survives. Production code never calls it. |
| +func RestartObligationsForTest(dir string) error { |
| + owed.mu.Lock() |
| + owed.pending, owed.dir = nil, "" |
| + owed.mu.Unlock() |
| + return SetObligationDir(dir) |
| +} |
| + |
| func obligationStoreSet() bool { |
| owed.mu.Lock() |
| defer owed.mu.Unlock() |
diff --git a/internal/lease/release.go b/internal/lease/release.go| index bd215e7dc..e50cfcfc6 100644 |
| --- a/internal/lease/release.go |
| +++ b/internal/lease/release.go |
| @@ -137,6 +137,11 @@ func (h *Held) ReleaseMarker(ctx context.Context, m hafresh.Marker) error { |
| // HA reports whether the replicator holds an HA lease. |
| func (r *Replicator) HA() bool { return r.ha } |
| +// MonitorInterval is the interval litestream's monitor captures the |
| +// database's WAL at, as Replicate built it (Held.SetMonitorInterval, else |
| +// the 10 s default). |
| +func (r *Replicator) MonitorInterval() time.Duration { return r.db.MonitorInterval } |
| + |
| // Finish is the clean release's first half: a forced full sync, then the |
| // store's close (its own final sync), with the lease still held and |
| // renewing. txid is the replica's position after the close. err is |