From c70dc8f9081269d8bc1a6806af1eea955164f92d Mon Sep 17 00:00:00 2001 From: Blake Rouse Date: Wed, 22 Sep 2021 10:07:20 -0400 Subject: [PATCH] [Elastic Agent] Fix lazy acker to only add new actions to the batch (#27981) * Fix lazy acker. * Add changelog. (cherry picked from commit bdec63a5f1d6d834ad7e4db56b9168e448e82161) --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../pkg/fleetapi/acker/lazy/lazy_acker.go | 14 ++++++++++++-- .../pkg/fleetapi/acker/lazy/lazy_acker_test.go | 9 +++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 392179b29c8..55ce2e8d704 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -87,6 +87,7 @@ - Add "_monitoring" suffix to monitoring instance names to remove ambiguity with the status command. {issue}25449[25449] - Ignore ErrNotExists when fixing permissions. {issue}27836[27836] {pull}27846[27846] - Snapshot artifact lookup will use agent.download proxy settings. {issue}27903[27903] {pull}27904[27904] +- Fix lazy acker to only add new actions to the batch. {pull}27981[27981] ==== New features diff --git a/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker.go b/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker.go index 39f6fb5cd30..c48ac004233 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker.go +++ b/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker.go @@ -37,8 +37,7 @@ func NewAcker(baseAcker batchAcker, log *logger.Logger) *Acker { // Ack acknowledges action. func (f *Acker) Ack(ctx context.Context, action fleetapi.Action) error { - f.queue = append(f.queue, action) - f.log.Debugf("appending action with id '%s' to the queue", action.ID()) + f.enqueue(action) if _, isAckForced := action.(ackForcer); isAckForced { return f.Commit(ctx) @@ -58,3 +57,14 @@ func (f *Acker) Commit(ctx context.Context) error { f.queue = make([]fleetapi.Action, 0) return nil } + +func (f *Acker) enqueue(action fleetapi.Action) { + for _, a := range f.queue { + if a.ID() == action.ID() { + f.log.Debugf("action with id '%s' has already been queued", action.ID()) + return + } + } + f.queue = append(f.queue, action) + f.log.Debugf("appending action with id '%s' to the queue", action.ID()) +} diff --git a/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker_test.go b/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker_test.go index 1e34d303bc5..216349b90af 100644 --- a/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker_test.go +++ b/x-pack/elastic-agent/pkg/fleetapi/acker/lazy/lazy_acker_test.go @@ -79,12 +79,21 @@ func TestLazyAcker(t *testing.T) { }() c := context.Background() + if err := lacker.Ack(c, testAction1); err != nil { + t.Fatal(err) + } if err := lacker.Ack(c, testAction1); err != nil { t.Fatal(err) } if err := lacker.Ack(c, testAction2); err != nil { t.Fatal(err) } + if err := lacker.Ack(c, testAction2); err != nil { + t.Fatal(err) + } + if err := lacker.Ack(c, testAction3); err != nil { + t.Fatal(err) + } if err := lacker.Ack(c, testAction3); err != nil { t.Fatal(err) }