storage.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  18. "github.com/coreos/etcd/pkg/pbutil"
  19. "github.com/coreos/etcd/pkg/types"
  20. "github.com/coreos/etcd/raft/raftpb"
  21. "github.com/coreos/etcd/snap"
  22. "github.com/coreos/etcd/wal"
  23. "github.com/coreos/etcd/wal/walpb"
  24. )
  25. type Storage interface {
  26. // Save function saves ents and state to the underlying stable storage.
  27. // Save MUST block until st and ents are on stable storage.
  28. Save(st raftpb.HardState, ents []raftpb.Entry) error
  29. // SaveSnap function saves snapshot to the underlying stable storage.
  30. SaveSnap(snap raftpb.Snapshot) error
  31. // DBFilePath returns the file path of database snapshot saved with given
  32. // id.
  33. DBFilePath(id uint64) (string, 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. walsnap := walpb.Snapshot{
  48. Index: snap.Metadata.Index,
  49. Term: snap.Metadata.Term,
  50. }
  51. err := st.WAL.SaveSnapshot(walsnap)
  52. if err != nil {
  53. return err
  54. }
  55. err = st.Snapshotter.SaveSnap(snap)
  56. if err != nil {
  57. return err
  58. }
  59. return st.WAL.ReleaseLockTo(snap.Metadata.Index)
  60. }
  61. func readWAL(waldir string, snap walpb.Snapshot) (w *wal.WAL, id, cid types.ID, st raftpb.HardState, ents []raftpb.Entry) {
  62. var (
  63. err error
  64. wmetadata []byte
  65. )
  66. repaired := false
  67. for {
  68. if w, err = wal.Open(waldir, snap); err != nil {
  69. plog.Fatalf("open wal error: %v", err)
  70. }
  71. if wmetadata, st, ents, err = w.ReadAll(); err != nil {
  72. w.Close()
  73. // we can only repair ErrUnexpectedEOF and we never repair twice.
  74. if repaired || err != io.ErrUnexpectedEOF {
  75. plog.Fatalf("read wal error (%v) and cannot be repaired", err)
  76. }
  77. if !wal.Repair(waldir) {
  78. plog.Fatalf("WAL error (%v) cannot be repaired", err)
  79. } else {
  80. plog.Infof("repaired WAL error (%v)", err)
  81. repaired = true
  82. }
  83. continue
  84. }
  85. break
  86. }
  87. var metadata pb.Metadata
  88. pbutil.MustUnmarshal(&metadata, wmetadata)
  89. id = types.ID(metadata.NodeID)
  90. cid = types.ID(metadata.ClusterID)
  91. return
  92. }