Skip to content
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

Query: Fix panic on stores endpoint #4754

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re

## Unreleased

### Fixed

- [#4663](https://github.com/thanos-io/thanos/pull/4663) Fetcher: Fix discovered data races

### Added

- [#4680](https://github.com/thanos-io/thanos/pull/4680) Query: add `exemplar.partial-response` flag to control partial response.
Expand All @@ -23,6 +19,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
### Fixed

- [#4508](https://github.com/thanos-io/thanos/pull/4508) Adjust and rename `ThanosSidecarUnhealthy` to `ThanosSidecarNoConnectionToStartedPrometheus`; Remove `ThanosSidecarPrometheusDown` alert; Remove unused `thanos_sidecar_last_heartbeat_success_time_seconds` metrics.
- [#4663](https://github.com/thanos-io/thanos/pull/4663) Fetcher: Fix discovered data races.
- [#4754](https://github.com/thanos-io/thanos/pull/4754) Query: Fix possible panic on stores endpoint.

## [v0.23.1](https://github.com/thanos-io/thanos/tree/release-0.23) - 2021.10.1

Expand Down
2 changes: 1 addition & 1 deletion cmd/thanos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func runQuery(

api := v1.NewQueryAPI(
logger,
endpoints,
endpoints.GetEndpointStatus,
engineFactory(promql.NewEngine, engineOpts, dynamicLookbackDelta),
queryableCreator,
// NOTE: Will share the same replica label as the query for now.
Expand Down
14 changes: 9 additions & 5 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ type QueryAPI struct {
enableExemplarPartialResponse bool
disableCORS bool

replicaLabels []string
endpointSet *query.EndpointSet
replicaLabels []string
endpointStatus func() []query.EndpointStatus

defaultRangeQueryStep time.Duration
defaultInstantQueryMaxSourceResolution time.Duration
Expand All @@ -106,7 +106,7 @@ type QueryAPI struct {
// NewQueryAPI returns an initialized QueryAPI type.
func NewQueryAPI(
logger log.Logger,
endpointSet *query.EndpointSet,
endpointStatus func() []query.EndpointStatus,
qe func(int64) *promql.Engine,
c query.QueryableCreator,
ruleGroups rules.UnaryClient,
Expand Down Expand Up @@ -146,7 +146,7 @@ func NewQueryAPI(
enableMetricMetadataPartialResponse: enableMetricMetadataPartialResponse,
enableExemplarPartialResponse: enableExemplarPartialResponse,
replicaLabels: replicaLabels,
endpointSet: endpointSet,
endpointStatus: endpointStatus,
defaultRangeQueryStep: defaultRangeQueryStep,
defaultInstantQueryMaxSourceResolution: defaultInstantQueryMaxSourceResolution,
defaultMetadataTimeRange: defaultMetadataTimeRange,
Expand Down Expand Up @@ -715,7 +715,11 @@ func (qapi *QueryAPI) labelNames(r *http.Request) (interface{}, []error, *api.Ap

func (qapi *QueryAPI) stores(_ *http.Request) (interface{}, []error, *api.ApiError) {
statuses := make(map[string][]query.EndpointStatus)
for _, status := range qapi.endpointSet.GetEndpointStatus() {
for _, status := range qapi.endpointStatus() {
// Don't consider an endpoint if we cannot retrieve component type.
if status.ComponentType == nil {
continue
}
statuses[status.ComponentType.String()] = append(statuses[status.ComponentType.String()], status)
}
return statuses, nil, nil
Expand Down
87 changes: 87 additions & 0 deletions pkg/api/query/v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,93 @@ func TestMetadataEndpoints(t *testing.T) {
}
}

func TestStoresEndpoint(t *testing.T) {
apiWithNotEndpoints := &QueryAPI{
endpointStatus: func() []query.EndpointStatus {
return []query.EndpointStatus{}
},
}
apiWithValidEndpoints := &QueryAPI{
endpointStatus: func() []query.EndpointStatus {
return []query.EndpointStatus{
{
Name: "endpoint-1",
ComponentType: component.Store,
},
{
Name: "endpoint-2",
ComponentType: component.Store,
},
{
Name: "endpoint-3",
ComponentType: component.Sidecar,
},
}
},
}
apiWithInvalidEndpoint := &QueryAPI{
endpointStatus: func() []query.EndpointStatus {
return []query.EndpointStatus{
{
Name: "endpoint-1",
ComponentType: component.Store,
},
{
Name: "endpoint-2",
},
}
},
}

testCases := []endpointTestCase{
{
endpoint: apiWithNotEndpoints.stores,
method: http.MethodGet,
response: map[string][]query.EndpointStatus{},
},
{
endpoint: apiWithValidEndpoints.stores,
method: http.MethodGet,
response: map[string][]query.EndpointStatus{
"store": {
{
Name: "endpoint-1",
ComponentType: component.Store,
},
{
Name: "endpoint-2",
ComponentType: component.Store,
},
},
"sidecar": {
{
Name: "endpoint-3",
ComponentType: component.Sidecar,
},
},
},
},
{
endpoint: apiWithInvalidEndpoint.stores,
method: http.MethodGet,
response: map[string][]query.EndpointStatus{
"store": {
{
Name: "endpoint-1",
ComponentType: component.Store,
},
},
},
},
}

for i, test := range testCases {
if ok := testEndpoint(t, test, strings.TrimSpace(fmt.Sprintf("#%d %s", i, test.query.Encode())), reflect.DeepEqual); !ok {
return
}
}
}

func TestParseTime(t *testing.T) {
ts, err := time.Parse(time.RFC3339Nano, "2015-06-03T13:21:58.555Z")
if err != nil {
Expand Down