-
Notifications
You must be signed in to change notification settings - Fork 859
Reuse grpc buffer in querier's store-gateway stream #7519
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
Draft
eeldaly
wants to merge
2
commits into
cortexproject:master
Choose a base branch
from
eeldaly:querier-grpc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains hidden or 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,144 @@ | ||
| //go:build requires_docker | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "testing" | ||
|
|
||
| "github.com/prometheus/prometheus/model/labels" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/thanos-io/thanos/pkg/store/labelpb" | ||
| "github.com/thanos-io/thanos/pkg/store/storepb" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
|
|
||
| // Import cortexpb to register the cortexCodec (buffer pooling). | ||
| _ "github.com/cortexproject/cortex/pkg/cortexpb" | ||
| ) | ||
|
|
||
| // mockStoreGatewayServer implements storepb.StoreServer and streams | ||
| // pre-built SeriesResponse messages for benchmarking. | ||
| type mockStoreGatewayServer struct { | ||
| storepb.UnimplementedStoreServer | ||
| responses []*storepb.SeriesResponse | ||
| } | ||
|
|
||
| func (m *mockStoreGatewayServer) Series(_ *storepb.SeriesRequest, srv storepb.Store_SeriesServer) error { | ||
| for _, resp := range m.responses { | ||
| if err := srv.Send(resp); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // BenchmarkGrpcStoreGatewayCalls benchmarks the full gRPC path for store gateway | ||
| // Series streaming with the cortexCodec and compression enabled. | ||
| // This is the store-gateway equivalent of BenchmarkGrpcCalls (which tests the ingester path). | ||
| // | ||
| // With SeriesResponse implementing ReleasableMessage, calling Free() after each Recv() | ||
| // returns the unmarshal buffer to the pool, reducing per-message allocations by ~32KB. | ||
| func BenchmarkGrpcStoreGatewayCalls(b *testing.B) { | ||
| // Build realistic SeriesResponse messages (large enough to trigger buffer pooling). | ||
| responses := make([]*storepb.SeriesResponse, 10) | ||
| for i := range responses { | ||
| responses[i] = createStoreGatewayBenchResponse(i) | ||
| } | ||
|
|
||
| mock := &mockStoreGatewayServer{responses: responses} | ||
|
|
||
| // Start gRPC server. | ||
| listener, err := net.Listen("tcp", "localhost:0") | ||
| require.NoError(b, err) | ||
|
|
||
| gRPCServer := grpc.NewServer() | ||
| storepb.RegisterStoreServer(gRPCServer, mock) | ||
|
|
||
| go func() { | ||
| if err := gRPCServer.Serve(listener); err != nil && err != grpc.ErrServerStopped { | ||
| b.Error(err) | ||
| } | ||
| }() | ||
| defer gRPCServer.Stop() | ||
|
|
||
| // Connect client with compression (zstd via cortexCodec default call options). | ||
| conn, err := grpc.NewClient( | ||
| listener.Addr().String(), | ||
| grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
| ) | ||
| require.NoError(b, err) | ||
| defer conn.Close() | ||
|
|
||
| client := storepb.NewStoreClient(conn) | ||
|
|
||
| // freeable checks if the response supports Free() (i.e., has MessageWithBufRef embedded). | ||
| // This allows the benchmark to compile and run on both old builds (without Free) | ||
| // and new builds (with Free), so you can compare results via benchstat. | ||
| type freeable interface { | ||
| Free() | ||
| } | ||
|
|
||
| b.ReportAllocs() | ||
| b.ResetTimer() | ||
| for i := 0; i < b.N; i++ { | ||
| stream, err := client.Series(context.Background(), &storepb.SeriesRequest{}) | ||
| require.NoError(b, err) | ||
|
|
||
| for { | ||
| resp, err := stream.Recv() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| require.NoError(b, err) | ||
| if f, ok := interface{}(resp).(freeable); ok { | ||
| f.Free() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // createStoreGatewayBenchResponse creates a realistic SeriesResponse with chunk data | ||
| // large enough to exceed the buffer pooling threshold (~1KB). | ||
| func createStoreGatewayBenchResponse(n int) *storepb.SeriesResponse { | ||
| lbls := labels.FromStrings( | ||
| "__name__", fmt.Sprintf("http_requests_total_%d", n), | ||
| "cluster", "us-east-1", | ||
| "namespace", "production", | ||
| "pod", fmt.Sprintf("web-server-deployment-7f8b9c6d4f-abc%02d", n), | ||
| "container", "nginx", | ||
| "instance", fmt.Sprintf("10.0.%d.%d:8080", n, n+1), | ||
| "job", "kubernetes-pods", | ||
| ) | ||
|
|
||
| // Create chunk data (~4KB per chunk, simulating real store gateway responses). | ||
| chunkData := make([]byte, 4096) | ||
| for i := range chunkData { | ||
| chunkData[i] = byte((i + n) % 256) | ||
| } | ||
|
|
||
| numChunks := 5 + n | ||
| chunks := make([]storepb.AggrChunk, numChunks) | ||
| for i := 0; i < numChunks; i++ { | ||
| chunks[i] = storepb.AggrChunk{ | ||
| MinTime: int64(i * 7200000), | ||
| MaxTime: int64((i + 1) * 7200000), | ||
| Raw: &storepb.Chunk{ | ||
| Type: storepb.Chunk_XOR, | ||
| Data: chunkData, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| return &storepb.SeriesResponse{ | ||
| Result: &storepb.SeriesResponse_Series{ | ||
| Series: &storepb.Series{ | ||
| Labels: labelpb.ZLabelsFromPromLabels(lbls), | ||
| Chunks: chunks, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems not the right place for benchmarking store gateways. Can you move to its own folder?