2016-05-13 06:49:40 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-12-08 18:52:54 +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.
|
|
|
|
|
|
|
|
package etcdserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2018-08-29 03:11:43 +03:00
|
|
|
"go.etcd.io/etcd/etcdserver/api/snap"
|
|
|
|
"go.etcd.io/etcd/mvcc/backend"
|
|
|
|
"go.etcd.io/etcd/raft/raftpb"
|
2018-04-16 08:40:44 +03:00
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
|
|
|
"go.uber.org/zap"
|
2015-12-08 18:52:54 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// createMergedSnapshotMessage creates a snapshot message that contains: raft status (term, conf),
|
|
|
|
// a snapshot of v2 store inside raft.Snapshot as []byte, a snapshot of v3 KV in the top level message
|
|
|
|
// as ReadCloser.
|
2018-05-21 11:23:19 +03:00
|
|
|
func (s *EtcdServer) createMergedSnapshotMessage(m raftpb.Message, snapt, snapi uint64, confState raftpb.ConfState) snap.Message {
|
2015-12-08 18:52:54 +03:00
|
|
|
// get a snapshot of v2 store as []byte
|
2018-02-26 22:06:38 +03:00
|
|
|
clone := s.v2store.Clone()
|
2015-12-08 18:52:54 +03:00
|
|
|
d, err := clone.SaveNoCopy()
|
|
|
|
if err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg := s.getLogger(); lg != nil {
|
|
|
|
lg.Panic("failed to save v2 store data", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("store save should never fail: %v", err)
|
|
|
|
}
|
2015-12-08 18:52:54 +03:00
|
|
|
}
|
|
|
|
|
2016-07-03 20:16:44 +03:00
|
|
|
// commit kv to write metadata(for example: consistent index).
|
|
|
|
s.KV().Commit()
|
2016-05-07 04:02:55 +03:00
|
|
|
dbsnap := s.be.Snapshot()
|
2015-12-08 18:52:54 +03:00
|
|
|
// get a snapshot of v3 KV as readCloser
|
2018-04-16 08:40:44 +03:00
|
|
|
rc := newSnapshotReaderCloser(s.getLogger(), dbsnap)
|
2015-12-08 18:52:54 +03:00
|
|
|
|
|
|
|
// put the []byte snapshot of store into raft snapshot and return the merged snapshot with
|
|
|
|
// KV readCloser snapshot.
|
|
|
|
snapshot := raftpb.Snapshot{
|
|
|
|
Metadata: raftpb.SnapshotMetadata{
|
|
|
|
Index: snapi,
|
|
|
|
Term: snapt,
|
|
|
|
ConfState: confState,
|
|
|
|
},
|
|
|
|
Data: d,
|
|
|
|
}
|
|
|
|
m.Snapshot = snapshot
|
|
|
|
|
2018-05-21 11:23:19 +03:00
|
|
|
return *snap.NewMessage(m, rc, dbsnap.Size())
|
2015-12-08 18:52:54 +03:00
|
|
|
}
|
|
|
|
|
2018-04-16 08:40:44 +03:00
|
|
|
func newSnapshotReaderCloser(lg *zap.Logger, snapshot backend.Snapshot) io.ReadCloser {
|
2015-12-08 18:52:54 +03:00
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
2016-02-17 03:05:20 +03:00
|
|
|
n, err := snapshot.WriteTo(pw)
|
|
|
|
if err == nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Info(
|
|
|
|
"sent database snapshot to writer",
|
|
|
|
zap.Int64("bytes", n),
|
|
|
|
zap.String("size", humanize.Bytes(uint64(n))),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Infof("wrote database snapshot out [total bytes: %d]", n)
|
|
|
|
}
|
2017-05-18 00:45:10 +03:00
|
|
|
} else {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Warn(
|
|
|
|
"failed to send database snapshot to writer",
|
|
|
|
zap.String("size", humanize.Bytes(uint64(n))),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
plog.Warningf("failed to write database snapshot out [written bytes: %d]: %v", n, err)
|
|
|
|
}
|
2016-02-17 03:05:20 +03:00
|
|
|
}
|
2015-12-08 18:52:54 +03:00
|
|
|
pw.CloseWithError(err)
|
2017-05-18 00:45:10 +03:00
|
|
|
err = snapshot.Close()
|
|
|
|
if err != nil {
|
2018-04-16 08:40:44 +03:00
|
|
|
if lg != nil {
|
|
|
|
lg.Panic("failed to close database snapshot", zap.Error(err))
|
|
|
|
} else {
|
|
|
|
plog.Panicf("failed to close database snapshot: %v", err)
|
|
|
|
}
|
2017-05-18 00:45:10 +03:00
|
|
|
}
|
2015-12-08 18:52:54 +03:00
|
|
|
}()
|
|
|
|
return pr
|
|
|
|
}
|