diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-02-11 17:07:37 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-02-16 09:59:59 +0300 |
commit | 28292e0bcf04e496404798c72c2e3dc94c226de6 (patch) | |
tree | c7bcf6be149211a1edf20db1967778bb0fad4be8 | |
parent | bfafa50e7a63dfdf2f9eb32fab9875168bff5008 (diff) |
repository: Move writing of commit-graph into housekeeping package
We want to allow for OptimizeRepository to be called without invoking an
RPC in order to allow us to call it in more contexts. As a preparatory
step, we thus have to move all functionality which is invoked by it into
a package that is independent of gRPC services.
Move the writing of the commit-graph into the housekeeping package as
part of this move.
-rw-r--r-- | internal/git/housekeeping/commit_graph.go | 64 | ||||
-rw-r--r-- | internal/gitaly/service/repository/commit_graph.go | 74 | ||||
-rw-r--r-- | internal/gitaly/service/repository/gc.go | 2 | ||||
-rw-r--r-- | internal/gitaly/service/repository/repack.go | 3 |
4 files changed, 73 insertions, 70 deletions
diff --git a/internal/git/housekeeping/commit_graph.go b/internal/git/housekeeping/commit_graph.go new file mode 100644 index 000000000..dcdd2cb2e --- /dev/null +++ b/internal/git/housekeeping/commit_graph.go @@ -0,0 +1,64 @@ +package housekeeping + +import ( + "bytes" + "context" + "errors" + "fmt" + "os" + "path/filepath" + + "gitlab.com/gitlab-org/gitaly/v14/internal/git" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/stats" + "gitlab.com/gitlab-org/gitaly/v14/internal/helper" +) + +// WriteCommitGraph updates the commit-graph in the given repository. The commit-graph is updated +// incrementally, except in the case where it doesn't exist yet or in case it is detected that the +// commit-graph is missing bloom filters. +func WriteCommitGraph(ctx context.Context, repo *localrepo.Repo) error { + repoPath, err := repo.Path() + if err != nil { + return err + } + + missingBloomFilters := true + if _, err := os.Stat(filepath.Join(repoPath, stats.CommitGraphRelPath)); err != nil { + if !errors.Is(err, os.ErrNotExist) { + return helper.ErrInternal(fmt.Errorf("remove commit graph file: %w", err)) + } + + // objects/info/commit-graph file doesn't exists + // check if commit-graph chain exists and includes Bloom filters + if missingBloomFilters, err = stats.IsMissingBloomFilters(repoPath); err != nil { + return helper.ErrInternal(fmt.Errorf("should remove commit graph chain: %w", err)) + } + } + + flags := []git.Option{ + git.Flag{Name: "--reachable"}, + git.Flag{Name: "--changed-paths"}, // enables Bloom filters + git.ValueFlag{Name: "--size-multiple", Value: "4"}, + } + + if missingBloomFilters { + // if commit graph doesn't use Bloom filters we instruct operation to replace + // existent commit graph with the new one + // https://git-scm.com/docs/git-commit-graph#Documentation/git-commit-graph.txt-emwriteem + flags = append(flags, git.Flag{Name: "--split=replace"}) + } else { + flags = append(flags, git.Flag{Name: "--split"}) + } + + var stderr bytes.Buffer + if err := repo.ExecAndWait(ctx, git.SubSubCmd{ + Name: "commit-graph", + Action: "write", + Flags: flags, + }, git.WithStderr(&stderr)); err != nil { + return helper.ErrInternalf("writing commit-graph: %s: %v", err, stderr.String()) + } + + return nil +} diff --git a/internal/gitaly/service/repository/commit_graph.go b/internal/gitaly/service/repository/commit_graph.go index eaa3c9cbf..b329bc099 100644 --- a/internal/gitaly/service/repository/commit_graph.go +++ b/internal/gitaly/service/repository/commit_graph.go @@ -1,16 +1,9 @@ package repository import ( - "bytes" "context" - "errors" - "fmt" - "os" - "path/filepath" - "gitlab.com/gitlab-org/gitaly/v14/internal/git" - "gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo" - "gitlab.com/gitlab-org/gitaly/v14/internal/git/stats" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/housekeeping" "gitlab.com/gitlab-org/gitaly/v14/internal/helper" "gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb" ) @@ -22,68 +15,13 @@ func (s *server) WriteCommitGraph( ) (*gitalypb.WriteCommitGraphResponse, error) { repo := s.localrepo(in.GetRepository()) - if err := writeCommitGraph(ctx, repo, in.GetSplitStrategy()); err != nil { - return nil, err - } - - return &gitalypb.WriteCommitGraphResponse{}, nil -} - -func writeCommitGraph( - ctx context.Context, - repo *localrepo.Repo, - splitStrategy gitalypb.WriteCommitGraphRequest_SplitStrategy, -) error { - repoPath, err := repo.Path() - if err != nil { - return err - } - - missingBloomFilters := true - if _, err := os.Stat(filepath.Join(repoPath, stats.CommitGraphRelPath)); err != nil { - if !errors.Is(err, os.ErrNotExist) { - return helper.ErrInternal(fmt.Errorf("remove commit graph file: %w", err)) - } - - // objects/info/commit-graph file doesn't exists - // check if commit-graph chain exists and includes Bloom filters - if missingBloomFilters, err = stats.IsMissingBloomFilters(repoPath); err != nil { - return helper.ErrInternal(fmt.Errorf("should remove commit graph chain: %w", err)) - } - } - - flags := []git.Option{ - git.Flag{Name: "--reachable"}, - git.Flag{Name: "--changed-paths"}, // enables Bloom filters - } - - if missingBloomFilters { - // if commit graph doesn't use Bloom filters we instruct operation to replace - // existent commit graph with the new one - // https://git-scm.com/docs/git-commit-graph#Documentation/git-commit-graph.txt-emwriteem - flags = append(flags, git.Flag{Name: "--split=replace"}) - } else { - flags = append(flags, git.Flag{Name: "--split"}) + if in.GetSplitStrategy() != gitalypb.WriteCommitGraphRequest_SizeMultiple { + return nil, helper.ErrInvalidArgumentf("unsupported split strategy: %v", in.GetSplitStrategy()) } - switch splitStrategy { - case gitalypb.WriteCommitGraphRequest_SizeMultiple: - flags = append(flags, - // this flag has no effect if '--split=replace' is used - git.ValueFlag{Name: "--size-multiple", Value: "4"}, - ) - default: - return helper.ErrInvalidArgumentf("unsupported split strategy: %v", splitStrategy) - } - - var stderr bytes.Buffer - if err := repo.ExecAndWait(ctx, git.SubSubCmd{ - Name: "commit-graph", - Action: "write", - Flags: flags, - }, git.WithStderr(&stderr)); err != nil { - return helper.ErrInternalf("writing commit-graph: %s: %v", err, stderr.String()) + if err := housekeeping.WriteCommitGraph(ctx, repo); err != nil { + return nil, err } - return nil + return &gitalypb.WriteCommitGraphResponse{}, nil } diff --git a/internal/gitaly/service/repository/gc.go b/internal/gitaly/service/repository/gc.go index 13adfc326..cd609deeb 100644 --- a/internal/gitaly/service/repository/gc.go +++ b/internal/gitaly/service/repository/gc.go @@ -46,7 +46,7 @@ func (s *server) GarbageCollect(ctx context.Context, in *gitalypb.GarbageCollect return nil, err } - if err := writeCommitGraph(ctx, repo, gitalypb.WriteCommitGraphRequest_SizeMultiple); err != nil { + if err := housekeeping.WriteCommitGraph(ctx, repo); err != nil { return nil, err } diff --git a/internal/gitaly/service/repository/repack.go b/internal/gitaly/service/repository/repack.go index a3f428650..6d28084ed 100644 --- a/internal/gitaly/service/repository/repack.go +++ b/internal/gitaly/service/repository/repack.go @@ -8,6 +8,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "gitlab.com/gitlab-org/gitaly/v14/internal/git" + "gitlab.com/gitlab-org/gitaly/v14/internal/git/housekeeping" "gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo" "gitlab.com/gitlab-org/gitaly/v14/internal/git/stats" "gitlab.com/gitlab-org/gitaly/v14/internal/helper" @@ -85,7 +86,7 @@ func repack(ctx context.Context, repo *localrepo.Repo, cfg repackCommandConfig) return err } - if err := writeCommitGraph(ctx, repo, gitalypb.WriteCommitGraphRequest_SizeMultiple); err != nil { + if err := housekeeping.WriteCommitGraph(ctx, repo); err != nil { return err } |