Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't cache non success responses #109

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 26 additions & 9 deletions monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
package gosnowflake

import (
"bytes"
"context"
"database/sql/driver"
"encoding/json"
"fmt"
"io"
"net/url"
"runtime"
"strconv"
Expand Down Expand Up @@ -200,14 +202,13 @@ func (sc *snowflakeConn) getQueryResultResp(
resultPath string,
) (*execResponse, error) {
var cachedResponse *execResponse
cachedResponse = nil
if respd, ok := sc.execRespCache.load(resultPath); ok {
cachedResponse = respd
if res, ok := sc.execRespCache.load(resultPath); ok {
// return the cached response, unless we pass the flag saying to
// bypass the cache
if !shouldSkipCache(ctx) {
return respd, nil
if res.Success && !shouldSkipCache(ctx) {
return res, nil
}
cachedResponse = res
}

headers := getHeaders()
Expand All @@ -228,20 +229,32 @@ 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(res.Body).Decode(&respd); err != nil {
if err = json.NewDecoder(bytes.NewReader(bodyBytes)).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

// 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
Expand Down Expand Up @@ -275,7 +288,10 @@ func (sc *snowflakeConn) getQueryResultResp(
}
}

sc.execRespCache.store(resultPath, respd)
if respd.Success {
sc.execRespCache.store(resultPath, respd)
}

return respd, nil
}

Expand Down Expand Up @@ -330,9 +346,10 @@ 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
}

Expand Down