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

boost: decode from redis and save to redis #15

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions go/vt/vtgate/plan_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package vtgate

import (
"context"
"encoding/json"
"fmt"
"time"
"vitess.io/vitess/go/cache/redis"
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

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

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)...)
}

Expand Down
Loading