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

Support EVALSHA #21

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
75 changes: 35 additions & 40 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,7 @@
)

func (c *Client) luaGetBatch(ctx context.Context, keys []string, owner string) ([]interface{}, error) {
res, err := callLua(ctx, c.rdb, `-- luaGetBatch
local rets = {}
for i, key in ipairs(KEYS)
do
local v = redis.call('HGET', key, 'value')
local lu = redis.call('HGET', key, 'lockUntil')
if lu ~= false and tonumber(lu) < tonumber(ARGV[1]) or lu == false and v == false then
redis.call('HSET', key, 'lockUntil', ARGV[2])
redis.call('HSET', key, 'lockOwner', ARGV[3])
table.insert(rets, { v, 'LOCKED' })
else
table.insert(rets, {v, lu})
end
end
return rets
`, keys, []interface{}{now(), now() + int64(c.Options.LockExpire/time.Second), owner})
res, err := callLua(ctx, c.rdb, getBatchScript, keys, []interface{}{now(), now() + int64(c.Options.LockExpire/time.Second), owner})
debugf("luaGetBatch return: %v, %v", res, err)
if err != nil {
return nil, err
Expand All @@ -51,20 +36,7 @@
for _, ex := range expires {
vals = append(vals, ex)
}
_, err := callLua(ctx, c.rdb, `-- luaSetBatch
local n = #KEYS
for i, key in ipairs(KEYS)
do
local o = redis.call('HGET', key, 'lockOwner')
if o ~= ARGV[1] then
return
end
redis.call('HSET', key, 'value', ARGV[i+1])
redis.call('HDEL', key, 'lockUntil')
redis.call('HDEL', key, 'lockOwner')
redis.call('EXPIRE', key, ARGV[i+1+n])
end
`, keys, vals)
_, err := callLua(ctx, c.rdb, setBatchScript, keys, vals)
return err
}

Expand Down Expand Up @@ -192,10 +164,25 @@
go func(i int) {
defer wg.Done()
r, err := c.luaGet(ctx, keys[i], owner)
ticker := time.NewTimer(c.Options.LockSleep)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put ticker inside the for loop? Code can be cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reuse & reduce allocation

Copy link
Contributor

@yedf2 yedf2 Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The performance gained in this way should be very very little and can be ignored. Why not use the cleaner way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplified

defer ticker.Stop()
for err == nil && r[0] == nil && r[1].(string) != locked {
debugf("batch weak: empty result for %s locked by other, so sleep %s", keys[i], c.Options.LockSleep.String())
time.Sleep(c.Options.LockSleep)
select {
case <-ctx.Done():
ch <- pair{idx: i, err: ctx.Err()}
return

Check warning on line 174 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L172-L174

Added lines #L172 - L174 were not covered by tests
case <-ticker.C:
// equal to time.Sleep(c.Options.LockSleep) but can be canceled
}
r, err = c.luaGet(ctx, keys[i], owner)
// Reset ticker after luaGet
// If we reset ticker before luaGet, since luaGet takes a period of time,
// the actual sleep time will be shorter than expected
if !ticker.Stop() && len(ticker.C) > 0 {
<-ticker.C

Check warning on line 183 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L183

Added line #L183 was not covered by tests
}
ticker.Reset(c.Options.LockSleep)
}
if err != nil {
ch <- pair{idx: i, data: "", err: err}
Expand Down Expand Up @@ -302,10 +289,24 @@
go func(i int) {
defer wg.Done()
r, err := c.luaGet(ctx, keys[i], owner)
ticker := time.NewTimer(c.Options.LockSleep)
defer ticker.Stop()
for err == nil && r[1] != nil && r[1] != locked { // locked by other
debugf("batch: locked by other, so sleep %s", c.Options.LockSleep)
time.Sleep(c.Options.LockSleep)
select {
case <-ctx.Done():
ch <- pair{idx: i, err: ctx.Err()}

Check warning on line 298 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L297-L298

Added lines #L297 - L298 were not covered by tests
case <-ticker.C:
// equal to time.Sleep(c.Options.LockSleep) but can be canceled
}
r, err = c.luaGet(ctx, keys[i], owner)
// Reset ticker after luaGet
// If we reset ticker before luaGet, since luaGet takes a period of time,
// the actual sleep time will be shorter than expected
if !ticker.Stop() && len(ticker.C) > 0 {
<-ticker.C

Check warning on line 307 in batch.go

View check run for this annotation

Codecov / codecov/patch

batch.go#L307

Added line #L307 was not covered by tests
}
ticker.Reset(c.Options.LockSleep)
}
if err != nil {
ch <- pair{idx: i, data: "", err: err}
Expand Down Expand Up @@ -380,14 +381,8 @@
return nil
}
debugf("batch deleting: keys=%v", keys)
luaFn := func(con redisConn) error {
_, err := callLua(ctx, con, ` -- luaDeleteBatch
for i, key in ipairs(KEYS) do
redis.call('HSET', key, 'lockUntil', 0)
redis.call('HDEL', key, 'lockOwner')
redis.call('EXPIRE', key, ARGV[1])
end
`, keys, []interface{}{int64(c.Options.Delay / time.Second)})
luaFn := func(con redis.Scripter) error {
_, err := callLua(ctx, con, deleteBatchScript, keys, []interface{}{int64(c.Options.Delay / time.Second)})
return err
}
if c.Options.WaitReplicas > 0 {
Expand Down
85 changes: 39 additions & 46 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import (
"context"
"fmt"
"log"
"math"
"math/rand"
"time"
Expand Down Expand Up @@ -93,12 +94,8 @@
return nil
}
debugf("deleting: key=%s", key)
luaFn := func(con redisConn) error {
_, err := callLua(ctx, con, ` -- delete
redis.call('HSET', KEYS[1], 'lockUntil', 0)
redis.call('HDEL', KEYS[1], 'lockOwner')
redis.call('EXPIRE', KEYS[1], ARGV[1])
`, []string{key}, []interface{}{int64(c.Options.Delay / time.Second)})
luaFn := func(con redis.Scripter) error {
_, err := callLua(ctx, con, deleteScript, []string{key}, []interface{}{int64(c.Options.Delay / time.Second)})
return err
}
if c.Options.WaitReplicas > 0 {
Expand Down Expand Up @@ -141,16 +138,7 @@
}

func (c *Client) luaGet(ctx context.Context, key string, owner string) ([]interface{}, error) {
res, err := callLua(ctx, c.rdb, ` -- luaGet
local v = redis.call('HGET', KEYS[1], 'value')
local lu = redis.call('HGET', KEYS[1], 'lockUntil')
if lu ~= false and tonumber(lu) < tonumber(ARGV[1]) or lu == false and v == false then
redis.call('HSET', KEYS[1], 'lockUntil', ARGV[2])
redis.call('HSET', KEYS[1], 'lockOwner', ARGV[3])
return { v, 'LOCKED' }
end
return {v, lu}
`, []string{key}, []interface{}{now(), now() + int64(c.Options.LockExpire/time.Second), owner})
res, err := callLua(ctx, c.rdb, getScript, []string{key}, []interface{}{now(), now() + int64(c.Options.LockExpire/time.Second), owner})
debugf("luaGet return: %v, %v", res, err)
if err != nil {
return nil, err
Expand All @@ -159,16 +147,7 @@
}

func (c *Client) luaSet(ctx context.Context, key string, value string, expire int, owner string) error {
_, err := callLua(ctx, c.rdb, `-- luaSet
local o = redis.call('HGET', KEYS[1], 'lockOwner')
if o ~= ARGV[2] then
return
end
redis.call('HSET', KEYS[1], 'value', ARGV[1])
redis.call('HDEL', KEYS[1], 'lockUntil')
redis.call('HDEL', KEYS[1], 'lockOwner')
redis.call('EXPIRE', KEYS[1], ARGV[3])
`, []string{key}, []interface{}{value, owner, expire})
_, err := callLua(ctx, c.rdb, setScript, []string{key}, []interface{}{value, owner, expire})
return err
}

Expand All @@ -193,10 +172,25 @@
debugf("weakFetch: key=%s", key)
owner := shortuuid.New()
r, err := c.luaGet(ctx, key, owner)
ticker := time.NewTimer(c.Options.LockSleep)
defer ticker.Stop()
for err == nil && r[0] == nil && r[1].(string) != locked {
debugf("empty result for %s locked by other, so sleep %s", key, c.Options.LockSleep.String())
time.Sleep(c.Options.LockSleep)
select {
case <-ctx.Done():
return "", ctx.Err()

Check warning on line 181 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L180-L181

Added lines #L180 - L181 were not covered by tests
case <-ticker.C:
log.Printf("ticker")
// equal to time.Sleep(c.Options.LockSleep) but can be canceled
}
r, err = c.luaGet(ctx, key, owner)
// Reset ticker after luaGet
// If we reset ticker before luaGet, since luaGet takes a period of time,
// the actual sleep time will be shorter than expected
if !ticker.Stop() && len(ticker.C) > 0 {
<-ticker.C

Check warning on line 191 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L191

Added line #L191 was not covered by tests
}
ticker.Reset(c.Options.LockSleep)
}
if err != nil {
return "", err
Expand All @@ -217,10 +211,25 @@
debugf("strongFetch: key=%s", key)
owner := shortuuid.New()
r, err := c.luaGet(ctx, key, owner)
ticker := time.NewTimer(c.Options.LockSleep)
defer ticker.Stop()
for err == nil && r[1] != nil && r[1] != locked { // locked by other
debugf("locked by other, so sleep %s", c.Options.LockSleep)
time.Sleep(c.Options.LockSleep)
select {
case <-ctx.Done():
return "", ctx.Err()

Check warning on line 220 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L219-L220

Added lines #L219 - L220 were not covered by tests
case <-ticker.C:
log.Printf("ticker")
// equal to time.Sleep(c.Options.LockSleep) but can be canceled
}
r, err = c.luaGet(ctx, key, owner)
// Reset ticker after luaGet
// If we reset ticker before luaGet, since luaGet takes a period of time,
// the actual sleep time will be shorter than expected
if !ticker.Stop() && len(ticker.C) > 0 {
<-ticker.C

Check warning on line 230 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L230

Added line #L230 was not covered by tests
}
ticker.Reset(c.Options.LockSleep)
}
if err != nil {
return "", err
Expand Down Expand Up @@ -248,16 +257,7 @@
// LockForUpdate locks the key, used in very strict strong consistency mode
func (c *Client) LockForUpdate(ctx context.Context, key string, owner string) error {
lockUntil := math.Pow10(10)
res, err := callLua(ctx, c.rdb, ` -- luaLock
local lu = redis.call('HGET', KEYS[1], 'lockUntil')
local lo = redis.call('HGET', KEYS[1], 'lockOwner')
if lu == false or tonumber(lu) < tonumber(ARGV[2]) or lo == ARGV[1] then
redis.call('HSET', KEYS[1], 'lockUntil', ARGV[2])
redis.call('HSET', KEYS[1], 'lockOwner', ARGV[1])
return 'LOCKED'
end
return lo
`, []string{key}, []interface{}{owner, lockUntil})
res, err := callLua(ctx, c.rdb, lockScript, []string{key}, []interface{}{owner, lockUntil})
if err == nil && res != "LOCKED" {
return fmt.Errorf("%s has been locked by %s", key, res)
}
Expand All @@ -266,13 +266,6 @@

// UnlockForUpdate unlocks the key, used in very strict strong consistency mode
func (c *Client) UnlockForUpdate(ctx context.Context, key string, owner string) error {
_, err := callLua(ctx, c.rdb, ` -- luaUnlock
local lo = redis.call('HGET', KEYS[1], 'lockOwner')
if lo == ARGV[1] then
redis.call('HSET', KEYS[1], 'lockUntil', 0)
redis.call('HDEL', KEYS[1], 'lockOwner')
redis.call('EXPIRE', KEYS[1], ARGV[2])
end
`, []string{key}, []interface{}{owner, c.Options.LockExpire / time.Second})
_, err := callLua(ctx, c.rdb, unlockScript, []string{key}, []interface{}{owner, c.Options.LockExpire / time.Second})
return err
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/lithammer/shortuuid v3.0.0+incompatible
github.com/redis/go-redis/v9 v9.0.3
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.1.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
77 changes: 77 additions & 0 deletions script.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package rockscache

import "github.com/redis/go-redis/v9"

var (
deleteScript = redis.NewScript(`redis.call('HSET', KEYS[1], 'lockUntil', 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a new line at the first of the script? That may make the code more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

redis.call('HDEL', KEYS[1], 'lockOwner')
redis.call('EXPIRE', KEYS[1], ARGV[1])`)

getScript = redis.NewScript(`local v = redis.call('HGET', KEYS[1], 'value')
local lu = redis.call('HGET', KEYS[1], 'lockUntil')
if lu ~= false and tonumber(lu) < tonumber(ARGV[1]) or lu == false and v == false then
redis.call('HSET', KEYS[1], 'lockUntil', ARGV[2])
redis.call('HSET', KEYS[1], 'lockOwner', ARGV[3])
return { v, 'LOCKED' }
end
return {v, lu}`)

setScript = redis.NewScript(`local o = redis.call('HGET', KEYS[1], 'lockOwner')
if o ~= ARGV[2] then
return
end
redis.call('HSET', KEYS[1], 'value', ARGV[1])
redis.call('HDEL', KEYS[1], 'lockUntil')
redis.call('HDEL', KEYS[1], 'lockOwner')
redis.call('EXPIRE', KEYS[1], ARGV[3])`)

lockScript = redis.NewScript(`local lu = redis.call('HGET', KEYS[1], 'lockUntil')
local lo = redis.call('HGET', KEYS[1], 'lockOwner')
if lu == false or tonumber(lu) < tonumber(ARGV[2]) or lo == ARGV[1] then
redis.call('HSET', KEYS[1], 'lockUntil', ARGV[2])
redis.call('HSET', KEYS[1], 'lockOwner', ARGV[1])
return 'LOCKED'
end
return lo`)

unlockScript = redis.NewScript(`local lo = redis.call('HGET', KEYS[1], 'lockOwner')
if lo == ARGV[1] then
redis.call('HSET', KEYS[1], 'lockUntil', 0)
redis.call('HDEL', KEYS[1], 'lockOwner')
redis.call('EXPIRE', KEYS[1], ARGV[2])
end`)

getBatchScript = redis.NewScript(`local rets = {}
for i, key in ipairs(KEYS)
do
local v = redis.call('HGET', key, 'value')
local lu = redis.call('HGET', key, 'lockUntil')
if lu ~= false and tonumber(lu) < tonumber(ARGV[1]) or lu == false and v == false then
redis.call('HSET', key, 'lockUntil', ARGV[2])
redis.call('HSET', key, 'lockOwner', ARGV[3])
table.insert(rets, { v, 'LOCKED' })
else
table.insert(rets, {v, lu})
end
end
return rets`)

setBatchScript = redis.NewScript(`local n = #KEYS
for i, key in ipairs(KEYS)
do
local o = redis.call('HGET', key, 'lockOwner')
if o ~= ARGV[1] then
return
end
redis.call('HSET', key, 'value', ARGV[i+1])
redis.call('HDEL', key, 'lockUntil')
redis.call('HDEL', key, 'lockOwner')
redis.call('EXPIRE', key, ARGV[i+1+n])
end`)

deleteBatchScript = redis.NewScript(`for i, key in ipairs(KEYS) do
redis.call('HSET', key, 'lockUntil', 0)
redis.call('HDEL', key, 'lockOwner')
redis.call('EXPIRE', key, ARGV[1])
end`)
)
20 changes: 13 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@
return time.Now().Unix()
}

type redisConn interface {
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
}

func callLua(ctx context.Context, rdb redisConn, script string, keys []string, args []interface{}) (interface{}, error) {
debugf("callLua: script=%s, keys=%v, args=%v", script, keys, args)
v, err := rdb.Eval(ctx, script, keys, args).Result()
func callLua(ctx context.Context, rdb redis.Scripter, script *redis.Script, keys []string, args []interface{}) (interface{}, error) {
debugf("callLua: script=%s, keys=%v, args=%v", script.Hash(), keys, args)
r := script.EvalSha(ctx, rdb, keys, args)
if redis.HasErrorPrefix(r.Err(), "NOSCRIPT") {
// try load script
if err := script.Load(ctx, rdb).Err(); err != nil {
debugf("callLua: load script failed: %v", err)
r = script.Eval(ctx, rdb, keys, args) // fallback to EVAL

Check warning on line 35 in utils.go

View check run for this annotation

Codecov / codecov/patch

utils.go#L34-L35

Added lines #L34 - L35 were not covered by tests
} else {
r = script.EvalSha(ctx, rdb, keys, args) // retry EVALSHA
}
}
v, err := r.Result()
if err == redis.Nil {
err = nil
}
Expand Down
Loading