storage.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Close closes the Storage and performs finalization.
  32. Close() error
  33. }
  34. type storage struct {
  35. *wal.WAL
  36. *snap.Snapshotter
  37. }
  38. func NewStorage(w *wal.WAL, s *snap.Snapshotter) Storage {
  39. return &storage{w, s}
  40. }
  41. // SaveSnap saves the snapshot to disk and release the locked
  42. // wal files since they will not be used.
  43. func (st *storage) SaveSnap(snap raftpb.Snapshot) error {
  44. walsnap := walpb.Snapshot{
  45. Index: snap.Metadata.Index,
  46. Term: snap.Metadata.Term,
  47. }
  48. err := st.WAL.SaveSnapshot(walsnap)
  49. if err != nil {
  50. return err
  51. }
  52. err = st.Snapshotter.SaveSnap(snap)
  53. if err != nil {
  54. return err
  55. }
  56. return st.WAL.ReleaseLockTo(snap.Metadata.Index)
  57. }
  58. func readWAL(waldir string, snap walpb.Snapshot) (w *wal.WAL, id, cid types.ID, st raftpb.HardState, ents []raftpb.Entry) {
  59. var (
  60. err error
  61. wmetadata []byte
  62. )
  63. repaired := false
  64. for {
  65. if w, err = wal.Open(waldir, snap); err != nil {
  66. plog.Fatalf("open wal error: %v", err)
  67. }
  68. if wmetadata, st, ents, err = w.ReadAll(); err != nil {
  69. w.Close()
  70. // we can only repair ErrUnexpectedEOF and we never repair twice.
  71. if repaired || err != io.ErrUnexpectedEOF {
  72. plog.Fatalf("read wal error (%v) and cannot be repaired", err)
  73. }
  74. if !wal.Repair(waldir) {
  75. plog.Fatalf("WAL error (%v) cannot be repaired", err)
  76. } else {
  77. plog.Infof("repaired WAL error (%v)", err)
  78. repaired = true
  79. }
  80. continue
  81. }
  82. break
  83. }
  84. var metadata pb.Metadata
  85. pbutil.MustUnmarshal(&metadata, wmetadata)
  86. id = types.ID(metadata.NodeID)
  87. cid = types.ID(metadata.ClusterID)
  88. return
  89. }