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

Close iterator properly and check nil before releasing buffers. #1480

Merged
merged 1 commit into from
Jan 8, 2020
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
15 changes: 10 additions & 5 deletions pkg/chunkenc/memchunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,16 +695,21 @@ func (si *bufferedIterator) close() {
current.BytesDecompressed += si.bytesDecompressed
current.BytesCompressed += int64(len(si.origBytes))
})
si.pool.PutReader(si.reader)
BufReaderPool.Put(si.bufReader)
if si.reader != nil {
si.pool.PutReader(si.reader)
si.reader = nil
}
if si.bufReader != nil {
BufReaderPool.Put(si.bufReader)
si.bufReader = nil
}

if si.buf != nil {
BytesBufferPool.Put(si.buf)
si.buf = nil
}
si.origBytes = nil
si.bufReader = nil
si.buf = nil
si.decBuf = nil
si.reader = nil
}

func (si *bufferedIterator) Labels() string { return "" }
42 changes: 42 additions & 0 deletions pkg/chunkenc/memchunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/dustin/go-humanize"
"github.com/grafana/loki/pkg/chunkenc/testdata"
"github.com/grafana/loki/pkg/iter"
"github.com/grafana/loki/pkg/logproto"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -394,6 +395,47 @@ func TestChunkSize(t *testing.T) {
}
}

func TestIteratorClose(t *testing.T) {
for _, enc := range testEncoding {
t.Run(enc.String(), func(t *testing.T) {
for _, test := range []func(iter iter.EntryIterator, t *testing.T){
func(iter iter.EntryIterator, t *testing.T) {
// close without iterating
if err := iter.Close(); err != nil {
t.Fatal(err)
}
},
func(iter iter.EntryIterator, t *testing.T) {
// close after iterating
for iter.Next() {
_ = iter.Entry()
}
if err := iter.Close(); err != nil {
t.Fatal(err)
}
},
func(iter iter.EntryIterator, t *testing.T) {
// close after a single iteration
iter.Next()
_ = iter.Entry()
if err := iter.Close(); err != nil {
t.Fatal(err)
}
},
} {
c := NewMemChunk(enc)
inserted := fillChunk(c)
iter, err := c.Iterator(context.Background(), time.Unix(0, 0), time.Unix(0, inserted), logproto.BACKWARD, nil)
if err != nil {
t.Fatal(err)
}
test(iter, t)
}

})
}
}

var result []Chunk

func BenchmarkWrite(b *testing.B) {
Expand Down
14 changes: 12 additions & 2 deletions pkg/iter/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,12 @@ func (i *entryIteratorBackward) Entry() logproto.Entry {
return i.cur
}

func (i *entryIteratorBackward) Close() error { return nil }
func (i *entryIteratorBackward) Close() error {
if !i.loaded {
return i.forwardIter.Close()
}
return nil
}

func (i *entryIteratorBackward) Error() error { return nil }

Expand Down Expand Up @@ -608,7 +613,12 @@ func (i *entryIteratorForward) Entry() logproto.Entry {
return i.cur.entry
}

func (i *entryIteratorForward) Close() error { return nil }
func (i *entryIteratorForward) Close() error {
if !i.loaded {
cyriltovena marked this conversation as resolved.
Show resolved Hide resolved
return i.backwardIter.Close()
}
return nil
}

func (i *entryIteratorForward) Error() error { return nil }

Expand Down