Skip to content

Commit 05f4f59

Browse files
authored
fix(proxy): return Clickhouse error message when query fails (#192)
1 parent 6d45135 commit 05f4f59

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

cache/async_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func NewAsyncCache(cfg config.Cache, maxExecutionTime time.Duration) (*AsyncCach
9494
var redisClient redis.UniversalClient
9595
redisClient, err = clients.NewRedisClient(cfg.Redis)
9696
cache = newRedisCache(redisClient, cfg)
97-
transaction = newRedisTransactionRegistry(redisClient, time.Duration(cfg.GraceTime))
97+
transaction = newRedisTransactionRegistry(redisClient, transactionDeadline)
9898
default:
9999
return nil, fmt.Errorf("unknown config mode")
100100
}

io.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ func RespondWithData(rw http.ResponseWriter, data io.Reader, metadata cache.Cont
5252
return nil
5353
}
5454

55-
func RespondWithoutData(rw http.ResponseWriter) error {
56-
_, err := rw.Write([]byte{})
57-
return err
58-
}
59-
6055
func (rw *statResponseWriter) Write(b []byte) (int, error) {
6156
if rw.statusCode == 0 {
6257
rw.statusCode = http.StatusOK

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ func TestServe(t *testing.T) {
625625
// scenario: 1st query fails before grace_time elapsed. 2nd query fails as well.
626626

627627
q := "SELECT ERROR"
628-
executeTwoConcurrentRequests(t, q, http.StatusTeapot, http.StatusInternalServerError, "", "concurrent query failed")
628+
executeTwoConcurrentRequests(t, q, http.StatusTeapot, http.StatusInternalServerError, "DB::Exception\n", "concurrent query failed")
629629
},
630630
startHTTP,
631631
},

proxy.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,13 @@ func (rp *reverseProxy) serveFromCache(s *scope, srw *statResponseWriter, req *h
314314

315315
// proxy request and capture response along with headers to [[BufferedResponseWriter]]
316316
rp.proxyRequest(s, bufferedRespWriter, srw, req)
317+
318+
contentEncoding := bufferedRespWriter.GetCapturedContentEncoding()
319+
contentType := bufferedRespWriter.GetCapturedContentType()
320+
contentLength := bufferedRespWriter.GetCapturedContentLength()
321+
reader := bufferedRespWriter.Reader()
322+
contentMetadata := cache.ContentMetadata{Length: contentLength, Encoding: contentEncoding, Type: contentType}
323+
317324
if bufferedRespWriter.StatusCode() != http.StatusOK || s.canceled {
318325
// Do not cache non-200 or cancelled responses.
319326
// Restore the original status code by proxyRequest if it was set.
@@ -327,22 +334,20 @@ func (rp *reverseProxy) serveFromCache(s *scope, srw *statResponseWriter, req *h
327334
if err = userCache.Fail(key); err != nil {
328335
log.Errorf("%s: %s; query: %q", s, err, q)
329336
}
330-
err = RespondWithoutData(srw)
337+
338+
err = RespondWithData(srw, reader, contentMetadata, 0*time.Second, bufferedRespWriter.StatusCode())
331339
if err != nil {
332-
log.Errorf("%s: %s; query: %q - failed to put response in the cache", s, err, q)
340+
err = fmt.Errorf("%s: %w; query: %q", s, err, q)
341+
respondWith(srw, err, http.StatusInternalServerError)
342+
return
333343
}
334344
} else {
335345
cacheMiss.With(labels).Inc()
336346
log.Debugf("%s: cache miss", s)
337-
contentEncoding := bufferedRespWriter.GetCapturedContentEncoding()
338-
contentType := bufferedRespWriter.GetCapturedContentType()
339-
contentLength := bufferedRespWriter.GetCapturedContentLength()
340-
reader := bufferedRespWriter.Reader()
341347

342348
// we create this buffer to be able to stream data both to cache as well as to an end user
343349
var buf bytes.Buffer
344350
tee := io.TeeReader(reader, &buf)
345-
contentMetadata := cache.ContentMetadata{Length: contentLength, Encoding: contentEncoding, Type: contentType}
346351
expiration, err := userCache.Put(tee, contentMetadata, key)
347352
if err != nil {
348353
log.Errorf("%s: %s; query: %q - failed to put response in the cache", s, err, q)

0 commit comments

Comments
 (0)