Skip to content

Commit

Permalink
support routing the FCALL and FCALL_RO commands
Browse files Browse the repository at this point in the history
  • Loading branch information
stlava committed Apr 11, 2024
1 parent d865d6d commit e500e4c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion redis/command_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var replicaSafe = makeSet(strings.Split(
"SCARD SDIFF SINTER SISMEMBER SMEMBERS SRANDMEMBER STRLEN SUNION "+
"ZCARD ZCOUNT ZLEXCOUNT ZRANGE ZRANGEBYLEX ZREVRANGEBYLEX "+
"ZRANGEBYSCORE ZRANK ZREVRANGE ZREVRANGEBYSCORE ZREVRANK ZSCORE "+
"XPENDING XREVRANGE XREAD XLEN ", " "))
"XPENDING XREVRANGE XREAD XLEN FCALL_RO", " "))

// ReplicaSafe returns true if command is readonly and "safe to run on replica".
// Some commands like "scan" are not included, because their result could differ between
Expand Down
4 changes: 4 additions & 0 deletions redis/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (r Request) Key() (string, bool) {
}
var n int
switch r.Cmd {
case "FCALL", "FCALL_RO":
n = 2
case "EVAL", "EVALSHA":
n = 2
case "BITOP":
Expand All @@ -63,6 +65,8 @@ func (r Request) KeyByte() ([]byte, bool) {
}
var n int
switch r.Cmd {
case "FCALL", "FCALL_RO":
n = 2
case "EVAL", "EVALSHA":
n = 2
case "BITOP":
Expand Down
48 changes: 48 additions & 0 deletions redis/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,59 @@ func TestRequestKey(t *testing.T) {
assert.Equal(t, "2", k)
assert.True(t, ok)

k, ok = Req("FCALL", "foo", 1, 2, 3).Key()
assert.Equal(t, "2", k)
assert.True(t, ok)

k, ok = Req("FCALL_RO", "foo", 1, 2, 3).Key()
assert.Equal(t, "2", k)
assert.True(t, ok)

k, ok = Req("BITOP", "AND", 1, 2).Key()
assert.Equal(t, "1", k)
assert.True(t, ok)
}

func TestRequestKeyByte(t *testing.T) {
var k []byte
var ok bool

k, ok = Req("GET", 1).KeyByte()
assert.Equal(t, []byte("1"), k)
assert.True(t, ok)

_, ok = Req("GET").Key()
assert.False(t, ok)

k, ok = Req("SET", 1, 2).KeyByte()
assert.Equal(t, []byte("1"), k)
assert.True(t, ok)

k, ok = Req("RANDOMKEY").KeyByte()
assert.Equal(t, []byte("RANDOMKEY"), k)
assert.False(t, ok)

k, ok = Req("EVAL", "return KEY[1]", 1, 2, 3).KeyByte()
assert.Equal(t, []byte("2"), k)
assert.True(t, ok)

k, ok = Req("EVALSHA", "1234abcdef", 1, 2, 3).KeyByte()
assert.Equal(t, []byte("2"), k)
assert.True(t, ok)

k, ok = Req("FCALL", "foo", 1, 2, 3).KeyByte()
assert.Equal(t, []byte("2"), k)
assert.True(t, ok)

k, ok = Req("FCALL_RO", "foo", 1, 2, 3).KeyByte()
assert.Equal(t, []byte("2"), k)
assert.True(t, ok)

k, ok = Req("BITOP", "AND", 1, 2).KeyByte()
assert.Equal(t, []byte("1"), k)
assert.True(t, ok)
}

func TestArgToString(t *testing.T) {
var k string
var ok bool
Expand Down

0 comments on commit e500e4c

Please sign in to comment.