-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for snappy compression in receivers (#5575)
* Add support for snappy compression in receivers Distributing series between receivers is currently done by sending uncompressed payloads which can lead to high inter-zone egress costs. This commit adds support for using snappy compression for sending data from one receiver to another. Signed-off-by: Filip Petkovski <[email protected]> * Use snappy from klauspost/compress Signed-off-by: Filip Petkovski <[email protected]> * Add copyright Signed-off-by: Filip Petkovski <[email protected]>
- Loading branch information
1 parent
291b6fa
commit 6670093
Showing
5 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package snappy | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
"github.com/klauspost/compress/snappy" | ||
"google.golang.org/grpc/encoding" | ||
) | ||
|
||
// Name is the name registered for the snappy compressor. | ||
const Name = "snappy" | ||
|
||
func init() { | ||
encoding.RegisterCompressor(newCompressor()) | ||
} | ||
|
||
type compressor struct { | ||
writersPool sync.Pool | ||
readersPool sync.Pool | ||
} | ||
|
||
func newCompressor() *compressor { | ||
c := &compressor{} | ||
c.readersPool = sync.Pool{ | ||
New: func() interface{} { | ||
return snappy.NewReader(nil) | ||
}, | ||
} | ||
c.writersPool = sync.Pool{ | ||
New: func() interface{} { | ||
return snappy.NewBufferedWriter(nil) | ||
}, | ||
} | ||
return c | ||
} | ||
|
||
func (c *compressor) Name() string { | ||
return Name | ||
} | ||
|
||
func (c *compressor) Compress(w io.Writer) (io.WriteCloser, error) { | ||
wr := c.writersPool.Get().(*snappy.Writer) | ||
wr.Reset(w) | ||
return writeCloser{wr, &c.writersPool}, nil | ||
} | ||
|
||
func (c *compressor) Decompress(r io.Reader) (io.Reader, error) { | ||
dr := c.readersPool.Get().(*snappy.Reader) | ||
dr.Reset(r) | ||
return reader{dr, &c.readersPool}, nil | ||
} | ||
|
||
type writeCloser struct { | ||
writer *snappy.Writer | ||
pool *sync.Pool | ||
} | ||
|
||
func (w writeCloser) Write(p []byte) (n int, err error) { | ||
return w.writer.Write(p) | ||
} | ||
|
||
func (w writeCloser) Close() error { | ||
defer func() { | ||
w.writer.Reset(nil) | ||
w.pool.Put(w.writer) | ||
}() | ||
|
||
if w.writer != nil { | ||
return w.writer.Close() | ||
} | ||
return nil | ||
} | ||
|
||
type reader struct { | ||
reader *snappy.Reader | ||
pool *sync.Pool | ||
} | ||
|
||
func (r reader) Read(p []byte) (n int, err error) { | ||
n, err = r.reader.Read(p) | ||
if err == io.EOF { | ||
r.reader.Reset(nil) | ||
r.pool.Put(r.reader) | ||
} | ||
return n, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) The Thanos Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
package snappy | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSnappy(t *testing.T) { | ||
c := newCompressor() | ||
assert.Equal(t, "snappy", c.Name()) | ||
|
||
tests := []struct { | ||
test string | ||
input string | ||
}{ | ||
{"empty", ""}, | ||
{"short", "hello world"}, | ||
{"long", strings.Repeat("123456789", 1024)}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.test, func(t *testing.T) { | ||
var buf bytes.Buffer | ||
// Compress | ||
w, err := c.Compress(&buf) | ||
require.NoError(t, err) | ||
n, err := w.Write([]byte(test.input)) | ||
require.NoError(t, err) | ||
assert.Len(t, test.input, n) | ||
err = w.Close() | ||
require.NoError(t, err) | ||
// Decompress | ||
r, err := c.Decompress(&buf) | ||
require.NoError(t, err) | ||
out, err := io.ReadAll(r) | ||
require.NoError(t, err) | ||
assert.Equal(t, test.input, string(out)) | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkSnappyCompress(b *testing.B) { | ||
data := []byte(strings.Repeat("123456789", 1024)) | ||
c := newCompressor() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
w, _ := c.Compress(io.Discard) | ||
_, _ = w.Write(data) | ||
_ = w.Close() | ||
} | ||
} | ||
|
||
func BenchmarkSnappyDecompress(b *testing.B) { | ||
data := []byte(strings.Repeat("123456789", 1024)) | ||
c := newCompressor() | ||
var buf bytes.Buffer | ||
w, _ := c.Compress(&buf) | ||
_, _ = w.Write(data) | ||
reader := bytes.NewReader(buf.Bytes()) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
r, _ := c.Decompress(reader) | ||
_, _ = io.ReadAll(r) | ||
_, _ = reader.Seek(0, io.SeekStart) | ||
} | ||
} |