Git designed for AI agents.
e2e: the review roots backstop without waiting for a minute of the clock
Federico Elia041e6cc86286
diff --git a/internal/cli/gitaly/daily_optimizer_test.go b/internal/cli/gitaly/daily_optimizer_test.gonew file mode 100644index 0000000..078cf51--- /dev/null+++ b/internal/cli/gitaly/daily_optimizer_test.go@@ -0,0 +1,71 @@+package gitaly++import (+ "context"+ "errors"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/feder-cr/git-ai/internal/git/localrepo"+ "github.com/feder-cr/git-ai/internal/gitaly/storage"+ "github.com/feder-cr/git-ai/internal/log"+ "github.com/feder-cr/git-ai/internal/testhelper"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+)++// TestDailyOptimizer checks the job of the daily maintenance for one repository: the backstop of+// the review roots runs first, on the repository's local repo, and a failed backstop skips the+// housekeeping that could prune the commits it protects.+func TestDailyOptimizer(t *testing.T) {+ t.Parallel()++ ctx := testhelper.Context(t)+ repo := &gitalypb.Repository{StorageName: "default", RelativePath: "repo.git"}+ local := &localrepo.Repo{}++ for _, tc := range []struct {+ desc string+ backstopErr error+ expectedCalls []string+ expectedErr error+ }{+ {+ desc: "the backstop runs before the housekeeping",+ expectedCalls: []string{"backstop", "optimize"},+ },+ {+ desc: "a failed backstop skips the housekeeping",+ backstopErr: errors.New("backfill failed"),+ expectedCalls: []string{"backstop"},+ expectedErr: errors.New("backfill failed"),+ },+ } {+ t.Run(tc.desc, func(t *testing.T) {+ t.Parallel()++ var calls []string+ optimizer := dailyOptimizer(+ func(_ log.Logger, r storage.Repository) *localrepo.Repo {+ require.Equal(t, repo, r)+ return local+ },+ func(_ context.Context, _ log.Logger, l *localrepo.Repo, r storage.Repository) error {+ require.Same(t, local, l)+ require.Equal(t, repo, r)+ calls = append(calls, "backstop")+ return tc.backstopErr+ },+ func(_ context.Context, l *localrepo.Repo) error {+ require.Same(t, local, l)+ calls = append(calls, "optimize")+ return nil+ },+ )++ err := optimizer.OptimizeRepository(ctx, testhelper.SharedLogger(t), repo)+ require.Equal(t, tc.expectedErr, err)+ require.Equal(t, tc.expectedCalls, calls)+ })+ }+}diff --git a/internal/cli/gitaly/serve.go b/internal/cli/gitaly/serve.goindex be0cf21..efac6ac 100644--- a/internal/cli/gitaly/serve.go+++ b/internal/cli/gitaly/serve.go@@ -883,18 +883,17 @@ func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger) error {shutdownWorkers, err := maintenance.StartWorkers(ctx,logger,- maintenance.DailyOptimizationWorker(cfg, maintenance.OptimizerFunc(func(ctx context.Context, logger log.Logger, repo storage.Repository) error {- local := localrepo.New(logger, locator, gitCmdFactory, catfileCache, repo)- // Graft: the backstop of the review roots, in every mode, before the- // housekeeping that prunes unreferenced objects (doc/graft/PLAN7.md,- // REVIEW ROOTS RULE): the commits of an open review written without- // its keep-around references are referenced before a prune could- // take them.- if err := housekeepReviewRoots(ctx, logger, storageCore, storageSyncer, local, repo); err != nil {- return err- }- return housekeepingManager.OptimizeRepository(ctx, local)- })),+ maintenance.DailyOptimizationWorker(cfg, dailyOptimizer(+ func(logger log.Logger, repo storage.Repository) *localrepo.Repo {+ return localrepo.New(logger, locator, gitCmdFactory, catfileCache, repo)+ },+ func(ctx context.Context, logger log.Logger, local *localrepo.Repo, repo storage.Repository) error {+ return housekeepReviewRoots(ctx, logger, storageCore, storageSyncer, local, repo)+ },+ func(ctx context.Context, local *localrepo.Repo) error {+ return housekeepingManager.OptimizeRepository(ctx, local)+ },+ )),)if err != nil {return fmt.Errorf("initialize auxiliary workers: %w", err)@@ -935,6 +934,25 @@ func run(appCtx *cli.Command, cfg config.Cfg, logger log.Logger) error {return waitErr}+// dailyOptimizer is the job of the daily maintenance for one repository: the backstop of the+// review roots, in every mode, then the housekeeping that prunes unreferenced objects+// (doc/graft/PLAN7.md, REVIEW ROOTS RULE). The commits of an open review written without its+// keep-around references are referenced before a prune could take them, so a failed backstop+// skips the housekeeping of the repository.+func dailyOptimizer(+ newLocal func(log.Logger, storage.Repository) *localrepo.Repo,+ backstop func(context.Context, log.Logger, *localrepo.Repo, storage.Repository) error,+ optimize func(context.Context, *localrepo.Repo) error,+) maintenance.Optimizer {+ return maintenance.OptimizerFunc(func(ctx context.Context, logger log.Logger, repo storage.Repository) error {+ local := newLocal(logger, repo)+ if err := backstop(ctx, logger, local, repo); err != nil {+ return err+ }+ return optimize(ctx, local)+ })+}+// housekeepReviewRoots runs the backstop of the review roots before the daily// housekeeping of a repository: it writes the keep-around references that// open reviews lack and deletes those no open review uses (in authoritativediff --git a/internal/graft/e2e/storage_localrefs_test.go b/internal/graft/e2e/storage_localrefs_test.goindex 921bee6..a3100f4 100644--- a/internal/graft/e2e/storage_localrefs_test.go+++ b/internal/graft/e2e/storage_localrefs_test.go@@ -1,7 +1,6 @@package e2eimport (- "bytes""context""encoding/json""errors"@@ -30,7 +29,6 @@ import ("github.com/feder-cr/git-ai/internal/graft/corestore/coretest""github.com/feder-cr/git-ai/internal/graft/corestore/localrefs""github.com/feder-cr/git-ai/internal/graft/corestore/sqlstore"- "github.com/feder-cr/git-ai/internal/helper/duration""github.com/feder-cr/git-ai/internal/testhelper""github.com/feder-cr/git-ai/internal/testhelper/testcfg""github.com/feder-cr/git-ai/proto/go/gitalypb"@@ -500,35 +498,22 @@ func TestStorageBackgroundMaintenanceCheckpoints(t *testing.T) {n.requireServed(t, rel, written)}-// TestStorageHousekeepingBackfillsReviewRoots runs the daily housekeeping of-// the real server in authoritative mode (doc/graft/PLAN7.md, REVIEW ROOTS RULE,-// the backstop of the daily worker): a review written without the keep-around-// references of its commits (a review from before the rule) gets them, in the-// database and on the node, through the ordinary write path.-func TestStorageHousekeepingBackfillsReviewRoots(t *testing.T) {+// TestStorageRealNodeBackfillsReviewRoots runs the backstop of the review+// roots in the real server binary in authoritative mode (doc/graft/PLAN7.md,+// REVIEW ROOTS RULE): a review written without the keep-around references of+// its commits (a review from before the rule) gets them, in the database and+// on the node, through the ordinary write path, and the server counts them.+// The backstop is the one of the daily housekeeping and of every checkpoint+// (reviewroots.Sweeper.Housekeep); the test runs it through the operator's+// checkpoint, which runs it at once, rather than waiting for a minute of the+// clock. That the daily job runs it before the housekeeping is+// TestDailyOptimizer (internal/cli/gitaly); the checkpoint's use of it is+// TestStorageCheckpointBackfillsReviewRoots.+func TestStorageRealNodeBackfillsReviewRoots(t *testing.T) {t.Parallel()- // The daily worker starts at a minute of the clock, chosen after the slow- // part of the setup (the node's binaries), two minutes ahead: the server- // must schedule it before that minute, which is checked below.n := newRealNode(t)- start := time.Now().Add(3 * time.Minute).Truncate(time.Minute)- n.cfg.DailyMaintenance = config.DailyJob{- Hour: uint(start.Hour()),- Minute: uint(start.Minute()),- Duration: duration.Duration(5 * time.Minute),- Storages: []string{n.cfg.Storages[0].Name},- }- n.cfgPath = testcfg.WriteTemporaryGitalyConfigFile(t, n.cfg)n.start(t)- require.Eventually(t, func() bool {- data, err := os.ReadFile(n.logPath)- require.NoError(t, err)- return bytes.Contains(data, []byte("maintenance: daily scheduled"))- }, time.Minute, 100*time.Millisecond, "the server schedules its daily maintenance")- require.True(t, time.Now().Before(start),- "the server scheduled its daily maintenance after %s, the minute the test chose, so for tomorrow: it started slower than the test's two-minute margin",- start.Format(time.TimeOnly))rel := n.createRepository(t)onA := n.agent(t, rel)@@ -549,16 +534,21 @@ func TestStorageHousekeepingBackfillsReviewRoots(t *testing.T) {require.NoError(t, err)roots := []string{corestore.KeepAroundRef(head), corestore.KeepAroundRef(base)}- require.Eventually(t, func() bool {- refs, _, err := meta.Refs(n.ctx, state.RepositoryID)- require.NoError(t, err)- for _, root := range roots {- if refs[root].Absent() {- return false- }- }- return true- }, time.Until(start)+3*time.Minute, time.Second, "the daily housekeeping backfills the roots")+ refs, _, err := meta.Refs(n.ctx, state.RepositoryID)+ require.NoError(t, err)+ for _, root := range roots {+ require.True(t, refs[root].Absent(), "the review has no roots yet: %s", root)+ }++ // The checkpoint runs the backstop before it reads the reference snapshot.+ _, err = n.operator(t, rel).StorageCheckpoint(n.ctx)+ require.NoError(t, err)++ refs, _, err = meta.Refs(n.ctx, state.RepositoryID)+ require.NoError(t, err)+ for _, root := range roots {+ require.False(t, refs[root].Absent(), "the backstop wrote %s", root)+ }require.GreaterOrEqual(t, n.counter(t, "graft_review_roots_backfilled_total"), float64(len(roots)))for _, root := range roots {require.NotEmpty(t, n.git(t, rel, "rev-parse", "--verify", root), "the node has %s", root)