-
Notifications
You must be signed in to change notification settings - Fork 4
/
restore.go
269 lines (227 loc) · 8.29 KB
/
restore.go
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
/*
Copyright AppsCode Inc. and Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package restic
import (
"sync"
"time"
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
"k8s.io/apimachinery/pkg/util/errors"
)
// RunRestore run restore process for a single host.
func (w *ResticWrapper) RunRestore(restoreOptions RestoreOptions, targetRef api_v1beta1.TargetRef) (*RestoreOutput, error) {
// Start clock to measure total restore duration
startTime := time.Now()
restoreStats := api_v1beta1.HostRestoreStats{
Hostname: restoreOptions.Host,
}
err := w.runRestore(restoreOptions)
if err != nil {
return nil, err
}
// Restore successful. Now, calculate total session duration.
restoreStats.Duration = time.Since(startTime).String()
restoreStats.Phase = api_v1beta1.HostRestoreSucceeded
restoreOutput := &RestoreOutput{
RestoreTargetStatus: api_v1beta1.RestoreMemberStatus{
Ref: targetRef,
Stats: []api_v1beta1.HostRestoreStats{restoreStats},
},
}
return restoreOutput, nil
}
// RunParallelRestore run restore process for multiple hosts in parallel using go routine.
// You can control maximum number of parallel restore using maxConcurrency parameter.
func (w *ResticWrapper) RunParallelRestore(restoreOptions []RestoreOptions, targetRef api_v1beta1.TargetRef, maxConcurrency int) (*RestoreOutput, error) {
// WaitGroup to wait until all go routine finish
wg := sync.WaitGroup{}
// concurrencyLimiter channel is used to limit maximum number simultaneous go routine
concurrencyLimiter := make(chan bool, maxConcurrency)
defer close(concurrencyLimiter)
var (
restoreErrs []error
mu sync.Mutex
)
restoreOutput := &RestoreOutput{
RestoreTargetStatus: api_v1beta1.RestoreMemberStatus{
Ref: targetRef,
},
}
for i := range restoreOptions {
// try to send message in concurrencyLimiter channel.
// if maximum allowed concurrent restore is already running, program control will stuck here.
concurrencyLimiter <- true
// starting new go routine. add it to WaitGroup
wg.Add(1)
go func(opt RestoreOptions, startTime time.Time) {
// when this go routine completes it task, release a slot from the concurrencyLimiter channel
// so that another go routine can start. Also, tell the WaitGroup that it is done with its task.
defer func() {
<-concurrencyLimiter
wg.Done()
}()
// sh field in ResticWrapper is a pointer. we must not use same w in multiple go routine.
// otherwise they might enter in a racing condition.
nw := w.Copy()
// run restore
err := nw.runRestore(opt)
if err != nil {
mu.Lock()
restoreErrs = append(restoreErrs, err)
mu.Unlock()
return
}
hostStats := api_v1beta1.HostRestoreStats{
Hostname: opt.Host,
}
hostStats.Duration = time.Since(startTime).String()
hostStats.Phase = api_v1beta1.HostRestoreSucceeded
// add hostStats to restoreOutput
mu.Lock()
restoreOutput.upsertHostRestoreStats(hostStats)
mu.Unlock()
}(restoreOptions[i], time.Now())
}
// wait for all the go routines to complete
wg.Wait()
return restoreOutput, errors.NewAggregate(restoreErrs)
}
// Dump run restore process for a single host and output the restored files in stdout.
func (w *ResticWrapper) Dump(dumpOptions DumpOptions, targetRef api_v1beta1.TargetRef) (*RestoreOutput, error) {
// Start clock to measure total restore duration
startTime := time.Now()
restoreStats := api_v1beta1.HostRestoreStats{
Hostname: dumpOptions.Host,
}
// if source host is not specified then use current host as source host
if dumpOptions.SourceHost == "" {
dumpOptions.SourceHost = dumpOptions.Host
}
if _, err := w.DumpOnce(dumpOptions); err != nil {
return nil, err
}
// Dump successful. Now, calculate total session duration.
restoreStats.Duration = time.Since(startTime).String()
restoreStats.Phase = api_v1beta1.HostRestoreSucceeded
restoreOutput := &RestoreOutput{
RestoreTargetStatus: api_v1beta1.RestoreMemberStatus{
Ref: targetRef,
Stats: []api_v1beta1.HostRestoreStats{restoreStats},
},
}
return restoreOutput, nil
}
// ParallelDump run DumpOnce for multiple hosts concurrently using go routine.
// You can control maximum number of parallel restore process using maxConcurrency parameter.
func (w *ResticWrapper) ParallelDump(dumpOptions []DumpOptions, targetRef api_v1beta1.TargetRef, maxConcurrency int) (*RestoreOutput, error) {
// WaitGroup to wait until all go routine finish
wg := sync.WaitGroup{}
// concurrencyLimiter channel is used to limit maximum number simultaneous go routine
concurrencyLimiter := make(chan bool, maxConcurrency)
defer close(concurrencyLimiter)
var (
restoreErrs []error
mu sync.Mutex
)
restoreOutput := &RestoreOutput{
RestoreTargetStatus: api_v1beta1.RestoreMemberStatus{
Ref: targetRef,
},
}
for i := range dumpOptions {
// try to send message in concurrencyLimiter channel.
// if maximum allowed concurrent restore is already running, program control will stuck here.
concurrencyLimiter <- true
// starting new go routine. add it to WaitGroup
wg.Add(1)
go func(opt DumpOptions, startTime time.Time) {
// when this go routine completes its task, release a slot from the concurrencyLimiter channel
// so that another go routine can start. Also, tell the WaitGroup that it is done with its task.
defer func() {
<-concurrencyLimiter
wg.Done()
}()
// sh field in ResticWrapper is a pointer. we must not use same w in multiple go routine.
// otherwise they might enter in a racing condition.
nw := w.Copy()
// if source host is not specified then use current host as source host
if opt.SourceHost == "" {
opt.SourceHost = opt.Host
}
hostStats := api_v1beta1.HostRestoreStats{
Hostname: opt.Host,
}
// run restore
_, err := nw.DumpOnce(opt)
hostStats.Duration = time.Since(startTime).String()
if err != nil {
hostStats.Phase = api_v1beta1.HostRestoreFailed
hostStats.Error = err.Error()
mu.Lock()
restoreErrs = append(restoreErrs, err)
mu.Unlock()
} else {
hostStats.Phase = api_v1beta1.HostRestoreSucceeded
}
// add hostStats to restoreOutput. use lock to avoid racing condition.
mu.Lock()
restoreOutput.upsertHostRestoreStats(hostStats)
mu.Unlock()
}(dumpOptions[i], time.Now())
}
// wait for all the go routines to complete
wg.Wait()
return restoreOutput, errors.NewAggregate(restoreErrs)
}
func (w *ResticWrapper) runRestore(restoreOptions RestoreOptions) error {
if len(restoreOptions.Snapshots) != 0 {
for _, snapshot := range restoreOptions.Snapshots {
// if snapshot is specified then host and path does not matter.
params := restoreParams{
destination: restoreOptions.Destination,
snapshotId: snapshot,
excludes: restoreOptions.Exclude,
includes: restoreOptions.Include,
args: restoreOptions.Args,
}
if _, err := w.restore(params); err != nil {
return err
}
}
} else if len(restoreOptions.RestorePaths) != 0 {
for _, path := range restoreOptions.RestorePaths {
params := restoreParams{
path: path,
host: restoreOptions.SourceHost,
destination: restoreOptions.Destination,
excludes: restoreOptions.Exclude,
includes: restoreOptions.Include,
args: restoreOptions.Args,
}
if _, err := w.restore(params); err != nil {
return err
}
}
}
return nil
}
func (restoreOutput *RestoreOutput) upsertHostRestoreStats(hostStats api_v1beta1.HostRestoreStats) {
// check if a entry already exist for this host in restoreOutput. If exist then update it.
for i, v := range restoreOutput.RestoreTargetStatus.Stats {
if v.Hostname == hostStats.Hostname {
restoreOutput.RestoreTargetStatus.Stats[i] = hostStats
return
}
}
// no entry for this host. add a new entry
restoreOutput.RestoreTargetStatus.Stats = append(restoreOutput.RestoreTargetStatus.Stats, hostStats)
}