make_mirror_command.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2016 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 command
  15. import (
  16. "errors"
  17. "fmt"
  18. "strings"
  19. "sync/atomic"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/clientv3/mirror"
  23. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  24. "github.com/coreos/etcd/mvcc/mvccpb"
  25. "github.com/spf13/cobra"
  26. "golang.org/x/net/context"
  27. )
  28. var (
  29. mminsecureTr bool
  30. mmcert string
  31. mmkey string
  32. mmcacert string
  33. mmprefix string
  34. mmdestprefix string
  35. mmnodestprefix bool
  36. )
  37. // NewMakeMirrorCommand returns the cobra command for "makeMirror".
  38. func NewMakeMirrorCommand() *cobra.Command {
  39. c := &cobra.Command{
  40. Use: "make-mirror [options] <destination>",
  41. Short: "Makes a mirror at the destination etcd cluster",
  42. Run: makeMirrorCommandFunc,
  43. }
  44. c.Flags().StringVar(&mmprefix, "prefix", "", "Key-value prefix to mirror")
  45. c.Flags().StringVar(&mmdestprefix, "dest-prefix", "", "destination prefix to mirror a prefix to a different prefix in the destination cluster")
  46. c.Flags().BoolVar(&mmnodestprefix, "no-dest-prefix", false, "mirror key-values to the root of the destination cluster")
  47. c.Flags().StringVar(&mmcert, "dest-cert", "", "Identify secure client using this TLS certificate file for the destination cluster")
  48. c.Flags().StringVar(&mmkey, "dest-key", "", "Identify secure client using this TLS key file")
  49. c.Flags().StringVar(&mmcacert, "dest-cacert", "", "Verify certificates of TLS enabled secure servers using this CA bundle")
  50. // TODO: secure by default when etcd enables secure gRPC by default.
  51. c.Flags().BoolVar(&mminsecureTr, "dest-insecure-transport", true, "Disable transport security for client connections")
  52. return c
  53. }
  54. func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
  55. if len(args) != 1 {
  56. ExitWithError(ExitBadArgs, errors.New("make-mirror takes one destination argument."))
  57. }
  58. dialTimeout := dialTimeoutFromCmd(cmd)
  59. sec := &secureCfg{
  60. cert: mmcert,
  61. key: mmkey,
  62. cacert: mmcacert,
  63. insecureTransport: mminsecureTr,
  64. }
  65. dc := mustClient([]string{args[0]}, dialTimeout, sec, nil)
  66. c := mustClientFromCmd(cmd)
  67. err := makeMirror(context.TODO(), c, dc)
  68. ExitWithError(ExitError, err)
  69. }
  70. func makeMirror(ctx context.Context, c *clientv3.Client, dc *clientv3.Client) error {
  71. total := int64(0)
  72. go func() {
  73. for {
  74. time.Sleep(30 * time.Second)
  75. fmt.Println(atomic.LoadInt64(&total))
  76. }
  77. }()
  78. s := mirror.NewSyncer(c, mmprefix, 0)
  79. rc, errc := s.SyncBase(ctx)
  80. // if destination prefix is specified and remove destination prefix is true return error
  81. if mmnodestprefix && len(mmdestprefix) > 0 {
  82. ExitWithError(ExitBadArgs, fmt.Errorf("`--dest-prefix` and `--no-dest-prefix` cannot be set at the same time, choose one."))
  83. }
  84. // if remove destination prefix is false and destination prefix is empty set the value of destination prefix same as prefix
  85. if !mmnodestprefix && len(mmdestprefix) == 0 {
  86. mmdestprefix = mmprefix
  87. }
  88. for r := range rc {
  89. for _, kv := range r.Kvs {
  90. _, err := dc.Put(ctx, modifyPrefix(string(kv.Key)), string(kv.Value))
  91. if err != nil {
  92. return err
  93. }
  94. atomic.AddInt64(&total, 1)
  95. }
  96. }
  97. err := <-errc
  98. if err != nil {
  99. return err
  100. }
  101. wc := s.SyncUpdates(ctx)
  102. for wr := range wc {
  103. if wr.CompactRevision != 0 {
  104. return rpctypes.ErrCompacted
  105. }
  106. var lastRev int64
  107. ops := []clientv3.Op{}
  108. for _, ev := range wr.Events {
  109. nextRev := ev.Kv.ModRevision
  110. if lastRev != 0 && nextRev > lastRev {
  111. _, err := dc.Txn(ctx).Then(ops...).Commit()
  112. if err != nil {
  113. return err
  114. }
  115. ops = []clientv3.Op{}
  116. }
  117. lastRev = nextRev
  118. switch ev.Type {
  119. case mvccpb.PUT:
  120. ops = append(ops, clientv3.OpPut(modifyPrefix(string(ev.Kv.Key)), string(ev.Kv.Value)))
  121. atomic.AddInt64(&total, 1)
  122. case mvccpb.DELETE:
  123. ops = append(ops, clientv3.OpDelete(modifyPrefix(string(ev.Kv.Key))))
  124. atomic.AddInt64(&total, 1)
  125. default:
  126. panic("unexpected event type")
  127. }
  128. }
  129. if len(ops) != 0 {
  130. _, err := dc.Txn(ctx).Then(ops...).Commit()
  131. if err != nil {
  132. return err
  133. }
  134. }
  135. }
  136. return nil
  137. }
  138. func modifyPrefix(key string) string {
  139. return strings.Replace(key, mmprefix, mmdestprefix, 1)
  140. }