/src/multi.c
C | 338 lines | 204 code | 40 blank | 94 comment | 39 complexity | f46ebb3c20e1255910ce7cb2cd984feb MD5 | raw file
1/*
2 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
16 *
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.
28 */
29
30#include "server.h"
31
32/* ================================ MULTI/EXEC ============================== */
33
34/* Client state initialization for MULTI/EXEC */
35void initClientMultiState(client *c) {
36 c->mstate.commands = NULL;
37 c->mstate.count = 0;
38}
39
40/* Release all the resources associated with MULTI/EXEC state */
41void freeClientMultiState(client *c) {
42 int j;
43
44 for (j = 0; j < c->mstate.count; j++) {
45 int i;
46 multiCmd *mc = c->mstate.commands+j;
47
48 for (i = 0; i < mc->argc; i++)
49 decrRefCount(mc->argv[i]);
50 zfree(mc->argv);
51 }
52 zfree(c->mstate.commands);
53}
54
55/* Add a new command into the MULTI commands queue */
56void queueMultiCommand(client *c) {
57 multiCmd *mc;
58 int j;
59
60 c->mstate.commands = zrealloc(c->mstate.commands,
61 sizeof(multiCmd)*(c->mstate.count+1));
62 mc = c->mstate.commands+c->mstate.count;
63 mc->cmd = c->cmd;
64 mc->argc = c->argc;
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]);
69 c->mstate.count++;
70}
71
72void discardTransaction(client *c) {
73 freeClientMultiState(c);
74 initClientMultiState(c);
75 c->flags &= ~(CLIENT_MULTI|CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC);
76 unwatchAllKeys(c);
77}
78
79/* Flag the transacation as DIRTY_EXEC so that EXEC will fail.
80 * Should be called every time there is an error while queueing a command. */
81void flagTransaction(client *c) {
82 if (c->flags & CLIENT_MULTI)
83 c->flags |= CLIENT_DIRTY_EXEC;
84}
85
86void multiCommand(client *c) {
87 if (c->flags & CLIENT_MULTI) {
88 addReplyError(c,"MULTI calls can not be nested");
89 return;
90 }
91 c->flags |= CLIENT_MULTI;
92 addReply(c,shared.ok);
93}
94
95void discardCommand(client *c) {
96 if (!(c->flags & CLIENT_MULTI)) {
97 addReplyError(c,"DISCARD without MULTI");
98 return;
99 }
100 discardTransaction(c);
101 addReply(c,shared.ok);
102}
103
104/* Send a MULTI command to all the slaves and AOF file. Check the execCommand
105 * implementation for more information. */
106void execCommandPropagateMulti(client *c) {
107 robj *multistring = createStringObject("MULTI",5);
108
109 propagate(server.multiCommand,c->db->id,&multistring,1,
110 PROPAGATE_AOF|PROPAGATE_REPL);
111 decrRefCount(multistring);
112}
113
114void execCommand(client *c) {
115 int j;
116 robj **orig_argv;
117 int orig_argc;
118 struct redisCommand *orig_cmd;
119 int must_propagate = 0; /* Need to propagate MULTI/EXEC to AOF / slaves? */
120 int was_master = server.masterhost == NULL;
121
122 if (!(c->flags & CLIENT_MULTI)) {
123 addReplyError(c,"EXEC without MULTI");
124 return;
125 }
126
127 /* Check if we need to abort the EXEC because:
128 * 1) Some WATCHed key was touched.
129 * 2) There was a previous error while queueing commands.
130 * A failed EXEC in the first case returns a multi bulk nil object
131 * (technically it is not an error but a special behavior), while
132 * in the second an EXECABORT error is returned. */
133 if (c->flags & (CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC)) {
134 addReply(c, c->flags & CLIENT_DIRTY_EXEC ? shared.execaborterr :
135 shared.nullmultibulk);
136 discardTransaction(c);
137 goto handle_monitor;
138 }
139
140 /* Exec all the queued commands */
141 unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */
142 orig_argv = c->argv;
143 orig_argc = c->argc;
144 orig_cmd = c->cmd;
145 addReplyMultiBulkLen(c,c->mstate.count);
146 for (j = 0; j < c->mstate.count; j++) {
147 c->argc = c->mstate.commands[j].argc;
148 c->argv = c->mstate.commands[j].argv;
149 c->cmd = c->mstate.commands[j].cmd;
150
151 /* Propagate a MULTI request once we encounter the first command which
152 * is not readonly nor an administrative one.
153 * This way we'll deliver the MULTI/..../EXEC block as a whole and
154 * both the AOF and the replication link will have the same consistency
155 * and atomicity guarantees. */
156 if (!must_propagate && !(c->cmd->flags & (CMD_READONLY|CMD_ADMIN))) {
157 execCommandPropagateMulti(c);
158 must_propagate = 1;
159 }
160
161 call(c,CMD_CALL_FULL);
162
163 /* Commands may alter argc/argv, restore mstate. */
164 c->mstate.commands[j].argc = c->argc;
165 c->mstate.commands[j].argv = c->argv;
166 c->mstate.commands[j].cmd = c->cmd;
167 }
168 c->argv = orig_argv;
169 c->argc = orig_argc;
170 c->cmd = orig_cmd;
171 discardTransaction(c);
172
173 /* Make sure the EXEC command will be propagated as well if MULTI
174 * was already propagated. */
175 if (must_propagate) {
176 int is_master = server.masterhost == NULL;
177 server.dirty++;
178 /* If inside the MULTI/EXEC block this instance was suddenly
179 * switched from master to slave (using the SLAVEOF command), the
180 * initial MULTI was propagated into the replication backlog, but the
181 * rest was not. We need to make sure to at least terminate the
182 * backlog with the final EXEC. */
183 if (server.repl_backlog && was_master && !is_master) {
184 char *execcmd = "*1\r\n$4\r\nEXEC\r\n";
185 feedReplicationBacklog(execcmd,strlen(execcmd));
186 }
187 }
188
189handle_monitor:
190 /* Send EXEC to clients waiting data from MONITOR. We do it here
191 * since the natural order of commands execution is actually:
192 * MUTLI, EXEC, ... commands inside transaction ...
193 * Instead EXEC is flagged as CMD_SKIP_MONITOR in the command
194 * table, and we do it here with correct ordering. */
195 if (listLength(server.monitors) && !server.loading)
196 replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
197}
198
199/* ===================== WATCH (CAS alike for MULTI/EXEC) ===================
200 *
201 * The implementation uses a per-DB hash table mapping keys to list of clients
202 * WATCHing those keys, so that given a key that is going to be modified
203 * we can mark all the associated clients as dirty.
204 *
205 * Also every client contains a list of WATCHed keys so that's possible to
206 * un-watch such keys when the client is freed or when UNWATCH is called. */
207
208/* In the client->watched_keys list we need to use watchedKey structures
209 * as in order to identify a key in Redis we need both the key name and the
210 * DB */
211typedef struct watchedKey {
212 robj *key;
213 redisDb *db;
214} watchedKey;
215
216/* Watch for the specified key */
217void watchForKey(client *c, robj *key) {
218 list *clients = NULL;
219 listIter li;
220 listNode *ln;
221 watchedKey *wk;
222
223 /* Check if we are already watching for this key */
224 listRewind(c->watched_keys,&li);
225 while((ln = listNext(&li))) {
226 wk = listNodeValue(ln);
227 if (wk->db == c->db && equalStringObjects(key,wk->key))
228 return; /* Key already watched */
229 }
230 /* This key is not already watched in this DB. Let's add it */
231 clients = dictFetchValue(c->db->watched_keys,key);
232 if (!clients) {
233 clients = listCreate();
234 dictAdd(c->db->watched_keys,key,clients);
235 incrRefCount(key);
236 }
237 listAddNodeTail(clients,c);
238 /* Add the new key to the list of keys watched by this client */
239 wk = zmalloc(sizeof(*wk));
240 wk->key = key;
241 wk->db = c->db;
242 incrRefCount(key);
243 listAddNodeTail(c->watched_keys,wk);
244}
245
246/* Unwatch all the keys watched by this client. To clean the EXEC dirty
247 * flag is up to the caller. */
248void unwatchAllKeys(client *c) {
249 listIter li;
250 listNode *ln;
251
252 if (listLength(c->watched_keys) == 0) return;
253 listRewind(c->watched_keys,&li);
254 while((ln = listNext(&li))) {
255 list *clients;
256 watchedKey *wk;
257
258 /* Lookup the watched key -> clients list and remove the client
259 * from the list */
260 wk = listNodeValue(ln);
261 clients = dictFetchValue(wk->db->watched_keys, wk->key);
262 serverAssertWithInfo(c,NULL,clients != NULL);
263 listDelNode(clients,listSearchKey(clients,c));
264 /* Kill the entry at all if this was the only client */
265 if (listLength(clients) == 0)
266 dictDelete(wk->db->watched_keys, wk->key);
267 /* Remove this watched key from the client->watched list */
268 listDelNode(c->watched_keys,ln);
269 decrRefCount(wk->key);
270 zfree(wk);
271 }
272}
273
274/* "Touch" a key, so that if this key is being WATCHed by some client the
275 * next EXEC will fail. */
276void touchWatchedKey(redisDb *db, robj *key) {
277 list *clients;
278 listIter li;
279 listNode *ln;
280
281 if (dictSize(db->watched_keys) == 0) return;
282 clients = dictFetchValue(db->watched_keys, key);
283 if (!clients) return;
284
285 /* Mark all the clients watching this key as CLIENT_DIRTY_CAS */
286 /* Check if we are already watching for this key */
287 listRewind(clients,&li);
288 while((ln = listNext(&li))) {
289 client *c = listNodeValue(ln);
290
291 c->flags |= CLIENT_DIRTY_CAS;
292 }
293}
294
295/* On FLUSHDB or FLUSHALL all the watched keys that are present before the
296 * flush but will be deleted as effect of the flushing operation should
297 * be touched. "dbid" is the DB that's getting the flush. -1 if it is
298 * a FLUSHALL operation (all the DBs flushed). */
299void touchWatchedKeysOnFlush(int dbid) {
300 listIter li1, li2;
301 listNode *ln;
302
303 /* For every client, check all the waited keys */
304 listRewind(server.clients,&li1);
305 while((ln = listNext(&li1))) {
306 client *c = listNodeValue(ln);
307 listRewind(c->watched_keys,&li2);
308 while((ln = listNext(&li2))) {
309 watchedKey *wk = listNodeValue(ln);
310
311 /* For every watched key matching the specified DB, if the
312 * key exists, mark the client as dirty, as the key will be
313 * removed. */
314 if (dbid == -1 || wk->db->id == dbid) {
315 if (dictFind(wk->db->dict, wk->key->ptr) != NULL)
316 c->flags |= CLIENT_DIRTY_CAS;
317 }
318 }
319 }
320}
321
322void watchCommand(client *c) {
323 int j;
324
325 if (c->flags & CLIENT_MULTI) {
326 addReplyError(c,"WATCH inside MULTI is not allowed");
327 return;
328 }
329 for (j = 1; j < c->argc; j++)
330 watchForKey(c,c->argv[j]);
331 addReply(c,shared.ok);
332}
333
334void unwatchCommand(client *c) {
335 unwatchAllKeys(c);
336 c->flags &= (~CLIENT_DIRTY_CAS);
337 addReply(c,shared.ok);
338}