snapshot_command.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/clientv3"
  20. "github.com/coreos/etcd/clientv3/mirror"
  21. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  22. "github.com/spf13/cobra"
  23. "golang.org/x/net/context"
  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. wr := <-c.Watch(context.TODO(), "", clientv3.WithPrefix(), clientv3.WithRev(1))
  50. if wr.Err() == nil {
  51. wr.CompactRevision = 1
  52. }
  53. if rev := snapshot(os.Stdout, c, wr.CompactRevision+1); rev != 0 {
  54. err := fmt.Errorf("snapshot interrupted by compaction %v", rev)
  55. ExitWithError(ExitInterrupted, err)
  56. }
  57. os.Stdout.Sync()
  58. }
  59. // snapshotToFile atomically writes a snapshot to a file
  60. func snapshotToFile(c *clientv3.Client, path string) {
  61. partpath := path + ".part"
  62. f, err := os.Create(partpath)
  63. defer f.Close()
  64. if err != nil {
  65. exiterr := fmt.Errorf("could not open %s (%v)", partpath, err)
  66. ExitWithError(ExitBadArgs, exiterr)
  67. }
  68. rev := int64(1)
  69. for rev != 0 {
  70. f.Seek(0, 0)
  71. f.Truncate(0)
  72. rev = snapshot(f, c, rev)
  73. }
  74. f.Sync()
  75. if err := os.Rename(partpath, path); err != nil {
  76. exiterr := fmt.Errorf("could not rename %s to %s (%v)", partpath, path, err)
  77. ExitWithError(ExitIO, exiterr)
  78. }
  79. }
  80. // snapshot reads all of a watcher; returns compaction revision if incomplete
  81. // TODO: stabilize snapshot format
  82. func snapshot(w io.Writer, c *clientv3.Client, rev int64) int64 {
  83. s := mirror.NewSyncer(c, "", rev)
  84. rc, errc := s.SyncBase(context.TODO())
  85. for r := range rc {
  86. for _, kv := range r.Kvs {
  87. fmt.Fprintln(w, kv)
  88. }
  89. }
  90. err := <-errc
  91. if err != nil {
  92. if err == rpctypes.ErrCompacted {
  93. // will get correct compact revision on retry
  94. return rev + 1
  95. }
  96. // failed for some unknown reason, retry on same revision
  97. return rev
  98. }
  99. wc := s.SyncUpdates(context.TODO())
  100. for wr := range wc {
  101. if wr.Err() != nil {
  102. return wr.CompactRevision
  103. }
  104. for _, ev := range wr.Events {
  105. fmt.Fprintln(w, ev)
  106. }
  107. rev := wr.Events[len(wr.Events)-1].Kv.ModRevision
  108. if rev >= wr.Header.Revision {
  109. break
  110. }
  111. }
  112. return 0
  113. }