forked from wal-g/wal-g
/
extract.go
203 lines (179 loc) · 5.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
package internal
import (
"archive/tar"
"context"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/tinsane/tracelog"
"github.com/wal-g/wal-g/internal/compression"
"github.com/wal-g/wal-g/internal/crypto"
"github.com/wal-g/wal-g/internal/ioextensions"
"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)
}
// EmptyWriteIgnorer handles 0 byte write in LZ4 package
// to stop pipe reader/writer from blocking.
type EmptyWriteIgnorer struct {
io.WriteCloser
}
func (e EmptyWriteIgnorer) Write(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
return e.WriteCloser.Write(p)
}
// TODO : unit tests
// Extract exactly one tar bundle.
func extractOne(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
}
// TODO : unit tests
// Ensures that file extension is valid. Any subsequent behavior
// depends on file type.
func DecryptAndDecompressTar(writer io.Writer, readerMaker ReaderMaker, crypter crypto.Crypter) error {
readCloser, err := readerMaker.Reader()
if err != nil {
return errors.Wrap(err, "DecryptAndDecompressTar: failed to create new reader")
}
defer utility.LoggedClose(readCloser, "")
if crypter != nil {
var reader io.Reader
reader, err = crypter.Decrypt(readCloser)
if err != nil {
return errors.Wrap(err, "DecryptAndDecompressTar: decrypt failed")
}
readCloser = ioextensions.ReadCascadeCloser{
Reader: reader,
Closer: readCloser,
}
}
fileExtension := utility.GetFileExtension(readerMaker.Path())
for _, decompressor := range compression.Decompressors {
if fileExtension != decompressor.FileExtension() {
continue
}
err = decompressor.Decompress(writer, readCloser)
return errors.Wrapf(err, "DecryptAndDecompressTar: %v decompress failed. Is archive encrypted?", decompressor.FileExtension())
}
switch fileExtension {
case "tar":
_, err = io.Copy(writer, readCloser)
return errors.Wrap(err, "DecryptAndDecompressTar: tar extract failed")
case "nop":
case "lzo":
return NewUnsupportedFileTypeError(readerMaker.Path(), fileExtension)
default:
return NewUnsupportedFileTypeError(readerMaker.Path(), fileExtension)
}
return nil
}
// TODO : unit tests
// 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.
// Returns the first error encountered.
func ExtractAll(tarInterpreter TarInterpreter, files []ReaderMaker) error {
if len(files) == 0 {
return NewNoFilesToExtractError()
}
retrier := NewExponentialRetrier(MinExtractRetryWait, MaxExtractRetryWait)
// Set maximum number of goroutines spun off by ExtractAll
downloadingConcurrency, err := GetMaxDownloadConcurrency()
if err != nil {
return err
}
for currentRun := files; len(currentRun) > 0; {
var failed []ReaderMaker
failed = tryExtractFiles(currentRun, tarInterpreter, downloadingConcurrency)
if downloadingConcurrency > 1 {
downloadingConcurrency /= 2
} else if len(failed) == len(currentRun) {
return errors.Errorf("failed to extract files:\n%s\n",
strings.Join(ReaderMakersToFilePaths(failed), "\n"))
}
currentRun = failed
if len(failed) > 0 {
retrier.retry()
}
}
return nil
}
// 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 {
_ = downloadingSemaphore.Acquire(downloadingContext, 1)
fileClosure := file
extractingReader, pipeWriter := io.Pipe()
decompressingWriter := &EmptyWriteIgnorer{pipeWriter}
go func() {
err := DecryptAndDecompressTar(decompressingWriter, fileClosure, crypter)
utility.LoggedClose(decompressingWriter, "")
tracelog.InfoLogger.Printf("Finished decompression of %s", fileClosure.Path())
if err != nil {
isFailed.Store(fileClosure, true)
tracelog.ErrorLogger.Println(fileClosure.Path(), err)
}
}()
go func() {
defer downloadingSemaphore.Release(1)
err := extractOne(tarInterpreter, extractingReader)
err = errors.Wrapf(err, "Extraction error in %s", fileClosure.Path())
utility.LoggedClose(extractingReader, "")
tracelog.InfoLogger.Printf("Finished extraction of %s", fileClosure.Path())
if err != nil {
isFailed.Store(fileClosure, true)
tracelog.ErrorLogger.Println(err)
}
}()
}
_ = downloadingSemaphore.Acquire(downloadingContext, int64(downloadingConcurrency))
isFailed.Range(func(failedFile, _ interface{}) bool {
failed = append(failed, failedFile.(ReaderMaker))
return true
})
return failed
}