syncer.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 sync
  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. // SyncBase 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. kapi := clientv3.NewKV(s.c)
  44. // if rev is not specified, we will choose the most recent revision.
  45. if s.rev == 0 {
  46. resp, err := kapi.Get(ctx, "")
  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, end 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. // (For example, when the given prefix is 0xffff, the next prefix does not
  70. // exist).
  71. key = s.prefix
  72. end = string(incr([]byte(s.prefix)))
  73. if len(end) == 0 {
  74. opts = append(opts, clientv3.WithFromKey())
  75. } else {
  76. opts = append(opts, clientv3.WithRange(string(end)))
  77. }
  78. }
  79. for {
  80. resp, err := kapi.Get(ctx, key, opts...)
  81. if err != nil {
  82. errchan <- err
  83. return
  84. }
  85. respchan <- (clientv3.GetResponse)(*resp)
  86. if !resp.More {
  87. return
  88. }
  89. // move to next key
  90. key = string(append(resp.Kvs[len(resp.Kvs)-1].Key, 0))
  91. }
  92. }()
  93. return respchan, errchan
  94. }
  95. func (s *syncer) SyncUpdates(ctx context.Context) clientv3.WatchChan {
  96. if s.rev == 0 {
  97. panic("unexpected revision = 0. Calling SyncUpdates before SyncBase finishes?")
  98. }
  99. respchan := make(chan clientv3.WatchResponse, 1024)
  100. go func() {
  101. wapi := clientv3.NewWatcher(s.c)
  102. defer wapi.Close()
  103. defer close(respchan)
  104. // get all events since revision (or get non-compacted revision, if
  105. // rev is too far behind)
  106. wch := wapi.WatchPrefix(ctx, s.prefix, s.rev)
  107. for wr := range wch {
  108. respchan <- wr
  109. }
  110. }()
  111. return respchan
  112. }
  113. func incr(bs []byte) []byte {
  114. c := int8(1)
  115. for i := range bs {
  116. j := len(bs) - i - 1
  117. n := int8(bs[j])
  118. n += c
  119. bs[j] = byte(n)
  120. if n == 0 {
  121. c = 1
  122. } else {
  123. c = 0
  124. return bs
  125. }
  126. }
  127. return nil
  128. }