-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
handler.go
448 lines (400 loc) · 13.4 KB
/
handler.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package receive
import (
"bytes"
"context"
"fmt"
"io/ioutil"
stdlog "log"
"net"
"net/http"
"strconv"
"sync"
"sync/atomic"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
conntrack "github.com/mwitkow/go-conntrack"
"github.com/opentracing-contrib/go-stdlib/nethttp"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/route"
promtsdb "github.com/prometheus/prometheus/storage/tsdb"
terrors "github.com/prometheus/prometheus/tsdb/errors"
"github.com/thanos-io/thanos/pkg/runutil"
"github.com/thanos-io/thanos/pkg/store/prompb"
)
// Options for the web Handler.
type Options struct {
Receiver *Writer
ListenAddress string
Registry prometheus.Registerer
ReadyStorage *promtsdb.ReadyStorage
Endpoint string
TenantHeader string
ReplicaHeader string
ReplicationFactor uint64
}
// Handler serves a Prometheus remote write receiving HTTP endpoint.
type Handler struct {
readyStorage *promtsdb.ReadyStorage
logger log.Logger
receiver *Writer
router *route.Router
options *Options
listener net.Listener
mtx sync.RWMutex
hashring Hashring
// Metrics
requestDuration *prometheus.HistogramVec
requestsTotal *prometheus.CounterVec
responseSize *prometheus.HistogramVec
forwardRequestsTotal *prometheus.CounterVec
// These fields are uint32 rather than boolean to be able to use atomic functions.
storageReady uint32
}
func NewHandler(logger log.Logger, o *Options) *Handler {
if logger == nil {
logger = log.NewNopLogger()
}
h := &Handler{
logger: logger,
readyStorage: o.ReadyStorage,
receiver: o.Receiver,
options: o,
requestDuration: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "thanos_http_request_duration_seconds",
Help: "Histogram of latencies for HTTP requests.",
Buckets: []float64{.1, .2, .4, 1, 3, 8, 20, 60, 120},
},
[]string{"handler"},
),
requestsTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "thanos_http_requests_total",
Help: "Tracks the number of HTTP requests.",
}, []string{"code", "handler", "method"},
),
responseSize: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "thanos_http_response_size_bytes",
Help: "Histogram of response size for HTTP requests.",
Buckets: prometheus.ExponentialBuckets(100, 10, 8),
},
[]string{"handler"},
),
forwardRequestsTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "thanos_receive_forward_requests_total",
Help: "The number of forward requests.",
}, []string{"result"},
),
}
router := route.New().WithInstrumentation(h.instrumentHandler)
h.router = router
readyf := h.testReady
router.Post("/api/v1/receive", readyf(h.receive))
if o.Registry != nil {
o.Registry.MustRegister(
h.requestDuration,
h.requestsTotal,
h.responseSize,
h.forwardRequestsTotal,
)
}
return h
}
func (h *Handler) instrumentHandler(handlerName string, handler http.HandlerFunc) http.HandlerFunc {
return promhttp.InstrumentHandlerDuration(
h.requestDuration.MustCurryWith(prometheus.Labels{"handler": handlerName}),
promhttp.InstrumentHandlerResponseSize(
h.responseSize.MustCurryWith(prometheus.Labels{"handler": handlerName}),
promhttp.InstrumentHandlerCounter(
h.requestsTotal.MustCurryWith(prometheus.Labels{"handler": handlerName}),
handler,
),
),
)
}
// StorageReady marks the storage as ready.
func (h *Handler) StorageReady() {
atomic.StoreUint32(&h.storageReady, 1)
}
// Hashring sets the hashring for the handler and marks the hashring as ready.
// If the hashring is nil, then the hashring is marked as not ready.
func (h *Handler) Hashring(hashring Hashring) {
h.mtx.Lock()
defer h.mtx.Unlock()
h.hashring = hashring
}
// Verifies whether the server is ready or not.
func (h *Handler) isReady() bool {
sr := atomic.LoadUint32(&h.storageReady)
h.mtx.RLock()
hr := h.hashring != nil
h.mtx.RUnlock()
return sr > 0 && hr
}
// Checks if server is ready, calls f if it is, returns 503 if it is not.
func (h *Handler) testReady(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if h.isReady() {
f(w, r)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
_, err := fmt.Fprintf(w, "Service Unavailable")
if err != nil {
h.logger.Log("msg", "failed to write to response body", "err", err)
}
}
}
// Close stops the Handler.
func (h *Handler) Close() {
if h.listener != nil {
runutil.CloseWithLogOnErr(h.logger, h.listener, "receive HTTP listener")
}
}
// Run serves the HTTP endpoints.
func (h *Handler) Run() error {
level.Info(h.logger).Log("msg", "Start listening for connections", "address", h.options.ListenAddress)
var err error
h.listener, err = net.Listen("tcp", h.options.ListenAddress)
if err != nil {
return err
}
// Monitor incoming connections with conntrack.
h.listener = conntrack.NewListener(h.listener,
conntrack.TrackWithName("http"),
conntrack.TrackWithTracing())
operationName := nethttp.OperationNameFunc(func(r *http.Request) string {
return fmt.Sprintf("%s %s", r.Method, r.URL.Path)
})
mux := http.NewServeMux()
mux.Handle("/", h.router)
errlog := stdlog.New(log.NewStdlibAdapter(level.Error(h.logger)), "", 0)
httpSrv := &http.Server{
Handler: nethttp.Middleware(opentracing.GlobalTracer(), mux, operationName),
ErrorLog: errlog,
}
return httpSrv.Serve(h.listener)
}
// replica encapsulates the replica number of a request and if the request is
// already replicated.
type replica struct {
n uint64
replicated bool
}
func (h *Handler) receive(w http.ResponseWriter, r *http.Request) {
compressed, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
reqBuf, err := snappy.Decode(nil, compressed)
if err != nil {
level.Error(h.logger).Log("msg", "snappy decode error", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var wreq prompb.WriteRequest
if err := proto.Unmarshal(reqBuf, &wreq); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var rep replica
replicaRaw := r.Header.Get(h.options.ReplicaHeader)
// If the header is empty, we assume the request is not yet replicated.
if replicaRaw != "" {
if rep.n, err = strconv.ParseUint(replicaRaw, 10, 64); err != nil {
http.Error(w, "could not parse replica header", http.StatusBadRequest)
return
}
rep.replicated = true
}
// The replica value in the header is zero-indexed, thus we need >=.
if rep.n >= h.options.ReplicationFactor {
http.Error(w, "replica count exceeds replication factor", http.StatusBadRequest)
return
}
tenant := r.Header.Get(h.options.TenantHeader)
// Forward any time series as necessary. All time series
// destined for the local node will be written to the receiver.
// Time series will be replicated as necessary.
if err := h.forward(r.Context(), tenant, rep, &wreq); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// forward accepts a write request, batches its time series by
// corresponding endpoint, and forwards them in parallel to the
// correct endpoint. Requests destined for the local node are written
// the the local receiver. For a given write request, at most one outgoing
// write request will be made to every other node in the hashring,
// unless the request needs to be replicated.
// The function only returns when all requests have finished
// or the context is canceled.
func (h *Handler) forward(ctx context.Context, tenant string, r replica, wreq *prompb.WriteRequest) error {
wreqs := make(map[string]*prompb.WriteRequest)
replicas := make(map[string]replica)
// It is possible that hashring is ready in testReady() but unready now,
// so need to lock here.
h.mtx.RLock()
if h.hashring == nil {
h.mtx.RUnlock()
return errors.New("hashring is not ready")
}
// Batch all of the time series in the write request
// into several smaller write requests that are
// grouped by target endpoint. This ensures that
// for any incoming write request to a node,
// at most one outgoing write request will be made
// to every other node in the hashring, rather than
// one request per time series.
for i := range wreq.Timeseries {
endpoint, err := h.hashring.GetN(tenant, &wreq.Timeseries[i], r.n)
if err != nil {
h.mtx.RUnlock()
return err
}
if _, ok := wreqs[endpoint]; !ok {
wreqs[endpoint] = &prompb.WriteRequest{}
replicas[endpoint] = r
}
wr := wreqs[endpoint]
wr.Timeseries = append(wr.Timeseries, wreq.Timeseries[i])
}
h.mtx.RUnlock()
return h.parallelizeRequests(ctx, tenant, replicas, wreqs)
}
// parallelizeRequests parallelizes a given set of write requests.
// The function only returns when all requests have finished
// or the context is canceled.
func (h *Handler) parallelizeRequests(ctx context.Context, tenant string, replicas map[string]replica, wreqs map[string]*prompb.WriteRequest) error {
ec := make(chan error)
defer close(ec)
// We don't wan't to use a sync.WaitGroup here because that
// introduces an unnecessary second synchronization mechanism,
// the first being the error chan. Plus, it saves us a goroutine
// as in order to collect errors while doing wg.Wait, we would
// need a separate error collection goroutine.
var n int
for endpoint := range wreqs {
n++
// If the request is not yet replicated, let's replicate it.
// If the replication factor isn't greater than 1, let's
// just forward the requests.
if !replicas[endpoint].replicated && h.options.ReplicationFactor > 1 {
go func(endpoint string) {
ec <- h.replicate(ctx, tenant, wreqs[endpoint])
}(endpoint)
continue
}
// If the endpoint for the write request is the
// local node, then don't make a request but store locally.
// By handing replication to the local node in the same
// function as replication to other nodes, we can treat
// a failure to write locally as just another error that
// can be ignored if the replication factor is met.
if endpoint == h.options.Endpoint {
go func(endpoint string) {
err := h.receiver.Receive(wreqs[endpoint])
if err != nil {
level.Error(h.logger).Log("msg", "storing locally", "err", err, "endpoint", endpoint)
}
ec <- err
}(endpoint)
continue
}
// Make a request to the specified endpoint.
go func(endpoint string) {
buf, err := proto.Marshal(wreqs[endpoint])
if err != nil {
level.Error(h.logger).Log("msg", "marshaling proto", "err", err, "endpoint", endpoint)
ec <- err
return
}
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(snappy.Encode(nil, buf)))
if err != nil {
level.Error(h.logger).Log("msg", "creating request", "err", err, "endpoint", endpoint)
ec <- err
return
}
req.Header.Add(h.options.TenantHeader, tenant)
req.Header.Add(h.options.ReplicaHeader, strconv.FormatUint(replicas[endpoint].n, 10))
// Increment the counters as necessary now that
// the requests will go out.
defer func() {
if err != nil {
h.forwardRequestsTotal.WithLabelValues("error").Inc()
return
}
h.forwardRequestsTotal.WithLabelValues("success").Inc()
}()
// Actually make the request against the endpoint
// we determined should handle these time series.
var res *http.Response
res, err = http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
level.Error(h.logger).Log("msg", "forwarding request", "err", err, "endpoint", endpoint)
ec <- err
return
}
if res.StatusCode != http.StatusOK {
err = errors.New(res.Status)
level.Error(h.logger).Log("msg", "forwarding returned non-200 status", "err", err, "endpoint", endpoint)
ec <- err
return
}
ec <- nil
}(endpoint)
}
// Collect any errors from forwarding the time series.
// Rather than doing a wg.Wait here, we decrement a counter
// for every error received on the chan. This simplifies
// error collection and avoids data races with a separate
// error collection goroutine.
var errs terrors.MultiError
for ; n > 0; n-- {
if err := <-ec; err != nil {
errs.Add(err)
}
}
return errs.Err()
}
// replicate replicates a write request to (replication-factor) nodes
// selected by the tenant and time series.
// The function only returns when all replication requests have finished
// or the context is canceled.
func (h *Handler) replicate(ctx context.Context, tenant string, wreq *prompb.WriteRequest) error {
wreqs := make(map[string]*prompb.WriteRequest)
replicas := make(map[string]replica)
var i uint64
// It is possible that hashring is ready in testReady() but unready now,
// so need to lock here.
h.mtx.RLock()
if h.hashring == nil {
h.mtx.RUnlock()
return errors.New("hashring is not ready")
}
for i = 0; i < h.options.ReplicationFactor; i++ {
endpoint, err := h.hashring.GetN(tenant, &wreq.Timeseries[0], i)
if err != nil {
h.mtx.RUnlock()
return err
}
wreqs[endpoint] = wreq
replicas[endpoint] = replica{i, true}
}
h.mtx.RUnlock()
err := h.parallelizeRequests(ctx, tenant, replicas, wreqs)
if errs, ok := err.(terrors.MultiError); ok {
if uint64(len(errs)) >= (h.options.ReplicationFactor+1)/2 {
return errors.New("did not meet replication threshold")
}
}
return errors.Wrap(err, "could not replicate write request")
}