-
Notifications
You must be signed in to change notification settings - Fork 524
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
Fix premature context cancellation in Distributor.QueryStream() when experimental ingest storage is enabled #7437
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5599,9 +5599,6 @@ func (i *mockIngester) series() map[uint32]*mimirpb.PreallocTimeseries { | |
} | ||
|
||
func (i *mockIngester) Check(context.Context, *grpc_health_v1.HealthCheckRequest, ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) { | ||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("Check") | ||
|
||
return &grpc_health_v1.HealthCheckResponse{}, nil | ||
|
@@ -5612,13 +5609,13 @@ func (i *mockIngester) Close() error { | |
} | ||
|
||
func (i *mockIngester) Push(ctx context.Context, req *mimirpb.WriteRequest, _ ...grpc.CallOption) (*mimirpb.WriteResponse, error) { | ||
i.trackCall("Push") | ||
|
||
time.Sleep(i.pushDelay) | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("Push") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -5690,17 +5687,19 @@ func makeWireChunk(c chunk.EncodedChunk) client.Chunk { | |
} | ||
|
||
func (i *mockIngester) QueryStream(ctx context.Context, req *client.QueryRequest, _ ...grpc.CallOption) (client.Ingester_QueryStreamClient, error) { | ||
i.trackCall("QueryStream") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to reviews: moving call to |
||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
time.Sleep(i.queryDelay) | ||
if err := i.enforceQueryDelay(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("QueryStream") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -5845,20 +5844,21 @@ func (i *mockIngester) QueryStream(ctx context.Context, req *client.QueryRequest | |
} | ||
|
||
return &stream{ | ||
ctx: ctx, | ||
results: results, | ||
}, nil | ||
} | ||
|
||
func (i *mockIngester) QueryExemplars(ctx context.Context, req *client.ExemplarQueryRequest, _ ...grpc.CallOption) (*client.ExemplarQueryResponse, error) { | ||
i.trackCall("QueryExemplars") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("QueryExemplars") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -5922,15 +5922,15 @@ func (i *mockIngester) QueryExemplars(ctx context.Context, req *client.ExemplarQ | |
} | ||
|
||
func (i *mockIngester) MetricsForLabelMatchers(ctx context.Context, req *client.MetricsForLabelMatchersRequest, _ ...grpc.CallOption) (*client.MetricsForLabelMatchersResponse, error) { | ||
i.trackCall("MetricsForLabelMatchers") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("MetricsForLabelMatchers") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -5952,15 +5952,15 @@ func (i *mockIngester) MetricsForLabelMatchers(ctx context.Context, req *client. | |
} | ||
|
||
func (i *mockIngester) LabelValues(ctx context.Context, req *client.LabelValuesRequest, _ ...grpc.CallOption) (*client.LabelValuesResponse, error) { | ||
i.trackCall("LabelValues") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("LabelValues") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -6003,15 +6003,15 @@ func (i *mockIngester) LabelValues(ctx context.Context, req *client.LabelValuesR | |
} | ||
|
||
func (i *mockIngester) LabelNames(ctx context.Context, req *client.LabelNamesRequest, _ ...grpc.CallOption) (*client.LabelNamesResponse, error) { | ||
i.trackCall("LabelNames") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("LabelNames") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -6035,15 +6035,15 @@ func (i *mockIngester) LabelNames(ctx context.Context, req *client.LabelNamesReq | |
} | ||
|
||
func (i *mockIngester) MetricsMetadata(ctx context.Context, _ *client.MetricsMetadataRequest, _ ...grpc.CallOption) (*client.MetricsMetadataResponse, error) { | ||
i.trackCall("MetricsMetadata") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("MetricsMetadata") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -6110,15 +6110,15 @@ func (s *labelNamesAndValuesMockStream) Recv() (*client.LabelNamesAndValuesRespo | |
} | ||
|
||
func (i *mockIngester) LabelValuesCardinality(ctx context.Context, req *client.LabelValuesCardinalityRequest, _ ...grpc.CallOption) (client.Ingester_LabelValuesCardinalityClient, error) { | ||
i.trackCall("LabelValuesCardinality") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("LabelValuesCardinality") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -6184,15 +6184,15 @@ func (s *labelValuesCardinalityStream) Recv() (*client.LabelValuesCardinalityRes | |
} | ||
|
||
func (i *mockIngester) ActiveSeries(ctx context.Context, req *client.ActiveSeriesRequest, _ ...grpc.CallOption) (client.Ingester_ActiveSeriesClient, error) { | ||
i.trackCall("ActiveSeries") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("ActiveSeries") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
@@ -6242,6 +6242,9 @@ func (s *activeSeriesStream) CloseSend() error { | |
} | ||
|
||
func (i *mockIngester) trackCall(name string) { | ||
i.Lock() | ||
defer i.Unlock() | ||
|
||
if i.calls == nil { | ||
i.calls = map[string]int{} | ||
} | ||
|
@@ -6270,6 +6273,16 @@ func (i *mockIngester) enforceReadConsistency(ctx context.Context) error { | |
return i.partitionReader.WaitReadConsistency(ctx) | ||
} | ||
|
||
func (i *mockIngester) enforceQueryDelay(ctx context.Context) error { | ||
select { | ||
case <-time.After(i.queryDelay): | ||
return nil | ||
|
||
case <-ctx.Done(): | ||
return ctx.Err() | ||
} | ||
} | ||
|
||
type mockIngesterPusherAdapter struct { | ||
ingester *mockIngester | ||
} | ||
|
@@ -6301,6 +6314,10 @@ func (i *noopIngester) Push(context.Context, *mimirpb.WriteRequest, ...grpc.Call | |
|
||
type stream struct { | ||
grpc.ClientStream | ||
|
||
// The mocked gRPC client's context. | ||
ctx context.Context | ||
|
||
i int | ||
results []*client.QueryStreamResponse | ||
} | ||
|
@@ -6310,6 +6327,12 @@ func (*stream) CloseSend() error { | |
} | ||
|
||
func (s *stream) Recv() (*client.QueryStreamResponse, error) { | ||
// Check whether the context has been canceled, so that we can test the case the context | ||
// gets cancelled while reading messages from gRPC client. | ||
if s.ctx.Err() != nil { | ||
return nil, s.ctx.Err() | ||
} | ||
|
||
if s.i >= len(s.results) { | ||
return nil, io.EOF | ||
} | ||
|
@@ -6319,23 +6342,23 @@ func (s *stream) Recv() (*client.QueryStreamResponse, error) { | |
} | ||
|
||
func (s *stream) Context() context.Context { | ||
return context.Background() | ||
return s.ctx | ||
} | ||
|
||
func (i *mockIngester) AllUserStats(context.Context, *client.UserStatsRequest, ...grpc.CallOption) (*client.UsersStatsResponse, error) { | ||
return &i.stats, nil | ||
} | ||
|
||
func (i *mockIngester) UserStats(ctx context.Context, _ *client.UserStatsRequest, _ ...grpc.CallOption) (*client.UserStatsResponse, error) { | ||
i.trackCall("UserStats") | ||
|
||
if err := i.enforceReadConsistency(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
i.Lock() | ||
defer i.Unlock() | ||
|
||
i.trackCall("UserStats") | ||
|
||
if !i.happy { | ||
return nil, errFail | ||
} | ||
|
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.
Note to reviewers: I moved calls to
i.trackCall()
as first thing and it takes the lock. See details here.