storage.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "go.etcd.io/etcd/etcdserver/api/snap"
  18. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  19. "go.etcd.io/etcd/pkg/pbutil"
  20. "go.etcd.io/etcd/pkg/types"
  21. "go.etcd.io/etcd/raft/raftpb"
  22. "go.etcd.io/etcd/wal"
  23. "go.etcd.io/etcd/wal/walpb"
  24. "go.uber.org/zap"
  25. )
  26. type Storage interface {
  27. // Save function saves ents and state to the underlying stable storage.
  28. // Save MUST block until st and ents are on stable storage.
  29. Save(st raftpb.HardState, ents []raftpb.Entry) error
  30. // SaveSnap function saves snapshot to the underlying stable storage.
  31. SaveSnap(snap raftpb.Snapshot) error
  32. // Close closes the Storage and performs finalization.
  33. Close() error
  34. }
  35. type storage struct {
  36. *wal.WAL
  37. *snap.Snapshotter
  38. }
  39. func NewStorage(w *wal.WAL, s *snap.Snapshotter) Storage {
  40. return &storage{w, s}
  41. }
  42. // SaveSnap saves the snapshot to disk and release the locked
  43. // wal files since they will not be used.
  44. func (st *storage) SaveSnap(snap raftpb.Snapshot) error {
  45. walsnap := walpb.Snapshot{
  46. Index: snap.Metadata.Index,
  47. Term: snap.Metadata.Term,
  48. }
  49. err := st.WAL.SaveSnapshot(walsnap)
  50. if err != nil {
  51. return err
  52. }
  53. err = st.Snapshotter.SaveSnap(snap)
  54. if err != nil {
  55. return err
  56. }
  57. return st.WAL.ReleaseLockTo(snap.Metadata.Index)
  58. }
  59. func readWAL(lg *zap.Logger, waldir string, snap walpb.Snapshot) (w *wal.WAL, id, cid types.ID, st raftpb.HardState, ents []raftpb.Entry) {
  60. var (
  61. err error
  62. wmetadata []byte
  63. )
  64. repaired := false
  65. for {
  66. if w, err = wal.Open(lg, waldir, snap); err != nil {
  67. if lg != nil {
  68. lg.Fatal("failed to open WAL", zap.Error(err))
  69. } else {
  70. plog.Fatalf("open wal error: %v", err)
  71. }
  72. }
  73. if wmetadata, st, ents, err = w.ReadAll(); err != nil {
  74. w.Close()
  75. // we can only repair ErrUnexpectedEOF and we never repair twice.
  76. if repaired || err != io.ErrUnexpectedEOF {
  77. if lg != nil {
  78. lg.Fatal("failed to read WAL, cannot be repaired", zap.Error(err))
  79. } else {
  80. plog.Fatalf("read wal error (%v) and cannot be repaired", err)
  81. }
  82. }
  83. if !wal.Repair(lg, waldir) {
  84. if lg != nil {
  85. lg.Fatal("failed to repair WAL", zap.Error(err))
  86. } else {
  87. plog.Fatalf("WAL error (%v) cannot be repaired", err)
  88. }
  89. } else {
  90. if lg != nil {
  91. lg.Info("repaired WAL", zap.Error(err))
  92. } else {
  93. plog.Infof("repaired WAL error (%v)", err)
  94. }
  95. repaired = true
  96. }
  97. continue
  98. }
  99. break
  100. }
  101. var metadata pb.Metadata
  102. pbutil.MustUnmarshal(&metadata, wmetadata)
  103. id = types.ID(metadata.NodeID)
  104. cid = types.ID(metadata.ClusterID)
  105. return w, id, cid, st, ents
  106. }