khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | // Copyright 2015 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package snap |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | "io" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "time" |
| 25 | |
| 26 | "go.etcd.io/etcd/pkg/fileutil" |
| 27 | |
| 28 | humanize "github.com/dustin/go-humanize" |
| 29 | "go.uber.org/zap" |
| 30 | ) |
| 31 | |
| 32 | var ErrNoDBSnapshot = errors.New("snap: snapshot file doesn't exist") |
| 33 | |
| 34 | // SaveDBFrom saves snapshot of the database from the given reader. It |
| 35 | // guarantees the save operation is atomic. |
| 36 | func (s *Snapshotter) SaveDBFrom(r io.Reader, id uint64) (int64, error) { |
| 37 | start := time.Now() |
| 38 | |
| 39 | f, err := ioutil.TempFile(s.dir, "tmp") |
| 40 | if err != nil { |
| 41 | return 0, err |
| 42 | } |
| 43 | var n int64 |
| 44 | n, err = io.Copy(f, r) |
| 45 | if err == nil { |
| 46 | fsyncStart := time.Now() |
| 47 | err = fileutil.Fsync(f) |
| 48 | snapDBFsyncSec.Observe(time.Since(fsyncStart).Seconds()) |
| 49 | } |
| 50 | f.Close() |
| 51 | if err != nil { |
| 52 | os.Remove(f.Name()) |
| 53 | return n, err |
| 54 | } |
| 55 | fn := s.dbFilePath(id) |
| 56 | if fileutil.Exist(fn) { |
| 57 | os.Remove(f.Name()) |
| 58 | return n, nil |
| 59 | } |
| 60 | err = os.Rename(f.Name(), fn) |
| 61 | if err != nil { |
| 62 | os.Remove(f.Name()) |
| 63 | return n, err |
| 64 | } |
| 65 | |
| 66 | if s.lg != nil { |
| 67 | s.lg.Info( |
| 68 | "saved database snapshot to disk", |
| 69 | zap.String("path", fn), |
| 70 | zap.Int64("bytes", n), |
| 71 | zap.String("size", humanize.Bytes(uint64(n))), |
| 72 | ) |
| 73 | } else { |
| 74 | plog.Infof("saved database snapshot to disk [total bytes: %d]", n) |
| 75 | } |
| 76 | |
| 77 | snapDBSaveSec.Observe(time.Since(start).Seconds()) |
| 78 | return n, nil |
| 79 | } |
| 80 | |
| 81 | // DBFilePath returns the file path for the snapshot of the database with |
| 82 | // given id. If the snapshot does not exist, it returns error. |
| 83 | func (s *Snapshotter) DBFilePath(id uint64) (string, error) { |
| 84 | if _, err := fileutil.ReadDir(s.dir); err != nil { |
| 85 | return "", err |
| 86 | } |
| 87 | fn := s.dbFilePath(id) |
| 88 | if fileutil.Exist(fn) { |
| 89 | return fn, nil |
| 90 | } |
| 91 | if s.lg != nil { |
| 92 | s.lg.Warn( |
| 93 | "failed to find [SNAPSHOT-INDEX].snap.db", |
| 94 | zap.Uint64("snapshot-index", id), |
| 95 | zap.String("snapshot-file-path", fn), |
| 96 | zap.Error(ErrNoDBSnapshot), |
| 97 | ) |
| 98 | } |
| 99 | return "", ErrNoDBSnapshot |
| 100 | } |
| 101 | |
| 102 | func (s *Snapshotter) dbFilePath(id uint64) string { |
| 103 | return filepath.Join(s.dir, fmt.Sprintf("%016x.snap.db", id)) |
| 104 | } |