storage.go 3.8 KB

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