watch_broadcast.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 grpcproxy
  15. import (
  16. "sync"
  17. "golang.org/x/net/context"
  18. "github.com/coreos/etcd/clientv3"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. )
  21. // watchBroadcast broadcasts a server watcher to many client watchers.
  22. type watchBroadcast struct {
  23. // cancel stops the underlying etcd server watcher and closes ch.
  24. cancel context.CancelFunc
  25. donec chan struct{}
  26. // mu protects rev and receivers.
  27. mu sync.RWMutex
  28. // nextrev is the minimum expected next revision of the watcher on ch.
  29. nextrev int64
  30. // receivers contains all the client-side watchers to serve.
  31. receivers map[*watcher]struct{}
  32. // responses counts the number of responses
  33. responses int
  34. }
  35. func newWatchBroadcast(wp *watchProxy, w *watcher, update func(*watchBroadcast)) *watchBroadcast {
  36. cctx, cancel := context.WithCancel(wp.ctx)
  37. wb := &watchBroadcast{
  38. cancel: cancel,
  39. nextrev: w.nextrev,
  40. receivers: make(map[*watcher]struct{}),
  41. donec: make(chan struct{}),
  42. }
  43. wb.add(w)
  44. go func() {
  45. defer close(wb.donec)
  46. opts := []clientv3.OpOption{
  47. clientv3.WithRange(w.wr.end),
  48. clientv3.WithProgressNotify(),
  49. clientv3.WithRev(wb.nextrev),
  50. clientv3.WithPrevKV(),
  51. clientv3.WithCreatedNotify(),
  52. }
  53. wch := wp.cw.Watch(cctx, w.wr.key, opts...)
  54. for wr := range wch {
  55. wb.bcast(wr)
  56. update(wb)
  57. }
  58. }()
  59. return wb
  60. }
  61. func (wb *watchBroadcast) bcast(wr clientv3.WatchResponse) {
  62. wb.mu.Lock()
  63. defer wb.mu.Unlock()
  64. // watchers start on the given revision, if any; ignore header rev on create
  65. if wb.responses > 0 || wb.nextrev == 0 {
  66. wb.nextrev = wr.Header.Revision + 1
  67. }
  68. wb.responses++
  69. for r := range wb.receivers {
  70. r.send(wr)
  71. }
  72. if len(wb.receivers) > 0 {
  73. eventsCoalescing.Add(float64(len(wb.receivers) - 1))
  74. }
  75. }
  76. // add puts a watcher into receiving a broadcast if its revision at least
  77. // meets the broadcast revision. Returns true if added.
  78. func (wb *watchBroadcast) add(w *watcher) bool {
  79. wb.mu.Lock()
  80. defer wb.mu.Unlock()
  81. if wb.nextrev > w.nextrev || (wb.nextrev == 0 && w.nextrev != 0) {
  82. // wb is too far ahead, w will miss events
  83. // or wb is being established with a current watcher
  84. return false
  85. }
  86. if wb.responses == 0 {
  87. // Newly created; create event will be sent by etcd.
  88. wb.receivers[w] = struct{}{}
  89. return true
  90. }
  91. // already sent by etcd; emulate create event
  92. ok := w.post(&pb.WatchResponse{
  93. Header: &pb.ResponseHeader{
  94. // todo: fill in ClusterId
  95. // todo: fill in MemberId:
  96. Revision: w.nextrev,
  97. // todo: fill in RaftTerm:
  98. },
  99. WatchId: w.id,
  100. Created: true,
  101. })
  102. if !ok {
  103. return false
  104. }
  105. wb.receivers[w] = struct{}{}
  106. watchersCoalescing.Inc()
  107. return true
  108. }
  109. func (wb *watchBroadcast) delete(w *watcher) {
  110. wb.mu.Lock()
  111. defer wb.mu.Unlock()
  112. if _, ok := wb.receivers[w]; !ok {
  113. panic("deleting missing watcher from broadcast")
  114. }
  115. delete(wb.receivers, w)
  116. if len(wb.receivers) > 0 {
  117. // do not dec the only left watcher for coalescing.
  118. watchersCoalescing.Dec()
  119. }
  120. }
  121. func (wb *watchBroadcast) size() int {
  122. wb.mu.RLock()
  123. defer wb.mu.RUnlock()
  124. return len(wb.receivers)
  125. }
  126. func (wb *watchBroadcast) empty() bool { return wb.size() == 0 }
  127. func (wb *watchBroadcast) stop() {
  128. if !wb.empty() {
  129. // do not dec the only left watcher for coalescing.
  130. watchersCoalescing.Sub(float64(wb.size() - 1))
  131. }
  132. wb.cancel()
  133. <-wb.donec
  134. }