-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathfrontend.go
412 lines (349 loc) · 11.2 KB
/
frontend.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package frontend
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"path"
"sync"
"time"
"github.com/NYTimes/gziphandler"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
opentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/httpgrpc/server"
"github.com/weaveworks/common/user"
)
const (
// StatusClientClosedRequest is the status code for when a client request cancellation of an http request
StatusClientClosedRequest = 499
)
var (
errTooManyRequest = httpgrpc.Errorf(http.StatusTooManyRequests, "too many outstanding requests")
errCanceled = httpgrpc.Errorf(StatusClientClosedRequest, context.Canceled.Error())
errDeadlineExceeded = httpgrpc.Errorf(http.StatusGatewayTimeout, context.DeadlineExceeded.Error())
)
// Config for a Frontend.
type Config struct {
MaxOutstandingPerTenant int `yaml:"max_outstanding_per_tenant"`
CompressResponses bool `yaml:"compress_responses"`
DownstreamURL string `yaml:"downstream_url"`
LogQueriesLongerThan time.Duration `yaml:"log_queries_longer_than"`
}
// RegisterFlags adds the flags required to config this to the given FlagSet.
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
f.IntVar(&cfg.MaxOutstandingPerTenant, "querier.max-outstanding-requests-per-tenant", 100, "Maximum number of outstanding requests per tenant per frontend; requests beyond this error with HTTP 429.")
f.BoolVar(&cfg.CompressResponses, "querier.compress-http-responses", false, "Compress HTTP responses.")
f.StringVar(&cfg.DownstreamURL, "frontend.downstream-url", "", "URL of downstream Prometheus.")
f.DurationVar(&cfg.LogQueriesLongerThan, "frontend.log-queries-longer-than", 0, "Log queries that are slower than the specified duration. 0 to disable.")
}
// Frontend queues HTTP requests, dispatches them to backends, and handles retries
// for requests which failed.
type Frontend struct {
cfg Config
log log.Logger
roundTripper http.RoundTripper
mtx sync.Mutex
cond *sync.Cond
queues map[string]chan *request
// Metrics.
queueDuration prometheus.Histogram
queueLength prometheus.Gauge
}
type request struct {
enqueueTime time.Time
queueSpan opentracing.Span
originalCtx context.Context
request *ProcessRequest
err chan error
response chan *ProcessResponse
}
// New creates a new frontend.
func New(cfg Config, log log.Logger, registerer prometheus.Registerer) (*Frontend, error) {
f := &Frontend{
cfg: cfg,
log: log,
queues: map[string]chan *request{},
queueDuration: promauto.With(registerer).NewHistogram(prometheus.HistogramOpts{
Namespace: "cortex",
Name: "query_frontend_queue_duration_seconds",
Help: "Time spend by requests queued.",
Buckets: prometheus.DefBuckets,
}),
queueLength: promauto.With(registerer).NewGauge(prometheus.GaugeOpts{
Namespace: "cortex",
Name: "query_frontend_queue_length",
Help: "Number of queries in the queue.",
}),
}
f.cond = sync.NewCond(&f.mtx)
// The front end implements http.RoundTripper using a GRPC worker queue by default.
f.roundTripper = f
// However if the user has specified a downstream Prometheus, then we should use that.
if cfg.DownstreamURL != "" {
u, err := url.Parse(cfg.DownstreamURL)
if err != nil {
return nil, err
}
f.roundTripper = RoundTripFunc(func(r *http.Request) (*http.Response, error) {
r.URL.Scheme = u.Scheme
r.URL.Host = u.Host
r.URL.Path = path.Join(u.Path, r.URL.Path)
return http.DefaultTransport.RoundTrip(r)
})
}
return f, nil
}
// Wrap uses a Tripperware to chain a new RoundTripper to the frontend.
func (f *Frontend) Wrap(trw Tripperware) {
f.roundTripper = trw(f.roundTripper)
}
// Tripperware is a signature for all http client-side middleware.
type Tripperware func(http.RoundTripper) http.RoundTripper
// RoundTripFunc is to http.RoundTripper what http.HandlerFunc is to http.Handler.
type RoundTripFunc func(*http.Request) (*http.Response, error)
// RoundTrip implements http.RoundTripper.
func (f RoundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
// Close stops new requests and errors out any pending requests.
func (f *Frontend) Close() {
f.mtx.Lock()
defer f.mtx.Unlock()
for len(f.queues) > 0 {
f.cond.Wait()
}
}
// Handler for HTTP requests.
func (f *Frontend) Handler() http.Handler {
if f.cfg.CompressResponses {
return gziphandler.GzipHandler(http.HandlerFunc(f.handle))
}
return http.HandlerFunc(f.handle)
}
func (f *Frontend) handle(w http.ResponseWriter, r *http.Request) {
userID, err := user.ExtractOrgID(r.Context())
if err != nil {
server.WriteError(w, err)
return
}
startTime := time.Now()
resp, err := f.roundTripper.RoundTrip(r)
queryResponseTime := time.Since(startTime)
if f.cfg.LogQueriesLongerThan > 0 && queryResponseTime > f.cfg.LogQueriesLongerThan {
level.Info(f.log).Log("msg", "slow query", "org_id", userID, "url", fmt.Sprintf("http://%s", r.Host+r.RequestURI), "time_taken", queryResponseTime.String())
}
if err != nil {
writeError(w, err)
return
}
hs := w.Header()
for h, vs := range resp.Header {
hs[h] = vs
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
func writeError(w http.ResponseWriter, err error) {
switch err {
case context.Canceled:
err = errCanceled
case context.DeadlineExceeded:
err = errDeadlineExceeded
default:
}
server.WriteError(w, err)
}
// RoundTrip implement http.Transport.
func (f *Frontend) RoundTrip(r *http.Request) (*http.Response, error) {
req, err := server.HTTPRequest(r)
if err != nil {
return nil, err
}
resp, err := f.RoundTripGRPC(r.Context(), &ProcessRequest{
HttpRequest: req,
})
if err != nil {
return nil, err
}
httpResp := &http.Response{
StatusCode: int(resp.HttpResponse.Code),
Body: ioutil.NopCloser(bytes.NewReader(resp.HttpResponse.Body)),
Header: http.Header{},
}
for _, h := range resp.HttpResponse.Headers {
httpResp.Header[h.Key] = h.Values
}
return httpResp, nil
}
type httpgrpcHeadersCarrier httpgrpc.HTTPRequest
func (c *httpgrpcHeadersCarrier) Set(key, val string) {
c.Headers = append(c.Headers, &httpgrpc.Header{
Key: key,
Values: []string{val},
})
}
// RoundTripGRPC round trips a proto (instead of a HTTP request).
func (f *Frontend) RoundTripGRPC(ctx context.Context, req *ProcessRequest) (*ProcessResponse, error) {
// Propagate trace context in gRPC too - this will be ignored if using HTTP.
tracer, span := opentracing.GlobalTracer(), opentracing.SpanFromContext(ctx)
if tracer != nil && span != nil {
carrier := (*httpgrpcHeadersCarrier)(req.HttpRequest)
tracer.Inject(span.Context(), opentracing.HTTPHeaders, carrier)
}
request := request{
request: req,
originalCtx: ctx,
// Buffer of 1 to ensure response can be written by the server side
// of the Process stream, even if this goroutine goes away due to
// client context cancellation.
err: make(chan error, 1),
response: make(chan *ProcessResponse, 1),
}
if err := f.queueRequest(ctx, &request); err != nil {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case resp := <-request.response:
return resp, nil
case err := <-request.err:
return nil, err
}
}
// Process allows backends to pull requests from the frontend.
func (f *Frontend) Process(server Frontend_ProcessServer) error {
// If the downstream request(from querier -> frontend) is cancelled,
// we need to ping the condition variable to unblock getNextRequest.
// Ideally we'd have ctx aware condition variables...
go func() {
<-server.Context().Done()
f.cond.Broadcast()
}()
for {
req, err := f.getNextRequest(server.Context())
if err != nil {
return err
}
// Handle the stream sending & receiving on a goroutine so we can
// monitoring the contexts in a select and cancel things appropriately.
resps := make(chan *ProcessResponse, 1)
errs := make(chan error, 1)
go func() {
err = server.Send(req.request)
if err != nil {
errs <- err
return
}
resp, err := server.Recv()
if err != nil {
errs <- err
return
}
resps <- resp
}()
select {
// If the upstream request is cancelled, we need to cancel the
// downstream req. Only way we can do that is to close the stream.
// The worker client is expecting this semantics.
case <-req.originalCtx.Done():
return req.originalCtx.Err()
// Is there was an error handling this request due to network IO,
// then error out this upstream request _and_ stream.
case err := <-errs:
req.err <- err
return err
// Happy path: propagate the response.
case resp := <-resps:
req.response <- resp
}
}
}
func (f *Frontend) queueRequest(ctx context.Context, req *request) error {
userID, err := user.ExtractOrgID(ctx)
if err != nil {
return err
}
req.enqueueTime = time.Now()
req.queueSpan, _ = opentracing.StartSpanFromContext(ctx, "queued")
f.mtx.Lock()
defer f.mtx.Unlock()
queue, ok := f.queues[userID]
if !ok {
queue = make(chan *request, f.cfg.MaxOutstandingPerTenant)
f.queues[userID] = queue
}
select {
case queue <- req:
f.queueLength.Add(1)
f.cond.Broadcast()
return nil
default:
return errTooManyRequest
}
}
// getQueue picks a random queue and takes the next unexpired request off of it, so we
// fairly process users queries. Will block if there are no requests.
func (f *Frontend) getNextRequest(ctx context.Context) (*request, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
FindQueue:
for len(f.queues) == 0 && ctx.Err() == nil {
f.cond.Wait()
}
if err := ctx.Err(); err != nil {
return nil, err
}
i, n := 0, rand.Intn(len(f.queues))
for userID, queue := range f.queues {
if i < n {
i++
continue
}
/*
We want to dequeue the next unexpired request from the chosen tenant queue.
The chance of choosing a particular tenant for dequeueing is (1/active_tenants).
This is problematic under load, especially with other middleware enabled such as
querier.split-by-interval, where one request may fan out into many.
If expired requests aren't exhausted before checking another tenant, it would take
n_active_tenants * n_expired_requests_at_front_of_queue requests being processed
before an active request was handled for the tenant in question.
If this tenant meanwhile continued to queue requests,
it's possible that it's own queue would perpetually contain only expired requests.
*/
// Pick the first non-expired request from this user's queue (if any).
for {
lastRequest := false
request := <-queue
if len(queue) == 0 {
delete(f.queues, userID)
lastRequest = true
}
// Tell close() we've processed a request.
f.cond.Broadcast()
f.queueDuration.Observe(time.Since(request.enqueueTime).Seconds())
f.queueLength.Add(-1)
request.queueSpan.Finish()
// Ensure the request has not already expired.
if request.originalCtx.Err() == nil {
return request, nil
}
// Stop iterating on this queue if we've just consumed the last request.
if lastRequest {
break
}
}
}
// There are no unexpired requests, so we can get back
// and wait for more requests.
goto FindQueue
}