carlos / platform Public

lease: one-shot checkpoint replicators start no compaction monitors

ReplicateWithLease (the moves checkpoint: sync, then Close) built its
litestream store like a wake's. Each compaction and snapshot monitor
runs one pass at store.Open, concurrently with the sync, and Close
cancels a pass still in flight. S3 (and the in-process test server) can
apply that cancelled request after Close returns. The late L1 or L9
write then lands while the same moves pass mirrors the prefix, which
fails the round ("final mirror round: 1 object(s) changed at the source
during the copy", or "the staged copy is not the replica"). It also
lands after the lease was released or tombstoned.

Seen on hosted CI in TestOfflineLandRefusesAnOlderLiveBuildOnTheTarget,
TestOfflineLandConfiguredInstanceKey and (no log) the nested seal test.
A fake that holds the first L9 PUT until the round's first copy fails
the seal 2 of 3 times; with the monitors off it passes 3 of 3.

The store now matches Held.Checkpoint's: no compaction or snapshot
monitor, no L0 retention monitor, retention off. A checkpoint writes
only its L0 sync and the lease; the next wake's replicator compacts at
its own Open. Wake replicators are unchanged (their late-write case is
under separate review).

TestCheckpointReplicatorWritesOnlyL0AndTheLease holds level listings
until the first L0 lands, so a monitor pass, if one ran, would upload;
without the change it records the 0001 and 0009 uploads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 2fc00aea978398abed65c54a108fb474caf4b084 parent 78f5045
2 files changed, +138 −1
  • internal/lease/checkpoint_monitors_test.go +113 −0
  • internal/lease/lease.go +25 −1

CI cancelled — superseded by a newer push — run details

Intent

Attributed to this commit: 147 g CO₂e (17,718,077 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on lease-checkpoint-no-compaction, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/internal/lease/checkpoint_monitors_test.go b/internal/lease/checkpoint_monitors_test.go
new file mode 100644
index 000000000..c9a968c2b
--- /dev/null
+++ b/internal/lease/checkpoint_monitors_test.go
@@ -0,0 +1,113 @@
+package lease_test
+
+import (
+ "context"
+ "database/sql"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "path/filepath"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/lease"
+ "github.com/carlosframework/platform/internal/lease/leasetest"
+)
+
+// A one-shot checkpoint (ReplicateWithLease: sync, then Close) writes its
+// L0 sync and the lease, and nothing else. Litestream's compaction and
+// snapshot monitors each run one pass at store.Open, and Close cancels a
+// pass still in flight; S3 can apply that request after Close returns, so
+// it landed while the moves pass mirrored the prefix ("final mirror round:
+// 1 object(s) changed at the source during the copy") and after the lease
+// was released. The fixture holds every level listing until the first L0
+// upload has landed, so a monitor pass, if one runs, finds an L0 to
+// compact or snapshot and uploads it.
+func TestCheckpointReplicatorWritesOnlyL0AndTheLease(t *testing.T) {
+ bucket := leasetest.NewVersionedBucket()
+ bucket.AddBucket("data")
+ inner, _ := leasetest.NewS3Server(bucket)
+ var (
+ mu sync.Mutex
+ puts []string
+ l0Once sync.Once
+ )
+ l0 := make(chan struct{})
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ key := strings.TrimPrefix(r.URL.Path, "/data/")
+ if p := r.URL.Query().Get("prefix"); r.Method == http.MethodGet && strings.Contains(p, "/000") && !strings.Contains(p, "/0000/") {
+ select {
+ case <-l0:
+ case <-time.After(2 * time.Second):
+ }
+ }
+ inner.ServeHTTP(w, r)
+ if r.Method == http.MethodPut {
+ mu.Lock()
+ puts = append(puts, key)
+ mu.Unlock()
+ if strings.Contains(key, "/0000/") {
+ l0Once.Do(func() { close(l0) })
+ }
+ }
+ }))
+ t.Cleanup(srv.Close)
+ t.Setenv("AWS_ACCESS_KEY_ID", "test-key")
+ t.Setenv("AWS_SECRET_ACCESS_KEY", "test-secret")
+ t.Setenv("AWS_SESSION_TOKEN", "")
+ t.Setenv("AWS_EC2_METADATA_DISABLED", "true")
+ t.Setenv("AWS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent"))
+ t.Setenv("AWS_SHARED_CREDENTIALS_FILE", filepath.Join(t.TempDir(), "absent"))
+ t.Setenv("CARLOS_S3_ENDPOINT", srv.URL)
+
+ dbPath := filepath.Join(t.TempDir(), "h.example.com.db")
+ db, err := sql.Open("sqlite", dbPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, q := range []string{`PRAGMA journal_mode=WAL`, `CREATE TABLE t (id INTEGER PRIMARY KEY, body TEXT)`} {
+ if _, err := db.Exec(q); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for i := range 20 {
+ if _, err := db.Exec(`INSERT INTO t (body) VALUES (?)`, fmt.Sprintf("row-%d", i)); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := db.Close(); err != nil {
+ t.Fatal(err)
+ }
+
+ ctx := context.Background()
+ rep, err := lease.ReplicateWithLease(ctx, dbPath, "data", "instances/h.example.com", "test-region")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := rep.SyncAndWait(ctx); err != nil {
+ t.Fatal(err)
+ }
+ // Leave the monitors' Open passes, which wait for the L0, time to run.
+ time.Sleep(300 * time.Millisecond)
+ if err := rep.Close(ctx); err != nil {
+ t.Fatal(err)
+ }
+
+ mu.Lock()
+ defer mu.Unlock()
+ sawL0 := false
+ for _, k := range puts {
+ switch rel := strings.TrimPrefix(k, "instances/h.example.com/"); {
+ case rel == "lock.json":
+ case strings.HasPrefix(rel, "0000/"):
+ sawL0 = true
+ default:
+ t.Errorf("the checkpoint wrote %s; a one-shot replicator writes only L0 files and the lease", k)
+ }
+ }
+ if !sawL0 {
+ t.Fatalf("no L0 upload among %v", puts)
+ }
+}
diff --git a/internal/lease/lease.go b/internal/lease/lease.go
index e3d9bb2bc..72e964aa2 100644
--- a/internal/lease/lease.go
+++ b/internal/lease/lease.go
@@ -163,6 +163,10 @@ type Held struct {
// monitor is the DB monitor interval Replicate's store runs at; 0
// keeps dbMonitorInterval. Guarded by mu.
monitor time.Duration
+ // oneShot is a replicator that only syncs and closes (ReplicateWithLease,
+ // the moves checkpoint): its store starts no compaction, snapshot or L0
+ // retention monitor. Guarded by mu.
+ oneShot bool
}
// Acquire claims the single-writer lease at s3://bucket/prefix/lock.json
@@ -466,6 +470,22 @@ func (h *Held) newStore(dbPath string, syncInterval time.Duration) (*litestream.
store.SetRetentionEnabled(false)
}
store.L0RetentionCheckInterval = l0RetentionCheckInterval
+ h.mu.Lock()
+ oneShot := h.oneShot
+ h.mu.Unlock()
+ if oneShot {
+ // A checkpoint writes its L0 sync and nothing else. Each monitor
+ // runs one pass at store.Open, concurrently with the sync, and
+ // Close cancels a pass still in flight; S3 can apply that request
+ // after Close returns, so a late compaction or snapshot lands
+ // while the moves pass mirrors the prefix ("final mirror round:
+ // changed at the source"), after the lease is released. The next
+ // wake's replicator compacts at its own Open. Held.Checkpoint
+ // builds its store the same way.
+ store.CompactionMonitorEnabled = false
+ store.L0RetentionCheckInterval = 0
+ store.SetRetentionEnabled(false)
+ }
return store, db, nil
}
@@ -504,7 +524,8 @@ type Replicator struct {
// ReplicateWithLease acquires the single-writer lease at
// s3://bucket/prefix/lock.json and, only if that succeeds, starts litestream
// replication of dbPath to s3://bucket/prefix at the default (stream-class)
-// sync interval. If another process holds the lease it fails LOUDLY with a
+// sync interval, for a one-shot checkpoint: sync, then Close. Its store
+// runs no compaction, snapshot or retention monitor (Held.oneShot). If another process holds the lease it fails LOUDLY with a
// *HeldError (check with errors.As) — this is the signal
// the activator reacts to.
func ReplicateWithLease(ctx context.Context, dbPath, bucket, prefix, region string) (*Replicator, error) {
@@ -512,6 +533,9 @@ func ReplicateWithLease(ctx context.Context, dbPath, bucket, prefix, region stri
if err != nil {
return nil, err
}
+ held.mu.Lock()
+ held.oneShot = true
+ held.mu.Unlock()
r, err := held.Replicate(ctx, dbPath, 0)
if err != nil {
_ = held.Release(ctx)