-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapshotbyidsharding.go
318 lines (274 loc) · 8.88 KB
/
snapshotbyidsharding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (C) 2023 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package frontend
import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/intergral/deep/pkg/model/snapshot"
"github.com/intergral/deep/pkg/util"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/protobuf/proto" //nolint:all //deprecated
"github.com/intergral/deep/modules/querier"
"github.com/intergral/deep/pkg/api"
"github.com/intergral/deep/pkg/deeppb"
"github.com/opentracing/opentracing-go"
)
const (
minQueryShards = 2
maxQueryShards = 256
)
func newSnapshotByIDSharder(queryShards, maxFailedBlocks int, sloCfg SLOConfig, logger log.Logger) Middleware {
return MiddlewareFunc(func(next http.RoundTripper) http.RoundTripper {
return shardQuery{
next: next,
queryShards: queryShards,
sloCfg: sloCfg,
logger: logger,
blockBoundaries: createBlockBoundaries(queryShards - 1), // one shard will be used to query ingesters
maxFailedBlocks: uint32(maxFailedBlocks),
}
})
}
type shardQuery struct {
next http.RoundTripper
queryShards int
sloCfg SLOConfig
logger log.Logger
blockBoundaries [][]byte
maxFailedBlocks uint32
}
// RoundTrip implements http.RoundTripper
func (s shardQuery) RoundTrip(r *http.Request) (*http.Response, error) {
ctx := r.Context()
span, ctx := opentracing.StartSpanFromContext(ctx, "frontend.ShardQuery")
defer span.Finish()
tenantID, err := util.ExtractTenantID(ctx)
if err != nil {
return &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(strings.NewReader(err.Error())),
}, nil
}
span.SetTag("tenantID", tenantID)
reqStart := time.Now()
// context propagation
r = r.WithContext(ctx)
reqs, err := s.buildShardedRequests(r)
if err != nil {
return nil, err
}
// execute requests
wg := sync.WaitGroup{}
mtx := sync.Mutex{}
var overallError error
var totalFailedBlocks uint32
handler := snapshot.NewResultHandler()
statusCode := http.StatusNotFound
statusMsg := "snapshot not found"
// todo: We want the first result from below, i think there is a better way to do this
for _, req := range reqs {
wg.Add(1)
go func(innerR *http.Request) {
defer wg.Done()
if handler.IsComplete() {
return
}
resp, err := s.next.RoundTrip(innerR)
mtx.Lock()
defer mtx.Unlock()
if err != nil {
overallError = err
}
if handler.IsComplete() || shouldQuit(r.Context(), statusCode, overallError) {
return
}
// check http error
if err != nil {
_ = level.Error(s.logger).Log("msg", "error querying proxy target", "url", innerR.RequestURI, "err", err)
overallError = err
return
}
// if the status code is anything but happy, save the error and pass it down the line
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound {
// todo: if we cancel the parent context here will it shortcircuit the other queries and fail fast?
statusCode = resp.StatusCode
bytesMsg, err := io.ReadAll(resp.Body)
if err != nil {
_ = level.Error(s.logger).Log("msg", "error reading response body status != ok", "url", innerR.RequestURI, "err", err)
}
statusMsg = string(bytesMsg)
return
}
// read the body
buff, err := io.ReadAll(resp.Body)
if err != nil {
_ = level.Error(s.logger).Log("msg", "error reading response body status == ok", "url", innerR.RequestURI, "err", err)
overallError = err
return
}
snapshotResponse := &deeppb.SnapshotByIDResponse{}
err = proto.Unmarshal(buff, snapshotResponse)
if err != nil {
_ = level.Error(s.logger).Log("msg", "error unmarshalling response", "url", innerR.RequestURI, "err", err, "body", string(buff))
overallError = err
return
}
if snapshotResponse.Metrics != nil {
totalFailedBlocks += snapshotResponse.Metrics.FailedBlocks
if totalFailedBlocks > s.maxFailedBlocks {
overallError = fmt.Errorf("too many failed block queries %d (max %d)", totalFailedBlocks, s.maxFailedBlocks)
return
}
}
// if not found bail
if resp.StatusCode == http.StatusNotFound {
return
}
// happy path
statusCode = http.StatusOK
err = handler.Complete(snapshotResponse.Snapshot)
if err != nil {
overallError = fmt.Errorf("too many results found")
return
}
}(req)
}
wg.Wait()
reqTime := time.Since(reqStart)
foundSnapshot := handler.Result()
if overallError != nil && foundSnapshot == nil {
return nil, overallError
}
if totalFailedBlocks > 0 {
_ = level.Warn(s.logger).Log("msg", "some blocks failed. returning success due to tolerate_failed_blocks", "failed", totalFailedBlocks, "tolerate_failed_blocks", s.maxFailedBlocks)
}
if foundSnapshot == nil || statusCode != http.StatusOK {
// translate non-404s into 500s. if, for instance, we get a 400 back from an internal component
// it means that we created a bad request. 400 should not be propagated back to the user b/c
// the bad request was due to a bug on our side, so return 500 instead.
if statusCode != http.StatusNotFound {
statusCode = 500
}
return &http.Response{
StatusCode: statusCode,
Body: io.NopCloser(strings.NewReader(statusMsg)),
Header: http.Header{},
}, nil
}
if foundSnapshot == nil {
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(strings.NewReader("snapshot not found")),
Header: http.Header{},
}, nil
}
buff, err := proto.Marshal(&deeppb.SnapshotByIDResponse{
Snapshot: foundSnapshot,
Metrics: &deeppb.SnapshotByIDMetrics{
FailedBlocks: totalFailedBlocks,
},
})
if err != nil {
_ = level.Error(s.logger).Log("msg", "error marshalling response to proto", "err", err)
return nil, err
}
// only record metric when it's enabled and within slo
if s.sloCfg.DurationSLO != 0 {
if reqTime < s.sloCfg.DurationSLO {
// we are within SLO if query returned 200 within DurationSLO seconds
// TODO: we don't have throughput metrics for SnapshotByID.
sloSnapshotByIDCounter.WithLabelValues(tenantID).Inc()
}
}
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
api.HeaderContentType: {api.HeaderAcceptProtobuf},
},
Body: io.NopCloser(bytes.NewReader(buff)),
ContentLength: int64(len(buff)),
}, nil
}
// buildShardedRequests returns a slice of requests sharded on the precalculated
// block boundaries
func (s *shardQuery) buildShardedRequests(parent *http.Request) ([]*http.Request, error) {
ctx := parent.Context()
tenantID, err := util.ExtractTenantID(ctx)
if err != nil {
return nil, err
}
reqs := make([]*http.Request, s.queryShards)
// build sharded block queries
for i := 0; i < len(s.blockBoundaries); i++ {
reqs[i] = parent.Clone(ctx)
q := reqs[i].URL.Query()
if i == 0 {
// ingester query
q.Add(querier.QueryModeKey, querier.QueryModeIngesters)
} else {
// block queries
q.Add(querier.BlockStartKey, hex.EncodeToString(s.blockBoundaries[i-1]))
q.Add(querier.BlockEndKey, hex.EncodeToString(s.blockBoundaries[i]))
q.Add(querier.QueryModeKey, querier.QueryModeBlocks)
}
reqs[i].Header.Set(util.TenantIDHeaderName, tenantID)
uri := buildUpstreamRequestURI(api.PathPrefixQuerier, reqs[i].URL.Path, q)
reqs[i].RequestURI = uri
}
return reqs, nil
}
// createBlockBoundaries splits the range of blockIDs into queryShards parts
func createBlockBoundaries(queryShards int) [][]byte {
if queryShards == 0 {
return nil
}
// create sharded queries
blockBoundaries := make([][]byte, queryShards+1)
for i := 0; i < queryShards+1; i++ {
blockBoundaries[i] = make([]byte, 16)
}
const MaxUint = uint64(^uint8(0))
for i := 0; i < queryShards; i++ {
binary.LittleEndian.PutUint64(blockBoundaries[i][:8], (MaxUint/uint64(queryShards))*uint64(i))
binary.LittleEndian.PutUint64(blockBoundaries[i][8:], 0)
}
const MaxUint64 = ^uint64(0)
binary.LittleEndian.PutUint64(blockBoundaries[queryShards][:8], MaxUint64)
binary.LittleEndian.PutUint64(blockBoundaries[queryShards][8:], MaxUint64)
return blockBoundaries
}
func shouldQuit(ctx context.Context, statusCode int, err error) bool {
if err != nil {
return true
}
if ctx.Err() != nil {
return true
}
if statusCode/100 == 5 { // bail on any 5xx's
return true
}
return false
}