watch.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. sws.nextWatcherID++
  102. case *pb.WatchRequest_CancelRequest:
  103. sws.removeWatcher(uv.CancelRequest.WatchId)
  104. default:
  105. panic("not implemented")
  106. }
  107. }
  108. }
  109. func (sws *serverWatchStream) sendLoop() {
  110. for {
  111. select {
  112. case wresp, ok := <-sws.watchCh:
  113. if !ok {
  114. return
  115. }
  116. if err := sws.gRPCStream.Send(wresp); err != nil {
  117. return
  118. }
  119. case c, ok := <-sws.ctrlCh:
  120. if !ok {
  121. return
  122. }
  123. if err := sws.gRPCStream.Send(c); err != nil {
  124. return
  125. }
  126. }
  127. }
  128. }
  129. func (sws *serverWatchStream) addCoalescedWatcher(w watcher) {
  130. sws.mu.Lock()
  131. defer sws.mu.Unlock()
  132. rid := receiverID{streamID: sws.id, watcherID: w.id}
  133. sws.groups.addWatcher(rid, w)
  134. }
  135. func (sws *serverWatchStream) addDedicatedWatcher(w watcher, rev int64) {
  136. sws.mu.Lock()
  137. defer sws.mu.Unlock()
  138. ctx, cancel := context.WithCancel(context.Background())
  139. wch := sws.cw.Watch(ctx,
  140. w.wr.key, clientv3.WithRange(w.wr.end),
  141. clientv3.WithRev(rev),
  142. clientv3.WithProgressNotify(),
  143. clientv3.WithCreatedNotify(),
  144. )
  145. ws := newWatcherSingle(wch, cancel, w, sws)
  146. sws.singles[w.id] = ws
  147. go ws.run()
  148. }
  149. func (sws *serverWatchStream) maybeCoalesceWatcher(ws watcherSingle) bool {
  150. sws.mu.Lock()
  151. defer sws.mu.Unlock()
  152. rid := receiverID{streamID: sws.id, watcherID: ws.w.id}
  153. if sws.groups.maybeJoinWatcherSingle(rid, ws) {
  154. delete(sws.singles, ws.w.id)
  155. return true
  156. }
  157. return false
  158. }
  159. func (sws *serverWatchStream) removeWatcher(id int64) {
  160. sws.mu.Lock()
  161. defer sws.mu.Unlock()
  162. if sws.groups.removeWatcher(receiverID{streamID: sws.id, watcherID: id}) {
  163. return
  164. }
  165. if ws, ok := sws.singles[id]; ok {
  166. delete(sws.singles, id)
  167. ws.stop()
  168. }
  169. }