watcher.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2017 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 v2v3
  15. import (
  16. "context"
  17. "strings"
  18. "github.com/coreos/etcd/clientv3"
  19. etcdErr "github.com/coreos/etcd/error"
  20. "github.com/coreos/etcd/store"
  21. )
  22. func (s *v2v3Store) Watch(prefix string, recursive, stream bool, sinceIndex uint64) (store.Watcher, error) {
  23. ctx, cancel := context.WithCancel(s.ctx)
  24. wch := s.c.Watch(
  25. ctx,
  26. // TODO: very pricey; use a single store-wide watch in future
  27. s.pfx,
  28. clientv3.WithPrefix(),
  29. clientv3.WithRev(int64(sinceIndex)),
  30. clientv3.WithCreatedNotify(),
  31. clientv3.WithPrevKV())
  32. resp, ok := <-wch
  33. if err := resp.Err(); err != nil || !ok {
  34. cancel()
  35. return nil, etcdErr.NewError(etcdErr.EcodeRaftInternal, prefix, 0)
  36. }
  37. evc, donec := make(chan *store.Event), make(chan struct{})
  38. go func() {
  39. defer func() {
  40. close(evc)
  41. close(donec)
  42. }()
  43. for resp := range wch {
  44. for _, ev := range s.mkV2Events(resp) {
  45. k := ev.Node.Key
  46. if recursive {
  47. if !strings.HasPrefix(k, prefix) {
  48. continue
  49. }
  50. // accept events on hidden keys given in prefix
  51. k = strings.Replace(k, prefix, "/", 1)
  52. // ignore hidden keys deeper than prefix
  53. if strings.Contains(k, "/_") {
  54. continue
  55. }
  56. }
  57. if !recursive && k != prefix {
  58. continue
  59. }
  60. select {
  61. case evc <- ev:
  62. case <-ctx.Done():
  63. return
  64. }
  65. if !stream {
  66. return
  67. }
  68. }
  69. }
  70. }()
  71. return &v2v3Watcher{
  72. startRev: resp.Header.Revision,
  73. evc: evc,
  74. donec: donec,
  75. cancel: cancel,
  76. }, nil
  77. }
  78. func (s *v2v3Store) mkV2Events(wr clientv3.WatchResponse) (evs []*store.Event) {
  79. ak := s.mkActionKey()
  80. for _, rev := range mkRevs(wr) {
  81. var act, key *clientv3.Event
  82. for _, ev := range rev {
  83. if string(ev.Kv.Key) == ak {
  84. act = ev
  85. } else if key != nil && len(key.Kv.Key) < len(ev.Kv.Key) {
  86. // use longest key to ignore intermediate new
  87. // directories from Create.
  88. key = ev
  89. } else if key == nil {
  90. key = ev
  91. }
  92. }
  93. v2ev := &store.Event{
  94. Action: string(act.Kv.Value),
  95. Node: s.mkV2Node(key.Kv),
  96. PrevNode: s.mkV2Node(key.PrevKv),
  97. EtcdIndex: mkV2Rev(wr.Header.Revision),
  98. }
  99. evs = append(evs, v2ev)
  100. }
  101. return evs
  102. }
  103. func mkRevs(wr clientv3.WatchResponse) (revs [][]*clientv3.Event) {
  104. var curRev []*clientv3.Event
  105. for _, ev := range wr.Events {
  106. if curRev != nil && ev.Kv.ModRevision != curRev[0].Kv.ModRevision {
  107. revs = append(revs, curRev)
  108. curRev = nil
  109. }
  110. curRev = append(curRev, ev)
  111. }
  112. if curRev != nil {
  113. revs = append(revs, curRev)
  114. }
  115. return revs
  116. }
  117. type v2v3Watcher struct {
  118. startRev int64
  119. evc chan *store.Event
  120. donec chan struct{}
  121. cancel context.CancelFunc
  122. }
  123. func (w *v2v3Watcher) StartIndex() uint64 { return mkV2Rev(w.startRev) }
  124. func (w *v2v3Watcher) Remove() {
  125. w.cancel()
  126. <-w.donec
  127. }
  128. func (w *v2v3Watcher) EventChan() chan *store.Event { return w.evc }