watcher.go 4.2 KB

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