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 panic when rejecting empty batch #5524

Merged
merged 1 commit into from
Mar 4, 2019
Merged
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
7 changes: 6 additions & 1 deletion internal/models/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func (b *Buffer) Reject(batch []telegraf.Metric) {
b.Lock()
defer b.Unlock()

if len(batch) == 0 {
return
}

older := b.dist(b.first, b.batchFirst)
free := b.cap - b.size
restore := min(len(batch), free+older)
Expand All @@ -191,7 +195,8 @@ func (b *Buffer) Reject(batch []telegraf.Metric) {
rp := b.last
re := b.nextby(rp, restore)
b.last = re
for rb != rp {

for rb != rp && rp != re {
rp = b.prev(rp)
re = b.prev(re)

Expand Down
12 changes: 12 additions & 0 deletions internal/models/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,15 @@ func TestBuffer_AddOverwriteAndRejectOffset(t *testing.T) {
require.Equal(t, 13, reject)
require.Equal(t, 5, accept)
}

func TestBuffer_RejectEmptyBatch(t *testing.T) {
b := setup(NewBuffer("test", 5))
batch := b.Batch(2)
b.Add(MetricTime(1))
b.Reject(batch)
b.Add(MetricTime(2))
batch = b.Batch(2)
for _, m := range batch {
require.NotNil(t, m)
}
}