vitess.io/vitess@v0.16.2/go/vt/mysqlctl/backupstorage/interface.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package backupstorage contains the interface and file system implementation
    18  // of the backup system.
    19  package backupstorage
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"io"
    25  
    26  	"github.com/spf13/pflag"
    27  
    28  	"vitess.io/vitess/go/vt/concurrency"
    29  	"vitess.io/vitess/go/vt/servenv"
    30  )
    31  
    32  var (
    33  	// BackupStorageImplementation is the implementation to use
    34  	// for BackupStorage. Exported for test purposes.
    35  	BackupStorageImplementation string
    36  	// FileSizeUnknown is a special value indicating that the file size is not known.
    37  	// This is typically used while creating a file programmatically, where it is
    38  	// impossible to compute the final size on disk ahead of time.
    39  	FileSizeUnknown = int64(-1)
    40  )
    41  
    42  func registerBackupFlags(fs *pflag.FlagSet) {
    43  	fs.StringVar(&BackupStorageImplementation, "backup_storage_implementation", "", "Which backup storage implementation to use for creating and restoring backups.")
    44  }
    45  
    46  func init() {
    47  	servenv.OnParseFor("vtbackup", registerBackupFlags)
    48  	servenv.OnParseFor("vtctl", registerBackupFlags)
    49  	servenv.OnParseFor("vtctld", registerBackupFlags)
    50  	servenv.OnParseFor("vttablet", registerBackupFlags)
    51  }
    52  
    53  // BackupHandle describes an individual backup.
    54  type BackupHandle interface {
    55  	// Directory is the location of the backup. Will contain keyspace/shard.
    56  	Directory() string
    57  
    58  	// Name is the individual name of the backup. Will contain
    59  	// tabletAlias-timestamp.
    60  	Name() string
    61  
    62  	// AddFile opens a new file to be added to the backup.
    63  	// Only works for read-write backups (created by StartBackup).
    64  	// filename is guaranteed to only contain alphanumerical
    65  	// characters and hyphens.
    66  	// It should be thread safe, it is possible to call AddFile in
    67  	// multiple go routines once a backup has been started.
    68  	// The context is valid for the duration of the writes, until the
    69  	// WriteCloser is closed.
    70  	// filesize should not be treated as an exact value but rather
    71  	// as an approximate value.
    72  	// A filesize of -1 should be treated as a special value indicating that
    73  	// the file size is unknown.
    74  	AddFile(ctx context.Context, filename string, filesize int64) (io.WriteCloser, error)
    75  
    76  	// EndBackup stops and closes a backup. The contents should be kept.
    77  	// Only works for read-write backups (created by StartBackup).
    78  	EndBackup(ctx context.Context) error
    79  
    80  	// AbortBackup stops a backup, and removes the contents that
    81  	// have been copied already. It is called if an error occurs
    82  	// while the backup is being taken, and the backup cannot be finished.
    83  	// Only works for read-write backups (created by StartBackup).
    84  	AbortBackup(ctx context.Context) error
    85  
    86  	// ReadFile starts reading a file from a backup.
    87  	// Only works for read-only backups (created by ListBackups).
    88  	// The context is valid for the duration of the reads, until the
    89  	// ReadCloser is closed.
    90  	ReadFile(ctx context.Context, filename string) (io.ReadCloser, error)
    91  
    92  	// concurrency.ErrorRecorder is embedded here to coordinate reporting and
    93  	// handling of errors among all the components involved in taking a backup.
    94  	concurrency.ErrorRecorder
    95  }
    96  
    97  // BackupStorage is the interface to the storage system
    98  type BackupStorage interface {
    99  	// ListBackups returns all the backups in a directory.  The
   100  	// returned backups are read-only (ReadFile can be called, but
   101  	// AddFile/EndBackup/AbortBackup cannot).
   102  	// The backups are string-sorted by Name(), ascending (ends up
   103  	// being the oldest backup first).
   104  	ListBackups(ctx context.Context, dir string) ([]BackupHandle, error)
   105  
   106  	// StartBackup creates a new backup with the given name.  If a
   107  	// backup with the same name already exists, it's an error.
   108  	// The returned backup is read-write
   109  	// (AddFile/EndBackup/AbortBackup can all be called, not
   110  	// ReadFile). The provided context is only valid for that
   111  	// function, and should not be stored by the implementation.
   112  	StartBackup(ctx context.Context, dir, name string) (BackupHandle, error)
   113  
   114  	// RemoveBackup removes all the data associated with a backup.
   115  	// It will not appear in ListBackups after RemoveBackup succeeds.
   116  	RemoveBackup(ctx context.Context, dir, name string) error
   117  
   118  	// Close frees resources associated with an active backup
   119  	// session, such as closing connections. Implementations of
   120  	// BackupStorage must support being reused after Close() is called.
   121  	Close() error
   122  }
   123  
   124  // BackupStorageMap contains the registered implementations for BackupStorage
   125  var BackupStorageMap = make(map[string]BackupStorage)
   126  
   127  // GetBackupStorage returns the current BackupStorage implementation.
   128  // Should be called after flags have been initialized.
   129  // When all operations are done, call BackupStorage.Close() to free resources.
   130  func GetBackupStorage() (BackupStorage, error) {
   131  	bs, ok := BackupStorageMap[BackupStorageImplementation]
   132  	if !ok {
   133  		return nil, fmt.Errorf("no registered implementation of BackupStorage")
   134  	}
   135  	return bs, nil
   136  }