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

Store gateway mmap removal: use a shared pool of buffers across all Decbuf instances #3691

Merged
merged 3 commits into from
Dec 13, 2022
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
18 changes: 2 additions & 16 deletions pkg/storegateway/indexheader/encoding/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
package encoding

import (
"bufio"
"encoding/binary"
"hash/crc32"
"os"
"sync"

"github.com/grafana/dskit/multierror"
"github.com/pkg/errors"
Expand All @@ -19,17 +17,11 @@ const readerBufferSize = 4096

// DecbufFactory creates new file-backed decoding buffer instances for a specific index-header file.
type DecbufFactory struct {
pool sync.Pool
path string
}

func NewDecbufFactory(path string) *DecbufFactory {
return &DecbufFactory{
pool: sync.Pool{
New: func() any {
return bufio.NewReaderSize(nil, readerBufferSize)
},
},
path: path,
}
}
Expand Down Expand Up @@ -65,10 +57,9 @@ func (df *DecbufFactory) NewDecbufAtChecked(offset int, table *crc32.Table) Decb
return Decbuf{E: errors.Wrapf(ErrInvalidSize, "insufficient bytes read for size (got %d, wanted %d)", n, 4)}
}

bufReader := df.pool.Get().(*bufio.Reader)
contentLength := int(binary.BigEndian.Uint32(lengthBytes))
bufferLength := len(lengthBytes) + contentLength + crc32.Size
r, err := NewFileReader(f, offset, bufferLength, bufReader)
r, err := NewFileReader(f, offset, bufferLength)
if err != nil {
return Decbuf{E: errors.Wrap(err, "create file reader")}
}
Expand Down Expand Up @@ -124,9 +115,8 @@ func (df *DecbufFactory) NewRawDecbuf() Decbuf {
return Decbuf{E: errors.Wrap(err, "stat file for decbuf")}
}

bufReader := df.pool.Get().(*bufio.Reader)
fileSize := stat.Size()
reader, err := NewFileReader(f, 0, int(fileSize), bufReader)
reader, err := NewFileReader(f, 0, int(fileSize))
if err != nil {
return Decbuf{E: errors.Wrap(err, "file reader for decbuf")}
}
Expand All @@ -137,10 +127,6 @@ func (df *DecbufFactory) NewRawDecbuf() Decbuf {

// Close cleans up any resources associated with the Decbuf
charleskorn marked this conversation as resolved.
Show resolved Hide resolved
func (df *DecbufFactory) Close(d Decbuf) error {
if d.r != nil {
df.pool.Put(d.r.buf)
}

return d.close()
}

Expand Down
15 changes: 13 additions & 2 deletions pkg/storegateway/indexheader/encoding/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"sync"
)

type FileReader struct {
Expand All @@ -18,12 +19,18 @@ type FileReader struct {
pos int
}

var bufferPool = sync.Pool{
New: func() any {
return bufio.NewReaderSize(nil, readerBufferSize)
},
}

// NewFileReader creates a new FileReader for the segment of file beginning at base bytes
// extending length bytes using the supplied buffered reader.
func NewFileReader(file *os.File, base, length int, buf *bufio.Reader) (*FileReader, error) {
func NewFileReader(file *os.File, base, length int) (*FileReader, error) {
f := &FileReader{
file: file,
buf: buf,
buf: bufferPool.Get().(*bufio.Reader),
base: base,
length: length,
}
Expand Down Expand Up @@ -143,5 +150,9 @@ func (f *FileReader) Len() int {
// is unexported to ensure that all resource management is handled by DecbufFactory
// which pools resources.
func (f *FileReader) close() error {
// Note that we don't do anything to clean up the buffer before returning it to the pool here:
// we reset the buffer when we retrieve it from the pool instead.
bufferPool.Put(f.buf)
charleskorn marked this conversation as resolved.
Show resolved Hide resolved

return f.file.Close()
}
7 changes: 3 additions & 4 deletions pkg/storegateway/indexheader/encoding/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package encoding

import (
"bufio"
"os"
"path"
"testing"
Expand Down Expand Up @@ -185,7 +184,7 @@ func TestReaders_CreationWithEmptyContents(t *testing.T) {
require.NoError(t, f.Close())
})

r, err := NewFileReader(f, 0, 0, bufio.NewReader(f))
r, err := NewFileReader(f, 0, 0)
require.NoError(t, err)
require.ErrorIs(t, r.Skip(1), ErrInvalidSize)
require.ErrorIs(t, r.ResetAt(1), ErrInvalidSize)
Expand All @@ -206,7 +205,7 @@ func testReaders(t *testing.T, test func(t *testing.T, r *FileReader)) {
require.NoError(t, f.Close())
})

r, err := NewFileReader(f, 0, len(testReaderContents), bufio.NewReader(f))
r, err := NewFileReader(f, 0, len(testReaderContents))
require.NoError(t, err)

test(t, r)
Expand All @@ -226,7 +225,7 @@ func testReaders(t *testing.T, test func(t *testing.T, r *FileReader)) {
require.NoError(t, f.Close())
})

r, err := NewFileReader(f, len(offsetBytes), len(testReaderContents), bufio.NewReader(f))
r, err := NewFileReader(f, len(offsetBytes), len(testReaderContents))
require.NoError(t, err)

test(t, r)
Expand Down