snapshot_merge.go 2.0 KB

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