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

Fix/redis reacquire #4325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions core/stores/redis/lockscript.lua

This file was deleted.

36 changes: 15 additions & 21 deletions core/stores/redis/redislock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
_ "embed"
"errors"
"math/rand"
"strconv"
"sync/atomic"
"time"

Expand All @@ -15,16 +14,10 @@ import (
)

const (
randomLen = 16
tolerance = 500 // milliseconds
millisPerSecond = 1000
randomLen = 16
)

var (
//go:embed lockscript.lua
lockLuaScript string
lockScript = NewScript(lockLuaScript)

//go:embed delscript.lua
delLuaScript string
delScript = NewScript(delLuaScript)
Expand Down Expand Up @@ -58,26 +51,27 @@ func (rl *RedisLock) Acquire() (bool, error) {

// AcquireCtx acquires the lock with the given ctx.
func (rl *RedisLock) AcquireCtx(ctx context.Context) (bool, error) {
seconds := atomic.LoadUint32(&rl.seconds)
resp, err := rl.store.ScriptRunCtx(ctx, lockScript, []string{rl.key}, []string{
rl.id, strconv.Itoa(int(seconds)*millisPerSecond + tolerance),
})

var (
seconds = atomic.LoadUint32(&rl.seconds)
res bool
err error
)

if seconds == 0 {
res, err = rl.store.SetnxCtx(ctx, rl.key, rl.id)
} else {
res, err = rl.store.SetnxExCtx(ctx, rl.key, rl.id, int(seconds))
}

if errors.Is(err, red.Nil) {
return false, nil
} else if err != nil {
logx.Errorf("Error on acquiring lock for %s, %s", rl.key, err.Error())
return false, err
} else if resp == nil {
return false, nil
}

reply, ok := resp.(string)
if ok && reply == "OK" {
return true, nil
}

logx.Errorf("Unknown reply when acquiring lock for %s: %v", rl.key, resp)
return false, nil
return res, nil
}

// Release releases the lock.
Expand Down
87 changes: 64 additions & 23 deletions core/stores/redis/redislock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,45 @@ package redis
import (
"context"
"testing"
"time"

"github.com/alicebob/miniredis/v2"
"github.com/stretchr/testify/assert"

"github.com/zeromicro/go-zero/core/stringx"
)

func TestRedisLock_SameAcquire(t *testing.T) {

var (
s = miniredis.RunT(t)
seconds = 5
)
client := MustNewRedis(
RedisConf{
Host: s.Addr(),
Type: NodeType,
},
)

key := stringx.Rand()
firstLock := NewRedisLock(client, key)
firstLock.SetExpire(seconds)
firstAcquire, err := firstLock.Acquire()
assert.Nil(t, err)
assert.True(t, firstAcquire)

secondAcquire, err := firstLock.Acquire()
assert.Nil(t, err)
assert.False(t, secondAcquire)

s.FastForward(time.Second * time.Duration(seconds+1))

thirdAcquire, err := firstLock.Acquire()
assert.Nil(t, err)
assert.True(t, thirdAcquire)
}

func TestRedisLock(t *testing.T) {
testFn := func(ctx context.Context) func(client *Redis) {
return func(client *Redis) {
Expand All @@ -35,31 +68,39 @@ func TestRedisLock(t *testing.T) {
}
}

t.Run("normal", func(t *testing.T) {
runOnRedis(t, testFn(nil))
})
t.Run(
"normal", func(t *testing.T) {
runOnRedis(t, testFn(nil))
},
)

t.Run("withContext", func(t *testing.T) {
runOnRedis(t, testFn(context.Background()))
})
t.Run(
"withContext", func(t *testing.T) {
runOnRedis(t, testFn(context.Background()))
},
)
}

func TestRedisLock_Expired(t *testing.T) {
runOnRedis(t, func(client *Redis) {
key := stringx.Rand()
redisLock := NewRedisLock(client, key)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := redisLock.AcquireCtx(ctx)
assert.NotNil(t, err)
})

runOnRedis(t, func(client *Redis) {
key := stringx.Rand()
redisLock := NewRedisLock(client, key)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := redisLock.ReleaseCtx(ctx)
assert.NotNil(t, err)
})
runOnRedis(
t, func(client *Redis) {
key := stringx.Rand()
redisLock := NewRedisLock(client, key)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := redisLock.AcquireCtx(ctx)
assert.NotNil(t, err)
},
)

runOnRedis(
t, func(client *Redis) {
key := stringx.Rand()
redisLock := NewRedisLock(client, key)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := redisLock.ReleaseCtx(ctx)
assert.NotNil(t, err)
},
)
}