]> git.saurik.com Git - redis.git/blame - redis.c
Redis version is now 1.3.12
[redis.git] / redis.c
CommitLineData
ed9b544e 1/*
12d090d2 2 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
ed9b544e 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
26ef09a8 30#define REDIS_VERSION "1.3.12"
23d4709d 31
32#include "fmacros.h"
fbf9bcdb 33#include "config.h"
ed9b544e 34
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <time.h>
39#include <unistd.h>
40#include <signal.h>
fbf9bcdb 41
42#ifdef HAVE_BACKTRACE
c9468bcf 43#include <execinfo.h>
44#include <ucontext.h>
fbf9bcdb 45#endif /* HAVE_BACKTRACE */
46
ed9b544e 47#include <sys/wait.h>
48#include <errno.h>
49#include <assert.h>
50#include <ctype.h>
51#include <stdarg.h>
52#include <inttypes.h>
53#include <arpa/inet.h>
54#include <sys/stat.h>
55#include <fcntl.h>
56#include <sys/time.h>
57#include <sys/resource.h>
2895e862 58#include <sys/uio.h>
f78fd11b 59#include <limits.h>
fb82e75c 60#include <float.h>
a7866db6 61#include <math.h>
92f8e882 62#include <pthread.h>
0bc1b2f6 63
64#if defined(__sun)
5043dff3 65#include "solarisfixes.h"
66#endif
ed9b544e 67
c9468bcf 68#include "redis.h"
ed9b544e 69#include "ae.h" /* Event driven programming library */
70#include "sds.h" /* Dynamic safe strings */
71#include "anet.h" /* Networking the easy way */
72#include "dict.h" /* Hash tables */
73#include "adlist.h" /* Linked lists */
74#include "zmalloc.h" /* total memory usage aware version of malloc/free */
5f5b9840 75#include "lzf.h" /* LZF compression library */
76#include "pqsort.h" /* Partial qsort for SORT+LIMIT */
ba798261 77#include "zipmap.h" /* Compact dictionary-alike data structure */
78#include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
ed9b544e 79
80/* Error codes */
81#define REDIS_OK 0
82#define REDIS_ERR -1
83
84/* Static server configuration */
85#define REDIS_SERVERPORT 6379 /* TCP port */
86#define REDIS_MAXIDLETIME (60*5) /* default client timeout */
6208b3a7 87#define REDIS_IOBUF_LEN 1024
ed9b544e 88#define REDIS_LOADBUF_LEN 1024
248ea310 89#define REDIS_STATIC_ARGS 8
ed9b544e 90#define REDIS_DEFAULT_DBNUM 16
91#define REDIS_CONFIGLINE_MAX 1024
92#define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
93#define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
8ca3e9d1 94#define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */
6f376729 95#define REDIS_MAX_WRITE_PER_EVENT (1024*64)
2895e862 96#define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
97
98/* If more then REDIS_WRITEV_THRESHOLD write packets are pending use writev */
99#define REDIS_WRITEV_THRESHOLD 3
100/* Max number of iovecs used for each writev call */
101#define REDIS_WRITEV_IOVEC_COUNT 256
ed9b544e 102
103/* Hash table parameters */
104#define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
ed9b544e 105
106/* Command flags */
3fd78bcd 107#define REDIS_CMD_BULK 1 /* Bulk write command */
108#define REDIS_CMD_INLINE 2 /* Inline command */
109/* REDIS_CMD_DENYOOM reserves a longer comment: all the commands marked with
110 this flags will return an error when the 'maxmemory' option is set in the
111 config file and the server is using more than maxmemory bytes of memory.
112 In short this commands are denied on low memory conditions. */
113#define REDIS_CMD_DENYOOM 4
4005fef1 114#define REDIS_CMD_FORCE_REPLICATION 8 /* Force replication even if dirty is 0 */
ed9b544e 115
116/* Object types */
117#define REDIS_STRING 0
118#define REDIS_LIST 1
119#define REDIS_SET 2
1812e024 120#define REDIS_ZSET 3
121#define REDIS_HASH 4
f78fd11b 122
5234952b 123/* Objects encoding. Some kind of objects like Strings and Hashes can be
124 * internally represented in multiple ways. The 'encoding' field of the object
125 * is set to one of this fields for this object. */
942a3961 126#define REDIS_ENCODING_RAW 0 /* Raw representation */
127#define REDIS_ENCODING_INT 1 /* Encoded as integer */
5234952b 128#define REDIS_ENCODING_ZIPMAP 2 /* Encoded as zipmap */
129#define REDIS_ENCODING_HT 3 /* Encoded as an hash table */
942a3961 130
07efaf74 131static char* strencoding[] = {
132 "raw", "int", "zipmap", "hashtable"
133};
134
f78fd11b 135/* Object types only used for dumping to disk */
bb32ede5 136#define REDIS_EXPIRETIME 253
ed9b544e 137#define REDIS_SELECTDB 254
138#define REDIS_EOF 255
139
f78fd11b 140/* Defines related to the dump file format. To store 32 bits lengths for short
141 * keys requires a lot of space, so we check the most significant 2 bits of
142 * the first byte to interpreter the length:
143 *
144 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
145 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
146 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
a4d1ba9a 147 * 11|000000 this means: specially encoded object will follow. The six bits
148 * number specify the kind of object that follows.
149 * See the REDIS_RDB_ENC_* defines.
f78fd11b 150 *
10c43610 151 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
152 * values, will fit inside. */
f78fd11b 153#define REDIS_RDB_6BITLEN 0
154#define REDIS_RDB_14BITLEN 1
155#define REDIS_RDB_32BITLEN 2
17be1a4a 156#define REDIS_RDB_ENCVAL 3
f78fd11b 157#define REDIS_RDB_LENERR UINT_MAX
158
a4d1ba9a 159/* When a length of a string object stored on disk has the first two bits
160 * set, the remaining two bits specify a special encoding for the object
161 * accordingly to the following defines: */
162#define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
163#define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
164#define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
774e3047 165#define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
a4d1ba9a 166
75680a3c 167/* Virtual memory object->where field. */
168#define REDIS_VM_MEMORY 0 /* The object is on memory */
169#define REDIS_VM_SWAPPED 1 /* The object is on disk */
170#define REDIS_VM_SWAPPING 2 /* Redis is swapping this object on disk */
171#define REDIS_VM_LOADING 3 /* Redis is loading this object from disk */
172
06224fec 173/* Virtual memory static configuration stuff.
174 * Check vmFindContiguousPages() to know more about this magic numbers. */
175#define REDIS_VM_MAX_NEAR_PAGES 65536
176#define REDIS_VM_MAX_RANDOM_JUMP 4096
92f8e882 177#define REDIS_VM_MAX_THREADS 32
bcaa7a4f 178#define REDIS_THREAD_STACK_SIZE (1024*1024*4)
f6c0bba8 179/* The following is the *percentage* of completed I/O jobs to process when the
180 * handelr is called. While Virtual Memory I/O operations are performed by
181 * threads, this operations must be processed by the main thread when completed
182 * in order to take effect. */
c953f24b 183#define REDIS_MAX_COMPLETED_JOBS_PROCESSED 1
06224fec 184
ed9b544e 185/* Client flags */
d5d55fc3 186#define REDIS_SLAVE 1 /* This client is a slave server */
187#define REDIS_MASTER 2 /* This client is a master server */
188#define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */
189#define REDIS_MULTI 8 /* This client is in a MULTI context */
190#define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */
191#define REDIS_IO_WAIT 32 /* The client is waiting for Virtual Memory I/O */
ed9b544e 192
40d224a9 193/* Slave replication state - slave side */
ed9b544e 194#define REDIS_REPL_NONE 0 /* No active replication */
195#define REDIS_REPL_CONNECT 1 /* Must connect to master */
196#define REDIS_REPL_CONNECTED 2 /* Connected to master */
197
40d224a9 198/* Slave replication state - from the point of view of master
199 * Note that in SEND_BULK and ONLINE state the slave receives new updates
200 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
201 * to start the next background saving in order to send updates to it. */
202#define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
203#define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
204#define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
205#define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
206
ed9b544e 207/* List related stuff */
208#define REDIS_HEAD 0
209#define REDIS_TAIL 1
210
211/* Sort operations */
212#define REDIS_SORT_GET 0
443c6409 213#define REDIS_SORT_ASC 1
214#define REDIS_SORT_DESC 2
ed9b544e 215#define REDIS_SORTKEY_MAX 1024
216
217/* Log levels */
218#define REDIS_DEBUG 0
f870935d 219#define REDIS_VERBOSE 1
220#define REDIS_NOTICE 2
221#define REDIS_WARNING 3
ed9b544e 222
223/* Anti-warning macro... */
224#define REDIS_NOTUSED(V) ((void) V)
225
6b47e12e 226#define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
227#define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
ed9b544e 228
48f0308a 229/* Append only defines */
230#define APPENDFSYNC_NO 0
231#define APPENDFSYNC_ALWAYS 1
232#define APPENDFSYNC_EVERYSEC 2
233
cbba7dd7 234/* Hashes related defaults */
235#define REDIS_HASH_MAX_ZIPMAP_ENTRIES 64
236#define REDIS_HASH_MAX_ZIPMAP_VALUE 512
237
dfc5e96c 238/* We can print the stacktrace, so our assert is defined this way: */
478c2c6f 239#define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
c651fd9e 240#define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
6c96ba7d 241static void _redisAssert(char *estr, char *file, int line);
c651fd9e 242static void _redisPanic(char *msg, char *file, int line);
dfc5e96c 243
ed9b544e 244/*================================= Data types ============================== */
245
246/* A redis object, that is a type able to hold a string / list / set */
75680a3c 247
248/* The VM object structure */
249struct redisObjectVM {
3a66edc7 250 off_t page; /* the page at witch the object is stored on disk */
251 off_t usedpages; /* number of pages used on disk */
252 time_t atime; /* Last access time */
75680a3c 253} vm;
254
255/* The actual Redis Object */
ed9b544e 256typedef struct redisObject {
ed9b544e 257 void *ptr;
942a3961 258 unsigned char type;
259 unsigned char encoding;
d894161b 260 unsigned char storage; /* If this object is a key, where is the value?
261 * REDIS_VM_MEMORY, REDIS_VM_SWAPPED, ... */
262 unsigned char vtype; /* If this object is a key, and value is swapped out,
263 * this is the type of the swapped out object. */
ed9b544e 264 int refcount;
75680a3c 265 /* VM fields, this are only allocated if VM is active, otherwise the
266 * object allocation function will just allocate
267 * sizeof(redisObjct) minus sizeof(redisObjectVM), so using
268 * Redis without VM active will not have any overhead. */
269 struct redisObjectVM vm;
ed9b544e 270} robj;
271
dfc5e96c 272/* Macro used to initalize a Redis object allocated on the stack.
273 * Note that this macro is taken near the structure definition to make sure
274 * we'll update it when the structure is changed, to avoid bugs like
275 * bug #85 introduced exactly in this way. */
276#define initStaticStringObject(_var,_ptr) do { \
277 _var.refcount = 1; \
278 _var.type = REDIS_STRING; \
279 _var.encoding = REDIS_ENCODING_RAW; \
280 _var.ptr = _ptr; \
3a66edc7 281 if (server.vm_enabled) _var.storage = REDIS_VM_MEMORY; \
dfc5e96c 282} while(0);
283
3305306f 284typedef struct redisDb {
4409877e 285 dict *dict; /* The keyspace for this DB */
286 dict *expires; /* Timeout of keys with a timeout set */
287 dict *blockingkeys; /* Keys with clients waiting for data (BLPOP) */
d5d55fc3 288 dict *io_keys; /* Keys with clients waiting for VM I/O */
3305306f 289 int id;
290} redisDb;
291
6e469882 292/* Client MULTI/EXEC state */
293typedef struct multiCmd {
294 robj **argv;
295 int argc;
296 struct redisCommand *cmd;
297} multiCmd;
298
299typedef struct multiState {
300 multiCmd *commands; /* Array of MULTI commands */
301 int count; /* Total number of MULTI commands */
302} multiState;
303
ed9b544e 304/* With multiplexing we need to take per-clinet state.
305 * Clients are taken in a liked list. */
306typedef struct redisClient {
307 int fd;
3305306f 308 redisDb *db;
ed9b544e 309 int dictid;
310 sds querybuf;
e8a74421 311 robj **argv, **mbargv;
312 int argc, mbargc;
40d224a9 313 int bulklen; /* bulk read len. -1 if not in bulk read mode */
e8a74421 314 int multibulk; /* multi bulk command format active */
ed9b544e 315 list *reply;
316 int sentlen;
317 time_t lastinteraction; /* time of the last interaction, used for timeout */
d5d55fc3 318 int flags; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */
40d224a9 319 int slaveseldb; /* slave selected db, if this client is a slave */
320 int authenticated; /* when requirepass is non-NULL */
321 int replstate; /* replication state if this is a slave */
322 int repldbfd; /* replication DB file descriptor */
6e469882 323 long repldboff; /* replication DB file offset */
40d224a9 324 off_t repldbsize; /* replication DB file size */
6e469882 325 multiState mstate; /* MULTI/EXEC state */
d5d55fc3 326 robj **blockingkeys; /* The key we are waiting to terminate a blocking
4409877e 327 * operation such as BLPOP. Otherwise NULL. */
b177fd30 328 int blockingkeysnum; /* Number of blocking keys */
4409877e 329 time_t blockingto; /* Blocking operation timeout. If UNIX current time
330 * is >= blockingto then the operation timed out. */
92f8e882 331 list *io_keys; /* Keys this client is waiting to be loaded from the
332 * swap file in order to continue. */
ffc6b7f8 333 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
334 list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */
ed9b544e 335} redisClient;
336
337struct saveparam {
338 time_t seconds;
339 int changes;
340};
341
342/* Global server state structure */
343struct redisServer {
344 int port;
345 int fd;
3305306f 346 redisDb *db;
ed9b544e 347 long long dirty; /* changes to DB from the last save */
348 list *clients;
87eca727 349 list *slaves, *monitors;
ed9b544e 350 char neterr[ANET_ERR_LEN];
351 aeEventLoop *el;
352 int cronloops; /* number of times the cron function run */
353 list *objfreelist; /* A list of freed objects to avoid malloc() */
354 time_t lastsave; /* Unix time of last save succeeede */
ed9b544e 355 /* Fields used only for stats */
356 time_t stat_starttime; /* server start time */
357 long long stat_numcommands; /* number of processed commands */
358 long long stat_numconnections; /* number of connections received */
2a6a2ed1 359 long long stat_expiredkeys; /* number of expired keys */
ed9b544e 360 /* Configuration */
361 int verbosity;
362 int glueoutputbuf;
363 int maxidletime;
364 int dbnum;
365 int daemonize;
44b38ef4 366 int appendonly;
48f0308a 367 int appendfsync;
368 time_t lastfsync;
44b38ef4 369 int appendfd;
370 int appendseldb;
ed329fcf 371 char *pidfile;
9f3c422c 372 pid_t bgsavechildpid;
9d65a1bb 373 pid_t bgrewritechildpid;
374 sds bgrewritebuf; /* buffer taken by parent during oppend only rewrite */
28ed1f33 375 sds aofbuf; /* AOF buffer, written before entering the event loop */
ed9b544e 376 struct saveparam *saveparams;
377 int saveparamslen;
378 char *logfile;
379 char *bindaddr;
380 char *dbfilename;
44b38ef4 381 char *appendfilename;
abcb223e 382 char *requirepass;
121f70cf 383 int rdbcompression;
8ca3e9d1 384 int activerehashing;
ed9b544e 385 /* Replication related */
386 int isslave;
d0ccebcf 387 char *masterauth;
ed9b544e 388 char *masterhost;
389 int masterport;
40d224a9 390 redisClient *master; /* client that is master for this slave */
ed9b544e 391 int replstate;
285add55 392 unsigned int maxclients;
4ef8de8a 393 unsigned long long maxmemory;
d5d55fc3 394 unsigned int blpop_blocked_clients;
395 unsigned int vm_blocked_clients;
ed9b544e 396 /* Sort parameters - qsort_r() is only available under BSD so we
397 * have to take this state global, in order to pass it to sortCompare() */
398 int sort_desc;
399 int sort_alpha;
400 int sort_bypattern;
75680a3c 401 /* Virtual memory configuration */
402 int vm_enabled;
054e426d 403 char *vm_swap_file;
75680a3c 404 off_t vm_page_size;
405 off_t vm_pages;
4ef8de8a 406 unsigned long long vm_max_memory;
cbba7dd7 407 /* Hashes config */
408 size_t hash_max_zipmap_entries;
409 size_t hash_max_zipmap_value;
75680a3c 410 /* Virtual memory state */
411 FILE *vm_fp;
412 int vm_fd;
413 off_t vm_next_page; /* Next probably empty page */
414 off_t vm_near_pages; /* Number of pages allocated sequentially */
06224fec 415 unsigned char *vm_bitmap; /* Bitmap of free/used pages */
3a66edc7 416 time_t unixtime; /* Unix time sampled every second. */
92f8e882 417 /* Virtual memory I/O threads stuff */
92f8e882 418 /* An I/O thread process an element taken from the io_jobs queue and
996cb5f7 419 * put the result of the operation in the io_done list. While the
420 * job is being processed, it's put on io_processing queue. */
421 list *io_newjobs; /* List of VM I/O jobs yet to be processed */
422 list *io_processing; /* List of VM I/O jobs being processed */
423 list *io_processed; /* List of VM I/O jobs already processed */
d5d55fc3 424 list *io_ready_clients; /* Clients ready to be unblocked. All keys loaded */
996cb5f7 425 pthread_mutex_t io_mutex; /* lock to access io_jobs/io_done/io_thread_job */
a5819310 426 pthread_mutex_t obj_freelist_mutex; /* safe redis objects creation/free */
427 pthread_mutex_t io_swapfile_mutex; /* So we can lseek + write */
bcaa7a4f 428 pthread_attr_t io_threads_attr; /* attributes for threads creation */
92f8e882 429 int io_active_threads; /* Number of running I/O threads */
430 int vm_max_threads; /* Max number of I/O threads running at the same time */
996cb5f7 431 /* Our main thread is blocked on the event loop, locking for sockets ready
432 * to be read or written, so when a threaded I/O operation is ready to be
433 * processed by the main thread, the I/O thread will use a unix pipe to
434 * awake the main thread. The followings are the two pipe FDs. */
435 int io_ready_pipe_read;
436 int io_ready_pipe_write;
7d98e08c 437 /* Virtual memory stats */
438 unsigned long long vm_stats_used_pages;
439 unsigned long long vm_stats_swapped_objects;
440 unsigned long long vm_stats_swapouts;
441 unsigned long long vm_stats_swapins;
befec3cd 442 /* Pubsub */
ffc6b7f8 443 dict *pubsub_channels; /* Map channels to list of subscribed clients */
444 list *pubsub_patterns; /* A list of pubsub_patterns */
befec3cd 445 /* Misc */
b9bc0eef 446 FILE *devnull;
ed9b544e 447};
448
ffc6b7f8 449typedef struct pubsubPattern {
450 redisClient *client;
451 robj *pattern;
452} pubsubPattern;
453
ed9b544e 454typedef void redisCommandProc(redisClient *c);
ca1788b5 455typedef void redisVmPreloadProc(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
ed9b544e 456struct redisCommand {
457 char *name;
458 redisCommandProc *proc;
459 int arity;
460 int flags;
76583ea4
PN
461 /* Use a function to determine which keys need to be loaded
462 * in the background prior to executing this command. Takes precedence
463 * over vm_firstkey and others, ignored when NULL */
ca1788b5 464 redisVmPreloadProc *vm_preload_proc;
7c775e09 465 /* What keys should be loaded in background when calling this command? */
466 int vm_firstkey; /* The first argument that's a key (0 = no keys) */
467 int vm_lastkey; /* THe last argument that's a key */
468 int vm_keystep; /* The step between first and last key */
ed9b544e 469};
470
de96dbfe 471struct redisFunctionSym {
472 char *name;
56906eef 473 unsigned long pointer;
de96dbfe 474};
475
ed9b544e 476typedef struct _redisSortObject {
477 robj *obj;
478 union {
479 double score;
480 robj *cmpobj;
481 } u;
482} redisSortObject;
483
484typedef struct _redisSortOperation {
485 int type;
486 robj *pattern;
487} redisSortOperation;
488
6b47e12e 489/* ZSETs use a specialized version of Skiplists */
490
491typedef struct zskiplistNode {
492 struct zskiplistNode **forward;
e3870fab 493 struct zskiplistNode *backward;
912b9165 494 unsigned int *span;
6b47e12e 495 double score;
496 robj *obj;
497} zskiplistNode;
498
499typedef struct zskiplist {
e3870fab 500 struct zskiplistNode *header, *tail;
d13f767c 501 unsigned long length;
6b47e12e 502 int level;
503} zskiplist;
504
1812e024 505typedef struct zset {
506 dict *dict;
6b47e12e 507 zskiplist *zsl;
1812e024 508} zset;
509
6b47e12e 510/* Our shared "common" objects */
511
05df7621 512#define REDIS_SHARED_INTEGERS 10000
ed9b544e 513struct sharedObjectsStruct {
c937aa89 514 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
6e469882 515 *colon, *nullbulk, *nullmultibulk, *queued,
c937aa89 516 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
517 *outofrangeerr, *plus,
ed9b544e 518 *select0, *select1, *select2, *select3, *select4,
befec3cd 519 *select5, *select6, *select7, *select8, *select9,
c8d0ea0e 520 *messagebulk, *pmessagebulk, *subscribebulk, *unsubscribebulk, *mbulk3,
521 *mbulk4, *psubscribebulk, *punsubscribebulk,
522 *integers[REDIS_SHARED_INTEGERS];
ed9b544e 523} shared;
524
a7866db6 525/* Global vars that are actally used as constants. The following double
526 * values are used for double on-disk serialization, and are initialized
527 * at runtime to avoid strange compiler optimizations. */
528
529static double R_Zero, R_PosInf, R_NegInf, R_Nan;
530
92f8e882 531/* VM threaded I/O request message */
b9bc0eef 532#define REDIS_IOJOB_LOAD 0 /* Load from disk to memory */
533#define REDIS_IOJOB_PREPARE_SWAP 1 /* Compute needed pages */
534#define REDIS_IOJOB_DO_SWAP 2 /* Swap from memory to disk */
d5d55fc3 535typedef struct iojob {
996cb5f7 536 int type; /* Request type, REDIS_IOJOB_* */
b9bc0eef 537 redisDb *db;/* Redis database */
92f8e882 538 robj *key; /* This I/O request is about swapping this key */
b9bc0eef 539 robj *val; /* the value to swap for REDIS_IOREQ_*_SWAP, otherwise this
92f8e882 540 * field is populated by the I/O thread for REDIS_IOREQ_LOAD. */
541 off_t page; /* Swap page where to read/write the object */
248ea310 542 off_t pages; /* Swap pages needed to save object. PREPARE_SWAP return val */
996cb5f7 543 int canceled; /* True if this command was canceled by blocking side of VM */
544 pthread_t thread; /* ID of the thread processing this entry */
545} iojob;
92f8e882 546
ed9b544e 547/*================================ Prototypes =============================== */
548
549static void freeStringObject(robj *o);
550static void freeListObject(robj *o);
551static void freeSetObject(robj *o);
552static void decrRefCount(void *o);
553static robj *createObject(int type, void *ptr);
554static void freeClient(redisClient *c);
f78fd11b 555static int rdbLoad(char *filename);
ed9b544e 556static void addReply(redisClient *c, robj *obj);
557static void addReplySds(redisClient *c, sds s);
558static void incrRefCount(robj *o);
f78fd11b 559static int rdbSaveBackground(char *filename);
ed9b544e 560static robj *createStringObject(char *ptr, size_t len);
4ef8de8a 561static robj *dupStringObject(robj *o);
248ea310 562static void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
dd142b9c 563static void replicationFeedMonitors(list *monitors, int dictid, robj **argv, int argc);
28ed1f33 564static void flushAppendOnlyFile(void);
44b38ef4 565static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);
ed9b544e 566static int syncWithMaster(void);
05df7621 567static robj *tryObjectEncoding(robj *o);
9d65a1bb 568static robj *getDecodedObject(robj *o);
3305306f 569static int removeExpire(redisDb *db, robj *key);
570static int expireIfNeeded(redisDb *db, robj *key);
571static int deleteIfVolatile(redisDb *db, robj *key);
1b03836c 572static int deleteIfSwapped(redisDb *db, robj *key);
94754ccc 573static int deleteKey(redisDb *db, robj *key);
bb32ede5 574static time_t getExpire(redisDb *db, robj *key);
575static int setExpire(redisDb *db, robj *key, time_t when);
a3b21203 576static void updateSlavesWaitingBgsave(int bgsaveerr);
3fd78bcd 577static void freeMemoryIfNeeded(void);
de96dbfe 578static int processCommand(redisClient *c);
56906eef 579static void setupSigSegvAction(void);
a3b21203 580static void rdbRemoveTempFile(pid_t childpid);
9d65a1bb 581static void aofRemoveTempFile(pid_t childpid);
0ea663ea 582static size_t stringObjectLen(robj *o);
638e42ac 583static void processInputBuffer(redisClient *c);
6b47e12e 584static zskiplist *zslCreate(void);
fd8ccf44 585static void zslFree(zskiplist *zsl);
2b59cfdf 586static void zslInsert(zskiplist *zsl, double score, robj *obj);
2895e862 587static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask);
6e469882 588static void initClientMultiState(redisClient *c);
589static void freeClientMultiState(redisClient *c);
590static void queueMultiCommand(redisClient *c, struct redisCommand *cmd);
b0d8747d 591static void unblockClientWaitingData(redisClient *c);
4409877e 592static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele);
75680a3c 593static void vmInit(void);
a35ddf12 594static void vmMarkPagesFree(off_t page, off_t count);
55cf8433 595static robj *vmLoadObject(robj *key);
7e69548d 596static robj *vmPreviewObject(robj *key);
a69a0c9c 597static int vmSwapOneObjectBlocking(void);
598static int vmSwapOneObjectThreaded(void);
7e69548d 599static int vmCanSwapOut(void);
a5819310 600static int tryFreeOneObjectFromFreelist(void);
996cb5f7 601static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);
602static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, int mask);
603static void vmCancelThreadedIOJob(robj *o);
b9bc0eef 604static void lockThreadedIO(void);
605static void unlockThreadedIO(void);
606static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db);
607static void freeIOJob(iojob *j);
608static void queueIOJob(iojob *j);
a5819310 609static int vmWriteObjectOnSwap(robj *o, off_t page);
610static robj *vmReadObjectFromSwap(off_t page, int type);
054e426d 611static void waitEmptyIOJobsQueue(void);
612static void vmReopenSwapFile(void);
970e10bb 613static int vmFreePage(off_t page);
ca1788b5 614static void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
3805e04f 615static void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
0a6f3f0f 616static int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd);
d5d55fc3 617static int dontWaitForSwappedKey(redisClient *c, robj *key);
618static void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key);
619static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask);
620static struct redisCommand *lookupCommand(char *name);
621static void call(redisClient *c, struct redisCommand *cmd);
622static void resetClient(redisClient *c);
ada386b2 623static void convertToRealHash(robj *o);
ffc6b7f8 624static int pubsubUnsubscribeAllChannels(redisClient *c, int notify);
625static int pubsubUnsubscribeAllPatterns(redisClient *c, int notify);
626static void freePubsubPattern(void *p);
627static int listMatchPubsubPattern(void *a, void *b);
628static int compareStringObjects(robj *a, robj *b);
bf028098 629static int equalStringObjects(robj *a, robj *b);
befec3cd 630static void usage();
8f63ddca 631static int rewriteAppendOnlyFileBackground(void);
242a64f3 632static int vmSwapObjectBlocking(robj *key, robj *val);
ed9b544e 633
abcb223e 634static void authCommand(redisClient *c);
ed9b544e 635static void pingCommand(redisClient *c);
636static void echoCommand(redisClient *c);
637static void setCommand(redisClient *c);
638static void setnxCommand(redisClient *c);
526d00a5 639static void setexCommand(redisClient *c);
ed9b544e 640static void getCommand(redisClient *c);
641static void delCommand(redisClient *c);
642static void existsCommand(redisClient *c);
643static void incrCommand(redisClient *c);
644static void decrCommand(redisClient *c);
645static void incrbyCommand(redisClient *c);
646static void decrbyCommand(redisClient *c);
647static void selectCommand(redisClient *c);
648static void randomkeyCommand(redisClient *c);
649static void keysCommand(redisClient *c);
650static void dbsizeCommand(redisClient *c);
651static void lastsaveCommand(redisClient *c);
652static void saveCommand(redisClient *c);
653static void bgsaveCommand(redisClient *c);
9d65a1bb 654static void bgrewriteaofCommand(redisClient *c);
ed9b544e 655static void shutdownCommand(redisClient *c);
656static void moveCommand(redisClient *c);
657static void renameCommand(redisClient *c);
658static void renamenxCommand(redisClient *c);
659static void lpushCommand(redisClient *c);
660static void rpushCommand(redisClient *c);
661static void lpopCommand(redisClient *c);
662static void rpopCommand(redisClient *c);
663static void llenCommand(redisClient *c);
664static void lindexCommand(redisClient *c);
665static void lrangeCommand(redisClient *c);
666static void ltrimCommand(redisClient *c);
667static void typeCommand(redisClient *c);
668static void lsetCommand(redisClient *c);
669static void saddCommand(redisClient *c);
670static void sremCommand(redisClient *c);
a4460ef4 671static void smoveCommand(redisClient *c);
ed9b544e 672static void sismemberCommand(redisClient *c);
673static void scardCommand(redisClient *c);
12fea928 674static void spopCommand(redisClient *c);
2abb95a9 675static void srandmemberCommand(redisClient *c);
ed9b544e 676static void sinterCommand(redisClient *c);
677static void sinterstoreCommand(redisClient *c);
40d224a9 678static void sunionCommand(redisClient *c);
679static void sunionstoreCommand(redisClient *c);
f4f56e1d 680static void sdiffCommand(redisClient *c);
681static void sdiffstoreCommand(redisClient *c);
ed9b544e 682static void syncCommand(redisClient *c);
683static void flushdbCommand(redisClient *c);
684static void flushallCommand(redisClient *c);
685static void sortCommand(redisClient *c);
686static void lremCommand(redisClient *c);
0f5f7e9a 687static void rpoplpushcommand(redisClient *c);
ed9b544e 688static void infoCommand(redisClient *c);
70003d28 689static void mgetCommand(redisClient *c);
87eca727 690static void monitorCommand(redisClient *c);
3305306f 691static void expireCommand(redisClient *c);
802e8373 692static void expireatCommand(redisClient *c);
f6b141c5 693static void getsetCommand(redisClient *c);
fd88489a 694static void ttlCommand(redisClient *c);
321b0e13 695static void slaveofCommand(redisClient *c);
7f957c92 696static void debugCommand(redisClient *c);
f6b141c5 697static void msetCommand(redisClient *c);
698static void msetnxCommand(redisClient *c);
fd8ccf44 699static void zaddCommand(redisClient *c);
7db723ad 700static void zincrbyCommand(redisClient *c);
cc812361 701static void zrangeCommand(redisClient *c);
50c55df5 702static void zrangebyscoreCommand(redisClient *c);
f44dd428 703static void zcountCommand(redisClient *c);
e3870fab 704static void zrevrangeCommand(redisClient *c);
3c41331e 705static void zcardCommand(redisClient *c);
1b7106e7 706static void zremCommand(redisClient *c);
6e333bbe 707static void zscoreCommand(redisClient *c);
1807985b 708static void zremrangebyscoreCommand(redisClient *c);
6e469882 709static void multiCommand(redisClient *c);
710static void execCommand(redisClient *c);
18b6cb76 711static void discardCommand(redisClient *c);
4409877e 712static void blpopCommand(redisClient *c);
713static void brpopCommand(redisClient *c);
4b00bebd 714static void appendCommand(redisClient *c);
39191553 715static void substrCommand(redisClient *c);
69d95c3e 716static void zrankCommand(redisClient *c);
798d9e55 717static void zrevrankCommand(redisClient *c);
978c2c94 718static void hsetCommand(redisClient *c);
1f1c7695 719static void hsetnxCommand(redisClient *c);
978c2c94 720static void hgetCommand(redisClient *c);
09aeb579
PN
721static void hmsetCommand(redisClient *c);
722static void hmgetCommand(redisClient *c);
07efaf74 723static void hdelCommand(redisClient *c);
92b27fe9 724static void hlenCommand(redisClient *c);
9212eafd 725static void zremrangebyrankCommand(redisClient *c);
5d373da9 726static void zunionstoreCommand(redisClient *c);
727static void zinterstoreCommand(redisClient *c);
78409a0f 728static void hkeysCommand(redisClient *c);
729static void hvalsCommand(redisClient *c);
730static void hgetallCommand(redisClient *c);
a86f14b1 731static void hexistsCommand(redisClient *c);
500ece7c 732static void configCommand(redisClient *c);
01426b05 733static void hincrbyCommand(redisClient *c);
befec3cd 734static void subscribeCommand(redisClient *c);
735static void unsubscribeCommand(redisClient *c);
ffc6b7f8 736static void psubscribeCommand(redisClient *c);
737static void punsubscribeCommand(redisClient *c);
befec3cd 738static void publishCommand(redisClient *c);
f6b141c5 739
ed9b544e 740/*================================= Globals ================================= */
741
742/* Global vars */
743static struct redisServer server; /* server global state */
744static struct redisCommand cmdTable[] = {
76583ea4
PN
745 {"get",getCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
746 {"set",setCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
747 {"setnx",setnxCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
526d00a5 748 {"setex",setexCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
76583ea4
PN
749 {"append",appendCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
750 {"substr",substrCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
751 {"del",delCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
752 {"exists",existsCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
753 {"incr",incrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
754 {"decr",decrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
755 {"mget",mgetCommand,-2,REDIS_CMD_INLINE,NULL,1,-1,1},
756 {"rpush",rpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
757 {"lpush",lpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
758 {"rpop",rpopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
759 {"lpop",lpopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
760 {"brpop",brpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
761 {"blpop",blpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
762 {"llen",llenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
763 {"lindex",lindexCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
764 {"lset",lsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
765 {"lrange",lrangeCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
766 {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
767 {"lrem",lremCommand,4,REDIS_CMD_BULK,NULL,1,1,1},
768 {"rpoplpush",rpoplpushcommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,2,1},
769 {"sadd",saddCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
770 {"srem",sremCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
771 {"smove",smoveCommand,4,REDIS_CMD_BULK,NULL,1,2,1},
772 {"sismember",sismemberCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
773 {"scard",scardCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
774 {"spop",spopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
775 {"srandmember",srandmemberCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
776 {"sinter",sinterCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
777 {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
778 {"sunion",sunionCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
779 {"sunionstore",sunionstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
780 {"sdiff",sdiffCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
781 {"sdiffstore",sdiffstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
782 {"smembers",sinterCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
783 {"zadd",zaddCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
784 {"zincrby",zincrbyCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
785 {"zrem",zremCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
786 {"zremrangebyscore",zremrangebyscoreCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
787 {"zremrangebyrank",zremrangebyrankCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
5d373da9 788 {"zunionstore",zunionstoreCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
789 {"zinterstore",zinterstoreCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
76583ea4
PN
790 {"zrange",zrangeCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
791 {"zrangebyscore",zrangebyscoreCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
792 {"zcount",zcountCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
793 {"zrevrange",zrevrangeCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
794 {"zcard",zcardCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
795 {"zscore",zscoreCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
796 {"zrank",zrankCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
797 {"zrevrank",zrevrankCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
798 {"hset",hsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
1f1c7695 799 {"hsetnx",hsetnxCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
09aeb579 800 {"hget",hgetCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
d33278d1 801 {"hmset",hmsetCommand,-4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
09aeb579 802 {"hmget",hmgetCommand,-3,REDIS_CMD_BULK,NULL,1,1,1},
01426b05 803 {"hincrby",hincrbyCommand,4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
76583ea4
PN
804 {"hdel",hdelCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
805 {"hlen",hlenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
806 {"hkeys",hkeysCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
807 {"hvals",hvalsCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
808 {"hgetall",hgetallCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
4583c4f0 809 {"hexists",hexistsCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
76583ea4
PN
810 {"incrby",incrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
811 {"decrby",decrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
812 {"getset",getsetCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
813 {"mset",msetCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,-1,2},
814 {"msetnx",msetnxCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,-1,2},
815 {"randomkey",randomkeyCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
816 {"select",selectCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
817 {"move",moveCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
818 {"rename",renameCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
819 {"renamenx",renamenxCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
820 {"expire",expireCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
821 {"expireat",expireatCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
822 {"keys",keysCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
823 {"dbsize",dbsizeCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
824 {"auth",authCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
825 {"ping",pingCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
826 {"echo",echoCommand,2,REDIS_CMD_BULK,NULL,0,0,0},
827 {"save",saveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
828 {"bgsave",bgsaveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
829 {"bgrewriteaof",bgrewriteaofCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
830 {"shutdown",shutdownCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
831 {"lastsave",lastsaveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
832 {"type",typeCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
833 {"multi",multiCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
3805e04f 834 {"exec",execCommand,1,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,execBlockClientOnSwappedKeys,0,0,0},
76583ea4
PN
835 {"discard",discardCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
836 {"sync",syncCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
837 {"flushdb",flushdbCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
838 {"flushall",flushallCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
839 {"sort",sortCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
840 {"info",infoCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
841 {"monitor",monitorCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
842 {"ttl",ttlCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
843 {"slaveof",slaveofCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
844 {"debug",debugCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
500ece7c 845 {"config",configCommand,-2,REDIS_CMD_BULK,NULL,0,0,0},
befec3cd 846 {"subscribe",subscribeCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
847 {"unsubscribe",unsubscribeCommand,-1,REDIS_CMD_INLINE,NULL,0,0,0},
ffc6b7f8 848 {"psubscribe",psubscribeCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
849 {"punsubscribe",punsubscribeCommand,-1,REDIS_CMD_INLINE,NULL,0,0,0},
4005fef1 850 {"publish",publishCommand,3,REDIS_CMD_BULK|REDIS_CMD_FORCE_REPLICATION,NULL,0,0,0},
76583ea4 851 {NULL,NULL,0,0,NULL,0,0,0}
ed9b544e 852};
bcfc686d 853
ed9b544e 854/*============================ Utility functions ============================ */
855
856/* Glob-style pattern matching. */
500ece7c 857static int stringmatchlen(const char *pattern, int patternLen,
ed9b544e 858 const char *string, int stringLen, int nocase)
859{
860 while(patternLen) {
861 switch(pattern[0]) {
862 case '*':
863 while (pattern[1] == '*') {
864 pattern++;
865 patternLen--;
866 }
867 if (patternLen == 1)
868 return 1; /* match */
869 while(stringLen) {
870 if (stringmatchlen(pattern+1, patternLen-1,
871 string, stringLen, nocase))
872 return 1; /* match */
873 string++;
874 stringLen--;
875 }
876 return 0; /* no match */
877 break;
878 case '?':
879 if (stringLen == 0)
880 return 0; /* no match */
881 string++;
882 stringLen--;
883 break;
884 case '[':
885 {
886 int not, match;
887
888 pattern++;
889 patternLen--;
890 not = pattern[0] == '^';
891 if (not) {
892 pattern++;
893 patternLen--;
894 }
895 match = 0;
896 while(1) {
897 if (pattern[0] == '\\') {
898 pattern++;
899 patternLen--;
900 if (pattern[0] == string[0])
901 match = 1;
902 } else if (pattern[0] == ']') {
903 break;
904 } else if (patternLen == 0) {
905 pattern--;
906 patternLen++;
907 break;
908 } else if (pattern[1] == '-' && patternLen >= 3) {
909 int start = pattern[0];
910 int end = pattern[2];
911 int c = string[0];
912 if (start > end) {
913 int t = start;
914 start = end;
915 end = t;
916 }
917 if (nocase) {
918 start = tolower(start);
919 end = tolower(end);
920 c = tolower(c);
921 }
922 pattern += 2;
923 patternLen -= 2;
924 if (c >= start && c <= end)
925 match = 1;
926 } else {
927 if (!nocase) {
928 if (pattern[0] == string[0])
929 match = 1;
930 } else {
931 if (tolower((int)pattern[0]) == tolower((int)string[0]))
932 match = 1;
933 }
934 }
935 pattern++;
936 patternLen--;
937 }
938 if (not)
939 match = !match;
940 if (!match)
941 return 0; /* no match */
942 string++;
943 stringLen--;
944 break;
945 }
946 case '\\':
947 if (patternLen >= 2) {
948 pattern++;
949 patternLen--;
950 }
951 /* fall through */
952 default:
953 if (!nocase) {
954 if (pattern[0] != string[0])
955 return 0; /* no match */
956 } else {
957 if (tolower((int)pattern[0]) != tolower((int)string[0]))
958 return 0; /* no match */
959 }
960 string++;
961 stringLen--;
962 break;
963 }
964 pattern++;
965 patternLen--;
966 if (stringLen == 0) {
967 while(*pattern == '*') {
968 pattern++;
969 patternLen--;
970 }
971 break;
972 }
973 }
974 if (patternLen == 0 && stringLen == 0)
975 return 1;
976 return 0;
977}
978
500ece7c 979static int stringmatch(const char *pattern, const char *string, int nocase) {
980 return stringmatchlen(pattern,strlen(pattern),string,strlen(string),nocase);
981}
982
2b619329 983/* Convert a string representing an amount of memory into the number of
984 * bytes, so for instance memtoll("1Gi") will return 1073741824 that is
985 * (1024*1024*1024).
986 *
987 * On parsing error, if *err is not NULL, it's set to 1, otherwise it's
988 * set to 0 */
989static long long memtoll(const char *p, int *err) {
990 const char *u;
991 char buf[128];
992 long mul; /* unit multiplier */
993 long long val;
994 unsigned int digits;
995
996 if (err) *err = 0;
997 /* Search the first non digit character. */
998 u = p;
999 if (*u == '-') u++;
1000 while(*u && isdigit(*u)) u++;
1001 if (*u == '\0' || !strcasecmp(u,"b")) {
1002 mul = 1;
72324005 1003 } else if (!strcasecmp(u,"k")) {
2b619329 1004 mul = 1000;
72324005 1005 } else if (!strcasecmp(u,"kb")) {
2b619329 1006 mul = 1024;
72324005 1007 } else if (!strcasecmp(u,"m")) {
2b619329 1008 mul = 1000*1000;
72324005 1009 } else if (!strcasecmp(u,"mb")) {
2b619329 1010 mul = 1024*1024;
72324005 1011 } else if (!strcasecmp(u,"g")) {
2b619329 1012 mul = 1000L*1000*1000;
72324005 1013 } else if (!strcasecmp(u,"gb")) {
2b619329 1014 mul = 1024L*1024*1024;
1015 } else {
1016 if (err) *err = 1;
1017 mul = 1;
1018 }
1019 digits = u-p;
1020 if (digits >= sizeof(buf)) {
1021 if (err) *err = 1;
1022 return LLONG_MAX;
1023 }
1024 memcpy(buf,p,digits);
1025 buf[digits] = '\0';
1026 val = strtoll(buf,NULL,10);
1027 return val*mul;
1028}
1029
ee14da56 1030/* Convert a long long into a string. Returns the number of
1031 * characters needed to represent the number, that can be shorter if passed
1032 * buffer length is not enough to store the whole number. */
1033static int ll2string(char *s, size_t len, long long value) {
1034 char buf[32], *p;
1035 unsigned long long v;
1036 size_t l;
1037
1038 if (len == 0) return 0;
1039 v = (value < 0) ? -value : value;
1040 p = buf+31; /* point to the last character */
1041 do {
1042 *p-- = '0'+(v%10);
1043 v /= 10;
1044 } while(v);
1045 if (value < 0) *p-- = '-';
1046 p++;
1047 l = 32-(p-buf);
1048 if (l+1 > len) l = len-1; /* Make sure it fits, including the nul term */
1049 memcpy(s,p,l);
1050 s[l] = '\0';
1051 return l;
1052}
1053
56906eef 1054static void redisLog(int level, const char *fmt, ...) {
ed9b544e 1055 va_list ap;
1056 FILE *fp;
1057
1058 fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
1059 if (!fp) return;
1060
1061 va_start(ap, fmt);
1062 if (level >= server.verbosity) {
6766f45e 1063 char *c = ".-*#";
1904ecc1 1064 char buf[64];
1065 time_t now;
1066
1067 now = time(NULL);
6c9385e0 1068 strftime(buf,64,"%d %b %H:%M:%S",localtime(&now));
054e426d 1069 fprintf(fp,"[%d] %s %c ",(int)getpid(),buf,c[level]);
ed9b544e 1070 vfprintf(fp, fmt, ap);
1071 fprintf(fp,"\n");
1072 fflush(fp);
1073 }
1074 va_end(ap);
1075
1076 if (server.logfile) fclose(fp);
1077}
1078
1079/*====================== Hash table type implementation ==================== */
1080
1081/* This is an hash table type that uses the SDS dynamic strings libary as
1082 * keys and radis objects as values (objects can hold SDS strings,
1083 * lists, sets). */
1084
1812e024 1085static void dictVanillaFree(void *privdata, void *val)
1086{
1087 DICT_NOTUSED(privdata);
1088 zfree(val);
1089}
1090
4409877e 1091static void dictListDestructor(void *privdata, void *val)
1092{
1093 DICT_NOTUSED(privdata);
1094 listRelease((list*)val);
1095}
1096
ed9b544e 1097static int sdsDictKeyCompare(void *privdata, const void *key1,
1098 const void *key2)
1099{
1100 int l1,l2;
1101 DICT_NOTUSED(privdata);
1102
1103 l1 = sdslen((sds)key1);
1104 l2 = sdslen((sds)key2);
1105 if (l1 != l2) return 0;
1106 return memcmp(key1, key2, l1) == 0;
1107}
1108
1109static void dictRedisObjectDestructor(void *privdata, void *val)
1110{
1111 DICT_NOTUSED(privdata);
1112
a35ddf12 1113 if (val == NULL) return; /* Values of swapped out keys as set to NULL */
ed9b544e 1114 decrRefCount(val);
1115}
1116
942a3961 1117static int dictObjKeyCompare(void *privdata, const void *key1,
ed9b544e 1118 const void *key2)
1119{
1120 const robj *o1 = key1, *o2 = key2;
1121 return sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
1122}
1123
942a3961 1124static unsigned int dictObjHash(const void *key) {
ed9b544e 1125 const robj *o = key;
1126 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1127}
1128
942a3961 1129static int dictEncObjKeyCompare(void *privdata, const void *key1,
1130 const void *key2)
1131{
9d65a1bb 1132 robj *o1 = (robj*) key1, *o2 = (robj*) key2;
1133 int cmp;
942a3961 1134
2a1198b4 1135 if (o1->encoding == REDIS_ENCODING_INT &&
dc05abde 1136 o2->encoding == REDIS_ENCODING_INT)
1137 return o1->ptr == o2->ptr;
2a1198b4 1138
9d65a1bb 1139 o1 = getDecodedObject(o1);
1140 o2 = getDecodedObject(o2);
1141 cmp = sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
1142 decrRefCount(o1);
1143 decrRefCount(o2);
1144 return cmp;
942a3961 1145}
1146
1147static unsigned int dictEncObjHash(const void *key) {
9d65a1bb 1148 robj *o = (robj*) key;
942a3961 1149
ed9e4966 1150 if (o->encoding == REDIS_ENCODING_RAW) {
1151 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1152 } else {
1153 if (o->encoding == REDIS_ENCODING_INT) {
1154 char buf[32];
1155 int len;
1156
ee14da56 1157 len = ll2string(buf,32,(long)o->ptr);
ed9e4966 1158 return dictGenHashFunction((unsigned char*)buf, len);
1159 } else {
1160 unsigned int hash;
1161
1162 o = getDecodedObject(o);
1163 hash = dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1164 decrRefCount(o);
1165 return hash;
1166 }
1167 }
942a3961 1168}
1169
f2d9f50f 1170/* Sets type and expires */
ed9b544e 1171static dictType setDictType = {
942a3961 1172 dictEncObjHash, /* hash function */
ed9b544e 1173 NULL, /* key dup */
1174 NULL, /* val dup */
942a3961 1175 dictEncObjKeyCompare, /* key compare */
ed9b544e 1176 dictRedisObjectDestructor, /* key destructor */
1177 NULL /* val destructor */
1178};
1179
f2d9f50f 1180/* Sorted sets hash (note: a skiplist is used in addition to the hash table) */
1812e024 1181static dictType zsetDictType = {
1182 dictEncObjHash, /* hash function */
1183 NULL, /* key dup */
1184 NULL, /* val dup */
1185 dictEncObjKeyCompare, /* key compare */
1186 dictRedisObjectDestructor, /* key destructor */
da0a1620 1187 dictVanillaFree /* val destructor of malloc(sizeof(double)) */
1812e024 1188};
1189
f2d9f50f 1190/* Db->dict */
5234952b 1191static dictType dbDictType = {
942a3961 1192 dictObjHash, /* hash function */
ed9b544e 1193 NULL, /* key dup */
1194 NULL, /* val dup */
942a3961 1195 dictObjKeyCompare, /* key compare */
ed9b544e 1196 dictRedisObjectDestructor, /* key destructor */
1197 dictRedisObjectDestructor /* val destructor */
1198};
1199
f2d9f50f 1200/* Db->expires */
1201static dictType keyptrDictType = {
1202 dictObjHash, /* hash function */
1203 NULL, /* key dup */
1204 NULL, /* val dup */
1205 dictObjKeyCompare, /* key compare */
1206 dictRedisObjectDestructor, /* key destructor */
1207 NULL /* val destructor */
1208};
1209
5234952b 1210/* Hash type hash table (note that small hashes are represented with zimpaps) */
1211static dictType hashDictType = {
1212 dictEncObjHash, /* hash function */
1213 NULL, /* key dup */
1214 NULL, /* val dup */
1215 dictEncObjKeyCompare, /* key compare */
1216 dictRedisObjectDestructor, /* key destructor */
1217 dictRedisObjectDestructor /* val destructor */
1218};
1219
4409877e 1220/* Keylist hash table type has unencoded redis objects as keys and
d5d55fc3 1221 * lists as values. It's used for blocking operations (BLPOP) and to
1222 * map swapped keys to a list of clients waiting for this keys to be loaded. */
4409877e 1223static dictType keylistDictType = {
1224 dictObjHash, /* hash function */
1225 NULL, /* key dup */
1226 NULL, /* val dup */
1227 dictObjKeyCompare, /* key compare */
1228 dictRedisObjectDestructor, /* key destructor */
1229 dictListDestructor /* val destructor */
1230};
1231
42ab0172
AO
1232static void version();
1233
ed9b544e 1234/* ========================= Random utility functions ======================= */
1235
1236/* Redis generally does not try to recover from out of memory conditions
1237 * when allocating objects or strings, it is not clear if it will be possible
1238 * to report this condition to the client since the networking layer itself
1239 * is based on heap allocation for send buffers, so we simply abort.
1240 * At least the code will be simpler to read... */
1241static void oom(const char *msg) {
71c54b21 1242 redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
ed9b544e 1243 sleep(1);
1244 abort();
1245}
1246
1247/* ====================== Redis server networking stuff ===================== */
56906eef 1248static void closeTimedoutClients(void) {
ed9b544e 1249 redisClient *c;
ed9b544e 1250 listNode *ln;
1251 time_t now = time(NULL);
c7df85a4 1252 listIter li;
ed9b544e 1253
c7df85a4 1254 listRewind(server.clients,&li);
1255 while ((ln = listNext(&li)) != NULL) {
ed9b544e 1256 c = listNodeValue(ln);
f86a74e9 1257 if (server.maxidletime &&
1258 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
c7cf2ec9 1259 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
ffc6b7f8 1260 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
1261 listLength(c->pubsub_patterns) == 0 &&
d6cc8867 1262 (now - c->lastinteraction > server.maxidletime))
f86a74e9 1263 {
f870935d 1264 redisLog(REDIS_VERBOSE,"Closing idle client");
ed9b544e 1265 freeClient(c);
f86a74e9 1266 } else if (c->flags & REDIS_BLOCKED) {
58d976b8 1267 if (c->blockingto != 0 && c->blockingto < now) {
b177fd30 1268 addReply(c,shared.nullmultibulk);
b0d8747d 1269 unblockClientWaitingData(c);
f86a74e9 1270 }
ed9b544e 1271 }
1272 }
ed9b544e 1273}
1274
12fea928 1275static int htNeedsResize(dict *dict) {
1276 long long size, used;
1277
1278 size = dictSlots(dict);
1279 used = dictSize(dict);
1280 return (size && used && size > DICT_HT_INITIAL_SIZE &&
1281 (used*100/size < REDIS_HT_MINFILL));
1282}
1283
0bc03378 1284/* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
1285 * we resize the hash table to save memory */
56906eef 1286static void tryResizeHashTables(void) {
0bc03378 1287 int j;
1288
1289 for (j = 0; j < server.dbnum; j++) {
5413c40d 1290 if (htNeedsResize(server.db[j].dict))
0bc03378 1291 dictResize(server.db[j].dict);
12fea928 1292 if (htNeedsResize(server.db[j].expires))
1293 dictResize(server.db[j].expires);
0bc03378 1294 }
1295}
1296
8ca3e9d1 1297/* Our hash table implementation performs rehashing incrementally while
1298 * we write/read from the hash table. Still if the server is idle, the hash
1299 * table will use two tables for a long time. So we try to use 1 millisecond
1300 * of CPU time at every serverCron() loop in order to rehash some key. */
1301static void incrementallyRehash(void) {
1302 int j;
1303
1304 for (j = 0; j < server.dbnum; j++) {
1305 if (dictIsRehashing(server.db[j].dict)) {
1306 dictRehashMilliseconds(server.db[j].dict,1);
1307 break; /* already used our millisecond for this loop... */
1308 }
1309 }
1310}
1311
9d65a1bb 1312/* A background saving child (BGSAVE) terminated its work. Handle this. */
1313void backgroundSaveDoneHandler(int statloc) {
1314 int exitcode = WEXITSTATUS(statloc);
1315 int bysignal = WIFSIGNALED(statloc);
1316
1317 if (!bysignal && exitcode == 0) {
1318 redisLog(REDIS_NOTICE,
1319 "Background saving terminated with success");
1320 server.dirty = 0;
1321 server.lastsave = time(NULL);
1322 } else if (!bysignal && exitcode != 0) {
1323 redisLog(REDIS_WARNING, "Background saving error");
1324 } else {
1325 redisLog(REDIS_WARNING,
454eea7c 1326 "Background saving terminated by signal %d", WTERMSIG(statloc));
9d65a1bb 1327 rdbRemoveTempFile(server.bgsavechildpid);
1328 }
1329 server.bgsavechildpid = -1;
1330 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1331 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1332 updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR);
1333}
1334
1335/* A background append only file rewriting (BGREWRITEAOF) terminated its work.
1336 * Handle this. */
1337void backgroundRewriteDoneHandler(int statloc) {
1338 int exitcode = WEXITSTATUS(statloc);
1339 int bysignal = WIFSIGNALED(statloc);
1340
1341 if (!bysignal && exitcode == 0) {
1342 int fd;
1343 char tmpfile[256];
1344
1345 redisLog(REDIS_NOTICE,
1346 "Background append only file rewriting terminated with success");
1347 /* Now it's time to flush the differences accumulated by the parent */
1348 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) server.bgrewritechildpid);
1349 fd = open(tmpfile,O_WRONLY|O_APPEND);
1350 if (fd == -1) {
1351 redisLog(REDIS_WARNING, "Not able to open the temp append only file produced by the child: %s", strerror(errno));
1352 goto cleanup;
1353 }
1354 /* Flush our data... */
1355 if (write(fd,server.bgrewritebuf,sdslen(server.bgrewritebuf)) !=
1356 (signed) sdslen(server.bgrewritebuf)) {
1357 redisLog(REDIS_WARNING, "Error or short write trying to flush the parent diff of the append log file in the child temp file: %s", strerror(errno));
1358 close(fd);
1359 goto cleanup;
1360 }
b32627cd 1361 redisLog(REDIS_NOTICE,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server.bgrewritebuf));
9d65a1bb 1362 /* Now our work is to rename the temp file into the stable file. And
1363 * switch the file descriptor used by the server for append only. */
1364 if (rename(tmpfile,server.appendfilename) == -1) {
1365 redisLog(REDIS_WARNING,"Can't rename the temp append only file into the stable one: %s", strerror(errno));
1366 close(fd);
1367 goto cleanup;
1368 }
1369 /* Mission completed... almost */
1370 redisLog(REDIS_NOTICE,"Append only file successfully rewritten.");
1371 if (server.appendfd != -1) {
1372 /* If append only is actually enabled... */
1373 close(server.appendfd);
1374 server.appendfd = fd;
1375 fsync(fd);
85a83172 1376 server.appendseldb = -1; /* Make sure it will issue SELECT */
9d65a1bb 1377 redisLog(REDIS_NOTICE,"The new append only file was selected for future appends.");
1378 } else {
1379 /* If append only is disabled we just generate a dump in this
1380 * format. Why not? */
1381 close(fd);
1382 }
1383 } else if (!bysignal && exitcode != 0) {
1384 redisLog(REDIS_WARNING, "Background append only file rewriting error");
1385 } else {
1386 redisLog(REDIS_WARNING,
454eea7c 1387 "Background append only file rewriting terminated by signal %d",
1388 WTERMSIG(statloc));
9d65a1bb 1389 }
1390cleanup:
1391 sdsfree(server.bgrewritebuf);
1392 server.bgrewritebuf = sdsempty();
1393 aofRemoveTempFile(server.bgrewritechildpid);
1394 server.bgrewritechildpid = -1;
1395}
1396
884d4b39 1397/* This function is called once a background process of some kind terminates,
1398 * as we want to avoid resizing the hash tables when there is a child in order
1399 * to play well with copy-on-write (otherwise when a resize happens lots of
1400 * memory pages are copied). The goal of this function is to update the ability
1401 * for dict.c to resize the hash tables accordingly to the fact we have o not
1402 * running childs. */
1403static void updateDictResizePolicy(void) {
1404 if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1)
1405 dictEnableResize();
1406 else
1407 dictDisableResize();
1408}
1409
56906eef 1410static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
94754ccc 1411 int j, loops = server.cronloops++;
ed9b544e 1412 REDIS_NOTUSED(eventLoop);
1413 REDIS_NOTUSED(id);
1414 REDIS_NOTUSED(clientData);
1415
3a66edc7 1416 /* We take a cached value of the unix time in the global state because
1417 * with virtual memory and aging there is to store the current time
1418 * in objects at every object access, and accuracy is not needed.
1419 * To access a global var is faster than calling time(NULL) */
1420 server.unixtime = time(NULL);
1421
0bc03378 1422 /* Show some info about non-empty databases */
ed9b544e 1423 for (j = 0; j < server.dbnum; j++) {
dec423d9 1424 long long size, used, vkeys;
94754ccc 1425
3305306f 1426 size = dictSlots(server.db[j].dict);
1427 used = dictSize(server.db[j].dict);
94754ccc 1428 vkeys = dictSize(server.db[j].expires);
1763929f 1429 if (!(loops % 50) && (used || vkeys)) {
f870935d 1430 redisLog(REDIS_VERBOSE,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j,used,vkeys,size);
a4d1ba9a 1431 /* dictPrintStats(server.dict); */
ed9b544e 1432 }
ed9b544e 1433 }
1434
0bc03378 1435 /* We don't want to resize the hash tables while a bacground saving
1436 * is in progress: the saving child is created using fork() that is
1437 * implemented with a copy-on-write semantic in most modern systems, so
1438 * if we resize the HT while there is the saving child at work actually
1439 * a lot of memory movements in the parent will cause a lot of pages
1440 * copied. */
8ca3e9d1 1441 if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1) {
1442 if (!(loops % 10)) tryResizeHashTables();
1443 if (server.activerehashing) incrementallyRehash();
884d4b39 1444 }
0bc03378 1445
ed9b544e 1446 /* Show information about connected clients */
1763929f 1447 if (!(loops % 50)) {
bdcb92f2 1448 redisLog(REDIS_VERBOSE,"%d clients connected (%d slaves), %zu bytes in use",
ed9b544e 1449 listLength(server.clients)-listLength(server.slaves),
1450 listLength(server.slaves),
bdcb92f2 1451 zmalloc_used_memory());
ed9b544e 1452 }
1453
1454 /* Close connections of timedout clients */
1763929f 1455 if ((server.maxidletime && !(loops % 100)) || server.blpop_blocked_clients)
ed9b544e 1456 closeTimedoutClients();
1457
9d65a1bb 1458 /* Check if a background saving or AOF rewrite in progress terminated */
1459 if (server.bgsavechildpid != -1 || server.bgrewritechildpid != -1) {
ed9b544e 1460 int statloc;
9d65a1bb 1461 pid_t pid;
1462
1463 if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
1464 if (pid == server.bgsavechildpid) {
1465 backgroundSaveDoneHandler(statloc);
ed9b544e 1466 } else {
9d65a1bb 1467 backgroundRewriteDoneHandler(statloc);
ed9b544e 1468 }
884d4b39 1469 updateDictResizePolicy();
ed9b544e 1470 }
1471 } else {
1472 /* If there is not a background saving in progress check if
1473 * we have to save now */
1474 time_t now = time(NULL);
1475 for (j = 0; j < server.saveparamslen; j++) {
1476 struct saveparam *sp = server.saveparams+j;
1477
1478 if (server.dirty >= sp->changes &&
1479 now-server.lastsave > sp->seconds) {
1480 redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
1481 sp->changes, sp->seconds);
f78fd11b 1482 rdbSaveBackground(server.dbfilename);
ed9b544e 1483 break;
1484 }
1485 }
1486 }
94754ccc 1487
f2324293 1488 /* Try to expire a few timed out keys. The algorithm used is adaptive and
1489 * will use few CPU cycles if there are few expiring keys, otherwise
1490 * it will get more aggressive to avoid that too much memory is used by
1491 * keys that can be removed from the keyspace. */
94754ccc 1492 for (j = 0; j < server.dbnum; j++) {
f2324293 1493 int expired;
94754ccc 1494 redisDb *db = server.db+j;
94754ccc 1495
f2324293 1496 /* Continue to expire if at the end of the cycle more than 25%
1497 * of the keys were expired. */
1498 do {
4ef8de8a 1499 long num = dictSize(db->expires);
94754ccc 1500 time_t now = time(NULL);
1501
f2324293 1502 expired = 0;
94754ccc 1503 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
1504 num = REDIS_EXPIRELOOKUPS_PER_CRON;
1505 while (num--) {
1506 dictEntry *de;
1507 time_t t;
1508
1509 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
1510 t = (time_t) dictGetEntryVal(de);
1511 if (now > t) {
1512 deleteKey(db,dictGetEntryKey(de));
f2324293 1513 expired++;
2a6a2ed1 1514 server.stat_expiredkeys++;
94754ccc 1515 }
1516 }
f2324293 1517 } while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4);
94754ccc 1518 }
1519
4ef8de8a 1520 /* Swap a few keys on disk if we are over the memory limit and VM
f870935d 1521 * is enbled. Try to free objects from the free list first. */
7e69548d 1522 if (vmCanSwapOut()) {
1523 while (server.vm_enabled && zmalloc_used_memory() >
f870935d 1524 server.vm_max_memory)
1525 {
72e9fd40 1526 int retval;
1527
a5819310 1528 if (tryFreeOneObjectFromFreelist() == REDIS_OK) continue;
72e9fd40 1529 retval = (server.vm_max_threads == 0) ?
1530 vmSwapOneObjectBlocking() :
1531 vmSwapOneObjectThreaded();
1763929f 1532 if (retval == REDIS_ERR && !(loops % 300) &&
72e9fd40 1533 zmalloc_used_memory() >
1534 (server.vm_max_memory+server.vm_max_memory/10))
1535 {
1536 redisLog(REDIS_WARNING,"WARNING: vm-max-memory limit exceeded by more than 10%% but unable to swap more objects out!");
7e69548d 1537 }
72e9fd40 1538 /* Note that when using threade I/O we free just one object,
1539 * because anyway when the I/O thread in charge to swap this
1540 * object out will finish, the handler of completed jobs
1541 * will try to swap more objects if we are still out of memory. */
1542 if (retval == REDIS_ERR || server.vm_max_threads > 0) break;
4ef8de8a 1543 }
1544 }
1545
ed9b544e 1546 /* Check if we should connect to a MASTER */
1763929f 1547 if (server.replstate == REDIS_REPL_CONNECT && !(loops % 10)) {
ed9b544e 1548 redisLog(REDIS_NOTICE,"Connecting to MASTER...");
1549 if (syncWithMaster() == REDIS_OK) {
1550 redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync succeeded");
8f63ddca 1551 if (server.appendonly) rewriteAppendOnlyFileBackground();
ed9b544e 1552 }
1553 }
1763929f 1554 return 100;
ed9b544e 1555}
1556
d5d55fc3 1557/* This function gets called every time Redis is entering the
1558 * main loop of the event driven library, that is, before to sleep
1559 * for ready file descriptors. */
1560static void beforeSleep(struct aeEventLoop *eventLoop) {
1561 REDIS_NOTUSED(eventLoop);
1562
28ed1f33 1563 /* Awake clients that got all the swapped keys they requested */
d5d55fc3 1564 if (server.vm_enabled && listLength(server.io_ready_clients)) {
1565 listIter li;
1566 listNode *ln;
1567
1568 listRewind(server.io_ready_clients,&li);
1569 while((ln = listNext(&li))) {
1570 redisClient *c = ln->value;
1571 struct redisCommand *cmd;
1572
1573 /* Resume the client. */
1574 listDelNode(server.io_ready_clients,ln);
1575 c->flags &= (~REDIS_IO_WAIT);
1576 server.vm_blocked_clients--;
1577 aeCreateFileEvent(server.el, c->fd, AE_READABLE,
1578 readQueryFromClient, c);
1579 cmd = lookupCommand(c->argv[0]->ptr);
1580 assert(cmd != NULL);
1581 call(c,cmd);
1582 resetClient(c);
1583 /* There may be more data to process in the input buffer. */
1584 if (c->querybuf && sdslen(c->querybuf) > 0)
1585 processInputBuffer(c);
1586 }
1587 }
28ed1f33 1588 /* Write the AOF buffer on disk */
1589 flushAppendOnlyFile();
d5d55fc3 1590}
1591
ed9b544e 1592static void createSharedObjects(void) {
05df7621 1593 int j;
1594
ed9b544e 1595 shared.crlf = createObject(REDIS_STRING,sdsnew("\r\n"));
1596 shared.ok = createObject(REDIS_STRING,sdsnew("+OK\r\n"));
1597 shared.err = createObject(REDIS_STRING,sdsnew("-ERR\r\n"));
c937aa89 1598 shared.emptybulk = createObject(REDIS_STRING,sdsnew("$0\r\n\r\n"));
1599 shared.czero = createObject(REDIS_STRING,sdsnew(":0\r\n"));
1600 shared.cone = createObject(REDIS_STRING,sdsnew(":1\r\n"));
1601 shared.nullbulk = createObject(REDIS_STRING,sdsnew("$-1\r\n"));
1602 shared.nullmultibulk = createObject(REDIS_STRING,sdsnew("*-1\r\n"));
1603 shared.emptymultibulk = createObject(REDIS_STRING,sdsnew("*0\r\n"));
ed9b544e 1604 shared.pong = createObject(REDIS_STRING,sdsnew("+PONG\r\n"));
6e469882 1605 shared.queued = createObject(REDIS_STRING,sdsnew("+QUEUED\r\n"));
ed9b544e 1606 shared.wrongtypeerr = createObject(REDIS_STRING,sdsnew(
1607 "-ERR Operation against a key holding the wrong kind of value\r\n"));
ed9b544e 1608 shared.nokeyerr = createObject(REDIS_STRING,sdsnew(
1609 "-ERR no such key\r\n"));
ed9b544e 1610 shared.syntaxerr = createObject(REDIS_STRING,sdsnew(
1611 "-ERR syntax error\r\n"));
c937aa89 1612 shared.sameobjecterr = createObject(REDIS_STRING,sdsnew(
1613 "-ERR source and destination objects are the same\r\n"));
1614 shared.outofrangeerr = createObject(REDIS_STRING,sdsnew(
1615 "-ERR index out of range\r\n"));
ed9b544e 1616 shared.space = createObject(REDIS_STRING,sdsnew(" "));
c937aa89 1617 shared.colon = createObject(REDIS_STRING,sdsnew(":"));
1618 shared.plus = createObject(REDIS_STRING,sdsnew("+"));
ed9b544e 1619 shared.select0 = createStringObject("select 0\r\n",10);
1620 shared.select1 = createStringObject("select 1\r\n",10);
1621 shared.select2 = createStringObject("select 2\r\n",10);
1622 shared.select3 = createStringObject("select 3\r\n",10);
1623 shared.select4 = createStringObject("select 4\r\n",10);
1624 shared.select5 = createStringObject("select 5\r\n",10);
1625 shared.select6 = createStringObject("select 6\r\n",10);
1626 shared.select7 = createStringObject("select 7\r\n",10);
1627 shared.select8 = createStringObject("select 8\r\n",10);
1628 shared.select9 = createStringObject("select 9\r\n",10);
befec3cd 1629 shared.messagebulk = createStringObject("$7\r\nmessage\r\n",13);
c8d0ea0e 1630 shared.pmessagebulk = createStringObject("$8\r\npmessage\r\n",14);
befec3cd 1631 shared.subscribebulk = createStringObject("$9\r\nsubscribe\r\n",15);
fc46bb71 1632 shared.unsubscribebulk = createStringObject("$11\r\nunsubscribe\r\n",18);
ffc6b7f8 1633 shared.psubscribebulk = createStringObject("$10\r\npsubscribe\r\n",17);
1634 shared.punsubscribebulk = createStringObject("$12\r\npunsubscribe\r\n",19);
befec3cd 1635 shared.mbulk3 = createStringObject("*3\r\n",4);
c8d0ea0e 1636 shared.mbulk4 = createStringObject("*4\r\n",4);
05df7621 1637 for (j = 0; j < REDIS_SHARED_INTEGERS; j++) {
1638 shared.integers[j] = createObject(REDIS_STRING,(void*)(long)j);
1639 shared.integers[j]->encoding = REDIS_ENCODING_INT;
1640 }
ed9b544e 1641}
1642
1643static void appendServerSaveParams(time_t seconds, int changes) {
1644 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
ed9b544e 1645 server.saveparams[server.saveparamslen].seconds = seconds;
1646 server.saveparams[server.saveparamslen].changes = changes;
1647 server.saveparamslen++;
1648}
1649
bcfc686d 1650static void resetServerSaveParams() {
ed9b544e 1651 zfree(server.saveparams);
1652 server.saveparams = NULL;
1653 server.saveparamslen = 0;
1654}
1655
1656static void initServerConfig() {
1657 server.dbnum = REDIS_DEFAULT_DBNUM;
1658 server.port = REDIS_SERVERPORT;
f870935d 1659 server.verbosity = REDIS_VERBOSE;
ed9b544e 1660 server.maxidletime = REDIS_MAXIDLETIME;
1661 server.saveparams = NULL;
1662 server.logfile = NULL; /* NULL = log on standard output */
1663 server.bindaddr = NULL;
1664 server.glueoutputbuf = 1;
1665 server.daemonize = 0;
44b38ef4 1666 server.appendonly = 0;
1b677732 1667 server.appendfsync = APPENDFSYNC_EVERYSEC;
48f0308a 1668 server.lastfsync = time(NULL);
44b38ef4 1669 server.appendfd = -1;
1670 server.appendseldb = -1; /* Make sure the first time will not match */
500ece7c 1671 server.pidfile = zstrdup("/var/run/redis.pid");
1672 server.dbfilename = zstrdup("dump.rdb");
1673 server.appendfilename = zstrdup("appendonly.aof");
abcb223e 1674 server.requirepass = NULL;
b0553789 1675 server.rdbcompression = 1;
8ca3e9d1 1676 server.activerehashing = 1;
285add55 1677 server.maxclients = 0;
d5d55fc3 1678 server.blpop_blocked_clients = 0;
3fd78bcd 1679 server.maxmemory = 0;
75680a3c 1680 server.vm_enabled = 0;
054e426d 1681 server.vm_swap_file = zstrdup("/tmp/redis-%p.vm");
75680a3c 1682 server.vm_page_size = 256; /* 256 bytes per page */
1683 server.vm_pages = 1024*1024*100; /* 104 millions of pages */
1684 server.vm_max_memory = 1024LL*1024*1024*1; /* 1 GB of RAM */
92f8e882 1685 server.vm_max_threads = 4;
d5d55fc3 1686 server.vm_blocked_clients = 0;
cbba7dd7 1687 server.hash_max_zipmap_entries = REDIS_HASH_MAX_ZIPMAP_ENTRIES;
1688 server.hash_max_zipmap_value = REDIS_HASH_MAX_ZIPMAP_VALUE;
75680a3c 1689
bcfc686d 1690 resetServerSaveParams();
ed9b544e 1691
1692 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1693 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1694 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1695 /* Replication related */
1696 server.isslave = 0;
d0ccebcf 1697 server.masterauth = NULL;
ed9b544e 1698 server.masterhost = NULL;
1699 server.masterport = 6379;
1700 server.master = NULL;
1701 server.replstate = REDIS_REPL_NONE;
a7866db6 1702
1703 /* Double constants initialization */
1704 R_Zero = 0.0;
1705 R_PosInf = 1.0/R_Zero;
1706 R_NegInf = -1.0/R_Zero;
1707 R_Nan = R_Zero/R_Zero;
ed9b544e 1708}
1709
1710static void initServer() {
1711 int j;
1712
1713 signal(SIGHUP, SIG_IGN);
1714 signal(SIGPIPE, SIG_IGN);
fe3bbfbe 1715 setupSigSegvAction();
ed9b544e 1716
b9bc0eef 1717 server.devnull = fopen("/dev/null","w");
1718 if (server.devnull == NULL) {
1719 redisLog(REDIS_WARNING, "Can't open /dev/null: %s", server.neterr);
1720 exit(1);
1721 }
ed9b544e 1722 server.clients = listCreate();
1723 server.slaves = listCreate();
87eca727 1724 server.monitors = listCreate();
ed9b544e 1725 server.objfreelist = listCreate();
1726 createSharedObjects();
1727 server.el = aeCreateEventLoop();
3305306f 1728 server.db = zmalloc(sizeof(redisDb)*server.dbnum);
ed9b544e 1729 server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr);
1730 if (server.fd == -1) {
1731 redisLog(REDIS_WARNING, "Opening TCP port: %s", server.neterr);
1732 exit(1);
1733 }
3305306f 1734 for (j = 0; j < server.dbnum; j++) {
5234952b 1735 server.db[j].dict = dictCreate(&dbDictType,NULL);
f2d9f50f 1736 server.db[j].expires = dictCreate(&keyptrDictType,NULL);
4409877e 1737 server.db[j].blockingkeys = dictCreate(&keylistDictType,NULL);
d5d55fc3 1738 if (server.vm_enabled)
1739 server.db[j].io_keys = dictCreate(&keylistDictType,NULL);
3305306f 1740 server.db[j].id = j;
1741 }
ffc6b7f8 1742 server.pubsub_channels = dictCreate(&keylistDictType,NULL);
1743 server.pubsub_patterns = listCreate();
1744 listSetFreeMethod(server.pubsub_patterns,freePubsubPattern);
1745 listSetMatchMethod(server.pubsub_patterns,listMatchPubsubPattern);
ed9b544e 1746 server.cronloops = 0;
9f3c422c 1747 server.bgsavechildpid = -1;
9d65a1bb 1748 server.bgrewritechildpid = -1;
1749 server.bgrewritebuf = sdsempty();
28ed1f33 1750 server.aofbuf = sdsempty();
ed9b544e 1751 server.lastsave = time(NULL);
1752 server.dirty = 0;
ed9b544e 1753 server.stat_numcommands = 0;
1754 server.stat_numconnections = 0;
2a6a2ed1 1755 server.stat_expiredkeys = 0;
ed9b544e 1756 server.stat_starttime = time(NULL);
3a66edc7 1757 server.unixtime = time(NULL);
d8f8b666 1758 aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
996cb5f7 1759 if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,
1760 acceptHandler, NULL) == AE_ERR) oom("creating file event");
44b38ef4 1761
1762 if (server.appendonly) {
3bb225d6 1763 server.appendfd = open(server.appendfilename,O_WRONLY|O_APPEND|O_CREAT,0644);
44b38ef4 1764 if (server.appendfd == -1) {
1765 redisLog(REDIS_WARNING, "Can't open the append-only file: %s",
1766 strerror(errno));
1767 exit(1);
1768 }
1769 }
75680a3c 1770
1771 if (server.vm_enabled) vmInit();
ed9b544e 1772}
1773
1774/* Empty the whole database */
ca37e9cd 1775static long long emptyDb() {
ed9b544e 1776 int j;
ca37e9cd 1777 long long removed = 0;
ed9b544e 1778
3305306f 1779 for (j = 0; j < server.dbnum; j++) {
ca37e9cd 1780 removed += dictSize(server.db[j].dict);
3305306f 1781 dictEmpty(server.db[j].dict);
1782 dictEmpty(server.db[j].expires);
1783 }
ca37e9cd 1784 return removed;
ed9b544e 1785}
1786
85dd2f3a 1787static int yesnotoi(char *s) {
1788 if (!strcasecmp(s,"yes")) return 1;
1789 else if (!strcasecmp(s,"no")) return 0;
1790 else return -1;
1791}
1792
ed9b544e 1793/* I agree, this is a very rudimental way to load a configuration...
1794 will improve later if the config gets more complex */
1795static void loadServerConfig(char *filename) {
c9a111ac 1796 FILE *fp;
ed9b544e 1797 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
1798 int linenum = 0;
1799 sds line = NULL;
c9a111ac 1800
1801 if (filename[0] == '-' && filename[1] == '\0')
1802 fp = stdin;
1803 else {
1804 if ((fp = fopen(filename,"r")) == NULL) {
9a22de82 1805 redisLog(REDIS_WARNING, "Fatal error, can't open config file '%s'", filename);
c9a111ac 1806 exit(1);
1807 }
ed9b544e 1808 }
c9a111ac 1809
ed9b544e 1810 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
1811 sds *argv;
1812 int argc, j;
1813
1814 linenum++;
1815 line = sdsnew(buf);
1816 line = sdstrim(line," \t\r\n");
1817
1818 /* Skip comments and blank lines*/
1819 if (line[0] == '#' || line[0] == '\0') {
1820 sdsfree(line);
1821 continue;
1822 }
1823
1824 /* Split into arguments */
1825 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
1826 sdstolower(argv[0]);
1827
1828 /* Execute config directives */
bb0b03a3 1829 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
ed9b544e 1830 server.maxidletime = atoi(argv[1]);
0150db36 1831 if (server.maxidletime < 0) {
ed9b544e 1832 err = "Invalid timeout value"; goto loaderr;
1833 }
bb0b03a3 1834 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
ed9b544e 1835 server.port = atoi(argv[1]);
1836 if (server.port < 1 || server.port > 65535) {
1837 err = "Invalid port"; goto loaderr;
1838 }
bb0b03a3 1839 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
ed9b544e 1840 server.bindaddr = zstrdup(argv[1]);
bb0b03a3 1841 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
ed9b544e 1842 int seconds = atoi(argv[1]);
1843 int changes = atoi(argv[2]);
1844 if (seconds < 1 || changes < 0) {
1845 err = "Invalid save parameters"; goto loaderr;
1846 }
1847 appendServerSaveParams(seconds,changes);
bb0b03a3 1848 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
ed9b544e 1849 if (chdir(argv[1]) == -1) {
1850 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
1851 argv[1], strerror(errno));
1852 exit(1);
1853 }
bb0b03a3 1854 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
1855 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
f870935d 1856 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
bb0b03a3 1857 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
1858 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
ed9b544e 1859 else {
1860 err = "Invalid log level. Must be one of debug, notice, warning";
1861 goto loaderr;
1862 }
bb0b03a3 1863 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
c9a111ac 1864 FILE *logfp;
ed9b544e 1865
1866 server.logfile = zstrdup(argv[1]);
bb0b03a3 1867 if (!strcasecmp(server.logfile,"stdout")) {
ed9b544e 1868 zfree(server.logfile);
1869 server.logfile = NULL;
1870 }
1871 if (server.logfile) {
1872 /* Test if we are able to open the file. The server will not
1873 * be able to abort just for this problem later... */
c9a111ac 1874 logfp = fopen(server.logfile,"a");
1875 if (logfp == NULL) {
ed9b544e 1876 err = sdscatprintf(sdsempty(),
1877 "Can't open the log file: %s", strerror(errno));
1878 goto loaderr;
1879 }
c9a111ac 1880 fclose(logfp);
ed9b544e 1881 }
bb0b03a3 1882 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
ed9b544e 1883 server.dbnum = atoi(argv[1]);
1884 if (server.dbnum < 1) {
1885 err = "Invalid number of databases"; goto loaderr;
1886 }
b3f83f12
JZ
1887 } else if (!strcasecmp(argv[0],"include") && argc == 2) {
1888 loadServerConfig(argv[1]);
285add55 1889 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
1890 server.maxclients = atoi(argv[1]);
3fd78bcd 1891 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
2b619329 1892 server.maxmemory = memtoll(argv[1],NULL);
bb0b03a3 1893 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
ed9b544e 1894 server.masterhost = sdsnew(argv[1]);
1895 server.masterport = atoi(argv[2]);
1896 server.replstate = REDIS_REPL_CONNECT;
d0ccebcf 1897 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
1898 server.masterauth = zstrdup(argv[1]);
bb0b03a3 1899 } else if (!strcasecmp(argv[0],"glueoutputbuf") && argc == 2) {
85dd2f3a 1900 if ((server.glueoutputbuf = yesnotoi(argv[1])) == -1) {
ed9b544e 1901 err = "argument must be 'yes' or 'no'"; goto loaderr;
1902 }
121f70cf 1903 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
1904 if ((server.rdbcompression = yesnotoi(argv[1])) == -1) {
8ca3e9d1 1905 err = "argument must be 'yes' or 'no'"; goto loaderr;
1906 }
1907 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
1908 if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
121f70cf 1909 err = "argument must be 'yes' or 'no'"; goto loaderr;
1910 }
bb0b03a3 1911 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
85dd2f3a 1912 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
ed9b544e 1913 err = "argument must be 'yes' or 'no'"; goto loaderr;
1914 }
44b38ef4 1915 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
1916 if ((server.appendonly = yesnotoi(argv[1])) == -1) {
1917 err = "argument must be 'yes' or 'no'"; goto loaderr;
1918 }
f3b52411
PN
1919 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
1920 zfree(server.appendfilename);
1921 server.appendfilename = zstrdup(argv[1]);
48f0308a 1922 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
1766c6da 1923 if (!strcasecmp(argv[1],"no")) {
48f0308a 1924 server.appendfsync = APPENDFSYNC_NO;
1766c6da 1925 } else if (!strcasecmp(argv[1],"always")) {
48f0308a 1926 server.appendfsync = APPENDFSYNC_ALWAYS;
1766c6da 1927 } else if (!strcasecmp(argv[1],"everysec")) {
48f0308a 1928 server.appendfsync = APPENDFSYNC_EVERYSEC;
1929 } else {
1930 err = "argument must be 'no', 'always' or 'everysec'";
1931 goto loaderr;
1932 }
bb0b03a3 1933 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
054e426d 1934 server.requirepass = zstrdup(argv[1]);
bb0b03a3 1935 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
500ece7c 1936 zfree(server.pidfile);
054e426d 1937 server.pidfile = zstrdup(argv[1]);
bb0b03a3 1938 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
500ece7c 1939 zfree(server.dbfilename);
054e426d 1940 server.dbfilename = zstrdup(argv[1]);
75680a3c 1941 } else if (!strcasecmp(argv[0],"vm-enabled") && argc == 2) {
1942 if ((server.vm_enabled = yesnotoi(argv[1])) == -1) {
1943 err = "argument must be 'yes' or 'no'"; goto loaderr;
1944 }
054e426d 1945 } else if (!strcasecmp(argv[0],"vm-swap-file") && argc == 2) {
fefed597 1946 zfree(server.vm_swap_file);
054e426d 1947 server.vm_swap_file = zstrdup(argv[1]);
4ef8de8a 1948 } else if (!strcasecmp(argv[0],"vm-max-memory") && argc == 2) {
2b619329 1949 server.vm_max_memory = memtoll(argv[1],NULL);
4ef8de8a 1950 } else if (!strcasecmp(argv[0],"vm-page-size") && argc == 2) {
2b619329 1951 server.vm_page_size = memtoll(argv[1], NULL);
4ef8de8a 1952 } else if (!strcasecmp(argv[0],"vm-pages") && argc == 2) {
2b619329 1953 server.vm_pages = memtoll(argv[1], NULL);
92f8e882 1954 } else if (!strcasecmp(argv[0],"vm-max-threads") && argc == 2) {
1955 server.vm_max_threads = strtoll(argv[1], NULL, 10);
cbba7dd7 1956 } else if (!strcasecmp(argv[0],"hash-max-zipmap-entries") && argc == 2){
2b619329 1957 server.hash_max_zipmap_entries = memtoll(argv[1], NULL);
cbba7dd7 1958 } else if (!strcasecmp(argv[0],"hash-max-zipmap-value") && argc == 2){
2b619329 1959 server.hash_max_zipmap_value = memtoll(argv[1], NULL);
ed9b544e 1960 } else {
1961 err = "Bad directive or wrong number of arguments"; goto loaderr;
1962 }
1963 for (j = 0; j < argc; j++)
1964 sdsfree(argv[j]);
1965 zfree(argv);
1966 sdsfree(line);
1967 }
c9a111ac 1968 if (fp != stdin) fclose(fp);
ed9b544e 1969 return;
1970
1971loaderr:
1972 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
1973 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
1974 fprintf(stderr, ">>> '%s'\n", line);
1975 fprintf(stderr, "%s\n", err);
1976 exit(1);
1977}
1978
1979static void freeClientArgv(redisClient *c) {
1980 int j;
1981
1982 for (j = 0; j < c->argc; j++)
1983 decrRefCount(c->argv[j]);
e8a74421 1984 for (j = 0; j < c->mbargc; j++)
1985 decrRefCount(c->mbargv[j]);
ed9b544e 1986 c->argc = 0;
e8a74421 1987 c->mbargc = 0;
ed9b544e 1988}
1989
1990static void freeClient(redisClient *c) {
1991 listNode *ln;
1992
4409877e 1993 /* Note that if the client we are freeing is blocked into a blocking
b0d8747d 1994 * call, we have to set querybuf to NULL *before* to call
1995 * unblockClientWaitingData() to avoid processInputBuffer() will get
1996 * called. Also it is important to remove the file events after
1997 * this, because this call adds the READABLE event. */
4409877e 1998 sdsfree(c->querybuf);
1999 c->querybuf = NULL;
2000 if (c->flags & REDIS_BLOCKED)
b0d8747d 2001 unblockClientWaitingData(c);
4409877e 2002
ffc6b7f8 2003 /* Unsubscribe from all the pubsub channels */
2004 pubsubUnsubscribeAllChannels(c,0);
2005 pubsubUnsubscribeAllPatterns(c,0);
2006 dictRelease(c->pubsub_channels);
2007 listRelease(c->pubsub_patterns);
befec3cd 2008 /* Obvious cleanup */
ed9b544e 2009 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
2010 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
ed9b544e 2011 listRelease(c->reply);
2012 freeClientArgv(c);
2013 close(c->fd);
92f8e882 2014 /* Remove from the list of clients */
ed9b544e 2015 ln = listSearchKey(server.clients,c);
dfc5e96c 2016 redisAssert(ln != NULL);
ed9b544e 2017 listDelNode(server.clients,ln);
d5d55fc3 2018 /* Remove from the list of clients waiting for swapped keys */
2019 if (c->flags & REDIS_IO_WAIT && listLength(c->io_keys) == 0) {
2020 ln = listSearchKey(server.io_ready_clients,c);
2021 if (ln) {
2022 listDelNode(server.io_ready_clients,ln);
2023 server.vm_blocked_clients--;
2024 }
2025 }
2026 while (server.vm_enabled && listLength(c->io_keys)) {
2027 ln = listFirst(c->io_keys);
2028 dontWaitForSwappedKey(c,ln->value);
92f8e882 2029 }
b3e3d0d7 2030 listRelease(c->io_keys);
befec3cd 2031 /* Master/slave cleanup */
ed9b544e 2032 if (c->flags & REDIS_SLAVE) {
6208b3a7 2033 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
2034 close(c->repldbfd);
87eca727 2035 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
2036 ln = listSearchKey(l,c);
dfc5e96c 2037 redisAssert(ln != NULL);
87eca727 2038 listDelNode(l,ln);
ed9b544e 2039 }
2040 if (c->flags & REDIS_MASTER) {
2041 server.master = NULL;
2042 server.replstate = REDIS_REPL_CONNECT;
2043 }
befec3cd 2044 /* Release memory */
93ea3759 2045 zfree(c->argv);
e8a74421 2046 zfree(c->mbargv);
6e469882 2047 freeClientMultiState(c);
ed9b544e 2048 zfree(c);
2049}
2050
cc30e368 2051#define GLUEREPLY_UP_TO (1024)
ed9b544e 2052static void glueReplyBuffersIfNeeded(redisClient *c) {
c28b42ac 2053 int copylen = 0;
2054 char buf[GLUEREPLY_UP_TO];
6208b3a7 2055 listNode *ln;
c7df85a4 2056 listIter li;
ed9b544e 2057 robj *o;
2058
c7df85a4 2059 listRewind(c->reply,&li);
2060 while((ln = listNext(&li))) {
c28b42ac 2061 int objlen;
2062
ed9b544e 2063 o = ln->value;
c28b42ac 2064 objlen = sdslen(o->ptr);
2065 if (copylen + objlen <= GLUEREPLY_UP_TO) {
2066 memcpy(buf+copylen,o->ptr,objlen);
2067 copylen += objlen;
ed9b544e 2068 listDelNode(c->reply,ln);
c28b42ac 2069 } else {
2070 if (copylen == 0) return;
2071 break;
ed9b544e 2072 }
ed9b544e 2073 }
c28b42ac 2074 /* Now the output buffer is empty, add the new single element */
2075 o = createObject(REDIS_STRING,sdsnewlen(buf,copylen));
2076 listAddNodeHead(c->reply,o);
ed9b544e 2077}
2078
2079static void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
2080 redisClient *c = privdata;
2081 int nwritten = 0, totwritten = 0, objlen;
2082 robj *o;
2083 REDIS_NOTUSED(el);
2084 REDIS_NOTUSED(mask);
2085
2895e862 2086 /* Use writev() if we have enough buffers to send */
7ea870c0 2087 if (!server.glueoutputbuf &&
e0a62c7f 2088 listLength(c->reply) > REDIS_WRITEV_THRESHOLD &&
7ea870c0 2089 !(c->flags & REDIS_MASTER))
2895e862 2090 {
2091 sendReplyToClientWritev(el, fd, privdata, mask);
2092 return;
2093 }
2895e862 2094
ed9b544e 2095 while(listLength(c->reply)) {
c28b42ac 2096 if (server.glueoutputbuf && listLength(c->reply) > 1)
2097 glueReplyBuffersIfNeeded(c);
2098
ed9b544e 2099 o = listNodeValue(listFirst(c->reply));
2100 objlen = sdslen(o->ptr);
2101
2102 if (objlen == 0) {
2103 listDelNode(c->reply,listFirst(c->reply));
2104 continue;
2105 }
2106
2107 if (c->flags & REDIS_MASTER) {
6f376729 2108 /* Don't reply to a master */
ed9b544e 2109 nwritten = objlen - c->sentlen;
2110 } else {
a4d1ba9a 2111 nwritten = write(fd, ((char*)o->ptr)+c->sentlen, objlen - c->sentlen);
ed9b544e 2112 if (nwritten <= 0) break;
2113 }
2114 c->sentlen += nwritten;
2115 totwritten += nwritten;
2116 /* If we fully sent the object on head go to the next one */
2117 if (c->sentlen == objlen) {
2118 listDelNode(c->reply,listFirst(c->reply));
2119 c->sentlen = 0;
2120 }
6f376729 2121 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
12f9d551 2122 * bytes, in a single threaded server it's a good idea to serve
6f376729 2123 * other clients as well, even if a very large request comes from
2124 * super fast link that is always able to accept data (in real world
12f9d551 2125 * scenario think about 'KEYS *' against the loopback interfae) */
6f376729 2126 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
ed9b544e 2127 }
2128 if (nwritten == -1) {
2129 if (errno == EAGAIN) {
2130 nwritten = 0;
2131 } else {
f870935d 2132 redisLog(REDIS_VERBOSE,
ed9b544e 2133 "Error writing to client: %s", strerror(errno));
2134 freeClient(c);
2135 return;
2136 }
2137 }
2138 if (totwritten > 0) c->lastinteraction = time(NULL);
2139 if (listLength(c->reply) == 0) {
2140 c->sentlen = 0;
2141 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2142 }
2143}
2144
2895e862 2145static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask)
2146{
2147 redisClient *c = privdata;
2148 int nwritten = 0, totwritten = 0, objlen, willwrite;
2149 robj *o;
2150 struct iovec iov[REDIS_WRITEV_IOVEC_COUNT];
2151 int offset, ion = 0;
2152 REDIS_NOTUSED(el);
2153 REDIS_NOTUSED(mask);
2154
2155 listNode *node;
2156 while (listLength(c->reply)) {
2157 offset = c->sentlen;
2158 ion = 0;
2159 willwrite = 0;
2160
2161 /* fill-in the iov[] array */
2162 for(node = listFirst(c->reply); node; node = listNextNode(node)) {
2163 o = listNodeValue(node);
2164 objlen = sdslen(o->ptr);
2165
e0a62c7f 2166 if (totwritten + objlen - offset > REDIS_MAX_WRITE_PER_EVENT)
2895e862 2167 break;
2168
2169 if(ion == REDIS_WRITEV_IOVEC_COUNT)
2170 break; /* no more iovecs */
2171
2172 iov[ion].iov_base = ((char*)o->ptr) + offset;
2173 iov[ion].iov_len = objlen - offset;
2174 willwrite += objlen - offset;
2175 offset = 0; /* just for the first item */
2176 ion++;
2177 }
2178
2179 if(willwrite == 0)
2180 break;
2181
2182 /* write all collected blocks at once */
2183 if((nwritten = writev(fd, iov, ion)) < 0) {
2184 if (errno != EAGAIN) {
f870935d 2185 redisLog(REDIS_VERBOSE,
2895e862 2186 "Error writing to client: %s", strerror(errno));
2187 freeClient(c);
2188 return;
2189 }
2190 break;
2191 }
2192
2193 totwritten += nwritten;
2194 offset = c->sentlen;
2195
2196 /* remove written robjs from c->reply */
2197 while (nwritten && listLength(c->reply)) {
2198 o = listNodeValue(listFirst(c->reply));
2199 objlen = sdslen(o->ptr);
2200
2201 if(nwritten >= objlen - offset) {
2202 listDelNode(c->reply, listFirst(c->reply));
2203 nwritten -= objlen - offset;
2204 c->sentlen = 0;
2205 } else {
2206 /* partial write */
2207 c->sentlen += nwritten;
2208 break;
2209 }
2210 offset = 0;
2211 }
2212 }
2213
e0a62c7f 2214 if (totwritten > 0)
2895e862 2215 c->lastinteraction = time(NULL);
2216
2217 if (listLength(c->reply) == 0) {
2218 c->sentlen = 0;
2219 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2220 }
2221}
2222
ed9b544e 2223static struct redisCommand *lookupCommand(char *name) {
2224 int j = 0;
2225 while(cmdTable[j].name != NULL) {
bb0b03a3 2226 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
ed9b544e 2227 j++;
2228 }
2229 return NULL;
2230}
2231
2232/* resetClient prepare the client to process the next command */
2233static void resetClient(redisClient *c) {
2234 freeClientArgv(c);
2235 c->bulklen = -1;
e8a74421 2236 c->multibulk = 0;
ed9b544e 2237}
2238
6e469882 2239/* Call() is the core of Redis execution of a command */
2240static void call(redisClient *c, struct redisCommand *cmd) {
2241 long long dirty;
2242
2243 dirty = server.dirty;
2244 cmd->proc(c);
4005fef1 2245 dirty = server.dirty-dirty;
2246
2247 if (server.appendonly && dirty)
6e469882 2248 feedAppendOnlyFile(cmd,c->db->id,c->argv,c->argc);
4005fef1 2249 if ((dirty || cmd->flags & REDIS_CMD_FORCE_REPLICATION) &&
2250 listLength(server.slaves))
248ea310 2251 replicationFeedSlaves(server.slaves,c->db->id,c->argv,c->argc);
6e469882 2252 if (listLength(server.monitors))
dd142b9c 2253 replicationFeedMonitors(server.monitors,c->db->id,c->argv,c->argc);
6e469882 2254 server.stat_numcommands++;
2255}
2256
ed9b544e 2257/* If this function gets called we already read a whole
2258 * command, argments are in the client argv/argc fields.
2259 * processCommand() execute the command or prepare the
2260 * server for a bulk read from the client.
2261 *
2262 * If 1 is returned the client is still alive and valid and
2263 * and other operations can be performed by the caller. Otherwise
2264 * if 0 is returned the client was destroied (i.e. after QUIT). */
2265static int processCommand(redisClient *c) {
2266 struct redisCommand *cmd;
ed9b544e 2267
3fd78bcd 2268 /* Free some memory if needed (maxmemory setting) */
2269 if (server.maxmemory) freeMemoryIfNeeded();
2270
e8a74421 2271 /* Handle the multi bulk command type. This is an alternative protocol
2272 * supported by Redis in order to receive commands that are composed of
2273 * multiple binary-safe "bulk" arguments. The latency of processing is
2274 * a bit higher but this allows things like multi-sets, so if this
2275 * protocol is used only for MSET and similar commands this is a big win. */
2276 if (c->multibulk == 0 && c->argc == 1 && ((char*)(c->argv[0]->ptr))[0] == '*') {
2277 c->multibulk = atoi(((char*)c->argv[0]->ptr)+1);
2278 if (c->multibulk <= 0) {
2279 resetClient(c);
2280 return 1;
2281 } else {
2282 decrRefCount(c->argv[c->argc-1]);
2283 c->argc--;
2284 return 1;
2285 }
2286 } else if (c->multibulk) {
2287 if (c->bulklen == -1) {
2288 if (((char*)c->argv[0]->ptr)[0] != '$') {
2289 addReplySds(c,sdsnew("-ERR multi bulk protocol error\r\n"));
2290 resetClient(c);
2291 return 1;
2292 } else {
2293 int bulklen = atoi(((char*)c->argv[0]->ptr)+1);
2294 decrRefCount(c->argv[0]);
2295 if (bulklen < 0 || bulklen > 1024*1024*1024) {
2296 c->argc--;
2297 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
2298 resetClient(c);
2299 return 1;
2300 }
2301 c->argc--;
2302 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
2303 return 1;
2304 }
2305 } else {
2306 c->mbargv = zrealloc(c->mbargv,(sizeof(robj*))*(c->mbargc+1));
2307 c->mbargv[c->mbargc] = c->argv[0];
2308 c->mbargc++;
2309 c->argc--;
2310 c->multibulk--;
2311 if (c->multibulk == 0) {
2312 robj **auxargv;
2313 int auxargc;
2314
2315 /* Here we need to swap the multi-bulk argc/argv with the
2316 * normal argc/argv of the client structure. */
2317 auxargv = c->argv;
2318 c->argv = c->mbargv;
2319 c->mbargv = auxargv;
2320
2321 auxargc = c->argc;
2322 c->argc = c->mbargc;
2323 c->mbargc = auxargc;
2324
2325 /* We need to set bulklen to something different than -1
2326 * in order for the code below to process the command without
2327 * to try to read the last argument of a bulk command as
2328 * a special argument. */
2329 c->bulklen = 0;
2330 /* continue below and process the command */
2331 } else {
2332 c->bulklen = -1;
2333 return 1;
2334 }
2335 }
2336 }
2337 /* -- end of multi bulk commands processing -- */
2338
ed9b544e 2339 /* The QUIT command is handled as a special case. Normal command
2340 * procs are unable to close the client connection safely */
bb0b03a3 2341 if (!strcasecmp(c->argv[0]->ptr,"quit")) {
ed9b544e 2342 freeClient(c);
2343 return 0;
2344 }
d5d55fc3 2345
2346 /* Now lookup the command and check ASAP about trivial error conditions
2347 * such wrong arity, bad command name and so forth. */
ed9b544e 2348 cmd = lookupCommand(c->argv[0]->ptr);
2349 if (!cmd) {
2c14807b 2350 addReplySds(c,
2351 sdscatprintf(sdsempty(), "-ERR unknown command '%s'\r\n",
2352 (char*)c->argv[0]->ptr));
ed9b544e 2353 resetClient(c);
2354 return 1;
2355 } else if ((cmd->arity > 0 && cmd->arity != c->argc) ||
2356 (c->argc < -cmd->arity)) {
454d4e43 2357 addReplySds(c,
2358 sdscatprintf(sdsempty(),
2359 "-ERR wrong number of arguments for '%s' command\r\n",
2360 cmd->name));
ed9b544e 2361 resetClient(c);
2362 return 1;
2363 } else if (cmd->flags & REDIS_CMD_BULK && c->bulklen == -1) {
d5d55fc3 2364 /* This is a bulk command, we have to read the last argument yet. */
ed9b544e 2365 int bulklen = atoi(c->argv[c->argc-1]->ptr);
2366
2367 decrRefCount(c->argv[c->argc-1]);
2368 if (bulklen < 0 || bulklen > 1024*1024*1024) {
2369 c->argc--;
2370 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
2371 resetClient(c);
2372 return 1;
2373 }
2374 c->argc--;
2375 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
2376 /* It is possible that the bulk read is already in the
8d0490e7 2377 * buffer. Check this condition and handle it accordingly.
2378 * This is just a fast path, alternative to call processInputBuffer().
2379 * It's a good idea since the code is small and this condition
2380 * happens most of the times. */
ed9b544e 2381 if ((signed)sdslen(c->querybuf) >= c->bulklen) {
2382 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2383 c->argc++;
2384 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
2385 } else {
d5d55fc3 2386 /* Otherwise return... there is to read the last argument
2387 * from the socket. */
ed9b544e 2388 return 1;
2389 }
2390 }
942a3961 2391 /* Let's try to encode the bulk object to save space. */
2392 if (cmd->flags & REDIS_CMD_BULK)
05df7621 2393 c->argv[c->argc-1] = tryObjectEncoding(c->argv[c->argc-1]);
942a3961 2394
e63943a4 2395 /* Check if the user is authenticated */
2396 if (server.requirepass && !c->authenticated && cmd->proc != authCommand) {
2397 addReplySds(c,sdsnew("-ERR operation not permitted\r\n"));
2398 resetClient(c);
2399 return 1;
2400 }
2401
b61a28fe 2402 /* Handle the maxmemory directive */
2403 if (server.maxmemory && (cmd->flags & REDIS_CMD_DENYOOM) &&
2404 zmalloc_used_memory() > server.maxmemory)
2405 {
2406 addReplySds(c,sdsnew("-ERR command not allowed when used memory > 'maxmemory'\r\n"));
2407 resetClient(c);
2408 return 1;
2409 }
2410
d6cc8867 2411 /* Only allow SUBSCRIBE and UNSUBSCRIBE in the context of Pub/Sub */
e6cca5db 2412 if ((dictSize(c->pubsub_channels) > 0 || listLength(c->pubsub_patterns) > 0)
2413 &&
ffc6b7f8 2414 cmd->proc != subscribeCommand && cmd->proc != unsubscribeCommand &&
2415 cmd->proc != psubscribeCommand && cmd->proc != punsubscribeCommand) {
2416 addReplySds(c,sdsnew("-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context\r\n"));
d6cc8867 2417 resetClient(c);
2418 return 1;
2419 }
2420
ed9b544e 2421 /* Exec the command */
18b6cb76 2422 if (c->flags & REDIS_MULTI && cmd->proc != execCommand && cmd->proc != discardCommand) {
6e469882 2423 queueMultiCommand(c,cmd);
2424 addReply(c,shared.queued);
2425 } else {
d5d55fc3 2426 if (server.vm_enabled && server.vm_max_threads > 0 &&
0a6f3f0f 2427 blockClientOnSwappedKeys(c,cmd)) return 1;
6e469882 2428 call(c,cmd);
2429 }
ed9b544e 2430
2431 /* Prepare the client for the next command */
ed9b544e 2432 resetClient(c);
2433 return 1;
2434}
2435
248ea310 2436static void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
6208b3a7 2437 listNode *ln;
c7df85a4 2438 listIter li;
ed9b544e 2439 int outc = 0, j;
93ea3759 2440 robj **outv;
248ea310 2441 /* We need 1+(ARGS*3) objects since commands are using the new protocol
2442 * and we one 1 object for the first "*<count>\r\n" multibulk count, then
2443 * for every additional object we have "$<count>\r\n" + object + "\r\n". */
2444 robj *static_outv[REDIS_STATIC_ARGS*3+1];
2445 robj *lenobj;
93ea3759 2446
2447 if (argc <= REDIS_STATIC_ARGS) {
2448 outv = static_outv;
2449 } else {
248ea310 2450 outv = zmalloc(sizeof(robj*)*(argc*3+1));
93ea3759 2451 }
248ea310 2452
2453 lenobj = createObject(REDIS_STRING,
2454 sdscatprintf(sdsempty(), "*%d\r\n", argc));
2455 lenobj->refcount = 0;
2456 outv[outc++] = lenobj;
ed9b544e 2457 for (j = 0; j < argc; j++) {
248ea310 2458 lenobj = createObject(REDIS_STRING,
2459 sdscatprintf(sdsempty(),"$%lu\r\n",
2460 (unsigned long) stringObjectLen(argv[j])));
2461 lenobj->refcount = 0;
2462 outv[outc++] = lenobj;
ed9b544e 2463 outv[outc++] = argv[j];
248ea310 2464 outv[outc++] = shared.crlf;
ed9b544e 2465 }
ed9b544e 2466
40d224a9 2467 /* Increment all the refcounts at start and decrement at end in order to
2468 * be sure to free objects if there is no slave in a replication state
2469 * able to be feed with commands */
2470 for (j = 0; j < outc; j++) incrRefCount(outv[j]);
c7df85a4 2471 listRewind(slaves,&li);
2472 while((ln = listNext(&li))) {
ed9b544e 2473 redisClient *slave = ln->value;
40d224a9 2474
2475 /* Don't feed slaves that are still waiting for BGSAVE to start */
6208b3a7 2476 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue;
40d224a9 2477
2478 /* Feed all the other slaves, MONITORs and so on */
ed9b544e 2479 if (slave->slaveseldb != dictid) {
2480 robj *selectcmd;
2481
2482 switch(dictid) {
2483 case 0: selectcmd = shared.select0; break;
2484 case 1: selectcmd = shared.select1; break;
2485 case 2: selectcmd = shared.select2; break;
2486 case 3: selectcmd = shared.select3; break;
2487 case 4: selectcmd = shared.select4; break;
2488 case 5: selectcmd = shared.select5; break;
2489 case 6: selectcmd = shared.select6; break;
2490 case 7: selectcmd = shared.select7; break;
2491 case 8: selectcmd = shared.select8; break;
2492 case 9: selectcmd = shared.select9; break;
2493 default:
2494 selectcmd = createObject(REDIS_STRING,
2495 sdscatprintf(sdsempty(),"select %d\r\n",dictid));
2496 selectcmd->refcount = 0;
2497 break;
2498 }
2499 addReply(slave,selectcmd);
2500 slave->slaveseldb = dictid;
2501 }
2502 for (j = 0; j < outc; j++) addReply(slave,outv[j]);
ed9b544e 2503 }
40d224a9 2504 for (j = 0; j < outc; j++) decrRefCount(outv[j]);
93ea3759 2505 if (outv != static_outv) zfree(outv);
ed9b544e 2506}
2507
dd142b9c 2508static sds sdscatrepr(sds s, char *p, size_t len) {
2509 s = sdscatlen(s,"\"",1);
2510 while(len--) {
2511 switch(*p) {
2512 case '\\':
2513 case '"':
2514 s = sdscatprintf(s,"\\%c",*p);
2515 break;
2516 case '\n': s = sdscatlen(s,"\\n",1); break;
2517 case '\r': s = sdscatlen(s,"\\r",1); break;
2518 case '\t': s = sdscatlen(s,"\\t",1); break;
2519 case '\a': s = sdscatlen(s,"\\a",1); break;
2520 case '\b': s = sdscatlen(s,"\\b",1); break;
2521 default:
2522 if (isprint(*p))
2523 s = sdscatprintf(s,"%c",*p);
2524 else
2525 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
2526 break;
2527 }
2528 p++;
2529 }
2530 return sdscatlen(s,"\"",1);
2531}
2532
2533static void replicationFeedMonitors(list *monitors, int dictid, robj **argv, int argc) {
2534 listNode *ln;
2535 listIter li;
2536 int j;
2537 sds cmdrepr = sdsnew("+");
2538 robj *cmdobj;
2539 struct timeval tv;
2540
2541 gettimeofday(&tv,NULL);
2542 cmdrepr = sdscatprintf(cmdrepr,"%ld.%ld ",(long)tv.tv_sec,(long)tv.tv_usec);
2543 if (dictid != 0) cmdrepr = sdscatprintf(cmdrepr,"(db %d) ", dictid);
2544
2545 for (j = 0; j < argc; j++) {
2546 if (argv[j]->encoding == REDIS_ENCODING_INT) {
2547 cmdrepr = sdscatprintf(cmdrepr, "%ld", (long)argv[j]->ptr);
2548 } else {
2549 cmdrepr = sdscatrepr(cmdrepr,(char*)argv[j]->ptr,
2550 sdslen(argv[j]->ptr));
2551 }
2552 if (j != argc-1)
2553 cmdrepr = sdscatlen(cmdrepr," ",1);
2554 }
2555 cmdrepr = sdscatlen(cmdrepr,"\r\n",2);
2556 cmdobj = createObject(REDIS_STRING,cmdrepr);
2557
2558 listRewind(monitors,&li);
2559 while((ln = listNext(&li))) {
2560 redisClient *monitor = ln->value;
2561 addReply(monitor,cmdobj);
2562 }
2563 decrRefCount(cmdobj);
2564}
2565
638e42ac 2566static void processInputBuffer(redisClient *c) {
ed9b544e 2567again:
4409877e 2568 /* Before to process the input buffer, make sure the client is not
2569 * waitig for a blocking operation such as BLPOP. Note that the first
2570 * iteration the client is never blocked, otherwise the processInputBuffer
2571 * would not be called at all, but after the execution of the first commands
2572 * in the input buffer the client may be blocked, and the "goto again"
2573 * will try to reiterate. The following line will make it return asap. */
92f8e882 2574 if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return;
ed9b544e 2575 if (c->bulklen == -1) {
2576 /* Read the first line of the query */
2577 char *p = strchr(c->querybuf,'\n');
2578 size_t querylen;
644fafa3 2579
ed9b544e 2580 if (p) {
2581 sds query, *argv;
2582 int argc, j;
e0a62c7f 2583
ed9b544e 2584 query = c->querybuf;
2585 c->querybuf = sdsempty();
2586 querylen = 1+(p-(query));
2587 if (sdslen(query) > querylen) {
2588 /* leave data after the first line of the query in the buffer */
2589 c->querybuf = sdscatlen(c->querybuf,query+querylen,sdslen(query)-querylen);
2590 }
2591 *p = '\0'; /* remove "\n" */
2592 if (*(p-1) == '\r') *(p-1) = '\0'; /* and "\r" if any */
2593 sdsupdatelen(query);
2594
2595 /* Now we can split the query in arguments */
ed9b544e 2596 argv = sdssplitlen(query,sdslen(query)," ",1,&argc);
93ea3759 2597 sdsfree(query);
2598
2599 if (c->argv) zfree(c->argv);
2600 c->argv = zmalloc(sizeof(robj*)*argc);
93ea3759 2601
2602 for (j = 0; j < argc; j++) {
ed9b544e 2603 if (sdslen(argv[j])) {
2604 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
2605 c->argc++;
2606 } else {
2607 sdsfree(argv[j]);
2608 }
2609 }
2610 zfree(argv);
7c49733c 2611 if (c->argc) {
2612 /* Execute the command. If the client is still valid
2613 * after processCommand() return and there is something
2614 * on the query buffer try to process the next command. */
2615 if (processCommand(c) && sdslen(c->querybuf)) goto again;
2616 } else {
2617 /* Nothing to process, argc == 0. Just process the query
2618 * buffer if it's not empty or return to the caller */
2619 if (sdslen(c->querybuf)) goto again;
2620 }
ed9b544e 2621 return;
644fafa3 2622 } else if (sdslen(c->querybuf) >= REDIS_REQUEST_MAX_SIZE) {
f870935d 2623 redisLog(REDIS_VERBOSE, "Client protocol error");
ed9b544e 2624 freeClient(c);
2625 return;
2626 }
2627 } else {
2628 /* Bulk read handling. Note that if we are at this point
2629 the client already sent a command terminated with a newline,
2630 we are reading the bulk data that is actually the last
2631 argument of the command. */
2632 int qbl = sdslen(c->querybuf);
2633
2634 if (c->bulklen <= qbl) {
2635 /* Copy everything but the final CRLF as final argument */
2636 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2637 c->argc++;
2638 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
638e42ac 2639 /* Process the command. If the client is still valid after
2640 * the processing and there is more data in the buffer
2641 * try to parse it. */
2642 if (processCommand(c) && sdslen(c->querybuf)) goto again;
ed9b544e 2643 return;
2644 }
2645 }
2646}
2647
638e42ac 2648static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
2649 redisClient *c = (redisClient*) privdata;
2650 char buf[REDIS_IOBUF_LEN];
2651 int nread;
2652 REDIS_NOTUSED(el);
2653 REDIS_NOTUSED(mask);
2654
2655 nread = read(fd, buf, REDIS_IOBUF_LEN);
2656 if (nread == -1) {
2657 if (errno == EAGAIN) {
2658 nread = 0;
2659 } else {
f870935d 2660 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
638e42ac 2661 freeClient(c);
2662 return;
2663 }
2664 } else if (nread == 0) {
f870935d 2665 redisLog(REDIS_VERBOSE, "Client closed connection");
638e42ac 2666 freeClient(c);
2667 return;
2668 }
2669 if (nread) {
2670 c->querybuf = sdscatlen(c->querybuf, buf, nread);
2671 c->lastinteraction = time(NULL);
2672 } else {
2673 return;
2674 }
168ac5c6 2675 processInputBuffer(c);
638e42ac 2676}
2677
ed9b544e 2678static int selectDb(redisClient *c, int id) {
2679 if (id < 0 || id >= server.dbnum)
2680 return REDIS_ERR;
3305306f 2681 c->db = &server.db[id];
ed9b544e 2682 return REDIS_OK;
2683}
2684
40d224a9 2685static void *dupClientReplyValue(void *o) {
2686 incrRefCount((robj*)o);
12d090d2 2687 return o;
40d224a9 2688}
2689
ffc6b7f8 2690static int listMatchObjects(void *a, void *b) {
bf028098 2691 return equalStringObjects(a,b);
ffc6b7f8 2692}
2693
ed9b544e 2694static redisClient *createClient(int fd) {
2695 redisClient *c = zmalloc(sizeof(*c));
2696
2697 anetNonBlock(NULL,fd);
2698 anetTcpNoDelay(NULL,fd);
2699 if (!c) return NULL;
2700 selectDb(c,0);
2701 c->fd = fd;
2702 c->querybuf = sdsempty();
2703 c->argc = 0;
93ea3759 2704 c->argv = NULL;
ed9b544e 2705 c->bulklen = -1;
e8a74421 2706 c->multibulk = 0;
2707 c->mbargc = 0;
2708 c->mbargv = NULL;
ed9b544e 2709 c->sentlen = 0;
2710 c->flags = 0;
2711 c->lastinteraction = time(NULL);
abcb223e 2712 c->authenticated = 0;
40d224a9 2713 c->replstate = REDIS_REPL_NONE;
6b47e12e 2714 c->reply = listCreate();
ed9b544e 2715 listSetFreeMethod(c->reply,decrRefCount);
40d224a9 2716 listSetDupMethod(c->reply,dupClientReplyValue);
92f8e882 2717 c->blockingkeys = NULL;
2718 c->blockingkeysnum = 0;
2719 c->io_keys = listCreate();
2720 listSetFreeMethod(c->io_keys,decrRefCount);
ffc6b7f8 2721 c->pubsub_channels = dictCreate(&setDictType,NULL);
2722 c->pubsub_patterns = listCreate();
2723 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
2724 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
ed9b544e 2725 if (aeCreateFileEvent(server.el, c->fd, AE_READABLE,
266373b2 2726 readQueryFromClient, c) == AE_ERR) {
ed9b544e 2727 freeClient(c);
2728 return NULL;
2729 }
6b47e12e 2730 listAddNodeTail(server.clients,c);
6e469882 2731 initClientMultiState(c);
ed9b544e 2732 return c;
2733}
2734
2735static void addReply(redisClient *c, robj *obj) {
2736 if (listLength(c->reply) == 0 &&
6208b3a7 2737 (c->replstate == REDIS_REPL_NONE ||
2738 c->replstate == REDIS_REPL_ONLINE) &&
ed9b544e 2739 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
266373b2 2740 sendReplyToClient, c) == AE_ERR) return;
e3cadb8a 2741
2742 if (server.vm_enabled && obj->storage != REDIS_VM_MEMORY) {
2743 obj = dupStringObject(obj);
2744 obj->refcount = 0; /* getDecodedObject() will increment the refcount */
2745 }
9d65a1bb 2746 listAddNodeTail(c->reply,getDecodedObject(obj));
ed9b544e 2747}
2748
2749static void addReplySds(redisClient *c, sds s) {
2750 robj *o = createObject(REDIS_STRING,s);
2751 addReply(c,o);
2752 decrRefCount(o);
2753}
2754
e2665397 2755static void addReplyDouble(redisClient *c, double d) {
2756 char buf[128];
2757
2758 snprintf(buf,sizeof(buf),"%.17g",d);
682ac724 2759 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
83c6a618 2760 (unsigned long) strlen(buf),buf));
e2665397 2761}
2762
aa7c2934
PN
2763static void addReplyLongLong(redisClient *c, long long ll) {
2764 char buf[128];
2765 size_t len;
2766
2767 if (ll == 0) {
2768 addReply(c,shared.czero);
2769 return;
2770 } else if (ll == 1) {
2771 addReply(c,shared.cone);
2772 return;
2773 }
482b672d 2774 buf[0] = ':';
2775 len = ll2string(buf+1,sizeof(buf)-1,ll);
2776 buf[len+1] = '\r';
2777 buf[len+2] = '\n';
2778 addReplySds(c,sdsnewlen(buf,len+3));
aa7c2934
PN
2779}
2780
92b27fe9 2781static void addReplyUlong(redisClient *c, unsigned long ul) {
2782 char buf[128];
2783 size_t len;
2784
dd88747b 2785 if (ul == 0) {
2786 addReply(c,shared.czero);
2787 return;
2788 } else if (ul == 1) {
2789 addReply(c,shared.cone);
2790 return;
2791 }
92b27fe9 2792 len = snprintf(buf,sizeof(buf),":%lu\r\n",ul);
2793 addReplySds(c,sdsnewlen(buf,len));
2794}
2795
942a3961 2796static void addReplyBulkLen(redisClient *c, robj *obj) {
482b672d 2797 size_t len, intlen;
2798 char buf[128];
942a3961 2799
2800 if (obj->encoding == REDIS_ENCODING_RAW) {
2801 len = sdslen(obj->ptr);
2802 } else {
2803 long n = (long)obj->ptr;
2804
e054afda 2805 /* Compute how many bytes will take this integer as a radix 10 string */
942a3961 2806 len = 1;
2807 if (n < 0) {
2808 len++;
2809 n = -n;
2810 }
2811 while((n = n/10) != 0) {
2812 len++;
2813 }
2814 }
482b672d 2815 buf[0] = '$';
2816 intlen = ll2string(buf+1,sizeof(buf)-1,(long long)len);
2817 buf[intlen+1] = '\r';
2818 buf[intlen+2] = '\n';
2819 addReplySds(c,sdsnewlen(buf,intlen+3));
942a3961 2820}
2821
dd88747b 2822static void addReplyBulk(redisClient *c, robj *obj) {
2823 addReplyBulkLen(c,obj);
2824 addReply(c,obj);
2825 addReply(c,shared.crlf);
2826}
2827
500ece7c 2828/* In the CONFIG command we need to add vanilla C string as bulk replies */
2829static void addReplyBulkCString(redisClient *c, char *s) {
2830 if (s == NULL) {
2831 addReply(c,shared.nullbulk);
2832 } else {
2833 robj *o = createStringObject(s,strlen(s));
2834 addReplyBulk(c,o);
2835 decrRefCount(o);
2836 }
2837}
2838
ed9b544e 2839static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
2840 int cport, cfd;
2841 char cip[128];
285add55 2842 redisClient *c;
ed9b544e 2843 REDIS_NOTUSED(el);
2844 REDIS_NOTUSED(mask);
2845 REDIS_NOTUSED(privdata);
2846
2847 cfd = anetAccept(server.neterr, fd, cip, &cport);
2848 if (cfd == AE_ERR) {
f870935d 2849 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
ed9b544e 2850 return;
2851 }
f870935d 2852 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
285add55 2853 if ((c = createClient(cfd)) == NULL) {
ed9b544e 2854 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
2855 close(cfd); /* May be already closed, just ingore errors */
2856 return;
2857 }
285add55 2858 /* If maxclient directive is set and this is one client more... close the
2859 * connection. Note that we create the client instead to check before
2860 * for this condition, since now the socket is already set in nonblocking
2861 * mode and we can send an error for free using the Kernel I/O */
2862 if (server.maxclients && listLength(server.clients) > server.maxclients) {
2863 char *err = "-ERR max number of clients reached\r\n";
2864
2865 /* That's a best effort error message, don't check write errors */
fee803ba 2866 if (write(c->fd,err,strlen(err)) == -1) {
2867 /* Nothing to do, Just to avoid the warning... */
2868 }
285add55 2869 freeClient(c);
2870 return;
2871 }
ed9b544e 2872 server.stat_numconnections++;
2873}
2874
2875/* ======================= Redis objects implementation ===================== */
2876
2877static robj *createObject(int type, void *ptr) {
2878 robj *o;
2879
a5819310 2880 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
ed9b544e 2881 if (listLength(server.objfreelist)) {
2882 listNode *head = listFirst(server.objfreelist);
2883 o = listNodeValue(head);
2884 listDelNode(server.objfreelist,head);
a5819310 2885 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
ed9b544e 2886 } else {
75680a3c 2887 if (server.vm_enabled) {
a5819310 2888 pthread_mutex_unlock(&server.obj_freelist_mutex);
75680a3c 2889 o = zmalloc(sizeof(*o));
2890 } else {
2891 o = zmalloc(sizeof(*o)-sizeof(struct redisObjectVM));
2892 }
ed9b544e 2893 }
ed9b544e 2894 o->type = type;
942a3961 2895 o->encoding = REDIS_ENCODING_RAW;
ed9b544e 2896 o->ptr = ptr;
2897 o->refcount = 1;
3a66edc7 2898 if (server.vm_enabled) {
1064ef87 2899 /* Note that this code may run in the context of an I/O thread
2900 * and accessing to server.unixtime in theory is an error
2901 * (no locks). But in practice this is safe, and even if we read
2902 * garbage Redis will not fail, as it's just a statistical info */
3a66edc7 2903 o->vm.atime = server.unixtime;
2904 o->storage = REDIS_VM_MEMORY;
2905 }
ed9b544e 2906 return o;
2907}
2908
2909static robj *createStringObject(char *ptr, size_t len) {
2910 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
2911}
2912
3f973463
PN
2913static robj *createStringObjectFromLongLong(long long value) {
2914 robj *o;
2915 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
2916 incrRefCount(shared.integers[value]);
2917 o = shared.integers[value];
2918 } else {
2919 o = createObject(REDIS_STRING, NULL);
2920 if (value >= LONG_MIN && value <= LONG_MAX) {
2921 o->encoding = REDIS_ENCODING_INT;
2922 o->ptr = (void*)((long)value);
2923 } else {
ee14da56 2924 o = createObject(REDIS_STRING,sdsfromlonglong(value));
3f973463
PN
2925 }
2926 }
2927 return o;
2928}
2929
4ef8de8a 2930static robj *dupStringObject(robj *o) {
b9bc0eef 2931 assert(o->encoding == REDIS_ENCODING_RAW);
4ef8de8a 2932 return createStringObject(o->ptr,sdslen(o->ptr));
2933}
2934
ed9b544e 2935static robj *createListObject(void) {
2936 list *l = listCreate();
2937
ed9b544e 2938 listSetFreeMethod(l,decrRefCount);
2939 return createObject(REDIS_LIST,l);
2940}
2941
2942static robj *createSetObject(void) {
2943 dict *d = dictCreate(&setDictType,NULL);
ed9b544e 2944 return createObject(REDIS_SET,d);
2945}
2946
5234952b 2947static robj *createHashObject(void) {
2948 /* All the Hashes start as zipmaps. Will be automatically converted
2949 * into hash tables if there are enough elements or big elements
2950 * inside. */
2951 unsigned char *zm = zipmapNew();
2952 robj *o = createObject(REDIS_HASH,zm);
2953 o->encoding = REDIS_ENCODING_ZIPMAP;
2954 return o;
2955}
2956
1812e024 2957static robj *createZsetObject(void) {
6b47e12e 2958 zset *zs = zmalloc(sizeof(*zs));
2959
2960 zs->dict = dictCreate(&zsetDictType,NULL);
2961 zs->zsl = zslCreate();
2962 return createObject(REDIS_ZSET,zs);
1812e024 2963}
2964
ed9b544e 2965static void freeStringObject(robj *o) {
942a3961 2966 if (o->encoding == REDIS_ENCODING_RAW) {
2967 sdsfree(o->ptr);
2968 }
ed9b544e 2969}
2970
2971static void freeListObject(robj *o) {
2972 listRelease((list*) o->ptr);
2973}
2974
2975static void freeSetObject(robj *o) {
2976 dictRelease((dict*) o->ptr);
2977}
2978
fd8ccf44 2979static void freeZsetObject(robj *o) {
2980 zset *zs = o->ptr;
2981
2982 dictRelease(zs->dict);
2983 zslFree(zs->zsl);
2984 zfree(zs);
2985}
2986
ed9b544e 2987static void freeHashObject(robj *o) {
cbba7dd7 2988 switch (o->encoding) {
2989 case REDIS_ENCODING_HT:
2990 dictRelease((dict*) o->ptr);
2991 break;
2992 case REDIS_ENCODING_ZIPMAP:
2993 zfree(o->ptr);
2994 break;
2995 default:
f83c6cb5 2996 redisPanic("Unknown hash encoding type");
cbba7dd7 2997 break;
2998 }
ed9b544e 2999}
3000
3001static void incrRefCount(robj *o) {
3002 o->refcount++;
3003}
3004
3005static void decrRefCount(void *obj) {
3006 robj *o = obj;
94754ccc 3007
c651fd9e 3008 if (o->refcount <= 0) redisPanic("decrRefCount against refcount <= 0");
970e10bb 3009 /* Object is a key of a swapped out value, or in the process of being
3010 * loaded. */
996cb5f7 3011 if (server.vm_enabled &&
3012 (o->storage == REDIS_VM_SWAPPED || o->storage == REDIS_VM_LOADING))
3013 {
996cb5f7 3014 if (o->storage == REDIS_VM_LOADING) vmCancelThreadedIOJob(obj);
f2b8ab34 3015 redisAssert(o->type == REDIS_STRING);
a35ddf12 3016 freeStringObject(o);
3017 vmMarkPagesFree(o->vm.page,o->vm.usedpages);
a5819310 3018 pthread_mutex_lock(&server.obj_freelist_mutex);
a35ddf12 3019 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
3020 !listAddNodeHead(server.objfreelist,o))
3021 zfree(o);
a5819310 3022 pthread_mutex_unlock(&server.obj_freelist_mutex);
7d98e08c 3023 server.vm_stats_swapped_objects--;
a35ddf12 3024 return;
3025 }
996cb5f7 3026 /* Object is in memory, or in the process of being swapped out. */
ed9b544e 3027 if (--(o->refcount) == 0) {
996cb5f7 3028 if (server.vm_enabled && o->storage == REDIS_VM_SWAPPING)
3029 vmCancelThreadedIOJob(obj);
ed9b544e 3030 switch(o->type) {
3031 case REDIS_STRING: freeStringObject(o); break;
3032 case REDIS_LIST: freeListObject(o); break;
3033 case REDIS_SET: freeSetObject(o); break;
fd8ccf44 3034 case REDIS_ZSET: freeZsetObject(o); break;
ed9b544e 3035 case REDIS_HASH: freeHashObject(o); break;
f83c6cb5 3036 default: redisPanic("Unknown object type"); break;
ed9b544e 3037 }
a5819310 3038 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
ed9b544e 3039 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
3040 !listAddNodeHead(server.objfreelist,o))
3041 zfree(o);
a5819310 3042 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
ed9b544e 3043 }
3044}
3045
942a3961 3046static robj *lookupKey(redisDb *db, robj *key) {
3047 dictEntry *de = dictFind(db->dict,key);
3a66edc7 3048 if (de) {
55cf8433 3049 robj *key = dictGetEntryKey(de);
3050 robj *val = dictGetEntryVal(de);
3a66edc7 3051
55cf8433 3052 if (server.vm_enabled) {
996cb5f7 3053 if (key->storage == REDIS_VM_MEMORY ||
3054 key->storage == REDIS_VM_SWAPPING)
3055 {
3056 /* If we were swapping the object out, stop it, this key
3057 * was requested. */
3058 if (key->storage == REDIS_VM_SWAPPING)
3059 vmCancelThreadedIOJob(key);
55cf8433 3060 /* Update the access time of the key for the aging algorithm. */
3061 key->vm.atime = server.unixtime;
3062 } else {
d5d55fc3 3063 int notify = (key->storage == REDIS_VM_LOADING);
3064
55cf8433 3065 /* Our value was swapped on disk. Bring it at home. */
f2b8ab34 3066 redisAssert(val == NULL);
55cf8433 3067 val = vmLoadObject(key);
3068 dictGetEntryVal(de) = val;
d5d55fc3 3069
3070 /* Clients blocked by the VM subsystem may be waiting for
3071 * this key... */
3072 if (notify) handleClientsBlockedOnSwappedKey(db,key);
55cf8433 3073 }
3074 }
3075 return val;
3a66edc7 3076 } else {
3077 return NULL;
3078 }
942a3961 3079}
3080
3081static robj *lookupKeyRead(redisDb *db, robj *key) {
3082 expireIfNeeded(db,key);
3083 return lookupKey(db,key);
3084}
3085
3086static robj *lookupKeyWrite(redisDb *db, robj *key) {
3087 deleteIfVolatile(db,key);
3088 return lookupKey(db,key);
3089}
3090
92b27fe9 3091static robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
3092 robj *o = lookupKeyRead(c->db, key);
3093 if (!o) addReply(c,reply);
3094 return o;
3095}
3096
3097static robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
3098 robj *o = lookupKeyWrite(c->db, key);
3099 if (!o) addReply(c,reply);
3100 return o;
3101}
3102
3103static int checkType(redisClient *c, robj *o, int type) {
3104 if (o->type != type) {
3105 addReply(c,shared.wrongtypeerr);
3106 return 1;
3107 }
3108 return 0;
3109}
3110
942a3961 3111static int deleteKey(redisDb *db, robj *key) {
3112 int retval;
3113
3114 /* We need to protect key from destruction: after the first dictDelete()
3115 * it may happen that 'key' is no longer valid if we don't increment
3116 * it's count. This may happen when we get the object reference directly
3117 * from the hash table with dictRandomKey() or dict iterators */
3118 incrRefCount(key);
3119 if (dictSize(db->expires)) dictDelete(db->expires,key);
3120 retval = dictDelete(db->dict,key);
3121 decrRefCount(key);
3122
3123 return retval == DICT_OK;
3124}
3125
724a51b1 3126/* Check if the nul-terminated string 's' can be represented by a long
3127 * (that is, is a number that fits into long without any other space or
3128 * character before or after the digits).
3129 *
3130 * If so, the function returns REDIS_OK and *longval is set to the value
3131 * of the number. Otherwise REDIS_ERR is returned */
f69f2cba 3132static int isStringRepresentableAsLong(sds s, long *longval) {
724a51b1 3133 char buf[32], *endptr;
3134 long value;
3135 int slen;
e0a62c7f 3136
724a51b1 3137 value = strtol(s, &endptr, 10);
3138 if (endptr[0] != '\0') return REDIS_ERR;
ee14da56 3139 slen = ll2string(buf,32,value);
724a51b1 3140
3141 /* If the number converted back into a string is not identical
3142 * then it's not possible to encode the string as integer */
f69f2cba 3143 if (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR;
724a51b1 3144 if (longval) *longval = value;
3145 return REDIS_OK;
3146}
3147
942a3961 3148/* Try to encode a string object in order to save space */
05df7621 3149static robj *tryObjectEncoding(robj *o) {
942a3961 3150 long value;
942a3961 3151 sds s = o->ptr;
3305306f 3152
942a3961 3153 if (o->encoding != REDIS_ENCODING_RAW)
05df7621 3154 return o; /* Already encoded */
3305306f 3155
05df7621 3156 /* It's not safe to encode shared objects: shared objects can be shared
942a3961 3157 * everywhere in the "object space" of Redis. Encoded objects can only
3158 * appear as "values" (and not, for instance, as keys) */
05df7621 3159 if (o->refcount > 1) return o;
3305306f 3160
942a3961 3161 /* Currently we try to encode only strings */
dfc5e96c 3162 redisAssert(o->type == REDIS_STRING);
94754ccc 3163
724a51b1 3164 /* Check if we can represent this string as a long integer */
05df7621 3165 if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return o;
942a3961 3166
3167 /* Ok, this object can be encoded */
05df7621 3168 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
3169 decrRefCount(o);
3170 incrRefCount(shared.integers[value]);
3171 return shared.integers[value];
3172 } else {
3173 o->encoding = REDIS_ENCODING_INT;
3174 sdsfree(o->ptr);
3175 o->ptr = (void*) value;
3176 return o;
3177 }
942a3961 3178}
3179
9d65a1bb 3180/* Get a decoded version of an encoded object (returned as a new object).
3181 * If the object is already raw-encoded just increment the ref count. */
3182static robj *getDecodedObject(robj *o) {
942a3961 3183 robj *dec;
e0a62c7f 3184
9d65a1bb 3185 if (o->encoding == REDIS_ENCODING_RAW) {
3186 incrRefCount(o);
3187 return o;
3188 }
942a3961 3189 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_INT) {
3190 char buf[32];
3191
ee14da56 3192 ll2string(buf,32,(long)o->ptr);
942a3961 3193 dec = createStringObject(buf,strlen(buf));
3194 return dec;
3195 } else {
08ee9b57 3196 redisPanic("Unknown encoding type");
942a3961 3197 }
3305306f 3198}
3199
d7f43c08 3200/* Compare two string objects via strcmp() or alike.
3201 * Note that the objects may be integer-encoded. In such a case we
ee14da56 3202 * use ll2string() to get a string representation of the numbers on the stack
1fd9bc8a 3203 * and compare the strings, it's much faster than calling getDecodedObject().
3204 *
3205 * Important note: if objects are not integer encoded, but binary-safe strings,
3206 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
3207 * binary safe. */
724a51b1 3208static int compareStringObjects(robj *a, robj *b) {
dfc5e96c 3209 redisAssert(a->type == REDIS_STRING && b->type == REDIS_STRING);
d7f43c08 3210 char bufa[128], bufb[128], *astr, *bstr;
3211 int bothsds = 1;
724a51b1 3212
e197b441 3213 if (a == b) return 0;
d7f43c08 3214 if (a->encoding != REDIS_ENCODING_RAW) {
ee14da56 3215 ll2string(bufa,sizeof(bufa),(long) a->ptr);
d7f43c08 3216 astr = bufa;
3217 bothsds = 0;
724a51b1 3218 } else {
d7f43c08 3219 astr = a->ptr;
724a51b1 3220 }
d7f43c08 3221 if (b->encoding != REDIS_ENCODING_RAW) {
ee14da56 3222 ll2string(bufb,sizeof(bufb),(long) b->ptr);
d7f43c08 3223 bstr = bufb;
3224 bothsds = 0;
3225 } else {
3226 bstr = b->ptr;
3227 }
3228 return bothsds ? sdscmp(astr,bstr) : strcmp(astr,bstr);
724a51b1 3229}
3230
bf028098 3231/* Equal string objects return 1 if the two objects are the same from the
3232 * point of view of a string comparison, otherwise 0 is returned. Note that
3233 * this function is faster then checking for (compareStringObject(a,b) == 0)
3234 * because it can perform some more optimization. */
3235static int equalStringObjects(robj *a, robj *b) {
3236 if (a->encoding != REDIS_ENCODING_RAW && b->encoding != REDIS_ENCODING_RAW){
3237 return a->ptr == b->ptr;
3238 } else {
3239 return compareStringObjects(a,b) == 0;
3240 }
3241}
3242
0ea663ea 3243static size_t stringObjectLen(robj *o) {
dfc5e96c 3244 redisAssert(o->type == REDIS_STRING);
0ea663ea 3245 if (o->encoding == REDIS_ENCODING_RAW) {
3246 return sdslen(o->ptr);
3247 } else {
3248 char buf[32];
3249
ee14da56 3250 return ll2string(buf,32,(long)o->ptr);
0ea663ea 3251 }
3252}
3253
bd79a6bd
PN
3254static int getDoubleFromObject(robj *o, double *target) {
3255 double value;
682c73e8 3256 char *eptr;
bbe025e0 3257
bd79a6bd
PN
3258 if (o == NULL) {
3259 value = 0;
3260 } else {
3261 redisAssert(o->type == REDIS_STRING);
3262 if (o->encoding == REDIS_ENCODING_RAW) {
3263 value = strtod(o->ptr, &eptr);
682c73e8 3264 if (eptr[0] != '\0') return REDIS_ERR;
bd79a6bd
PN
3265 } else if (o->encoding == REDIS_ENCODING_INT) {
3266 value = (long)o->ptr;
3267 } else {
946342c1 3268 redisPanic("Unknown string encoding");
bd79a6bd
PN
3269 }
3270 }
3271
bd79a6bd
PN
3272 *target = value;
3273 return REDIS_OK;
3274}
bbe025e0 3275
bd79a6bd
PN
3276static int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg) {
3277 double value;
3278 if (getDoubleFromObject(o, &value) != REDIS_OK) {
3279 if (msg != NULL) {
3280 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3281 } else {
3282 addReplySds(c, sdsnew("-ERR value is not a double\r\n"));
3283 }
bbe025e0
AM
3284 return REDIS_ERR;
3285 }
3286
bd79a6bd 3287 *target = value;
bbe025e0
AM
3288 return REDIS_OK;
3289}
3290
bd79a6bd
PN
3291static int getLongLongFromObject(robj *o, long long *target) {
3292 long long value;
682c73e8 3293 char *eptr;
bbe025e0 3294
bd79a6bd
PN
3295 if (o == NULL) {
3296 value = 0;
3297 } else {
3298 redisAssert(o->type == REDIS_STRING);
3299 if (o->encoding == REDIS_ENCODING_RAW) {
3300 value = strtoll(o->ptr, &eptr, 10);
682c73e8 3301 if (eptr[0] != '\0') return REDIS_ERR;
bd79a6bd
PN
3302 } else if (o->encoding == REDIS_ENCODING_INT) {
3303 value = (long)o->ptr;
3304 } else {
946342c1 3305 redisPanic("Unknown string encoding");
bd79a6bd
PN
3306 }
3307 }
3308
bd79a6bd
PN
3309 *target = value;
3310 return REDIS_OK;
3311}
bbe025e0 3312
bd79a6bd
PN
3313static int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg) {
3314 long long value;
3315 if (getLongLongFromObject(o, &value) != REDIS_OK) {
3316 if (msg != NULL) {
3317 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3318 } else {
3319 addReplySds(c, sdsnew("-ERR value is not an integer\r\n"));
3320 }
bbe025e0
AM
3321 return REDIS_ERR;
3322 }
3323
bd79a6bd 3324 *target = value;
bbe025e0
AM
3325 return REDIS_OK;
3326}
3327
bd79a6bd
PN
3328static int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg) {
3329 long long value;
bbe025e0 3330
bd79a6bd
PN
3331 if (getLongLongFromObjectOrReply(c, o, &value, msg) != REDIS_OK) return REDIS_ERR;
3332 if (value < LONG_MIN || value > LONG_MAX) {
3333 if (msg != NULL) {
3334 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3335 } else {
3336 addReplySds(c, sdsnew("-ERR value is out of range\r\n"));
3337 }
bbe025e0
AM
3338 return REDIS_ERR;
3339 }
3340
bd79a6bd 3341 *target = value;
bbe025e0
AM
3342 return REDIS_OK;
3343}
3344
06233c45 3345/*============================ RDB saving/loading =========================== */
ed9b544e 3346
f78fd11b 3347static int rdbSaveType(FILE *fp, unsigned char type) {
3348 if (fwrite(&type,1,1,fp) == 0) return -1;
3349 return 0;
3350}
3351
bb32ede5 3352static int rdbSaveTime(FILE *fp, time_t t) {
3353 int32_t t32 = (int32_t) t;
3354 if (fwrite(&t32,4,1,fp) == 0) return -1;
3355 return 0;
3356}
3357
e3566d4b 3358/* check rdbLoadLen() comments for more info */
f78fd11b 3359static int rdbSaveLen(FILE *fp, uint32_t len) {
3360 unsigned char buf[2];
3361
3362 if (len < (1<<6)) {
3363 /* Save a 6 bit len */
10c43610 3364 buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
f78fd11b 3365 if (fwrite(buf,1,1,fp) == 0) return -1;
3366 } else if (len < (1<<14)) {
3367 /* Save a 14 bit len */
10c43610 3368 buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
f78fd11b 3369 buf[1] = len&0xFF;
17be1a4a 3370 if (fwrite(buf,2,1,fp) == 0) return -1;
f78fd11b 3371 } else {
3372 /* Save a 32 bit len */
10c43610 3373 buf[0] = (REDIS_RDB_32BITLEN<<6);
f78fd11b 3374 if (fwrite(buf,1,1,fp) == 0) return -1;
3375 len = htonl(len);
3376 if (fwrite(&len,4,1,fp) == 0) return -1;
3377 }
3378 return 0;
3379}
3380
32a66513 3381/* Encode 'value' as an integer if possible (if integer will fit the
3382 * supported range). If the function sucessful encoded the integer
3383 * then the (up to 5 bytes) encoded representation is written in the
3384 * string pointed by 'enc' and the length is returned. Otherwise
3385 * 0 is returned. */
3386static int rdbEncodeInteger(long long value, unsigned char *enc) {
e3566d4b 3387 /* Finally check if it fits in our ranges */
3388 if (value >= -(1<<7) && value <= (1<<7)-1) {
3389 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
3390 enc[1] = value&0xFF;
3391 return 2;
3392 } else if (value >= -(1<<15) && value <= (1<<15)-1) {
3393 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
3394 enc[1] = value&0xFF;
3395 enc[2] = (value>>8)&0xFF;
3396 return 3;
3397 } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
3398 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
3399 enc[1] = value&0xFF;
3400 enc[2] = (value>>8)&0xFF;
3401 enc[3] = (value>>16)&0xFF;
3402 enc[4] = (value>>24)&0xFF;
3403 return 5;
3404 } else {
3405 return 0;
3406 }
3407}
3408
32a66513 3409/* String objects in the form "2391" "-100" without any space and with a
3410 * range of values that can fit in an 8, 16 or 32 bit signed value can be
3411 * encoded as integers to save space */
3412static int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {
3413 long long value;
3414 char *endptr, buf[32];
3415
3416 /* Check if it's possible to encode this value as a number */
3417 value = strtoll(s, &endptr, 10);
3418 if (endptr[0] != '\0') return 0;
3419 ll2string(buf,32,value);
3420
3421 /* If the number converted back into a string is not identical
3422 * then it's not possible to encode the string as integer */
3423 if (strlen(buf) != len || memcmp(buf,s,len)) return 0;
3424
3425 return rdbEncodeInteger(value,enc);
3426}
3427
b1befe6a 3428static int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) {
3429 size_t comprlen, outlen;
774e3047 3430 unsigned char byte;
3431 void *out;
3432
3433 /* We require at least four bytes compression for this to be worth it */
b1befe6a 3434 if (len <= 4) return 0;
3435 outlen = len-4;
3a2694c4 3436 if ((out = zmalloc(outlen+1)) == NULL) return 0;
b1befe6a 3437 comprlen = lzf_compress(s, len, out, outlen);
774e3047 3438 if (comprlen == 0) {
88e85998 3439 zfree(out);
774e3047 3440 return 0;
3441 }
3442 /* Data compressed! Let's save it on disk */
3443 byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
3444 if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
3445 if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
b1befe6a 3446 if (rdbSaveLen(fp,len) == -1) goto writeerr;
774e3047 3447 if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
88e85998 3448 zfree(out);
774e3047 3449 return comprlen;
3450
3451writeerr:
88e85998 3452 zfree(out);
774e3047 3453 return -1;
3454}
3455
e3566d4b 3456/* Save a string objet as [len][data] on disk. If the object is a string
3457 * representation of an integer value we try to safe it in a special form */
b1befe6a 3458static int rdbSaveRawString(FILE *fp, unsigned char *s, size_t len) {
e3566d4b 3459 int enclen;
10c43610 3460
774e3047 3461 /* Try integer encoding */
e3566d4b 3462 if (len <= 11) {
3463 unsigned char buf[5];
b1befe6a 3464 if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) {
e3566d4b 3465 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3466 return 0;
3467 }
3468 }
774e3047 3469
3470 /* Try LZF compression - under 20 bytes it's unable to compress even
88e85998 3471 * aaaaaaaaaaaaaaaaaa so skip it */
121f70cf 3472 if (server.rdbcompression && len > 20) {
774e3047 3473 int retval;
3474
b1befe6a 3475 retval = rdbSaveLzfStringObject(fp,s,len);
774e3047 3476 if (retval == -1) return -1;
3477 if (retval > 0) return 0;
3478 /* retval == 0 means data can't be compressed, save the old way */
3479 }
3480
3481 /* Store verbatim */
10c43610 3482 if (rdbSaveLen(fp,len) == -1) return -1;
b1befe6a 3483 if (len && fwrite(s,len,1,fp) == 0) return -1;
10c43610 3484 return 0;
3485}
3486
942a3961 3487/* Like rdbSaveStringObjectRaw() but handle encoded objects */
3488static int rdbSaveStringObject(FILE *fp, robj *obj) {
3489 int retval;
942a3961 3490
32a66513 3491 /* Avoid to decode the object, then encode it again, if the
3492 * object is alrady integer encoded. */
3493 if (obj->encoding == REDIS_ENCODING_INT) {
3494 long val = (long) obj->ptr;
3495 unsigned char buf[5];
3496 int enclen;
3497
3498 if ((enclen = rdbEncodeInteger(val,buf)) > 0) {
3499 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3500 return 0;
3501 }
3502 /* otherwise... fall throught and continue with the usual
3503 * code path. */
3504 }
3505
f2d9f50f 3506 /* Avoid incr/decr ref count business when possible.
3507 * This plays well with copy-on-write given that we are probably
3508 * in a child process (BGSAVE). Also this makes sure key objects
3509 * of swapped objects are not incRefCount-ed (an assert does not allow
3510 * this in order to avoid bugs) */
3511 if (obj->encoding != REDIS_ENCODING_RAW) {
996cb5f7 3512 obj = getDecodedObject(obj);
b1befe6a 3513 retval = rdbSaveRawString(fp,obj->ptr,sdslen(obj->ptr));
996cb5f7 3514 decrRefCount(obj);
3515 } else {
b1befe6a 3516 retval = rdbSaveRawString(fp,obj->ptr,sdslen(obj->ptr));
996cb5f7 3517 }
9d65a1bb 3518 return retval;
942a3961 3519}
3520
a7866db6 3521/* Save a double value. Doubles are saved as strings prefixed by an unsigned
3522 * 8 bit integer specifing the length of the representation.
3523 * This 8 bit integer has special values in order to specify the following
3524 * conditions:
3525 * 253: not a number
3526 * 254: + inf
3527 * 255: - inf
3528 */
3529static int rdbSaveDoubleValue(FILE *fp, double val) {
3530 unsigned char buf[128];
3531 int len;
3532
3533 if (isnan(val)) {
3534 buf[0] = 253;
3535 len = 1;
3536 } else if (!isfinite(val)) {
3537 len = 1;
3538 buf[0] = (val < 0) ? 255 : 254;
3539 } else {
88e8d89f 3540#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
fe244589 3541 /* Check if the float is in a safe range to be casted into a
3542 * long long. We are assuming that long long is 64 bit here.
3543 * Also we are assuming that there are no implementations around where
3544 * double has precision < 52 bit.
3545 *
3546 * Under this assumptions we test if a double is inside an interval
3547 * where casting to long long is safe. Then using two castings we
3548 * make sure the decimal part is zero. If all this is true we use
3549 * integer printing function that is much faster. */
fb82e75c 3550 double min = -4503599627370495; /* (2^52)-1 */
3551 double max = 4503599627370496; /* -(2^52) */
fe244589 3552 if (val > min && val < max && val == ((double)((long long)val)))
8c096b16 3553 ll2string((char*)buf+1,sizeof(buf),(long long)val);
3554 else
88e8d89f 3555#endif
8c096b16 3556 snprintf((char*)buf+1,sizeof(buf)-1,"%.17g",val);
6c446631 3557 buf[0] = strlen((char*)buf+1);
a7866db6 3558 len = buf[0]+1;
3559 }
3560 if (fwrite(buf,len,1,fp) == 0) return -1;
3561 return 0;
3562}
3563
06233c45 3564/* Save a Redis object. */
3565static int rdbSaveObject(FILE *fp, robj *o) {
3566 if (o->type == REDIS_STRING) {
3567 /* Save a string value */
3568 if (rdbSaveStringObject(fp,o) == -1) return -1;
3569 } else if (o->type == REDIS_LIST) {
3570 /* Save a list value */
3571 list *list = o->ptr;
c7df85a4 3572 listIter li;
06233c45 3573 listNode *ln;
3574
06233c45 3575 if (rdbSaveLen(fp,listLength(list)) == -1) return -1;
c7df85a4 3576 listRewind(list,&li);
3577 while((ln = listNext(&li))) {
06233c45 3578 robj *eleobj = listNodeValue(ln);
3579
3580 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3581 }
3582 } else if (o->type == REDIS_SET) {
3583 /* Save a set value */
3584 dict *set = o->ptr;
3585 dictIterator *di = dictGetIterator(set);
3586 dictEntry *de;
3587
3588 if (rdbSaveLen(fp,dictSize(set)) == -1) return -1;
3589 while((de = dictNext(di)) != NULL) {
3590 robj *eleobj = dictGetEntryKey(de);
3591
3592 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3593 }
3594 dictReleaseIterator(di);
3595 } else if (o->type == REDIS_ZSET) {
3596 /* Save a set value */
3597 zset *zs = o->ptr;
3598 dictIterator *di = dictGetIterator(zs->dict);
3599 dictEntry *de;
3600
3601 if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) return -1;
3602 while((de = dictNext(di)) != NULL) {
3603 robj *eleobj = dictGetEntryKey(de);
3604 double *score = dictGetEntryVal(de);
3605
3606 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3607 if (rdbSaveDoubleValue(fp,*score) == -1) return -1;
3608 }
3609 dictReleaseIterator(di);
b1befe6a 3610 } else if (o->type == REDIS_HASH) {
3611 /* Save a hash value */
3612 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
3613 unsigned char *p = zipmapRewind(o->ptr);
3614 unsigned int count = zipmapLen(o->ptr);
3615 unsigned char *key, *val;
3616 unsigned int klen, vlen;
3617
3618 if (rdbSaveLen(fp,count) == -1) return -1;
3619 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
3620 if (rdbSaveRawString(fp,key,klen) == -1) return -1;
3621 if (rdbSaveRawString(fp,val,vlen) == -1) return -1;
3622 }
3623 } else {
3624 dictIterator *di = dictGetIterator(o->ptr);
3625 dictEntry *de;
3626
3627 if (rdbSaveLen(fp,dictSize((dict*)o->ptr)) == -1) return -1;
3628 while((de = dictNext(di)) != NULL) {
3629 robj *key = dictGetEntryKey(de);
3630 robj *val = dictGetEntryVal(de);
3631
3632 if (rdbSaveStringObject(fp,key) == -1) return -1;
3633 if (rdbSaveStringObject(fp,val) == -1) return -1;
3634 }
3635 dictReleaseIterator(di);
3636 }
06233c45 3637 } else {
f83c6cb5 3638 redisPanic("Unknown object type");
06233c45 3639 }
3640 return 0;
3641}
3642
3643/* Return the length the object will have on disk if saved with
3644 * the rdbSaveObject() function. Currently we use a trick to get
3645 * this length with very little changes to the code. In the future
3646 * we could switch to a faster solution. */
b9bc0eef 3647static off_t rdbSavedObjectLen(robj *o, FILE *fp) {
3648 if (fp == NULL) fp = server.devnull;
06233c45 3649 rewind(fp);
3650 assert(rdbSaveObject(fp,o) != 1);
3651 return ftello(fp);
3652}
3653
06224fec 3654/* Return the number of pages required to save this object in the swap file */
b9bc0eef 3655static off_t rdbSavedObjectPages(robj *o, FILE *fp) {
3656 off_t bytes = rdbSavedObjectLen(o,fp);
e0a62c7f 3657
06224fec 3658 return (bytes+(server.vm_page_size-1))/server.vm_page_size;
3659}
3660
ed9b544e 3661/* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
f78fd11b 3662static int rdbSave(char *filename) {
ed9b544e 3663 dictIterator *di = NULL;
3664 dictEntry *de;
ed9b544e 3665 FILE *fp;
3666 char tmpfile[256];
3667 int j;
bb32ede5 3668 time_t now = time(NULL);
ed9b544e 3669
2316bb3b 3670 /* Wait for I/O therads to terminate, just in case this is a
3671 * foreground-saving, to avoid seeking the swap file descriptor at the
3672 * same time. */
3673 if (server.vm_enabled)
3674 waitEmptyIOJobsQueue();
3675
a3b21203 3676 snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
ed9b544e 3677 fp = fopen(tmpfile,"w");
3678 if (!fp) {
3679 redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno));
3680 return REDIS_ERR;
3681 }
f78fd11b 3682 if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
ed9b544e 3683 for (j = 0; j < server.dbnum; j++) {
bb32ede5 3684 redisDb *db = server.db+j;
3685 dict *d = db->dict;
3305306f 3686 if (dictSize(d) == 0) continue;
ed9b544e 3687 di = dictGetIterator(d);
3688 if (!di) {
3689 fclose(fp);
3690 return REDIS_ERR;
3691 }
3692
3693 /* Write the SELECT DB opcode */
f78fd11b 3694 if (rdbSaveType(fp,REDIS_SELECTDB) == -1) goto werr;
3695 if (rdbSaveLen(fp,j) == -1) goto werr;
ed9b544e 3696
3697 /* Iterate this DB writing every entry */
3698 while((de = dictNext(di)) != NULL) {
3699 robj *key = dictGetEntryKey(de);
3700 robj *o = dictGetEntryVal(de);
bb32ede5 3701 time_t expiretime = getExpire(db,key);
3702
3703 /* Save the expire time */
3704 if (expiretime != -1) {
3705 /* If this key is already expired skip it */
3706 if (expiretime < now) continue;
3707 if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr;
3708 if (rdbSaveTime(fp,expiretime) == -1) goto werr;
3709 }
7e69548d 3710 /* Save the key and associated value. This requires special
3711 * handling if the value is swapped out. */
996cb5f7 3712 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
3713 key->storage == REDIS_VM_SWAPPING) {
7e69548d 3714 /* Save type, key, value */
3715 if (rdbSaveType(fp,o->type) == -1) goto werr;
3716 if (rdbSaveStringObject(fp,key) == -1) goto werr;
3717 if (rdbSaveObject(fp,o) == -1) goto werr;
3718 } else {
996cb5f7 3719 /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
b9bc0eef 3720 robj *po;
7e69548d 3721 /* Get a preview of the object in memory */
3722 po = vmPreviewObject(key);
7e69548d 3723 /* Save type, key, value */
3724 if (rdbSaveType(fp,key->vtype) == -1) goto werr;
b9bc0eef 3725 if (rdbSaveStringObject(fp,key) == -1) goto werr;
7e69548d 3726 if (rdbSaveObject(fp,po) == -1) goto werr;
3727 /* Remove the loaded object from memory */
3728 decrRefCount(po);
7e69548d 3729 }
ed9b544e 3730 }
3731 dictReleaseIterator(di);
3732 }
3733 /* EOF opcode */
f78fd11b 3734 if (rdbSaveType(fp,REDIS_EOF) == -1) goto werr;
3735
3736 /* Make sure data will not remain on the OS's output buffers */
ed9b544e 3737 fflush(fp);
3738 fsync(fileno(fp));
3739 fclose(fp);
e0a62c7f 3740
ed9b544e 3741 /* Use RENAME to make sure the DB file is changed atomically only
3742 * if the generate DB file is ok. */
3743 if (rename(tmpfile,filename) == -1) {
325d1eb4 3744 redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
ed9b544e 3745 unlink(tmpfile);
3746 return REDIS_ERR;
3747 }
3748 redisLog(REDIS_NOTICE,"DB saved on disk");
3749 server.dirty = 0;
3750 server.lastsave = time(NULL);
3751 return REDIS_OK;
3752
3753werr:
3754 fclose(fp);
3755 unlink(tmpfile);
3756 redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
3757 if (di) dictReleaseIterator(di);
3758 return REDIS_ERR;
3759}
3760
f78fd11b 3761static int rdbSaveBackground(char *filename) {
ed9b544e 3762 pid_t childpid;
3763
9d65a1bb 3764 if (server.bgsavechildpid != -1) return REDIS_ERR;
054e426d 3765 if (server.vm_enabled) waitEmptyIOJobsQueue();
ed9b544e 3766 if ((childpid = fork()) == 0) {
3767 /* Child */
054e426d 3768 if (server.vm_enabled) vmReopenSwapFile();
ed9b544e 3769 close(server.fd);
f78fd11b 3770 if (rdbSave(filename) == REDIS_OK) {
478c2c6f 3771 _exit(0);
ed9b544e 3772 } else {
478c2c6f 3773 _exit(1);
ed9b544e 3774 }
3775 } else {
3776 /* Parent */
5a7c647e 3777 if (childpid == -1) {
3778 redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
3779 strerror(errno));
3780 return REDIS_ERR;
3781 }
ed9b544e 3782 redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);
9f3c422c 3783 server.bgsavechildpid = childpid;
884d4b39 3784 updateDictResizePolicy();
ed9b544e 3785 return REDIS_OK;
3786 }
3787 return REDIS_OK; /* unreached */
3788}
3789
a3b21203 3790static void rdbRemoveTempFile(pid_t childpid) {
3791 char tmpfile[256];
3792
3793 snprintf(tmpfile,256,"temp-%d.rdb", (int) childpid);
3794 unlink(tmpfile);
3795}
3796
f78fd11b 3797static int rdbLoadType(FILE *fp) {
3798 unsigned char type;
7b45bfb2 3799 if (fread(&type,1,1,fp) == 0) return -1;
3800 return type;
3801}
3802
bb32ede5 3803static time_t rdbLoadTime(FILE *fp) {
3804 int32_t t32;
3805 if (fread(&t32,4,1,fp) == 0) return -1;
3806 return (time_t) t32;
3807}
3808
e3566d4b 3809/* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
3810 * of this file for a description of how this are stored on disk.
3811 *
3812 * isencoded is set to 1 if the readed length is not actually a length but
3813 * an "encoding type", check the above comments for more info */
c78a8ccc 3814static uint32_t rdbLoadLen(FILE *fp, int *isencoded) {
f78fd11b 3815 unsigned char buf[2];
3816 uint32_t len;
c78a8ccc 3817 int type;
f78fd11b 3818
e3566d4b 3819 if (isencoded) *isencoded = 0;
c78a8ccc 3820 if (fread(buf,1,1,fp) == 0) return REDIS_RDB_LENERR;
3821 type = (buf[0]&0xC0)>>6;
3822 if (type == REDIS_RDB_6BITLEN) {
3823 /* Read a 6 bit len */
3824 return buf[0]&0x3F;
3825 } else if (type == REDIS_RDB_ENCVAL) {
3826 /* Read a 6 bit len encoding type */
3827 if (isencoded) *isencoded = 1;
3828 return buf[0]&0x3F;
3829 } else if (type == REDIS_RDB_14BITLEN) {
3830 /* Read a 14 bit len */
3831 if (fread(buf+1,1,1,fp) == 0) return REDIS_RDB_LENERR;
3832 return ((buf[0]&0x3F)<<8)|buf[1];
3833 } else {
3834 /* Read a 32 bit len */
f78fd11b 3835 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
3836 return ntohl(len);
f78fd11b 3837 }
f78fd11b 3838}
3839
ad30aa60 3840/* Load an integer-encoded object from file 'fp', with the specified
3841 * encoding type 'enctype'. If encode is true the function may return
3842 * an integer-encoded object as reply, otherwise the returned object
3843 * will always be encoded as a raw string. */
3844static robj *rdbLoadIntegerObject(FILE *fp, int enctype, int encode) {
e3566d4b 3845 unsigned char enc[4];
3846 long long val;
3847
3848 if (enctype == REDIS_RDB_ENC_INT8) {
3849 if (fread(enc,1,1,fp) == 0) return NULL;
3850 val = (signed char)enc[0];
3851 } else if (enctype == REDIS_RDB_ENC_INT16) {
3852 uint16_t v;
3853 if (fread(enc,2,1,fp) == 0) return NULL;
3854 v = enc[0]|(enc[1]<<8);
3855 val = (int16_t)v;
3856 } else if (enctype == REDIS_RDB_ENC_INT32) {
3857 uint32_t v;
3858 if (fread(enc,4,1,fp) == 0) return NULL;
3859 v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
3860 val = (int32_t)v;
3861 } else {
3862 val = 0; /* anti-warning */
f83c6cb5 3863 redisPanic("Unknown RDB integer encoding type");
e3566d4b 3864 }
ad30aa60 3865 if (encode)
3866 return createStringObjectFromLongLong(val);
3867 else
3868 return createObject(REDIS_STRING,sdsfromlonglong(val));
e3566d4b 3869}
3870
c78a8ccc 3871static robj *rdbLoadLzfStringObject(FILE*fp) {
88e85998 3872 unsigned int len, clen;
3873 unsigned char *c = NULL;
3874 sds val = NULL;
3875
c78a8ccc 3876 if ((clen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3877 if ((len = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
88e85998 3878 if ((c = zmalloc(clen)) == NULL) goto err;
3879 if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
3880 if (fread(c,clen,1,fp) == 0) goto err;
3881 if (lzf_decompress(c,clen,val,len) == 0) goto err;
5109cdff 3882 zfree(c);
88e85998 3883 return createObject(REDIS_STRING,val);
3884err:
3885 zfree(c);
3886 sdsfree(val);
3887 return NULL;
3888}
3889
ad30aa60 3890static robj *rdbGenericLoadStringObject(FILE*fp, int encode) {
e3566d4b 3891 int isencoded;
3892 uint32_t len;
f78fd11b 3893 sds val;
3894
c78a8ccc 3895 len = rdbLoadLen(fp,&isencoded);
e3566d4b 3896 if (isencoded) {
3897 switch(len) {
3898 case REDIS_RDB_ENC_INT8:
3899 case REDIS_RDB_ENC_INT16:
3900 case REDIS_RDB_ENC_INT32:
ad30aa60 3901 return rdbLoadIntegerObject(fp,len,encode);
88e85998 3902 case REDIS_RDB_ENC_LZF:
bdcb92f2 3903 return rdbLoadLzfStringObject(fp);
e3566d4b 3904 default:
f83c6cb5 3905 redisPanic("Unknown RDB encoding type");
e3566d4b 3906 }
3907 }
3908
f78fd11b 3909 if (len == REDIS_RDB_LENERR) return NULL;
3910 val = sdsnewlen(NULL,len);
3911 if (len && fread(val,len,1,fp) == 0) {
3912 sdsfree(val);
3913 return NULL;
3914 }
bdcb92f2 3915 return createObject(REDIS_STRING,val);
f78fd11b 3916}
3917
ad30aa60 3918static robj *rdbLoadStringObject(FILE *fp) {
3919 return rdbGenericLoadStringObject(fp,0);
3920}
3921
3922static robj *rdbLoadEncodedStringObject(FILE *fp) {
3923 return rdbGenericLoadStringObject(fp,1);
3924}
3925
a7866db6 3926/* For information about double serialization check rdbSaveDoubleValue() */
3927static int rdbLoadDoubleValue(FILE *fp, double *val) {
3928 char buf[128];
3929 unsigned char len;
3930
3931 if (fread(&len,1,1,fp) == 0) return -1;
3932 switch(len) {
3933 case 255: *val = R_NegInf; return 0;
3934 case 254: *val = R_PosInf; return 0;
3935 case 253: *val = R_Nan; return 0;
3936 default:
3937 if (fread(buf,len,1,fp) == 0) return -1;
231d758e 3938 buf[len] = '\0';
a7866db6 3939 sscanf(buf, "%lg", val);
3940 return 0;
3941 }
3942}
3943
c78a8ccc 3944/* Load a Redis object of the specified type from the specified file.
3945 * On success a newly allocated object is returned, otherwise NULL. */
3946static robj *rdbLoadObject(int type, FILE *fp) {
3947 robj *o;
3948
bcd11906 3949 redisLog(REDIS_DEBUG,"LOADING OBJECT %d (at %d)\n",type,ftell(fp));
c78a8ccc 3950 if (type == REDIS_STRING) {
3951 /* Read string value */
ad30aa60 3952 if ((o = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
05df7621 3953 o = tryObjectEncoding(o);
c78a8ccc 3954 } else if (type == REDIS_LIST || type == REDIS_SET) {
3955 /* Read list/set value */
3956 uint32_t listlen;
3957
3958 if ((listlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3959 o = (type == REDIS_LIST) ? createListObject() : createSetObject();
3c68de9b 3960 /* It's faster to expand the dict to the right size asap in order
3961 * to avoid rehashing */
3962 if (type == REDIS_SET && listlen > DICT_HT_INITIAL_SIZE)
3963 dictExpand(o->ptr,listlen);
c78a8ccc 3964 /* Load every single element of the list/set */
3965 while(listlen--) {
3966 robj *ele;
3967
ad30aa60 3968 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
05df7621 3969 ele = tryObjectEncoding(ele);
c78a8ccc 3970 if (type == REDIS_LIST) {
3971 listAddNodeTail((list*)o->ptr,ele);
3972 } else {
3973 dictAdd((dict*)o->ptr,ele,NULL);
3974 }
3975 }
3976 } else if (type == REDIS_ZSET) {
3977 /* Read list/set value */
ada386b2 3978 size_t zsetlen;
c78a8ccc 3979 zset *zs;
3980
3981 if ((zsetlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3982 o = createZsetObject();
3983 zs = o->ptr;
3984 /* Load every single element of the list/set */
3985 while(zsetlen--) {
3986 robj *ele;
3987 double *score = zmalloc(sizeof(double));
3988
ad30aa60 3989 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
05df7621 3990 ele = tryObjectEncoding(ele);
c78a8ccc 3991 if (rdbLoadDoubleValue(fp,score) == -1) return NULL;
3992 dictAdd(zs->dict,ele,score);
3993 zslInsert(zs->zsl,*score,ele);
3994 incrRefCount(ele); /* added to skiplist */
3995 }
ada386b2 3996 } else if (type == REDIS_HASH) {
3997 size_t hashlen;
3998
3999 if ((hashlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4000 o = createHashObject();
4001 /* Too many entries? Use an hash table. */
4002 if (hashlen > server.hash_max_zipmap_entries)
4003 convertToRealHash(o);
4004 /* Load every key/value, then set it into the zipmap or hash
4005 * table, as needed. */
4006 while(hashlen--) {
4007 robj *key, *val;
4008
4009 if ((key = rdbLoadStringObject(fp)) == NULL) return NULL;
4010 if ((val = rdbLoadStringObject(fp)) == NULL) return NULL;
4011 /* If we are using a zipmap and there are too big values
4012 * the object is converted to real hash table encoding. */
4013 if (o->encoding != REDIS_ENCODING_HT &&
4014 (sdslen(key->ptr) > server.hash_max_zipmap_value ||
4015 sdslen(val->ptr) > server.hash_max_zipmap_value))
4016 {
4017 convertToRealHash(o);
4018 }
4019
4020 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
4021 unsigned char *zm = o->ptr;
4022
4023 zm = zipmapSet(zm,key->ptr,sdslen(key->ptr),
4024 val->ptr,sdslen(val->ptr),NULL);
4025 o->ptr = zm;
4026 decrRefCount(key);
4027 decrRefCount(val);
4028 } else {
05df7621 4029 key = tryObjectEncoding(key);
4030 val = tryObjectEncoding(val);
ada386b2 4031 dictAdd((dict*)o->ptr,key,val);
ada386b2 4032 }
4033 }
c78a8ccc 4034 } else {
f83c6cb5 4035 redisPanic("Unknown object type");
c78a8ccc 4036 }
4037 return o;
4038}
4039
f78fd11b 4040static int rdbLoad(char *filename) {
ed9b544e 4041 FILE *fp;
f78fd11b 4042 uint32_t dbid;
bb32ede5 4043 int type, retval, rdbver;
585af7e2 4044 int swap_all_values = 0;
3305306f 4045 dict *d = server.db[0].dict;
bb32ede5 4046 redisDb *db = server.db+0;
f78fd11b 4047 char buf[1024];
242a64f3 4048 time_t expiretime, now = time(NULL);
b492cf00 4049 long long loadedkeys = 0;
bb32ede5 4050
ed9b544e 4051 fp = fopen(filename,"r");
4052 if (!fp) return REDIS_ERR;
4053 if (fread(buf,9,1,fp) == 0) goto eoferr;
f78fd11b 4054 buf[9] = '\0';
4055 if (memcmp(buf,"REDIS",5) != 0) {
ed9b544e 4056 fclose(fp);
4057 redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
4058 return REDIS_ERR;
4059 }
f78fd11b 4060 rdbver = atoi(buf+5);
c78a8ccc 4061 if (rdbver != 1) {
f78fd11b 4062 fclose(fp);
4063 redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
4064 return REDIS_ERR;
4065 }
ed9b544e 4066 while(1) {
585af7e2 4067 robj *key, *val;
ed9b544e 4068
585af7e2 4069 expiretime = -1;
ed9b544e 4070 /* Read type. */
f78fd11b 4071 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
bb32ede5 4072 if (type == REDIS_EXPIRETIME) {
4073 if ((expiretime = rdbLoadTime(fp)) == -1) goto eoferr;
4074 /* We read the time so we need to read the object type again */
4075 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
4076 }
ed9b544e 4077 if (type == REDIS_EOF) break;
4078 /* Handle SELECT DB opcode as a special case */
4079 if (type == REDIS_SELECTDB) {
c78a8ccc 4080 if ((dbid = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR)
e3566d4b 4081 goto eoferr;
ed9b544e 4082 if (dbid >= (unsigned)server.dbnum) {
f78fd11b 4083 redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum);
ed9b544e 4084 exit(1);
4085 }
bb32ede5 4086 db = server.db+dbid;
4087 d = db->dict;
ed9b544e 4088 continue;
4089 }
4090 /* Read key */
585af7e2 4091 if ((key = rdbLoadStringObject(fp)) == NULL) goto eoferr;
c78a8ccc 4092 /* Read value */
585af7e2 4093 if ((val = rdbLoadObject(type,fp)) == NULL) goto eoferr;
89e689c5 4094 /* Check if the key already expired */
4095 if (expiretime != -1 && expiretime < now) {
4096 decrRefCount(key);
4097 decrRefCount(val);
4098 continue;
4099 }
ed9b544e 4100 /* Add the new object in the hash table */
585af7e2 4101 retval = dictAdd(d,key,val);
ed9b544e 4102 if (retval == DICT_ERR) {
585af7e2 4103 redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key->ptr);
ed9b544e 4104 exit(1);
4105 }
242a64f3 4106 loadedkeys++;
bb32ede5 4107 /* Set the expire time if needed */
89e689c5 4108 if (expiretime != -1) setExpire(db,key,expiretime);
242a64f3 4109
b492cf00 4110 /* Handle swapping while loading big datasets when VM is on */
242a64f3 4111
4112 /* If we detecter we are hopeless about fitting something in memory
4113 * we just swap every new key on disk. Directly...
4114 * Note that's important to check for this condition before resorting
4115 * to random sampling, otherwise we may try to swap already
4116 * swapped keys. */
585af7e2 4117 if (swap_all_values) {
4118 dictEntry *de = dictFind(d,key);
242a64f3 4119
4120 /* de may be NULL since the key already expired */
4121 if (de) {
585af7e2 4122 key = dictGetEntryKey(de);
4123 val = dictGetEntryVal(de);
242a64f3 4124
585af7e2 4125 if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
242a64f3 4126 dictGetEntryVal(de) = NULL;
4127 }
4128 }
4129 continue;
4130 }
4131
4132 /* If we have still some hope of having some value fitting memory
4133 * then we try random sampling. */
585af7e2 4134 if (!swap_all_values && server.vm_enabled && (loadedkeys % 5000) == 0) {
b492cf00 4135 while (zmalloc_used_memory() > server.vm_max_memory) {
a69a0c9c 4136 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
b492cf00 4137 }
242a64f3 4138 if (zmalloc_used_memory() > server.vm_max_memory)
585af7e2 4139 swap_all_values = 1; /* We are already using too much mem */
b492cf00 4140 }
ed9b544e 4141 }
4142 fclose(fp);
4143 return REDIS_OK;
4144
4145eoferr: /* unexpected end of file is handled here with a fatal exit */
f80dff62 4146 redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
ed9b544e 4147 exit(1);
4148 return REDIS_ERR; /* Just to avoid warning */
4149}
4150
4151/*================================== Commands =============================== */
4152
abcb223e 4153static void authCommand(redisClient *c) {
2e77c2ee 4154 if (!server.requirepass || !strcmp(c->argv[1]->ptr, server.requirepass)) {
abcb223e
BH
4155 c->authenticated = 1;
4156 addReply(c,shared.ok);
4157 } else {
4158 c->authenticated = 0;
fa4c0aba 4159 addReplySds(c,sdscatprintf(sdsempty(),"-ERR invalid password\r\n"));
abcb223e
BH
4160 }
4161}
4162
ed9b544e 4163static void pingCommand(redisClient *c) {
4164 addReply(c,shared.pong);
4165}
4166
4167static void echoCommand(redisClient *c) {
dd88747b 4168 addReplyBulk(c,c->argv[1]);
ed9b544e 4169}
4170
4171/*=================================== Strings =============================== */
4172
526d00a5 4173static void setGenericCommand(redisClient *c, int nx, robj *key, robj *val, robj *expire) {
ed9b544e 4174 int retval;
10ce1276 4175 long seconds = 0; /* initialized to avoid an harmness warning */
ed9b544e 4176
526d00a5 4177 if (expire) {
4178 if (getLongFromObjectOrReply(c, expire, &seconds, NULL) != REDIS_OK)
4179 return;
4180 if (seconds <= 0) {
4181 addReplySds(c,sdsnew("-ERR invalid expire time in SETEX\r\n"));
4182 return;
4183 }
4184 }
4185
4186 if (nx) deleteIfVolatile(c->db,key);
4187 retval = dictAdd(c->db->dict,key,val);
ed9b544e 4188 if (retval == DICT_ERR) {
4189 if (!nx) {
1b03836c 4190 /* If the key is about a swapped value, we want a new key object
4191 * to overwrite the old. So we delete the old key in the database.
4192 * This will also make sure that swap pages about the old object
4193 * will be marked as free. */
526d00a5 4194 if (server.vm_enabled && deleteIfSwapped(c->db,key))
4195 incrRefCount(key);
4196 dictReplace(c->db->dict,key,val);
4197 incrRefCount(val);
ed9b544e 4198 } else {
c937aa89 4199 addReply(c,shared.czero);
ed9b544e 4200 return;
4201 }
4202 } else {
526d00a5 4203 incrRefCount(key);
4204 incrRefCount(val);
ed9b544e 4205 }
4206 server.dirty++;
526d00a5 4207 removeExpire(c->db,key);
4208 if (expire) setExpire(c->db,key,time(NULL)+seconds);
c937aa89 4209 addReply(c, nx ? shared.cone : shared.ok);
ed9b544e 4210}
4211
4212static void setCommand(redisClient *c) {
526d00a5 4213 setGenericCommand(c,0,c->argv[1],c->argv[2],NULL);
ed9b544e 4214}
4215
4216static void setnxCommand(redisClient *c) {
526d00a5 4217 setGenericCommand(c,1,c->argv[1],c->argv[2],NULL);
4218}
4219
4220static void setexCommand(redisClient *c) {
4221 setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
ed9b544e 4222}
4223
322fc7d8 4224static int getGenericCommand(redisClient *c) {
dd88747b 4225 robj *o;
e0a62c7f 4226
dd88747b 4227 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
322fc7d8 4228 return REDIS_OK;
dd88747b 4229
4230 if (o->type != REDIS_STRING) {
4231 addReply(c,shared.wrongtypeerr);
4232 return REDIS_ERR;
ed9b544e 4233 } else {
dd88747b 4234 addReplyBulk(c,o);
4235 return REDIS_OK;
ed9b544e 4236 }
4237}
4238
322fc7d8 4239static void getCommand(redisClient *c) {
4240 getGenericCommand(c);
4241}
4242
f6b141c5 4243static void getsetCommand(redisClient *c) {
322fc7d8 4244 if (getGenericCommand(c) == REDIS_ERR) return;
a431eb74 4245 if (dictAdd(c->db->dict,c->argv[1],c->argv[2]) == DICT_ERR) {
4246 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
4247 } else {
4248 incrRefCount(c->argv[1]);
4249 }
4250 incrRefCount(c->argv[2]);
4251 server.dirty++;
4252 removeExpire(c->db,c->argv[1]);
4253}
4254
70003d28 4255static void mgetCommand(redisClient *c) {
70003d28 4256 int j;
e0a62c7f 4257
c937aa89 4258 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1));
70003d28 4259 for (j = 1; j < c->argc; j++) {
3305306f 4260 robj *o = lookupKeyRead(c->db,c->argv[j]);
4261 if (o == NULL) {
c937aa89 4262 addReply(c,shared.nullbulk);
70003d28 4263 } else {
70003d28 4264 if (o->type != REDIS_STRING) {
c937aa89 4265 addReply(c,shared.nullbulk);
70003d28 4266 } else {
dd88747b 4267 addReplyBulk(c,o);
70003d28 4268 }
4269 }
4270 }
4271}
4272
6c446631 4273static void msetGenericCommand(redisClient *c, int nx) {
906573e7 4274 int j, busykeys = 0;
6c446631 4275
4276 if ((c->argc % 2) == 0) {
454d4e43 4277 addReplySds(c,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
6c446631 4278 return;
4279 }
4280 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
4281 * set nothing at all if at least one already key exists. */
4282 if (nx) {
4283 for (j = 1; j < c->argc; j += 2) {
906573e7 4284 if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
4285 busykeys++;
6c446631 4286 }
4287 }
4288 }
906573e7 4289 if (busykeys) {
4290 addReply(c, shared.czero);
4291 return;
4292 }
6c446631 4293
4294 for (j = 1; j < c->argc; j += 2) {
4295 int retval;
4296
05df7621 4297 c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
6c446631 4298 retval = dictAdd(c->db->dict,c->argv[j],c->argv[j+1]);
4299 if (retval == DICT_ERR) {
4300 dictReplace(c->db->dict,c->argv[j],c->argv[j+1]);
4301 incrRefCount(c->argv[j+1]);
4302 } else {
4303 incrRefCount(c->argv[j]);
4304 incrRefCount(c->argv[j+1]);
4305 }
4306 removeExpire(c->db,c->argv[j]);
4307 }
4308 server.dirty += (c->argc-1)/2;
4309 addReply(c, nx ? shared.cone : shared.ok);
4310}
4311
4312static void msetCommand(redisClient *c) {
4313 msetGenericCommand(c,0);
4314}
4315
4316static void msetnxCommand(redisClient *c) {
4317 msetGenericCommand(c,1);
4318}
4319
d68ed120 4320static void incrDecrCommand(redisClient *c, long long incr) {
ed9b544e 4321 long long value;
4322 int retval;
4323 robj *o;
e0a62c7f 4324
3305306f 4325 o = lookupKeyWrite(c->db,c->argv[1]);
6485f293
PN
4326 if (o != NULL && checkType(c,o,REDIS_STRING)) return;
4327 if (getLongLongFromObjectOrReply(c,o,&value,NULL) != REDIS_OK) return;
ed9b544e 4328
4329 value += incr;
d6f4c262 4330 o = createStringObjectFromLongLong(value);
3305306f 4331 retval = dictAdd(c->db->dict,c->argv[1],o);
ed9b544e 4332 if (retval == DICT_ERR) {
3305306f 4333 dictReplace(c->db->dict,c->argv[1],o);
4334 removeExpire(c->db,c->argv[1]);
ed9b544e 4335 } else {
4336 incrRefCount(c->argv[1]);
4337 }
4338 server.dirty++;
c937aa89 4339 addReply(c,shared.colon);
ed9b544e 4340 addReply(c,o);
4341 addReply(c,shared.crlf);
4342}
4343
4344static void incrCommand(redisClient *c) {
a4d1ba9a 4345 incrDecrCommand(c,1);
ed9b544e 4346}
4347
4348static void decrCommand(redisClient *c) {
a4d1ba9a 4349 incrDecrCommand(c,-1);
ed9b544e 4350}
4351
4352static void incrbyCommand(redisClient *c) {
bbe025e0
AM
4353 long long incr;
4354
bd79a6bd 4355 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
a4d1ba9a 4356 incrDecrCommand(c,incr);
ed9b544e 4357}
4358
4359static void decrbyCommand(redisClient *c) {
bbe025e0
AM
4360 long long incr;
4361
bd79a6bd 4362 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
a4d1ba9a 4363 incrDecrCommand(c,-incr);
ed9b544e 4364}
4365
4b00bebd 4366static void appendCommand(redisClient *c) {
4367 int retval;
4368 size_t totlen;
4369 robj *o;
4370
4371 o = lookupKeyWrite(c->db,c->argv[1]);
4372 if (o == NULL) {
4373 /* Create the key */
4374 retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
4375 incrRefCount(c->argv[1]);
4376 incrRefCount(c->argv[2]);
4377 totlen = stringObjectLen(c->argv[2]);
4378 } else {
4379 dictEntry *de;
e0a62c7f 4380
4b00bebd 4381 de = dictFind(c->db->dict,c->argv[1]);
4382 assert(de != NULL);
4383
4384 o = dictGetEntryVal(de);
4385 if (o->type != REDIS_STRING) {
4386 addReply(c,shared.wrongtypeerr);
4387 return;
4388 }
4389 /* If the object is specially encoded or shared we have to make
4390 * a copy */
4391 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
4392 robj *decoded = getDecodedObject(o);
4393
4394 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
4395 decrRefCount(decoded);
4396 dictReplace(c->db->dict,c->argv[1],o);
4397 }
4398 /* APPEND! */
4399 if (c->argv[2]->encoding == REDIS_ENCODING_RAW) {
4400 o->ptr = sdscatlen(o->ptr,
4401 c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
4402 } else {
4403 o->ptr = sdscatprintf(o->ptr, "%ld",
4404 (unsigned long) c->argv[2]->ptr);
4405 }
4406 totlen = sdslen(o->ptr);
4407 }
4408 server.dirty++;
4409 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen));
4410}
4411
39191553 4412static void substrCommand(redisClient *c) {
4413 robj *o;
4414 long start = atoi(c->argv[2]->ptr);
4415 long end = atoi(c->argv[3]->ptr);
dd88747b 4416 size_t rangelen, strlen;
4417 sds range;
39191553 4418
dd88747b 4419 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4420 checkType(c,o,REDIS_STRING)) return;
39191553 4421
dd88747b 4422 o = getDecodedObject(o);
4423 strlen = sdslen(o->ptr);
8fe7fad7 4424
dd88747b 4425 /* convert negative indexes */
4426 if (start < 0) start = strlen+start;
4427 if (end < 0) end = strlen+end;
4428 if (start < 0) start = 0;
4429 if (end < 0) end = 0;
39191553 4430
dd88747b 4431 /* indexes sanity checks */
4432 if (start > end || (size_t)start >= strlen) {
4433 /* Out of range start or start > end result in null reply */
4434 addReply(c,shared.nullbulk);
4435 decrRefCount(o);
4436 return;
39191553 4437 }
dd88747b 4438 if ((size_t)end >= strlen) end = strlen-1;
4439 rangelen = (end-start)+1;
4440
4441 /* Return the result */
4442 addReplySds(c,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen));
4443 range = sdsnewlen((char*)o->ptr+start,rangelen);
4444 addReplySds(c,range);
4445 addReply(c,shared.crlf);
4446 decrRefCount(o);
39191553 4447}
4448
ed9b544e 4449/* ========================= Type agnostic commands ========================= */
4450
4451static void delCommand(redisClient *c) {
5109cdff 4452 int deleted = 0, j;
4453
4454 for (j = 1; j < c->argc; j++) {
4455 if (deleteKey(c->db,c->argv[j])) {
4456 server.dirty++;
4457 deleted++;
4458 }
4459 }
482b672d 4460 addReplyLongLong(c,deleted);
ed9b544e 4461}
4462
4463static void existsCommand(redisClient *c) {
f4f06efc
PN
4464 expireIfNeeded(c->db,c->argv[1]);
4465 if (dictFind(c->db->dict,c->argv[1])) {
4466 addReply(c, shared.cone);
4467 } else {
4468 addReply(c, shared.czero);
4469 }
ed9b544e 4470}
4471
4472static void selectCommand(redisClient *c) {
4473 int id = atoi(c->argv[1]->ptr);
e0a62c7f 4474
ed9b544e 4475 if (selectDb(c,id) == REDIS_ERR) {
774e3047 4476 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
ed9b544e 4477 } else {
4478 addReply(c,shared.ok);
4479 }
4480}
4481
4482static void randomkeyCommand(redisClient *c) {
4483 dictEntry *de;
dc4be23e 4484 robj *key;
e0a62c7f 4485
3305306f 4486 while(1) {
4487 de = dictGetRandomKey(c->db->dict);
ce7bef07 4488 if (!de || expireIfNeeded(c->db,dictGetEntryKey(de)) == 0) break;
3305306f 4489 }
2b619329 4490
ed9b544e 4491 if (de == NULL) {
dc4be23e 4492 addReply(c,shared.nullbulk);
4493 return;
4494 }
4495
4496 key = dictGetEntryKey(de);
4497 if (server.vm_enabled) {
4498 key = dupStringObject(key);
4499 addReplyBulk(c,key);
4500 decrRefCount(key);
ed9b544e 4501 } else {
dc4be23e 4502 addReplyBulk(c,key);
ed9b544e 4503 }
4504}
4505
4506static void keysCommand(redisClient *c) {
4507 dictIterator *di;
4508 dictEntry *de;
4509 sds pattern = c->argv[1]->ptr;
4510 int plen = sdslen(pattern);
a3f9eec2 4511 unsigned long numkeys = 0;
ed9b544e 4512 robj *lenobj = createObject(REDIS_STRING,NULL);
4513
3305306f 4514 di = dictGetIterator(c->db->dict);
ed9b544e 4515 addReply(c,lenobj);
4516 decrRefCount(lenobj);
4517 while((de = dictNext(di)) != NULL) {
4518 robj *keyobj = dictGetEntryKey(de);
3305306f 4519
ed9b544e 4520 sds key = keyobj->ptr;
4521 if ((pattern[0] == '*' && pattern[1] == '\0') ||
4522 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
3305306f 4523 if (expireIfNeeded(c->db,keyobj) == 0) {
dd88747b 4524 addReplyBulk(c,keyobj);
3305306f 4525 numkeys++;
3305306f 4526 }
ed9b544e 4527 }
4528 }
4529 dictReleaseIterator(di);
a3f9eec2 4530 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",numkeys);
ed9b544e 4531}
4532
4533static void dbsizeCommand(redisClient *c) {
4534 addReplySds(c,
3305306f 4535 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
ed9b544e 4536}
4537
4538static void lastsaveCommand(redisClient *c) {
4539 addReplySds(c,
c937aa89 4540 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
ed9b544e 4541}
4542
4543static void typeCommand(redisClient *c) {
3305306f 4544 robj *o;
ed9b544e 4545 char *type;
3305306f 4546
4547 o = lookupKeyRead(c->db,c->argv[1]);
4548 if (o == NULL) {
c937aa89 4549 type = "+none";
ed9b544e 4550 } else {
ed9b544e 4551 switch(o->type) {
c937aa89 4552 case REDIS_STRING: type = "+string"; break;
4553 case REDIS_LIST: type = "+list"; break;
4554 case REDIS_SET: type = "+set"; break;
412a8bce 4555 case REDIS_ZSET: type = "+zset"; break;
ada386b2 4556 case REDIS_HASH: type = "+hash"; break;
4557 default: type = "+unknown"; break;
ed9b544e 4558 }
4559 }
4560 addReplySds(c,sdsnew(type));
4561 addReply(c,shared.crlf);
4562}
4563
4564static void saveCommand(redisClient *c) {
9d65a1bb 4565 if (server.bgsavechildpid != -1) {
05557f6d 4566 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
4567 return;
4568 }
f78fd11b 4569 if (rdbSave(server.dbfilename) == REDIS_OK) {
ed9b544e 4570 addReply(c,shared.ok);
4571 } else {
4572 addReply(c,shared.err);
4573 }
4574}
4575
4576static void bgsaveCommand(redisClient *c) {
9d65a1bb 4577 if (server.bgsavechildpid != -1) {
ed9b544e 4578 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
4579 return;
4580 }
f78fd11b 4581 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
49b99ab4 4582 char *status = "+Background saving started\r\n";
4583 addReplySds(c,sdsnew(status));
ed9b544e 4584 } else {
4585 addReply(c,shared.err);
4586 }
4587}
4588
4589static void shutdownCommand(redisClient *c) {
4590 redisLog(REDIS_WARNING,"User requested shutdown, saving DB...");
a3b21203 4591 /* Kill the saving child if there is a background saving in progress.
4592 We want to avoid race conditions, for instance our saving child may
4593 overwrite the synchronous saving did by SHUTDOWN. */
9d65a1bb 4594 if (server.bgsavechildpid != -1) {
9f3c422c 4595 redisLog(REDIS_WARNING,"There is a live saving child. Killing it!");
4596 kill(server.bgsavechildpid,SIGKILL);
a3b21203 4597 rdbRemoveTempFile(server.bgsavechildpid);
9f3c422c 4598 }
ac945e2d 4599 if (server.appendonly) {
4600 /* Append only file: fsync() the AOF and exit */
4601 fsync(server.appendfd);
054e426d 4602 if (server.vm_enabled) unlink(server.vm_swap_file);
ac945e2d 4603 exit(0);
ed9b544e 4604 } else {
ac945e2d 4605 /* Snapshotting. Perform a SYNC SAVE and exit */
4606 if (rdbSave(server.dbfilename) == REDIS_OK) {
4607 if (server.daemonize)
4608 unlink(server.pidfile);
4609 redisLog(REDIS_WARNING,"%zu bytes used at exit",zmalloc_used_memory());
4610 redisLog(REDIS_WARNING,"Server exit now, bye bye...");
4611 exit(0);
4612 } else {
dd88747b 4613 /* Ooops.. error saving! The best we can do is to continue
4614 * operating. Note that if there was a background saving process,
4615 * in the next cron() Redis will be notified that the background
4616 * saving aborted, handling special stuff like slaves pending for
4617 * synchronization... */
e0a62c7f 4618 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit");
dd88747b 4619 addReplySds(c,
4620 sdsnew("-ERR can't quit, problems saving the DB\r\n"));
ac945e2d 4621 }
ed9b544e 4622 }
4623}
4624
4625static void renameGenericCommand(redisClient *c, int nx) {
ed9b544e 4626 robj *o;
4627
4628 /* To use the same key as src and dst is probably an error */
4629 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
c937aa89 4630 addReply(c,shared.sameobjecterr);
ed9b544e 4631 return;
4632 }
4633
dd88747b 4634 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
ed9b544e 4635 return;
dd88747b 4636
ed9b544e 4637 incrRefCount(o);
3305306f 4638 deleteIfVolatile(c->db,c->argv[2]);
4639 if (dictAdd(c->db->dict,c->argv[2],o) == DICT_ERR) {
ed9b544e 4640 if (nx) {
4641 decrRefCount(o);
c937aa89 4642 addReply(c,shared.czero);
ed9b544e 4643 return;
4644 }
3305306f 4645 dictReplace(c->db->dict,c->argv[2],o);
ed9b544e 4646 } else {
4647 incrRefCount(c->argv[2]);
4648 }
3305306f 4649 deleteKey(c->db,c->argv[1]);
ed9b544e 4650 server.dirty++;
c937aa89 4651 addReply(c,nx ? shared.cone : shared.ok);
ed9b544e 4652}
4653
4654static void renameCommand(redisClient *c) {
4655 renameGenericCommand(c,0);
4656}
4657
4658static void renamenxCommand(redisClient *c) {
4659 renameGenericCommand(c,1);
4660}
4661
4662static void moveCommand(redisClient *c) {
3305306f 4663 robj *o;
4664 redisDb *src, *dst;
ed9b544e 4665 int srcid;
4666
4667 /* Obtain source and target DB pointers */
3305306f 4668 src = c->db;
4669 srcid = c->db->id;
ed9b544e 4670 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
c937aa89 4671 addReply(c,shared.outofrangeerr);
ed9b544e 4672 return;
4673 }
3305306f 4674 dst = c->db;
4675 selectDb(c,srcid); /* Back to the source DB */
ed9b544e 4676
4677 /* If the user is moving using as target the same
4678 * DB as the source DB it is probably an error. */
4679 if (src == dst) {
c937aa89 4680 addReply(c,shared.sameobjecterr);
ed9b544e 4681 return;
4682 }
4683
4684 /* Check if the element exists and get a reference */
3305306f 4685 o = lookupKeyWrite(c->db,c->argv[1]);
4686 if (!o) {
c937aa89 4687 addReply(c,shared.czero);
ed9b544e 4688 return;
4689 }
4690
4691 /* Try to add the element to the target DB */
3305306f 4692 deleteIfVolatile(dst,c->argv[1]);
4693 if (dictAdd(dst->dict,c->argv[1],o) == DICT_ERR) {
c937aa89 4694 addReply(c,shared.czero);
ed9b544e 4695 return;
4696 }
3305306f 4697 incrRefCount(c->argv[1]);
ed9b544e 4698 incrRefCount(o);
4699
4700 /* OK! key moved, free the entry in the source DB */
3305306f 4701 deleteKey(src,c->argv[1]);
ed9b544e 4702 server.dirty++;
c937aa89 4703 addReply(c,shared.cone);
ed9b544e 4704}
4705
4706/* =================================== Lists ================================ */
4707static void pushGenericCommand(redisClient *c, int where) {
4708 robj *lobj;
ed9b544e 4709 list *list;
3305306f 4710
4711 lobj = lookupKeyWrite(c->db,c->argv[1]);
4712 if (lobj == NULL) {
95242ab5 4713 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
520b5a33 4714 addReply(c,shared.cone);
95242ab5 4715 return;
4716 }
ed9b544e 4717 lobj = createListObject();
4718 list = lobj->ptr;
4719 if (where == REDIS_HEAD) {
6b47e12e 4720 listAddNodeHead(list,c->argv[2]);
ed9b544e 4721 } else {
6b47e12e 4722 listAddNodeTail(list,c->argv[2]);
ed9b544e 4723 }
3305306f 4724 dictAdd(c->db->dict,c->argv[1],lobj);
ed9b544e 4725 incrRefCount(c->argv[1]);
4726 incrRefCount(c->argv[2]);
4727 } else {
ed9b544e 4728 if (lobj->type != REDIS_LIST) {
4729 addReply(c,shared.wrongtypeerr);
4730 return;
4731 }
95242ab5 4732 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
520b5a33 4733 addReply(c,shared.cone);
95242ab5 4734 return;
4735 }
ed9b544e 4736 list = lobj->ptr;
4737 if (where == REDIS_HEAD) {
6b47e12e 4738 listAddNodeHead(list,c->argv[2]);
ed9b544e 4739 } else {
6b47e12e 4740 listAddNodeTail(list,c->argv[2]);
ed9b544e 4741 }
4742 incrRefCount(c->argv[2]);
4743 }
4744 server.dirty++;
482b672d 4745 addReplyLongLong(c,listLength(list));
ed9b544e 4746}
4747
4748static void lpushCommand(redisClient *c) {
4749 pushGenericCommand(c,REDIS_HEAD);
4750}
4751
4752static void rpushCommand(redisClient *c) {
4753 pushGenericCommand(c,REDIS_TAIL);
4754}
4755
4756static void llenCommand(redisClient *c) {
3305306f 4757 robj *o;
ed9b544e 4758 list *l;
dd88747b 4759
4760 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
4761 checkType(c,o,REDIS_LIST)) return;
e0a62c7f 4762
dd88747b 4763 l = o->ptr;
4764 addReplyUlong(c,listLength(l));
ed9b544e 4765}
4766
4767static void lindexCommand(redisClient *c) {
3305306f 4768 robj *o;
ed9b544e 4769 int index = atoi(c->argv[2]->ptr);
dd88747b 4770 list *list;
4771 listNode *ln;
4772
4773 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4774 checkType(c,o,REDIS_LIST)) return;
4775 list = o->ptr;
4776
4777 ln = listIndex(list, index);
4778 if (ln == NULL) {
c937aa89 4779 addReply(c,shared.nullbulk);
ed9b544e 4780 } else {
dd88747b 4781 robj *ele = listNodeValue(ln);
4782 addReplyBulk(c,ele);
ed9b544e 4783 }
4784}
4785
4786static void lsetCommand(redisClient *c) {
3305306f 4787 robj *o;
ed9b544e 4788 int index = atoi(c->argv[2]->ptr);
dd88747b 4789 list *list;
4790 listNode *ln;
4791
4792 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL ||
4793 checkType(c,o,REDIS_LIST)) return;
4794 list = o->ptr;
4795
4796 ln = listIndex(list, index);
4797 if (ln == NULL) {
4798 addReply(c,shared.outofrangeerr);
ed9b544e 4799 } else {
dd88747b 4800 robj *ele = listNodeValue(ln);
ed9b544e 4801
dd88747b 4802 decrRefCount(ele);
4803 listNodeValue(ln) = c->argv[3];
4804 incrRefCount(c->argv[3]);
4805 addReply(c,shared.ok);
4806 server.dirty++;
ed9b544e 4807 }
4808}
4809
4810static void popGenericCommand(redisClient *c, int where) {
3305306f 4811 robj *o;
dd88747b 4812 list *list;
4813 listNode *ln;
3305306f 4814
dd88747b 4815 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4816 checkType(c,o,REDIS_LIST)) return;
4817 list = o->ptr;
ed9b544e 4818
dd88747b 4819 if (where == REDIS_HEAD)
4820 ln = listFirst(list);
4821 else
4822 ln = listLast(list);
ed9b544e 4823
dd88747b 4824 if (ln == NULL) {
4825 addReply(c,shared.nullbulk);
4826 } else {
4827 robj *ele = listNodeValue(ln);
4828 addReplyBulk(c,ele);
4829 listDelNode(list,ln);
3ea27d37 4830 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 4831 server.dirty++;
ed9b544e 4832 }
4833}
4834
4835static void lpopCommand(redisClient *c) {
4836 popGenericCommand(c,REDIS_HEAD);
4837}
4838
4839static void rpopCommand(redisClient *c) {
4840 popGenericCommand(c,REDIS_TAIL);
4841}
4842
4843static void lrangeCommand(redisClient *c) {
3305306f 4844 robj *o;
ed9b544e 4845 int start = atoi(c->argv[2]->ptr);
4846 int end = atoi(c->argv[3]->ptr);
dd88747b 4847 int llen;
4848 int rangelen, j;
4849 list *list;
4850 listNode *ln;
4851 robj *ele;
4852
4e27f268 4853 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
4854 || checkType(c,o,REDIS_LIST)) return;
dd88747b 4855 list = o->ptr;
4856 llen = listLength(list);
4857
4858 /* convert negative indexes */
4859 if (start < 0) start = llen+start;
4860 if (end < 0) end = llen+end;
4861 if (start < 0) start = 0;
4862 if (end < 0) end = 0;
4863
4864 /* indexes sanity checks */
4865 if (start > end || start >= llen) {
4866 /* Out of range start or start > end result in empty list */
4867 addReply(c,shared.emptymultibulk);
4868 return;
4869 }
4870 if (end >= llen) end = llen-1;
4871 rangelen = (end-start)+1;
3305306f 4872
dd88747b 4873 /* Return the result in form of a multi-bulk reply */
4874 ln = listIndex(list, start);
4875 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen));
4876 for (j = 0; j < rangelen; j++) {
4877 ele = listNodeValue(ln);
4878 addReplyBulk(c,ele);
4879 ln = ln->next;
ed9b544e 4880 }
4881}
4882
4883static void ltrimCommand(redisClient *c) {
3305306f 4884 robj *o;
ed9b544e 4885 int start = atoi(c->argv[2]->ptr);
4886 int end = atoi(c->argv[3]->ptr);
dd88747b 4887 int llen;
4888 int j, ltrim, rtrim;
4889 list *list;
4890 listNode *ln;
4891
4892 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == NULL ||
4893 checkType(c,o,REDIS_LIST)) return;
4894 list = o->ptr;
4895 llen = listLength(list);
4896
4897 /* convert negative indexes */
4898 if (start < 0) start = llen+start;
4899 if (end < 0) end = llen+end;
4900 if (start < 0) start = 0;
4901 if (end < 0) end = 0;
4902
4903 /* indexes sanity checks */
4904 if (start > end || start >= llen) {
4905 /* Out of range start or start > end result in empty list */
4906 ltrim = llen;
4907 rtrim = 0;
ed9b544e 4908 } else {
dd88747b 4909 if (end >= llen) end = llen-1;
4910 ltrim = start;
4911 rtrim = llen-end-1;
4912 }
ed9b544e 4913
dd88747b 4914 /* Remove list elements to perform the trim */
4915 for (j = 0; j < ltrim; j++) {
4916 ln = listFirst(list);
4917 listDelNode(list,ln);
4918 }
4919 for (j = 0; j < rtrim; j++) {
4920 ln = listLast(list);
4921 listDelNode(list,ln);
ed9b544e 4922 }
3ea27d37 4923 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 4924 server.dirty++;
4925 addReply(c,shared.ok);
ed9b544e 4926}
4927
4928static void lremCommand(redisClient *c) {
3305306f 4929 robj *o;
dd88747b 4930 list *list;
4931 listNode *ln, *next;
4932 int toremove = atoi(c->argv[2]->ptr);
4933 int removed = 0;
4934 int fromtail = 0;
a4d1ba9a 4935
dd88747b 4936 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
4937 checkType(c,o,REDIS_LIST)) return;
4938 list = o->ptr;
4939
4940 if (toremove < 0) {
4941 toremove = -toremove;
4942 fromtail = 1;
4943 }
4944 ln = fromtail ? list->tail : list->head;
4945 while (ln) {
4946 robj *ele = listNodeValue(ln);
4947
4948 next = fromtail ? ln->prev : ln->next;
bf028098 4949 if (equalStringObjects(ele,c->argv[3])) {
dd88747b 4950 listDelNode(list,ln);
4951 server.dirty++;
4952 removed++;
4953 if (toremove && removed == toremove) break;
ed9b544e 4954 }
dd88747b 4955 ln = next;
ed9b544e 4956 }
3ea27d37 4957 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 4958 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
ed9b544e 4959}
4960
12f9d551 4961/* This is the semantic of this command:
0f5f7e9a 4962 * RPOPLPUSH srclist dstlist:
12f9d551 4963 * IF LLEN(srclist) > 0
4964 * element = RPOP srclist
4965 * LPUSH dstlist element
4966 * RETURN element
4967 * ELSE
4968 * RETURN nil
4969 * END
4970 * END
4971 *
4972 * The idea is to be able to get an element from a list in a reliable way
4973 * since the element is not just returned but pushed against another list
4974 * as well. This command was originally proposed by Ezra Zygmuntowicz.
4975 */
0f5f7e9a 4976static void rpoplpushcommand(redisClient *c) {
12f9d551 4977 robj *sobj;
dd88747b 4978 list *srclist;
4979 listNode *ln;
4980
4981 if ((sobj = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4982 checkType(c,sobj,REDIS_LIST)) return;
4983 srclist = sobj->ptr;
4984 ln = listLast(srclist);
12f9d551 4985
dd88747b 4986 if (ln == NULL) {
12f9d551 4987 addReply(c,shared.nullbulk);
4988 } else {
dd88747b 4989 robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
4990 robj *ele = listNodeValue(ln);
4991 list *dstlist;
e20fb74f 4992
dd88747b 4993 if (dobj && dobj->type != REDIS_LIST) {
4994 addReply(c,shared.wrongtypeerr);
4995 return;
4996 }
12f9d551 4997
dd88747b 4998 /* Add the element to the target list (unless it's directly
4999 * passed to some BLPOP-ing client */
5000 if (!handleClientsWaitingListPush(c,c->argv[2],ele)) {
5001 if (dobj == NULL) {
5002 /* Create the list if the key does not exist */
5003 dobj = createListObject();
5004 dictAdd(c->db->dict,c->argv[2],dobj);
5005 incrRefCount(c->argv[2]);
12f9d551 5006 }
dd88747b 5007 dstlist = dobj->ptr;
5008 listAddNodeHead(dstlist,ele);
5009 incrRefCount(ele);
12f9d551 5010 }
dd88747b 5011
5012 /* Send the element to the client as reply as well */
5013 addReplyBulk(c,ele);
5014
5015 /* Finally remove the element from the source list */
5016 listDelNode(srclist,ln);
3ea27d37 5017 if (listLength(srclist) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5018 server.dirty++;
12f9d551 5019 }
5020}
5021
ed9b544e 5022/* ==================================== Sets ================================ */
5023
5024static void saddCommand(redisClient *c) {
ed9b544e 5025 robj *set;
5026
3305306f 5027 set = lookupKeyWrite(c->db,c->argv[1]);
5028 if (set == NULL) {
ed9b544e 5029 set = createSetObject();
3305306f 5030 dictAdd(c->db->dict,c->argv[1],set);
ed9b544e 5031 incrRefCount(c->argv[1]);
5032 } else {
ed9b544e 5033 if (set->type != REDIS_SET) {
c937aa89 5034 addReply(c,shared.wrongtypeerr);
ed9b544e 5035 return;
5036 }
5037 }
5038 if (dictAdd(set->ptr,c->argv[2],NULL) == DICT_OK) {
5039 incrRefCount(c->argv[2]);
5040 server.dirty++;
c937aa89 5041 addReply(c,shared.cone);
ed9b544e 5042 } else {
c937aa89 5043 addReply(c,shared.czero);
ed9b544e 5044 }
5045}
5046
5047static void sremCommand(redisClient *c) {
3305306f 5048 robj *set;
ed9b544e 5049
dd88747b 5050 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5051 checkType(c,set,REDIS_SET)) return;
5052
5053 if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
5054 server.dirty++;
5055 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
3ea27d37 5056 if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5057 addReply(c,shared.cone);
ed9b544e 5058 } else {
dd88747b 5059 addReply(c,shared.czero);
ed9b544e 5060 }
5061}
5062
a4460ef4 5063static void smoveCommand(redisClient *c) {
5064 robj *srcset, *dstset;
5065
5066 srcset = lookupKeyWrite(c->db,c->argv[1]);
5067 dstset = lookupKeyWrite(c->db,c->argv[2]);
5068
5069 /* If the source key does not exist return 0, if it's of the wrong type
5070 * raise an error */
5071 if (srcset == NULL || srcset->type != REDIS_SET) {
5072 addReply(c, srcset ? shared.wrongtypeerr : shared.czero);
5073 return;
5074 }
5075 /* Error if the destination key is not a set as well */
5076 if (dstset && dstset->type != REDIS_SET) {
5077 addReply(c,shared.wrongtypeerr);
5078 return;
5079 }
5080 /* Remove the element from the source set */
5081 if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) {
5082 /* Key not found in the src set! return zero */
5083 addReply(c,shared.czero);
5084 return;
5085 }
3ea27d37 5086 if (dictSize((dict*)srcset->ptr) == 0 && srcset != dstset)
5087 deleteKey(c->db,c->argv[1]);
a4460ef4 5088 server.dirty++;
5089 /* Add the element to the destination set */
5090 if (!dstset) {
5091 dstset = createSetObject();
5092 dictAdd(c->db->dict,c->argv[2],dstset);
5093 incrRefCount(c->argv[2]);
5094 }
5095 if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK)
5096 incrRefCount(c->argv[3]);
5097 addReply(c,shared.cone);
5098}
5099
ed9b544e 5100static void sismemberCommand(redisClient *c) {
3305306f 5101 robj *set;
ed9b544e 5102
dd88747b 5103 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5104 checkType(c,set,REDIS_SET)) return;
5105
5106 if (dictFind(set->ptr,c->argv[2]))
5107 addReply(c,shared.cone);
5108 else
c937aa89 5109 addReply(c,shared.czero);
ed9b544e 5110}
5111
5112static void scardCommand(redisClient *c) {
3305306f 5113 robj *o;
ed9b544e 5114 dict *s;
dd88747b 5115
5116 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5117 checkType(c,o,REDIS_SET)) return;
e0a62c7f 5118
dd88747b 5119 s = o->ptr;
5120 addReplyUlong(c,dictSize(s));
ed9b544e 5121}
5122
12fea928 5123static void spopCommand(redisClient *c) {
5124 robj *set;
5125 dictEntry *de;
5126
dd88747b 5127 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5128 checkType(c,set,REDIS_SET)) return;
5129
5130 de = dictGetRandomKey(set->ptr);
5131 if (de == NULL) {
12fea928 5132 addReply(c,shared.nullbulk);
5133 } else {
dd88747b 5134 robj *ele = dictGetEntryKey(de);
12fea928 5135
dd88747b 5136 addReplyBulk(c,ele);
5137 dictDelete(set->ptr,ele);
5138 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
3ea27d37 5139 if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5140 server.dirty++;
12fea928 5141 }
5142}
5143
2abb95a9 5144static void srandmemberCommand(redisClient *c) {
5145 robj *set;
5146 dictEntry *de;
5147
dd88747b 5148 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5149 checkType(c,set,REDIS_SET)) return;
5150
5151 de = dictGetRandomKey(set->ptr);
5152 if (de == NULL) {
2abb95a9 5153 addReply(c,shared.nullbulk);
5154 } else {
dd88747b 5155 robj *ele = dictGetEntryKey(de);
2abb95a9 5156
dd88747b 5157 addReplyBulk(c,ele);
2abb95a9 5158 }
5159}
5160
ed9b544e 5161static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
5162 dict **d1 = (void*) s1, **d2 = (void*) s2;
5163
3305306f 5164 return dictSize(*d1)-dictSize(*d2);
ed9b544e 5165}
5166
682ac724 5167static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long setsnum, robj *dstkey) {
ed9b544e 5168 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5169 dictIterator *di;
5170 dictEntry *de;
5171 robj *lenobj = NULL, *dstset = NULL;
682ac724 5172 unsigned long j, cardinality = 0;
ed9b544e 5173
ed9b544e 5174 for (j = 0; j < setsnum; j++) {
5175 robj *setobj;
3305306f 5176
5177 setobj = dstkey ?
5178 lookupKeyWrite(c->db,setskeys[j]) :
5179 lookupKeyRead(c->db,setskeys[j]);
5180 if (!setobj) {
ed9b544e 5181 zfree(dv);
5faa6025 5182 if (dstkey) {
fdcaae84 5183 if (deleteKey(c->db,dstkey))
5184 server.dirty++;
0d36ded0 5185 addReply(c,shared.czero);
5faa6025 5186 } else {
4e27f268 5187 addReply(c,shared.emptymultibulk);
5faa6025 5188 }
ed9b544e 5189 return;
5190 }
ed9b544e 5191 if (setobj->type != REDIS_SET) {
5192 zfree(dv);
c937aa89 5193 addReply(c,shared.wrongtypeerr);
ed9b544e 5194 return;
5195 }
5196 dv[j] = setobj->ptr;
5197 }
5198 /* Sort sets from the smallest to largest, this will improve our
5199 * algorithm's performace */
5200 qsort(dv,setsnum,sizeof(dict*),qsortCompareSetsByCardinality);
5201
5202 /* The first thing we should output is the total number of elements...
5203 * since this is a multi-bulk write, but at this stage we don't know
5204 * the intersection set size, so we use a trick, append an empty object
5205 * to the output list and save the pointer to later modify it with the
5206 * right length */
5207 if (!dstkey) {
5208 lenobj = createObject(REDIS_STRING,NULL);
5209 addReply(c,lenobj);
5210 decrRefCount(lenobj);
5211 } else {
5212 /* If we have a target key where to store the resulting set
5213 * create this key with an empty set inside */
5214 dstset = createSetObject();
ed9b544e 5215 }
5216
5217 /* Iterate all the elements of the first (smallest) set, and test
5218 * the element against all the other sets, if at least one set does
5219 * not include the element it is discarded */
5220 di = dictGetIterator(dv[0]);
ed9b544e 5221
5222 while((de = dictNext(di)) != NULL) {
5223 robj *ele;
5224
5225 for (j = 1; j < setsnum; j++)
5226 if (dictFind(dv[j],dictGetEntryKey(de)) == NULL) break;
5227 if (j != setsnum)
5228 continue; /* at least one set does not contain the member */
5229 ele = dictGetEntryKey(de);
5230 if (!dstkey) {
dd88747b 5231 addReplyBulk(c,ele);
ed9b544e 5232 cardinality++;
5233 } else {
5234 dictAdd(dstset->ptr,ele,NULL);
5235 incrRefCount(ele);
5236 }
5237 }
5238 dictReleaseIterator(di);
5239
83cdfe18 5240 if (dstkey) {
3ea27d37 5241 /* Store the resulting set into the target, if the intersection
5242 * is not an empty set. */
83cdfe18 5243 deleteKey(c->db,dstkey);
3ea27d37 5244 if (dictSize((dict*)dstset->ptr) > 0) {
5245 dictAdd(c->db->dict,dstkey,dstset);
5246 incrRefCount(dstkey);
482b672d 5247 addReplyLongLong(c,dictSize((dict*)dstset->ptr));
3ea27d37 5248 } else {
5249 decrRefCount(dstset);
d36c4e97 5250 addReply(c,shared.czero);
3ea27d37 5251 }
40d224a9 5252 server.dirty++;
d36c4e97 5253 } else {
5254 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
40d224a9 5255 }
ed9b544e 5256 zfree(dv);
5257}
5258
5259static void sinterCommand(redisClient *c) {
5260 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
5261}
5262
5263static void sinterstoreCommand(redisClient *c) {
5264 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
5265}
5266
f4f56e1d 5267#define REDIS_OP_UNION 0
5268#define REDIS_OP_DIFF 1
2830ca53 5269#define REDIS_OP_INTER 2
f4f56e1d 5270
5271static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey, int op) {
40d224a9 5272 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5273 dictIterator *di;
5274 dictEntry *de;
f4f56e1d 5275 robj *dstset = NULL;
40d224a9 5276 int j, cardinality = 0;
5277
40d224a9 5278 for (j = 0; j < setsnum; j++) {
5279 robj *setobj;
5280
5281 setobj = dstkey ?
5282 lookupKeyWrite(c->db,setskeys[j]) :
5283 lookupKeyRead(c->db,setskeys[j]);
5284 if (!setobj) {
5285 dv[j] = NULL;
5286 continue;
5287 }
5288 if (setobj->type != REDIS_SET) {
5289 zfree(dv);
5290 addReply(c,shared.wrongtypeerr);
5291 return;
5292 }
5293 dv[j] = setobj->ptr;
5294 }
5295
5296 /* We need a temp set object to store our union. If the dstkey
5297 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
5298 * this set object will be the resulting object to set into the target key*/
5299 dstset = createSetObject();
5300
40d224a9 5301 /* Iterate all the elements of all the sets, add every element a single
5302 * time to the result set */
5303 for (j = 0; j < setsnum; j++) {
51829ed3 5304 if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */
40d224a9 5305 if (!dv[j]) continue; /* non existing keys are like empty sets */
5306
5307 di = dictGetIterator(dv[j]);
40d224a9 5308
5309 while((de = dictNext(di)) != NULL) {
5310 robj *ele;
5311
5312 /* dictAdd will not add the same element multiple times */
5313 ele = dictGetEntryKey(de);
f4f56e1d 5314 if (op == REDIS_OP_UNION || j == 0) {
5315 if (dictAdd(dstset->ptr,ele,NULL) == DICT_OK) {
5316 incrRefCount(ele);
40d224a9 5317 cardinality++;
5318 }
f4f56e1d 5319 } else if (op == REDIS_OP_DIFF) {
5320 if (dictDelete(dstset->ptr,ele) == DICT_OK) {
5321 cardinality--;
5322 }
40d224a9 5323 }
5324 }
5325 dictReleaseIterator(di);
51829ed3 5326
d36c4e97 5327 /* result set is empty? Exit asap. */
5328 if (op == REDIS_OP_DIFF && cardinality == 0) break;
40d224a9 5329 }
5330
f4f56e1d 5331 /* Output the content of the resulting set, if not in STORE mode */
5332 if (!dstkey) {
5333 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
5334 di = dictGetIterator(dstset->ptr);
f4f56e1d 5335 while((de = dictNext(di)) != NULL) {
5336 robj *ele;
5337
5338 ele = dictGetEntryKey(de);
dd88747b 5339 addReplyBulk(c,ele);
f4f56e1d 5340 }
5341 dictReleaseIterator(di);
d36c4e97 5342 decrRefCount(dstset);
83cdfe18
AG
5343 } else {
5344 /* If we have a target key where to store the resulting set
5345 * create this key with the result set inside */
5346 deleteKey(c->db,dstkey);
3ea27d37 5347 if (dictSize((dict*)dstset->ptr) > 0) {
5348 dictAdd(c->db->dict,dstkey,dstset);
5349 incrRefCount(dstkey);
482b672d 5350 addReplyLongLong(c,dictSize((dict*)dstset->ptr));
3ea27d37 5351 } else {
5352 decrRefCount(dstset);
d36c4e97 5353 addReply(c,shared.czero);
3ea27d37 5354 }
40d224a9 5355 server.dirty++;
5356 }
5357 zfree(dv);
5358}
5359
5360static void sunionCommand(redisClient *c) {
f4f56e1d 5361 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
40d224a9 5362}
5363
5364static void sunionstoreCommand(redisClient *c) {
f4f56e1d 5365 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
5366}
5367
5368static void sdiffCommand(redisClient *c) {
5369 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
5370}
5371
5372static void sdiffstoreCommand(redisClient *c) {
5373 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
40d224a9 5374}
5375
6b47e12e 5376/* ==================================== ZSets =============================== */
5377
5378/* ZSETs are ordered sets using two data structures to hold the same elements
5379 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
5380 * data structure.
5381 *
5382 * The elements are added to an hash table mapping Redis objects to scores.
5383 * At the same time the elements are added to a skip list mapping scores
5384 * to Redis objects (so objects are sorted by scores in this "view"). */
5385
5386/* This skiplist implementation is almost a C translation of the original
5387 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
5388 * Alternative to Balanced Trees", modified in three ways:
5389 * a) this implementation allows for repeated values.
5390 * b) the comparison is not just by key (our 'score') but by satellite data.
5391 * c) there is a back pointer, so it's a doubly linked list with the back
5392 * pointers being only at "level 1". This allows to traverse the list
5393 * from tail to head, useful for ZREVRANGE. */
5394
5395static zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
5396 zskiplistNode *zn = zmalloc(sizeof(*zn));
5397
5398 zn->forward = zmalloc(sizeof(zskiplistNode*) * level);
2b37892e
PN
5399 if (level > 0)
5400 zn->span = zmalloc(sizeof(unsigned int) * (level - 1));
6b47e12e 5401 zn->score = score;
5402 zn->obj = obj;
5403 return zn;
5404}
5405
5406static zskiplist *zslCreate(void) {
5407 int j;
5408 zskiplist *zsl;
e0a62c7f 5409
6b47e12e 5410 zsl = zmalloc(sizeof(*zsl));
5411 zsl->level = 1;
cc812361 5412 zsl->length = 0;
6b47e12e 5413 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);
69d95c3e 5414 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
6b47e12e 5415 zsl->header->forward[j] = NULL;
94e543b5 5416
5417 /* span has space for ZSKIPLIST_MAXLEVEL-1 elements */
5418 if (j < ZSKIPLIST_MAXLEVEL-1)
5419 zsl->header->span[j] = 0;
69d95c3e 5420 }
e3870fab 5421 zsl->header->backward = NULL;
5422 zsl->tail = NULL;
6b47e12e 5423 return zsl;
5424}
5425
fd8ccf44 5426static void zslFreeNode(zskiplistNode *node) {
5427 decrRefCount(node->obj);
ad807e6f 5428 zfree(node->forward);
69d95c3e 5429 zfree(node->span);
fd8ccf44 5430 zfree(node);
5431}
5432
5433static void zslFree(zskiplist *zsl) {
ad807e6f 5434 zskiplistNode *node = zsl->header->forward[0], *next;
fd8ccf44 5435
ad807e6f 5436 zfree(zsl->header->forward);
69d95c3e 5437 zfree(zsl->header->span);
ad807e6f 5438 zfree(zsl->header);
fd8ccf44 5439 while(node) {
599379dd 5440 next = node->forward[0];
fd8ccf44 5441 zslFreeNode(node);
5442 node = next;
5443 }
ad807e6f 5444 zfree(zsl);
fd8ccf44 5445}
5446
6b47e12e 5447static int zslRandomLevel(void) {
5448 int level = 1;
5449 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
5450 level += 1;
10c2baa5 5451 return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;
6b47e12e 5452}
5453
5454static void zslInsert(zskiplist *zsl, double score, robj *obj) {
5455 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
2b37892e 5456 unsigned int rank[ZSKIPLIST_MAXLEVEL];
6b47e12e 5457 int i, level;
5458
5459 x = zsl->header;
5460 for (i = zsl->level-1; i >= 0; i--) {
2b37892e
PN
5461 /* store rank that is crossed to reach the insert position */
5462 rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
69d95c3e 5463
9d60e6e4 5464 while (x->forward[i] &&
5465 (x->forward[i]->score < score ||
5466 (x->forward[i]->score == score &&
69d95c3e 5467 compareStringObjects(x->forward[i]->obj,obj) < 0))) {
a50ea45c 5468 rank[i] += i > 0 ? x->span[i-1] : 1;
6b47e12e 5469 x = x->forward[i];
69d95c3e 5470 }
6b47e12e 5471 update[i] = x;
5472 }
6b47e12e 5473 /* we assume the key is not already inside, since we allow duplicated
5474 * scores, and the re-insertion of score and redis object should never
5475 * happpen since the caller of zslInsert() should test in the hash table
5476 * if the element is already inside or not. */
5477 level = zslRandomLevel();
5478 if (level > zsl->level) {
69d95c3e 5479 for (i = zsl->level; i < level; i++) {
2b37892e 5480 rank[i] = 0;
6b47e12e 5481 update[i] = zsl->header;
2b37892e 5482 update[i]->span[i-1] = zsl->length;
69d95c3e 5483 }
6b47e12e 5484 zsl->level = level;
5485 }
5486 x = zslCreateNode(level,score,obj);
5487 for (i = 0; i < level; i++) {
5488 x->forward[i] = update[i]->forward[i];
5489 update[i]->forward[i] = x;
69d95c3e
PN
5490
5491 /* update span covered by update[i] as x is inserted here */
2b37892e
PN
5492 if (i > 0) {
5493 x->span[i-1] = update[i]->span[i-1] - (rank[0] - rank[i]);
5494 update[i]->span[i-1] = (rank[0] - rank[i]) + 1;
5495 }
6b47e12e 5496 }
69d95c3e
PN
5497
5498 /* increment span for untouched levels */
5499 for (i = level; i < zsl->level; i++) {
2b37892e 5500 update[i]->span[i-1]++;
69d95c3e
PN
5501 }
5502
bb975144 5503 x->backward = (update[0] == zsl->header) ? NULL : update[0];
e3870fab 5504 if (x->forward[0])
5505 x->forward[0]->backward = x;
5506 else
5507 zsl->tail = x;
cc812361 5508 zsl->length++;
6b47e12e 5509}
5510
84105336
PN
5511/* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
5512void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
5513 int i;
5514 for (i = 0; i < zsl->level; i++) {
5515 if (update[i]->forward[i] == x) {
5516 if (i > 0) {
5517 update[i]->span[i-1] += x->span[i-1] - 1;
5518 }
5519 update[i]->forward[i] = x->forward[i];
5520 } else {
5521 /* invariant: i > 0, because update[0]->forward[0]
5522 * is always equal to x */
5523 update[i]->span[i-1] -= 1;
5524 }
5525 }
5526 if (x->forward[0]) {
5527 x->forward[0]->backward = x->backward;
5528 } else {
5529 zsl->tail = x->backward;
5530 }
5531 while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
5532 zsl->level--;
5533 zsl->length--;
5534}
5535
50c55df5 5536/* Delete an element with matching score/object from the skiplist. */
fd8ccf44 5537static int zslDelete(zskiplist *zsl, double score, robj *obj) {
e197b441 5538 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5539 int i;
5540
5541 x = zsl->header;
5542 for (i = zsl->level-1; i >= 0; i--) {
9d60e6e4 5543 while (x->forward[i] &&
5544 (x->forward[i]->score < score ||
5545 (x->forward[i]->score == score &&
5546 compareStringObjects(x->forward[i]->obj,obj) < 0)))
e197b441 5547 x = x->forward[i];
5548 update[i] = x;
5549 }
5550 /* We may have multiple elements with the same score, what we need
5551 * is to find the element with both the right score and object. */
5552 x = x->forward[0];
bf028098 5553 if (x && score == x->score && equalStringObjects(x->obj,obj)) {
84105336 5554 zslDeleteNode(zsl, x, update);
9d60e6e4 5555 zslFreeNode(x);
9d60e6e4 5556 return 1;
5557 } else {
5558 return 0; /* not found */
e197b441 5559 }
5560 return 0; /* not found */
fd8ccf44 5561}
5562
1807985b 5563/* Delete all the elements with score between min and max from the skiplist.
5564 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
5565 * Note that this function takes the reference to the hash table view of the
5566 * sorted set, in order to remove the elements from the hash table too. */
f84d3933 5567static unsigned long zslDeleteRangeByScore(zskiplist *zsl, double min, double max, dict *dict) {
1807985b 5568 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5569 unsigned long removed = 0;
5570 int i;
5571
5572 x = zsl->header;
5573 for (i = zsl->level-1; i >= 0; i--) {
5574 while (x->forward[i] && x->forward[i]->score < min)
5575 x = x->forward[i];
5576 update[i] = x;
5577 }
5578 /* We may have multiple elements with the same score, what we need
5579 * is to find the element with both the right score and object. */
5580 x = x->forward[0];
5581 while (x && x->score <= max) {
84105336
PN
5582 zskiplistNode *next = x->forward[0];
5583 zslDeleteNode(zsl, x, update);
1807985b 5584 dictDelete(dict,x->obj);
5585 zslFreeNode(x);
1807985b 5586 removed++;
5587 x = next;
5588 }
5589 return removed; /* not found */
5590}
1807985b 5591
9212eafd 5592/* Delete all the elements with rank between start and end from the skiplist.
2424490f 5593 * Start and end are inclusive. Note that start and end need to be 1-based */
9212eafd
PN
5594static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) {
5595 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5596 unsigned long traversed = 0, removed = 0;
5597 int i;
5598
9212eafd
PN
5599 x = zsl->header;
5600 for (i = zsl->level-1; i >= 0; i--) {
5601 while (x->forward[i] && (traversed + (i > 0 ? x->span[i-1] : 1)) < start) {
5602 traversed += i > 0 ? x->span[i-1] : 1;
5603 x = x->forward[i];
1807985b 5604 }
9212eafd
PN
5605 update[i] = x;
5606 }
5607
5608 traversed++;
5609 x = x->forward[0];
5610 while (x && traversed <= end) {
84105336
PN
5611 zskiplistNode *next = x->forward[0];
5612 zslDeleteNode(zsl, x, update);
1807985b 5613 dictDelete(dict,x->obj);
5614 zslFreeNode(x);
1807985b 5615 removed++;
9212eafd 5616 traversed++;
1807985b 5617 x = next;
5618 }
9212eafd 5619 return removed;
1807985b 5620}
5621
50c55df5 5622/* Find the first node having a score equal or greater than the specified one.
5623 * Returns NULL if there is no match. */
5624static zskiplistNode *zslFirstWithScore(zskiplist *zsl, double score) {
5625 zskiplistNode *x;
5626 int i;
5627
5628 x = zsl->header;
5629 for (i = zsl->level-1; i >= 0; i--) {
5630 while (x->forward[i] && x->forward[i]->score < score)
5631 x = x->forward[i];
5632 }
5633 /* We may have multiple elements with the same score, what we need
5634 * is to find the element with both the right score and object. */
5635 return x->forward[0];
5636}
5637
27b0ccca
PN
5638/* Find the rank for an element by both score and key.
5639 * Returns 0 when the element cannot be found, rank otherwise.
5640 * Note that the rank is 1-based due to the span of zsl->header to the
5641 * first element. */
5642static unsigned long zslGetRank(zskiplist *zsl, double score, robj *o) {
5643 zskiplistNode *x;
5644 unsigned long rank = 0;
5645 int i;
5646
5647 x = zsl->header;
5648 for (i = zsl->level-1; i >= 0; i--) {
5649 while (x->forward[i] &&
5650 (x->forward[i]->score < score ||
5651 (x->forward[i]->score == score &&
5652 compareStringObjects(x->forward[i]->obj,o) <= 0))) {
a50ea45c 5653 rank += i > 0 ? x->span[i-1] : 1;
27b0ccca
PN
5654 x = x->forward[i];
5655 }
5656
5657 /* x might be equal to zsl->header, so test if obj is non-NULL */
bf028098 5658 if (x->obj && equalStringObjects(x->obj,o)) {
27b0ccca
PN
5659 return rank;
5660 }
5661 }
5662 return 0;
5663}
5664
e74825c2
PN
5665/* Finds an element by its rank. The rank argument needs to be 1-based. */
5666zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) {
5667 zskiplistNode *x;
5668 unsigned long traversed = 0;
5669 int i;
5670
5671 x = zsl->header;
5672 for (i = zsl->level-1; i >= 0; i--) {
dd88747b 5673 while (x->forward[i] && (traversed + (i>0 ? x->span[i-1] : 1)) <= rank)
5674 {
a50ea45c 5675 traversed += i > 0 ? x->span[i-1] : 1;
e74825c2
PN
5676 x = x->forward[i];
5677 }
e74825c2
PN
5678 if (traversed == rank) {
5679 return x;
5680 }
5681 }
5682 return NULL;
5683}
5684
fd8ccf44 5685/* The actual Z-commands implementations */
5686
7db723ad 5687/* This generic command implements both ZADD and ZINCRBY.
e2665397 5688 * scoreval is the score if the operation is a ZADD (doincrement == 0) or
7db723ad 5689 * the increment if the operation is a ZINCRBY (doincrement == 1). */
e2665397 5690static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scoreval, int doincrement) {
fd8ccf44 5691 robj *zsetobj;
5692 zset *zs;
5693 double *score;
5694
e2665397 5695 zsetobj = lookupKeyWrite(c->db,key);
fd8ccf44 5696 if (zsetobj == NULL) {
5697 zsetobj = createZsetObject();
e2665397 5698 dictAdd(c->db->dict,key,zsetobj);
5699 incrRefCount(key);
fd8ccf44 5700 } else {
5701 if (zsetobj->type != REDIS_ZSET) {
5702 addReply(c,shared.wrongtypeerr);
5703 return;
5704 }
5705 }
fd8ccf44 5706 zs = zsetobj->ptr;
e2665397 5707
7db723ad 5708 /* Ok now since we implement both ZADD and ZINCRBY here the code
e2665397 5709 * needs to handle the two different conditions. It's all about setting
5710 * '*score', that is, the new score to set, to the right value. */
5711 score = zmalloc(sizeof(double));
5712 if (doincrement) {
5713 dictEntry *de;
5714
5715 /* Read the old score. If the element was not present starts from 0 */
5716 de = dictFind(zs->dict,ele);
5717 if (de) {
5718 double *oldscore = dictGetEntryVal(de);
5719 *score = *oldscore + scoreval;
5720 } else {
5721 *score = scoreval;
5722 }
5723 } else {
5724 *score = scoreval;
5725 }
5726
5727 /* What follows is a simple remove and re-insert operation that is common
7db723ad 5728 * to both ZADD and ZINCRBY... */
e2665397 5729 if (dictAdd(zs->dict,ele,score) == DICT_OK) {
fd8ccf44 5730 /* case 1: New element */
e2665397 5731 incrRefCount(ele); /* added to hash */
5732 zslInsert(zs->zsl,*score,ele);
5733 incrRefCount(ele); /* added to skiplist */
fd8ccf44 5734 server.dirty++;
e2665397 5735 if (doincrement)
e2665397 5736 addReplyDouble(c,*score);
91d71bfc 5737 else
5738 addReply(c,shared.cone);
fd8ccf44 5739 } else {
5740 dictEntry *de;
5741 double *oldscore;
e0a62c7f 5742
fd8ccf44 5743 /* case 2: Score update operation */
e2665397 5744 de = dictFind(zs->dict,ele);
dfc5e96c 5745 redisAssert(de != NULL);
fd8ccf44 5746 oldscore = dictGetEntryVal(de);
5747 if (*score != *oldscore) {
5748 int deleted;
5749
e2665397 5750 /* Remove and insert the element in the skip list with new score */
5751 deleted = zslDelete(zs->zsl,*oldscore,ele);
dfc5e96c 5752 redisAssert(deleted != 0);
e2665397 5753 zslInsert(zs->zsl,*score,ele);
5754 incrRefCount(ele);
5755 /* Update the score in the hash table */
5756 dictReplace(zs->dict,ele,score);
fd8ccf44 5757 server.dirty++;
2161a965 5758 } else {
5759 zfree(score);
fd8ccf44 5760 }
e2665397 5761 if (doincrement)
5762 addReplyDouble(c,*score);
5763 else
5764 addReply(c,shared.czero);
fd8ccf44 5765 }
5766}
5767
e2665397 5768static void zaddCommand(redisClient *c) {
5769 double scoreval;
5770
bd79a6bd 5771 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
e2665397 5772 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,0);
5773}
5774
7db723ad 5775static void zincrbyCommand(redisClient *c) {
e2665397 5776 double scoreval;
5777
bd79a6bd 5778 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
e2665397 5779 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,1);
5780}
5781
1b7106e7 5782static void zremCommand(redisClient *c) {
5783 robj *zsetobj;
5784 zset *zs;
dd88747b 5785 dictEntry *de;
5786 double *oldscore;
5787 int deleted;
1b7106e7 5788
dd88747b 5789 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5790 checkType(c,zsetobj,REDIS_ZSET)) return;
1b7106e7 5791
dd88747b 5792 zs = zsetobj->ptr;
5793 de = dictFind(zs->dict,c->argv[2]);
5794 if (de == NULL) {
5795 addReply(c,shared.czero);
5796 return;
1b7106e7 5797 }
dd88747b 5798 /* Delete from the skiplist */
5799 oldscore = dictGetEntryVal(de);
5800 deleted = zslDelete(zs->zsl,*oldscore,c->argv[2]);
5801 redisAssert(deleted != 0);
5802
5803 /* Delete from the hash table */
5804 dictDelete(zs->dict,c->argv[2]);
5805 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
3ea27d37 5806 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5807 server.dirty++;
5808 addReply(c,shared.cone);
1b7106e7 5809}
5810
1807985b 5811static void zremrangebyscoreCommand(redisClient *c) {
bbe025e0
AM
5812 double min;
5813 double max;
dd88747b 5814 long deleted;
1807985b 5815 robj *zsetobj;
5816 zset *zs;
5817
bd79a6bd
PN
5818 if ((getDoubleFromObjectOrReply(c, c->argv[2], &min, NULL) != REDIS_OK) ||
5819 (getDoubleFromObjectOrReply(c, c->argv[3], &max, NULL) != REDIS_OK)) return;
bbe025e0 5820
dd88747b 5821 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5822 checkType(c,zsetobj,REDIS_ZSET)) return;
1807985b 5823
dd88747b 5824 zs = zsetobj->ptr;
5825 deleted = zslDeleteRangeByScore(zs->zsl,min,max,zs->dict);
5826 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
3ea27d37 5827 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5828 server.dirty += deleted;
482b672d 5829 addReplyLongLong(c,deleted);
1807985b 5830}
5831
9212eafd 5832static void zremrangebyrankCommand(redisClient *c) {
bbe025e0
AM
5833 long start;
5834 long end;
dd88747b 5835 int llen;
5836 long deleted;
9212eafd
PN
5837 robj *zsetobj;
5838 zset *zs;
5839
bd79a6bd
PN
5840 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
5841 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
bbe025e0 5842
dd88747b 5843 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5844 checkType(c,zsetobj,REDIS_ZSET)) return;
5845 zs = zsetobj->ptr;
5846 llen = zs->zsl->length;
9212eafd 5847
dd88747b 5848 /* convert negative indexes */
5849 if (start < 0) start = llen+start;
5850 if (end < 0) end = llen+end;
5851 if (start < 0) start = 0;
5852 if (end < 0) end = 0;
9212eafd 5853
dd88747b 5854 /* indexes sanity checks */
5855 if (start > end || start >= llen) {
5856 addReply(c,shared.czero);
5857 return;
9212eafd 5858 }
dd88747b 5859 if (end >= llen) end = llen-1;
5860
5861 /* increment start and end because zsl*Rank functions
5862 * use 1-based rank */
5863 deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
5864 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
3ea27d37 5865 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5866 server.dirty += deleted;
482b672d 5867 addReplyLongLong(c, deleted);
9212eafd
PN
5868}
5869
8f92e768
PN
5870typedef struct {
5871 dict *dict;
5872 double weight;
5873} zsetopsrc;
5874
5875static int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) {
5876 zsetopsrc *d1 = (void*) s1, *d2 = (void*) s2;
5877 unsigned long size1, size2;
5878 size1 = d1->dict ? dictSize(d1->dict) : 0;
5879 size2 = d2->dict ? dictSize(d2->dict) : 0;
5880 return size1 - size2;
5881}
5882
d2764cd6
PN
5883#define REDIS_AGGR_SUM 1
5884#define REDIS_AGGR_MIN 2
5885#define REDIS_AGGR_MAX 3
5886
5887inline static void zunionInterAggregate(double *target, double val, int aggregate) {
5888 if (aggregate == REDIS_AGGR_SUM) {
5889 *target = *target + val;
5890 } else if (aggregate == REDIS_AGGR_MIN) {
5891 *target = val < *target ? val : *target;
5892 } else if (aggregate == REDIS_AGGR_MAX) {
5893 *target = val > *target ? val : *target;
5894 } else {
5895 /* safety net */
f83c6cb5 5896 redisPanic("Unknown ZUNION/INTER aggregate type");
d2764cd6
PN
5897 }
5898}
5899
2830ca53 5900static void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) {
8f92e768 5901 int i, j, zsetnum;
d2764cd6 5902 int aggregate = REDIS_AGGR_SUM;
8f92e768 5903 zsetopsrc *src;
2830ca53
PN
5904 robj *dstobj;
5905 zset *dstzset;
b287c9bb
PN
5906 dictIterator *di;
5907 dictEntry *de;
5908
2830ca53
PN
5909 /* expect zsetnum input keys to be given */
5910 zsetnum = atoi(c->argv[2]->ptr);
5911 if (zsetnum < 1) {
5d373da9 5912 addReplySds(c,sdsnew("-ERR at least 1 input key is needed for ZUNIONSTORE/ZINTERSTORE\r\n"));
2830ca53 5913 return;
b287c9bb 5914 }
2830ca53
PN
5915
5916 /* test if the expected number of keys would overflow */
5917 if (3+zsetnum > c->argc) {
b287c9bb
PN
5918 addReply(c,shared.syntaxerr);
5919 return;
5920 }
5921
2830ca53 5922 /* read keys to be used for input */
b9eed483 5923 src = zmalloc(sizeof(zsetopsrc) * zsetnum);
2830ca53 5924 for (i = 0, j = 3; i < zsetnum; i++, j++) {
b287c9bb
PN
5925 robj *zsetobj = lookupKeyWrite(c->db,c->argv[j]);
5926 if (!zsetobj) {
8f92e768 5927 src[i].dict = NULL;
b287c9bb
PN
5928 } else {
5929 if (zsetobj->type != REDIS_ZSET) {
8f92e768 5930 zfree(src);
b287c9bb
PN
5931 addReply(c,shared.wrongtypeerr);
5932 return;
5933 }
8f92e768 5934 src[i].dict = ((zset*)zsetobj->ptr)->dict;
b287c9bb 5935 }
2830ca53
PN
5936
5937 /* default all weights to 1 */
8f92e768 5938 src[i].weight = 1.0;
b287c9bb
PN
5939 }
5940
2830ca53
PN
5941 /* parse optional extra arguments */
5942 if (j < c->argc) {
d2764cd6 5943 int remaining = c->argc - j;
b287c9bb 5944
2830ca53 5945 while (remaining) {
d2764cd6 5946 if (remaining >= (zsetnum + 1) && !strcasecmp(c->argv[j]->ptr,"weights")) {
2830ca53 5947 j++; remaining--;
2830ca53 5948 for (i = 0; i < zsetnum; i++, j++, remaining--) {
bd79a6bd 5949 if (getDoubleFromObjectOrReply(c, c->argv[j], &src[i].weight, NULL) != REDIS_OK)
bbe025e0 5950 return;
2830ca53 5951 }
d2764cd6
PN
5952 } else if (remaining >= 2 && !strcasecmp(c->argv[j]->ptr,"aggregate")) {
5953 j++; remaining--;
5954 if (!strcasecmp(c->argv[j]->ptr,"sum")) {
5955 aggregate = REDIS_AGGR_SUM;
5956 } else if (!strcasecmp(c->argv[j]->ptr,"min")) {
5957 aggregate = REDIS_AGGR_MIN;
5958 } else if (!strcasecmp(c->argv[j]->ptr,"max")) {
5959 aggregate = REDIS_AGGR_MAX;
5960 } else {
5961 zfree(src);
5962 addReply(c,shared.syntaxerr);
5963 return;
5964 }
5965 j++; remaining--;
2830ca53 5966 } else {
8f92e768 5967 zfree(src);
2830ca53
PN
5968 addReply(c,shared.syntaxerr);
5969 return;
5970 }
5971 }
5972 }
b287c9bb 5973
d2764cd6
PN
5974 /* sort sets from the smallest to largest, this will improve our
5975 * algorithm's performance */
5976 qsort(src,zsetnum,sizeof(zsetopsrc), qsortCompareZsetopsrcByCardinality);
5977
2830ca53
PN
5978 dstobj = createZsetObject();
5979 dstzset = dstobj->ptr;
5980
5981 if (op == REDIS_OP_INTER) {
8f92e768
PN
5982 /* skip going over all entries if the smallest zset is NULL or empty */
5983 if (src[0].dict && dictSize(src[0].dict) > 0) {
5984 /* precondition: as src[0].dict is non-empty and the zsets are ordered
5985 * from small to large, all src[i > 0].dict are non-empty too */
5986 di = dictGetIterator(src[0].dict);
2830ca53 5987 while((de = dictNext(di)) != NULL) {
d2764cd6
PN
5988 double *score = zmalloc(sizeof(double)), value;
5989 *score = src[0].weight * (*(double*)dictGetEntryVal(de));
2830ca53 5990
d2764cd6
PN
5991 for (j = 1; j < zsetnum; j++) {
5992 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
2830ca53 5993 if (other) {
d2764cd6
PN
5994 value = src[j].weight * (*(double*)dictGetEntryVal(other));
5995 zunionInterAggregate(score, value, aggregate);
2830ca53
PN
5996 } else {
5997 break;
5998 }
5999 }
b287c9bb 6000
2830ca53 6001 /* skip entry when not present in every source dict */
8f92e768 6002 if (j != zsetnum) {
2830ca53
PN
6003 zfree(score);
6004 } else {
6005 robj *o = dictGetEntryKey(de);
6006 dictAdd(dstzset->dict,o,score);
6007 incrRefCount(o); /* added to dictionary */
6008 zslInsert(dstzset->zsl,*score,o);
6009 incrRefCount(o); /* added to skiplist */
b287c9bb
PN
6010 }
6011 }
2830ca53
PN
6012 dictReleaseIterator(di);
6013 }
6014 } else if (op == REDIS_OP_UNION) {
6015 for (i = 0; i < zsetnum; i++) {
8f92e768 6016 if (!src[i].dict) continue;
2830ca53 6017
8f92e768 6018 di = dictGetIterator(src[i].dict);
2830ca53
PN
6019 while((de = dictNext(di)) != NULL) {
6020 /* skip key when already processed */
6021 if (dictFind(dstzset->dict,dictGetEntryKey(de)) != NULL) continue;
6022
d2764cd6
PN
6023 double *score = zmalloc(sizeof(double)), value;
6024 *score = src[i].weight * (*(double*)dictGetEntryVal(de));
2830ca53 6025
d2764cd6
PN
6026 /* because the zsets are sorted by size, its only possible
6027 * for sets at larger indices to hold this entry */
6028 for (j = (i+1); j < zsetnum; j++) {
6029 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
2830ca53 6030 if (other) {
d2764cd6
PN
6031 value = src[j].weight * (*(double*)dictGetEntryVal(other));
6032 zunionInterAggregate(score, value, aggregate);
2830ca53
PN
6033 }
6034 }
b287c9bb 6035
2830ca53
PN
6036 robj *o = dictGetEntryKey(de);
6037 dictAdd(dstzset->dict,o,score);
6038 incrRefCount(o); /* added to dictionary */
6039 zslInsert(dstzset->zsl,*score,o);
6040 incrRefCount(o); /* added to skiplist */
6041 }
6042 dictReleaseIterator(di);
b287c9bb 6043 }
2830ca53
PN
6044 } else {
6045 /* unknown operator */
6046 redisAssert(op == REDIS_OP_INTER || op == REDIS_OP_UNION);
b287c9bb
PN
6047 }
6048
6049 deleteKey(c->db,dstkey);
3ea27d37 6050 if (dstzset->zsl->length) {
6051 dictAdd(c->db->dict,dstkey,dstobj);
6052 incrRefCount(dstkey);
482b672d 6053 addReplyLongLong(c, dstzset->zsl->length);
3ea27d37 6054 server.dirty++;
6055 } else {
8bca8773 6056 decrRefCount(dstobj);
3ea27d37 6057 addReply(c, shared.czero);
6058 }
8f92e768 6059 zfree(src);
b287c9bb
PN
6060}
6061
5d373da9 6062static void zunionstoreCommand(redisClient *c) {
2830ca53 6063 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_UNION);
b287c9bb
PN
6064}
6065
5d373da9 6066static void zinterstoreCommand(redisClient *c) {
2830ca53 6067 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_INTER);
b287c9bb
PN
6068}
6069
e3870fab 6070static void zrangeGenericCommand(redisClient *c, int reverse) {
cc812361 6071 robj *o;
bbe025e0
AM
6072 long start;
6073 long end;
752da584 6074 int withscores = 0;
dd88747b 6075 int llen;
6076 int rangelen, j;
6077 zset *zsetobj;
6078 zskiplist *zsl;
6079 zskiplistNode *ln;
6080 robj *ele;
752da584 6081
bd79a6bd
PN
6082 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
6083 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
bbe025e0 6084
752da584 6085 if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) {
6086 withscores = 1;
6087 } else if (c->argc >= 5) {
6088 addReply(c,shared.syntaxerr);
6089 return;
6090 }
cc812361 6091
4e27f268 6092 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
6093 || checkType(c,o,REDIS_ZSET)) return;
dd88747b 6094 zsetobj = o->ptr;
6095 zsl = zsetobj->zsl;
6096 llen = zsl->length;
cc812361 6097
dd88747b 6098 /* convert negative indexes */
6099 if (start < 0) start = llen+start;
6100 if (end < 0) end = llen+end;
6101 if (start < 0) start = 0;
6102 if (end < 0) end = 0;
cc812361 6103
dd88747b 6104 /* indexes sanity checks */
6105 if (start > end || start >= llen) {
6106 /* Out of range start or start > end result in empty list */
6107 addReply(c,shared.emptymultibulk);
6108 return;
6109 }
6110 if (end >= llen) end = llen-1;
6111 rangelen = (end-start)+1;
cc812361 6112
dd88747b 6113 /* check if starting point is trivial, before searching
6114 * the element in log(N) time */
6115 if (reverse) {
6116 ln = start == 0 ? zsl->tail : zslGetElementByRank(zsl, llen-start);
6117 } else {
6118 ln = start == 0 ?
6119 zsl->header->forward[0] : zslGetElementByRank(zsl, start+1);
6120 }
cc812361 6121
dd88747b 6122 /* Return the result in form of a multi-bulk reply */
6123 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",
6124 withscores ? (rangelen*2) : rangelen));
6125 for (j = 0; j < rangelen; j++) {
6126 ele = ln->obj;
6127 addReplyBulk(c,ele);
6128 if (withscores)
6129 addReplyDouble(c,ln->score);
6130 ln = reverse ? ln->backward : ln->forward[0];
cc812361 6131 }
6132}
6133
e3870fab 6134static void zrangeCommand(redisClient *c) {
6135 zrangeGenericCommand(c,0);
6136}
6137
6138static void zrevrangeCommand(redisClient *c) {
6139 zrangeGenericCommand(c,1);
6140}
6141
f44dd428 6142/* This command implements both ZRANGEBYSCORE and ZCOUNT.
6143 * If justcount is non-zero, just the count is returned. */
6144static void genericZrangebyscoreCommand(redisClient *c, int justcount) {
50c55df5 6145 robj *o;
f44dd428 6146 double min, max;
6147 int minex = 0, maxex = 0; /* are min or max exclusive? */
80181f78 6148 int offset = 0, limit = -1;
0500ef27
SH
6149 int withscores = 0;
6150 int badsyntax = 0;
6151
f44dd428 6152 /* Parse the min-max interval. If one of the values is prefixed
6153 * by the "(" character, it's considered "open". For instance
6154 * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max
6155 * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */
6156 if (((char*)c->argv[2]->ptr)[0] == '(') {
6157 min = strtod((char*)c->argv[2]->ptr+1,NULL);
6158 minex = 1;
6159 } else {
6160 min = strtod(c->argv[2]->ptr,NULL);
6161 }
6162 if (((char*)c->argv[3]->ptr)[0] == '(') {
6163 max = strtod((char*)c->argv[3]->ptr+1,NULL);
6164 maxex = 1;
6165 } else {
6166 max = strtod(c->argv[3]->ptr,NULL);
6167 }
6168
6169 /* Parse "WITHSCORES": note that if the command was called with
6170 * the name ZCOUNT then we are sure that c->argc == 4, so we'll never
6171 * enter the following paths to parse WITHSCORES and LIMIT. */
0500ef27 6172 if (c->argc == 5 || c->argc == 8) {
3a3978b1 6173 if (strcasecmp(c->argv[c->argc-1]->ptr,"withscores") == 0)
6174 withscores = 1;
6175 else
6176 badsyntax = 1;
0500ef27 6177 }
3a3978b1 6178 if (c->argc != (4 + withscores) && c->argc != (7 + withscores))
0500ef27 6179 badsyntax = 1;
0500ef27 6180 if (badsyntax) {
454d4e43 6181 addReplySds(c,
6182 sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
80181f78 6183 return;
0500ef27
SH
6184 }
6185
f44dd428 6186 /* Parse "LIMIT" */
0500ef27 6187 if (c->argc == (7 + withscores) && strcasecmp(c->argv[4]->ptr,"limit")) {
80181f78 6188 addReply(c,shared.syntaxerr);
6189 return;
0500ef27 6190 } else if (c->argc == (7 + withscores)) {
80181f78 6191 offset = atoi(c->argv[5]->ptr);
6192 limit = atoi(c->argv[6]->ptr);
0b13687c 6193 if (offset < 0) offset = 0;
80181f78 6194 }
50c55df5 6195
f44dd428 6196 /* Ok, lookup the key and get the range */
50c55df5 6197 o = lookupKeyRead(c->db,c->argv[1]);
6198 if (o == NULL) {
4e27f268 6199 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
50c55df5 6200 } else {
6201 if (o->type != REDIS_ZSET) {
6202 addReply(c,shared.wrongtypeerr);
6203 } else {
6204 zset *zsetobj = o->ptr;
6205 zskiplist *zsl = zsetobj->zsl;
6206 zskiplistNode *ln;
f44dd428 6207 robj *ele, *lenobj = NULL;
6208 unsigned long rangelen = 0;
50c55df5 6209
f44dd428 6210 /* Get the first node with the score >= min, or with
6211 * score > min if 'minex' is true. */
50c55df5 6212 ln = zslFirstWithScore(zsl,min);
f44dd428 6213 while (minex && ln && ln->score == min) ln = ln->forward[0];
6214
50c55df5 6215 if (ln == NULL) {
6216 /* No element matching the speciifed interval */
f44dd428 6217 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
50c55df5 6218 return;
6219 }
6220
6221 /* We don't know in advance how many matching elements there
6222 * are in the list, so we push this object that will represent
6223 * the multi-bulk length in the output buffer, and will "fix"
6224 * it later */
f44dd428 6225 if (!justcount) {
6226 lenobj = createObject(REDIS_STRING,NULL);
6227 addReply(c,lenobj);
6228 decrRefCount(lenobj);
6229 }
50c55df5 6230
f44dd428 6231 while(ln && (maxex ? (ln->score < max) : (ln->score <= max))) {
80181f78 6232 if (offset) {
6233 offset--;
6234 ln = ln->forward[0];
6235 continue;
6236 }
6237 if (limit == 0) break;
f44dd428 6238 if (!justcount) {
6239 ele = ln->obj;
dd88747b 6240 addReplyBulk(c,ele);
f44dd428 6241 if (withscores)
6242 addReplyDouble(c,ln->score);
6243 }
50c55df5 6244 ln = ln->forward[0];
6245 rangelen++;
80181f78 6246 if (limit > 0) limit--;
50c55df5 6247 }
f44dd428 6248 if (justcount) {
482b672d 6249 addReplyLongLong(c,(long)rangelen);
f44dd428 6250 } else {
6251 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",
6252 withscores ? (rangelen*2) : rangelen);
6253 }
50c55df5 6254 }
6255 }
6256}
6257
f44dd428 6258static void zrangebyscoreCommand(redisClient *c) {
6259 genericZrangebyscoreCommand(c,0);
6260}
6261
6262static void zcountCommand(redisClient *c) {
6263 genericZrangebyscoreCommand(c,1);
6264}
6265
3c41331e 6266static void zcardCommand(redisClient *c) {
e197b441 6267 robj *o;
6268 zset *zs;
dd88747b 6269
6270 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6271 checkType(c,o,REDIS_ZSET)) return;
6272
6273 zs = o->ptr;
6274 addReplyUlong(c,zs->zsl->length);
e197b441 6275}
6276
6e333bbe 6277static void zscoreCommand(redisClient *c) {
6278 robj *o;
6279 zset *zs;
dd88747b 6280 dictEntry *de;
6281
6282 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6283 checkType(c,o,REDIS_ZSET)) return;
6284
6285 zs = o->ptr;
6286 de = dictFind(zs->dict,c->argv[2]);
6287 if (!de) {
96d8b4ee 6288 addReply(c,shared.nullbulk);
6e333bbe 6289 } else {
dd88747b 6290 double *score = dictGetEntryVal(de);
6e333bbe 6291
dd88747b 6292 addReplyDouble(c,*score);
6e333bbe 6293 }
6294}
6295
798d9e55 6296static void zrankGenericCommand(redisClient *c, int reverse) {
69d95c3e 6297 robj *o;
dd88747b 6298 zset *zs;
6299 zskiplist *zsl;
6300 dictEntry *de;
6301 unsigned long rank;
6302 double *score;
6303
6304 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6305 checkType(c,o,REDIS_ZSET)) return;
6306
6307 zs = o->ptr;
6308 zsl = zs->zsl;
6309 de = dictFind(zs->dict,c->argv[2]);
6310 if (!de) {
69d95c3e
PN
6311 addReply(c,shared.nullbulk);
6312 return;
6313 }
69d95c3e 6314
dd88747b 6315 score = dictGetEntryVal(de);
6316 rank = zslGetRank(zsl, *score, c->argv[2]);
6317 if (rank) {
6318 if (reverse) {
482b672d 6319 addReplyLongLong(c, zsl->length - rank);
27b0ccca 6320 } else {
482b672d 6321 addReplyLongLong(c, rank-1);
69d95c3e 6322 }
dd88747b 6323 } else {
6324 addReply(c,shared.nullbulk);
978c2c94 6325 }
6326}
6327
798d9e55
PN
6328static void zrankCommand(redisClient *c) {
6329 zrankGenericCommand(c, 0);
6330}
6331
6332static void zrevrankCommand(redisClient *c) {
6333 zrankGenericCommand(c, 1);
6334}
6335
7fb16bac
PN
6336/* ========================= Hashes utility functions ======================= */
6337#define REDIS_HASH_KEY 1
6338#define REDIS_HASH_VALUE 2
978c2c94 6339
7fb16bac
PN
6340/* Check the length of a number of objects to see if we need to convert a
6341 * zipmap to a real hash. Note that we only check string encoded objects
6342 * as their string length can be queried in constant time. */
6343static void hashTryConversion(robj *subject, robj **argv, int start, int end) {
6344 int i;
6345 if (subject->encoding != REDIS_ENCODING_ZIPMAP) return;
978c2c94 6346
7fb16bac
PN
6347 for (i = start; i <= end; i++) {
6348 if (argv[i]->encoding == REDIS_ENCODING_RAW &&
6349 sdslen(argv[i]->ptr) > server.hash_max_zipmap_value)
6350 {
6351 convertToRealHash(subject);
978c2c94 6352 return;
6353 }
6354 }
7fb16bac 6355}
bae2c7ec 6356
97224de7
PN
6357/* Encode given objects in-place when the hash uses a dict. */
6358static void hashTryObjectEncoding(robj *subject, robj **o1, robj **o2) {
6359 if (subject->encoding == REDIS_ENCODING_HT) {
3f973463
PN
6360 if (o1) *o1 = tryObjectEncoding(*o1);
6361 if (o2) *o2 = tryObjectEncoding(*o2);
97224de7
PN
6362 }
6363}
6364
7fb16bac 6365/* Get the value from a hash identified by key. Returns either a string
a3f3af86
PN
6366 * object or NULL if the value cannot be found. The refcount of the object
6367 * is always increased by 1 when the value was found. */
7fb16bac
PN
6368static robj *hashGet(robj *o, robj *key) {
6369 robj *value = NULL;
978c2c94 6370 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
7fb16bac
PN
6371 unsigned char *v;
6372 unsigned int vlen;
6373 key = getDecodedObject(key);
6374 if (zipmapGet(o->ptr,key->ptr,sdslen(key->ptr),&v,&vlen)) {
6375 value = createStringObject((char*)v,vlen);
6376 }
6377 decrRefCount(key);
6378 } else {
6379 dictEntry *de = dictFind(o->ptr,key);
6380 if (de != NULL) {
6381 value = dictGetEntryVal(de);
a3f3af86 6382 incrRefCount(value);
7fb16bac
PN
6383 }
6384 }
6385 return value;
6386}
978c2c94 6387
7fb16bac
PN
6388/* Test if the key exists in the given hash. Returns 1 if the key
6389 * exists and 0 when it doesn't. */
6390static int hashExists(robj *o, robj *key) {
6391 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6392 key = getDecodedObject(key);
6393 if (zipmapExists(o->ptr,key->ptr,sdslen(key->ptr))) {
6394 decrRefCount(key);
6395 return 1;
6396 }
6397 decrRefCount(key);
6398 } else {
6399 if (dictFind(o->ptr,key) != NULL) {
6400 return 1;
6401 }
6402 }
6403 return 0;
6404}
bae2c7ec 6405
7fb16bac
PN
6406/* Add an element, discard the old if the key already exists.
6407 * Return 0 on insert and 1 on update. */
feb8d7e6 6408static int hashSet(robj *o, robj *key, robj *value) {
7fb16bac
PN
6409 int update = 0;
6410 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6411 key = getDecodedObject(key);
6412 value = getDecodedObject(value);
6413 o->ptr = zipmapSet(o->ptr,
6414 key->ptr,sdslen(key->ptr),
6415 value->ptr,sdslen(value->ptr), &update);
6416 decrRefCount(key);
6417 decrRefCount(value);
6418
6419 /* Check if the zipmap needs to be upgraded to a real hash table */
6420 if (zipmapLen(o->ptr) > server.hash_max_zipmap_entries)
bae2c7ec 6421 convertToRealHash(o);
978c2c94 6422 } else {
7fb16bac
PN
6423 if (dictReplace(o->ptr,key,value)) {
6424 /* Insert */
6425 incrRefCount(key);
978c2c94 6426 } else {
7fb16bac 6427 /* Update */
978c2c94 6428 update = 1;
6429 }
7fb16bac 6430 incrRefCount(value);
978c2c94 6431 }
7fb16bac 6432 return update;
978c2c94 6433}
6434
7fb16bac
PN
6435/* Delete an element from a hash.
6436 * Return 1 on deleted and 0 on not found. */
6437static int hashDelete(robj *o, robj *key) {
6438 int deleted = 0;
6439 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6440 key = getDecodedObject(key);
6441 o->ptr = zipmapDel(o->ptr,key->ptr,sdslen(key->ptr), &deleted);
6442 decrRefCount(key);
6443 } else {
6444 deleted = dictDelete((dict*)o->ptr,key) == DICT_OK;
6445 /* Always check if the dictionary needs a resize after a delete. */
6446 if (deleted && htNeedsResize(o->ptr)) dictResize(o->ptr);
d33278d1 6447 }
7fb16bac
PN
6448 return deleted;
6449}
d33278d1 6450
7fb16bac 6451/* Return the number of elements in a hash. */
c811bb38 6452static unsigned long hashLength(robj *o) {
7fb16bac
PN
6453 return (o->encoding == REDIS_ENCODING_ZIPMAP) ?
6454 zipmapLen((unsigned char*)o->ptr) : dictSize((dict*)o->ptr);
6455}
6456
6457/* Structure to hold hash iteration abstration. Note that iteration over
6458 * hashes involves both fields and values. Because it is possible that
6459 * not both are required, store pointers in the iterator to avoid
6460 * unnecessary memory allocation for fields/values. */
6461typedef struct {
6462 int encoding;
6463 unsigned char *zi;
6464 unsigned char *zk, *zv;
6465 unsigned int zklen, zvlen;
6466
6467 dictIterator *di;
6468 dictEntry *de;
6469} hashIterator;
6470
c44d3b56
PN
6471static hashIterator *hashInitIterator(robj *subject) {
6472 hashIterator *hi = zmalloc(sizeof(hashIterator));
7fb16bac
PN
6473 hi->encoding = subject->encoding;
6474 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6475 hi->zi = zipmapRewind(subject->ptr);
6476 } else if (hi->encoding == REDIS_ENCODING_HT) {
6477 hi->di = dictGetIterator(subject->ptr);
d33278d1 6478 } else {
7fb16bac 6479 redisAssert(NULL);
d33278d1 6480 }
c44d3b56 6481 return hi;
7fb16bac 6482}
d33278d1 6483
7fb16bac
PN
6484static void hashReleaseIterator(hashIterator *hi) {
6485 if (hi->encoding == REDIS_ENCODING_HT) {
6486 dictReleaseIterator(hi->di);
d33278d1 6487 }
c44d3b56 6488 zfree(hi);
7fb16bac 6489}
d33278d1 6490
7fb16bac
PN
6491/* Move to the next entry in the hash. Return REDIS_OK when the next entry
6492 * could be found and REDIS_ERR when the iterator reaches the end. */
c811bb38 6493static int hashNext(hashIterator *hi) {
7fb16bac
PN
6494 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6495 if ((hi->zi = zipmapNext(hi->zi, &hi->zk, &hi->zklen,
6496 &hi->zv, &hi->zvlen)) == NULL) return REDIS_ERR;
6497 } else {
6498 if ((hi->de = dictNext(hi->di)) == NULL) return REDIS_ERR;
6499 }
6500 return REDIS_OK;
6501}
d33278d1 6502
0c390abc 6503/* Get key or value object at current iteration position.
a3f3af86 6504 * This increases the refcount of the field object by 1. */
c811bb38 6505static robj *hashCurrent(hashIterator *hi, int what) {
7fb16bac
PN
6506 robj *o;
6507 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6508 if (what & REDIS_HASH_KEY) {
6509 o = createStringObject((char*)hi->zk,hi->zklen);
6510 } else {
6511 o = createStringObject((char*)hi->zv,hi->zvlen);
d33278d1 6512 }
d33278d1 6513 } else {
7fb16bac
PN
6514 if (what & REDIS_HASH_KEY) {
6515 o = dictGetEntryKey(hi->de);
6516 } else {
6517 o = dictGetEntryVal(hi->de);
d33278d1 6518 }
a3f3af86 6519 incrRefCount(o);
d33278d1 6520 }
7fb16bac 6521 return o;
d33278d1
PN
6522}
6523
7fb16bac
PN
6524static robj *hashLookupWriteOrCreate(redisClient *c, robj *key) {
6525 robj *o = lookupKeyWrite(c->db,key);
01426b05
PN
6526 if (o == NULL) {
6527 o = createHashObject();
7fb16bac
PN
6528 dictAdd(c->db->dict,key,o);
6529 incrRefCount(key);
01426b05
PN
6530 } else {
6531 if (o->type != REDIS_HASH) {
6532 addReply(c,shared.wrongtypeerr);
7fb16bac 6533 return NULL;
01426b05
PN
6534 }
6535 }
7fb16bac
PN
6536 return o;
6537}
01426b05 6538
7fb16bac
PN
6539/* ============================= Hash commands ============================== */
6540static void hsetCommand(redisClient *c) {
6e9e463f 6541 int update;
7fb16bac 6542 robj *o;
bbe025e0 6543
7fb16bac
PN
6544 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6545 hashTryConversion(o,c->argv,2,3);
97224de7 6546 hashTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
feb8d7e6 6547 update = hashSet(o,c->argv[2],c->argv[3]);
6e9e463f 6548 addReply(c, update ? shared.czero : shared.cone);
7fb16bac
PN
6549 server.dirty++;
6550}
01426b05 6551
1f1c7695
PN
6552static void hsetnxCommand(redisClient *c) {
6553 robj *o;
6554 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6555 hashTryConversion(o,c->argv,2,3);
6556
6557 if (hashExists(o, c->argv[2])) {
6558 addReply(c, shared.czero);
01426b05 6559 } else {
97224de7 6560 hashTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
feb8d7e6 6561 hashSet(o,c->argv[2],c->argv[3]);
1f1c7695
PN
6562 addReply(c, shared.cone);
6563 server.dirty++;
6564 }
6565}
01426b05 6566
7fb16bac
PN
6567static void hmsetCommand(redisClient *c) {
6568 int i;
6569 robj *o;
01426b05 6570
7fb16bac
PN
6571 if ((c->argc % 2) == 1) {
6572 addReplySds(c,sdsnew("-ERR wrong number of arguments for HMSET\r\n"));
6573 return;
6574 }
01426b05 6575
7fb16bac
PN
6576 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6577 hashTryConversion(o,c->argv,2,c->argc-1);
6578 for (i = 2; i < c->argc; i += 2) {
97224de7 6579 hashTryObjectEncoding(o,&c->argv[i], &c->argv[i+1]);
feb8d7e6 6580 hashSet(o,c->argv[i],c->argv[i+1]);
7fb16bac
PN
6581 }
6582 addReply(c, shared.ok);
edc2f63a 6583 server.dirty++;
7fb16bac
PN
6584}
6585
6586static void hincrbyCommand(redisClient *c) {
6587 long long value, incr;
6588 robj *o, *current, *new;
6589
bd79a6bd 6590 if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return;
7fb16bac
PN
6591 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6592 if ((current = hashGet(o,c->argv[2])) != NULL) {
946342c1
PN
6593 if (getLongLongFromObjectOrReply(c,current,&value,
6594 "hash value is not an integer") != REDIS_OK) {
6595 decrRefCount(current);
6596 return;
6597 }
a3f3af86 6598 decrRefCount(current);
7fb16bac
PN
6599 } else {
6600 value = 0;
01426b05
PN
6601 }
6602
7fb16bac 6603 value += incr;
3f973463
PN
6604 new = createStringObjectFromLongLong(value);
6605 hashTryObjectEncoding(o,&c->argv[2],NULL);
feb8d7e6 6606 hashSet(o,c->argv[2],new);
7fb16bac
PN
6607 decrRefCount(new);
6608 addReplyLongLong(c,value);
01426b05 6609 server.dirty++;
01426b05
PN
6610}
6611
978c2c94 6612static void hgetCommand(redisClient *c) {
7fb16bac 6613 robj *o, *value;
dd88747b 6614 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6615 checkType(c,o,REDIS_HASH)) return;
6616
7fb16bac
PN
6617 if ((value = hashGet(o,c->argv[2])) != NULL) {
6618 addReplyBulk(c,value);
a3f3af86 6619 decrRefCount(value);
dd88747b 6620 } else {
7fb16bac 6621 addReply(c,shared.nullbulk);
69d95c3e 6622 }
69d95c3e
PN
6623}
6624
09aeb579
PN
6625static void hmgetCommand(redisClient *c) {
6626 int i;
7fb16bac
PN
6627 robj *o, *value;
6628 o = lookupKeyRead(c->db,c->argv[1]);
6629 if (o != NULL && o->type != REDIS_HASH) {
6630 addReply(c,shared.wrongtypeerr);
09aeb579
PN
6631 }
6632
7fb16bac
PN
6633 /* Note the check for o != NULL happens inside the loop. This is
6634 * done because objects that cannot be found are considered to be
6635 * an empty hash. The reply should then be a series of NULLs. */
09aeb579 6636 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-2));
7fb16bac
PN
6637 for (i = 2; i < c->argc; i++) {
6638 if (o != NULL && (value = hashGet(o,c->argv[i])) != NULL) {
6639 addReplyBulk(c,value);
a3f3af86 6640 decrRefCount(value);
7fb16bac
PN
6641 } else {
6642 addReply(c,shared.nullbulk);
09aeb579
PN
6643 }
6644 }
6645}
6646
07efaf74 6647static void hdelCommand(redisClient *c) {
dd88747b 6648 robj *o;
dd88747b 6649 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
6650 checkType(c,o,REDIS_HASH)) return;
07efaf74 6651
7fb16bac
PN
6652 if (hashDelete(o,c->argv[2])) {
6653 if (hashLength(o) == 0) deleteKey(c->db,c->argv[1]);
6654 addReply(c,shared.cone);
6655 server.dirty++;
dd88747b 6656 } else {
7fb16bac 6657 addReply(c,shared.czero);
07efaf74 6658 }
6659}
6660
92b27fe9 6661static void hlenCommand(redisClient *c) {
6662 robj *o;
dd88747b 6663 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
92b27fe9 6664 checkType(c,o,REDIS_HASH)) return;
6665
7fb16bac 6666 addReplyUlong(c,hashLength(o));
92b27fe9 6667}
6668
78409a0f 6669static void genericHgetallCommand(redisClient *c, int flags) {
7fb16bac 6670 robj *o, *lenobj, *obj;
78409a0f 6671 unsigned long count = 0;
c44d3b56 6672 hashIterator *hi;
78409a0f 6673
4e27f268 6674 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
78409a0f 6675 || checkType(c,o,REDIS_HASH)) return;
6676
6677 lenobj = createObject(REDIS_STRING,NULL);
6678 addReply(c,lenobj);
6679 decrRefCount(lenobj);
6680
c44d3b56
PN
6681 hi = hashInitIterator(o);
6682 while (hashNext(hi) != REDIS_ERR) {
7fb16bac 6683 if (flags & REDIS_HASH_KEY) {
c44d3b56 6684 obj = hashCurrent(hi,REDIS_HASH_KEY);
7fb16bac 6685 addReplyBulk(c,obj);
a3f3af86 6686 decrRefCount(obj);
7fb16bac 6687 count++;
78409a0f 6688 }
7fb16bac 6689 if (flags & REDIS_HASH_VALUE) {
c44d3b56 6690 obj = hashCurrent(hi,REDIS_HASH_VALUE);
7fb16bac 6691 addReplyBulk(c,obj);
a3f3af86 6692 decrRefCount(obj);
7fb16bac 6693 count++;
78409a0f 6694 }
78409a0f 6695 }
c44d3b56 6696 hashReleaseIterator(hi);
7fb16bac 6697
78409a0f 6698 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",count);
6699}
6700
6701static void hkeysCommand(redisClient *c) {
7fb16bac 6702 genericHgetallCommand(c,REDIS_HASH_KEY);
78409a0f 6703}
6704
6705static void hvalsCommand(redisClient *c) {
7fb16bac 6706 genericHgetallCommand(c,REDIS_HASH_VALUE);
78409a0f 6707}
6708
6709static void hgetallCommand(redisClient *c) {
7fb16bac 6710 genericHgetallCommand(c,REDIS_HASH_KEY|REDIS_HASH_VALUE);
78409a0f 6711}
6712
a86f14b1 6713static void hexistsCommand(redisClient *c) {
6714 robj *o;
a86f14b1 6715 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6716 checkType(c,o,REDIS_HASH)) return;
6717
7fb16bac 6718 addReply(c, hashExists(o,c->argv[2]) ? shared.cone : shared.czero);
a86f14b1 6719}
6720
ada386b2 6721static void convertToRealHash(robj *o) {
6722 unsigned char *key, *val, *p, *zm = o->ptr;
6723 unsigned int klen, vlen;
6724 dict *dict = dictCreate(&hashDictType,NULL);
6725
6726 assert(o->type == REDIS_HASH && o->encoding != REDIS_ENCODING_HT);
6727 p = zipmapRewind(zm);
6728 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
6729 robj *keyobj, *valobj;
6730
6731 keyobj = createStringObject((char*)key,klen);
6732 valobj = createStringObject((char*)val,vlen);
05df7621 6733 keyobj = tryObjectEncoding(keyobj);
6734 valobj = tryObjectEncoding(valobj);
ada386b2 6735 dictAdd(dict,keyobj,valobj);
6736 }
6737 o->encoding = REDIS_ENCODING_HT;
6738 o->ptr = dict;
6739 zfree(zm);
6740}
6741
6b47e12e 6742/* ========================= Non type-specific commands ==================== */
6743
ed9b544e 6744static void flushdbCommand(redisClient *c) {
ca37e9cd 6745 server.dirty += dictSize(c->db->dict);
3305306f 6746 dictEmpty(c->db->dict);
6747 dictEmpty(c->db->expires);
ed9b544e 6748 addReply(c,shared.ok);
ed9b544e 6749}
6750
6751static void flushallCommand(redisClient *c) {
ca37e9cd 6752 server.dirty += emptyDb();
ed9b544e 6753 addReply(c,shared.ok);
500ece7c 6754 if (server.bgsavechildpid != -1) {
6755 kill(server.bgsavechildpid,SIGKILL);
6756 rdbRemoveTempFile(server.bgsavechildpid);
6757 }
f78fd11b 6758 rdbSave(server.dbfilename);
ca37e9cd 6759 server.dirty++;
ed9b544e 6760}
6761
56906eef 6762static redisSortOperation *createSortOperation(int type, robj *pattern) {
ed9b544e 6763 redisSortOperation *so = zmalloc(sizeof(*so));
ed9b544e 6764 so->type = type;
6765 so->pattern = pattern;
6766 return so;
6767}
6768
6769/* Return the value associated to the key with a name obtained
55017f9d
PN
6770 * substituting the first occurence of '*' in 'pattern' with 'subst'.
6771 * The returned object will always have its refcount increased by 1
6772 * when it is non-NULL. */
56906eef 6773static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
6d7d1370 6774 char *p, *f;
ed9b544e 6775 sds spat, ssub;
6d7d1370
PN
6776 robj keyobj, fieldobj, *o;
6777 int prefixlen, sublen, postfixlen, fieldlen;
ed9b544e 6778 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
6779 struct {
f1017b3f 6780 long len;
6781 long free;
ed9b544e 6782 char buf[REDIS_SORTKEY_MAX+1];
6d7d1370 6783 } keyname, fieldname;
ed9b544e 6784
28173a49 6785 /* If the pattern is "#" return the substitution object itself in order
6786 * to implement the "SORT ... GET #" feature. */
6787 spat = pattern->ptr;
6788 if (spat[0] == '#' && spat[1] == '\0') {
55017f9d 6789 incrRefCount(subst);
28173a49 6790 return subst;
6791 }
6792
6793 /* The substitution object may be specially encoded. If so we create
9d65a1bb 6794 * a decoded object on the fly. Otherwise getDecodedObject will just
6795 * increment the ref count, that we'll decrement later. */
6796 subst = getDecodedObject(subst);
942a3961 6797
ed9b544e 6798 ssub = subst->ptr;
6799 if (sdslen(spat)+sdslen(ssub)-1 > REDIS_SORTKEY_MAX) return NULL;
6800 p = strchr(spat,'*');
ed5a857a 6801 if (!p) {
6802 decrRefCount(subst);
6803 return NULL;
6804 }
ed9b544e 6805
6d7d1370
PN
6806 /* Find out if we're dealing with a hash dereference. */
6807 if ((f = strstr(p+1, "->")) != NULL) {
6808 fieldlen = sdslen(spat)-(f-spat);
6809 /* this also copies \0 character */
6810 memcpy(fieldname.buf,f+2,fieldlen-1);
6811 fieldname.len = fieldlen-2;
6812 } else {
6813 fieldlen = 0;
6814 }
6815
ed9b544e 6816 prefixlen = p-spat;
6817 sublen = sdslen(ssub);
6d7d1370 6818 postfixlen = sdslen(spat)-(prefixlen+1)-fieldlen;
ed9b544e 6819 memcpy(keyname.buf,spat,prefixlen);
6820 memcpy(keyname.buf+prefixlen,ssub,sublen);
6821 memcpy(keyname.buf+prefixlen+sublen,p+1,postfixlen);
6822 keyname.buf[prefixlen+sublen+postfixlen] = '\0';
6823 keyname.len = prefixlen+sublen+postfixlen;
942a3961 6824 decrRefCount(subst);
6825
6d7d1370
PN
6826 /* Lookup substituted key */
6827 initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2));
6828 o = lookupKeyRead(db,&keyobj);
55017f9d
PN
6829 if (o == NULL) return NULL;
6830
6831 if (fieldlen > 0) {
6832 if (o->type != REDIS_HASH || fieldname.len < 1) return NULL;
6d7d1370 6833
705dad38
PN
6834 /* Retrieve value from hash by the field name. This operation
6835 * already increases the refcount of the returned object. */
6d7d1370
PN
6836 initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(long)*2));
6837 o = hashGet(o, &fieldobj);
705dad38 6838 } else {
55017f9d 6839 if (o->type != REDIS_STRING) return NULL;
b6f07345 6840
705dad38
PN
6841 /* Every object that this function returns needs to have its refcount
6842 * increased. sortCommand decreases it again. */
6843 incrRefCount(o);
6d7d1370
PN
6844 }
6845
6846 return o;
ed9b544e 6847}
6848
6849/* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
6850 * the additional parameter is not standard but a BSD-specific we have to
6851 * pass sorting parameters via the global 'server' structure */
6852static int sortCompare(const void *s1, const void *s2) {
6853 const redisSortObject *so1 = s1, *so2 = s2;
6854 int cmp;
6855
6856 if (!server.sort_alpha) {
6857 /* Numeric sorting. Here it's trivial as we precomputed scores */
6858 if (so1->u.score > so2->u.score) {
6859 cmp = 1;
6860 } else if (so1->u.score < so2->u.score) {
6861 cmp = -1;
6862 } else {
6863 cmp = 0;
6864 }
6865 } else {
6866 /* Alphanumeric sorting */
6867 if (server.sort_bypattern) {
6868 if (!so1->u.cmpobj || !so2->u.cmpobj) {
6869 /* At least one compare object is NULL */
6870 if (so1->u.cmpobj == so2->u.cmpobj)
6871 cmp = 0;
6872 else if (so1->u.cmpobj == NULL)
6873 cmp = -1;
6874 else
6875 cmp = 1;
6876 } else {
6877 /* We have both the objects, use strcoll */
6878 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
6879 }
6880 } else {
08ee9b57 6881 /* Compare elements directly. */
6882 cmp = compareStringObjects(so1->obj,so2->obj);
ed9b544e 6883 }
6884 }
6885 return server.sort_desc ? -cmp : cmp;
6886}
6887
6888/* The SORT command is the most complex command in Redis. Warning: this code
6889 * is optimized for speed and a bit less for readability */
6890static void sortCommand(redisClient *c) {
ed9b544e 6891 list *operations;
6892 int outputlen = 0;
6893 int desc = 0, alpha = 0;
6894 int limit_start = 0, limit_count = -1, start, end;
6895 int j, dontsort = 0, vectorlen;
6896 int getop = 0; /* GET operation counter */
443c6409 6897 robj *sortval, *sortby = NULL, *storekey = NULL;
ed9b544e 6898 redisSortObject *vector; /* Resulting vector to sort */
6899
6900 /* Lookup the key to sort. It must be of the right types */
3305306f 6901 sortval = lookupKeyRead(c->db,c->argv[1]);
6902 if (sortval == NULL) {
4e27f268 6903 addReply(c,shared.emptymultibulk);
ed9b544e 6904 return;
6905 }
a5eb649b 6906 if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
6907 sortval->type != REDIS_ZSET)
6908 {
c937aa89 6909 addReply(c,shared.wrongtypeerr);
ed9b544e 6910 return;
6911 }
6912
6913 /* Create a list of operations to perform for every sorted element.
6914 * Operations can be GET/DEL/INCR/DECR */
6915 operations = listCreate();
092dac2a 6916 listSetFreeMethod(operations,zfree);
ed9b544e 6917 j = 2;
6918
6919 /* Now we need to protect sortval incrementing its count, in the future
6920 * SORT may have options able to overwrite/delete keys during the sorting
6921 * and the sorted key itself may get destroied */
6922 incrRefCount(sortval);
6923
6924 /* The SORT command has an SQL-alike syntax, parse it */
6925 while(j < c->argc) {
6926 int leftargs = c->argc-j-1;
6927 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
6928 desc = 0;
6929 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
6930 desc = 1;
6931 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
6932 alpha = 1;
6933 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
6934 limit_start = atoi(c->argv[j+1]->ptr);
6935 limit_count = atoi(c->argv[j+2]->ptr);
6936 j+=2;
443c6409 6937 } else if (!strcasecmp(c->argv[j]->ptr,"store") && leftargs >= 1) {
6938 storekey = c->argv[j+1];
6939 j++;
ed9b544e 6940 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
6941 sortby = c->argv[j+1];
6942 /* If the BY pattern does not contain '*', i.e. it is constant,
6943 * we don't need to sort nor to lookup the weight keys. */
6944 if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
6945 j++;
6946 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
6947 listAddNodeTail(operations,createSortOperation(
6948 REDIS_SORT_GET,c->argv[j+1]));
6949 getop++;
6950 j++;
ed9b544e 6951 } else {
6952 decrRefCount(sortval);
6953 listRelease(operations);
c937aa89 6954 addReply(c,shared.syntaxerr);
ed9b544e 6955 return;
6956 }
6957 j++;
6958 }
6959
6960 /* Load the sorting vector with all the objects to sort */
a5eb649b 6961 switch(sortval->type) {
6962 case REDIS_LIST: vectorlen = listLength((list*)sortval->ptr); break;
6963 case REDIS_SET: vectorlen = dictSize((dict*)sortval->ptr); break;
6964 case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
f83c6cb5 6965 default: vectorlen = 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
a5eb649b 6966 }
ed9b544e 6967 vector = zmalloc(sizeof(redisSortObject)*vectorlen);
ed9b544e 6968 j = 0;
a5eb649b 6969
ed9b544e 6970 if (sortval->type == REDIS_LIST) {
6971 list *list = sortval->ptr;
6208b3a7 6972 listNode *ln;
c7df85a4 6973 listIter li;
6208b3a7 6974
c7df85a4 6975 listRewind(list,&li);
6976 while((ln = listNext(&li))) {
ed9b544e 6977 robj *ele = ln->value;
6978 vector[j].obj = ele;
6979 vector[j].u.score = 0;
6980 vector[j].u.cmpobj = NULL;
ed9b544e 6981 j++;
6982 }
6983 } else {
a5eb649b 6984 dict *set;
ed9b544e 6985 dictIterator *di;
6986 dictEntry *setele;
6987
a5eb649b 6988 if (sortval->type == REDIS_SET) {
6989 set = sortval->ptr;
6990 } else {
6991 zset *zs = sortval->ptr;
6992 set = zs->dict;
6993 }
6994
ed9b544e 6995 di = dictGetIterator(set);
ed9b544e 6996 while((setele = dictNext(di)) != NULL) {
6997 vector[j].obj = dictGetEntryKey(setele);
6998 vector[j].u.score = 0;
6999 vector[j].u.cmpobj = NULL;
7000 j++;
7001 }
7002 dictReleaseIterator(di);
7003 }
dfc5e96c 7004 redisAssert(j == vectorlen);
ed9b544e 7005
7006 /* Now it's time to load the right scores in the sorting vector */
7007 if (dontsort == 0) {
7008 for (j = 0; j < vectorlen; j++) {
6d7d1370 7009 robj *byval;
ed9b544e 7010 if (sortby) {
6d7d1370 7011 /* lookup value to sort by */
3305306f 7012 byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
705dad38 7013 if (!byval) continue;
ed9b544e 7014 } else {
6d7d1370
PN
7015 /* use object itself to sort by */
7016 byval = vector[j].obj;
7017 }
7018
7019 if (alpha) {
08ee9b57 7020 if (sortby) vector[j].u.cmpobj = getDecodedObject(byval);
6d7d1370
PN
7021 } else {
7022 if (byval->encoding == REDIS_ENCODING_RAW) {
7023 vector[j].u.score = strtod(byval->ptr,NULL);
16fa22f1 7024 } else if (byval->encoding == REDIS_ENCODING_INT) {
6d7d1370
PN
7025 /* Don't need to decode the object if it's
7026 * integer-encoded (the only encoding supported) so
7027 * far. We can just cast it */
16fa22f1
PN
7028 vector[j].u.score = (long)byval->ptr;
7029 } else {
7030 redisAssert(1 != 1);
942a3961 7031 }
ed9b544e 7032 }
6d7d1370 7033
705dad38
PN
7034 /* when the object was retrieved using lookupKeyByPattern,
7035 * its refcount needs to be decreased. */
7036 if (sortby) {
7037 decrRefCount(byval);
ed9b544e 7038 }
7039 }
7040 }
7041
7042 /* We are ready to sort the vector... perform a bit of sanity check
7043 * on the LIMIT option too. We'll use a partial version of quicksort. */
7044 start = (limit_start < 0) ? 0 : limit_start;
7045 end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
7046 if (start >= vectorlen) {
7047 start = vectorlen-1;
7048 end = vectorlen-2;
7049 }
7050 if (end >= vectorlen) end = vectorlen-1;
7051
7052 if (dontsort == 0) {
7053 server.sort_desc = desc;
7054 server.sort_alpha = alpha;
7055 server.sort_bypattern = sortby ? 1 : 0;
5f5b9840 7056 if (sortby && (start != 0 || end != vectorlen-1))
7057 pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
7058 else
7059 qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
ed9b544e 7060 }
7061
7062 /* Send command output to the output buffer, performing the specified
7063 * GET/DEL/INCR/DECR operations if any. */
7064 outputlen = getop ? getop*(end-start+1) : end-start+1;
443c6409 7065 if (storekey == NULL) {
7066 /* STORE option not specified, sent the sorting result to client */
7067 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen));
7068 for (j = start; j <= end; j++) {
7069 listNode *ln;
c7df85a4 7070 listIter li;
7071
dd88747b 7072 if (!getop) addReplyBulk(c,vector[j].obj);
c7df85a4 7073 listRewind(operations,&li);
7074 while((ln = listNext(&li))) {
443c6409 7075 redisSortOperation *sop = ln->value;
7076 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7077 vector[j].obj);
7078
7079 if (sop->type == REDIS_SORT_GET) {
55017f9d 7080 if (!val) {
443c6409 7081 addReply(c,shared.nullbulk);
7082 } else {
dd88747b 7083 addReplyBulk(c,val);
55017f9d 7084 decrRefCount(val);
443c6409 7085 }
7086 } else {
dfc5e96c 7087 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
443c6409 7088 }
7089 }
ed9b544e 7090 }
443c6409 7091 } else {
7092 robj *listObject = createListObject();
7093 list *listPtr = (list*) listObject->ptr;
7094
7095 /* STORE option specified, set the sorting result as a List object */
7096 for (j = start; j <= end; j++) {
7097 listNode *ln;
c7df85a4 7098 listIter li;
7099
443c6409 7100 if (!getop) {
7101 listAddNodeTail(listPtr,vector[j].obj);
7102 incrRefCount(vector[j].obj);
7103 }
c7df85a4 7104 listRewind(operations,&li);
7105 while((ln = listNext(&li))) {
443c6409 7106 redisSortOperation *sop = ln->value;
7107 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7108 vector[j].obj);
7109
7110 if (sop->type == REDIS_SORT_GET) {
55017f9d 7111 if (!val) {
443c6409 7112 listAddNodeTail(listPtr,createStringObject("",0));
7113 } else {
55017f9d
PN
7114 /* We should do a incrRefCount on val because it is
7115 * added to the list, but also a decrRefCount because
7116 * it is returned by lookupKeyByPattern. This results
7117 * in doing nothing at all. */
443c6409 7118 listAddNodeTail(listPtr,val);
443c6409 7119 }
ed9b544e 7120 } else {
dfc5e96c 7121 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
ed9b544e 7122 }
ed9b544e 7123 }
ed9b544e 7124 }
121796f7 7125 if (dictReplace(c->db->dict,storekey,listObject)) {
7126 incrRefCount(storekey);
7127 }
443c6409 7128 /* Note: we add 1 because the DB is dirty anyway since even if the
7129 * SORT result is empty a new key is set and maybe the old content
7130 * replaced. */
7131 server.dirty += 1+outputlen;
7132 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",outputlen));
ed9b544e 7133 }
7134
7135 /* Cleanup */
7136 decrRefCount(sortval);
7137 listRelease(operations);
7138 for (j = 0; j < vectorlen; j++) {
16fa22f1 7139 if (alpha && vector[j].u.cmpobj)
ed9b544e 7140 decrRefCount(vector[j].u.cmpobj);
7141 }
7142 zfree(vector);
7143}
7144
ec6c7a1d 7145/* Convert an amount of bytes into a human readable string in the form
7146 * of 100B, 2G, 100M, 4K, and so forth. */
7147static void bytesToHuman(char *s, unsigned long long n) {
7148 double d;
7149
7150 if (n < 1024) {
7151 /* Bytes */
7152 sprintf(s,"%lluB",n);
7153 return;
7154 } else if (n < (1024*1024)) {
7155 d = (double)n/(1024);
7156 sprintf(s,"%.2fK",d);
7157 } else if (n < (1024LL*1024*1024)) {
7158 d = (double)n/(1024*1024);
7159 sprintf(s,"%.2fM",d);
7160 } else if (n < (1024LL*1024*1024*1024)) {
7161 d = (double)n/(1024LL*1024*1024);
b72f6a4b 7162 sprintf(s,"%.2fG",d);
ec6c7a1d 7163 }
7164}
7165
1c85b79f 7166/* Create the string returned by the INFO command. This is decoupled
7167 * by the INFO command itself as we need to report the same information
7168 * on memory corruption problems. */
7169static sds genRedisInfoString(void) {
ed9b544e 7170 sds info;
7171 time_t uptime = time(NULL)-server.stat_starttime;
c3cb078d 7172 int j;
ec6c7a1d 7173 char hmem[64];
55a8298f 7174
b72f6a4b 7175 bytesToHuman(hmem,zmalloc_used_memory());
ed9b544e 7176 info = sdscatprintf(sdsempty(),
7177 "redis_version:%s\r\n"
f1017b3f 7178 "arch_bits:%s\r\n"
7a932b74 7179 "multiplexing_api:%s\r\n"
0d7170a4 7180 "process_id:%ld\r\n"
682ac724 7181 "uptime_in_seconds:%ld\r\n"
7182 "uptime_in_days:%ld\r\n"
ed9b544e 7183 "connected_clients:%d\r\n"
7184 "connected_slaves:%d\r\n"
f86a74e9 7185 "blocked_clients:%d\r\n"
5fba9f71 7186 "used_memory:%zu\r\n"
ec6c7a1d 7187 "used_memory_human:%s\r\n"
ed9b544e 7188 "changes_since_last_save:%lld\r\n"
be2bb6b0 7189 "bgsave_in_progress:%d\r\n"
682ac724 7190 "last_save_time:%ld\r\n"
b3fad521 7191 "bgrewriteaof_in_progress:%d\r\n"
ed9b544e 7192 "total_connections_received:%lld\r\n"
7193 "total_commands_processed:%lld\r\n"
2a6a2ed1 7194 "expired_keys:%lld\r\n"
3be2c9d7 7195 "hash_max_zipmap_entries:%zu\r\n"
7196 "hash_max_zipmap_value:%zu\r\n"
ffc6b7f8 7197 "pubsub_channels:%ld\r\n"
7198 "pubsub_patterns:%u\r\n"
7d98e08c 7199 "vm_enabled:%d\r\n"
a0f643ea 7200 "role:%s\r\n"
ed9b544e 7201 ,REDIS_VERSION,
f1017b3f 7202 (sizeof(long) == 8) ? "64" : "32",
7a932b74 7203 aeGetApiName(),
0d7170a4 7204 (long) getpid(),
a0f643ea 7205 uptime,
7206 uptime/(3600*24),
ed9b544e 7207 listLength(server.clients)-listLength(server.slaves),
7208 listLength(server.slaves),
d5d55fc3 7209 server.blpop_blocked_clients,
b72f6a4b 7210 zmalloc_used_memory(),
ec6c7a1d 7211 hmem,
ed9b544e 7212 server.dirty,
9d65a1bb 7213 server.bgsavechildpid != -1,
ed9b544e 7214 server.lastsave,
b3fad521 7215 server.bgrewritechildpid != -1,
ed9b544e 7216 server.stat_numconnections,
7217 server.stat_numcommands,
2a6a2ed1 7218 server.stat_expiredkeys,
55a8298f 7219 server.hash_max_zipmap_entries,
7220 server.hash_max_zipmap_value,
ffc6b7f8 7221 dictSize(server.pubsub_channels),
7222 listLength(server.pubsub_patterns),
7d98e08c 7223 server.vm_enabled != 0,
a0f643ea 7224 server.masterhost == NULL ? "master" : "slave"
ed9b544e 7225 );
a0f643ea 7226 if (server.masterhost) {
7227 info = sdscatprintf(info,
7228 "master_host:%s\r\n"
7229 "master_port:%d\r\n"
7230 "master_link_status:%s\r\n"
7231 "master_last_io_seconds_ago:%d\r\n"
7232 ,server.masterhost,
7233 server.masterport,
7234 (server.replstate == REDIS_REPL_CONNECTED) ?
7235 "up" : "down",
f72b934d 7236 server.master ? ((int)(time(NULL)-server.master->lastinteraction)) : -1
a0f643ea 7237 );
7238 }
7d98e08c 7239 if (server.vm_enabled) {
1064ef87 7240 lockThreadedIO();
7d98e08c 7241 info = sdscatprintf(info,
7242 "vm_conf_max_memory:%llu\r\n"
7243 "vm_conf_page_size:%llu\r\n"
7244 "vm_conf_pages:%llu\r\n"
7245 "vm_stats_used_pages:%llu\r\n"
7246 "vm_stats_swapped_objects:%llu\r\n"
7247 "vm_stats_swappin_count:%llu\r\n"
7248 "vm_stats_swappout_count:%llu\r\n"
b9bc0eef 7249 "vm_stats_io_newjobs_len:%lu\r\n"
7250 "vm_stats_io_processing_len:%lu\r\n"
7251 "vm_stats_io_processed_len:%lu\r\n"
25fd2cb2 7252 "vm_stats_io_active_threads:%lu\r\n"
d5d55fc3 7253 "vm_stats_blocked_clients:%lu\r\n"
7d98e08c 7254 ,(unsigned long long) server.vm_max_memory,
7255 (unsigned long long) server.vm_page_size,
7256 (unsigned long long) server.vm_pages,
7257 (unsigned long long) server.vm_stats_used_pages,
7258 (unsigned long long) server.vm_stats_swapped_objects,
7259 (unsigned long long) server.vm_stats_swapins,
b9bc0eef 7260 (unsigned long long) server.vm_stats_swapouts,
7261 (unsigned long) listLength(server.io_newjobs),
7262 (unsigned long) listLength(server.io_processing),
7263 (unsigned long) listLength(server.io_processed),
d5d55fc3 7264 (unsigned long) server.io_active_threads,
7265 (unsigned long) server.vm_blocked_clients
7d98e08c 7266 );
1064ef87 7267 unlockThreadedIO();
7d98e08c 7268 }
c3cb078d 7269 for (j = 0; j < server.dbnum; j++) {
7270 long long keys, vkeys;
7271
7272 keys = dictSize(server.db[j].dict);
7273 vkeys = dictSize(server.db[j].expires);
7274 if (keys || vkeys) {
9d65a1bb 7275 info = sdscatprintf(info, "db%d:keys=%lld,expires=%lld\r\n",
c3cb078d 7276 j, keys, vkeys);
7277 }
7278 }
1c85b79f 7279 return info;
7280}
7281
7282static void infoCommand(redisClient *c) {
7283 sds info = genRedisInfoString();
83c6a618 7284 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
7285 (unsigned long)sdslen(info)));
ed9b544e 7286 addReplySds(c,info);
70003d28 7287 addReply(c,shared.crlf);
ed9b544e 7288}
7289
3305306f 7290static void monitorCommand(redisClient *c) {
7291 /* ignore MONITOR if aleady slave or in monitor mode */
7292 if (c->flags & REDIS_SLAVE) return;
7293
7294 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
7295 c->slaveseldb = 0;
6b47e12e 7296 listAddNodeTail(server.monitors,c);
3305306f 7297 addReply(c,shared.ok);
7298}
7299
7300/* ================================= Expire ================================= */
7301static int removeExpire(redisDb *db, robj *key) {
7302 if (dictDelete(db->expires,key) == DICT_OK) {
7303 return 1;
7304 } else {
7305 return 0;
7306 }
7307}
7308
7309static int setExpire(redisDb *db, robj *key, time_t when) {
7310 if (dictAdd(db->expires,key,(void*)when) == DICT_ERR) {
7311 return 0;
7312 } else {
7313 incrRefCount(key);
7314 return 1;
7315 }
7316}
7317
bb32ede5 7318/* Return the expire time of the specified key, or -1 if no expire
7319 * is associated with this key (i.e. the key is non volatile) */
7320static time_t getExpire(redisDb *db, robj *key) {
7321 dictEntry *de;
7322
7323 /* No expire? return ASAP */
7324 if (dictSize(db->expires) == 0 ||
7325 (de = dictFind(db->expires,key)) == NULL) return -1;
7326
7327 return (time_t) dictGetEntryVal(de);
7328}
7329
3305306f 7330static int expireIfNeeded(redisDb *db, robj *key) {
7331 time_t when;
7332 dictEntry *de;
7333
7334 /* No expire? return ASAP */
7335 if (dictSize(db->expires) == 0 ||
7336 (de = dictFind(db->expires,key)) == NULL) return 0;
7337
7338 /* Lookup the expire */
7339 when = (time_t) dictGetEntryVal(de);
7340 if (time(NULL) <= when) return 0;
7341
7342 /* Delete the key */
7343 dictDelete(db->expires,key);
2a6a2ed1 7344 server.stat_expiredkeys++;
3305306f 7345 return dictDelete(db->dict,key) == DICT_OK;
7346}
7347
7348static int deleteIfVolatile(redisDb *db, robj *key) {
7349 dictEntry *de;
7350
7351 /* No expire? return ASAP */
7352 if (dictSize(db->expires) == 0 ||
7353 (de = dictFind(db->expires,key)) == NULL) return 0;
7354
7355 /* Delete the key */
0c66a471 7356 server.dirty++;
2a6a2ed1 7357 server.stat_expiredkeys++;
3305306f 7358 dictDelete(db->expires,key);
7359 return dictDelete(db->dict,key) == DICT_OK;
7360}
7361
bbe025e0 7362static void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
3305306f 7363 dictEntry *de;
bbe025e0
AM
7364 time_t seconds;
7365
bd79a6bd 7366 if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
bbe025e0
AM
7367
7368 seconds -= offset;
3305306f 7369
802e8373 7370 de = dictFind(c->db->dict,key);
3305306f 7371 if (de == NULL) {
7372 addReply(c,shared.czero);
7373 return;
7374 }
d4dd6556 7375 if (seconds <= 0) {
43e5ccdf 7376 if (deleteKey(c->db,key)) server.dirty++;
7377 addReply(c, shared.cone);
3305306f 7378 return;
7379 } else {
7380 time_t when = time(NULL)+seconds;
802e8373 7381 if (setExpire(c->db,key,when)) {
3305306f 7382 addReply(c,shared.cone);
77423026 7383 server.dirty++;
7384 } else {
3305306f 7385 addReply(c,shared.czero);
77423026 7386 }
3305306f 7387 return;
7388 }
7389}
7390
802e8373 7391static void expireCommand(redisClient *c) {
bbe025e0 7392 expireGenericCommand(c,c->argv[1],c->argv[2],0);
802e8373 7393}
7394
7395static void expireatCommand(redisClient *c) {
bbe025e0 7396 expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
802e8373 7397}
7398
fd88489a 7399static void ttlCommand(redisClient *c) {
7400 time_t expire;
7401 int ttl = -1;
7402
7403 expire = getExpire(c->db,c->argv[1]);
7404 if (expire != -1) {
7405 ttl = (int) (expire-time(NULL));
7406 if (ttl < 0) ttl = -1;
7407 }
7408 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",ttl));
7409}
7410
6e469882 7411/* ================================ MULTI/EXEC ============================== */
7412
7413/* Client state initialization for MULTI/EXEC */
7414static void initClientMultiState(redisClient *c) {
7415 c->mstate.commands = NULL;
7416 c->mstate.count = 0;
7417}
7418
7419/* Release all the resources associated with MULTI/EXEC state */
7420static void freeClientMultiState(redisClient *c) {
7421 int j;
7422
7423 for (j = 0; j < c->mstate.count; j++) {
7424 int i;
7425 multiCmd *mc = c->mstate.commands+j;
7426
7427 for (i = 0; i < mc->argc; i++)
7428 decrRefCount(mc->argv[i]);
7429 zfree(mc->argv);
7430 }
7431 zfree(c->mstate.commands);
7432}
7433
7434/* Add a new command into the MULTI commands queue */
7435static void queueMultiCommand(redisClient *c, struct redisCommand *cmd) {
7436 multiCmd *mc;
7437 int j;
7438
7439 c->mstate.commands = zrealloc(c->mstate.commands,
7440 sizeof(multiCmd)*(c->mstate.count+1));
7441 mc = c->mstate.commands+c->mstate.count;
7442 mc->cmd = cmd;
7443 mc->argc = c->argc;
7444 mc->argv = zmalloc(sizeof(robj*)*c->argc);
7445 memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
7446 for (j = 0; j < c->argc; j++)
7447 incrRefCount(mc->argv[j]);
7448 c->mstate.count++;
7449}
7450
7451static void multiCommand(redisClient *c) {
7452 c->flags |= REDIS_MULTI;
36c548f0 7453 addReply(c,shared.ok);
6e469882 7454}
7455
18b6cb76
DJ
7456static void discardCommand(redisClient *c) {
7457 if (!(c->flags & REDIS_MULTI)) {
7458 addReplySds(c,sdsnew("-ERR DISCARD without MULTI\r\n"));
7459 return;
7460 }
7461
7462 freeClientMultiState(c);
7463 initClientMultiState(c);
7464 c->flags &= (~REDIS_MULTI);
7465 addReply(c,shared.ok);
7466}
7467
66c8853f 7468/* Send a MULTI command to all the slaves and AOF file. Check the execCommand
7469 * implememntation for more information. */
7470static void execCommandReplicateMulti(redisClient *c) {
7471 struct redisCommand *cmd;
7472 robj *multistring = createStringObject("MULTI",5);
7473
7474 cmd = lookupCommand("multi");
7475 if (server.appendonly)
7476 feedAppendOnlyFile(cmd,c->db->id,&multistring,1);
7477 if (listLength(server.slaves))
7478 replicationFeedSlaves(server.slaves,c->db->id,&multistring,1);
7479 decrRefCount(multistring);
7480}
7481
6e469882 7482static void execCommand(redisClient *c) {
7483 int j;
7484 robj **orig_argv;
7485 int orig_argc;
7486
7487 if (!(c->flags & REDIS_MULTI)) {
7488 addReplySds(c,sdsnew("-ERR EXEC without MULTI\r\n"));
7489 return;
7490 }
7491
66c8853f 7492 /* Replicate a MULTI request now that we are sure the block is executed.
7493 * This way we'll deliver the MULTI/..../EXEC block as a whole and
7494 * both the AOF and the replication link will have the same consistency
7495 * and atomicity guarantees. */
7496 execCommandReplicateMulti(c);
7497
7498 /* Exec all the queued commands */
6e469882 7499 orig_argv = c->argv;
7500 orig_argc = c->argc;
7501 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->mstate.count));
7502 for (j = 0; j < c->mstate.count; j++) {
7503 c->argc = c->mstate.commands[j].argc;
7504 c->argv = c->mstate.commands[j].argv;
7505 call(c,c->mstate.commands[j].cmd);
7506 }
7507 c->argv = orig_argv;
7508 c->argc = orig_argc;
7509 freeClientMultiState(c);
7510 initClientMultiState(c);
7511 c->flags &= (~REDIS_MULTI);
66c8853f 7512 /* Make sure the EXEC command is always replicated / AOF, since we
7513 * always send the MULTI command (we can't know beforehand if the
7514 * next operations will contain at least a modification to the DB). */
7515 server.dirty++;
6e469882 7516}
7517
4409877e 7518/* =========================== Blocking Operations ========================= */
7519
7520/* Currently Redis blocking operations support is limited to list POP ops,
7521 * so the current implementation is not fully generic, but it is also not
7522 * completely specific so it will not require a rewrite to support new
7523 * kind of blocking operations in the future.
7524 *
7525 * Still it's important to note that list blocking operations can be already
7526 * used as a notification mechanism in order to implement other blocking
7527 * operations at application level, so there must be a very strong evidence
7528 * of usefulness and generality before new blocking operations are implemented.
7529 *
7530 * This is how the current blocking POP works, we use BLPOP as example:
7531 * - If the user calls BLPOP and the key exists and contains a non empty list
7532 * then LPOP is called instead. So BLPOP is semantically the same as LPOP
7533 * if there is not to block.
7534 * - If instead BLPOP is called and the key does not exists or the list is
7535 * empty we need to block. In order to do so we remove the notification for
7536 * new data to read in the client socket (so that we'll not serve new
7537 * requests if the blocking request is not served). Also we put the client
95242ab5 7538 * in a dictionary (db->blockingkeys) mapping keys to a list of clients
4409877e 7539 * blocking for this keys.
7540 * - If a PUSH operation against a key with blocked clients waiting is
7541 * performed, we serve the first in the list: basically instead to push
7542 * the new element inside the list we return it to the (first / oldest)
7543 * blocking client, unblock the client, and remove it form the list.
7544 *
7545 * The above comment and the source code should be enough in order to understand
7546 * the implementation and modify / fix it later.
7547 */
7548
7549/* Set a client in blocking mode for the specified key, with the specified
7550 * timeout */
b177fd30 7551static void blockForKeys(redisClient *c, robj **keys, int numkeys, time_t timeout) {
4409877e 7552 dictEntry *de;
7553 list *l;
b177fd30 7554 int j;
4409877e 7555
b177fd30 7556 c->blockingkeys = zmalloc(sizeof(robj*)*numkeys);
7557 c->blockingkeysnum = numkeys;
4409877e 7558 c->blockingto = timeout;
b177fd30 7559 for (j = 0; j < numkeys; j++) {
7560 /* Add the key in the client structure, to map clients -> keys */
7561 c->blockingkeys[j] = keys[j];
7562 incrRefCount(keys[j]);
4409877e 7563
b177fd30 7564 /* And in the other "side", to map keys -> clients */
7565 de = dictFind(c->db->blockingkeys,keys[j]);
7566 if (de == NULL) {
7567 int retval;
7568
7569 /* For every key we take a list of clients blocked for it */
7570 l = listCreate();
7571 retval = dictAdd(c->db->blockingkeys,keys[j],l);
7572 incrRefCount(keys[j]);
7573 assert(retval == DICT_OK);
7574 } else {
7575 l = dictGetEntryVal(de);
7576 }
7577 listAddNodeTail(l,c);
4409877e 7578 }
b177fd30 7579 /* Mark the client as a blocked client */
4409877e 7580 c->flags |= REDIS_BLOCKED;
d5d55fc3 7581 server.blpop_blocked_clients++;
4409877e 7582}
7583
7584/* Unblock a client that's waiting in a blocking operation such as BLPOP */
b0d8747d 7585static void unblockClientWaitingData(redisClient *c) {
4409877e 7586 dictEntry *de;
7587 list *l;
b177fd30 7588 int j;
4409877e 7589
b177fd30 7590 assert(c->blockingkeys != NULL);
7591 /* The client may wait for multiple keys, so unblock it for every key. */
7592 for (j = 0; j < c->blockingkeysnum; j++) {
7593 /* Remove this client from the list of clients waiting for this key. */
7594 de = dictFind(c->db->blockingkeys,c->blockingkeys[j]);
7595 assert(de != NULL);
7596 l = dictGetEntryVal(de);
7597 listDelNode(l,listSearchKey(l,c));
7598 /* If the list is empty we need to remove it to avoid wasting memory */
7599 if (listLength(l) == 0)
7600 dictDelete(c->db->blockingkeys,c->blockingkeys[j]);
7601 decrRefCount(c->blockingkeys[j]);
7602 }
7603 /* Cleanup the client structure */
7604 zfree(c->blockingkeys);
7605 c->blockingkeys = NULL;
4409877e 7606 c->flags &= (~REDIS_BLOCKED);
d5d55fc3 7607 server.blpop_blocked_clients--;
5921aa36 7608 /* We want to process data if there is some command waiting
b0d8747d 7609 * in the input buffer. Note that this is safe even if
7610 * unblockClientWaitingData() gets called from freeClient() because
7611 * freeClient() will be smart enough to call this function
7612 * *after* c->querybuf was set to NULL. */
4409877e 7613 if (c->querybuf && sdslen(c->querybuf) > 0) processInputBuffer(c);
7614}
7615
7616/* This should be called from any function PUSHing into lists.
7617 * 'c' is the "pushing client", 'key' is the key it is pushing data against,
7618 * 'ele' is the element pushed.
7619 *
7620 * If the function returns 0 there was no client waiting for a list push
7621 * against this key.
7622 *
7623 * If the function returns 1 there was a client waiting for a list push
7624 * against this key, the element was passed to this client thus it's not
7625 * needed to actually add it to the list and the caller should return asap. */
7626static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele) {
7627 struct dictEntry *de;
7628 redisClient *receiver;
7629 list *l;
7630 listNode *ln;
7631
7632 de = dictFind(c->db->blockingkeys,key);
7633 if (de == NULL) return 0;
7634 l = dictGetEntryVal(de);
7635 ln = listFirst(l);
7636 assert(ln != NULL);
7637 receiver = ln->value;
4409877e 7638
b177fd30 7639 addReplySds(receiver,sdsnew("*2\r\n"));
dd88747b 7640 addReplyBulk(receiver,key);
7641 addReplyBulk(receiver,ele);
b0d8747d 7642 unblockClientWaitingData(receiver);
4409877e 7643 return 1;
7644}
7645
7646/* Blocking RPOP/LPOP */
7647static void blockingPopGenericCommand(redisClient *c, int where) {
7648 robj *o;
7649 time_t timeout;
b177fd30 7650 int j;
4409877e 7651
b177fd30 7652 for (j = 1; j < c->argc-1; j++) {
7653 o = lookupKeyWrite(c->db,c->argv[j]);
7654 if (o != NULL) {
7655 if (o->type != REDIS_LIST) {
7656 addReply(c,shared.wrongtypeerr);
4409877e 7657 return;
b177fd30 7658 } else {
7659 list *list = o->ptr;
7660 if (listLength(list) != 0) {
7661 /* If the list contains elements fall back to the usual
7662 * non-blocking POP operation */
7663 robj *argv[2], **orig_argv;
7664 int orig_argc;
e0a62c7f 7665
b177fd30 7666 /* We need to alter the command arguments before to call
7667 * popGenericCommand() as the command takes a single key. */
7668 orig_argv = c->argv;
7669 orig_argc = c->argc;
7670 argv[1] = c->argv[j];
7671 c->argv = argv;
7672 c->argc = 2;
7673
7674 /* Also the return value is different, we need to output
7675 * the multi bulk reply header and the key name. The
7676 * "real" command will add the last element (the value)
7677 * for us. If this souds like an hack to you it's just
7678 * because it is... */
7679 addReplySds(c,sdsnew("*2\r\n"));
dd88747b 7680 addReplyBulk(c,argv[1]);
b177fd30 7681 popGenericCommand(c,where);
7682
7683 /* Fix the client structure with the original stuff */
7684 c->argv = orig_argv;
7685 c->argc = orig_argc;
7686 return;
7687 }
4409877e 7688 }
7689 }
7690 }
7691 /* If the list is empty or the key does not exists we must block */
b177fd30 7692 timeout = strtol(c->argv[c->argc-1]->ptr,NULL,10);
4409877e 7693 if (timeout > 0) timeout += time(NULL);
b177fd30 7694 blockForKeys(c,c->argv+1,c->argc-2,timeout);
4409877e 7695}
7696
7697static void blpopCommand(redisClient *c) {
7698 blockingPopGenericCommand(c,REDIS_HEAD);
7699}
7700
7701static void brpopCommand(redisClient *c) {
7702 blockingPopGenericCommand(c,REDIS_TAIL);
7703}
7704
ed9b544e 7705/* =============================== Replication ============================= */
7706
a4d1ba9a 7707static int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
ed9b544e 7708 ssize_t nwritten, ret = size;
7709 time_t start = time(NULL);
7710
7711 timeout++;
7712 while(size) {
7713 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
7714 nwritten = write(fd,ptr,size);
7715 if (nwritten == -1) return -1;
7716 ptr += nwritten;
7717 size -= nwritten;
7718 }
7719 if ((time(NULL)-start) > timeout) {
7720 errno = ETIMEDOUT;
7721 return -1;
7722 }
7723 }
7724 return ret;
7725}
7726
a4d1ba9a 7727static int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
ed9b544e 7728 ssize_t nread, totread = 0;
7729 time_t start = time(NULL);
7730
7731 timeout++;
7732 while(size) {
7733 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
7734 nread = read(fd,ptr,size);
7735 if (nread == -1) return -1;
7736 ptr += nread;
7737 size -= nread;
7738 totread += nread;
7739 }
7740 if ((time(NULL)-start) > timeout) {
7741 errno = ETIMEDOUT;
7742 return -1;
7743 }
7744 }
7745 return totread;
7746}
7747
7748static int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
7749 ssize_t nread = 0;
7750
7751 size--;
7752 while(size) {
7753 char c;
7754
7755 if (syncRead(fd,&c,1,timeout) == -1) return -1;
7756 if (c == '\n') {
7757 *ptr = '\0';
7758 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
7759 return nread;
7760 } else {
7761 *ptr++ = c;
7762 *ptr = '\0';
7763 nread++;
7764 }
7765 }
7766 return nread;
7767}
7768
7769static void syncCommand(redisClient *c) {
40d224a9 7770 /* ignore SYNC if aleady slave or in monitor mode */
7771 if (c->flags & REDIS_SLAVE) return;
7772
7773 /* SYNC can't be issued when the server has pending data to send to
7774 * the client about already issued commands. We need a fresh reply
7775 * buffer registering the differences between the BGSAVE and the current
7776 * dataset, so that we can copy to other slaves if needed. */
7777 if (listLength(c->reply) != 0) {
7778 addReplySds(c,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
7779 return;
7780 }
7781
7782 redisLog(REDIS_NOTICE,"Slave ask for synchronization");
7783 /* Here we need to check if there is a background saving operation
7784 * in progress, or if it is required to start one */
9d65a1bb 7785 if (server.bgsavechildpid != -1) {
40d224a9 7786 /* Ok a background save is in progress. Let's check if it is a good
7787 * one for replication, i.e. if there is another slave that is
7788 * registering differences since the server forked to save */
7789 redisClient *slave;
7790 listNode *ln;
c7df85a4 7791 listIter li;
40d224a9 7792
c7df85a4 7793 listRewind(server.slaves,&li);
7794 while((ln = listNext(&li))) {
40d224a9 7795 slave = ln->value;
7796 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;
40d224a9 7797 }
7798 if (ln) {
7799 /* Perfect, the server is already registering differences for
7800 * another slave. Set the right state, and copy the buffer. */
7801 listRelease(c->reply);
7802 c->reply = listDup(slave->reply);
40d224a9 7803 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7804 redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
7805 } else {
7806 /* No way, we need to wait for the next BGSAVE in order to
7807 * register differences */
7808 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
7809 redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");
7810 }
7811 } else {
7812 /* Ok we don't have a BGSAVE in progress, let's start one */
7813 redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC");
7814 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
7815 redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");
7816 addReplySds(c,sdsnew("-ERR Unalbe to perform background save\r\n"));
7817 return;
7818 }
7819 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7820 }
6208b3a7 7821 c->repldbfd = -1;
40d224a9 7822 c->flags |= REDIS_SLAVE;
7823 c->slaveseldb = 0;
6b47e12e 7824 listAddNodeTail(server.slaves,c);
40d224a9 7825 return;
7826}
7827
6208b3a7 7828static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
7829 redisClient *slave = privdata;
7830 REDIS_NOTUSED(el);
7831 REDIS_NOTUSED(mask);
7832 char buf[REDIS_IOBUF_LEN];
7833 ssize_t nwritten, buflen;
7834
7835 if (slave->repldboff == 0) {
7836 /* Write the bulk write count before to transfer the DB. In theory here
7837 * we don't know how much room there is in the output buffer of the
7838 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
7839 * operations) will never be smaller than the few bytes we need. */
7840 sds bulkcount;
7841
7842 bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
7843 slave->repldbsize);
7844 if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount))
7845 {
7846 sdsfree(bulkcount);
7847 freeClient(slave);
7848 return;
7849 }
7850 sdsfree(bulkcount);
7851 }
7852 lseek(slave->repldbfd,slave->repldboff,SEEK_SET);
7853 buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);
7854 if (buflen <= 0) {
7855 redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",
7856 (buflen == 0) ? "premature EOF" : strerror(errno));
7857 freeClient(slave);
7858 return;
7859 }
7860 if ((nwritten = write(fd,buf,buflen)) == -1) {
f870935d 7861 redisLog(REDIS_VERBOSE,"Write error sending DB to slave: %s",
6208b3a7 7862 strerror(errno));
7863 freeClient(slave);
7864 return;
7865 }
7866 slave->repldboff += nwritten;
7867 if (slave->repldboff == slave->repldbsize) {
7868 close(slave->repldbfd);
7869 slave->repldbfd = -1;
7870 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
7871 slave->replstate = REDIS_REPL_ONLINE;
7872 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
266373b2 7873 sendReplyToClient, slave) == AE_ERR) {
6208b3a7 7874 freeClient(slave);
7875 return;
7876 }
7877 addReplySds(slave,sdsempty());
7878 redisLog(REDIS_NOTICE,"Synchronization with slave succeeded");
7879 }
7880}
ed9b544e 7881
a3b21203 7882/* This function is called at the end of every backgrond saving.
7883 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
7884 * otherwise REDIS_ERR is passed to the function.
7885 *
7886 * The goal of this function is to handle slaves waiting for a successful
7887 * background saving in order to perform non-blocking synchronization. */
7888static void updateSlavesWaitingBgsave(int bgsaveerr) {
6208b3a7 7889 listNode *ln;
7890 int startbgsave = 0;
c7df85a4 7891 listIter li;
ed9b544e 7892
c7df85a4 7893 listRewind(server.slaves,&li);
7894 while((ln = listNext(&li))) {
6208b3a7 7895 redisClient *slave = ln->value;
ed9b544e 7896
6208b3a7 7897 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
7898 startbgsave = 1;
7899 slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7900 } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
dde65f3f 7901 struct redis_stat buf;
e0a62c7f 7902
6208b3a7 7903 if (bgsaveerr != REDIS_OK) {
7904 freeClient(slave);
7905 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
7906 continue;
7907 }
7908 if ((slave->repldbfd = open(server.dbfilename,O_RDONLY)) == -1 ||
dde65f3f 7909 redis_fstat(slave->repldbfd,&buf) == -1) {
6208b3a7 7910 freeClient(slave);
7911 redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
7912 continue;
7913 }
7914 slave->repldboff = 0;
7915 slave->repldbsize = buf.st_size;
7916 slave->replstate = REDIS_REPL_SEND_BULK;
7917 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
266373b2 7918 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {
6208b3a7 7919 freeClient(slave);
7920 continue;
7921 }
7922 }
ed9b544e 7923 }
6208b3a7 7924 if (startbgsave) {
7925 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
c7df85a4 7926 listIter li;
7927
7928 listRewind(server.slaves,&li);
6208b3a7 7929 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
c7df85a4 7930 while((ln = listNext(&li))) {
6208b3a7 7931 redisClient *slave = ln->value;
ed9b544e 7932
6208b3a7 7933 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
7934 freeClient(slave);
7935 }
7936 }
7937 }
ed9b544e 7938}
7939
7940static int syncWithMaster(void) {
d0ccebcf 7941 char buf[1024], tmpfile[256], authcmd[1024];
18e61fa2 7942 long dumpsize;
ed9b544e 7943 int fd = anetTcpConnect(NULL,server.masterhost,server.masterport);
8c5abee8 7944 int dfd, maxtries = 5;
ed9b544e 7945
7946 if (fd == -1) {
7947 redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",
7948 strerror(errno));
7949 return REDIS_ERR;
7950 }
d0ccebcf 7951
7952 /* AUTH with the master if required. */
7953 if(server.masterauth) {
7954 snprintf(authcmd, 1024, "AUTH %s\r\n", server.masterauth);
7955 if (syncWrite(fd, authcmd, strlen(server.masterauth)+7, 5) == -1) {
7956 close(fd);
7957 redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",
7958 strerror(errno));
7959 return REDIS_ERR;
7960 }
7961 /* Read the AUTH result. */
7962 if (syncReadLine(fd,buf,1024,3600) == -1) {
7963 close(fd);
7964 redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s",
7965 strerror(errno));
7966 return REDIS_ERR;
7967 }
7968 if (buf[0] != '+') {
7969 close(fd);
7970 redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?");
7971 return REDIS_ERR;
7972 }
7973 }
7974
ed9b544e 7975 /* Issue the SYNC command */
7976 if (syncWrite(fd,"SYNC \r\n",7,5) == -1) {
7977 close(fd);
7978 redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",
7979 strerror(errno));
7980 return REDIS_ERR;
7981 }
7982 /* Read the bulk write count */
8c4d91fc 7983 if (syncReadLine(fd,buf,1024,3600) == -1) {
ed9b544e 7984 close(fd);
7985 redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s",
7986 strerror(errno));
7987 return REDIS_ERR;
7988 }
4aa701c1 7989 if (buf[0] != '$') {
7990 close(fd);
7991 redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
7992 return REDIS_ERR;
7993 }
18e61fa2 7994 dumpsize = strtol(buf+1,NULL,10);
7995 redisLog(REDIS_NOTICE,"Receiving %ld bytes data dump from MASTER",dumpsize);
ed9b544e 7996 /* Read the bulk write data on a temp file */
8c5abee8 7997 while(maxtries--) {
7998 snprintf(tmpfile,256,
7999 "temp-%d.%ld.rdb",(int)time(NULL),(long int)getpid());
8000 dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);
8001 if (dfd != -1) break;
5de9ad7c 8002 sleep(1);
8c5abee8 8003 }
ed9b544e 8004 if (dfd == -1) {
8005 close(fd);
8006 redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
8007 return REDIS_ERR;
8008 }
8009 while(dumpsize) {
8010 int nread, nwritten;
8011
8012 nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024);
8013 if (nread == -1) {
8014 redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
8015 strerror(errno));
8016 close(fd);
8017 close(dfd);
8018 return REDIS_ERR;
8019 }
8020 nwritten = write(dfd,buf,nread);
8021 if (nwritten == -1) {
8022 redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
8023 close(fd);
8024 close(dfd);
8025 return REDIS_ERR;
8026 }
8027 dumpsize -= nread;
8028 }
8029 close(dfd);
8030 if (rename(tmpfile,server.dbfilename) == -1) {
8031 redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
8032 unlink(tmpfile);
8033 close(fd);
8034 return REDIS_ERR;
8035 }
8036 emptyDb();
f78fd11b 8037 if (rdbLoad(server.dbfilename) != REDIS_OK) {
ed9b544e 8038 redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
8039 close(fd);
8040 return REDIS_ERR;
8041 }
8042 server.master = createClient(fd);
8043 server.master->flags |= REDIS_MASTER;
179b3952 8044 server.master->authenticated = 1;
ed9b544e 8045 server.replstate = REDIS_REPL_CONNECTED;
8046 return REDIS_OK;
8047}
8048
321b0e13 8049static void slaveofCommand(redisClient *c) {
8050 if (!strcasecmp(c->argv[1]->ptr,"no") &&
8051 !strcasecmp(c->argv[2]->ptr,"one")) {
8052 if (server.masterhost) {
8053 sdsfree(server.masterhost);
8054 server.masterhost = NULL;
8055 if (server.master) freeClient(server.master);
8056 server.replstate = REDIS_REPL_NONE;
8057 redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
8058 }
8059 } else {
8060 sdsfree(server.masterhost);
8061 server.masterhost = sdsdup(c->argv[1]->ptr);
8062 server.masterport = atoi(c->argv[2]->ptr);
8063 if (server.master) freeClient(server.master);
8064 server.replstate = REDIS_REPL_CONNECT;
8065 redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
8066 server.masterhost, server.masterport);
8067 }
8068 addReply(c,shared.ok);
8069}
8070
3fd78bcd 8071/* ============================ Maxmemory directive ======================== */
8072
a5819310 8073/* Try to free one object form the pre-allocated objects free list.
8074 * This is useful under low mem conditions as by default we take 1 million
8075 * free objects allocated. On success REDIS_OK is returned, otherwise
8076 * REDIS_ERR. */
8077static int tryFreeOneObjectFromFreelist(void) {
f870935d 8078 robj *o;
8079
a5819310 8080 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
8081 if (listLength(server.objfreelist)) {
8082 listNode *head = listFirst(server.objfreelist);
8083 o = listNodeValue(head);
8084 listDelNode(server.objfreelist,head);
8085 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8086 zfree(o);
8087 return REDIS_OK;
8088 } else {
8089 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8090 return REDIS_ERR;
8091 }
f870935d 8092}
8093
3fd78bcd 8094/* This function gets called when 'maxmemory' is set on the config file to limit
8095 * the max memory used by the server, and we are out of memory.
8096 * This function will try to, in order:
8097 *
8098 * - Free objects from the free list
8099 * - Try to remove keys with an EXPIRE set
8100 *
8101 * It is not possible to free enough memory to reach used-memory < maxmemory
8102 * the server will start refusing commands that will enlarge even more the
8103 * memory usage.
8104 */
8105static void freeMemoryIfNeeded(void) {
8106 while (server.maxmemory && zmalloc_used_memory() > server.maxmemory) {
a5819310 8107 int j, k, freed = 0;
8108
8109 if (tryFreeOneObjectFromFreelist() == REDIS_OK) continue;
8110 for (j = 0; j < server.dbnum; j++) {
8111 int minttl = -1;
8112 robj *minkey = NULL;
8113 struct dictEntry *de;
8114
8115 if (dictSize(server.db[j].expires)) {
8116 freed = 1;
8117 /* From a sample of three keys drop the one nearest to
8118 * the natural expire */
8119 for (k = 0; k < 3; k++) {
8120 time_t t;
8121
8122 de = dictGetRandomKey(server.db[j].expires);
8123 t = (time_t) dictGetEntryVal(de);
8124 if (minttl == -1 || t < minttl) {
8125 minkey = dictGetEntryKey(de);
8126 minttl = t;
3fd78bcd 8127 }
3fd78bcd 8128 }
a5819310 8129 deleteKey(server.db+j,minkey);
3fd78bcd 8130 }
3fd78bcd 8131 }
a5819310 8132 if (!freed) return; /* nothing to free... */
3fd78bcd 8133 }
8134}
8135
f80dff62 8136/* ============================== Append Only file ========================== */
8137
28ed1f33 8138/* Write the append only file buffer on disk.
8139 *
8140 * Since we are required to write the AOF before replying to the client,
8141 * and the only way the client socket can get a write is entering when the
8142 * the event loop, we accumulate all the AOF writes in a memory
8143 * buffer and write it on disk using this function just before entering
8144 * the event loop again. */
8145static void flushAppendOnlyFile(void) {
8146 time_t now;
8147 ssize_t nwritten;
8148
8149 if (sdslen(server.aofbuf) == 0) return;
8150
8151 /* We want to perform a single write. This should be guaranteed atomic
8152 * at least if the filesystem we are writing is a real physical one.
8153 * While this will save us against the server being killed I don't think
8154 * there is much to do about the whole server stopping for power problems
8155 * or alike */
8156 nwritten = write(server.appendfd,server.aofbuf,sdslen(server.aofbuf));
8157 if (nwritten != (signed)sdslen(server.aofbuf)) {
8158 /* Ooops, we are in troubles. The best thing to do for now is
8159 * aborting instead of giving the illusion that everything is
8160 * working as expected. */
8161 if (nwritten == -1) {
8162 redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno));
8163 } else {
8164 redisLog(REDIS_WARNING,"Exiting on short write while writing to the append-only file: %s",strerror(errno));
8165 }
8166 exit(1);
8167 }
8168 sdsfree(server.aofbuf);
8169 server.aofbuf = sdsempty();
8170
8171 /* Fsync if needed */
8172 now = time(NULL);
8173 if (server.appendfsync == APPENDFSYNC_ALWAYS ||
8174 (server.appendfsync == APPENDFSYNC_EVERYSEC &&
8175 now-server.lastfsync > 1))
8176 {
8177 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
8178 * flushing metadata. */
8179 aof_fsync(server.appendfd); /* Let's try to get this data on the disk */
8180 server.lastfsync = now;
8181 }
8182}
8183
9376e434
PN
8184static sds catAppendOnlyGenericCommand(sds buf, int argc, robj **argv) {
8185 int j;
8186 buf = sdscatprintf(buf,"*%d\r\n",argc);
8187 for (j = 0; j < argc; j++) {
8188 robj *o = getDecodedObject(argv[j]);
8189 buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
8190 buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
8191 buf = sdscatlen(buf,"\r\n",2);
8192 decrRefCount(o);
8193 }
8194 return buf;
8195}
8196
8197static sds catAppendOnlyExpireAtCommand(sds buf, robj *key, robj *seconds) {
8198 int argc = 3;
8199 long when;
8200 robj *argv[3];
8201
8202 /* Make sure we can use strtol */
8203 seconds = getDecodedObject(seconds);
8204 when = time(NULL)+strtol(seconds->ptr,NULL,10);
8205 decrRefCount(seconds);
8206
8207 argv[0] = createStringObject("EXPIREAT",8);
8208 argv[1] = key;
8209 argv[2] = createObject(REDIS_STRING,
8210 sdscatprintf(sdsempty(),"%ld",when));
8211 buf = catAppendOnlyGenericCommand(buf, argc, argv);
8212 decrRefCount(argv[0]);
8213 decrRefCount(argv[2]);
8214 return buf;
8215}
8216
f80dff62 8217static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
8218 sds buf = sdsempty();
f80dff62 8219 robj *tmpargv[3];
8220
8221 /* The DB this command was targetting is not the same as the last command
8222 * we appendend. To issue a SELECT command is needed. */
8223 if (dictid != server.appendseldb) {
8224 char seldb[64];
8225
8226 snprintf(seldb,sizeof(seldb),"%d",dictid);
682ac724 8227 buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
83c6a618 8228 (unsigned long)strlen(seldb),seldb);
f80dff62 8229 server.appendseldb = dictid;
8230 }
8231
f80dff62 8232 if (cmd->proc == expireCommand) {
9376e434
PN
8233 /* Translate EXPIRE into EXPIREAT */
8234 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8235 } else if (cmd->proc == setexCommand) {
8236 /* Translate SETEX to SET and EXPIREAT */
8237 tmpargv[0] = createStringObject("SET",3);
f80dff62 8238 tmpargv[1] = argv[1];
9376e434
PN
8239 tmpargv[2] = argv[3];
8240 buf = catAppendOnlyGenericCommand(buf,3,tmpargv);
8241 decrRefCount(tmpargv[0]);
8242 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8243 } else {
8244 buf = catAppendOnlyGenericCommand(buf,argc,argv);
f80dff62 8245 }
8246
28ed1f33 8247 /* Append to the AOF buffer. This will be flushed on disk just before
8248 * of re-entering the event loop, so before the client will get a
8249 * positive reply about the operation performed. */
8250 server.aofbuf = sdscatlen(server.aofbuf,buf,sdslen(buf));
8251
85a83172 8252 /* If a background append only file rewriting is in progress we want to
8253 * accumulate the differences between the child DB and the current one
8254 * in a buffer, so that when the child process will do its work we
8255 * can append the differences to the new append only file. */
8256 if (server.bgrewritechildpid != -1)
8257 server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf));
8258
8259 sdsfree(buf);
f80dff62 8260}
8261
8262/* In Redis commands are always executed in the context of a client, so in
8263 * order to load the append only file we need to create a fake client. */
8264static struct redisClient *createFakeClient(void) {
8265 struct redisClient *c = zmalloc(sizeof(*c));
8266
8267 selectDb(c,0);
8268 c->fd = -1;
8269 c->querybuf = sdsempty();
8270 c->argc = 0;
8271 c->argv = NULL;
8272 c->flags = 0;
9387d17d 8273 /* We set the fake client as a slave waiting for the synchronization
8274 * so that Redis will not try to send replies to this client. */
8275 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
f80dff62 8276 c->reply = listCreate();
8277 listSetFreeMethod(c->reply,decrRefCount);
8278 listSetDupMethod(c->reply,dupClientReplyValue);
4132ad8d 8279 initClientMultiState(c);
f80dff62 8280 return c;
8281}
8282
8283static void freeFakeClient(struct redisClient *c) {
8284 sdsfree(c->querybuf);
8285 listRelease(c->reply);
4132ad8d 8286 freeClientMultiState(c);
f80dff62 8287 zfree(c);
8288}
8289
8290/* Replay the append log file. On error REDIS_OK is returned. On non fatal
8291 * error (the append only file is zero-length) REDIS_ERR is returned. On
8292 * fatal error an error message is logged and the program exists. */
8293int loadAppendOnlyFile(char *filename) {
8294 struct redisClient *fakeClient;
8295 FILE *fp = fopen(filename,"r");
8296 struct redis_stat sb;
b492cf00 8297 unsigned long long loadedkeys = 0;
4132ad8d 8298 int appendonly = server.appendonly;
f80dff62 8299
8300 if (redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0)
8301 return REDIS_ERR;
8302
8303 if (fp == NULL) {
8304 redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
8305 exit(1);
8306 }
8307
4132ad8d
PN
8308 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
8309 * to the same file we're about to read. */
8310 server.appendonly = 0;
8311
f80dff62 8312 fakeClient = createFakeClient();
8313 while(1) {
8314 int argc, j;
8315 unsigned long len;
8316 robj **argv;
8317 char buf[128];
8318 sds argsds;
8319 struct redisCommand *cmd;
8320
8321 if (fgets(buf,sizeof(buf),fp) == NULL) {
8322 if (feof(fp))
8323 break;
8324 else
8325 goto readerr;
8326 }
8327 if (buf[0] != '*') goto fmterr;
8328 argc = atoi(buf+1);
8329 argv = zmalloc(sizeof(robj*)*argc);
8330 for (j = 0; j < argc; j++) {
8331 if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr;
8332 if (buf[0] != '$') goto fmterr;
8333 len = strtol(buf+1,NULL,10);
8334 argsds = sdsnewlen(NULL,len);
0f151ef1 8335 if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
f80dff62 8336 argv[j] = createObject(REDIS_STRING,argsds);
8337 if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
8338 }
8339
8340 /* Command lookup */
8341 cmd = lookupCommand(argv[0]->ptr);
8342 if (!cmd) {
8343 redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr);
8344 exit(1);
8345 }
bdcb92f2 8346 /* Try object encoding */
f80dff62 8347 if (cmd->flags & REDIS_CMD_BULK)
05df7621 8348 argv[argc-1] = tryObjectEncoding(argv[argc-1]);
f80dff62 8349 /* Run the command in the context of a fake client */
8350 fakeClient->argc = argc;
8351 fakeClient->argv = argv;
8352 cmd->proc(fakeClient);
8353 /* Discard the reply objects list from the fake client */
8354 while(listLength(fakeClient->reply))
8355 listDelNode(fakeClient->reply,listFirst(fakeClient->reply));
8356 /* Clean up, ready for the next command */
8357 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
8358 zfree(argv);
b492cf00 8359 /* Handle swapping while loading big datasets when VM is on */
8360 loadedkeys++;
8361 if (server.vm_enabled && (loadedkeys % 5000) == 0) {
8362 while (zmalloc_used_memory() > server.vm_max_memory) {
a69a0c9c 8363 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
b492cf00 8364 }
8365 }
f80dff62 8366 }
4132ad8d
PN
8367
8368 /* This point can only be reached when EOF is reached without errors.
8369 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
8370 if (fakeClient->flags & REDIS_MULTI) goto readerr;
8371
f80dff62 8372 fclose(fp);
8373 freeFakeClient(fakeClient);
4132ad8d 8374 server.appendonly = appendonly;
f80dff62 8375 return REDIS_OK;
8376
8377readerr:
8378 if (feof(fp)) {
8379 redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file");
8380 } else {
8381 redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
8382 }
8383 exit(1);
8384fmterr:
8385 redisLog(REDIS_WARNING,"Bad file format reading the append only file");
8386 exit(1);
8387}
8388
9d65a1bb 8389/* Write an object into a file in the bulk format $<count>\r\n<payload>\r\n */
9c8e3cee 8390static int fwriteBulkObject(FILE *fp, robj *obj) {
9d65a1bb 8391 char buf[128];
b9bc0eef 8392 int decrrc = 0;
8393
f2d9f50f 8394 /* Avoid the incr/decr ref count business if possible to help
8395 * copy-on-write (we are often in a child process when this function
8396 * is called).
8397 * Also makes sure that key objects don't get incrRefCount-ed when VM
8398 * is enabled */
8399 if (obj->encoding != REDIS_ENCODING_RAW) {
b9bc0eef 8400 obj = getDecodedObject(obj);
8401 decrrc = 1;
8402 }
9d65a1bb 8403 snprintf(buf,sizeof(buf),"$%ld\r\n",(long)sdslen(obj->ptr));
8404 if (fwrite(buf,strlen(buf),1,fp) == 0) goto err;
e96e4fbf 8405 if (sdslen(obj->ptr) && fwrite(obj->ptr,sdslen(obj->ptr),1,fp) == 0)
8406 goto err;
9d65a1bb 8407 if (fwrite("\r\n",2,1,fp) == 0) goto err;
b9bc0eef 8408 if (decrrc) decrRefCount(obj);
9d65a1bb 8409 return 1;
8410err:
b9bc0eef 8411 if (decrrc) decrRefCount(obj);
9d65a1bb 8412 return 0;
8413}
8414
9c8e3cee 8415/* Write binary-safe string into a file in the bulkformat
8416 * $<count>\r\n<payload>\r\n */
8417static int fwriteBulkString(FILE *fp, char *s, unsigned long len) {
8418 char buf[128];
8419
8420 snprintf(buf,sizeof(buf),"$%ld\r\n",(unsigned long)len);
8421 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8422 if (len && fwrite(s,len,1,fp) == 0) return 0;
8423 if (fwrite("\r\n",2,1,fp) == 0) return 0;
8424 return 1;
8425}
8426
9d65a1bb 8427/* Write a double value in bulk format $<count>\r\n<payload>\r\n */
8428static int fwriteBulkDouble(FILE *fp, double d) {
8429 char buf[128], dbuf[128];
8430
8431 snprintf(dbuf,sizeof(dbuf),"%.17g\r\n",d);
8432 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(dbuf)-2);
8433 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8434 if (fwrite(dbuf,strlen(dbuf),1,fp) == 0) return 0;
8435 return 1;
8436}
8437
8438/* Write a long value in bulk format $<count>\r\n<payload>\r\n */
8439static int fwriteBulkLong(FILE *fp, long l) {
8440 char buf[128], lbuf[128];
8441
8442 snprintf(lbuf,sizeof(lbuf),"%ld\r\n",l);
8443 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(lbuf)-2);
8444 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8445 if (fwrite(lbuf,strlen(lbuf),1,fp) == 0) return 0;
8446 return 1;
8447}
8448
8449/* Write a sequence of commands able to fully rebuild the dataset into
8450 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
8451static int rewriteAppendOnlyFile(char *filename) {
8452 dictIterator *di = NULL;
8453 dictEntry *de;
8454 FILE *fp;
8455 char tmpfile[256];
8456 int j;
8457 time_t now = time(NULL);
8458
8459 /* Note that we have to use a different temp name here compared to the
8460 * one used by rewriteAppendOnlyFileBackground() function. */
8461 snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid());
8462 fp = fopen(tmpfile,"w");
8463 if (!fp) {
8464 redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno));
8465 return REDIS_ERR;
8466 }
8467 for (j = 0; j < server.dbnum; j++) {
8468 char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
8469 redisDb *db = server.db+j;
8470 dict *d = db->dict;
8471 if (dictSize(d) == 0) continue;
8472 di = dictGetIterator(d);
8473 if (!di) {
8474 fclose(fp);
8475 return REDIS_ERR;
8476 }
8477
8478 /* SELECT the new DB */
8479 if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr;
85a83172 8480 if (fwriteBulkLong(fp,j) == 0) goto werr;
9d65a1bb 8481
8482 /* Iterate this DB writing every entry */
8483 while((de = dictNext(di)) != NULL) {
e7546c63 8484 robj *key, *o;
8485 time_t expiretime;
8486 int swapped;
8487
8488 key = dictGetEntryKey(de);
b9bc0eef 8489 /* If the value for this key is swapped, load a preview in memory.
8490 * We use a "swapped" flag to remember if we need to free the
8491 * value object instead to just increment the ref count anyway
8492 * in order to avoid copy-on-write of pages if we are forked() */
996cb5f7 8493 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
8494 key->storage == REDIS_VM_SWAPPING) {
e7546c63 8495 o = dictGetEntryVal(de);
8496 swapped = 0;
8497 } else {
8498 o = vmPreviewObject(key);
e7546c63 8499 swapped = 1;
8500 }
8501 expiretime = getExpire(db,key);
9d65a1bb 8502
8503 /* Save the key and associated value */
9d65a1bb 8504 if (o->type == REDIS_STRING) {
8505 /* Emit a SET command */
8506 char cmd[]="*3\r\n$3\r\nSET\r\n";
8507 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8508 /* Key and value */
9c8e3cee 8509 if (fwriteBulkObject(fp,key) == 0) goto werr;
8510 if (fwriteBulkObject(fp,o) == 0) goto werr;
9d65a1bb 8511 } else if (o->type == REDIS_LIST) {
8512 /* Emit the RPUSHes needed to rebuild the list */
8513 list *list = o->ptr;
8514 listNode *ln;
c7df85a4 8515 listIter li;
9d65a1bb 8516
c7df85a4 8517 listRewind(list,&li);
8518 while((ln = listNext(&li))) {
9d65a1bb 8519 char cmd[]="*3\r\n$5\r\nRPUSH\r\n";
8520 robj *eleobj = listNodeValue(ln);
8521
8522 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8523 if (fwriteBulkObject(fp,key) == 0) goto werr;
8524 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9d65a1bb 8525 }
8526 } else if (o->type == REDIS_SET) {
8527 /* Emit the SADDs needed to rebuild the set */
8528 dict *set = o->ptr;
8529 dictIterator *di = dictGetIterator(set);
8530 dictEntry *de;
8531
8532 while((de = dictNext(di)) != NULL) {
8533 char cmd[]="*3\r\n$4\r\nSADD\r\n";
8534 robj *eleobj = dictGetEntryKey(de);
8535
8536 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8537 if (fwriteBulkObject(fp,key) == 0) goto werr;
8538 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9d65a1bb 8539 }
8540 dictReleaseIterator(di);
8541 } else if (o->type == REDIS_ZSET) {
8542 /* Emit the ZADDs needed to rebuild the sorted set */
8543 zset *zs = o->ptr;
8544 dictIterator *di = dictGetIterator(zs->dict);
8545 dictEntry *de;
8546
8547 while((de = dictNext(di)) != NULL) {
8548 char cmd[]="*4\r\n$4\r\nZADD\r\n";
8549 robj *eleobj = dictGetEntryKey(de);
8550 double *score = dictGetEntryVal(de);
8551
8552 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8553 if (fwriteBulkObject(fp,key) == 0) goto werr;
9d65a1bb 8554 if (fwriteBulkDouble(fp,*score) == 0) goto werr;
9c8e3cee 8555 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9d65a1bb 8556 }
8557 dictReleaseIterator(di);
9c8e3cee 8558 } else if (o->type == REDIS_HASH) {
8559 char cmd[]="*4\r\n$4\r\nHSET\r\n";
8560
8561 /* Emit the HSETs needed to rebuild the hash */
8562 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
8563 unsigned char *p = zipmapRewind(o->ptr);
8564 unsigned char *field, *val;
8565 unsigned int flen, vlen;
8566
8567 while((p = zipmapNext(p,&field,&flen,&val,&vlen)) != NULL) {
8568 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8569 if (fwriteBulkObject(fp,key) == 0) goto werr;
8570 if (fwriteBulkString(fp,(char*)field,flen) == -1)
8571 return -1;
8572 if (fwriteBulkString(fp,(char*)val,vlen) == -1)
8573 return -1;
8574 }
8575 } else {
8576 dictIterator *di = dictGetIterator(o->ptr);
8577 dictEntry *de;
8578
8579 while((de = dictNext(di)) != NULL) {
8580 robj *field = dictGetEntryKey(de);
8581 robj *val = dictGetEntryVal(de);
8582
8583 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8584 if (fwriteBulkObject(fp,key) == 0) goto werr;
8585 if (fwriteBulkObject(fp,field) == -1) return -1;
8586 if (fwriteBulkObject(fp,val) == -1) return -1;
8587 }
8588 dictReleaseIterator(di);
8589 }
9d65a1bb 8590 } else {
f83c6cb5 8591 redisPanic("Unknown object type");
9d65a1bb 8592 }
8593 /* Save the expire time */
8594 if (expiretime != -1) {
e96e4fbf 8595 char cmd[]="*3\r\n$8\r\nEXPIREAT\r\n";
9d65a1bb 8596 /* If this key is already expired skip it */
8597 if (expiretime < now) continue;
8598 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8599 if (fwriteBulkObject(fp,key) == 0) goto werr;
9d65a1bb 8600 if (fwriteBulkLong(fp,expiretime) == 0) goto werr;
8601 }
b9bc0eef 8602 if (swapped) decrRefCount(o);
9d65a1bb 8603 }
8604 dictReleaseIterator(di);
8605 }
8606
8607 /* Make sure data will not remain on the OS's output buffers */
8608 fflush(fp);
8609 fsync(fileno(fp));
8610 fclose(fp);
e0a62c7f 8611
9d65a1bb 8612 /* Use RENAME to make sure the DB file is changed atomically only
8613 * if the generate DB file is ok. */
8614 if (rename(tmpfile,filename) == -1) {
8615 redisLog(REDIS_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno));
8616 unlink(tmpfile);
8617 return REDIS_ERR;
8618 }
8619 redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed");
8620 return REDIS_OK;
8621
8622werr:
8623 fclose(fp);
8624 unlink(tmpfile);
e96e4fbf 8625 redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
9d65a1bb 8626 if (di) dictReleaseIterator(di);
8627 return REDIS_ERR;
8628}
8629
8630/* This is how rewriting of the append only file in background works:
8631 *
8632 * 1) The user calls BGREWRITEAOF
8633 * 2) Redis calls this function, that forks():
8634 * 2a) the child rewrite the append only file in a temp file.
8635 * 2b) the parent accumulates differences in server.bgrewritebuf.
8636 * 3) When the child finished '2a' exists.
8637 * 4) The parent will trap the exit code, if it's OK, will append the
8638 * data accumulated into server.bgrewritebuf into the temp file, and
8639 * finally will rename(2) the temp file in the actual file name.
8640 * The the new file is reopened as the new append only file. Profit!
8641 */
8642static int rewriteAppendOnlyFileBackground(void) {
8643 pid_t childpid;
8644
8645 if (server.bgrewritechildpid != -1) return REDIS_ERR;
054e426d 8646 if (server.vm_enabled) waitEmptyIOJobsQueue();
9d65a1bb 8647 if ((childpid = fork()) == 0) {
8648 /* Child */
8649 char tmpfile[256];
9d65a1bb 8650
054e426d 8651 if (server.vm_enabled) vmReopenSwapFile();
8652 close(server.fd);
9d65a1bb 8653 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
8654 if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
478c2c6f 8655 _exit(0);
9d65a1bb 8656 } else {
478c2c6f 8657 _exit(1);
9d65a1bb 8658 }
8659 } else {
8660 /* Parent */
8661 if (childpid == -1) {
8662 redisLog(REDIS_WARNING,
8663 "Can't rewrite append only file in background: fork: %s",
8664 strerror(errno));
8665 return REDIS_ERR;
8666 }
8667 redisLog(REDIS_NOTICE,
8668 "Background append only file rewriting started by pid %d",childpid);
8669 server.bgrewritechildpid = childpid;
884d4b39 8670 updateDictResizePolicy();
85a83172 8671 /* We set appendseldb to -1 in order to force the next call to the
8672 * feedAppendOnlyFile() to issue a SELECT command, so the differences
8673 * accumulated by the parent into server.bgrewritebuf will start
8674 * with a SELECT statement and it will be safe to merge. */
8675 server.appendseldb = -1;
9d65a1bb 8676 return REDIS_OK;
8677 }
8678 return REDIS_OK; /* unreached */
8679}
8680
8681static void bgrewriteaofCommand(redisClient *c) {
8682 if (server.bgrewritechildpid != -1) {
8683 addReplySds(c,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
8684 return;
8685 }
8686 if (rewriteAppendOnlyFileBackground() == REDIS_OK) {
49b99ab4 8687 char *status = "+Background append only file rewriting started\r\n";
8688 addReplySds(c,sdsnew(status));
9d65a1bb 8689 } else {
8690 addReply(c,shared.err);
8691 }
8692}
8693
8694static void aofRemoveTempFile(pid_t childpid) {
8695 char tmpfile[256];
8696
8697 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid);
8698 unlink(tmpfile);
8699}
8700
996cb5f7 8701/* Virtual Memory is composed mainly of two subsystems:
8702 * - Blocking Virutal Memory
8703 * - Threaded Virtual Memory I/O
8704 * The two parts are not fully decoupled, but functions are split among two
8705 * different sections of the source code (delimited by comments) in order to
8706 * make more clear what functionality is about the blocking VM and what about
8707 * the threaded (not blocking) VM.
8708 *
8709 * Redis VM design:
8710 *
8711 * Redis VM is a blocking VM (one that blocks reading swapped values from
8712 * disk into memory when a value swapped out is needed in memory) that is made
8713 * unblocking by trying to examine the command argument vector in order to
8714 * load in background values that will likely be needed in order to exec
8715 * the command. The command is executed only once all the relevant keys
8716 * are loaded into memory.
8717 *
8718 * This basically is almost as simple of a blocking VM, but almost as parallel
8719 * as a fully non-blocking VM.
8720 */
8721
8722/* =================== Virtual Memory - Blocking Side ====================== */
054e426d 8723
75680a3c 8724static void vmInit(void) {
8725 off_t totsize;
996cb5f7 8726 int pipefds[2];
bcaa7a4f 8727 size_t stacksize;
8b5bb414 8728 struct flock fl;
75680a3c 8729
4ad37480 8730 if (server.vm_max_threads != 0)
8731 zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */
8732
054e426d 8733 redisLog(REDIS_NOTICE,"Using '%s' as swap file",server.vm_swap_file);
8b5bb414 8734 /* Try to open the old swap file, otherwise create it */
6fa987e3 8735 if ((server.vm_fp = fopen(server.vm_swap_file,"r+b")) == NULL) {
8736 server.vm_fp = fopen(server.vm_swap_file,"w+b");
8737 }
75680a3c 8738 if (server.vm_fp == NULL) {
6fa987e3 8739 redisLog(REDIS_WARNING,
8b5bb414 8740 "Can't open the swap file: %s. Exiting.",
6fa987e3 8741 strerror(errno));
75680a3c 8742 exit(1);
8743 }
8744 server.vm_fd = fileno(server.vm_fp);
8b5bb414 8745 /* Lock the swap file for writing, this is useful in order to avoid
8746 * another instance to use the same swap file for a config error. */
8747 fl.l_type = F_WRLCK;
8748 fl.l_whence = SEEK_SET;
8749 fl.l_start = fl.l_len = 0;
8750 if (fcntl(server.vm_fd,F_SETLK,&fl) == -1) {
8751 redisLog(REDIS_WARNING,
8752 "Can't lock the swap file at '%s': %s. Make sure it is not used by another Redis instance.", server.vm_swap_file, strerror(errno));
8753 exit(1);
8754 }
8755 /* Initialize */
75680a3c 8756 server.vm_next_page = 0;
8757 server.vm_near_pages = 0;
7d98e08c 8758 server.vm_stats_used_pages = 0;
8759 server.vm_stats_swapped_objects = 0;
8760 server.vm_stats_swapouts = 0;
8761 server.vm_stats_swapins = 0;
75680a3c 8762 totsize = server.vm_pages*server.vm_page_size;
8763 redisLog(REDIS_NOTICE,"Allocating %lld bytes of swap file",totsize);
8764 if (ftruncate(server.vm_fd,totsize) == -1) {
8765 redisLog(REDIS_WARNING,"Can't ftruncate swap file: %s. Exiting.",
8766 strerror(errno));
8767 exit(1);
8768 } else {
8769 redisLog(REDIS_NOTICE,"Swap file allocated with success");
8770 }
7d30035d 8771 server.vm_bitmap = zmalloc((server.vm_pages+7)/8);
f870935d 8772 redisLog(REDIS_VERBOSE,"Allocated %lld bytes page table for %lld pages",
4ef8de8a 8773 (long long) (server.vm_pages+7)/8, server.vm_pages);
7d30035d 8774 memset(server.vm_bitmap,0,(server.vm_pages+7)/8);
92f8e882 8775
996cb5f7 8776 /* Initialize threaded I/O (used by Virtual Memory) */
8777 server.io_newjobs = listCreate();
8778 server.io_processing = listCreate();
8779 server.io_processed = listCreate();
d5d55fc3 8780 server.io_ready_clients = listCreate();
92f8e882 8781 pthread_mutex_init(&server.io_mutex,NULL);
a5819310 8782 pthread_mutex_init(&server.obj_freelist_mutex,NULL);
8783 pthread_mutex_init(&server.io_swapfile_mutex,NULL);
92f8e882 8784 server.io_active_threads = 0;
996cb5f7 8785 if (pipe(pipefds) == -1) {
8786 redisLog(REDIS_WARNING,"Unable to intialized VM: pipe(2): %s. Exiting."
8787 ,strerror(errno));
8788 exit(1);
8789 }
8790 server.io_ready_pipe_read = pipefds[0];
8791 server.io_ready_pipe_write = pipefds[1];
8792 redisAssert(anetNonBlock(NULL,server.io_ready_pipe_read) != ANET_ERR);
bcaa7a4f 8793 /* LZF requires a lot of stack */
8794 pthread_attr_init(&server.io_threads_attr);
8795 pthread_attr_getstacksize(&server.io_threads_attr, &stacksize);
8796 while (stacksize < REDIS_THREAD_STACK_SIZE) stacksize *= 2;
8797 pthread_attr_setstacksize(&server.io_threads_attr, stacksize);
b9bc0eef 8798 /* Listen for events in the threaded I/O pipe */
8799 if (aeCreateFileEvent(server.el, server.io_ready_pipe_read, AE_READABLE,
8800 vmThreadedIOCompletedJob, NULL) == AE_ERR)
8801 oom("creating file event");
75680a3c 8802}
8803
06224fec 8804/* Mark the page as used */
8805static void vmMarkPageUsed(off_t page) {
8806 off_t byte = page/8;
8807 int bit = page&7;
970e10bb 8808 redisAssert(vmFreePage(page) == 1);
06224fec 8809 server.vm_bitmap[byte] |= 1<<bit;
8810}
8811
8812/* Mark N contiguous pages as used, with 'page' being the first. */
8813static void vmMarkPagesUsed(off_t page, off_t count) {
8814 off_t j;
8815
8816 for (j = 0; j < count; j++)
7d30035d 8817 vmMarkPageUsed(page+j);
7d98e08c 8818 server.vm_stats_used_pages += count;
7c775e09 8819 redisLog(REDIS_DEBUG,"Mark USED pages: %lld pages at %lld\n",
8820 (long long)count, (long long)page);
06224fec 8821}
8822
8823/* Mark the page as free */
8824static void vmMarkPageFree(off_t page) {
8825 off_t byte = page/8;
8826 int bit = page&7;
970e10bb 8827 redisAssert(vmFreePage(page) == 0);
06224fec 8828 server.vm_bitmap[byte] &= ~(1<<bit);
8829}
8830
8831/* Mark N contiguous pages as free, with 'page' being the first. */
8832static void vmMarkPagesFree(off_t page, off_t count) {
8833 off_t j;
8834
8835 for (j = 0; j < count; j++)
7d30035d 8836 vmMarkPageFree(page+j);
7d98e08c 8837 server.vm_stats_used_pages -= count;
7c775e09 8838 redisLog(REDIS_DEBUG,"Mark FREE pages: %lld pages at %lld\n",
8839 (long long)count, (long long)page);
06224fec 8840}
8841
8842/* Test if the page is free */
8843static int vmFreePage(off_t page) {
8844 off_t byte = page/8;
8845 int bit = page&7;
7d30035d 8846 return (server.vm_bitmap[byte] & (1<<bit)) == 0;
06224fec 8847}
8848
8849/* Find N contiguous free pages storing the first page of the cluster in *first.
e0a62c7f 8850 * Returns REDIS_OK if it was able to find N contiguous pages, otherwise
3a66edc7 8851 * REDIS_ERR is returned.
06224fec 8852 *
8853 * This function uses a simple algorithm: we try to allocate
8854 * REDIS_VM_MAX_NEAR_PAGES sequentially, when we reach this limit we start
8855 * again from the start of the swap file searching for free spaces.
8856 *
8857 * If it looks pretty clear that there are no free pages near our offset
8858 * we try to find less populated places doing a forward jump of
8859 * REDIS_VM_MAX_RANDOM_JUMP, then we start scanning again a few pages
8860 * without hurry, and then we jump again and so forth...
e0a62c7f 8861 *
06224fec 8862 * This function can be improved using a free list to avoid to guess
8863 * too much, since we could collect data about freed pages.
8864 *
8865 * note: I implemented this function just after watching an episode of
8866 * Battlestar Galactica, where the hybrid was continuing to say "JUMP!"
8867 */
c7df85a4 8868static int vmFindContiguousPages(off_t *first, off_t n) {
06224fec 8869 off_t base, offset = 0, since_jump = 0, numfree = 0;
8870
8871 if (server.vm_near_pages == REDIS_VM_MAX_NEAR_PAGES) {
8872 server.vm_near_pages = 0;
8873 server.vm_next_page = 0;
8874 }
8875 server.vm_near_pages++; /* Yet another try for pages near to the old ones */
8876 base = server.vm_next_page;
8877
8878 while(offset < server.vm_pages) {
8879 off_t this = base+offset;
8880
8881 /* If we overflow, restart from page zero */
8882 if (this >= server.vm_pages) {
8883 this -= server.vm_pages;
8884 if (this == 0) {
8885 /* Just overflowed, what we found on tail is no longer
8886 * interesting, as it's no longer contiguous. */
8887 numfree = 0;
8888 }
8889 }
8890 if (vmFreePage(this)) {
8891 /* This is a free page */
8892 numfree++;
8893 /* Already got N free pages? Return to the caller, with success */
8894 if (numfree == n) {
7d30035d 8895 *first = this-(n-1);
8896 server.vm_next_page = this+1;
7c775e09 8897 redisLog(REDIS_DEBUG, "FOUND CONTIGUOUS PAGES: %lld pages at %lld\n", (long long) n, (long long) *first);
3a66edc7 8898 return REDIS_OK;
06224fec 8899 }
8900 } else {
8901 /* The current one is not a free page */
8902 numfree = 0;
8903 }
8904
8905 /* Fast-forward if the current page is not free and we already
8906 * searched enough near this place. */
8907 since_jump++;
8908 if (!numfree && since_jump >= REDIS_VM_MAX_RANDOM_JUMP/4) {
8909 offset += random() % REDIS_VM_MAX_RANDOM_JUMP;
8910 since_jump = 0;
8911 /* Note that even if we rewind after the jump, we are don't need
8912 * to make sure numfree is set to zero as we only jump *if* it
8913 * is set to zero. */
8914 } else {
8915 /* Otherwise just check the next page */
8916 offset++;
8917 }
8918 }
3a66edc7 8919 return REDIS_ERR;
8920}
8921
a5819310 8922/* Write the specified object at the specified page of the swap file */
8923static int vmWriteObjectOnSwap(robj *o, off_t page) {
8924 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
8925 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
8926 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8927 redisLog(REDIS_WARNING,
9ebed7cf 8928 "Critical VM problem in vmWriteObjectOnSwap(): can't seek: %s",
a5819310 8929 strerror(errno));
8930 return REDIS_ERR;
8931 }
8932 rdbSaveObject(server.vm_fp,o);
ba76a8f9 8933 fflush(server.vm_fp);
a5819310 8934 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8935 return REDIS_OK;
8936}
8937
3a66edc7 8938/* Swap the 'val' object relative to 'key' into disk. Store all the information
8939 * needed to later retrieve the object into the key object.
8940 * If we can't find enough contiguous empty pages to swap the object on disk
8941 * REDIS_ERR is returned. */
a69a0c9c 8942static int vmSwapObjectBlocking(robj *key, robj *val) {
b9bc0eef 8943 off_t pages = rdbSavedObjectPages(val,NULL);
3a66edc7 8944 off_t page;
8945
8946 assert(key->storage == REDIS_VM_MEMORY);
4ef8de8a 8947 assert(key->refcount == 1);
3a66edc7 8948 if (vmFindContiguousPages(&page,pages) == REDIS_ERR) return REDIS_ERR;
a5819310 8949 if (vmWriteObjectOnSwap(val,page) == REDIS_ERR) return REDIS_ERR;
3a66edc7 8950 key->vm.page = page;
8951 key->vm.usedpages = pages;
8952 key->storage = REDIS_VM_SWAPPED;
d894161b 8953 key->vtype = val->type;
3a66edc7 8954 decrRefCount(val); /* Deallocate the object from memory. */
8955 vmMarkPagesUsed(page,pages);
7d30035d 8956 redisLog(REDIS_DEBUG,"VM: object %s swapped out at %lld (%lld pages)",
8957 (unsigned char*) key->ptr,
8958 (unsigned long long) page, (unsigned long long) pages);
7d98e08c 8959 server.vm_stats_swapped_objects++;
8960 server.vm_stats_swapouts++;
3a66edc7 8961 return REDIS_OK;
8962}
8963
a5819310 8964static robj *vmReadObjectFromSwap(off_t page, int type) {
8965 robj *o;
3a66edc7 8966
a5819310 8967 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
8968 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
3a66edc7 8969 redisLog(REDIS_WARNING,
d5d55fc3 8970 "Unrecoverable VM problem in vmReadObjectFromSwap(): can't seek: %s",
3a66edc7 8971 strerror(errno));
478c2c6f 8972 _exit(1);
3a66edc7 8973 }
a5819310 8974 o = rdbLoadObject(type,server.vm_fp);
8975 if (o == NULL) {
d5d55fc3 8976 redisLog(REDIS_WARNING, "Unrecoverable VM problem in vmReadObjectFromSwap(): can't load object from swap file: %s", strerror(errno));
478c2c6f 8977 _exit(1);
3a66edc7 8978 }
a5819310 8979 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8980 return o;
8981}
8982
8983/* Load the value object relative to the 'key' object from swap to memory.
8984 * The newly allocated object is returned.
8985 *
8986 * If preview is true the unserialized object is returned to the caller but
8987 * no changes are made to the key object, nor the pages are marked as freed */
8988static robj *vmGenericLoadObject(robj *key, int preview) {
8989 robj *val;
8990
d5d55fc3 8991 redisAssert(key->storage == REDIS_VM_SWAPPED || key->storage == REDIS_VM_LOADING);
a5819310 8992 val = vmReadObjectFromSwap(key->vm.page,key->vtype);
7e69548d 8993 if (!preview) {
8994 key->storage = REDIS_VM_MEMORY;
8995 key->vm.atime = server.unixtime;
8996 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
8997 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk",
8998 (unsigned char*) key->ptr);
7d98e08c 8999 server.vm_stats_swapped_objects--;
38aba9a1 9000 } else {
9001 redisLog(REDIS_DEBUG, "VM: object %s previewed from disk",
9002 (unsigned char*) key->ptr);
7e69548d 9003 }
7d98e08c 9004 server.vm_stats_swapins++;
3a66edc7 9005 return val;
06224fec 9006}
9007
7e69548d 9008/* Plain object loading, from swap to memory */
9009static robj *vmLoadObject(robj *key) {
996cb5f7 9010 /* If we are loading the object in background, stop it, we
9011 * need to load this object synchronously ASAP. */
9012 if (key->storage == REDIS_VM_LOADING)
9013 vmCancelThreadedIOJob(key);
7e69548d 9014 return vmGenericLoadObject(key,0);
9015}
9016
9017/* Just load the value on disk, without to modify the key.
9018 * This is useful when we want to perform some operation on the value
9019 * without to really bring it from swap to memory, like while saving the
9020 * dataset or rewriting the append only log. */
9021static robj *vmPreviewObject(robj *key) {
9022 return vmGenericLoadObject(key,1);
9023}
9024
4ef8de8a 9025/* How a good candidate is this object for swapping?
9026 * The better candidate it is, the greater the returned value.
9027 *
9028 * Currently we try to perform a fast estimation of the object size in
9029 * memory, and combine it with aging informations.
9030 *
9031 * Basically swappability = idle-time * log(estimated size)
9032 *
9033 * Bigger objects are preferred over smaller objects, but not
9034 * proportionally, this is why we use the logarithm. This algorithm is
9035 * just a first try and will probably be tuned later. */
9036static double computeObjectSwappability(robj *o) {
9037 time_t age = server.unixtime - o->vm.atime;
9038 long asize = 0;
9039 list *l;
9040 dict *d;
9041 struct dictEntry *de;
9042 int z;
9043
9044 if (age <= 0) return 0;
9045 switch(o->type) {
9046 case REDIS_STRING:
9047 if (o->encoding != REDIS_ENCODING_RAW) {
9048 asize = sizeof(*o);
9049 } else {
9050 asize = sdslen(o->ptr)+sizeof(*o)+sizeof(long)*2;
9051 }
9052 break;
9053 case REDIS_LIST:
9054 l = o->ptr;
9055 listNode *ln = listFirst(l);
9056
9057 asize = sizeof(list);
9058 if (ln) {
9059 robj *ele = ln->value;
9060 long elesize;
9061
9062 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9063 (sizeof(*o)+sdslen(ele->ptr)) :
9064 sizeof(*o);
9065 asize += (sizeof(listNode)+elesize)*listLength(l);
9066 }
9067 break;
9068 case REDIS_SET:
9069 case REDIS_ZSET:
9070 z = (o->type == REDIS_ZSET);
9071 d = z ? ((zset*)o->ptr)->dict : o->ptr;
9072
9073 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9074 if (z) asize += sizeof(zset)-sizeof(dict);
9075 if (dictSize(d)) {
9076 long elesize;
9077 robj *ele;
9078
9079 de = dictGetRandomKey(d);
9080 ele = dictGetEntryKey(de);
9081 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9082 (sizeof(*o)+sdslen(ele->ptr)) :
9083 sizeof(*o);
9084 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9085 if (z) asize += sizeof(zskiplistNode)*dictSize(d);
9086 }
9087 break;
a97b9060 9088 case REDIS_HASH:
9089 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
9090 unsigned char *p = zipmapRewind((unsigned char*)o->ptr);
9091 unsigned int len = zipmapLen((unsigned char*)o->ptr);
9092 unsigned int klen, vlen;
9093 unsigned char *key, *val;
9094
9095 if ((p = zipmapNext(p,&key,&klen,&val,&vlen)) == NULL) {
9096 klen = 0;
9097 vlen = 0;
9098 }
9099 asize = len*(klen+vlen+3);
9100 } else if (o->encoding == REDIS_ENCODING_HT) {
9101 d = o->ptr;
9102 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9103 if (dictSize(d)) {
9104 long elesize;
9105 robj *ele;
9106
9107 de = dictGetRandomKey(d);
9108 ele = dictGetEntryKey(de);
9109 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9110 (sizeof(*o)+sdslen(ele->ptr)) :
9111 sizeof(*o);
9112 ele = dictGetEntryVal(de);
9113 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9114 (sizeof(*o)+sdslen(ele->ptr)) :
9115 sizeof(*o);
9116 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9117 }
9118 }
9119 break;
4ef8de8a 9120 }
c8c72447 9121 return (double)age*log(1+asize);
4ef8de8a 9122}
9123
9124/* Try to swap an object that's a good candidate for swapping.
9125 * Returns REDIS_OK if the object was swapped, REDIS_ERR if it's not possible
a69a0c9c 9126 * to swap any object at all.
9127 *
9128 * If 'usethreaded' is true, Redis will try to swap the object in background
9129 * using I/O threads. */
9130static int vmSwapOneObject(int usethreads) {
4ef8de8a 9131 int j, i;
9132 struct dictEntry *best = NULL;
9133 double best_swappability = 0;
b9bc0eef 9134 redisDb *best_db = NULL;
4ef8de8a 9135 robj *key, *val;
9136
9137 for (j = 0; j < server.dbnum; j++) {
9138 redisDb *db = server.db+j;
b72f6a4b 9139 /* Why maxtries is set to 100?
9140 * Because this way (usually) we'll find 1 object even if just 1% - 2%
9141 * are swappable objects */
b0d8747d 9142 int maxtries = 100;
4ef8de8a 9143
9144 if (dictSize(db->dict) == 0) continue;
9145 for (i = 0; i < 5; i++) {
9146 dictEntry *de;
9147 double swappability;
9148
e3cadb8a 9149 if (maxtries) maxtries--;
4ef8de8a 9150 de = dictGetRandomKey(db->dict);
9151 key = dictGetEntryKey(de);
9152 val = dictGetEntryVal(de);
1064ef87 9153 /* Only swap objects that are currently in memory.
9154 *
9155 * Also don't swap shared objects if threaded VM is on, as we
9156 * try to ensure that the main thread does not touch the
9157 * object while the I/O thread is using it, but we can't
9158 * control other keys without adding additional mutex. */
9159 if (key->storage != REDIS_VM_MEMORY ||
9160 (server.vm_max_threads != 0 && val->refcount != 1)) {
e3cadb8a 9161 if (maxtries) i--; /* don't count this try */
9162 continue;
9163 }
4ef8de8a 9164 swappability = computeObjectSwappability(val);
9165 if (!best || swappability > best_swappability) {
9166 best = de;
9167 best_swappability = swappability;
b9bc0eef 9168 best_db = db;
4ef8de8a 9169 }
9170 }
9171 }
7c775e09 9172 if (best == NULL) return REDIS_ERR;
4ef8de8a 9173 key = dictGetEntryKey(best);
9174 val = dictGetEntryVal(best);
9175
e3cadb8a 9176 redisLog(REDIS_DEBUG,"Key with best swappability: %s, %f",
4ef8de8a 9177 key->ptr, best_swappability);
9178
9179 /* Unshare the key if needed */
9180 if (key->refcount > 1) {
9181 robj *newkey = dupStringObject(key);
9182 decrRefCount(key);
9183 key = dictGetEntryKey(best) = newkey;
9184 }
9185 /* Swap it */
a69a0c9c 9186 if (usethreads) {
b9bc0eef 9187 vmSwapObjectThreaded(key,val,best_db);
4ef8de8a 9188 return REDIS_OK;
9189 } else {
a69a0c9c 9190 if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
9191 dictGetEntryVal(best) = NULL;
9192 return REDIS_OK;
9193 } else {
9194 return REDIS_ERR;
9195 }
4ef8de8a 9196 }
9197}
9198
a69a0c9c 9199static int vmSwapOneObjectBlocking() {
9200 return vmSwapOneObject(0);
9201}
9202
9203static int vmSwapOneObjectThreaded() {
9204 return vmSwapOneObject(1);
9205}
9206
7e69548d 9207/* Return true if it's safe to swap out objects in a given moment.
9208 * Basically we don't want to swap objects out while there is a BGSAVE
9209 * or a BGAEOREWRITE running in backgroud. */
9210static int vmCanSwapOut(void) {
9211 return (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1);
9212}
9213
1b03836c 9214/* Delete a key if swapped. Returns 1 if the key was found, was swapped
9215 * and was deleted. Otherwise 0 is returned. */
9216static int deleteIfSwapped(redisDb *db, robj *key) {
9217 dictEntry *de;
9218 robj *foundkey;
9219
9220 if ((de = dictFind(db->dict,key)) == NULL) return 0;
9221 foundkey = dictGetEntryKey(de);
9222 if (foundkey->storage == REDIS_VM_MEMORY) return 0;
9223 deleteKey(db,key);
9224 return 1;
9225}
9226
996cb5f7 9227/* =================== Virtual Memory - Threaded I/O ======================= */
9228
b9bc0eef 9229static void freeIOJob(iojob *j) {
d5d55fc3 9230 if ((j->type == REDIS_IOJOB_PREPARE_SWAP ||
9231 j->type == REDIS_IOJOB_DO_SWAP ||
9232 j->type == REDIS_IOJOB_LOAD) && j->val != NULL)
b9bc0eef 9233 decrRefCount(j->val);
78ebe4c8 9234 /* We don't decrRefCount the j->key field as we did't incremented
9235 * the count creating IO Jobs. This is because the key field here is
9236 * just used as an indentifier and if a key is removed the Job should
9237 * never be touched again. */
b9bc0eef 9238 zfree(j);
9239}
9240
996cb5f7 9241/* Every time a thread finished a Job, it writes a byte into the write side
9242 * of an unix pipe in order to "awake" the main thread, and this function
9243 * is called. */
9244static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata,
9245 int mask)
9246{
9247 char buf[1];
b0d8747d 9248 int retval, processed = 0, toprocess = -1, trytoswap = 1;
996cb5f7 9249 REDIS_NOTUSED(el);
9250 REDIS_NOTUSED(mask);
9251 REDIS_NOTUSED(privdata);
9252
9253 /* For every byte we read in the read side of the pipe, there is one
9254 * I/O job completed to process. */
9255 while((retval = read(fd,buf,1)) == 1) {
b9bc0eef 9256 iojob *j;
9257 listNode *ln;
9258 robj *key;
9259 struct dictEntry *de;
9260
996cb5f7 9261 redisLog(REDIS_DEBUG,"Processing I/O completed job");
b9bc0eef 9262
9263 /* Get the processed element (the oldest one) */
9264 lockThreadedIO();
1064ef87 9265 assert(listLength(server.io_processed) != 0);
f6c0bba8 9266 if (toprocess == -1) {
9267 toprocess = (listLength(server.io_processed)*REDIS_MAX_COMPLETED_JOBS_PROCESSED)/100;
9268 if (toprocess <= 0) toprocess = 1;
9269 }
b9bc0eef 9270 ln = listFirst(server.io_processed);
9271 j = ln->value;
9272 listDelNode(server.io_processed,ln);
9273 unlockThreadedIO();
9274 /* If this job is marked as canceled, just ignore it */
9275 if (j->canceled) {
9276 freeIOJob(j);
9277 continue;
9278 }
9279 /* Post process it in the main thread, as there are things we
9280 * can do just here to avoid race conditions and/or invasive locks */
6c96ba7d 9281 redisLog(REDIS_DEBUG,"Job %p type: %d, key at %p (%s) refcount: %d\n", (void*) j, j->type, (void*)j->key, (char*)j->key->ptr, j->key->refcount);
b9bc0eef 9282 de = dictFind(j->db->dict,j->key);
9283 assert(de != NULL);
9284 key = dictGetEntryKey(de);
9285 if (j->type == REDIS_IOJOB_LOAD) {
d5d55fc3 9286 redisDb *db;
9287
b9bc0eef 9288 /* Key loaded, bring it at home */
9289 key->storage = REDIS_VM_MEMORY;
9290 key->vm.atime = server.unixtime;
9291 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
9292 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk (threaded)",
9293 (unsigned char*) key->ptr);
9294 server.vm_stats_swapped_objects--;
9295 server.vm_stats_swapins++;
d5d55fc3 9296 dictGetEntryVal(de) = j->val;
9297 incrRefCount(j->val);
9298 db = j->db;
b9bc0eef 9299 freeIOJob(j);
d5d55fc3 9300 /* Handle clients waiting for this key to be loaded. */
9301 handleClientsBlockedOnSwappedKey(db,key);
b9bc0eef 9302 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
9303 /* Now we know the amount of pages required to swap this object.
9304 * Let's find some space for it, and queue this task again
9305 * rebranded as REDIS_IOJOB_DO_SWAP. */
054e426d 9306 if (!vmCanSwapOut() ||
9307 vmFindContiguousPages(&j->page,j->pages) == REDIS_ERR)
9308 {
9309 /* Ooops... no space or we can't swap as there is
9310 * a fork()ed Redis trying to save stuff on disk. */
b9bc0eef 9311 freeIOJob(j);
054e426d 9312 key->storage = REDIS_VM_MEMORY; /* undo operation */
b9bc0eef 9313 } else {
c7df85a4 9314 /* Note that we need to mark this pages as used now,
9315 * if the job will be canceled, we'll mark them as freed
9316 * again. */
9317 vmMarkPagesUsed(j->page,j->pages);
b9bc0eef 9318 j->type = REDIS_IOJOB_DO_SWAP;
9319 lockThreadedIO();
9320 queueIOJob(j);
9321 unlockThreadedIO();
9322 }
9323 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
9324 robj *val;
9325
9326 /* Key swapped. We can finally free some memory. */
6c96ba7d 9327 if (key->storage != REDIS_VM_SWAPPING) {
9328 printf("key->storage: %d\n",key->storage);
9329 printf("key->name: %s\n",(char*)key->ptr);
9330 printf("key->refcount: %d\n",key->refcount);
9331 printf("val: %p\n",(void*)j->val);
9332 printf("val->type: %d\n",j->val->type);
9333 printf("val->ptr: %s\n",(char*)j->val->ptr);
9334 }
9335 redisAssert(key->storage == REDIS_VM_SWAPPING);
b9bc0eef 9336 val = dictGetEntryVal(de);
9337 key->vm.page = j->page;
9338 key->vm.usedpages = j->pages;
9339 key->storage = REDIS_VM_SWAPPED;
9340 key->vtype = j->val->type;
9341 decrRefCount(val); /* Deallocate the object from memory. */
f11b8647 9342 dictGetEntryVal(de) = NULL;
b9bc0eef 9343 redisLog(REDIS_DEBUG,
9344 "VM: object %s swapped out at %lld (%lld pages) (threaded)",
9345 (unsigned char*) key->ptr,
9346 (unsigned long long) j->page, (unsigned long long) j->pages);
9347 server.vm_stats_swapped_objects++;
9348 server.vm_stats_swapouts++;
9349 freeIOJob(j);
f11b8647 9350 /* Put a few more swap requests in queue if we are still
9351 * out of memory */
b0d8747d 9352 if (trytoswap && vmCanSwapOut() &&
9353 zmalloc_used_memory() > server.vm_max_memory)
9354 {
f11b8647 9355 int more = 1;
9356 while(more) {
9357 lockThreadedIO();
9358 more = listLength(server.io_newjobs) <
9359 (unsigned) server.vm_max_threads;
9360 unlockThreadedIO();
9361 /* Don't waste CPU time if swappable objects are rare. */
b0d8747d 9362 if (vmSwapOneObjectThreaded() == REDIS_ERR) {
9363 trytoswap = 0;
9364 break;
9365 }
f11b8647 9366 }
9367 }
b9bc0eef 9368 }
c953f24b 9369 processed++;
f6c0bba8 9370 if (processed == toprocess) return;
996cb5f7 9371 }
9372 if (retval < 0 && errno != EAGAIN) {
9373 redisLog(REDIS_WARNING,
9374 "WARNING: read(2) error in vmThreadedIOCompletedJob() %s",
9375 strerror(errno));
9376 }
9377}
9378
9379static void lockThreadedIO(void) {
9380 pthread_mutex_lock(&server.io_mutex);
9381}
9382
9383static void unlockThreadedIO(void) {
9384 pthread_mutex_unlock(&server.io_mutex);
9385}
9386
9387/* Remove the specified object from the threaded I/O queue if still not
9388 * processed, otherwise make sure to flag it as canceled. */
9389static void vmCancelThreadedIOJob(robj *o) {
9390 list *lists[3] = {
6c96ba7d 9391 server.io_newjobs, /* 0 */
9392 server.io_processing, /* 1 */
9393 server.io_processed /* 2 */
996cb5f7 9394 };
9395 int i;
9396
9397 assert(o->storage == REDIS_VM_LOADING || o->storage == REDIS_VM_SWAPPING);
2e111efe 9398again:
996cb5f7 9399 lockThreadedIO();
9400 /* Search for a matching key in one of the queues */
9401 for (i = 0; i < 3; i++) {
9402 listNode *ln;
c7df85a4 9403 listIter li;
996cb5f7 9404
c7df85a4 9405 listRewind(lists[i],&li);
9406 while ((ln = listNext(&li)) != NULL) {
996cb5f7 9407 iojob *job = ln->value;
9408
6c96ba7d 9409 if (job->canceled) continue; /* Skip this, already canceled. */
78ebe4c8 9410 if (job->key == o) {
970e10bb 9411 redisLog(REDIS_DEBUG,"*** CANCELED %p (%s) (type %d) (LIST ID %d)\n",
9412 (void*)job, (char*)o->ptr, job->type, i);
427a2153 9413 /* Mark the pages as free since the swap didn't happened
9414 * or happened but is now discarded. */
970e10bb 9415 if (i != 1 && job->type == REDIS_IOJOB_DO_SWAP)
427a2153 9416 vmMarkPagesFree(job->page,job->pages);
9417 /* Cancel the job. It depends on the list the job is
9418 * living in. */
996cb5f7 9419 switch(i) {
9420 case 0: /* io_newjobs */
6c96ba7d 9421 /* If the job was yet not processed the best thing to do
996cb5f7 9422 * is to remove it from the queue at all */
6c96ba7d 9423 freeIOJob(job);
996cb5f7 9424 listDelNode(lists[i],ln);
9425 break;
9426 case 1: /* io_processing */
d5d55fc3 9427 /* Oh Shi- the thread is messing with the Job:
9428 *
9429 * Probably it's accessing the object if this is a
9430 * PREPARE_SWAP or DO_SWAP job.
9431 * If it's a LOAD job it may be reading from disk and
9432 * if we don't wait for the job to terminate before to
9433 * cancel it, maybe in a few microseconds data can be
9434 * corrupted in this pages. So the short story is:
9435 *
9436 * Better to wait for the job to move into the
9437 * next queue (processed)... */
9438
9439 /* We try again and again until the job is completed. */
9440 unlockThreadedIO();
9441 /* But let's wait some time for the I/O thread
9442 * to finish with this job. After all this condition
9443 * should be very rare. */
9444 usleep(1);
9445 goto again;
996cb5f7 9446 case 2: /* io_processed */
2e111efe 9447 /* The job was already processed, that's easy...
9448 * just mark it as canceled so that we'll ignore it
9449 * when processing completed jobs. */
996cb5f7 9450 job->canceled = 1;
9451 break;
9452 }
c7df85a4 9453 /* Finally we have to adjust the storage type of the object
9454 * in order to "UNDO" the operaiton. */
996cb5f7 9455 if (o->storage == REDIS_VM_LOADING)
9456 o->storage = REDIS_VM_SWAPPED;
9457 else if (o->storage == REDIS_VM_SWAPPING)
9458 o->storage = REDIS_VM_MEMORY;
9459 unlockThreadedIO();
9460 return;
9461 }
9462 }
9463 }
9464 unlockThreadedIO();
9465 assert(1 != 1); /* We should never reach this */
9466}
9467
b9bc0eef 9468static void *IOThreadEntryPoint(void *arg) {
9469 iojob *j;
9470 listNode *ln;
9471 REDIS_NOTUSED(arg);
9472
9473 pthread_detach(pthread_self());
9474 while(1) {
9475 /* Get a new job to process */
9476 lockThreadedIO();
9477 if (listLength(server.io_newjobs) == 0) {
9478 /* No new jobs in queue, exit. */
9ebed7cf 9479 redisLog(REDIS_DEBUG,"Thread %ld exiting, nothing to do",
9480 (long) pthread_self());
b9bc0eef 9481 server.io_active_threads--;
9482 unlockThreadedIO();
9483 return NULL;
9484 }
9485 ln = listFirst(server.io_newjobs);
9486 j = ln->value;
9487 listDelNode(server.io_newjobs,ln);
9488 /* Add the job in the processing queue */
9489 j->thread = pthread_self();
9490 listAddNodeTail(server.io_processing,j);
9491 ln = listLast(server.io_processing); /* We use ln later to remove it */
9492 unlockThreadedIO();
9ebed7cf 9493 redisLog(REDIS_DEBUG,"Thread %ld got a new job (type %d): %p about key '%s'",
9494 (long) pthread_self(), j->type, (void*)j, (char*)j->key->ptr);
b9bc0eef 9495
9496 /* Process the Job */
9497 if (j->type == REDIS_IOJOB_LOAD) {
d5d55fc3 9498 j->val = vmReadObjectFromSwap(j->page,j->key->vtype);
b9bc0eef 9499 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
9500 FILE *fp = fopen("/dev/null","w+");
9501 j->pages = rdbSavedObjectPages(j->val,fp);
9502 fclose(fp);
9503 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
a5819310 9504 if (vmWriteObjectOnSwap(j->val,j->page) == REDIS_ERR)
9505 j->canceled = 1;
b9bc0eef 9506 }
9507
9508 /* Done: insert the job into the processed queue */
9ebed7cf 9509 redisLog(REDIS_DEBUG,"Thread %ld completed the job: %p (key %s)",
9510 (long) pthread_self(), (void*)j, (char*)j->key->ptr);
b9bc0eef 9511 lockThreadedIO();
9512 listDelNode(server.io_processing,ln);
9513 listAddNodeTail(server.io_processed,j);
9514 unlockThreadedIO();
e0a62c7f 9515
b9bc0eef 9516 /* Signal the main thread there is new stuff to process */
9517 assert(write(server.io_ready_pipe_write,"x",1) == 1);
9518 }
9519 return NULL; /* never reached */
9520}
9521
9522static void spawnIOThread(void) {
9523 pthread_t thread;
478c2c6f 9524 sigset_t mask, omask;
a97b9060 9525 int err;
b9bc0eef 9526
478c2c6f 9527 sigemptyset(&mask);
9528 sigaddset(&mask,SIGCHLD);
9529 sigaddset(&mask,SIGHUP);
9530 sigaddset(&mask,SIGPIPE);
9531 pthread_sigmask(SIG_SETMASK, &mask, &omask);
a97b9060 9532 while ((err = pthread_create(&thread,&server.io_threads_attr,IOThreadEntryPoint,NULL)) != 0) {
9533 redisLog(REDIS_WARNING,"Unable to spawn an I/O thread: %s",
9534 strerror(err));
9535 usleep(1000000);
9536 }
478c2c6f 9537 pthread_sigmask(SIG_SETMASK, &omask, NULL);
b9bc0eef 9538 server.io_active_threads++;
9539}
9540
4ee9488d 9541/* We need to wait for the last thread to exit before we are able to
9542 * fork() in order to BGSAVE or BGREWRITEAOF. */
054e426d 9543static void waitEmptyIOJobsQueue(void) {
4ee9488d 9544 while(1) {
76b7233a 9545 int io_processed_len;
9546
4ee9488d 9547 lockThreadedIO();
054e426d 9548 if (listLength(server.io_newjobs) == 0 &&
9549 listLength(server.io_processing) == 0 &&
9550 server.io_active_threads == 0)
9551 {
4ee9488d 9552 unlockThreadedIO();
9553 return;
9554 }
76b7233a 9555 /* While waiting for empty jobs queue condition we post-process some
9556 * finshed job, as I/O threads may be hanging trying to write against
9557 * the io_ready_pipe_write FD but there are so much pending jobs that
9558 * it's blocking. */
9559 io_processed_len = listLength(server.io_processed);
4ee9488d 9560 unlockThreadedIO();
76b7233a 9561 if (io_processed_len) {
9562 vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read,NULL,0);
9563 usleep(1000); /* 1 millisecond */
9564 } else {
9565 usleep(10000); /* 10 milliseconds */
9566 }
4ee9488d 9567 }
9568}
9569
054e426d 9570static void vmReopenSwapFile(void) {
478c2c6f 9571 /* Note: we don't close the old one as we are in the child process
9572 * and don't want to mess at all with the original file object. */
054e426d 9573 server.vm_fp = fopen(server.vm_swap_file,"r+b");
9574 if (server.vm_fp == NULL) {
9575 redisLog(REDIS_WARNING,"Can't re-open the VM swap file: %s. Exiting.",
9576 server.vm_swap_file);
478c2c6f 9577 _exit(1);
054e426d 9578 }
9579 server.vm_fd = fileno(server.vm_fp);
9580}
9581
b9bc0eef 9582/* This function must be called while with threaded IO locked */
9583static void queueIOJob(iojob *j) {
6c96ba7d 9584 redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n",
9585 (void*)j, j->type, (char*)j->key->ptr);
b9bc0eef 9586 listAddNodeTail(server.io_newjobs,j);
9587 if (server.io_active_threads < server.vm_max_threads)
9588 spawnIOThread();
9589}
9590
9591static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db) {
9592 iojob *j;
e0a62c7f 9593
b9bc0eef 9594 assert(key->storage == REDIS_VM_MEMORY);
9595 assert(key->refcount == 1);
9596
9597 j = zmalloc(sizeof(*j));
9598 j->type = REDIS_IOJOB_PREPARE_SWAP;
9599 j->db = db;
78ebe4c8 9600 j->key = key;
b9bc0eef 9601 j->val = val;
9602 incrRefCount(val);
9603 j->canceled = 0;
9604 j->thread = (pthread_t) -1;
f11b8647 9605 key->storage = REDIS_VM_SWAPPING;
b9bc0eef 9606
9607 lockThreadedIO();
9608 queueIOJob(j);
9609 unlockThreadedIO();
9610 return REDIS_OK;
9611}
9612
b0d8747d 9613/* ============ Virtual Memory - Blocking clients on missing keys =========== */
9614
d5d55fc3 9615/* This function makes the clinet 'c' waiting for the key 'key' to be loaded.
9616 * If there is not already a job loading the key, it is craeted.
9617 * The key is added to the io_keys list in the client structure, and also
9618 * in the hash table mapping swapped keys to waiting clients, that is,
9619 * server.io_waited_keys. */
9620static int waitForSwappedKey(redisClient *c, robj *key) {
9621 struct dictEntry *de;
9622 robj *o;
9623 list *l;
9624
9625 /* If the key does not exist or is already in RAM we don't need to
9626 * block the client at all. */
9627 de = dictFind(c->db->dict,key);
9628 if (de == NULL) return 0;
9629 o = dictGetEntryKey(de);
9630 if (o->storage == REDIS_VM_MEMORY) {
9631 return 0;
9632 } else if (o->storage == REDIS_VM_SWAPPING) {
9633 /* We were swapping the key, undo it! */
9634 vmCancelThreadedIOJob(o);
9635 return 0;
9636 }
e0a62c7f 9637
d5d55fc3 9638 /* OK: the key is either swapped, or being loaded just now. */
9639
9640 /* Add the key to the list of keys this client is waiting for.
9641 * This maps clients to keys they are waiting for. */
9642 listAddNodeTail(c->io_keys,key);
9643 incrRefCount(key);
9644
9645 /* Add the client to the swapped keys => clients waiting map. */
9646 de = dictFind(c->db->io_keys,key);
9647 if (de == NULL) {
9648 int retval;
9649
9650 /* For every key we take a list of clients blocked for it */
9651 l = listCreate();
9652 retval = dictAdd(c->db->io_keys,key,l);
9653 incrRefCount(key);
9654 assert(retval == DICT_OK);
9655 } else {
9656 l = dictGetEntryVal(de);
9657 }
9658 listAddNodeTail(l,c);
9659
9660 /* Are we already loading the key from disk? If not create a job */
9661 if (o->storage == REDIS_VM_SWAPPED) {
9662 iojob *j;
9663
9664 o->storage = REDIS_VM_LOADING;
9665 j = zmalloc(sizeof(*j));
9666 j->type = REDIS_IOJOB_LOAD;
9667 j->db = c->db;
78ebe4c8 9668 j->key = o;
d5d55fc3 9669 j->key->vtype = o->vtype;
9670 j->page = o->vm.page;
9671 j->val = NULL;
9672 j->canceled = 0;
9673 j->thread = (pthread_t) -1;
9674 lockThreadedIO();
9675 queueIOJob(j);
9676 unlockThreadedIO();
9677 }
9678 return 1;
9679}
9680
6f078746
PN
9681/* Preload keys for any command with first, last and step values for
9682 * the command keys prototype, as defined in the command table. */
9683static void waitForMultipleSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
9684 int j, last;
9685 if (cmd->vm_firstkey == 0) return;
9686 last = cmd->vm_lastkey;
9687 if (last < 0) last = argc+last;
9688 for (j = cmd->vm_firstkey; j <= last; j += cmd->vm_keystep) {
9689 redisAssert(j < argc);
9690 waitForSwappedKey(c,argv[j]);
9691 }
9692}
9693
5d373da9 9694/* Preload keys needed for the ZUNIONSTORE and ZINTERSTORE commands.
739ba0d2
PN
9695 * Note that the number of keys to preload is user-defined, so we need to
9696 * apply a sanity check against argc. */
ca1788b5 9697static void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
76583ea4 9698 int i, num;
ca1788b5 9699 REDIS_NOTUSED(cmd);
ca1788b5
PN
9700
9701 num = atoi(argv[2]->ptr);
739ba0d2 9702 if (num > (argc-3)) return;
76583ea4 9703 for (i = 0; i < num; i++) {
ca1788b5 9704 waitForSwappedKey(c,argv[3+i]);
76583ea4
PN
9705 }
9706}
9707
3805e04f
PN
9708/* Preload keys needed to execute the entire MULTI/EXEC block.
9709 *
9710 * This function is called by blockClientOnSwappedKeys when EXEC is issued,
9711 * and will block the client when any command requires a swapped out value. */
9712static void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
9713 int i, margc;
9714 struct redisCommand *mcmd;
9715 robj **margv;
9716 REDIS_NOTUSED(cmd);
9717 REDIS_NOTUSED(argc);
9718 REDIS_NOTUSED(argv);
9719
9720 if (!(c->flags & REDIS_MULTI)) return;
9721 for (i = 0; i < c->mstate.count; i++) {
9722 mcmd = c->mstate.commands[i].cmd;
9723 margc = c->mstate.commands[i].argc;
9724 margv = c->mstate.commands[i].argv;
9725
9726 if (mcmd->vm_preload_proc != NULL) {
9727 mcmd->vm_preload_proc(c,mcmd,margc,margv);
9728 } else {
9729 waitForMultipleSwappedKeys(c,mcmd,margc,margv);
9730 }
76583ea4
PN
9731 }
9732}
9733
b0d8747d 9734/* Is this client attempting to run a command against swapped keys?
d5d55fc3 9735 * If so, block it ASAP, load the keys in background, then resume it.
b0d8747d 9736 *
d5d55fc3 9737 * The important idea about this function is that it can fail! If keys will
9738 * still be swapped when the client is resumed, this key lookups will
9739 * just block loading keys from disk. In practical terms this should only
9740 * happen with SORT BY command or if there is a bug in this function.
9741 *
9742 * Return 1 if the client is marked as blocked, 0 if the client can
9743 * continue as the keys it is going to access appear to be in memory. */
0a6f3f0f 9744static int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) {
76583ea4 9745 if (cmd->vm_preload_proc != NULL) {
ca1788b5 9746 cmd->vm_preload_proc(c,cmd,c->argc,c->argv);
76583ea4 9747 } else {
6f078746 9748 waitForMultipleSwappedKeys(c,cmd,c->argc,c->argv);
76583ea4
PN
9749 }
9750
d5d55fc3 9751 /* If the client was blocked for at least one key, mark it as blocked. */
9752 if (listLength(c->io_keys)) {
9753 c->flags |= REDIS_IO_WAIT;
9754 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
9755 server.vm_blocked_clients++;
9756 return 1;
9757 } else {
9758 return 0;
9759 }
9760}
9761
9762/* Remove the 'key' from the list of blocked keys for a given client.
9763 *
9764 * The function returns 1 when there are no longer blocking keys after
9765 * the current one was removed (and the client can be unblocked). */
9766static int dontWaitForSwappedKey(redisClient *c, robj *key) {
9767 list *l;
9768 listNode *ln;
9769 listIter li;
9770 struct dictEntry *de;
9771
9772 /* Remove the key from the list of keys this client is waiting for. */
9773 listRewind(c->io_keys,&li);
9774 while ((ln = listNext(&li)) != NULL) {
bf028098 9775 if (equalStringObjects(ln->value,key)) {
d5d55fc3 9776 listDelNode(c->io_keys,ln);
9777 break;
9778 }
9779 }
9780 assert(ln != NULL);
9781
9782 /* Remove the client form the key => waiting clients map. */
9783 de = dictFind(c->db->io_keys,key);
9784 assert(de != NULL);
9785 l = dictGetEntryVal(de);
9786 ln = listSearchKey(l,c);
9787 assert(ln != NULL);
9788 listDelNode(l,ln);
9789 if (listLength(l) == 0)
9790 dictDelete(c->db->io_keys,key);
9791
9792 return listLength(c->io_keys) == 0;
9793}
9794
9795static void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key) {
9796 struct dictEntry *de;
9797 list *l;
9798 listNode *ln;
9799 int len;
9800
9801 de = dictFind(db->io_keys,key);
9802 if (!de) return;
9803
9804 l = dictGetEntryVal(de);
9805 len = listLength(l);
9806 /* Note: we can't use something like while(listLength(l)) as the list
9807 * can be freed by the calling function when we remove the last element. */
9808 while (len--) {
9809 ln = listFirst(l);
9810 redisClient *c = ln->value;
9811
9812 if (dontWaitForSwappedKey(c,key)) {
9813 /* Put the client in the list of clients ready to go as we
9814 * loaded all the keys about it. */
9815 listAddNodeTail(server.io_ready_clients,c);
9816 }
9817 }
b0d8747d 9818}
b0d8747d 9819
500ece7c 9820/* =========================== Remote Configuration ========================= */
9821
9822static void configSetCommand(redisClient *c) {
9823 robj *o = getDecodedObject(c->argv[3]);
9824 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
9825 zfree(server.dbfilename);
9826 server.dbfilename = zstrdup(o->ptr);
9827 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
9828 zfree(server.requirepass);
9829 server.requirepass = zstrdup(o->ptr);
9830 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
9831 zfree(server.masterauth);
9832 server.masterauth = zstrdup(o->ptr);
9833 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
9834 server.maxmemory = strtoll(o->ptr, NULL, 10);
1b677732 9835 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
9836 if (!strcasecmp(o->ptr,"no")) {
9837 server.appendfsync = APPENDFSYNC_NO;
9838 } else if (!strcasecmp(o->ptr,"everysec")) {
9839 server.appendfsync = APPENDFSYNC_EVERYSEC;
9840 } else if (!strcasecmp(o->ptr,"always")) {
9841 server.appendfsync = APPENDFSYNC_ALWAYS;
9842 } else {
9843 goto badfmt;
9844 }
a34e0a25 9845 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
9846 int vlen, j;
9847 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
9848
9849 /* Perform sanity check before setting the new config:
9850 * - Even number of args
9851 * - Seconds >= 1, changes >= 0 */
9852 if (vlen & 1) {
9853 sdsfreesplitres(v,vlen);
9854 goto badfmt;
9855 }
9856 for (j = 0; j < vlen; j++) {
9857 char *eptr;
9858 long val;
9859
9860 val = strtoll(v[j], &eptr, 10);
9861 if (eptr[0] != '\0' ||
9862 ((j & 1) == 0 && val < 1) ||
9863 ((j & 1) == 1 && val < 0)) {
9864 sdsfreesplitres(v,vlen);
9865 goto badfmt;
9866 }
9867 }
9868 /* Finally set the new config */
9869 resetServerSaveParams();
9870 for (j = 0; j < vlen; j += 2) {
9871 time_t seconds;
9872 int changes;
9873
9874 seconds = strtoll(v[j],NULL,10);
9875 changes = strtoll(v[j+1],NULL,10);
9876 appendServerSaveParams(seconds, changes);
9877 }
9878 sdsfreesplitres(v,vlen);
500ece7c 9879 } else {
9880 addReplySds(c,sdscatprintf(sdsempty(),
9881 "-ERR not supported CONFIG parameter %s\r\n",
9882 (char*)c->argv[2]->ptr));
9883 decrRefCount(o);
9884 return;
9885 }
9886 decrRefCount(o);
9887 addReply(c,shared.ok);
a34e0a25 9888 return;
9889
9890badfmt: /* Bad format errors */
9891 addReplySds(c,sdscatprintf(sdsempty(),
9892 "-ERR invalid argument '%s' for CONFIG SET '%s'\r\n",
9893 (char*)o->ptr,
9894 (char*)c->argv[2]->ptr));
9895 decrRefCount(o);
500ece7c 9896}
9897
9898static void configGetCommand(redisClient *c) {
9899 robj *o = getDecodedObject(c->argv[2]);
9900 robj *lenobj = createObject(REDIS_STRING,NULL);
9901 char *pattern = o->ptr;
9902 int matches = 0;
9903
9904 addReply(c,lenobj);
9905 decrRefCount(lenobj);
9906
9907 if (stringmatch(pattern,"dbfilename",0)) {
9908 addReplyBulkCString(c,"dbfilename");
9909 addReplyBulkCString(c,server.dbfilename);
9910 matches++;
9911 }
9912 if (stringmatch(pattern,"requirepass",0)) {
9913 addReplyBulkCString(c,"requirepass");
9914 addReplyBulkCString(c,server.requirepass);
9915 matches++;
9916 }
9917 if (stringmatch(pattern,"masterauth",0)) {
9918 addReplyBulkCString(c,"masterauth");
9919 addReplyBulkCString(c,server.masterauth);
9920 matches++;
9921 }
9922 if (stringmatch(pattern,"maxmemory",0)) {
9923 char buf[128];
9924
9925 snprintf(buf,128,"%llu\n",server.maxmemory);
9926 addReplyBulkCString(c,"maxmemory");
9927 addReplyBulkCString(c,buf);
9928 matches++;
9929 }
1b677732 9930 if (stringmatch(pattern,"appendfsync",0)) {
9931 char *policy;
9932
9933 switch(server.appendfsync) {
9934 case APPENDFSYNC_NO: policy = "no"; break;
9935 case APPENDFSYNC_EVERYSEC: policy = "everysec"; break;
9936 case APPENDFSYNC_ALWAYS: policy = "always"; break;
9937 default: policy = "unknown"; break; /* too harmless to panic */
9938 }
9939 addReplyBulkCString(c,"appendfsync");
9940 addReplyBulkCString(c,policy);
9941 matches++;
9942 }
a34e0a25 9943 if (stringmatch(pattern,"save",0)) {
9944 sds buf = sdsempty();
9945 int j;
9946
9947 for (j = 0; j < server.saveparamslen; j++) {
9948 buf = sdscatprintf(buf,"%ld %d",
9949 server.saveparams[j].seconds,
9950 server.saveparams[j].changes);
9951 if (j != server.saveparamslen-1)
9952 buf = sdscatlen(buf," ",1);
9953 }
9954 addReplyBulkCString(c,"save");
9955 addReplyBulkCString(c,buf);
9956 sdsfree(buf);
9957 matches++;
9958 }
500ece7c 9959 decrRefCount(o);
9960 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",matches*2);
9961}
9962
9963static void configCommand(redisClient *c) {
9964 if (!strcasecmp(c->argv[1]->ptr,"set")) {
9965 if (c->argc != 4) goto badarity;
9966 configSetCommand(c);
9967 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
9968 if (c->argc != 3) goto badarity;
9969 configGetCommand(c);
9970 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
9971 if (c->argc != 2) goto badarity;
9972 server.stat_numcommands = 0;
9973 server.stat_numconnections = 0;
9974 server.stat_expiredkeys = 0;
9975 server.stat_starttime = time(NULL);
9976 addReply(c,shared.ok);
9977 } else {
9978 addReplySds(c,sdscatprintf(sdsempty(),
9979 "-ERR CONFIG subcommand must be one of GET, SET, RESETSTAT\r\n"));
9980 }
9981 return;
9982
9983badarity:
9984 addReplySds(c,sdscatprintf(sdsempty(),
9985 "-ERR Wrong number of arguments for CONFIG %s\r\n",
9986 (char*) c->argv[1]->ptr));
9987}
9988
befec3cd 9989/* =========================== Pubsub implementation ======================== */
9990
ffc6b7f8 9991static void freePubsubPattern(void *p) {
9992 pubsubPattern *pat = p;
9993
9994 decrRefCount(pat->pattern);
9995 zfree(pat);
9996}
9997
9998static int listMatchPubsubPattern(void *a, void *b) {
9999 pubsubPattern *pa = a, *pb = b;
10000
10001 return (pa->client == pb->client) &&
bf028098 10002 (equalStringObjects(pa->pattern,pb->pattern));
ffc6b7f8 10003}
10004
10005/* Subscribe a client to a channel. Returns 1 if the operation succeeded, or
10006 * 0 if the client was already subscribed to that channel. */
10007static int pubsubSubscribeChannel(redisClient *c, robj *channel) {
befec3cd 10008 struct dictEntry *de;
10009 list *clients = NULL;
10010 int retval = 0;
10011
ffc6b7f8 10012 /* Add the channel to the client -> channels hash table */
10013 if (dictAdd(c->pubsub_channels,channel,NULL) == DICT_OK) {
befec3cd 10014 retval = 1;
ffc6b7f8 10015 incrRefCount(channel);
10016 /* Add the client to the channel -> list of clients hash table */
10017 de = dictFind(server.pubsub_channels,channel);
befec3cd 10018 if (de == NULL) {
10019 clients = listCreate();
ffc6b7f8 10020 dictAdd(server.pubsub_channels,channel,clients);
10021 incrRefCount(channel);
befec3cd 10022 } else {
10023 clients = dictGetEntryVal(de);
10024 }
10025 listAddNodeTail(clients,c);
10026 }
10027 /* Notify the client */
10028 addReply(c,shared.mbulk3);
10029 addReply(c,shared.subscribebulk);
ffc6b7f8 10030 addReplyBulk(c,channel);
482b672d 10031 addReplyLongLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
befec3cd 10032 return retval;
10033}
10034
ffc6b7f8 10035/* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10036 * 0 if the client was not subscribed to the specified channel. */
10037static int pubsubUnsubscribeChannel(redisClient *c, robj *channel, int notify) {
befec3cd 10038 struct dictEntry *de;
10039 list *clients;
10040 listNode *ln;
10041 int retval = 0;
10042
ffc6b7f8 10043 /* Remove the channel from the client -> channels hash table */
10044 incrRefCount(channel); /* channel may be just a pointer to the same object
201037f5 10045 we have in the hash tables. Protect it... */
ffc6b7f8 10046 if (dictDelete(c->pubsub_channels,channel) == DICT_OK) {
befec3cd 10047 retval = 1;
ffc6b7f8 10048 /* Remove the client from the channel -> clients list hash table */
10049 de = dictFind(server.pubsub_channels,channel);
befec3cd 10050 assert(de != NULL);
10051 clients = dictGetEntryVal(de);
10052 ln = listSearchKey(clients,c);
10053 assert(ln != NULL);
10054 listDelNode(clients,ln);
ff767a75 10055 if (listLength(clients) == 0) {
10056 /* Free the list and associated hash entry at all if this was
10057 * the latest client, so that it will be possible to abuse
ffc6b7f8 10058 * Redis PUBSUB creating millions of channels. */
10059 dictDelete(server.pubsub_channels,channel);
ff767a75 10060 }
befec3cd 10061 }
10062 /* Notify the client */
10063 if (notify) {
10064 addReply(c,shared.mbulk3);
10065 addReply(c,shared.unsubscribebulk);
ffc6b7f8 10066 addReplyBulk(c,channel);
482b672d 10067 addReplyLongLong(c,dictSize(c->pubsub_channels)+
ffc6b7f8 10068 listLength(c->pubsub_patterns));
10069
10070 }
10071 decrRefCount(channel); /* it is finally safe to release it */
10072 return retval;
10073}
10074
10075/* Subscribe a client to a pattern. Returns 1 if the operation succeeded, or 0 if the clinet was already subscribed to that pattern. */
10076static int pubsubSubscribePattern(redisClient *c, robj *pattern) {
10077 int retval = 0;
10078
10079 if (listSearchKey(c->pubsub_patterns,pattern) == NULL) {
10080 retval = 1;
10081 pubsubPattern *pat;
10082 listAddNodeTail(c->pubsub_patterns,pattern);
10083 incrRefCount(pattern);
10084 pat = zmalloc(sizeof(*pat));
10085 pat->pattern = getDecodedObject(pattern);
10086 pat->client = c;
10087 listAddNodeTail(server.pubsub_patterns,pat);
10088 }
10089 /* Notify the client */
10090 addReply(c,shared.mbulk3);
10091 addReply(c,shared.psubscribebulk);
10092 addReplyBulk(c,pattern);
482b672d 10093 addReplyLongLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
ffc6b7f8 10094 return retval;
10095}
10096
10097/* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10098 * 0 if the client was not subscribed to the specified channel. */
10099static int pubsubUnsubscribePattern(redisClient *c, robj *pattern, int notify) {
10100 listNode *ln;
10101 pubsubPattern pat;
10102 int retval = 0;
10103
10104 incrRefCount(pattern); /* Protect the object. May be the same we remove */
10105 if ((ln = listSearchKey(c->pubsub_patterns,pattern)) != NULL) {
10106 retval = 1;
10107 listDelNode(c->pubsub_patterns,ln);
10108 pat.client = c;
10109 pat.pattern = pattern;
10110 ln = listSearchKey(server.pubsub_patterns,&pat);
10111 listDelNode(server.pubsub_patterns,ln);
10112 }
10113 /* Notify the client */
10114 if (notify) {
10115 addReply(c,shared.mbulk3);
10116 addReply(c,shared.punsubscribebulk);
10117 addReplyBulk(c,pattern);
482b672d 10118 addReplyLongLong(c,dictSize(c->pubsub_channels)+
ffc6b7f8 10119 listLength(c->pubsub_patterns));
befec3cd 10120 }
ffc6b7f8 10121 decrRefCount(pattern);
befec3cd 10122 return retval;
10123}
10124
ffc6b7f8 10125/* Unsubscribe from all the channels. Return the number of channels the
10126 * client was subscribed from. */
10127static int pubsubUnsubscribeAllChannels(redisClient *c, int notify) {
10128 dictIterator *di = dictGetIterator(c->pubsub_channels);
befec3cd 10129 dictEntry *de;
10130 int count = 0;
10131
10132 while((de = dictNext(di)) != NULL) {
ffc6b7f8 10133 robj *channel = dictGetEntryKey(de);
befec3cd 10134
ffc6b7f8 10135 count += pubsubUnsubscribeChannel(c,channel,notify);
befec3cd 10136 }
10137 dictReleaseIterator(di);
10138 return count;
10139}
10140
ffc6b7f8 10141/* Unsubscribe from all the patterns. Return the number of patterns the
10142 * client was subscribed from. */
10143static int pubsubUnsubscribeAllPatterns(redisClient *c, int notify) {
10144 listNode *ln;
10145 listIter li;
10146 int count = 0;
10147
10148 listRewind(c->pubsub_patterns,&li);
10149 while ((ln = listNext(&li)) != NULL) {
10150 robj *pattern = ln->value;
10151
10152 count += pubsubUnsubscribePattern(c,pattern,notify);
10153 }
10154 return count;
10155}
10156
befec3cd 10157/* Publish a message */
ffc6b7f8 10158static int pubsubPublishMessage(robj *channel, robj *message) {
befec3cd 10159 int receivers = 0;
10160 struct dictEntry *de;
ffc6b7f8 10161 listNode *ln;
10162 listIter li;
befec3cd 10163
ffc6b7f8 10164 /* Send to clients listening for that channel */
10165 de = dictFind(server.pubsub_channels,channel);
befec3cd 10166 if (de) {
10167 list *list = dictGetEntryVal(de);
10168 listNode *ln;
10169 listIter li;
10170
10171 listRewind(list,&li);
10172 while ((ln = listNext(&li)) != NULL) {
10173 redisClient *c = ln->value;
10174
10175 addReply(c,shared.mbulk3);
10176 addReply(c,shared.messagebulk);
ffc6b7f8 10177 addReplyBulk(c,channel);
befec3cd 10178 addReplyBulk(c,message);
10179 receivers++;
10180 }
10181 }
ffc6b7f8 10182 /* Send to clients listening to matching channels */
10183 if (listLength(server.pubsub_patterns)) {
10184 listRewind(server.pubsub_patterns,&li);
10185 channel = getDecodedObject(channel);
10186 while ((ln = listNext(&li)) != NULL) {
10187 pubsubPattern *pat = ln->value;
10188
10189 if (stringmatchlen((char*)pat->pattern->ptr,
10190 sdslen(pat->pattern->ptr),
10191 (char*)channel->ptr,
10192 sdslen(channel->ptr),0)) {
c8d0ea0e 10193 addReply(pat->client,shared.mbulk4);
10194 addReply(pat->client,shared.pmessagebulk);
10195 addReplyBulk(pat->client,pat->pattern);
ffc6b7f8 10196 addReplyBulk(pat->client,channel);
10197 addReplyBulk(pat->client,message);
10198 receivers++;
10199 }
10200 }
10201 decrRefCount(channel);
10202 }
befec3cd 10203 return receivers;
10204}
10205
10206static void subscribeCommand(redisClient *c) {
10207 int j;
10208
10209 for (j = 1; j < c->argc; j++)
ffc6b7f8 10210 pubsubSubscribeChannel(c,c->argv[j]);
befec3cd 10211}
10212
10213static void unsubscribeCommand(redisClient *c) {
10214 if (c->argc == 1) {
ffc6b7f8 10215 pubsubUnsubscribeAllChannels(c,1);
10216 return;
10217 } else {
10218 int j;
10219
10220 for (j = 1; j < c->argc; j++)
10221 pubsubUnsubscribeChannel(c,c->argv[j],1);
10222 }
10223}
10224
10225static void psubscribeCommand(redisClient *c) {
10226 int j;
10227
10228 for (j = 1; j < c->argc; j++)
10229 pubsubSubscribePattern(c,c->argv[j]);
10230}
10231
10232static void punsubscribeCommand(redisClient *c) {
10233 if (c->argc == 1) {
10234 pubsubUnsubscribeAllPatterns(c,1);
befec3cd 10235 return;
10236 } else {
10237 int j;
10238
10239 for (j = 1; j < c->argc; j++)
ffc6b7f8 10240 pubsubUnsubscribePattern(c,c->argv[j],1);
befec3cd 10241 }
10242}
10243
10244static void publishCommand(redisClient *c) {
10245 int receivers = pubsubPublishMessage(c->argv[1],c->argv[2]);
482b672d 10246 addReplyLongLong(c,receivers);
befec3cd 10247}
10248
7f957c92 10249/* ================================= Debugging ============================== */
10250
ba798261 10251/* Compute the sha1 of string at 's' with 'len' bytes long.
10252 * The SHA1 is then xored againt the string pointed by digest.
10253 * Since xor is commutative, this operation is used in order to
10254 * "add" digests relative to unordered elements.
10255 *
10256 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
10257static void xorDigest(unsigned char *digest, void *ptr, size_t len) {
10258 SHA1_CTX ctx;
10259 unsigned char hash[20], *s = ptr;
10260 int j;
10261
10262 SHA1Init(&ctx);
10263 SHA1Update(&ctx,s,len);
10264 SHA1Final(hash,&ctx);
10265
10266 for (j = 0; j < 20; j++)
10267 digest[j] ^= hash[j];
10268}
10269
10270static void xorObjectDigest(unsigned char *digest, robj *o) {
10271 o = getDecodedObject(o);
10272 xorDigest(digest,o->ptr,sdslen(o->ptr));
10273 decrRefCount(o);
10274}
10275
10276/* This function instead of just computing the SHA1 and xoring it
10277 * against diget, also perform the digest of "digest" itself and
10278 * replace the old value with the new one.
10279 *
10280 * So the final digest will be:
10281 *
10282 * digest = SHA1(digest xor SHA1(data))
10283 *
10284 * This function is used every time we want to preserve the order so
10285 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
10286 *
10287 * Also note that mixdigest("foo") followed by mixdigest("bar")
10288 * will lead to a different digest compared to "fo", "obar".
10289 */
10290static void mixDigest(unsigned char *digest, void *ptr, size_t len) {
10291 SHA1_CTX ctx;
10292 char *s = ptr;
10293
10294 xorDigest(digest,s,len);
10295 SHA1Init(&ctx);
10296 SHA1Update(&ctx,digest,20);
10297 SHA1Final(digest,&ctx);
10298}
10299
10300static void mixObjectDigest(unsigned char *digest, robj *o) {
10301 o = getDecodedObject(o);
10302 mixDigest(digest,o->ptr,sdslen(o->ptr));
10303 decrRefCount(o);
10304}
10305
10306/* Compute the dataset digest. Since keys, sets elements, hashes elements
10307 * are not ordered, we use a trick: every aggregate digest is the xor
10308 * of the digests of their elements. This way the order will not change
10309 * the result. For list instead we use a feedback entering the output digest
10310 * as input in order to ensure that a different ordered list will result in
10311 * a different digest. */
10312static void computeDatasetDigest(unsigned char *final) {
10313 unsigned char digest[20];
10314 char buf[128];
10315 dictIterator *di = NULL;
10316 dictEntry *de;
10317 int j;
10318 uint32_t aux;
10319
10320 memset(final,0,20); /* Start with a clean result */
10321
10322 for (j = 0; j < server.dbnum; j++) {
10323 redisDb *db = server.db+j;
10324
10325 if (dictSize(db->dict) == 0) continue;
10326 di = dictGetIterator(db->dict);
10327
10328 /* hash the DB id, so the same dataset moved in a different
10329 * DB will lead to a different digest */
10330 aux = htonl(j);
10331 mixDigest(final,&aux,sizeof(aux));
10332
10333 /* Iterate this DB writing every entry */
10334 while((de = dictNext(di)) != NULL) {
10335 robj *key, *o;
10336 time_t expiretime;
10337
10338 memset(digest,0,20); /* This key-val digest */
10339 key = dictGetEntryKey(de);
10340 mixObjectDigest(digest,key);
10341 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
10342 key->storage == REDIS_VM_SWAPPING) {
10343 o = dictGetEntryVal(de);
10344 incrRefCount(o);
10345 } else {
10346 o = vmPreviewObject(key);
10347 }
10348 aux = htonl(o->type);
10349 mixDigest(digest,&aux,sizeof(aux));
10350 expiretime = getExpire(db,key);
10351
10352 /* Save the key and associated value */
10353 if (o->type == REDIS_STRING) {
10354 mixObjectDigest(digest,o);
10355 } else if (o->type == REDIS_LIST) {
10356 list *list = o->ptr;
10357 listNode *ln;
10358 listIter li;
10359
10360 listRewind(list,&li);
10361 while((ln = listNext(&li))) {
10362 robj *eleobj = listNodeValue(ln);
10363
10364 mixObjectDigest(digest,eleobj);
10365 }
10366 } else if (o->type == REDIS_SET) {
10367 dict *set = o->ptr;
10368 dictIterator *di = dictGetIterator(set);
10369 dictEntry *de;
10370
10371 while((de = dictNext(di)) != NULL) {
10372 robj *eleobj = dictGetEntryKey(de);
10373
10374 xorObjectDigest(digest,eleobj);
10375 }
10376 dictReleaseIterator(di);
10377 } else if (o->type == REDIS_ZSET) {
10378 zset *zs = o->ptr;
10379 dictIterator *di = dictGetIterator(zs->dict);
10380 dictEntry *de;
10381
10382 while((de = dictNext(di)) != NULL) {
10383 robj *eleobj = dictGetEntryKey(de);
10384 double *score = dictGetEntryVal(de);
10385 unsigned char eledigest[20];
10386
10387 snprintf(buf,sizeof(buf),"%.17g",*score);
10388 memset(eledigest,0,20);
10389 mixObjectDigest(eledigest,eleobj);
10390 mixDigest(eledigest,buf,strlen(buf));
10391 xorDigest(digest,eledigest,20);
10392 }
10393 dictReleaseIterator(di);
10394 } else if (o->type == REDIS_HASH) {
10395 hashIterator *hi;
10396 robj *obj;
10397
10398 hi = hashInitIterator(o);
10399 while (hashNext(hi) != REDIS_ERR) {
10400 unsigned char eledigest[20];
10401
10402 memset(eledigest,0,20);
10403 obj = hashCurrent(hi,REDIS_HASH_KEY);
10404 mixObjectDigest(eledigest,obj);
10405 decrRefCount(obj);
10406 obj = hashCurrent(hi,REDIS_HASH_VALUE);
10407 mixObjectDigest(eledigest,obj);
10408 decrRefCount(obj);
10409 xorDigest(digest,eledigest,20);
10410 }
10411 hashReleaseIterator(hi);
10412 } else {
10413 redisPanic("Unknown object type");
10414 }
10415 decrRefCount(o);
10416 /* If the key has an expire, add it to the mix */
10417 if (expiretime != -1) xorDigest(digest,"!!expire!!",10);
10418 /* We can finally xor the key-val digest to the final digest */
10419 xorDigest(final,digest,20);
10420 }
10421 dictReleaseIterator(di);
10422 }
10423}
10424
7f957c92 10425static void debugCommand(redisClient *c) {
10426 if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
10427 *((char*)-1) = 'x';
210e29f7 10428 } else if (!strcasecmp(c->argv[1]->ptr,"reload")) {
10429 if (rdbSave(server.dbfilename) != REDIS_OK) {
10430 addReply(c,shared.err);
10431 return;
10432 }
10433 emptyDb();
10434 if (rdbLoad(server.dbfilename) != REDIS_OK) {
10435 addReply(c,shared.err);
10436 return;
10437 }
10438 redisLog(REDIS_WARNING,"DB reloaded by DEBUG RELOAD");
10439 addReply(c,shared.ok);
71c2b467 10440 } else if (!strcasecmp(c->argv[1]->ptr,"loadaof")) {
10441 emptyDb();
10442 if (loadAppendOnlyFile(server.appendfilename) != REDIS_OK) {
10443 addReply(c,shared.err);
10444 return;
10445 }
10446 redisLog(REDIS_WARNING,"Append Only File loaded by DEBUG LOADAOF");
10447 addReply(c,shared.ok);
333298da 10448 } else if (!strcasecmp(c->argv[1]->ptr,"object") && c->argc == 3) {
10449 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
10450 robj *key, *val;
10451
10452 if (!de) {
10453 addReply(c,shared.nokeyerr);
10454 return;
10455 }
10456 key = dictGetEntryKey(de);
10457 val = dictGetEntryVal(de);
59146ef3 10458 if (!server.vm_enabled || (key->storage == REDIS_VM_MEMORY ||
10459 key->storage == REDIS_VM_SWAPPING)) {
07efaf74 10460 char *strenc;
10461 char buf[128];
10462
10463 if (val->encoding < (sizeof(strencoding)/sizeof(char*))) {
10464 strenc = strencoding[val->encoding];
10465 } else {
10466 snprintf(buf,64,"unknown encoding %d\n", val->encoding);
10467 strenc = buf;
10468 }
ace06542 10469 addReplySds(c,sdscatprintf(sdsempty(),
10470 "+Key at:%p refcount:%d, value at:%p refcount:%d "
07efaf74 10471 "encoding:%s serializedlength:%lld\r\n",
682ac724 10472 (void*)key, key->refcount, (void*)val, val->refcount,
07efaf74 10473 strenc, (long long) rdbSavedObjectLen(val,NULL)));
ace06542 10474 } else {
10475 addReplySds(c,sdscatprintf(sdsempty(),
10476 "+Key at:%p refcount:%d, value swapped at: page %llu "
10477 "using %llu pages\r\n",
10478 (void*)key, key->refcount, (unsigned long long) key->vm.page,
10479 (unsigned long long) key->vm.usedpages));
10480 }
78ebe4c8 10481 } else if (!strcasecmp(c->argv[1]->ptr,"swapin") && c->argc == 3) {
10482 lookupKeyRead(c->db,c->argv[2]);
10483 addReply(c,shared.ok);
7d30035d 10484 } else if (!strcasecmp(c->argv[1]->ptr,"swapout") && c->argc == 3) {
10485 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
10486 robj *key, *val;
10487
10488 if (!server.vm_enabled) {
10489 addReplySds(c,sdsnew("-ERR Virtual Memory is disabled\r\n"));
10490 return;
10491 }
10492 if (!de) {
10493 addReply(c,shared.nokeyerr);
10494 return;
10495 }
10496 key = dictGetEntryKey(de);
10497 val = dictGetEntryVal(de);
4ef8de8a 10498 /* If the key is shared we want to create a copy */
10499 if (key->refcount > 1) {
10500 robj *newkey = dupStringObject(key);
10501 decrRefCount(key);
10502 key = dictGetEntryKey(de) = newkey;
10503 }
10504 /* Swap it */
7d30035d 10505 if (key->storage != REDIS_VM_MEMORY) {
10506 addReplySds(c,sdsnew("-ERR This key is not in memory\r\n"));
a69a0c9c 10507 } else if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
7d30035d 10508 dictGetEntryVal(de) = NULL;
10509 addReply(c,shared.ok);
10510 } else {
10511 addReply(c,shared.err);
10512 }
59305dc7 10513 } else if (!strcasecmp(c->argv[1]->ptr,"populate") && c->argc == 3) {
10514 long keys, j;
10515 robj *key, *val;
10516 char buf[128];
10517
10518 if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != REDIS_OK)
10519 return;
10520 for (j = 0; j < keys; j++) {
10521 snprintf(buf,sizeof(buf),"key:%lu",j);
10522 key = createStringObject(buf,strlen(buf));
10523 if (lookupKeyRead(c->db,key) != NULL) {
10524 decrRefCount(key);
10525 continue;
10526 }
10527 snprintf(buf,sizeof(buf),"value:%lu",j);
10528 val = createStringObject(buf,strlen(buf));
10529 dictAdd(c->db->dict,key,val);
10530 }
10531 addReply(c,shared.ok);
ba798261 10532 } else if (!strcasecmp(c->argv[1]->ptr,"digest") && c->argc == 2) {
10533 unsigned char digest[20];
10534 sds d = sdsnew("+");
10535 int j;
10536
10537 computeDatasetDigest(digest);
10538 for (j = 0; j < 20; j++)
10539 d = sdscatprintf(d, "%02x",digest[j]);
10540
10541 d = sdscatlen(d,"\r\n",2);
10542 addReplySds(c,d);
7f957c92 10543 } else {
333298da 10544 addReplySds(c,sdsnew(
bdcb92f2 10545 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]\r\n"));
7f957c92 10546 }
10547}
56906eef 10548
6c96ba7d 10549static void _redisAssert(char *estr, char *file, int line) {
dfc5e96c 10550 redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
6c96ba7d 10551 redisLog(REDIS_WARNING,"==> %s:%d '%s' is not true\n",file,line,estr);
dfc5e96c 10552#ifdef HAVE_BACKTRACE
10553 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
10554 *((char*)-1) = 'x';
10555#endif
10556}
10557
c651fd9e 10558static void _redisPanic(char *msg, char *file, int line) {
10559 redisLog(REDIS_WARNING,"!!! Software Failure. Press left mouse button to continue");
17772754 10560 redisLog(REDIS_WARNING,"Guru Meditation: %s #%s:%d",msg,file,line);
c651fd9e 10561#ifdef HAVE_BACKTRACE
10562 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
10563 *((char*)-1) = 'x';
10564#endif
10565}
10566
bcfc686d 10567/* =================================== Main! ================================ */
56906eef 10568
bcfc686d 10569#ifdef __linux__
10570int linuxOvercommitMemoryValue(void) {
10571 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
10572 char buf[64];
56906eef 10573
bcfc686d 10574 if (!fp) return -1;
10575 if (fgets(buf,64,fp) == NULL) {
10576 fclose(fp);
10577 return -1;
10578 }
10579 fclose(fp);
56906eef 10580
bcfc686d 10581 return atoi(buf);
10582}
10583
10584void linuxOvercommitMemoryWarning(void) {
10585 if (linuxOvercommitMemoryValue() == 0) {
7ccd2d0a 10586 redisLog(REDIS_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.");
bcfc686d 10587 }
10588}
10589#endif /* __linux__ */
10590
10591static void daemonize(void) {
10592 int fd;
10593 FILE *fp;
10594
10595 if (fork() != 0) exit(0); /* parent exits */
10596 setsid(); /* create a new session */
10597
10598 /* Every output goes to /dev/null. If Redis is daemonized but
10599 * the 'logfile' is set to 'stdout' in the configuration file
10600 * it will not log at all. */
10601 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
10602 dup2(fd, STDIN_FILENO);
10603 dup2(fd, STDOUT_FILENO);
10604 dup2(fd, STDERR_FILENO);
10605 if (fd > STDERR_FILENO) close(fd);
10606 }
10607 /* Try to write the pid file */
10608 fp = fopen(server.pidfile,"w");
10609 if (fp) {
10610 fprintf(fp,"%d\n",getpid());
10611 fclose(fp);
56906eef 10612 }
56906eef 10613}
10614
42ab0172
AO
10615static void version() {
10616 printf("Redis server version %s\n", REDIS_VERSION);
10617 exit(0);
10618}
10619
723fb69b
AO
10620static void usage() {
10621 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf]\n");
e9409273 10622 fprintf(stderr," ./redis-server - (read config from stdin)\n");
723fb69b
AO
10623 exit(1);
10624}
10625
bcfc686d 10626int main(int argc, char **argv) {
9651a787 10627 time_t start;
10628
bcfc686d 10629 initServerConfig();
10630 if (argc == 2) {
44efe66e 10631 if (strcmp(argv[1], "-v") == 0 ||
10632 strcmp(argv[1], "--version") == 0) version();
10633 if (strcmp(argv[1], "--help") == 0) usage();
bcfc686d 10634 resetServerSaveParams();
10635 loadServerConfig(argv[1]);
723fb69b
AO
10636 } else if ((argc > 2)) {
10637 usage();
bcfc686d 10638 } else {
10639 redisLog(REDIS_WARNING,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
10640 }
bcfc686d 10641 if (server.daemonize) daemonize();
71c54b21 10642 initServer();
bcfc686d 10643 redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
10644#ifdef __linux__
10645 linuxOvercommitMemoryWarning();
10646#endif
9651a787 10647 start = time(NULL);
bcfc686d 10648 if (server.appendonly) {
10649 if (loadAppendOnlyFile(server.appendfilename) == REDIS_OK)
9651a787 10650 redisLog(REDIS_NOTICE,"DB loaded from append only file: %ld seconds",time(NULL)-start);
bcfc686d 10651 } else {
10652 if (rdbLoad(server.dbfilename) == REDIS_OK)
9651a787 10653 redisLog(REDIS_NOTICE,"DB loaded from disk: %ld seconds",time(NULL)-start);
bcfc686d 10654 }
bcfc686d 10655 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
d5d55fc3 10656 aeSetBeforeSleepProc(server.el,beforeSleep);
bcfc686d 10657 aeMain(server.el);
10658 aeDeleteEventLoop(server.el);
10659 return 0;
10660}
10661
10662/* ============================= Backtrace support ========================= */
10663
10664#ifdef HAVE_BACKTRACE
10665static char *findFuncName(void *pointer, unsigned long *offset);
10666
56906eef 10667static void *getMcontextEip(ucontext_t *uc) {
10668#if defined(__FreeBSD__)
10669 return (void*) uc->uc_mcontext.mc_eip;
10670#elif defined(__dietlibc__)
10671 return (void*) uc->uc_mcontext.eip;
06db1f50 10672#elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
da0a1620 10673 #if __x86_64__
10674 return (void*) uc->uc_mcontext->__ss.__rip;
10675 #else
56906eef 10676 return (void*) uc->uc_mcontext->__ss.__eip;
da0a1620 10677 #endif
06db1f50 10678#elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
cb7e07cc 10679 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
06db1f50 10680 return (void*) uc->uc_mcontext->__ss.__rip;
cbc59b38 10681 #else
10682 return (void*) uc->uc_mcontext->__ss.__eip;
e0a62c7f 10683 #endif
54bac49d 10684#elif defined(__i386__) || defined(__X86_64__) || defined(__x86_64__)
c04c9ac9 10685 return (void*) uc->uc_mcontext.gregs[REG_EIP]; /* Linux 32/64 bit */
b91cf5ef 10686#elif defined(__ia64__) /* Linux IA64 */
10687 return (void*) uc->uc_mcontext.sc_ip;
10688#else
10689 return NULL;
56906eef 10690#endif
10691}
10692
10693static void segvHandler(int sig, siginfo_t *info, void *secret) {
10694 void *trace[100];
10695 char **messages = NULL;
10696 int i, trace_size = 0;
10697 unsigned long offset=0;
56906eef 10698 ucontext_t *uc = (ucontext_t*) secret;
1c85b79f 10699 sds infostring;
56906eef 10700 REDIS_NOTUSED(info);
10701
10702 redisLog(REDIS_WARNING,
10703 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION, sig);
1c85b79f 10704 infostring = genRedisInfoString();
10705 redisLog(REDIS_WARNING, "%s",infostring);
10706 /* It's not safe to sdsfree() the returned string under memory
10707 * corruption conditions. Let it leak as we are going to abort */
e0a62c7f 10708
56906eef 10709 trace_size = backtrace(trace, 100);
de96dbfe 10710 /* overwrite sigaction with caller's address */
b91cf5ef 10711 if (getMcontextEip(uc) != NULL) {
10712 trace[1] = getMcontextEip(uc);
10713 }
56906eef 10714 messages = backtrace_symbols(trace, trace_size);
fe3bbfbe 10715
d76412d1 10716 for (i=1; i<trace_size; ++i) {
56906eef 10717 char *fn = findFuncName(trace[i], &offset), *p;
10718
10719 p = strchr(messages[i],'+');
10720 if (!fn || (p && ((unsigned long)strtol(p+1,NULL,10)) < offset)) {
10721 redisLog(REDIS_WARNING,"%s", messages[i]);
10722 } else {
10723 redisLog(REDIS_WARNING,"%d redis-server %p %s + %d", i, trace[i], fn, (unsigned int)offset);
10724 }
10725 }
b177fd30 10726 /* free(messages); Don't call free() with possibly corrupted memory. */
478c2c6f 10727 _exit(0);
fe3bbfbe 10728}
56906eef 10729
10730static void setupSigSegvAction(void) {
10731 struct sigaction act;
10732
10733 sigemptyset (&act.sa_mask);
10734 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
10735 * is used. Otherwise, sa_handler is used */
10736 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
10737 act.sa_sigaction = segvHandler;
10738 sigaction (SIGSEGV, &act, NULL);
10739 sigaction (SIGBUS, &act, NULL);
12fea928 10740 sigaction (SIGFPE, &act, NULL);
10741 sigaction (SIGILL, &act, NULL);
10742 sigaction (SIGBUS, &act, NULL);
e65fdc78 10743 return;
56906eef 10744}
e65fdc78 10745
bcfc686d 10746#include "staticsymbols.h"
10747/* This function try to convert a pointer into a function name. It's used in
10748 * oreder to provide a backtrace under segmentation fault that's able to
10749 * display functions declared as static (otherwise the backtrace is useless). */
10750static char *findFuncName(void *pointer, unsigned long *offset){
10751 int i, ret = -1;
10752 unsigned long off, minoff = 0;
ed9b544e 10753
bcfc686d 10754 /* Try to match against the Symbol with the smallest offset */
10755 for (i=0; symsTable[i].pointer; i++) {
10756 unsigned long lp = (unsigned long) pointer;
0bc03378 10757
bcfc686d 10758 if (lp != (unsigned long)-1 && lp >= symsTable[i].pointer) {
10759 off=lp-symsTable[i].pointer;
10760 if (ret < 0 || off < minoff) {
10761 minoff=off;
10762 ret=i;
10763 }
10764 }
0bc03378 10765 }
bcfc686d 10766 if (ret == -1) return NULL;
10767 *offset = minoff;
10768 return symsTable[ret].name;
0bc03378 10769}
bcfc686d 10770#else /* HAVE_BACKTRACE */
10771static void setupSigSegvAction(void) {
0bc03378 10772}
bcfc686d 10773#endif /* HAVE_BACKTRACE */
0bc03378 10774
ed9b544e 10775
ed9b544e 10776
bcfc686d 10777/* The End */
10778
10779
ed9b544e 10780