watcher.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2015 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 mvcc
  15. import (
  16. "errors"
  17. "sync"
  18. "github.com/coreos/etcd/mvcc/mvccpb"
  19. )
  20. var (
  21. ErrWatcherNotExist = errors.New("mvcc: watcher does not exist")
  22. )
  23. type WatchID int64
  24. // FilterFunc returns true if the given event should be filtered out.
  25. type FilterFunc func(e mvccpb.Event) bool
  26. type WatchStream interface {
  27. // Watch creates a watcher. The watcher watches the events happening or
  28. // happened on the given key or range [key, end) from the given startRev.
  29. //
  30. // The whole event history can be watched unless compacted.
  31. // If `startRev` <=0, watch observes events after currentRev.
  32. //
  33. // The returned `id` is the ID of this watcher. It appears as WatchID
  34. // in events that are sent to the created watcher through stream channel.
  35. //
  36. Watch(key, end []byte, startRev int64, fcs ...FilterFunc) WatchID
  37. // Chan returns a chan. All watch response will be sent to the returned chan.
  38. Chan() <-chan WatchResponse
  39. // RequestProgress requests the progress of the watcher with given ID. The response
  40. // will only be sent if the watcher is currently synced.
  41. // The responses will be sent through the WatchRespone Chan attached
  42. // with this stream to ensure correct ordering.
  43. // The responses contains no events. The revision in the response is the progress
  44. // of the watchers since the watcher is currently synced.
  45. RequestProgress(id WatchID)
  46. // Cancel cancels a watcher by giving its ID. If watcher does not exist, an error will be
  47. // returned.
  48. Cancel(id WatchID) error
  49. // Close closes Chan and release all related resources.
  50. Close()
  51. // Rev returns the current revision of the KV the stream watches on.
  52. Rev() int64
  53. }
  54. type WatchResponse struct {
  55. // WatchID is the WatchID of the watcher this response sent to.
  56. WatchID WatchID
  57. // Events contains all the events that needs to send.
  58. Events []mvccpb.Event
  59. // Revision is the revision of the KV when the watchResponse is created.
  60. // For a normal response, the revision should be the same as the last
  61. // modified revision inside Events. For a delayed response to a unsynced
  62. // watcher, the revision is greater than the last modified revision
  63. // inside Events.
  64. Revision int64
  65. // CompactRevision is set when the watcher is cancelled due to compaction.
  66. CompactRevision int64
  67. }
  68. // watchStream contains a collection of watchers that share
  69. // one streaming chan to send out watched events and other control events.
  70. type watchStream struct {
  71. watchable watchable
  72. ch chan WatchResponse
  73. mu sync.Mutex // guards fields below it
  74. // nextID is the ID pre-allocated for next new watcher in this stream
  75. nextID WatchID
  76. closed bool
  77. cancels map[WatchID]cancelFunc
  78. watchers map[WatchID]*watcher
  79. }
  80. // Watch creates a new watcher in the stream and returns its WatchID.
  81. // TODO: return error if ws is closed?
  82. func (ws *watchStream) Watch(key, end []byte, startRev int64, fcs ...FilterFunc) WatchID {
  83. ws.mu.Lock()
  84. defer ws.mu.Unlock()
  85. if ws.closed {
  86. return -1
  87. }
  88. id := ws.nextID
  89. ws.nextID++
  90. w, c := ws.watchable.watch(key, end, startRev, id, ws.ch, fcs...)
  91. ws.cancels[id] = c
  92. ws.watchers[id] = w
  93. return id
  94. }
  95. func (ws *watchStream) Chan() <-chan WatchResponse {
  96. return ws.ch
  97. }
  98. func (ws *watchStream) Cancel(id WatchID) error {
  99. ws.mu.Lock()
  100. cancel, ok := ws.cancels[id]
  101. ok = ok && !ws.closed
  102. if ok {
  103. delete(ws.cancels, id)
  104. delete(ws.watchers, id)
  105. }
  106. ws.mu.Unlock()
  107. if !ok {
  108. return ErrWatcherNotExist
  109. }
  110. cancel()
  111. return nil
  112. }
  113. func (ws *watchStream) Close() {
  114. ws.mu.Lock()
  115. defer ws.mu.Unlock()
  116. for _, cancel := range ws.cancels {
  117. cancel()
  118. }
  119. ws.closed = true
  120. close(ws.ch)
  121. watchStreamGauge.Dec()
  122. }
  123. func (ws *watchStream) Rev() int64 {
  124. ws.mu.Lock()
  125. defer ws.mu.Unlock()
  126. return ws.watchable.rev()
  127. }
  128. func (ws *watchStream) RequestProgress(id WatchID) {
  129. ws.mu.Lock()
  130. w, ok := ws.watchers[id]
  131. ws.mu.Unlock()
  132. if !ok {
  133. return
  134. }
  135. ws.watchable.progress(w)
  136. }