snapshot_store.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "github.com/coreos/etcd/pkg/fileutil"
  22. "github.com/coreos/etcd/raft"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. dstorage "github.com/coreos/etcd/storage"
  25. )
  26. type snapshot struct {
  27. r raftpb.Snapshot
  28. kv dstorage.Snapshot
  29. }
  30. func (s *snapshot) raft() raftpb.Snapshot { return s.r }
  31. func (s *snapshot) size() int64 { return s.kv.Size() }
  32. func (s *snapshot) writeTo(w io.Writer) (n int64, err error) { return s.kv.WriteTo(w) }
  33. func (s *snapshot) close() error { return s.kv.Close() }
  34. type snapshotStore struct {
  35. // dir to save snapshot data
  36. dir string
  37. kv dstorage.KV
  38. // send empty to reqsnapc to notify the channel receiver to send back latest
  39. // snapshot to snapc
  40. reqsnapc chan struct{}
  41. // a chan to receive the requested raft snapshot
  42. // snapshotStore will receive from the chan immediately after it sends empty to reqsnapc
  43. raftsnapc chan raftpb.Snapshot
  44. snap *snapshot
  45. }
  46. func newSnapshotStore(dir string, kv dstorage.KV) *snapshotStore {
  47. return &snapshotStore{
  48. dir: dir,
  49. kv: kv,
  50. reqsnapc: make(chan struct{}),
  51. raftsnapc: make(chan raftpb.Snapshot),
  52. }
  53. }
  54. // getSnap returns a snapshot.
  55. // If there is no available snapshot, ErrSnapshotTemporarilyUnavaliable will be returned.
  56. func (ss *snapshotStore) getSnap() (*snapshot, error) {
  57. if ss.snap != nil {
  58. return nil, raft.ErrSnapshotTemporarilyUnavailable
  59. }
  60. // ask to generate v2 snapshot
  61. ss.reqsnapc <- struct{}{}
  62. // generate KV snapshot
  63. kvsnap := ss.kv.Snapshot()
  64. raftsnap := <-ss.raftsnapc
  65. ss.snap = &snapshot{
  66. r: raftsnap,
  67. kv: kvsnap,
  68. }
  69. return ss.snap, nil
  70. }
  71. // saveSnap saves snapshot into disk.
  72. //
  73. // If snapshot has existed in disk, it keeps the original snapshot and returns error.
  74. // The function guarantees that it always saves either complete snapshot or no snapshot,
  75. // even if the call is aborted because program is hard killed.
  76. func (ss *snapshotStore) saveSnap(s *snapshot) error {
  77. f, err := ioutil.TempFile(ss.dir, "tmp")
  78. if err != nil {
  79. return err
  80. }
  81. _, err = s.writeTo(f)
  82. f.Close()
  83. if err != nil {
  84. os.Remove(f.Name())
  85. return err
  86. }
  87. fn := path.Join(ss.dir, fmt.Sprintf("%016x.db", s.raft().Metadata.Index))
  88. if fileutil.Exist(fn) {
  89. os.Remove(f.Name())
  90. return fmt.Errorf("snapshot to save has existed")
  91. }
  92. err = os.Rename(f.Name(), fn)
  93. if err != nil {
  94. os.Remove(f.Name())
  95. return err
  96. }
  97. return nil
  98. }
  99. // getSnapFilePath returns the file path for the snapshot with given index.
  100. // If the snapshot does not exist, it returns error.
  101. func (ss *snapshotStore) getSnapFilePath(index uint64) (string, error) {
  102. fns, err := fileutil.ReadDir(ss.dir)
  103. if err != nil {
  104. return "", err
  105. }
  106. wfn := fmt.Sprintf("%016x.db", index)
  107. for _, fn := range fns {
  108. if fn == wfn {
  109. return path.Join(ss.dir, fn), nil
  110. }
  111. }
  112. return "", fmt.Errorf("snapshot file doesn't exist")
  113. }