-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.go
335 lines (300 loc) · 8.13 KB
/
backup.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
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
package duckdbreplicator
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
"time"
"gocloud.dev/blob"
"gocloud.dev/blob/gcsblob"
"gocloud.dev/gcerrors"
"golang.org/x/sync/errgroup"
)
type BackupFormat string
const (
BackupFormatUnknown BackupFormat = "unknown"
BackupFormatDB BackupFormat = "db"
BackupFormatParquet BackupFormat = "parquet"
)
type BackupProvider struct {
bucket *blob.Bucket
}
func (b *BackupProvider) Close() error {
return b.bucket.Close()
}
type GCSBackupProviderOptions struct {
// UseHostCredentials specifies whether to use the host's default credentials.
UseHostCredentials bool
ApplicationCredentialsJSON string
// Bucket is the GCS bucket to use for backups. Should be of the form `bucket-name`.
Bucket string
// BackupFormat specifies the format of the backup.
// TODO :: implement backup format. Fixed to DuckDB for now.
BackupFormat BackupFormat
// UnqiueIdentifier is used to store backups in a unique location.
// This must be set when multiple databases are writing to the same bucket.
UniqueIdentifier string
}
// NewGCSBackupProvider creates a new BackupProvider based on GCS.
func NewGCSBackupProvider(ctx context.Context, opts *GCSBackupProviderOptions) (*BackupProvider, error) {
client, err := newClient(ctx, opts.ApplicationCredentialsJSON, opts.UseHostCredentials)
if err != nil {
return nil, err
}
bucket, err := gcsblob.OpenBucket(ctx, client, opts.Bucket, nil)
if err != nil {
return nil, fmt.Errorf("failed to open bucket %q, %w", opts.Bucket, err)
}
if opts.UniqueIdentifier != "" {
if !strings.HasSuffix(opts.UniqueIdentifier, "/") {
opts.UniqueIdentifier += "/"
}
bucket = blob.PrefixedBucket(bucket, opts.UniqueIdentifier)
}
return &BackupProvider{
bucket: bucket,
}, nil
}
// syncWrite syncs the write path with the backup location.
func (d *db) syncWrite(ctx context.Context) error {
if !d.writeDirty || d.backup == nil {
// optimisation to skip sync if write was already synced
return nil
}
d.logger.Debug("syncing from backup")
// Create an errgroup for background downloads with limited concurrency.
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(8)
objects := d.backup.List(&blob.ListOptions{
Delimiter: "/", // only list directories with a trailing slash and IsDir set to true
})
tblVersions := make(map[string]string)
for {
// Stop the loop if the ctx was cancelled
var stop bool
select {
case <-ctx.Done():
stop = true
default:
// don't break
}
if stop {
break // can't use break inside the select
}
obj, err := objects.Next(ctx)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
if !obj.IsDir {
continue
}
table := strings.TrimSuffix(obj.Key, "/")
d.logger.Debug("SyncWithObjectStorage: discovered table", slog.String("table", table))
// get version of the table
var backedUpVersion string
err = retry(func() error {
res, err := d.backup.ReadAll(ctx, filepath.Join(table, "version.txt"))
if err != nil {
return err
}
backedUpVersion = string(res)
return nil
})
if err != nil {
if gcerrors.Code(err) == gcerrors.NotFound {
// invalid table directory
d.logger.Debug("SyncWithObjectStorage: invalid table directory", slog.String("table", table))
_ = d.deleteBackup(ctx, table, "")
}
return err
}
tblVersions[table] = backedUpVersion
// check with current version
version, exists, _ := tableVersion(d.writePath, table)
if exists && version == backedUpVersion {
d.logger.Debug("SyncWithObjectStorage: table is already up to date", slog.String("table", table))
continue
}
tableDir := filepath.Join(d.writePath, table)
// truncate existing table directory
if err := os.RemoveAll(tableDir); err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(tableDir, backedUpVersion), os.ModePerm); err != nil {
return err
}
tblIter := d.backup.List(&blob.ListOptions{Prefix: filepath.Join(table, backedUpVersion)})
// download all objects in the table and current version
for {
obj, err := tblIter.Next(ctx)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
g.Go(func() error {
return retry(func() error {
file, err := os.Create(filepath.Join(d.writePath, obj.Key))
if err != nil {
return err
}
defer file.Close()
rdr, err := d.backup.NewReader(ctx, obj.Key, nil)
if err != nil {
return err
}
defer rdr.Close()
_, err = io.Copy(file, rdr)
return err
})
})
}
}
// Wait for all outstanding downloads to complete
err := g.Wait()
if err != nil {
return err
}
// Update table versions
for table, version := range tblVersions {
err = os.WriteFile(filepath.Join(d.writePath, table, "version.txt"), []byte(version), fs.ModePerm)
if err != nil {
return err
}
}
// remove any tables that are not in backup
entries, err := os.ReadDir(d.writePath)
if err != nil {
return err
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
if _, ok := tblVersions[entry.Name()]; ok {
continue
}
err = os.RemoveAll(filepath.Join(d.writePath, entry.Name()))
if err != nil {
return err
}
}
return nil
}
func (d *db) syncBackup(ctx context.Context, table string) error {
if d.backup == nil {
return nil
}
d.logger.Debug("syncing table", slog.String("table", table))
version, exist, err := tableVersion(d.writePath, table)
if err != nil {
return err
}
if !exist {
return fmt.Errorf("table %q not found", table)
}
path := filepath.Join(d.writePath, table, version)
entries, err := os.ReadDir(path)
if err != nil {
return err
}
for _, entry := range entries {
d.logger.Debug("replicating file", slog.String("file", entry.Name()), slog.String("path", path))
// no directory should exist as of now
if entry.IsDir() {
d.logger.Debug("found directory in path which should not exist", slog.String("file", entry.Name()), slog.String("path", path))
continue
}
wr, err := os.Open(filepath.Join(path, entry.Name()))
if err != nil {
return err
}
// upload to cloud storage
err = retry(func() error {
return d.backup.Upload(ctx, filepath.Join(table, version, entry.Name()), wr, &blob.WriterOptions{
ContentType: "application/octet-stream",
})
})
wr.Close()
if err != nil {
return err
}
}
// update version.txt
// Ideally if this fails it is a non recoverable error but for now we will rely on retries
err = retry(func() error {
return d.backup.WriteAll(ctx, filepath.Join(table, "version.txt"), []byte(version), nil)
})
if err != nil {
d.logger.Error("failed to update version.txt in backup", slog.Any("error", err))
}
return err
}
// deleteBackup deletes backup.
// If table is specified, only that table is deleted.
// If table and version is specified, only that version of the table is deleted.
func (d *db) deleteBackup(ctx context.Context, table, version string) error {
if d.backup == nil {
return nil
}
if table == "" && version != "" {
return fmt.Errorf("table must be specified if version is specified")
}
var prefix string
if table != "" {
if version != "" {
prefix = filepath.Join(table, version) + "/"
} else {
// deleting the entire table
prefix = table + "/"
// delete version.txt first
err := retry(func() error { return d.backup.Delete(ctx, "version.txt") })
if err != nil && gcerrors.Code(err) != gcerrors.NotFound {
d.logger.Error("failed to delete version.txt in backup", slog.Any("error", err))
return err
}
}
}
iter := d.backup.List(&blob.ListOptions{Prefix: prefix})
for {
obj, err := iter.Next(ctx)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return err
}
err = retry(func() error { return d.backup.Delete(ctx, obj.Key) })
if err != nil {
return err
}
}
return nil
}
func retry(fn func() error) error {
var err error
for i := 0; i < _maxRetries; i++ {
err = fn()
if err == nil {
return nil // success
} else if strings.Contains(err.Error(), "stream error: stream ID") {
time.Sleep(_retryDelay) // retry
} else {
break // return error
}
}
return err
}
const (
_maxRetries = 5
_retryDelay = 10 * time.Second
)