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