-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support multi commands transaction in cluster mode
- Loading branch information
Showing
14 changed files
with
312 additions
and
34 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cluster | ||
|
||
import ( | ||
"github.com/hdt3213/godis" | ||
"github.com/hdt3213/godis/interface/redis" | ||
"github.com/hdt3213/godis/redis/reply" | ||
) | ||
|
||
const relayMulti = "_multi" | ||
|
||
var relayMultiBytes = []byte(relayMulti) | ||
|
||
// cmdLine == []string{"exec"} | ||
func execMulti(cluster *Cluster, conn redis.Connection, cmdLine CmdLine) redis.Reply { | ||
if !conn.InMultiState() { | ||
return reply.MakeErrReply("ERR EXEC without MULTI") | ||
} | ||
defer conn.SetMultiState(false) | ||
cmdLines := conn.GetQueuedCmdLine() | ||
|
||
// analysis related keys | ||
keys := make([]string, 0) // may contains duplicate | ||
for _, cl := range cmdLines { | ||
wKeys, rKeys := cluster.db.GetRelatedKeys(cl) | ||
keys = append(keys, wKeys...) | ||
keys = append(keys, rKeys...) | ||
} | ||
if len(keys) == 0 { | ||
// empty transaction or only `PING`s | ||
return godis.ExecMulti(cluster.db, cmdLines) | ||
} | ||
groupMap := cluster.groupBy(keys) | ||
if len(groupMap) > 1 { | ||
return reply.MakeErrReply("ERR MULTI commands transaction must within one slot in cluster mode") | ||
} | ||
var peer string | ||
// assert len(groupMap) == 1 | ||
for p := range groupMap { | ||
peer = p | ||
} | ||
|
||
// out parser not support reply.MultiRawReply, so we have to encode it | ||
if peer == cluster.self { | ||
return godis.ExecMulti(cluster.db, cmdLines) | ||
} | ||
return execMultiOnOtherNode(cluster, conn, peer, cmdLines) | ||
} | ||
|
||
func execMultiOnOtherNode(cluster *Cluster, conn redis.Connection, peer string, cmdLines []CmdLine) redis.Reply { | ||
defer func() { | ||
conn.ClearQueuedCmds() | ||
conn.SetMultiState(false) | ||
}() | ||
relayCmdLine := [][]byte{ // relay it to executing node | ||
relayMultiBytes, | ||
} | ||
relayCmdLine = append(relayCmdLine, encodeCmdLine(cmdLines)...) | ||
rawRelayResult := cluster.relay(peer, conn, relayCmdLine) | ||
if reply.IsErrorReply(rawRelayResult) { | ||
return rawRelayResult | ||
} | ||
relayResult, ok := rawRelayResult.(*reply.MultiBulkReply) | ||
if !ok { | ||
return reply.MakeErrReply("execute failed") | ||
} | ||
rep, err := parseEncodedMultiRawReply(relayResult.Args) | ||
if err != nil { | ||
return reply.MakeErrReply(err.Error()) | ||
} | ||
return rep | ||
} | ||
|
||
// execRelayedMulti execute relayed multi commands transaction | ||
// cmdLine format: _multi base64ed-cmdLine | ||
// result format: base64ed-reply list | ||
func execRelayedMulti(cluster *Cluster, conn redis.Connection, cmdLine CmdLine) redis.Reply { | ||
decoded, err := parseEncodedMultiRawReply(cmdLine[1:]) | ||
if err != nil { | ||
return reply.MakeErrReply(err.Error()) | ||
} | ||
var cmdLines []CmdLine | ||
for _, rep := range decoded.Replies { | ||
mbr, ok := rep.(*reply.MultiBulkReply) | ||
if !ok { | ||
return reply.MakeErrReply("exec failed") | ||
} | ||
cmdLines = append(cmdLines, mbr.Args) | ||
} | ||
rawResult := godis.ExecMulti(cluster.db, cmdLines) | ||
resultMBR, ok := rawResult.(*reply.MultiRawReply) | ||
if !ok { | ||
return reply.MakeErrReply("exec failed") | ||
} | ||
return encodeMultiRawReply(resultMBR) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cluster | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"github.com/hdt3213/godis/redis/parser" | ||
"github.com/hdt3213/godis/redis/reply" | ||
) | ||
|
||
func encodeCmdLine(cmdLines []CmdLine) [][]byte { | ||
var result [][]byte | ||
for _, line := range cmdLines { | ||
raw := reply.MakeMultiBulkReply(line).ToBytes() | ||
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(raw))) | ||
base64.StdEncoding.Encode(encoded, raw) | ||
result = append(result, encoded) | ||
} | ||
return result | ||
} | ||
|
||
func parseEncodedMultiRawReply(args [][]byte) (*reply.MultiRawReply, error) { | ||
cmdBuf := new(bytes.Buffer) | ||
for _, arg := range args { | ||
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(arg))) | ||
n, err := base64.StdEncoding.Decode(dbuf, arg) | ||
if err != nil { | ||
continue | ||
} | ||
cmdBuf.Write(dbuf[:n]) | ||
} | ||
cmds, err := parser.ParseBytes(cmdBuf.Bytes()) | ||
if err != nil { | ||
return nil, reply.MakeErrReply(err.Error()) | ||
} | ||
return reply.MakeMultiRawReply(cmds), nil | ||
} | ||
|
||
func encodeMultiRawReply(src *reply.MultiRawReply) *reply.MultiBulkReply { | ||
args := make([][]byte, 0, len(src.Replies)) | ||
for _, rep := range src.Replies { | ||
raw := rep.ToBytes() | ||
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(raw))) | ||
base64.StdEncoding.Encode(encoded, raw) | ||
args = append(args, encoded) | ||
} | ||
return reply.MakeMultiBulkReply(args) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cluster | ||
|
||
import ( | ||
"github.com/hdt3213/godis/lib/utils" | ||
"github.com/hdt3213/godis/redis/connection" | ||
"github.com/hdt3213/godis/redis/reply" | ||
"github.com/hdt3213/godis/redis/reply/asserts" | ||
"testing" | ||
) | ||
|
||
func TestMultiExecOnSelf(t *testing.T) { | ||
testCluster.db.Flush() | ||
conn := new(connection.FakeConn) | ||
result := testCluster.Exec(conn, toArgs("MULTI")) | ||
asserts.AssertNotError(t, result) | ||
key := utils.RandString(10) | ||
value := utils.RandString(10) | ||
testCluster.Exec(conn, utils.ToCmdLine("set", key, value)) | ||
key2 := utils.RandString(10) | ||
testCluster.Exec(conn, utils.ToCmdLine("rpush", key2, value)) | ||
result = testCluster.Exec(conn, utils.ToCmdLine("exec")) | ||
asserts.AssertNotError(t, result) | ||
result = testCluster.Exec(conn, utils.ToCmdLine("get", key)) | ||
asserts.AssertBulkReply(t, result, value) | ||
result = testCluster.Exec(conn, utils.ToCmdLine("lrange", key2, "0", "-1")) | ||
asserts.AssertMultiBulkReply(t, result, []string{value}) | ||
} | ||
|
||
func TestEmptyMulti(t *testing.T) { | ||
testCluster.db.Flush() | ||
conn := new(connection.FakeConn) | ||
result := testCluster.Exec(conn, toArgs("MULTI")) | ||
asserts.AssertNotError(t, result) | ||
result = testCluster.Exec(conn, utils.ToCmdLine("PING")) | ||
asserts.AssertNotError(t, result) | ||
result = testCluster.Exec(conn, utils.ToCmdLine("EXEC")) | ||
asserts.AssertNotError(t, result) | ||
mbr := result.(*reply.MultiRawReply) | ||
asserts.AssertStatusReply(t, mbr.Replies[0], "PONG") | ||
} | ||
|
||
func TestMultiExecOnOthers(t *testing.T) { | ||
testCluster.db.Flush() | ||
conn := new(connection.FakeConn) | ||
result := testCluster.Exec(conn, toArgs("MULTI")) | ||
asserts.AssertNotError(t, result) | ||
key := utils.RandString(10) | ||
value := utils.RandString(10) | ||
testCluster.Exec(conn, utils.ToCmdLine("rpush", key, value)) | ||
testCluster.Exec(conn, utils.ToCmdLine("lrange", key, "0", "-1")) | ||
|
||
cmdLines := conn.GetQueuedCmdLine() | ||
relayCmdLine := [][]byte{ // relay it to executing node | ||
relayMultiBytes, | ||
} | ||
relayCmdLine = append(relayCmdLine, encodeCmdLine(cmdLines)...) | ||
rawRelayResult := execRelayedMulti(testCluster, conn, relayCmdLine) | ||
if reply.IsErrorReply(rawRelayResult) { | ||
t.Error() | ||
} | ||
relayResult, ok := rawRelayResult.(*reply.MultiBulkReply) | ||
if !ok { | ||
t.Error() | ||
} | ||
rep, err := parseEncodedMultiRawReply(relayResult.Args) | ||
if err != nil { | ||
t.Error() | ||
} | ||
if len(rep.Replies) != 2 { | ||
t.Errorf("expect 2 replies actual %d", len(rep.Replies)) | ||
} | ||
asserts.AssertMultiBulkReply(t, rep.Replies[1], []string{value}) | ||
} |
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
File renamed without changes.
File renamed without changes.
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
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
Oops, something went wrong.