watch.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. for _, ws := range sws.singles {
  70. ws.stop()
  71. }
  72. sws.groups.stop()
  73. }
  74. func (sws *serverWatchStream) recvLoop() error {
  75. defer sws.close()
  76. for {
  77. req, err := sws.gRPCStream.Recv()
  78. if err == io.EOF {
  79. return nil
  80. }
  81. if err != nil {
  82. return err
  83. }
  84. switch uv := req.RequestUnion.(type) {
  85. case *pb.WatchRequest_CreateRequest:
  86. cr := uv.CreateRequest
  87. watcher := watcher{
  88. wr: watchRange{
  89. key: string(cr.Key),
  90. end: string(cr.RangeEnd),
  91. },
  92. id: sws.nextWatcherID,
  93. ch: sws.watchCh,
  94. progress: cr.ProgressNotify,
  95. filters: v3rpc.FiltersFromRequest(cr),
  96. }
  97. if cr.StartRevision != 0 {
  98. sws.addDedicatedWatcher(watcher, cr.StartRevision)
  99. } else {
  100. sws.addCoalescedWatcher(watcher)
  101. }
  102. sws.nextWatcherID++
  103. case *pb.WatchRequest_CancelRequest:
  104. sws.removeWatcher(uv.CancelRequest.WatchId)
  105. default:
  106. panic("not implemented")
  107. }
  108. }
  109. }
  110. func (sws *serverWatchStream) sendLoop() {
  111. for {
  112. select {
  113. case wresp, ok := <-sws.watchCh:
  114. if !ok {
  115. return
  116. }
  117. if err := sws.gRPCStream.Send(wresp); err != nil {
  118. return
  119. }
  120. case c, ok := <-sws.ctrlCh:
  121. if !ok {
  122. return
  123. }
  124. if err := sws.gRPCStream.Send(c); err != nil {
  125. return
  126. }
  127. }
  128. }
  129. }
  130. func (sws *serverWatchStream) addCoalescedWatcher(w watcher) {
  131. sws.mu.Lock()
  132. defer sws.mu.Unlock()
  133. rid := receiverID{streamID: sws.id, watcherID: w.id}
  134. sws.groups.addWatcher(rid, w)
  135. }
  136. func (sws *serverWatchStream) addDedicatedWatcher(w watcher, rev int64) {
  137. sws.mu.Lock()
  138. defer sws.mu.Unlock()
  139. ctx, cancel := context.WithCancel(context.Background())
  140. wch := sws.cw.Watch(ctx,
  141. w.wr.key, clientv3.WithRange(w.wr.end),
  142. clientv3.WithRev(rev),
  143. clientv3.WithProgressNotify(),
  144. clientv3.WithCreatedNotify(),
  145. )
  146. ws := newWatcherSingle(wch, cancel, w, sws)
  147. sws.singles[w.id] = ws
  148. go ws.run()
  149. }
  150. func (sws *serverWatchStream) maybeCoalesceWatcher(ws watcherSingle) bool {
  151. sws.mu.Lock()
  152. defer sws.mu.Unlock()
  153. rid := receiverID{streamID: sws.id, watcherID: ws.w.id}
  154. if sws.groups.maybeJoinWatcherSingle(rid, ws) {
  155. delete(sws.singles, ws.w.id)
  156. return true
  157. }
  158. return false
  159. }
  160. func (sws *serverWatchStream) removeWatcher(id int64) {
  161. sws.mu.Lock()
  162. defer sws.mu.Unlock()
  163. if sws.groups.removeWatcher(receiverID{streamID: sws.id, watcherID: id}) {
  164. return
  165. }
  166. if ws, ok := sws.singles[id]; ok {
  167. delete(sws.singles, id)
  168. ws.stop()
  169. }
  170. }