watcher.go 4.4 KB

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