watcher_group.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 mvcc
  15. import (
  16. "fmt"
  17. "math"
  18. "go.etcd.io/etcd/mvcc/mvccpb"
  19. "go.etcd.io/etcd/pkg/adt"
  20. )
  21. var (
  22. // watchBatchMaxRevs is the maximum distinct revisions that
  23. // may be sent to an unsynced watcher at a time. Declared as
  24. // var instead of const for testing purposes.
  25. watchBatchMaxRevs = 1000
  26. )
  27. type eventBatch struct {
  28. // evs is a batch of revision-ordered events
  29. evs []mvccpb.Event
  30. // revs is the minimum unique revisions observed for this batch
  31. revs int
  32. // moreRev is first revision with more events following this batch
  33. moreRev int64
  34. }
  35. func (eb *eventBatch) add(ev mvccpb.Event) {
  36. if eb.revs > watchBatchMaxRevs {
  37. // maxed out batch size
  38. return
  39. }
  40. if len(eb.evs) == 0 {
  41. // base case
  42. eb.revs = 1
  43. eb.evs = append(eb.evs, ev)
  44. return
  45. }
  46. // revision accounting
  47. ebRev := eb.evs[len(eb.evs)-1].Kv.ModRevision
  48. evRev := ev.Kv.ModRevision
  49. if evRev > ebRev {
  50. eb.revs++
  51. if eb.revs > watchBatchMaxRevs {
  52. eb.moreRev = evRev
  53. return
  54. }
  55. }
  56. eb.evs = append(eb.evs, ev)
  57. }
  58. type watcherBatch map[*watcher]*eventBatch
  59. func (wb watcherBatch) add(w *watcher, ev mvccpb.Event) {
  60. eb := wb[w]
  61. if eb == nil {
  62. eb = &eventBatch{}
  63. wb[w] = eb
  64. }
  65. eb.add(ev)
  66. }
  67. // newWatcherBatch maps watchers to their matched events. It enables quick
  68. // events look up by watcher.
  69. func newWatcherBatch(wg *watcherGroup, evs []mvccpb.Event) watcherBatch {
  70. if len(wg.watchers) == 0 {
  71. return nil
  72. }
  73. wb := make(watcherBatch)
  74. for _, ev := range evs {
  75. for w := range wg.watcherSetByKey(string(ev.Kv.Key)) {
  76. if ev.Kv.ModRevision >= w.minRev {
  77. // don't double notify
  78. wb.add(w, ev)
  79. }
  80. }
  81. }
  82. return wb
  83. }
  84. type watcherSet map[*watcher]struct{}
  85. func (w watcherSet) add(wa *watcher) {
  86. if _, ok := w[wa]; ok {
  87. panic("add watcher twice!")
  88. }
  89. w[wa] = struct{}{}
  90. }
  91. func (w watcherSet) union(ws watcherSet) {
  92. for wa := range ws {
  93. w.add(wa)
  94. }
  95. }
  96. func (w watcherSet) delete(wa *watcher) {
  97. if _, ok := w[wa]; !ok {
  98. panic("removing missing watcher!")
  99. }
  100. delete(w, wa)
  101. }
  102. type watcherSetByKey map[string]watcherSet
  103. func (w watcherSetByKey) add(wa *watcher) {
  104. set := w[string(wa.key)]
  105. if set == nil {
  106. set = make(watcherSet)
  107. w[string(wa.key)] = set
  108. }
  109. set.add(wa)
  110. }
  111. func (w watcherSetByKey) delete(wa *watcher) bool {
  112. k := string(wa.key)
  113. if v, ok := w[k]; ok {
  114. if _, ok := v[wa]; ok {
  115. delete(v, wa)
  116. if len(v) == 0 {
  117. // remove the set; nothing left
  118. delete(w, k)
  119. }
  120. return true
  121. }
  122. }
  123. return false
  124. }
  125. // watcherGroup is a collection of watchers organized by their ranges
  126. type watcherGroup struct {
  127. // keyWatchers has the watchers that watch on a single key
  128. keyWatchers watcherSetByKey
  129. // ranges has the watchers that watch a range; it is sorted by interval
  130. ranges adt.IntervalTree
  131. // watchers is the set of all watchers
  132. watchers watcherSet
  133. }
  134. func newWatcherGroup() watcherGroup {
  135. return watcherGroup{
  136. keyWatchers: make(watcherSetByKey),
  137. ranges: adt.NewIntervalTree(),
  138. watchers: make(watcherSet),
  139. }
  140. }
  141. // add puts a watcher in the group.
  142. func (wg *watcherGroup) add(wa *watcher) {
  143. wg.watchers.add(wa)
  144. if wa.end == nil {
  145. wg.keyWatchers.add(wa)
  146. return
  147. }
  148. // interval already registered?
  149. ivl := adt.NewStringAffineInterval(string(wa.key), string(wa.end))
  150. if iv := wg.ranges.Find(ivl); iv != nil {
  151. iv.Val.(watcherSet).add(wa)
  152. return
  153. }
  154. // not registered, put in interval tree
  155. ws := make(watcherSet)
  156. ws.add(wa)
  157. wg.ranges.Insert(ivl, ws)
  158. }
  159. // contains is whether the given key has a watcher in the group.
  160. func (wg *watcherGroup) contains(key string) bool {
  161. _, ok := wg.keyWatchers[key]
  162. return ok || wg.ranges.Intersects(adt.NewStringAffinePoint(key))
  163. }
  164. // size gives the number of unique watchers in the group.
  165. func (wg *watcherGroup) size() int { return len(wg.watchers) }
  166. // delete removes a watcher from the group.
  167. func (wg *watcherGroup) delete(wa *watcher) bool {
  168. if _, ok := wg.watchers[wa]; !ok {
  169. return false
  170. }
  171. wg.watchers.delete(wa)
  172. if wa.end == nil {
  173. wg.keyWatchers.delete(wa)
  174. return true
  175. }
  176. ivl := adt.NewStringAffineInterval(string(wa.key), string(wa.end))
  177. iv := wg.ranges.Find(ivl)
  178. if iv == nil {
  179. return false
  180. }
  181. ws := iv.Val.(watcherSet)
  182. delete(ws, wa)
  183. if len(ws) == 0 {
  184. // remove interval missing watchers
  185. if ok := wg.ranges.Delete(ivl); !ok {
  186. panic("could not remove watcher from interval tree")
  187. }
  188. }
  189. return true
  190. }
  191. // choose selects watchers from the watcher group to update
  192. func (wg *watcherGroup) choose(maxWatchers int, curRev, compactRev int64) (*watcherGroup, int64) {
  193. if len(wg.watchers) < maxWatchers {
  194. return wg, wg.chooseAll(curRev, compactRev)
  195. }
  196. ret := newWatcherGroup()
  197. for w := range wg.watchers {
  198. if maxWatchers <= 0 {
  199. break
  200. }
  201. maxWatchers--
  202. ret.add(w)
  203. }
  204. return &ret, ret.chooseAll(curRev, compactRev)
  205. }
  206. func (wg *watcherGroup) chooseAll(curRev, compactRev int64) int64 {
  207. minRev := int64(math.MaxInt64)
  208. for w := range wg.watchers {
  209. if w.minRev > curRev {
  210. // after network partition, possibly choosing future revision watcher from restore operation
  211. // with watch key "proxy-namespace__lostleader" and revision "math.MaxInt64 - 2"
  212. // do not panic when such watcher had been moved from "synced" watcher during restore operation
  213. if !w.restore {
  214. panic(fmt.Errorf("watcher minimum revision %d should not exceed current revision %d", w.minRev, curRev))
  215. }
  216. // mark 'restore' done, since it's chosen
  217. w.restore = false
  218. }
  219. if w.minRev < compactRev {
  220. select {
  221. case w.ch <- WatchResponse{WatchID: w.id, CompactRevision: compactRev}:
  222. w.compacted = true
  223. wg.delete(w)
  224. default:
  225. // retry next time
  226. }
  227. continue
  228. }
  229. if minRev > w.minRev {
  230. minRev = w.minRev
  231. }
  232. }
  233. return minRev
  234. }
  235. // watcherSetByKey gets the set of watchers that receive events on the given key.
  236. func (wg *watcherGroup) watcherSetByKey(key string) watcherSet {
  237. wkeys := wg.keyWatchers[key]
  238. wranges := wg.ranges.Stab(adt.NewStringAffinePoint(key))
  239. // zero-copy cases
  240. switch {
  241. case len(wranges) == 0:
  242. // no need to merge ranges or copy; reuse single-key set
  243. return wkeys
  244. case len(wranges) == 0 && len(wkeys) == 0:
  245. return nil
  246. case len(wranges) == 1 && len(wkeys) == 0:
  247. return wranges[0].Val.(watcherSet)
  248. }
  249. // copy case
  250. ret := make(watcherSet)
  251. ret.union(wg.keyWatchers[key])
  252. for _, item := range wranges {
  253. ret.union(item.Val.(watcherSet))
  254. }
  255. return ret
  256. }