Skip to content

querymiddleware: Fix race condition in shardActiveSeriesMiddleware #7290

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

Merged
merged 2 commits into from
Feb 5, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
* `active_series`: active series query
* `other`: any other request
* [BUGFIX] Fix performance regression introduced in Mimir 2.11.0 when uploading blocks to AWS S3. #7240
* [BUGFIX] Query-frontend: fix race condition when sharding active series is enabled (see above) and response is compressed with snappy. #7290

### Mixin

Expand Down
12 changes: 4 additions & 8 deletions pkg/frontend/querymiddleware/shard_active_series.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,13 @@ type shardActiveSeriesMiddleware struct {
upstream http.RoundTripper
limits Limits
logger log.Logger
encoder *s2.Writer
}

func newShardActiveSeriesMiddleware(upstream http.RoundTripper, limits Limits, logger log.Logger) http.RoundTripper {
return &shardActiveSeriesMiddleware{
upstream: upstream,
limits: limits,
logger: logger,
encoder: s2.NewWriter(nil),
}
}

Expand Down Expand Up @@ -340,19 +338,17 @@ func (s *shardActiveSeriesMiddleware) mergeResponses(ctx context.Context, respon
}

func (s *shardActiveSeriesMiddleware) writeMergedResponse(ctx context.Context, check func() error, w io.WriteCloser, items <-chan *labels.Builder, encodingType string) {
defer func(encoder, w io.Closer) {
_ = encoder.Close()
_ = w.Close()
}(s.encoder, w)
defer w.Close()

span, _ := opentracing.StartSpanFromContext(ctx, "shardActiveSeries.writeMergedResponse")
defer span.Finish()

var out io.Writer = w
if encodingType == encodingTypeSnappyFramed {
span.LogFields(otlog.String("encoding", encodingTypeSnappyFramed))
out = s.encoder
s.encoder.Reset(w)
enc := s2.NewWriter(w)
defer enc.Close()
out = enc
} else {
span.LogFields(otlog.String("encoding", "none"))
}
Expand Down
84 changes: 0 additions & 84 deletions pkg/frontend/querymiddleware/shard_active_series_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ func Test_shardActiveSeriesMiddleware_RoundTrip(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

// Stub upstream with valid or invalid responses.
var requestCount atomic.Int32
upstream := RoundTripFunc(func(r *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -359,69 +358,6 @@ func Test_shardActiveSeriesMiddleware_RoundTrip(t *testing.T) {
}
}

func Test_shardActiveSeriesMiddleware_RoundTrip_ResponseBodyStreamed(t *testing.T) {
// This value needs to be set at least as large as the buffer size used by the
// implementation for this test to make sense.
const bufferSize = 512
const shardCount = 2

// Stub upstream with two responses that are larger than the buffer size and
// retain a reference to the response bodies. The responses use a custom body
// type that counts the number of bytes read, so we can assert on that later in
// the test.
var upstreamResponseBodies [shardCount]*bodyReadBytesCounter
var responseSize [shardCount]int
upstream := RoundTripFunc(func(r *http.Request) (*http.Response, error) {
// Extract requested shard index
require.NoError(t, r.ParseForm())
req, err := cardinality.DecodeActiveSeriesRequestFromValues(r.Form)
require.NoError(t, err)
shard, _, err := sharding.ShardFromMatchers(req.Matchers)
require.NoError(t, err)
require.NotNil(t, shard, "this test requires a shard to be requested")

// Make sure the response body is big enough to not be buffered entirely.
response := fmt.Sprintf(fmt.Sprintf(`{"data": [{"__name__": "metric-%%0%dd"}]}`, bufferSize), shard.ShardIndex)
body := &bodyReadBytesCounter{body: io.NopCloser(strings.NewReader(response))}
upstreamResponseBodies[shard.ShardIndex] = body
responseSize[shard.ShardIndex] = len(response)

return &http.Response{StatusCode: http.StatusOK, Body: body}, nil
})

s := newShardActiveSeriesMiddleware(
upstream,
mockLimits{maxShardedQueries: shardCount, totalShards: shardCount},
log.NewNopLogger(),
)

r := httptest.NewRequest("POST", "/active_series", strings.NewReader(`selector={__name__=~"metric-.*"}`))
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := s.RoundTrip(r.WithContext(user.InjectOrgID(r.Context(), "test")))
require.NoError(t, err)
defer func(body io.ReadCloser) {
_, _ = io.ReadAll(body)
_ = body.Close()
}(resp.Body)

// Check that upstream responses have been read only up to a max of the buffer size.
for _, body := range upstreamResponseBodies {
bytesRead := int(body.BytesRead())
require.GreaterOrEqual(t, bufferSize, bytesRead)
}

// Read and close the response body.
_, _ = io.ReadAll(resp.Body)
_ = resp.Body.Close()

// Check that upstream responses have been fully read now.
for i, body := range upstreamResponseBodies {
bytesRead := int(body.BytesRead())
assert.Equal(t, responseSize[i], bytesRead)
}
}

func BenchmarkActiveSeriesMiddlewareMergeResponses(b *testing.B) {
type activeSeriesResponse struct {
Data []labels.Labels `json:"data"`
Expand Down Expand Up @@ -465,26 +401,6 @@ func BenchmarkActiveSeriesMiddlewareMergeResponses(b *testing.B) {
}
}

// bodyReadBytesCounter is a wrapper around a response body that counts the number of bytes read from it.
type bodyReadBytesCounter struct {
body io.ReadCloser
bytesRead atomic.Uint64
}

func (b *bodyReadBytesCounter) Read(p []byte) (n int, err error) {
read, err := b.body.Read(p)
b.bytesRead.Add(uint64(read))
return read, err
}

func (b *bodyReadBytesCounter) Close() error {
return b.body.Close()
}

func (b *bodyReadBytesCounter) BytesRead() uint64 {
return b.bytesRead.Load()
}

type result struct {
Data []labels.Labels `json:"data"`
Status string `json:"status,omitempty"`
Expand Down