snapshotter.go 6.7 KB

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