Skip to content

Loki: Fix bug where items are returned to a sync.Pool incorrectly #4518

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

Merged
merged 2 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions pkg/iter/sample_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"container/heap"
"context"
"io"
"sync"

"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/logqlmodel/stats"
Expand Down Expand Up @@ -344,26 +345,31 @@ type seriesIterator struct {
}

type withCloseSampleIterator struct {
closeFn func() error
closeOnce sync.Once
closeFn func() error
errs []error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could keep the multi error as an error that would simplify the code.

SampleIterator
}

func (w *withCloseSampleIterator) Close() error {
var errs []error
if err := w.SampleIterator.Close(); err != nil {
errs = append(errs, err)
}
if err := w.closeFn(); err != nil {
errs = append(errs, err)
}
if len(errs) == 0 {
w.closeOnce.Do(func() {
if err := w.SampleIterator.Close(); err != nil {
w.errs = append(w.errs, err)
}
if err := w.closeFn(); err != nil {
w.errs = append(w.errs, err)
}

})
if len(w.errs) == 0 {
return nil
}
return util.MultiError(errs)
return util.MultiError(w.errs)
}

func SampleIteratorWithClose(it SampleIterator, closeFn func() error) SampleIterator {
return &withCloseSampleIterator{
closeOnce: sync.Once{},
closeFn: closeFn,
SampleIterator: it,
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/iter/sample_iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"testing"
"time"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"

"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/util"
)

func TestNewPeekingSampleIterator(t *testing.T) {
Expand Down Expand Up @@ -223,3 +226,51 @@ func TestNonOverlappingSampleClose(t *testing.T) {
require.Equal(t, true, a.closed.Load())
require.Equal(t, true, b.closed.Load())
}

func TestSampleIteratorWithClose_CloseIdempotent(t *testing.T) {
c := 0
closeFn := func() error {
c++
return nil
}
ni := noOpIterator{}
it := SampleIteratorWithClose(ni, closeFn)
// Multiple calls to close should result in c only ever having been incremented one time from 0 to 1
err := it.Close()
assert.NoError(t, err)
assert.EqualValues(t, 1, c)
err = it.Close()
assert.NoError(t, err)
assert.EqualValues(t, 1, c)
err = it.Close()
assert.NoError(t, err)
assert.EqualValues(t, 1, c)
}

type alwaysErrorIterator struct {
noOpIterator
}

func (alwaysErrorIterator) Close() error {
return errors.New("i always error")
}

func TestSampleIteratorWithClose_ReturnsError(t *testing.T) {
closeFn := func() error {
return errors.New("i broke")
}
ei := alwaysErrorIterator{}
it := SampleIteratorWithClose(ei, closeFn)
err := it.Close()
// Verify that a proper multi error is returned when both the iterator and the close function return errors
if me, ok := err.(util.MultiError); ok {
assert.True(t, len(me) == 2, "Expected 2 errors, one from the iterator and one from the close function")
assert.EqualError(t, me[0], "i always error")
assert.EqualError(t, me[1], "i broke")
} else {
t.Error("Expected returned error to be of type util.MultiError")
}
// A second call to Close should return the same error
err2 := it.Close()
assert.Equal(t, err, err2)
}