khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -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 | "hash/crc32" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "sort" |
| 25 | "strings" |
| 26 | "time" |
| 27 | |
| 28 | "go.etcd.io/etcd/etcdserver/api/snap/snappb" |
| 29 | pioutil "go.etcd.io/etcd/pkg/ioutil" |
| 30 | "go.etcd.io/etcd/pkg/pbutil" |
| 31 | "go.etcd.io/etcd/raft" |
| 32 | "go.etcd.io/etcd/raft/raftpb" |
| 33 | |
| 34 | "github.com/coreos/pkg/capnslog" |
| 35 | "go.uber.org/zap" |
| 36 | ) |
| 37 | |
| 38 | const snapSuffix = ".snap" |
| 39 | |
| 40 | var ( |
| 41 | plog = capnslog.NewPackageLogger("go.etcd.io/etcd/v3", "snap") |
| 42 | |
| 43 | ErrNoSnapshot = errors.New("snap: no available snapshot") |
| 44 | ErrEmptySnapshot = errors.New("snap: empty snapshot") |
| 45 | ErrCRCMismatch = errors.New("snap: crc mismatch") |
| 46 | crcTable = crc32.MakeTable(crc32.Castagnoli) |
| 47 | |
| 48 | // A map of valid files that can be present in the snap folder. |
| 49 | validFiles = map[string]bool{ |
| 50 | "db": true, |
| 51 | } |
| 52 | ) |
| 53 | |
| 54 | type Snapshotter struct { |
| 55 | lg *zap.Logger |
| 56 | dir string |
| 57 | } |
| 58 | |
| 59 | func New(lg *zap.Logger, dir string) *Snapshotter { |
| 60 | return &Snapshotter{ |
| 61 | lg: lg, |
| 62 | dir: dir, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (s *Snapshotter) SaveSnap(snapshot raftpb.Snapshot) error { |
| 67 | if raft.IsEmptySnap(snapshot) { |
| 68 | return nil |
| 69 | } |
| 70 | return s.save(&snapshot) |
| 71 | } |
| 72 | |
| 73 | func (s *Snapshotter) save(snapshot *raftpb.Snapshot) error { |
| 74 | start := time.Now() |
| 75 | |
| 76 | fname := fmt.Sprintf("%016x-%016x%s", snapshot.Metadata.Term, snapshot.Metadata.Index, snapSuffix) |
| 77 | b := pbutil.MustMarshal(snapshot) |
| 78 | crc := crc32.Update(0, crcTable, b) |
| 79 | snap := snappb.Snapshot{Crc: crc, Data: b} |
| 80 | d, err := snap.Marshal() |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | snapMarshallingSec.Observe(time.Since(start).Seconds()) |
| 85 | |
| 86 | spath := filepath.Join(s.dir, fname) |
| 87 | |
| 88 | fsyncStart := time.Now() |
| 89 | err = pioutil.WriteAndSyncFile(spath, d, 0666) |
| 90 | snapFsyncSec.Observe(time.Since(fsyncStart).Seconds()) |
| 91 | |
| 92 | if err != nil { |
| 93 | if s.lg != nil { |
| 94 | s.lg.Warn("failed to write a snap file", zap.String("path", spath), zap.Error(err)) |
| 95 | } |
| 96 | rerr := os.Remove(spath) |
| 97 | if rerr != nil { |
| 98 | if s.lg != nil { |
| 99 | s.lg.Warn("failed to remove a broken snap file", zap.String("path", spath), zap.Error(err)) |
| 100 | } else { |
| 101 | plog.Errorf("failed to remove broken snapshot file %s", spath) |
| 102 | } |
| 103 | } |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | snapSaveSec.Observe(time.Since(start).Seconds()) |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func (s *Snapshotter) Load() (*raftpb.Snapshot, error) { |
| 112 | names, err := s.snapNames() |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | var snap *raftpb.Snapshot |
| 117 | for _, name := range names { |
| 118 | if snap, err = loadSnap(s.lg, s.dir, name); err == nil { |
| 119 | break |
| 120 | } |
| 121 | } |
| 122 | if err != nil { |
| 123 | return nil, ErrNoSnapshot |
| 124 | } |
| 125 | return snap, nil |
| 126 | } |
| 127 | |
| 128 | func loadSnap(lg *zap.Logger, dir, name string) (*raftpb.Snapshot, error) { |
| 129 | fpath := filepath.Join(dir, name) |
| 130 | snap, err := Read(lg, fpath) |
| 131 | if err != nil { |
| 132 | brokenPath := fpath + ".broken" |
| 133 | if lg != nil { |
| 134 | lg.Warn("failed to read a snap file", zap.String("path", fpath), zap.Error(err)) |
| 135 | } |
| 136 | if rerr := os.Rename(fpath, brokenPath); rerr != nil { |
| 137 | if lg != nil { |
| 138 | lg.Warn("failed to rename a broken snap file", zap.String("path", fpath), zap.String("broken-path", brokenPath), zap.Error(rerr)) |
| 139 | } else { |
| 140 | plog.Warningf("cannot rename broken snapshot file %v to %v: %v", fpath, brokenPath, rerr) |
| 141 | } |
| 142 | } else { |
| 143 | if lg != nil { |
| 144 | lg.Warn("renamed to a broken snap file", zap.String("path", fpath), zap.String("broken-path", brokenPath)) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | return snap, err |
| 149 | } |
| 150 | |
| 151 | // Read reads the snapshot named by snapname and returns the snapshot. |
| 152 | func Read(lg *zap.Logger, snapname string) (*raftpb.Snapshot, error) { |
| 153 | b, err := ioutil.ReadFile(snapname) |
| 154 | if err != nil { |
| 155 | if lg != nil { |
| 156 | lg.Warn("failed to read a snap file", zap.String("path", snapname), zap.Error(err)) |
| 157 | } else { |
| 158 | plog.Errorf("cannot read file %v: %v", snapname, err) |
| 159 | } |
| 160 | return nil, err |
| 161 | } |
| 162 | |
| 163 | if len(b) == 0 { |
| 164 | if lg != nil { |
| 165 | lg.Warn("failed to read empty snapshot file", zap.String("path", snapname)) |
| 166 | } else { |
| 167 | plog.Errorf("unexpected empty snapshot") |
| 168 | } |
| 169 | return nil, ErrEmptySnapshot |
| 170 | } |
| 171 | |
| 172 | var serializedSnap snappb.Snapshot |
| 173 | if err = serializedSnap.Unmarshal(b); err != nil { |
| 174 | if lg != nil { |
| 175 | lg.Warn("failed to unmarshal snappb.Snapshot", zap.String("path", snapname), zap.Error(err)) |
| 176 | } else { |
| 177 | plog.Errorf("corrupted snapshot file %v: %v", snapname, err) |
| 178 | } |
| 179 | return nil, err |
| 180 | } |
| 181 | |
| 182 | if len(serializedSnap.Data) == 0 || serializedSnap.Crc == 0 { |
| 183 | if lg != nil { |
| 184 | lg.Warn("failed to read empty snapshot data", zap.String("path", snapname)) |
| 185 | } else { |
| 186 | plog.Errorf("unexpected empty snapshot") |
| 187 | } |
| 188 | return nil, ErrEmptySnapshot |
| 189 | } |
| 190 | |
| 191 | crc := crc32.Update(0, crcTable, serializedSnap.Data) |
| 192 | if crc != serializedSnap.Crc { |
| 193 | if lg != nil { |
| 194 | lg.Warn("snap file is corrupt", |
| 195 | zap.String("path", snapname), |
| 196 | zap.Uint32("prev-crc", serializedSnap.Crc), |
| 197 | zap.Uint32("new-crc", crc), |
| 198 | ) |
| 199 | } else { |
| 200 | plog.Errorf("corrupted snapshot file %v: crc mismatch", snapname) |
| 201 | } |
| 202 | return nil, ErrCRCMismatch |
| 203 | } |
| 204 | |
| 205 | var snap raftpb.Snapshot |
| 206 | if err = snap.Unmarshal(serializedSnap.Data); err != nil { |
| 207 | if lg != nil { |
| 208 | lg.Warn("failed to unmarshal raftpb.Snapshot", zap.String("path", snapname), zap.Error(err)) |
| 209 | } else { |
| 210 | plog.Errorf("corrupted snapshot file %v: %v", snapname, err) |
| 211 | } |
| 212 | return nil, err |
| 213 | } |
| 214 | return &snap, nil |
| 215 | } |
| 216 | |
| 217 | // snapNames returns the filename of the snapshots in logical time order (from newest to oldest). |
| 218 | // If there is no available snapshots, an ErrNoSnapshot will be returned. |
| 219 | func (s *Snapshotter) snapNames() ([]string, error) { |
| 220 | dir, err := os.Open(s.dir) |
| 221 | if err != nil { |
| 222 | return nil, err |
| 223 | } |
| 224 | defer dir.Close() |
| 225 | names, err := dir.Readdirnames(-1) |
| 226 | if err != nil { |
| 227 | return nil, err |
| 228 | } |
| 229 | snaps := checkSuffix(s.lg, names) |
| 230 | if len(snaps) == 0 { |
| 231 | return nil, ErrNoSnapshot |
| 232 | } |
| 233 | sort.Sort(sort.Reverse(sort.StringSlice(snaps))) |
| 234 | return snaps, nil |
| 235 | } |
| 236 | |
| 237 | func checkSuffix(lg *zap.Logger, names []string) []string { |
| 238 | snaps := []string{} |
| 239 | for i := range names { |
| 240 | if strings.HasSuffix(names[i], snapSuffix) { |
| 241 | snaps = append(snaps, names[i]) |
| 242 | } else { |
| 243 | // If we find a file which is not a snapshot then check if it's |
| 244 | // a vaild file. If not throw out a warning. |
| 245 | if _, ok := validFiles[names[i]]; !ok { |
| 246 | if lg != nil { |
| 247 | lg.Warn("found unexpected non-snap file; skipping", zap.String("path", names[i])) |
| 248 | } else { |
| 249 | plog.Warningf("skipped unexpected non snapshot file %v", names[i]) |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | return snaps |
| 255 | } |