From d0cc07ae69e14552698c29631babe06385d32a40 Mon Sep 17 00:00:00 2001 From: Alain Jobart Date: Thu, 21 May 2015 08:54:52 -0700 Subject: [PATCH] Simplifying file backup storage (implementation and plugins). --- go/cmd/vtctl/plugin_filebackupstorage.go | 10 +++------ go/cmd/vttablet/plugin_filebackupstorage.go | 16 -------------- go/vt/mysqlctl/backupstorage/file.go | 24 +++++++-------------- go/vt/mysqlctl/backupstorage/file_test.go | 7 +++--- go/vt/wrangler/testlib/backup_test.go | 1 - 5 files changed, 14 insertions(+), 44 deletions(-) delete mode 100644 go/cmd/vttablet/plugin_filebackupstorage.go diff --git a/go/cmd/vtctl/plugin_filebackupstorage.go b/go/cmd/vtctl/plugin_filebackupstorage.go index fedb3f853f..25b4da38bf 100644 --- a/go/cmd/vtctl/plugin_filebackupstorage.go +++ b/go/cmd/vtctl/plugin_filebackupstorage.go @@ -4,10 +4,6 @@ package main -import "github.com/youtube/vitess/go/vt/mysqlctl/backupstorage" - -func init() { - initFuncs = append(initFuncs, func() { - backupstorage.RegisterFileBackupStorage() - }) -} +import ( + _ "github.com/youtube/vitess/go/vt/mysqlctl/backupstorage" +) diff --git a/go/cmd/vttablet/plugin_filebackupstorage.go b/go/cmd/vttablet/plugin_filebackupstorage.go deleted file mode 100644 index ae1bec8e4a..0000000000 --- a/go/cmd/vttablet/plugin_filebackupstorage.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2015, Google Inc. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import ( - "github.com/youtube/vitess/go/vt/mysqlctl/backupstorage" - "github.com/youtube/vitess/go/vt/servenv" -) - -func init() { - servenv.OnRun(func() { - backupstorage.RegisterFileBackupStorage() - }) -} diff --git a/go/vt/mysqlctl/backupstorage/file.go b/go/vt/mysqlctl/backupstorage/file.go index 479afc754c..e293301031 100644 --- a/go/vt/mysqlctl/backupstorage/file.go +++ b/go/vt/mysqlctl/backupstorage/file.go @@ -45,7 +45,7 @@ func (fbh *FileBackupHandle) AddFile(filename string) (io.WriteCloser, error) { if fbh.readOnly { return nil, fmt.Errorf("AddFile cannot be called on read-only backup") } - p := path.Join(fbh.fbs.root, fbh.bucket, fbh.name, filename) + p := path.Join(*FileBackupStorageRoot, fbh.bucket, fbh.name, filename) return os.Create(p) } @@ -70,19 +70,17 @@ func (fbh *FileBackupHandle) ReadFile(filename string) (io.ReadCloser, error) { if !fbh.readOnly { return nil, fmt.Errorf("ReadFile cannot be called on read-write backup") } - p := path.Join(fbh.fbs.root, fbh.bucket, fbh.name, filename) + p := path.Join(*FileBackupStorageRoot, fbh.bucket, fbh.name, filename) return os.Open(p) } // FileBackupStorage implements BackupStorage for local file system. -type FileBackupStorage struct { - root string -} +type FileBackupStorage struct{} // ListBackups is part of the BackupStorage interface func (fbs *FileBackupStorage) ListBackups(bucket string) ([]BackupHandle, error) { // ReadDir already sorts the results - p := path.Join(fbs.root, bucket) + p := path.Join(*FileBackupStorageRoot, bucket) fi, err := ioutil.ReadDir(p) if err != nil { if os.IsNotExist(err) { @@ -112,7 +110,7 @@ func (fbs *FileBackupStorage) ListBackups(bucket string) ([]BackupHandle, error) // StartBackup is part of the BackupStorage interface func (fbs *FileBackupStorage) StartBackup(bucket, name string) (BackupHandle, error) { // make sure the bucket directory exists - p := path.Join(fbs.root, bucket) + p := path.Join(*FileBackupStorageRoot, bucket) if err := os.MkdirAll(p, os.ModePerm); err != nil { return nil, err } @@ -133,16 +131,10 @@ func (fbs *FileBackupStorage) StartBackup(bucket, name string) (BackupHandle, er // RemoveBackup is part of the BackupStorage interface func (fbs *FileBackupStorage) RemoveBackup(bucket, name string) error { - p := path.Join(fbs.root, bucket, name) + p := path.Join(*FileBackupStorageRoot, bucket, name) return os.RemoveAll(p) } -// RegisterFileBackupStorage should be called after Flags has been -// initialized, to register the FileBackupStorage implementation -func RegisterFileBackupStorage() { - if *FileBackupStorageRoot != "" { - BackupStorageMap["file"] = &FileBackupStorage{ - root: *FileBackupStorageRoot, - } - } +func init() { + BackupStorageMap["file"] = &FileBackupStorage{} } diff --git a/go/vt/mysqlctl/backupstorage/file_test.go b/go/vt/mysqlctl/backupstorage/file_test.go index 1d0d58279c..25f5eae6f9 100644 --- a/go/vt/mysqlctl/backupstorage/file_test.go +++ b/go/vt/mysqlctl/backupstorage/file_test.go @@ -25,14 +25,13 @@ func setupFileBackupStorage(t *testing.T) *FileBackupStorage { if err != nil { t.Fatalf("os.TempDir failed: %v", err) } - return &FileBackupStorage{ - root: root, - } + *FileBackupStorageRoot = root + return &FileBackupStorage{} } // cleanupFileBackupStorage removes the entire directory func cleanupFileBackupStorage(fbs *FileBackupStorage) { - os.RemoveAll(fbs.root) + os.RemoveAll(*FileBackupStorageRoot) } func TestListBackups(t *testing.T) { diff --git a/go/vt/wrangler/testlib/backup_test.go b/go/vt/wrangler/testlib/backup_test.go index 95b7c04072..02c01d13f3 100644 --- a/go/vt/wrangler/testlib/backup_test.go +++ b/go/vt/wrangler/testlib/backup_test.go @@ -40,7 +40,6 @@ func TestBackupRestore(t *testing.T) { fbsRoot := path.Join(root, "fbs") *backupstorage.FileBackupStorageRoot = fbsRoot *backupstorage.BackupStorageImplementation = "file" - backupstorage.RegisterFileBackupStorage() // Initialize the fake mysql root directories sourceInnodbDataDir := path.Join(root, "source_innodb_data")