watcher.go 3.5 KB

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