-
Notifications
You must be signed in to change notification settings - Fork 455
/
distributor.go
323 lines (278 loc) · 9.63 KB
/
distributor.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/alertmanager/distributor.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package alertmanager
import (
"context"
"hash/fnv"
"io"
"math/rand"
"net/http"
"path"
"strings"
"sync"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/httpgrpc"
"github.com/grafana/dskit/ring"
"github.com/grafana/dskit/ring/client"
"github.com/grafana/dskit/services"
"github.com/grafana/dskit/tenant"
"github.com/grafana/dskit/user"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/mimir/pkg/alertmanager/merger"
"github.com/grafana/mimir/pkg/util"
util_log "github.com/grafana/mimir/pkg/util/log"
)
// Distributor forwards requests to individual alertmanagers.
type Distributor struct {
services.Service
cfg ClientConfig
maxRecvMsgSize int64
requestsInFlight sync.WaitGroup
alertmanagerRing ring.ReadRing
alertmanagerClientsPool ClientsPool
logger log.Logger
}
// NewDistributor constructs a new Distributor
func NewDistributor(cfg ClientConfig, maxRecvMsgSize int64, alertmanagersRing *ring.Ring, alertmanagerClientsPool ClientsPool, logger log.Logger, reg prometheus.Registerer) (d *Distributor, err error) {
if alertmanagerClientsPool == nil {
alertmanagerClientsPool = newAlertmanagerClientsPool(client.NewRingServiceDiscovery(alertmanagersRing), cfg, logger, reg)
}
d = &Distributor{
cfg: cfg,
logger: logger,
maxRecvMsgSize: maxRecvMsgSize,
alertmanagerRing: alertmanagersRing,
alertmanagerClientsPool: alertmanagerClientsPool,
}
d.Service = services.NewBasicService(nil, d.running, nil)
return d, nil
}
func (d *Distributor) running(ctx context.Context) error {
<-ctx.Done()
d.requestsInFlight.Wait()
return nil
}
func (d *Distributor) isQuorumWritePath(p string) bool {
return strings.HasSuffix(p, "/alerts")
}
func (d *Distributor) isUnaryWritePath(p string) bool {
return strings.HasSuffix(p, "/silences")
}
func (d *Distributor) isUnaryDeletePath(p string) bool {
return strings.HasSuffix(path.Dir(p), "/silence")
}
func (d *Distributor) isQuorumReadPath(p string) (bool, merger.Merger) {
if strings.HasSuffix(p, "/v2/alerts") {
return true, merger.V2Alerts{}
}
if strings.HasSuffix(p, "/v2/alerts/groups") {
return true, merger.V2AlertGroups{}
}
if strings.HasSuffix(p, "/v2/silences") {
return true, merger.V2Silences{}
}
if strings.HasSuffix(path.Dir(p), "/v2/silence") {
return true, merger.V2SilenceID{}
}
return false, nil
}
// DistributeRequest shards the writes and returns as soon as the quorum is satisfied.
// In case of reads, it proxies the request to one of the alertmanagers.
func (d *Distributor) DistributeRequest(w http.ResponseWriter, r *http.Request) {
d.requestsInFlight.Add(1)
defer d.requestsInFlight.Done()
userID, err := tenant.TenantID(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
logger := util_log.WithContext(r.Context(), d.logger)
if r.Method == http.MethodPost {
if d.isQuorumWritePath(r.URL.Path) {
d.doQuorum(userID, w, r, logger, merger.Noop{})
return
}
if d.isUnaryWritePath(r.URL.Path) {
d.doUnary(userID, w, r, logger)
return
}
}
if r.Method == http.MethodDelete {
if d.isUnaryDeletePath(r.URL.Path) {
d.doUnary(userID, w, r, logger)
return
}
}
if r.Method == http.MethodGet || r.Method == http.MethodHead {
if ok, m := d.isQuorumReadPath(r.URL.Path); ok {
d.doQuorum(userID, w, r, logger, m)
return
}
// All other paths are just passed to a random replica.
// This is primarily used for serving the web user interface.
d.doUnary(userID, w, r, logger)
return
}
http.Error(w, "route not supported by distributor", http.StatusNotFound)
}
func (d *Distributor) doQuorum(userID string, w http.ResponseWriter, r *http.Request, logger log.Logger, m merger.Merger) {
var body []byte
var err error
if r.Body != nil {
body, err = io.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
if err != nil {
if util.IsRequestBodyTooLarge(err) {
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
return
}
level.Error(logger).Log("msg", "failed to read the request body during write", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
var responses []*httpgrpc.HTTPResponse
var responsesMtx sync.Mutex
grpcHeaders := httpToHttpgrpcHeaders(r.Header)
err = ring.DoBatchWithOptions(r.Context(), RingOp, d.alertmanagerRing, []uint32{shardByUser(userID)}, func(am ring.InstanceDesc, _ []int) error {
// Use a background context to make sure all alertmanagers get the request even if we return early.
localCtx := user.InjectOrgID(context.Background(), userID)
sp, localCtx := opentracing.StartSpanFromContext(localCtx, "Distributor.doQuorum")
defer sp.Finish()
resp, err := d.doRequest(localCtx, am, &httpgrpc.HTTPRequest{
Method: r.Method,
Url: r.RequestURI,
Body: body,
Headers: grpcHeaders,
})
if err != nil {
return err
}
if resp.Code/100 != 2 {
return httpgrpc.ErrorFromHTTPResponse(resp)
}
responsesMtx.Lock()
responses = append(responses, resp)
responsesMtx.Unlock()
return nil
}, ring.DoBatchOptions{})
if err != nil {
respondFromError(err, w, logger)
return
}
responsesMtx.Lock() // Another request might be ongoing after quorum.
resps := responses
responsesMtx.Unlock()
if len(resps) > 0 {
respondFromMultipleHTTPGRPCResponses(w, logger, resps, m)
} else {
// This should not happen.
level.Error(logger).Log("msg", "distributor did not receive any response from alertmanagers, but there were no errors")
w.WriteHeader(http.StatusInternalServerError)
}
}
func (d *Distributor) doUnary(userID string, w http.ResponseWriter, r *http.Request, logger log.Logger) {
key := shardByUser(userID)
replicationSet, err := d.alertmanagerRing.Get(key, RingOp, nil, nil, nil)
if err != nil {
level.Error(logger).Log("msg", "failed to get replication set from the ring", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, d.maxRecvMsgSize))
if err != nil {
if util.IsRequestBodyTooLarge(err) {
http.Error(w, "Request body too large", http.StatusRequestEntityTooLarge)
return
}
level.Error(logger).Log("msg", "failed to read the request body during read", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
req := &httpgrpc.HTTPRequest{
Method: r.Method,
Url: r.RequestURI,
Body: body,
Headers: httpToHttpgrpcHeaders(r.Header),
}
sp, ctx := opentracing.StartSpanFromContext(r.Context(), "Distributor.doUnary")
defer sp.Finish()
// Until we have a mechanism to combine the results from multiple alertmanagers,
// we forward the request to only only of the alertmanagers.
amDesc := replicationSet.Instances[rand.Intn(len(replicationSet.Instances))]
resp, err := d.doRequest(ctx, amDesc, req)
if err != nil {
respondFromError(err, w, logger)
return
}
respondFromHTTPGRPCResponse(w, resp)
}
func respondFromError(err error, w http.ResponseWriter, logger log.Logger) {
httpResp, ok := httpgrpc.HTTPResponseFromError(err)
if !ok {
level.Error(logger).Log("msg", "failed to process the request to the alertmanager", "err", err)
http.Error(w, "Failed to process the request to the alertmanager", http.StatusInternalServerError)
return
}
respondFromHTTPGRPCResponse(w, httpResp)
}
func respondFromHTTPGRPCResponse(w http.ResponseWriter, httpResp *httpgrpc.HTTPResponse) {
for _, h := range httpResp.Headers {
for _, v := range h.Values {
w.Header().Add(h.Key, v)
}
}
w.WriteHeader(int(httpResp.Code))
w.Write(httpResp.Body) //nolint
}
func (d *Distributor) doRequest(ctx context.Context, am ring.InstanceDesc, req *httpgrpc.HTTPRequest) (*httpgrpc.HTTPResponse, error) {
ctx, cancel := context.WithTimeout(ctx, d.cfg.RemoteTimeout)
defer cancel()
amClient, err := d.alertmanagerClientsPool.GetClientFor(am.Addr)
if err != nil {
return nil, errors.Wrapf(err, "failed to get alertmanager client from pool (alertmanager address: %s)", am.Addr)
}
return amClient.HandleRequest(ctx, req)
}
func shardByUser(userID string) uint32 {
ringHasher := fnv.New32a()
// Hasher never returns err.
_, _ = ringHasher.Write([]byte(userID))
return ringHasher.Sum32()
}
func httpToHttpgrpcHeaders(hs http.Header) []*httpgrpc.Header {
result := make([]*httpgrpc.Header, 0, len(hs))
for k, vs := range hs {
result = append(result, &httpgrpc.Header{
Key: k,
Values: vs,
})
}
return result
}
func respondFromMultipleHTTPGRPCResponses(w http.ResponseWriter, logger log.Logger, responses []*httpgrpc.HTTPResponse, merger merger.Merger) {
bodies := make([][]byte, len(responses))
for i, r := range responses {
bodies[i] = r.Body
}
body, err := merger.MergeResponses(bodies)
if err != nil {
level.Error(logger).Log("msg", "failed to merge responses for request", "err", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// It is assumed by using this function, the caller knows that the responses it receives
// have already been checked for success or failure, and that the headers will always
// match due to the nature of the request. If this is not the case, a different merge
// function should be implemented to cope with the differing responses.
response := &httpgrpc.HTTPResponse{
Code: responses[0].Code,
Headers: responses[0].Headers,
Body: body,
}
respondFromHTTPGRPCResponse(w, response)
}