watch.go 4.1 KB

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