| 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 |
|
| 15 |
package etcdserver |
| 16 |
|
| 17 |
import ( |
| 18 |
"io" |
| 19 |
|
| 20 |
pb "github.com/coreos/etcd/etcdserver/etcdserverpb" |
| 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 |
|
| 29 |
type Storage interface { |
| 30 |
// Save function saves ents and state to the underlying stable storage. |
| 31 |
// Save MUST block until st and ents are on stable storage. |
| 32 |
Save(st raftpb.HardState, ents []raftpb.Entry) error |
| 33 |
// SaveSnap function saves snapshot to the underlying stable storage. |
| 34 |
SaveSnap(snap raftpb.Snapshot) error |
| 35 |
// Close closes the Storage and performs finalization. |
| 36 |
Close() error |
| 37 |
} |
| 38 |
|
| 39 |
type storage struct { |
| 40 |
*wal.WAL |
| 41 |
*snap.Snapshotter |
| 42 |
} |
| 43 |
|
| 44 |
func NewStorage(w *wal.WAL, s *snap.Snapshotter) Storage { |
| 45 |
return &storage{w, s} |
| 46 |
} |
| 47 |
|
| 48 |
// SaveSnap saves the snapshot to disk and release the locked |
| 49 |
// wal files since they will not be used. |
| 50 |
func (st *storage) SaveSnap(snap raftpb.Snapshot) error { |
| 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.Snapshotter.SaveSnap(snap) |
| 60 |
if err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
return st.WAL.ReleaseLockTo(snap.Metadata.Index) |
| 64 |
} |
| 65 |
|
| 66 |
func readWAL(waldir string, snap walpb.Snapshot) (w *wal.WAL, id, cid types.ID, st raftpb.HardState, ents []raftpb.Entry) { |
| 67 |
var ( |
| 68 |
err error |
| 69 |
wmetadata []byte |
| 70 |
) |
| 71 |
|
| 72 |
repaired := false |
| 73 |
for { |
| 74 |
if w, err = wal.Open(waldir, snap); err != nil { |
| 75 |
plog.Fatalf("open wal error: %v", err) |
| 76 |
} |
| 77 |
if wmetadata, st, ents, err = w.ReadAll(); err != nil { |
| 78 |
w.Close() |
| 79 |
// we can only repair ErrUnexpectedEOF and we never repair twice. |
| 80 |
if repaired || err != io.ErrUnexpectedEOF { |
| 81 |
plog.Fatalf("read wal error (%v) and cannot be repaired", err) |
| 82 |
} |
| 83 |
if !wal.Repair(waldir) { |
| 84 |
plog.Fatalf("WAL error (%v) cannot be repaired", err) |
| 85 |
} else { |
| 86 |
plog.Infof("repaired WAL error (%v)", err) |
| 87 |
repaired = true |
| 88 |
} |
| 89 |
continue |
| 90 |
} |
| 91 |
break |
| 92 |
} |
| 93 |
var metadata pb.Metadata |
| 94 |
pbutil.MustUnmarshal(&metadata, wmetadata) |
| 95 |
id = types.ID(metadata.NodeID) |
| 96 |
cid = types.ID(metadata.ClusterID) |
| 97 |
return w, id, cid, st, ents |
| 98 |
} |