storage.go 3.9 KB

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