watcher_hub.go 3.8 KB

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