storage.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "io"
  17. "os"
  18. "path"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/pkg/pbutil"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft/raftpb"
  23. "github.com/coreos/etcd/snap"
  24. "github.com/coreos/etcd/version"
  25. "github.com/coreos/etcd/wal"
  26. "github.com/coreos/etcd/wal/walpb"
  27. )
  28. type Storage interface {
  29. // Save function saves ents and state to the underlying stable storage.
  30. // Save MUST block until st and ents are on stable storage.
  31. Save(st raftpb.HardState, ents []raftpb.Entry) error
  32. // SaveSnap function saves snapshot to the underlying stable storage.
  33. SaveSnap(snap raftpb.Snapshot) error
  34. // DBFilePath returns the file path of database snapshot saved with given
  35. // id.
  36. DBFilePath(id uint64) (string, error)
  37. // Close closes the Storage and performs finalization.
  38. Close() error
  39. }
  40. type storage struct {
  41. *wal.WAL
  42. *snap.Snapshotter
  43. }
  44. func NewStorage(w *wal.WAL, s *snap.Snapshotter) Storage {
  45. return &storage{w, s}
  46. }
  47. // SaveSnap saves the snapshot to disk and release the locked
  48. // wal files since they will not be used.
  49. func (st *storage) SaveSnap(snap raftpb.Snapshot) error {
  50. walsnap := walpb.Snapshot{
  51. Index: snap.Metadata.Index,
  52. Term: snap.Metadata.Term,
  53. }
  54. err := st.WAL.SaveSnapshot(walsnap)
  55. if err != nil {
  56. return err
  57. }
  58. err = st.Snapshotter.SaveSnap(snap)
  59. if err != nil {
  60. return err
  61. }
  62. err = st.WAL.ReleaseLockTo(snap.Metadata.Index)
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. func readWAL(waldir string, snap walpb.Snapshot) (w *wal.WAL, id, cid types.ID, st raftpb.HardState, ents []raftpb.Entry) {
  69. var (
  70. err error
  71. wmetadata []byte
  72. )
  73. repaired := false
  74. for {
  75. if w, err = wal.Open(waldir, snap); err != nil {
  76. plog.Fatalf("open wal error: %v", err)
  77. }
  78. if wmetadata, st, ents, err = w.ReadAll(); err != nil {
  79. w.Close()
  80. // we can only repair ErrUnexpectedEOF and we never repair twice.
  81. if repaired || err != io.ErrUnexpectedEOF {
  82. plog.Fatalf("read wal error (%v) and cannot be repaired", err)
  83. }
  84. if !wal.Repair(waldir) {
  85. plog.Fatalf("WAL error (%v) cannot be repaired", err)
  86. } else {
  87. plog.Infof("repaired WAL error (%v)", err)
  88. repaired = true
  89. }
  90. continue
  91. }
  92. break
  93. }
  94. var metadata pb.Metadata
  95. pbutil.MustUnmarshal(&metadata, wmetadata)
  96. id = types.ID(metadata.NodeID)
  97. cid = types.ID(metadata.ClusterID)
  98. return
  99. }
  100. // upgradeDataDir converts an older version of the etcdServer data to the newest version.
  101. // It must ensure that, after upgrading, the most recent version is present.
  102. func upgradeDataDir(baseDataDir string, name string, ver version.DataDirVersion) error {
  103. switch ver {
  104. case version.DataDir2_0:
  105. err := makeMemberDir(baseDataDir)
  106. if err != nil {
  107. return err
  108. }
  109. fallthrough
  110. case version.DataDir2_0_1:
  111. fallthrough
  112. default:
  113. }
  114. return nil
  115. }
  116. func makeMemberDir(dir string) error {
  117. membdir := path.Join(dir, "member")
  118. _, err := os.Stat(membdir)
  119. switch {
  120. case err == nil:
  121. return nil
  122. case !os.IsNotExist(err):
  123. return err
  124. }
  125. if err := os.MkdirAll(membdir, 0700); err != nil {
  126. return err
  127. }
  128. names := []string{"snap", "wal"}
  129. for _, name := range names {
  130. if err := os.Rename(path.Join(dir, name), path.Join(membdir, name)); err != nil {
  131. return err
  132. }
  133. }
  134. return nil
  135. }