snapshot_store.go 3.3 KB

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