forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_reporter.go
347 lines (326 loc) · 10.9 KB
/
status_reporter.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) 2016-2019 Tigera, Inc. All rights reserved.
//
// 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 statusrep
import (
"context"
"time"
log "github.com/sirupsen/logrus"
"github.com/projectcalico/felix/jitter"
"github.com/projectcalico/felix/proto"
"github.com/projectcalico/libcalico-go/lib/backend/model"
"github.com/projectcalico/libcalico-go/lib/errors"
"github.com/projectcalico/libcalico-go/lib/set"
)
type EndpointStatusReporter struct {
hostname string
region string
endpointUpdates <-chan interface{}
inSync <-chan bool
stop chan bool
datastore datastore
epStatusIDToStatus map[model.Key]string
queuedDirtyIDs set.Set
activeDirtyIDs set.Set
reportingDelay time.Duration
resyncInterval time.Duration
resyncTicker stoppable
resyncTickerC <-chan time.Time
rateLimitTicker stoppable
rateLimitTickerC <-chan time.Time
}
func NewEndpointStatusReporter(hostname string,
region string,
endpointUpdates <-chan interface{},
inSync <-chan bool,
datastore datastore,
reportingDelay time.Duration,
resyncInterval time.Duration) *EndpointStatusReporter {
resyncSchedulingTicker := jitter.NewTicker(resyncInterval, resyncInterval/10)
updateRateLimitTicker := jitter.NewTicker(reportingDelay, reportingDelay/10)
return newEndpointStatusReporterWithTickerChans(
hostname,
region,
endpointUpdates,
inSync,
datastore,
resyncSchedulingTicker,
resyncSchedulingTicker.C,
updateRateLimitTicker,
updateRateLimitTicker.C,
reportingDelay,
resyncInterval,
)
}
// newEndpointStatusReporterWithTickerChans is an internal constructor allowing
// the tickers to be mocked for UT.
func newEndpointStatusReporterWithTickerChans(hostname string,
region string,
endpointUpdates <-chan interface{},
inSync <-chan bool,
datastore datastore,
resyncTicker stoppable,
resyncTickerChan <-chan time.Time,
rateLimitTicker stoppable,
rateLimitTickerChan <-chan time.Time,
reportingDelay time.Duration,
resyncInterval time.Duration) *EndpointStatusReporter {
return &EndpointStatusReporter{
hostname: hostname,
region: region,
endpointUpdates: endpointUpdates,
datastore: datastore,
inSync: inSync,
stop: make(chan bool),
epStatusIDToStatus: make(map[model.Key]string),
queuedDirtyIDs: set.New(),
activeDirtyIDs: set.New(),
resyncTicker: resyncTicker,
resyncTickerC: resyncTickerChan,
rateLimitTicker: rateLimitTicker,
rateLimitTickerC: rateLimitTickerChan,
reportingDelay: reportingDelay,
resyncInterval: resyncInterval,
}
}
// datastore is a copy of the parts of the backend client API that we need.
// See github.com/projectcalico/libcalico-go/lib/backend/api for more detail.
type datastore interface {
List(ctx context.Context, list model.ListInterface, revision string) (*model.KVPairList, error)
Apply(ctx context.Context, object *model.KVPair) (*model.KVPair, error)
Delete(ctx context.Context, key model.Key, revision string) (*model.KVPair, error)
}
type stoppable interface {
Stop()
}
func (esr *EndpointStatusReporter) Start() {
go esr.loopHandlingEndpointStatusUpdates()
}
// loopHandlingEndpointStatusUpdates is the main loop for the status reporter;
// its processing is divided into two phases. In the first phase, it waits on
// its various input channels and updates its cached state. In the second
// phase, it works to bring the datastore into sync. Datastore updates are
// rate-limited and jittered to coalesce flapping status updates and to avoid
// thundering herd issues.
func (esr *EndpointStatusReporter) loopHandlingEndpointStatusUpdates() {
log.Infof("Starting endpoint status reporter loop with resync "+
"interval %v, report rate limit: 1/%v", esr.resyncInterval,
esr.reportingDelay)
datamodelInSync := false
resyncRequested := false
ctx := context.Background()
loop:
for {
updatesAllowed := false
select {
case <-esr.stop:
log.Info("Stopping endpoint status reporter")
esr.resyncTicker.Stop()
esr.rateLimitTicker.Stop()
break loop
case <-esr.resyncTickerC:
log.Debug("Endpoint status resync tick: scheduling cleanup")
resyncRequested = true
case <-esr.rateLimitTickerC:
updatesAllowed = true
case inSync := <-esr.inSync:
log.Debug("Datamodel in sync, enabling status resync")
datamodelInSync = datamodelInSync || inSync
case msg := <-esr.endpointUpdates:
var statID model.Key
var status string
switch msg := msg.(type) {
case *proto.WorkloadEndpointStatusUpdate:
statID = model.WorkloadEndpointStatusKey{
Hostname: esr.hostname,
OrchestratorID: msg.Id.OrchestratorId,
WorkloadID: msg.Id.WorkloadId,
EndpointID: msg.Id.EndpointId,
RegionString: model.RegionString(esr.region),
}
status = msg.Status.Status
case *proto.WorkloadEndpointStatusRemove:
statID = model.WorkloadEndpointStatusKey{
Hostname: esr.hostname,
OrchestratorID: msg.Id.OrchestratorId,
WorkloadID: msg.Id.WorkloadId,
EndpointID: msg.Id.EndpointId,
RegionString: model.RegionString(esr.region),
}
case *proto.HostEndpointStatusUpdate:
statID = model.HostEndpointStatusKey{
Hostname: esr.hostname,
EndpointID: msg.Id.EndpointId,
}
status = msg.Status.Status
case *proto.HostEndpointStatusRemove:
statID = model.HostEndpointStatusKey{
Hostname: esr.hostname,
EndpointID: msg.Id.EndpointId,
}
default:
log.Panicf("Unexpected message: %#v", msg)
}
if esr.epStatusIDToStatus[statID] != status {
if status != "" {
esr.epStatusIDToStatus[statID] = status
} else {
delete(esr.epStatusIDToStatus, statID)
}
if !esr.activeDirtyIDs.Contains(statID) &&
!esr.queuedDirtyIDs.Contains(statID) {
// Add the update into the queued set so that
// we delay its initial update. That prevent
// flapping at start of day.
esr.queuedDirtyIDs.Add(statID)
}
}
}
if datamodelInSync && resyncRequested {
// We're in sync and the resync timer has popped,
// do a resync with the datastore. This will look for
// out-of-sync keys in the datastore and mark them as
// dirty so that we'll make delete/update them below.
log.Debug("Doing endpoint status resync")
esr.attemptResync(ctx)
resyncRequested = false
}
if updatesAllowed {
if esr.activeDirtyIDs.Len() > 0 {
// Not throttled and there's at least one update
// pending. Choose an arbitrary update from the dirty
// set.
log.WithField("numDirtyEndpoints", esr.activeDirtyIDs.Len()).Debug(
"Unthrottled and updates pending")
var statID model.Key
esr.activeDirtyIDs.Iter(func(item interface{}) error {
statID = item.(model.Key)
return set.StopIteration
})
// Then try to write the update to the datastore.
// Note: the update could be a deletion, in which case
// the read from the cache wil return nil.
err := esr.writeEndpointStatus(ctx, statID,
esr.epStatusIDToStatus[statID])
if err != nil {
log.WithError(err).Warn(
"Failed to write endpoint status; is datastore up?")
} else {
// Success, remove the status from the dirty set.
log.WithField("statID", statID).Debug("Write successful")
esr.activeDirtyIDs.Discard(statID)
}
}
if esr.queuedDirtyIDs.Len() > 0 {
// Now copy the queued statuses to the main dirty set.
// Doing this after the attempt to write above means that
// endpoints always spend at least one interval in the
// queued set.
log.WithField("numQueuedUpdates", esr.queuedDirtyIDs.Len()).Debug(
"Copying queued set to dirty set")
esr.queuedDirtyIDs.Iter(func(item interface{}) error {
esr.activeDirtyIDs.Add(item)
return nil
})
esr.queuedDirtyIDs = set.New()
}
}
}
}
func (esr *EndpointStatusReporter) attemptResync(ctx context.Context) {
var kvs []*model.KVPair
wlListOpts := model.WorkloadEndpointStatusListOptions{
Hostname: esr.hostname,
RegionString: model.RegionString(esr.region),
}
kvl, err := esr.datastore.List(ctx, wlListOpts, "")
if err == nil {
kvs = kvl.KVPairs
} else {
log.WithError(err).Errorf("Failed to load workload endpoint statuses")
kvs = nil // Skip the following loop and try host endpoints.
}
for _, kv := range kvs {
if kv.Value == nil {
// Parse error, needs refresh.
esr.activeDirtyIDs.Add(kv.Key)
} else {
status := kv.Value.(*model.WorkloadEndpointStatus).Status
if status != esr.epStatusIDToStatus[kv.Key] {
log.WithFields(log.Fields{
"key": kv.Key,
"datastoreState": status,
"desiredState": esr.epStatusIDToStatus[kv.Key],
}).Info("Found out-of-sync workload endpoint status")
esr.activeDirtyIDs.Add(kv.Key)
}
}
}
hostListOpts := model.HostEndpointStatusListOptions{
Hostname: esr.hostname,
}
kvl, err = esr.datastore.List(ctx, hostListOpts, "")
if err == nil {
kvs = kvl.KVPairs
} else {
log.WithError(err).Error("Failed to load host endpoint statuses")
kvs = nil // Make sure we skip the following loop.
}
for _, kv := range kvs {
if kv.Value == nil {
// Parse error, needs refresh.
esr.activeDirtyIDs.Add(kv.Key)
} else {
status := kv.Value.(*model.HostEndpointStatus).Status
if status != esr.epStatusIDToStatus[kv.Key] {
log.WithFields(log.Fields{
"key": kv.Key,
"datastoreState": status,
"desiredState": esr.epStatusIDToStatus[kv.Key],
}).Infof("Found out-of-sync host endpoint status")
esr.activeDirtyIDs.Add(kv.Key)
}
}
}
}
func (esr *EndpointStatusReporter) writeEndpointStatus(ctx context.Context, epID model.Key, status string) (err error) {
kv := model.KVPair{Key: epID}
logCxt := log.WithFields(log.Fields{
"newStatus": status,
"endpointID": epID,
})
if status != "" {
logCxt.Info("Writing endpoint status")
switch epID.(type) {
case model.HostEndpointStatusKey:
kv.Value = &model.HostEndpointStatus{status}
case model.WorkloadEndpointStatusKey:
kv.Value = &model.WorkloadEndpointStatus{status}
}
applyCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
_, err = esr.datastore.Apply(applyCtx, &kv)
cancel()
} else {
logCxt.Info("Deleting endpoint status")
_, err = esr.datastore.Delete(ctx, epID, "")
if _, ok := err.(errors.ErrorResourceDoesNotExist); ok {
// Ignore non-existent resource.
err = nil
}
}
return
}
func (esr *EndpointStatusReporter) Stop() {
log.Info("Stopping endpoint status reporter")
esr.stop <- true
}