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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
package cache
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"sync"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitaly/v16/internal/dontpanic"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm"
"gitlab.com/gitlab-org/gitaly/v16/internal/safe"
"gitlab.com/gitlab-org/gitaly/v16/proto/go/gitalypb"
"google.golang.org/protobuf/proto"
)
// maps a cache path to the number of active writers
type activeFiles struct {
*sync.Mutex
m map[string]int
}
// trackFile returns a function that indicates if the current
// writing of a file is the last known one, which
// would indicate the current write is the "winner".
func (af activeFiles) trackFile(path string) func() bool {
af.Lock()
defer af.Unlock()
af.m[path]++
return func() bool {
af.Lock()
defer af.Unlock()
af.m[path]--
winner := af.m[path] == 0
if winner {
delete(af.m, path) // reclaim memory
}
return winner
}
}
type cacheConfig struct {
disableMoveAndClear bool // only used to disable move and clear in tests
disableWalker bool // only used to disable object walker in tests
}
// Option is an option for the cache.
type Option func(*cacheConfig)
// withDisabledMoveAndClear disables the initial move and cleanup of preexisting cache directories.
// This option is only for test purposes.
func withDisabledMoveAndClear() Option {
return func(cfg *cacheConfig) {
cfg.disableMoveAndClear = true
}
}
// withDisabledWalker disables the cache walker which cleans up the cache asynchronously. This
// option is only for test purposes.
func withDisabledWalker() Option {
return func(cfg *cacheConfig) {
cfg.disableWalker = true
}
}
// DiskCache stores and retrieves byte streams for repository related RPCs
type DiskCache struct {
locator storage.Locator
storages []config.Storage
keyer leaseKeyer
af activeFiles
cacheConfig cacheConfig
walkersDone chan struct{}
walkerLoops []*dontpanic.Forever
requestTotals prometheus.Counter
missTotals prometheus.Counter
bytesStoredtotals prometheus.Counter
bytesFetchedtotals prometheus.Counter
bytesLoserTotals prometheus.Counter
errTotal *prometheus.CounterVec
walkerCheckTotal prometheus.Counter
walkerRemovalTotal prometheus.Counter
walkerErrorTotal prometheus.Counter
walkerEmptyDirTotal prometheus.Counter
walkerEmptyDirRemovalTotal prometheus.Counter
}
// New will create a new DiskCache with the given Keyer.
func New(cfg config.Cfg, locator storage.Locator, opts ...Option) *DiskCache {
var cacheConfig cacheConfig
for _, opt := range opts {
opt(&cacheConfig)
}
cache := &DiskCache{
locator: locator,
storages: cfg.Storages,
af: activeFiles{
Mutex: &sync.Mutex{},
m: map[string]int{},
},
cacheConfig: cacheConfig,
walkersDone: make(chan struct{}),
requestTotals: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_requests_total",
Help: "Total number of disk cache requests",
},
),
missTotals: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_miss_total",
Help: "Total number of disk cache misses",
},
),
bytesStoredtotals: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_bytes_stored_total",
Help: "Total number of disk cache bytes stored",
},
),
bytesFetchedtotals: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_bytes_fetched_total",
Help: "Total number of disk cache bytes fetched",
},
),
bytesLoserTotals: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_bytes_loser_total",
Help: "Total number of disk cache bytes from losing writes",
},
),
errTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gitaly_diskcache_errors_total",
Help: "Total number of errors encountered by disk cache",
},
[]string{"error"},
),
walkerCheckTotal: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_walker_check_total",
Help: "Total number of events during diskcache filesystem walks",
},
),
walkerRemovalTotal: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_walker_removal_total",
Help: "Total number of events during diskcache filesystem walks",
},
),
walkerErrorTotal: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_walker_error_total",
Help: "Total number of errors during diskcache filesystem walks",
},
),
walkerEmptyDirTotal: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_walker_empty_dir_total",
Help: "Total number of empty directories encountered",
},
),
walkerEmptyDirRemovalTotal: prometheus.NewCounter(
prometheus.CounterOpts{
Name: "gitaly_diskcache_walker_empty_dir_removal_total",
Help: "Total number of empty directories removed",
},
),
}
cache.keyer = newLeaseKeyer(locator, cache.countErr)
return cache
}
// Describe is used to describe Prometheus metrics.
func (c *DiskCache) Describe(descs chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(c, descs)
}
// Collect is used to collect Prometheus metrics.
func (c *DiskCache) Collect(metrics chan<- prometheus.Metric) {
c.requestTotals.Collect(metrics)
c.missTotals.Collect(metrics)
c.bytesStoredtotals.Collect(metrics)
c.bytesFetchedtotals.Collect(metrics)
c.bytesLoserTotals.Collect(metrics)
c.errTotal.Collect(metrics)
c.walkerRemovalTotal.Collect(metrics)
c.walkerErrorTotal.Collect(metrics)
c.walkerEmptyDirTotal.Collect(metrics)
c.walkerEmptyDirRemovalTotal.Collect(metrics)
}
func (c *DiskCache) countErr(err error) error {
switch err {
case ErrMissingLeaseFile:
c.errTotal.WithLabelValues("ErrMissingLeaseFile").Inc()
case ErrPendingExists:
c.errTotal.WithLabelValues("ErrPendingExists").Inc()
}
return err
}
// ErrReqNotFound indicates the request does not exist within the repo digest
var ErrReqNotFound = errors.New("request digest not found within repo namespace")
// GetStream will fetch the cached stream for a request. It is the
// responsibility of the caller to close the stream when done.
func (c *DiskCache) GetStream(ctx context.Context, repo *gitalypb.Repository, req proto.Message) (_ io.ReadCloser, err error) {
defer func() {
if err != nil {
c.missTotals.Inc()
}
}()
c.requestTotals.Inc()
respPath, err := c.KeyPath(ctx, repo, req)
switch {
case os.IsNotExist(err):
return nil, ErrReqNotFound
case err == nil:
break
default:
return nil, err
}
ctxlogrus.Extract(ctx).
WithField("stream_path", respPath).
Info("getting stream")
respF, err := os.Open(respPath)
switch {
case os.IsNotExist(err):
return nil, ErrReqNotFound
case err == nil:
break
default:
return nil, err
}
return instrumentedReadCloser{
ReadCloser: respF,
counter: c.bytesFetchedtotals,
}, nil
}
type instrumentedReadCloser struct {
io.ReadCloser
counter prometheus.Counter
}
func (irc instrumentedReadCloser) Read(p []byte) (n int, err error) {
n, err = irc.ReadCloser.Read(p)
irc.counter.Add(float64(n))
return
}
// PutStream will store a stream in a repo-namespace keyed by the digest of the
// request protobuf message.
func (c *DiskCache) PutStream(ctx context.Context, repo *gitalypb.Repository, req proto.Message, src io.Reader) (returnedErr error) {
reqPath, err := c.KeyPath(ctx, repo, req)
if err != nil {
return err
}
ctxlogrus.Extract(ctx).
WithField("stream_path", reqPath).
Info("putting stream")
var n int64
isWinner := c.af.trackFile(reqPath)
defer func() {
if !isWinner() {
c.bytesLoserTotals.Add(float64(n))
}
}()
if err := os.MkdirAll(filepath.Dir(reqPath), perm.SharedDir); err != nil {
return err
}
sf, err := safe.NewFileWriter(reqPath)
if err != nil {
return err
}
defer func() {
if err := sf.Close(); err != nil && returnedErr == nil {
if !errors.Is(err, safe.ErrAlreadyDone) {
returnedErr = err
}
}
}()
n, err = io.Copy(sf, src)
if err != nil {
return err
}
c.bytesStoredtotals.Add(float64(n))
if err := sf.Commit(); err != nil {
c.errTotal.WithLabelValues("ErrSafefileCommit").Inc()
return err
}
return nil
}
// KeyPath returns the cache path for the given request.
func (c *DiskCache) KeyPath(ctx context.Context, repo *gitalypb.Repository, req proto.Message) (string, error) {
return c.keyer.keyPath(ctx, repo, req)
}
// StartLease will mark the repository as being in an indeterministic state. This is typically used
// when modifying the repo, since the cache is not stable until after the modification is complete.
// A lease object will be returned that allows the caller to signal the end of the lease.
func (c *DiskCache) StartLease(repo *gitalypb.Repository) (LeaseEnder, error) {
pendingPath, err := c.keyer.newPendingLease(repo)
if err != nil {
return lease{}, err
}
return lease{
pendingPath: pendingPath,
repo: repo,
keyer: c.keyer,
countErr: c.countErr,
}, nil
}
type lease struct {
pendingPath string
repo *gitalypb.Repository
keyer leaseKeyer
countErr func(error) error
}
// EndLease will end the lease by removing the pending lease file and updating
// the key file with the current lease ID.
func (l lease) EndLease(ctx context.Context) error {
_, err := l.keyer.updateLatest(ctx, l.repo)
if err != nil {
return err
}
if err := os.Remove(l.pendingPath); err != nil {
if os.IsNotExist(err) {
return l.countErr(ErrMissingLeaseFile)
}
return err
}
return nil
}
|