-
Notifications
You must be signed in to change notification settings - Fork 435
/
extract.go
277 lines (239 loc) · 7.9 KB
/
extract.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
270
271
272
273
274
275
276
277
package internal
import (
"archive/tar"
"context"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/internal/compression"
conf "github.com/wal-g/wal-g/internal/config"
"github.com/wal-g/wal-g/internal/crypto"
"github.com/wal-g/wal-g/utility"
"golang.org/x/sync/semaphore"
)
var MinExtractRetryWait = time.Minute
var MaxExtractRetryWait = 5 * time.Minute
type NoFilesToExtractError struct {
error
}
func newNoFilesToExtractError() NoFilesToExtractError {
return NoFilesToExtractError{errors.New("ExtractAll: did not provide files to extract")}
}
func (err NoFilesToExtractError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
// UnsupportedFileTypeError is used to signal file types
// that are unsupported by WAL-G.
type UnsupportedFileTypeError struct {
error
}
func newUnsupportedFileTypeError(path string, fileFormat string) UnsupportedFileTypeError {
return UnsupportedFileTypeError{errors.Errorf("WAL-G does not support the file format '%s' in '%s'", fileFormat, path)}
}
func (err UnsupportedFileTypeError) Error() string {
return fmt.Sprintf(tracelog.GetErrorFormatter(), err.error)
}
// TarInterpreter behaves differently
// for different file types.
type TarInterpreter interface {
Interpret(reader io.Reader, header *tar.Header) error
}
type DevNullWriter struct {
io.WriteCloser
statPrinter sync.Once
totalBytes int64
}
func (e *DevNullWriter) Write(p []byte) (int, error) {
e.statPrinter.Do(func() {
go func() {
for {
time.Sleep(1 * time.Second)
tracelog.ErrorLogger.Printf("/dev/null size %d", e.totalBytes)
}
}()
})
e.totalBytes += int64(len(p))
return len(p), nil
}
var _ io.Writer = &DevNullWriter{}
// TODO : unit tests
// Extract exactly one tar bundle.
func extractOneTar(tarInterpreter TarInterpreter, source io.Reader) error {
tarReader := tar.NewReader(source)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return errors.Wrap(err, "extractOne: tar extract failed")
}
err = tarInterpreter.Interpret(tarReader, header)
if err != nil {
return errors.Wrap(err, "extractOne: Interpret failed")
}
}
return nil
}
func extractNonTar(tarInterpreter TarInterpreter, source io.Reader, path string, fileType FileType, mode int64) error {
var typeFlag byte
if fileType == RegularFileType {
typeFlag = tar.TypeReg
} else {
typeFlag = tar.TypeDir
}
return tarInterpreter.Interpret(source, &tar.Header{
Name: path,
Mode: mode,
Typeflag: typeFlag,
})
}
// DecryptAndDecompressTar decrypts file and checks its extension.
// If it's tar, a decompression is not needed.
// Otherwise it uses corresponding decompressor. If none found an error will be returned.
func DecryptAndDecompressTar(reader io.Reader, filePath string, crypter crypto.Crypter) (io.ReadCloser, error) {
var err error
if crypter != nil {
reader, err = crypter.Decrypt(reader)
if err != nil {
return nil, errors.Wrap(err, "DecryptAndDecompressTar: decrypt failed")
}
}
fileExtension := utility.GetFileExtension(filePath)
if fileExtension == "tar" || fileExtension == "" {
return io.NopCloser(reader), nil
}
decompressor := compression.FindDecompressor(fileExtension)
if decompressor == nil {
return nil, newUnsupportedFileTypeError(filePath, fileExtension)
}
return decompressor.Decompress(reader)
}
// ExtractAll Handles all files passed in. Supports `.lzo`, `.lz4`, `.lzma`, and `.tar`.
// File type `.nop` is used for testing purposes. Each file is extracted
// in its own goroutine and ExtractAll will wait for all goroutines to finish.
// Retries unsuccessful attempts log2(MaxConcurrency) times, dividing concurrency by two each time.
func ExtractAll(tarInterpreter TarInterpreter, files []ReaderMaker) error {
return ExtractAllWithSleeper(tarInterpreter, files, NewExponentialSleeper(MinExtractRetryWait, MaxExtractRetryWait))
}
func ExtractAllWithSleeper(tarInterpreter TarInterpreter, files []ReaderMaker, sleeper Sleeper) error {
if len(files) == 0 {
return newNoFilesToExtractError()
}
// Set maximum number of goroutines spun off by ExtractAll
downloadingConcurrency, err := conf.GetMaxDownloadConcurrency()
if err != nil {
return err
}
retries := conf.GetFetchRetries()
for currentRun := files; len(currentRun) > 0; {
failed := tryExtractFiles(currentRun, tarInterpreter, downloadingConcurrency)
if downloadingConcurrency > 1 {
downloadingConcurrency /= 2
} else if len(failed) == len(currentRun) && retries <= 0 {
return errors.Errorf("failed to extract files:\n%s\n",
strings.Join(readerMakersToFilePaths(failed), "\n"))
}
retries--
currentRun = failed
if len(failed) > 0 {
tracelog.WarningLogger.Printf("%d files failed to download: %s. Going to sleep and retry downloading them.\n",
len(failed), readerMakersToFilePaths(failed))
tracelog.WarningLogger.Printf("retries left: %d", retries)
sleeper.Sleep()
}
}
return nil
}
// Extract single file from backup
// If it is .tar file unpack it and store internal files (there will be .tar file if you work with wal-g backup)
// Otherwise store this file (there will be regular file if you work with pgbackrest backup)
func extractFile(tarInterpreter TarInterpreter, extractingReader io.Reader, fileClosure ReaderMaker) error {
switch fileClosure.FileType() {
case TarFileType:
err := extractOneTar(tarInterpreter, extractingReader)
if err == nil {
err = readTrailingZeros(extractingReader)
}
return err
case RegularFileType:
return extractNonTar(tarInterpreter, extractingReader, fileClosure.LocalPath(), fileClosure.FileType(), fileClosure.Mode())
default:
tracelog.InfoLogger.Print()
return errors.New("Unknown fileType " + string(fileClosure.FileType()))
}
}
// TODO : unit tests
func tryExtractFiles(files []ReaderMaker,
tarInterpreter TarInterpreter,
downloadingConcurrency int) (failed []ReaderMaker) {
downloadingContext := context.TODO()
downloadingSemaphore := semaphore.NewWeighted(int64(downloadingConcurrency))
crypter := ConfigureCrypter()
isFailed := sync.Map{}
for _, file := range files {
err := downloadingSemaphore.Acquire(downloadingContext, 1)
if err != nil {
tracelog.ErrorLogger.Println(err)
return files //Should never happen, but if we are asked to cancel - consider all files unfinished
}
fileClosure := file
go func() {
defer downloadingSemaphore.Release(1)
readCloser, err := fileClosure.Reader()
if err == nil {
defer utility.LoggedClose(readCloser, "")
filePath := fileClosure.StoragePath()
var extractingReader io.ReadCloser
extractingReader, err = DecryptAndDecompressTar(readCloser, filePath, crypter)
if err == nil {
defer extractingReader.Close()
err = extractFile(tarInterpreter, extractingReader, fileClosure)
err = errors.Wrapf(err, "Extraction error in %s", filePath)
tracelog.InfoLogger.Printf("Finished extraction of %s", filePath)
}
}
if err != nil {
isFailed.Store(fileClosure, true)
tracelog.ErrorLogger.Println(err)
}
}()
}
err := downloadingSemaphore.Acquire(downloadingContext, int64(downloadingConcurrency))
if err != nil {
tracelog.ErrorLogger.Println(err)
return files //Should never happen, but if we are asked to cancel - consider all files unfinished
}
isFailed.Range(func(failedFile, _ interface{}) bool {
failed = append(failed, failedFile.(ReaderMaker))
return true
})
return failed
}
func readTrailingZeros(r io.Reader) error {
// on first iteration we read small chunk
// in most cases we will return fast without memory allocation
b := make([]byte, 1024)
for {
n, err := r.Read(b)
if n > 0 {
if !utility.AllZero(b[:n]) {
return io.ErrClosedPipe
}
}
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if len(b) < utility.CompressedBlockMaxSize {
// but if we found zeroes, allocate large buffer to speed up reading
b = make([]byte, utility.CompressedBlockMaxSize)
}
}
}