diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 52bea97fc46a..1c19af428bb4 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 39f6fb5cd30c..c48ac0042334 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 1e34d303bc5e..216349b90aff 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) }