snapshotter.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package snap
  14. import (
  15. "errors"
  16. "fmt"
  17. "hash/crc32"
  18. "io/ioutil"
  19. "log"
  20. "os"
  21. "path"
  22. "sort"
  23. "strings"
  24. "github.com/coreos/etcd/pkg/pbutil"
  25. "github.com/coreos/etcd/raft"
  26. "github.com/coreos/etcd/raft/raftpb"
  27. "github.com/coreos/etcd/snap/snappb"
  28. )
  29. const (
  30. snapSuffix = ".snap"
  31. )
  32. var (
  33. ErrNoSnapshot = errors.New("snap: no available snapshot")
  34. ErrCRCMismatch = errors.New("snap: crc mismatch")
  35. crcTable = crc32.MakeTable(crc32.Castagnoli)
  36. )
  37. type Snapshotter struct {
  38. dir string
  39. }
  40. func New(dir string) *Snapshotter {
  41. return &Snapshotter{
  42. dir: dir,
  43. }
  44. }
  45. func (s *Snapshotter) SaveSnap(snapshot raftpb.Snapshot) error {
  46. if raft.IsEmptySnap(snapshot) {
  47. return nil
  48. }
  49. return s.save(&snapshot)
  50. }
  51. func (s *Snapshotter) save(snapshot *raftpb.Snapshot) error {
  52. fname := fmt.Sprintf("%016x-%016x%s", snapshot.Term, snapshot.Index, snapSuffix)
  53. b := pbutil.MustMarshal(snapshot)
  54. crc := crc32.Update(0, crcTable, b)
  55. snap := snappb.Snapshot{Crc: crc, Data: b}
  56. d, err := snap.Marshal()
  57. if err != nil {
  58. return err
  59. }
  60. return ioutil.WriteFile(path.Join(s.dir, fname), d, 0666)
  61. }
  62. func (s *Snapshotter) Load() (*raftpb.Snapshot, error) {
  63. names, err := s.snapNames()
  64. if err != nil {
  65. return nil, err
  66. }
  67. var snap *raftpb.Snapshot
  68. for _, name := range names {
  69. if snap, err = loadSnap(s.dir, name); err == nil {
  70. break
  71. }
  72. }
  73. return snap, err
  74. }
  75. func loadSnap(dir, name string) (*raftpb.Snapshot, error) {
  76. var err error
  77. var b []byte
  78. fpath := path.Join(dir, name)
  79. defer func() {
  80. if err != nil {
  81. renameBroken(fpath)
  82. }
  83. }()
  84. b, err = ioutil.ReadFile(fpath)
  85. if err != nil {
  86. log.Printf("snap: snapshotter cannot read file %v: %v", name, err)
  87. return nil, err
  88. }
  89. var serializedSnap snappb.Snapshot
  90. if err = serializedSnap.Unmarshal(b); err != nil {
  91. log.Printf("snap: corrupted snapshot file %v: %v", name, err)
  92. return nil, err
  93. }
  94. crc := crc32.Update(0, crcTable, serializedSnap.Data)
  95. if crc != serializedSnap.Crc {
  96. log.Printf("snap: corrupted snapshot file %v: crc mismatch", name)
  97. err = ErrCRCMismatch
  98. return nil, err
  99. }
  100. var snap raftpb.Snapshot
  101. if err = snap.Unmarshal(serializedSnap.Data); err != nil {
  102. log.Printf("snap: corrupted snapshot file %v: %v", name, err)
  103. return nil, err
  104. }
  105. return &snap, nil
  106. }
  107. // snapNames returns the filename of the snapshots in logical time order (from newest to oldest).
  108. // If there is no avaliable snapshots, an ErrNoSnapshot will be returned.
  109. func (s *Snapshotter) snapNames() ([]string, error) {
  110. dir, err := os.Open(s.dir)
  111. if err != nil {
  112. return nil, err
  113. }
  114. defer dir.Close()
  115. names, err := dir.Readdirnames(-1)
  116. if err != nil {
  117. return nil, err
  118. }
  119. snaps := checkSuffix(names)
  120. if len(snaps) == 0 {
  121. return nil, ErrNoSnapshot
  122. }
  123. sort.Sort(sort.Reverse(sort.StringSlice(snaps)))
  124. return snaps, nil
  125. }
  126. func checkSuffix(names []string) []string {
  127. snaps := []string{}
  128. for i := range names {
  129. if strings.HasSuffix(names[i], snapSuffix) {
  130. snaps = append(snaps, names[i])
  131. } else {
  132. log.Printf("snap: unexpected non-snap file %v", names[i])
  133. }
  134. }
  135. return snaps
  136. }
  137. func renameBroken(path string) {
  138. brokenPath := path + ".broken"
  139. if err := os.Rename(path, brokenPath); err != nil {
  140. log.Printf("snap: cannot rename broken snapshot file %v to %v: %v", path, brokenPath, err)
  141. }
  142. }