watcher_groups.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if rev == 0 {
  36. // The group is newly created, the create event has not been delivered
  37. // to this group yet.
  38. // We can rely on etcd server to deliver the create event.
  39. // Or we might end up sending created event twice.
  40. return
  41. }
  42. resp := &pb.WatchResponse{
  43. Header: &pb.ResponseHeader{
  44. // todo: fill in ClusterId
  45. // todo: fill in MemberId:
  46. Revision: rev,
  47. // todo: fill in RaftTerm:
  48. },
  49. WatchId: rid.watcherID,
  50. Created: true,
  51. }
  52. w.ch <- resp
  53. return
  54. }
  55. ctx, cancel := context.WithCancel(wgs.proxyCtx)
  56. wch := wgs.cw.Watch(ctx, w.wr.key,
  57. clientv3.WithRange(w.wr.end),
  58. clientv3.WithProgressNotify(),
  59. clientv3.WithCreatedNotify(),
  60. )
  61. watchg := newWatchergroup(wch, cancel)
  62. watchg.add(rid, w)
  63. go watchg.run()
  64. groups[w.wr] = watchg
  65. wgs.idToGroup[rid] = watchg
  66. }
  67. func (wgs *watchergroups) removeWatcher(rid receiverID) (int64, bool) {
  68. wgs.mu.Lock()
  69. defer wgs.mu.Unlock()
  70. if g, ok := wgs.idToGroup[rid]; ok {
  71. g.delete(rid)
  72. delete(wgs.idToGroup, rid)
  73. if g.isEmpty() {
  74. g.stop()
  75. }
  76. return g.revision(), true
  77. }
  78. return -1, false
  79. }
  80. func (wgs *watchergroups) maybeJoinWatcherSingle(rid receiverID, ws watcherSingle) bool {
  81. wgs.mu.Lock()
  82. defer wgs.mu.Unlock()
  83. group, ok := wgs.groups[ws.w.wr]
  84. if ok {
  85. if ws.w.rev >= group.rev {
  86. group.add(receiverID{streamID: ws.sws.id, watcherID: ws.w.id}, ws.w)
  87. return true
  88. }
  89. return false
  90. }
  91. if ws.canPromote() {
  92. wg := newWatchergroup(ws.ch, ws.cancel)
  93. wgs.groups[ws.w.wr] = wg
  94. wg.add(receiverID{streamID: ws.sws.id, watcherID: ws.w.id}, ws.w)
  95. go wg.run()
  96. return true
  97. }
  98. return false
  99. }
  100. func (wgs *watchergroups) stop() {
  101. wgs.mu.Lock()
  102. defer wgs.mu.Unlock()
  103. for _, wg := range wgs.groups {
  104. wg.stop()
  105. }
  106. wgs.groups = nil
  107. }