make_mirror_command.go 3.9 KB

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