Git designed for AI agents.
sqlstore, dbreview: condition review writes on the roots snapshot (R1)
Federico Eliae38c0d86c92c
diff --git a/internal/graft/corestore/checkpoint/checkpoint_test.go b/internal/graft/corestore/checkpoint/checkpoint_test.goindex 9430c1b..a85dae8 100644--- a/internal/graft/corestore/checkpoint/checkpoint_test.go+++ b/internal/graft/corestore/checkpoint/checkpoint_test.go@@ -223,7 +223,7 @@ func (e *env) review(t *testing.T, id int64, head, base git.ObjectID, state gitaBaseCommitId: base.String(),State: state,}, nil- })+ }, 0)require.NoError(t, err)return r}@@ -504,7 +504,7 @@ func TestCreateRefusesMissingReviewCommits(t *testing.T) {stored, version, err := e.core.Meta.GetReview(e.ctx, repo.ID, r.GetId())require.NoError(t, err)stored.State = gitalypb.Review_STATE_CLOSED- require.NoError(t, e.core.Meta.UpdateReview(e.ctx, repo.ID, r.GetId(), version, stored))+ require.NoError(t, e.core.Meta.UpdateReview(e.ctx, repo.ID, r.GetId(), version, stored, 0))_, created, err := e.cp.Create(e.ctx, e.pbRepo())require.NoError(t, err)require.True(t, created)diff --git a/internal/graft/corestore/dbreview/dbreview.go b/internal/graft/corestore/dbreview/dbreview.goindex 39ca3bb..f9bd2a4 100644--- a/internal/graft/corestore/dbreview/dbreview.go+++ b/internal/graft/corestore/dbreview/dbreview.go@@ -37,6 +37,10 @@ type Store struct {ctx context.Contextmeta corestore.MetaStorerepoID int64+ // rootsAt is the sequence of the reference snapshot that the commits written+ // by Create and Update were checked against, 0 when they move no commit+ // (doc/graft/PLAN7.md, REVIEW ROOTS RULE R1).+ rootsAt uint64}var _ store.ReviewStore = (*Store)(nil)@@ -47,6 +51,23 @@ func New(ctx context.Context, meta corestore.MetaStore, repoID int64) store.Revireturn &Store{ctx: ctx, meta: meta, repoID: repoID}}+// Refs returns the references of the repository in the storage core and the+// sequence of that snapshot, which the commits of a review are checked+// against.+func (s *Store) Refs() (map[string]corestore.Target, uint64, error) {+ return s.meta.Refs(s.ctx, s.repoID)+}++// WithRootsAt returns a store whose Create and Update write only while no+// checkpoint newer than rootsAt exists, else they fail with+// corestore.ErrReviewRootsStale and write nothing. rootsAt is the sequence+// returned by the Refs call the review's new commits were checked against.+func (s *Store) WithRootsAt(rootsAt uint64) store.ReviewStore {+ at := *s+ at.rootsAt = rootsAt+ return &at+}+// Create stores a new review. It assigns the id, created_at and updated_at.// requirements and approved are not stored. The returned review is a copy.func (s *Store) Create(r *gitalypb.Review) (*gitalypb.Review, error) {@@ -59,7 +80,7 @@ func (s *Store) Create(r *gitalypb.Review) (*gitalypb.Review, error) {review.Requirements = nilreview.Approved = falsereturn review, nil- })+ }, s.rootsAt)if err != nil {return nil, mapErr(err)}@@ -111,7 +132,7 @@ func (s *Store) Update(id string, fn func(*gitalypb.Review) error) (*gitalypb.Restored.Requirements = nilstored.Approved = false- err = s.meta.UpdateReview(s.ctx, s.repoID, id, version, stored)+ err = s.meta.UpdateReview(s.ctx, s.repoID, id, version, stored, s.rootsAt)if err == nil {return proto.Clone(r).(*gitalypb.Review), nil}diff --git a/internal/graft/corestore/dbreview/dbreview_test.go b/internal/graft/corestore/dbreview/dbreview_test.goindex b2f8678..fe17a41 100644--- a/internal/graft/corestore/dbreview/dbreview_test.go+++ b/internal/graft/corestore/dbreview/dbreview_test.go@@ -207,6 +207,8 @@ type conflictingMeta struct {corestore.MetaStoregets, updates intupdateErr error+ // rootsAt are the rootsAt arguments of the writes, in order.+ rootsAt []uint64}func (m *conflictingMeta) GetReview(context.Context, int64, string) (*gitalypb.Review, int64, error) {@@ -214,11 +216,44 @@ func (m *conflictingMeta) GetReview(context.Context, int64, string) (*gitalypb.Rreturn &gitalypb.Review{Id: "1", TargetBranch: []byte("main")}, int64(m.gets), nil}-func (m *conflictingMeta) UpdateReview(context.Context, int64, string, int64, *gitalypb.Review) error {+func (m *conflictingMeta) UpdateReview(_ context.Context, _ int64, _ string, _ int64, _ *gitalypb.Review, rootsAt uint64) error {m.updates+++ m.rootsAt = append(m.rootsAt, rootsAt)return m.updateErr}+func (m *conflictingMeta) InsertReview(_ context.Context, _ int64, build func(string) (*gitalypb.Review, error), rootsAt uint64) (*gitalypb.Review, error) {+ m.rootsAt = append(m.rootsAt, rootsAt)+ return build("1")+}++// TestRootsAtReachesTheDatabase: the snapshot sequence of WithRootsAt is the+// rootsAt of every write; New's store moves no commit (0). A stale snapshot is+// returned at once, never retried with the same snapshot.+func TestRootsAtReachesTheDatabase(t *testing.T) {+ ctx := testhelper.Context(t)+ meta := &conflictingMeta{}+ plain := dbreview.New(ctx, meta, 1)++ _, err := plain.Create(&gitalypb.Review{})+ require.NoError(t, err)+ _, err = plain.Update("1", func(*gitalypb.Review) error { return nil })+ require.NoError(t, err)++ at := plain.(*dbreview.Store).WithRootsAt(7)+ _, err = at.Create(&gitalypb.Review{})+ require.NoError(t, err)+ _, err = at.Update("1", func(*gitalypb.Review) error { return nil })+ require.NoError(t, err)+ require.Equal(t, []uint64{0, 0, 7, 7}, meta.rootsAt)++ meta.updateErr = corestore.ErrReviewRootsStale+ meta.updates = 0+ _, err = at.Update("1", func(*gitalypb.Review) error { return nil })+ require.ErrorIs(t, err, corestore.ErrReviewRootsStale)+ require.Equal(t, 1, meta.updates)+}+func TestUpdateGivesUpAfterTenConflicts(t *testing.T) {ctx := testhelper.Context(t)meta := &conflictingMeta{updateErr: corestore.ErrReviewVersionConflict}diff --git a/internal/graft/corestore/metastore.go b/internal/graft/corestore/metastore.goindex fba4387..63a8dd0 100644--- a/internal/graft/corestore/metastore.go+++ b/internal/graft/corestore/metastore.go@@ -147,7 +147,14 @@ type MetaStore interface {// InsertReview allocates the next review ID of the repository, calls build// with it and stores the returned review (version 1), atomically. build runs// inside the write transaction and must not call the MetaStore.- InsertReview(ctx context.Context, repoID int64, build func(id string) (*gitalypb.Review, error)) (*gitalypb.Review, error)+ //+ // rootsAt is the sequence of the reference snapshot the review's head and+ // base commits were checked against (doc/graft/PLAN7.md, REVIEW ROOTS RULE+ // R1). When it is not 0 the transaction, under the repository row lock,+ // requires the newest checkpoint's sequence to be at most rootsAt, else+ // ErrReviewRootsStale and nothing is written. Writes that do not check+ // commits (imports) pass 0.+ InsertReview(ctx context.Context, repoID int64, build func(id string) (*gitalypb.Review, error), rootsAt uint64) (*gitalypb.Review, error)// GetReview returns the review and its version, ErrReviewNotFound if unknown.GetReview(ctx context.Context, repoID int64, id string) (*gitalypb.Review, int64, error)// ListReviews returns the reviews of the repository newest first@@ -158,7 +165,14 @@ type MetaStore interface {ListReviews(ctx context.Context, repoID int64, targetBranch string, includeClosed bool, limit int) ([]*gitalypb.Review, error)// UpdateReview stores r if the review is still at version, which becomes// version+1. It returns ErrReviewVersionConflict otherwise.- UpdateReview(ctx context.Context, repoID int64, id string, version int64, r *gitalypb.Review) error+ //+ // rootsAt is as for InsertReview: an update that moves the review's head+ // passes the sequence of the snapshot the new head was checked against, and+ // the transaction then takes the repository row lock first and requires the+ // newest checkpoint's sequence to be at most rootsAt, else+ // ErrReviewRootsStale. Updates that move no commit (comments, verdicts,+ // closes) pass 0 and do not lock the repository row (LOCK ORDER).+ UpdateReview(ctx context.Context, repoID int64, id string, version int64, r *gitalypb.Review, rootsAt uint64) error// CreateCheckpoint records a checkpoint and its reference snapshot in one// transaction, with the database time as its creation time. Its firstdiff --git a/internal/graft/corestore/sqlstore/checkpoint.go b/internal/graft/corestore/sqlstore/checkpoint.goindex 5787967..abc3d44 100644--- a/internal/graft/corestore/sqlstore/checkpoint.go+++ b/internal/graft/corestore/sqlstore/checkpoint.go@@ -137,6 +137,22 @@ func validateCheckpoint(repoID int64, in corestore.CheckpointInput) error {return nil}+// checkRootsAt refuses a review write whose commits were checked against the+// reference snapshot at rootsAt when a newer checkpoint was recorded since+// (doc/graft/PLAN7.md, REVIEW ROOTS RULE R1): that checkpoint may not include+// them. The caller holds the repository row, which CreateCheckpoint takes+// first too, so the two are serialized.+func (s *Store) checkRootsAt(ctx context.Context, tx *sql.Tx, repoID int64, rootsAt uint64) error {+ newest, ok, err := s.newestCheckpoint(ctx, tx, repoID)+ if err != nil {+ return err+ }+ if ok && newest.Seq > rootsAt {+ return fmt.Errorf("checkpoint at changeset %d, review commits checked at %d: %w", newest.Seq, rootsAt, corestore.ErrReviewRootsStale)+ }+ return nil+}+// checkReviewRoots refuses the checkpoint when an open review has a head or// base commit that is not one of the roots the checkpoint's pack includes: the// review was created, or moved its head, during the build (doc/graft/PLAN7.md,diff --git a/internal/graft/corestore/sqlstore/gc_test.go b/internal/graft/corestore/sqlstore/gc_test.goindex 2eabb60..6eabe17 100644--- a/internal/graft/corestore/sqlstore/gc_test.go+++ b/internal/graft/corestore/sqlstore/gc_test.go@@ -249,7 +249,7 @@ func TestCheckpointRequiresOpenReviewRoots(t *testing.T) {head, base := string(oid(10)), string(oid(11))insert := func(r *gitalypb.Review) {- _, err := s.InsertReview(ctx, repo.ID, func(string) (*gitalypb.Review, error) { return r, nil })+ _, err := s.InsertReview(ctx, repo.ID, func(string) (*gitalypb.Review, error) { return r, nil }, 0)require.NoError(t, err)}insert(&gitalypb.Review{TargetBranch: []byte("main"), HeadCommitId: head, BaseCommitId: base})@@ -278,6 +278,63 @@ func TestCheckpointRequiresOpenReviewRoots(t *testing.T) {})}+// TestReviewWritesRequireFreshRoots is REVIEW ROOTS R1 in the store: a review+// write whose commits were checked at a snapshot older than the newest+// checkpoint is refused and writes nothing; rootsAt 0 is unconditioned.+func TestReviewWritesRequireFreshRoots(t *testing.T) {+ forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {+ repo, _ := headRepo(t, ctx, s, "fresh-roots", 3)+ build := func(head string) func(string) (*gitalypb.Review, error) {+ return func(string) (*gitalypb.Review, error) {+ return &gitalypb.Review{TargetBranch: []byte("main"), HeadCommitId: head}, nil+ }+ }++ // Without a checkpoint any snapshot is fresh.+ _, err := s.InsertReview(ctx, repo.ID, build(string(oid(10))), 1)+ require.NoError(t, err)++ _, _, err = s.CreateCheckpoint(ctx, repo.ID, corestore.CheckpointInput{+ Seq: 2, Refs: map[string]corestore.Target{"refs/heads/main": oid(2)}, PackKey: ownedKey(repo.ID, "fresh-roots"),+ Roots: []string{string(oid(10))},+ })+ require.NoError(t, err)++ _, err = s.InsertReview(ctx, repo.ID, build(string(oid(11))), 1)+ require.ErrorIs(t, err, corestore.ErrReviewRootsStale)+ reviews, err := s.ListReviews(ctx, repo.ID, "", true, 0)+ require.NoError(t, err)+ require.Len(t, reviews, 1, "the refused creation wrote nothing")++ for _, rootsAt := range []uint64{2, 3, 0} {+ _, err = s.InsertReview(ctx, repo.ID, build(string(oid(12))), rootsAt)+ require.NoError(t, err, "rootsAt %d", rootsAt)+ }++ r, version, err := s.GetReview(ctx, repo.ID, "1")+ require.NoError(t, err)+ r.HeadCommitId = string(oid(13))+ require.ErrorIs(t, s.UpdateReview(ctx, repo.ID, "1", version, r, 1), corestore.ErrReviewRootsStale)+ unchanged, unchangedVersion, err := s.GetReview(ctx, repo.ID, "1")+ require.NoError(t, err)+ require.Equal(t, version, unchangedVersion, "the refused update wrote nothing")+ require.Equal(t, string(oid(10)), unchanged.GetHeadCommitId())++ require.NoError(t, s.UpdateReview(ctx, repo.ID, "1", version, r, 2))+ r.Title = []byte("a comment moves no commit")+ require.NoError(t, s.UpdateReview(ctx, repo.ID, "1", version+1, r, 0))++ // A head move of a removed repository finds no repository row.+ gone, _ := headRepo(t, ctx, s, "fresh-roots-gone", 1)+ _, err = s.InsertReview(ctx, gone.ID, build(string(oid(14))), 1)+ require.NoError(t, err)+ removed, err := s.DeleteRepository(ctx, gone.Key)+ require.NoError(t, err)+ require.True(t, removed)+ require.ErrorIs(t, s.UpdateReview(ctx, gone.ID, "1", 1, r, 1), corestore.ErrRepositoryNotFound)+ })+}+func TestSyncBoundaryUsesTheDatabaseClock(t *testing.T) {forEachDialect(t, func(t *testing.T, ctx context.Context, s *sqlstore.Store) {repo, _ := headRepo(t, ctx, s, "boundary", 3)@@ -542,7 +599,7 @@ func TestLockOrderConcurrentWriters(t *testing.T) {_, err := a.InsertReview(ctx, repo.ID, func(string) (*gitalypb.Review, error) {return &gitalypb.Review{TargetBranch: []byte("main"), HeadCommitId: string(oid(500))}, nil- })+ }, 0)require.NoError(t, err)const (@@ -556,12 +613,34 @@ func TestLockOrderConcurrentWriters(t *testing.T) {mu sync.MutexcreatedSeqs []uint64rootsRefused int+ rootsStale intunexpected []errorrecordUnexpected = func(err error) { mu.Lock(); unexpected = append(unexpected, err); mu.Unlock() }nodes = []*sqlstore.Store{a, b}g errgroup.Group)+ // writeRoots runs a review write that moves commits as the review service+ // does (REVIEW ROOTS R1): with the sequence of a fresh snapshot, again+ // after ErrReviewRootsStale. Its own bound is generous: the test counts+ // the conflicts instead of failing on the service's third one.+ writeRoots := func(s *sqlstore.Store, write func(rootsAt uint64) error) error {+ for range 20 {+ _, rootsAt, err := s.Refs(ctx, repo.ID)+ if err != nil {+ return err+ }+ err = write(rootsAt)+ if !errors.Is(err, corestore.ErrReviewRootsStale) {+ return err+ }+ mu.Lock()+ rootsStale+++ mu.Unlock()+ }+ return errors.New("the review write kept conflicting with checkpoints")+ }+for n, s := range nodes {g.Go(func() error {for i := range landsPerNode {@@ -575,8 +654,11 @@ func TestLockOrderConcurrentWriters(t *testing.T) {g.Go(func() error {for i := range reviewsPerNode {head := string(oid(2000 + 100*n + i))- if _, err := s.InsertReview(ctx, repo.ID, func(string) (*gitalypb.Review, error) {- return &gitalypb.Review{TargetBranch: []byte("main"), HeadCommitId: head}, nil+ if err := writeRoots(s, func(rootsAt uint64) error {+ _, err := s.InsertReview(ctx, repo.ID, func(string) (*gitalypb.Review, error) {+ return &gitalypb.Review{TargetBranch: []byte("main"), HeadCommitId: head}, nil+ }, rootsAt)+ return err}); err != nil {recordUnexpected(fmt.Errorf("insert review: %w", err))}@@ -585,6 +667,30 @@ func TestLockOrderConcurrentWriters(t *testing.T) {})}+ // Head moves of review 1: the repository row first, then the version+ // compare-and-swap of the review row.+ g.Go(func() error {+ for done := 0; done < updates; {+ err := writeRoots(a, func(rootsAt uint64) error {+ r, version, err := a.GetReview(ctx, repo.ID, "1")+ if err != nil {+ return err+ }+ r.HeadCommitId = string(oid(3000 + done))+ return a.UpdateReview(ctx, repo.ID, "1", version, r, rootsAt)+ })+ switch {+ case err == nil:+ done+++ case errors.Is(err, corestore.ErrReviewVersionConflict):+ default:+ recordUnexpected(fmt.Errorf("move review head: %w", err))+ return nil+ }+ }+ return nil+ })+// Updates of one review that do not move its commits (a comment): a// version compare-and-swap on the review row only.g.Go(func() error {@@ -595,7 +701,7 @@ func TestLockOrderConcurrentWriters(t *testing.T) {return nil}r.Title = []byte(fmt.Sprintf("comment %d", done))- switch err := b.UpdateReview(ctx, repo.ID, "1", version, r); {+ switch err := b.UpdateReview(ctx, repo.ID, "1", version, r, 0); {case err == nil:done++case errors.Is(err, corestore.ErrReviewVersionConflict):@@ -607,8 +713,20 @@ func TestLockOrderConcurrentWriters(t *testing.T) {return nil})- // Checkpoints at the head, with the open reviews read before the commit as- // roots: a review created in between makes the commit refuse (R2).+ // Checkpoints at the head. Their roots include every head the review+ // writers above will record (R2 requires a superset of the open+ // reviews' commits), so that checkpoints commit while the R1 writes run+ // instead of being refused by them; R2's refusal is tested+ // deterministically in TestCheckpointRequiresOpenReviewRoots.+ roots := []string{string(oid(500))}+ for n := range nodes {+ for i := range reviewsPerNode {+ roots = append(roots, string(oid(2000+100*n+i)))+ }+ }+ for done := range updates {+ roots = append(roots, string(oid(3000+done)))+ }g.Go(func() error {for range checkpoints {refs, head, err := a.Refs(ctx, repo.ID)@@ -616,15 +734,6 @@ func TestLockOrderConcurrentWriters(t *testing.T) {recordUnexpected(err)return nil}- open, err := a.ListReviews(ctx, repo.ID, "", false, 0)- if err != nil {- recordUnexpected(err)- return nil- }- var roots []string- for _, r := range open {- roots = append(roots, r.GetHeadCommitId())- }cp, created, err := a.CreateCheckpoint(ctx, repo.ID, corestore.CheckpointInput{Seq: head, Refs: refs, PackKey: ownedKey(repo.ID, fmt.Sprint("checkpoint ", head)), Roots: roots,@@ -689,6 +798,11 @@ func TestLockOrderConcurrentWriters(t *testing.T) {recorded = append(recorded, cp.Seq)}require.Equal(t, createdSeqs, recorded)- t.Logf("checkpoints created %d, refused because a review changed %d", len(createdSeqs), rootsRefused)+ require.Zero(t, rootsRefused, "the roots include every review commit")+ t.Logf("checkpoints created %d; review writes refused after a newer checkpoint %d", len(createdSeqs), rootsStale)++ r, _, err := a.GetReview(ctx, repo.ID, "1")+ require.NoError(t, err)+ require.Equal(t, string(oid(3000+updates-1)), r.GetHeadCommitId(), "every head move applied")})}diff --git a/internal/graft/corestore/sqlstore/history_test.go b/internal/graft/corestore/sqlstore/history_test.goindex 02599b8..c3739d0 100644--- a/internal/graft/corestore/sqlstore/history_test.go+++ b/internal/graft/corestore/sqlstore/history_test.go@@ -85,7 +85,7 @@ func TestImportHistory(t *testing.T) {inserted, err := s.InsertReview(ctx, repo.ID, func(id string) (*gitalypb.Review, error) {return newReview("next", "main", gitalypb.Review_STATE_OPEN_UNSPECIFIED), nil- })+ }, 0)require.NoError(t, err)require.Equal(t, "8", inserted.GetId(), "the next review ID follows the largest imported one")@@ -140,7 +140,7 @@ func TestImportHistoryReplacesReviewsChangedInShadowMode(t *testing.T) {// Review 2 was changed in the database after the file (authoritative mode).dbChanged := review("2", "changed in the database", 0, 2*time.Hour)- require.NoError(t, s.UpdateReview(ctx, repo.ID, "2", 1, dbChanged))+ require.NoError(t, s.UpdateReview(ctx, repo.ID, "2", 1, dbChanged, 0))closed := review("1", "closed in shadow mode", 0, time.Hour)closed.State = gitalypb.Review_STATE_CLOSED@@ -183,7 +183,7 @@ func TestImportHistoryReplacesReviewsChangedInShadowMode(t *testing.T) {require.Equal(t, []string{"5", "4", "3", "2"}, openIDs)// An update based on the version before the import re-reads the review.- require.ErrorIs(t, s.UpdateReview(ctx, repo.ID, "1", 1, review("1", "stale", 0, 4*time.Hour)), corestore.ErrReviewVersionConflict)+ require.ErrorIs(t, s.UpdateReview(ctx, repo.ID, "1", 1, review("1", "stale", 0, 4*time.Hour), 0), corestore.ErrReviewVersionConflict)// Idempotent: the same file reviews write nothing new._, reviews, err = s.ImportHistory(ctx, repo.ID, nil, []*gitalypb.Review{closed, review("5", "created in shadow mode", 3*time.Hour, 3*time.Hour)})diff --git a/internal/graft/corestore/sqlstore/review.go b/internal/graft/corestore/sqlstore/review.goindex b111126..7c1ef9d 100644--- a/internal/graft/corestore/sqlstore/review.go+++ b/internal/graft/corestore/sqlstore/review.go@@ -41,16 +41,22 @@ func unmarshalReview(body string) (*gitalypb.Review, error) {}// InsertReview implements corestore.MetaStore.-func (s *Store) InsertReview(ctx context.Context, repoID int64, build func(id string) (*gitalypb.Review, error)) (*gitalypb.Review, error) {+func (s *Store) InsertReview(ctx context.Context, repoID int64, build func(id string) (*gitalypb.Review, error), rootsAt uint64) (*gitalypb.Review, error) {var stored *gitalypb.Reviewerr := s.withTx(ctx, nil, func(tx *sql.Tx) error {stored = nil+ // LOCK ORDER (doc/graft/PLAN7.md): the repository row first.repo, err := s.selectRepository(ctx, tx, "repo_id = ?", []any{repoID}, true)if err != nil {return err}+ if rootsAt != 0 {+ if err := s.checkRootsAt(ctx, tx, repoID, rootsAt); err != nil {+ return err+ }+ }id := repo.nextReviewIDbuilt, err := build(fmt.Sprintf("%d", id))@@ -164,13 +170,25 @@ func (s *Store) ListReviews(ctx context.Context, repoID int64, targetBranch stri}// UpdateReview implements corestore.MetaStore.-func (s *Store) UpdateReview(ctx context.Context, repoID int64, id string, version int64, r *gitalypb.Review) error {+func (s *Store) UpdateReview(ctx context.Context, repoID int64, id string, version int64, r *gitalypb.Review, rootsAt uint64) error {reviewID, ok := parseReviewID(id)if !ok {return corestore.ErrReviewNotFound}return s.withTx(ctx, nil, func(tx *sql.Tx) error {+ // An update that moves the review's head serializes with checkpoint+ // commits: LOCK ORDER, the repository row first. The others touch only+ // the review row and never lock the repository row.+ if rootsAt != 0 {+ if _, err := s.selectRepository(ctx, tx, "repo_id = ?", []any{repoID}, true); err != nil {+ return err+ }+ if err := s.checkRootsAt(ctx, tx, repoID, rootsAt); err != nil {+ return err+ }+ }+var current int64err := tx.QueryRowContext(ctx, s.bind("SELECT version FROM graft_reviews WHERE repo_id = ? AND review_id = ?"),repoID, reviewID).Scan(¤t)diff --git a/internal/graft/corestore/sqlstore/review_test.go b/internal/graft/corestore/sqlstore/review_test.goindex 21ab585..417e55b 100644--- a/internal/graft/corestore/sqlstore/review_test.go+++ b/internal/graft/corestore/sqlstore/review_test.go@@ -19,7 +19,7 @@ func insertReview(t *testing.T, ctx context.Context, s *sqlstore.Store, repoID iinserted, err := s.InsertReview(ctx, repoID, func(id string) (*gitalypb.Review, error) {return r, nil- })+ }, 0)require.NoError(t, err)return inserted}@@ -49,17 +49,17 @@ func TestReviewsCRUD(t *testing.T) {// Updates succeed on the current version only.updated := newReview("two, edited", "release", gitalypb.Review_STATE_OPEN_UNSPECIFIED)- require.NoError(t, s.UpdateReview(ctx, repo.ID, "2", 1, updated))+ require.NoError(t, s.UpdateReview(ctx, repo.ID, "2", 1, updated, 0))_, version, err = s.GetReview(ctx, repo.ID, "2")require.NoError(t, err)require.Equal(t, int64(2), version)- err = s.UpdateReview(ctx, repo.ID, "2", 1, updated)+ err = s.UpdateReview(ctx, repo.ID, "2", 1, updated, 0)require.ErrorIs(t, err, corestore.ErrReviewVersionConflict)- err = s.UpdateReview(ctx, repo.ID, "99", 1, updated)+ err = s.UpdateReview(ctx, repo.ID, "99", 1, updated, 0)require.ErrorIs(t, err, corestore.ErrReviewNotFound)- err = s.UpdateReview(ctx, repo.ID, "abc", 1, updated)+ err = s.UpdateReview(ctx, repo.ID, "abc", 1, updated, 0)require.ErrorIs(t, err, corestore.ErrReviewNotFound)// Listing is newest first, filtered by target and by state.@@ -113,7 +113,7 @@ func TestReviewsConcurrentUpdatesLoseNothing(t *testing.T) {next := fmt.Sprintf("%d", i)current.Title = []byte(next)- err = store.UpdateReview(ctx, repo.ID, "1", version, current)+ err = store.UpdateReview(ctx, repo.ID, "1", version, current, 0)if errors.Is(err, corestore.ErrReviewVersionConflict) {continue}diff --git a/internal/graft/corestore/sqlstore/robustness_test.go b/internal/graft/corestore/sqlstore/robustness_test.goindex 1fde9ca..a43033c 100644--- a/internal/graft/corestore/sqlstore/robustness_test.go+++ b/internal/graft/corestore/sqlstore/robustness_test.go@@ -79,7 +79,7 @@ func TestWritesHonourDeadlineWhileLocked(t *testing.T) {call: func(ctx context.Context) error {_, err := s.InsertReview(ctx, repo.ID, func(string) (*gitalypb.Review, error) {return newReview("t", "refs/heads/main", gitalypb.Review_STATE_OPEN_UNSPECIFIED), nil- })+ }, 0)return err},},diff --git a/internal/graft/corestore/sqlstore/store_test.go b/internal/graft/corestore/sqlstore/store_test.goindex 3a1aaea..decd5c1 100644--- a/internal/graft/corestore/sqlstore/store_test.go+++ b/internal/graft/corestore/sqlstore/store_test.go@@ -454,7 +454,7 @@ func TestRegisterWithHistory(t *testing.T) {inserted, err := s.InsertReview(ctx, repo.ID, func(id string) (*gitalypb.Review, error) {require.Equal(t, "4", id)return newReview("new", "main", gitalypb.Review_STATE_OPEN_UNSPECIFIED), nil- })+ }, 0)require.NoError(t, err)require.Equal(t, "4", inserted.GetId())@@ -491,7 +491,7 @@ func TestDeleteCascadesAndReRegisterGetsNewID(t *testing.T) {require.NoError(t, err)_, err = s.InsertReview(ctx, first.ID, func(id string) (*gitalypb.Review, error) {return newReview("doomed", "main", gitalypb.Review_STATE_OPEN_UNSPECIFIED), nil- })+ }, 0)require.NoError(t, err)deleted, err := s.DeleteRepository(ctx, key)diff --git a/internal/graft/corestore/types.go b/internal/graft/corestore/types.goindex 1ab1a21..a15f789 100644--- a/internal/graft/corestore/types.go+++ b/internal/graft/corestore/types.go@@ -232,6 +232,11 @@ var (// include: the review was created or moved during the build// (doc/graft/PLAN7.md, REVIEW ROOTS RULE R2).ErrCheckpointRootsChanged = errors.New("storage core: the open reviews changed during the checkpoint build")+ // ErrReviewRootsStale is returned by MetaStore.InsertReview and UpdateReview+ // when a checkpoint newer than the snapshot the review's commits were checked+ // at was recorded in between: the caller checks the commits again against a+ // new snapshot (doc/graft/PLAN7.md, REVIEW ROOTS RULE R1).+ ErrReviewRootsStale = errors.New("storage core: a checkpoint was recorded after the review's commits were checked")// ErrPackDeadline is returned when a pack upload, or the commit of the row// that references it, would end after the write deadline: garbage collection// may then already treat the pack as an orphan (doc/graft/PLAN7.md, PACK