xref: /f-stack/app/redis-5.0.5/src/multi.c (revision 572c4311)
1*572c4311Sfengbojiang /*
2*572c4311Sfengbojiang  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3*572c4311Sfengbojiang  * All rights reserved.
4*572c4311Sfengbojiang  *
5*572c4311Sfengbojiang  * Redistribution and use in source and binary forms, with or without
6*572c4311Sfengbojiang  * modification, are permitted provided that the following conditions are met:
7*572c4311Sfengbojiang  *
8*572c4311Sfengbojiang  *   * Redistributions of source code must retain the above copyright notice,
9*572c4311Sfengbojiang  *     this list of conditions and the following disclaimer.
10*572c4311Sfengbojiang  *   * Redistributions in binary form must reproduce the above copyright
11*572c4311Sfengbojiang  *     notice, this list of conditions and the following disclaimer in the
12*572c4311Sfengbojiang  *     documentation and/or other materials provided with the distribution.
13*572c4311Sfengbojiang  *   * Neither the name of Redis nor the names of its contributors may be used
14*572c4311Sfengbojiang  *     to endorse or promote products derived from this software without
15*572c4311Sfengbojiang  *     specific prior written permission.
16*572c4311Sfengbojiang  *
17*572c4311Sfengbojiang  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18*572c4311Sfengbojiang  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*572c4311Sfengbojiang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*572c4311Sfengbojiang  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21*572c4311Sfengbojiang  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*572c4311Sfengbojiang  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*572c4311Sfengbojiang  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*572c4311Sfengbojiang  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*572c4311Sfengbojiang  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*572c4311Sfengbojiang  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*572c4311Sfengbojiang  * POSSIBILITY OF SUCH DAMAGE.
28*572c4311Sfengbojiang  */
29*572c4311Sfengbojiang 
30*572c4311Sfengbojiang #include "server.h"
31*572c4311Sfengbojiang 
32*572c4311Sfengbojiang /* ================================ MULTI/EXEC ============================== */
33*572c4311Sfengbojiang 
34*572c4311Sfengbojiang /* Client state initialization for MULTI/EXEC */
initClientMultiState(client * c)35*572c4311Sfengbojiang void initClientMultiState(client *c) {
36*572c4311Sfengbojiang     c->mstate.commands = NULL;
37*572c4311Sfengbojiang     c->mstate.count = 0;
38*572c4311Sfengbojiang     c->mstate.cmd_flags = 0;
39*572c4311Sfengbojiang }
40*572c4311Sfengbojiang 
41*572c4311Sfengbojiang /* Release all the resources associated with MULTI/EXEC state */
freeClientMultiState(client * c)42*572c4311Sfengbojiang void freeClientMultiState(client *c) {
43*572c4311Sfengbojiang     int j;
44*572c4311Sfengbojiang 
45*572c4311Sfengbojiang     for (j = 0; j < c->mstate.count; j++) {
46*572c4311Sfengbojiang         int i;
47*572c4311Sfengbojiang         multiCmd *mc = c->mstate.commands+j;
48*572c4311Sfengbojiang 
49*572c4311Sfengbojiang         for (i = 0; i < mc->argc; i++)
50*572c4311Sfengbojiang             decrRefCount(mc->argv[i]);
51*572c4311Sfengbojiang         zfree(mc->argv);
52*572c4311Sfengbojiang     }
53*572c4311Sfengbojiang     zfree(c->mstate.commands);
54*572c4311Sfengbojiang }
55*572c4311Sfengbojiang 
56*572c4311Sfengbojiang /* Add a new command into the MULTI commands queue */
queueMultiCommand(client * c)57*572c4311Sfengbojiang void queueMultiCommand(client *c) {
58*572c4311Sfengbojiang     multiCmd *mc;
59*572c4311Sfengbojiang     int j;
60*572c4311Sfengbojiang 
61*572c4311Sfengbojiang     c->mstate.commands = zrealloc(c->mstate.commands,
62*572c4311Sfengbojiang             sizeof(multiCmd)*(c->mstate.count+1));
63*572c4311Sfengbojiang     mc = c->mstate.commands+c->mstate.count;
64*572c4311Sfengbojiang     mc->cmd = c->cmd;
65*572c4311Sfengbojiang     mc->argc = c->argc;
66*572c4311Sfengbojiang     mc->argv = zmalloc(sizeof(robj*)*c->argc);
67*572c4311Sfengbojiang     memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
68*572c4311Sfengbojiang     for (j = 0; j < c->argc; j++)
69*572c4311Sfengbojiang         incrRefCount(mc->argv[j]);
70*572c4311Sfengbojiang     c->mstate.count++;
71*572c4311Sfengbojiang     c->mstate.cmd_flags |= c->cmd->flags;
72*572c4311Sfengbojiang }
73*572c4311Sfengbojiang 
discardTransaction(client * c)74*572c4311Sfengbojiang void discardTransaction(client *c) {
75*572c4311Sfengbojiang     freeClientMultiState(c);
76*572c4311Sfengbojiang     initClientMultiState(c);
77*572c4311Sfengbojiang     c->flags &= ~(CLIENT_MULTI|CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC);
78*572c4311Sfengbojiang     unwatchAllKeys(c);
79*572c4311Sfengbojiang }
80*572c4311Sfengbojiang 
81*572c4311Sfengbojiang /* Flag the transacation as DIRTY_EXEC so that EXEC will fail.
82*572c4311Sfengbojiang  * Should be called every time there is an error while queueing a command. */
flagTransaction(client * c)83*572c4311Sfengbojiang void flagTransaction(client *c) {
84*572c4311Sfengbojiang     if (c->flags & CLIENT_MULTI)
85*572c4311Sfengbojiang         c->flags |= CLIENT_DIRTY_EXEC;
86*572c4311Sfengbojiang }
87*572c4311Sfengbojiang 
multiCommand(client * c)88*572c4311Sfengbojiang void multiCommand(client *c) {
89*572c4311Sfengbojiang     if (c->flags & CLIENT_MULTI) {
90*572c4311Sfengbojiang         addReplyError(c,"MULTI calls can not be nested");
91*572c4311Sfengbojiang         return;
92*572c4311Sfengbojiang     }
93*572c4311Sfengbojiang     c->flags |= CLIENT_MULTI;
94*572c4311Sfengbojiang     addReply(c,shared.ok);
95*572c4311Sfengbojiang }
96*572c4311Sfengbojiang 
discardCommand(client * c)97*572c4311Sfengbojiang void discardCommand(client *c) {
98*572c4311Sfengbojiang     if (!(c->flags & CLIENT_MULTI)) {
99*572c4311Sfengbojiang         addReplyError(c,"DISCARD without MULTI");
100*572c4311Sfengbojiang         return;
101*572c4311Sfengbojiang     }
102*572c4311Sfengbojiang     discardTransaction(c);
103*572c4311Sfengbojiang     addReply(c,shared.ok);
104*572c4311Sfengbojiang }
105*572c4311Sfengbojiang 
106*572c4311Sfengbojiang /* Send a MULTI command to all the slaves and AOF file. Check the execCommand
107*572c4311Sfengbojiang  * implementation for more information. */
execCommandPropagateMulti(client * c)108*572c4311Sfengbojiang void execCommandPropagateMulti(client *c) {
109*572c4311Sfengbojiang     robj *multistring = createStringObject("MULTI",5);
110*572c4311Sfengbojiang 
111*572c4311Sfengbojiang     propagate(server.multiCommand,c->db->id,&multistring,1,
112*572c4311Sfengbojiang               PROPAGATE_AOF|PROPAGATE_REPL);
113*572c4311Sfengbojiang     decrRefCount(multistring);
114*572c4311Sfengbojiang }
115*572c4311Sfengbojiang 
execCommand(client * c)116*572c4311Sfengbojiang void execCommand(client *c) {
117*572c4311Sfengbojiang     int j;
118*572c4311Sfengbojiang     robj **orig_argv;
119*572c4311Sfengbojiang     int orig_argc;
120*572c4311Sfengbojiang     struct redisCommand *orig_cmd;
121*572c4311Sfengbojiang     int must_propagate = 0; /* Need to propagate MULTI/EXEC to AOF / slaves? */
122*572c4311Sfengbojiang     int was_master = server.masterhost == NULL;
123*572c4311Sfengbojiang 
124*572c4311Sfengbojiang     if (!(c->flags & CLIENT_MULTI)) {
125*572c4311Sfengbojiang         addReplyError(c,"EXEC without MULTI");
126*572c4311Sfengbojiang         return;
127*572c4311Sfengbojiang     }
128*572c4311Sfengbojiang 
129*572c4311Sfengbojiang     /* Check if we need to abort the EXEC because:
130*572c4311Sfengbojiang      * 1) Some WATCHed key was touched.
131*572c4311Sfengbojiang      * 2) There was a previous error while queueing commands.
132*572c4311Sfengbojiang      * A failed EXEC in the first case returns a multi bulk nil object
133*572c4311Sfengbojiang      * (technically it is not an error but a special behavior), while
134*572c4311Sfengbojiang      * in the second an EXECABORT error is returned. */
135*572c4311Sfengbojiang     if (c->flags & (CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC)) {
136*572c4311Sfengbojiang         addReply(c, c->flags & CLIENT_DIRTY_EXEC ? shared.execaborterr :
137*572c4311Sfengbojiang                                                   shared.nullmultibulk);
138*572c4311Sfengbojiang         discardTransaction(c);
139*572c4311Sfengbojiang         goto handle_monitor;
140*572c4311Sfengbojiang     }
141*572c4311Sfengbojiang 
142*572c4311Sfengbojiang     /* If there are write commands inside the transaction, and this is a read
143*572c4311Sfengbojiang      * only slave, we want to send an error. This happens when the transaction
144*572c4311Sfengbojiang      * was initiated when the instance was a master or a writable replica and
145*572c4311Sfengbojiang      * then the configuration changed (for example instance was turned into
146*572c4311Sfengbojiang      * a replica). */
147*572c4311Sfengbojiang     if (!server.loading && server.masterhost && server.repl_slave_ro &&
148*572c4311Sfengbojiang         !(c->flags & CLIENT_MASTER) && c->mstate.cmd_flags & CMD_WRITE)
149*572c4311Sfengbojiang     {
150*572c4311Sfengbojiang         addReplyError(c,
151*572c4311Sfengbojiang             "Transaction contains write commands but instance "
152*572c4311Sfengbojiang             "is now a read-only slave. EXEC aborted.");
153*572c4311Sfengbojiang         discardTransaction(c);
154*572c4311Sfengbojiang         goto handle_monitor;
155*572c4311Sfengbojiang     }
156*572c4311Sfengbojiang 
157*572c4311Sfengbojiang     /* Exec all the queued commands */
158*572c4311Sfengbojiang     unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */
159*572c4311Sfengbojiang     orig_argv = c->argv;
160*572c4311Sfengbojiang     orig_argc = c->argc;
161*572c4311Sfengbojiang     orig_cmd = c->cmd;
162*572c4311Sfengbojiang     addReplyMultiBulkLen(c,c->mstate.count);
163*572c4311Sfengbojiang     for (j = 0; j < c->mstate.count; j++) {
164*572c4311Sfengbojiang         c->argc = c->mstate.commands[j].argc;
165*572c4311Sfengbojiang         c->argv = c->mstate.commands[j].argv;
166*572c4311Sfengbojiang         c->cmd = c->mstate.commands[j].cmd;
167*572c4311Sfengbojiang 
168*572c4311Sfengbojiang         /* Propagate a MULTI request once we encounter the first command which
169*572c4311Sfengbojiang          * is not readonly nor an administrative one.
170*572c4311Sfengbojiang          * This way we'll deliver the MULTI/..../EXEC block as a whole and
171*572c4311Sfengbojiang          * both the AOF and the replication link will have the same consistency
172*572c4311Sfengbojiang          * and atomicity guarantees. */
173*572c4311Sfengbojiang         if (!must_propagate && !(c->cmd->flags & (CMD_READONLY|CMD_ADMIN))) {
174*572c4311Sfengbojiang             execCommandPropagateMulti(c);
175*572c4311Sfengbojiang             must_propagate = 1;
176*572c4311Sfengbojiang         }
177*572c4311Sfengbojiang 
178*572c4311Sfengbojiang         call(c,server.loading ? CMD_CALL_NONE : CMD_CALL_FULL);
179*572c4311Sfengbojiang 
180*572c4311Sfengbojiang         /* Commands may alter argc/argv, restore mstate. */
181*572c4311Sfengbojiang         c->mstate.commands[j].argc = c->argc;
182*572c4311Sfengbojiang         c->mstate.commands[j].argv = c->argv;
183*572c4311Sfengbojiang         c->mstate.commands[j].cmd = c->cmd;
184*572c4311Sfengbojiang     }
185*572c4311Sfengbojiang     c->argv = orig_argv;
186*572c4311Sfengbojiang     c->argc = orig_argc;
187*572c4311Sfengbojiang     c->cmd = orig_cmd;
188*572c4311Sfengbojiang     discardTransaction(c);
189*572c4311Sfengbojiang 
190*572c4311Sfengbojiang     /* Make sure the EXEC command will be propagated as well if MULTI
191*572c4311Sfengbojiang      * was already propagated. */
192*572c4311Sfengbojiang     if (must_propagate) {
193*572c4311Sfengbojiang         int is_master = server.masterhost == NULL;
194*572c4311Sfengbojiang         server.dirty++;
195*572c4311Sfengbojiang         /* If inside the MULTI/EXEC block this instance was suddenly
196*572c4311Sfengbojiang          * switched from master to slave (using the SLAVEOF command), the
197*572c4311Sfengbojiang          * initial MULTI was propagated into the replication backlog, but the
198*572c4311Sfengbojiang          * rest was not. We need to make sure to at least terminate the
199*572c4311Sfengbojiang          * backlog with the final EXEC. */
200*572c4311Sfengbojiang         if (server.repl_backlog && was_master && !is_master) {
201*572c4311Sfengbojiang             char *execcmd = "*1\r\n$4\r\nEXEC\r\n";
202*572c4311Sfengbojiang             feedReplicationBacklog(execcmd,strlen(execcmd));
203*572c4311Sfengbojiang         }
204*572c4311Sfengbojiang     }
205*572c4311Sfengbojiang 
206*572c4311Sfengbojiang handle_monitor:
207*572c4311Sfengbojiang     /* Send EXEC to clients waiting data from MONITOR. We do it here
208*572c4311Sfengbojiang      * since the natural order of commands execution is actually:
209*572c4311Sfengbojiang      * MUTLI, EXEC, ... commands inside transaction ...
210*572c4311Sfengbojiang      * Instead EXEC is flagged as CMD_SKIP_MONITOR in the command
211*572c4311Sfengbojiang      * table, and we do it here with correct ordering. */
212*572c4311Sfengbojiang     if (listLength(server.monitors) && !server.loading)
213*572c4311Sfengbojiang         replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
214*572c4311Sfengbojiang }
215*572c4311Sfengbojiang 
216*572c4311Sfengbojiang /* ===================== WATCH (CAS alike for MULTI/EXEC) ===================
217*572c4311Sfengbojiang  *
218*572c4311Sfengbojiang  * The implementation uses a per-DB hash table mapping keys to list of clients
219*572c4311Sfengbojiang  * WATCHing those keys, so that given a key that is going to be modified
220*572c4311Sfengbojiang  * we can mark all the associated clients as dirty.
221*572c4311Sfengbojiang  *
222*572c4311Sfengbojiang  * Also every client contains a list of WATCHed keys so that's possible to
223*572c4311Sfengbojiang  * un-watch such keys when the client is freed or when UNWATCH is called. */
224*572c4311Sfengbojiang 
225*572c4311Sfengbojiang /* In the client->watched_keys list we need to use watchedKey structures
226*572c4311Sfengbojiang  * as in order to identify a key in Redis we need both the key name and the
227*572c4311Sfengbojiang  * DB */
228*572c4311Sfengbojiang typedef struct watchedKey {
229*572c4311Sfengbojiang     robj *key;
230*572c4311Sfengbojiang     redisDb *db;
231*572c4311Sfengbojiang } watchedKey;
232*572c4311Sfengbojiang 
233*572c4311Sfengbojiang /* Watch for the specified key */
watchForKey(client * c,robj * key)234*572c4311Sfengbojiang void watchForKey(client *c, robj *key) {
235*572c4311Sfengbojiang     list *clients = NULL;
236*572c4311Sfengbojiang     listIter li;
237*572c4311Sfengbojiang     listNode *ln;
238*572c4311Sfengbojiang     watchedKey *wk;
239*572c4311Sfengbojiang 
240*572c4311Sfengbojiang     /* Check if we are already watching for this key */
241*572c4311Sfengbojiang     listRewind(c->watched_keys,&li);
242*572c4311Sfengbojiang     while((ln = listNext(&li))) {
243*572c4311Sfengbojiang         wk = listNodeValue(ln);
244*572c4311Sfengbojiang         if (wk->db == c->db && equalStringObjects(key,wk->key))
245*572c4311Sfengbojiang             return; /* Key already watched */
246*572c4311Sfengbojiang     }
247*572c4311Sfengbojiang     /* This key is not already watched in this DB. Let's add it */
248*572c4311Sfengbojiang     clients = dictFetchValue(c->db->watched_keys,key);
249*572c4311Sfengbojiang     if (!clients) {
250*572c4311Sfengbojiang         clients = listCreate();
251*572c4311Sfengbojiang         dictAdd(c->db->watched_keys,key,clients);
252*572c4311Sfengbojiang         incrRefCount(key);
253*572c4311Sfengbojiang     }
254*572c4311Sfengbojiang     listAddNodeTail(clients,c);
255*572c4311Sfengbojiang     /* Add the new key to the list of keys watched by this client */
256*572c4311Sfengbojiang     wk = zmalloc(sizeof(*wk));
257*572c4311Sfengbojiang     wk->key = key;
258*572c4311Sfengbojiang     wk->db = c->db;
259*572c4311Sfengbojiang     incrRefCount(key);
260*572c4311Sfengbojiang     listAddNodeTail(c->watched_keys,wk);
261*572c4311Sfengbojiang }
262*572c4311Sfengbojiang 
263*572c4311Sfengbojiang /* Unwatch all the keys watched by this client. To clean the EXEC dirty
264*572c4311Sfengbojiang  * flag is up to the caller. */
unwatchAllKeys(client * c)265*572c4311Sfengbojiang void unwatchAllKeys(client *c) {
266*572c4311Sfengbojiang     listIter li;
267*572c4311Sfengbojiang     listNode *ln;
268*572c4311Sfengbojiang 
269*572c4311Sfengbojiang     if (listLength(c->watched_keys) == 0) return;
270*572c4311Sfengbojiang     listRewind(c->watched_keys,&li);
271*572c4311Sfengbojiang     while((ln = listNext(&li))) {
272*572c4311Sfengbojiang         list *clients;
273*572c4311Sfengbojiang         watchedKey *wk;
274*572c4311Sfengbojiang 
275*572c4311Sfengbojiang         /* Lookup the watched key -> clients list and remove the client
276*572c4311Sfengbojiang          * from the list */
277*572c4311Sfengbojiang         wk = listNodeValue(ln);
278*572c4311Sfengbojiang         clients = dictFetchValue(wk->db->watched_keys, wk->key);
279*572c4311Sfengbojiang         serverAssertWithInfo(c,NULL,clients != NULL);
280*572c4311Sfengbojiang         listDelNode(clients,listSearchKey(clients,c));
281*572c4311Sfengbojiang         /* Kill the entry at all if this was the only client */
282*572c4311Sfengbojiang         if (listLength(clients) == 0)
283*572c4311Sfengbojiang             dictDelete(wk->db->watched_keys, wk->key);
284*572c4311Sfengbojiang         /* Remove this watched key from the client->watched list */
285*572c4311Sfengbojiang         listDelNode(c->watched_keys,ln);
286*572c4311Sfengbojiang         decrRefCount(wk->key);
287*572c4311Sfengbojiang         zfree(wk);
288*572c4311Sfengbojiang     }
289*572c4311Sfengbojiang }
290*572c4311Sfengbojiang 
291*572c4311Sfengbojiang /* "Touch" a key, so that if this key is being WATCHed by some client the
292*572c4311Sfengbojiang  * next EXEC will fail. */
touchWatchedKey(redisDb * db,robj * key)293*572c4311Sfengbojiang void touchWatchedKey(redisDb *db, robj *key) {
294*572c4311Sfengbojiang     list *clients;
295*572c4311Sfengbojiang     listIter li;
296*572c4311Sfengbojiang     listNode *ln;
297*572c4311Sfengbojiang 
298*572c4311Sfengbojiang     if (dictSize(db->watched_keys) == 0) return;
299*572c4311Sfengbojiang     clients = dictFetchValue(db->watched_keys, key);
300*572c4311Sfengbojiang     if (!clients) return;
301*572c4311Sfengbojiang 
302*572c4311Sfengbojiang     /* Mark all the clients watching this key as CLIENT_DIRTY_CAS */
303*572c4311Sfengbojiang     /* Check if we are already watching for this key */
304*572c4311Sfengbojiang     listRewind(clients,&li);
305*572c4311Sfengbojiang     while((ln = listNext(&li))) {
306*572c4311Sfengbojiang         client *c = listNodeValue(ln);
307*572c4311Sfengbojiang 
308*572c4311Sfengbojiang         c->flags |= CLIENT_DIRTY_CAS;
309*572c4311Sfengbojiang     }
310*572c4311Sfengbojiang }
311*572c4311Sfengbojiang 
312*572c4311Sfengbojiang /* On FLUSHDB or FLUSHALL all the watched keys that are present before the
313*572c4311Sfengbojiang  * flush but will be deleted as effect of the flushing operation should
314*572c4311Sfengbojiang  * be touched. "dbid" is the DB that's getting the flush. -1 if it is
315*572c4311Sfengbojiang  * a FLUSHALL operation (all the DBs flushed). */
touchWatchedKeysOnFlush(int dbid)316*572c4311Sfengbojiang void touchWatchedKeysOnFlush(int dbid) {
317*572c4311Sfengbojiang     listIter li1, li2;
318*572c4311Sfengbojiang     listNode *ln;
319*572c4311Sfengbojiang 
320*572c4311Sfengbojiang     /* For every client, check all the waited keys */
321*572c4311Sfengbojiang     listRewind(server.clients,&li1);
322*572c4311Sfengbojiang     while((ln = listNext(&li1))) {
323*572c4311Sfengbojiang         client *c = listNodeValue(ln);
324*572c4311Sfengbojiang         listRewind(c->watched_keys,&li2);
325*572c4311Sfengbojiang         while((ln = listNext(&li2))) {
326*572c4311Sfengbojiang             watchedKey *wk = listNodeValue(ln);
327*572c4311Sfengbojiang 
328*572c4311Sfengbojiang             /* For every watched key matching the specified DB, if the
329*572c4311Sfengbojiang              * key exists, mark the client as dirty, as the key will be
330*572c4311Sfengbojiang              * removed. */
331*572c4311Sfengbojiang             if (dbid == -1 || wk->db->id == dbid) {
332*572c4311Sfengbojiang                 if (dictFind(wk->db->dict, wk->key->ptr) != NULL)
333*572c4311Sfengbojiang                     c->flags |= CLIENT_DIRTY_CAS;
334*572c4311Sfengbojiang             }
335*572c4311Sfengbojiang         }
336*572c4311Sfengbojiang     }
337*572c4311Sfengbojiang }
338*572c4311Sfengbojiang 
watchCommand(client * c)339*572c4311Sfengbojiang void watchCommand(client *c) {
340*572c4311Sfengbojiang     int j;
341*572c4311Sfengbojiang 
342*572c4311Sfengbojiang     if (c->flags & CLIENT_MULTI) {
343*572c4311Sfengbojiang         addReplyError(c,"WATCH inside MULTI is not allowed");
344*572c4311Sfengbojiang         return;
345*572c4311Sfengbojiang     }
346*572c4311Sfengbojiang     for (j = 1; j < c->argc; j++)
347*572c4311Sfengbojiang         watchForKey(c,c->argv[j]);
348*572c4311Sfengbojiang     addReply(c,shared.ok);
349*572c4311Sfengbojiang }
350*572c4311Sfengbojiang 
unwatchCommand(client * c)351*572c4311Sfengbojiang void unwatchCommand(client *c) {
352*572c4311Sfengbojiang     unwatchAllKeys(c);
353*572c4311Sfengbojiang     c->flags &= (~CLIENT_DIRTY_CAS);
354*572c4311Sfengbojiang     addReply(c,shared.ok);
355*572c4311Sfengbojiang }
356