watch.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. "golang.org/x/time/rate"
  19. "google.golang.org/grpc/metadata"
  20. "github.com/coreos/etcd/clientv3"
  21. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  22. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  23. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. )
  25. type watchProxy struct {
  26. cw clientv3.Watcher
  27. ctx context.Context
  28. ranges *watchRanges
  29. // retryLimiter controls the create watch retry rate on lost leaders.
  30. retryLimiter *rate.Limiter
  31. // mu protects leaderc updates.
  32. mu sync.RWMutex
  33. leaderc chan struct{}
  34. // wg waits until all outstanding watch servers quit.
  35. wg sync.WaitGroup
  36. }
  37. const (
  38. lostLeaderKey = "__lostleader" // watched to detect leader l oss
  39. retryPerSecond = 10
  40. )
  41. func NewWatchProxy(c *clientv3.Client) pb.WatchServer {
  42. wp := &watchProxy{
  43. cw: c.Watcher,
  44. ctx: clientv3.WithRequireLeader(c.Ctx()),
  45. retryLimiter: rate.NewLimiter(rate.Limit(retryPerSecond), retryPerSecond),
  46. leaderc: make(chan struct{}),
  47. }
  48. wp.ranges = newWatchRanges(wp)
  49. go func() {
  50. // a new streams without opening any watchers won't catch
  51. // a lost leader event, so have a special watch to monitor it
  52. rev := int64((uint64(1) << 63) - 2)
  53. for wp.ctx.Err() == nil {
  54. wch := wp.cw.Watch(wp.ctx, lostLeaderKey, clientv3.WithRev(rev))
  55. for range wch {
  56. }
  57. wp.mu.Lock()
  58. close(wp.leaderc)
  59. wp.leaderc = make(chan struct{})
  60. wp.mu.Unlock()
  61. wp.retryLimiter.Wait(wp.ctx)
  62. }
  63. wp.mu.Lock()
  64. <-wp.ctx.Done()
  65. wp.mu.Unlock()
  66. wp.wg.Wait()
  67. wp.ranges.stop()
  68. }()
  69. return wp
  70. }
  71. func (wp *watchProxy) Watch(stream pb.Watch_WatchServer) (err error) {
  72. wp.mu.Lock()
  73. select {
  74. case <-wp.ctx.Done():
  75. wp.mu.Unlock()
  76. return
  77. default:
  78. wp.wg.Add(1)
  79. }
  80. wp.mu.Unlock()
  81. ctx, cancel := context.WithCancel(stream.Context())
  82. wps := &watchProxyStream{
  83. ranges: wp.ranges,
  84. watchers: make(map[int64]*watcher),
  85. stream: stream,
  86. watchCh: make(chan *pb.WatchResponse, 1024),
  87. ctx: ctx,
  88. cancel: cancel,
  89. }
  90. var leaderc <-chan struct{}
  91. if md, ok := metadata.FromContext(stream.Context()); ok {
  92. v := md[rpctypes.MetadataRequireLeaderKey]
  93. if len(v) > 0 && v[0] == rpctypes.MetadataHasLeader {
  94. leaderc = wp.lostLeaderNotify()
  95. }
  96. }
  97. // post to stopc => terminate server stream; can't use a waitgroup
  98. // since all goroutines will only terminate after Watch() exits.
  99. stopc := make(chan struct{}, 3)
  100. go func() {
  101. defer func() { stopc <- struct{}{} }()
  102. wps.recvLoop()
  103. }()
  104. go func() {
  105. defer func() { stopc <- struct{}{} }()
  106. wps.sendLoop()
  107. }()
  108. if leaderc != nil {
  109. go func() {
  110. defer func() { stopc <- struct{}{} }()
  111. select {
  112. case <-leaderc:
  113. case <-ctx.Done():
  114. }
  115. }()
  116. }
  117. <-stopc
  118. // recv/send may only shutdown after function exits;
  119. // goroutine notifies proxy that stream is through
  120. go func() {
  121. if leaderc != nil {
  122. <-stopc
  123. }
  124. <-stopc
  125. wps.close()
  126. wp.wg.Done()
  127. }()
  128. select {
  129. case <-leaderc:
  130. return rpctypes.ErrNoLeader
  131. default:
  132. return wps.ctx.Err()
  133. }
  134. }
  135. func (wp *watchProxy) lostLeaderNotify() <-chan struct{} {
  136. wp.mu.RLock()
  137. defer wp.mu.RUnlock()
  138. return wp.leaderc
  139. }
  140. // watchProxyStream forwards etcd watch events to a proxied client stream.
  141. type watchProxyStream struct {
  142. ranges *watchRanges
  143. // mu protects watchers and nextWatcherID
  144. mu sync.Mutex
  145. // watchers receive events from watch broadcast.
  146. watchers map[int64]*watcher
  147. // nextWatcherID is the id to assign the next watcher on this stream.
  148. nextWatcherID int64
  149. stream pb.Watch_WatchServer
  150. // watchCh receives watch responses from the watchers.
  151. watchCh chan *pb.WatchResponse
  152. ctx context.Context
  153. cancel context.CancelFunc
  154. }
  155. func (wps *watchProxyStream) close() {
  156. var wg sync.WaitGroup
  157. wps.cancel()
  158. wps.mu.Lock()
  159. wg.Add(len(wps.watchers))
  160. for _, wpsw := range wps.watchers {
  161. go func(w *watcher) {
  162. wps.ranges.delete(w)
  163. wg.Done()
  164. }(wpsw)
  165. }
  166. wps.watchers = nil
  167. wps.mu.Unlock()
  168. wg.Wait()
  169. close(wps.watchCh)
  170. }
  171. func (wps *watchProxyStream) recvLoop() error {
  172. for {
  173. req, err := wps.stream.Recv()
  174. if err != nil {
  175. return err
  176. }
  177. switch uv := req.RequestUnion.(type) {
  178. case *pb.WatchRequest_CreateRequest:
  179. cr := uv.CreateRequest
  180. w := &watcher{
  181. wr: watchRange{string(cr.Key), string(cr.RangeEnd)},
  182. id: wps.nextWatcherID,
  183. wps: wps,
  184. nextrev: cr.StartRevision,
  185. progress: cr.ProgressNotify,
  186. prevKV: cr.PrevKv,
  187. filters: v3rpc.FiltersFromRequest(cr),
  188. }
  189. if !w.wr.valid() {
  190. w.post(&pb.WatchResponse{WatchId: -1, Created: true, Canceled: true})
  191. continue
  192. }
  193. wps.nextWatcherID++
  194. w.nextrev = cr.StartRevision
  195. wps.watchers[w.id] = w
  196. wps.ranges.add(w)
  197. case *pb.WatchRequest_CancelRequest:
  198. wps.delete(uv.CancelRequest.WatchId)
  199. default:
  200. panic("not implemented")
  201. }
  202. }
  203. }
  204. func (wps *watchProxyStream) sendLoop() {
  205. for {
  206. select {
  207. case wresp, ok := <-wps.watchCh:
  208. if !ok {
  209. return
  210. }
  211. if err := wps.stream.Send(wresp); err != nil {
  212. return
  213. }
  214. case <-wps.ctx.Done():
  215. return
  216. }
  217. }
  218. }
  219. func (wps *watchProxyStream) delete(id int64) {
  220. wps.mu.Lock()
  221. defer wps.mu.Unlock()
  222. w, ok := wps.watchers[id]
  223. if !ok {
  224. return
  225. }
  226. wps.ranges.delete(w)
  227. delete(wps.watchers, id)
  228. resp := &pb.WatchResponse{
  229. Header: &w.lastHeader,
  230. WatchId: id,
  231. Canceled: true,
  232. }
  233. wps.watchCh <- resp
  234. }