2016-05-13 06:50:33 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-09-15 23:54:11 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-04-25 22:32:58 +03:00
|
|
|
package mvcc
|
2015-09-09 19:49:28 +03:00
|
|
|
|
|
|
|
import (
|
2016-01-03 11:11:26 +03:00
|
|
|
"errors"
|
2015-09-09 19:49:28 +03:00
|
|
|
"sync"
|
|
|
|
|
2016-04-25 22:32:58 +03:00
|
|
|
"github.com/coreos/etcd/mvcc/mvccpb"
|
2015-09-09 19:49:28 +03:00
|
|
|
)
|
|
|
|
|
2016-01-03 11:11:26 +03:00
|
|
|
var (
|
2016-04-25 22:32:58 +03:00
|
|
|
ErrWatcherNotExist = errors.New("mvcc: watcher does not exist")
|
2016-01-03 11:11:26 +03:00
|
|
|
)
|
|
|
|
|
2016-01-05 06:50:17 +03:00
|
|
|
type WatchID int64
|
|
|
|
|
2016-01-03 02:59:32 +03:00
|
|
|
type WatchStream interface {
|
2016-01-03 07:20:22 +03:00
|
|
|
// Watch creates a watcher. The watcher watches the events happening or
|
2016-02-26 19:55:28 +03:00
|
|
|
// happened on the given key or range [key, end) from the given startRev.
|
2016-01-03 07:20:22 +03:00
|
|
|
//
|
2015-11-03 04:43:41 +03:00
|
|
|
// The whole event history can be watched unless compacted.
|
|
|
|
// If `startRev` <=0, watch observes events after currentRev.
|
2016-01-03 07:20:22 +03:00
|
|
|
//
|
|
|
|
// The returned `id` is the ID of this watcher. It appears as WatchID
|
|
|
|
// in events that are sent to the created watcher through stream channel.
|
2016-01-03 11:11:26 +03:00
|
|
|
//
|
2016-02-26 19:55:28 +03:00
|
|
|
Watch(key, end []byte, startRev int64) WatchID
|
2015-10-29 07:10:58 +03:00
|
|
|
|
2016-01-03 23:48:58 +03:00
|
|
|
// Chan returns a chan. All watch response will be sent to the returned chan.
|
|
|
|
Chan() <-chan WatchResponse
|
2015-11-03 04:43:41 +03:00
|
|
|
|
2016-03-03 09:34:54 +03:00
|
|
|
// RequestProgress requests the progress of the watcher with given ID. The response
|
|
|
|
// will only be sent if the watcher is currently synced.
|
|
|
|
// The responses will be sent through the WatchRespone Chan attached
|
|
|
|
// with this stream to ensure correct ordering.
|
|
|
|
// The responses contains no events. The revision in the response is the progress
|
|
|
|
// of the watchers since the watcher is currently synced.
|
|
|
|
RequestProgress(id WatchID)
|
|
|
|
|
2016-01-03 11:11:26 +03:00
|
|
|
// Cancel cancels a watcher by giving its ID. If watcher does not exist, an error will be
|
|
|
|
// returned.
|
2016-01-05 06:50:17 +03:00
|
|
|
Cancel(id WatchID) error
|
2016-01-03 11:11:26 +03:00
|
|
|
|
2016-02-01 08:42:39 +03:00
|
|
|
// Close closes Chan and release all related resources.
|
2015-11-03 04:43:41 +03:00
|
|
|
Close()
|
2016-01-06 08:33:50 +03:00
|
|
|
|
|
|
|
// Rev returns the current revision of the KV the stream watches on.
|
|
|
|
Rev() int64
|
2015-10-29 07:10:58 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:48:58 +03:00
|
|
|
type WatchResponse struct {
|
2016-01-05 06:50:17 +03:00
|
|
|
// WatchID is the WatchID of the watcher this response sent to.
|
|
|
|
WatchID WatchID
|
2016-01-06 08:33:50 +03:00
|
|
|
|
2016-01-03 23:48:58 +03:00
|
|
|
// Events contains all the events that needs to send.
|
2016-04-25 22:32:58 +03:00
|
|
|
Events []mvccpb.Event
|
2016-01-06 08:33:50 +03:00
|
|
|
|
|
|
|
// Revision is the revision of the KV when the watchResponse is created.
|
|
|
|
// For a normal response, the revision should be the same as the last
|
|
|
|
// modified revision inside Events. For a delayed response to a unsynced
|
|
|
|
// watcher, the revision is greater than the last modified revision
|
|
|
|
// inside Events.
|
|
|
|
Revision int64
|
2016-02-03 10:17:27 +03:00
|
|
|
|
2016-02-12 02:00:03 +03:00
|
|
|
// CompactRevision is set when the watcher is cancelled due to compaction.
|
|
|
|
CompactRevision int64
|
2016-01-03 23:48:58 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 07:20:22 +03:00
|
|
|
// watchStream contains a collection of watchers that share
|
2016-01-03 02:59:32 +03:00
|
|
|
// one streaming chan to send out watched events and other control events.
|
|
|
|
type watchStream struct {
|
2015-11-03 04:43:41 +03:00
|
|
|
watchable watchable
|
2016-01-03 23:48:58 +03:00
|
|
|
ch chan WatchResponse
|
2015-09-09 19:49:28 +03:00
|
|
|
|
2016-01-03 07:20:22 +03:00
|
|
|
mu sync.Mutex // guards fields below it
|
|
|
|
// nextID is the ID pre-allocated for next new watcher in this stream
|
2016-03-03 09:34:54 +03:00
|
|
|
nextID WatchID
|
|
|
|
closed bool
|
|
|
|
cancels map[WatchID]cancelFunc
|
|
|
|
watchers map[WatchID]*watcher
|
2015-09-09 19:49:28 +03:00
|
|
|
}
|
|
|
|
|
2016-02-21 16:05:03 +03:00
|
|
|
// Watch creates a new watcher in the stream and returns its WatchID.
|
2015-11-03 04:43:41 +03:00
|
|
|
// TODO: return error if ws is closed?
|
2016-02-26 19:55:28 +03:00
|
|
|
func (ws *watchStream) Watch(key, end []byte, startRev int64) WatchID {
|
2015-11-03 04:43:41 +03:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
|
|
|
if ws.closed {
|
2016-01-05 03:17:55 +03:00
|
|
|
return -1
|
2015-09-09 19:49:28 +03:00
|
|
|
}
|
2015-11-20 21:50:53 +03:00
|
|
|
|
2016-01-05 03:17:55 +03:00
|
|
|
id := ws.nextID
|
2015-11-20 21:50:53 +03:00
|
|
|
ws.nextID++
|
|
|
|
|
2016-03-03 09:34:54 +03:00
|
|
|
w, c := ws.watchable.watch(key, end, startRev, id, ws.ch)
|
2015-11-20 21:50:53 +03:00
|
|
|
|
2016-01-03 11:11:26 +03:00
|
|
|
ws.cancels[id] = c
|
2016-03-03 09:34:54 +03:00
|
|
|
ws.watchers[id] = w
|
2016-01-05 03:17:55 +03:00
|
|
|
return id
|
2015-09-09 19:49:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:48:58 +03:00
|
|
|
func (ws *watchStream) Chan() <-chan WatchResponse {
|
2015-11-03 04:43:41 +03:00
|
|
|
return ws.ch
|
2015-09-09 19:49:28 +03:00
|
|
|
}
|
|
|
|
|
2016-01-05 06:50:17 +03:00
|
|
|
func (ws *watchStream) Cancel(id WatchID) error {
|
2016-06-03 21:08:00 +03:00
|
|
|
ws.mu.Lock()
|
2016-01-03 11:11:26 +03:00
|
|
|
cancel, ok := ws.cancels[id]
|
2016-06-03 21:08:00 +03:00
|
|
|
ok = ok && !ws.closed
|
|
|
|
if ok {
|
|
|
|
delete(ws.cancels, id)
|
|
|
|
delete(ws.watchers, id)
|
|
|
|
}
|
|
|
|
ws.mu.Unlock()
|
2016-01-03 11:11:26 +03:00
|
|
|
if !ok {
|
|
|
|
return ErrWatcherNotExist
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 02:59:32 +03:00
|
|
|
func (ws *watchStream) Close() {
|
2015-11-03 04:43:41 +03:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
|
|
|
|
|
|
|
for _, cancel := range ws.cancels {
|
|
|
|
cancel()
|
2015-09-09 19:49:28 +03:00
|
|
|
}
|
2015-11-03 04:43:41 +03:00
|
|
|
ws.closed = true
|
|
|
|
close(ws.ch)
|
2016-01-03 02:59:32 +03:00
|
|
|
watchStreamGauge.Dec()
|
2015-09-09 19:49:28 +03:00
|
|
|
}
|
2016-01-06 08:33:50 +03:00
|
|
|
|
|
|
|
func (ws *watchStream) Rev() int64 {
|
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
|
|
|
return ws.watchable.rev()
|
|
|
|
}
|
2016-03-03 09:34:54 +03:00
|
|
|
|
|
|
|
func (ws *watchStream) RequestProgress(id WatchID) {
|
|
|
|
ws.mu.Lock()
|
|
|
|
w, ok := ws.watchers[id]
|
|
|
|
ws.mu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.watchable.progress(w)
|
|
|
|
}
|