forked from ContentSquare/chproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
499 lines (440 loc) · 14.1 KB
/
proxy.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package main
import (
"context"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"sync"
"time"
"github.com/Vertamedia/chproxy/cache"
"github.com/Vertamedia/chproxy/config"
"github.com/Vertamedia/chproxy/log"
"github.com/prometheus/client_golang/prometheus"
)
type reverseProxy struct {
rp *httputil.ReverseProxy
// configLock serializes access to applyConfig.
// It protects reload* fields.
configLock sync.Mutex
reloadSignal chan struct{}
reloadWG sync.WaitGroup
// lock protects users, clusters and caches.
// RWMutex enables concurrent access to getScope.
lock sync.RWMutex
users map[string]*user
clusters map[string]*cluster
caches map[string]*cache.Cache
}
func newReverseProxy() *reverseProxy {
return &reverseProxy{
rp: &httputil.ReverseProxy{
Director: func(*http.Request) {},
// Suppress error logging in ReverseProxy, since all the errors
// are handled and logged in the code below.
ErrorLog: log.NilLogger,
},
reloadSignal: make(chan struct{}),
reloadWG: sync.WaitGroup{},
}
}
func (rp *reverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
startTime := time.Now()
s, status, err := rp.getScope(req)
if err != nil {
q := getQuerySnippet(req)
err = fmt.Errorf("%q: %s; query: %q", req.RemoteAddr, err, q)
respondWith(rw, err, status)
return
}
// WARNING: don't use s.labels before s.incQueued,
// since `replica` and `cluster_node` may change inside incQueued.
if err := s.incQueued(); err != nil {
limitExcess.With(s.labels).Inc()
q := getQuerySnippet(req)
err = fmt.Errorf("%s: %s; query: %q", s, err, q)
respondWith(rw, err, http.StatusTooManyRequests)
return
}
defer s.dec()
log.Debugf("%s: request start", s)
requestSum.With(s.labels).Inc()
if s.user.allowCORS {
origin := req.Header.Get("Origin")
if len(origin) == 0 {
origin = "*"
}
rw.Header().Set("Access-Control-Allow-Origin", origin)
}
req.Body = &statReadCloser{
ReadCloser: req.Body,
bytesRead: requestBodyBytes.With(s.labels),
}
srw := &statResponseWriter{
ResponseWriter: rw,
bytesWritten: responseBodyBytes.With(s.labels),
}
req, origParams := s.decorateRequest(req)
// wrap body into cachedReadCloser, so we could obtain the original
// request on error.
req.Body = &cachedReadCloser{
ReadCloser: req.Body,
}
// publish session_id if needed
if s.sessionId != "" {
rw.Header().Set("X-ClickHouse-Server-Session-Id", s.sessionId)
}
if s.user.cache == nil {
rp.proxyRequest(s, srw, srw, req)
} else {
rp.serveFromCache(s, srw, req, origParams)
}
// It is safe calling getQuerySnippet here, since the request
// has been already read in proxyRequest or serveFromCache.
q := getQuerySnippet(req)
if srw.statusCode == http.StatusOK {
requestSuccess.With(s.labels).Inc()
log.Debugf("%s: request success; query: %q; Method: %s; URL: %q", s, q, req.Method, req.URL.String())
} else {
log.Debugf("%s: request failure: non-200 status code %d; query: %q; Method: %s; URL: %q", s, srw.statusCode, q, req.Method, req.URL.String())
}
statusCodes.With(
prometheus.Labels{
"user": s.user.name,
"cluster": s.cluster.name,
"cluster_user": s.clusterUser.name,
"replica": s.host.replica.name,
"cluster_node": s.host.addr.Host,
"code": strconv.Itoa(srw.statusCode),
},
).Inc()
since := float64(time.Since(startTime).Seconds())
requestDuration.With(s.labels).Observe(since)
}
// proxyRequest proxies the given request to clickhouse and sends response
// to rw.
//
// srw is required only for setting non-200 status codes on timeouts
// or on client connection disconnects.
func (rp *reverseProxy) proxyRequest(s *scope, rw http.ResponseWriter, srw *statResponseWriter, req *http.Request) {
// wrap body into cachedReadCloser, so we could obtain the original
// request on error.
if _, ok := req.Body.(*cachedReadCloser); !ok {
req.Body = &cachedReadCloser{
ReadCloser: req.Body,
}
}
timeout, timeoutErrMsg := s.getTimeoutWithErrMsg()
ctx := context.Background()
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
// Cancel the ctx if client closes the remote connection,
// so the proxied query may be killed instantly.
ctx, ctxCancel := context.WithCancel(ctx)
defer ctxCancel()
// rw must implement http.CloseNotifier.
ch := rw.(http.CloseNotifier).CloseNotify()
go func() {
select {
case <-ch:
ctxCancel()
case <-ctx.Done():
}
}()
req = req.WithContext(ctx)
startTime := time.Now()
rp.rp.ServeHTTP(rw, req)
err := ctx.Err()
switch err {
case nil:
// The request has been successfully proxied.
since := float64(time.Since(startTime).Seconds())
proxiedResponseDuration.With(s.labels).Observe(since)
// cache.ResponseWriter pushes status code to srw on Commit/Rollback actions
// but they didn't happen yet, so manually propagate the status code from crw to srw.
if crw, ok := rw.(*cache.ResponseWriter); ok {
srw.statusCode = crw.StatusCode()
}
// StatusBadGateway response is returned by http.ReverseProxy when
// it cannot establish connection to remote host.
if srw.statusCode == http.StatusBadGateway {
s.host.penalize()
q := getQuerySnippet(req)
err := fmt.Errorf("%s: cannot reach %s; query: %q", s, s.host.addr.Host, q)
respondWith(srw, err, srw.statusCode)
}
case context.Canceled:
canceledRequest.With(s.labels).Inc()
q := getQuerySnippet(req)
log.Debugf("%s: remote client closed the connection in %s; query: %q", s, time.Since(startTime), q)
if err := s.killQuery(); err != nil {
log.Errorf("%s: cannot kill query: %s; query: %q", s, err, q)
}
srw.statusCode = 499 // See https://httpstatuses.com/499 .
case context.DeadlineExceeded:
timeoutRequest.With(s.labels).Inc()
// Penalize host with the timed out query, because it may be overloaded.
s.host.penalize()
q := getQuerySnippet(req)
log.Debugf("%s: query timeout in %s; query: %q", s, time.Since(startTime), q)
if err := s.killQuery(); err != nil {
log.Errorf("%s: cannot kill query: %s; query: %q", s, err, q)
}
err = fmt.Errorf("%s: %s; query: %q", s, timeoutErrMsg, q)
respondWith(rw, err, http.StatusGatewayTimeout)
srw.statusCode = http.StatusGatewayTimeout
default:
panic(fmt.Sprintf("BUG: context.Context.Err() returned unexpected error: %s", err))
}
}
func (rp *reverseProxy) serveFromCache(s *scope, srw *statResponseWriter, req *http.Request, origParams url.Values) {
noCache := origParams.Get("no_cache")
if noCache == "1" || noCache == "true" {
// The response caching is disabled.
rp.proxyRequest(s, srw, srw, req)
return
}
q, err := getFullQuery(req)
if err != nil {
err = fmt.Errorf("%s: cannot read query: %s", s, err)
respondWith(srw, err, http.StatusBadRequest)
return
}
if !canCacheQuery(q) {
// The query cannot be cached, so just proxy it.
rp.proxyRequest(s, srw, srw, req)
return
}
// Do not store `replica` and `cluster_node` in labels, since they have
// no sense for cache metrics.
labels := prometheus.Labels{
"cache": s.user.cache.Name,
"user": s.labels["user"],
"cluster": s.labels["cluster"],
"cluster_user": s.labels["cluster_user"],
}
var paramsHash uint32
if s.user.params != nil {
paramsHash = s.user.params.key
}
key := &cache.Key{
Query: skipLeadingComments(q),
// sort `Accept-Encoding` header to get the same combination for different browsers
AcceptEncoding: sortHeader(req.Header.Get("Accept-Encoding")),
DefaultFormat: origParams.Get("default_format"),
Database: origParams.Get("database"),
Compress: origParams.Get("compress"),
EnableHTTPCompression: origParams.Get("enable_http_compression"),
Namespace: origParams.Get("cache_namespace"),
Extremes: origParams.Get("extremes"),
MaxResultRows: origParams.Get("max_result_rows"),
ResultOverflowMode: origParams.Get("result_overflow_mode"),
UserParamsHash: paramsHash,
}
startTime := time.Now()
err = s.user.cache.WriteTo(srw, key)
if err == nil {
// The response has been successfully served from cache.
cacheHit.With(labels).Inc()
since := float64(time.Since(startTime).Seconds())
cachedResponseDuration.With(labels).Observe(since)
log.Debugf("%s: cache hit", s)
return
}
if err != cache.ErrMissing {
// Unexpected error while serving the response.
err = fmt.Errorf("%s: %s; query: %q", s, err, q)
respondWith(srw, err, http.StatusInternalServerError)
return
}
// The response wasn't found in the cache.
// Request it from clickhouse.
cacheMiss.With(labels).Inc()
log.Debugf("%s: cache miss", s)
crw, err := s.user.cache.NewResponseWriter(srw, key)
if err != nil {
err = fmt.Errorf("%s: %s; query: %q", s, err, q)
respondWith(srw, err, http.StatusInternalServerError)
return
}
rp.proxyRequest(s, crw, srw, req)
if crw.StatusCode() != http.StatusOK || s.canceled {
// Do not cache non-200 or cancelled responses.
// Restore the original status code by proxyRequest if it was set.
if srw.statusCode != 0 {
crw.WriteHeader(srw.statusCode)
}
err = crw.Rollback()
} else {
err = crw.Commit()
}
if err != nil {
err = fmt.Errorf("%s: %s; query: %q", s, err, q)
respondWith(srw, err, http.StatusInternalServerError)
return
}
}
// applyConfig applies the given cfg to reverseProxy.
//
// New config is applied only if non-nil error returned.
// Otherwise old config version is kept.
func (rp *reverseProxy) applyConfig(cfg *config.Config) error {
// configLock protects from concurrent calls to applyConfig
// by serializing such calls.
// configLock shouldn't be used in other places.
rp.configLock.Lock()
defer rp.configLock.Unlock()
clusters, err := newClusters(cfg.Clusters)
if err != nil {
return err
}
caches := make(map[string]*cache.Cache, len(cfg.Caches))
defer func() {
// caches is swapped with old caches from rp.caches
// on successful config reload - see the end of reloadConfig.
for _, tmpCache := range caches {
// Speed up applyConfig by closing caches in background,
// since the process of cache closing may be lengthy
// due to cleaning.
go tmpCache.Close()
}
}()
for _, cc := range cfg.Caches {
if _, ok := caches[cc.Name]; ok {
return fmt.Errorf("duplicate config for cache %q", cc.Name)
}
tmpCache, err := cache.New(cc)
if err != nil {
return fmt.Errorf("cannot initialize cache %q: %s", cc.Name, err)
}
caches[cc.Name] = tmpCache
}
params := make(map[string]*paramsRegistry, len(cfg.ParamGroups))
for _, p := range cfg.ParamGroups {
if _, ok := params[p.Name]; ok {
return fmt.Errorf("duplicate config for ParamGroups %q", p.Name)
}
params[p.Name], err = newParamsRegistry(p.Params)
if err != nil {
return fmt.Errorf("cannot initialize params %q: %s", p.Name, err)
}
}
profile := &usersProfile{
cfg: cfg.Users,
clusters: clusters,
caches: caches,
params: params,
}
users, err := profile.newUsers()
if err != nil {
return err
}
// New configs have been successfully prepared.
// Restart service goroutines with new configs.
// Stop the previous service goroutines.
close(rp.reloadSignal)
rp.reloadWG.Wait()
rp.reloadSignal = make(chan struct{})
// Reset metrics from the previous configs, which may become irrelevant
// with new configs.
// Counters and Summary metrics are always relevant.
// Gauge metrics may become irrelevant if they may freeze at non-zero
// value after config reload.
hostHealth.Reset()
cacheSize.Reset()
cacheItems.Reset()
// Start service goroutines with new configs.
for _, c := range clusters {
for _, r := range c.replicas {
for _, h := range r.hosts {
rp.reloadWG.Add(1)
go func(h *host) {
h.runHeartbeat(rp.reloadSignal)
rp.reloadWG.Done()
}(h)
}
}
for _, cu := range c.users {
rp.reloadWG.Add(1)
go func(cu *clusterUser) {
cu.rateLimiter.run(rp.reloadSignal)
rp.reloadWG.Done()
}(cu)
}
}
for _, u := range users {
rp.reloadWG.Add(1)
go func(u *user) {
u.rateLimiter.run(rp.reloadSignal)
rp.reloadWG.Done()
}(u)
}
// Substitute old configs with the new configs in rp.
// All the currently running requests will continue with old configs,
// while all the new requests will use new configs.
rp.lock.Lock()
rp.clusters = clusters
rp.users = users
// Swap is needed for deferred closing of old caches.
// See the code above where new caches are created.
caches, rp.caches = rp.caches, caches
rp.lock.Unlock()
return nil
}
// refreshCacheMetrics refreshes cacheSize and cacheItems metrics.
func (rp *reverseProxy) refreshCacheMetrics() {
rp.lock.RLock()
defer rp.lock.RUnlock()
for _, c := range rp.caches {
stats := c.Stats()
labels := prometheus.Labels{
"cache": c.Name,
}
cacheSize.With(labels).Set(float64(stats.Size))
cacheItems.With(labels).Set(float64(stats.Items))
}
}
func (rp *reverseProxy) getScope(req *http.Request) (*scope, int, error) {
name, password := getAuth(req)
sessionId := getSessionId(req)
sessionTimeout := getSessionTimeout(req)
var (
u *user
c *cluster
cu *clusterUser
)
rp.lock.RLock()
u = rp.users[name]
if u != nil {
// c and cu for toCluster and toUser must exist if applyConfig
// is correct.
// Fix applyConfig if c or cu equal to nil.
c = rp.clusters[u.toCluster]
cu = c.users[u.toUser]
}
rp.lock.RUnlock()
if u == nil {
return nil, http.StatusUnauthorized, fmt.Errorf("invalid username or password for user %q", name)
}
if u.password != password {
return nil, http.StatusUnauthorized, fmt.Errorf("invalid username or password for user %q", name)
}
if u.denyHTTP && req.TLS == nil {
return nil, http.StatusForbidden, fmt.Errorf("user %q is not allowed to access via http", u.name)
}
if u.denyHTTPS && req.TLS != nil {
return nil, http.StatusForbidden, fmt.Errorf("user %q is not allowed to access via https", u.name)
}
if !u.allowedNetworks.Contains(req.RemoteAddr) {
return nil, http.StatusForbidden, fmt.Errorf("user %q is not allowed to access", u.name)
}
if !cu.allowedNetworks.Contains(req.RemoteAddr) {
return nil, http.StatusForbidden, fmt.Errorf("cluster user %q is not allowed to access", cu.name)
}
s := newScope(req, u, c, cu, sessionId, sessionTimeout)
return s, 0, nil
}