Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event deletion in the memqueue #41340

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libbeat/publisher/queue/memqueue/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ var batchPool = sync.Pool{
}

func newBatch(queue *broker, start, count int) *batch {
batch := batchPool.Get().(*batch)
batch := batchPool.Get().(*batch) //nolint: errcheck // we want this to panic if there's an error
batch.next = nil
batch.queue = queue
batch.start = start
Expand Down
10 changes: 6 additions & 4 deletions libbeat/publisher/queue/memqueue/runloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,14 @@ func (l *runLoop) handleGetReply(req *getRequest) {

func (l *runLoop) handleDelete(count int) {
byteCount := 0
// remove entries from the queue
for i := 0; i < count; i++ {
entry := l.broker.buf[(l.bufPos+i)%len(l.broker.buf)]
byteCount += entry.eventSize
index := (l.bufPos + i) % len(l.broker.buf)
byteCount += l.broker.buf[index].eventSize
l.broker.buf[index].event = nil
}
// Advance position and counters. Event data was already cleared in
// batch.FreeEntries when the events were vended.

// advance the buffer position and update tracking fields
l.bufPos = (l.bufPos + count) % len(l.broker.buf)
l.eventCount -= count
l.consumedCount -= count
Expand Down
10 changes: 10 additions & 0 deletions libbeat/publisher/queue/memqueue/runloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package memqueue

import (
"context"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -176,11 +177,20 @@ func TestObserverRemoveEvents(t *testing.T) {
// Initialize the queue entries to a test byte size
for i := range rl.broker.buf {
rl.broker.buf[i].eventSize = 123
rl.broker.buf[i].event = publisher.Event{}
}
const deleteCount = 25
rl.broker.deleteChan <- deleteCount
// Run one iteration of the run loop, so it can handle the delete request
rl.runIteration()

// The entries should actually be deleted
expectedRemaining := len(rl.broker.buf) - deleteCount
remainingEntries := slices.DeleteFunc(slices.Clone(rl.broker.buf), func(entry queueEntry) bool {
return entry.event == nil
})
assert.Len(t, remainingEntries, expectedRemaining)

// It should have deleted 25 events, so we expect the size to be 25 * 123.
assertRegistryUint(t, reg, "queue.removed.events", deleteCount, "Deleting from the queue should report the removed events")
assertRegistryUint(t, reg, "queue.removed.bytes", deleteCount*123, "Deleting from the queue should report the removed bytes")
Expand Down
Loading