snapshot_merge.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. package etcdserver
  15. import (
  16. "io"
  17. "github.com/coreos/etcd/mvcc/backend"
  18. "github.com/coreos/etcd/raft/raftpb"
  19. "github.com/coreos/etcd/snap"
  20. )
  21. // createMergedSnapshotMessage creates a snapshot message that contains: raft status (term, conf),
  22. // a snapshot of v2 store inside raft.Snapshot as []byte, a snapshot of v3 KV in the top level message
  23. // as ReadCloser.
  24. func (s *EtcdServer) createMergedSnapshotMessage(m raftpb.Message, snapt, snapi uint64, confState raftpb.ConfState) snap.Message {
  25. // get a snapshot of v2 store as []byte
  26. clone := s.store.Clone()
  27. d, err := clone.SaveNoCopy()
  28. if err != nil {
  29. plog.Panicf("store save should never fail: %v", err)
  30. }
  31. // commit kv to write metadata(for example: consistent index).
  32. s.KV().Commit()
  33. dbsnap := s.be.Snapshot()
  34. // get a snapshot of v3 KV as readCloser
  35. rc := newSnapshotReaderCloser(dbsnap)
  36. // put the []byte snapshot of store into raft snapshot and return the merged snapshot with
  37. // KV readCloser snapshot.
  38. snapshot := raftpb.Snapshot{
  39. Metadata: raftpb.SnapshotMetadata{
  40. Index: snapi,
  41. Term: snapt,
  42. ConfState: confState,
  43. },
  44. Data: d,
  45. }
  46. m.Snapshot = snapshot
  47. return *snap.NewMessage(m, rc, dbsnap.Size())
  48. }
  49. func newSnapshotReaderCloser(snapshot backend.Snapshot) io.ReadCloser {
  50. pr, pw := io.Pipe()
  51. go func() {
  52. n, err := snapshot.WriteTo(pw)
  53. if err == nil {
  54. plog.Infof("wrote database snapshot out [total bytes: %d]", n)
  55. } else {
  56. plog.Warningf("failed to write database snapshot out [written bytes: %d]: %v", n, err)
  57. }
  58. pw.CloseWithError(err)
  59. err = snapshot.Close()
  60. if err != nil {
  61. plog.Panicf("failed to close database snapshot: %v", err)
  62. }
  63. }()
  64. return pr
  65. }