storage.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. "log"
  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/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. // Close closes the Storage and performs finalization.
  35. Close() error
  36. }
  37. type storage struct {
  38. *wal.WAL
  39. *snap.Snapshotter
  40. }
  41. func NewStorage(w *wal.WAL, s *snap.Snapshotter) Storage {
  42. return &storage{w, s}
  43. }
  44. // SaveSnap saves the snapshot to disk and release the locked
  45. // wal files since they will not be used.
  46. func (st *storage) SaveSnap(snap raftpb.Snapshot) error {
  47. err := st.Snapshotter.SaveSnap(snap)
  48. if err != nil {
  49. return err
  50. }
  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.WAL.ReleaseLockTo(snap.Metadata.Index)
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. func readWAL(waldir string, snap walpb.Snapshot) (w *wal.WAL, id, cid types.ID, st raftpb.HardState, ents []raftpb.Entry) {
  66. var err error
  67. if w, err = wal.Open(waldir, snap); err != nil {
  68. log.Fatalf("etcdserver: open wal error: %v", err)
  69. }
  70. var wmetadata []byte
  71. if wmetadata, st, ents, err = w.ReadAll(); err != nil {
  72. log.Fatalf("etcdserver: read wal error: %v", err)
  73. }
  74. var metadata pb.Metadata
  75. pbutil.MustUnmarshal(&metadata, wmetadata)
  76. id = types.ID(metadata.NodeID)
  77. cid = types.ID(metadata.ClusterID)
  78. return
  79. }
  80. // upgradeWAL converts an older version of the etcdServer data to the newest version.
  81. // It must ensure that, after upgrading, the most recent version is present.
  82. func upgradeWAL(baseDataDir string, name string, ver wal.WalVersion) error {
  83. switch ver {
  84. case wal.WALv0_4:
  85. log.Print("etcdserver: converting v0.4 log to v2.0")
  86. err := migrate.Migrate4To2(baseDataDir, name)
  87. if err != nil {
  88. log.Fatalf("etcdserver: failed migrating data-dir: %v", err)
  89. return err
  90. }
  91. fallthrough
  92. case wal.WALv2_0:
  93. err := makeMemberDir(baseDataDir)
  94. if err != nil {
  95. return err
  96. }
  97. fallthrough
  98. case wal.WALv2_0_1:
  99. fallthrough
  100. default:
  101. log.Printf("datadir is valid for the 2.0.1 format")
  102. }
  103. return nil
  104. }
  105. func makeMemberDir(dir string) error {
  106. membdir := path.Join(dir, "member")
  107. _, err := os.Stat(membdir)
  108. switch {
  109. case err == nil:
  110. return nil
  111. case !os.IsNotExist(err):
  112. return err
  113. }
  114. if err := os.MkdirAll(membdir, 0700); err != nil {
  115. return err
  116. }
  117. names := []string{"snap", "wal"}
  118. for _, name := range names {
  119. if err := os.Rename(path.Join(dir, name), path.Join(membdir, name)); err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }