syncer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 mirror implements etcd mirroring operations.
  15. package mirror
  16. import (
  17. "context"
  18. "go.etcd.io/etcd/clientv3"
  19. )
  20. const (
  21. batchLimit = 1000
  22. )
  23. // Syncer syncs with the key-value state of an etcd cluster.
  24. type Syncer interface {
  25. // SyncBase syncs the base state of the key-value state.
  26. // The key-value state are sent through the returned chan.
  27. SyncBase(ctx context.Context) (<-chan clientv3.GetResponse, chan error)
  28. // SyncUpdates syncs the updates of the key-value state.
  29. // The update events are sent through the returned chan.
  30. SyncUpdates(ctx context.Context) clientv3.WatchChan
  31. }
  32. // NewSyncer creates a Syncer.
  33. func NewSyncer(c *clientv3.Client, prefix string, rev int64) Syncer {
  34. return &syncer{c: c, prefix: prefix, rev: rev}
  35. }
  36. type syncer struct {
  37. c *clientv3.Client
  38. rev int64
  39. prefix string
  40. }
  41. func (s *syncer) SyncBase(ctx context.Context) (<-chan clientv3.GetResponse, chan error) {
  42. respchan := make(chan clientv3.GetResponse, 1024)
  43. errchan := make(chan error, 1)
  44. // if rev is not specified, we will choose the most recent revision.
  45. if s.rev == 0 {
  46. resp, err := s.c.Get(ctx, "foo")
  47. if err != nil {
  48. errchan <- err
  49. close(respchan)
  50. close(errchan)
  51. return respchan, errchan
  52. }
  53. s.rev = resp.Header.Revision
  54. }
  55. go func() {
  56. defer close(respchan)
  57. defer close(errchan)
  58. var key string
  59. opts := []clientv3.OpOption{clientv3.WithLimit(batchLimit), clientv3.WithRev(s.rev)}
  60. if len(s.prefix) == 0 {
  61. // If len(s.prefix) == 0, we will sync the entire key-value space.
  62. // We then range from the smallest key (0x00) to the end.
  63. opts = append(opts, clientv3.WithFromKey())
  64. key = "\x00"
  65. } else {
  66. // If len(s.prefix) != 0, we will sync key-value space with given prefix.
  67. // We then range from the prefix to the next prefix if exists. Or we will
  68. // range from the prefix to the end if the next prefix does not exists.
  69. opts = append(opts, clientv3.WithRange(clientv3.GetPrefixRangeEnd(s.prefix)))
  70. key = s.prefix
  71. }
  72. for {
  73. resp, err := s.c.Get(ctx, key, opts...)
  74. if err != nil {
  75. errchan <- err
  76. return
  77. }
  78. respchan <- *resp
  79. if !resp.More {
  80. return
  81. }
  82. // move to next key
  83. key = string(append(resp.Kvs[len(resp.Kvs)-1].Key, 0))
  84. }
  85. }()
  86. return respchan, errchan
  87. }
  88. func (s *syncer) SyncUpdates(ctx context.Context) clientv3.WatchChan {
  89. if s.rev == 0 {
  90. panic("unexpected revision = 0. Calling SyncUpdates before SyncBase finishes?")
  91. }
  92. return s.c.Watch(ctx, s.prefix, clientv3.WithPrefix(), clientv3.WithRev(s.rev+1))
  93. }