snapshot_command.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 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 command
  15. import (
  16. "fmt"
  17. "io"
  18. "os"
  19. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/clientv3/mirror"
  23. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  24. )
  25. // NewSnapshotCommand returns the cobra command for "snapshot".
  26. func NewSnapshotCommand() *cobra.Command {
  27. return &cobra.Command{
  28. Use: "snapshot [filename]",
  29. Short: "Snapshot streams a point-in-time snapshot of the store",
  30. Run: snapshotCommandFunc,
  31. }
  32. }
  33. // snapshotCommandFunc watches for the length of the entire store and records
  34. // to a file.
  35. func snapshotCommandFunc(cmd *cobra.Command, args []string) {
  36. switch {
  37. case len(args) == 0:
  38. snapshotToStdout(mustClientFromCmd(cmd))
  39. case len(args) == 1:
  40. snapshotToFile(mustClientFromCmd(cmd), args[0])
  41. default:
  42. err := fmt.Errorf("snapshot takes at most one argument")
  43. ExitWithError(ExitBadArgs, err)
  44. }
  45. }
  46. // snapshotToStdout streams a snapshot over stdout
  47. func snapshotToStdout(c *clientv3.Client) {
  48. // must explicitly fetch first revision since no retry on stdout
  49. wapi := clientv3.NewWatcher(c)
  50. defer wapi.Close()
  51. wr := <-wapi.Watch(context.TODO(), "", clientv3.WithPrefix(), clientv3.WithRev(1))
  52. if len(wr.Events) > 0 {
  53. wr.CompactRevision = 1
  54. }
  55. if rev := snapshot(os.Stdout, c, wr.CompactRevision+1); rev != 0 {
  56. err := fmt.Errorf("snapshot interrupted by compaction %v", rev)
  57. ExitWithError(ExitInterrupted, err)
  58. }
  59. os.Stdout.Sync()
  60. }
  61. // snapshotToFile atomically writes a snapshot to a file
  62. func snapshotToFile(c *clientv3.Client, path string) {
  63. partpath := path + ".part"
  64. f, err := os.Create(partpath)
  65. defer f.Close()
  66. if err != nil {
  67. exiterr := fmt.Errorf("could not open %s (%v)", partpath, err)
  68. ExitWithError(ExitBadArgs, exiterr)
  69. }
  70. rev := int64(1)
  71. for rev != 0 {
  72. f.Seek(0, 0)
  73. f.Truncate(0)
  74. rev = snapshot(f, c, rev)
  75. }
  76. f.Sync()
  77. if err := os.Rename(partpath, path); err != nil {
  78. exiterr := fmt.Errorf("could not rename %s to %s (%v)", partpath, path, err)
  79. ExitWithError(ExitIO, exiterr)
  80. }
  81. }
  82. // snapshot reads all of a watcher; returns compaction revision if incomplete
  83. // TODO: stabilize snapshot format
  84. func snapshot(w io.Writer, c *clientv3.Client, rev int64) int64 {
  85. s := mirror.NewSyncer(c, "", rev)
  86. rc, errc := s.SyncBase(context.TODO())
  87. for r := range rc {
  88. for _, kv := range r.Kvs {
  89. fmt.Fprintln(w, kv)
  90. }
  91. }
  92. err := <-errc
  93. if err != nil {
  94. if err == v3rpc.ErrCompacted {
  95. // will get correct compact revision on retry
  96. return rev + 1
  97. }
  98. // failed for some unknown reason, retry on same revision
  99. return rev
  100. }
  101. wc := s.SyncUpdates(context.TODO())
  102. for wr := range wc {
  103. if len(wr.Events) == 0 {
  104. return wr.CompactRevision
  105. }
  106. for _, ev := range wr.Events {
  107. fmt.Fprintln(w, ev)
  108. }
  109. rev := wr.Events[len(wr.Events)-1].Kv.ModRevision
  110. if rev >= wr.Header.Revision {
  111. break
  112. }
  113. }
  114. return 0
  115. }