14365e5b2Santirez /*
24365e5b2Santirez * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
34365e5b2Santirez * All rights reserved.
44365e5b2Santirez *
54365e5b2Santirez * Redistribution and use in source and binary forms, with or without
64365e5b2Santirez * modification, are permitted provided that the following conditions are met:
74365e5b2Santirez *
84365e5b2Santirez * * Redistributions of source code must retain the above copyright notice,
94365e5b2Santirez * this list of conditions and the following disclaimer.
104365e5b2Santirez * * Redistributions in binary form must reproduce the above copyright
114365e5b2Santirez * notice, this list of conditions and the following disclaimer in the
124365e5b2Santirez * documentation and/or other materials provided with the distribution.
134365e5b2Santirez * * Neither the name of Redis nor the names of its contributors may be used
144365e5b2Santirez * to endorse or promote products derived from this software without
154365e5b2Santirez * specific prior written permission.
164365e5b2Santirez *
174365e5b2Santirez * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
184365e5b2Santirez * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194365e5b2Santirez * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204365e5b2Santirez * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
214365e5b2Santirez * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
224365e5b2Santirez * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
234365e5b2Santirez * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
244365e5b2Santirez * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
254365e5b2Santirez * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
264365e5b2Santirez * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
274365e5b2Santirez * POSSIBILITY OF SUCH DAMAGE.
284365e5b2Santirez */
294365e5b2Santirez
30cef054e8Santirez #include "server.h"
31e2641e09Santirez
32e2641e09Santirez /* ================================ MULTI/EXEC ============================== */
33e2641e09Santirez
34e2641e09Santirez /* Client state initialization for MULTI/EXEC */
initClientMultiState(client * c)35554bd0e7Santirez void initClientMultiState(client *c) {
36e2641e09Santirez c->mstate.commands = NULL;
37e2641e09Santirez c->mstate.count = 0;
38e2641e09Santirez }
39e2641e09Santirez
40e2641e09Santirez /* Release all the resources associated with MULTI/EXEC state */
freeClientMultiState(client * c)41554bd0e7Santirez void freeClientMultiState(client *c) {
42e2641e09Santirez int j;
43e2641e09Santirez
44e2641e09Santirez for (j = 0; j < c->mstate.count; j++) {
45e2641e09Santirez int i;
46e2641e09Santirez multiCmd *mc = c->mstate.commands+j;
47e2641e09Santirez
48e2641e09Santirez for (i = 0; i < mc->argc; i++)
49e2641e09Santirez decrRefCount(mc->argv[i]);
50e2641e09Santirez zfree(mc->argv);
51e2641e09Santirez }
52e2641e09Santirez zfree(c->mstate.commands);
53e2641e09Santirez }
54e2641e09Santirez
55e2641e09Santirez /* Add a new command into the MULTI commands queue */
queueMultiCommand(client * c)56554bd0e7Santirez void queueMultiCommand(client *c) {
57e2641e09Santirez multiCmd *mc;
58e2641e09Santirez int j;
59e2641e09Santirez
60e2641e09Santirez c->mstate.commands = zrealloc(c->mstate.commands,
61e2641e09Santirez sizeof(multiCmd)*(c->mstate.count+1));
62e2641e09Santirez mc = c->mstate.commands+c->mstate.count;
6309e2d9eeSantirez mc->cmd = c->cmd;
64e2641e09Santirez mc->argc = c->argc;
65e2641e09Santirez mc->argv = zmalloc(sizeof(robj*)*c->argc);
66e2641e09Santirez memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
67e2641e09Santirez for (j = 0; j < c->argc; j++)
68e2641e09Santirez incrRefCount(mc->argv[j]);
69e2641e09Santirez c->mstate.count++;
70e2641e09Santirez }
71e2641e09Santirez
discardTransaction(client * c)72554bd0e7Santirez void discardTransaction(client *c) {
73f3fd419fSantirez freeClientMultiState(c);
74f3fd419fSantirez initClientMultiState(c);
75*32f80e2fSantirez c->flags &= ~(CLIENT_MULTI|CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC);
76f3fd419fSantirez unwatchAllKeys(c);
77f3fd419fSantirez }
78f3fd419fSantirez
793d139127Santirez /* Flag the transacation as DIRTY_EXEC so that EXEC will fail.
803d139127Santirez * Should be called every time there is an error while queueing a command. */
flagTransaction(client * c)81554bd0e7Santirez void flagTransaction(client *c) {
82*32f80e2fSantirez if (c->flags & CLIENT_MULTI)
83*32f80e2fSantirez c->flags |= CLIENT_DIRTY_EXEC;
843d139127Santirez }
853d139127Santirez
multiCommand(client * c)86554bd0e7Santirez void multiCommand(client *c) {
87*32f80e2fSantirez if (c->flags & CLIENT_MULTI) {
883ab20376SPieter Noordhuis addReplyError(c,"MULTI calls can not be nested");
89e2641e09Santirez return;
90e2641e09Santirez }
91*32f80e2fSantirez c->flags |= CLIENT_MULTI;
92e2641e09Santirez addReply(c,shared.ok);
93e2641e09Santirez }
94e2641e09Santirez
discardCommand(client * c)95554bd0e7Santirez void discardCommand(client *c) {
96*32f80e2fSantirez if (!(c->flags & CLIENT_MULTI)) {
973ab20376SPieter Noordhuis addReplyError(c,"DISCARD without MULTI");
98e2641e09Santirez return;
99e2641e09Santirez }
100f3fd419fSantirez discardTransaction(c);
101e2641e09Santirez addReply(c,shared.ok);
102e2641e09Santirez }
103e2641e09Santirez
104e2641e09Santirez /* Send a MULTI command to all the slaves and AOF file. Check the execCommand
1059d09ce39Sguiquanz * implementation for more information. */
execCommandPropagateMulti(client * c)106554bd0e7Santirez void execCommandPropagateMulti(client *c) {
107e2641e09Santirez robj *multistring = createStringObject("MULTI",5);
108e2641e09Santirez
1092f497340Santirez propagate(server.multiCommand,c->db->id,&multistring,1,
110*32f80e2fSantirez PROPAGATE_AOF|PROPAGATE_REPL);
111e2641e09Santirez decrRefCount(multistring);
112e2641e09Santirez }
113e2641e09Santirez
execCommand(client * c)114554bd0e7Santirez void execCommand(client *c) {
115e2641e09Santirez int j;
116e2641e09Santirez robj **orig_argv;
117e2641e09Santirez int orig_argc;
11809e2d9eeSantirez struct redisCommand *orig_cmd;
11971f3e743Santirez int must_propagate = 0; /* Need to propagate MULTI/EXEC to AOF / slaves? */
120e2641e09Santirez
121*32f80e2fSantirez if (!(c->flags & CLIENT_MULTI)) {
1223ab20376SPieter Noordhuis addReplyError(c,"EXEC without MULTI");
123e2641e09Santirez return;
124e2641e09Santirez }
125e2641e09Santirez
1263d139127Santirez /* Check if we need to abort the EXEC because:
1273d139127Santirez * 1) Some WATCHed key was touched.
1283d139127Santirez * 2) There was a previous error while queueing commands.
1293d139127Santirez * A failed EXEC in the first case returns a multi bulk nil object
1303d139127Santirez * (technically it is not an error but a special behavior), while
1313d139127Santirez * in the second an EXECABORT error is returned. */
132*32f80e2fSantirez if (c->flags & (CLIENT_DIRTY_CAS|CLIENT_DIRTY_EXEC)) {
133*32f80e2fSantirez addReply(c, c->flags & CLIENT_DIRTY_EXEC ? shared.execaborterr :
1343d139127Santirez shared.nullmultibulk);
13502c269e2Santirez discardTransaction(c);
136a1b1c1eaSantirez goto handle_monitor;
137e2641e09Santirez }
138e2641e09Santirez
139e2641e09Santirez /* Exec all the queued commands */
140e2641e09Santirez unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */
141e2641e09Santirez orig_argv = c->argv;
142e2641e09Santirez orig_argc = c->argc;
14309e2d9eeSantirez orig_cmd = c->cmd;
1440537e7bfSPieter Noordhuis addReplyMultiBulkLen(c,c->mstate.count);
145e2641e09Santirez for (j = 0; j < c->mstate.count; j++) {
146e2641e09Santirez c->argc = c->mstate.commands[j].argc;
147e2641e09Santirez c->argv = c->mstate.commands[j].argv;
14809e2d9eeSantirez c->cmd = c->mstate.commands[j].cmd;
14971f3e743Santirez
15071f3e743Santirez /* Propagate a MULTI request once we encounter the first write op.
15171f3e743Santirez * This way we'll deliver the MULTI/..../EXEC block as a whole and
15271f3e743Santirez * both the AOF and the replication link will have the same consistency
15371f3e743Santirez * and atomicity guarantees. */
154*32f80e2fSantirez if (!must_propagate && !(c->cmd->flags & CMD_READONLY)) {
15571f3e743Santirez execCommandPropagateMulti(c);
15671f3e743Santirez must_propagate = 1;
15771f3e743Santirez }
15871f3e743Santirez
159*32f80e2fSantirez call(c,CMD_CALL_FULL);
1606c682e55SPieter Noordhuis
1616c682e55SPieter Noordhuis /* Commands may alter argc/argv, restore mstate. */
1626c682e55SPieter Noordhuis c->mstate.commands[j].argc = c->argc;
1636c682e55SPieter Noordhuis c->mstate.commands[j].argv = c->argv;
16409e2d9eeSantirez c->mstate.commands[j].cmd = c->cmd;
165e2641e09Santirez }
166e2641e09Santirez c->argv = orig_argv;
167e2641e09Santirez c->argc = orig_argc;
16809e2d9eeSantirez c->cmd = orig_cmd;
16902c269e2Santirez discardTransaction(c);
17071f3e743Santirez /* Make sure the EXEC command will be propagated as well if MULTI
17171f3e743Santirez * was already propagated. */
17271f3e743Santirez if (must_propagate) server.dirty++;
173a1b1c1eaSantirez
174a1b1c1eaSantirez handle_monitor:
175a1b1c1eaSantirez /* Send EXEC to clients waiting data from MONITOR. We do it here
176a1b1c1eaSantirez * since the natural order of commands execution is actually:
177a1b1c1eaSantirez * MUTLI, EXEC, ... commands inside transaction ...
178*32f80e2fSantirez * Instead EXEC is flagged as CMD_SKIP_MONITOR in the command
179a1b1c1eaSantirez * table, and we do it here with correct ordering. */
180a1b1c1eaSantirez if (listLength(server.monitors) && !server.loading)
181a1b1c1eaSantirez replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
182e2641e09Santirez }
183e2641e09Santirez
184e2641e09Santirez /* ===================== WATCH (CAS alike for MULTI/EXEC) ===================
185e2641e09Santirez *
186e2641e09Santirez * The implementation uses a per-DB hash table mapping keys to list of clients
187e2641e09Santirez * WATCHing those keys, so that given a key that is going to be modified
188e2641e09Santirez * we can mark all the associated clients as dirty.
189e2641e09Santirez *
190e2641e09Santirez * Also every client contains a list of WATCHed keys so that's possible to
191e2641e09Santirez * un-watch such keys when the client is freed or when UNWATCH is called. */
192e2641e09Santirez
193e2641e09Santirez /* In the client->watched_keys list we need to use watchedKey structures
194e2641e09Santirez * as in order to identify a key in Redis we need both the key name and the
195e2641e09Santirez * DB */
196e2641e09Santirez typedef struct watchedKey {
197e2641e09Santirez robj *key;
198e2641e09Santirez redisDb *db;
199e2641e09Santirez } watchedKey;
200e2641e09Santirez
201e2641e09Santirez /* Watch for the specified key */
watchForKey(client * c,robj * key)202554bd0e7Santirez void watchForKey(client *c, robj *key) {
203e2641e09Santirez list *clients = NULL;
204e2641e09Santirez listIter li;
205e2641e09Santirez listNode *ln;
206e2641e09Santirez watchedKey *wk;
207e2641e09Santirez
208e2641e09Santirez /* Check if we are already watching for this key */
209e2641e09Santirez listRewind(c->watched_keys,&li);
210e2641e09Santirez while((ln = listNext(&li))) {
211e2641e09Santirez wk = listNodeValue(ln);
212e2641e09Santirez if (wk->db == c->db && equalStringObjects(key,wk->key))
213e2641e09Santirez return; /* Key already watched */
214e2641e09Santirez }
215e2641e09Santirez /* This key is not already watched in this DB. Let's add it */
216e2641e09Santirez clients = dictFetchValue(c->db->watched_keys,key);
217e2641e09Santirez if (!clients) {
218e2641e09Santirez clients = listCreate();
219e2641e09Santirez dictAdd(c->db->watched_keys,key,clients);
220e2641e09Santirez incrRefCount(key);
221e2641e09Santirez }
222e2641e09Santirez listAddNodeTail(clients,c);
2239d09ce39Sguiquanz /* Add the new key to the list of keys watched by this client */
224e2641e09Santirez wk = zmalloc(sizeof(*wk));
225e2641e09Santirez wk->key = key;
226e2641e09Santirez wk->db = c->db;
227e2641e09Santirez incrRefCount(key);
228e2641e09Santirez listAddNodeTail(c->watched_keys,wk);
229e2641e09Santirez }
230e2641e09Santirez
231e2641e09Santirez /* Unwatch all the keys watched by this client. To clean the EXEC dirty
232e2641e09Santirez * flag is up to the caller. */
unwatchAllKeys(client * c)233554bd0e7Santirez void unwatchAllKeys(client *c) {
234e2641e09Santirez listIter li;
235e2641e09Santirez listNode *ln;
236e2641e09Santirez
237e2641e09Santirez if (listLength(c->watched_keys) == 0) return;
238e2641e09Santirez listRewind(c->watched_keys,&li);
239e2641e09Santirez while((ln = listNext(&li))) {
240e2641e09Santirez list *clients;
241e2641e09Santirez watchedKey *wk;
242e2641e09Santirez
243e2641e09Santirez /* Lookup the watched key -> clients list and remove the client
244e2641e09Santirez * from the list */
245e2641e09Santirez wk = listNodeValue(ln);
246e2641e09Santirez clients = dictFetchValue(wk->db->watched_keys, wk->key);
2472d9e3eb1Santirez serverAssertWithInfo(c,NULL,clients != NULL);
248e2641e09Santirez listDelNode(clients,listSearchKey(clients,c));
249e2641e09Santirez /* Kill the entry at all if this was the only client */
250e2641e09Santirez if (listLength(clients) == 0)
251e2641e09Santirez dictDelete(wk->db->watched_keys, wk->key);
252e2641e09Santirez /* Remove this watched key from the client->watched list */
253e2641e09Santirez listDelNode(c->watched_keys,ln);
254e2641e09Santirez decrRefCount(wk->key);
255e2641e09Santirez zfree(wk);
256e2641e09Santirez }
257e2641e09Santirez }
258e2641e09Santirez
259e2641e09Santirez /* "Touch" a key, so that if this key is being WATCHed by some client the
260e2641e09Santirez * next EXEC will fail. */
touchWatchedKey(redisDb * db,robj * key)261e2641e09Santirez void touchWatchedKey(redisDb *db, robj *key) {
262e2641e09Santirez list *clients;
263e2641e09Santirez listIter li;
264e2641e09Santirez listNode *ln;
265e2641e09Santirez
266e2641e09Santirez if (dictSize(db->watched_keys) == 0) return;
267e2641e09Santirez clients = dictFetchValue(db->watched_keys, key);
268e2641e09Santirez if (!clients) return;
269e2641e09Santirez
270*32f80e2fSantirez /* Mark all the clients watching this key as CLIENT_DIRTY_CAS */
271e2641e09Santirez /* Check if we are already watching for this key */
272e2641e09Santirez listRewind(clients,&li);
273e2641e09Santirez while((ln = listNext(&li))) {
274554bd0e7Santirez client *c = listNodeValue(ln);
275e2641e09Santirez
276*32f80e2fSantirez c->flags |= CLIENT_DIRTY_CAS;
277e2641e09Santirez }
278e2641e09Santirez }
279e2641e09Santirez
280e2641e09Santirez /* On FLUSHDB or FLUSHALL all the watched keys that are present before the
281e2641e09Santirez * flush but will be deleted as effect of the flushing operation should
282e2641e09Santirez * be touched. "dbid" is the DB that's getting the flush. -1 if it is
283e2641e09Santirez * a FLUSHALL operation (all the DBs flushed). */
touchWatchedKeysOnFlush(int dbid)284e2641e09Santirez void touchWatchedKeysOnFlush(int dbid) {
285e2641e09Santirez listIter li1, li2;
286e2641e09Santirez listNode *ln;
287e2641e09Santirez
288e2641e09Santirez /* For every client, check all the waited keys */
289e2641e09Santirez listRewind(server.clients,&li1);
290e2641e09Santirez while((ln = listNext(&li1))) {
291554bd0e7Santirez client *c = listNodeValue(ln);
292e2641e09Santirez listRewind(c->watched_keys,&li2);
293e2641e09Santirez while((ln = listNext(&li2))) {
294e2641e09Santirez watchedKey *wk = listNodeValue(ln);
295e2641e09Santirez
296e2641e09Santirez /* For every watched key matching the specified DB, if the
297e2641e09Santirez * key exists, mark the client as dirty, as the key will be
298e2641e09Santirez * removed. */
299e2641e09Santirez if (dbid == -1 || wk->db->id == dbid) {
300e2641e09Santirez if (dictFind(wk->db->dict, wk->key->ptr) != NULL)
301*32f80e2fSantirez c->flags |= CLIENT_DIRTY_CAS;
302e2641e09Santirez }
303e2641e09Santirez }
304e2641e09Santirez }
305e2641e09Santirez }
306e2641e09Santirez
watchCommand(client * c)307554bd0e7Santirez void watchCommand(client *c) {
308e2641e09Santirez int j;
309e2641e09Santirez
310*32f80e2fSantirez if (c->flags & CLIENT_MULTI) {
3113ab20376SPieter Noordhuis addReplyError(c,"WATCH inside MULTI is not allowed");
312e2641e09Santirez return;
313e2641e09Santirez }
314e2641e09Santirez for (j = 1; j < c->argc; j++)
315e2641e09Santirez watchForKey(c,c->argv[j]);
316e2641e09Santirez addReply(c,shared.ok);
317e2641e09Santirez }
318e2641e09Santirez
unwatchCommand(client * c)319554bd0e7Santirez void unwatchCommand(client *c) {
320e2641e09Santirez unwatchAllKeys(c);
321*32f80e2fSantirez c->flags &= (~CLIENT_DIRTY_CAS);
322e2641e09Santirez addReply(c,shared.ok);
323e2641e09Santirez }
324