watcher.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 key prefix from the given startRev.
  27. //
  28. // The whole event history can be watched unless compacted.
  29. // If `prefix` is true, watch observes all events whose key prefix could be the given `key`.
  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 []byte, prefix bool, startRev int64) WatchID
  36. // Chan returns a chan. All watch response will be sent to the returned chan.
  37. Chan() <-chan WatchResponse
  38. // Cancel cancels a watcher by giving its ID. If watcher does not exist, an error will be
  39. // returned.
  40. Cancel(id WatchID) error
  41. // Close closes Chan and release all related resources.
  42. Close()
  43. // Rev returns the current revision of the KV the stream watches on.
  44. Rev() int64
  45. }
  46. type WatchResponse struct {
  47. // WatchID is the WatchID of the watcher this response sent to.
  48. WatchID WatchID
  49. // Events contains all the events that needs to send.
  50. Events []storagepb.Event
  51. // Revision is the revision of the KV when the watchResponse is created.
  52. // For a normal response, the revision should be the same as the last
  53. // modified revision inside Events. For a delayed response to a unsynced
  54. // watcher, the revision is greater than the last modified revision
  55. // inside Events.
  56. Revision int64
  57. // CompactRevision is set when the watcher is cancelled due to compaction.
  58. CompactRevision int64
  59. }
  60. // watchStream contains a collection of watchers that share
  61. // one streaming chan to send out watched events and other control events.
  62. type watchStream struct {
  63. watchable watchable
  64. ch chan WatchResponse
  65. mu sync.Mutex // guards fields below it
  66. // nextID is the ID pre-allocated for next new watcher in this stream
  67. nextID WatchID
  68. closed bool
  69. cancels map[WatchID]cancelFunc
  70. }
  71. // Watch creates a new watcher in the stream and returns its WatchID.
  72. // TODO: return error if ws is closed?
  73. func (ws *watchStream) Watch(key []byte, prefix bool, startRev int64) WatchID {
  74. ws.mu.Lock()
  75. defer ws.mu.Unlock()
  76. if ws.closed {
  77. return -1
  78. }
  79. id := ws.nextID
  80. ws.nextID++
  81. _, c := ws.watchable.watch(key, prefix, startRev, id, ws.ch)
  82. ws.cancels[id] = c
  83. return id
  84. }
  85. func (ws *watchStream) Chan() <-chan WatchResponse {
  86. return ws.ch
  87. }
  88. func (ws *watchStream) Cancel(id WatchID) error {
  89. cancel, ok := ws.cancels[id]
  90. if !ok {
  91. return ErrWatcherNotExist
  92. }
  93. cancel()
  94. delete(ws.cancels, id)
  95. return nil
  96. }
  97. func (ws *watchStream) Close() {
  98. ws.mu.Lock()
  99. defer ws.mu.Unlock()
  100. for _, cancel := range ws.cancels {
  101. cancel()
  102. }
  103. ws.closed = true
  104. close(ws.ch)
  105. watchStreamGauge.Dec()
  106. }
  107. func (ws *watchStream) Rev() int64 {
  108. ws.mu.Lock()
  109. defer ws.mu.Unlock()
  110. return ws.watchable.rev()
  111. }