watcher_groups.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "sync"
  17. "github.com/coreos/etcd/clientv3"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. "golang.org/x/net/context"
  20. )
  21. type watchergroups struct {
  22. cw clientv3.Watcher
  23. mu sync.Mutex
  24. groups map[watchRange]*watcherGroup
  25. idToGroup map[receiverID]*watcherGroup
  26. proxyCtx context.Context
  27. }
  28. func (wgs *watchergroups) addWatcher(rid receiverID, w watcher) {
  29. wgs.mu.Lock()
  30. defer wgs.mu.Unlock()
  31. groups := wgs.groups
  32. if wg, ok := groups[w.wr]; ok {
  33. rev := wg.add(rid, w)
  34. wgs.idToGroup[rid] = wg
  35. resp := &pb.WatchResponse{
  36. Header: &pb.ResponseHeader{
  37. // todo: fill in ClusterId
  38. // todo: fill in MemberId:
  39. Revision: rev,
  40. // todo: fill in RaftTerm:
  41. },
  42. WatchId: rid.watcherID,
  43. Created: true,
  44. }
  45. w.ch <- resp
  46. return
  47. }
  48. ctx, cancel := context.WithCancel(wgs.proxyCtx)
  49. wch := wgs.cw.Watch(ctx, w.wr.key,
  50. clientv3.WithRange(w.wr.end),
  51. clientv3.WithProgressNotify(),
  52. clientv3.WithCreatedNotify(),
  53. )
  54. watchg := newWatchergroup(wch, cancel)
  55. watchg.add(rid, w)
  56. go watchg.run()
  57. groups[w.wr] = watchg
  58. wgs.idToGroup[rid] = watchg
  59. }
  60. func (wgs *watchergroups) removeWatcher(rid receiverID) (int64, bool) {
  61. wgs.mu.Lock()
  62. defer wgs.mu.Unlock()
  63. if g, ok := wgs.idToGroup[rid]; ok {
  64. g.delete(rid)
  65. delete(wgs.idToGroup, rid)
  66. if g.isEmpty() {
  67. g.stop()
  68. }
  69. return g.revision(), true
  70. }
  71. return -1, false
  72. }
  73. func (wgs *watchergroups) maybeJoinWatcherSingle(rid receiverID, ws watcherSingle) bool {
  74. wgs.mu.Lock()
  75. defer wgs.mu.Unlock()
  76. group, ok := wgs.groups[ws.w.wr]
  77. if ok {
  78. if ws.w.rev >= group.rev {
  79. group.add(receiverID{streamID: ws.sws.id, watcherID: ws.w.id}, ws.w)
  80. return true
  81. }
  82. return false
  83. }
  84. if ws.canPromote() {
  85. wg := newWatchergroup(ws.ch, ws.cancel)
  86. wgs.groups[ws.w.wr] = wg
  87. wg.add(receiverID{streamID: ws.sws.id, watcherID: ws.w.id}, ws.w)
  88. go wg.run()
  89. return true
  90. }
  91. return false
  92. }
  93. func (wgs *watchergroups) stop() {
  94. wgs.mu.Lock()
  95. defer wgs.mu.Unlock()
  96. for _, wg := range wgs.groups {
  97. wg.stop()
  98. }
  99. wgs.groups = nil
  100. }