2016-05-13 06:49:40 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2014-12-12 23:48:44 +03:00
|
|
|
package etcdserver
|
|
|
|
|
|
|
|
import (
|
2015-03-28 20:33:52 +03:00
|
|
|
"io"
|
2014-12-25 09:02:46 +03:00
|
|
|
|
2014-12-26 08:37:20 +03:00
|
|
|
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
|
|
|
|
"github.com/coreos/etcd/pkg/pbutil"
|
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2014-12-12 23:48:44 +03:00
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
|
|
|
"github.com/coreos/etcd/snap"
|
|
|
|
"github.com/coreos/etcd/wal"
|
2015-01-06 10:27:03 +03:00
|
|
|
"github.com/coreos/etcd/wal/walpb"
|
2014-12-12 23:48:44 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Storage interface {
|
|
|
|
// Save function saves ents and state to the underlying stable storage.
|
|
|
|
// Save MUST block until st and ents are on stable storage.
|
|
|
|
Save(st raftpb.HardState, ents []raftpb.Entry) error
|
|
|
|
// SaveSnap function saves snapshot to the underlying stable storage.
|
|
|
|
SaveSnap(snap raftpb.Snapshot) error
|
|
|
|
// Close closes the Storage and performs finalization.
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type storage struct {
|
|
|
|
*wal.WAL
|
|
|
|
*snap.Snapshotter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStorage(w *wal.WAL, s *snap.Snapshotter) Storage {
|
|
|
|
return &storage{w, s}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSnap saves the snapshot to disk and release the locked
|
|
|
|
// wal files since they will not be used.
|
|
|
|
func (st *storage) SaveSnap(snap raftpb.Snapshot) error {
|
2015-01-06 10:27:03 +03:00
|
|
|
walsnap := walpb.Snapshot{
|
|
|
|
Index: snap.Metadata.Index,
|
|
|
|
Term: snap.Metadata.Term,
|
|
|
|
}
|
2015-03-29 18:36:26 +03:00
|
|
|
err := st.WAL.SaveSnapshot(walsnap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = st.Snapshotter.SaveSnap(snap)
|
2015-01-06 10:27:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-08 21:36:41 +03:00
|
|
|
return st.WAL.ReleaseLockTo(snap.Metadata.Index)
|
2014-12-12 23:48:44 +03:00
|
|
|
}
|
2014-12-25 09:02:46 +03:00
|
|
|
|
2015-01-06 10:27:03 +03:00
|
|
|
func readWAL(waldir string, snap walpb.Snapshot) (w *wal.WAL, id, cid types.ID, st raftpb.HardState, ents []raftpb.Entry) {
|
2015-03-28 20:33:52 +03:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
wmetadata []byte
|
|
|
|
)
|
|
|
|
|
2015-03-30 07:05:26 +03:00
|
|
|
repaired := false
|
|
|
|
for {
|
2015-03-28 20:33:52 +03:00
|
|
|
if w, err = wal.Open(waldir, snap); err != nil {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Fatalf("open wal error: %v", err)
|
2015-03-28 20:33:52 +03:00
|
|
|
}
|
|
|
|
if wmetadata, st, ents, err = w.ReadAll(); err != nil {
|
|
|
|
w.Close()
|
2015-03-30 07:05:26 +03:00
|
|
|
// we can only repair ErrUnexpectedEOF and we never repair twice.
|
|
|
|
if repaired || err != io.ErrUnexpectedEOF {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Fatalf("read wal error (%v) and cannot be repaired", err)
|
2015-03-28 20:33:52 +03:00
|
|
|
}
|
|
|
|
if !wal.Repair(waldir) {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Fatalf("WAL error (%v) cannot be repaired", err)
|
2015-03-28 20:33:52 +03:00
|
|
|
} else {
|
2015-06-08 23:28:34 +03:00
|
|
|
plog.Infof("repaired WAL error (%v)", err)
|
2015-03-30 07:05:26 +03:00
|
|
|
repaired = true
|
2015-03-28 20:33:52 +03:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2014-12-26 08:37:20 +03:00
|
|
|
}
|
|
|
|
var metadata pb.Metadata
|
|
|
|
pbutil.MustUnmarshal(&metadata, wmetadata)
|
|
|
|
id = types.ID(metadata.NodeID)
|
|
|
|
cid = types.ID(metadata.ClusterID)
|
2017-11-11 05:28:57 +03:00
|
|
|
return w, id, cid, st, ents
|
2014-12-26 08:37:20 +03:00
|
|
|
}
|