Skip to content

Commit

Permalink
Return Canceled when a LabelNames or LabelValues request to a store-g…
Browse files Browse the repository at this point in the history
…ateway is cancelled by the caller, and return Internal otherwise.

See #4007 for explanation.
  • Loading branch information
charleskorn committed Jan 25, 2023
1 parent 5b529d2 commit 3d8677f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
12 changes: 10 additions & 2 deletions pkg/storegateway/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ func (s *BucketStore) Series(req *storepb.SeriesRequest, srv storepb.Store_Serie
if err == nil {
return
}
code := codes.Aborted
code := codes.Internal
if st, ok := status.FromError(errors.Cause(err)); ok {
code = st.Code()
} else if errors.Is(err, context.Canceled) {
Expand Down Expand Up @@ -1269,6 +1269,10 @@ func (s *BucketStore) LabelNames(ctx context.Context, req *storepb.LabelNamesReq
s.blocksMx.RUnlock()

if err := g.Wait(); err != nil {
if errors.Is(err, context.Canceled) {
return nil, status.Error(codes.Canceled, err.Error())
}

return nil, status.Error(codes.Internal, err.Error())
}

Expand Down Expand Up @@ -1430,7 +1434,11 @@ func (s *BucketStore) LabelValues(ctx context.Context, req *storepb.LabelValuesR
s.blocksMx.RUnlock()

if err := g.Wait(); err != nil {
return nil, status.Error(codes.Aborted, err.Error())
if errors.Is(err, context.Canceled) {
return nil, status.Error(codes.Canceled, err.Error())
}

return nil, status.Error(codes.Internal, err.Error())
}

anyHints, err := types.MarshalAny(resHints)
Expand Down
53 changes: 53 additions & 0 deletions pkg/storegateway/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2298,6 +2298,59 @@ func TestLabelNamesAndValuesHints(t *testing.T) {
}
}

func TestLabelNames_Cancelled(t *testing.T) {
_, store, _, _, _, _, close := setupStoreForHintsTest(t)
defer close()

req := &storepb.LabelNamesRequest{
Start: 0,
End: 1,
Matchers: []storepb.LabelMatcher{
{
Name: "__name__",
Type: storepb.LabelMatcher_RE,
Value: ".*",
},
},
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err := store.LabelNames(ctx, req)
assert.Error(t, err)
s, ok := status.FromError(err)
assert.True(t, ok)
assert.Equal(t, codes.Canceled, s.Code())
}

func TestLabelValues_Cancelled(t *testing.T) {
_, store, _, _, _, _, close := setupStoreForHintsTest(t)
defer close()

req := &storepb.LabelValuesRequest{
Label: "ext1",
Start: 0,
End: 1,
Matchers: []storepb.LabelMatcher{
{
Name: "__name__",
Type: storepb.LabelMatcher_RE,
Value: ".*",
},
},
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err := store.LabelValues(ctx, req)
assert.Error(t, err)
s, ok := status.FromError(err)
assert.True(t, ok)
assert.Equal(t, codes.Canceled, s.Code())
}

func labelNamesFromSeriesSet(series []*storepb.Series) []string {
labelsMap := map[string]struct{}{}

Expand Down

0 comments on commit 3d8677f

Please sign in to comment.