]>
git.saurik.com Git - redis.git/blob - src/multi.c
eee9748c5fabb0b0fa5745c23c6f4b1533151ebf
   3 /* ================================ MULTI/EXEC ============================== */ 
   5 /* Client state initialization for MULTI/EXEC */ 
   6 void initClientMultiState(redisClient 
*c
) { 
   7     c
->mstate
.commands 
= NULL
; 
  11 /* Release all the resources associated with MULTI/EXEC state */ 
  12 void freeClientMultiState(redisClient 
*c
) { 
  15     for (j 
= 0; j 
< c
->mstate
.count
; j
++) { 
  17         multiCmd 
*mc 
= c
->mstate
.commands
+j
; 
  19         for (i 
= 0; i 
< mc
->argc
; i
++) 
  20             decrRefCount(mc
->argv
[i
]); 
  23     zfree(c
->mstate
.commands
); 
  26 /* Add a new command into the MULTI commands queue */ 
  27 void queueMultiCommand(redisClient 
*c
) { 
  31     c
->mstate
.commands 
= zrealloc(c
->mstate
.commands
, 
  32             sizeof(multiCmd
)*(c
->mstate
.count
+1)); 
  33     mc 
= c
->mstate
.commands
+c
->mstate
.count
; 
  36     mc
->argv 
= zmalloc(sizeof(robj
*)*c
->argc
); 
  37     memcpy(mc
->argv
,c
->argv
,sizeof(robj
*)*c
->argc
); 
  38     for (j 
= 0; j 
< c
->argc
; j
++) 
  39         incrRefCount(mc
->argv
[j
]); 
  43 void discardTransaction(redisClient 
*c
) { 
  44     freeClientMultiState(c
); 
  45     initClientMultiState(c
); 
  46     c
->flags 
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
);; 
  50 void multiCommand(redisClient 
*c
) { 
  51     if (c
->flags 
& REDIS_MULTI
) { 
  52         addReplyError(c
,"MULTI calls can not be nested"); 
  55     c
->flags 
|= REDIS_MULTI
; 
  56     addReply(c
,shared
.ok
); 
  59 void discardCommand(redisClient 
*c
) { 
  60     if (!(c
->flags 
& REDIS_MULTI
)) { 
  61         addReplyError(c
,"DISCARD without MULTI"); 
  64     discardTransaction(c
); 
  65     addReply(c
,shared
.ok
); 
  68 /* Send a MULTI command to all the slaves and AOF file. Check the execCommand 
  69  * implememntation for more information. */ 
  70 void execCommandReplicateMulti(redisClient 
*c
) { 
  71     robj 
*multistring 
= createStringObject("MULTI",5); 
  73     if (server
.aof_state 
!= REDIS_AOF_OFF
) 
  74         feedAppendOnlyFile(server
.multiCommand
,c
->db
->id
,&multistring
,1); 
  75     if (listLength(server
.slaves
)) 
  76         replicationFeedSlaves(server
.slaves
,c
->db
->id
,&multistring
,1); 
  77     decrRefCount(multistring
); 
  80 void execCommand(redisClient 
*c
) { 
  84     struct redisCommand 
*orig_cmd
; 
  86     if (!(c
->flags 
& REDIS_MULTI
)) { 
  87         addReplyError(c
,"EXEC without MULTI"); 
  91     /* Check if we need to abort the EXEC if some WATCHed key was touched. 
  92      * A failed EXEC will return a multi bulk nil object. */ 
  93     if (c
->flags 
& REDIS_DIRTY_CAS
) { 
  94         freeClientMultiState(c
); 
  95         initClientMultiState(c
); 
  96         c
->flags 
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
); 
  98         addReply(c
,shared
.nullmultibulk
); 
 102     /* Replicate a MULTI request now that we are sure the block is executed. 
 103      * This way we'll deliver the MULTI/..../EXEC block as a whole and 
 104      * both the AOF and the replication link will have the same consistency 
 105      * and atomicity guarantees. */ 
 106     execCommandReplicateMulti(c
); 
 108     /* Exec all the queued commands */ 
 109     unwatchAllKeys(c
); /* Unwatch ASAP otherwise we'll waste CPU cycles */ 
 113     addReplyMultiBulkLen(c
,c
->mstate
.count
); 
 114     for (j 
= 0; j 
< c
->mstate
.count
; j
++) { 
 115         c
->argc 
= c
->mstate
.commands
[j
].argc
; 
 116         c
->argv 
= c
->mstate
.commands
[j
].argv
; 
 117         c
->cmd 
= c
->mstate
.commands
[j
].cmd
; 
 118         call(c
,REDIS_CALL_FULL
); 
 120         /* Commands may alter argc/argv, restore mstate. */ 
 121         c
->mstate
.commands
[j
].argc 
= c
->argc
; 
 122         c
->mstate
.commands
[j
].argv 
= c
->argv
; 
 123         c
->mstate
.commands
[j
].cmd 
= c
->cmd
; 
 128     freeClientMultiState(c
); 
 129     initClientMultiState(c
); 
 130     c
->flags 
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
); 
 131     /* Make sure the EXEC command is always replicated / AOF, since we 
 132      * always send the MULTI command (we can't know beforehand if the 
 133      * next operations will contain at least a modification to the DB). */ 
 137 /* ===================== WATCH (CAS alike for MULTI/EXEC) =================== 
 139  * The implementation uses a per-DB hash table mapping keys to list of clients 
 140  * WATCHing those keys, so that given a key that is going to be modified 
 141  * we can mark all the associated clients as dirty. 
 143  * Also every client contains a list of WATCHed keys so that's possible to 
 144  * un-watch such keys when the client is freed or when UNWATCH is called. */ 
 146 /* In the client->watched_keys list we need to use watchedKey structures 
 147  * as in order to identify a key in Redis we need both the key name and the 
 149 typedef struct watchedKey 
{ 
 154 /* Watch for the specified key */ 
 155 void watchForKey(redisClient 
*c
, robj 
*key
) { 
 156     list 
*clients 
= NULL
; 
 161     /* Check if we are already watching for this key */ 
 162     listRewind(c
->watched_keys
,&li
); 
 163     while((ln 
= listNext(&li
))) { 
 164         wk 
= listNodeValue(ln
); 
 165         if (wk
->db 
== c
->db 
&& equalStringObjects(key
,wk
->key
)) 
 166             return; /* Key already watched */ 
 168     /* This key is not already watched in this DB. Let's add it */ 
 169     clients 
= dictFetchValue(c
->db
->watched_keys
,key
); 
 171         clients 
= listCreate(); 
 172         dictAdd(c
->db
->watched_keys
,key
,clients
); 
 175     listAddNodeTail(clients
,c
); 
 176     /* Add the new key to the lits of keys watched by this client */ 
 177     wk 
= zmalloc(sizeof(*wk
)); 
 181     listAddNodeTail(c
->watched_keys
,wk
); 
 184 /* Unwatch all the keys watched by this client. To clean the EXEC dirty 
 185  * flag is up to the caller. */ 
 186 void unwatchAllKeys(redisClient 
*c
) { 
 190     if (listLength(c
->watched_keys
) == 0) return; 
 191     listRewind(c
->watched_keys
,&li
); 
 192     while((ln 
= listNext(&li
))) { 
 196         /* Lookup the watched key -> clients list and remove the client 
 198         wk 
= listNodeValue(ln
); 
 199         clients 
= dictFetchValue(wk
->db
->watched_keys
, wk
->key
); 
 200         redisAssertWithInfo(c
,NULL
,clients 
!= NULL
); 
 201         listDelNode(clients
,listSearchKey(clients
,c
)); 
 202         /* Kill the entry at all if this was the only client */ 
 203         if (listLength(clients
) == 0) 
 204             dictDelete(wk
->db
->watched_keys
, wk
->key
); 
 205         /* Remove this watched key from the client->watched list */ 
 206         listDelNode(c
->watched_keys
,ln
); 
 207         decrRefCount(wk
->key
); 
 212 /* "Touch" a key, so that if this key is being WATCHed by some client the 
 213  * next EXEC will fail. */ 
 214 void touchWatchedKey(redisDb 
*db
, robj 
*key
) { 
 219     if (dictSize(db
->watched_keys
) == 0) return; 
 220     clients 
= dictFetchValue(db
->watched_keys
, key
); 
 221     if (!clients
) return; 
 223     /* Mark all the clients watching this key as REDIS_DIRTY_CAS */ 
 224     /* Check if we are already watching for this key */ 
 225     listRewind(clients
,&li
); 
 226     while((ln 
= listNext(&li
))) { 
 227         redisClient 
*c 
= listNodeValue(ln
); 
 229         c
->flags 
|= REDIS_DIRTY_CAS
; 
 233 /* On FLUSHDB or FLUSHALL all the watched keys that are present before the 
 234  * flush but will be deleted as effect of the flushing operation should 
 235  * be touched. "dbid" is the DB that's getting the flush. -1 if it is 
 236  * a FLUSHALL operation (all the DBs flushed). */ 
 237 void touchWatchedKeysOnFlush(int dbid
) { 
 241     /* For every client, check all the waited keys */ 
 242     listRewind(server
.clients
,&li1
); 
 243     while((ln 
= listNext(&li1
))) { 
 244         redisClient 
*c 
= listNodeValue(ln
); 
 245         listRewind(c
->watched_keys
,&li2
); 
 246         while((ln 
= listNext(&li2
))) { 
 247             watchedKey 
*wk 
= listNodeValue(ln
); 
 249             /* For every watched key matching the specified DB, if the 
 250              * key exists, mark the client as dirty, as the key will be 
 252             if (dbid 
== -1 || wk
->db
->id 
== dbid
) { 
 253                 if (dictFind(wk
->db
->dict
, wk
->key
->ptr
) != NULL
) 
 254                     c
->flags 
|= REDIS_DIRTY_CAS
; 
 260 void watchCommand(redisClient 
*c
) { 
 263     if (c
->flags 
& REDIS_MULTI
) { 
 264         addReplyError(c
,"WATCH inside MULTI is not allowed"); 
 267     for (j 
= 1; j 
< c
->argc
; j
++) 
 268         watchForKey(c
,c
->argv
[j
]); 
 269     addReply(c
,shared
.ok
); 
 272 void unwatchCommand(redisClient 
*c
) { 
 274     c
->flags 
&= (~REDIS_DIRTY_CAS
); 
 275     addReply(c
,shared
.ok
);