| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "sync/atomic" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | // A snapshot taken while another connection commits and checkpoints is |
| 15 | // one committed state, never a mix of two: every transaction moves the |
| 16 | // first and the last row together, pages apart, so a torn copy shows |
| 17 | // them differing. Nothing is copied to disk on the way. |
| 18 | func TestSnapshotConsistentUnderConcurrentWrites(t *testing.T) { |
| 19 | // No scratch space on disk: the temporary directory is not even |
| 20 | // writable. |
| 21 | tmp := t.TempDir() |
| 22 | if err := os.Chmod(tmp, 0o500); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | t.Cleanup(func() { os.Chmod(tmp, 0o700) }) |
| 26 | t.Setenv("TMPDIR", tmp) |
| 27 | dir := t.TempDir() |
| 28 | path := filepath.Join(dir, "state.db") |
| 29 | w, err := sql.Open("sqlite", "file:"+path+"?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)") |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | defer w.Close() |
| 34 | w.SetMaxOpenConns(1) |
| 35 | if _, err := w.Exec("CREATE TABLE t(id INTEGER PRIMARY KEY, n INTEGER NOT NULL, pad BLOB NOT NULL)"); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | const rows = 500 |
| 39 | for i := 1; i <= rows; i++ { |
| 40 | if _, err := w.Exec("INSERT INTO t(id,n,pad) VALUES(?,0,zeroblob(1500))", i); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | ctx, cancel := context.WithCancel(context.Background()) |
| 46 | defer cancel() |
| 47 | var wg sync.WaitGroup |
| 48 | var commits atomic.Int64 |
| 49 | wg.Add(1) |
| 50 | go func() { |
| 51 | defer wg.Done() |
| 52 | for i := 1; ctx.Err() == nil; i++ { |
| 53 | tx, err := w.Begin() |
| 54 | if err != nil { |
| 55 | t.Error(err) |
| 56 | return |
| 57 | } |
| 58 | // Every page between first and last is rewritten too, so a |
| 59 | // checkpoint moves the whole file under a copy. |
| 60 | if _, err := tx.Exec("UPDATE t SET n=n+1, pad=randomblob(1500)"); err != nil { |
| 61 | tx.Rollback() |
| 62 | t.Error(err) |
| 63 | return |
| 64 | } |
| 65 | if err := tx.Commit(); err != nil { |
| 66 | t.Error(err) |
| 67 | return |
| 68 | } |
| 69 | commits.Add(1) |
| 70 | { |
| 71 | if _, err := w.Exec("PRAGMA wal_checkpoint(PASSIVE)"); err != nil && !strings.Contains(err.Error(), "locked") && !strings.Contains(err.Error(), "busy") { |
| 72 | t.Error(err) |
| 73 | return |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | }() |
| 78 | |
| 79 | // A failure stops the writer first, so it never reports after the test. |
| 80 | fail := func(format string, args ...any) { |
| 81 | t.Helper() |
| 82 | cancel() |
| 83 | wg.Wait() |
| 84 | t.Fatalf(format, args...) |
| 85 | } |
| 86 | seen := map[int64]bool{} |
| 87 | for i := 0; i < 100; i++ { |
| 88 | db, err := snapshot(context.Background(), path) |
| 89 | if err != nil { |
| 90 | fail("snapshot %d: %v", i, err) |
| 91 | } |
| 92 | var lo, hi, count int64 |
| 93 | err = db.QueryRow("SELECT (SELECT n FROM t WHERE id=1), (SELECT n FROM t WHERE id=?), (SELECT count(DISTINCT n) FROM t)", rows).Scan(&lo, &hi, &count) |
| 94 | db.Close() |
| 95 | if err != nil { |
| 96 | fail("snapshot %d does not read: %v", i, err) |
| 97 | } |
| 98 | if lo != hi || count != 1 { |
| 99 | fail("snapshot %d mixes states: first %d last %d, %d distinct", i, lo, hi, count) |
| 100 | } |
| 101 | seen[lo] = true |
| 102 | } |
| 103 | cancel() |
| 104 | wg.Wait() |
| 105 | if len(seen) < 3 { |
| 106 | t.Fatalf("too little concurrency to mean anything: %d commits, %d states seen", commits.Load(), len(seen)) |
| 107 | } |
| 108 | if ents, _ := os.ReadDir(tmp); len(ents) != 0 { |
| 109 | t.Fatalf("a snapshot wrote to the temporary directory: %d entries", len(ents)) |
| 110 | } |
| 111 | ents, _ := os.ReadDir(dir) |
| 112 | for _, e := range ents { |
| 113 | switch e.Name() { |
| 114 | case "state.db", "state.db-wal", "state.db-shm": |
| 115 | default: |
| 116 | t.Fatalf("a snapshot left %s beside the state", e.Name()) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |