-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
watch.go
279 lines (243 loc) · 7.75 KB
/
watch.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Copyright 2019 The Vitess Authors.
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.
*/
package etcd2topo
import (
"context"
"path"
"strings"
"time"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/topo"
)
// Watch is part of the topo.Conn interface.
func (s *Server) Watch(ctx context.Context, filePath string) (*topo.WatchData, <-chan *topo.WatchData, error) {
nodePath := path.Join(s.root, filePath)
// Get the initial version of the file
initialCtx, initialCancel := context.WithTimeout(ctx, topo.RemoteOperationTimeout)
defer initialCancel()
initial, err := s.cli.Get(initialCtx, nodePath)
if err != nil {
// Generic error.
return nil, nil, convertError(err, nodePath)
}
if len(initial.Kvs) != 1 {
// Node doesn't exist.
return nil, nil, topo.NewError(topo.NoNode, nodePath)
}
wd := &topo.WatchData{
Contents: initial.Kvs[0].Value,
// ModRevision is used for the topo.Version value as we get the new Revision value back
// when updating the file/key within a transaction in file.go and so this is the opaque
// version that we can use to enforce serializabile writes for the file/key.
Version: EtcdVersion(initial.Kvs[0].ModRevision),
}
// Create an outer context that will be canceled on return and will cancel all inner watches.
outerCtx, outerCancel := context.WithCancel(ctx)
// Create a context, will be used to cancel the watch on retry.
watchCtx, watchCancel := context.WithCancel(outerCtx)
// Create the Watcher. We start watching from the response we
// got, not from the file original version, as the server may
// not have that much history.
watcher := s.cli.Watch(watchCtx, nodePath, clientv3.WithRev(initial.Header.Revision))
if watcher == nil {
watchCancel()
outerCancel()
return nil, nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "Watch failed")
}
// Create the notifications channel, send updates to it.
notifications := make(chan *topo.WatchData, 10)
go func() {
defer close(notifications)
defer outerCancel()
var rev = initial.Header.Revision
var watchRetries int
for {
select {
case <-s.running:
return
case <-watchCtx.Done():
// This includes context cancellation errors.
notifications <- &topo.WatchData{
Err: convertError(watchCtx.Err(), nodePath),
}
return
case wresp, ok := <-watcher:
if !ok {
if watchRetries > 10 {
t := time.NewTimer(time.Duration(watchRetries) * time.Second)
select {
case <-t.C:
t.Stop()
case <-s.running:
t.Stop()
continue
case <-watchCtx.Done():
t.Stop()
continue
}
}
watchRetries++
// Cancel inner context on retry and create new one.
watchCancel()
watchCtx, watchCancel = context.WithCancel(ctx)
newWatcher := s.cli.Watch(watchCtx, nodePath, clientv3.WithRev(rev))
if newWatcher == nil {
log.Warningf("watch %v failed and get a nil channel returned, rev: %v", nodePath, rev)
} else {
watcher = newWatcher
}
continue
}
watchRetries = 0
if wresp.Canceled {
// Final notification.
notifications <- &topo.WatchData{
Err: convertError(wresp.Err(), nodePath),
}
return
}
rev = wresp.Header.GetRevision()
for _, ev := range wresp.Events {
switch ev.Type {
case mvccpb.PUT:
notifications <- &topo.WatchData{
Contents: ev.Kv.Value,
Version: EtcdVersion(ev.Kv.Version),
}
case mvccpb.DELETE:
// Node is gone, send a final notice.
notifications <- &topo.WatchData{
Err: topo.NewError(topo.NoNode, nodePath),
}
return
default:
notifications <- &topo.WatchData{
Err: vterrors.Errorf(vtrpc.Code_INTERNAL, "unexpected event received: %v", ev),
}
return
}
}
}
}
}()
return wd, notifications, nil
}
// WatchRecursive is part of the topo.Conn interface.
func (s *Server) WatchRecursive(ctx context.Context, dirpath string) ([]*topo.WatchDataRecursive, <-chan *topo.WatchDataRecursive, error) {
nodePath := path.Join(s.root, dirpath)
if !strings.HasSuffix(nodePath, "/") {
nodePath = nodePath + "/"
}
// Get the initial version of the file
initial, err := s.cli.Get(ctx, nodePath, clientv3.WithPrefix())
if err != nil {
return nil, nil, convertError(err, nodePath)
}
var initialwd []*topo.WatchDataRecursive
for _, kv := range initial.Kvs {
var wd topo.WatchDataRecursive
wd.Path = string(kv.Key)
wd.Contents = kv.Value
wd.Version = EtcdVersion(initial.Kvs[0].Version)
initialwd = append(initialwd, &wd)
}
// Create an outer context that will be canceled on return and will cancel all inner watches.
outerCtx, outerCancel := context.WithCancel(ctx)
// Create a context, will be used to cancel the watch on retry.
watchCtx, watchCancel := context.WithCancel(outerCtx)
// Create the Watcher. We start watching from the response we
// got, not from the file original version, as the server may
// not have that much history.
watcher := s.cli.Watch(watchCtx, nodePath, clientv3.WithRev(initial.Header.Revision), clientv3.WithPrefix())
if watcher == nil {
watchCancel()
outerCancel()
return nil, nil, vterrors.Errorf(vtrpc.Code_INVALID_ARGUMENT, "Watch failed")
}
// Create the notifications channel, send updates to it.
notifications := make(chan *topo.WatchDataRecursive, 10)
go func() {
defer close(notifications)
defer outerCancel()
var rev = initial.Header.Revision
var watchRetries int
for {
select {
case <-s.running:
return
case <-watchCtx.Done():
// This includes context cancellation errors.
notifications <- &topo.WatchDataRecursive{
WatchData: topo.WatchData{Err: convertError(watchCtx.Err(), nodePath)},
}
return
case wresp, ok := <-watcher:
if !ok {
if watchRetries > 10 {
select {
case <-time.After(time.Duration(watchRetries) * time.Second):
case <-s.running:
continue
case <-watchCtx.Done():
continue
}
}
watchRetries++
// Cancel inner context on retry and create new one.
watchCancel()
watchCtx, watchCancel = context.WithCancel(ctx)
newWatcher := s.cli.Watch(watchCtx, nodePath, clientv3.WithRev(rev), clientv3.WithPrefix())
if newWatcher == nil {
log.Warningf("watch %v failed and get a nil channel returned, rev: %v", nodePath, rev)
} else {
watcher = newWatcher
}
continue
}
watchRetries = 0
if wresp.Canceled {
// Final notification.
notifications <- &topo.WatchDataRecursive{
WatchData: topo.WatchData{Err: convertError(wresp.Err(), nodePath)},
}
return
}
rev = wresp.Header.GetRevision()
for _, ev := range wresp.Events {
switch ev.Type {
case mvccpb.PUT:
notifications <- &topo.WatchDataRecursive{
Path: string(ev.Kv.Key),
WatchData: topo.WatchData{
Contents: ev.Kv.Value,
Version: EtcdVersion(ev.Kv.Version),
},
}
case mvccpb.DELETE:
notifications <- &topo.WatchDataRecursive{
Path: string(ev.Kv.Key),
WatchData: topo.WatchData{
Err: topo.NewError(topo.NoNode, nodePath),
},
}
}
}
}
}
}()
return initialwd, notifications, nil
}