]>
git.saurik.com Git - redis.git/blob - src/multi.c
96931fc5b34d5347548615edb24206ca6d4a6644
   2  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> 
   5  * Redistribution and use in source and binary forms, with or without 
   6  * modification, are permitted provided that the following conditions are met: 
   8  *   * Redistributions of source code must retain the above copyright notice, 
   9  *     this list of conditions and the following disclaimer. 
  10  *   * Redistributions in binary form must reproduce the above copyright 
  11  *     notice, this list of conditions and the following disclaimer in the 
  12  *     documentation and/or other materials provided with the distribution. 
  13  *   * Neither the name of Redis nor the names of its contributors may be used 
  14  *     to endorse or promote products derived from this software without 
  15  *     specific prior written permission. 
  17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
  21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  27  * POSSIBILITY OF SUCH DAMAGE. 
  32 /* ================================ MULTI/EXEC ============================== */ 
  34 /* Client state initialization for MULTI/EXEC */ 
  35 void initClientMultiState(redisClient 
*c
) { 
  36     c
->mstate
.commands 
= NULL
; 
  40 /* Release all the resources associated with MULTI/EXEC state */ 
  41 void freeClientMultiState(redisClient 
*c
) { 
  44     for (j 
= 0; j 
< c
->mstate
.count
; j
++) { 
  46         multiCmd 
*mc 
= c
->mstate
.commands
+j
; 
  48         for (i 
= 0; i 
< mc
->argc
; i
++) 
  49             decrRefCount(mc
->argv
[i
]); 
  52     zfree(c
->mstate
.commands
); 
  55 /* Add a new command into the MULTI commands queue */ 
  56 void queueMultiCommand(redisClient 
*c
) { 
  60     c
->mstate
.commands 
= zrealloc(c
->mstate
.commands
, 
  61             sizeof(multiCmd
)*(c
->mstate
.count
+1)); 
  62     mc 
= c
->mstate
.commands
+c
->mstate
.count
; 
  65     mc
->argv 
= zmalloc(sizeof(robj
*)*c
->argc
); 
  66     memcpy(mc
->argv
,c
->argv
,sizeof(robj
*)*c
->argc
); 
  67     for (j 
= 0; j 
< c
->argc
; j
++) 
  68         incrRefCount(mc
->argv
[j
]); 
  72 void discardTransaction(redisClient 
*c
) { 
  73     freeClientMultiState(c
); 
  74     initClientMultiState(c
); 
  75     c
->flags 
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
);; 
  79 void multiCommand(redisClient 
*c
) { 
  80     if (c
->flags 
& REDIS_MULTI
) { 
  81         addReplyError(c
,"MULTI calls can not be nested"); 
  84     c
->flags 
|= REDIS_MULTI
; 
  85     addReply(c
,shared
.ok
); 
  88 void discardCommand(redisClient 
*c
) { 
  89     if (!(c
->flags 
& REDIS_MULTI
)) { 
  90         addReplyError(c
,"DISCARD without MULTI"); 
  93     discardTransaction(c
); 
  94     addReply(c
,shared
.ok
); 
  97 /* Send a MULTI command to all the slaves and AOF file. Check the execCommand 
  98  * implememntation for more information. */ 
  99 void execCommandReplicateMulti(redisClient 
*c
) { 
 100     robj 
*multistring 
= createStringObject("MULTI",5); 
 102     if (server
.aof_state 
!= REDIS_AOF_OFF
) 
 103         feedAppendOnlyFile(server
.multiCommand
,c
->db
->id
,&multistring
,1); 
 104     if (listLength(server
.slaves
)) 
 105         replicationFeedSlaves(server
.slaves
,c
->db
->id
,&multistring
,1); 
 106     decrRefCount(multistring
); 
 109 void execCommand(redisClient 
*c
) { 
 113     struct redisCommand 
*orig_cmd
; 
 115     if (!(c
->flags 
& REDIS_MULTI
)) { 
 116         addReplyError(c
,"EXEC without MULTI"); 
 120     /* Check if we need to abort the EXEC if some WATCHed key was touched. 
 121      * A failed EXEC will return a multi bulk nil object. */ 
 122     if (c
->flags 
& REDIS_DIRTY_CAS
) { 
 123         freeClientMultiState(c
); 
 124         initClientMultiState(c
); 
 125         c
->flags 
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
); 
 127         addReply(c
,shared
.nullmultibulk
); 
 131     /* Replicate a MULTI request now that we are sure the block is executed. 
 132      * This way we'll deliver the MULTI/..../EXEC block as a whole and 
 133      * both the AOF and the replication link will have the same consistency 
 134      * and atomicity guarantees. */ 
 135     execCommandReplicateMulti(c
); 
 137     /* Exec all the queued commands */ 
 138     unwatchAllKeys(c
); /* Unwatch ASAP otherwise we'll waste CPU cycles */ 
 142     addReplyMultiBulkLen(c
,c
->mstate
.count
); 
 143     for (j 
= 0; j 
< c
->mstate
.count
; j
++) { 
 144         c
->argc 
= c
->mstate
.commands
[j
].argc
; 
 145         c
->argv 
= c
->mstate
.commands
[j
].argv
; 
 146         c
->cmd 
= c
->mstate
.commands
[j
].cmd
; 
 147         call(c
,REDIS_CALL_FULL
); 
 149         /* Commands may alter argc/argv, restore mstate. */ 
 150         c
->mstate
.commands
[j
].argc 
= c
->argc
; 
 151         c
->mstate
.commands
[j
].argv 
= c
->argv
; 
 152         c
->mstate
.commands
[j
].cmd 
= c
->cmd
; 
 157     freeClientMultiState(c
); 
 158     initClientMultiState(c
); 
 159     c
->flags 
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
); 
 160     /* Make sure the EXEC command is always replicated / AOF, since we 
 161      * always send the MULTI command (we can't know beforehand if the 
 162      * next operations will contain at least a modification to the DB). */ 
 166     /* Send EXEC to clients waiting data from MONITOR. We do it here 
 167      * since the natural order of commands execution is actually: 
 168      * MUTLI, EXEC, ... commands inside transaction ... 
 169      * Instead EXEC is flagged as REDIS_CMD_SKIP_MONITOR in the command 
 170      * table, and we do it here with correct ordering. */ 
 171     if (listLength(server
.monitors
) && !server
.loading
) 
 172         replicationFeedMonitors(c
,server
.monitors
,c
->db
->id
,c
->argv
,c
->argc
); 
 175 /* ===================== WATCH (CAS alike for MULTI/EXEC) =================== 
 177  * The implementation uses a per-DB hash table mapping keys to list of clients 
 178  * WATCHing those keys, so that given a key that is going to be modified 
 179  * we can mark all the associated clients as dirty. 
 181  * Also every client contains a list of WATCHed keys so that's possible to 
 182  * un-watch such keys when the client is freed or when UNWATCH is called. */ 
 184 /* In the client->watched_keys list we need to use watchedKey structures 
 185  * as in order to identify a key in Redis we need both the key name and the 
 187 typedef struct watchedKey 
{ 
 192 /* Watch for the specified key */ 
 193 void watchForKey(redisClient 
*c
, robj 
*key
) { 
 194     list 
*clients 
= NULL
; 
 199     /* Check if we are already watching for this key */ 
 200     listRewind(c
->watched_keys
,&li
); 
 201     while((ln 
= listNext(&li
))) { 
 202         wk 
= listNodeValue(ln
); 
 203         if (wk
->db 
== c
->db 
&& equalStringObjects(key
,wk
->key
)) 
 204             return; /* Key already watched */ 
 206     /* This key is not already watched in this DB. Let's add it */ 
 207     clients 
= dictFetchValue(c
->db
->watched_keys
,key
); 
 209         clients 
= listCreate(); 
 210         dictAdd(c
->db
->watched_keys
,key
,clients
); 
 213     listAddNodeTail(clients
,c
); 
 214     /* Add the new key to the lits of keys watched by this client */ 
 215     wk 
= zmalloc(sizeof(*wk
)); 
 219     listAddNodeTail(c
->watched_keys
,wk
); 
 222 /* Unwatch all the keys watched by this client. To clean the EXEC dirty 
 223  * flag is up to the caller. */ 
 224 void unwatchAllKeys(redisClient 
*c
) { 
 228     if (listLength(c
->watched_keys
) == 0) return; 
 229     listRewind(c
->watched_keys
,&li
); 
 230     while((ln 
= listNext(&li
))) { 
 234         /* Lookup the watched key -> clients list and remove the client 
 236         wk 
= listNodeValue(ln
); 
 237         clients 
= dictFetchValue(wk
->db
->watched_keys
, wk
->key
); 
 238         redisAssertWithInfo(c
,NULL
,clients 
!= NULL
); 
 239         listDelNode(clients
,listSearchKey(clients
,c
)); 
 240         /* Kill the entry at all if this was the only client */ 
 241         if (listLength(clients
) == 0) 
 242             dictDelete(wk
->db
->watched_keys
, wk
->key
); 
 243         /* Remove this watched key from the client->watched list */ 
 244         listDelNode(c
->watched_keys
,ln
); 
 245         decrRefCount(wk
->key
); 
 250 /* "Touch" a key, so that if this key is being WATCHed by some client the 
 251  * next EXEC will fail. */ 
 252 void touchWatchedKey(redisDb 
*db
, robj 
*key
) { 
 257     if (dictSize(db
->watched_keys
) == 0) return; 
 258     clients 
= dictFetchValue(db
->watched_keys
, key
); 
 259     if (!clients
) return; 
 261     /* Mark all the clients watching this key as REDIS_DIRTY_CAS */ 
 262     /* Check if we are already watching for this key */ 
 263     listRewind(clients
,&li
); 
 264     while((ln 
= listNext(&li
))) { 
 265         redisClient 
*c 
= listNodeValue(ln
); 
 267         c
->flags 
|= REDIS_DIRTY_CAS
; 
 271 /* On FLUSHDB or FLUSHALL all the watched keys that are present before the 
 272  * flush but will be deleted as effect of the flushing operation should 
 273  * be touched. "dbid" is the DB that's getting the flush. -1 if it is 
 274  * a FLUSHALL operation (all the DBs flushed). */ 
 275 void touchWatchedKeysOnFlush(int dbid
) { 
 279     /* For every client, check all the waited keys */ 
 280     listRewind(server
.clients
,&li1
); 
 281     while((ln 
= listNext(&li1
))) { 
 282         redisClient 
*c 
= listNodeValue(ln
); 
 283         listRewind(c
->watched_keys
,&li2
); 
 284         while((ln 
= listNext(&li2
))) { 
 285             watchedKey 
*wk 
= listNodeValue(ln
); 
 287             /* For every watched key matching the specified DB, if the 
 288              * key exists, mark the client as dirty, as the key will be 
 290             if (dbid 
== -1 || wk
->db
->id 
== dbid
) { 
 291                 if (dictFind(wk
->db
->dict
, wk
->key
->ptr
) != NULL
) 
 292                     c
->flags 
|= REDIS_DIRTY_CAS
; 
 298 void watchCommand(redisClient 
*c
) { 
 301     if (c
->flags 
& REDIS_MULTI
) { 
 302         addReplyError(c
,"WATCH inside MULTI is not allowed"); 
 305     for (j 
= 1; j 
< c
->argc
; j
++) 
 306         watchForKey(c
,c
->argv
[j
]); 
 307     addReply(c
,shared
.ok
); 
 310 void unwatchCommand(redisClient 
*c
) { 
 312     c
->flags 
&= (~REDIS_DIRTY_CAS
); 
 313     addReply(c
,shared
.ok
);