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. c *clientv3.Client
  25. wgs watchergroups
  26. mu sync.Mutex
  27. nextStreamID int64
  28. }
  29. func NewWatchProxy(c *clientv3.Client) *watchProxy {
  30. return &watchProxy{
  31. c: c,
  32. wgs: watchergroups{
  33. c: c,
  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. c: wp.c,
  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. c *clientv3.Client
  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) recvLoop() error {
  66. for {
  67. req, err := sws.gRPCStream.Recv()
  68. if err == io.EOF {
  69. return nil
  70. }
  71. if err != nil {
  72. return err
  73. }
  74. switch uv := req.RequestUnion.(type) {
  75. case *pb.WatchRequest_CreateRequest:
  76. cr := uv.CreateRequest
  77. watcher := watcher{
  78. wr: watchRange{
  79. key: string(cr.Key),
  80. end: string(cr.RangeEnd),
  81. },
  82. id: sws.nextWatcherID,
  83. ch: sws.watchCh,
  84. progress: cr.ProgressNotify,
  85. filters: v3rpc.FiltersFromRequest(cr),
  86. }
  87. if cr.StartRevision != 0 {
  88. sws.addDedicatedWatcher(watcher, cr.StartRevision)
  89. } else {
  90. sws.addCoalescedWatcher(watcher)
  91. }
  92. wresp := &pb.WatchResponse{
  93. Header: &pb.ResponseHeader{}, // TODO: fill in header
  94. WatchId: sws.nextWatcherID,
  95. Created: true,
  96. }
  97. sws.nextWatcherID++
  98. select {
  99. case sws.ctrlCh <- wresp:
  100. default:
  101. panic("handle this")
  102. }
  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.c.Watch(ctx,
  141. w.wr.key, clientv3.WithRange(w.wr.end),
  142. clientv3.WithRev(rev),
  143. clientv3.WithProgressNotify(),
  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. }