forked from elastic/fleet-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.go
519 lines (436 loc) · 13.6 KB
/
monitor.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package policy
import (
"context"
"errors"
"sync"
"time"
"github.com/rs/zerolog"
"go.elastic.co/apm/v2"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/dl"
"github.com/elastic/fleet-server/v7/internal/pkg/es"
"github.com/elastic/fleet-server/v7/internal/pkg/logger"
"github.com/elastic/fleet-server/v7/internal/pkg/model"
"github.com/elastic/fleet-server/v7/internal/pkg/monitor"
)
const cloudPolicyID = "policy-elastic-agent-on-cloud"
/*
Design should have the following properties
Policy rollout scheduling should...
1) be fair; delivered in first come first server order.
2) be throttled to avoid uncontrolled impact on resources, particularly CPU.
3) adapt to subscribers that drop offline.
4) attempt to deliver the latest policy to each subscriber at the time of delivery.
5) prioritize delivery to agents that supervise fleet-servers.
This implementation addresses the above issues by queuing subscription requests per
policy, and moving requests to the pending queue when the requirement is met; ie.
the policy is updateable.
If the subscription is unsubscribed (ie. the agent drops offline), this implementation
will remove the subscription request from its current location in either the waiting
queue on the policy or the pending queue.
Ordering is achieved with a simple double linked list implementation that allows object
migration across queues, and O(1) unlink without knowledge about which queue the subscription
is in.
*/
type Subscription interface {
// Output returns a new policy that needs to be sent based on the current subscription.
Output() <-chan *ParsedPolicy
}
type Monitor interface {
// Run runs the monitor.
Run(ctx context.Context) error
// Subscribe creates a new subscription for a policy update.
Subscribe(agentID string, policyID string, revisionIdx int64, coordinatorIdx int64) (Subscription, error)
// Unsubscribe removes the current subscription.
Unsubscribe(sub Subscription) error
}
type policyFetcher func(ctx context.Context, bulker bulk.Bulk, opt ...dl.Option) ([]model.Policy, error)
type policyT struct {
pp ParsedPolicy
head *subT
}
type monitorT struct {
log zerolog.Logger
mut sync.Mutex
bulker bulk.Bulk
monitor monitor.Monitor
kickCh chan struct{}
deployCh chan struct{}
policies map[string]policyT
pendingQ *subT
policyF policyFetcher
policiesIndex string
startCh chan struct{}
}
// NewMonitor creates the policy monitor for subscribing agents.
func NewMonitor(bulker bulk.Bulk, monitor monitor.Monitor) Monitor {
return &monitorT{
bulker: bulker,
monitor: monitor,
kickCh: make(chan struct{}, 1),
deployCh: make(chan struct{}, 1),
policies: make(map[string]policyT),
pendingQ: makeHead(),
policyF: dl.QueryLatestPolicies,
policiesIndex: dl.FleetPolicies,
startCh: make(chan struct{}),
}
}
// endTrans is a convenience function to end the passed transaction if it's not nil
func endTrans(t *apm.Transaction) {
if t != nil {
t.End()
}
}
// Run runs the monitor.
func (m *monitorT) Run(ctx context.Context) error {
m.log = zerolog.Ctx(ctx).With().Str("ctx", "policy agent monitor").Logger()
m.log.Info().
Msg("run policy monitor")
s := m.monitor.Subscribe()
defer m.monitor.Unsubscribe(s)
close(m.startCh)
var iCtx context.Context
var trans *apm.Transaction
LOOP:
for {
m.log.Trace().Msg("policy monitor loop start")
iCtx = ctx
select {
case <-m.kickCh:
m.log.Trace().Msg("policy monitor kicked")
if m.bulker.HasTracer() {
trans = m.bulker.StartTransaction("initial policies", "policy_monitor")
iCtx = apm.ContextWithTransaction(ctx, trans)
}
if err := m.loadPolicies(iCtx); err != nil {
endTrans(trans)
return err
}
m.dispatchPending(iCtx)
endTrans(trans)
case <-m.deployCh:
m.log.Trace().Msg("policy monitor deploy ch")
if m.bulker.HasTracer() {
trans = m.bulker.StartTransaction("forced policies", "policy_monitor")
iCtx = apm.ContextWithTransaction(ctx, trans)
}
m.dispatchPending(iCtx)
endTrans(trans)
case hits := <-s.Output(): // TODO would be nice to attach transaction IDs to hits, but would likely need a bigger refactor.
m.log.Trace().Int("hits", len(hits)).Msg("policy monitor hits from sub")
if m.bulker.HasTracer() {
trans = m.bulker.StartTransaction("output policies", "policy_monitor")
iCtx = apm.ContextWithTransaction(ctx, trans)
}
if err := m.processHits(iCtx, hits); err != nil {
endTrans(trans)
return err
}
m.dispatchPending(iCtx)
endTrans(trans)
case <-ctx.Done():
break LOOP
}
}
return nil
}
func unmarshalHits(hits []es.HitT) ([]model.Policy, error) {
policies := make([]model.Policy, len(hits))
for i, hit := range hits {
err := hit.Unmarshal(&policies[i])
if err != nil {
return nil, err
}
}
return policies, nil
}
func (m *monitorT) processHits(ctx context.Context, hits []es.HitT) error {
span, ctx := apm.StartSpan(ctx, "process hits", "process")
defer span.End()
policies, err := unmarshalHits(hits)
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("fail unmarshal hits")
return err
}
return m.processPolicies(ctx, policies)
}
// waitStart returns once Run has started
// It's used in tests.
func (m *monitorT) waitStart(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-m.startCh:
}
return nil
}
// dispatchPending will dispatch all pending policy changes to the subscriptions in the queue.
// dispatches are rate limited by the monitor's limiter.
func (m *monitorT) dispatchPending(ctx context.Context) {
span, ctx := apm.StartSpan(ctx, "dispatch pending", "dispatch")
defer span.End()
m.mut.Lock()
defer m.mut.Unlock()
ts := time.Now()
nQueued := 0
s := m.pendingQ.popFront()
if s == nil {
return
}
for s != nil {
// Lookup the latest policy for this subscription
policy, ok := m.policies[s.policyID]
if !ok {
m.log.Warn().
Str(logger.PolicyID, s.policyID).
Msg("logic error: policy missing on dispatch")
return
}
select {
case <-ctx.Done():
m.log.Debug().Err(ctx.Err()).Msg("context termination detected in policy dispatch")
return
case s.ch <- &policy.pp:
m.log.Debug().
Str(logger.AgentID, s.agentID).
Str(logger.PolicyID, s.policyID).
Int64("subscription_revision_idx", s.revIdx).
Int64("subscription_coordinator_idx", s.coordIdx).
Int64(dl.FieldRevisionIdx, policy.pp.Policy.RevisionIdx).
Int64(dl.FieldCoordinatorIdx, policy.pp.Policy.CoordinatorIdx).
Msg("dispatch policy change")
default:
// Should never block on a channel; we created a channel of size one.
// A block here indicates a logic error somewheres.
m.log.Error().
Str(logger.PolicyID, s.policyID).
Str(logger.AgentID, s.agentID).
Msg("logic error: should never block on policy channel")
return
}
s = m.pendingQ.popFront()
nQueued += 1
}
dur := time.Since(ts)
m.log.Debug().Dur("event.duration", dur).Int("nSubs", nQueued).
Msg("policy monitor dispatch complete")
}
func (m *monitorT) loadPolicies(ctx context.Context) error {
span, ctx := apm.StartSpan(ctx, "Load policies", "load")
defer span.End()
if m.bulker.HasTracer() {
tctx := span.TraceContext()
trans := m.bulker.StartTransactionOptions("Load policies", "bulker", apm.TransactionOptions{Links: []apm.SpanLink{{
Trace: tctx.Trace,
Span: tctx.Span,
}}})
ctx = apm.ContextWithTransaction(ctx, trans)
defer trans.End()
}
policies, err := m.policyF(ctx, m.bulker, dl.WithIndexName(m.policiesIndex))
if err != nil {
if errors.Is(err, es.ErrIndexNotFound) {
m.log.Debug().
Str("index", m.policiesIndex).
Msg(es.ErrIndexNotFound.Error())
return nil
}
return err
}
if len(policies) == 0 {
m.log.Debug().Msg("no policy to monitor")
return nil
}
return m.processPolicies(ctx, policies)
}
func (m *monitorT) processPolicies(ctx context.Context, policies []model.Policy) error {
span, ctx := apm.StartSpan(ctx, "process policies", "process")
defer span.End()
if len(policies) == 0 {
return nil
}
m.log.Debug().Int64(dl.FieldRevisionIdx, policies[0].RevisionIdx).
Int64(dl.FieldCoordinatorIdx, policies[0].CoordinatorIdx).
Str(logger.PolicyID, policies[0].PolicyID).Msg("process policies")
latest := m.groupByLatest(policies)
for _, policy := range latest {
pp, err := NewParsedPolicy(ctx, m.bulker, policy)
if err != nil {
return err
}
m.updatePolicy(ctx, pp)
}
return nil
}
func groupByLatest(policies []model.Policy) map[string]model.Policy {
latest := make(map[string]model.Policy)
for _, policy := range policies {
curr, ok := latest[policy.PolicyID]
if !ok {
latest[policy.PolicyID] = policy
continue
}
if policy.RevisionIdx > curr.RevisionIdx {
latest[policy.PolicyID] = policy
continue
} else if policy.RevisionIdx == curr.RevisionIdx && policy.CoordinatorIdx > curr.CoordinatorIdx {
latest[policy.PolicyID] = policy
}
}
return latest
}
func (m *monitorT) groupByLatest(policies []model.Policy) map[string]model.Policy {
return groupByLatest(policies)
}
func (m *monitorT) updatePolicy(ctx context.Context, pp *ParsedPolicy) bool {
newPolicy := pp.Policy
span, _ := apm.StartSpan(ctx, "update policy", "process")
span.Context.SetLabel(logger.PolicyID, newPolicy.PolicyID)
span.Context.SetLabel(dl.FieldRevisionIdx, newPolicy.RevisionIdx)
span.Context.SetLabel(dl.FieldCoordinatorIdx, newPolicy.CoordinatorIdx)
defer span.End()
zlog := m.log.With().
Str(logger.PolicyID, newPolicy.PolicyID).
Int64(dl.FieldRevisionIdx, newPolicy.RevisionIdx).
Int64(dl.FieldCoordinatorIdx, newPolicy.CoordinatorIdx).
Logger()
if newPolicy.CoordinatorIdx <= 0 {
zlog.Info().Str(logger.PolicyID, newPolicy.PolicyID).Msg("Ignore policy that has not passed through coordinator")
return false
}
m.mut.Lock()
defer m.mut.Unlock()
p, ok := m.policies[newPolicy.PolicyID]
if !ok {
p = policyT{
pp: *pp,
head: makeHead(),
}
m.policies[newPolicy.PolicyID] = p
zlog.Info().Str(logger.PolicyID, newPolicy.PolicyID).Msg("New policy found on update and added")
return false
}
// Cache the old stored policy for logging
oldPolicy := p.pp.Policy
// Update the policy in our data structure
p.pp = *pp
m.policies[newPolicy.PolicyID] = p
zlog.Debug().Str(logger.PolicyID, newPolicy.PolicyID).Msg("Update policy revision")
// Iterate through the subscriptions on this policy;
// schedule any subscription for delivery that requires an update.
nQueued := 0
iter := NewIterator(p.head)
for sub := iter.Next(); sub != nil; sub = iter.Next() {
if sub.isUpdate(&newPolicy) {
// Unlink the target node from the list
iter.Unlink()
// Push the node onto the pendingQ
// HACK: if update is for cloud agent, put on front of queue
// not at the end for immediate delivery.
if newPolicy.PolicyID == cloudPolicyID {
m.pendingQ.pushFront(sub)
} else {
m.pendingQ.pushBack(sub)
}
zlog.Debug().
Str(logger.AgentID, sub.agentID).
Msg("scheduled pendingQ on policy revision")
nQueued += 1
}
}
zlog.Info().
Int64("old_revision_idx", oldPolicy.RevisionIdx).
Int64("old_coordinator_idx", oldPolicy.CoordinatorIdx).
Int("nSubs", nQueued).
Str(logger.PolicyID, newPolicy.PolicyID).
Msg("New revision of policy received and added to the queue")
return true
}
func (m *monitorT) kickLoad() {
select {
case m.kickCh <- struct{}{}:
default:
m.log.Debug().Msg("kick channel full")
}
}
func (m *monitorT) kickDeploy() {
select {
case m.deployCh <- struct{}{}:
default:
}
}
// Subscribe creates a new subscription for a policy update.
func (m *monitorT) Subscribe(agentID string, policyID string, revisionIdx int64, coordinatorIdx int64) (Subscription, error) {
if revisionIdx < 0 {
return nil, errors.New("revisionIdx must be greater than or equal to 0")
}
if coordinatorIdx < 0 {
return nil, errors.New("coordinatorIdx must be greater than or equal to 0")
}
m.log.Debug().
Str(logger.AgentID, agentID).
Str(logger.PolicyID, policyID).
Int64(dl.FieldRevisionIdx, revisionIdx).
Int64(dl.FieldCoordinatorIdx, coordinatorIdx).
Msg("subscribed to policy monitor")
s := NewSub(
policyID,
agentID,
revisionIdx,
coordinatorIdx,
)
m.mut.Lock()
defer m.mut.Unlock()
p, ok := m.policies[policyID]
switch {
case !ok:
// We've not seen this policy before, force load.
m.log.Info().
Str(logger.PolicyID, policyID).
Str(logger.AgentID, s.agentID).
Msg("force load on unknown policyId")
p = policyT{head: makeHead()}
p.head.pushBack(s)
m.policies[policyID] = p
m.kickLoad()
case s.isUpdate(&p.pp.Policy):
empty := m.pendingQ.isEmpty()
m.pendingQ.pushBack(s)
m.log.Debug().
Str(logger.AgentID, s.agentID).
Int64(dl.FieldRevisionIdx, (&p.pp.Policy).RevisionIdx).
Msg("deploy pending on subscribe")
if empty {
m.kickDeploy()
}
default:
m.log.Debug().
Str(logger.PolicyID, policyID).
Str(logger.AgentID, s.agentID).
Int64(dl.FieldRevisionIdx, (&p.pp.Policy).RevisionIdx).
Msg("subscription added without new revision")
p.head.pushBack(s)
}
return s, nil
}
// Unsubscribe removes the current subscription.
func (m *monitorT) Unsubscribe(sub Subscription) error {
s, ok := sub.(*subT)
if !ok {
return errors.New("not a subscription returned from this monitor")
}
m.mut.Lock()
s.unlink()
m.mut.Unlock()
m.log.Debug().
Str(logger.AgentID, s.agentID).
Str(logger.PolicyID, s.policyID).
Int64(dl.FieldRevisionIdx, s.revIdx).
Int64(dl.FieldCoordinatorIdx, s.coordIdx).
Msg("unsubscribe")
return nil
}