-
Notifications
You must be signed in to change notification settings - Fork 489
/
tracebyidsharding.go
255 lines (220 loc) · 7 KB
/
tracebyidsharding.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
package frontend
import (
"bytes"
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"
"sync"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/protobuf/proto"
"github.com/grafana/tempo/modules/querier"
"github.com/grafana/tempo/pkg/model/trace"
"github.com/grafana/tempo/pkg/tempopb"
"github.com/opentracing/opentracing-go"
"github.com/weaveworks/common/user"
)
const (
minQueryShards = 2
maxQueryShards = 256
)
func newTraceByIDSharder(queryShards, maxFailedBlocks int, logger log.Logger) Middleware {
return MiddlewareFunc(func(next http.RoundTripper) http.RoundTripper {
return shardQuery{
next: next,
queryShards: queryShards,
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
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()
// 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
combiner := trace.NewCombiner()
combiner.Consume(&tempopb.Trace{}) // The query path returns a non-nil result even if no inputs (which is different than other paths which return nil for no inputs)
statusCode := http.StatusNotFound
statusMsg := "trace not found"
for _, req := range reqs {
wg.Add(1)
go func(innerR *http.Request) {
defer wg.Done()
resp, err := s.next.RoundTrip(innerR)
mtx.Lock()
defer mtx.Unlock()
if err != nil {
overallError = err
}
if 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
}
// marshal into a trace to combine.
// todo: better define responsibilities between middleware. the parent middleware in frontend.go actually sets the header
// which forces the body here to be a proto encoded tempopb.Trace{}
traceResp := &tempopb.TraceByIDResponse{}
err = proto.Unmarshal(buff, traceResp)
if err != nil {
_ = level.Error(s.logger).Log("msg", "error unmarshalling response", "url", innerR.RequestURI, "err", err, "body", string(buff))
overallError = err
return
}
if traceResp.Metrics != nil {
totalFailedBlocks += traceResp.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
combiner.Consume(traceResp.Trace)
}(req)
}
wg.Wait()
if overallError != nil {
return nil, overallError
}
overallTrace, _ := combiner.Result()
if overallTrace == 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
}
buff, err := proto.Marshal(&tempopb.TraceByIDResponse{
Trace: overallTrace,
Metrics: &tempopb.TraceByIDMetrics{
FailedBlocks: totalFailedBlocks,
},
})
if err != nil {
_ = level.Error(s.logger).Log("msg", "error marshalling response to proto", "err", err)
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
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()
userID, err := user.ExtractOrgID(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(user.OrgIDHeaderName, userID)
uri := buildUpstreamRequestURI(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
}