snapshot_store.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2015 CoreOS, Inc.
  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 etcdserver
  15. import (
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "sync"
  22. "time"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/jonboulle/clockwork"
  24. "github.com/coreos/etcd/pkg/fileutil"
  25. "github.com/coreos/etcd/raft"
  26. "github.com/coreos/etcd/raft/raftpb"
  27. "github.com/coreos/etcd/rafthttp"
  28. dstorage "github.com/coreos/etcd/storage"
  29. )
  30. // clearUnusedSnapshotInterval specifies the time interval to wait
  31. // before clearing unused snapshot.
  32. // The newly created snapshot should be retrieved within one heartbeat
  33. // interval because raft state machine retries to send snapshot
  34. // to slow follower when receiving MsgHeartbeatResp from the follower.
  35. // Set it as 5s to match the upper limit of heartbeat interval.
  36. const clearUnusedSnapshotInterval = 5 * time.Second
  37. type snapshot struct {
  38. r raftpb.Snapshot
  39. io.ReadCloser // used to read out v3 snapshot
  40. done chan struct{}
  41. }
  42. func newSnapshot(r raftpb.Snapshot, kv dstorage.Snapshot) *snapshot {
  43. done := make(chan struct{})
  44. pr, pw := io.Pipe()
  45. go func() {
  46. _, err := kv.WriteTo(pw)
  47. pw.CloseWithError(err)
  48. kv.Close()
  49. close(done)
  50. }()
  51. return &snapshot{
  52. r: r,
  53. ReadCloser: pr,
  54. done: done,
  55. }
  56. }
  57. func (s *snapshot) raft() raftpb.Snapshot { return s.r }
  58. func (s *snapshot) isClosed() bool {
  59. select {
  60. case <-s.done:
  61. return true
  62. default:
  63. return false
  64. }
  65. }
  66. // TODO: remove snapshotStore. getSnap part could be put into memoryStorage,
  67. // while SaveFrom could be put into another struct, or even put into dstorage package.
  68. type snapshotStore struct {
  69. // dir to save snapshot data
  70. dir string
  71. kv dstorage.KV
  72. tr rafthttp.Transporter
  73. // send empty to reqsnapc to notify the channel receiver to send back latest
  74. // snapshot to snapc
  75. reqsnapc chan struct{}
  76. // a chan to receive the requested raft snapshot
  77. // snapshotStore will receive from the chan immediately after it sends empty to reqsnapc
  78. raftsnapc chan raftpb.Snapshot
  79. mu sync.Mutex // protect belowing vars
  80. // snap is nil iff there is no snapshot stored
  81. snap *snapshot
  82. inUse bool
  83. createOnce sync.Once // ensure at most one snapshot is created when no snapshot stored
  84. clock clockwork.Clock
  85. }
  86. func newSnapshotStore(dir string, kv dstorage.KV) *snapshotStore {
  87. return &snapshotStore{
  88. dir: dir,
  89. kv: kv,
  90. reqsnapc: make(chan struct{}),
  91. raftsnapc: make(chan raftpb.Snapshot),
  92. clock: clockwork.NewRealClock(),
  93. }
  94. }
  95. // getSnap returns a snapshot.
  96. // If there is no available snapshot, ErrSnapshotTemporarilyUnavaliable will be returned.
  97. //
  98. // If the snapshot stored is in use, it returns ErrSnapshotTemporarilyUnavailable.
  99. // If there is no snapshot stored, it creates new snapshot
  100. // asynchronously and returns ErrSnapshotTemporarilyUnavailable, so
  101. // caller could get snapshot later when the snapshot is created.
  102. // Otherwise, it returns the snapshot stored.
  103. //
  104. // The created snapshot is cleared from the snapshot store if it is
  105. // either unused after clearUnusedSnapshotInterval, or explicitly cleared
  106. // through clearUsedSnap after using.
  107. // closeSnapBefore is used to close outdated snapshot,
  108. // so the snapshot will be cleared faster when in use.
  109. //
  110. // snapshot store stores at most one snapshot at a time.
  111. // If raft state machine wants to send two snapshot messages to two followers,
  112. // the second snapshot message will keep getting snapshot and succeed only after
  113. // the first message is sent. This increases the time used to send messages,
  114. // but it is acceptable because this should happen seldomly.
  115. func (ss *snapshotStore) getSnap() (*snapshot, error) {
  116. ss.mu.Lock()
  117. defer ss.mu.Unlock()
  118. if ss.inUse {
  119. return nil, raft.ErrSnapshotTemporarilyUnavailable
  120. }
  121. if ss.snap == nil {
  122. // create snapshot asynchronously
  123. ss.createOnce.Do(func() { go ss.createSnap() })
  124. return nil, raft.ErrSnapshotTemporarilyUnavailable
  125. }
  126. ss.inUse = true
  127. // give transporter the generated snapshot that is ready to send out
  128. ss.tr.SnapshotReady(ss.snap, ss.snap.raft().Metadata.Index)
  129. return ss.snap, nil
  130. }
  131. // clearUsedSnap clears the snapshot from the snapshot store after it
  132. // is used.
  133. // After clear, snapshotStore could create new snapshot when getSnap.
  134. func (ss *snapshotStore) clearUsedSnap() {
  135. ss.mu.Lock()
  136. defer ss.mu.Unlock()
  137. if !ss.inUse {
  138. plog.Panicf("unexpected clearUsedSnap when snapshot is not in use")
  139. }
  140. ss.clear()
  141. }
  142. // closeSnapBefore closes the stored snapshot if its index is not greater
  143. // than the given compact index.
  144. // If it closes the snapshot, it returns true.
  145. func (ss *snapshotStore) closeSnapBefore(index uint64) bool {
  146. ss.mu.Lock()
  147. defer ss.mu.Unlock()
  148. if ss.snap != nil && ss.snap.raft().Metadata.Index <= index {
  149. if err := ss.snap.Close(); err != nil {
  150. plog.Errorf("snapshot close error (%v)", err)
  151. }
  152. return true
  153. }
  154. return false
  155. }
  156. // createSnap creates a new snapshot and stores it into the snapshot store.
  157. // It also sets a timer to clear the snapshot if it is not in use after
  158. // some time interval.
  159. // It should only be called in snapshotStore functions.
  160. func (ss *snapshotStore) createSnap() {
  161. // ask to generate v2 snapshot
  162. ss.reqsnapc <- struct{}{}
  163. // generate KV snapshot
  164. kvsnap := ss.kv.Snapshot()
  165. raftsnap := <-ss.raftsnapc
  166. snap := newSnapshot(raftsnap, kvsnap)
  167. ss.mu.Lock()
  168. ss.snap = snap
  169. ss.mu.Unlock()
  170. go func() {
  171. <-ss.clock.After(clearUnusedSnapshotInterval)
  172. ss.mu.Lock()
  173. defer ss.mu.Unlock()
  174. if snap == ss.snap && !ss.inUse {
  175. ss.clear()
  176. }
  177. }()
  178. }
  179. // clear clears snapshot related variables in snapshotStore. It closes
  180. // the snapshot stored and sets the variables to initial values.
  181. // It should only be called in snapshotStore functions.
  182. func (ss *snapshotStore) clear() {
  183. if err := ss.snap.Close(); err != nil {
  184. plog.Errorf("snapshot close error (%v)", err)
  185. }
  186. ss.snap = nil
  187. ss.inUse = false
  188. ss.createOnce = sync.Once{}
  189. }
  190. // SaveFrom saves snapshot at the given index from the given reader.
  191. // If the snapshot with the given index has been saved successfully, it keeps
  192. // the original saved snapshot and returns error.
  193. // The function guarantees that SaveFrom always saves either complete
  194. // snapshot or no snapshot, even if the call is aborted because program
  195. // is hard killed.
  196. func (ss *snapshotStore) SaveFrom(r io.Reader, index uint64) error {
  197. f, err := ioutil.TempFile(ss.dir, "tmp")
  198. if err != nil {
  199. return err
  200. }
  201. _, err = io.Copy(f, r)
  202. f.Close()
  203. if err != nil {
  204. os.Remove(f.Name())
  205. return err
  206. }
  207. fn := path.Join(ss.dir, fmt.Sprintf("%016x.db", index))
  208. if fileutil.Exist(fn) {
  209. os.Remove(f.Name())
  210. return fmt.Errorf("snapshot to save has existed")
  211. }
  212. err = os.Rename(f.Name(), fn)
  213. if err != nil {
  214. os.Remove(f.Name())
  215. return err
  216. }
  217. return nil
  218. }
  219. // getSnapFilePath returns the file path for the snapshot with given index.
  220. // If the snapshot does not exist, it returns error.
  221. func (ss *snapshotStore) getSnapFilePath(index uint64) (string, error) {
  222. fns, err := fileutil.ReadDir(ss.dir)
  223. if err != nil {
  224. return "", err
  225. }
  226. wfn := fmt.Sprintf("%016x.db", index)
  227. for _, fn := range fns {
  228. if fn == wfn {
  229. return path.Join(ss.dir, fn), nil
  230. }
  231. }
  232. return "", fmt.Errorf("snapshot file doesn't exist")
  233. }