Skip to content

Commit 81df899

Browse files
author
Jacob Marble
committed
chore: integrate review feedback
1 parent 09b03c0 commit 81df899

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

cmd/influx/backup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var backupCmd = &cobra.Command{
2020
Long: fmt.Sprintf(
2121
`Backs up data and meta data for the running InfluxDB instance.
2222
Downloaded files are written to the directory indicated by --path.
23+
The target directory, and any parent directories, are created automatically.
2324
Data file have extension .tsm; meta data is written to %s in the same directory.`,
2425
bolt.DefaultFilename),
2526
RunE: backupF,
@@ -58,7 +59,7 @@ func backupF(cmd *cobra.Command, args []string) error {
5859
return fmt.Errorf("must specify path")
5960
}
6061

61-
err := os.Mkdir(backupFlags.Path, 0770)
62+
err := os.MkdirAll(backupFlags.Path, 0770)
6263
if err != nil && !os.IsExist(err) {
6364
return err
6465
}

http/backup_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (s *BackupService) FetchBackupFile(ctx context.Context, backupID int, backu
211211
return err
212212
}
213213

214-
_, err = io.CopyBuffer(w, resp.Body, make([]byte, 1024*1024))
214+
_, err = io.Copy(w, resp.Body)
215215
if err != nil {
216216
return err
217217
}

storage/engine.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/influxdata/influxql"
2727
"github.com/pkg/errors"
2828
"github.com/prometheus/client_golang/prometheus"
29+
"go.uber.org/multierr"
2930
"go.uber.org/zap"
3031
)
3132

@@ -672,7 +673,13 @@ func (e *Engine) deleteBucketRangeLocked(ctx context.Context, orgID, bucketID pl
672673
// 3) Return a unique backup ID (invalid after the process terminates) and list of files.
673674
func (e *Engine) CreateBackup(ctx context.Context) (int, []string, error) {
674675
span, ctx := tracing.StartSpanFromContext(ctx)
675-
span.LogKV("path", e.path)
676+
defer span.Finish()
677+
678+
e.mu.RLock()
679+
defer e.mu.RUnlock()
680+
if e.closing == nil {
681+
return 0, nil, ErrEngineClosed
682+
}
676683

677684
if err := e.engine.WriteSnapshot(ctx, tsm1.CacheStatusBackup); err != nil {
678685
return 0, nil, err
@@ -699,7 +706,13 @@ func (e *Engine) CreateBackup(ctx context.Context) (int, []string, error) {
699706
// After a successful write, the internal copy is removed.
700707
func (e *Engine) FetchBackupFile(ctx context.Context, backupID int, backupFile string, w io.Writer) error {
701708
span, ctx := tracing.StartSpanFromContext(ctx)
702-
span.LogKV("path", e.path)
709+
defer span.Finish()
710+
711+
e.mu.RLock()
712+
defer e.mu.RUnlock()
713+
if e.closing == nil {
714+
return ErrEngineClosed
715+
}
703716

704717
backupPath := e.engine.FileStore.InternalBackupPath(backupID)
705718
if fi, err := os.Stat(backupPath); err != nil {
@@ -721,12 +734,15 @@ func (e *Engine) FetchBackupFile(ctx context.Context, backupID int, backupFile s
721734
}
722735
defer file.Close()
723736

724-
buf := make([]byte, 1024*1024)
725-
_, err = io.CopyBuffer(w, file, buf)
726-
if err != nil {
737+
if _, err = io.Copy(w, file); err != nil {
738+
err = multierr.Append(err, file.Close())
727739
return errors.WithMessagef(err, "failed to copy backup file %d/%s to writer", backupID, backupFile)
728740
}
729741

742+
if err = file.Close(); err != nil {
743+
return errors.WithMessagef(err, "failed to close backup file %d/%s", backupID, backupFile)
744+
}
745+
730746
if err = os.Remove(backupFileFullPath); err != nil {
731747
e.logger.Info("Failed to remove backup file after fetch", zap.Error(err), zap.Int("backup_id", backupID), zap.String("backup_file", backupFile))
732748
}

tsdb/tsm1/file_store.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ func (f *FileStore) locations(key []byte, t int64, ascending bool) []*location {
12081208

12091209
// CreateSnapshot creates hardlinks for all tsm and tombstone files
12101210
// in the path provided.
1211-
func (f *FileStore) CreateSnapshot(ctx context.Context) (int, string, error) {
1211+
func (f *FileStore) CreateSnapshot(ctx context.Context) (backupID int, backupDirFullPath string, err error) {
12121212
span, _ := tracing.StartSpanFromContext(ctx)
12131213
defer span.Finish()
12141214

@@ -1228,31 +1228,31 @@ func (f *FileStore) CreateSnapshot(ctx context.Context) (int, string, error) {
12281228
// increment and keep track of the current temp dir for when we drop the lock.
12291229
// this ensures we are the only writer to the directory.
12301230
f.currentTempDirID += 1
1231-
backupID := f.currentTempDirID
1231+
backupID = f.currentTempDirID
12321232
f.mu.Unlock()
12331233

1234-
path := f.InternalBackupPath(backupID)
1234+
backupDirFullPath = f.InternalBackupPath(backupID)
12351235

12361236
// create the tmp directory and add the hard links. there is no longer any shared
12371237
// mutable state.
1238-
err := os.Mkdir(path, 0777)
1238+
err := os.Mkdir(backupDirFullPath, 0777)
12391239
if err != nil {
12401240
return 0, "", err
12411241
}
12421242
for _, tsmf := range files {
1243-
newpath := filepath.Join(path, filepath.Base(tsmf.Path()))
1243+
newpath := filepath.Join(backupDirFullPath, filepath.Base(tsmf.Path()))
12441244
if err := os.Link(tsmf.Path(), newpath); err != nil {
12451245
return 0, "", fmt.Errorf("error creating tsm hard link: %q", err)
12461246
}
12471247
for _, tf := range tsmf.TombstoneFiles() {
1248-
newpath := filepath.Join(path, filepath.Base(tf.Path))
1248+
newpath := filepath.Join(backupDirFullPath, filepath.Base(tf.Path))
12491249
if err := os.Link(tf.Path, newpath); err != nil {
12501250
return 0, "", fmt.Errorf("error creating tombstone hard link: %q", err)
12511251
}
12521252
}
12531253
}
12541254

1255-
return backupID, path, nil
1255+
return backupID, backupDirFullPath, nil
12561256
}
12571257

12581258
func (f *FileStore) InternalBackupPath(backupID int) string {

0 commit comments

Comments
 (0)