1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package repository
import (
"context"
"fmt"
"math"
"runtime"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/repository"
"gitlab.com/gitlab-org/gitaly/internal/git/stats"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
repackCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitaly_repack_total",
Help: "Counter of Git repack operations",
},
[]string{"bitmap"},
)
)
func init() {
prometheus.MustRegister(repackCounter)
}
// log2Threads returns the log-2 number of threads based on the number of
// provided CPUs. This prevents repacking operations from exhausting all
// available CPUs and increasing request latency
func log2Threads(numCPUs int) git.ValueFlag {
n := math.Max(1, math.Floor(math.Log2(float64(numCPUs))))
return git.ValueFlag{Name: "--threads", Value: fmt.Sprint(n)}
}
func (s *server) RepackFull(ctx context.Context, in *gitalypb.RepackFullRequest) (*gitalypb.RepackFullResponse, error) {
options := []git.Option{
git.Flag{Name: "-A"},
git.Flag{Name: "--pack-kept-objects"},
git.Flag{Name: "-l"},
log2Threads(runtime.NumCPU()),
}
if err := s.repackCommand(ctx, in.GetRepository(), in.GetCreateBitmap(), options...); err != nil {
return nil, err
}
return &gitalypb.RepackFullResponse{}, nil
}
func (s *server) RepackIncremental(ctx context.Context, in *gitalypb.RepackIncrementalRequest) (*gitalypb.RepackIncrementalResponse, error) {
if err := s.repackCommand(ctx, in.GetRepository(), false); err != nil {
return nil, err
}
return &gitalypb.RepackIncrementalResponse{}, nil
}
func (s *server) repackCommand(ctx context.Context, repo repository.GitRepo, bitmap bool, args ...git.Option) error {
cmd, err := s.gitCmdFactory.New(ctx, repo,
git.SubCmd{
Name: "repack",
Flags: append([]git.Option{git.Flag{Name: "-d"}}, args...),
},
git.WithConfig(repackConfig(ctx, bitmap)...),
)
if err != nil {
if _, ok := status.FromError(err); ok {
return err
}
return status.Errorf(codes.Internal, err.Error())
}
if err := cmd.Wait(); err != nil {
return status.Errorf(codes.Internal, err.Error())
}
stats.LogObjectsInfo(ctx, s.gitCmdFactory, repo)
return nil
}
func repackConfig(ctx context.Context, bitmap bool) []git.ConfigPair {
config := []git.ConfigPair{
git.ConfigPair{Key: "pack.island", Value: "r(e)fs/heads"},
git.ConfigPair{Key: "pack.island", Value: "r(e)fs/tags"},
git.ConfigPair{Key: "pack.islandCore", Value: "e"},
git.ConfigPair{Key: "repack.useDeltaIslands", Value: "true"},
}
if bitmap {
config = append(config, git.ConfigPair{Key: "repack.writeBitmaps", Value: "true"})
config = append(config, git.ConfigPair{Key: "pack.writeBitmapHashCache", Value: "true"})
} else {
config = append(config, git.ConfigPair{Key: "repack.writeBitmaps", Value: "false"})
}
repackCounter.WithLabelValues(fmt.Sprint(bitmap)).Inc()
return config
}
|