make_mirror_command.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "context"
  17. "errors"
  18. "fmt"
  19. "strings"
  20. "sync/atomic"
  21. "time"
  22. "github.com/coreos/etcd/clientv3"
  23. "github.com/coreos/etcd/clientv3/mirror"
  24. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  25. "github.com/coreos/etcd/mvcc/mvccpb"
  26. "github.com/spf13/cobra"
  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. keepAliveTime := keepAliveTimeFromCmd(cmd)
  60. keepAliveTimeout := keepAliveTimeoutFromCmd(cmd)
  61. sec := &secureCfg{
  62. cert: mmcert,
  63. key: mmkey,
  64. cacert: mmcacert,
  65. insecureTransport: mminsecureTr,
  66. }
  67. cc := &clientConfig{
  68. endpoints: []string{args[0]},
  69. dialTimeout: dialTimeout,
  70. keepAliveTime: keepAliveTime,
  71. keepAliveTimeout: keepAliveTimeout,
  72. scfg: sec,
  73. acfg: nil,
  74. }
  75. dc := cc.mustClient()
  76. c := mustClientFromCmd(cmd)
  77. err := makeMirror(context.TODO(), c, dc)
  78. ExitWithError(ExitError, err)
  79. }
  80. func makeMirror(ctx context.Context, c *clientv3.Client, dc *clientv3.Client) error {
  81. total := int64(0)
  82. go func() {
  83. for {
  84. time.Sleep(30 * time.Second)
  85. fmt.Println(atomic.LoadInt64(&total))
  86. }
  87. }()
  88. s := mirror.NewSyncer(c, mmprefix, 0)
  89. rc, errc := s.SyncBase(ctx)
  90. // if destination prefix is specified and remove destination prefix is true return error
  91. if mmnodestprefix && len(mmdestprefix) > 0 {
  92. ExitWithError(ExitBadArgs, fmt.Errorf("`--dest-prefix` and `--no-dest-prefix` cannot be set at the same time, choose one."))
  93. }
  94. // if remove destination prefix is false and destination prefix is empty set the value of destination prefix same as prefix
  95. if !mmnodestprefix && len(mmdestprefix) == 0 {
  96. mmdestprefix = mmprefix
  97. }
  98. for r := range rc {
  99. for _, kv := range r.Kvs {
  100. _, err := dc.Put(ctx, modifyPrefix(string(kv.Key)), string(kv.Value))
  101. if err != nil {
  102. return err
  103. }
  104. atomic.AddInt64(&total, 1)
  105. }
  106. }
  107. err := <-errc
  108. if err != nil {
  109. return err
  110. }
  111. wc := s.SyncUpdates(ctx)
  112. for wr := range wc {
  113. if wr.CompactRevision != 0 {
  114. return rpctypes.ErrCompacted
  115. }
  116. var lastRev int64
  117. ops := []clientv3.Op{}
  118. for _, ev := range wr.Events {
  119. nextRev := ev.Kv.ModRevision
  120. if lastRev != 0 && nextRev > lastRev {
  121. _, err := dc.Txn(ctx).Then(ops...).Commit()
  122. if err != nil {
  123. return err
  124. }
  125. ops = []clientv3.Op{}
  126. }
  127. lastRev = nextRev
  128. switch ev.Type {
  129. case mvccpb.PUT:
  130. ops = append(ops, clientv3.OpPut(modifyPrefix(string(ev.Kv.Key)), string(ev.Kv.Value)))
  131. atomic.AddInt64(&total, 1)
  132. case mvccpb.DELETE:
  133. ops = append(ops, clientv3.OpDelete(modifyPrefix(string(ev.Kv.Key))))
  134. atomic.AddInt64(&total, 1)
  135. default:
  136. panic("unexpected event type")
  137. }
  138. }
  139. if len(ops) != 0 {
  140. _, err := dc.Txn(ctx).Then(ops...).Commit()
  141. if err != nil {
  142. return err
  143. }
  144. }
  145. }
  146. return nil
  147. }
  148. func modifyPrefix(key string) string {
  149. return strings.Replace(key, mmprefix, mmdestprefix, 1)
  150. }