forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
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
boost: decode from redis and save to redis #15
Merged
DeathBorn
merged 1 commit into
origin/vinted/11-0-4-backport-11-boost
from
vinted/zygis/encode-boosted-queries-save-redis
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ package vtgate | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
"vitess.io/vitess/go/cache/redis" | ||
|
@@ -123,27 +124,74 @@ func (e *Executor) newExecute(ctx context.Context, safeSession *SafeSession, sql | |
e.executePlan(ctx, plan, vcursor, bindVars, execStart)) | ||
} | ||
|
||
// Check if boosted and hit Redis | ||
|
||
if plan.BoostPlanConfig != nil && plan.BoostPlanConfig.IsBoosted { | ||
cacheKey := cacheKey(plan.BoostPlanConfig, bindVars) | ||
cacheKey := getCacheKey(plan.BoostPlanConfig, bindVars) | ||
cacheHit, decodedResults := e.fetchFromRedis(cacheKey) | ||
|
||
if cacheHit { | ||
return sqlparser.StmtSelect, decodedResults, nil | ||
} else { | ||
stmt, sqlResult, err1 := e.executePlan(ctx, plan, vcursor, bindVars, execStart)(logStats, safeSession) | ||
go e.setBoostCache(sqlResult, cacheKey) | ||
|
||
return stmt, sqlResult, err1 | ||
} | ||
} | ||
|
||
fmt.Println("Cache Key: ", cacheKey) | ||
return e.executePlan(ctx, plan, vcursor, bindVars, execStart)(logStats, safeSession) | ||
} | ||
|
||
redisResults, err := e.boostCache.Get(cacheKey) | ||
func (e *Executor) setBoostCache(sqlResult *sqltypes.Result, cacheKey string) { | ||
encodedResults, _ := e.encodeResults(sqlResult) | ||
e.boostCache.Set(cacheKey, encodedResults) | ||
} | ||
|
||
func (e *Executor) fetchFromRedis(cacheKey string) (bool, *sqltypes.Result) { | ||
redisResults, err := e.boostCache.Get(cacheKey) | ||
var cacheHit bool | ||
|
||
var decodeErr error | ||
var decodedResults *sqltypes.Result | ||
|
||
if redisResults == "" || err != nil { | ||
cacheHit = false | ||
} else { | ||
decodedResults, decodeErr = e.decodeResults(redisResults) | ||
|
||
fmt.Println("Redis Results: ", redisResults) | ||
fmt.Println("Error: ", err) | ||
if decodeErr != nil { | ||
fmt.Printf("Failed to decode: %v\n", decodeErr) | ||
cacheHit = false | ||
} | ||
|
||
cacheHit = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will be reached always |
||
} | ||
return cacheHit, decodedResults | ||
} | ||
|
||
func (e *Executor) encodeResults(result *sqltypes.Result) (string, error) { | ||
jsonData, err := json.Marshal(result) | ||
if err != nil { | ||
fmt.Printf("Failed to encode: %v\n", err) | ||
return "", err | ||
} | ||
|
||
statementTypeResult, sqlResult, err := e.executePlan(ctx, plan, vcursor, bindVars, execStart)(logStats, safeSession) | ||
jsonString := string(jsonData) | ||
|
||
return jsonString, nil | ||
} | ||
|
||
// Maybe store in Redis here if boosted, but cache miss | ||
func (e *Executor) decodeResults(redisResults string) (*sqltypes.Result, error) { | ||
var result sqltypes.Result | ||
err := json.Unmarshal([]byte(redisResults), &result) | ||
if err != nil { | ||
fmt.Printf("Failed to decode: %v\n", err) | ||
return nil, err | ||
} | ||
|
||
return statementTypeResult, sqlResult, err | ||
return &result, nil | ||
} | ||
|
||
func cacheKey(config *boost.PlanConfig, vars map[string]*querypb.BindVariable) string { | ||
func getCacheKey(config *boost.PlanConfig, vars map[string]*querypb.BindVariable) string { | ||
return redis.GenerateCacheKey(cacheKeyParams(config, vars)...) | ||
} | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets log error at least