From 8af66753479caa039989ed1e31ee783a6eb65f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Fri, 17 Jan 2020 10:02:37 +0200 Subject: [PATCH] frontend: implement cache control (#1974) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * querier: do not cache results if requested Add an extra `Headers` field to the `PrometheusResponse` message which contains the headers and their values that came from Prometheus. Use them in other places to deduce if the response should be cached. If `Cache-Control` is equal to `no-store` then it is *not* cached. This will be used by the Thanos project to indicate when a partial response has been returned. In such cases the result should not be cached so that invalid data would not be stored there. Signed-off-by: Giedrius Statkevičius * queryrange: factor out cache checking + add tests Signed-off-by: Giedrius Statkevičius * querier: gofmt Signed-off-by: Giedrius Statkevičius * querier: tests: add missing member Signed-off-by: Giedrius Statkevičius * querier: fix logical mistake Signed-off-by: Giedrius Statkevičius * queryrange: fix generated code Signed-off-by: Giedrius Statkevičius * querier: test adjustments Signed-off-by: Giedrius Statkevičius * querier: test adjustments Signed-off-by: Giedrius Statkevičius * querier: results_cache: cache by default Signed-off-by: Giedrius Statkevičius * queryrange: cache: check all header values `Cache-Control` might contain more than one value so check all of them. Signed-off-by: Giedrius Statkevičius * Update according to Goutham's comments Signed-off-by: Giedrius Statkevičius * CHANGELOG: add full stop, PR's number Signed-off-by: Giedrius Statkevičius --- CHANGELOG.md | 2 +- pkg/querier/queryrange/query_range.go | 7 + pkg/querier/queryrange/query_range_test.go | 4 +- pkg/querier/queryrange/queryrange.pb.go | 471 ++++++++++++++++--- pkg/querier/queryrange/queryrange.proto | 6 + pkg/querier/queryrange/results_cache.go | 35 ++ pkg/querier/queryrange/results_cache_test.go | 57 +++ 7 files changed, 523 insertions(+), 59 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e692b8c1aa..9661f7de01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ## master / unreleased +* [CHANGE] The frontend component now does not cache results if it finds a `Cache-Control` header and if one of its values is `no-store`. #1974 * [ENHANCEMENT] metric `cortex_ingester_flush_reasons` gets a new `reason` value: `Spread`, when `-ingester.spread-flushes` option is enabled. - * [CHANGE] Flags changed with transition to upstream Prometheus rules manager: * `ruler.client-timeout` is now `ruler.configs.client-timeout` in order to match `ruler.configs.url` * `ruler.group-timeout`has been removed diff --git a/pkg/querier/queryrange/query_range.go b/pkg/querier/queryrange/query_range.go index 2dfde812df..bac9d1d754 100644 --- a/pkg/querier/queryrange/query_range.go +++ b/pkg/querier/queryrange/query_range.go @@ -33,6 +33,9 @@ var ( // PrometheusCodec is a codec to encode and decode Prometheus query range requests and responses. PrometheusCodec Codec = &prometheusCodec{} + + // Name of the cache control header. + cachecontrolHeader = "Cache-Control" ) // Codec is used to encode/decode query range requests and responses so they can be passed down to middlewares. @@ -221,6 +224,10 @@ func (prometheusCodec) DecodeResponse(ctx context.Context, r *http.Response, _ R if err := json.Unmarshal(buf, &resp); err != nil { return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err) } + + for h, hv := range r.Header { + resp.Headers = append(resp.Headers, &PrometheusResponseHeader{Name: h, Values: hv}) + } return &resp, nil } diff --git a/pkg/querier/queryrange/query_range_test.go b/pkg/querier/queryrange/query_range_test.go index 543dd01a71..7bfd0c1ed9 100644 --- a/pkg/querier/queryrange/query_range_test.go +++ b/pkg/querier/queryrange/query_range_test.go @@ -74,13 +74,15 @@ func TestRequest(t *testing.T) { } func TestResponse(t *testing.T) { + r := *parsedResponse + r.Headers = respHeaders for i, tc := range []struct { body string expected *PrometheusResponse }{ { body: responseBody, - expected: parsedResponse, + expected: &r, }, } { t.Run(strconv.Itoa(i), func(t *testing.T) { diff --git a/pkg/querier/queryrange/queryrange.pb.go b/pkg/querier/queryrange/queryrange.pb.go index c06ab38905..b355ce69ba 100644 --- a/pkg/querier/queryrange/queryrange.pb.go +++ b/pkg/querier/queryrange/queryrange.pb.go @@ -114,17 +114,69 @@ func (m *PrometheusRequest) GetQuery() string { return "" } +type PrometheusResponseHeader struct { + Name string `protobuf:"bytes,1,opt,name=Name,json=name,proto3" json:"-"` + Values []string `protobuf:"bytes,2,rep,name=Values,json=values,proto3" json:"-"` +} + +func (m *PrometheusResponseHeader) Reset() { *m = PrometheusResponseHeader{} } +func (*PrometheusResponseHeader) ProtoMessage() {} +func (*PrometheusResponseHeader) Descriptor() ([]byte, []int) { + return fileDescriptor_79b02382e213d0b2, []int{1} +} +func (m *PrometheusResponseHeader) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PrometheusResponseHeader) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PrometheusResponseHeader.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PrometheusResponseHeader) XXX_Merge(src proto.Message) { + xxx_messageInfo_PrometheusResponseHeader.Merge(m, src) +} +func (m *PrometheusResponseHeader) XXX_Size() int { + return m.Size() +} +func (m *PrometheusResponseHeader) XXX_DiscardUnknown() { + xxx_messageInfo_PrometheusResponseHeader.DiscardUnknown(m) +} + +var xxx_messageInfo_PrometheusResponseHeader proto.InternalMessageInfo + +func (m *PrometheusResponseHeader) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +func (m *PrometheusResponseHeader) GetValues() []string { + if m != nil { + return m.Values + } + return nil +} + type PrometheusResponse struct { - Status string `protobuf:"bytes,1,opt,name=Status,json=status,proto3" json:"status"` - Data PrometheusData `protobuf:"bytes,2,opt,name=Data,json=data,proto3" json:"data,omitempty"` - ErrorType string `protobuf:"bytes,3,opt,name=ErrorType,json=errorType,proto3" json:"errorType,omitempty"` - Error string `protobuf:"bytes,4,opt,name=Error,json=error,proto3" json:"error,omitempty"` + Status string `protobuf:"bytes,1,opt,name=Status,json=status,proto3" json:"status"` + Data PrometheusData `protobuf:"bytes,2,opt,name=Data,json=data,proto3" json:"data,omitempty"` + ErrorType string `protobuf:"bytes,3,opt,name=ErrorType,json=errorType,proto3" json:"errorType,omitempty"` + Error string `protobuf:"bytes,4,opt,name=Error,json=error,proto3" json:"error,omitempty"` + Headers []*PrometheusResponseHeader `protobuf:"bytes,5,rep,name=Headers,json=headers,proto3" json:"-"` } func (m *PrometheusResponse) Reset() { *m = PrometheusResponse{} } func (*PrometheusResponse) ProtoMessage() {} func (*PrometheusResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_79b02382e213d0b2, []int{1} + return fileDescriptor_79b02382e213d0b2, []int{2} } func (m *PrometheusResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -181,6 +233,13 @@ func (m *PrometheusResponse) GetError() string { return "" } +func (m *PrometheusResponse) GetHeaders() []*PrometheusResponseHeader { + if m != nil { + return m.Headers + } + return nil +} + type PrometheusData struct { ResultType string `protobuf:"bytes,1,opt,name=ResultType,json=resultType,proto3" json:"resultType"` Result []SampleStream `protobuf:"bytes,2,rep,name=Result,json=result,proto3" json:"result"` @@ -189,7 +248,7 @@ type PrometheusData struct { func (m *PrometheusData) Reset() { *m = PrometheusData{} } func (*PrometheusData) ProtoMessage() {} func (*PrometheusData) Descriptor() ([]byte, []int) { - return fileDescriptor_79b02382e213d0b2, []int{2} + return fileDescriptor_79b02382e213d0b2, []int{3} } func (m *PrometheusData) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -240,7 +299,7 @@ type SampleStream struct { func (m *SampleStream) Reset() { *m = SampleStream{} } func (*SampleStream) ProtoMessage() {} func (*SampleStream) Descriptor() ([]byte, []int) { - return fileDescriptor_79b02382e213d0b2, []int{3} + return fileDescriptor_79b02382e213d0b2, []int{4} } func (m *SampleStream) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -285,7 +344,7 @@ type CachedResponse struct { func (m *CachedResponse) Reset() { *m = CachedResponse{} } func (*CachedResponse) ProtoMessage() {} func (*CachedResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_79b02382e213d0b2, []int{4} + return fileDescriptor_79b02382e213d0b2, []int{5} } func (m *CachedResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -338,7 +397,7 @@ type Extent struct { func (m *Extent) Reset() { *m = Extent{} } func (*Extent) ProtoMessage() {} func (*Extent) Descriptor() ([]byte, []int) { - return fileDescriptor_79b02382e213d0b2, []int{5} + return fileDescriptor_79b02382e213d0b2, []int{6} } func (m *Extent) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -397,6 +456,7 @@ func (m *Extent) GetResponse() *types.Any { func init() { proto.RegisterType((*PrometheusRequest)(nil), "queryrange.PrometheusRequest") + proto.RegisterType((*PrometheusResponseHeader)(nil), "queryrange.PrometheusResponseHeader") proto.RegisterType((*PrometheusResponse)(nil), "queryrange.PrometheusResponse") proto.RegisterType((*PrometheusData)(nil), "queryrange.PrometheusData") proto.RegisterType((*SampleStream)(nil), "queryrange.SampleStream") @@ -407,53 +467,57 @@ func init() { func init() { proto.RegisterFile("queryrange.proto", fileDescriptor_79b02382e213d0b2) } var fileDescriptor_79b02382e213d0b2 = []byte{ - // 730 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x54, 0xbd, 0x6e, 0xe3, 0x46, - 0x10, 0xd6, 0x5a, 0x12, 0x25, 0xad, 0x0d, 0xd9, 0x5e, 0x1b, 0x09, 0xe5, 0x82, 0x14, 0x54, 0x29, - 0x40, 0x4c, 0x01, 0x0e, 0x02, 0xa4, 0x49, 0x60, 0x33, 0x76, 0x80, 0x04, 0x29, 0x8c, 0x75, 0xaa, - 0x34, 0xc1, 0x4a, 0x9c, 0x50, 0xb4, 0xc5, 0x1f, 0x2f, 0x97, 0x81, 0x55, 0x04, 0x08, 0xf2, 0x04, - 0x57, 0xde, 0x23, 0x5c, 0x71, 0x0f, 0x70, 0x6f, 0x70, 0x2e, 0x5d, 0x1a, 0x57, 0xf0,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 0xa7, 0x26, 0x3a, 0xa4, 0x1d, 0x09, 0xff, 0xea, 0x90, 0x9f, 0x70, 0x97, 0x97, 0xed, 0x94, - 0xe7, 0xb5, 0xff, 0xd9, 0x79, 0x9d, 0x04, 0x4b, 0x7b, 0x2b, 0x4f, 0xcd, 0x15, 0x93, 0xae, 0xac, - 0xdf, 0x5a, 0xdd, 0xe6, 0x4e, 0xcb, 0x3e, 0xbe, 0x7b, 0x30, 0x1a, 0xf7, 0x0f, 0x46, 0xe3, 0xe9, - 0xc1, 0x40, 0xff, 0x65, 0x06, 0x7a, 0x91, 0x19, 0xe8, 0x36, 0x33, 0xd0, 0x5d, 0x66, 0xa0, 0x77, - 0x99, 0x81, 0xde, 0x67, 0x46, 0xe3, 0x29, 0x33, 0xd0, 0xb3, 0x47, 0xa3, 0x71, 0xf7, 0x68, 0x34, - 0xee, 0x1f, 0x8d, 0xc6, 0x9f, 0xb5, 0xdf, 0xbd, 0xa9, 0x26, 0xab, 0x7d, 0xf7, 0x21, 0x00, 0x00, - 0xff, 0xff, 0x29, 0x0e, 0x39, 0xd1, 0x1e, 0x05, 0x00, 0x00, + // 792 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x54, 0x4f, 0x8f, 0xdb, 0x44, + 0x14, 0xcf, 0xac, 0x1d, 0x67, 0x33, 0xa9, 0xd2, 0xed, 0xb4, 0x02, 0x67, 0x25, 0xec, 0xc8, 0xe2, + 0x10, 0x24, 0xea, 0x48, 0x41, 0x48, 0x5c, 0x40, 0x5b, 0xd3, 0x45, 0x80, 0x10, 0xaa, 0x66, 0x2b, + 0x0e, 0x5c, 0xd0, 0x24, 0x7e, 0x38, 0x6e, 0xe3, 0x3f, 0x1d, 0x8f, 0x51, 0x73, 0x40, 0x42, 0x7c, + 0x02, 0x8e, 0x7c, 0x04, 0x90, 0xf8, 0x00, 0x7c, 0x03, 0x7a, 0xdc, 0x63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xf3, + 0xa7, 0x39, 0x29, 0x78, 0x2a, 0x20, 0x15, 0xed, 0x18, 0xd2, 0x55, 0xee, 0x5c, 0x86, 0x82, 0x9b, + 0x7a, 0x54, 0x9b, 0x4a, 0x5b, 0xc3, 0xfb, 0x0d, 0x61, 0x4b, 0x25, 0x11, 0xb7, 0x3d, 0xec, 0x66, + 0x8c, 0x11, 0x0c, 0xeb, 0xca, 0x55, 0x40, 0x7b, 0xe3, 0x13, 0x75, 0xe3, 0xf2, 0xee, 0x15, 0x0b, + 0x48, 0x43, 0x75, 0xec, 0x53, 0x7c, 0x2c, 0x38, 0x5b, 0xc1, 0x37, 0x71, 0xa8, 0x9f, 0x5d, 0xfb, + 0x46, 0x24, 0xfc, 0x59, 0x48, 0x3e, 0xc2, 0xc7, 0x5c, 0xaf, 0xa3, 0x6f, 0xff, 0xce, 0x2b, 0xb7, + 0x7f, 0x2f, 0xdd, 0x06, 0x37, 0xea, 0xca, 0xdd, 0x67, 0xd2, 0xbd, 0xf5, 0xb9, 0x79, 0x6c, 0x9c, + 0x98, 0xc1, 0xd9, 0xe5, 0x95, 0xd3, 0x7b, 0x7e, 0xe5, 0xf4, 0x5e, 0x5c, 0x39, 0xe8, 0x87, 0x9d, + 0x83, 0x7e, 0xd9, 0x39, 0xe8, 0xd9, 0xce, 0x41, 0x97, 0x3b, 0x07, 0xfd, 0xbd, 0x73, 0xd0, 0x3f, + 0x3b, 0xa7, 0xf7, 0x62, 0xe7, 0xa0, 0x9f, 0xae, 0x9d, 0xde, 0xe5, 0xb5, 0xd3, 0x7b, 0x7e, 0xed, + 0xf4, 0xbe, 0xee, 0xfc, 0x53, 0x5e, 0x5a, 0x72, 0xda, 0x7b, 0xff, 0x06, 0x00, 0x00, 0xff, 0xff, + 0xa8, 0x2f, 0xc9, 0x09, 0xbb, 0x05, 0x00, 0x00, } func (this *PrometheusRequest) Equal(that interface{}) bool { @@ -495,6 +559,38 @@ func (this *PrometheusRequest) Equal(that interface{}) bool { } return true } +func (this *PrometheusResponseHeader) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*PrometheusResponseHeader) + if !ok { + that2, ok := that.(PrometheusResponseHeader) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.Name != that1.Name { + return false + } + if len(this.Values) != len(that1.Values) { + return false + } + for i := range this.Values { + if this.Values[i] != that1.Values[i] { + return false + } + } + return true +} func (this *PrometheusResponse) Equal(that interface{}) bool { if that == nil { return this == nil @@ -526,6 +622,14 @@ func (this *PrometheusResponse) Equal(that interface{}) bool { if this.Error != that1.Error { return false } + if len(this.Headers) != len(that1.Headers) { + return false + } + for i := range this.Headers { + if !this.Headers[i].Equal(that1.Headers[i]) { + return false + } + } return true } func (this *PrometheusData) Equal(that interface{}) bool { @@ -677,16 +781,30 @@ func (this *PrometheusRequest) GoString() string { s = append(s, "}") return strings.Join(s, "") } +func (this *PrometheusResponseHeader) GoString() string { + if this == nil { + return "nil" + } + s := make([]string, 0, 6) + s = append(s, "&queryrange.PrometheusResponseHeader{") + s = append(s, "Name: "+fmt.Sprintf("%#v", this.Name)+",\n") + s = append(s, "Values: "+fmt.Sprintf("%#v", this.Values)+",\n") + s = append(s, "}") + return strings.Join(s, "") +} func (this *PrometheusResponse) GoString() string { if this == nil { return "nil" } - s := make([]string, 0, 8) + s := make([]string, 0, 9) s = append(s, "&queryrange.PrometheusResponse{") s = append(s, "Status: "+fmt.Sprintf("%#v", this.Status)+",\n") s = append(s, "Data: "+strings.Replace(this.Data.GoString(), `&`, ``, 1)+",\n") s = append(s, "ErrorType: "+fmt.Sprintf("%#v", this.ErrorType)+",\n") s = append(s, "Error: "+fmt.Sprintf("%#v", this.Error)+",\n") + if this.Headers != nil { + s = append(s, "Headers: "+fmt.Sprintf("%#v", this.Headers)+",\n") + } s = append(s, "}") return strings.Join(s, "") } @@ -817,6 +935,45 @@ func (m *PrometheusRequest) MarshalTo(dAtA []byte) (int, error) { return i, nil } +func (m *PrometheusResponseHeader) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PrometheusResponseHeader) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if len(m.Name) > 0 { + dAtA[i] = 0xa + i++ + i = encodeVarintQueryrange(dAtA, i, uint64(len(m.Name))) + i += copy(dAtA[i:], m.Name) + } + if len(m.Values) > 0 { + for _, s := range m.Values { + dAtA[i] = 0x12 + i++ + l = len(s) + for l >= 1<<7 { + dAtA[i] = uint8(uint64(l)&0x7f | 0x80) + l >>= 7 + i++ + } + dAtA[i] = uint8(l) + i++ + i += copy(dAtA[i:], s) + } + } + return i, nil +} + func (m *PrometheusResponse) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -858,6 +1015,18 @@ func (m *PrometheusResponse) MarshalTo(dAtA []byte) (int, error) { i = encodeVarintQueryrange(dAtA, i, uint64(len(m.Error))) i += copy(dAtA[i:], m.Error) } + if len(m.Headers) > 0 { + for _, msg := range m.Headers { + dAtA[i] = 0x2a + i++ + i = encodeVarintQueryrange(dAtA, i, uint64(msg.Size())) + n, err := msg.MarshalTo(dAtA[i:]) + if err != nil { + return 0, err + } + i += n + } + } return i, nil } @@ -1056,6 +1225,25 @@ func (m *PrometheusRequest) Size() (n int) { return n } +func (m *PrometheusResponseHeader) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Name) + if l > 0 { + n += 1 + l + sovQueryrange(uint64(l)) + } + if len(m.Values) > 0 { + for _, s := range m.Values { + l = len(s) + n += 1 + l + sovQueryrange(uint64(l)) + } + } + return n +} + func (m *PrometheusResponse) Size() (n int) { if m == nil { return 0 @@ -1076,6 +1264,12 @@ func (m *PrometheusResponse) Size() (n int) { if l > 0 { n += 1 + l + sovQueryrange(uint64(l)) } + if len(m.Headers) > 0 { + for _, e := range m.Headers { + l = e.Size() + n += 1 + l + sovQueryrange(uint64(l)) + } + } return n } @@ -1189,6 +1383,17 @@ func (this *PrometheusRequest) String() string { }, "") return s } +func (this *PrometheusResponseHeader) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&PrometheusResponseHeader{`, + `Name:` + fmt.Sprintf("%v", this.Name) + `,`, + `Values:` + fmt.Sprintf("%v", this.Values) + `,`, + `}`, + }, "") + return s +} func (this *PrometheusResponse) String() string { if this == nil { return "nil" @@ -1198,6 +1403,7 @@ func (this *PrometheusResponse) String() string { `Data:` + strings.Replace(strings.Replace(this.Data.String(), "PrometheusData", "PrometheusData", 1), `&`, ``, 1) + `,`, `ErrorType:` + fmt.Sprintf("%v", this.ErrorType) + `,`, `Error:` + fmt.Sprintf("%v", this.Error) + `,`, + `Headers:` + strings.Replace(fmt.Sprintf("%v", this.Headers), "PrometheusResponseHeader", "PrometheusResponseHeader", 1) + `,`, `}`, }, "") return s @@ -1463,6 +1669,123 @@ func (m *PrometheusRequest) Unmarshal(dAtA []byte) error { } return nil } +func (m *PrometheusResponseHeader) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PrometheusResponseHeader: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PrometheusResponseHeader: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQueryrange + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQueryrange + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Name = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Values", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQueryrange + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQueryrange + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Values = append(m.Values, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQueryrange(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthQueryrange + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthQueryrange + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *PrometheusResponse) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 @@ -1621,6 +1944,40 @@ func (m *PrometheusResponse) Unmarshal(dAtA []byte) error { } m.Error = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Headers", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQueryrange + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQueryrange + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Headers = append(m.Headers, &PrometheusResponseHeader{}) + if err := m.Headers[len(m.Headers)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipQueryrange(dAtA[iNdEx:]) diff --git a/pkg/querier/queryrange/queryrange.proto b/pkg/querier/queryrange/queryrange.proto index 9761905265..e6b8dc8ff7 100644 --- a/pkg/querier/queryrange/queryrange.proto +++ b/pkg/querier/queryrange/queryrange.proto @@ -21,11 +21,17 @@ message PrometheusRequest { string query = 6; } +message PrometheusResponseHeader { + string Name = 1 [(gogoproto.jsontag) = "-"]; + repeated string Values = 2 [(gogoproto.jsontag) = "-"]; +} + message PrometheusResponse { string Status = 1 [(gogoproto.jsontag) = "status"]; PrometheusData Data = 2 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "data,omitempty"]; string ErrorType = 3 [(gogoproto.jsontag) = "errorType,omitempty"]; string Error = 4 [(gogoproto.jsontag) = "error,omitempty"]; + repeated PrometheusResponseHeader Headers = 5 [(gogoproto.jsontag) = "-"]; } message PrometheusData { diff --git a/pkg/querier/queryrange/results_cache.go b/pkg/querier/queryrange/results_cache.go index dde104fcdb..802cb32fde 100644 --- a/pkg/querier/queryrange/results_cache.go +++ b/pkg/querier/queryrange/results_cache.go @@ -22,6 +22,11 @@ import ( "github.com/cortexproject/cortex/pkg/util/spanlogger" ) +var ( + // Value that cachecontrolHeader has if the response indicates that the results should not be cached. + noCacheValue = "no-store" +) + // ResultsCacheConfig is the config for the results cache. type ResultsCacheConfig struct { CacheConfig cache.Config `yaml:"cache"` @@ -59,6 +64,7 @@ var PrometheusResponseExtractor = ExtractorFunc(func(start, end int64, from Resp ResultType: promRes.Data.ResultType, Result: extractMatrix(start, end, promRes.Data.Result), }, + Headers: promRes.Headers, } }) @@ -133,12 +139,41 @@ func (s resultsCache) Do(ctx context.Context, r Request) (Response, error) { return response, err } +// shouldCacheResponse says whether the response should be cached or not. +func shouldCacheResponse(r Response) bool { + if promResp, ok := r.(*PrometheusResponse); ok { + shouldCache := true + outer: + for _, hv := range promResp.Headers { + if hv == nil { + continue + } + if hv.Name != cachecontrolHeader { + continue + } + for _, v := range hv.Values { + if v == noCacheValue { + shouldCache = false + break outer + } + } + } + return shouldCache + } + return true +} + func (s resultsCache) handleMiss(ctx context.Context, r Request) (Response, []Extent, error) { response, err := s.next.Do(ctx, r) if err != nil { return nil, nil, err } + if !shouldCacheResponse(response) { + level.Debug(s.logger).Log("msg", fmt.Sprintf("%s header in response is equal to %s, not caching the response", cachecontrolHeader, noCacheValue)) + return response, []Extent{}, nil + } + extent, err := toExtent(ctx, r, response) if err != nil { return nil, nil, err diff --git a/pkg/querier/queryrange/results_cache_test.go b/pkg/querier/queryrange/results_cache_test.go index cf8c5c41e9..6eb0c3266d 100644 --- a/pkg/querier/queryrange/results_cache_test.go +++ b/pkg/querier/queryrange/results_cache_test.go @@ -31,6 +31,12 @@ var ( Step: 120 * 1e3, Query: "sum(container_memory_rss) by (namespace)", } + respHeaders = []*PrometheusResponseHeader{ + { + Name: "Content-Type", + Values: []string{"application/json"}, + }, + } parsedResponse = &PrometheusResponse{ Status: "success", Data: PrometheusData{ @@ -108,6 +114,57 @@ func mkExtent(start, end int64) Extent { } } +func TestShouldCache(t *testing.T) { + for i, tc := range []struct { + input Response + expected bool + }{ + // Does not contain the needed header. + { + input: Response(&PrometheusResponse{ + Headers: []*PrometheusResponseHeader{ + { + Name: "meaninglessheader", + Values: []string{}, + }, + }, + }), + expected: true, + }, + // Does contain the header which has the value. + { + input: Response(&PrometheusResponse{ + Headers: []*PrometheusResponseHeader{ + { + Name: cachecontrolHeader, + Values: []string{noCacheValue}, + }, + }, + }), + expected: false, + }, + // Header contains extra values but still good. + { + input: Response(&PrometheusResponse{ + Headers: []*PrometheusResponseHeader{ + { + Name: cachecontrolHeader, + Values: []string{"foo", noCacheValue}, + }, + }, + }), + expected: false, + }, + } { + { + t.Run(strconv.Itoa(i), func(t *testing.T) { + ret := shouldCacheResponse(tc.input) + require.Equal(t, tc.expected, ret) + }) + } + } +} + func TestPartiton(t *testing.T) { for i, tc := range []struct { input Request