forked from k3s-io/kine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
191 lines (163 loc) · 4.31 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
package server
import (
"context"
"sync"
"sync/atomic"
"github.com/sirupsen/logrus"
"go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
)
var watchID int64
// explicit interface check
var _ etcdserverpb.WatchServer = (*KVServerBridge)(nil)
func (s *KVServerBridge) Watch(ws etcdserverpb.Watch_WatchServer) error {
w := watcher{
server: ws,
backend: s.limited.backend,
watches: map[int64]func(){},
}
defer w.Close()
logrus.Tracef("WATCH SERVER CREATE")
for {
msg, err := ws.Recv()
if err != nil {
return err
}
if cr := msg.GetCreateRequest(); cr != nil {
w.Start(ws.Context(), cr)
}
if cr := msg.GetCancelRequest(); cr != nil {
logrus.Tracef("WATCH CANCEL REQ id=%d", cr.WatchId)
w.Cancel(cr.WatchId, 0, 0, nil)
}
}
}
type watcher struct {
sync.Mutex
wg sync.WaitGroup
backend Backend
server etcdserverpb.Watch_WatchServer
watches map[int64]func()
}
func (w *watcher) Start(ctx context.Context, r *etcdserverpb.WatchCreateRequest) {
w.Lock()
defer w.Unlock()
ctx, cancel := context.WithCancel(ctx)
id := atomic.AddInt64(&watchID, 1)
w.watches[id] = cancel
w.wg.Add(1)
key := string(r.Key)
logrus.Tracef("WATCH START id=%d, count=%d, key=%s, revision=%d", id, len(w.watches), key, r.StartRevision)
go func() {
defer w.wg.Done()
if err := w.server.Send(&etcdserverpb.WatchResponse{
Header: &etcdserverpb.ResponseHeader{},
Created: true,
WatchId: id,
}); err != nil {
w.Cancel(id, 0, 0, err)
return
}
wr := w.backend.Watch(ctx, key, r.StartRevision)
// If the watch result has a non-zero CompactRevision, then the watch request failed due to
// the requested start revision having been compacted. Pass the current and and compact
// revision to the client via the cancel response, along with the correct error message.
if wr.CompactRevision != 0 {
w.Cancel(id, wr.CurrentRevision, wr.CompactRevision, ErrCompacted)
return
}
outer := true
for outer {
// Block on initial read from channel
reads := 1
events := <-wr.Events
// Collect additional queued events from the channel
inner := true
for inner {
select {
case e, ok := <-wr.Events:
reads++
events = append(events, e...)
if !ok {
// channel was closed, break out of both loops
inner = false
outer = false
}
default:
inner = false
}
}
// Send collected events in a single response
if len(events) > 0 {
if logrus.IsLevelEnabled(logrus.TraceLevel) {
for _, event := range events {
logrus.Tracef("WATCH READ id=%d, key=%s, revision=%d", id, event.KV.Key, event.KV.ModRevision)
}
}
wr := &etcdserverpb.WatchResponse{
Header: txnHeader(events[len(events)-1].KV.ModRevision),
WatchId: id,
Events: toEvents(events...),
}
logrus.Tracef("WATCH SEND id=%d, events=%d, size=%d reads=%d", id, len(wr.Events), wr.Size(), reads)
if err := w.server.Send(wr); err != nil {
w.Cancel(id, 0, 0, err)
}
}
}
w.Cancel(id, 0, 0, nil)
logrus.Tracef("WATCH CLOSE id=%d, key=%s", id, key)
}()
}
func toEvents(events ...*Event) []*mvccpb.Event {
ret := make([]*mvccpb.Event, 0, len(events))
for _, e := range events {
ret = append(ret, toEvent(e))
}
return ret
}
func toEvent(event *Event) *mvccpb.Event {
e := &mvccpb.Event{
Kv: toKV(event.KV),
PrevKv: toKV(event.PrevKV),
}
if event.Delete {
e.Type = mvccpb.DELETE
} else {
e.Type = mvccpb.PUT
}
return e
}
func (w *watcher) Cancel(watchID, revision, compactRev int64, err error) {
w.Lock()
if cancel, ok := w.watches[watchID]; ok {
cancel()
delete(w.watches, watchID)
}
w.Unlock()
reason := ""
if err != nil {
reason = err.Error()
}
logrus.Tracef("WATCH CANCEL id=%d, reason=%s, compactRev=%d", watchID, reason, compactRev)
serr := w.server.Send(&etcdserverpb.WatchResponse{
Header: txnHeader(revision),
Canceled: err != nil,
CancelReason: reason,
WatchId: watchID,
CompactRevision: compactRev,
})
if serr != nil && err != nil && !clientv3.IsConnCanceled(serr) {
logrus.Errorf("WATCH Failed to send cancel response for watchID %d: %v", watchID, serr)
}
}
func (w *watcher) Close() {
logrus.Tracef("WATCH SERVER CLOSE")
w.Lock()
for _, v := range w.watches {
v()
}
w.Unlock()
w.wg.Wait()
}