watch.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "io"
  17. "sync"
  18. "golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. )
  23. type watchProxy struct {
  24. cw clientv3.Watcher
  25. wgs watchergroups
  26. mu sync.Mutex
  27. nextStreamID int64
  28. }
  29. func NewWatchProxy(c *clientv3.Client) pb.WatchServer {
  30. return &watchProxy{
  31. cw: c.Watcher,
  32. wgs: watchergroups{
  33. cw: c.Watcher,
  34. groups: make(map[watchRange]*watcherGroup),
  35. },
  36. }
  37. }
  38. func (wp *watchProxy) Watch(stream pb.Watch_WatchServer) (err error) {
  39. wp.mu.Lock()
  40. wp.nextStreamID++
  41. wp.mu.Unlock()
  42. sws := serverWatchStream{
  43. cw: wp.cw,
  44. groups: &wp.wgs,
  45. singles: make(map[int64]*watcherSingle),
  46. id: wp.nextStreamID,
  47. gRPCStream: stream,
  48. ctrlCh: make(chan *pb.WatchResponse, 10),
  49. watchCh: make(chan *pb.WatchResponse, 10),
  50. }
  51. go sws.recvLoop()
  52. sws.sendLoop()
  53. return nil
  54. }
  55. type serverWatchStream struct {
  56. id int64
  57. cw clientv3.Watcher
  58. mu sync.Mutex // make sure any access of groups and singles is atomic
  59. groups *watchergroups
  60. singles map[int64]*watcherSingle
  61. gRPCStream pb.Watch_WatchServer
  62. ctrlCh chan *pb.WatchResponse
  63. watchCh chan *pb.WatchResponse
  64. nextWatcherID int64
  65. }
  66. func (sws *serverWatchStream) close() {
  67. close(sws.watchCh)
  68. close(sws.ctrlCh)
  69. var wg sync.WaitGroup
  70. sws.mu.Lock()
  71. for _, ws := range sws.singles {
  72. wg.Add(1)
  73. ws.stop()
  74. // copy the range variable to avoid race
  75. copyws := ws
  76. go func() {
  77. <-copyws.stopNotify()
  78. wg.Done()
  79. }()
  80. }
  81. sws.mu.Unlock()
  82. wg.Wait()
  83. sws.groups.stop()
  84. }
  85. func (sws *serverWatchStream) recvLoop() error {
  86. defer sws.close()
  87. for {
  88. req, err := sws.gRPCStream.Recv()
  89. if err == io.EOF {
  90. return nil
  91. }
  92. if err != nil {
  93. return err
  94. }
  95. switch uv := req.RequestUnion.(type) {
  96. case *pb.WatchRequest_CreateRequest:
  97. cr := uv.CreateRequest
  98. watcher := watcher{
  99. wr: watchRange{
  100. key: string(cr.Key),
  101. end: string(cr.RangeEnd),
  102. },
  103. id: sws.nextWatcherID,
  104. ch: sws.watchCh,
  105. progress: cr.ProgressNotify,
  106. filters: v3rpc.FiltersFromRequest(cr),
  107. }
  108. if cr.StartRevision != 0 {
  109. sws.addDedicatedWatcher(watcher, cr.StartRevision)
  110. } else {
  111. sws.addCoalescedWatcher(watcher)
  112. }
  113. sws.nextWatcherID++
  114. case *pb.WatchRequest_CancelRequest:
  115. sws.removeWatcher(uv.CancelRequest.WatchId)
  116. default:
  117. panic("not implemented")
  118. }
  119. }
  120. }
  121. func (sws *serverWatchStream) sendLoop() {
  122. for {
  123. select {
  124. case wresp, ok := <-sws.watchCh:
  125. if !ok {
  126. return
  127. }
  128. if err := sws.gRPCStream.Send(wresp); err != nil {
  129. return
  130. }
  131. case c, ok := <-sws.ctrlCh:
  132. if !ok {
  133. return
  134. }
  135. if err := sws.gRPCStream.Send(c); err != nil {
  136. return
  137. }
  138. }
  139. }
  140. }
  141. func (sws *serverWatchStream) addCoalescedWatcher(w watcher) {
  142. sws.mu.Lock()
  143. defer sws.mu.Unlock()
  144. rid := receiverID{streamID: sws.id, watcherID: w.id}
  145. sws.groups.addWatcher(rid, w)
  146. }
  147. func (sws *serverWatchStream) addDedicatedWatcher(w watcher, rev int64) {
  148. sws.mu.Lock()
  149. defer sws.mu.Unlock()
  150. ctx, cancel := context.WithCancel(context.Background())
  151. wch := sws.cw.Watch(ctx,
  152. w.wr.key, clientv3.WithRange(w.wr.end),
  153. clientv3.WithRev(rev),
  154. clientv3.WithProgressNotify(),
  155. clientv3.WithCreatedNotify(),
  156. )
  157. ws := newWatcherSingle(wch, cancel, w, sws)
  158. sws.singles[w.id] = ws
  159. go ws.run()
  160. }
  161. func (sws *serverWatchStream) maybeCoalesceWatcher(ws watcherSingle) bool {
  162. sws.mu.Lock()
  163. defer sws.mu.Unlock()
  164. rid := receiverID{streamID: sws.id, watcherID: ws.w.id}
  165. if sws.groups.maybeJoinWatcherSingle(rid, ws) {
  166. delete(sws.singles, ws.w.id)
  167. return true
  168. }
  169. return false
  170. }
  171. func (sws *serverWatchStream) removeWatcher(id int64) {
  172. sws.mu.Lock()
  173. defer sws.mu.Unlock()
  174. if sws.groups.removeWatcher(receiverID{streamID: sws.id, watcherID: id}) {
  175. return
  176. }
  177. if ws, ok := sws.singles[id]; ok {
  178. delete(sws.singles, id)
  179. ws.stop()
  180. }
  181. }