From 130a3a72d6de498d70164debb411569dcb90dc0e Mon Sep 17 00:00:00 2001 From: Mihai Claudiu Toader Date: Thu, 29 Dec 2022 03:00:55 -0800 Subject: [PATCH] Revert "Don't cache non success responses" (#111) Revert "Don't cache non success responses (#109)" This reverts commit ff7d6b960dceadfd459d8f08c70f154e51c21d7b. --- monitoring.go | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/monitoring.go b/monitoring.go index c222d4877..588e14d93 100644 --- a/monitoring.go +++ b/monitoring.go @@ -3,12 +3,10 @@ package gosnowflake import ( - "bytes" "context" "database/sql/driver" "encoding/json" "fmt" - "io" "net/url" "runtime" "strconv" @@ -209,13 +207,14 @@ func (sc *snowflakeConn) getQueryResultResp( resultPath string, ) (*execResponse, error) { var cachedResponse *execResponse - if res, ok := sc.execRespCache.load(resultPath); ok { + cachedResponse = nil + if respd, ok := sc.execRespCache.load(resultPath); ok { + cachedResponse = respd // return the cached response, unless we pass the flag saying to // bypass the cache - if res.Success && !shouldSkipCache(ctx) { - return res, nil + if !shouldSkipCache(ctx) { + return respd, nil } - cachedResponse = res } headers := getHeaders() @@ -238,25 +237,12 @@ func (sc *snowflakeConn) getQueryResultResp( logger.WithContext(ctx).Errorf("failed to get response. err: %v", err) return nil, err } - if res.Body != nil { - defer func() { _ = res.Body.Close() }() - } - - bodyBytes, err := io.ReadAll(res.Body) - if err != nil { - return nil, err - } - var respd *execResponse - if err = json.NewDecoder(bytes.NewReader(bodyBytes)).Decode(&respd); err != nil { + if err = json.NewDecoder(res.Body).Decode(&respd); err != nil { logger.WithContext(ctx).Errorf("failed to decode JSON. err: %v", err) return nil, err } - if !respd.Success && respd.Code == "" && respd.Message == "" { - logger.WithContext(ctx).Errorf("failed to build a proper exec response. received body: %s", string(bodyBytes)) - } - // if we are skipping the cache, log difference between cached and non cached result if shouldSkipCache(ctx) { qid := respd.Data.QueryID @@ -264,6 +250,7 @@ func (sc *snowflakeConn) getQueryResultResp( // if there was no response in the cache anyway, log that and dont try to log anything else if cachedResponse == nil { logger.WithContext(ctx).Errorf("cached queryId: %v did not use cache", qid) + } else { // log if there are any differences in the arrow encooded first chunk arrowCached := cachedResponse.Data.RowSetBase64 @@ -297,10 +284,7 @@ func (sc *snowflakeConn) getQueryResultResp( } } - if respd.Success { - sc.execRespCache.store(resultPath, respd) - } - + sc.execRespCache.store(resultPath, respd) return respd, nil } @@ -355,10 +339,9 @@ func (sc *snowflakeConn) waitForCompletedQueryResultResp( if !response.Success { logEverything(ctx, qid, response, startTime) - } else { - sc.execRespCache.store(resultPath, response) } + sc.execRespCache.store(resultPath, response) return response, nil }