watcher_hub.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package store
  2. import (
  3. "container/list"
  4. "path"
  5. "strings"
  6. "sync"
  7. "sync/atomic"
  8. etcdErr "github.com/coreos/etcd/error"
  9. )
  10. // A watcherHub contains all subscribed watchers
  11. // watchers is a map with watched path as key and watcher as value
  12. // EventHistory keeps the old events for watcherHub. It is used to help
  13. // watcher to get a continuous event history. Or a watcher might miss the
  14. // event happens between the end of the first watch command and the start
  15. // of the second command.
  16. type watcherHub struct {
  17. mutex sync.Mutex // protect the hash map
  18. watchers map[string]*list.List
  19. count int64 // current number of watchers.
  20. EventHistory *EventHistory
  21. }
  22. // newWatchHub creates a watchHub. The capacity determines how many events we will
  23. // keep in the eventHistory.
  24. // Typically, we only need to keep a small size of history[smaller than 20K].
  25. // Ideally, it should smaller than 20K/s[max throughput] * 2 * 50ms[RTT] = 2000
  26. func newWatchHub(capacity int) *watcherHub {
  27. return &watcherHub{
  28. watchers: make(map[string]*list.List),
  29. EventHistory: newEventHistory(capacity),
  30. }
  31. }
  32. // Watch function returns a watcher.
  33. // If recursive is true, the first change after index under key will be sent to the event channel of the watcher.
  34. // If recursive is false, the first change after index at key will be sent to the event channel of the watcher.
  35. // If index is zero, watch will start from the current index + 1.
  36. func (wh *watcherHub) watch(key string, recursive bool, index uint64) (*Watcher, *etcdErr.Error) {
  37. event, err := wh.EventHistory.scan(key, recursive, index)
  38. if err != nil {
  39. return nil, err
  40. }
  41. w := &Watcher{
  42. EventChan: make(chan *Event, 1), // use a buffered channel
  43. recursive: recursive,
  44. sinceIndex: index,
  45. }
  46. if event != nil {
  47. w.EventChan <- event
  48. return w, nil
  49. }
  50. wh.mutex.Lock()
  51. defer wh.mutex.Unlock()
  52. l, ok := wh.watchers[key]
  53. var elem *list.Element
  54. if ok { // add the new watcher to the back of the list
  55. elem = l.PushBack(w)
  56. } else { // create a new list and add the new watcher
  57. l = list.New()
  58. elem = l.PushBack(w)
  59. wh.watchers[key] = l
  60. }
  61. w.remove = func() {
  62. wh.mutex.Lock()
  63. defer wh.mutex.Unlock()
  64. l.Remove(elem)
  65. atomic.AddInt64(&wh.count, -1)
  66. if l.Len() == 0 {
  67. delete(wh.watchers, key)
  68. }
  69. }
  70. atomic.AddInt64(&wh.count, 1)
  71. return w, nil
  72. }
  73. // notify function accepts an event and notify to the watchers.
  74. func (wh *watcherHub) notify(e *Event) {
  75. e = wh.EventHistory.addEvent(e) // add event into the eventHistory
  76. segments := strings.Split(e.Node.Key, "/")
  77. currPath := "/"
  78. // walk through all the segments of the path and notify the watchers
  79. // if the path is "/foo/bar", it will notify watchers with path "/",
  80. // "/foo" and "/foo/bar"
  81. for _, segment := range segments {
  82. currPath = path.Join(currPath, segment)
  83. // notify the watchers who interests in the changes of current path
  84. wh.notifyWatchers(e, currPath, false)
  85. }
  86. }
  87. func (wh *watcherHub) notifyWatchers(e *Event, path string, deleted bool) {
  88. wh.mutex.Lock()
  89. defer wh.mutex.Unlock()
  90. l, ok := wh.watchers[path]
  91. if ok {
  92. curr := l.Front()
  93. for curr != nil {
  94. next := curr.Next() // save reference to the next one in the list
  95. w, _ := curr.Value.(*Watcher)
  96. if w.notify(e, e.Node.Key == path, deleted) {
  97. // if we successfully notify a watcher
  98. // we need to remove the watcher from the list
  99. // and decrease the counter
  100. l.Remove(curr)
  101. atomic.AddInt64(&wh.count, -1)
  102. }
  103. curr = next // update current to the next element in the list
  104. }
  105. if l.Len() == 0 {
  106. // if we have notified all watcher in the list
  107. // we can delete the list
  108. delete(wh.watchers, path)
  109. }
  110. }
  111. }
  112. // clone function clones the watcherHub and return the cloned one.
  113. // only clone the static content. do not clone the current watchers.
  114. func (wh *watcherHub) clone() *watcherHub {
  115. clonedHistory := wh.EventHistory.clone()
  116. return &watcherHub{
  117. EventHistory: clonedHistory,
  118. }
  119. }