watch.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. id: wp.nextStreamID,
  46. gRPCStream: stream,
  47. ctrlCh: make(chan *pb.WatchResponse, 10),
  48. watchCh: make(chan *pb.WatchResponse, 10),
  49. }
  50. go sws.recvLoop()
  51. sws.sendLoop()
  52. return nil
  53. }
  54. type serverWatchStream struct {
  55. id int64
  56. cw clientv3.Watcher
  57. mu sync.Mutex // make sure any access of groups and singles is atomic
  58. groups *watchergroups
  59. singles map[int64]*watcherSingle
  60. gRPCStream pb.Watch_WatchServer
  61. ctrlCh chan *pb.WatchResponse
  62. watchCh chan *pb.WatchResponse
  63. nextWatcherID int64
  64. }
  65. func (sws *serverWatchStream) close() {
  66. close(sws.watchCh)
  67. close(sws.ctrlCh)
  68. for _, ws := range sws.singles {
  69. ws.stop()
  70. }
  71. sws.groups.stop()
  72. }
  73. func (sws *serverWatchStream) recvLoop() error {
  74. defer sws.close()
  75. for {
  76. req, err := sws.gRPCStream.Recv()
  77. if err == io.EOF {
  78. return nil
  79. }
  80. if err != nil {
  81. return err
  82. }
  83. switch uv := req.RequestUnion.(type) {
  84. case *pb.WatchRequest_CreateRequest:
  85. cr := uv.CreateRequest
  86. watcher := watcher{
  87. wr: watchRange{
  88. key: string(cr.Key),
  89. end: string(cr.RangeEnd),
  90. },
  91. id: sws.nextWatcherID,
  92. ch: sws.watchCh,
  93. progress: cr.ProgressNotify,
  94. filters: v3rpc.FiltersFromRequest(cr),
  95. }
  96. if cr.StartRevision != 0 {
  97. sws.addDedicatedWatcher(watcher, cr.StartRevision)
  98. } else {
  99. sws.addCoalescedWatcher(watcher)
  100. }
  101. wresp := &pb.WatchResponse{
  102. Header: &pb.ResponseHeader{}, // TODO: fill in header
  103. WatchId: sws.nextWatcherID,
  104. Created: true,
  105. }
  106. sws.nextWatcherID++
  107. select {
  108. case sws.ctrlCh <- wresp:
  109. default:
  110. panic("handle this")
  111. }
  112. case *pb.WatchRequest_CancelRequest:
  113. sws.removeWatcher(uv.CancelRequest.WatchId)
  114. default:
  115. panic("not implemented")
  116. }
  117. }
  118. }
  119. func (sws *serverWatchStream) sendLoop() {
  120. for {
  121. select {
  122. case wresp, ok := <-sws.watchCh:
  123. if !ok {
  124. return
  125. }
  126. if err := sws.gRPCStream.Send(wresp); err != nil {
  127. return
  128. }
  129. case c, ok := <-sws.ctrlCh:
  130. if !ok {
  131. return
  132. }
  133. if err := sws.gRPCStream.Send(c); err != nil {
  134. return
  135. }
  136. }
  137. }
  138. }
  139. func (sws *serverWatchStream) addCoalescedWatcher(w watcher) {
  140. sws.mu.Lock()
  141. defer sws.mu.Unlock()
  142. rid := receiverID{streamID: sws.id, watcherID: w.id}
  143. sws.groups.addWatcher(rid, w)
  144. }
  145. func (sws *serverWatchStream) addDedicatedWatcher(w watcher, rev int64) {
  146. sws.mu.Lock()
  147. defer sws.mu.Unlock()
  148. ctx, cancel := context.WithCancel(context.Background())
  149. wch := sws.cw.Watch(ctx,
  150. w.wr.key, clientv3.WithRange(w.wr.end),
  151. clientv3.WithRev(rev),
  152. clientv3.WithProgressNotify(),
  153. )
  154. ws := newWatcherSingle(wch, cancel, w, sws)
  155. sws.singles[w.id] = ws
  156. go ws.run()
  157. }
  158. func (sws *serverWatchStream) maybeCoalesceWatcher(ws watcherSingle) bool {
  159. sws.mu.Lock()
  160. defer sws.mu.Unlock()
  161. rid := receiverID{streamID: sws.id, watcherID: ws.w.id}
  162. if sws.groups.maybeJoinWatcherSingle(rid, ws) {
  163. delete(sws.singles, ws.w.id)
  164. return true
  165. }
  166. return false
  167. }
  168. func (sws *serverWatchStream) removeWatcher(id int64) {
  169. sws.mu.Lock()
  170. defer sws.mu.Unlock()
  171. if sws.groups.removeWatcher(receiverID{streamID: sws.id, watcherID: id}) {
  172. return
  173. }
  174. if ws, ok := sws.singles[id]; ok {
  175. delete(sws.singles, id)
  176. ws.stop()
  177. }
  178. }