Skip to content

Git designed for AI agents.

Review M4: record a checkpoint without the repository lock, in batches

Federico Elia043c2e65e6c2
diff --git a/doc/graft/PLAN7.md b/doc/graft/PLAN7.md
index 8da1276..2309b45 100644
--- a/doc/graft/PLAN7.md
+++ b/doc/graft/PLAN7.md
@@ -451,18 +451,36 @@ guarded nor swept).
`gitops.IsReachable`. `CheckpointInput` has no root set, and the checkpoint
build reads no reviews.
* **LOCK ORDER (PM check 3).** Every transaction that must serialize with a
- changeset, a review write that requires its roots, or a checkpoint commit
- takes the repository row (`SELECT ... FOR UPDATE` on Postgres; SQLite's
- immediate transaction holds the database write lock from `BEGIN`) **as its
- first statement**, before it reads or writes any child table:
- `CommitChangeset`, `RegisterRepository`, `ImportHistory`, `InsertReview`,
- `UpdateReview` with `requireRoots`, `CreateCheckpoint` and the lease methods.
- Transactions that touch only child rows without changing their `repo_id`
- (`UpdateReview` without `requireRoots`, `ReportApplied`, the orphan
- observations, `DeleteCheckpoint`) never lock the repository row and never
- upgrade a lock, so they cannot form a cycle with the first group.
- Insert-only child transactions take Postgres's implicit `KEY SHARE` on the
- parent and nothing after it. Tested with `-race` on Postgres and SQLite
+ changeset, or with a review write that requires its roots, takes the
+ repository row **as its first statement**, before it reads or writes any
+ child table. On Postgres that is `SELECT ... FOR NO KEY UPDATE`; SQLite's
+ immediate transaction holds the database write lock from `BEGIN`. The
+ transactions: `CommitChangeset`, `RegisterRepository`, `ImportHistory`,
+ `InsertReview`, `UpdateReview` with `requireRoots`, and the lease methods.
+ * Transactions that touch only child rows without changing their `repo_id`
+ never lock the repository row and never upgrade a lock, so they cannot form
+ a cycle with the first group: `UpdateReview` without `requireRoots`,
+ `ReportApplied`, the orphan observations, `DeleteCheckpoint`, and since the
+ integrated review `CreateCheckpoint`.
+ * Insert-only child transactions take Postgres's implicit `KEY SHARE` on the
+ parent and nothing after it. `FOR NO KEY UPDATE` does not conflict with
+ `KEY SHARE`, so they never hold up a writer. Deleting the repository still
+ waits for them.
+ * **Changed by the integrated review (M4).** `CreateCheckpoint` used to lock
+ the row and insert its snapshot one statement per reference. At 100k
+ references on Postgres a writer of the repository waited 38–46 s, locally.
+ * It now reads the row without locking it, and writes the snapshot 1000
+ references per statement. Nothing in it needs the writers excluded:
+ * the head only grows;
+ * the primary key makes one checkpoint per sequence: a concurrent build of
+ the same sequence inserts nothing (`ON CONFLICT DO NOTHING`) and returns
+ the one that committed;
+ * a checkpoint below a newer one is never the newest, so no seed reads it.
+ * Measured at 100k references (`TestCheckpointOfManyReferences`):
+ * Postgres: the record takes 1.6–1.7 s, while about 500 changesets commit,
+ the longest in 15–20 ms;
+ * SQLite: 0.23–0.27 s, before 0.63–0.70 s. SQLite has one write lock for
+ the whole database, so a writer waits for that. Tested with `-race` on Postgres and SQLite
(`TestLockOrderConcurrentWriters`): concurrent lands, review creations and
head moves (requiring their roots), a sweeper that deletes keep-around
references until the writers are done, comments, checkpoint commits and
diff --git a/internal/graft/corestore/sqlstore/checkpoint.go b/internal/graft/corestore/sqlstore/checkpoint.go
index edee86c..8d2b940 100644
--- a/internal/graft/corestore/sqlstore/checkpoint.go
+++ b/internal/graft/corestore/sqlstore/checkpoint.go
@@ -7,11 +7,17 @@ import (
"fmt"
"math"
"sort"
+ "strings"
"time"
"github.com/feder-cr/git-ai/internal/graft/corestore"
)
+// checkpointRefBatch is how many snapshot references one INSERT of
+// CreateCheckpoint writes: 5000 parameters, far below the limits of both
+// dialects (SQLite 32766, Postgres 65535).
+const checkpointRefBatch = 1000
+
// CreateCheckpoint implements corestore.MetaStore.
func (s *Store) CreateCheckpoint(ctx context.Context, repoID int64, in corestore.CheckpointInput) (corestore.Checkpoint, bool, error) {
if err := validateCheckpoint(repoID, in); err != nil {
@@ -25,9 +31,16 @@ func (s *Store) CreateCheckpoint(ctx context.Context, repoID int64, in corestore
err := s.withTx(ctx, nil, func(tx *sql.Tx) error {
cp, created = corestore.Checkpoint{}, false
- // LOCK ORDER (doc/graft/PLAN7.md): the repository row first, which
- // serializes the checkpoint with changesets.
- repo, err := s.selectRepository(ctx, tx, "repo_id = ?", []any{repoID}, true)
+ // The repository row is read, not locked (integrated review M4): the
+ // record of a large snapshot would hold up every writer of the
+ // repository. Nothing here needs the writers excluded:
+ // * the head only grows, so a sequence at or below it stays so;
+ // * the primary key makes one checkpoint per sequence;
+ // * a checkpoint below a newer one is never the newest, so no seed reads
+ // it;
+ // * a concurrent deletion of the repository fails this insert (its
+ // foreign key) or waits for it.
+ repo, err := s.selectRepository(ctx, tx, "repo_id = ?", []any{repoID}, false)
if err != nil {
return err
}
@@ -57,24 +70,51 @@ func (s *Store) CreateCheckpoint(ctx context.Context, repoID int64, in corestore
return err
}
- if _, err := tx.ExecContext(ctx, s.bind(`
+ // Another build of the same sequence may commit first (the repository
+ // is not locked): its checkpoint is kept and returned, as one found
+ // above.
+ res, err := tx.ExecContext(ctx, s.bind(`
INSERT INTO graft_checkpoints (repo_id, seq, pack_key, pack_bytes, created_at)
VALUES (?, ?, ?, ?, ?)
- `), repoID, int64(in.Seq), in.PackKey, in.PackBytes, s.timeArg(now)); err != nil {
+ ON CONFLICT (repo_id, seq) DO NOTHING
+ `), repoID, int64(in.Seq), in.PackKey, in.PackBytes, s.timeArg(now))
+ if err != nil {
return fmt.Errorf("insert checkpoint: %w", err)
}
+ if inserted, err := res.RowsAffected(); err != nil {
+ return fmt.Errorf("insert checkpoint: %w", err)
+ } else if inserted == 0 {
+ existing, found, err := s.checkpointAt(ctx, tx, repoID, in.Seq)
+ if err != nil {
+ return err
+ }
+ if !found {
+ return fmt.Errorf("storage core: checkpoint at changeset %d of repository %d conflicts with one that is not visible", in.Seq, repoID)
+ }
+ cp = existing
+ return nil
+ }
names := make([]string, 0, len(in.Refs))
for name := range in.Refs {
names = append(names, name)
}
sort.Strings(names)
- for ordinal, name := range names {
- if _, err := tx.ExecContext(ctx, s.bind(`
- INSERT INTO graft_checkpoint_refs (repo_id, seq, ordinal, name, target)
- VALUES (?, ?, ?, ?, ?)
- `), repoID, int64(in.Seq), ordinal, bytesArg([]byte(name)), string(in.Refs[name])); err != nil {
- return fmt.Errorf("insert checkpoint reference: %w", err)
+ // The snapshot is written in batches, one statement each: a statement
+ // per reference made the transaction grow with the round trips (tens of
+ // seconds at 100k references on a networked database).
+ for start := 0; start < len(names); start += checkpointRefBatch {
+ end := min(start+checkpointRefBatch, len(names))
+ values := make([]string, 0, end-start)
+ args := make([]any, 0, 5*(end-start))
+ for ordinal := start; ordinal < end; ordinal++ {
+ values = append(values, "(?, ?, ?, ?, ?)")
+ args = append(args, repoID, int64(in.Seq), ordinal, bytesArg([]byte(names[ordinal])), string(in.Refs[names[ordinal]]))
+ }
+ if _, err := tx.ExecContext(ctx, s.bind(
+ "INSERT INTO graft_checkpoint_refs (repo_id, seq, ordinal, name, target) VALUES "+strings.Join(values, ", ")),
+ args...); err != nil {
+ return fmt.Errorf("insert checkpoint references: %w", err)
}
}
diff --git a/internal/graft/corestore/sqlstore/checkpoint_large_test.go b/internal/graft/corestore/sqlstore/checkpoint_large_test.go
new file mode 100644
index 0000000..bcd2fd4
--- /dev/null
+++ b/internal/graft/corestore/sqlstore/checkpoint_large_test.go
@@ -0,0 +1,78 @@
+package sqlstore_test
+
+import (
+ "context"
+ "fmt"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/feder-cr/git-ai/internal/graft/corestore"
+ "github.com/feder-cr/git-ai/internal/graft/corestore/sqlstore"
+)
+
+// TestCheckpointOfManyReferences records the snapshot of a repository with
+// 100,000 references (the batched inserts of CreateCheckpoint) and reads it back
+// whole and in order, while a writer commits changesets of the same repository
+// back to back. It logs how long the record took and the longest changeset
+// commit during it: the cost writers pay for a checkpoint of a large
+// repository (integrated review M4). The record does not lock the repository
+// row, so on Postgres the writer does not wait for it; SQLite has one write
+// lock for the whole database, so there the writer waits for the record's
+// transaction.
+func TestCheckpointOfManyReferences(t *testing.T) {
+ const references = 100_000
+
+ forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {
+ repo, _ := headRepo(t, ctx, s, "many references", 1)
+ refs := make(map[string]corestore.Target, references)
+ refs["HEAD"] = corestore.SymbolicTarget("refs/heads/main")
+ for i := 1; i < references; i++ {
+ refs[fmt.Sprintf("refs/heads/branch-%06d", i)] = oid(i)
+ }
+
+ done := make(chan struct{})
+ type writes struct {
+ n int
+ longest time.Duration
+ err error
+ }
+ written := make(chan writes, 1)
+ go func() {
+ var w writes
+ defer func() { written <- w }()
+ for prev := oid(1); ; w.n++ {
+ select {
+ case <-done:
+ return
+ default:
+ }
+ next := oid(2 + w.n)
+ began := time.Now()
+ if _, w.err = commitPack(ctx, s, repo.ID, update("refs/heads/main", prev, next), ownedKey(repo.ID, fmt.Sprintf("write %d", w.n))); w.err != nil {
+ return
+ }
+ w.longest = max(w.longest, time.Since(began))
+ prev = next
+ }
+ }()
+
+ began := time.Now()
+ _, created, err := s.CreateCheckpoint(ctx, repo.ID, corestore.CheckpointInput{
+ Seq: 1, Refs: refs, PackKey: ownedKey(repo.ID, "large checkpoint"), PackBytes: 1,
+ })
+ took := time.Since(began)
+ close(done)
+ require.NoError(t, err)
+ require.True(t, created)
+ w := <-written
+ require.NoError(t, w.err)
+ t.Logf("%s: checkpoint of %d references recorded in %s; %d changesets committed meanwhile, the longest in %s",
+ s.Dialect(), references, took, w.n, w.longest)
+
+ got, err := s.CheckpointRefs(ctx, repo.ID, 1)
+ require.NoError(t, err)
+ require.Equal(t, refs, got)
+ })
+}
diff --git a/internal/graft/corestore/sqlstore/repository.go b/internal/graft/corestore/sqlstore/repository.go
index 0601593..92e0fdd 100644
--- a/internal/graft/corestore/sqlstore/repository.go
+++ b/internal/graft/corestore/sqlstore/repository.go
@@ -31,10 +31,18 @@ const repoColumns = "repo_id, storage, relative_path, object_format, head_seq, l
// locks the row until the end of the transaction, which serializes the
// changesets of the repository. SQLite needs no such clause: the immediate
// transaction already holds the write lock.
+//
+// The lock is FOR NO KEY UPDATE: the lockers update only columns that no
+// foreign key references (head_seq, log_seq, next_review_id), and two of them
+// still exclude each other. Unlike FOR UPDATE it does not wait for the key-share
+// locks that inserts into the tables referencing the repository take: a
+// checkpoint record, which inserts its snapshot without the repository lock,
+// does not hold up the writers (integrated review M4). Deleting the repository
+// still waits for those inserts.
func (s *Store) selectRepository(ctx context.Context, q querier, where string, args []any, lock bool) (repoState, error) {
query := "SELECT " + repoColumns + " FROM graft_repositories WHERE " + where
if lock && s.dialect == Postgres {
- query += " FOR UPDATE"
+ query += " FOR NO KEY UPDATE"
}
r, err := scanRepoState(q.QueryRowContext(ctx, s.bind(query), args...).Scan)