This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
backup.go
576 lines (490 loc) · 15.4 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package ctl
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"time"
pilosa "github.com/featurebasedb/featurebase/v3"
"github.com/featurebasedb/featurebase/v3/authn"
"github.com/featurebasedb/featurebase/v3/disco"
"github.com/featurebasedb/featurebase/v3/encoding/proto"
"github.com/featurebasedb/featurebase/v3/logger"
"github.com/featurebasedb/featurebase/v3/server"
"github.com/ricochet2200/go-disk-usage/du"
"golang.org/x/sync/errgroup"
)
// TODO(rdp): add refresh token to this as well
// BackupCommand represents a command for backing up a FeatureBase node.
type BackupCommand struct { // nolint: maligned
tlsConfig *tls.Config
// Destination host and port.
Host string `json:"host"`
// Optional Index filter
Index string `json:"index"`
// Path to write the backup to.
OutputDir string
// If true, skips file sync.
NoSync bool
// Number of concurrent backup goroutines running at a time.
Concurrency int
// Amount of time after first failed request to continue retrying.
RetryPeriod time.Duration `json:"retry-period"`
// Response Header Timeout for HTTP Requests
HeaderTimeoutStr string
HeaderTimeout time.Duration `json:"header-timeout"`
// Host:port on which to listen for pprof.
Pprof string `json:"pprof"`
// Reusable client.
client *pilosa.InternalClient
// Standard input/output
logDest logger.Logger
TLS server.TLSConfig
AuthToken string
IgnoreSpaceCheck bool
}
// Logger returns the command's associated Logger to maintain CommandWithTLSSupport interface compatibility
func (cmd *BackupCommand) Logger() logger.Logger {
return cmd.logDest
}
// NewBackupCommand returns a new instance of BackupCommand.
func NewBackupCommand(logdest logger.Logger) *BackupCommand {
return &BackupCommand{
logDest: logdest,
Concurrency: 1,
RetryPeriod: time.Minute,
HeaderTimeout: time.Second * 3,
Pprof: "localhost:0",
}
}
// Run executes the main program execution.
func (cmd *BackupCommand) Run(ctx context.Context) (err error) {
logger := cmd.Logger()
close, err := startProfilingServer(cmd.Pprof, logger)
if err != nil {
return fmt.Errorf("starting profiling server: %w", err)
}
defer close()
// Validate arguments.
if cmd.OutputDir == "" {
return fmt.Errorf("%w: -o flag required", UsageError)
} else if cmd.Concurrency <= 0 {
return fmt.Errorf("%w: concurrency must be at least one", UsageError)
}
if cmd.HeaderTimeoutStr != "" {
if dur, err := time.ParseDuration(cmd.HeaderTimeoutStr); err != nil {
return fmt.Errorf("%w: could not parse '%s' as a duration: %v", UsageError, cmd.HeaderTimeoutStr, err)
} else {
cmd.HeaderTimeout = dur
}
}
// Parse TLS configuration for node-specific clients.
tls := cmd.TLSConfiguration()
if cmd.tlsConfig, err = server.GetTLSConfig(&tls, cmd.Logger()); err != nil {
return fmt.Errorf("parsing tls config: %w", err)
}
// Create a client to the server.
client, err := commandClient(cmd, pilosa.WithClientRetryPeriod(cmd.RetryPeriod), pilosa.ClientResponseHeaderTimeoutOption(cmd.HeaderTimeout))
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
cmd.client = client
if cmd.AuthToken != "" {
ctx = authn.WithAccessToken(ctx, "Bearer "+cmd.AuthToken)
}
// Determine the field type in order to correctly handle the input data.
indexes, err := cmd.client.Schema(ctx)
if err != nil {
return fmt.Errorf("getting schema: %w", err)
}
if cmd.Index != "" {
for _, idx := range indexes {
if idx.Name == cmd.Index {
indexes = make([]*pilosa.IndexInfo, 0)
indexes = append(indexes, idx)
break
}
}
if len(indexes) <= 0 {
return fmt.Errorf("index not found to back up")
}
}
schema := &pilosa.Schema{Indexes: indexes}
// Ensure output directory doesn't exist; then create output directory.
if _, err := os.Stat(cmd.OutputDir); !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("output directory already exists")
} else if err := os.MkdirAll(cmd.OutputDir, 0o750); err != nil {
return err
}
if !cmd.IgnoreSpaceCheck {
// Ensure there is enough free space
if err := cmd.checkFreeSpace(ctx); err != nil {
return fmt.Errorf("not enough disk space available: %w", err)
}
}
// Backup schema.
if err := cmd.backupSchema(ctx, schema); err != nil {
return fmt.Errorf("cannot back up schema: %w", err)
} else if err := cmd.backupIDAllocData(ctx); err != nil {
return fmt.Errorf("cannot back up id alloc data: %w", err)
}
// Backup data for each index.
for _, ii := range schema.Indexes {
if err := cmd.backupIndexData(ctx, ii); err != nil {
return err
}
}
// Backup translation data. This has to happen separately, because
// otherwise a field which uses foreign key translation can reasonably
// contain values which got created for the foreign index after we
// backed up that index.
for _, ii := range schema.Indexes {
if err := cmd.backupIndexTranslation(ctx, ii); err != nil {
return err
}
}
// Wait for the OS to persist all directories.
err = cmd.syncDirectories(ctx)
if err != nil {
return fmt.Errorf("syncing directories: %w", err)
}
return nil
}
// backupSchema writes the schema to the archive.
func (cmd *BackupCommand) backupSchema(ctx context.Context, schema *pilosa.Schema) error {
logger := cmd.Logger()
logger.Printf("backing up schema")
buf, err := json.MarshalIndent(schema, "", "\t")
if err != nil {
return fmt.Errorf("marshaling schema: %w", err)
}
if err := os.WriteFile(filepath.Join(cmd.OutputDir, "schema"), buf, 0o600); err != nil {
return fmt.Errorf("writing schema: %w", err)
}
return nil
}
func (cmd *BackupCommand) backupIDAllocData(ctx context.Context) error {
logger := cmd.Logger()
logger.Printf("backing up id alloc data")
rc, err := cmd.client.IDAllocDataReader(ctx)
if err != nil {
return fmt.Errorf("fetching id alloc data reader: %w", err)
}
defer rc.Close()
f, err := os.Create(filepath.Join(cmd.OutputDir, "idalloc"))
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, rc); err != nil {
return err
} else if err := cmd.syncFile(f); err != nil {
return err
}
return f.Close()
}
// backupIndexTranslation backs up both field and index-wide key translation for
// the given index. it has to run after the index's data has been backed up,
// but also after the data for any index which might have a foreign-key
// relation to this index has been backed up.
func (cmd *BackupCommand) backupIndexTranslation(ctx context.Context, ii *pilosa.IndexInfo) error {
logger := cmd.Logger()
logger.Printf("backing up index translation: %q", ii.Name)
if ii.Options.Keys {
if err := cmd.backupIndexTranslateData(ctx, ii.Name); err != nil {
return err
}
}
// Back up field translation data.
for _, fi := range ii.Fields {
if !fi.Options.Keys {
continue
}
if err := cmd.backupFieldTranslateData(ctx, ii.Name, fi.Name); err != nil {
return fmt.Errorf("cannot backup field translation data for field %q on index %q: %w", fi.Name, ii.Name, err)
}
}
return nil
}
// backupIndexData backs up all shard data for a given index.
func (cmd *BackupCommand) backupIndexData(ctx context.Context, ii *pilosa.IndexInfo) error {
logger := cmd.Logger()
logger.Printf("backing up index data: %q", ii.Name)
shards, err := cmd.client.AvailableShards(ctx, ii.Name)
if err != nil {
return fmt.Errorf("cannot find available shards for index %q: %w", ii.Name, err)
}
// Back up all bitmap data for the index.
ch := make(chan uint64, len(shards))
for _, shard := range shards {
ch <- shard
}
close(ch)
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < cmd.Concurrency; i++ {
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case shard, ok := <-ch:
if !ok {
return nil
} else if err := cmd.backupShard(ctx, ii.Name, shard); err != nil {
return fmt.Errorf("cannot backup shard %d on index %q: %w", shard, ii.Name, err)
}
}
}
})
}
return g.Wait()
}
// checkFreeSpace checks if there is enough space in the output directory to backup data
func (cmd *BackupCommand) checkFreeSpace(ctx context.Context) (err error) {
freeSpace := du.NewDiskUsage(cmd.OutputDir).Free()
var usage pilosa.DiskUsage
if cmd.Index == "" {
usage, err = cmd.client.GetDiskUsage(ctx)
} else {
usage, err = cmd.client.GetIndexUsage(ctx, cmd.Index)
}
if err != nil {
return fmt.Errorf("getting size of data to be backed up: %s", err)
}
if freeSpace < uint64(usage.Usage) {
return fmt.Errorf("not enough disk space available, free: %v, index usage: %v", freeSpace, usage.Usage)
}
return nil
}
// backupShard backs up a single shard from a single index.
func (cmd *BackupCommand) backupShard(ctx context.Context, indexName string, shard uint64) (err error) {
nodes, err := cmd.client.FragmentNodes(ctx, indexName, shard)
if err != nil {
return fmt.Errorf("cannot determine fragment nodes: %w", err)
} else if len(nodes) == 0 {
return fmt.Errorf("no nodes available")
}
for _, node := range nodes {
if e := cmd.backupShardNode(ctx, indexName, shard, node); e == nil {
break
} else if err == nil {
err = e // save first error, try next node
}
}
for _, node := range nodes {
if e := cmd.backupShardDataframe(ctx, indexName, shard, node); e == nil {
return nil // backup ok, exit
} else if err == nil {
err = e // save first error, try next node
}
}
return err
}
// backupShardNode backs up a single shard from a single index on a specific node.
func (cmd *BackupCommand) backupShardNode(ctx context.Context, indexName string, shard uint64, node *disco.Node) error {
logger := cmd.Logger()
logger.Printf("backing up shard: index=%q id=%d", indexName, shard)
client := pilosa.NewInternalClientFromURI(&node.URI,
pilosa.GetHTTPClient(cmd.tlsConfig, pilosa.ClientResponseHeaderTimeoutOption(cmd.HeaderTimeout)),
pilosa.WithClientRetryPeriod(cmd.RetryPeriod),
pilosa.WithSerializer(proto.Serializer{}))
rc, err := client.ShardReader(ctx, indexName, shard)
if err != nil {
return fmt.Errorf("fetching shard reader: %w", err)
}
defer rc.Close()
filename := filepath.Join(cmd.OutputDir, "indexes", indexName, "shards", fmt.Sprintf("%04d", shard))
if err := os.MkdirAll(filepath.Dir(filename), 0o750); err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, rc); err != nil {
return err
} else if err := cmd.syncFile(f); err != nil {
return err
}
return f.Close()
}
func (cmd *BackupCommand) backupIndexTranslateData(ctx context.Context, name string) error {
partitionN := disco.DefaultPartitionN
ch := make(chan int, partitionN)
for partitionID := 0; partitionID < partitionN; partitionID++ {
ch <- partitionID
}
close(ch)
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < cmd.Concurrency; i++ {
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case partitionID, ok := <-ch:
if !ok {
return nil
} else if err := cmd.backupIndexPartitionTranslateData(ctx, name, partitionID); err != nil {
return fmt.Errorf("cannot backup index translation data for partition %d on %q: %w", partitionID, name, err)
}
}
}
})
}
return g.Wait()
}
func (cmd *BackupCommand) backupIndexPartitionTranslateData(ctx context.Context, name string, partitionID int) error {
logger := cmd.Logger()
logger.Printf("backing up index translation data: %s/%d", name, partitionID)
rc, err := cmd.client.IndexTranslateDataReader(ctx, name, partitionID)
if err != nil {
return fmt.Errorf("fetching translate data reader: %w", err)
}
defer rc.Close()
filename := filepath.Join(cmd.OutputDir, "indexes", name, "translate", fmt.Sprintf("%04d", partitionID))
if err := os.MkdirAll(filepath.Dir(filename), 0o750); err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, rc); err != nil {
return err
} else if err := cmd.syncFile(f); err != nil {
return err
}
return f.Close()
}
func (cmd *BackupCommand) backupFieldTranslateData(ctx context.Context, indexName, fieldName string) error {
logger := cmd.Logger()
logger.Printf("backing up field translation data: %s/%s", indexName, fieldName)
rc, err := cmd.client.FieldTranslateDataReader(ctx, indexName, fieldName)
if err != nil {
return fmt.Errorf("fetching translate data reader: %w", err)
}
defer rc.Close()
filename := filepath.Join(cmd.OutputDir, "indexes", indexName, "fields", fieldName, "translate")
if err := os.MkdirAll(filepath.Dir(filename), 0o750); err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, rc); err != nil {
return err
} else if err := cmd.syncFile(f); err != nil {
return err
}
return f.Close()
}
func (cmd *BackupCommand) syncFile(f *os.File) error {
if cmd.NoSync {
return nil
}
return f.Sync()
}
func (cmd *BackupCommand) TLSHost() string { return cmd.Host }
func (cmd *BackupCommand) TLSConfiguration() server.TLSConfig { return cmd.TLS }
// syncDirectories fsyncs all directories required for the backup to be persisted to the filesystem.
func (cmd *BackupCommand) syncDirectories(ctx context.Context) error {
if cmd.NoSync {
return nil
}
syncChan := make(chan string, cmd.Concurrency)
syncChan <- filepath.Dir(cmd.OutputDir)
g, ctx := errgroup.WithContext(ctx)
for i := 0; i < cmd.Concurrency; i++ {
g.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case path, ok := <-syncChan:
if !ok {
return nil
} else if err := cmd.syncDir(path); err != nil {
return fmt.Errorf("cannot sync directory %q: %w", path, err)
}
}
}
})
}
err := filepath.Walk(cmd.OutputDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
select {
case <-ctx.Done():
return ctx.Err()
case syncChan <- path:
}
}
return nil
})
close(syncChan)
if err != nil {
return fmt.Errorf("walking output directory tree: %w", err)
}
return g.Wait()
}
func (cmd *BackupCommand) syncDir(path string) error {
logger := cmd.Logger()
logger.Printf("syncing directory: %s", path)
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("opening directory for sync: %w", err)
}
defer f.Close()
err = f.Sync()
if err != nil {
return fmt.Errorf("syncing directory: %w", err)
}
return f.Close()
}
func (cmd *BackupCommand) backupShardDataframe(ctx context.Context, indexName string, shard uint64, node *disco.Node) error {
logger := cmd.Logger()
logger.Printf("backing up dataframe shard: index=%q shard=%d", indexName, shard)
client := pilosa.NewInternalClientFromURI(&node.URI,
pilosa.GetHTTPClient(cmd.tlsConfig, pilosa.ClientResponseHeaderTimeoutOption(cmd.HeaderTimeout)),
)
resp, err := client.GetDataframeShard(ctx, indexName, shard)
// no error if doesn't exist
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
// no error if not present server maynot have it turned on
return nil
}
filename := filepath.Join(cmd.OutputDir, "indexes", indexName, "dataframe", fmt.Sprintf("%04d", shard))
if err := os.MkdirAll(filepath.Dir(filename), 0o750); err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
return err
} else if err := cmd.syncFile(f); err != nil {
return err
}
return f.Close()
}