-
Notifications
You must be signed in to change notification settings - Fork 1
/
deployment.pb.query_watcher.go
332 lines (301 loc) · 9.73 KB
/
deployment.pb.query_watcher.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Code generated by protoc-gen-goten-access
// Resource: Deployment
// DO NOT EDIT!!!
package deployment_access
import (
"context"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"
gotenaccess "github.com/cloudwan/goten-sdk/runtime/access"
gotenresource "github.com/cloudwan/goten-sdk/runtime/resource"
"github.com/cloudwan/goten-sdk/types/view"
"github.com/cloudwan/goten-sdk/types/watch_type"
deployment_client "github.com/cloudwan/edgelq-sdk/meta/client/v1alpha2/deployment"
deployment "github.com/cloudwan/edgelq-sdk/meta/resources/v1alpha2/deployment"
)
// QueryWatcher is a low-level, stateless watcher.
// Initial updates are sent in chunks. Once snapshot is complete, further
// changes are incremental - unless Reset flag is set, in which case another
// snapshot is received.
type QueryWatcher struct {
client deployment_client.DeploymentServiceClient
params *QueryWatcherParams
syncDeadline time.Time
identifier int
evtsChan chan *QueryWatcherEvent
iEvtsChan chan gotenaccess.QueryWatcherEvent
resumeToken string
startingTime *timestamppb.Timestamp
}
type QueryWatcherParams struct {
Parent *deployment.ParentName
Filter *deployment.Filter
View view.View
FieldMask *deployment.Deployment_FieldMask
OrderBy *deployment.OrderBy
Cursor *deployment.PagerCursor
ChunkSize int
PageSize int
WatchType watch_type.WatchType
StartingTime *timestamppb.Timestamp
RecoveryDeadline time.Duration
RetryTimeout time.Duration
}
type QueryWatcherEvent struct {
Identifier int
Changes []*deployment.DeploymentChange
Reset bool
LostSync bool
InSync bool
SnapshotSize int64
CheckSize bool
}
func (e *QueryWatcherEvent) GetWatcherIdentifier() int {
return e.Identifier
}
func (e *QueryWatcherEvent) GetChanges() gotenresource.ResourceChangeList {
return deployment.DeploymentChangeList(e.Changes)
}
func (e *QueryWatcherEvent) IsReset() bool {
return e.Reset
}
func (e *QueryWatcherEvent) IsLostSync() bool {
return e.LostSync
}
func (e *QueryWatcherEvent) IsSync() bool {
return e.InSync
}
func (e *QueryWatcherEvent) GetSnapshotSize() int64 {
return e.SnapshotSize
}
func (e *QueryWatcherEvent) HasSnapshotSize() bool {
return e.CheckSize
}
func NewQueryWatcher(id int, client deployment_client.DeploymentServiceClient,
params *QueryWatcherParams, evtsChan chan *QueryWatcherEvent) *QueryWatcher {
return &QueryWatcher{
client: client,
params: params,
identifier: id,
evtsChan: evtsChan,
startingTime: params.StartingTime,
}
}
func NewQueryWatcherWithIChan(id int, client deployment_client.DeploymentServiceClient,
params *QueryWatcherParams, evtsChan chan gotenaccess.QueryWatcherEvent) *QueryWatcher {
return &QueryWatcher{
client: client,
params: params,
identifier: id,
iEvtsChan: evtsChan,
startingTime: params.StartingTime,
}
}
func (qw *QueryWatcher) QueryWatcher() {}
func (qw *QueryWatcher) Run(ctx context.Context) error {
log := ctxlogrus.Extract(ctx).
WithField("query-watcher", "deployment-query-watcher").
WithField("query-parent", qw.params.Parent.String()).
WithField("query-filter", qw.params.Filter.String()).
WithField("query-order-by", qw.params.OrderBy.String()).
WithField("query-cursor", qw.params.Cursor.String())
ctx = ctxlogrus.ToContext(ctx, log)
log.Infof("Running new query")
inSync := false
skipErrorBackoff := false
for {
stream, err := qw.client.WatchDeployments(ctx, &deployment_client.WatchDeploymentsRequest{
Type: qw.params.WatchType,
Parent: qw.params.Parent,
Filter: qw.params.Filter,
View: qw.params.View,
FieldMask: qw.params.FieldMask,
MaxChunkSize: int32(qw.params.ChunkSize),
OrderBy: qw.params.OrderBy,
ResumeToken: qw.resumeToken,
PageSize: int32(qw.params.PageSize),
PageToken: qw.params.Cursor,
StartingTime: qw.startingTime,
})
if err != nil {
if ctx.Err() == nil {
log.WithError(err).Warnf("watch initialization error")
}
} else {
pending := make([]*deployment.DeploymentChange, 0)
for {
resp, err := stream.Recv()
if err != nil {
if ctx.Err() == nil {
log.WithError(err).Warnf("watch error")
}
break
} else {
var outputEvt *QueryWatcherEvent
// until we reach initial sync, we will send all the data as we get to minimize
// potential impact on memory (if receiver does not need state). Later on, we will
// collect changes and send once IsCurrent flag is sent. This is to handle soft reset
// flag. Changes after initial sync are however practically always small.
skipErrorBackoff = true
if inSync {
pending = append(pending, resp.GetDeploymentChanges()...)
if resp.IsSoftReset {
log.Debugf("received soft reset after %d changes", len(pending))
pending = nil
} else if resp.IsHardReset {
log.Warnf("received hard reset after %d changes", len(pending))
qw.resumeToken = ""
inSync = false
pending = nil
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
Reset: true,
}
} else if resp.GetSnapshotSize() >= 0 {
log.Debugf("received snapshot size info: %d", resp.GetSnapshotSize())
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
SnapshotSize: resp.GetSnapshotSize(),
CheckSize: true,
Changes: pending,
InSync: true,
}
} else if resp.GetIsCurrent() {
qw.syncDeadline = time.Time{}
if resp.GetResumeToken() != "" {
qw.resumeToken = resp.GetResumeToken()
qw.startingTime = nil
}
if len(pending) > 0 {
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
Changes: pending,
InSync: true,
}
}
pending = nil
}
} else {
if resp.IsCurrent {
log.Infof("query synchronized")
inSync = true
qw.syncDeadline = time.Time{}
if resp.GetResumeToken() != "" {
qw.resumeToken = resp.GetResumeToken()
qw.startingTime = nil
}
}
outputEvt = &QueryWatcherEvent{
Identifier: qw.identifier,
Changes: resp.GetDeploymentChanges(),
SnapshotSize: resp.SnapshotSize,
Reset: resp.IsHardReset || resp.IsSoftReset,
InSync: inSync,
CheckSize: resp.SnapshotSize >= 0,
}
}
if outputEvt != nil {
qw.sendEvt(ctx, outputEvt)
}
}
}
}
if ctx.Err() != nil {
return ctx.Err()
}
// if we disconnected during initial snapshot (we were not in sync), send a message to cancel all data
if !inSync {
evt := &QueryWatcherEvent{
Identifier: qw.identifier,
Reset: true,
}
qw.sendEvt(ctx, evt)
}
if qw.syncDeadline.IsZero() && qw.params.RecoveryDeadline > 0 {
qw.syncDeadline = time.Now().UTC().Add(qw.params.RecoveryDeadline)
log.Infof("lost sync, scheduling recovery with timeout %s", qw.syncDeadline)
} else if !qw.syncDeadline.IsZero() && time.Now().UTC().After(qw.syncDeadline) {
log.Errorf("could not recover within %s, reporting lost sync", qw.syncDeadline)
evt := &QueryWatcherEvent{
Identifier: qw.identifier,
LostSync: true,
Reset: true,
}
qw.resumeToken = ""
inSync = false
qw.sendEvt(ctx, evt)
}
// If we had working watch, dont sleep on first disconnection, we are likely to be able to
// reconnect quickly and then we dont want to miss updates
if !skipErrorBackoff {
backoff := time.After(qw.params.RetryTimeout)
select {
case <-backoff:
log.Debugf("after backoff %s", qw.params.RetryTimeout)
case <-ctx.Done():
log.Debugf("context done, reason: %s", ctx.Err())
return ctx.Err()
}
} else {
skipErrorBackoff = false
}
}
}
func (qw *QueryWatcher) sendEvt(ctx context.Context, evt *QueryWatcherEvent) {
if qw.evtsChan != nil {
select {
case <-ctx.Done():
case qw.evtsChan <- evt:
}
} else {
select {
case <-ctx.Done():
case qw.iEvtsChan <- evt:
}
}
}
func init() {
gotenaccess.GetRegistry().RegisterQueryWatcherEventConstructor(deployment.GetDescriptor(),
func(evtId int, changes gotenresource.ResourceChangeList, isReset, isLostSync, isCurrent bool, snapshotSize int64) gotenaccess.QueryWatcherEvent {
return &QueryWatcherEvent{
Identifier: evtId,
Changes: changes.(deployment.DeploymentChangeList),
Reset: isReset,
LostSync: isLostSync,
InSync: isCurrent,
SnapshotSize: snapshotSize,
CheckSize: snapshotSize >= 0,
}
},
)
gotenaccess.GetRegistry().RegisterQueryWatcherConstructor(deployment.GetDescriptor(), func(id int, cc grpc.ClientConnInterface,
params *gotenaccess.QueryWatcherConfigParams, ch chan gotenaccess.QueryWatcherEvent) gotenaccess.QueryWatcher {
cfg := &QueryWatcherParams{
WatchType: params.WatchType,
View: params.View,
ChunkSize: params.ChunkSize,
PageSize: params.PageSize,
StartingTime: params.StartingTime,
RecoveryDeadline: params.RecoveryDeadline,
RetryTimeout: params.RetryTimeout,
}
if params.FieldMask != nil {
cfg.FieldMask = params.FieldMask.(*deployment.Deployment_FieldMask)
}
if params.OrderBy != nil {
cfg.OrderBy = params.OrderBy.(*deployment.OrderBy)
}
if params.Cursor != nil {
cfg.Cursor = params.Cursor.(*deployment.PagerCursor)
}
if params.Parent != nil {
cfg.Parent = params.Parent.(*deployment.ParentName)
}
if params.Filter != nil {
cfg.Filter = params.Filter.(*deployment.Filter)
}
return NewQueryWatcherWithIChan(id, deployment_client.NewDeploymentServiceClient(cc), cfg, ch)
})
}