Skip to content

Commit ba6b156

Browse files
authored
Always request querier to return gzipped response (#4960)
* Always request querier to return gzipped response Signed-off-by: Alan Protasio <[email protected]> * make lint happy Signed-off-by: Alan Protasio <[email protected]> Signed-off-by: Alan Protasio <[email protected]>
1 parent 0f1dd3f commit ba6b156

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
* [BUGFIX] AlertManager: fixed issue introduced by #4495 where templates files were being deleted when using alertmanager local store. #4890
7676
* [BUGFIX] Ingester: fixed incorrect logging at the start of ingester block shipping logic. #4934
7777
* [BUGFIX] Storage/Bucket: fixed global mark missing on deletion. #4949
78+
* [BUGFIX] QueryFrontend/Querier: fixed regression added by #4863 where we stopped compressing the response between querier and query frontend. #4960
7879

7980
## 1.13.0 2022-07-14
8081

pkg/querier/tripperware/instantquery/instant_query.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ func (instantQueryCodec) EncodeRequest(ctx context.Context, r tripperware.Reques
203203
}
204204
}
205205

206+
// Always ask gzip to the querier
207+
h.Set("Accept-Encoding", "gzip")
208+
206209
req := &http.Request{
207210
Method: "GET",
208211
RequestURI: u.String(), // This is what the httpgrpc code looks at.

pkg/querier/tripperware/query.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package tripperware
22

33
import (
44
"bytes"
5+
"compress/gzip"
56
"context"
67
"encoding/json"
8+
"io"
79
"net/http"
810
"sort"
911
"strconv"
12+
"strings"
1013
"time"
1114
"unsafe"
1215

@@ -146,19 +149,34 @@ type Buffer interface {
146149
}
147150

148151
func BodyBuffer(res *http.Response) ([]byte, error) {
152+
var buf *bytes.Buffer
153+
149154
// Attempt to cast the response body to a Buffer and use it if possible.
150155
// This is because the frontend may have already read the body and buffered it.
151156
if buffer, ok := res.Body.(Buffer); ok {
152-
return buffer.Bytes(), nil
157+
buf = bytes.NewBuffer(buffer.Bytes())
158+
} else {
159+
// Preallocate the buffer with the exact size so we don't waste allocations
160+
// while progressively growing an initial small buffer. The buffer capacity
161+
// is increased by MinRead to avoid extra allocations due to how ReadFrom()
162+
// internally works.
163+
buf = bytes.NewBuffer(make([]byte, 0, res.ContentLength+bytes.MinRead))
164+
if _, err := buf.ReadFrom(res.Body); err != nil {
165+
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err)
166+
}
153167
}
154-
// Preallocate the buffer with the exact size so we don't waste allocations
155-
// while progressively growing an initial small buffer. The buffer capacity
156-
// is increased by MinRead to avoid extra allocations due to how ReadFrom()
157-
// internally works.
158-
buf := bytes.NewBuffer(make([]byte, 0, res.ContentLength+bytes.MinRead))
159-
if _, err := buf.ReadFrom(res.Body); err != nil {
160-
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err)
168+
169+
// if the response is gziped, lets unzip it here
170+
if strings.EqualFold(res.Header.Get("Content-Encoding"), "gzip") {
171+
gReader, err := gzip.NewReader(buf)
172+
173+
if err != nil {
174+
return nil, err
175+
}
176+
177+
return io.ReadAll(gReader)
161178
}
179+
162180
return buf.Bytes(), nil
163181
}
164182

pkg/querier/tripperware/queryrange/query_range.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ func (prometheusCodec) EncodeRequest(ctx context.Context, r tripperware.Request)
243243
}
244244
}
245245

246+
// Always ask gzip to the querier
247+
h.Set("Accept-Encoding", "gzip")
248+
246249
req := &http.Request{
247250
Method: "GET",
248251
RequestURI: u.String(), // This is what the httpgrpc code looks at.

0 commit comments

Comments
 (0)