-
Notifications
You must be signed in to change notification settings - Fork 455
/
manager.go
172 lines (150 loc) · 5.05 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package watchmanager
import (
"context"
"fmt"
"time"
"github.com/m3db/m3x/log"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
"github.com/uber-go/tally"
)
// NewWatchManager creates a new watch manager
func NewWatchManager(opts Options) (WatchManager, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
scope := opts.InstrumentsOptions().MetricsScope()
return &manager{
opts: opts,
logger: opts.InstrumentsOptions().Logger(),
m: metrics{
etcdWatchCreate: scope.Counter("etcd-watch-create"),
etcdWatchError: scope.Counter("etcd-watch-error"),
etcdWatchReset: scope.Counter("etcd-watch-reset"),
},
updateFn: opts.UpdateFn(),
tickAndStopFn: opts.TickAndStopFn(),
}, nil
}
type manager struct {
opts Options
logger log.Logger
m metrics
updateFn UpdateFn
tickAndStopFn TickAndStopFn
}
type metrics struct {
etcdWatchCreate tally.Counter
etcdWatchError tally.Counter
etcdWatchReset tally.Counter
}
func (w *manager) watchChanWithTimeout(key string, rev int64) (clientv3.WatchChan, context.CancelFunc, error) {
doneCh := make(chan struct{})
ctx, cancelFn := context.WithCancel(clientv3.WithRequireLeader(context.Background()))
var watchChan clientv3.WatchChan
go func() {
wOpts := w.opts.WatchOptions()
if rev > 0 {
wOpts = append(wOpts, clientv3.WithRev(rev))
}
watchChan = w.opts.Watcher().Watch(
ctx,
key,
wOpts...,
)
close(doneCh)
}()
timeout := w.opts.WatchChanInitTimeout()
select {
case <-doneCh:
return watchChan, cancelFn, nil
case <-time.After(timeout):
cancelFn()
return nil, nil, fmt.Errorf("etcd watch create timed out after %s for key: %s", timeout.String(), key)
}
}
func (w *manager) Watch(key string) {
ticker := time.Tick(w.opts.WatchChanCheckInterval()) //nolint: megacheck
var (
revOverride int64
watchChan clientv3.WatchChan
cancelFn context.CancelFunc
err error
)
for {
if watchChan == nil {
w.m.etcdWatchCreate.Inc(1)
watchChan, cancelFn, err = w.watchChanWithTimeout(key, revOverride)
if err != nil {
w.logger.Errorf("could not create etcd watch: %v", err)
// NB(cw) when we failed to create a etcd watch channel
// we do a get for now and will try to recreate the watch chan later
if err = w.updateFn(key, nil); err != nil {
w.logger.Errorf("failed to get value for key %s: %v", key, err)
}
// avoid recreating watch channel too frequently
time.Sleep(w.opts.WatchChanResetInterval())
continue
}
}
select {
case r, ok := <-watchChan:
if !ok {
// the watch chan is closed, set it to nil so it will be recreated
// this is unlikely to happen but just to be defensive
cancelFn()
watchChan = nil
w.logger.Warnf("etcd watch channel closed on key %s, recreating a watch channel", key)
// avoid recreating watch channel too frequently
time.Sleep(w.opts.WatchChanResetInterval())
w.m.etcdWatchReset.Inc(1)
continue
}
// handle the update
if err = r.Err(); err != nil {
w.logger.Errorf("received error on watch channel: %v", err)
w.m.etcdWatchError.Inc(1)
// do not stop here, even though the update contains an error
// we still take this chance to attempt a Get() for the latest value
// If the current revision has been compacted, set watchChan to
// nil so the watch is recreated with a valid start revision
if err == rpctypes.ErrCompacted {
w.logger.Warnf("recreating watch at revision %d", r.CompactRevision)
revOverride = r.CompactRevision
watchChan = nil
}
}
if r.IsProgressNotify() {
// Do not call updateFn on ProgressNotify as it happens periodically with no update events
continue
}
if err = w.updateFn(key, r.Events); err != nil {
w.logger.Errorf("received notification for key %s, but failed to get value: %v", key, err)
}
case <-ticker:
if w.tickAndStopFn(key) {
w.logger.Infof("watch on key %s ended", key)
return
}
}
}
}