watcher_group.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 storage
  15. import (
  16. "math"
  17. "github.com/coreos/etcd/pkg/adt"
  18. "github.com/coreos/etcd/storage/storagepb"
  19. )
  20. var (
  21. // watchBatchMaxRevs is the maximum distinct revisions that
  22. // may be sent to an unsynced watcher at a time. Declared as
  23. // var instead of const for testing purposes.
  24. watchBatchMaxRevs = 1000
  25. )
  26. type eventBatch struct {
  27. // evs is a batch of revision-ordered events
  28. evs []storagepb.Event
  29. // revs is the minimum unique revisions observed for this batch
  30. revs int
  31. // moreRev is first revision with more events following this batch
  32. moreRev int64
  33. }
  34. func (eb *eventBatch) add(ev storagepb.Event) {
  35. if eb.revs > watchBatchMaxRevs {
  36. // maxed out batch size
  37. return
  38. }
  39. if len(eb.evs) == 0 {
  40. // base case
  41. eb.revs = 1
  42. eb.evs = append(eb.evs, ev)
  43. return
  44. }
  45. // revision accounting
  46. ebRev := eb.evs[len(eb.evs)-1].Kv.ModRevision
  47. evRev := ev.Kv.ModRevision
  48. if evRev > ebRev {
  49. eb.revs++
  50. if eb.revs > watchBatchMaxRevs {
  51. eb.moreRev = evRev
  52. return
  53. }
  54. }
  55. eb.evs = append(eb.evs, ev)
  56. }
  57. type watcherBatch map[*watcher]*eventBatch
  58. func (wb watcherBatch) add(w *watcher, ev storagepb.Event) {
  59. eb := wb[w]
  60. if eb == nil {
  61. eb = &eventBatch{}
  62. wb[w] = eb
  63. }
  64. eb.add(ev)
  65. }
  66. func (wb watcherBatch) contains(w *watcher) bool {
  67. _, ok := wb[w]
  68. return ok
  69. }
  70. // newWatcherBatch maps watchers to their matched events. It enables quick
  71. // events look up by watcher.
  72. func newWatcherBatch(wg *watcherGroup, evs []storagepb.Event) watcherBatch {
  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.cur {
  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. type interval struct {
  126. begin string
  127. end string
  128. }
  129. type watcherSetByInterval map[interval]watcherSet
  130. // watcherGroup is a collection of watchers organized by their ranges
  131. type watcherGroup struct {
  132. // keyWatchers has the watchers that watch on a single key
  133. keyWatchers watcherSetByKey
  134. // ranges has the watchers that watch a range; it is sorted by interval
  135. ranges adt.IntervalTree
  136. // watchers is the set of all watchers
  137. watchers watcherSet
  138. }
  139. func newWatcherGroup() watcherGroup {
  140. return watcherGroup{
  141. keyWatchers: make(watcherSetByKey),
  142. watchers: make(watcherSet),
  143. }
  144. }
  145. // add puts a watcher in the group.
  146. func (wg *watcherGroup) add(wa *watcher) {
  147. wg.watchers.add(wa)
  148. if wa.end == nil {
  149. wg.keyWatchers.add(wa)
  150. return
  151. }
  152. // interval already registered?
  153. ivl := adt.NewStringAffineInterval(string(wa.key), string(wa.end))
  154. if iv := wg.ranges.Find(ivl); iv != nil {
  155. iv.Val.(watcherSet).add(wa)
  156. return
  157. }
  158. // not registered, put in interval tree
  159. ws := make(watcherSet)
  160. ws.add(wa)
  161. wg.ranges.Insert(ivl, ws)
  162. }
  163. // contains is whether the given key has a watcher in the group.
  164. func (wg *watcherGroup) contains(key string) bool {
  165. _, ok := wg.keyWatchers[key]
  166. return ok || wg.ranges.Contains(adt.NewStringAffinePoint(key))
  167. }
  168. // size gives the number of unique watchers in the group.
  169. func (wg *watcherGroup) size() int { return len(wg.watchers) }
  170. // delete removes a watcher from the group.
  171. func (wg *watcherGroup) delete(wa *watcher) bool {
  172. if _, ok := wg.watchers[wa]; !ok {
  173. return false
  174. }
  175. wg.watchers.delete(wa)
  176. if wa.end == nil {
  177. wg.keyWatchers.delete(wa)
  178. return true
  179. }
  180. ivl := adt.NewStringAffineInterval(string(wa.key), string(wa.end))
  181. iv := wg.ranges.Find(ivl)
  182. if iv == nil {
  183. return false
  184. }
  185. ws := iv.Val.(watcherSet)
  186. delete(ws, wa)
  187. if len(ws) == 0 {
  188. // remove interval missing watchers
  189. if ok := wg.ranges.Delete(ivl); !ok {
  190. panic("could not remove watcher from interval tree")
  191. }
  192. }
  193. return true
  194. }
  195. func (wg *watcherGroup) scanMinRev(curRev int64, compactRev int64) int64 {
  196. minRev := int64(math.MaxInt64)
  197. for w := range wg.watchers {
  198. if w.cur > curRev {
  199. panic("watcher current revision should not exceed current revision")
  200. }
  201. if w.cur < compactRev {
  202. select {
  203. case w.ch <- WatchResponse{WatchID: w.id, CompactRevision: compactRev}:
  204. wg.delete(w)
  205. default:
  206. // retry next time
  207. }
  208. continue
  209. }
  210. if minRev > w.cur {
  211. minRev = w.cur
  212. }
  213. }
  214. return minRev
  215. }
  216. // watcherSetByKey gets the set of watchers that recieve events on the given key.
  217. func (wg *watcherGroup) watcherSetByKey(key string) watcherSet {
  218. wkeys := wg.keyWatchers[key]
  219. wranges := wg.ranges.Stab(adt.NewStringAffinePoint(key))
  220. // zero-copy cases
  221. switch {
  222. case len(wranges) == 0:
  223. // no need to merge ranges or copy; reuse single-key set
  224. return wkeys
  225. case len(wranges) == 0 && len(wkeys) == 0:
  226. return nil
  227. case len(wranges) == 1 && len(wkeys) == 0:
  228. return wranges[0].Val.(watcherSet)
  229. }
  230. // copy case
  231. ret := make(watcherSet)
  232. ret.union(wg.keyWatchers[key])
  233. for _, item := range wranges {
  234. ret.union(item.Val.(watcherSet))
  235. }
  236. return ret
  237. }