X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/054061685add05f30722971537e084f0589b601d..d64a9cf514d2101fbad002b8721c9fe1176916ac:/src/multi.c diff --git a/src/multi.c b/src/multi.c index eee9748c..064a4094 100644 --- a/src/multi.c +++ b/src/multi.c @@ -1,3 +1,32 @@ +/* + * Copyright (c) 2009-2012, Salvatore Sanfilippo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Redis nor the names of its contributors may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + #include "redis.h" /* ================================ MULTI/EXEC ============================== */ @@ -43,10 +72,17 @@ void queueMultiCommand(redisClient *c) { void discardTransaction(redisClient *c) { freeClientMultiState(c); initClientMultiState(c); - c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS);; + c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS|REDIS_DIRTY_EXEC);; unwatchAllKeys(c); } +/* Flag the transacation as DIRTY_EXEC so that EXEC will fail. + * Should be called every time there is an error while queueing a command. */ +void flagTransaction(redisClient *c) { + if (c->flags & REDIS_MULTI) + c->flags |= REDIS_DIRTY_EXEC; +} + void multiCommand(redisClient *c) { if (c->flags & REDIS_MULTI) { addReplyError(c,"MULTI calls can not be nested"); @@ -88,15 +124,20 @@ void execCommand(redisClient *c) { return; } - /* Check if we need to abort the EXEC if some WATCHed key was touched. - * A failed EXEC will return a multi bulk nil object. */ - if (c->flags & REDIS_DIRTY_CAS) { + /* Check if we need to abort the EXEC because: + * 1) Some WATCHed key was touched. + * 2) There was a previous error while queueing commands. + * A failed EXEC in the first case returns a multi bulk nil object + * (technically it is not an error but a special behavior), while + * in the second an EXECABORT error is returned. */ + if (c->flags & (REDIS_DIRTY_CAS|REDIS_DIRTY_EXEC)) { + addReply(c, c->flags & REDIS_DIRTY_EXEC ? shared.execaborterr : + shared.nullmultibulk); freeClientMultiState(c); initClientMultiState(c); - c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS); + c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS|REDIS_DIRTY_EXEC); unwatchAllKeys(c); - addReply(c,shared.nullmultibulk); - return; + goto handle_monitor; } /* Replicate a MULTI request now that we are sure the block is executed. @@ -127,11 +168,20 @@ void execCommand(redisClient *c) { c->cmd = orig_cmd; freeClientMultiState(c); initClientMultiState(c); - c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS); + c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS|REDIS_DIRTY_EXEC); /* Make sure the EXEC command is always replicated / AOF, since we * 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) ===================