Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pipeline.go « backup « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f18a80e3763301825d2877392fdd7245851ea8e4 (plain)
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
package backup

import (
	"context"
	"errors"
	"fmt"
	"sync"
	"sync/atomic"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
)

// Strategy used to create/restore backups
type Strategy interface {
	Create(context.Context, *CreateRequest) error
	Restore(context.Context, *RestoreRequest) error
}

// CreatePipeline is a pipeline that only handles creating backups
type CreatePipeline interface {
	Create(context.Context, *CreateRequest)
	Done() error
}

// Pipeline handles a series of requests to create/restore backups. Pipeline
// encapsulates error handling for the caller.
type Pipeline struct {
	log      logrus.FieldLogger
	strategy Strategy
	failed   int64
}

// NewPipeline creates a new pipeline
func NewPipeline(log logrus.FieldLogger, strategy Strategy) *Pipeline {
	return &Pipeline{
		log:      log,
		strategy: strategy,
	}
}

// Create requests that a repository backup be created
func (p *Pipeline) Create(ctx context.Context, req *CreateRequest) {
	repoLog := p.repoLogger(req.Repository)
	repoLog.Info("started backup")

	if err := p.strategy.Create(ctx, req); err != nil {
		if errors.Is(err, ErrSkipped) {
			repoLog.WithError(err).Warn("skipped backup")
		} else {
			repoLog.WithError(err).Error("backup failed")
			atomic.AddInt64(&p.failed, 1)
		}
		return
	}

	repoLog.Info("completed backup")
}

// Restore requests that a repository be restored from backup
func (p *Pipeline) Restore(ctx context.Context, req *RestoreRequest) {
	repoLog := p.repoLogger(req.Repository)
	repoLog.Info("started restore")

	if err := p.strategy.Restore(ctx, req); err != nil {
		if errors.Is(err, ErrSkipped) {
			repoLog.WithError(err).Warn("skipped restore")
		} else {
			repoLog.WithError(err).Error("restore failed")
			atomic.AddInt64(&p.failed, 1)
		}
		return
	}

	repoLog.Info("completed restore")
}

// Done indicates that the pipeline is complete and returns any accumulated errors
func (p *Pipeline) Done() error {
	if p.failed > 0 {
		return fmt.Errorf("pipeline: %d failures encountered", p.failed)
	}
	return nil
}

func (p *Pipeline) repoLogger(repo *gitalypb.Repository) logrus.FieldLogger {
	return p.log.WithFields(logrus.Fields{
		"storage_name":    repo.StorageName,
		"relative_path":   repo.RelativePath,
		"gl_project_path": repo.GlProjectPath,
	})
}

// ParallelCreatePipeline is a pipeline that creates backups in parallel
type ParallelCreatePipeline struct {
	next CreatePipeline
	n    int

	workersOnce sync.Once
	wg          sync.WaitGroup
	done        chan struct{}
	requests    chan *CreateRequest

	mu  sync.Mutex
	err error
}

// NewParallelCreatePipeline creates a new ParallelCreatePipeline where `next`
// is the pipeline called to create the backups and `n` is the number of
// parallel backups that will run.
func NewParallelCreatePipeline(next CreatePipeline, n int) *ParallelCreatePipeline {
	return &ParallelCreatePipeline{
		next:     next,
		n:        n,
		done:     make(chan struct{}),
		requests: make(chan *CreateRequest),
	}
}

// Create queues a call to `next.Create` which will be run in parallel
func (p *ParallelCreatePipeline) Create(ctx context.Context, req *CreateRequest) {
	p.workersOnce.Do(p.startWorkers)

	select {
	case <-ctx.Done():
		p.setErr(ctx.Err())
	case p.requests <- req:
	}
}

// Done waits for any in progress calls to `Create` to complete then reports any accumulated errors
func (p *ParallelCreatePipeline) Done() error {
	close(p.done)
	p.wg.Wait()
	if err := p.next.Done(); err != nil {
		return err
	}
	return p.err
}

func (p *ParallelCreatePipeline) startWorkers() {
	for i := 0; i < p.n; i++ {
		p.wg.Add(1)
		go p.worker()
	}
}

func (p *ParallelCreatePipeline) worker() {
	defer p.wg.Done()
	for {
		select {
		case <-p.done:
			return
		case req := <-p.requests:
			p.next.Create(context.TODO(), req)
		}
	}
}

func (p *ParallelCreatePipeline) setErr(err error) {
	p.mu.Lock()
	defer p.mu.Unlock()
	if p.err != nil {
		return
	}
	p.err = err
}