Skip to content

Commit b3e9aa0

Browse files
sseshaalexqyle
authored andcommitted
Support Zstd compression for gRPC (cortexproject#5092)
* add zstd compression to grpcclient Signed-off-by: Sesha Sendhil <[email protected]> * fix use of deprecated ioutil pkg Signed-off-by: Sesha Sendhil <[email protected]> * update docs Signed-off-by: Sesha Sendhil <[email protected]> * update CHANGELOG Signed-off-by: Sesha Sendhil <[email protected]> * mark as feature Signed-off-by: Sesha Sendhil <[email protected]> Signed-off-by: Sesha Sendhil <[email protected]> Signed-off-by: Alex Le <[email protected]>
1 parent 18df1e8 commit b3e9aa0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+26588
-657
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [FEATURE] Query Frontend/Scheduler: Add a new counter metric `cortex_request_queue_requests_total` for total requests going to queue. #5030
1919
* [FEATURE] Build ARM docker images. #5041
2020
* [FEATURE] Query-frontend/Querier: Create spans to measure time to merge promql responses. #5041
21+
* [FEATURE] Added zstd as an option for grpc compression #5092
2122
* [BUGFIX] Updated `golang.org/x/net` dependency to fix CVE-2022-27664. #5008
2223
* [BUGFIX] Fix panic when otel and xray tracing is enabled. #5044
2324

docs/configuration/config-file-reference.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ query_scheduler:
198198
[max_send_msg_size: <int> | default = 16777216]
199199

200200
# Use compression when sending messages. Supported values are: 'gzip',
201-
# 'snappy' and '' (disable compression)
201+
# 'snappy', 'zstd' and '' (disable compression)
202202
# CLI flag: -query-scheduler.grpc-client-config.grpc-compression
203203
[grpc_compression: <string> | default = ""]
204204

@@ -963,7 +963,7 @@ grpc_client_config:
963963
[max_send_msg_size: <int> | default = 16777216]
964964
965965
# Use compression when sending messages. Supported values are: 'gzip',
966-
# 'snappy' and '' (disable compression)
966+
# 'snappy', 'zstd' and '' (disable compression)
967967
# CLI flag: -frontend.grpc-client-config.grpc-compression
968968
[grpc_compression: <string> | default = ""]
969969
@@ -1126,7 +1126,7 @@ ruler_client:
11261126
[max_send_msg_size: <int> | default = 16777216]
11271127
11281128
# Use compression when sending messages. Supported values are: 'gzip',
1129-
# 'snappy' and '' (disable compression)
1129+
# 'snappy', 'zstd' and '' (disable compression)
11301130
# CLI flag: -ruler.client.grpc-compression
11311131
[grpc_compression: <string> | default = ""]
11321132
@@ -2118,7 +2118,7 @@ grpc_client_config:
21182118
[max_send_msg_size: <int> | default = 16777216]
21192119
21202120
# Use compression when sending messages. Supported values are: 'gzip',
2121-
# 'snappy' and '' (disable compression)
2121+
# 'snappy', 'zstd' and '' (disable compression)
21222122
# CLI flag: -ingester.client.grpc-compression
21232123
[grpc_compression: <string> | default = ""]
21242124
@@ -2225,7 +2225,7 @@ grpc_client_config:
22252225
[max_send_msg_size: <int> | default = 16777216]
22262226
22272227
# Use compression when sending messages. Supported values are: 'gzip',
2228-
# 'snappy' and '' (disable compression)
2228+
# 'snappy', 'zstd' and '' (disable compression)
22292229
# CLI flag: -querier.frontend-client.grpc-compression
22302230
[grpc_compression: <string> | default = ""]
22312231

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/hashicorp/go-sockaddr v1.0.2
3232
github.com/hashicorp/memberlist v0.5.0
3333
github.com/json-iterator/go v1.1.12
34+
github.com/klauspost/compress v1.15.14
3435
github.com/lib/pq v1.10.7
3536
github.com/minio/minio-go/v7 v7.0.46
3637
github.com/mitchellh/go-wordwrap v1.0.1
@@ -150,7 +151,6 @@ require (
150151
github.com/josharian/intern v1.0.0 // indirect
151152
github.com/jpillora/backoff v1.0.0 // indirect
152153
github.com/julienschmidt/httprouter v1.3.0 // indirect
153-
github.com/klauspost/compress v1.15.9 // indirect
154154
github.com/klauspost/cpuid/v2 v2.1.0 // indirect
155155
github.com/kylelemons/godebug v1.1.0 // indirect
156156
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,8 +1022,8 @@ github.com/klauspost/compress v1.11.13/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdY
10221022
github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
10231023
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
10241024
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
1025-
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
1026-
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
1025+
github.com/klauspost/compress v1.15.14 h1:i7WCKDToww0wA+9qrUZ1xOjp218vfFo3nTU6UHp+gOc=
1026+
github.com/klauspost/compress v1.15.14/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
10271027
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
10281028
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
10291029
github.com/klauspost/cpuid/v2 v2.1.0 h1:eyi1Ad2aNJMW95zcSbmGg7Cg6cq3ADwLpMAP96d8rF0=

pkg/util/grpcclient/grpcclient.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/cortexproject/cortex/pkg/util/backoff"
1515
"github.com/cortexproject/cortex/pkg/util/grpcencoding/snappy"
16+
"github.com/cortexproject/cortex/pkg/util/grpcencoding/zstd"
1617
"github.com/cortexproject/cortex/pkg/util/tls"
1718
)
1819

@@ -40,7 +41,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
4041
func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
4142
f.IntVar(&cfg.MaxRecvMsgSize, prefix+".grpc-max-recv-msg-size", 100<<20, "gRPC client max receive message size (bytes).")
4243
f.IntVar(&cfg.MaxSendMsgSize, prefix+".grpc-max-send-msg-size", 16<<20, "gRPC client max send message size (bytes).")
43-
f.StringVar(&cfg.GRPCCompression, prefix+".grpc-compression", "", "Use compression when sending messages. Supported values are: 'gzip', 'snappy' and '' (disable compression)")
44+
f.StringVar(&cfg.GRPCCompression, prefix+".grpc-compression", "", "Use compression when sending messages. Supported values are: 'gzip', 'snappy', 'zstd' and '' (disable compression)")
4445
f.Float64Var(&cfg.RateLimit, prefix+".grpc-client-rate-limit", 0., "Rate limit for gRPC client; 0 means disabled.")
4546
f.IntVar(&cfg.RateLimitBurst, prefix+".grpc-client-rate-limit-burst", 0, "Rate limit burst for gRPC client.")
4647
f.BoolVar(&cfg.BackoffOnRatelimits, prefix+".backoff-on-ratelimits", false, "Enable backoff and retry when we hit ratelimits.")
@@ -53,7 +54,7 @@ func (cfg *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
5354

5455
func (cfg *Config) Validate(log log.Logger) error {
5556
switch cfg.GRPCCompression {
56-
case gzip.Name, snappy.Name, "":
57+
case gzip.Name, snappy.Name, zstd.Name, "":
5758
// valid
5859
default:
5960
return errors.Errorf("unsupported compression type: %s", cfg.GRPCCompression)

pkg/util/grpcencoding/zstd/zstd.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package zstd
2+
3+
import (
4+
"bytes"
5+
"io"
6+
7+
"github.com/klauspost/compress/zstd"
8+
"google.golang.org/grpc/encoding"
9+
)
10+
11+
const Name = "zstd"
12+
13+
type compressor struct {
14+
encoder *zstd.Encoder
15+
decoder *zstd.Decoder
16+
}
17+
18+
func init() {
19+
enc, _ := zstd.NewWriter(nil)
20+
dec, _ := zstd.NewReader(nil)
21+
c := &compressor{
22+
encoder: enc,
23+
decoder: dec,
24+
}
25+
encoding.RegisterCompressor(c)
26+
}
27+
28+
// SetLevel updates the registered compressor to use a particular compression
29+
// level. NOTE: this function must only be called from an init function, and
30+
// is not threadsafe.
31+
func SetLevel(level zstd.EncoderLevel) error {
32+
c := encoding.GetCompressor(Name).(*compressor)
33+
34+
enc, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(level))
35+
if err != nil {
36+
return err
37+
}
38+
39+
c.encoder = enc
40+
return nil
41+
}
42+
43+
func (c *compressor) Compress(w io.Writer) (io.WriteCloser, error) {
44+
return &zstdWriteCloser{
45+
enc: c.encoder,
46+
writer: w,
47+
}, nil
48+
}
49+
50+
type zstdWriteCloser struct {
51+
enc *zstd.Encoder
52+
writer io.Writer // Compressed data will be written here.
53+
buf bytes.Buffer // Buffer uncompressed data here, compress on Close.
54+
}
55+
56+
func (z *zstdWriteCloser) Write(p []byte) (int, error) {
57+
return z.buf.Write(p)
58+
}
59+
60+
func (z *zstdWriteCloser) Close() error {
61+
compressed := z.enc.EncodeAll(z.buf.Bytes(), nil)
62+
_, err := io.Copy(z.writer, bytes.NewReader(compressed))
63+
return err
64+
}
65+
66+
func (c *compressor) Decompress(r io.Reader) (io.Reader, error) {
67+
compressed, err := io.ReadAll(r)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
uncompressed, err := c.decoder.DecodeAll(compressed, nil)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
return bytes.NewReader(uncompressed), nil
78+
}
79+
80+
func (c *compressor) Name() string {
81+
return Name
82+
}

vendor/github.com/klauspost/compress/.gitattributes

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/klauspost/compress/.gitignore

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/klauspost/compress/.goreleaser.yml

Lines changed: 141 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)