forked from HDT3213/godis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti.go
151 lines (139 loc) · 4.23 KB
/
multi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package godis
import (
"github.com/hdt3213/godis/datastruct/set"
"github.com/hdt3213/godis/interface/redis"
"github.com/hdt3213/godis/redis/reply"
"strings"
)
var forbiddenInMulti = set.Make(
"flushdb",
"flushall",
)
// Watch set watching keys
func Watch(db *DB, conn redis.Connection, args [][]byte) redis.Reply {
watching := conn.GetWatching()
for _, bkey := range args {
key := string(bkey)
watching[key] = db.GetVersion(key)
}
return reply.MakeOkReply()
}
func execGetVersion(db *DB, args [][]byte) redis.Reply {
key := string(args[0])
ver := db.GetVersion(key)
return reply.MakeIntReply(int64(ver))
}
func init() {
RegisterCommand("GetVer", execGetVersion, readAllKeys, nil, 2)
}
// invoker should lock watching keys
func isWatchingChanged(db *DB, watching map[string]uint32) bool {
for key, ver := range watching {
currentVersion := db.GetVersion(key)
if ver != currentVersion {
return true
}
}
return false
}
// StartMulti starts multi-command-transaction
func StartMulti(db *DB, conn redis.Connection) redis.Reply {
if conn.InMultiState() {
return reply.MakeErrReply("ERR MULTI calls can not be nested")
}
conn.SetMultiState(true)
return reply.MakeOkReply()
}
// EnqueueCmd puts command line into `multi` pending queue
func EnqueueCmd(db *DB, conn redis.Connection, cmdLine [][]byte) redis.Reply {
cmdName := strings.ToLower(string(cmdLine[0]))
cmd, ok := cmdTable[cmdName]
if !ok {
return reply.MakeErrReply("ERR unknown command '" + cmdName + "'")
}
if forbiddenInMulti.Has(cmdName) {
return reply.MakeErrReply("ERR command '" + cmdName + "' cannot be used in MULTI")
}
if cmd.prepare == nil {
return reply.MakeErrReply("ERR command '" + cmdName + "' cannot be used in MULTI")
}
if !validateArity(cmd.arity, cmdLine) {
// difference with redis: we won't enqueue command line with wrong arity
return reply.MakeArgNumErrReply(cmdName)
}
conn.EnqueueCmd(cmdLine)
return reply.MakeQueuedReply()
}
func execMulti(db *DB, conn redis.Connection) redis.Reply {
if !conn.InMultiState() {
return reply.MakeErrReply("ERR EXEC without MULTI")
}
defer conn.SetMultiState(false)
cmdLines := conn.GetQueuedCmdLine()
return ExecMulti(db, conn, conn.GetWatching(), cmdLines)
}
// ExecMulti executes multi commands transaction Atomically and Isolated
func ExecMulti(db *DB, conn redis.Connection, watching map[string]uint32, cmdLines []CmdLine) redis.Reply {
// prepare
writeKeys := make([]string, 0) // may contains duplicate
readKeys := make([]string, 0)
for _, cmdLine := range cmdLines {
cmdName := strings.ToLower(string(cmdLine[0]))
cmd := cmdTable[cmdName]
prepare := cmd.prepare
write, read := prepare(cmdLine[1:])
writeKeys = append(writeKeys, write...)
readKeys = append(readKeys, read...)
}
// set watch
watchingKeys := make([]string, 0, len(watching))
for key := range watching {
watchingKeys = append(watchingKeys, key)
}
readKeys = append(readKeys, watchingKeys...)
db.RWLocks(writeKeys, readKeys)
defer db.RWUnLocks(writeKeys, readKeys)
if isWatchingChanged(db, watching) { // watching keys changed, abort
return reply.MakeEmptyMultiBulkReply()
}
// execute
results := make([]redis.Reply, 0, len(cmdLines))
aborted := false
undoCmdLines := make([][]CmdLine, 0, len(cmdLines))
for _, cmdLine := range cmdLines {
undoCmdLines = append(undoCmdLines, db.GetUndoLogs(cmdLine))
result := db.ExecWithLock(cmdLine)
if reply.IsErrorReply(result) {
aborted = true
// don't rollback failed commands
undoCmdLines = undoCmdLines[:len(undoCmdLines)-1]
break
}
results = append(results, result)
}
if !aborted { //success
db.addVersion(writeKeys...)
return reply.MakeMultiRawReply(results)
}
// undo if aborted
size := len(undoCmdLines)
for i := size - 1; i >= 0; i-- {
curCmdLines := undoCmdLines[i]
if len(curCmdLines) == 0 {
continue
}
for _, cmdLine := range curCmdLines {
db.ExecWithLock(cmdLine)
}
}
return reply.MakeErrReply("EXECABORT Transaction discarded because of previous errors.")
}
// DiscardMulti drops MULTI pending commands
func DiscardMulti(db *DB, conn redis.Connection) redis.Reply {
if !conn.InMultiState() {
return reply.MakeErrReply("ERR DISCARD without MULTI")
}
conn.ClearQueuedCmds()
conn.SetMultiState(false)
return reply.MakeQueuedReply()
}