make_mirror_command.go 3.6 KB

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