syncer.go 3.1 KB

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