}
/* Add a new command into the MULTI commands queue */
-void queueMultiCommand(redisClient *c, struct redisCommand *cmd) {
+void queueMultiCommand(redisClient *c) {
multiCmd *mc;
int j;
c->mstate.commands = zrealloc(c->mstate.commands,
sizeof(multiCmd)*(c->mstate.count+1));
mc = c->mstate.commands+c->mstate.count;
- mc->cmd = cmd;
+ mc->cmd = c->cmd;
mc->argc = c->argc;
mc->argv = zmalloc(sizeof(robj*)*c->argc);
memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
c->mstate.count++;
}
+void discardTransaction(redisClient *c) {
+ freeClientMultiState(c);
+ initClientMultiState(c);
+ c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS);;
+ unwatchAllKeys(c);
+}
+
void multiCommand(redisClient *c) {
if (c->flags & REDIS_MULTI) {
addReplyError(c,"MULTI calls can not be nested");
addReplyError(c,"DISCARD without MULTI");
return;
}
-
- freeClientMultiState(c);
- initClientMultiState(c);
- c->flags &= (~REDIS_MULTI);
- unwatchAllKeys(c);
+ discardTransaction(c);
addReply(c,shared.ok);
}
/* Send a MULTI command to all the slaves and AOF file. Check the execCommand
* implememntation for more information. */
void execCommandReplicateMulti(redisClient *c) {
- struct redisCommand *cmd;
robj *multistring = createStringObject("MULTI",5);
- cmd = lookupCommand("multi");
- if (server.appendonly)
- feedAppendOnlyFile(cmd,c->db->id,&multistring,1);
+ if (server.aof_state != REDIS_AOF_OFF)
+ feedAppendOnlyFile(server.multiCommand,c->db->id,&multistring,1);
if (listLength(server.slaves))
replicationFeedSlaves(server.slaves,c->db->id,&multistring,1);
decrRefCount(multistring);
int j;
robj **orig_argv;
int orig_argc;
+ struct redisCommand *orig_cmd;
if (!(c->flags & REDIS_MULTI)) {
addReplyError(c,"EXEC without MULTI");
c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS);
unwatchAllKeys(c);
addReply(c,shared.nullmultibulk);
- return;
+ goto handle_monitor;
}
/* Replicate a MULTI request now that we are sure the block is executed.
unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */
orig_argv = c->argv;
orig_argc = c->argc;
+ orig_cmd = c->cmd;
addReplyMultiBulkLen(c,c->mstate.count);
for (j = 0; j < c->mstate.count; j++) {
c->argc = c->mstate.commands[j].argc;
c->argv = c->mstate.commands[j].argv;
- call(c,c->mstate.commands[j].cmd);
+ c->cmd = c->mstate.commands[j].cmd;
+ call(c,REDIS_CALL_FULL);
+
+ /* Commands may alter argc/argv, restore mstate. */
+ c->mstate.commands[j].argc = c->argc;
+ c->mstate.commands[j].argv = c->argv;
+ c->mstate.commands[j].cmd = c->cmd;
}
c->argv = orig_argv;
c->argc = orig_argc;
+ c->cmd = orig_cmd;
freeClientMultiState(c);
initClientMultiState(c);
c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS);
* always send the MULTI command (we can't know beforehand if the
* next operations will contain at least a modification to the DB). */
server.dirty++;
+
+handle_monitor:
+ /* Send EXEC to clients waiting data from MONITOR. We do it here
+ * since the natural order of commands execution is actually:
+ * MUTLI, EXEC, ... commands inside transaction ...
+ * Instead EXEC is flagged as REDIS_CMD_SKIP_MONITOR in the command
+ * table, and we do it here with correct ordering. */
+ if (listLength(server.monitors) && !server.loading)
+ replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
}
/* ===================== WATCH (CAS alike for MULTI/EXEC) ===================
* from the list */
wk = listNodeValue(ln);
clients = dictFetchValue(wk->db->watched_keys, wk->key);
- redisAssert(clients != NULL);
+ redisAssertWithInfo(c,NULL,clients != NULL);
listDelNode(clients,listSearchKey(clients,c));
/* Kill the entry at all if this was the only client */
if (listLength(clients) == 0)