watcher_hub.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 key will be sent to the event channel.
  32. // If recursive is false, the first change after index at key 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(key string, recursive bool, index uint64) (<-chan *Event, *etcdErr.Error) {
  35. event, err := wh.EventHistory.scan(key, recursive, index)
  36. if err != nil {
  37. return nil, err
  38. }
  39. eventChan := make(chan *Event, 1) // use a buffered channel
  40. if event != nil {
  41. eventChan <- event
  42. return eventChan, nil
  43. }
  44. w := &watcher{
  45. eventChan: eventChan,
  46. recursive: recursive,
  47. sinceIndex: index,
  48. }
  49. l, ok := wh.watchers[key]
  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[key] = 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.Node.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. for {
  79. if curr == nil { // we have reached the end of the list
  80. if l.Len() == 0 {
  81. // if we have notified all watcher in the list
  82. // we can delete the list
  83. delete(wh.watchers, path)
  84. }
  85. break
  86. }
  87. next := curr.Next() // save reference to the next one in the list
  88. w, _ := curr.Value.(*watcher)
  89. if w.notify(e, e.Node.Key == path, deleted) {
  90. // if we successfully notify a watcher
  91. // we need to remove the watcher from the list
  92. // and decrease the counter
  93. l.Remove(curr)
  94. atomic.AddInt64(&wh.count, -1)
  95. }
  96. curr = next // update current to the next
  97. }
  98. }
  99. }
  100. // clone function clones the watcherHub and return the cloned one.
  101. // only clone the static content. do not clone the current watchers.
  102. func (wh *watcherHub) clone() *watcherHub {
  103. clonedHistory := wh.EventHistory.clone()
  104. return &watcherHub{
  105. EventHistory: clonedHistory,
  106. }
  107. }