make_mirror_command.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. dc := mustClient([]string{args[0]}, dialTimeout, keepAliveTime, keepAliveTimeout, sec, nil)
  68. c := mustClientFromCmd(cmd)
  69. err := makeMirror(context.TODO(), c, dc)
  70. ExitWithError(ExitError, err)
  71. }
  72. func makeMirror(ctx context.Context, c *clientv3.Client, dc *clientv3.Client) error {
  73. total := int64(0)
  74. go func() {
  75. for {
  76. time.Sleep(30 * time.Second)
  77. fmt.Println(atomic.LoadInt64(&total))
  78. }
  79. }()
  80. s := mirror.NewSyncer(c, mmprefix, 0)
  81. rc, errc := s.SyncBase(ctx)
  82. // if destination prefix is specified and remove destination prefix is true return error
  83. if mmnodestprefix && len(mmdestprefix) > 0 {
  84. ExitWithError(ExitBadArgs, fmt.Errorf("`--dest-prefix` and `--no-dest-prefix` cannot be set at the same time, choose one."))
  85. }
  86. // if remove destination prefix is false and destination prefix is empty set the value of destination prefix same as prefix
  87. if !mmnodestprefix && len(mmdestprefix) == 0 {
  88. mmdestprefix = mmprefix
  89. }
  90. for r := range rc {
  91. for _, kv := range r.Kvs {
  92. _, err := dc.Put(ctx, modifyPrefix(string(kv.Key)), string(kv.Value))
  93. if err != nil {
  94. return err
  95. }
  96. atomic.AddInt64(&total, 1)
  97. }
  98. }
  99. err := <-errc
  100. if err != nil {
  101. return err
  102. }
  103. wc := s.SyncUpdates(ctx)
  104. for wr := range wc {
  105. if wr.CompactRevision != 0 {
  106. return rpctypes.ErrCompacted
  107. }
  108. var lastRev int64
  109. ops := []clientv3.Op{}
  110. for _, ev := range wr.Events {
  111. nextRev := ev.Kv.ModRevision
  112. if lastRev != 0 && nextRev > lastRev {
  113. _, err := dc.Txn(ctx).Then(ops...).Commit()
  114. if err != nil {
  115. return err
  116. }
  117. ops = []clientv3.Op{}
  118. }
  119. lastRev = nextRev
  120. switch ev.Type {
  121. case mvccpb.PUT:
  122. ops = append(ops, clientv3.OpPut(modifyPrefix(string(ev.Kv.Key)), string(ev.Kv.Value)))
  123. atomic.AddInt64(&total, 1)
  124. case mvccpb.DELETE:
  125. ops = append(ops, clientv3.OpDelete(modifyPrefix(string(ev.Kv.Key))))
  126. atomic.AddInt64(&total, 1)
  127. default:
  128. panic("unexpected event type")
  129. }
  130. }
  131. if len(ops) != 0 {
  132. _, err := dc.Txn(ctx).Then(ops...).Commit()
  133. if err != nil {
  134. return err
  135. }
  136. }
  137. }
  138. return nil
  139. }
  140. func modifyPrefix(key string) string {
  141. return strings.Replace(key, mmprefix, mmdestprefix, 1)
  142. }