-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmanager.go
More file actions
601 lines (506 loc) · 15.3 KB
/
manager.go
File metadata and controls
601 lines (506 loc) · 15.3 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package gitclone
import (
"context"
"io/fs"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/alecthomas/errors"
"github.com/block/cachew/internal/logging"
)
type State int
const (
StateEmpty State = iota // Not cloned yet
StateCloning // Clone in progress
StateReady // Ready to use
)
func (s State) String() string {
switch s {
case StateEmpty:
return "empty"
case StateCloning:
return "cloning"
case StateReady:
return "ready"
default:
return "unknown"
}
}
type GitTuningConfig struct {
PostBuffer int // http.postBuffer size in bytes
LowSpeedLimit int // http.lowSpeedLimit in bytes/sec
LowSpeedTime time.Duration // http.lowSpeedTime
}
func DefaultGitTuningConfig() GitTuningConfig {
return GitTuningConfig{
PostBuffer: 524288000, // 500MB buffer
LowSpeedLimit: 1000, // 1KB/s minimum speed
LowSpeedTime: 10 * time.Minute,
}
}
type Config struct {
MirrorRoot string `hcl:"mirror-root,optional" help:"Directory to store git clones." default:"${CACHEW_STATE}/git-mirrors"`
FetchInterval time.Duration `hcl:"fetch-interval,optional" help:"How often to fetch from upstream in minutes." default:"15m"`
RefCheckInterval time.Duration `hcl:"ref-check-interval,optional" help:"How long to cache ref checks." default:"10s"`
Maintenance bool `hcl:"maintenance,optional" help:"Enable git maintenance scheduling for mirror repos." default:"false"`
PackThreads int `hcl:"pack-threads,optional" help:"Threads for git pack operations (0 = all CPU cores)." default:"0"`
}
// CredentialProvider provides credentials for git operations.
type CredentialProvider interface {
GetTokenForURL(ctx context.Context, url string) (string, error)
}
type CredentialProviderProvider func() (CredentialProvider, error)
type Repository struct {
mu sync.RWMutex
config Config
state State
path string
upstreamURL string
lastFetch time.Time
lastRefCheck time.Time
refCheckValid bool
fetchSem chan struct{}
credentialProvider CredentialProvider
}
type Manager struct {
config Config
gitTuningConfig GitTuningConfig
clones map[string]*Repository
clonesMu sync.RWMutex
credentialProvider CredentialProvider
}
// ManagerProvider is a function that lazily creates a singleton Manager.
type ManagerProvider func() (*Manager, error)
func NewManagerProvider(ctx context.Context, config Config, credentialProviderProvider CredentialProviderProvider) ManagerProvider {
return sync.OnceValues(func() (*Manager, error) {
var credentialProvider CredentialProvider
if credentialProviderProvider != nil {
var err error
credentialProvider, err = credentialProviderProvider()
if err != nil {
return nil, errors.WithStack(err)
}
}
return NewManager(ctx, config, credentialProvider)
})
}
func NewManager(ctx context.Context, config Config, credentialProvider CredentialProvider) (*Manager, error) {
if config.MirrorRoot == "" {
return nil, errors.New("mirror-root is required")
}
if config.FetchInterval == 0 {
config.FetchInterval = 15 * time.Minute
}
if config.RefCheckInterval == 0 {
config.RefCheckInterval = 10 * time.Second
}
if config.PackThreads <= 0 {
config.PackThreads = runtime.NumCPU()
}
if err := os.MkdirAll(config.MirrorRoot, 0o750); err != nil {
return nil, errors.Wrap(err, "create root directory")
}
if config.Maintenance {
if err := startMaintenance(ctx); err != nil {
logging.FromContext(ctx).WarnContext(ctx, "Failed to start git maintenance scheduler", "error", err)
}
}
logging.FromContext(ctx).InfoContext(ctx, "Git clone manager initialised",
"mirror_root", config.MirrorRoot,
"fetch_interval", config.FetchInterval,
"ref_check_interval", config.RefCheckInterval)
return &Manager{
config: config,
gitTuningConfig: DefaultGitTuningConfig(),
clones: make(map[string]*Repository),
credentialProvider: credentialProvider,
}, nil
}
func (m *Manager) Config() Config {
return m.config
}
func (m *Manager) GetOrCreate(_ context.Context, upstreamURL string) (*Repository, error) {
m.clonesMu.RLock()
repo, exists := m.clones[upstreamURL]
m.clonesMu.RUnlock()
if exists {
return repo, nil
}
m.clonesMu.Lock()
defer m.clonesMu.Unlock()
if repo, exists = m.clones[upstreamURL]; exists {
return repo, nil
}
clonePath, err := m.clonePathForURL(upstreamURL)
if err != nil {
return nil, err
}
repo = &Repository{
state: StateEmpty,
config: m.config,
path: clonePath,
upstreamURL: upstreamURL,
fetchSem: make(chan struct{}, 1),
credentialProvider: m.credentialProvider,
}
headFile := filepath.Join(clonePath, "HEAD")
if _, err := os.Stat(headFile); err == nil {
repo.state = StateReady
}
repo.fetchSem <- struct{}{}
m.clones[upstreamURL] = repo
return repo, nil
}
func (m *Manager) Get(upstreamURL string) *Repository {
m.clonesMu.RLock()
defer m.clonesMu.RUnlock()
return m.clones[upstreamURL]
}
func (m *Manager) DiscoverExisting(ctx context.Context) ([]*Repository, error) {
var discovered []*Repository
err := filepath.Walk(m.config.MirrorRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return nil
}
headPath := filepath.Join(path, "HEAD")
if _, statErr := os.Stat(headPath); statErr != nil {
if errors.Is(statErr, os.ErrNotExist) {
return nil
}
return errors.Wrap(statErr, "stat HEAD file")
}
relPath, err := filepath.Rel(m.config.MirrorRoot, path)
if err != nil {
return errors.Wrap(err, "get relative path")
}
urlPath := filepath.ToSlash(relPath)
idx := strings.Index(urlPath, "/")
if idx == -1 {
return nil
}
host := urlPath[:idx]
repoPath := urlPath[idx+1:]
upstreamURL := "https://" + host + "/" + repoPath
repo := &Repository{
state: StateReady,
config: m.config,
path: path,
upstreamURL: upstreamURL,
fetchSem: make(chan struct{}, 1),
credentialProvider: m.credentialProvider,
}
repo.fetchSem <- struct{}{}
if err := configureMirror(ctx, path, m.config.PackThreads); err != nil {
return errors.Wrapf(err, "configure mirror for %s", upstreamURL)
}
if m.config.Maintenance {
if err := registerMaintenance(ctx, path); err != nil {
return errors.Wrapf(err, "register maintenance for %s", upstreamURL)
}
}
m.clonesMu.Lock()
m.clones[upstreamURL] = repo
m.clonesMu.Unlock()
discovered = append(discovered, repo)
return fs.SkipDir
})
if err != nil {
return nil, errors.Wrap(err, "walk root directory")
}
return discovered, nil
}
// RepoPathFromURL extracts a normalised "host/path" from an upstream Git URL,
// stripping any ".git" suffix.
func RepoPathFromURL(upstreamURL string) (string, error) {
parsed, err := url.Parse(upstreamURL)
if err != nil {
return "", errors.Wrap(err, "parse upstream URL")
}
return filepath.Join(parsed.Host, strings.TrimSuffix(parsed.Path, ".git")), nil
}
func (m *Manager) clonePathForURL(upstreamURL string) (string, error) {
repoPath, err := RepoPathFromURL(upstreamURL)
if err != nil {
return "", err
}
return filepath.Join(m.config.MirrorRoot, repoPath), nil
}
func (r *Repository) State() State {
r.mu.RLock()
defer r.mu.RUnlock()
return r.state
}
func (r *Repository) Path() string {
return r.path
}
func (r *Repository) UpstreamURL() string {
return r.upstreamURL
}
func (r *Repository) LastFetch() time.Time {
r.mu.RLock()
defer r.mu.RUnlock()
return r.lastFetch
}
func (r *Repository) NeedsFetch(fetchInterval time.Duration) bool {
r.mu.RLock()
defer r.mu.RUnlock()
return time.Since(r.lastFetch) >= fetchInterval
}
func (r *Repository) WithReadLock(fn func() error) error {
r.mu.RLock()
defer r.mu.RUnlock()
return fn()
}
func WithReadLockReturn[T any](repo *Repository, fn func() (T, error)) (T, error) {
repo.mu.RLock()
defer repo.mu.RUnlock()
return fn()
}
func (r *Repository) Clone(ctx context.Context) error {
r.mu.Lock()
if r.state != StateEmpty {
r.mu.Unlock()
return nil
}
r.state = StateCloning
r.mu.Unlock()
err := r.executeClone(ctx)
r.mu.Lock()
if err != nil {
r.state = StateEmpty
r.mu.Unlock()
return err
}
r.state = StateReady
r.lastFetch = time.Now()
r.mu.Unlock()
return nil
}
// mirrorConfigSettings returns git config key-value pairs applied to mirror
// clones to optimise upload-pack serving performance.
func mirrorConfigSettings(packThreads int) [][2]string {
return [][2]string{
// Protocol
{"protocol.version", "2"},
{"uploadpack.allowFilter", "true"},
{"uploadpack.allowReachableSHA1InWant", "true"},
// Bitmaps
{"repack.writeBitmaps", "true"},
{"pack.useBitmaps", "true"},
{"pack.useBitmapBoundaryTraversal", "true"},
// Commit graph
{"core.commitGraph", "true"},
{"gc.writeCommitGraph", "true"},
{"fetch.writeCommitGraph", "true"},
// Multi-pack-index
{"core.multiPackIndex", "true"},
// Keep fetched objects as packs
{"transfer.unpackLimit", "1"},
{"fetch.unpackLimit", "1"},
// Disable auto GC
{"gc.auto", "0"},
// Pack performance
{"pack.threads", strconv.Itoa(packThreads)},
{"pack.deltaCacheSize", "512m"},
{"pack.windowMemory", "1g"},
}
}
func registerMaintenance(ctx context.Context, repoPath string) error {
// #nosec G204 - repoPath is controlled by us
cmd := exec.CommandContext(ctx, "git", "-C", repoPath, "config", "maintenance.strategy", "incremental")
if output, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "set maintenance.strategy: %s", string(output))
}
// #nosec G204 - repoPath is controlled by us
cmd = exec.CommandContext(ctx, "git", "-C", repoPath, "maintenance", "register")
if output, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "maintenance register: %s", string(output))
}
return nil
}
func startMaintenance(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "git", "maintenance", "start")
if output, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "maintenance start: %s", string(output))
}
return nil
}
func configureMirror(ctx context.Context, repoPath string, packThreads int) error {
for _, kv := range mirrorConfigSettings(packThreads) {
// #nosec G204 - repoPath and config values are controlled by us
cmd := exec.CommandContext(ctx, "git", "-C", repoPath, "config", kv[0], kv[1])
output, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "configure %s: %s", kv[0], string(output))
}
}
return nil
}
func (r *Repository) executeClone(ctx context.Context) error {
if err := os.MkdirAll(filepath.Dir(r.path), 0o750); err != nil {
return errors.Wrap(err, "create clone directory")
}
config := DefaultGitTuningConfig()
// #nosec G204 - r.upstreamURL and r.path are controlled by us
args := []string{
"clone", "--mirror",
"-c", "http.postBuffer=" + strconv.Itoa(config.PostBuffer),
"-c", "http.lowSpeedLimit=" + strconv.Itoa(config.LowSpeedLimit),
"-c", "http.lowSpeedTime=" + strconv.Itoa(int(config.LowSpeedTime.Seconds())),
r.upstreamURL, r.path,
}
cmd, err := r.gitCommand(ctx, args...)
if err != nil {
return errors.Wrap(err, "create git command")
}
output, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "git clone --mirror: %s", string(output))
}
if err := configureMirror(ctx, r.path, r.config.PackThreads); err != nil {
return errors.Wrap(err, "configure mirror")
}
if r.config.Maintenance {
if err := registerMaintenance(ctx, r.path); err != nil {
return errors.Wrap(err, "register maintenance")
}
}
return nil
}
func (r *Repository) Fetch(ctx context.Context) error {
select {
case <-r.fetchSem:
defer func() {
r.fetchSem <- struct{}{}
}()
case <-ctx.Done():
return errors.Wrap(ctx.Err(), "context cancelled before acquiring fetch semaphore")
default:
select {
case <-r.fetchSem:
r.fetchSem <- struct{}{}
return nil
case <-ctx.Done():
return errors.Wrap(ctx.Err(), "context cancelled while waiting for fetch")
}
}
r.mu.Lock()
defer r.mu.Unlock()
config := DefaultGitTuningConfig()
// #nosec G204 - r.path is controlled by us
cmd, err := r.gitCommand(ctx, "-C", r.path,
"-c", "http.postBuffer="+strconv.Itoa(config.PostBuffer),
"-c", "http.lowSpeedLimit="+strconv.Itoa(config.LowSpeedLimit),
"-c", "http.lowSpeedTime="+strconv.Itoa(int(config.LowSpeedTime.Seconds())),
"fetch", "--prune", "--prune-tags")
if err != nil {
return errors.Wrap(err, "create git command")
}
output, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "git fetch: %s", string(output))
}
r.lastFetch = time.Now()
return nil
}
func (r *Repository) EnsureRefsUpToDate(ctx context.Context) error {
r.mu.Lock()
if r.refCheckValid && time.Since(r.lastRefCheck) < r.config.RefCheckInterval {
r.mu.Unlock()
return nil
}
r.lastRefCheck = time.Now()
r.refCheckValid = true
r.mu.Unlock()
localRefs, err := r.GetLocalRefs(ctx)
if err != nil {
return errors.Wrap(err, "get local refs")
}
upstreamRefs, err := r.GetUpstreamRefs(ctx)
if err != nil {
return errors.Wrap(err, "get upstream refs")
}
needsFetch := false
for ref, upstreamSHA := range upstreamRefs {
if strings.HasSuffix(ref, "^{}") {
continue
}
if !strings.HasPrefix(ref, "refs/heads/") {
continue
}
localSHA, exists := localRefs[ref]
if !exists || localSHA != upstreamSHA {
needsFetch = true
break
}
}
if !needsFetch {
r.mu.Lock()
r.refCheckValid = true
r.mu.Unlock()
return nil
}
err = r.Fetch(ctx)
if err != nil {
r.mu.Lock()
r.refCheckValid = false
r.mu.Unlock()
}
return err
}
func (r *Repository) GetLocalRefs(ctx context.Context) (map[string]string, error) {
var output []byte
var err error
r.mu.RLock()
// #nosec G204 - r.path is controlled by us
cmd := exec.CommandContext(ctx, "git", "-C", r.path, "for-each-ref", "--format=%(objectname) %(refname)")
output, err = cmd.CombinedOutput()
r.mu.RUnlock()
if err != nil {
return nil, errors.Wrap(err, "git for-each-ref")
}
return ParseGitRefs(output), nil
}
func (r *Repository) GetUpstreamRefs(ctx context.Context) (map[string]string, error) {
// #nosec G204 - r.upstreamURL is controlled by us
cmd, err := r.gitCommand(ctx, "ls-remote", r.upstreamURL)
if err != nil {
return nil, errors.Wrap(err, "create git command")
}
output, err := cmd.CombinedOutput()
if err != nil {
return nil, errors.Wrap(err, "git ls-remote")
}
return ParseGitRefs(output), nil
}
func (r *Repository) Repack(ctx context.Context) error {
r.mu.RLock()
defer r.mu.RUnlock()
logger := logging.FromContext(ctx)
logger.InfoContext(ctx, "Full repack started", "upstream", r.upstreamURL)
// #nosec G204 - r.path is controlled by us
cmd := exec.CommandContext(ctx, "git", "-C", r.path, "repack", "-adb", "--write-midx", "--write-bitmap-index")
output, err := cmd.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "git repack: %s", string(output))
}
logger.InfoContext(ctx, "Full repack completed", "upstream", r.upstreamURL)
return nil
}
func (r *Repository) HasCommit(ctx context.Context, ref string) bool {
r.mu.RLock()
defer r.mu.RUnlock()
// #nosec G204 - r.path and ref are controlled by us
cmd := exec.CommandContext(ctx, "git", "-C", r.path, "cat-file", "-e", ref)
err := cmd.Run()
return err == nil
}