2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// 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 (
|
2016-01-04 22:24:49 +03:00
|
|
|
"fmt"
|
2015-03-28 20:33:52 +03:00
|
|
|
"io"
|
2015-02-12 02:24:32 +03:00
|
|
|
"os"
|
|
|
|
"path"
|
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"
|
2016-01-04 22:24:49 +03:00
|
|
|
"github.com/coreos/etcd/pkg/testutil"
|
2014-12-26 08:37:20 +03:00
|
|
|
"github.com/coreos/etcd/pkg/types"
|
2016-01-04 22:24:49 +03:00
|
|
|
"github.com/coreos/etcd/raft"
|
2014-12-12 23:48:44 +03:00
|
|
|
"github.com/coreos/etcd/raft/raftpb"
|
|
|
|
"github.com/coreos/etcd/snap"
|
2015-03-25 01:13:34 +03:00
|
|
|
"github.com/coreos/etcd/version"
|
2014-12-12 23:48:44 +03:00
|
|
|
"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
|
2015-12-08 18:52:54 +03:00
|
|
|
// DBFilePath returns the file path of database snapshot saved with given
|
|
|
|
// id.
|
|
|
|
DBFilePath(id uint64) (string, error)
|
2014-12-12 23:48:44 +03:00
|
|
|
// 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
|
|
|
|
}
|
2014-12-12 23:48:44 +03:00
|
|
|
err = st.WAL.ReleaseLockTo(snap.Metadata.Index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// upgradeWAL converts an older version of the etcdServer data to the newest version.
|
2014-12-25 09:02:46 +03:00
|
|
|
// It must ensure that, after upgrading, the most recent version is present.
|
2015-03-25 01:13:34 +03:00
|
|
|
func upgradeDataDir(baseDataDir string, name string, ver version.DataDirVersion) error {
|
2015-02-12 02:24:32 +03:00
|
|
|
switch ver {
|
2015-03-25 01:13:34 +03:00
|
|
|
case version.DataDir2_0:
|
2015-02-12 02:24:32 +03:00
|
|
|
err := makeMemberDir(baseDataDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fallthrough
|
2015-03-25 01:13:34 +03:00
|
|
|
case version.DataDir2_0_1:
|
2015-02-12 02:24:32 +03:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeMemberDir(dir string) error {
|
|
|
|
membdir := path.Join(dir, "member")
|
|
|
|
_, err := os.Stat(membdir)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return nil
|
|
|
|
case !os.IsNotExist(err):
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(membdir, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
names := []string{"snap", "wal"}
|
|
|
|
for _, name := range names {
|
|
|
|
if err := os.Rename(path.Join(dir, name), path.Join(membdir, name)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-25 09:02:46 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-04 22:24:49 +03:00
|
|
|
|
|
|
|
type storageRecorder struct {
|
2016-01-04 22:30:07 +03:00
|
|
|
testutil.RecorderBuffered
|
2016-01-04 22:24:49 +03:00
|
|
|
dbPath string // must have '/' suffix if set
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *storageRecorder) Save(st raftpb.HardState, ents []raftpb.Entry) error {
|
|
|
|
p.Record(testutil.Action{Name: "Save"})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *storageRecorder) SaveSnap(st raftpb.Snapshot) error {
|
|
|
|
if !raft.IsEmptySnap(st) {
|
|
|
|
p.Record(testutil.Action{Name: "SaveSnap"})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *storageRecorder) DBFilePath(id uint64) (string, error) {
|
|
|
|
p.Record(testutil.Action{Name: "DBFilePath"})
|
|
|
|
path := p.dbPath
|
|
|
|
if path != "" {
|
|
|
|
path = path + "/"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s%016x.snap.db", path, id), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *storageRecorder) Close() error { return nil }
|