Skip to content

Commit 74055d8

Browse files
authored
Fixes a panic in the query-tee. (#4465)
* Fixes a panic in the query-tee. This was happening because of a race. I've change the test suite to cover the code and the race detector shows it up. The panic: ``` panic: runtime error: index out of range [1] with length 1 goroutine 26118 [running]: github.com/cortexproject/cortex/tools/querytee.(*ProxyEndpoint).executeBackendRequests(0xc0003160c0, 0xc0002c2600, 0xc0004c44e0) /__w/cortex/cortex/tools/querytee/proxy_endpoint.go:122 +0x5a5 created by github.com/cortexproject/cortex/tools/querytee.(*ProxyEndpoint).ServeHTTP /__w/cortex/cortex/tools/querytee/proxy_endpoint.go:57 +0x25b ``` Signed-off-by: Cyril Tovena <[email protected]> * update changelog. Signed-off-by: Cyril Tovena <[email protected]>
1 parent c00eaf1 commit 74055d8

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* [ENHANCEMENT] Updated Prometheus to include changes from prometheus/prometheus#9083. Now whenever `/labels` API calls include matchers, blocks store is queried for `LabelNames` with matchers instead of `Series` calls which was inefficient. #4380
4040
* [ENHANCEMENT] Exemplars are now emitted for all gRPC calls and many operations tracked by histograms. #4462
4141
* [ENHANCEMENT] New options `-server.http-listen-network` and `-server.grpc-listen-network` allow binding as 'tcp4' or 'tcp6'. #4462
42+
* [BUGFIX] Fixes a panic in the query-tee when comparing result. #4465
4243
* [BUGFIX] Frontend: Fixes @ modifier functions (start/end) when splitting queries by time. #4464
4344
* [BUGFIX] Compactor: compactor will no longer try to compact blocks that are already marked for deletion. Previously compactor would consider blocks marked for deletion within `-compactor.deletion-delay / 2` period as eligible for compaction. #4328
4445
* [BUGFIX] HA Tracker: when cleaning up obsolete elected replicas from KV store, tracker didn't update number of cluster per user correctly. #4336

tools/querytee/proxy_endpoint.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ func (p *ProxyEndpoint) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7474
func (p *ProxyEndpoint) executeBackendRequests(r *http.Request, resCh chan *backendResponse) {
7575
responses := make([]*backendResponse, 0, len(p.backends))
7676

77-
wg := sync.WaitGroup{}
77+
var (
78+
wg = sync.WaitGroup{}
79+
mtx = sync.Mutex{}
80+
)
7881
wg.Add(len(p.backends))
7982

8083
for _, b := range p.backends {
@@ -105,7 +108,9 @@ func (p *ProxyEndpoint) executeBackendRequests(r *http.Request, resCh chan *back
105108

106109
// Keep track of the response if required.
107110
if p.comparator != nil {
111+
mtx.Lock()
108112
responses = append(responses, res)
113+
mtx.Unlock()
109114
}
110115

111116
resCh <- res

tools/querytee/proxy_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ import (
1717
)
1818

1919
var testRoutes = []Route{
20-
{Path: "/api/v1/query", RouteName: "api_v1_query", Methods: []string{"GET"}, ResponseComparator: nil},
20+
{Path: "/api/v1/query", RouteName: "api_v1_query", Methods: []string{"GET"}, ResponseComparator: &testComparator{}},
2121
}
2222

23+
type testComparator struct{}
24+
25+
func (testComparator) Compare(expected, actual []byte) error { return nil }
26+
2327
func Test_NewProxy(t *testing.T) {
2428
cfg := ProxyConfig{}
2529

@@ -156,6 +160,10 @@ func Test_Proxy_RequestsForwarding(t *testing.T) {
156160
BackendReadTimeout: time.Second,
157161
}
158162

163+
if len(backendURLs) == 2 {
164+
cfg.CompareResponses = true
165+
}
166+
159167
p, err := NewProxy(cfg, log.NewNopLogger(), testRoutes, nil)
160168
require.NoError(t, err)
161169
require.NotNil(t, p)

0 commit comments

Comments
 (0)