Don't rely on cliReadReply being able to return on shutdown
[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
24df7698 30#define REDIS_VERSION "1.3.10"
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);
2830ca53
PN
726static void zunionCommand(redisClient *c);
727static void zinterCommand(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},
788 {"zunion",zunionCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
789 {"zinter",zinterCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
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
f44dd428 2763static void addReplyLong(redisClient *c, long l) {
2764 char buf[128];
2765 size_t len;
2766
dd88747b 2767 if (l == 0) {
2768 addReply(c,shared.czero);
2769 return;
2770 } else if (l == 1) {
2771 addReply(c,shared.cone);
2772 return;
2773 }
f44dd428 2774 len = snprintf(buf,sizeof(buf),":%ld\r\n",l);
2775 addReplySds(c,sdsnewlen(buf,len));
2776}
2777
aa7c2934
PN
2778static void addReplyLongLong(redisClient *c, long long ll) {
2779 char buf[128];
2780 size_t len;
2781
2782 if (ll == 0) {
2783 addReply(c,shared.czero);
2784 return;
2785 } else if (ll == 1) {
2786 addReply(c,shared.cone);
2787 return;
2788 }
2789 len = snprintf(buf,sizeof(buf),":%lld\r\n",ll);
2790 addReplySds(c,sdsnewlen(buf,len));
2791}
2792
92b27fe9 2793static void addReplyUlong(redisClient *c, unsigned long ul) {
2794 char buf[128];
2795 size_t len;
2796
dd88747b 2797 if (ul == 0) {
2798 addReply(c,shared.czero);
2799 return;
2800 } else if (ul == 1) {
2801 addReply(c,shared.cone);
2802 return;
2803 }
92b27fe9 2804 len = snprintf(buf,sizeof(buf),":%lu\r\n",ul);
2805 addReplySds(c,sdsnewlen(buf,len));
2806}
2807
942a3961 2808static void addReplyBulkLen(redisClient *c, robj *obj) {
2809 size_t len;
2810
2811 if (obj->encoding == REDIS_ENCODING_RAW) {
2812 len = sdslen(obj->ptr);
2813 } else {
2814 long n = (long)obj->ptr;
2815
e054afda 2816 /* Compute how many bytes will take this integer as a radix 10 string */
942a3961 2817 len = 1;
2818 if (n < 0) {
2819 len++;
2820 n = -n;
2821 }
2822 while((n = n/10) != 0) {
2823 len++;
2824 }
2825 }
83c6a618 2826 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",(unsigned long)len));
942a3961 2827}
2828
dd88747b 2829static void addReplyBulk(redisClient *c, robj *obj) {
2830 addReplyBulkLen(c,obj);
2831 addReply(c,obj);
2832 addReply(c,shared.crlf);
2833}
2834
500ece7c 2835/* In the CONFIG command we need to add vanilla C string as bulk replies */
2836static void addReplyBulkCString(redisClient *c, char *s) {
2837 if (s == NULL) {
2838 addReply(c,shared.nullbulk);
2839 } else {
2840 robj *o = createStringObject(s,strlen(s));
2841 addReplyBulk(c,o);
2842 decrRefCount(o);
2843 }
2844}
2845
ed9b544e 2846static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
2847 int cport, cfd;
2848 char cip[128];
285add55 2849 redisClient *c;
ed9b544e 2850 REDIS_NOTUSED(el);
2851 REDIS_NOTUSED(mask);
2852 REDIS_NOTUSED(privdata);
2853
2854 cfd = anetAccept(server.neterr, fd, cip, &cport);
2855 if (cfd == AE_ERR) {
f870935d 2856 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
ed9b544e 2857 return;
2858 }
f870935d 2859 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
285add55 2860 if ((c = createClient(cfd)) == NULL) {
ed9b544e 2861 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
2862 close(cfd); /* May be already closed, just ingore errors */
2863 return;
2864 }
285add55 2865 /* If maxclient directive is set and this is one client more... close the
2866 * connection. Note that we create the client instead to check before
2867 * for this condition, since now the socket is already set in nonblocking
2868 * mode and we can send an error for free using the Kernel I/O */
2869 if (server.maxclients && listLength(server.clients) > server.maxclients) {
2870 char *err = "-ERR max number of clients reached\r\n";
2871
2872 /* That's a best effort error message, don't check write errors */
fee803ba 2873 if (write(c->fd,err,strlen(err)) == -1) {
2874 /* Nothing to do, Just to avoid the warning... */
2875 }
285add55 2876 freeClient(c);
2877 return;
2878 }
ed9b544e 2879 server.stat_numconnections++;
2880}
2881
2882/* ======================= Redis objects implementation ===================== */
2883
2884static robj *createObject(int type, void *ptr) {
2885 robj *o;
2886
a5819310 2887 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
ed9b544e 2888 if (listLength(server.objfreelist)) {
2889 listNode *head = listFirst(server.objfreelist);
2890 o = listNodeValue(head);
2891 listDelNode(server.objfreelist,head);
a5819310 2892 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
ed9b544e 2893 } else {
75680a3c 2894 if (server.vm_enabled) {
a5819310 2895 pthread_mutex_unlock(&server.obj_freelist_mutex);
75680a3c 2896 o = zmalloc(sizeof(*o));
2897 } else {
2898 o = zmalloc(sizeof(*o)-sizeof(struct redisObjectVM));
2899 }
ed9b544e 2900 }
ed9b544e 2901 o->type = type;
942a3961 2902 o->encoding = REDIS_ENCODING_RAW;
ed9b544e 2903 o->ptr = ptr;
2904 o->refcount = 1;
3a66edc7 2905 if (server.vm_enabled) {
1064ef87 2906 /* Note that this code may run in the context of an I/O thread
2907 * and accessing to server.unixtime in theory is an error
2908 * (no locks). But in practice this is safe, and even if we read
2909 * garbage Redis will not fail, as it's just a statistical info */
3a66edc7 2910 o->vm.atime = server.unixtime;
2911 o->storage = REDIS_VM_MEMORY;
2912 }
ed9b544e 2913 return o;
2914}
2915
2916static robj *createStringObject(char *ptr, size_t len) {
2917 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
2918}
2919
3f973463
PN
2920static robj *createStringObjectFromLongLong(long long value) {
2921 robj *o;
2922 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
2923 incrRefCount(shared.integers[value]);
2924 o = shared.integers[value];
2925 } else {
2926 o = createObject(REDIS_STRING, NULL);
2927 if (value >= LONG_MIN && value <= LONG_MAX) {
2928 o->encoding = REDIS_ENCODING_INT;
2929 o->ptr = (void*)((long)value);
2930 } else {
ee14da56 2931 o = createObject(REDIS_STRING,sdsfromlonglong(value));
3f973463
PN
2932 }
2933 }
2934 return o;
2935}
2936
4ef8de8a 2937static robj *dupStringObject(robj *o) {
b9bc0eef 2938 assert(o->encoding == REDIS_ENCODING_RAW);
4ef8de8a 2939 return createStringObject(o->ptr,sdslen(o->ptr));
2940}
2941
ed9b544e 2942static robj *createListObject(void) {
2943 list *l = listCreate();
2944
ed9b544e 2945 listSetFreeMethod(l,decrRefCount);
2946 return createObject(REDIS_LIST,l);
2947}
2948
2949static robj *createSetObject(void) {
2950 dict *d = dictCreate(&setDictType,NULL);
ed9b544e 2951 return createObject(REDIS_SET,d);
2952}
2953
5234952b 2954static robj *createHashObject(void) {
2955 /* All the Hashes start as zipmaps. Will be automatically converted
2956 * into hash tables if there are enough elements or big elements
2957 * inside. */
2958 unsigned char *zm = zipmapNew();
2959 robj *o = createObject(REDIS_HASH,zm);
2960 o->encoding = REDIS_ENCODING_ZIPMAP;
2961 return o;
2962}
2963
1812e024 2964static robj *createZsetObject(void) {
6b47e12e 2965 zset *zs = zmalloc(sizeof(*zs));
2966
2967 zs->dict = dictCreate(&zsetDictType,NULL);
2968 zs->zsl = zslCreate();
2969 return createObject(REDIS_ZSET,zs);
1812e024 2970}
2971
ed9b544e 2972static void freeStringObject(robj *o) {
942a3961 2973 if (o->encoding == REDIS_ENCODING_RAW) {
2974 sdsfree(o->ptr);
2975 }
ed9b544e 2976}
2977
2978static void freeListObject(robj *o) {
2979 listRelease((list*) o->ptr);
2980}
2981
2982static void freeSetObject(robj *o) {
2983 dictRelease((dict*) o->ptr);
2984}
2985
fd8ccf44 2986static void freeZsetObject(robj *o) {
2987 zset *zs = o->ptr;
2988
2989 dictRelease(zs->dict);
2990 zslFree(zs->zsl);
2991 zfree(zs);
2992}
2993
ed9b544e 2994static void freeHashObject(robj *o) {
cbba7dd7 2995 switch (o->encoding) {
2996 case REDIS_ENCODING_HT:
2997 dictRelease((dict*) o->ptr);
2998 break;
2999 case REDIS_ENCODING_ZIPMAP:
3000 zfree(o->ptr);
3001 break;
3002 default:
f83c6cb5 3003 redisPanic("Unknown hash encoding type");
cbba7dd7 3004 break;
3005 }
ed9b544e 3006}
3007
3008static void incrRefCount(robj *o) {
3009 o->refcount++;
3010}
3011
3012static void decrRefCount(void *obj) {
3013 robj *o = obj;
94754ccc 3014
c651fd9e 3015 if (o->refcount <= 0) redisPanic("decrRefCount against refcount <= 0");
970e10bb 3016 /* Object is a key of a swapped out value, or in the process of being
3017 * loaded. */
996cb5f7 3018 if (server.vm_enabled &&
3019 (o->storage == REDIS_VM_SWAPPED || o->storage == REDIS_VM_LOADING))
3020 {
996cb5f7 3021 if (o->storage == REDIS_VM_LOADING) vmCancelThreadedIOJob(obj);
f2b8ab34 3022 redisAssert(o->type == REDIS_STRING);
a35ddf12 3023 freeStringObject(o);
3024 vmMarkPagesFree(o->vm.page,o->vm.usedpages);
a5819310 3025 pthread_mutex_lock(&server.obj_freelist_mutex);
a35ddf12 3026 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
3027 !listAddNodeHead(server.objfreelist,o))
3028 zfree(o);
a5819310 3029 pthread_mutex_unlock(&server.obj_freelist_mutex);
7d98e08c 3030 server.vm_stats_swapped_objects--;
a35ddf12 3031 return;
3032 }
996cb5f7 3033 /* Object is in memory, or in the process of being swapped out. */
ed9b544e 3034 if (--(o->refcount) == 0) {
996cb5f7 3035 if (server.vm_enabled && o->storage == REDIS_VM_SWAPPING)
3036 vmCancelThreadedIOJob(obj);
ed9b544e 3037 switch(o->type) {
3038 case REDIS_STRING: freeStringObject(o); break;
3039 case REDIS_LIST: freeListObject(o); break;
3040 case REDIS_SET: freeSetObject(o); break;
fd8ccf44 3041 case REDIS_ZSET: freeZsetObject(o); break;
ed9b544e 3042 case REDIS_HASH: freeHashObject(o); break;
f83c6cb5 3043 default: redisPanic("Unknown object type"); break;
ed9b544e 3044 }
a5819310 3045 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
ed9b544e 3046 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
3047 !listAddNodeHead(server.objfreelist,o))
3048 zfree(o);
a5819310 3049 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
ed9b544e 3050 }
3051}
3052
942a3961 3053static robj *lookupKey(redisDb *db, robj *key) {
3054 dictEntry *de = dictFind(db->dict,key);
3a66edc7 3055 if (de) {
55cf8433 3056 robj *key = dictGetEntryKey(de);
3057 robj *val = dictGetEntryVal(de);
3a66edc7 3058
55cf8433 3059 if (server.vm_enabled) {
996cb5f7 3060 if (key->storage == REDIS_VM_MEMORY ||
3061 key->storage == REDIS_VM_SWAPPING)
3062 {
3063 /* If we were swapping the object out, stop it, this key
3064 * was requested. */
3065 if (key->storage == REDIS_VM_SWAPPING)
3066 vmCancelThreadedIOJob(key);
55cf8433 3067 /* Update the access time of the key for the aging algorithm. */
3068 key->vm.atime = server.unixtime;
3069 } else {
d5d55fc3 3070 int notify = (key->storage == REDIS_VM_LOADING);
3071
55cf8433 3072 /* Our value was swapped on disk. Bring it at home. */
f2b8ab34 3073 redisAssert(val == NULL);
55cf8433 3074 val = vmLoadObject(key);
3075 dictGetEntryVal(de) = val;
d5d55fc3 3076
3077 /* Clients blocked by the VM subsystem may be waiting for
3078 * this key... */
3079 if (notify) handleClientsBlockedOnSwappedKey(db,key);
55cf8433 3080 }
3081 }
3082 return val;
3a66edc7 3083 } else {
3084 return NULL;
3085 }
942a3961 3086}
3087
3088static robj *lookupKeyRead(redisDb *db, robj *key) {
3089 expireIfNeeded(db,key);
3090 return lookupKey(db,key);
3091}
3092
3093static robj *lookupKeyWrite(redisDb *db, robj *key) {
3094 deleteIfVolatile(db,key);
3095 return lookupKey(db,key);
3096}
3097
92b27fe9 3098static robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
3099 robj *o = lookupKeyRead(c->db, key);
3100 if (!o) addReply(c,reply);
3101 return o;
3102}
3103
3104static robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
3105 robj *o = lookupKeyWrite(c->db, key);
3106 if (!o) addReply(c,reply);
3107 return o;
3108}
3109
3110static int checkType(redisClient *c, robj *o, int type) {
3111 if (o->type != type) {
3112 addReply(c,shared.wrongtypeerr);
3113 return 1;
3114 }
3115 return 0;
3116}
3117
942a3961 3118static int deleteKey(redisDb *db, robj *key) {
3119 int retval;
3120
3121 /* We need to protect key from destruction: after the first dictDelete()
3122 * it may happen that 'key' is no longer valid if we don't increment
3123 * it's count. This may happen when we get the object reference directly
3124 * from the hash table with dictRandomKey() or dict iterators */
3125 incrRefCount(key);
3126 if (dictSize(db->expires)) dictDelete(db->expires,key);
3127 retval = dictDelete(db->dict,key);
3128 decrRefCount(key);
3129
3130 return retval == DICT_OK;
3131}
3132
724a51b1 3133/* Check if the nul-terminated string 's' can be represented by a long
3134 * (that is, is a number that fits into long without any other space or
3135 * character before or after the digits).
3136 *
3137 * If so, the function returns REDIS_OK and *longval is set to the value
3138 * of the number. Otherwise REDIS_ERR is returned */
f69f2cba 3139static int isStringRepresentableAsLong(sds s, long *longval) {
724a51b1 3140 char buf[32], *endptr;
3141 long value;
3142 int slen;
e0a62c7f 3143
724a51b1 3144 value = strtol(s, &endptr, 10);
3145 if (endptr[0] != '\0') return REDIS_ERR;
ee14da56 3146 slen = ll2string(buf,32,value);
724a51b1 3147
3148 /* If the number converted back into a string is not identical
3149 * then it's not possible to encode the string as integer */
f69f2cba 3150 if (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR;
724a51b1 3151 if (longval) *longval = value;
3152 return REDIS_OK;
3153}
3154
942a3961 3155/* Try to encode a string object in order to save space */
05df7621 3156static robj *tryObjectEncoding(robj *o) {
942a3961 3157 long value;
942a3961 3158 sds s = o->ptr;
3305306f 3159
942a3961 3160 if (o->encoding != REDIS_ENCODING_RAW)
05df7621 3161 return o; /* Already encoded */
3305306f 3162
05df7621 3163 /* It's not safe to encode shared objects: shared objects can be shared
942a3961 3164 * everywhere in the "object space" of Redis. Encoded objects can only
3165 * appear as "values" (and not, for instance, as keys) */
05df7621 3166 if (o->refcount > 1) return o;
3305306f 3167
942a3961 3168 /* Currently we try to encode only strings */
dfc5e96c 3169 redisAssert(o->type == REDIS_STRING);
94754ccc 3170
724a51b1 3171 /* Check if we can represent this string as a long integer */
05df7621 3172 if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return o;
942a3961 3173
3174 /* Ok, this object can be encoded */
05df7621 3175 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
3176 decrRefCount(o);
3177 incrRefCount(shared.integers[value]);
3178 return shared.integers[value];
3179 } else {
3180 o->encoding = REDIS_ENCODING_INT;
3181 sdsfree(o->ptr);
3182 o->ptr = (void*) value;
3183 return o;
3184 }
942a3961 3185}
3186
9d65a1bb 3187/* Get a decoded version of an encoded object (returned as a new object).
3188 * If the object is already raw-encoded just increment the ref count. */
3189static robj *getDecodedObject(robj *o) {
942a3961 3190 robj *dec;
e0a62c7f 3191
9d65a1bb 3192 if (o->encoding == REDIS_ENCODING_RAW) {
3193 incrRefCount(o);
3194 return o;
3195 }
942a3961 3196 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_INT) {
3197 char buf[32];
3198
ee14da56 3199 ll2string(buf,32,(long)o->ptr);
942a3961 3200 dec = createStringObject(buf,strlen(buf));
3201 return dec;
3202 } else {
08ee9b57 3203 redisPanic("Unknown encoding type");
942a3961 3204 }
3305306f 3205}
3206
d7f43c08 3207/* Compare two string objects via strcmp() or alike.
3208 * Note that the objects may be integer-encoded. In such a case we
ee14da56 3209 * use ll2string() to get a string representation of the numbers on the stack
1fd9bc8a 3210 * and compare the strings, it's much faster than calling getDecodedObject().
3211 *
3212 * Important note: if objects are not integer encoded, but binary-safe strings,
3213 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
3214 * binary safe. */
724a51b1 3215static int compareStringObjects(robj *a, robj *b) {
dfc5e96c 3216 redisAssert(a->type == REDIS_STRING && b->type == REDIS_STRING);
d7f43c08 3217 char bufa[128], bufb[128], *astr, *bstr;
3218 int bothsds = 1;
724a51b1 3219
e197b441 3220 if (a == b) return 0;
d7f43c08 3221 if (a->encoding != REDIS_ENCODING_RAW) {
ee14da56 3222 ll2string(bufa,sizeof(bufa),(long) a->ptr);
d7f43c08 3223 astr = bufa;
3224 bothsds = 0;
724a51b1 3225 } else {
d7f43c08 3226 astr = a->ptr;
724a51b1 3227 }
d7f43c08 3228 if (b->encoding != REDIS_ENCODING_RAW) {
ee14da56 3229 ll2string(bufb,sizeof(bufb),(long) b->ptr);
d7f43c08 3230 bstr = bufb;
3231 bothsds = 0;
3232 } else {
3233 bstr = b->ptr;
3234 }
3235 return bothsds ? sdscmp(astr,bstr) : strcmp(astr,bstr);
724a51b1 3236}
3237
bf028098 3238/* Equal string objects return 1 if the two objects are the same from the
3239 * point of view of a string comparison, otherwise 0 is returned. Note that
3240 * this function is faster then checking for (compareStringObject(a,b) == 0)
3241 * because it can perform some more optimization. */
3242static int equalStringObjects(robj *a, robj *b) {
3243 if (a->encoding != REDIS_ENCODING_RAW && b->encoding != REDIS_ENCODING_RAW){
3244 return a->ptr == b->ptr;
3245 } else {
3246 return compareStringObjects(a,b) == 0;
3247 }
3248}
3249
0ea663ea 3250static size_t stringObjectLen(robj *o) {
dfc5e96c 3251 redisAssert(o->type == REDIS_STRING);
0ea663ea 3252 if (o->encoding == REDIS_ENCODING_RAW) {
3253 return sdslen(o->ptr);
3254 } else {
3255 char buf[32];
3256
ee14da56 3257 return ll2string(buf,32,(long)o->ptr);
0ea663ea 3258 }
3259}
3260
bd79a6bd
PN
3261static int getDoubleFromObject(robj *o, double *target) {
3262 double value;
682c73e8 3263 char *eptr;
bbe025e0 3264
bd79a6bd
PN
3265 if (o == NULL) {
3266 value = 0;
3267 } else {
3268 redisAssert(o->type == REDIS_STRING);
3269 if (o->encoding == REDIS_ENCODING_RAW) {
3270 value = strtod(o->ptr, &eptr);
682c73e8 3271 if (eptr[0] != '\0') return REDIS_ERR;
bd79a6bd
PN
3272 } else if (o->encoding == REDIS_ENCODING_INT) {
3273 value = (long)o->ptr;
3274 } else {
946342c1 3275 redisPanic("Unknown string encoding");
bd79a6bd
PN
3276 }
3277 }
3278
bd79a6bd
PN
3279 *target = value;
3280 return REDIS_OK;
3281}
bbe025e0 3282
bd79a6bd
PN
3283static int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg) {
3284 double value;
3285 if (getDoubleFromObject(o, &value) != REDIS_OK) {
3286 if (msg != NULL) {
3287 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3288 } else {
3289 addReplySds(c, sdsnew("-ERR value is not a double\r\n"));
3290 }
bbe025e0
AM
3291 return REDIS_ERR;
3292 }
3293
bd79a6bd 3294 *target = value;
bbe025e0
AM
3295 return REDIS_OK;
3296}
3297
bd79a6bd
PN
3298static int getLongLongFromObject(robj *o, long long *target) {
3299 long long value;
682c73e8 3300 char *eptr;
bbe025e0 3301
bd79a6bd
PN
3302 if (o == NULL) {
3303 value = 0;
3304 } else {
3305 redisAssert(o->type == REDIS_STRING);
3306 if (o->encoding == REDIS_ENCODING_RAW) {
3307 value = strtoll(o->ptr, &eptr, 10);
682c73e8 3308 if (eptr[0] != '\0') return REDIS_ERR;
bd79a6bd
PN
3309 } else if (o->encoding == REDIS_ENCODING_INT) {
3310 value = (long)o->ptr;
3311 } else {
946342c1 3312 redisPanic("Unknown string encoding");
bd79a6bd
PN
3313 }
3314 }
3315
bd79a6bd
PN
3316 *target = value;
3317 return REDIS_OK;
3318}
bbe025e0 3319
bd79a6bd
PN
3320static int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg) {
3321 long long value;
3322 if (getLongLongFromObject(o, &value) != REDIS_OK) {
3323 if (msg != NULL) {
3324 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3325 } else {
3326 addReplySds(c, sdsnew("-ERR value is not an integer\r\n"));
3327 }
bbe025e0
AM
3328 return REDIS_ERR;
3329 }
3330
bd79a6bd 3331 *target = value;
bbe025e0
AM
3332 return REDIS_OK;
3333}
3334
bd79a6bd
PN
3335static int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg) {
3336 long long value;
bbe025e0 3337
bd79a6bd
PN
3338 if (getLongLongFromObjectOrReply(c, o, &value, msg) != REDIS_OK) return REDIS_ERR;
3339 if (value < LONG_MIN || value > LONG_MAX) {
3340 if (msg != NULL) {
3341 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3342 } else {
3343 addReplySds(c, sdsnew("-ERR value is out of range\r\n"));
3344 }
bbe025e0
AM
3345 return REDIS_ERR;
3346 }
3347
bd79a6bd 3348 *target = value;
bbe025e0
AM
3349 return REDIS_OK;
3350}
3351
06233c45 3352/*============================ RDB saving/loading =========================== */
ed9b544e 3353
f78fd11b 3354static int rdbSaveType(FILE *fp, unsigned char type) {
3355 if (fwrite(&type,1,1,fp) == 0) return -1;
3356 return 0;
3357}
3358
bb32ede5 3359static int rdbSaveTime(FILE *fp, time_t t) {
3360 int32_t t32 = (int32_t) t;
3361 if (fwrite(&t32,4,1,fp) == 0) return -1;
3362 return 0;
3363}
3364
e3566d4b 3365/* check rdbLoadLen() comments for more info */
f78fd11b 3366static int rdbSaveLen(FILE *fp, uint32_t len) {
3367 unsigned char buf[2];
3368
3369 if (len < (1<<6)) {
3370 /* Save a 6 bit len */
10c43610 3371 buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
f78fd11b 3372 if (fwrite(buf,1,1,fp) == 0) return -1;
3373 } else if (len < (1<<14)) {
3374 /* Save a 14 bit len */
10c43610 3375 buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
f78fd11b 3376 buf[1] = len&0xFF;
17be1a4a 3377 if (fwrite(buf,2,1,fp) == 0) return -1;
f78fd11b 3378 } else {
3379 /* Save a 32 bit len */
10c43610 3380 buf[0] = (REDIS_RDB_32BITLEN<<6);
f78fd11b 3381 if (fwrite(buf,1,1,fp) == 0) return -1;
3382 len = htonl(len);
3383 if (fwrite(&len,4,1,fp) == 0) return -1;
3384 }
3385 return 0;
3386}
3387
32a66513 3388/* Encode 'value' as an integer if possible (if integer will fit the
3389 * supported range). If the function sucessful encoded the integer
3390 * then the (up to 5 bytes) encoded representation is written in the
3391 * string pointed by 'enc' and the length is returned. Otherwise
3392 * 0 is returned. */
3393static int rdbEncodeInteger(long long value, unsigned char *enc) {
e3566d4b 3394 /* Finally check if it fits in our ranges */
3395 if (value >= -(1<<7) && value <= (1<<7)-1) {
3396 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
3397 enc[1] = value&0xFF;
3398 return 2;
3399 } else if (value >= -(1<<15) && value <= (1<<15)-1) {
3400 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
3401 enc[1] = value&0xFF;
3402 enc[2] = (value>>8)&0xFF;
3403 return 3;
3404 } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
3405 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
3406 enc[1] = value&0xFF;
3407 enc[2] = (value>>8)&0xFF;
3408 enc[3] = (value>>16)&0xFF;
3409 enc[4] = (value>>24)&0xFF;
3410 return 5;
3411 } else {
3412 return 0;
3413 }
3414}
3415
32a66513 3416/* String objects in the form "2391" "-100" without any space and with a
3417 * range of values that can fit in an 8, 16 or 32 bit signed value can be
3418 * encoded as integers to save space */
3419static int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {
3420 long long value;
3421 char *endptr, buf[32];
3422
3423 /* Check if it's possible to encode this value as a number */
3424 value = strtoll(s, &endptr, 10);
3425 if (endptr[0] != '\0') return 0;
3426 ll2string(buf,32,value);
3427
3428 /* If the number converted back into a string is not identical
3429 * then it's not possible to encode the string as integer */
3430 if (strlen(buf) != len || memcmp(buf,s,len)) return 0;
3431
3432 return rdbEncodeInteger(value,enc);
3433}
3434
b1befe6a 3435static int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) {
3436 size_t comprlen, outlen;
774e3047 3437 unsigned char byte;
3438 void *out;
3439
3440 /* We require at least four bytes compression for this to be worth it */
b1befe6a 3441 if (len <= 4) return 0;
3442 outlen = len-4;
3a2694c4 3443 if ((out = zmalloc(outlen+1)) == NULL) return 0;
b1befe6a 3444 comprlen = lzf_compress(s, len, out, outlen);
774e3047 3445 if (comprlen == 0) {
88e85998 3446 zfree(out);
774e3047 3447 return 0;
3448 }
3449 /* Data compressed! Let's save it on disk */
3450 byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
3451 if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
3452 if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
b1befe6a 3453 if (rdbSaveLen(fp,len) == -1) goto writeerr;
774e3047 3454 if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
88e85998 3455 zfree(out);
774e3047 3456 return comprlen;
3457
3458writeerr:
88e85998 3459 zfree(out);
774e3047 3460 return -1;
3461}
3462
e3566d4b 3463/* Save a string objet as [len][data] on disk. If the object is a string
3464 * representation of an integer value we try to safe it in a special form */
b1befe6a 3465static int rdbSaveRawString(FILE *fp, unsigned char *s, size_t len) {
e3566d4b 3466 int enclen;
10c43610 3467
774e3047 3468 /* Try integer encoding */
e3566d4b 3469 if (len <= 11) {
3470 unsigned char buf[5];
b1befe6a 3471 if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) {
e3566d4b 3472 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3473 return 0;
3474 }
3475 }
774e3047 3476
3477 /* Try LZF compression - under 20 bytes it's unable to compress even
88e85998 3478 * aaaaaaaaaaaaaaaaaa so skip it */
121f70cf 3479 if (server.rdbcompression && len > 20) {
774e3047 3480 int retval;
3481
b1befe6a 3482 retval = rdbSaveLzfStringObject(fp,s,len);
774e3047 3483 if (retval == -1) return -1;
3484 if (retval > 0) return 0;
3485 /* retval == 0 means data can't be compressed, save the old way */
3486 }
3487
3488 /* Store verbatim */
10c43610 3489 if (rdbSaveLen(fp,len) == -1) return -1;
b1befe6a 3490 if (len && fwrite(s,len,1,fp) == 0) return -1;
10c43610 3491 return 0;
3492}
3493
942a3961 3494/* Like rdbSaveStringObjectRaw() but handle encoded objects */
3495static int rdbSaveStringObject(FILE *fp, robj *obj) {
3496 int retval;
942a3961 3497
32a66513 3498 /* Avoid to decode the object, then encode it again, if the
3499 * object is alrady integer encoded. */
3500 if (obj->encoding == REDIS_ENCODING_INT) {
3501 long val = (long) obj->ptr;
3502 unsigned char buf[5];
3503 int enclen;
3504
3505 if ((enclen = rdbEncodeInteger(val,buf)) > 0) {
3506 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3507 return 0;
3508 }
3509 /* otherwise... fall throught and continue with the usual
3510 * code path. */
3511 }
3512
f2d9f50f 3513 /* Avoid incr/decr ref count business when possible.
3514 * This plays well with copy-on-write given that we are probably
3515 * in a child process (BGSAVE). Also this makes sure key objects
3516 * of swapped objects are not incRefCount-ed (an assert does not allow
3517 * this in order to avoid bugs) */
3518 if (obj->encoding != REDIS_ENCODING_RAW) {
996cb5f7 3519 obj = getDecodedObject(obj);
b1befe6a 3520 retval = rdbSaveRawString(fp,obj->ptr,sdslen(obj->ptr));
996cb5f7 3521 decrRefCount(obj);
3522 } else {
b1befe6a 3523 retval = rdbSaveRawString(fp,obj->ptr,sdslen(obj->ptr));
996cb5f7 3524 }
9d65a1bb 3525 return retval;
942a3961 3526}
3527
a7866db6 3528/* Save a double value. Doubles are saved as strings prefixed by an unsigned
3529 * 8 bit integer specifing the length of the representation.
3530 * This 8 bit integer has special values in order to specify the following
3531 * conditions:
3532 * 253: not a number
3533 * 254: + inf
3534 * 255: - inf
3535 */
3536static int rdbSaveDoubleValue(FILE *fp, double val) {
3537 unsigned char buf[128];
3538 int len;
3539
3540 if (isnan(val)) {
3541 buf[0] = 253;
3542 len = 1;
3543 } else if (!isfinite(val)) {
3544 len = 1;
3545 buf[0] = (val < 0) ? 255 : 254;
3546 } else {
88e8d89f 3547#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
fe244589 3548 /* Check if the float is in a safe range to be casted into a
3549 * long long. We are assuming that long long is 64 bit here.
3550 * Also we are assuming that there are no implementations around where
3551 * double has precision < 52 bit.
3552 *
3553 * Under this assumptions we test if a double is inside an interval
3554 * where casting to long long is safe. Then using two castings we
3555 * make sure the decimal part is zero. If all this is true we use
3556 * integer printing function that is much faster. */
fb82e75c 3557 double min = -4503599627370495; /* (2^52)-1 */
3558 double max = 4503599627370496; /* -(2^52) */
fe244589 3559 if (val > min && val < max && val == ((double)((long long)val)))
8c096b16 3560 ll2string((char*)buf+1,sizeof(buf),(long long)val);
3561 else
88e8d89f 3562#endif
8c096b16 3563 snprintf((char*)buf+1,sizeof(buf)-1,"%.17g",val);
6c446631 3564 buf[0] = strlen((char*)buf+1);
a7866db6 3565 len = buf[0]+1;
3566 }
3567 if (fwrite(buf,len,1,fp) == 0) return -1;
3568 return 0;
3569}
3570
06233c45 3571/* Save a Redis object. */
3572static int rdbSaveObject(FILE *fp, robj *o) {
3573 if (o->type == REDIS_STRING) {
3574 /* Save a string value */
3575 if (rdbSaveStringObject(fp,o) == -1) return -1;
3576 } else if (o->type == REDIS_LIST) {
3577 /* Save a list value */
3578 list *list = o->ptr;
c7df85a4 3579 listIter li;
06233c45 3580 listNode *ln;
3581
06233c45 3582 if (rdbSaveLen(fp,listLength(list)) == -1) return -1;
c7df85a4 3583 listRewind(list,&li);
3584 while((ln = listNext(&li))) {
06233c45 3585 robj *eleobj = listNodeValue(ln);
3586
3587 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3588 }
3589 } else if (o->type == REDIS_SET) {
3590 /* Save a set value */
3591 dict *set = o->ptr;
3592 dictIterator *di = dictGetIterator(set);
3593 dictEntry *de;
3594
3595 if (rdbSaveLen(fp,dictSize(set)) == -1) return -1;
3596 while((de = dictNext(di)) != NULL) {
3597 robj *eleobj = dictGetEntryKey(de);
3598
3599 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3600 }
3601 dictReleaseIterator(di);
3602 } else if (o->type == REDIS_ZSET) {
3603 /* Save a set value */
3604 zset *zs = o->ptr;
3605 dictIterator *di = dictGetIterator(zs->dict);
3606 dictEntry *de;
3607
3608 if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) return -1;
3609 while((de = dictNext(di)) != NULL) {
3610 robj *eleobj = dictGetEntryKey(de);
3611 double *score = dictGetEntryVal(de);
3612
3613 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3614 if (rdbSaveDoubleValue(fp,*score) == -1) return -1;
3615 }
3616 dictReleaseIterator(di);
b1befe6a 3617 } else if (o->type == REDIS_HASH) {
3618 /* Save a hash value */
3619 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
3620 unsigned char *p = zipmapRewind(o->ptr);
3621 unsigned int count = zipmapLen(o->ptr);
3622 unsigned char *key, *val;
3623 unsigned int klen, vlen;
3624
3625 if (rdbSaveLen(fp,count) == -1) return -1;
3626 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
3627 if (rdbSaveRawString(fp,key,klen) == -1) return -1;
3628 if (rdbSaveRawString(fp,val,vlen) == -1) return -1;
3629 }
3630 } else {
3631 dictIterator *di = dictGetIterator(o->ptr);
3632 dictEntry *de;
3633
3634 if (rdbSaveLen(fp,dictSize((dict*)o->ptr)) == -1) return -1;
3635 while((de = dictNext(di)) != NULL) {
3636 robj *key = dictGetEntryKey(de);
3637 robj *val = dictGetEntryVal(de);
3638
3639 if (rdbSaveStringObject(fp,key) == -1) return -1;
3640 if (rdbSaveStringObject(fp,val) == -1) return -1;
3641 }
3642 dictReleaseIterator(di);
3643 }
06233c45 3644 } else {
f83c6cb5 3645 redisPanic("Unknown object type");
06233c45 3646 }
3647 return 0;
3648}
3649
3650/* Return the length the object will have on disk if saved with
3651 * the rdbSaveObject() function. Currently we use a trick to get
3652 * this length with very little changes to the code. In the future
3653 * we could switch to a faster solution. */
b9bc0eef 3654static off_t rdbSavedObjectLen(robj *o, FILE *fp) {
3655 if (fp == NULL) fp = server.devnull;
06233c45 3656 rewind(fp);
3657 assert(rdbSaveObject(fp,o) != 1);
3658 return ftello(fp);
3659}
3660
06224fec 3661/* Return the number of pages required to save this object in the swap file */
b9bc0eef 3662static off_t rdbSavedObjectPages(robj *o, FILE *fp) {
3663 off_t bytes = rdbSavedObjectLen(o,fp);
e0a62c7f 3664
06224fec 3665 return (bytes+(server.vm_page_size-1))/server.vm_page_size;
3666}
3667
ed9b544e 3668/* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
f78fd11b 3669static int rdbSave(char *filename) {
ed9b544e 3670 dictIterator *di = NULL;
3671 dictEntry *de;
ed9b544e 3672 FILE *fp;
3673 char tmpfile[256];
3674 int j;
bb32ede5 3675 time_t now = time(NULL);
ed9b544e 3676
2316bb3b 3677 /* Wait for I/O therads to terminate, just in case this is a
3678 * foreground-saving, to avoid seeking the swap file descriptor at the
3679 * same time. */
3680 if (server.vm_enabled)
3681 waitEmptyIOJobsQueue();
3682
a3b21203 3683 snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
ed9b544e 3684 fp = fopen(tmpfile,"w");
3685 if (!fp) {
3686 redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno));
3687 return REDIS_ERR;
3688 }
f78fd11b 3689 if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
ed9b544e 3690 for (j = 0; j < server.dbnum; j++) {
bb32ede5 3691 redisDb *db = server.db+j;
3692 dict *d = db->dict;
3305306f 3693 if (dictSize(d) == 0) continue;
ed9b544e 3694 di = dictGetIterator(d);
3695 if (!di) {
3696 fclose(fp);
3697 return REDIS_ERR;
3698 }
3699
3700 /* Write the SELECT DB opcode */
f78fd11b 3701 if (rdbSaveType(fp,REDIS_SELECTDB) == -1) goto werr;
3702 if (rdbSaveLen(fp,j) == -1) goto werr;
ed9b544e 3703
3704 /* Iterate this DB writing every entry */
3705 while((de = dictNext(di)) != NULL) {
3706 robj *key = dictGetEntryKey(de);
3707 robj *o = dictGetEntryVal(de);
bb32ede5 3708 time_t expiretime = getExpire(db,key);
3709
3710 /* Save the expire time */
3711 if (expiretime != -1) {
3712 /* If this key is already expired skip it */
3713 if (expiretime < now) continue;
3714 if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr;
3715 if (rdbSaveTime(fp,expiretime) == -1) goto werr;
3716 }
7e69548d 3717 /* Save the key and associated value. This requires special
3718 * handling if the value is swapped out. */
996cb5f7 3719 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
3720 key->storage == REDIS_VM_SWAPPING) {
7e69548d 3721 /* Save type, key, value */
3722 if (rdbSaveType(fp,o->type) == -1) goto werr;
3723 if (rdbSaveStringObject(fp,key) == -1) goto werr;
3724 if (rdbSaveObject(fp,o) == -1) goto werr;
3725 } else {
996cb5f7 3726 /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
b9bc0eef 3727 robj *po;
7e69548d 3728 /* Get a preview of the object in memory */
3729 po = vmPreviewObject(key);
7e69548d 3730 /* Save type, key, value */
3731 if (rdbSaveType(fp,key->vtype) == -1) goto werr;
b9bc0eef 3732 if (rdbSaveStringObject(fp,key) == -1) goto werr;
7e69548d 3733 if (rdbSaveObject(fp,po) == -1) goto werr;
3734 /* Remove the loaded object from memory */
3735 decrRefCount(po);
7e69548d 3736 }
ed9b544e 3737 }
3738 dictReleaseIterator(di);
3739 }
3740 /* EOF opcode */
f78fd11b 3741 if (rdbSaveType(fp,REDIS_EOF) == -1) goto werr;
3742
3743 /* Make sure data will not remain on the OS's output buffers */
ed9b544e 3744 fflush(fp);
3745 fsync(fileno(fp));
3746 fclose(fp);
e0a62c7f 3747
ed9b544e 3748 /* Use RENAME to make sure the DB file is changed atomically only
3749 * if the generate DB file is ok. */
3750 if (rename(tmpfile,filename) == -1) {
325d1eb4 3751 redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
ed9b544e 3752 unlink(tmpfile);
3753 return REDIS_ERR;
3754 }
3755 redisLog(REDIS_NOTICE,"DB saved on disk");
3756 server.dirty = 0;
3757 server.lastsave = time(NULL);
3758 return REDIS_OK;
3759
3760werr:
3761 fclose(fp);
3762 unlink(tmpfile);
3763 redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
3764 if (di) dictReleaseIterator(di);
3765 return REDIS_ERR;
3766}
3767
f78fd11b 3768static int rdbSaveBackground(char *filename) {
ed9b544e 3769 pid_t childpid;
3770
9d65a1bb 3771 if (server.bgsavechildpid != -1) return REDIS_ERR;
054e426d 3772 if (server.vm_enabled) waitEmptyIOJobsQueue();
ed9b544e 3773 if ((childpid = fork()) == 0) {
3774 /* Child */
054e426d 3775 if (server.vm_enabled) vmReopenSwapFile();
ed9b544e 3776 close(server.fd);
f78fd11b 3777 if (rdbSave(filename) == REDIS_OK) {
478c2c6f 3778 _exit(0);
ed9b544e 3779 } else {
478c2c6f 3780 _exit(1);
ed9b544e 3781 }
3782 } else {
3783 /* Parent */
5a7c647e 3784 if (childpid == -1) {
3785 redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
3786 strerror(errno));
3787 return REDIS_ERR;
3788 }
ed9b544e 3789 redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);
9f3c422c 3790 server.bgsavechildpid = childpid;
884d4b39 3791 updateDictResizePolicy();
ed9b544e 3792 return REDIS_OK;
3793 }
3794 return REDIS_OK; /* unreached */
3795}
3796
a3b21203 3797static void rdbRemoveTempFile(pid_t childpid) {
3798 char tmpfile[256];
3799
3800 snprintf(tmpfile,256,"temp-%d.rdb", (int) childpid);
3801 unlink(tmpfile);
3802}
3803
f78fd11b 3804static int rdbLoadType(FILE *fp) {
3805 unsigned char type;
7b45bfb2 3806 if (fread(&type,1,1,fp) == 0) return -1;
3807 return type;
3808}
3809
bb32ede5 3810static time_t rdbLoadTime(FILE *fp) {
3811 int32_t t32;
3812 if (fread(&t32,4,1,fp) == 0) return -1;
3813 return (time_t) t32;
3814}
3815
e3566d4b 3816/* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
3817 * of this file for a description of how this are stored on disk.
3818 *
3819 * isencoded is set to 1 if the readed length is not actually a length but
3820 * an "encoding type", check the above comments for more info */
c78a8ccc 3821static uint32_t rdbLoadLen(FILE *fp, int *isencoded) {
f78fd11b 3822 unsigned char buf[2];
3823 uint32_t len;
c78a8ccc 3824 int type;
f78fd11b 3825
e3566d4b 3826 if (isencoded) *isencoded = 0;
c78a8ccc 3827 if (fread(buf,1,1,fp) == 0) return REDIS_RDB_LENERR;
3828 type = (buf[0]&0xC0)>>6;
3829 if (type == REDIS_RDB_6BITLEN) {
3830 /* Read a 6 bit len */
3831 return buf[0]&0x3F;
3832 } else if (type == REDIS_RDB_ENCVAL) {
3833 /* Read a 6 bit len encoding type */
3834 if (isencoded) *isencoded = 1;
3835 return buf[0]&0x3F;
3836 } else if (type == REDIS_RDB_14BITLEN) {
3837 /* Read a 14 bit len */
3838 if (fread(buf+1,1,1,fp) == 0) return REDIS_RDB_LENERR;
3839 return ((buf[0]&0x3F)<<8)|buf[1];
3840 } else {
3841 /* Read a 32 bit len */
f78fd11b 3842 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
3843 return ntohl(len);
f78fd11b 3844 }
f78fd11b 3845}
3846
ad30aa60 3847/* Load an integer-encoded object from file 'fp', with the specified
3848 * encoding type 'enctype'. If encode is true the function may return
3849 * an integer-encoded object as reply, otherwise the returned object
3850 * will always be encoded as a raw string. */
3851static robj *rdbLoadIntegerObject(FILE *fp, int enctype, int encode) {
e3566d4b 3852 unsigned char enc[4];
3853 long long val;
3854
3855 if (enctype == REDIS_RDB_ENC_INT8) {
3856 if (fread(enc,1,1,fp) == 0) return NULL;
3857 val = (signed char)enc[0];
3858 } else if (enctype == REDIS_RDB_ENC_INT16) {
3859 uint16_t v;
3860 if (fread(enc,2,1,fp) == 0) return NULL;
3861 v = enc[0]|(enc[1]<<8);
3862 val = (int16_t)v;
3863 } else if (enctype == REDIS_RDB_ENC_INT32) {
3864 uint32_t v;
3865 if (fread(enc,4,1,fp) == 0) return NULL;
3866 v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
3867 val = (int32_t)v;
3868 } else {
3869 val = 0; /* anti-warning */
f83c6cb5 3870 redisPanic("Unknown RDB integer encoding type");
e3566d4b 3871 }
ad30aa60 3872 if (encode)
3873 return createStringObjectFromLongLong(val);
3874 else
3875 return createObject(REDIS_STRING,sdsfromlonglong(val));
e3566d4b 3876}
3877
c78a8ccc 3878static robj *rdbLoadLzfStringObject(FILE*fp) {
88e85998 3879 unsigned int len, clen;
3880 unsigned char *c = NULL;
3881 sds val = NULL;
3882
c78a8ccc 3883 if ((clen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3884 if ((len = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
88e85998 3885 if ((c = zmalloc(clen)) == NULL) goto err;
3886 if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
3887 if (fread(c,clen,1,fp) == 0) goto err;
3888 if (lzf_decompress(c,clen,val,len) == 0) goto err;
5109cdff 3889 zfree(c);
88e85998 3890 return createObject(REDIS_STRING,val);
3891err:
3892 zfree(c);
3893 sdsfree(val);
3894 return NULL;
3895}
3896
ad30aa60 3897static robj *rdbGenericLoadStringObject(FILE*fp, int encode) {
e3566d4b 3898 int isencoded;
3899 uint32_t len;
f78fd11b 3900 sds val;
3901
c78a8ccc 3902 len = rdbLoadLen(fp,&isencoded);
e3566d4b 3903 if (isencoded) {
3904 switch(len) {
3905 case REDIS_RDB_ENC_INT8:
3906 case REDIS_RDB_ENC_INT16:
3907 case REDIS_RDB_ENC_INT32:
ad30aa60 3908 return rdbLoadIntegerObject(fp,len,encode);
88e85998 3909 case REDIS_RDB_ENC_LZF:
bdcb92f2 3910 return rdbLoadLzfStringObject(fp);
e3566d4b 3911 default:
f83c6cb5 3912 redisPanic("Unknown RDB encoding type");
e3566d4b 3913 }
3914 }
3915
f78fd11b 3916 if (len == REDIS_RDB_LENERR) return NULL;
3917 val = sdsnewlen(NULL,len);
3918 if (len && fread(val,len,1,fp) == 0) {
3919 sdsfree(val);
3920 return NULL;
3921 }
bdcb92f2 3922 return createObject(REDIS_STRING,val);
f78fd11b 3923}
3924
ad30aa60 3925static robj *rdbLoadStringObject(FILE *fp) {
3926 return rdbGenericLoadStringObject(fp,0);
3927}
3928
3929static robj *rdbLoadEncodedStringObject(FILE *fp) {
3930 return rdbGenericLoadStringObject(fp,1);
3931}
3932
a7866db6 3933/* For information about double serialization check rdbSaveDoubleValue() */
3934static int rdbLoadDoubleValue(FILE *fp, double *val) {
3935 char buf[128];
3936 unsigned char len;
3937
3938 if (fread(&len,1,1,fp) == 0) return -1;
3939 switch(len) {
3940 case 255: *val = R_NegInf; return 0;
3941 case 254: *val = R_PosInf; return 0;
3942 case 253: *val = R_Nan; return 0;
3943 default:
3944 if (fread(buf,len,1,fp) == 0) return -1;
231d758e 3945 buf[len] = '\0';
a7866db6 3946 sscanf(buf, "%lg", val);
3947 return 0;
3948 }
3949}
3950
c78a8ccc 3951/* Load a Redis object of the specified type from the specified file.
3952 * On success a newly allocated object is returned, otherwise NULL. */
3953static robj *rdbLoadObject(int type, FILE *fp) {
3954 robj *o;
3955
bcd11906 3956 redisLog(REDIS_DEBUG,"LOADING OBJECT %d (at %d)\n",type,ftell(fp));
c78a8ccc 3957 if (type == REDIS_STRING) {
3958 /* Read string value */
ad30aa60 3959 if ((o = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
05df7621 3960 o = tryObjectEncoding(o);
c78a8ccc 3961 } else if (type == REDIS_LIST || type == REDIS_SET) {
3962 /* Read list/set value */
3963 uint32_t listlen;
3964
3965 if ((listlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3966 o = (type == REDIS_LIST) ? createListObject() : createSetObject();
3c68de9b 3967 /* It's faster to expand the dict to the right size asap in order
3968 * to avoid rehashing */
3969 if (type == REDIS_SET && listlen > DICT_HT_INITIAL_SIZE)
3970 dictExpand(o->ptr,listlen);
c78a8ccc 3971 /* Load every single element of the list/set */
3972 while(listlen--) {
3973 robj *ele;
3974
ad30aa60 3975 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
05df7621 3976 ele = tryObjectEncoding(ele);
c78a8ccc 3977 if (type == REDIS_LIST) {
3978 listAddNodeTail((list*)o->ptr,ele);
3979 } else {
3980 dictAdd((dict*)o->ptr,ele,NULL);
3981 }
3982 }
3983 } else if (type == REDIS_ZSET) {
3984 /* Read list/set value */
ada386b2 3985 size_t zsetlen;
c78a8ccc 3986 zset *zs;
3987
3988 if ((zsetlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3989 o = createZsetObject();
3990 zs = o->ptr;
3991 /* Load every single element of the list/set */
3992 while(zsetlen--) {
3993 robj *ele;
3994 double *score = zmalloc(sizeof(double));
3995
ad30aa60 3996 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
05df7621 3997 ele = tryObjectEncoding(ele);
c78a8ccc 3998 if (rdbLoadDoubleValue(fp,score) == -1) return NULL;
3999 dictAdd(zs->dict,ele,score);
4000 zslInsert(zs->zsl,*score,ele);
4001 incrRefCount(ele); /* added to skiplist */
4002 }
ada386b2 4003 } else if (type == REDIS_HASH) {
4004 size_t hashlen;
4005
4006 if ((hashlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4007 o = createHashObject();
4008 /* Too many entries? Use an hash table. */
4009 if (hashlen > server.hash_max_zipmap_entries)
4010 convertToRealHash(o);
4011 /* Load every key/value, then set it into the zipmap or hash
4012 * table, as needed. */
4013 while(hashlen--) {
4014 robj *key, *val;
4015
4016 if ((key = rdbLoadStringObject(fp)) == NULL) return NULL;
4017 if ((val = rdbLoadStringObject(fp)) == NULL) return NULL;
4018 /* If we are using a zipmap and there are too big values
4019 * the object is converted to real hash table encoding. */
4020 if (o->encoding != REDIS_ENCODING_HT &&
4021 (sdslen(key->ptr) > server.hash_max_zipmap_value ||
4022 sdslen(val->ptr) > server.hash_max_zipmap_value))
4023 {
4024 convertToRealHash(o);
4025 }
4026
4027 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
4028 unsigned char *zm = o->ptr;
4029
4030 zm = zipmapSet(zm,key->ptr,sdslen(key->ptr),
4031 val->ptr,sdslen(val->ptr),NULL);
4032 o->ptr = zm;
4033 decrRefCount(key);
4034 decrRefCount(val);
4035 } else {
05df7621 4036 key = tryObjectEncoding(key);
4037 val = tryObjectEncoding(val);
ada386b2 4038 dictAdd((dict*)o->ptr,key,val);
ada386b2 4039 }
4040 }
c78a8ccc 4041 } else {
f83c6cb5 4042 redisPanic("Unknown object type");
c78a8ccc 4043 }
4044 return o;
4045}
4046
f78fd11b 4047static int rdbLoad(char *filename) {
ed9b544e 4048 FILE *fp;
f78fd11b 4049 uint32_t dbid;
bb32ede5 4050 int type, retval, rdbver;
585af7e2 4051 int swap_all_values = 0;
3305306f 4052 dict *d = server.db[0].dict;
bb32ede5 4053 redisDb *db = server.db+0;
f78fd11b 4054 char buf[1024];
242a64f3 4055 time_t expiretime, now = time(NULL);
b492cf00 4056 long long loadedkeys = 0;
bb32ede5 4057
ed9b544e 4058 fp = fopen(filename,"r");
4059 if (!fp) return REDIS_ERR;
4060 if (fread(buf,9,1,fp) == 0) goto eoferr;
f78fd11b 4061 buf[9] = '\0';
4062 if (memcmp(buf,"REDIS",5) != 0) {
ed9b544e 4063 fclose(fp);
4064 redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
4065 return REDIS_ERR;
4066 }
f78fd11b 4067 rdbver = atoi(buf+5);
c78a8ccc 4068 if (rdbver != 1) {
f78fd11b 4069 fclose(fp);
4070 redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
4071 return REDIS_ERR;
4072 }
ed9b544e 4073 while(1) {
585af7e2 4074 robj *key, *val;
ed9b544e 4075
585af7e2 4076 expiretime = -1;
ed9b544e 4077 /* Read type. */
f78fd11b 4078 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
bb32ede5 4079 if (type == REDIS_EXPIRETIME) {
4080 if ((expiretime = rdbLoadTime(fp)) == -1) goto eoferr;
4081 /* We read the time so we need to read the object type again */
4082 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
4083 }
ed9b544e 4084 if (type == REDIS_EOF) break;
4085 /* Handle SELECT DB opcode as a special case */
4086 if (type == REDIS_SELECTDB) {
c78a8ccc 4087 if ((dbid = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR)
e3566d4b 4088 goto eoferr;
ed9b544e 4089 if (dbid >= (unsigned)server.dbnum) {
f78fd11b 4090 redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum);
ed9b544e 4091 exit(1);
4092 }
bb32ede5 4093 db = server.db+dbid;
4094 d = db->dict;
ed9b544e 4095 continue;
4096 }
4097 /* Read key */
585af7e2 4098 if ((key = rdbLoadStringObject(fp)) == NULL) goto eoferr;
c78a8ccc 4099 /* Read value */
585af7e2 4100 if ((val = rdbLoadObject(type,fp)) == NULL) goto eoferr;
89e689c5 4101 /* Check if the key already expired */
4102 if (expiretime != -1 && expiretime < now) {
4103 decrRefCount(key);
4104 decrRefCount(val);
4105 continue;
4106 }
ed9b544e 4107 /* Add the new object in the hash table */
585af7e2 4108 retval = dictAdd(d,key,val);
ed9b544e 4109 if (retval == DICT_ERR) {
585af7e2 4110 redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key->ptr);
ed9b544e 4111 exit(1);
4112 }
242a64f3 4113 loadedkeys++;
bb32ede5 4114 /* Set the expire time if needed */
89e689c5 4115 if (expiretime != -1) setExpire(db,key,expiretime);
242a64f3 4116
b492cf00 4117 /* Handle swapping while loading big datasets when VM is on */
242a64f3 4118
4119 /* If we detecter we are hopeless about fitting something in memory
4120 * we just swap every new key on disk. Directly...
4121 * Note that's important to check for this condition before resorting
4122 * to random sampling, otherwise we may try to swap already
4123 * swapped keys. */
585af7e2 4124 if (swap_all_values) {
4125 dictEntry *de = dictFind(d,key);
242a64f3 4126
4127 /* de may be NULL since the key already expired */
4128 if (de) {
585af7e2 4129 key = dictGetEntryKey(de);
4130 val = dictGetEntryVal(de);
242a64f3 4131
585af7e2 4132 if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
242a64f3 4133 dictGetEntryVal(de) = NULL;
4134 }
4135 }
4136 continue;
4137 }
4138
4139 /* If we have still some hope of having some value fitting memory
4140 * then we try random sampling. */
585af7e2 4141 if (!swap_all_values && server.vm_enabled && (loadedkeys % 5000) == 0) {
b492cf00 4142 while (zmalloc_used_memory() > server.vm_max_memory) {
a69a0c9c 4143 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
b492cf00 4144 }
242a64f3 4145 if (zmalloc_used_memory() > server.vm_max_memory)
585af7e2 4146 swap_all_values = 1; /* We are already using too much mem */
b492cf00 4147 }
ed9b544e 4148 }
4149 fclose(fp);
4150 return REDIS_OK;
4151
4152eoferr: /* unexpected end of file is handled here with a fatal exit */
f80dff62 4153 redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
ed9b544e 4154 exit(1);
4155 return REDIS_ERR; /* Just to avoid warning */
4156}
4157
4158/*================================== Commands =============================== */
4159
abcb223e 4160static void authCommand(redisClient *c) {
2e77c2ee 4161 if (!server.requirepass || !strcmp(c->argv[1]->ptr, server.requirepass)) {
abcb223e
BH
4162 c->authenticated = 1;
4163 addReply(c,shared.ok);
4164 } else {
4165 c->authenticated = 0;
fa4c0aba 4166 addReplySds(c,sdscatprintf(sdsempty(),"-ERR invalid password\r\n"));
abcb223e
BH
4167 }
4168}
4169
ed9b544e 4170static void pingCommand(redisClient *c) {
4171 addReply(c,shared.pong);
4172}
4173
4174static void echoCommand(redisClient *c) {
dd88747b 4175 addReplyBulk(c,c->argv[1]);
ed9b544e 4176}
4177
4178/*=================================== Strings =============================== */
4179
526d00a5 4180static void setGenericCommand(redisClient *c, int nx, robj *key, robj *val, robj *expire) {
ed9b544e 4181 int retval;
10ce1276 4182 long seconds = 0; /* initialized to avoid an harmness warning */
ed9b544e 4183
526d00a5 4184 if (expire) {
4185 if (getLongFromObjectOrReply(c, expire, &seconds, NULL) != REDIS_OK)
4186 return;
4187 if (seconds <= 0) {
4188 addReplySds(c,sdsnew("-ERR invalid expire time in SETEX\r\n"));
4189 return;
4190 }
4191 }
4192
4193 if (nx) deleteIfVolatile(c->db,key);
4194 retval = dictAdd(c->db->dict,key,val);
ed9b544e 4195 if (retval == DICT_ERR) {
4196 if (!nx) {
1b03836c 4197 /* If the key is about a swapped value, we want a new key object
4198 * to overwrite the old. So we delete the old key in the database.
4199 * This will also make sure that swap pages about the old object
4200 * will be marked as free. */
526d00a5 4201 if (server.vm_enabled && deleteIfSwapped(c->db,key))
4202 incrRefCount(key);
4203 dictReplace(c->db->dict,key,val);
4204 incrRefCount(val);
ed9b544e 4205 } else {
c937aa89 4206 addReply(c,shared.czero);
ed9b544e 4207 return;
4208 }
4209 } else {
526d00a5 4210 incrRefCount(key);
4211 incrRefCount(val);
ed9b544e 4212 }
4213 server.dirty++;
526d00a5 4214 removeExpire(c->db,key);
4215 if (expire) setExpire(c->db,key,time(NULL)+seconds);
c937aa89 4216 addReply(c, nx ? shared.cone : shared.ok);
ed9b544e 4217}
4218
4219static void setCommand(redisClient *c) {
526d00a5 4220 setGenericCommand(c,0,c->argv[1],c->argv[2],NULL);
ed9b544e 4221}
4222
4223static void setnxCommand(redisClient *c) {
526d00a5 4224 setGenericCommand(c,1,c->argv[1],c->argv[2],NULL);
4225}
4226
4227static void setexCommand(redisClient *c) {
4228 setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
ed9b544e 4229}
4230
322fc7d8 4231static int getGenericCommand(redisClient *c) {
dd88747b 4232 robj *o;
e0a62c7f 4233
dd88747b 4234 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
322fc7d8 4235 return REDIS_OK;
dd88747b 4236
4237 if (o->type != REDIS_STRING) {
4238 addReply(c,shared.wrongtypeerr);
4239 return REDIS_ERR;
ed9b544e 4240 } else {
dd88747b 4241 addReplyBulk(c,o);
4242 return REDIS_OK;
ed9b544e 4243 }
4244}
4245
322fc7d8 4246static void getCommand(redisClient *c) {
4247 getGenericCommand(c);
4248}
4249
f6b141c5 4250static void getsetCommand(redisClient *c) {
322fc7d8 4251 if (getGenericCommand(c) == REDIS_ERR) return;
a431eb74 4252 if (dictAdd(c->db->dict,c->argv[1],c->argv[2]) == DICT_ERR) {
4253 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
4254 } else {
4255 incrRefCount(c->argv[1]);
4256 }
4257 incrRefCount(c->argv[2]);
4258 server.dirty++;
4259 removeExpire(c->db,c->argv[1]);
4260}
4261
70003d28 4262static void mgetCommand(redisClient *c) {
70003d28 4263 int j;
e0a62c7f 4264
c937aa89 4265 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1));
70003d28 4266 for (j = 1; j < c->argc; j++) {
3305306f 4267 robj *o = lookupKeyRead(c->db,c->argv[j]);
4268 if (o == NULL) {
c937aa89 4269 addReply(c,shared.nullbulk);
70003d28 4270 } else {
70003d28 4271 if (o->type != REDIS_STRING) {
c937aa89 4272 addReply(c,shared.nullbulk);
70003d28 4273 } else {
dd88747b 4274 addReplyBulk(c,o);
70003d28 4275 }
4276 }
4277 }
4278}
4279
6c446631 4280static void msetGenericCommand(redisClient *c, int nx) {
906573e7 4281 int j, busykeys = 0;
6c446631 4282
4283 if ((c->argc % 2) == 0) {
454d4e43 4284 addReplySds(c,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
6c446631 4285 return;
4286 }
4287 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
4288 * set nothing at all if at least one already key exists. */
4289 if (nx) {
4290 for (j = 1; j < c->argc; j += 2) {
906573e7 4291 if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
4292 busykeys++;
6c446631 4293 }
4294 }
4295 }
906573e7 4296 if (busykeys) {
4297 addReply(c, shared.czero);
4298 return;
4299 }
6c446631 4300
4301 for (j = 1; j < c->argc; j += 2) {
4302 int retval;
4303
05df7621 4304 c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
6c446631 4305 retval = dictAdd(c->db->dict,c->argv[j],c->argv[j+1]);
4306 if (retval == DICT_ERR) {
4307 dictReplace(c->db->dict,c->argv[j],c->argv[j+1]);
4308 incrRefCount(c->argv[j+1]);
4309 } else {
4310 incrRefCount(c->argv[j]);
4311 incrRefCount(c->argv[j+1]);
4312 }
4313 removeExpire(c->db,c->argv[j]);
4314 }
4315 server.dirty += (c->argc-1)/2;
4316 addReply(c, nx ? shared.cone : shared.ok);
4317}
4318
4319static void msetCommand(redisClient *c) {
4320 msetGenericCommand(c,0);
4321}
4322
4323static void msetnxCommand(redisClient *c) {
4324 msetGenericCommand(c,1);
4325}
4326
d68ed120 4327static void incrDecrCommand(redisClient *c, long long incr) {
ed9b544e 4328 long long value;
4329 int retval;
4330 robj *o;
e0a62c7f 4331
3305306f 4332 o = lookupKeyWrite(c->db,c->argv[1]);
6485f293
PN
4333 if (o != NULL && checkType(c,o,REDIS_STRING)) return;
4334 if (getLongLongFromObjectOrReply(c,o,&value,NULL) != REDIS_OK) return;
ed9b544e 4335
4336 value += incr;
4337 o = createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",value));
05df7621 4338 o = tryObjectEncoding(o);
3305306f 4339 retval = dictAdd(c->db->dict,c->argv[1],o);
ed9b544e 4340 if (retval == DICT_ERR) {
3305306f 4341 dictReplace(c->db->dict,c->argv[1],o);
4342 removeExpire(c->db,c->argv[1]);
ed9b544e 4343 } else {
4344 incrRefCount(c->argv[1]);
4345 }
4346 server.dirty++;
c937aa89 4347 addReply(c,shared.colon);
ed9b544e 4348 addReply(c,o);
4349 addReply(c,shared.crlf);
4350}
4351
4352static void incrCommand(redisClient *c) {
a4d1ba9a 4353 incrDecrCommand(c,1);
ed9b544e 4354}
4355
4356static void decrCommand(redisClient *c) {
a4d1ba9a 4357 incrDecrCommand(c,-1);
ed9b544e 4358}
4359
4360static void incrbyCommand(redisClient *c) {
bbe025e0
AM
4361 long long incr;
4362
bd79a6bd 4363 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
a4d1ba9a 4364 incrDecrCommand(c,incr);
ed9b544e 4365}
4366
4367static void decrbyCommand(redisClient *c) {
bbe025e0
AM
4368 long long incr;
4369
bd79a6bd 4370 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
a4d1ba9a 4371 incrDecrCommand(c,-incr);
ed9b544e 4372}
4373
4b00bebd 4374static void appendCommand(redisClient *c) {
4375 int retval;
4376 size_t totlen;
4377 robj *o;
4378
4379 o = lookupKeyWrite(c->db,c->argv[1]);
4380 if (o == NULL) {
4381 /* Create the key */
4382 retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
4383 incrRefCount(c->argv[1]);
4384 incrRefCount(c->argv[2]);
4385 totlen = stringObjectLen(c->argv[2]);
4386 } else {
4387 dictEntry *de;
e0a62c7f 4388
4b00bebd 4389 de = dictFind(c->db->dict,c->argv[1]);
4390 assert(de != NULL);
4391
4392 o = dictGetEntryVal(de);
4393 if (o->type != REDIS_STRING) {
4394 addReply(c,shared.wrongtypeerr);
4395 return;
4396 }
4397 /* If the object is specially encoded or shared we have to make
4398 * a copy */
4399 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
4400 robj *decoded = getDecodedObject(o);
4401
4402 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
4403 decrRefCount(decoded);
4404 dictReplace(c->db->dict,c->argv[1],o);
4405 }
4406 /* APPEND! */
4407 if (c->argv[2]->encoding == REDIS_ENCODING_RAW) {
4408 o->ptr = sdscatlen(o->ptr,
4409 c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
4410 } else {
4411 o->ptr = sdscatprintf(o->ptr, "%ld",
4412 (unsigned long) c->argv[2]->ptr);
4413 }
4414 totlen = sdslen(o->ptr);
4415 }
4416 server.dirty++;
4417 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen));
4418}
4419
39191553 4420static void substrCommand(redisClient *c) {
4421 robj *o;
4422 long start = atoi(c->argv[2]->ptr);
4423 long end = atoi(c->argv[3]->ptr);
dd88747b 4424 size_t rangelen, strlen;
4425 sds range;
39191553 4426
dd88747b 4427 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4428 checkType(c,o,REDIS_STRING)) return;
39191553 4429
dd88747b 4430 o = getDecodedObject(o);
4431 strlen = sdslen(o->ptr);
8fe7fad7 4432
dd88747b 4433 /* convert negative indexes */
4434 if (start < 0) start = strlen+start;
4435 if (end < 0) end = strlen+end;
4436 if (start < 0) start = 0;
4437 if (end < 0) end = 0;
39191553 4438
dd88747b 4439 /* indexes sanity checks */
4440 if (start > end || (size_t)start >= strlen) {
4441 /* Out of range start or start > end result in null reply */
4442 addReply(c,shared.nullbulk);
4443 decrRefCount(o);
4444 return;
39191553 4445 }
dd88747b 4446 if ((size_t)end >= strlen) end = strlen-1;
4447 rangelen = (end-start)+1;
4448
4449 /* Return the result */
4450 addReplySds(c,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen));
4451 range = sdsnewlen((char*)o->ptr+start,rangelen);
4452 addReplySds(c,range);
4453 addReply(c,shared.crlf);
4454 decrRefCount(o);
39191553 4455}
4456
ed9b544e 4457/* ========================= Type agnostic commands ========================= */
4458
4459static void delCommand(redisClient *c) {
5109cdff 4460 int deleted = 0, j;
4461
4462 for (j = 1; j < c->argc; j++) {
4463 if (deleteKey(c->db,c->argv[j])) {
4464 server.dirty++;
4465 deleted++;
4466 }
4467 }
dd88747b 4468 addReplyLong(c,deleted);
ed9b544e 4469}
4470
4471static void existsCommand(redisClient *c) {
f4f06efc
PN
4472 expireIfNeeded(c->db,c->argv[1]);
4473 if (dictFind(c->db->dict,c->argv[1])) {
4474 addReply(c, shared.cone);
4475 } else {
4476 addReply(c, shared.czero);
4477 }
ed9b544e 4478}
4479
4480static void selectCommand(redisClient *c) {
4481 int id = atoi(c->argv[1]->ptr);
e0a62c7f 4482
ed9b544e 4483 if (selectDb(c,id) == REDIS_ERR) {
774e3047 4484 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
ed9b544e 4485 } else {
4486 addReply(c,shared.ok);
4487 }
4488}
4489
4490static void randomkeyCommand(redisClient *c) {
4491 dictEntry *de;
dc4be23e 4492 robj *key;
e0a62c7f 4493
3305306f 4494 while(1) {
4495 de = dictGetRandomKey(c->db->dict);
ce7bef07 4496 if (!de || expireIfNeeded(c->db,dictGetEntryKey(de)) == 0) break;
3305306f 4497 }
2b619329 4498
ed9b544e 4499 if (de == NULL) {
dc4be23e 4500 addReply(c,shared.nullbulk);
4501 return;
4502 }
4503
4504 key = dictGetEntryKey(de);
4505 if (server.vm_enabled) {
4506 key = dupStringObject(key);
4507 addReplyBulk(c,key);
4508 decrRefCount(key);
ed9b544e 4509 } else {
dc4be23e 4510 addReplyBulk(c,key);
ed9b544e 4511 }
4512}
4513
4514static void keysCommand(redisClient *c) {
4515 dictIterator *di;
4516 dictEntry *de;
4517 sds pattern = c->argv[1]->ptr;
4518 int plen = sdslen(pattern);
a3f9eec2 4519 unsigned long numkeys = 0;
ed9b544e 4520 robj *lenobj = createObject(REDIS_STRING,NULL);
4521
3305306f 4522 di = dictGetIterator(c->db->dict);
ed9b544e 4523 addReply(c,lenobj);
4524 decrRefCount(lenobj);
4525 while((de = dictNext(di)) != NULL) {
4526 robj *keyobj = dictGetEntryKey(de);
3305306f 4527
ed9b544e 4528 sds key = keyobj->ptr;
4529 if ((pattern[0] == '*' && pattern[1] == '\0') ||
4530 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
3305306f 4531 if (expireIfNeeded(c->db,keyobj) == 0) {
dd88747b 4532 addReplyBulk(c,keyobj);
3305306f 4533 numkeys++;
3305306f 4534 }
ed9b544e 4535 }
4536 }
4537 dictReleaseIterator(di);
a3f9eec2 4538 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",numkeys);
ed9b544e 4539}
4540
4541static void dbsizeCommand(redisClient *c) {
4542 addReplySds(c,
3305306f 4543 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
ed9b544e 4544}
4545
4546static void lastsaveCommand(redisClient *c) {
4547 addReplySds(c,
c937aa89 4548 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
ed9b544e 4549}
4550
4551static void typeCommand(redisClient *c) {
3305306f 4552 robj *o;
ed9b544e 4553 char *type;
3305306f 4554
4555 o = lookupKeyRead(c->db,c->argv[1]);
4556 if (o == NULL) {
c937aa89 4557 type = "+none";
ed9b544e 4558 } else {
ed9b544e 4559 switch(o->type) {
c937aa89 4560 case REDIS_STRING: type = "+string"; break;
4561 case REDIS_LIST: type = "+list"; break;
4562 case REDIS_SET: type = "+set"; break;
412a8bce 4563 case REDIS_ZSET: type = "+zset"; break;
ada386b2 4564 case REDIS_HASH: type = "+hash"; break;
4565 default: type = "+unknown"; break;
ed9b544e 4566 }
4567 }
4568 addReplySds(c,sdsnew(type));
4569 addReply(c,shared.crlf);
4570}
4571
4572static void saveCommand(redisClient *c) {
9d65a1bb 4573 if (server.bgsavechildpid != -1) {
05557f6d 4574 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
4575 return;
4576 }
f78fd11b 4577 if (rdbSave(server.dbfilename) == REDIS_OK) {
ed9b544e 4578 addReply(c,shared.ok);
4579 } else {
4580 addReply(c,shared.err);
4581 }
4582}
4583
4584static void bgsaveCommand(redisClient *c) {
9d65a1bb 4585 if (server.bgsavechildpid != -1) {
ed9b544e 4586 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
4587 return;
4588 }
f78fd11b 4589 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
49b99ab4 4590 char *status = "+Background saving started\r\n";
4591 addReplySds(c,sdsnew(status));
ed9b544e 4592 } else {
4593 addReply(c,shared.err);
4594 }
4595}
4596
4597static void shutdownCommand(redisClient *c) {
4598 redisLog(REDIS_WARNING,"User requested shutdown, saving DB...");
a3b21203 4599 /* Kill the saving child if there is a background saving in progress.
4600 We want to avoid race conditions, for instance our saving child may
4601 overwrite the synchronous saving did by SHUTDOWN. */
9d65a1bb 4602 if (server.bgsavechildpid != -1) {
9f3c422c 4603 redisLog(REDIS_WARNING,"There is a live saving child. Killing it!");
4604 kill(server.bgsavechildpid,SIGKILL);
a3b21203 4605 rdbRemoveTempFile(server.bgsavechildpid);
9f3c422c 4606 }
ac945e2d 4607 if (server.appendonly) {
4608 /* Append only file: fsync() the AOF and exit */
4609 fsync(server.appendfd);
054e426d 4610 if (server.vm_enabled) unlink(server.vm_swap_file);
ac945e2d 4611 exit(0);
ed9b544e 4612 } else {
ac945e2d 4613 /* Snapshotting. Perform a SYNC SAVE and exit */
4614 if (rdbSave(server.dbfilename) == REDIS_OK) {
4615 if (server.daemonize)
4616 unlink(server.pidfile);
4617 redisLog(REDIS_WARNING,"%zu bytes used at exit",zmalloc_used_memory());
4618 redisLog(REDIS_WARNING,"Server exit now, bye bye...");
4619 exit(0);
4620 } else {
dd88747b 4621 /* Ooops.. error saving! The best we can do is to continue
4622 * operating. Note that if there was a background saving process,
4623 * in the next cron() Redis will be notified that the background
4624 * saving aborted, handling special stuff like slaves pending for
4625 * synchronization... */
e0a62c7f 4626 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit");
dd88747b 4627 addReplySds(c,
4628 sdsnew("-ERR can't quit, problems saving the DB\r\n"));
ac945e2d 4629 }
ed9b544e 4630 }
4631}
4632
4633static void renameGenericCommand(redisClient *c, int nx) {
ed9b544e 4634 robj *o;
4635
4636 /* To use the same key as src and dst is probably an error */
4637 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
c937aa89 4638 addReply(c,shared.sameobjecterr);
ed9b544e 4639 return;
4640 }
4641
dd88747b 4642 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
ed9b544e 4643 return;
dd88747b 4644
ed9b544e 4645 incrRefCount(o);
3305306f 4646 deleteIfVolatile(c->db,c->argv[2]);
4647 if (dictAdd(c->db->dict,c->argv[2],o) == DICT_ERR) {
ed9b544e 4648 if (nx) {
4649 decrRefCount(o);
c937aa89 4650 addReply(c,shared.czero);
ed9b544e 4651 return;
4652 }
3305306f 4653 dictReplace(c->db->dict,c->argv[2],o);
ed9b544e 4654 } else {
4655 incrRefCount(c->argv[2]);
4656 }
3305306f 4657 deleteKey(c->db,c->argv[1]);
ed9b544e 4658 server.dirty++;
c937aa89 4659 addReply(c,nx ? shared.cone : shared.ok);
ed9b544e 4660}
4661
4662static void renameCommand(redisClient *c) {
4663 renameGenericCommand(c,0);
4664}
4665
4666static void renamenxCommand(redisClient *c) {
4667 renameGenericCommand(c,1);
4668}
4669
4670static void moveCommand(redisClient *c) {
3305306f 4671 robj *o;
4672 redisDb *src, *dst;
ed9b544e 4673 int srcid;
4674
4675 /* Obtain source and target DB pointers */
3305306f 4676 src = c->db;
4677 srcid = c->db->id;
ed9b544e 4678 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
c937aa89 4679 addReply(c,shared.outofrangeerr);
ed9b544e 4680 return;
4681 }
3305306f 4682 dst = c->db;
4683 selectDb(c,srcid); /* Back to the source DB */
ed9b544e 4684
4685 /* If the user is moving using as target the same
4686 * DB as the source DB it is probably an error. */
4687 if (src == dst) {
c937aa89 4688 addReply(c,shared.sameobjecterr);
ed9b544e 4689 return;
4690 }
4691
4692 /* Check if the element exists and get a reference */
3305306f 4693 o = lookupKeyWrite(c->db,c->argv[1]);
4694 if (!o) {
c937aa89 4695 addReply(c,shared.czero);
ed9b544e 4696 return;
4697 }
4698
4699 /* Try to add the element to the target DB */
3305306f 4700 deleteIfVolatile(dst,c->argv[1]);
4701 if (dictAdd(dst->dict,c->argv[1],o) == DICT_ERR) {
c937aa89 4702 addReply(c,shared.czero);
ed9b544e 4703 return;
4704 }
3305306f 4705 incrRefCount(c->argv[1]);
ed9b544e 4706 incrRefCount(o);
4707
4708 /* OK! key moved, free the entry in the source DB */
3305306f 4709 deleteKey(src,c->argv[1]);
ed9b544e 4710 server.dirty++;
c937aa89 4711 addReply(c,shared.cone);
ed9b544e 4712}
4713
4714/* =================================== Lists ================================ */
4715static void pushGenericCommand(redisClient *c, int where) {
4716 robj *lobj;
ed9b544e 4717 list *list;
3305306f 4718
4719 lobj = lookupKeyWrite(c->db,c->argv[1]);
4720 if (lobj == NULL) {
95242ab5 4721 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
520b5a33 4722 addReply(c,shared.cone);
95242ab5 4723 return;
4724 }
ed9b544e 4725 lobj = createListObject();
4726 list = lobj->ptr;
4727 if (where == REDIS_HEAD) {
6b47e12e 4728 listAddNodeHead(list,c->argv[2]);
ed9b544e 4729 } else {
6b47e12e 4730 listAddNodeTail(list,c->argv[2]);
ed9b544e 4731 }
3305306f 4732 dictAdd(c->db->dict,c->argv[1],lobj);
ed9b544e 4733 incrRefCount(c->argv[1]);
4734 incrRefCount(c->argv[2]);
4735 } else {
ed9b544e 4736 if (lobj->type != REDIS_LIST) {
4737 addReply(c,shared.wrongtypeerr);
4738 return;
4739 }
95242ab5 4740 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
520b5a33 4741 addReply(c,shared.cone);
95242ab5 4742 return;
4743 }
ed9b544e 4744 list = lobj->ptr;
4745 if (where == REDIS_HEAD) {
6b47e12e 4746 listAddNodeHead(list,c->argv[2]);
ed9b544e 4747 } else {
6b47e12e 4748 listAddNodeTail(list,c->argv[2]);
ed9b544e 4749 }
4750 incrRefCount(c->argv[2]);
4751 }
4752 server.dirty++;
520b5a33 4753 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",listLength(list)));
ed9b544e 4754}
4755
4756static void lpushCommand(redisClient *c) {
4757 pushGenericCommand(c,REDIS_HEAD);
4758}
4759
4760static void rpushCommand(redisClient *c) {
4761 pushGenericCommand(c,REDIS_TAIL);
4762}
4763
4764static void llenCommand(redisClient *c) {
3305306f 4765 robj *o;
ed9b544e 4766 list *l;
dd88747b 4767
4768 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
4769 checkType(c,o,REDIS_LIST)) return;
e0a62c7f 4770
dd88747b 4771 l = o->ptr;
4772 addReplyUlong(c,listLength(l));
ed9b544e 4773}
4774
4775static void lindexCommand(redisClient *c) {
3305306f 4776 robj *o;
ed9b544e 4777 int index = atoi(c->argv[2]->ptr);
dd88747b 4778 list *list;
4779 listNode *ln;
4780
4781 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4782 checkType(c,o,REDIS_LIST)) return;
4783 list = o->ptr;
4784
4785 ln = listIndex(list, index);
4786 if (ln == NULL) {
c937aa89 4787 addReply(c,shared.nullbulk);
ed9b544e 4788 } else {
dd88747b 4789 robj *ele = listNodeValue(ln);
4790 addReplyBulk(c,ele);
ed9b544e 4791 }
4792}
4793
4794static void lsetCommand(redisClient *c) {
3305306f 4795 robj *o;
ed9b544e 4796 int index = atoi(c->argv[2]->ptr);
dd88747b 4797 list *list;
4798 listNode *ln;
4799
4800 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL ||
4801 checkType(c,o,REDIS_LIST)) return;
4802 list = o->ptr;
4803
4804 ln = listIndex(list, index);
4805 if (ln == NULL) {
4806 addReply(c,shared.outofrangeerr);
ed9b544e 4807 } else {
dd88747b 4808 robj *ele = listNodeValue(ln);
ed9b544e 4809
dd88747b 4810 decrRefCount(ele);
4811 listNodeValue(ln) = c->argv[3];
4812 incrRefCount(c->argv[3]);
4813 addReply(c,shared.ok);
4814 server.dirty++;
ed9b544e 4815 }
4816}
4817
4818static void popGenericCommand(redisClient *c, int where) {
3305306f 4819 robj *o;
dd88747b 4820 list *list;
4821 listNode *ln;
3305306f 4822
dd88747b 4823 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4824 checkType(c,o,REDIS_LIST)) return;
4825 list = o->ptr;
ed9b544e 4826
dd88747b 4827 if (where == REDIS_HEAD)
4828 ln = listFirst(list);
4829 else
4830 ln = listLast(list);
ed9b544e 4831
dd88747b 4832 if (ln == NULL) {
4833 addReply(c,shared.nullbulk);
4834 } else {
4835 robj *ele = listNodeValue(ln);
4836 addReplyBulk(c,ele);
4837 listDelNode(list,ln);
3ea27d37 4838 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 4839 server.dirty++;
ed9b544e 4840 }
4841}
4842
4843static void lpopCommand(redisClient *c) {
4844 popGenericCommand(c,REDIS_HEAD);
4845}
4846
4847static void rpopCommand(redisClient *c) {
4848 popGenericCommand(c,REDIS_TAIL);
4849}
4850
4851static void lrangeCommand(redisClient *c) {
3305306f 4852 robj *o;
ed9b544e 4853 int start = atoi(c->argv[2]->ptr);
4854 int end = atoi(c->argv[3]->ptr);
dd88747b 4855 int llen;
4856 int rangelen, j;
4857 list *list;
4858 listNode *ln;
4859 robj *ele;
4860
4e27f268 4861 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
4862 || checkType(c,o,REDIS_LIST)) return;
dd88747b 4863 list = o->ptr;
4864 llen = listLength(list);
4865
4866 /* convert negative indexes */
4867 if (start < 0) start = llen+start;
4868 if (end < 0) end = llen+end;
4869 if (start < 0) start = 0;
4870 if (end < 0) end = 0;
4871
4872 /* indexes sanity checks */
4873 if (start > end || start >= llen) {
4874 /* Out of range start or start > end result in empty list */
4875 addReply(c,shared.emptymultibulk);
4876 return;
4877 }
4878 if (end >= llen) end = llen-1;
4879 rangelen = (end-start)+1;
3305306f 4880
dd88747b 4881 /* Return the result in form of a multi-bulk reply */
4882 ln = listIndex(list, start);
4883 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen));
4884 for (j = 0; j < rangelen; j++) {
4885 ele = listNodeValue(ln);
4886 addReplyBulk(c,ele);
4887 ln = ln->next;
ed9b544e 4888 }
4889}
4890
4891static void ltrimCommand(redisClient *c) {
3305306f 4892 robj *o;
ed9b544e 4893 int start = atoi(c->argv[2]->ptr);
4894 int end = atoi(c->argv[3]->ptr);
dd88747b 4895 int llen;
4896 int j, ltrim, rtrim;
4897 list *list;
4898 listNode *ln;
4899
4900 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == NULL ||
4901 checkType(c,o,REDIS_LIST)) return;
4902 list = o->ptr;
4903 llen = listLength(list);
4904
4905 /* convert negative indexes */
4906 if (start < 0) start = llen+start;
4907 if (end < 0) end = llen+end;
4908 if (start < 0) start = 0;
4909 if (end < 0) end = 0;
4910
4911 /* indexes sanity checks */
4912 if (start > end || start >= llen) {
4913 /* Out of range start or start > end result in empty list */
4914 ltrim = llen;
4915 rtrim = 0;
ed9b544e 4916 } else {
dd88747b 4917 if (end >= llen) end = llen-1;
4918 ltrim = start;
4919 rtrim = llen-end-1;
4920 }
ed9b544e 4921
dd88747b 4922 /* Remove list elements to perform the trim */
4923 for (j = 0; j < ltrim; j++) {
4924 ln = listFirst(list);
4925 listDelNode(list,ln);
4926 }
4927 for (j = 0; j < rtrim; j++) {
4928 ln = listLast(list);
4929 listDelNode(list,ln);
ed9b544e 4930 }
3ea27d37 4931 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 4932 server.dirty++;
4933 addReply(c,shared.ok);
ed9b544e 4934}
4935
4936static void lremCommand(redisClient *c) {
3305306f 4937 robj *o;
dd88747b 4938 list *list;
4939 listNode *ln, *next;
4940 int toremove = atoi(c->argv[2]->ptr);
4941 int removed = 0;
4942 int fromtail = 0;
a4d1ba9a 4943
dd88747b 4944 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
4945 checkType(c,o,REDIS_LIST)) return;
4946 list = o->ptr;
4947
4948 if (toremove < 0) {
4949 toremove = -toremove;
4950 fromtail = 1;
4951 }
4952 ln = fromtail ? list->tail : list->head;
4953 while (ln) {
4954 robj *ele = listNodeValue(ln);
4955
4956 next = fromtail ? ln->prev : ln->next;
bf028098 4957 if (equalStringObjects(ele,c->argv[3])) {
dd88747b 4958 listDelNode(list,ln);
4959 server.dirty++;
4960 removed++;
4961 if (toremove && removed == toremove) break;
ed9b544e 4962 }
dd88747b 4963 ln = next;
ed9b544e 4964 }
3ea27d37 4965 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 4966 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
ed9b544e 4967}
4968
12f9d551 4969/* This is the semantic of this command:
0f5f7e9a 4970 * RPOPLPUSH srclist dstlist:
12f9d551 4971 * IF LLEN(srclist) > 0
4972 * element = RPOP srclist
4973 * LPUSH dstlist element
4974 * RETURN element
4975 * ELSE
4976 * RETURN nil
4977 * END
4978 * END
4979 *
4980 * The idea is to be able to get an element from a list in a reliable way
4981 * since the element is not just returned but pushed against another list
4982 * as well. This command was originally proposed by Ezra Zygmuntowicz.
4983 */
0f5f7e9a 4984static void rpoplpushcommand(redisClient *c) {
12f9d551 4985 robj *sobj;
dd88747b 4986 list *srclist;
4987 listNode *ln;
4988
4989 if ((sobj = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4990 checkType(c,sobj,REDIS_LIST)) return;
4991 srclist = sobj->ptr;
4992 ln = listLast(srclist);
12f9d551 4993
dd88747b 4994 if (ln == NULL) {
12f9d551 4995 addReply(c,shared.nullbulk);
4996 } else {
dd88747b 4997 robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
4998 robj *ele = listNodeValue(ln);
4999 list *dstlist;
e20fb74f 5000
dd88747b 5001 if (dobj && dobj->type != REDIS_LIST) {
5002 addReply(c,shared.wrongtypeerr);
5003 return;
5004 }
12f9d551 5005
dd88747b 5006 /* Add the element to the target list (unless it's directly
5007 * passed to some BLPOP-ing client */
5008 if (!handleClientsWaitingListPush(c,c->argv[2],ele)) {
5009 if (dobj == NULL) {
5010 /* Create the list if the key does not exist */
5011 dobj = createListObject();
5012 dictAdd(c->db->dict,c->argv[2],dobj);
5013 incrRefCount(c->argv[2]);
12f9d551 5014 }
dd88747b 5015 dstlist = dobj->ptr;
5016 listAddNodeHead(dstlist,ele);
5017 incrRefCount(ele);
12f9d551 5018 }
dd88747b 5019
5020 /* Send the element to the client as reply as well */
5021 addReplyBulk(c,ele);
5022
5023 /* Finally remove the element from the source list */
5024 listDelNode(srclist,ln);
3ea27d37 5025 if (listLength(srclist) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5026 server.dirty++;
12f9d551 5027 }
5028}
5029
ed9b544e 5030/* ==================================== Sets ================================ */
5031
5032static void saddCommand(redisClient *c) {
ed9b544e 5033 robj *set;
5034
3305306f 5035 set = lookupKeyWrite(c->db,c->argv[1]);
5036 if (set == NULL) {
ed9b544e 5037 set = createSetObject();
3305306f 5038 dictAdd(c->db->dict,c->argv[1],set);
ed9b544e 5039 incrRefCount(c->argv[1]);
5040 } else {
ed9b544e 5041 if (set->type != REDIS_SET) {
c937aa89 5042 addReply(c,shared.wrongtypeerr);
ed9b544e 5043 return;
5044 }
5045 }
5046 if (dictAdd(set->ptr,c->argv[2],NULL) == DICT_OK) {
5047 incrRefCount(c->argv[2]);
5048 server.dirty++;
c937aa89 5049 addReply(c,shared.cone);
ed9b544e 5050 } else {
c937aa89 5051 addReply(c,shared.czero);
ed9b544e 5052 }
5053}
5054
5055static void sremCommand(redisClient *c) {
3305306f 5056 robj *set;
ed9b544e 5057
dd88747b 5058 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5059 checkType(c,set,REDIS_SET)) return;
5060
5061 if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
5062 server.dirty++;
5063 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
3ea27d37 5064 if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5065 addReply(c,shared.cone);
ed9b544e 5066 } else {
dd88747b 5067 addReply(c,shared.czero);
ed9b544e 5068 }
5069}
5070
a4460ef4 5071static void smoveCommand(redisClient *c) {
5072 robj *srcset, *dstset;
5073
5074 srcset = lookupKeyWrite(c->db,c->argv[1]);
5075 dstset = lookupKeyWrite(c->db,c->argv[2]);
5076
5077 /* If the source key does not exist return 0, if it's of the wrong type
5078 * raise an error */
5079 if (srcset == NULL || srcset->type != REDIS_SET) {
5080 addReply(c, srcset ? shared.wrongtypeerr : shared.czero);
5081 return;
5082 }
5083 /* Error if the destination key is not a set as well */
5084 if (dstset && dstset->type != REDIS_SET) {
5085 addReply(c,shared.wrongtypeerr);
5086 return;
5087 }
5088 /* Remove the element from the source set */
5089 if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) {
5090 /* Key not found in the src set! return zero */
5091 addReply(c,shared.czero);
5092 return;
5093 }
3ea27d37 5094 if (dictSize((dict*)srcset->ptr) == 0 && srcset != dstset)
5095 deleteKey(c->db,c->argv[1]);
a4460ef4 5096 server.dirty++;
5097 /* Add the element to the destination set */
5098 if (!dstset) {
5099 dstset = createSetObject();
5100 dictAdd(c->db->dict,c->argv[2],dstset);
5101 incrRefCount(c->argv[2]);
5102 }
5103 if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK)
5104 incrRefCount(c->argv[3]);
5105 addReply(c,shared.cone);
5106}
5107
ed9b544e 5108static void sismemberCommand(redisClient *c) {
3305306f 5109 robj *set;
ed9b544e 5110
dd88747b 5111 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5112 checkType(c,set,REDIS_SET)) return;
5113
5114 if (dictFind(set->ptr,c->argv[2]))
5115 addReply(c,shared.cone);
5116 else
c937aa89 5117 addReply(c,shared.czero);
ed9b544e 5118}
5119
5120static void scardCommand(redisClient *c) {
3305306f 5121 robj *o;
ed9b544e 5122 dict *s;
dd88747b 5123
5124 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5125 checkType(c,o,REDIS_SET)) return;
e0a62c7f 5126
dd88747b 5127 s = o->ptr;
5128 addReplyUlong(c,dictSize(s));
ed9b544e 5129}
5130
12fea928 5131static void spopCommand(redisClient *c) {
5132 robj *set;
5133 dictEntry *de;
5134
dd88747b 5135 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5136 checkType(c,set,REDIS_SET)) return;
5137
5138 de = dictGetRandomKey(set->ptr);
5139 if (de == NULL) {
12fea928 5140 addReply(c,shared.nullbulk);
5141 } else {
dd88747b 5142 robj *ele = dictGetEntryKey(de);
12fea928 5143
dd88747b 5144 addReplyBulk(c,ele);
5145 dictDelete(set->ptr,ele);
5146 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
3ea27d37 5147 if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5148 server.dirty++;
12fea928 5149 }
5150}
5151
2abb95a9 5152static void srandmemberCommand(redisClient *c) {
5153 robj *set;
5154 dictEntry *de;
5155
dd88747b 5156 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5157 checkType(c,set,REDIS_SET)) return;
5158
5159 de = dictGetRandomKey(set->ptr);
5160 if (de == NULL) {
2abb95a9 5161 addReply(c,shared.nullbulk);
5162 } else {
dd88747b 5163 robj *ele = dictGetEntryKey(de);
2abb95a9 5164
dd88747b 5165 addReplyBulk(c,ele);
2abb95a9 5166 }
5167}
5168
ed9b544e 5169static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
5170 dict **d1 = (void*) s1, **d2 = (void*) s2;
5171
3305306f 5172 return dictSize(*d1)-dictSize(*d2);
ed9b544e 5173}
5174
682ac724 5175static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long setsnum, robj *dstkey) {
ed9b544e 5176 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5177 dictIterator *di;
5178 dictEntry *de;
5179 robj *lenobj = NULL, *dstset = NULL;
682ac724 5180 unsigned long j, cardinality = 0;
ed9b544e 5181
ed9b544e 5182 for (j = 0; j < setsnum; j++) {
5183 robj *setobj;
3305306f 5184
5185 setobj = dstkey ?
5186 lookupKeyWrite(c->db,setskeys[j]) :
5187 lookupKeyRead(c->db,setskeys[j]);
5188 if (!setobj) {
ed9b544e 5189 zfree(dv);
5faa6025 5190 if (dstkey) {
fdcaae84 5191 if (deleteKey(c->db,dstkey))
5192 server.dirty++;
0d36ded0 5193 addReply(c,shared.czero);
5faa6025 5194 } else {
4e27f268 5195 addReply(c,shared.emptymultibulk);
5faa6025 5196 }
ed9b544e 5197 return;
5198 }
ed9b544e 5199 if (setobj->type != REDIS_SET) {
5200 zfree(dv);
c937aa89 5201 addReply(c,shared.wrongtypeerr);
ed9b544e 5202 return;
5203 }
5204 dv[j] = setobj->ptr;
5205 }
5206 /* Sort sets from the smallest to largest, this will improve our
5207 * algorithm's performace */
5208 qsort(dv,setsnum,sizeof(dict*),qsortCompareSetsByCardinality);
5209
5210 /* The first thing we should output is the total number of elements...
5211 * since this is a multi-bulk write, but at this stage we don't know
5212 * the intersection set size, so we use a trick, append an empty object
5213 * to the output list and save the pointer to later modify it with the
5214 * right length */
5215 if (!dstkey) {
5216 lenobj = createObject(REDIS_STRING,NULL);
5217 addReply(c,lenobj);
5218 decrRefCount(lenobj);
5219 } else {
5220 /* If we have a target key where to store the resulting set
5221 * create this key with an empty set inside */
5222 dstset = createSetObject();
ed9b544e 5223 }
5224
5225 /* Iterate all the elements of the first (smallest) set, and test
5226 * the element against all the other sets, if at least one set does
5227 * not include the element it is discarded */
5228 di = dictGetIterator(dv[0]);
ed9b544e 5229
5230 while((de = dictNext(di)) != NULL) {
5231 robj *ele;
5232
5233 for (j = 1; j < setsnum; j++)
5234 if (dictFind(dv[j],dictGetEntryKey(de)) == NULL) break;
5235 if (j != setsnum)
5236 continue; /* at least one set does not contain the member */
5237 ele = dictGetEntryKey(de);
5238 if (!dstkey) {
dd88747b 5239 addReplyBulk(c,ele);
ed9b544e 5240 cardinality++;
5241 } else {
5242 dictAdd(dstset->ptr,ele,NULL);
5243 incrRefCount(ele);
5244 }
5245 }
5246 dictReleaseIterator(di);
5247
83cdfe18 5248 if (dstkey) {
3ea27d37 5249 /* Store the resulting set into the target, if the intersection
5250 * is not an empty set. */
83cdfe18 5251 deleteKey(c->db,dstkey);
3ea27d37 5252 if (dictSize((dict*)dstset->ptr) > 0) {
5253 dictAdd(c->db->dict,dstkey,dstset);
5254 incrRefCount(dstkey);
d36c4e97 5255 addReplyLong(c,dictSize((dict*)dstset->ptr));
3ea27d37 5256 } else {
5257 decrRefCount(dstset);
d36c4e97 5258 addReply(c,shared.czero);
3ea27d37 5259 }
40d224a9 5260 server.dirty++;
d36c4e97 5261 } else {
5262 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
40d224a9 5263 }
ed9b544e 5264 zfree(dv);
5265}
5266
5267static void sinterCommand(redisClient *c) {
5268 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
5269}
5270
5271static void sinterstoreCommand(redisClient *c) {
5272 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
5273}
5274
f4f56e1d 5275#define REDIS_OP_UNION 0
5276#define REDIS_OP_DIFF 1
2830ca53 5277#define REDIS_OP_INTER 2
f4f56e1d 5278
5279static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey, int op) {
40d224a9 5280 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5281 dictIterator *di;
5282 dictEntry *de;
f4f56e1d 5283 robj *dstset = NULL;
40d224a9 5284 int j, cardinality = 0;
5285
40d224a9 5286 for (j = 0; j < setsnum; j++) {
5287 robj *setobj;
5288
5289 setobj = dstkey ?
5290 lookupKeyWrite(c->db,setskeys[j]) :
5291 lookupKeyRead(c->db,setskeys[j]);
5292 if (!setobj) {
5293 dv[j] = NULL;
5294 continue;
5295 }
5296 if (setobj->type != REDIS_SET) {
5297 zfree(dv);
5298 addReply(c,shared.wrongtypeerr);
5299 return;
5300 }
5301 dv[j] = setobj->ptr;
5302 }
5303
5304 /* We need a temp set object to store our union. If the dstkey
5305 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
5306 * this set object will be the resulting object to set into the target key*/
5307 dstset = createSetObject();
5308
40d224a9 5309 /* Iterate all the elements of all the sets, add every element a single
5310 * time to the result set */
5311 for (j = 0; j < setsnum; j++) {
51829ed3 5312 if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */
40d224a9 5313 if (!dv[j]) continue; /* non existing keys are like empty sets */
5314
5315 di = dictGetIterator(dv[j]);
40d224a9 5316
5317 while((de = dictNext(di)) != NULL) {
5318 robj *ele;
5319
5320 /* dictAdd will not add the same element multiple times */
5321 ele = dictGetEntryKey(de);
f4f56e1d 5322 if (op == REDIS_OP_UNION || j == 0) {
5323 if (dictAdd(dstset->ptr,ele,NULL) == DICT_OK) {
5324 incrRefCount(ele);
40d224a9 5325 cardinality++;
5326 }
f4f56e1d 5327 } else if (op == REDIS_OP_DIFF) {
5328 if (dictDelete(dstset->ptr,ele) == DICT_OK) {
5329 cardinality--;
5330 }
40d224a9 5331 }
5332 }
5333 dictReleaseIterator(di);
51829ed3 5334
d36c4e97 5335 /* result set is empty? Exit asap. */
5336 if (op == REDIS_OP_DIFF && cardinality == 0) break;
40d224a9 5337 }
5338
f4f56e1d 5339 /* Output the content of the resulting set, if not in STORE mode */
5340 if (!dstkey) {
5341 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
5342 di = dictGetIterator(dstset->ptr);
f4f56e1d 5343 while((de = dictNext(di)) != NULL) {
5344 robj *ele;
5345
5346 ele = dictGetEntryKey(de);
dd88747b 5347 addReplyBulk(c,ele);
f4f56e1d 5348 }
5349 dictReleaseIterator(di);
d36c4e97 5350 decrRefCount(dstset);
83cdfe18
AG
5351 } else {
5352 /* If we have a target key where to store the resulting set
5353 * create this key with the result set inside */
5354 deleteKey(c->db,dstkey);
3ea27d37 5355 if (dictSize((dict*)dstset->ptr) > 0) {
5356 dictAdd(c->db->dict,dstkey,dstset);
5357 incrRefCount(dstkey);
d36c4e97 5358 addReplyLong(c,dictSize((dict*)dstset->ptr));
3ea27d37 5359 } else {
5360 decrRefCount(dstset);
d36c4e97 5361 addReply(c,shared.czero);
3ea27d37 5362 }
40d224a9 5363 server.dirty++;
5364 }
5365 zfree(dv);
5366}
5367
5368static void sunionCommand(redisClient *c) {
f4f56e1d 5369 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
40d224a9 5370}
5371
5372static void sunionstoreCommand(redisClient *c) {
f4f56e1d 5373 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
5374}
5375
5376static void sdiffCommand(redisClient *c) {
5377 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
5378}
5379
5380static void sdiffstoreCommand(redisClient *c) {
5381 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
40d224a9 5382}
5383
6b47e12e 5384/* ==================================== ZSets =============================== */
5385
5386/* ZSETs are ordered sets using two data structures to hold the same elements
5387 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
5388 * data structure.
5389 *
5390 * The elements are added to an hash table mapping Redis objects to scores.
5391 * At the same time the elements are added to a skip list mapping scores
5392 * to Redis objects (so objects are sorted by scores in this "view"). */
5393
5394/* This skiplist implementation is almost a C translation of the original
5395 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
5396 * Alternative to Balanced Trees", modified in three ways:
5397 * a) this implementation allows for repeated values.
5398 * b) the comparison is not just by key (our 'score') but by satellite data.
5399 * c) there is a back pointer, so it's a doubly linked list with the back
5400 * pointers being only at "level 1". This allows to traverse the list
5401 * from tail to head, useful for ZREVRANGE. */
5402
5403static zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
5404 zskiplistNode *zn = zmalloc(sizeof(*zn));
5405
5406 zn->forward = zmalloc(sizeof(zskiplistNode*) * level);
2b37892e
PN
5407 if (level > 0)
5408 zn->span = zmalloc(sizeof(unsigned int) * (level - 1));
6b47e12e 5409 zn->score = score;
5410 zn->obj = obj;
5411 return zn;
5412}
5413
5414static zskiplist *zslCreate(void) {
5415 int j;
5416 zskiplist *zsl;
e0a62c7f 5417
6b47e12e 5418 zsl = zmalloc(sizeof(*zsl));
5419 zsl->level = 1;
cc812361 5420 zsl->length = 0;
6b47e12e 5421 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);
69d95c3e 5422 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
6b47e12e 5423 zsl->header->forward[j] = NULL;
94e543b5 5424
5425 /* span has space for ZSKIPLIST_MAXLEVEL-1 elements */
5426 if (j < ZSKIPLIST_MAXLEVEL-1)
5427 zsl->header->span[j] = 0;
69d95c3e 5428 }
e3870fab 5429 zsl->header->backward = NULL;
5430 zsl->tail = NULL;
6b47e12e 5431 return zsl;
5432}
5433
fd8ccf44 5434static void zslFreeNode(zskiplistNode *node) {
5435 decrRefCount(node->obj);
ad807e6f 5436 zfree(node->forward);
69d95c3e 5437 zfree(node->span);
fd8ccf44 5438 zfree(node);
5439}
5440
5441static void zslFree(zskiplist *zsl) {
ad807e6f 5442 zskiplistNode *node = zsl->header->forward[0], *next;
fd8ccf44 5443
ad807e6f 5444 zfree(zsl->header->forward);
69d95c3e 5445 zfree(zsl->header->span);
ad807e6f 5446 zfree(zsl->header);
fd8ccf44 5447 while(node) {
599379dd 5448 next = node->forward[0];
fd8ccf44 5449 zslFreeNode(node);
5450 node = next;
5451 }
ad807e6f 5452 zfree(zsl);
fd8ccf44 5453}
5454
6b47e12e 5455static int zslRandomLevel(void) {
5456 int level = 1;
5457 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
5458 level += 1;
10c2baa5 5459 return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;
6b47e12e 5460}
5461
5462static void zslInsert(zskiplist *zsl, double score, robj *obj) {
5463 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
2b37892e 5464 unsigned int rank[ZSKIPLIST_MAXLEVEL];
6b47e12e 5465 int i, level;
5466
5467 x = zsl->header;
5468 for (i = zsl->level-1; i >= 0; i--) {
2b37892e
PN
5469 /* store rank that is crossed to reach the insert position */
5470 rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
69d95c3e 5471
9d60e6e4 5472 while (x->forward[i] &&
5473 (x->forward[i]->score < score ||
5474 (x->forward[i]->score == score &&
69d95c3e 5475 compareStringObjects(x->forward[i]->obj,obj) < 0))) {
a50ea45c 5476 rank[i] += i > 0 ? x->span[i-1] : 1;
6b47e12e 5477 x = x->forward[i];
69d95c3e 5478 }
6b47e12e 5479 update[i] = x;
5480 }
6b47e12e 5481 /* we assume the key is not already inside, since we allow duplicated
5482 * scores, and the re-insertion of score and redis object should never
5483 * happpen since the caller of zslInsert() should test in the hash table
5484 * if the element is already inside or not. */
5485 level = zslRandomLevel();
5486 if (level > zsl->level) {
69d95c3e 5487 for (i = zsl->level; i < level; i++) {
2b37892e 5488 rank[i] = 0;
6b47e12e 5489 update[i] = zsl->header;
2b37892e 5490 update[i]->span[i-1] = zsl->length;
69d95c3e 5491 }
6b47e12e 5492 zsl->level = level;
5493 }
5494 x = zslCreateNode(level,score,obj);
5495 for (i = 0; i < level; i++) {
5496 x->forward[i] = update[i]->forward[i];
5497 update[i]->forward[i] = x;
69d95c3e
PN
5498
5499 /* update span covered by update[i] as x is inserted here */
2b37892e
PN
5500 if (i > 0) {
5501 x->span[i-1] = update[i]->span[i-1] - (rank[0] - rank[i]);
5502 update[i]->span[i-1] = (rank[0] - rank[i]) + 1;
5503 }
6b47e12e 5504 }
69d95c3e
PN
5505
5506 /* increment span for untouched levels */
5507 for (i = level; i < zsl->level; i++) {
2b37892e 5508 update[i]->span[i-1]++;
69d95c3e
PN
5509 }
5510
bb975144 5511 x->backward = (update[0] == zsl->header) ? NULL : update[0];
e3870fab 5512 if (x->forward[0])
5513 x->forward[0]->backward = x;
5514 else
5515 zsl->tail = x;
cc812361 5516 zsl->length++;
6b47e12e 5517}
5518
84105336
PN
5519/* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
5520void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
5521 int i;
5522 for (i = 0; i < zsl->level; i++) {
5523 if (update[i]->forward[i] == x) {
5524 if (i > 0) {
5525 update[i]->span[i-1] += x->span[i-1] - 1;
5526 }
5527 update[i]->forward[i] = x->forward[i];
5528 } else {
5529 /* invariant: i > 0, because update[0]->forward[0]
5530 * is always equal to x */
5531 update[i]->span[i-1] -= 1;
5532 }
5533 }
5534 if (x->forward[0]) {
5535 x->forward[0]->backward = x->backward;
5536 } else {
5537 zsl->tail = x->backward;
5538 }
5539 while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
5540 zsl->level--;
5541 zsl->length--;
5542}
5543
50c55df5 5544/* Delete an element with matching score/object from the skiplist. */
fd8ccf44 5545static int zslDelete(zskiplist *zsl, double score, robj *obj) {
e197b441 5546 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5547 int i;
5548
5549 x = zsl->header;
5550 for (i = zsl->level-1; i >= 0; i--) {
9d60e6e4 5551 while (x->forward[i] &&
5552 (x->forward[i]->score < score ||
5553 (x->forward[i]->score == score &&
5554 compareStringObjects(x->forward[i]->obj,obj) < 0)))
e197b441 5555 x = x->forward[i];
5556 update[i] = x;
5557 }
5558 /* We may have multiple elements with the same score, what we need
5559 * is to find the element with both the right score and object. */
5560 x = x->forward[0];
bf028098 5561 if (x && score == x->score && equalStringObjects(x->obj,obj)) {
84105336 5562 zslDeleteNode(zsl, x, update);
9d60e6e4 5563 zslFreeNode(x);
9d60e6e4 5564 return 1;
5565 } else {
5566 return 0; /* not found */
e197b441 5567 }
5568 return 0; /* not found */
fd8ccf44 5569}
5570
1807985b 5571/* Delete all the elements with score between min and max from the skiplist.
5572 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
5573 * Note that this function takes the reference to the hash table view of the
5574 * sorted set, in order to remove the elements from the hash table too. */
f84d3933 5575static unsigned long zslDeleteRangeByScore(zskiplist *zsl, double min, double max, dict *dict) {
1807985b 5576 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5577 unsigned long removed = 0;
5578 int i;
5579
5580 x = zsl->header;
5581 for (i = zsl->level-1; i >= 0; i--) {
5582 while (x->forward[i] && x->forward[i]->score < min)
5583 x = x->forward[i];
5584 update[i] = x;
5585 }
5586 /* We may have multiple elements with the same score, what we need
5587 * is to find the element with both the right score and object. */
5588 x = x->forward[0];
5589 while (x && x->score <= max) {
84105336
PN
5590 zskiplistNode *next = x->forward[0];
5591 zslDeleteNode(zsl, x, update);
1807985b 5592 dictDelete(dict,x->obj);
5593 zslFreeNode(x);
1807985b 5594 removed++;
5595 x = next;
5596 }
5597 return removed; /* not found */
5598}
1807985b 5599
9212eafd 5600/* Delete all the elements with rank between start and end from the skiplist.
2424490f 5601 * Start and end are inclusive. Note that start and end need to be 1-based */
9212eafd
PN
5602static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) {
5603 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5604 unsigned long traversed = 0, removed = 0;
5605 int i;
5606
9212eafd
PN
5607 x = zsl->header;
5608 for (i = zsl->level-1; i >= 0; i--) {
5609 while (x->forward[i] && (traversed + (i > 0 ? x->span[i-1] : 1)) < start) {
5610 traversed += i > 0 ? x->span[i-1] : 1;
5611 x = x->forward[i];
1807985b 5612 }
9212eafd
PN
5613 update[i] = x;
5614 }
5615
5616 traversed++;
5617 x = x->forward[0];
5618 while (x && traversed <= end) {
84105336
PN
5619 zskiplistNode *next = x->forward[0];
5620 zslDeleteNode(zsl, x, update);
1807985b 5621 dictDelete(dict,x->obj);
5622 zslFreeNode(x);
1807985b 5623 removed++;
9212eafd 5624 traversed++;
1807985b 5625 x = next;
5626 }
9212eafd 5627 return removed;
1807985b 5628}
5629
50c55df5 5630/* Find the first node having a score equal or greater than the specified one.
5631 * Returns NULL if there is no match. */
5632static zskiplistNode *zslFirstWithScore(zskiplist *zsl, double score) {
5633 zskiplistNode *x;
5634 int i;
5635
5636 x = zsl->header;
5637 for (i = zsl->level-1; i >= 0; i--) {
5638 while (x->forward[i] && x->forward[i]->score < score)
5639 x = x->forward[i];
5640 }
5641 /* We may have multiple elements with the same score, what we need
5642 * is to find the element with both the right score and object. */
5643 return x->forward[0];
5644}
5645
27b0ccca
PN
5646/* Find the rank for an element by both score and key.
5647 * Returns 0 when the element cannot be found, rank otherwise.
5648 * Note that the rank is 1-based due to the span of zsl->header to the
5649 * first element. */
5650static unsigned long zslGetRank(zskiplist *zsl, double score, robj *o) {
5651 zskiplistNode *x;
5652 unsigned long rank = 0;
5653 int i;
5654
5655 x = zsl->header;
5656 for (i = zsl->level-1; i >= 0; i--) {
5657 while (x->forward[i] &&
5658 (x->forward[i]->score < score ||
5659 (x->forward[i]->score == score &&
5660 compareStringObjects(x->forward[i]->obj,o) <= 0))) {
a50ea45c 5661 rank += i > 0 ? x->span[i-1] : 1;
27b0ccca
PN
5662 x = x->forward[i];
5663 }
5664
5665 /* x might be equal to zsl->header, so test if obj is non-NULL */
bf028098 5666 if (x->obj && equalStringObjects(x->obj,o)) {
27b0ccca
PN
5667 return rank;
5668 }
5669 }
5670 return 0;
5671}
5672
e74825c2
PN
5673/* Finds an element by its rank. The rank argument needs to be 1-based. */
5674zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) {
5675 zskiplistNode *x;
5676 unsigned long traversed = 0;
5677 int i;
5678
5679 x = zsl->header;
5680 for (i = zsl->level-1; i >= 0; i--) {
dd88747b 5681 while (x->forward[i] && (traversed + (i>0 ? x->span[i-1] : 1)) <= rank)
5682 {
a50ea45c 5683 traversed += i > 0 ? x->span[i-1] : 1;
e74825c2
PN
5684 x = x->forward[i];
5685 }
e74825c2
PN
5686 if (traversed == rank) {
5687 return x;
5688 }
5689 }
5690 return NULL;
5691}
5692
fd8ccf44 5693/* The actual Z-commands implementations */
5694
7db723ad 5695/* This generic command implements both ZADD and ZINCRBY.
e2665397 5696 * scoreval is the score if the operation is a ZADD (doincrement == 0) or
7db723ad 5697 * the increment if the operation is a ZINCRBY (doincrement == 1). */
e2665397 5698static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scoreval, int doincrement) {
fd8ccf44 5699 robj *zsetobj;
5700 zset *zs;
5701 double *score;
5702
e2665397 5703 zsetobj = lookupKeyWrite(c->db,key);
fd8ccf44 5704 if (zsetobj == NULL) {
5705 zsetobj = createZsetObject();
e2665397 5706 dictAdd(c->db->dict,key,zsetobj);
5707 incrRefCount(key);
fd8ccf44 5708 } else {
5709 if (zsetobj->type != REDIS_ZSET) {
5710 addReply(c,shared.wrongtypeerr);
5711 return;
5712 }
5713 }
fd8ccf44 5714 zs = zsetobj->ptr;
e2665397 5715
7db723ad 5716 /* Ok now since we implement both ZADD and ZINCRBY here the code
e2665397 5717 * needs to handle the two different conditions. It's all about setting
5718 * '*score', that is, the new score to set, to the right value. */
5719 score = zmalloc(sizeof(double));
5720 if (doincrement) {
5721 dictEntry *de;
5722
5723 /* Read the old score. If the element was not present starts from 0 */
5724 de = dictFind(zs->dict,ele);
5725 if (de) {
5726 double *oldscore = dictGetEntryVal(de);
5727 *score = *oldscore + scoreval;
5728 } else {
5729 *score = scoreval;
5730 }
5731 } else {
5732 *score = scoreval;
5733 }
5734
5735 /* What follows is a simple remove and re-insert operation that is common
7db723ad 5736 * to both ZADD and ZINCRBY... */
e2665397 5737 if (dictAdd(zs->dict,ele,score) == DICT_OK) {
fd8ccf44 5738 /* case 1: New element */
e2665397 5739 incrRefCount(ele); /* added to hash */
5740 zslInsert(zs->zsl,*score,ele);
5741 incrRefCount(ele); /* added to skiplist */
fd8ccf44 5742 server.dirty++;
e2665397 5743 if (doincrement)
e2665397 5744 addReplyDouble(c,*score);
91d71bfc 5745 else
5746 addReply(c,shared.cone);
fd8ccf44 5747 } else {
5748 dictEntry *de;
5749 double *oldscore;
e0a62c7f 5750
fd8ccf44 5751 /* case 2: Score update operation */
e2665397 5752 de = dictFind(zs->dict,ele);
dfc5e96c 5753 redisAssert(de != NULL);
fd8ccf44 5754 oldscore = dictGetEntryVal(de);
5755 if (*score != *oldscore) {
5756 int deleted;
5757
e2665397 5758 /* Remove and insert the element in the skip list with new score */
5759 deleted = zslDelete(zs->zsl,*oldscore,ele);
dfc5e96c 5760 redisAssert(deleted != 0);
e2665397 5761 zslInsert(zs->zsl,*score,ele);
5762 incrRefCount(ele);
5763 /* Update the score in the hash table */
5764 dictReplace(zs->dict,ele,score);
fd8ccf44 5765 server.dirty++;
2161a965 5766 } else {
5767 zfree(score);
fd8ccf44 5768 }
e2665397 5769 if (doincrement)
5770 addReplyDouble(c,*score);
5771 else
5772 addReply(c,shared.czero);
fd8ccf44 5773 }
5774}
5775
e2665397 5776static void zaddCommand(redisClient *c) {
5777 double scoreval;
5778
bd79a6bd 5779 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
e2665397 5780 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,0);
5781}
5782
7db723ad 5783static void zincrbyCommand(redisClient *c) {
e2665397 5784 double scoreval;
5785
bd79a6bd 5786 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
e2665397 5787 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,1);
5788}
5789
1b7106e7 5790static void zremCommand(redisClient *c) {
5791 robj *zsetobj;
5792 zset *zs;
dd88747b 5793 dictEntry *de;
5794 double *oldscore;
5795 int deleted;
1b7106e7 5796
dd88747b 5797 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5798 checkType(c,zsetobj,REDIS_ZSET)) return;
1b7106e7 5799
dd88747b 5800 zs = zsetobj->ptr;
5801 de = dictFind(zs->dict,c->argv[2]);
5802 if (de == NULL) {
5803 addReply(c,shared.czero);
5804 return;
1b7106e7 5805 }
dd88747b 5806 /* Delete from the skiplist */
5807 oldscore = dictGetEntryVal(de);
5808 deleted = zslDelete(zs->zsl,*oldscore,c->argv[2]);
5809 redisAssert(deleted != 0);
5810
5811 /* Delete from the hash table */
5812 dictDelete(zs->dict,c->argv[2]);
5813 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
3ea27d37 5814 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5815 server.dirty++;
5816 addReply(c,shared.cone);
1b7106e7 5817}
5818
1807985b 5819static void zremrangebyscoreCommand(redisClient *c) {
bbe025e0
AM
5820 double min;
5821 double max;
dd88747b 5822 long deleted;
1807985b 5823 robj *zsetobj;
5824 zset *zs;
5825
bd79a6bd
PN
5826 if ((getDoubleFromObjectOrReply(c, c->argv[2], &min, NULL) != REDIS_OK) ||
5827 (getDoubleFromObjectOrReply(c, c->argv[3], &max, NULL) != REDIS_OK)) return;
bbe025e0 5828
dd88747b 5829 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5830 checkType(c,zsetobj,REDIS_ZSET)) return;
1807985b 5831
dd88747b 5832 zs = zsetobj->ptr;
5833 deleted = zslDeleteRangeByScore(zs->zsl,min,max,zs->dict);
5834 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
3ea27d37 5835 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5836 server.dirty += deleted;
5837 addReplyLong(c,deleted);
1807985b 5838}
5839
9212eafd 5840static void zremrangebyrankCommand(redisClient *c) {
bbe025e0
AM
5841 long start;
5842 long end;
dd88747b 5843 int llen;
5844 long deleted;
9212eafd
PN
5845 robj *zsetobj;
5846 zset *zs;
5847
bd79a6bd
PN
5848 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
5849 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
bbe025e0 5850
dd88747b 5851 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5852 checkType(c,zsetobj,REDIS_ZSET)) return;
5853 zs = zsetobj->ptr;
5854 llen = zs->zsl->length;
9212eafd 5855
dd88747b 5856 /* convert negative indexes */
5857 if (start < 0) start = llen+start;
5858 if (end < 0) end = llen+end;
5859 if (start < 0) start = 0;
5860 if (end < 0) end = 0;
9212eafd 5861
dd88747b 5862 /* indexes sanity checks */
5863 if (start > end || start >= llen) {
5864 addReply(c,shared.czero);
5865 return;
9212eafd 5866 }
dd88747b 5867 if (end >= llen) end = llen-1;
5868
5869 /* increment start and end because zsl*Rank functions
5870 * use 1-based rank */
5871 deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
5872 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
3ea27d37 5873 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
dd88747b 5874 server.dirty += deleted;
5875 addReplyLong(c, deleted);
9212eafd
PN
5876}
5877
8f92e768
PN
5878typedef struct {
5879 dict *dict;
5880 double weight;
5881} zsetopsrc;
5882
5883static int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) {
5884 zsetopsrc *d1 = (void*) s1, *d2 = (void*) s2;
5885 unsigned long size1, size2;
5886 size1 = d1->dict ? dictSize(d1->dict) : 0;
5887 size2 = d2->dict ? dictSize(d2->dict) : 0;
5888 return size1 - size2;
5889}
5890
d2764cd6
PN
5891#define REDIS_AGGR_SUM 1
5892#define REDIS_AGGR_MIN 2
5893#define REDIS_AGGR_MAX 3
5894
5895inline static void zunionInterAggregate(double *target, double val, int aggregate) {
5896 if (aggregate == REDIS_AGGR_SUM) {
5897 *target = *target + val;
5898 } else if (aggregate == REDIS_AGGR_MIN) {
5899 *target = val < *target ? val : *target;
5900 } else if (aggregate == REDIS_AGGR_MAX) {
5901 *target = val > *target ? val : *target;
5902 } else {
5903 /* safety net */
f83c6cb5 5904 redisPanic("Unknown ZUNION/INTER aggregate type");
d2764cd6
PN
5905 }
5906}
5907
2830ca53 5908static void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) {
8f92e768 5909 int i, j, zsetnum;
d2764cd6 5910 int aggregate = REDIS_AGGR_SUM;
8f92e768 5911 zsetopsrc *src;
2830ca53
PN
5912 robj *dstobj;
5913 zset *dstzset;
b287c9bb
PN
5914 dictIterator *di;
5915 dictEntry *de;
5916
2830ca53
PN
5917 /* expect zsetnum input keys to be given */
5918 zsetnum = atoi(c->argv[2]->ptr);
5919 if (zsetnum < 1) {
5920 addReplySds(c,sdsnew("-ERR at least 1 input key is needed for ZUNION/ZINTER\r\n"));
5921 return;
b287c9bb 5922 }
2830ca53
PN
5923
5924 /* test if the expected number of keys would overflow */
5925 if (3+zsetnum > c->argc) {
b287c9bb
PN
5926 addReply(c,shared.syntaxerr);
5927 return;
5928 }
5929
2830ca53 5930 /* read keys to be used for input */
b9eed483 5931 src = zmalloc(sizeof(zsetopsrc) * zsetnum);
2830ca53 5932 for (i = 0, j = 3; i < zsetnum; i++, j++) {
b287c9bb
PN
5933 robj *zsetobj = lookupKeyWrite(c->db,c->argv[j]);
5934 if (!zsetobj) {
8f92e768 5935 src[i].dict = NULL;
b287c9bb
PN
5936 } else {
5937 if (zsetobj->type != REDIS_ZSET) {
8f92e768 5938 zfree(src);
b287c9bb
PN
5939 addReply(c,shared.wrongtypeerr);
5940 return;
5941 }
8f92e768 5942 src[i].dict = ((zset*)zsetobj->ptr)->dict;
b287c9bb 5943 }
2830ca53
PN
5944
5945 /* default all weights to 1 */
8f92e768 5946 src[i].weight = 1.0;
b287c9bb
PN
5947 }
5948
2830ca53
PN
5949 /* parse optional extra arguments */
5950 if (j < c->argc) {
d2764cd6 5951 int remaining = c->argc - j;
b287c9bb 5952
2830ca53 5953 while (remaining) {
d2764cd6 5954 if (remaining >= (zsetnum + 1) && !strcasecmp(c->argv[j]->ptr,"weights")) {
2830ca53 5955 j++; remaining--;
2830ca53 5956 for (i = 0; i < zsetnum; i++, j++, remaining--) {
bd79a6bd 5957 if (getDoubleFromObjectOrReply(c, c->argv[j], &src[i].weight, NULL) != REDIS_OK)
bbe025e0 5958 return;
2830ca53 5959 }
d2764cd6
PN
5960 } else if (remaining >= 2 && !strcasecmp(c->argv[j]->ptr,"aggregate")) {
5961 j++; remaining--;
5962 if (!strcasecmp(c->argv[j]->ptr,"sum")) {
5963 aggregate = REDIS_AGGR_SUM;
5964 } else if (!strcasecmp(c->argv[j]->ptr,"min")) {
5965 aggregate = REDIS_AGGR_MIN;
5966 } else if (!strcasecmp(c->argv[j]->ptr,"max")) {
5967 aggregate = REDIS_AGGR_MAX;
5968 } else {
5969 zfree(src);
5970 addReply(c,shared.syntaxerr);
5971 return;
5972 }
5973 j++; remaining--;
2830ca53 5974 } else {
8f92e768 5975 zfree(src);
2830ca53
PN
5976 addReply(c,shared.syntaxerr);
5977 return;
5978 }
5979 }
5980 }
b287c9bb 5981
d2764cd6
PN
5982 /* sort sets from the smallest to largest, this will improve our
5983 * algorithm's performance */
5984 qsort(src,zsetnum,sizeof(zsetopsrc), qsortCompareZsetopsrcByCardinality);
5985
2830ca53
PN
5986 dstobj = createZsetObject();
5987 dstzset = dstobj->ptr;
5988
5989 if (op == REDIS_OP_INTER) {
8f92e768
PN
5990 /* skip going over all entries if the smallest zset is NULL or empty */
5991 if (src[0].dict && dictSize(src[0].dict) > 0) {
5992 /* precondition: as src[0].dict is non-empty and the zsets are ordered
5993 * from small to large, all src[i > 0].dict are non-empty too */
5994 di = dictGetIterator(src[0].dict);
2830ca53 5995 while((de = dictNext(di)) != NULL) {
d2764cd6
PN
5996 double *score = zmalloc(sizeof(double)), value;
5997 *score = src[0].weight * (*(double*)dictGetEntryVal(de));
2830ca53 5998
d2764cd6
PN
5999 for (j = 1; j < zsetnum; j++) {
6000 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
2830ca53 6001 if (other) {
d2764cd6
PN
6002 value = src[j].weight * (*(double*)dictGetEntryVal(other));
6003 zunionInterAggregate(score, value, aggregate);
2830ca53
PN
6004 } else {
6005 break;
6006 }
6007 }
b287c9bb 6008
2830ca53 6009 /* skip entry when not present in every source dict */
8f92e768 6010 if (j != zsetnum) {
2830ca53
PN
6011 zfree(score);
6012 } else {
6013 robj *o = dictGetEntryKey(de);
6014 dictAdd(dstzset->dict,o,score);
6015 incrRefCount(o); /* added to dictionary */
6016 zslInsert(dstzset->zsl,*score,o);
6017 incrRefCount(o); /* added to skiplist */
b287c9bb
PN
6018 }
6019 }
2830ca53
PN
6020 dictReleaseIterator(di);
6021 }
6022 } else if (op == REDIS_OP_UNION) {
6023 for (i = 0; i < zsetnum; i++) {
8f92e768 6024 if (!src[i].dict) continue;
2830ca53 6025
8f92e768 6026 di = dictGetIterator(src[i].dict);
2830ca53
PN
6027 while((de = dictNext(di)) != NULL) {
6028 /* skip key when already processed */
6029 if (dictFind(dstzset->dict,dictGetEntryKey(de)) != NULL) continue;
6030
d2764cd6
PN
6031 double *score = zmalloc(sizeof(double)), value;
6032 *score = src[i].weight * (*(double*)dictGetEntryVal(de));
2830ca53 6033
d2764cd6
PN
6034 /* because the zsets are sorted by size, its only possible
6035 * for sets at larger indices to hold this entry */
6036 for (j = (i+1); j < zsetnum; j++) {
6037 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
2830ca53 6038 if (other) {
d2764cd6
PN
6039 value = src[j].weight * (*(double*)dictGetEntryVal(other));
6040 zunionInterAggregate(score, value, aggregate);
2830ca53
PN
6041 }
6042 }
b287c9bb 6043
2830ca53
PN
6044 robj *o = dictGetEntryKey(de);
6045 dictAdd(dstzset->dict,o,score);
6046 incrRefCount(o); /* added to dictionary */
6047 zslInsert(dstzset->zsl,*score,o);
6048 incrRefCount(o); /* added to skiplist */
6049 }
6050 dictReleaseIterator(di);
b287c9bb 6051 }
2830ca53
PN
6052 } else {
6053 /* unknown operator */
6054 redisAssert(op == REDIS_OP_INTER || op == REDIS_OP_UNION);
b287c9bb
PN
6055 }
6056
6057 deleteKey(c->db,dstkey);
3ea27d37 6058 if (dstzset->zsl->length) {
6059 dictAdd(c->db->dict,dstkey,dstobj);
6060 incrRefCount(dstkey);
6061 addReplyLong(c, dstzset->zsl->length);
6062 server.dirty++;
6063 } else {
8bca8773 6064 decrRefCount(dstobj);
3ea27d37 6065 addReply(c, shared.czero);
6066 }
8f92e768 6067 zfree(src);
b287c9bb
PN
6068}
6069
2830ca53
PN
6070static void zunionCommand(redisClient *c) {
6071 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_UNION);
b287c9bb
PN
6072}
6073
2830ca53
PN
6074static void zinterCommand(redisClient *c) {
6075 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_INTER);
b287c9bb
PN
6076}
6077
e3870fab 6078static void zrangeGenericCommand(redisClient *c, int reverse) {
cc812361 6079 robj *o;
bbe025e0
AM
6080 long start;
6081 long end;
752da584 6082 int withscores = 0;
dd88747b 6083 int llen;
6084 int rangelen, j;
6085 zset *zsetobj;
6086 zskiplist *zsl;
6087 zskiplistNode *ln;
6088 robj *ele;
752da584 6089
bd79a6bd
PN
6090 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
6091 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
bbe025e0 6092
752da584 6093 if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) {
6094 withscores = 1;
6095 } else if (c->argc >= 5) {
6096 addReply(c,shared.syntaxerr);
6097 return;
6098 }
cc812361 6099
4e27f268 6100 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
6101 || checkType(c,o,REDIS_ZSET)) return;
dd88747b 6102 zsetobj = o->ptr;
6103 zsl = zsetobj->zsl;
6104 llen = zsl->length;
cc812361 6105
dd88747b 6106 /* convert negative indexes */
6107 if (start < 0) start = llen+start;
6108 if (end < 0) end = llen+end;
6109 if (start < 0) start = 0;
6110 if (end < 0) end = 0;
cc812361 6111
dd88747b 6112 /* indexes sanity checks */
6113 if (start > end || start >= llen) {
6114 /* Out of range start or start > end result in empty list */
6115 addReply(c,shared.emptymultibulk);
6116 return;
6117 }
6118 if (end >= llen) end = llen-1;
6119 rangelen = (end-start)+1;
cc812361 6120
dd88747b 6121 /* check if starting point is trivial, before searching
6122 * the element in log(N) time */
6123 if (reverse) {
6124 ln = start == 0 ? zsl->tail : zslGetElementByRank(zsl, llen-start);
6125 } else {
6126 ln = start == 0 ?
6127 zsl->header->forward[0] : zslGetElementByRank(zsl, start+1);
6128 }
cc812361 6129
dd88747b 6130 /* Return the result in form of a multi-bulk reply */
6131 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",
6132 withscores ? (rangelen*2) : rangelen));
6133 for (j = 0; j < rangelen; j++) {
6134 ele = ln->obj;
6135 addReplyBulk(c,ele);
6136 if (withscores)
6137 addReplyDouble(c,ln->score);
6138 ln = reverse ? ln->backward : ln->forward[0];
cc812361 6139 }
6140}
6141
e3870fab 6142static void zrangeCommand(redisClient *c) {
6143 zrangeGenericCommand(c,0);
6144}
6145
6146static void zrevrangeCommand(redisClient *c) {
6147 zrangeGenericCommand(c,1);
6148}
6149
f44dd428 6150/* This command implements both ZRANGEBYSCORE and ZCOUNT.
6151 * If justcount is non-zero, just the count is returned. */
6152static void genericZrangebyscoreCommand(redisClient *c, int justcount) {
50c55df5 6153 robj *o;
f44dd428 6154 double min, max;
6155 int minex = 0, maxex = 0; /* are min or max exclusive? */
80181f78 6156 int offset = 0, limit = -1;
0500ef27
SH
6157 int withscores = 0;
6158 int badsyntax = 0;
6159
f44dd428 6160 /* Parse the min-max interval. If one of the values is prefixed
6161 * by the "(" character, it's considered "open". For instance
6162 * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max
6163 * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */
6164 if (((char*)c->argv[2]->ptr)[0] == '(') {
6165 min = strtod((char*)c->argv[2]->ptr+1,NULL);
6166 minex = 1;
6167 } else {
6168 min = strtod(c->argv[2]->ptr,NULL);
6169 }
6170 if (((char*)c->argv[3]->ptr)[0] == '(') {
6171 max = strtod((char*)c->argv[3]->ptr+1,NULL);
6172 maxex = 1;
6173 } else {
6174 max = strtod(c->argv[3]->ptr,NULL);
6175 }
6176
6177 /* Parse "WITHSCORES": note that if the command was called with
6178 * the name ZCOUNT then we are sure that c->argc == 4, so we'll never
6179 * enter the following paths to parse WITHSCORES and LIMIT. */
0500ef27 6180 if (c->argc == 5 || c->argc == 8) {
3a3978b1 6181 if (strcasecmp(c->argv[c->argc-1]->ptr,"withscores") == 0)
6182 withscores = 1;
6183 else
6184 badsyntax = 1;
0500ef27 6185 }
3a3978b1 6186 if (c->argc != (4 + withscores) && c->argc != (7 + withscores))
0500ef27 6187 badsyntax = 1;
0500ef27 6188 if (badsyntax) {
454d4e43 6189 addReplySds(c,
6190 sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
80181f78 6191 return;
0500ef27
SH
6192 }
6193
f44dd428 6194 /* Parse "LIMIT" */
0500ef27 6195 if (c->argc == (7 + withscores) && strcasecmp(c->argv[4]->ptr,"limit")) {
80181f78 6196 addReply(c,shared.syntaxerr);
6197 return;
0500ef27 6198 } else if (c->argc == (7 + withscores)) {
80181f78 6199 offset = atoi(c->argv[5]->ptr);
6200 limit = atoi(c->argv[6]->ptr);
0b13687c 6201 if (offset < 0) offset = 0;
80181f78 6202 }
50c55df5 6203
f44dd428 6204 /* Ok, lookup the key and get the range */
50c55df5 6205 o = lookupKeyRead(c->db,c->argv[1]);
6206 if (o == NULL) {
4e27f268 6207 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
50c55df5 6208 } else {
6209 if (o->type != REDIS_ZSET) {
6210 addReply(c,shared.wrongtypeerr);
6211 } else {
6212 zset *zsetobj = o->ptr;
6213 zskiplist *zsl = zsetobj->zsl;
6214 zskiplistNode *ln;
f44dd428 6215 robj *ele, *lenobj = NULL;
6216 unsigned long rangelen = 0;
50c55df5 6217
f44dd428 6218 /* Get the first node with the score >= min, or with
6219 * score > min if 'minex' is true. */
50c55df5 6220 ln = zslFirstWithScore(zsl,min);
f44dd428 6221 while (minex && ln && ln->score == min) ln = ln->forward[0];
6222
50c55df5 6223 if (ln == NULL) {
6224 /* No element matching the speciifed interval */
f44dd428 6225 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
50c55df5 6226 return;
6227 }
6228
6229 /* We don't know in advance how many matching elements there
6230 * are in the list, so we push this object that will represent
6231 * the multi-bulk length in the output buffer, and will "fix"
6232 * it later */
f44dd428 6233 if (!justcount) {
6234 lenobj = createObject(REDIS_STRING,NULL);
6235 addReply(c,lenobj);
6236 decrRefCount(lenobj);
6237 }
50c55df5 6238
f44dd428 6239 while(ln && (maxex ? (ln->score < max) : (ln->score <= max))) {
80181f78 6240 if (offset) {
6241 offset--;
6242 ln = ln->forward[0];
6243 continue;
6244 }
6245 if (limit == 0) break;
f44dd428 6246 if (!justcount) {
6247 ele = ln->obj;
dd88747b 6248 addReplyBulk(c,ele);
f44dd428 6249 if (withscores)
6250 addReplyDouble(c,ln->score);
6251 }
50c55df5 6252 ln = ln->forward[0];
6253 rangelen++;
80181f78 6254 if (limit > 0) limit--;
50c55df5 6255 }
f44dd428 6256 if (justcount) {
6257 addReplyLong(c,(long)rangelen);
6258 } else {
6259 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",
6260 withscores ? (rangelen*2) : rangelen);
6261 }
50c55df5 6262 }
6263 }
6264}
6265
f44dd428 6266static void zrangebyscoreCommand(redisClient *c) {
6267 genericZrangebyscoreCommand(c,0);
6268}
6269
6270static void zcountCommand(redisClient *c) {
6271 genericZrangebyscoreCommand(c,1);
6272}
6273
3c41331e 6274static void zcardCommand(redisClient *c) {
e197b441 6275 robj *o;
6276 zset *zs;
dd88747b 6277
6278 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6279 checkType(c,o,REDIS_ZSET)) return;
6280
6281 zs = o->ptr;
6282 addReplyUlong(c,zs->zsl->length);
e197b441 6283}
6284
6e333bbe 6285static void zscoreCommand(redisClient *c) {
6286 robj *o;
6287 zset *zs;
dd88747b 6288 dictEntry *de;
6289
6290 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6291 checkType(c,o,REDIS_ZSET)) return;
6292
6293 zs = o->ptr;
6294 de = dictFind(zs->dict,c->argv[2]);
6295 if (!de) {
96d8b4ee 6296 addReply(c,shared.nullbulk);
6e333bbe 6297 } else {
dd88747b 6298 double *score = dictGetEntryVal(de);
6e333bbe 6299
dd88747b 6300 addReplyDouble(c,*score);
6e333bbe 6301 }
6302}
6303
798d9e55 6304static void zrankGenericCommand(redisClient *c, int reverse) {
69d95c3e 6305 robj *o;
dd88747b 6306 zset *zs;
6307 zskiplist *zsl;
6308 dictEntry *de;
6309 unsigned long rank;
6310 double *score;
6311
6312 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6313 checkType(c,o,REDIS_ZSET)) return;
6314
6315 zs = o->ptr;
6316 zsl = zs->zsl;
6317 de = dictFind(zs->dict,c->argv[2]);
6318 if (!de) {
69d95c3e
PN
6319 addReply(c,shared.nullbulk);
6320 return;
6321 }
69d95c3e 6322
dd88747b 6323 score = dictGetEntryVal(de);
6324 rank = zslGetRank(zsl, *score, c->argv[2]);
6325 if (rank) {
6326 if (reverse) {
6327 addReplyLong(c, zsl->length - rank);
27b0ccca 6328 } else {
dd88747b 6329 addReplyLong(c, rank-1);
69d95c3e 6330 }
dd88747b 6331 } else {
6332 addReply(c,shared.nullbulk);
978c2c94 6333 }
6334}
6335
798d9e55
PN
6336static void zrankCommand(redisClient *c) {
6337 zrankGenericCommand(c, 0);
6338}
6339
6340static void zrevrankCommand(redisClient *c) {
6341 zrankGenericCommand(c, 1);
6342}
6343
7fb16bac
PN
6344/* ========================= Hashes utility functions ======================= */
6345#define REDIS_HASH_KEY 1
6346#define REDIS_HASH_VALUE 2
978c2c94 6347
7fb16bac
PN
6348/* Check the length of a number of objects to see if we need to convert a
6349 * zipmap to a real hash. Note that we only check string encoded objects
6350 * as their string length can be queried in constant time. */
6351static void hashTryConversion(robj *subject, robj **argv, int start, int end) {
6352 int i;
6353 if (subject->encoding != REDIS_ENCODING_ZIPMAP) return;
978c2c94 6354
7fb16bac
PN
6355 for (i = start; i <= end; i++) {
6356 if (argv[i]->encoding == REDIS_ENCODING_RAW &&
6357 sdslen(argv[i]->ptr) > server.hash_max_zipmap_value)
6358 {
6359 convertToRealHash(subject);
978c2c94 6360 return;
6361 }
6362 }
7fb16bac 6363}
bae2c7ec 6364
97224de7
PN
6365/* Encode given objects in-place when the hash uses a dict. */
6366static void hashTryObjectEncoding(robj *subject, robj **o1, robj **o2) {
6367 if (subject->encoding == REDIS_ENCODING_HT) {
3f973463
PN
6368 if (o1) *o1 = tryObjectEncoding(*o1);
6369 if (o2) *o2 = tryObjectEncoding(*o2);
97224de7
PN
6370 }
6371}
6372
7fb16bac 6373/* Get the value from a hash identified by key. Returns either a string
a3f3af86
PN
6374 * object or NULL if the value cannot be found. The refcount of the object
6375 * is always increased by 1 when the value was found. */
7fb16bac
PN
6376static robj *hashGet(robj *o, robj *key) {
6377 robj *value = NULL;
978c2c94 6378 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
7fb16bac
PN
6379 unsigned char *v;
6380 unsigned int vlen;
6381 key = getDecodedObject(key);
6382 if (zipmapGet(o->ptr,key->ptr,sdslen(key->ptr),&v,&vlen)) {
6383 value = createStringObject((char*)v,vlen);
6384 }
6385 decrRefCount(key);
6386 } else {
6387 dictEntry *de = dictFind(o->ptr,key);
6388 if (de != NULL) {
6389 value = dictGetEntryVal(de);
a3f3af86 6390 incrRefCount(value);
7fb16bac
PN
6391 }
6392 }
6393 return value;
6394}
978c2c94 6395
7fb16bac
PN
6396/* Test if the key exists in the given hash. Returns 1 if the key
6397 * exists and 0 when it doesn't. */
6398static int hashExists(robj *o, robj *key) {
6399 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6400 key = getDecodedObject(key);
6401 if (zipmapExists(o->ptr,key->ptr,sdslen(key->ptr))) {
6402 decrRefCount(key);
6403 return 1;
6404 }
6405 decrRefCount(key);
6406 } else {
6407 if (dictFind(o->ptr,key) != NULL) {
6408 return 1;
6409 }
6410 }
6411 return 0;
6412}
bae2c7ec 6413
7fb16bac
PN
6414/* Add an element, discard the old if the key already exists.
6415 * Return 0 on insert and 1 on update. */
feb8d7e6 6416static int hashSet(robj *o, robj *key, robj *value) {
7fb16bac
PN
6417 int update = 0;
6418 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6419 key = getDecodedObject(key);
6420 value = getDecodedObject(value);
6421 o->ptr = zipmapSet(o->ptr,
6422 key->ptr,sdslen(key->ptr),
6423 value->ptr,sdslen(value->ptr), &update);
6424 decrRefCount(key);
6425 decrRefCount(value);
6426
6427 /* Check if the zipmap needs to be upgraded to a real hash table */
6428 if (zipmapLen(o->ptr) > server.hash_max_zipmap_entries)
bae2c7ec 6429 convertToRealHash(o);
978c2c94 6430 } else {
7fb16bac
PN
6431 if (dictReplace(o->ptr,key,value)) {
6432 /* Insert */
6433 incrRefCount(key);
978c2c94 6434 } else {
7fb16bac 6435 /* Update */
978c2c94 6436 update = 1;
6437 }
7fb16bac 6438 incrRefCount(value);
978c2c94 6439 }
7fb16bac 6440 return update;
978c2c94 6441}
6442
7fb16bac
PN
6443/* Delete an element from a hash.
6444 * Return 1 on deleted and 0 on not found. */
6445static int hashDelete(robj *o, robj *key) {
6446 int deleted = 0;
6447 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6448 key = getDecodedObject(key);
6449 o->ptr = zipmapDel(o->ptr,key->ptr,sdslen(key->ptr), &deleted);
6450 decrRefCount(key);
6451 } else {
6452 deleted = dictDelete((dict*)o->ptr,key) == DICT_OK;
6453 /* Always check if the dictionary needs a resize after a delete. */
6454 if (deleted && htNeedsResize(o->ptr)) dictResize(o->ptr);
d33278d1 6455 }
7fb16bac
PN
6456 return deleted;
6457}
d33278d1 6458
7fb16bac 6459/* Return the number of elements in a hash. */
c811bb38 6460static unsigned long hashLength(robj *o) {
7fb16bac
PN
6461 return (o->encoding == REDIS_ENCODING_ZIPMAP) ?
6462 zipmapLen((unsigned char*)o->ptr) : dictSize((dict*)o->ptr);
6463}
6464
6465/* Structure to hold hash iteration abstration. Note that iteration over
6466 * hashes involves both fields and values. Because it is possible that
6467 * not both are required, store pointers in the iterator to avoid
6468 * unnecessary memory allocation for fields/values. */
6469typedef struct {
6470 int encoding;
6471 unsigned char *zi;
6472 unsigned char *zk, *zv;
6473 unsigned int zklen, zvlen;
6474
6475 dictIterator *di;
6476 dictEntry *de;
6477} hashIterator;
6478
c44d3b56
PN
6479static hashIterator *hashInitIterator(robj *subject) {
6480 hashIterator *hi = zmalloc(sizeof(hashIterator));
7fb16bac
PN
6481 hi->encoding = subject->encoding;
6482 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6483 hi->zi = zipmapRewind(subject->ptr);
6484 } else if (hi->encoding == REDIS_ENCODING_HT) {
6485 hi->di = dictGetIterator(subject->ptr);
d33278d1 6486 } else {
7fb16bac 6487 redisAssert(NULL);
d33278d1 6488 }
c44d3b56 6489 return hi;
7fb16bac 6490}
d33278d1 6491
7fb16bac
PN
6492static void hashReleaseIterator(hashIterator *hi) {
6493 if (hi->encoding == REDIS_ENCODING_HT) {
6494 dictReleaseIterator(hi->di);
d33278d1 6495 }
c44d3b56 6496 zfree(hi);
7fb16bac 6497}
d33278d1 6498
7fb16bac
PN
6499/* Move to the next entry in the hash. Return REDIS_OK when the next entry
6500 * could be found and REDIS_ERR when the iterator reaches the end. */
c811bb38 6501static int hashNext(hashIterator *hi) {
7fb16bac
PN
6502 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6503 if ((hi->zi = zipmapNext(hi->zi, &hi->zk, &hi->zklen,
6504 &hi->zv, &hi->zvlen)) == NULL) return REDIS_ERR;
6505 } else {
6506 if ((hi->de = dictNext(hi->di)) == NULL) return REDIS_ERR;
6507 }
6508 return REDIS_OK;
6509}
d33278d1 6510
0c390abc 6511/* Get key or value object at current iteration position.
a3f3af86 6512 * This increases the refcount of the field object by 1. */
c811bb38 6513static robj *hashCurrent(hashIterator *hi, int what) {
7fb16bac
PN
6514 robj *o;
6515 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6516 if (what & REDIS_HASH_KEY) {
6517 o = createStringObject((char*)hi->zk,hi->zklen);
6518 } else {
6519 o = createStringObject((char*)hi->zv,hi->zvlen);
d33278d1 6520 }
d33278d1 6521 } else {
7fb16bac
PN
6522 if (what & REDIS_HASH_KEY) {
6523 o = dictGetEntryKey(hi->de);
6524 } else {
6525 o = dictGetEntryVal(hi->de);
d33278d1 6526 }
a3f3af86 6527 incrRefCount(o);
d33278d1 6528 }
7fb16bac 6529 return o;
d33278d1
PN
6530}
6531
7fb16bac
PN
6532static robj *hashLookupWriteOrCreate(redisClient *c, robj *key) {
6533 robj *o = lookupKeyWrite(c->db,key);
01426b05
PN
6534 if (o == NULL) {
6535 o = createHashObject();
7fb16bac
PN
6536 dictAdd(c->db->dict,key,o);
6537 incrRefCount(key);
01426b05
PN
6538 } else {
6539 if (o->type != REDIS_HASH) {
6540 addReply(c,shared.wrongtypeerr);
7fb16bac 6541 return NULL;
01426b05
PN
6542 }
6543 }
7fb16bac
PN
6544 return o;
6545}
01426b05 6546
7fb16bac
PN
6547/* ============================= Hash commands ============================== */
6548static void hsetCommand(redisClient *c) {
6e9e463f 6549 int update;
7fb16bac 6550 robj *o;
bbe025e0 6551
7fb16bac
PN
6552 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6553 hashTryConversion(o,c->argv,2,3);
97224de7 6554 hashTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
feb8d7e6 6555 update = hashSet(o,c->argv[2],c->argv[3]);
6e9e463f 6556 addReply(c, update ? shared.czero : shared.cone);
7fb16bac
PN
6557 server.dirty++;
6558}
01426b05 6559
1f1c7695
PN
6560static void hsetnxCommand(redisClient *c) {
6561 robj *o;
6562 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6563 hashTryConversion(o,c->argv,2,3);
6564
6565 if (hashExists(o, c->argv[2])) {
6566 addReply(c, shared.czero);
01426b05 6567 } else {
97224de7 6568 hashTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
feb8d7e6 6569 hashSet(o,c->argv[2],c->argv[3]);
1f1c7695
PN
6570 addReply(c, shared.cone);
6571 server.dirty++;
6572 }
6573}
01426b05 6574
7fb16bac
PN
6575static void hmsetCommand(redisClient *c) {
6576 int i;
6577 robj *o;
01426b05 6578
7fb16bac
PN
6579 if ((c->argc % 2) == 1) {
6580 addReplySds(c,sdsnew("-ERR wrong number of arguments for HMSET\r\n"));
6581 return;
6582 }
01426b05 6583
7fb16bac
PN
6584 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6585 hashTryConversion(o,c->argv,2,c->argc-1);
6586 for (i = 2; i < c->argc; i += 2) {
97224de7 6587 hashTryObjectEncoding(o,&c->argv[i], &c->argv[i+1]);
feb8d7e6 6588 hashSet(o,c->argv[i],c->argv[i+1]);
7fb16bac
PN
6589 }
6590 addReply(c, shared.ok);
edc2f63a 6591 server.dirty++;
7fb16bac
PN
6592}
6593
6594static void hincrbyCommand(redisClient *c) {
6595 long long value, incr;
6596 robj *o, *current, *new;
6597
bd79a6bd 6598 if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return;
7fb16bac
PN
6599 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6600 if ((current = hashGet(o,c->argv[2])) != NULL) {
946342c1
PN
6601 if (getLongLongFromObjectOrReply(c,current,&value,
6602 "hash value is not an integer") != REDIS_OK) {
6603 decrRefCount(current);
6604 return;
6605 }
a3f3af86 6606 decrRefCount(current);
7fb16bac
PN
6607 } else {
6608 value = 0;
01426b05
PN
6609 }
6610
7fb16bac 6611 value += incr;
3f973463
PN
6612 new = createStringObjectFromLongLong(value);
6613 hashTryObjectEncoding(o,&c->argv[2],NULL);
feb8d7e6 6614 hashSet(o,c->argv[2],new);
7fb16bac
PN
6615 decrRefCount(new);
6616 addReplyLongLong(c,value);
01426b05 6617 server.dirty++;
01426b05
PN
6618}
6619
978c2c94 6620static void hgetCommand(redisClient *c) {
7fb16bac 6621 robj *o, *value;
dd88747b 6622 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6623 checkType(c,o,REDIS_HASH)) return;
6624
7fb16bac
PN
6625 if ((value = hashGet(o,c->argv[2])) != NULL) {
6626 addReplyBulk(c,value);
a3f3af86 6627 decrRefCount(value);
dd88747b 6628 } else {
7fb16bac 6629 addReply(c,shared.nullbulk);
69d95c3e 6630 }
69d95c3e
PN
6631}
6632
09aeb579
PN
6633static void hmgetCommand(redisClient *c) {
6634 int i;
7fb16bac
PN
6635 robj *o, *value;
6636 o = lookupKeyRead(c->db,c->argv[1]);
6637 if (o != NULL && o->type != REDIS_HASH) {
6638 addReply(c,shared.wrongtypeerr);
09aeb579
PN
6639 }
6640
7fb16bac
PN
6641 /* Note the check for o != NULL happens inside the loop. This is
6642 * done because objects that cannot be found are considered to be
6643 * an empty hash. The reply should then be a series of NULLs. */
09aeb579 6644 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-2));
7fb16bac
PN
6645 for (i = 2; i < c->argc; i++) {
6646 if (o != NULL && (value = hashGet(o,c->argv[i])) != NULL) {
6647 addReplyBulk(c,value);
a3f3af86 6648 decrRefCount(value);
7fb16bac
PN
6649 } else {
6650 addReply(c,shared.nullbulk);
09aeb579
PN
6651 }
6652 }
6653}
6654
07efaf74 6655static void hdelCommand(redisClient *c) {
dd88747b 6656 robj *o;
dd88747b 6657 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
6658 checkType(c,o,REDIS_HASH)) return;
07efaf74 6659
7fb16bac
PN
6660 if (hashDelete(o,c->argv[2])) {
6661 if (hashLength(o) == 0) deleteKey(c->db,c->argv[1]);
6662 addReply(c,shared.cone);
6663 server.dirty++;
dd88747b 6664 } else {
7fb16bac 6665 addReply(c,shared.czero);
07efaf74 6666 }
6667}
6668
92b27fe9 6669static void hlenCommand(redisClient *c) {
6670 robj *o;
dd88747b 6671 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
92b27fe9 6672 checkType(c,o,REDIS_HASH)) return;
6673
7fb16bac 6674 addReplyUlong(c,hashLength(o));
92b27fe9 6675}
6676
78409a0f 6677static void genericHgetallCommand(redisClient *c, int flags) {
7fb16bac 6678 robj *o, *lenobj, *obj;
78409a0f 6679 unsigned long count = 0;
c44d3b56 6680 hashIterator *hi;
78409a0f 6681
4e27f268 6682 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
78409a0f 6683 || checkType(c,o,REDIS_HASH)) return;
6684
6685 lenobj = createObject(REDIS_STRING,NULL);
6686 addReply(c,lenobj);
6687 decrRefCount(lenobj);
6688
c44d3b56
PN
6689 hi = hashInitIterator(o);
6690 while (hashNext(hi) != REDIS_ERR) {
7fb16bac 6691 if (flags & REDIS_HASH_KEY) {
c44d3b56 6692 obj = hashCurrent(hi,REDIS_HASH_KEY);
7fb16bac 6693 addReplyBulk(c,obj);
a3f3af86 6694 decrRefCount(obj);
7fb16bac 6695 count++;
78409a0f 6696 }
7fb16bac 6697 if (flags & REDIS_HASH_VALUE) {
c44d3b56 6698 obj = hashCurrent(hi,REDIS_HASH_VALUE);
7fb16bac 6699 addReplyBulk(c,obj);
a3f3af86 6700 decrRefCount(obj);
7fb16bac 6701 count++;
78409a0f 6702 }
78409a0f 6703 }
c44d3b56 6704 hashReleaseIterator(hi);
7fb16bac 6705
78409a0f 6706 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",count);
6707}
6708
6709static void hkeysCommand(redisClient *c) {
7fb16bac 6710 genericHgetallCommand(c,REDIS_HASH_KEY);
78409a0f 6711}
6712
6713static void hvalsCommand(redisClient *c) {
7fb16bac 6714 genericHgetallCommand(c,REDIS_HASH_VALUE);
78409a0f 6715}
6716
6717static void hgetallCommand(redisClient *c) {
7fb16bac 6718 genericHgetallCommand(c,REDIS_HASH_KEY|REDIS_HASH_VALUE);
78409a0f 6719}
6720
a86f14b1 6721static void hexistsCommand(redisClient *c) {
6722 robj *o;
a86f14b1 6723 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6724 checkType(c,o,REDIS_HASH)) return;
6725
7fb16bac 6726 addReply(c, hashExists(o,c->argv[2]) ? shared.cone : shared.czero);
a86f14b1 6727}
6728
ada386b2 6729static void convertToRealHash(robj *o) {
6730 unsigned char *key, *val, *p, *zm = o->ptr;
6731 unsigned int klen, vlen;
6732 dict *dict = dictCreate(&hashDictType,NULL);
6733
6734 assert(o->type == REDIS_HASH && o->encoding != REDIS_ENCODING_HT);
6735 p = zipmapRewind(zm);
6736 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
6737 robj *keyobj, *valobj;
6738
6739 keyobj = createStringObject((char*)key,klen);
6740 valobj = createStringObject((char*)val,vlen);
05df7621 6741 keyobj = tryObjectEncoding(keyobj);
6742 valobj = tryObjectEncoding(valobj);
ada386b2 6743 dictAdd(dict,keyobj,valobj);
6744 }
6745 o->encoding = REDIS_ENCODING_HT;
6746 o->ptr = dict;
6747 zfree(zm);
6748}
6749
6b47e12e 6750/* ========================= Non type-specific commands ==================== */
6751
ed9b544e 6752static void flushdbCommand(redisClient *c) {
ca37e9cd 6753 server.dirty += dictSize(c->db->dict);
3305306f 6754 dictEmpty(c->db->dict);
6755 dictEmpty(c->db->expires);
ed9b544e 6756 addReply(c,shared.ok);
ed9b544e 6757}
6758
6759static void flushallCommand(redisClient *c) {
ca37e9cd 6760 server.dirty += emptyDb();
ed9b544e 6761 addReply(c,shared.ok);
500ece7c 6762 if (server.bgsavechildpid != -1) {
6763 kill(server.bgsavechildpid,SIGKILL);
6764 rdbRemoveTempFile(server.bgsavechildpid);
6765 }
f78fd11b 6766 rdbSave(server.dbfilename);
ca37e9cd 6767 server.dirty++;
ed9b544e 6768}
6769
56906eef 6770static redisSortOperation *createSortOperation(int type, robj *pattern) {
ed9b544e 6771 redisSortOperation *so = zmalloc(sizeof(*so));
ed9b544e 6772 so->type = type;
6773 so->pattern = pattern;
6774 return so;
6775}
6776
6777/* Return the value associated to the key with a name obtained
55017f9d
PN
6778 * substituting the first occurence of '*' in 'pattern' with 'subst'.
6779 * The returned object will always have its refcount increased by 1
6780 * when it is non-NULL. */
56906eef 6781static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
6d7d1370 6782 char *p, *f;
ed9b544e 6783 sds spat, ssub;
6d7d1370
PN
6784 robj keyobj, fieldobj, *o;
6785 int prefixlen, sublen, postfixlen, fieldlen;
ed9b544e 6786 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
6787 struct {
f1017b3f 6788 long len;
6789 long free;
ed9b544e 6790 char buf[REDIS_SORTKEY_MAX+1];
6d7d1370 6791 } keyname, fieldname;
ed9b544e 6792
28173a49 6793 /* If the pattern is "#" return the substitution object itself in order
6794 * to implement the "SORT ... GET #" feature. */
6795 spat = pattern->ptr;
6796 if (spat[0] == '#' && spat[1] == '\0') {
55017f9d 6797 incrRefCount(subst);
28173a49 6798 return subst;
6799 }
6800
6801 /* The substitution object may be specially encoded. If so we create
9d65a1bb 6802 * a decoded object on the fly. Otherwise getDecodedObject will just
6803 * increment the ref count, that we'll decrement later. */
6804 subst = getDecodedObject(subst);
942a3961 6805
ed9b544e 6806 ssub = subst->ptr;
6807 if (sdslen(spat)+sdslen(ssub)-1 > REDIS_SORTKEY_MAX) return NULL;
6808 p = strchr(spat,'*');
ed5a857a 6809 if (!p) {
6810 decrRefCount(subst);
6811 return NULL;
6812 }
ed9b544e 6813
6d7d1370
PN
6814 /* Find out if we're dealing with a hash dereference. */
6815 if ((f = strstr(p+1, "->")) != NULL) {
6816 fieldlen = sdslen(spat)-(f-spat);
6817 /* this also copies \0 character */
6818 memcpy(fieldname.buf,f+2,fieldlen-1);
6819 fieldname.len = fieldlen-2;
6820 } else {
6821 fieldlen = 0;
6822 }
6823
ed9b544e 6824 prefixlen = p-spat;
6825 sublen = sdslen(ssub);
6d7d1370 6826 postfixlen = sdslen(spat)-(prefixlen+1)-fieldlen;
ed9b544e 6827 memcpy(keyname.buf,spat,prefixlen);
6828 memcpy(keyname.buf+prefixlen,ssub,sublen);
6829 memcpy(keyname.buf+prefixlen+sublen,p+1,postfixlen);
6830 keyname.buf[prefixlen+sublen+postfixlen] = '\0';
6831 keyname.len = prefixlen+sublen+postfixlen;
942a3961 6832 decrRefCount(subst);
6833
6d7d1370
PN
6834 /* Lookup substituted key */
6835 initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2));
6836 o = lookupKeyRead(db,&keyobj);
55017f9d
PN
6837 if (o == NULL) return NULL;
6838
6839 if (fieldlen > 0) {
6840 if (o->type != REDIS_HASH || fieldname.len < 1) return NULL;
6d7d1370 6841
705dad38
PN
6842 /* Retrieve value from hash by the field name. This operation
6843 * already increases the refcount of the returned object. */
6d7d1370
PN
6844 initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(long)*2));
6845 o = hashGet(o, &fieldobj);
705dad38 6846 } else {
55017f9d 6847 if (o->type != REDIS_STRING) return NULL;
b6f07345 6848
705dad38
PN
6849 /* Every object that this function returns needs to have its refcount
6850 * increased. sortCommand decreases it again. */
6851 incrRefCount(o);
6d7d1370
PN
6852 }
6853
6854 return o;
ed9b544e 6855}
6856
6857/* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
6858 * the additional parameter is not standard but a BSD-specific we have to
6859 * pass sorting parameters via the global 'server' structure */
6860static int sortCompare(const void *s1, const void *s2) {
6861 const redisSortObject *so1 = s1, *so2 = s2;
6862 int cmp;
6863
6864 if (!server.sort_alpha) {
6865 /* Numeric sorting. Here it's trivial as we precomputed scores */
6866 if (so1->u.score > so2->u.score) {
6867 cmp = 1;
6868 } else if (so1->u.score < so2->u.score) {
6869 cmp = -1;
6870 } else {
6871 cmp = 0;
6872 }
6873 } else {
6874 /* Alphanumeric sorting */
6875 if (server.sort_bypattern) {
6876 if (!so1->u.cmpobj || !so2->u.cmpobj) {
6877 /* At least one compare object is NULL */
6878 if (so1->u.cmpobj == so2->u.cmpobj)
6879 cmp = 0;
6880 else if (so1->u.cmpobj == NULL)
6881 cmp = -1;
6882 else
6883 cmp = 1;
6884 } else {
6885 /* We have both the objects, use strcoll */
6886 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
6887 }
6888 } else {
08ee9b57 6889 /* Compare elements directly. */
6890 cmp = compareStringObjects(so1->obj,so2->obj);
ed9b544e 6891 }
6892 }
6893 return server.sort_desc ? -cmp : cmp;
6894}
6895
6896/* The SORT command is the most complex command in Redis. Warning: this code
6897 * is optimized for speed and a bit less for readability */
6898static void sortCommand(redisClient *c) {
ed9b544e 6899 list *operations;
6900 int outputlen = 0;
6901 int desc = 0, alpha = 0;
6902 int limit_start = 0, limit_count = -1, start, end;
6903 int j, dontsort = 0, vectorlen;
6904 int getop = 0; /* GET operation counter */
443c6409 6905 robj *sortval, *sortby = NULL, *storekey = NULL;
ed9b544e 6906 redisSortObject *vector; /* Resulting vector to sort */
6907
6908 /* Lookup the key to sort. It must be of the right types */
3305306f 6909 sortval = lookupKeyRead(c->db,c->argv[1]);
6910 if (sortval == NULL) {
4e27f268 6911 addReply(c,shared.emptymultibulk);
ed9b544e 6912 return;
6913 }
a5eb649b 6914 if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
6915 sortval->type != REDIS_ZSET)
6916 {
c937aa89 6917 addReply(c,shared.wrongtypeerr);
ed9b544e 6918 return;
6919 }
6920
6921 /* Create a list of operations to perform for every sorted element.
6922 * Operations can be GET/DEL/INCR/DECR */
6923 operations = listCreate();
092dac2a 6924 listSetFreeMethod(operations,zfree);
ed9b544e 6925 j = 2;
6926
6927 /* Now we need to protect sortval incrementing its count, in the future
6928 * SORT may have options able to overwrite/delete keys during the sorting
6929 * and the sorted key itself may get destroied */
6930 incrRefCount(sortval);
6931
6932 /* The SORT command has an SQL-alike syntax, parse it */
6933 while(j < c->argc) {
6934 int leftargs = c->argc-j-1;
6935 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
6936 desc = 0;
6937 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
6938 desc = 1;
6939 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
6940 alpha = 1;
6941 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
6942 limit_start = atoi(c->argv[j+1]->ptr);
6943 limit_count = atoi(c->argv[j+2]->ptr);
6944 j+=2;
443c6409 6945 } else if (!strcasecmp(c->argv[j]->ptr,"store") && leftargs >= 1) {
6946 storekey = c->argv[j+1];
6947 j++;
ed9b544e 6948 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
6949 sortby = c->argv[j+1];
6950 /* If the BY pattern does not contain '*', i.e. it is constant,
6951 * we don't need to sort nor to lookup the weight keys. */
6952 if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
6953 j++;
6954 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
6955 listAddNodeTail(operations,createSortOperation(
6956 REDIS_SORT_GET,c->argv[j+1]));
6957 getop++;
6958 j++;
ed9b544e 6959 } else {
6960 decrRefCount(sortval);
6961 listRelease(operations);
c937aa89 6962 addReply(c,shared.syntaxerr);
ed9b544e 6963 return;
6964 }
6965 j++;
6966 }
6967
6968 /* Load the sorting vector with all the objects to sort */
a5eb649b 6969 switch(sortval->type) {
6970 case REDIS_LIST: vectorlen = listLength((list*)sortval->ptr); break;
6971 case REDIS_SET: vectorlen = dictSize((dict*)sortval->ptr); break;
6972 case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
f83c6cb5 6973 default: vectorlen = 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
a5eb649b 6974 }
ed9b544e 6975 vector = zmalloc(sizeof(redisSortObject)*vectorlen);
ed9b544e 6976 j = 0;
a5eb649b 6977
ed9b544e 6978 if (sortval->type == REDIS_LIST) {
6979 list *list = sortval->ptr;
6208b3a7 6980 listNode *ln;
c7df85a4 6981 listIter li;
6208b3a7 6982
c7df85a4 6983 listRewind(list,&li);
6984 while((ln = listNext(&li))) {
ed9b544e 6985 robj *ele = ln->value;
6986 vector[j].obj = ele;
6987 vector[j].u.score = 0;
6988 vector[j].u.cmpobj = NULL;
ed9b544e 6989 j++;
6990 }
6991 } else {
a5eb649b 6992 dict *set;
ed9b544e 6993 dictIterator *di;
6994 dictEntry *setele;
6995
a5eb649b 6996 if (sortval->type == REDIS_SET) {
6997 set = sortval->ptr;
6998 } else {
6999 zset *zs = sortval->ptr;
7000 set = zs->dict;
7001 }
7002
ed9b544e 7003 di = dictGetIterator(set);
ed9b544e 7004 while((setele = dictNext(di)) != NULL) {
7005 vector[j].obj = dictGetEntryKey(setele);
7006 vector[j].u.score = 0;
7007 vector[j].u.cmpobj = NULL;
7008 j++;
7009 }
7010 dictReleaseIterator(di);
7011 }
dfc5e96c 7012 redisAssert(j == vectorlen);
ed9b544e 7013
7014 /* Now it's time to load the right scores in the sorting vector */
7015 if (dontsort == 0) {
7016 for (j = 0; j < vectorlen; j++) {
6d7d1370 7017 robj *byval;
ed9b544e 7018 if (sortby) {
6d7d1370 7019 /* lookup value to sort by */
3305306f 7020 byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
705dad38 7021 if (!byval) continue;
ed9b544e 7022 } else {
6d7d1370
PN
7023 /* use object itself to sort by */
7024 byval = vector[j].obj;
7025 }
7026
7027 if (alpha) {
08ee9b57 7028 if (sortby) vector[j].u.cmpobj = getDecodedObject(byval);
6d7d1370
PN
7029 } else {
7030 if (byval->encoding == REDIS_ENCODING_RAW) {
7031 vector[j].u.score = strtod(byval->ptr,NULL);
16fa22f1 7032 } else if (byval->encoding == REDIS_ENCODING_INT) {
6d7d1370
PN
7033 /* Don't need to decode the object if it's
7034 * integer-encoded (the only encoding supported) so
7035 * far. We can just cast it */
16fa22f1
PN
7036 vector[j].u.score = (long)byval->ptr;
7037 } else {
7038 redisAssert(1 != 1);
942a3961 7039 }
ed9b544e 7040 }
6d7d1370 7041
705dad38
PN
7042 /* when the object was retrieved using lookupKeyByPattern,
7043 * its refcount needs to be decreased. */
7044 if (sortby) {
7045 decrRefCount(byval);
ed9b544e 7046 }
7047 }
7048 }
7049
7050 /* We are ready to sort the vector... perform a bit of sanity check
7051 * on the LIMIT option too. We'll use a partial version of quicksort. */
7052 start = (limit_start < 0) ? 0 : limit_start;
7053 end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
7054 if (start >= vectorlen) {
7055 start = vectorlen-1;
7056 end = vectorlen-2;
7057 }
7058 if (end >= vectorlen) end = vectorlen-1;
7059
7060 if (dontsort == 0) {
7061 server.sort_desc = desc;
7062 server.sort_alpha = alpha;
7063 server.sort_bypattern = sortby ? 1 : 0;
5f5b9840 7064 if (sortby && (start != 0 || end != vectorlen-1))
7065 pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
7066 else
7067 qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
ed9b544e 7068 }
7069
7070 /* Send command output to the output buffer, performing the specified
7071 * GET/DEL/INCR/DECR operations if any. */
7072 outputlen = getop ? getop*(end-start+1) : end-start+1;
443c6409 7073 if (storekey == NULL) {
7074 /* STORE option not specified, sent the sorting result to client */
7075 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen));
7076 for (j = start; j <= end; j++) {
7077 listNode *ln;
c7df85a4 7078 listIter li;
7079
dd88747b 7080 if (!getop) addReplyBulk(c,vector[j].obj);
c7df85a4 7081 listRewind(operations,&li);
7082 while((ln = listNext(&li))) {
443c6409 7083 redisSortOperation *sop = ln->value;
7084 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7085 vector[j].obj);
7086
7087 if (sop->type == REDIS_SORT_GET) {
55017f9d 7088 if (!val) {
443c6409 7089 addReply(c,shared.nullbulk);
7090 } else {
dd88747b 7091 addReplyBulk(c,val);
55017f9d 7092 decrRefCount(val);
443c6409 7093 }
7094 } else {
dfc5e96c 7095 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
443c6409 7096 }
7097 }
ed9b544e 7098 }
443c6409 7099 } else {
7100 robj *listObject = createListObject();
7101 list *listPtr = (list*) listObject->ptr;
7102
7103 /* STORE option specified, set the sorting result as a List object */
7104 for (j = start; j <= end; j++) {
7105 listNode *ln;
c7df85a4 7106 listIter li;
7107
443c6409 7108 if (!getop) {
7109 listAddNodeTail(listPtr,vector[j].obj);
7110 incrRefCount(vector[j].obj);
7111 }
c7df85a4 7112 listRewind(operations,&li);
7113 while((ln = listNext(&li))) {
443c6409 7114 redisSortOperation *sop = ln->value;
7115 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7116 vector[j].obj);
7117
7118 if (sop->type == REDIS_SORT_GET) {
55017f9d 7119 if (!val) {
443c6409 7120 listAddNodeTail(listPtr,createStringObject("",0));
7121 } else {
55017f9d
PN
7122 /* We should do a incrRefCount on val because it is
7123 * added to the list, but also a decrRefCount because
7124 * it is returned by lookupKeyByPattern. This results
7125 * in doing nothing at all. */
443c6409 7126 listAddNodeTail(listPtr,val);
443c6409 7127 }
ed9b544e 7128 } else {
dfc5e96c 7129 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
ed9b544e 7130 }
ed9b544e 7131 }
ed9b544e 7132 }
121796f7 7133 if (dictReplace(c->db->dict,storekey,listObject)) {
7134 incrRefCount(storekey);
7135 }
443c6409 7136 /* Note: we add 1 because the DB is dirty anyway since even if the
7137 * SORT result is empty a new key is set and maybe the old content
7138 * replaced. */
7139 server.dirty += 1+outputlen;
7140 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",outputlen));
ed9b544e 7141 }
7142
7143 /* Cleanup */
7144 decrRefCount(sortval);
7145 listRelease(operations);
7146 for (j = 0; j < vectorlen; j++) {
16fa22f1 7147 if (alpha && vector[j].u.cmpobj)
ed9b544e 7148 decrRefCount(vector[j].u.cmpobj);
7149 }
7150 zfree(vector);
7151}
7152
ec6c7a1d 7153/* Convert an amount of bytes into a human readable string in the form
7154 * of 100B, 2G, 100M, 4K, and so forth. */
7155static void bytesToHuman(char *s, unsigned long long n) {
7156 double d;
7157
7158 if (n < 1024) {
7159 /* Bytes */
7160 sprintf(s,"%lluB",n);
7161 return;
7162 } else if (n < (1024*1024)) {
7163 d = (double)n/(1024);
7164 sprintf(s,"%.2fK",d);
7165 } else if (n < (1024LL*1024*1024)) {
7166 d = (double)n/(1024*1024);
7167 sprintf(s,"%.2fM",d);
7168 } else if (n < (1024LL*1024*1024*1024)) {
7169 d = (double)n/(1024LL*1024*1024);
b72f6a4b 7170 sprintf(s,"%.2fG",d);
ec6c7a1d 7171 }
7172}
7173
1c85b79f 7174/* Create the string returned by the INFO command. This is decoupled
7175 * by the INFO command itself as we need to report the same information
7176 * on memory corruption problems. */
7177static sds genRedisInfoString(void) {
ed9b544e 7178 sds info;
7179 time_t uptime = time(NULL)-server.stat_starttime;
c3cb078d 7180 int j;
ec6c7a1d 7181 char hmem[64];
55a8298f 7182
b72f6a4b 7183 bytesToHuman(hmem,zmalloc_used_memory());
ed9b544e 7184 info = sdscatprintf(sdsempty(),
7185 "redis_version:%s\r\n"
f1017b3f 7186 "arch_bits:%s\r\n"
7a932b74 7187 "multiplexing_api:%s\r\n"
0d7170a4 7188 "process_id:%ld\r\n"
682ac724 7189 "uptime_in_seconds:%ld\r\n"
7190 "uptime_in_days:%ld\r\n"
ed9b544e 7191 "connected_clients:%d\r\n"
7192 "connected_slaves:%d\r\n"
f86a74e9 7193 "blocked_clients:%d\r\n"
5fba9f71 7194 "used_memory:%zu\r\n"
ec6c7a1d 7195 "used_memory_human:%s\r\n"
ed9b544e 7196 "changes_since_last_save:%lld\r\n"
be2bb6b0 7197 "bgsave_in_progress:%d\r\n"
682ac724 7198 "last_save_time:%ld\r\n"
b3fad521 7199 "bgrewriteaof_in_progress:%d\r\n"
ed9b544e 7200 "total_connections_received:%lld\r\n"
7201 "total_commands_processed:%lld\r\n"
2a6a2ed1 7202 "expired_keys:%lld\r\n"
55a8298f 7203 "hash_max_zipmap_entries:%ld\r\n"
7204 "hash_max_zipmap_value:%ld\r\n"
ffc6b7f8 7205 "pubsub_channels:%ld\r\n"
7206 "pubsub_patterns:%u\r\n"
7d98e08c 7207 "vm_enabled:%d\r\n"
a0f643ea 7208 "role:%s\r\n"
ed9b544e 7209 ,REDIS_VERSION,
f1017b3f 7210 (sizeof(long) == 8) ? "64" : "32",
7a932b74 7211 aeGetApiName(),
0d7170a4 7212 (long) getpid(),
a0f643ea 7213 uptime,
7214 uptime/(3600*24),
ed9b544e 7215 listLength(server.clients)-listLength(server.slaves),
7216 listLength(server.slaves),
d5d55fc3 7217 server.blpop_blocked_clients,
b72f6a4b 7218 zmalloc_used_memory(),
ec6c7a1d 7219 hmem,
ed9b544e 7220 server.dirty,
9d65a1bb 7221 server.bgsavechildpid != -1,
ed9b544e 7222 server.lastsave,
b3fad521 7223 server.bgrewritechildpid != -1,
ed9b544e 7224 server.stat_numconnections,
7225 server.stat_numcommands,
2a6a2ed1 7226 server.stat_expiredkeys,
55a8298f 7227 server.hash_max_zipmap_entries,
7228 server.hash_max_zipmap_value,
ffc6b7f8 7229 dictSize(server.pubsub_channels),
7230 listLength(server.pubsub_patterns),
7d98e08c 7231 server.vm_enabled != 0,
a0f643ea 7232 server.masterhost == NULL ? "master" : "slave"
ed9b544e 7233 );
a0f643ea 7234 if (server.masterhost) {
7235 info = sdscatprintf(info,
7236 "master_host:%s\r\n"
7237 "master_port:%d\r\n"
7238 "master_link_status:%s\r\n"
7239 "master_last_io_seconds_ago:%d\r\n"
7240 ,server.masterhost,
7241 server.masterport,
7242 (server.replstate == REDIS_REPL_CONNECTED) ?
7243 "up" : "down",
f72b934d 7244 server.master ? ((int)(time(NULL)-server.master->lastinteraction)) : -1
a0f643ea 7245 );
7246 }
7d98e08c 7247 if (server.vm_enabled) {
1064ef87 7248 lockThreadedIO();
7d98e08c 7249 info = sdscatprintf(info,
7250 "vm_conf_max_memory:%llu\r\n"
7251 "vm_conf_page_size:%llu\r\n"
7252 "vm_conf_pages:%llu\r\n"
7253 "vm_stats_used_pages:%llu\r\n"
7254 "vm_stats_swapped_objects:%llu\r\n"
7255 "vm_stats_swappin_count:%llu\r\n"
7256 "vm_stats_swappout_count:%llu\r\n"
b9bc0eef 7257 "vm_stats_io_newjobs_len:%lu\r\n"
7258 "vm_stats_io_processing_len:%lu\r\n"
7259 "vm_stats_io_processed_len:%lu\r\n"
25fd2cb2 7260 "vm_stats_io_active_threads:%lu\r\n"
d5d55fc3 7261 "vm_stats_blocked_clients:%lu\r\n"
7d98e08c 7262 ,(unsigned long long) server.vm_max_memory,
7263 (unsigned long long) server.vm_page_size,
7264 (unsigned long long) server.vm_pages,
7265 (unsigned long long) server.vm_stats_used_pages,
7266 (unsigned long long) server.vm_stats_swapped_objects,
7267 (unsigned long long) server.vm_stats_swapins,
b9bc0eef 7268 (unsigned long long) server.vm_stats_swapouts,
7269 (unsigned long) listLength(server.io_newjobs),
7270 (unsigned long) listLength(server.io_processing),
7271 (unsigned long) listLength(server.io_processed),
d5d55fc3 7272 (unsigned long) server.io_active_threads,
7273 (unsigned long) server.vm_blocked_clients
7d98e08c 7274 );
1064ef87 7275 unlockThreadedIO();
7d98e08c 7276 }
c3cb078d 7277 for (j = 0; j < server.dbnum; j++) {
7278 long long keys, vkeys;
7279
7280 keys = dictSize(server.db[j].dict);
7281 vkeys = dictSize(server.db[j].expires);
7282 if (keys || vkeys) {
9d65a1bb 7283 info = sdscatprintf(info, "db%d:keys=%lld,expires=%lld\r\n",
c3cb078d 7284 j, keys, vkeys);
7285 }
7286 }
1c85b79f 7287 return info;
7288}
7289
7290static void infoCommand(redisClient *c) {
7291 sds info = genRedisInfoString();
83c6a618 7292 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
7293 (unsigned long)sdslen(info)));
ed9b544e 7294 addReplySds(c,info);
70003d28 7295 addReply(c,shared.crlf);
ed9b544e 7296}
7297
3305306f 7298static void monitorCommand(redisClient *c) {
7299 /* ignore MONITOR if aleady slave or in monitor mode */
7300 if (c->flags & REDIS_SLAVE) return;
7301
7302 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
7303 c->slaveseldb = 0;
6b47e12e 7304 listAddNodeTail(server.monitors,c);
3305306f 7305 addReply(c,shared.ok);
7306}
7307
7308/* ================================= Expire ================================= */
7309static int removeExpire(redisDb *db, robj *key) {
7310 if (dictDelete(db->expires,key) == DICT_OK) {
7311 return 1;
7312 } else {
7313 return 0;
7314 }
7315}
7316
7317static int setExpire(redisDb *db, robj *key, time_t when) {
7318 if (dictAdd(db->expires,key,(void*)when) == DICT_ERR) {
7319 return 0;
7320 } else {
7321 incrRefCount(key);
7322 return 1;
7323 }
7324}
7325
bb32ede5 7326/* Return the expire time of the specified key, or -1 if no expire
7327 * is associated with this key (i.e. the key is non volatile) */
7328static time_t getExpire(redisDb *db, robj *key) {
7329 dictEntry *de;
7330
7331 /* No expire? return ASAP */
7332 if (dictSize(db->expires) == 0 ||
7333 (de = dictFind(db->expires,key)) == NULL) return -1;
7334
7335 return (time_t) dictGetEntryVal(de);
7336}
7337
3305306f 7338static int expireIfNeeded(redisDb *db, robj *key) {
7339 time_t when;
7340 dictEntry *de;
7341
7342 /* No expire? return ASAP */
7343 if (dictSize(db->expires) == 0 ||
7344 (de = dictFind(db->expires,key)) == NULL) return 0;
7345
7346 /* Lookup the expire */
7347 when = (time_t) dictGetEntryVal(de);
7348 if (time(NULL) <= when) return 0;
7349
7350 /* Delete the key */
7351 dictDelete(db->expires,key);
2a6a2ed1 7352 server.stat_expiredkeys++;
3305306f 7353 return dictDelete(db->dict,key) == DICT_OK;
7354}
7355
7356static int deleteIfVolatile(redisDb *db, robj *key) {
7357 dictEntry *de;
7358
7359 /* No expire? return ASAP */
7360 if (dictSize(db->expires) == 0 ||
7361 (de = dictFind(db->expires,key)) == NULL) return 0;
7362
7363 /* Delete the key */
0c66a471 7364 server.dirty++;
2a6a2ed1 7365 server.stat_expiredkeys++;
3305306f 7366 dictDelete(db->expires,key);
7367 return dictDelete(db->dict,key) == DICT_OK;
7368}
7369
bbe025e0 7370static void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
3305306f 7371 dictEntry *de;
bbe025e0
AM
7372 time_t seconds;
7373
bd79a6bd 7374 if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
bbe025e0
AM
7375
7376 seconds -= offset;
3305306f 7377
802e8373 7378 de = dictFind(c->db->dict,key);
3305306f 7379 if (de == NULL) {
7380 addReply(c,shared.czero);
7381 return;
7382 }
d4dd6556 7383 if (seconds <= 0) {
43e5ccdf 7384 if (deleteKey(c->db,key)) server.dirty++;
7385 addReply(c, shared.cone);
3305306f 7386 return;
7387 } else {
7388 time_t when = time(NULL)+seconds;
802e8373 7389 if (setExpire(c->db,key,when)) {
3305306f 7390 addReply(c,shared.cone);
77423026 7391 server.dirty++;
7392 } else {
3305306f 7393 addReply(c,shared.czero);
77423026 7394 }
3305306f 7395 return;
7396 }
7397}
7398
802e8373 7399static void expireCommand(redisClient *c) {
bbe025e0 7400 expireGenericCommand(c,c->argv[1],c->argv[2],0);
802e8373 7401}
7402
7403static void expireatCommand(redisClient *c) {
bbe025e0 7404 expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
802e8373 7405}
7406
fd88489a 7407static void ttlCommand(redisClient *c) {
7408 time_t expire;
7409 int ttl = -1;
7410
7411 expire = getExpire(c->db,c->argv[1]);
7412 if (expire != -1) {
7413 ttl = (int) (expire-time(NULL));
7414 if (ttl < 0) ttl = -1;
7415 }
7416 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",ttl));
7417}
7418
6e469882 7419/* ================================ MULTI/EXEC ============================== */
7420
7421/* Client state initialization for MULTI/EXEC */
7422static void initClientMultiState(redisClient *c) {
7423 c->mstate.commands = NULL;
7424 c->mstate.count = 0;
7425}
7426
7427/* Release all the resources associated with MULTI/EXEC state */
7428static void freeClientMultiState(redisClient *c) {
7429 int j;
7430
7431 for (j = 0; j < c->mstate.count; j++) {
7432 int i;
7433 multiCmd *mc = c->mstate.commands+j;
7434
7435 for (i = 0; i < mc->argc; i++)
7436 decrRefCount(mc->argv[i]);
7437 zfree(mc->argv);
7438 }
7439 zfree(c->mstate.commands);
7440}
7441
7442/* Add a new command into the MULTI commands queue */
7443static void queueMultiCommand(redisClient *c, struct redisCommand *cmd) {
7444 multiCmd *mc;
7445 int j;
7446
7447 c->mstate.commands = zrealloc(c->mstate.commands,
7448 sizeof(multiCmd)*(c->mstate.count+1));
7449 mc = c->mstate.commands+c->mstate.count;
7450 mc->cmd = cmd;
7451 mc->argc = c->argc;
7452 mc->argv = zmalloc(sizeof(robj*)*c->argc);
7453 memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
7454 for (j = 0; j < c->argc; j++)
7455 incrRefCount(mc->argv[j]);
7456 c->mstate.count++;
7457}
7458
7459static void multiCommand(redisClient *c) {
7460 c->flags |= REDIS_MULTI;
36c548f0 7461 addReply(c,shared.ok);
6e469882 7462}
7463
18b6cb76
DJ
7464static void discardCommand(redisClient *c) {
7465 if (!(c->flags & REDIS_MULTI)) {
7466 addReplySds(c,sdsnew("-ERR DISCARD without MULTI\r\n"));
7467 return;
7468 }
7469
7470 freeClientMultiState(c);
7471 initClientMultiState(c);
7472 c->flags &= (~REDIS_MULTI);
7473 addReply(c,shared.ok);
7474}
7475
66c8853f 7476/* Send a MULTI command to all the slaves and AOF file. Check the execCommand
7477 * implememntation for more information. */
7478static void execCommandReplicateMulti(redisClient *c) {
7479 struct redisCommand *cmd;
7480 robj *multistring = createStringObject("MULTI",5);
7481
7482 cmd = lookupCommand("multi");
7483 if (server.appendonly)
7484 feedAppendOnlyFile(cmd,c->db->id,&multistring,1);
7485 if (listLength(server.slaves))
7486 replicationFeedSlaves(server.slaves,c->db->id,&multistring,1);
7487 decrRefCount(multistring);
7488}
7489
6e469882 7490static void execCommand(redisClient *c) {
7491 int j;
7492 robj **orig_argv;
7493 int orig_argc;
7494
7495 if (!(c->flags & REDIS_MULTI)) {
7496 addReplySds(c,sdsnew("-ERR EXEC without MULTI\r\n"));
7497 return;
7498 }
7499
66c8853f 7500 /* Replicate a MULTI request now that we are sure the block is executed.
7501 * This way we'll deliver the MULTI/..../EXEC block as a whole and
7502 * both the AOF and the replication link will have the same consistency
7503 * and atomicity guarantees. */
7504 execCommandReplicateMulti(c);
7505
7506 /* Exec all the queued commands */
6e469882 7507 orig_argv = c->argv;
7508 orig_argc = c->argc;
7509 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->mstate.count));
7510 for (j = 0; j < c->mstate.count; j++) {
7511 c->argc = c->mstate.commands[j].argc;
7512 c->argv = c->mstate.commands[j].argv;
7513 call(c,c->mstate.commands[j].cmd);
7514 }
7515 c->argv = orig_argv;
7516 c->argc = orig_argc;
7517 freeClientMultiState(c);
7518 initClientMultiState(c);
7519 c->flags &= (~REDIS_MULTI);
66c8853f 7520 /* Make sure the EXEC command is always replicated / AOF, since we
7521 * always send the MULTI command (we can't know beforehand if the
7522 * next operations will contain at least a modification to the DB). */
7523 server.dirty++;
6e469882 7524}
7525
4409877e 7526/* =========================== Blocking Operations ========================= */
7527
7528/* Currently Redis blocking operations support is limited to list POP ops,
7529 * so the current implementation is not fully generic, but it is also not
7530 * completely specific so it will not require a rewrite to support new
7531 * kind of blocking operations in the future.
7532 *
7533 * Still it's important to note that list blocking operations can be already
7534 * used as a notification mechanism in order to implement other blocking
7535 * operations at application level, so there must be a very strong evidence
7536 * of usefulness and generality before new blocking operations are implemented.
7537 *
7538 * This is how the current blocking POP works, we use BLPOP as example:
7539 * - If the user calls BLPOP and the key exists and contains a non empty list
7540 * then LPOP is called instead. So BLPOP is semantically the same as LPOP
7541 * if there is not to block.
7542 * - If instead BLPOP is called and the key does not exists or the list is
7543 * empty we need to block. In order to do so we remove the notification for
7544 * new data to read in the client socket (so that we'll not serve new
7545 * requests if the blocking request is not served). Also we put the client
95242ab5 7546 * in a dictionary (db->blockingkeys) mapping keys to a list of clients
4409877e 7547 * blocking for this keys.
7548 * - If a PUSH operation against a key with blocked clients waiting is
7549 * performed, we serve the first in the list: basically instead to push
7550 * the new element inside the list we return it to the (first / oldest)
7551 * blocking client, unblock the client, and remove it form the list.
7552 *
7553 * The above comment and the source code should be enough in order to understand
7554 * the implementation and modify / fix it later.
7555 */
7556
7557/* Set a client in blocking mode for the specified key, with the specified
7558 * timeout */
b177fd30 7559static void blockForKeys(redisClient *c, robj **keys, int numkeys, time_t timeout) {
4409877e 7560 dictEntry *de;
7561 list *l;
b177fd30 7562 int j;
4409877e 7563
b177fd30 7564 c->blockingkeys = zmalloc(sizeof(robj*)*numkeys);
7565 c->blockingkeysnum = numkeys;
4409877e 7566 c->blockingto = timeout;
b177fd30 7567 for (j = 0; j < numkeys; j++) {
7568 /* Add the key in the client structure, to map clients -> keys */
7569 c->blockingkeys[j] = keys[j];
7570 incrRefCount(keys[j]);
4409877e 7571
b177fd30 7572 /* And in the other "side", to map keys -> clients */
7573 de = dictFind(c->db->blockingkeys,keys[j]);
7574 if (de == NULL) {
7575 int retval;
7576
7577 /* For every key we take a list of clients blocked for it */
7578 l = listCreate();
7579 retval = dictAdd(c->db->blockingkeys,keys[j],l);
7580 incrRefCount(keys[j]);
7581 assert(retval == DICT_OK);
7582 } else {
7583 l = dictGetEntryVal(de);
7584 }
7585 listAddNodeTail(l,c);
4409877e 7586 }
b177fd30 7587 /* Mark the client as a blocked client */
4409877e 7588 c->flags |= REDIS_BLOCKED;
d5d55fc3 7589 server.blpop_blocked_clients++;
4409877e 7590}
7591
7592/* Unblock a client that's waiting in a blocking operation such as BLPOP */
b0d8747d 7593static void unblockClientWaitingData(redisClient *c) {
4409877e 7594 dictEntry *de;
7595 list *l;
b177fd30 7596 int j;
4409877e 7597
b177fd30 7598 assert(c->blockingkeys != NULL);
7599 /* The client may wait for multiple keys, so unblock it for every key. */
7600 for (j = 0; j < c->blockingkeysnum; j++) {
7601 /* Remove this client from the list of clients waiting for this key. */
7602 de = dictFind(c->db->blockingkeys,c->blockingkeys[j]);
7603 assert(de != NULL);
7604 l = dictGetEntryVal(de);
7605 listDelNode(l,listSearchKey(l,c));
7606 /* If the list is empty we need to remove it to avoid wasting memory */
7607 if (listLength(l) == 0)
7608 dictDelete(c->db->blockingkeys,c->blockingkeys[j]);
7609 decrRefCount(c->blockingkeys[j]);
7610 }
7611 /* Cleanup the client structure */
7612 zfree(c->blockingkeys);
7613 c->blockingkeys = NULL;
4409877e 7614 c->flags &= (~REDIS_BLOCKED);
d5d55fc3 7615 server.blpop_blocked_clients--;
5921aa36 7616 /* We want to process data if there is some command waiting
b0d8747d 7617 * in the input buffer. Note that this is safe even if
7618 * unblockClientWaitingData() gets called from freeClient() because
7619 * freeClient() will be smart enough to call this function
7620 * *after* c->querybuf was set to NULL. */
4409877e 7621 if (c->querybuf && sdslen(c->querybuf) > 0) processInputBuffer(c);
7622}
7623
7624/* This should be called from any function PUSHing into lists.
7625 * 'c' is the "pushing client", 'key' is the key it is pushing data against,
7626 * 'ele' is the element pushed.
7627 *
7628 * If the function returns 0 there was no client waiting for a list push
7629 * against this key.
7630 *
7631 * If the function returns 1 there was a client waiting for a list push
7632 * against this key, the element was passed to this client thus it's not
7633 * needed to actually add it to the list and the caller should return asap. */
7634static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele) {
7635 struct dictEntry *de;
7636 redisClient *receiver;
7637 list *l;
7638 listNode *ln;
7639
7640 de = dictFind(c->db->blockingkeys,key);
7641 if (de == NULL) return 0;
7642 l = dictGetEntryVal(de);
7643 ln = listFirst(l);
7644 assert(ln != NULL);
7645 receiver = ln->value;
4409877e 7646
b177fd30 7647 addReplySds(receiver,sdsnew("*2\r\n"));
dd88747b 7648 addReplyBulk(receiver,key);
7649 addReplyBulk(receiver,ele);
b0d8747d 7650 unblockClientWaitingData(receiver);
4409877e 7651 return 1;
7652}
7653
7654/* Blocking RPOP/LPOP */
7655static void blockingPopGenericCommand(redisClient *c, int where) {
7656 robj *o;
7657 time_t timeout;
b177fd30 7658 int j;
4409877e 7659
b177fd30 7660 for (j = 1; j < c->argc-1; j++) {
7661 o = lookupKeyWrite(c->db,c->argv[j]);
7662 if (o != NULL) {
7663 if (o->type != REDIS_LIST) {
7664 addReply(c,shared.wrongtypeerr);
4409877e 7665 return;
b177fd30 7666 } else {
7667 list *list = o->ptr;
7668 if (listLength(list) != 0) {
7669 /* If the list contains elements fall back to the usual
7670 * non-blocking POP operation */
7671 robj *argv[2], **orig_argv;
7672 int orig_argc;
e0a62c7f 7673
b177fd30 7674 /* We need to alter the command arguments before to call
7675 * popGenericCommand() as the command takes a single key. */
7676 orig_argv = c->argv;
7677 orig_argc = c->argc;
7678 argv[1] = c->argv[j];
7679 c->argv = argv;
7680 c->argc = 2;
7681
7682 /* Also the return value is different, we need to output
7683 * the multi bulk reply header and the key name. The
7684 * "real" command will add the last element (the value)
7685 * for us. If this souds like an hack to you it's just
7686 * because it is... */
7687 addReplySds(c,sdsnew("*2\r\n"));
dd88747b 7688 addReplyBulk(c,argv[1]);
b177fd30 7689 popGenericCommand(c,where);
7690
7691 /* Fix the client structure with the original stuff */
7692 c->argv = orig_argv;
7693 c->argc = orig_argc;
7694 return;
7695 }
4409877e 7696 }
7697 }
7698 }
7699 /* If the list is empty or the key does not exists we must block */
b177fd30 7700 timeout = strtol(c->argv[c->argc-1]->ptr,NULL,10);
4409877e 7701 if (timeout > 0) timeout += time(NULL);
b177fd30 7702 blockForKeys(c,c->argv+1,c->argc-2,timeout);
4409877e 7703}
7704
7705static void blpopCommand(redisClient *c) {
7706 blockingPopGenericCommand(c,REDIS_HEAD);
7707}
7708
7709static void brpopCommand(redisClient *c) {
7710 blockingPopGenericCommand(c,REDIS_TAIL);
7711}
7712
ed9b544e 7713/* =============================== Replication ============================= */
7714
a4d1ba9a 7715static int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
ed9b544e 7716 ssize_t nwritten, ret = size;
7717 time_t start = time(NULL);
7718
7719 timeout++;
7720 while(size) {
7721 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
7722 nwritten = write(fd,ptr,size);
7723 if (nwritten == -1) return -1;
7724 ptr += nwritten;
7725 size -= nwritten;
7726 }
7727 if ((time(NULL)-start) > timeout) {
7728 errno = ETIMEDOUT;
7729 return -1;
7730 }
7731 }
7732 return ret;
7733}
7734
a4d1ba9a 7735static int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
ed9b544e 7736 ssize_t nread, totread = 0;
7737 time_t start = time(NULL);
7738
7739 timeout++;
7740 while(size) {
7741 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
7742 nread = read(fd,ptr,size);
7743 if (nread == -1) return -1;
7744 ptr += nread;
7745 size -= nread;
7746 totread += nread;
7747 }
7748 if ((time(NULL)-start) > timeout) {
7749 errno = ETIMEDOUT;
7750 return -1;
7751 }
7752 }
7753 return totread;
7754}
7755
7756static int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
7757 ssize_t nread = 0;
7758
7759 size--;
7760 while(size) {
7761 char c;
7762
7763 if (syncRead(fd,&c,1,timeout) == -1) return -1;
7764 if (c == '\n') {
7765 *ptr = '\0';
7766 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
7767 return nread;
7768 } else {
7769 *ptr++ = c;
7770 *ptr = '\0';
7771 nread++;
7772 }
7773 }
7774 return nread;
7775}
7776
7777static void syncCommand(redisClient *c) {
40d224a9 7778 /* ignore SYNC if aleady slave or in monitor mode */
7779 if (c->flags & REDIS_SLAVE) return;
7780
7781 /* SYNC can't be issued when the server has pending data to send to
7782 * the client about already issued commands. We need a fresh reply
7783 * buffer registering the differences between the BGSAVE and the current
7784 * dataset, so that we can copy to other slaves if needed. */
7785 if (listLength(c->reply) != 0) {
7786 addReplySds(c,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
7787 return;
7788 }
7789
7790 redisLog(REDIS_NOTICE,"Slave ask for synchronization");
7791 /* Here we need to check if there is a background saving operation
7792 * in progress, or if it is required to start one */
9d65a1bb 7793 if (server.bgsavechildpid != -1) {
40d224a9 7794 /* Ok a background save is in progress. Let's check if it is a good
7795 * one for replication, i.e. if there is another slave that is
7796 * registering differences since the server forked to save */
7797 redisClient *slave;
7798 listNode *ln;
c7df85a4 7799 listIter li;
40d224a9 7800
c7df85a4 7801 listRewind(server.slaves,&li);
7802 while((ln = listNext(&li))) {
40d224a9 7803 slave = ln->value;
7804 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;
40d224a9 7805 }
7806 if (ln) {
7807 /* Perfect, the server is already registering differences for
7808 * another slave. Set the right state, and copy the buffer. */
7809 listRelease(c->reply);
7810 c->reply = listDup(slave->reply);
40d224a9 7811 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7812 redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
7813 } else {
7814 /* No way, we need to wait for the next BGSAVE in order to
7815 * register differences */
7816 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
7817 redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");
7818 }
7819 } else {
7820 /* Ok we don't have a BGSAVE in progress, let's start one */
7821 redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC");
7822 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
7823 redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");
7824 addReplySds(c,sdsnew("-ERR Unalbe to perform background save\r\n"));
7825 return;
7826 }
7827 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7828 }
6208b3a7 7829 c->repldbfd = -1;
40d224a9 7830 c->flags |= REDIS_SLAVE;
7831 c->slaveseldb = 0;
6b47e12e 7832 listAddNodeTail(server.slaves,c);
40d224a9 7833 return;
7834}
7835
6208b3a7 7836static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
7837 redisClient *slave = privdata;
7838 REDIS_NOTUSED(el);
7839 REDIS_NOTUSED(mask);
7840 char buf[REDIS_IOBUF_LEN];
7841 ssize_t nwritten, buflen;
7842
7843 if (slave->repldboff == 0) {
7844 /* Write the bulk write count before to transfer the DB. In theory here
7845 * we don't know how much room there is in the output buffer of the
7846 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
7847 * operations) will never be smaller than the few bytes we need. */
7848 sds bulkcount;
7849
7850 bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
7851 slave->repldbsize);
7852 if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount))
7853 {
7854 sdsfree(bulkcount);
7855 freeClient(slave);
7856 return;
7857 }
7858 sdsfree(bulkcount);
7859 }
7860 lseek(slave->repldbfd,slave->repldboff,SEEK_SET);
7861 buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);
7862 if (buflen <= 0) {
7863 redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",
7864 (buflen == 0) ? "premature EOF" : strerror(errno));
7865 freeClient(slave);
7866 return;
7867 }
7868 if ((nwritten = write(fd,buf,buflen)) == -1) {
f870935d 7869 redisLog(REDIS_VERBOSE,"Write error sending DB to slave: %s",
6208b3a7 7870 strerror(errno));
7871 freeClient(slave);
7872 return;
7873 }
7874 slave->repldboff += nwritten;
7875 if (slave->repldboff == slave->repldbsize) {
7876 close(slave->repldbfd);
7877 slave->repldbfd = -1;
7878 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
7879 slave->replstate = REDIS_REPL_ONLINE;
7880 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
266373b2 7881 sendReplyToClient, slave) == AE_ERR) {
6208b3a7 7882 freeClient(slave);
7883 return;
7884 }
7885 addReplySds(slave,sdsempty());
7886 redisLog(REDIS_NOTICE,"Synchronization with slave succeeded");
7887 }
7888}
ed9b544e 7889
a3b21203 7890/* This function is called at the end of every backgrond saving.
7891 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
7892 * otherwise REDIS_ERR is passed to the function.
7893 *
7894 * The goal of this function is to handle slaves waiting for a successful
7895 * background saving in order to perform non-blocking synchronization. */
7896static void updateSlavesWaitingBgsave(int bgsaveerr) {
6208b3a7 7897 listNode *ln;
7898 int startbgsave = 0;
c7df85a4 7899 listIter li;
ed9b544e 7900
c7df85a4 7901 listRewind(server.slaves,&li);
7902 while((ln = listNext(&li))) {
6208b3a7 7903 redisClient *slave = ln->value;
ed9b544e 7904
6208b3a7 7905 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
7906 startbgsave = 1;
7907 slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7908 } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
dde65f3f 7909 struct redis_stat buf;
e0a62c7f 7910
6208b3a7 7911 if (bgsaveerr != REDIS_OK) {
7912 freeClient(slave);
7913 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
7914 continue;
7915 }
7916 if ((slave->repldbfd = open(server.dbfilename,O_RDONLY)) == -1 ||
dde65f3f 7917 redis_fstat(slave->repldbfd,&buf) == -1) {
6208b3a7 7918 freeClient(slave);
7919 redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
7920 continue;
7921 }
7922 slave->repldboff = 0;
7923 slave->repldbsize = buf.st_size;
7924 slave->replstate = REDIS_REPL_SEND_BULK;
7925 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
266373b2 7926 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {
6208b3a7 7927 freeClient(slave);
7928 continue;
7929 }
7930 }
ed9b544e 7931 }
6208b3a7 7932 if (startbgsave) {
7933 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
c7df85a4 7934 listIter li;
7935
7936 listRewind(server.slaves,&li);
6208b3a7 7937 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
c7df85a4 7938 while((ln = listNext(&li))) {
6208b3a7 7939 redisClient *slave = ln->value;
ed9b544e 7940
6208b3a7 7941 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
7942 freeClient(slave);
7943 }
7944 }
7945 }
ed9b544e 7946}
7947
7948static int syncWithMaster(void) {
d0ccebcf 7949 char buf[1024], tmpfile[256], authcmd[1024];
18e61fa2 7950 long dumpsize;
ed9b544e 7951 int fd = anetTcpConnect(NULL,server.masterhost,server.masterport);
8c5abee8 7952 int dfd, maxtries = 5;
ed9b544e 7953
7954 if (fd == -1) {
7955 redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",
7956 strerror(errno));
7957 return REDIS_ERR;
7958 }
d0ccebcf 7959
7960 /* AUTH with the master if required. */
7961 if(server.masterauth) {
7962 snprintf(authcmd, 1024, "AUTH %s\r\n", server.masterauth);
7963 if (syncWrite(fd, authcmd, strlen(server.masterauth)+7, 5) == -1) {
7964 close(fd);
7965 redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",
7966 strerror(errno));
7967 return REDIS_ERR;
7968 }
7969 /* Read the AUTH result. */
7970 if (syncReadLine(fd,buf,1024,3600) == -1) {
7971 close(fd);
7972 redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s",
7973 strerror(errno));
7974 return REDIS_ERR;
7975 }
7976 if (buf[0] != '+') {
7977 close(fd);
7978 redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?");
7979 return REDIS_ERR;
7980 }
7981 }
7982
ed9b544e 7983 /* Issue the SYNC command */
7984 if (syncWrite(fd,"SYNC \r\n",7,5) == -1) {
7985 close(fd);
7986 redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",
7987 strerror(errno));
7988 return REDIS_ERR;
7989 }
7990 /* Read the bulk write count */
8c4d91fc 7991 if (syncReadLine(fd,buf,1024,3600) == -1) {
ed9b544e 7992 close(fd);
7993 redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s",
7994 strerror(errno));
7995 return REDIS_ERR;
7996 }
4aa701c1 7997 if (buf[0] != '$') {
7998 close(fd);
7999 redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
8000 return REDIS_ERR;
8001 }
18e61fa2 8002 dumpsize = strtol(buf+1,NULL,10);
8003 redisLog(REDIS_NOTICE,"Receiving %ld bytes data dump from MASTER",dumpsize);
ed9b544e 8004 /* Read the bulk write data on a temp file */
8c5abee8 8005 while(maxtries--) {
8006 snprintf(tmpfile,256,
8007 "temp-%d.%ld.rdb",(int)time(NULL),(long int)getpid());
8008 dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);
8009 if (dfd != -1) break;
5de9ad7c 8010 sleep(1);
8c5abee8 8011 }
ed9b544e 8012 if (dfd == -1) {
8013 close(fd);
8014 redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
8015 return REDIS_ERR;
8016 }
8017 while(dumpsize) {
8018 int nread, nwritten;
8019
8020 nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024);
8021 if (nread == -1) {
8022 redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
8023 strerror(errno));
8024 close(fd);
8025 close(dfd);
8026 return REDIS_ERR;
8027 }
8028 nwritten = write(dfd,buf,nread);
8029 if (nwritten == -1) {
8030 redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
8031 close(fd);
8032 close(dfd);
8033 return REDIS_ERR;
8034 }
8035 dumpsize -= nread;
8036 }
8037 close(dfd);
8038 if (rename(tmpfile,server.dbfilename) == -1) {
8039 redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
8040 unlink(tmpfile);
8041 close(fd);
8042 return REDIS_ERR;
8043 }
8044 emptyDb();
f78fd11b 8045 if (rdbLoad(server.dbfilename) != REDIS_OK) {
ed9b544e 8046 redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
8047 close(fd);
8048 return REDIS_ERR;
8049 }
8050 server.master = createClient(fd);
8051 server.master->flags |= REDIS_MASTER;
179b3952 8052 server.master->authenticated = 1;
ed9b544e 8053 server.replstate = REDIS_REPL_CONNECTED;
8054 return REDIS_OK;
8055}
8056
321b0e13 8057static void slaveofCommand(redisClient *c) {
8058 if (!strcasecmp(c->argv[1]->ptr,"no") &&
8059 !strcasecmp(c->argv[2]->ptr,"one")) {
8060 if (server.masterhost) {
8061 sdsfree(server.masterhost);
8062 server.masterhost = NULL;
8063 if (server.master) freeClient(server.master);
8064 server.replstate = REDIS_REPL_NONE;
8065 redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
8066 }
8067 } else {
8068 sdsfree(server.masterhost);
8069 server.masterhost = sdsdup(c->argv[1]->ptr);
8070 server.masterport = atoi(c->argv[2]->ptr);
8071 if (server.master) freeClient(server.master);
8072 server.replstate = REDIS_REPL_CONNECT;
8073 redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
8074 server.masterhost, server.masterport);
8075 }
8076 addReply(c,shared.ok);
8077}
8078
3fd78bcd 8079/* ============================ Maxmemory directive ======================== */
8080
a5819310 8081/* Try to free one object form the pre-allocated objects free list.
8082 * This is useful under low mem conditions as by default we take 1 million
8083 * free objects allocated. On success REDIS_OK is returned, otherwise
8084 * REDIS_ERR. */
8085static int tryFreeOneObjectFromFreelist(void) {
f870935d 8086 robj *o;
8087
a5819310 8088 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
8089 if (listLength(server.objfreelist)) {
8090 listNode *head = listFirst(server.objfreelist);
8091 o = listNodeValue(head);
8092 listDelNode(server.objfreelist,head);
8093 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8094 zfree(o);
8095 return REDIS_OK;
8096 } else {
8097 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8098 return REDIS_ERR;
8099 }
f870935d 8100}
8101
3fd78bcd 8102/* This function gets called when 'maxmemory' is set on the config file to limit
8103 * the max memory used by the server, and we are out of memory.
8104 * This function will try to, in order:
8105 *
8106 * - Free objects from the free list
8107 * - Try to remove keys with an EXPIRE set
8108 *
8109 * It is not possible to free enough memory to reach used-memory < maxmemory
8110 * the server will start refusing commands that will enlarge even more the
8111 * memory usage.
8112 */
8113static void freeMemoryIfNeeded(void) {
8114 while (server.maxmemory && zmalloc_used_memory() > server.maxmemory) {
a5819310 8115 int j, k, freed = 0;
8116
8117 if (tryFreeOneObjectFromFreelist() == REDIS_OK) continue;
8118 for (j = 0; j < server.dbnum; j++) {
8119 int minttl = -1;
8120 robj *minkey = NULL;
8121 struct dictEntry *de;
8122
8123 if (dictSize(server.db[j].expires)) {
8124 freed = 1;
8125 /* From a sample of three keys drop the one nearest to
8126 * the natural expire */
8127 for (k = 0; k < 3; k++) {
8128 time_t t;
8129
8130 de = dictGetRandomKey(server.db[j].expires);
8131 t = (time_t) dictGetEntryVal(de);
8132 if (minttl == -1 || t < minttl) {
8133 minkey = dictGetEntryKey(de);
8134 minttl = t;
3fd78bcd 8135 }
3fd78bcd 8136 }
a5819310 8137 deleteKey(server.db+j,minkey);
3fd78bcd 8138 }
3fd78bcd 8139 }
a5819310 8140 if (!freed) return; /* nothing to free... */
3fd78bcd 8141 }
8142}
8143
f80dff62 8144/* ============================== Append Only file ========================== */
8145
28ed1f33 8146/* Write the append only file buffer on disk.
8147 *
8148 * Since we are required to write the AOF before replying to the client,
8149 * and the only way the client socket can get a write is entering when the
8150 * the event loop, we accumulate all the AOF writes in a memory
8151 * buffer and write it on disk using this function just before entering
8152 * the event loop again. */
8153static void flushAppendOnlyFile(void) {
8154 time_t now;
8155 ssize_t nwritten;
8156
8157 if (sdslen(server.aofbuf) == 0) return;
8158
8159 /* We want to perform a single write. This should be guaranteed atomic
8160 * at least if the filesystem we are writing is a real physical one.
8161 * While this will save us against the server being killed I don't think
8162 * there is much to do about the whole server stopping for power problems
8163 * or alike */
8164 nwritten = write(server.appendfd,server.aofbuf,sdslen(server.aofbuf));
8165 if (nwritten != (signed)sdslen(server.aofbuf)) {
8166 /* Ooops, we are in troubles. The best thing to do for now is
8167 * aborting instead of giving the illusion that everything is
8168 * working as expected. */
8169 if (nwritten == -1) {
8170 redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno));
8171 } else {
8172 redisLog(REDIS_WARNING,"Exiting on short write while writing to the append-only file: %s",strerror(errno));
8173 }
8174 exit(1);
8175 }
8176 sdsfree(server.aofbuf);
8177 server.aofbuf = sdsempty();
8178
8179 /* Fsync if needed */
8180 now = time(NULL);
8181 if (server.appendfsync == APPENDFSYNC_ALWAYS ||
8182 (server.appendfsync == APPENDFSYNC_EVERYSEC &&
8183 now-server.lastfsync > 1))
8184 {
8185 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
8186 * flushing metadata. */
8187 aof_fsync(server.appendfd); /* Let's try to get this data on the disk */
8188 server.lastfsync = now;
8189 }
8190}
8191
9376e434
PN
8192static sds catAppendOnlyGenericCommand(sds buf, int argc, robj **argv) {
8193 int j;
8194 buf = sdscatprintf(buf,"*%d\r\n",argc);
8195 for (j = 0; j < argc; j++) {
8196 robj *o = getDecodedObject(argv[j]);
8197 buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
8198 buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
8199 buf = sdscatlen(buf,"\r\n",2);
8200 decrRefCount(o);
8201 }
8202 return buf;
8203}
8204
8205static sds catAppendOnlyExpireAtCommand(sds buf, robj *key, robj *seconds) {
8206 int argc = 3;
8207 long when;
8208 robj *argv[3];
8209
8210 /* Make sure we can use strtol */
8211 seconds = getDecodedObject(seconds);
8212 when = time(NULL)+strtol(seconds->ptr,NULL,10);
8213 decrRefCount(seconds);
8214
8215 argv[0] = createStringObject("EXPIREAT",8);
8216 argv[1] = key;
8217 argv[2] = createObject(REDIS_STRING,
8218 sdscatprintf(sdsempty(),"%ld",when));
8219 buf = catAppendOnlyGenericCommand(buf, argc, argv);
8220 decrRefCount(argv[0]);
8221 decrRefCount(argv[2]);
8222 return buf;
8223}
8224
f80dff62 8225static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
8226 sds buf = sdsempty();
f80dff62 8227 robj *tmpargv[3];
8228
8229 /* The DB this command was targetting is not the same as the last command
8230 * we appendend. To issue a SELECT command is needed. */
8231 if (dictid != server.appendseldb) {
8232 char seldb[64];
8233
8234 snprintf(seldb,sizeof(seldb),"%d",dictid);
682ac724 8235 buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
83c6a618 8236 (unsigned long)strlen(seldb),seldb);
f80dff62 8237 server.appendseldb = dictid;
8238 }
8239
f80dff62 8240 if (cmd->proc == expireCommand) {
9376e434
PN
8241 /* Translate EXPIRE into EXPIREAT */
8242 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8243 } else if (cmd->proc == setexCommand) {
8244 /* Translate SETEX to SET and EXPIREAT */
8245 tmpargv[0] = createStringObject("SET",3);
f80dff62 8246 tmpargv[1] = argv[1];
9376e434
PN
8247 tmpargv[2] = argv[3];
8248 buf = catAppendOnlyGenericCommand(buf,3,tmpargv);
8249 decrRefCount(tmpargv[0]);
8250 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8251 } else {
8252 buf = catAppendOnlyGenericCommand(buf,argc,argv);
f80dff62 8253 }
8254
28ed1f33 8255 /* Append to the AOF buffer. This will be flushed on disk just before
8256 * of re-entering the event loop, so before the client will get a
8257 * positive reply about the operation performed. */
8258 server.aofbuf = sdscatlen(server.aofbuf,buf,sdslen(buf));
8259
85a83172 8260 /* If a background append only file rewriting is in progress we want to
8261 * accumulate the differences between the child DB and the current one
8262 * in a buffer, so that when the child process will do its work we
8263 * can append the differences to the new append only file. */
8264 if (server.bgrewritechildpid != -1)
8265 server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf));
8266
8267 sdsfree(buf);
f80dff62 8268}
8269
8270/* In Redis commands are always executed in the context of a client, so in
8271 * order to load the append only file we need to create a fake client. */
8272static struct redisClient *createFakeClient(void) {
8273 struct redisClient *c = zmalloc(sizeof(*c));
8274
8275 selectDb(c,0);
8276 c->fd = -1;
8277 c->querybuf = sdsempty();
8278 c->argc = 0;
8279 c->argv = NULL;
8280 c->flags = 0;
9387d17d 8281 /* We set the fake client as a slave waiting for the synchronization
8282 * so that Redis will not try to send replies to this client. */
8283 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
f80dff62 8284 c->reply = listCreate();
8285 listSetFreeMethod(c->reply,decrRefCount);
8286 listSetDupMethod(c->reply,dupClientReplyValue);
4132ad8d 8287 initClientMultiState(c);
f80dff62 8288 return c;
8289}
8290
8291static void freeFakeClient(struct redisClient *c) {
8292 sdsfree(c->querybuf);
8293 listRelease(c->reply);
4132ad8d 8294 freeClientMultiState(c);
f80dff62 8295 zfree(c);
8296}
8297
8298/* Replay the append log file. On error REDIS_OK is returned. On non fatal
8299 * error (the append only file is zero-length) REDIS_ERR is returned. On
8300 * fatal error an error message is logged and the program exists. */
8301int loadAppendOnlyFile(char *filename) {
8302 struct redisClient *fakeClient;
8303 FILE *fp = fopen(filename,"r");
8304 struct redis_stat sb;
b492cf00 8305 unsigned long long loadedkeys = 0;
4132ad8d 8306 int appendonly = server.appendonly;
f80dff62 8307
8308 if (redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0)
8309 return REDIS_ERR;
8310
8311 if (fp == NULL) {
8312 redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
8313 exit(1);
8314 }
8315
4132ad8d
PN
8316 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
8317 * to the same file we're about to read. */
8318 server.appendonly = 0;
8319
f80dff62 8320 fakeClient = createFakeClient();
8321 while(1) {
8322 int argc, j;
8323 unsigned long len;
8324 robj **argv;
8325 char buf[128];
8326 sds argsds;
8327 struct redisCommand *cmd;
8328
8329 if (fgets(buf,sizeof(buf),fp) == NULL) {
8330 if (feof(fp))
8331 break;
8332 else
8333 goto readerr;
8334 }
8335 if (buf[0] != '*') goto fmterr;
8336 argc = atoi(buf+1);
8337 argv = zmalloc(sizeof(robj*)*argc);
8338 for (j = 0; j < argc; j++) {
8339 if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr;
8340 if (buf[0] != '$') goto fmterr;
8341 len = strtol(buf+1,NULL,10);
8342 argsds = sdsnewlen(NULL,len);
0f151ef1 8343 if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
f80dff62 8344 argv[j] = createObject(REDIS_STRING,argsds);
8345 if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
8346 }
8347
8348 /* Command lookup */
8349 cmd = lookupCommand(argv[0]->ptr);
8350 if (!cmd) {
8351 redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr);
8352 exit(1);
8353 }
bdcb92f2 8354 /* Try object encoding */
f80dff62 8355 if (cmd->flags & REDIS_CMD_BULK)
05df7621 8356 argv[argc-1] = tryObjectEncoding(argv[argc-1]);
f80dff62 8357 /* Run the command in the context of a fake client */
8358 fakeClient->argc = argc;
8359 fakeClient->argv = argv;
8360 cmd->proc(fakeClient);
8361 /* Discard the reply objects list from the fake client */
8362 while(listLength(fakeClient->reply))
8363 listDelNode(fakeClient->reply,listFirst(fakeClient->reply));
8364 /* Clean up, ready for the next command */
8365 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
8366 zfree(argv);
b492cf00 8367 /* Handle swapping while loading big datasets when VM is on */
8368 loadedkeys++;
8369 if (server.vm_enabled && (loadedkeys % 5000) == 0) {
8370 while (zmalloc_used_memory() > server.vm_max_memory) {
a69a0c9c 8371 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
b492cf00 8372 }
8373 }
f80dff62 8374 }
4132ad8d
PN
8375
8376 /* This point can only be reached when EOF is reached without errors.
8377 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
8378 if (fakeClient->flags & REDIS_MULTI) goto readerr;
8379
f80dff62 8380 fclose(fp);
8381 freeFakeClient(fakeClient);
4132ad8d 8382 server.appendonly = appendonly;
f80dff62 8383 return REDIS_OK;
8384
8385readerr:
8386 if (feof(fp)) {
8387 redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file");
8388 } else {
8389 redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
8390 }
8391 exit(1);
8392fmterr:
8393 redisLog(REDIS_WARNING,"Bad file format reading the append only file");
8394 exit(1);
8395}
8396
9d65a1bb 8397/* Write an object into a file in the bulk format $<count>\r\n<payload>\r\n */
9c8e3cee 8398static int fwriteBulkObject(FILE *fp, robj *obj) {
9d65a1bb 8399 char buf[128];
b9bc0eef 8400 int decrrc = 0;
8401
f2d9f50f 8402 /* Avoid the incr/decr ref count business if possible to help
8403 * copy-on-write (we are often in a child process when this function
8404 * is called).
8405 * Also makes sure that key objects don't get incrRefCount-ed when VM
8406 * is enabled */
8407 if (obj->encoding != REDIS_ENCODING_RAW) {
b9bc0eef 8408 obj = getDecodedObject(obj);
8409 decrrc = 1;
8410 }
9d65a1bb 8411 snprintf(buf,sizeof(buf),"$%ld\r\n",(long)sdslen(obj->ptr));
8412 if (fwrite(buf,strlen(buf),1,fp) == 0) goto err;
e96e4fbf 8413 if (sdslen(obj->ptr) && fwrite(obj->ptr,sdslen(obj->ptr),1,fp) == 0)
8414 goto err;
9d65a1bb 8415 if (fwrite("\r\n",2,1,fp) == 0) goto err;
b9bc0eef 8416 if (decrrc) decrRefCount(obj);
9d65a1bb 8417 return 1;
8418err:
b9bc0eef 8419 if (decrrc) decrRefCount(obj);
9d65a1bb 8420 return 0;
8421}
8422
9c8e3cee 8423/* Write binary-safe string into a file in the bulkformat
8424 * $<count>\r\n<payload>\r\n */
8425static int fwriteBulkString(FILE *fp, char *s, unsigned long len) {
8426 char buf[128];
8427
8428 snprintf(buf,sizeof(buf),"$%ld\r\n",(unsigned long)len);
8429 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8430 if (len && fwrite(s,len,1,fp) == 0) return 0;
8431 if (fwrite("\r\n",2,1,fp) == 0) return 0;
8432 return 1;
8433}
8434
9d65a1bb 8435/* Write a double value in bulk format $<count>\r\n<payload>\r\n */
8436static int fwriteBulkDouble(FILE *fp, double d) {
8437 char buf[128], dbuf[128];
8438
8439 snprintf(dbuf,sizeof(dbuf),"%.17g\r\n",d);
8440 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(dbuf)-2);
8441 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8442 if (fwrite(dbuf,strlen(dbuf),1,fp) == 0) return 0;
8443 return 1;
8444}
8445
8446/* Write a long value in bulk format $<count>\r\n<payload>\r\n */
8447static int fwriteBulkLong(FILE *fp, long l) {
8448 char buf[128], lbuf[128];
8449
8450 snprintf(lbuf,sizeof(lbuf),"%ld\r\n",l);
8451 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(lbuf)-2);
8452 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8453 if (fwrite(lbuf,strlen(lbuf),1,fp) == 0) return 0;
8454 return 1;
8455}
8456
8457/* Write a sequence of commands able to fully rebuild the dataset into
8458 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
8459static int rewriteAppendOnlyFile(char *filename) {
8460 dictIterator *di = NULL;
8461 dictEntry *de;
8462 FILE *fp;
8463 char tmpfile[256];
8464 int j;
8465 time_t now = time(NULL);
8466
8467 /* Note that we have to use a different temp name here compared to the
8468 * one used by rewriteAppendOnlyFileBackground() function. */
8469 snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid());
8470 fp = fopen(tmpfile,"w");
8471 if (!fp) {
8472 redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno));
8473 return REDIS_ERR;
8474 }
8475 for (j = 0; j < server.dbnum; j++) {
8476 char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
8477 redisDb *db = server.db+j;
8478 dict *d = db->dict;
8479 if (dictSize(d) == 0) continue;
8480 di = dictGetIterator(d);
8481 if (!di) {
8482 fclose(fp);
8483 return REDIS_ERR;
8484 }
8485
8486 /* SELECT the new DB */
8487 if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr;
85a83172 8488 if (fwriteBulkLong(fp,j) == 0) goto werr;
9d65a1bb 8489
8490 /* Iterate this DB writing every entry */
8491 while((de = dictNext(di)) != NULL) {
e7546c63 8492 robj *key, *o;
8493 time_t expiretime;
8494 int swapped;
8495
8496 key = dictGetEntryKey(de);
b9bc0eef 8497 /* If the value for this key is swapped, load a preview in memory.
8498 * We use a "swapped" flag to remember if we need to free the
8499 * value object instead to just increment the ref count anyway
8500 * in order to avoid copy-on-write of pages if we are forked() */
996cb5f7 8501 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
8502 key->storage == REDIS_VM_SWAPPING) {
e7546c63 8503 o = dictGetEntryVal(de);
8504 swapped = 0;
8505 } else {
8506 o = vmPreviewObject(key);
e7546c63 8507 swapped = 1;
8508 }
8509 expiretime = getExpire(db,key);
9d65a1bb 8510
8511 /* Save the key and associated value */
9d65a1bb 8512 if (o->type == REDIS_STRING) {
8513 /* Emit a SET command */
8514 char cmd[]="*3\r\n$3\r\nSET\r\n";
8515 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8516 /* Key and value */
9c8e3cee 8517 if (fwriteBulkObject(fp,key) == 0) goto werr;
8518 if (fwriteBulkObject(fp,o) == 0) goto werr;
9d65a1bb 8519 } else if (o->type == REDIS_LIST) {
8520 /* Emit the RPUSHes needed to rebuild the list */
8521 list *list = o->ptr;
8522 listNode *ln;
c7df85a4 8523 listIter li;
9d65a1bb 8524
c7df85a4 8525 listRewind(list,&li);
8526 while((ln = listNext(&li))) {
9d65a1bb 8527 char cmd[]="*3\r\n$5\r\nRPUSH\r\n";
8528 robj *eleobj = listNodeValue(ln);
8529
8530 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8531 if (fwriteBulkObject(fp,key) == 0) goto werr;
8532 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9d65a1bb 8533 }
8534 } else if (o->type == REDIS_SET) {
8535 /* Emit the SADDs needed to rebuild the set */
8536 dict *set = o->ptr;
8537 dictIterator *di = dictGetIterator(set);
8538 dictEntry *de;
8539
8540 while((de = dictNext(di)) != NULL) {
8541 char cmd[]="*3\r\n$4\r\nSADD\r\n";
8542 robj *eleobj = dictGetEntryKey(de);
8543
8544 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8545 if (fwriteBulkObject(fp,key) == 0) goto werr;
8546 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9d65a1bb 8547 }
8548 dictReleaseIterator(di);
8549 } else if (o->type == REDIS_ZSET) {
8550 /* Emit the ZADDs needed to rebuild the sorted set */
8551 zset *zs = o->ptr;
8552 dictIterator *di = dictGetIterator(zs->dict);
8553 dictEntry *de;
8554
8555 while((de = dictNext(di)) != NULL) {
8556 char cmd[]="*4\r\n$4\r\nZADD\r\n";
8557 robj *eleobj = dictGetEntryKey(de);
8558 double *score = dictGetEntryVal(de);
8559
8560 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8561 if (fwriteBulkObject(fp,key) == 0) goto werr;
9d65a1bb 8562 if (fwriteBulkDouble(fp,*score) == 0) goto werr;
9c8e3cee 8563 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9d65a1bb 8564 }
8565 dictReleaseIterator(di);
9c8e3cee 8566 } else if (o->type == REDIS_HASH) {
8567 char cmd[]="*4\r\n$4\r\nHSET\r\n";
8568
8569 /* Emit the HSETs needed to rebuild the hash */
8570 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
8571 unsigned char *p = zipmapRewind(o->ptr);
8572 unsigned char *field, *val;
8573 unsigned int flen, vlen;
8574
8575 while((p = zipmapNext(p,&field,&flen,&val,&vlen)) != NULL) {
8576 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8577 if (fwriteBulkObject(fp,key) == 0) goto werr;
8578 if (fwriteBulkString(fp,(char*)field,flen) == -1)
8579 return -1;
8580 if (fwriteBulkString(fp,(char*)val,vlen) == -1)
8581 return -1;
8582 }
8583 } else {
8584 dictIterator *di = dictGetIterator(o->ptr);
8585 dictEntry *de;
8586
8587 while((de = dictNext(di)) != NULL) {
8588 robj *field = dictGetEntryKey(de);
8589 robj *val = dictGetEntryVal(de);
8590
8591 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8592 if (fwriteBulkObject(fp,key) == 0) goto werr;
8593 if (fwriteBulkObject(fp,field) == -1) return -1;
8594 if (fwriteBulkObject(fp,val) == -1) return -1;
8595 }
8596 dictReleaseIterator(di);
8597 }
9d65a1bb 8598 } else {
f83c6cb5 8599 redisPanic("Unknown object type");
9d65a1bb 8600 }
8601 /* Save the expire time */
8602 if (expiretime != -1) {
e96e4fbf 8603 char cmd[]="*3\r\n$8\r\nEXPIREAT\r\n";
9d65a1bb 8604 /* If this key is already expired skip it */
8605 if (expiretime < now) continue;
8606 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9c8e3cee 8607 if (fwriteBulkObject(fp,key) == 0) goto werr;
9d65a1bb 8608 if (fwriteBulkLong(fp,expiretime) == 0) goto werr;
8609 }
b9bc0eef 8610 if (swapped) decrRefCount(o);
9d65a1bb 8611 }
8612 dictReleaseIterator(di);
8613 }
8614
8615 /* Make sure data will not remain on the OS's output buffers */
8616 fflush(fp);
8617 fsync(fileno(fp));
8618 fclose(fp);
e0a62c7f 8619
9d65a1bb 8620 /* Use RENAME to make sure the DB file is changed atomically only
8621 * if the generate DB file is ok. */
8622 if (rename(tmpfile,filename) == -1) {
8623 redisLog(REDIS_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno));
8624 unlink(tmpfile);
8625 return REDIS_ERR;
8626 }
8627 redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed");
8628 return REDIS_OK;
8629
8630werr:
8631 fclose(fp);
8632 unlink(tmpfile);
e96e4fbf 8633 redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
9d65a1bb 8634 if (di) dictReleaseIterator(di);
8635 return REDIS_ERR;
8636}
8637
8638/* This is how rewriting of the append only file in background works:
8639 *
8640 * 1) The user calls BGREWRITEAOF
8641 * 2) Redis calls this function, that forks():
8642 * 2a) the child rewrite the append only file in a temp file.
8643 * 2b) the parent accumulates differences in server.bgrewritebuf.
8644 * 3) When the child finished '2a' exists.
8645 * 4) The parent will trap the exit code, if it's OK, will append the
8646 * data accumulated into server.bgrewritebuf into the temp file, and
8647 * finally will rename(2) the temp file in the actual file name.
8648 * The the new file is reopened as the new append only file. Profit!
8649 */
8650static int rewriteAppendOnlyFileBackground(void) {
8651 pid_t childpid;
8652
8653 if (server.bgrewritechildpid != -1) return REDIS_ERR;
054e426d 8654 if (server.vm_enabled) waitEmptyIOJobsQueue();
9d65a1bb 8655 if ((childpid = fork()) == 0) {
8656 /* Child */
8657 char tmpfile[256];
9d65a1bb 8658
054e426d 8659 if (server.vm_enabled) vmReopenSwapFile();
8660 close(server.fd);
9d65a1bb 8661 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
8662 if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
478c2c6f 8663 _exit(0);
9d65a1bb 8664 } else {
478c2c6f 8665 _exit(1);
9d65a1bb 8666 }
8667 } else {
8668 /* Parent */
8669 if (childpid == -1) {
8670 redisLog(REDIS_WARNING,
8671 "Can't rewrite append only file in background: fork: %s",
8672 strerror(errno));
8673 return REDIS_ERR;
8674 }
8675 redisLog(REDIS_NOTICE,
8676 "Background append only file rewriting started by pid %d",childpid);
8677 server.bgrewritechildpid = childpid;
884d4b39 8678 updateDictResizePolicy();
85a83172 8679 /* We set appendseldb to -1 in order to force the next call to the
8680 * feedAppendOnlyFile() to issue a SELECT command, so the differences
8681 * accumulated by the parent into server.bgrewritebuf will start
8682 * with a SELECT statement and it will be safe to merge. */
8683 server.appendseldb = -1;
9d65a1bb 8684 return REDIS_OK;
8685 }
8686 return REDIS_OK; /* unreached */
8687}
8688
8689static void bgrewriteaofCommand(redisClient *c) {
8690 if (server.bgrewritechildpid != -1) {
8691 addReplySds(c,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
8692 return;
8693 }
8694 if (rewriteAppendOnlyFileBackground() == REDIS_OK) {
49b99ab4 8695 char *status = "+Background append only file rewriting started\r\n";
8696 addReplySds(c,sdsnew(status));
9d65a1bb 8697 } else {
8698 addReply(c,shared.err);
8699 }
8700}
8701
8702static void aofRemoveTempFile(pid_t childpid) {
8703 char tmpfile[256];
8704
8705 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid);
8706 unlink(tmpfile);
8707}
8708
996cb5f7 8709/* Virtual Memory is composed mainly of two subsystems:
8710 * - Blocking Virutal Memory
8711 * - Threaded Virtual Memory I/O
8712 * The two parts are not fully decoupled, but functions are split among two
8713 * different sections of the source code (delimited by comments) in order to
8714 * make more clear what functionality is about the blocking VM and what about
8715 * the threaded (not blocking) VM.
8716 *
8717 * Redis VM design:
8718 *
8719 * Redis VM is a blocking VM (one that blocks reading swapped values from
8720 * disk into memory when a value swapped out is needed in memory) that is made
8721 * unblocking by trying to examine the command argument vector in order to
8722 * load in background values that will likely be needed in order to exec
8723 * the command. The command is executed only once all the relevant keys
8724 * are loaded into memory.
8725 *
8726 * This basically is almost as simple of a blocking VM, but almost as parallel
8727 * as a fully non-blocking VM.
8728 */
8729
8730/* =================== Virtual Memory - Blocking Side ====================== */
054e426d 8731
75680a3c 8732static void vmInit(void) {
8733 off_t totsize;
996cb5f7 8734 int pipefds[2];
bcaa7a4f 8735 size_t stacksize;
8b5bb414 8736 struct flock fl;
75680a3c 8737
4ad37480 8738 if (server.vm_max_threads != 0)
8739 zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */
8740
054e426d 8741 redisLog(REDIS_NOTICE,"Using '%s' as swap file",server.vm_swap_file);
8b5bb414 8742 /* Try to open the old swap file, otherwise create it */
6fa987e3 8743 if ((server.vm_fp = fopen(server.vm_swap_file,"r+b")) == NULL) {
8744 server.vm_fp = fopen(server.vm_swap_file,"w+b");
8745 }
75680a3c 8746 if (server.vm_fp == NULL) {
6fa987e3 8747 redisLog(REDIS_WARNING,
8b5bb414 8748 "Can't open the swap file: %s. Exiting.",
6fa987e3 8749 strerror(errno));
75680a3c 8750 exit(1);
8751 }
8752 server.vm_fd = fileno(server.vm_fp);
8b5bb414 8753 /* Lock the swap file for writing, this is useful in order to avoid
8754 * another instance to use the same swap file for a config error. */
8755 fl.l_type = F_WRLCK;
8756 fl.l_whence = SEEK_SET;
8757 fl.l_start = fl.l_len = 0;
8758 if (fcntl(server.vm_fd,F_SETLK,&fl) == -1) {
8759 redisLog(REDIS_WARNING,
8760 "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));
8761 exit(1);
8762 }
8763 /* Initialize */
75680a3c 8764 server.vm_next_page = 0;
8765 server.vm_near_pages = 0;
7d98e08c 8766 server.vm_stats_used_pages = 0;
8767 server.vm_stats_swapped_objects = 0;
8768 server.vm_stats_swapouts = 0;
8769 server.vm_stats_swapins = 0;
75680a3c 8770 totsize = server.vm_pages*server.vm_page_size;
8771 redisLog(REDIS_NOTICE,"Allocating %lld bytes of swap file",totsize);
8772 if (ftruncate(server.vm_fd,totsize) == -1) {
8773 redisLog(REDIS_WARNING,"Can't ftruncate swap file: %s. Exiting.",
8774 strerror(errno));
8775 exit(1);
8776 } else {
8777 redisLog(REDIS_NOTICE,"Swap file allocated with success");
8778 }
7d30035d 8779 server.vm_bitmap = zmalloc((server.vm_pages+7)/8);
f870935d 8780 redisLog(REDIS_VERBOSE,"Allocated %lld bytes page table for %lld pages",
4ef8de8a 8781 (long long) (server.vm_pages+7)/8, server.vm_pages);
7d30035d 8782 memset(server.vm_bitmap,0,(server.vm_pages+7)/8);
92f8e882 8783
996cb5f7 8784 /* Initialize threaded I/O (used by Virtual Memory) */
8785 server.io_newjobs = listCreate();
8786 server.io_processing = listCreate();
8787 server.io_processed = listCreate();
d5d55fc3 8788 server.io_ready_clients = listCreate();
92f8e882 8789 pthread_mutex_init(&server.io_mutex,NULL);
a5819310 8790 pthread_mutex_init(&server.obj_freelist_mutex,NULL);
8791 pthread_mutex_init(&server.io_swapfile_mutex,NULL);
92f8e882 8792 server.io_active_threads = 0;
996cb5f7 8793 if (pipe(pipefds) == -1) {
8794 redisLog(REDIS_WARNING,"Unable to intialized VM: pipe(2): %s. Exiting."
8795 ,strerror(errno));
8796 exit(1);
8797 }
8798 server.io_ready_pipe_read = pipefds[0];
8799 server.io_ready_pipe_write = pipefds[1];
8800 redisAssert(anetNonBlock(NULL,server.io_ready_pipe_read) != ANET_ERR);
bcaa7a4f 8801 /* LZF requires a lot of stack */
8802 pthread_attr_init(&server.io_threads_attr);
8803 pthread_attr_getstacksize(&server.io_threads_attr, &stacksize);
8804 while (stacksize < REDIS_THREAD_STACK_SIZE) stacksize *= 2;
8805 pthread_attr_setstacksize(&server.io_threads_attr, stacksize);
b9bc0eef 8806 /* Listen for events in the threaded I/O pipe */
8807 if (aeCreateFileEvent(server.el, server.io_ready_pipe_read, AE_READABLE,
8808 vmThreadedIOCompletedJob, NULL) == AE_ERR)
8809 oom("creating file event");
75680a3c 8810}
8811
06224fec 8812/* Mark the page as used */
8813static void vmMarkPageUsed(off_t page) {
8814 off_t byte = page/8;
8815 int bit = page&7;
970e10bb 8816 redisAssert(vmFreePage(page) == 1);
06224fec 8817 server.vm_bitmap[byte] |= 1<<bit;
8818}
8819
8820/* Mark N contiguous pages as used, with 'page' being the first. */
8821static void vmMarkPagesUsed(off_t page, off_t count) {
8822 off_t j;
8823
8824 for (j = 0; j < count; j++)
7d30035d 8825 vmMarkPageUsed(page+j);
7d98e08c 8826 server.vm_stats_used_pages += count;
7c775e09 8827 redisLog(REDIS_DEBUG,"Mark USED pages: %lld pages at %lld\n",
8828 (long long)count, (long long)page);
06224fec 8829}
8830
8831/* Mark the page as free */
8832static void vmMarkPageFree(off_t page) {
8833 off_t byte = page/8;
8834 int bit = page&7;
970e10bb 8835 redisAssert(vmFreePage(page) == 0);
06224fec 8836 server.vm_bitmap[byte] &= ~(1<<bit);
8837}
8838
8839/* Mark N contiguous pages as free, with 'page' being the first. */
8840static void vmMarkPagesFree(off_t page, off_t count) {
8841 off_t j;
8842
8843 for (j = 0; j < count; j++)
7d30035d 8844 vmMarkPageFree(page+j);
7d98e08c 8845 server.vm_stats_used_pages -= count;
7c775e09 8846 redisLog(REDIS_DEBUG,"Mark FREE pages: %lld pages at %lld\n",
8847 (long long)count, (long long)page);
06224fec 8848}
8849
8850/* Test if the page is free */
8851static int vmFreePage(off_t page) {
8852 off_t byte = page/8;
8853 int bit = page&7;
7d30035d 8854 return (server.vm_bitmap[byte] & (1<<bit)) == 0;
06224fec 8855}
8856
8857/* Find N contiguous free pages storing the first page of the cluster in *first.
e0a62c7f 8858 * Returns REDIS_OK if it was able to find N contiguous pages, otherwise
3a66edc7 8859 * REDIS_ERR is returned.
06224fec 8860 *
8861 * This function uses a simple algorithm: we try to allocate
8862 * REDIS_VM_MAX_NEAR_PAGES sequentially, when we reach this limit we start
8863 * again from the start of the swap file searching for free spaces.
8864 *
8865 * If it looks pretty clear that there are no free pages near our offset
8866 * we try to find less populated places doing a forward jump of
8867 * REDIS_VM_MAX_RANDOM_JUMP, then we start scanning again a few pages
8868 * without hurry, and then we jump again and so forth...
e0a62c7f 8869 *
06224fec 8870 * This function can be improved using a free list to avoid to guess
8871 * too much, since we could collect data about freed pages.
8872 *
8873 * note: I implemented this function just after watching an episode of
8874 * Battlestar Galactica, where the hybrid was continuing to say "JUMP!"
8875 */
c7df85a4 8876static int vmFindContiguousPages(off_t *first, off_t n) {
06224fec 8877 off_t base, offset = 0, since_jump = 0, numfree = 0;
8878
8879 if (server.vm_near_pages == REDIS_VM_MAX_NEAR_PAGES) {
8880 server.vm_near_pages = 0;
8881 server.vm_next_page = 0;
8882 }
8883 server.vm_near_pages++; /* Yet another try for pages near to the old ones */
8884 base = server.vm_next_page;
8885
8886 while(offset < server.vm_pages) {
8887 off_t this = base+offset;
8888
8889 /* If we overflow, restart from page zero */
8890 if (this >= server.vm_pages) {
8891 this -= server.vm_pages;
8892 if (this == 0) {
8893 /* Just overflowed, what we found on tail is no longer
8894 * interesting, as it's no longer contiguous. */
8895 numfree = 0;
8896 }
8897 }
8898 if (vmFreePage(this)) {
8899 /* This is a free page */
8900 numfree++;
8901 /* Already got N free pages? Return to the caller, with success */
8902 if (numfree == n) {
7d30035d 8903 *first = this-(n-1);
8904 server.vm_next_page = this+1;
7c775e09 8905 redisLog(REDIS_DEBUG, "FOUND CONTIGUOUS PAGES: %lld pages at %lld\n", (long long) n, (long long) *first);
3a66edc7 8906 return REDIS_OK;
06224fec 8907 }
8908 } else {
8909 /* The current one is not a free page */
8910 numfree = 0;
8911 }
8912
8913 /* Fast-forward if the current page is not free and we already
8914 * searched enough near this place. */
8915 since_jump++;
8916 if (!numfree && since_jump >= REDIS_VM_MAX_RANDOM_JUMP/4) {
8917 offset += random() % REDIS_VM_MAX_RANDOM_JUMP;
8918 since_jump = 0;
8919 /* Note that even if we rewind after the jump, we are don't need
8920 * to make sure numfree is set to zero as we only jump *if* it
8921 * is set to zero. */
8922 } else {
8923 /* Otherwise just check the next page */
8924 offset++;
8925 }
8926 }
3a66edc7 8927 return REDIS_ERR;
8928}
8929
a5819310 8930/* Write the specified object at the specified page of the swap file */
8931static int vmWriteObjectOnSwap(robj *o, off_t page) {
8932 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
8933 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
8934 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8935 redisLog(REDIS_WARNING,
9ebed7cf 8936 "Critical VM problem in vmWriteObjectOnSwap(): can't seek: %s",
a5819310 8937 strerror(errno));
8938 return REDIS_ERR;
8939 }
8940 rdbSaveObject(server.vm_fp,o);
ba76a8f9 8941 fflush(server.vm_fp);
a5819310 8942 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8943 return REDIS_OK;
8944}
8945
3a66edc7 8946/* Swap the 'val' object relative to 'key' into disk. Store all the information
8947 * needed to later retrieve the object into the key object.
8948 * If we can't find enough contiguous empty pages to swap the object on disk
8949 * REDIS_ERR is returned. */
a69a0c9c 8950static int vmSwapObjectBlocking(robj *key, robj *val) {
b9bc0eef 8951 off_t pages = rdbSavedObjectPages(val,NULL);
3a66edc7 8952 off_t page;
8953
8954 assert(key->storage == REDIS_VM_MEMORY);
4ef8de8a 8955 assert(key->refcount == 1);
3a66edc7 8956 if (vmFindContiguousPages(&page,pages) == REDIS_ERR) return REDIS_ERR;
a5819310 8957 if (vmWriteObjectOnSwap(val,page) == REDIS_ERR) return REDIS_ERR;
3a66edc7 8958 key->vm.page = page;
8959 key->vm.usedpages = pages;
8960 key->storage = REDIS_VM_SWAPPED;
d894161b 8961 key->vtype = val->type;
3a66edc7 8962 decrRefCount(val); /* Deallocate the object from memory. */
8963 vmMarkPagesUsed(page,pages);
7d30035d 8964 redisLog(REDIS_DEBUG,"VM: object %s swapped out at %lld (%lld pages)",
8965 (unsigned char*) key->ptr,
8966 (unsigned long long) page, (unsigned long long) pages);
7d98e08c 8967 server.vm_stats_swapped_objects++;
8968 server.vm_stats_swapouts++;
3a66edc7 8969 return REDIS_OK;
8970}
8971
a5819310 8972static robj *vmReadObjectFromSwap(off_t page, int type) {
8973 robj *o;
3a66edc7 8974
a5819310 8975 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
8976 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
3a66edc7 8977 redisLog(REDIS_WARNING,
d5d55fc3 8978 "Unrecoverable VM problem in vmReadObjectFromSwap(): can't seek: %s",
3a66edc7 8979 strerror(errno));
478c2c6f 8980 _exit(1);
3a66edc7 8981 }
a5819310 8982 o = rdbLoadObject(type,server.vm_fp);
8983 if (o == NULL) {
d5d55fc3 8984 redisLog(REDIS_WARNING, "Unrecoverable VM problem in vmReadObjectFromSwap(): can't load object from swap file: %s", strerror(errno));
478c2c6f 8985 _exit(1);
3a66edc7 8986 }
a5819310 8987 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8988 return o;
8989}
8990
8991/* Load the value object relative to the 'key' object from swap to memory.
8992 * The newly allocated object is returned.
8993 *
8994 * If preview is true the unserialized object is returned to the caller but
8995 * no changes are made to the key object, nor the pages are marked as freed */
8996static robj *vmGenericLoadObject(robj *key, int preview) {
8997 robj *val;
8998
d5d55fc3 8999 redisAssert(key->storage == REDIS_VM_SWAPPED || key->storage == REDIS_VM_LOADING);
a5819310 9000 val = vmReadObjectFromSwap(key->vm.page,key->vtype);
7e69548d 9001 if (!preview) {
9002 key->storage = REDIS_VM_MEMORY;
9003 key->vm.atime = server.unixtime;
9004 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
9005 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk",
9006 (unsigned char*) key->ptr);
7d98e08c 9007 server.vm_stats_swapped_objects--;
38aba9a1 9008 } else {
9009 redisLog(REDIS_DEBUG, "VM: object %s previewed from disk",
9010 (unsigned char*) key->ptr);
7e69548d 9011 }
7d98e08c 9012 server.vm_stats_swapins++;
3a66edc7 9013 return val;
06224fec 9014}
9015
7e69548d 9016/* Plain object loading, from swap to memory */
9017static robj *vmLoadObject(robj *key) {
996cb5f7 9018 /* If we are loading the object in background, stop it, we
9019 * need to load this object synchronously ASAP. */
9020 if (key->storage == REDIS_VM_LOADING)
9021 vmCancelThreadedIOJob(key);
7e69548d 9022 return vmGenericLoadObject(key,0);
9023}
9024
9025/* Just load the value on disk, without to modify the key.
9026 * This is useful when we want to perform some operation on the value
9027 * without to really bring it from swap to memory, like while saving the
9028 * dataset or rewriting the append only log. */
9029static robj *vmPreviewObject(robj *key) {
9030 return vmGenericLoadObject(key,1);
9031}
9032
4ef8de8a 9033/* How a good candidate is this object for swapping?
9034 * The better candidate it is, the greater the returned value.
9035 *
9036 * Currently we try to perform a fast estimation of the object size in
9037 * memory, and combine it with aging informations.
9038 *
9039 * Basically swappability = idle-time * log(estimated size)
9040 *
9041 * Bigger objects are preferred over smaller objects, but not
9042 * proportionally, this is why we use the logarithm. This algorithm is
9043 * just a first try and will probably be tuned later. */
9044static double computeObjectSwappability(robj *o) {
9045 time_t age = server.unixtime - o->vm.atime;
9046 long asize = 0;
9047 list *l;
9048 dict *d;
9049 struct dictEntry *de;
9050 int z;
9051
9052 if (age <= 0) return 0;
9053 switch(o->type) {
9054 case REDIS_STRING:
9055 if (o->encoding != REDIS_ENCODING_RAW) {
9056 asize = sizeof(*o);
9057 } else {
9058 asize = sdslen(o->ptr)+sizeof(*o)+sizeof(long)*2;
9059 }
9060 break;
9061 case REDIS_LIST:
9062 l = o->ptr;
9063 listNode *ln = listFirst(l);
9064
9065 asize = sizeof(list);
9066 if (ln) {
9067 robj *ele = ln->value;
9068 long elesize;
9069
9070 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9071 (sizeof(*o)+sdslen(ele->ptr)) :
9072 sizeof(*o);
9073 asize += (sizeof(listNode)+elesize)*listLength(l);
9074 }
9075 break;
9076 case REDIS_SET:
9077 case REDIS_ZSET:
9078 z = (o->type == REDIS_ZSET);
9079 d = z ? ((zset*)o->ptr)->dict : o->ptr;
9080
9081 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9082 if (z) asize += sizeof(zset)-sizeof(dict);
9083 if (dictSize(d)) {
9084 long elesize;
9085 robj *ele;
9086
9087 de = dictGetRandomKey(d);
9088 ele = dictGetEntryKey(de);
9089 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9090 (sizeof(*o)+sdslen(ele->ptr)) :
9091 sizeof(*o);
9092 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9093 if (z) asize += sizeof(zskiplistNode)*dictSize(d);
9094 }
9095 break;
a97b9060 9096 case REDIS_HASH:
9097 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
9098 unsigned char *p = zipmapRewind((unsigned char*)o->ptr);
9099 unsigned int len = zipmapLen((unsigned char*)o->ptr);
9100 unsigned int klen, vlen;
9101 unsigned char *key, *val;
9102
9103 if ((p = zipmapNext(p,&key,&klen,&val,&vlen)) == NULL) {
9104 klen = 0;
9105 vlen = 0;
9106 }
9107 asize = len*(klen+vlen+3);
9108 } else if (o->encoding == REDIS_ENCODING_HT) {
9109 d = o->ptr;
9110 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9111 if (dictSize(d)) {
9112 long elesize;
9113 robj *ele;
9114
9115 de = dictGetRandomKey(d);
9116 ele = dictGetEntryKey(de);
9117 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9118 (sizeof(*o)+sdslen(ele->ptr)) :
9119 sizeof(*o);
9120 ele = dictGetEntryVal(de);
9121 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9122 (sizeof(*o)+sdslen(ele->ptr)) :
9123 sizeof(*o);
9124 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9125 }
9126 }
9127 break;
4ef8de8a 9128 }
c8c72447 9129 return (double)age*log(1+asize);
4ef8de8a 9130}
9131
9132/* Try to swap an object that's a good candidate for swapping.
9133 * Returns REDIS_OK if the object was swapped, REDIS_ERR if it's not possible
a69a0c9c 9134 * to swap any object at all.
9135 *
9136 * If 'usethreaded' is true, Redis will try to swap the object in background
9137 * using I/O threads. */
9138static int vmSwapOneObject(int usethreads) {
4ef8de8a 9139 int j, i;
9140 struct dictEntry *best = NULL;
9141 double best_swappability = 0;
b9bc0eef 9142 redisDb *best_db = NULL;
4ef8de8a 9143 robj *key, *val;
9144
9145 for (j = 0; j < server.dbnum; j++) {
9146 redisDb *db = server.db+j;
b72f6a4b 9147 /* Why maxtries is set to 100?
9148 * Because this way (usually) we'll find 1 object even if just 1% - 2%
9149 * are swappable objects */
b0d8747d 9150 int maxtries = 100;
4ef8de8a 9151
9152 if (dictSize(db->dict) == 0) continue;
9153 for (i = 0; i < 5; i++) {
9154 dictEntry *de;
9155 double swappability;
9156
e3cadb8a 9157 if (maxtries) maxtries--;
4ef8de8a 9158 de = dictGetRandomKey(db->dict);
9159 key = dictGetEntryKey(de);
9160 val = dictGetEntryVal(de);
1064ef87 9161 /* Only swap objects that are currently in memory.
9162 *
9163 * Also don't swap shared objects if threaded VM is on, as we
9164 * try to ensure that the main thread does not touch the
9165 * object while the I/O thread is using it, but we can't
9166 * control other keys without adding additional mutex. */
9167 if (key->storage != REDIS_VM_MEMORY ||
9168 (server.vm_max_threads != 0 && val->refcount != 1)) {
e3cadb8a 9169 if (maxtries) i--; /* don't count this try */
9170 continue;
9171 }
4ef8de8a 9172 swappability = computeObjectSwappability(val);
9173 if (!best || swappability > best_swappability) {
9174 best = de;
9175 best_swappability = swappability;
b9bc0eef 9176 best_db = db;
4ef8de8a 9177 }
9178 }
9179 }
7c775e09 9180 if (best == NULL) return REDIS_ERR;
4ef8de8a 9181 key = dictGetEntryKey(best);
9182 val = dictGetEntryVal(best);
9183
e3cadb8a 9184 redisLog(REDIS_DEBUG,"Key with best swappability: %s, %f",
4ef8de8a 9185 key->ptr, best_swappability);
9186
9187 /* Unshare the key if needed */
9188 if (key->refcount > 1) {
9189 robj *newkey = dupStringObject(key);
9190 decrRefCount(key);
9191 key = dictGetEntryKey(best) = newkey;
9192 }
9193 /* Swap it */
a69a0c9c 9194 if (usethreads) {
b9bc0eef 9195 vmSwapObjectThreaded(key,val,best_db);
4ef8de8a 9196 return REDIS_OK;
9197 } else {
a69a0c9c 9198 if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
9199 dictGetEntryVal(best) = NULL;
9200 return REDIS_OK;
9201 } else {
9202 return REDIS_ERR;
9203 }
4ef8de8a 9204 }
9205}
9206
a69a0c9c 9207static int vmSwapOneObjectBlocking() {
9208 return vmSwapOneObject(0);
9209}
9210
9211static int vmSwapOneObjectThreaded() {
9212 return vmSwapOneObject(1);
9213}
9214
7e69548d 9215/* Return true if it's safe to swap out objects in a given moment.
9216 * Basically we don't want to swap objects out while there is a BGSAVE
9217 * or a BGAEOREWRITE running in backgroud. */
9218static int vmCanSwapOut(void) {
9219 return (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1);
9220}
9221
1b03836c 9222/* Delete a key if swapped. Returns 1 if the key was found, was swapped
9223 * and was deleted. Otherwise 0 is returned. */
9224static int deleteIfSwapped(redisDb *db, robj *key) {
9225 dictEntry *de;
9226 robj *foundkey;
9227
9228 if ((de = dictFind(db->dict,key)) == NULL) return 0;
9229 foundkey = dictGetEntryKey(de);
9230 if (foundkey->storage == REDIS_VM_MEMORY) return 0;
9231 deleteKey(db,key);
9232 return 1;
9233}
9234
996cb5f7 9235/* =================== Virtual Memory - Threaded I/O ======================= */
9236
b9bc0eef 9237static void freeIOJob(iojob *j) {
d5d55fc3 9238 if ((j->type == REDIS_IOJOB_PREPARE_SWAP ||
9239 j->type == REDIS_IOJOB_DO_SWAP ||
9240 j->type == REDIS_IOJOB_LOAD) && j->val != NULL)
b9bc0eef 9241 decrRefCount(j->val);
78ebe4c8 9242 /* We don't decrRefCount the j->key field as we did't incremented
9243 * the count creating IO Jobs. This is because the key field here is
9244 * just used as an indentifier and if a key is removed the Job should
9245 * never be touched again. */
b9bc0eef 9246 zfree(j);
9247}
9248
996cb5f7 9249/* Every time a thread finished a Job, it writes a byte into the write side
9250 * of an unix pipe in order to "awake" the main thread, and this function
9251 * is called. */
9252static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata,
9253 int mask)
9254{
9255 char buf[1];
b0d8747d 9256 int retval, processed = 0, toprocess = -1, trytoswap = 1;
996cb5f7 9257 REDIS_NOTUSED(el);
9258 REDIS_NOTUSED(mask);
9259 REDIS_NOTUSED(privdata);
9260
9261 /* For every byte we read in the read side of the pipe, there is one
9262 * I/O job completed to process. */
9263 while((retval = read(fd,buf,1)) == 1) {
b9bc0eef 9264 iojob *j;
9265 listNode *ln;
9266 robj *key;
9267 struct dictEntry *de;
9268
996cb5f7 9269 redisLog(REDIS_DEBUG,"Processing I/O completed job");
b9bc0eef 9270
9271 /* Get the processed element (the oldest one) */
9272 lockThreadedIO();
1064ef87 9273 assert(listLength(server.io_processed) != 0);
f6c0bba8 9274 if (toprocess == -1) {
9275 toprocess = (listLength(server.io_processed)*REDIS_MAX_COMPLETED_JOBS_PROCESSED)/100;
9276 if (toprocess <= 0) toprocess = 1;
9277 }
b9bc0eef 9278 ln = listFirst(server.io_processed);
9279 j = ln->value;
9280 listDelNode(server.io_processed,ln);
9281 unlockThreadedIO();
9282 /* If this job is marked as canceled, just ignore it */
9283 if (j->canceled) {
9284 freeIOJob(j);
9285 continue;
9286 }
9287 /* Post process it in the main thread, as there are things we
9288 * can do just here to avoid race conditions and/or invasive locks */
6c96ba7d 9289 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 9290 de = dictFind(j->db->dict,j->key);
9291 assert(de != NULL);
9292 key = dictGetEntryKey(de);
9293 if (j->type == REDIS_IOJOB_LOAD) {
d5d55fc3 9294 redisDb *db;
9295
b9bc0eef 9296 /* Key loaded, bring it at home */
9297 key->storage = REDIS_VM_MEMORY;
9298 key->vm.atime = server.unixtime;
9299 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
9300 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk (threaded)",
9301 (unsigned char*) key->ptr);
9302 server.vm_stats_swapped_objects--;
9303 server.vm_stats_swapins++;
d5d55fc3 9304 dictGetEntryVal(de) = j->val;
9305 incrRefCount(j->val);
9306 db = j->db;
b9bc0eef 9307 freeIOJob(j);
d5d55fc3 9308 /* Handle clients waiting for this key to be loaded. */
9309 handleClientsBlockedOnSwappedKey(db,key);
b9bc0eef 9310 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
9311 /* Now we know the amount of pages required to swap this object.
9312 * Let's find some space for it, and queue this task again
9313 * rebranded as REDIS_IOJOB_DO_SWAP. */
054e426d 9314 if (!vmCanSwapOut() ||
9315 vmFindContiguousPages(&j->page,j->pages) == REDIS_ERR)
9316 {
9317 /* Ooops... no space or we can't swap as there is
9318 * a fork()ed Redis trying to save stuff on disk. */
b9bc0eef 9319 freeIOJob(j);
054e426d 9320 key->storage = REDIS_VM_MEMORY; /* undo operation */
b9bc0eef 9321 } else {
c7df85a4 9322 /* Note that we need to mark this pages as used now,
9323 * if the job will be canceled, we'll mark them as freed
9324 * again. */
9325 vmMarkPagesUsed(j->page,j->pages);
b9bc0eef 9326 j->type = REDIS_IOJOB_DO_SWAP;
9327 lockThreadedIO();
9328 queueIOJob(j);
9329 unlockThreadedIO();
9330 }
9331 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
9332 robj *val;
9333
9334 /* Key swapped. We can finally free some memory. */
6c96ba7d 9335 if (key->storage != REDIS_VM_SWAPPING) {
9336 printf("key->storage: %d\n",key->storage);
9337 printf("key->name: %s\n",(char*)key->ptr);
9338 printf("key->refcount: %d\n",key->refcount);
9339 printf("val: %p\n",(void*)j->val);
9340 printf("val->type: %d\n",j->val->type);
9341 printf("val->ptr: %s\n",(char*)j->val->ptr);
9342 }
9343 redisAssert(key->storage == REDIS_VM_SWAPPING);
b9bc0eef 9344 val = dictGetEntryVal(de);
9345 key->vm.page = j->page;
9346 key->vm.usedpages = j->pages;
9347 key->storage = REDIS_VM_SWAPPED;
9348 key->vtype = j->val->type;
9349 decrRefCount(val); /* Deallocate the object from memory. */
f11b8647 9350 dictGetEntryVal(de) = NULL;
b9bc0eef 9351 redisLog(REDIS_DEBUG,
9352 "VM: object %s swapped out at %lld (%lld pages) (threaded)",
9353 (unsigned char*) key->ptr,
9354 (unsigned long long) j->page, (unsigned long long) j->pages);
9355 server.vm_stats_swapped_objects++;
9356 server.vm_stats_swapouts++;
9357 freeIOJob(j);
f11b8647 9358 /* Put a few more swap requests in queue if we are still
9359 * out of memory */
b0d8747d 9360 if (trytoswap && vmCanSwapOut() &&
9361 zmalloc_used_memory() > server.vm_max_memory)
9362 {
f11b8647 9363 int more = 1;
9364 while(more) {
9365 lockThreadedIO();
9366 more = listLength(server.io_newjobs) <
9367 (unsigned) server.vm_max_threads;
9368 unlockThreadedIO();
9369 /* Don't waste CPU time if swappable objects are rare. */
b0d8747d 9370 if (vmSwapOneObjectThreaded() == REDIS_ERR) {
9371 trytoswap = 0;
9372 break;
9373 }
f11b8647 9374 }
9375 }
b9bc0eef 9376 }
c953f24b 9377 processed++;
f6c0bba8 9378 if (processed == toprocess) return;
996cb5f7 9379 }
9380 if (retval < 0 && errno != EAGAIN) {
9381 redisLog(REDIS_WARNING,
9382 "WARNING: read(2) error in vmThreadedIOCompletedJob() %s",
9383 strerror(errno));
9384 }
9385}
9386
9387static void lockThreadedIO(void) {
9388 pthread_mutex_lock(&server.io_mutex);
9389}
9390
9391static void unlockThreadedIO(void) {
9392 pthread_mutex_unlock(&server.io_mutex);
9393}
9394
9395/* Remove the specified object from the threaded I/O queue if still not
9396 * processed, otherwise make sure to flag it as canceled. */
9397static void vmCancelThreadedIOJob(robj *o) {
9398 list *lists[3] = {
6c96ba7d 9399 server.io_newjobs, /* 0 */
9400 server.io_processing, /* 1 */
9401 server.io_processed /* 2 */
996cb5f7 9402 };
9403 int i;
9404
9405 assert(o->storage == REDIS_VM_LOADING || o->storage == REDIS_VM_SWAPPING);
2e111efe 9406again:
996cb5f7 9407 lockThreadedIO();
9408 /* Search for a matching key in one of the queues */
9409 for (i = 0; i < 3; i++) {
9410 listNode *ln;
c7df85a4 9411 listIter li;
996cb5f7 9412
c7df85a4 9413 listRewind(lists[i],&li);
9414 while ((ln = listNext(&li)) != NULL) {
996cb5f7 9415 iojob *job = ln->value;
9416
6c96ba7d 9417 if (job->canceled) continue; /* Skip this, already canceled. */
78ebe4c8 9418 if (job->key == o) {
970e10bb 9419 redisLog(REDIS_DEBUG,"*** CANCELED %p (%s) (type %d) (LIST ID %d)\n",
9420 (void*)job, (char*)o->ptr, job->type, i);
427a2153 9421 /* Mark the pages as free since the swap didn't happened
9422 * or happened but is now discarded. */
970e10bb 9423 if (i != 1 && job->type == REDIS_IOJOB_DO_SWAP)
427a2153 9424 vmMarkPagesFree(job->page,job->pages);
9425 /* Cancel the job. It depends on the list the job is
9426 * living in. */
996cb5f7 9427 switch(i) {
9428 case 0: /* io_newjobs */
6c96ba7d 9429 /* If the job was yet not processed the best thing to do
996cb5f7 9430 * is to remove it from the queue at all */
6c96ba7d 9431 freeIOJob(job);
996cb5f7 9432 listDelNode(lists[i],ln);
9433 break;
9434 case 1: /* io_processing */
d5d55fc3 9435 /* Oh Shi- the thread is messing with the Job:
9436 *
9437 * Probably it's accessing the object if this is a
9438 * PREPARE_SWAP or DO_SWAP job.
9439 * If it's a LOAD job it may be reading from disk and
9440 * if we don't wait for the job to terminate before to
9441 * cancel it, maybe in a few microseconds data can be
9442 * corrupted in this pages. So the short story is:
9443 *
9444 * Better to wait for the job to move into the
9445 * next queue (processed)... */
9446
9447 /* We try again and again until the job is completed. */
9448 unlockThreadedIO();
9449 /* But let's wait some time for the I/O thread
9450 * to finish with this job. After all this condition
9451 * should be very rare. */
9452 usleep(1);
9453 goto again;
996cb5f7 9454 case 2: /* io_processed */
2e111efe 9455 /* The job was already processed, that's easy...
9456 * just mark it as canceled so that we'll ignore it
9457 * when processing completed jobs. */
996cb5f7 9458 job->canceled = 1;
9459 break;
9460 }
c7df85a4 9461 /* Finally we have to adjust the storage type of the object
9462 * in order to "UNDO" the operaiton. */
996cb5f7 9463 if (o->storage == REDIS_VM_LOADING)
9464 o->storage = REDIS_VM_SWAPPED;
9465 else if (o->storage == REDIS_VM_SWAPPING)
9466 o->storage = REDIS_VM_MEMORY;
9467 unlockThreadedIO();
9468 return;
9469 }
9470 }
9471 }
9472 unlockThreadedIO();
9473 assert(1 != 1); /* We should never reach this */
9474}
9475
b9bc0eef 9476static void *IOThreadEntryPoint(void *arg) {
9477 iojob *j;
9478 listNode *ln;
9479 REDIS_NOTUSED(arg);
9480
9481 pthread_detach(pthread_self());
9482 while(1) {
9483 /* Get a new job to process */
9484 lockThreadedIO();
9485 if (listLength(server.io_newjobs) == 0) {
9486 /* No new jobs in queue, exit. */
9ebed7cf 9487 redisLog(REDIS_DEBUG,"Thread %ld exiting, nothing to do",
9488 (long) pthread_self());
b9bc0eef 9489 server.io_active_threads--;
9490 unlockThreadedIO();
9491 return NULL;
9492 }
9493 ln = listFirst(server.io_newjobs);
9494 j = ln->value;
9495 listDelNode(server.io_newjobs,ln);
9496 /* Add the job in the processing queue */
9497 j->thread = pthread_self();
9498 listAddNodeTail(server.io_processing,j);
9499 ln = listLast(server.io_processing); /* We use ln later to remove it */
9500 unlockThreadedIO();
9ebed7cf 9501 redisLog(REDIS_DEBUG,"Thread %ld got a new job (type %d): %p about key '%s'",
9502 (long) pthread_self(), j->type, (void*)j, (char*)j->key->ptr);
b9bc0eef 9503
9504 /* Process the Job */
9505 if (j->type == REDIS_IOJOB_LOAD) {
d5d55fc3 9506 j->val = vmReadObjectFromSwap(j->page,j->key->vtype);
b9bc0eef 9507 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
9508 FILE *fp = fopen("/dev/null","w+");
9509 j->pages = rdbSavedObjectPages(j->val,fp);
9510 fclose(fp);
9511 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
a5819310 9512 if (vmWriteObjectOnSwap(j->val,j->page) == REDIS_ERR)
9513 j->canceled = 1;
b9bc0eef 9514 }
9515
9516 /* Done: insert the job into the processed queue */
9ebed7cf 9517 redisLog(REDIS_DEBUG,"Thread %ld completed the job: %p (key %s)",
9518 (long) pthread_self(), (void*)j, (char*)j->key->ptr);
b9bc0eef 9519 lockThreadedIO();
9520 listDelNode(server.io_processing,ln);
9521 listAddNodeTail(server.io_processed,j);
9522 unlockThreadedIO();
e0a62c7f 9523
b9bc0eef 9524 /* Signal the main thread there is new stuff to process */
9525 assert(write(server.io_ready_pipe_write,"x",1) == 1);
9526 }
9527 return NULL; /* never reached */
9528}
9529
9530static void spawnIOThread(void) {
9531 pthread_t thread;
478c2c6f 9532 sigset_t mask, omask;
a97b9060 9533 int err;
b9bc0eef 9534
478c2c6f 9535 sigemptyset(&mask);
9536 sigaddset(&mask,SIGCHLD);
9537 sigaddset(&mask,SIGHUP);
9538 sigaddset(&mask,SIGPIPE);
9539 pthread_sigmask(SIG_SETMASK, &mask, &omask);
a97b9060 9540 while ((err = pthread_create(&thread,&server.io_threads_attr,IOThreadEntryPoint,NULL)) != 0) {
9541 redisLog(REDIS_WARNING,"Unable to spawn an I/O thread: %s",
9542 strerror(err));
9543 usleep(1000000);
9544 }
478c2c6f 9545 pthread_sigmask(SIG_SETMASK, &omask, NULL);
b9bc0eef 9546 server.io_active_threads++;
9547}
9548
4ee9488d 9549/* We need to wait for the last thread to exit before we are able to
9550 * fork() in order to BGSAVE or BGREWRITEAOF. */
054e426d 9551static void waitEmptyIOJobsQueue(void) {
4ee9488d 9552 while(1) {
76b7233a 9553 int io_processed_len;
9554
4ee9488d 9555 lockThreadedIO();
054e426d 9556 if (listLength(server.io_newjobs) == 0 &&
9557 listLength(server.io_processing) == 0 &&
9558 server.io_active_threads == 0)
9559 {
4ee9488d 9560 unlockThreadedIO();
9561 return;
9562 }
76b7233a 9563 /* While waiting for empty jobs queue condition we post-process some
9564 * finshed job, as I/O threads may be hanging trying to write against
9565 * the io_ready_pipe_write FD but there are so much pending jobs that
9566 * it's blocking. */
9567 io_processed_len = listLength(server.io_processed);
4ee9488d 9568 unlockThreadedIO();
76b7233a 9569 if (io_processed_len) {
9570 vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read,NULL,0);
9571 usleep(1000); /* 1 millisecond */
9572 } else {
9573 usleep(10000); /* 10 milliseconds */
9574 }
4ee9488d 9575 }
9576}
9577
054e426d 9578static void vmReopenSwapFile(void) {
478c2c6f 9579 /* Note: we don't close the old one as we are in the child process
9580 * and don't want to mess at all with the original file object. */
054e426d 9581 server.vm_fp = fopen(server.vm_swap_file,"r+b");
9582 if (server.vm_fp == NULL) {
9583 redisLog(REDIS_WARNING,"Can't re-open the VM swap file: %s. Exiting.",
9584 server.vm_swap_file);
478c2c6f 9585 _exit(1);
054e426d 9586 }
9587 server.vm_fd = fileno(server.vm_fp);
9588}
9589
b9bc0eef 9590/* This function must be called while with threaded IO locked */
9591static void queueIOJob(iojob *j) {
6c96ba7d 9592 redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n",
9593 (void*)j, j->type, (char*)j->key->ptr);
b9bc0eef 9594 listAddNodeTail(server.io_newjobs,j);
9595 if (server.io_active_threads < server.vm_max_threads)
9596 spawnIOThread();
9597}
9598
9599static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db) {
9600 iojob *j;
e0a62c7f 9601
b9bc0eef 9602 assert(key->storage == REDIS_VM_MEMORY);
9603 assert(key->refcount == 1);
9604
9605 j = zmalloc(sizeof(*j));
9606 j->type = REDIS_IOJOB_PREPARE_SWAP;
9607 j->db = db;
78ebe4c8 9608 j->key = key;
b9bc0eef 9609 j->val = val;
9610 incrRefCount(val);
9611 j->canceled = 0;
9612 j->thread = (pthread_t) -1;
f11b8647 9613 key->storage = REDIS_VM_SWAPPING;
b9bc0eef 9614
9615 lockThreadedIO();
9616 queueIOJob(j);
9617 unlockThreadedIO();
9618 return REDIS_OK;
9619}
9620
b0d8747d 9621/* ============ Virtual Memory - Blocking clients on missing keys =========== */
9622
d5d55fc3 9623/* This function makes the clinet 'c' waiting for the key 'key' to be loaded.
9624 * If there is not already a job loading the key, it is craeted.
9625 * The key is added to the io_keys list in the client structure, and also
9626 * in the hash table mapping swapped keys to waiting clients, that is,
9627 * server.io_waited_keys. */
9628static int waitForSwappedKey(redisClient *c, robj *key) {
9629 struct dictEntry *de;
9630 robj *o;
9631 list *l;
9632
9633 /* If the key does not exist or is already in RAM we don't need to
9634 * block the client at all. */
9635 de = dictFind(c->db->dict,key);
9636 if (de == NULL) return 0;
9637 o = dictGetEntryKey(de);
9638 if (o->storage == REDIS_VM_MEMORY) {
9639 return 0;
9640 } else if (o->storage == REDIS_VM_SWAPPING) {
9641 /* We were swapping the key, undo it! */
9642 vmCancelThreadedIOJob(o);
9643 return 0;
9644 }
e0a62c7f 9645
d5d55fc3 9646 /* OK: the key is either swapped, or being loaded just now. */
9647
9648 /* Add the key to the list of keys this client is waiting for.
9649 * This maps clients to keys they are waiting for. */
9650 listAddNodeTail(c->io_keys,key);
9651 incrRefCount(key);
9652
9653 /* Add the client to the swapped keys => clients waiting map. */
9654 de = dictFind(c->db->io_keys,key);
9655 if (de == NULL) {
9656 int retval;
9657
9658 /* For every key we take a list of clients blocked for it */
9659 l = listCreate();
9660 retval = dictAdd(c->db->io_keys,key,l);
9661 incrRefCount(key);
9662 assert(retval == DICT_OK);
9663 } else {
9664 l = dictGetEntryVal(de);
9665 }
9666 listAddNodeTail(l,c);
9667
9668 /* Are we already loading the key from disk? If not create a job */
9669 if (o->storage == REDIS_VM_SWAPPED) {
9670 iojob *j;
9671
9672 o->storage = REDIS_VM_LOADING;
9673 j = zmalloc(sizeof(*j));
9674 j->type = REDIS_IOJOB_LOAD;
9675 j->db = c->db;
78ebe4c8 9676 j->key = o;
d5d55fc3 9677 j->key->vtype = o->vtype;
9678 j->page = o->vm.page;
9679 j->val = NULL;
9680 j->canceled = 0;
9681 j->thread = (pthread_t) -1;
9682 lockThreadedIO();
9683 queueIOJob(j);
9684 unlockThreadedIO();
9685 }
9686 return 1;
9687}
9688
6f078746
PN
9689/* Preload keys for any command with first, last and step values for
9690 * the command keys prototype, as defined in the command table. */
9691static void waitForMultipleSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
9692 int j, last;
9693 if (cmd->vm_firstkey == 0) return;
9694 last = cmd->vm_lastkey;
9695 if (last < 0) last = argc+last;
9696 for (j = cmd->vm_firstkey; j <= last; j += cmd->vm_keystep) {
9697 redisAssert(j < argc);
9698 waitForSwappedKey(c,argv[j]);
9699 }
9700}
9701
739ba0d2
PN
9702/* Preload keys needed for the ZUNION and ZINTER commands.
9703 * Note that the number of keys to preload is user-defined, so we need to
9704 * apply a sanity check against argc. */
ca1788b5 9705static void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
76583ea4 9706 int i, num;
ca1788b5 9707 REDIS_NOTUSED(cmd);
ca1788b5
PN
9708
9709 num = atoi(argv[2]->ptr);
739ba0d2 9710 if (num > (argc-3)) return;
76583ea4 9711 for (i = 0; i < num; i++) {
ca1788b5 9712 waitForSwappedKey(c,argv[3+i]);
76583ea4
PN
9713 }
9714}
9715
3805e04f
PN
9716/* Preload keys needed to execute the entire MULTI/EXEC block.
9717 *
9718 * This function is called by blockClientOnSwappedKeys when EXEC is issued,
9719 * and will block the client when any command requires a swapped out value. */
9720static void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
9721 int i, margc;
9722 struct redisCommand *mcmd;
9723 robj **margv;
9724 REDIS_NOTUSED(cmd);
9725 REDIS_NOTUSED(argc);
9726 REDIS_NOTUSED(argv);
9727
9728 if (!(c->flags & REDIS_MULTI)) return;
9729 for (i = 0; i < c->mstate.count; i++) {
9730 mcmd = c->mstate.commands[i].cmd;
9731 margc = c->mstate.commands[i].argc;
9732 margv = c->mstate.commands[i].argv;
9733
9734 if (mcmd->vm_preload_proc != NULL) {
9735 mcmd->vm_preload_proc(c,mcmd,margc,margv);
9736 } else {
9737 waitForMultipleSwappedKeys(c,mcmd,margc,margv);
9738 }
76583ea4
PN
9739 }
9740}
9741
b0d8747d 9742/* Is this client attempting to run a command against swapped keys?
d5d55fc3 9743 * If so, block it ASAP, load the keys in background, then resume it.
b0d8747d 9744 *
d5d55fc3 9745 * The important idea about this function is that it can fail! If keys will
9746 * still be swapped when the client is resumed, this key lookups will
9747 * just block loading keys from disk. In practical terms this should only
9748 * happen with SORT BY command or if there is a bug in this function.
9749 *
9750 * Return 1 if the client is marked as blocked, 0 if the client can
9751 * continue as the keys it is going to access appear to be in memory. */
0a6f3f0f 9752static int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) {
76583ea4 9753 if (cmd->vm_preload_proc != NULL) {
ca1788b5 9754 cmd->vm_preload_proc(c,cmd,c->argc,c->argv);
76583ea4 9755 } else {
6f078746 9756 waitForMultipleSwappedKeys(c,cmd,c->argc,c->argv);
76583ea4
PN
9757 }
9758
d5d55fc3 9759 /* If the client was blocked for at least one key, mark it as blocked. */
9760 if (listLength(c->io_keys)) {
9761 c->flags |= REDIS_IO_WAIT;
9762 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
9763 server.vm_blocked_clients++;
9764 return 1;
9765 } else {
9766 return 0;
9767 }
9768}
9769
9770/* Remove the 'key' from the list of blocked keys for a given client.
9771 *
9772 * The function returns 1 when there are no longer blocking keys after
9773 * the current one was removed (and the client can be unblocked). */
9774static int dontWaitForSwappedKey(redisClient *c, robj *key) {
9775 list *l;
9776 listNode *ln;
9777 listIter li;
9778 struct dictEntry *de;
9779
9780 /* Remove the key from the list of keys this client is waiting for. */
9781 listRewind(c->io_keys,&li);
9782 while ((ln = listNext(&li)) != NULL) {
bf028098 9783 if (equalStringObjects(ln->value,key)) {
d5d55fc3 9784 listDelNode(c->io_keys,ln);
9785 break;
9786 }
9787 }
9788 assert(ln != NULL);
9789
9790 /* Remove the client form the key => waiting clients map. */
9791 de = dictFind(c->db->io_keys,key);
9792 assert(de != NULL);
9793 l = dictGetEntryVal(de);
9794 ln = listSearchKey(l,c);
9795 assert(ln != NULL);
9796 listDelNode(l,ln);
9797 if (listLength(l) == 0)
9798 dictDelete(c->db->io_keys,key);
9799
9800 return listLength(c->io_keys) == 0;
9801}
9802
9803static void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key) {
9804 struct dictEntry *de;
9805 list *l;
9806 listNode *ln;
9807 int len;
9808
9809 de = dictFind(db->io_keys,key);
9810 if (!de) return;
9811
9812 l = dictGetEntryVal(de);
9813 len = listLength(l);
9814 /* Note: we can't use something like while(listLength(l)) as the list
9815 * can be freed by the calling function when we remove the last element. */
9816 while (len--) {
9817 ln = listFirst(l);
9818 redisClient *c = ln->value;
9819
9820 if (dontWaitForSwappedKey(c,key)) {
9821 /* Put the client in the list of clients ready to go as we
9822 * loaded all the keys about it. */
9823 listAddNodeTail(server.io_ready_clients,c);
9824 }
9825 }
b0d8747d 9826}
b0d8747d 9827
500ece7c 9828/* =========================== Remote Configuration ========================= */
9829
9830static void configSetCommand(redisClient *c) {
9831 robj *o = getDecodedObject(c->argv[3]);
9832 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
9833 zfree(server.dbfilename);
9834 server.dbfilename = zstrdup(o->ptr);
9835 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
9836 zfree(server.requirepass);
9837 server.requirepass = zstrdup(o->ptr);
9838 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
9839 zfree(server.masterauth);
9840 server.masterauth = zstrdup(o->ptr);
9841 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
9842 server.maxmemory = strtoll(o->ptr, NULL, 10);
1b677732 9843 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
9844 if (!strcasecmp(o->ptr,"no")) {
9845 server.appendfsync = APPENDFSYNC_NO;
9846 } else if (!strcasecmp(o->ptr,"everysec")) {
9847 server.appendfsync = APPENDFSYNC_EVERYSEC;
9848 } else if (!strcasecmp(o->ptr,"always")) {
9849 server.appendfsync = APPENDFSYNC_ALWAYS;
9850 } else {
9851 goto badfmt;
9852 }
a34e0a25 9853 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
9854 int vlen, j;
9855 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
9856
9857 /* Perform sanity check before setting the new config:
9858 * - Even number of args
9859 * - Seconds >= 1, changes >= 0 */
9860 if (vlen & 1) {
9861 sdsfreesplitres(v,vlen);
9862 goto badfmt;
9863 }
9864 for (j = 0; j < vlen; j++) {
9865 char *eptr;
9866 long val;
9867
9868 val = strtoll(v[j], &eptr, 10);
9869 if (eptr[0] != '\0' ||
9870 ((j & 1) == 0 && val < 1) ||
9871 ((j & 1) == 1 && val < 0)) {
9872 sdsfreesplitres(v,vlen);
9873 goto badfmt;
9874 }
9875 }
9876 /* Finally set the new config */
9877 resetServerSaveParams();
9878 for (j = 0; j < vlen; j += 2) {
9879 time_t seconds;
9880 int changes;
9881
9882 seconds = strtoll(v[j],NULL,10);
9883 changes = strtoll(v[j+1],NULL,10);
9884 appendServerSaveParams(seconds, changes);
9885 }
9886 sdsfreesplitres(v,vlen);
500ece7c 9887 } else {
9888 addReplySds(c,sdscatprintf(sdsempty(),
9889 "-ERR not supported CONFIG parameter %s\r\n",
9890 (char*)c->argv[2]->ptr));
9891 decrRefCount(o);
9892 return;
9893 }
9894 decrRefCount(o);
9895 addReply(c,shared.ok);
a34e0a25 9896 return;
9897
9898badfmt: /* Bad format errors */
9899 addReplySds(c,sdscatprintf(sdsempty(),
9900 "-ERR invalid argument '%s' for CONFIG SET '%s'\r\n",
9901 (char*)o->ptr,
9902 (char*)c->argv[2]->ptr));
9903 decrRefCount(o);
500ece7c 9904}
9905
9906static void configGetCommand(redisClient *c) {
9907 robj *o = getDecodedObject(c->argv[2]);
9908 robj *lenobj = createObject(REDIS_STRING,NULL);
9909 char *pattern = o->ptr;
9910 int matches = 0;
9911
9912 addReply(c,lenobj);
9913 decrRefCount(lenobj);
9914
9915 if (stringmatch(pattern,"dbfilename",0)) {
9916 addReplyBulkCString(c,"dbfilename");
9917 addReplyBulkCString(c,server.dbfilename);
9918 matches++;
9919 }
9920 if (stringmatch(pattern,"requirepass",0)) {
9921 addReplyBulkCString(c,"requirepass");
9922 addReplyBulkCString(c,server.requirepass);
9923 matches++;
9924 }
9925 if (stringmatch(pattern,"masterauth",0)) {
9926 addReplyBulkCString(c,"masterauth");
9927 addReplyBulkCString(c,server.masterauth);
9928 matches++;
9929 }
9930 if (stringmatch(pattern,"maxmemory",0)) {
9931 char buf[128];
9932
9933 snprintf(buf,128,"%llu\n",server.maxmemory);
9934 addReplyBulkCString(c,"maxmemory");
9935 addReplyBulkCString(c,buf);
9936 matches++;
9937 }
1b677732 9938 if (stringmatch(pattern,"appendfsync",0)) {
9939 char *policy;
9940
9941 switch(server.appendfsync) {
9942 case APPENDFSYNC_NO: policy = "no"; break;
9943 case APPENDFSYNC_EVERYSEC: policy = "everysec"; break;
9944 case APPENDFSYNC_ALWAYS: policy = "always"; break;
9945 default: policy = "unknown"; break; /* too harmless to panic */
9946 }
9947 addReplyBulkCString(c,"appendfsync");
9948 addReplyBulkCString(c,policy);
9949 matches++;
9950 }
a34e0a25 9951 if (stringmatch(pattern,"save",0)) {
9952 sds buf = sdsempty();
9953 int j;
9954
9955 for (j = 0; j < server.saveparamslen; j++) {
9956 buf = sdscatprintf(buf,"%ld %d",
9957 server.saveparams[j].seconds,
9958 server.saveparams[j].changes);
9959 if (j != server.saveparamslen-1)
9960 buf = sdscatlen(buf," ",1);
9961 }
9962 addReplyBulkCString(c,"save");
9963 addReplyBulkCString(c,buf);
9964 sdsfree(buf);
9965 matches++;
9966 }
500ece7c 9967 decrRefCount(o);
9968 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",matches*2);
9969}
9970
9971static void configCommand(redisClient *c) {
9972 if (!strcasecmp(c->argv[1]->ptr,"set")) {
9973 if (c->argc != 4) goto badarity;
9974 configSetCommand(c);
9975 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
9976 if (c->argc != 3) goto badarity;
9977 configGetCommand(c);
9978 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
9979 if (c->argc != 2) goto badarity;
9980 server.stat_numcommands = 0;
9981 server.stat_numconnections = 0;
9982 server.stat_expiredkeys = 0;
9983 server.stat_starttime = time(NULL);
9984 addReply(c,shared.ok);
9985 } else {
9986 addReplySds(c,sdscatprintf(sdsempty(),
9987 "-ERR CONFIG subcommand must be one of GET, SET, RESETSTAT\r\n"));
9988 }
9989 return;
9990
9991badarity:
9992 addReplySds(c,sdscatprintf(sdsempty(),
9993 "-ERR Wrong number of arguments for CONFIG %s\r\n",
9994 (char*) c->argv[1]->ptr));
9995}
9996
befec3cd 9997/* =========================== Pubsub implementation ======================== */
9998
ffc6b7f8 9999static void freePubsubPattern(void *p) {
10000 pubsubPattern *pat = p;
10001
10002 decrRefCount(pat->pattern);
10003 zfree(pat);
10004}
10005
10006static int listMatchPubsubPattern(void *a, void *b) {
10007 pubsubPattern *pa = a, *pb = b;
10008
10009 return (pa->client == pb->client) &&
bf028098 10010 (equalStringObjects(pa->pattern,pb->pattern));
ffc6b7f8 10011}
10012
10013/* Subscribe a client to a channel. Returns 1 if the operation succeeded, or
10014 * 0 if the client was already subscribed to that channel. */
10015static int pubsubSubscribeChannel(redisClient *c, robj *channel) {
befec3cd 10016 struct dictEntry *de;
10017 list *clients = NULL;
10018 int retval = 0;
10019
ffc6b7f8 10020 /* Add the channel to the client -> channels hash table */
10021 if (dictAdd(c->pubsub_channels,channel,NULL) == DICT_OK) {
befec3cd 10022 retval = 1;
ffc6b7f8 10023 incrRefCount(channel);
10024 /* Add the client to the channel -> list of clients hash table */
10025 de = dictFind(server.pubsub_channels,channel);
befec3cd 10026 if (de == NULL) {
10027 clients = listCreate();
ffc6b7f8 10028 dictAdd(server.pubsub_channels,channel,clients);
10029 incrRefCount(channel);
befec3cd 10030 } else {
10031 clients = dictGetEntryVal(de);
10032 }
10033 listAddNodeTail(clients,c);
10034 }
10035 /* Notify the client */
10036 addReply(c,shared.mbulk3);
10037 addReply(c,shared.subscribebulk);
ffc6b7f8 10038 addReplyBulk(c,channel);
10039 addReplyLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
befec3cd 10040 return retval;
10041}
10042
ffc6b7f8 10043/* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10044 * 0 if the client was not subscribed to the specified channel. */
10045static int pubsubUnsubscribeChannel(redisClient *c, robj *channel, int notify) {
befec3cd 10046 struct dictEntry *de;
10047 list *clients;
10048 listNode *ln;
10049 int retval = 0;
10050
ffc6b7f8 10051 /* Remove the channel from the client -> channels hash table */
10052 incrRefCount(channel); /* channel may be just a pointer to the same object
201037f5 10053 we have in the hash tables. Protect it... */
ffc6b7f8 10054 if (dictDelete(c->pubsub_channels,channel) == DICT_OK) {
befec3cd 10055 retval = 1;
ffc6b7f8 10056 /* Remove the client from the channel -> clients list hash table */
10057 de = dictFind(server.pubsub_channels,channel);
befec3cd 10058 assert(de != NULL);
10059 clients = dictGetEntryVal(de);
10060 ln = listSearchKey(clients,c);
10061 assert(ln != NULL);
10062 listDelNode(clients,ln);
ff767a75 10063 if (listLength(clients) == 0) {
10064 /* Free the list and associated hash entry at all if this was
10065 * the latest client, so that it will be possible to abuse
ffc6b7f8 10066 * Redis PUBSUB creating millions of channels. */
10067 dictDelete(server.pubsub_channels,channel);
ff767a75 10068 }
befec3cd 10069 }
10070 /* Notify the client */
10071 if (notify) {
10072 addReply(c,shared.mbulk3);
10073 addReply(c,shared.unsubscribebulk);
ffc6b7f8 10074 addReplyBulk(c,channel);
10075 addReplyLong(c,dictSize(c->pubsub_channels)+
10076 listLength(c->pubsub_patterns));
10077
10078 }
10079 decrRefCount(channel); /* it is finally safe to release it */
10080 return retval;
10081}
10082
10083/* Subscribe a client to a pattern. Returns 1 if the operation succeeded, or 0 if the clinet was already subscribed to that pattern. */
10084static int pubsubSubscribePattern(redisClient *c, robj *pattern) {
10085 int retval = 0;
10086
10087 if (listSearchKey(c->pubsub_patterns,pattern) == NULL) {
10088 retval = 1;
10089 pubsubPattern *pat;
10090 listAddNodeTail(c->pubsub_patterns,pattern);
10091 incrRefCount(pattern);
10092 pat = zmalloc(sizeof(*pat));
10093 pat->pattern = getDecodedObject(pattern);
10094 pat->client = c;
10095 listAddNodeTail(server.pubsub_patterns,pat);
10096 }
10097 /* Notify the client */
10098 addReply(c,shared.mbulk3);
10099 addReply(c,shared.psubscribebulk);
10100 addReplyBulk(c,pattern);
10101 addReplyLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
10102 return retval;
10103}
10104
10105/* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10106 * 0 if the client was not subscribed to the specified channel. */
10107static int pubsubUnsubscribePattern(redisClient *c, robj *pattern, int notify) {
10108 listNode *ln;
10109 pubsubPattern pat;
10110 int retval = 0;
10111
10112 incrRefCount(pattern); /* Protect the object. May be the same we remove */
10113 if ((ln = listSearchKey(c->pubsub_patterns,pattern)) != NULL) {
10114 retval = 1;
10115 listDelNode(c->pubsub_patterns,ln);
10116 pat.client = c;
10117 pat.pattern = pattern;
10118 ln = listSearchKey(server.pubsub_patterns,&pat);
10119 listDelNode(server.pubsub_patterns,ln);
10120 }
10121 /* Notify the client */
10122 if (notify) {
10123 addReply(c,shared.mbulk3);
10124 addReply(c,shared.punsubscribebulk);
10125 addReplyBulk(c,pattern);
10126 addReplyLong(c,dictSize(c->pubsub_channels)+
10127 listLength(c->pubsub_patterns));
befec3cd 10128 }
ffc6b7f8 10129 decrRefCount(pattern);
befec3cd 10130 return retval;
10131}
10132
ffc6b7f8 10133/* Unsubscribe from all the channels. Return the number of channels the
10134 * client was subscribed from. */
10135static int pubsubUnsubscribeAllChannels(redisClient *c, int notify) {
10136 dictIterator *di = dictGetIterator(c->pubsub_channels);
befec3cd 10137 dictEntry *de;
10138 int count = 0;
10139
10140 while((de = dictNext(di)) != NULL) {
ffc6b7f8 10141 robj *channel = dictGetEntryKey(de);
befec3cd 10142
ffc6b7f8 10143 count += pubsubUnsubscribeChannel(c,channel,notify);
befec3cd 10144 }
10145 dictReleaseIterator(di);
10146 return count;
10147}
10148
ffc6b7f8 10149/* Unsubscribe from all the patterns. Return the number of patterns the
10150 * client was subscribed from. */
10151static int pubsubUnsubscribeAllPatterns(redisClient *c, int notify) {
10152 listNode *ln;
10153 listIter li;
10154 int count = 0;
10155
10156 listRewind(c->pubsub_patterns,&li);
10157 while ((ln = listNext(&li)) != NULL) {
10158 robj *pattern = ln->value;
10159
10160 count += pubsubUnsubscribePattern(c,pattern,notify);
10161 }
10162 return count;
10163}
10164
befec3cd 10165/* Publish a message */
ffc6b7f8 10166static int pubsubPublishMessage(robj *channel, robj *message) {
befec3cd 10167 int receivers = 0;
10168 struct dictEntry *de;
ffc6b7f8 10169 listNode *ln;
10170 listIter li;
befec3cd 10171
ffc6b7f8 10172 /* Send to clients listening for that channel */
10173 de = dictFind(server.pubsub_channels,channel);
befec3cd 10174 if (de) {
10175 list *list = dictGetEntryVal(de);
10176 listNode *ln;
10177 listIter li;
10178
10179 listRewind(list,&li);
10180 while ((ln = listNext(&li)) != NULL) {
10181 redisClient *c = ln->value;
10182
10183 addReply(c,shared.mbulk3);
10184 addReply(c,shared.messagebulk);
ffc6b7f8 10185 addReplyBulk(c,channel);
befec3cd 10186 addReplyBulk(c,message);
10187 receivers++;
10188 }
10189 }
ffc6b7f8 10190 /* Send to clients listening to matching channels */
10191 if (listLength(server.pubsub_patterns)) {
10192 listRewind(server.pubsub_patterns,&li);
10193 channel = getDecodedObject(channel);
10194 while ((ln = listNext(&li)) != NULL) {
10195 pubsubPattern *pat = ln->value;
10196
10197 if (stringmatchlen((char*)pat->pattern->ptr,
10198 sdslen(pat->pattern->ptr),
10199 (char*)channel->ptr,
10200 sdslen(channel->ptr),0)) {
c8d0ea0e 10201 addReply(pat->client,shared.mbulk4);
10202 addReply(pat->client,shared.pmessagebulk);
10203 addReplyBulk(pat->client,pat->pattern);
ffc6b7f8 10204 addReplyBulk(pat->client,channel);
10205 addReplyBulk(pat->client,message);
10206 receivers++;
10207 }
10208 }
10209 decrRefCount(channel);
10210 }
befec3cd 10211 return receivers;
10212}
10213
10214static void subscribeCommand(redisClient *c) {
10215 int j;
10216
10217 for (j = 1; j < c->argc; j++)
ffc6b7f8 10218 pubsubSubscribeChannel(c,c->argv[j]);
befec3cd 10219}
10220
10221static void unsubscribeCommand(redisClient *c) {
10222 if (c->argc == 1) {
ffc6b7f8 10223 pubsubUnsubscribeAllChannels(c,1);
10224 return;
10225 } else {
10226 int j;
10227
10228 for (j = 1; j < c->argc; j++)
10229 pubsubUnsubscribeChannel(c,c->argv[j],1);
10230 }
10231}
10232
10233static void psubscribeCommand(redisClient *c) {
10234 int j;
10235
10236 for (j = 1; j < c->argc; j++)
10237 pubsubSubscribePattern(c,c->argv[j]);
10238}
10239
10240static void punsubscribeCommand(redisClient *c) {
10241 if (c->argc == 1) {
10242 pubsubUnsubscribeAllPatterns(c,1);
befec3cd 10243 return;
10244 } else {
10245 int j;
10246
10247 for (j = 1; j < c->argc; j++)
ffc6b7f8 10248 pubsubUnsubscribePattern(c,c->argv[j],1);
befec3cd 10249 }
10250}
10251
10252static void publishCommand(redisClient *c) {
10253 int receivers = pubsubPublishMessage(c->argv[1],c->argv[2]);
10254 addReplyLong(c,receivers);
10255}
10256
7f957c92 10257/* ================================= Debugging ============================== */
10258
ba798261 10259/* Compute the sha1 of string at 's' with 'len' bytes long.
10260 * The SHA1 is then xored againt the string pointed by digest.
10261 * Since xor is commutative, this operation is used in order to
10262 * "add" digests relative to unordered elements.
10263 *
10264 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
10265static void xorDigest(unsigned char *digest, void *ptr, size_t len) {
10266 SHA1_CTX ctx;
10267 unsigned char hash[20], *s = ptr;
10268 int j;
10269
10270 SHA1Init(&ctx);
10271 SHA1Update(&ctx,s,len);
10272 SHA1Final(hash,&ctx);
10273
10274 for (j = 0; j < 20; j++)
10275 digest[j] ^= hash[j];
10276}
10277
10278static void xorObjectDigest(unsigned char *digest, robj *o) {
10279 o = getDecodedObject(o);
10280 xorDigest(digest,o->ptr,sdslen(o->ptr));
10281 decrRefCount(o);
10282}
10283
10284/* This function instead of just computing the SHA1 and xoring it
10285 * against diget, also perform the digest of "digest" itself and
10286 * replace the old value with the new one.
10287 *
10288 * So the final digest will be:
10289 *
10290 * digest = SHA1(digest xor SHA1(data))
10291 *
10292 * This function is used every time we want to preserve the order so
10293 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
10294 *
10295 * Also note that mixdigest("foo") followed by mixdigest("bar")
10296 * will lead to a different digest compared to "fo", "obar".
10297 */
10298static void mixDigest(unsigned char *digest, void *ptr, size_t len) {
10299 SHA1_CTX ctx;
10300 char *s = ptr;
10301
10302 xorDigest(digest,s,len);
10303 SHA1Init(&ctx);
10304 SHA1Update(&ctx,digest,20);
10305 SHA1Final(digest,&ctx);
10306}
10307
10308static void mixObjectDigest(unsigned char *digest, robj *o) {
10309 o = getDecodedObject(o);
10310 mixDigest(digest,o->ptr,sdslen(o->ptr));
10311 decrRefCount(o);
10312}
10313
10314/* Compute the dataset digest. Since keys, sets elements, hashes elements
10315 * are not ordered, we use a trick: every aggregate digest is the xor
10316 * of the digests of their elements. This way the order will not change
10317 * the result. For list instead we use a feedback entering the output digest
10318 * as input in order to ensure that a different ordered list will result in
10319 * a different digest. */
10320static void computeDatasetDigest(unsigned char *final) {
10321 unsigned char digest[20];
10322 char buf[128];
10323 dictIterator *di = NULL;
10324 dictEntry *de;
10325 int j;
10326 uint32_t aux;
10327
10328 memset(final,0,20); /* Start with a clean result */
10329
10330 for (j = 0; j < server.dbnum; j++) {
10331 redisDb *db = server.db+j;
10332
10333 if (dictSize(db->dict) == 0) continue;
10334 di = dictGetIterator(db->dict);
10335
10336 /* hash the DB id, so the same dataset moved in a different
10337 * DB will lead to a different digest */
10338 aux = htonl(j);
10339 mixDigest(final,&aux,sizeof(aux));
10340
10341 /* Iterate this DB writing every entry */
10342 while((de = dictNext(di)) != NULL) {
10343 robj *key, *o;
10344 time_t expiretime;
10345
10346 memset(digest,0,20); /* This key-val digest */
10347 key = dictGetEntryKey(de);
10348 mixObjectDigest(digest,key);
10349 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
10350 key->storage == REDIS_VM_SWAPPING) {
10351 o = dictGetEntryVal(de);
10352 incrRefCount(o);
10353 } else {
10354 o = vmPreviewObject(key);
10355 }
10356 aux = htonl(o->type);
10357 mixDigest(digest,&aux,sizeof(aux));
10358 expiretime = getExpire(db,key);
10359
10360 /* Save the key and associated value */
10361 if (o->type == REDIS_STRING) {
10362 mixObjectDigest(digest,o);
10363 } else if (o->type == REDIS_LIST) {
10364 list *list = o->ptr;
10365 listNode *ln;
10366 listIter li;
10367
10368 listRewind(list,&li);
10369 while((ln = listNext(&li))) {
10370 robj *eleobj = listNodeValue(ln);
10371
10372 mixObjectDigest(digest,eleobj);
10373 }
10374 } else if (o->type == REDIS_SET) {
10375 dict *set = o->ptr;
10376 dictIterator *di = dictGetIterator(set);
10377 dictEntry *de;
10378
10379 while((de = dictNext(di)) != NULL) {
10380 robj *eleobj = dictGetEntryKey(de);
10381
10382 xorObjectDigest(digest,eleobj);
10383 }
10384 dictReleaseIterator(di);
10385 } else if (o->type == REDIS_ZSET) {
10386 zset *zs = o->ptr;
10387 dictIterator *di = dictGetIterator(zs->dict);
10388 dictEntry *de;
10389
10390 while((de = dictNext(di)) != NULL) {
10391 robj *eleobj = dictGetEntryKey(de);
10392 double *score = dictGetEntryVal(de);
10393 unsigned char eledigest[20];
10394
10395 snprintf(buf,sizeof(buf),"%.17g",*score);
10396 memset(eledigest,0,20);
10397 mixObjectDigest(eledigest,eleobj);
10398 mixDigest(eledigest,buf,strlen(buf));
10399 xorDigest(digest,eledigest,20);
10400 }
10401 dictReleaseIterator(di);
10402 } else if (o->type == REDIS_HASH) {
10403 hashIterator *hi;
10404 robj *obj;
10405
10406 hi = hashInitIterator(o);
10407 while (hashNext(hi) != REDIS_ERR) {
10408 unsigned char eledigest[20];
10409
10410 memset(eledigest,0,20);
10411 obj = hashCurrent(hi,REDIS_HASH_KEY);
10412 mixObjectDigest(eledigest,obj);
10413 decrRefCount(obj);
10414 obj = hashCurrent(hi,REDIS_HASH_VALUE);
10415 mixObjectDigest(eledigest,obj);
10416 decrRefCount(obj);
10417 xorDigest(digest,eledigest,20);
10418 }
10419 hashReleaseIterator(hi);
10420 } else {
10421 redisPanic("Unknown object type");
10422 }
10423 decrRefCount(o);
10424 /* If the key has an expire, add it to the mix */
10425 if (expiretime != -1) xorDigest(digest,"!!expire!!",10);
10426 /* We can finally xor the key-val digest to the final digest */
10427 xorDigest(final,digest,20);
10428 }
10429 dictReleaseIterator(di);
10430 }
10431}
10432
7f957c92 10433static void debugCommand(redisClient *c) {
10434 if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
10435 *((char*)-1) = 'x';
210e29f7 10436 } else if (!strcasecmp(c->argv[1]->ptr,"reload")) {
10437 if (rdbSave(server.dbfilename) != REDIS_OK) {
10438 addReply(c,shared.err);
10439 return;
10440 }
10441 emptyDb();
10442 if (rdbLoad(server.dbfilename) != REDIS_OK) {
10443 addReply(c,shared.err);
10444 return;
10445 }
10446 redisLog(REDIS_WARNING,"DB reloaded by DEBUG RELOAD");
10447 addReply(c,shared.ok);
71c2b467 10448 } else if (!strcasecmp(c->argv[1]->ptr,"loadaof")) {
10449 emptyDb();
10450 if (loadAppendOnlyFile(server.appendfilename) != REDIS_OK) {
10451 addReply(c,shared.err);
10452 return;
10453 }
10454 redisLog(REDIS_WARNING,"Append Only File loaded by DEBUG LOADAOF");
10455 addReply(c,shared.ok);
333298da 10456 } else if (!strcasecmp(c->argv[1]->ptr,"object") && c->argc == 3) {
10457 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
10458 robj *key, *val;
10459
10460 if (!de) {
10461 addReply(c,shared.nokeyerr);
10462 return;
10463 }
10464 key = dictGetEntryKey(de);
10465 val = dictGetEntryVal(de);
59146ef3 10466 if (!server.vm_enabled || (key->storage == REDIS_VM_MEMORY ||
10467 key->storage == REDIS_VM_SWAPPING)) {
07efaf74 10468 char *strenc;
10469 char buf[128];
10470
10471 if (val->encoding < (sizeof(strencoding)/sizeof(char*))) {
10472 strenc = strencoding[val->encoding];
10473 } else {
10474 snprintf(buf,64,"unknown encoding %d\n", val->encoding);
10475 strenc = buf;
10476 }
ace06542 10477 addReplySds(c,sdscatprintf(sdsempty(),
10478 "+Key at:%p refcount:%d, value at:%p refcount:%d "
07efaf74 10479 "encoding:%s serializedlength:%lld\r\n",
682ac724 10480 (void*)key, key->refcount, (void*)val, val->refcount,
07efaf74 10481 strenc, (long long) rdbSavedObjectLen(val,NULL)));
ace06542 10482 } else {
10483 addReplySds(c,sdscatprintf(sdsempty(),
10484 "+Key at:%p refcount:%d, value swapped at: page %llu "
10485 "using %llu pages\r\n",
10486 (void*)key, key->refcount, (unsigned long long) key->vm.page,
10487 (unsigned long long) key->vm.usedpages));
10488 }
78ebe4c8 10489 } else if (!strcasecmp(c->argv[1]->ptr,"swapin") && c->argc == 3) {
10490 lookupKeyRead(c->db,c->argv[2]);
10491 addReply(c,shared.ok);
7d30035d 10492 } else if (!strcasecmp(c->argv[1]->ptr,"swapout") && c->argc == 3) {
10493 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
10494 robj *key, *val;
10495
10496 if (!server.vm_enabled) {
10497 addReplySds(c,sdsnew("-ERR Virtual Memory is disabled\r\n"));
10498 return;
10499 }
10500 if (!de) {
10501 addReply(c,shared.nokeyerr);
10502 return;
10503 }
10504 key = dictGetEntryKey(de);
10505 val = dictGetEntryVal(de);
4ef8de8a 10506 /* If the key is shared we want to create a copy */
10507 if (key->refcount > 1) {
10508 robj *newkey = dupStringObject(key);
10509 decrRefCount(key);
10510 key = dictGetEntryKey(de) = newkey;
10511 }
10512 /* Swap it */
7d30035d 10513 if (key->storage != REDIS_VM_MEMORY) {
10514 addReplySds(c,sdsnew("-ERR This key is not in memory\r\n"));
a69a0c9c 10515 } else if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
7d30035d 10516 dictGetEntryVal(de) = NULL;
10517 addReply(c,shared.ok);
10518 } else {
10519 addReply(c,shared.err);
10520 }
59305dc7 10521 } else if (!strcasecmp(c->argv[1]->ptr,"populate") && c->argc == 3) {
10522 long keys, j;
10523 robj *key, *val;
10524 char buf[128];
10525
10526 if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != REDIS_OK)
10527 return;
10528 for (j = 0; j < keys; j++) {
10529 snprintf(buf,sizeof(buf),"key:%lu",j);
10530 key = createStringObject(buf,strlen(buf));
10531 if (lookupKeyRead(c->db,key) != NULL) {
10532 decrRefCount(key);
10533 continue;
10534 }
10535 snprintf(buf,sizeof(buf),"value:%lu",j);
10536 val = createStringObject(buf,strlen(buf));
10537 dictAdd(c->db->dict,key,val);
10538 }
10539 addReply(c,shared.ok);
ba798261 10540 } else if (!strcasecmp(c->argv[1]->ptr,"digest") && c->argc == 2) {
10541 unsigned char digest[20];
10542 sds d = sdsnew("+");
10543 int j;
10544
10545 computeDatasetDigest(digest);
10546 for (j = 0; j < 20; j++)
10547 d = sdscatprintf(d, "%02x",digest[j]);
10548
10549 d = sdscatlen(d,"\r\n",2);
10550 addReplySds(c,d);
7f957c92 10551 } else {
333298da 10552 addReplySds(c,sdsnew(
bdcb92f2 10553 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]\r\n"));
7f957c92 10554 }
10555}
56906eef 10556
6c96ba7d 10557static void _redisAssert(char *estr, char *file, int line) {
dfc5e96c 10558 redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
6c96ba7d 10559 redisLog(REDIS_WARNING,"==> %s:%d '%s' is not true\n",file,line,estr);
dfc5e96c 10560#ifdef HAVE_BACKTRACE
10561 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
10562 *((char*)-1) = 'x';
10563#endif
10564}
10565
c651fd9e 10566static void _redisPanic(char *msg, char *file, int line) {
10567 redisLog(REDIS_WARNING,"!!! Software Failure. Press left mouse button to continue");
17772754 10568 redisLog(REDIS_WARNING,"Guru Meditation: %s #%s:%d",msg,file,line);
c651fd9e 10569#ifdef HAVE_BACKTRACE
10570 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
10571 *((char*)-1) = 'x';
10572#endif
10573}
10574
bcfc686d 10575/* =================================== Main! ================================ */
56906eef 10576
bcfc686d 10577#ifdef __linux__
10578int linuxOvercommitMemoryValue(void) {
10579 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
10580 char buf[64];
56906eef 10581
bcfc686d 10582 if (!fp) return -1;
10583 if (fgets(buf,64,fp) == NULL) {
10584 fclose(fp);
10585 return -1;
10586 }
10587 fclose(fp);
56906eef 10588
bcfc686d 10589 return atoi(buf);
10590}
10591
10592void linuxOvercommitMemoryWarning(void) {
10593 if (linuxOvercommitMemoryValue() == 0) {
7ccd2d0a 10594 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 10595 }
10596}
10597#endif /* __linux__ */
10598
10599static void daemonize(void) {
10600 int fd;
10601 FILE *fp;
10602
10603 if (fork() != 0) exit(0); /* parent exits */
10604 setsid(); /* create a new session */
10605
10606 /* Every output goes to /dev/null. If Redis is daemonized but
10607 * the 'logfile' is set to 'stdout' in the configuration file
10608 * it will not log at all. */
10609 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
10610 dup2(fd, STDIN_FILENO);
10611 dup2(fd, STDOUT_FILENO);
10612 dup2(fd, STDERR_FILENO);
10613 if (fd > STDERR_FILENO) close(fd);
10614 }
10615 /* Try to write the pid file */
10616 fp = fopen(server.pidfile,"w");
10617 if (fp) {
10618 fprintf(fp,"%d\n",getpid());
10619 fclose(fp);
56906eef 10620 }
56906eef 10621}
10622
42ab0172
AO
10623static void version() {
10624 printf("Redis server version %s\n", REDIS_VERSION);
10625 exit(0);
10626}
10627
723fb69b
AO
10628static void usage() {
10629 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf]\n");
e9409273 10630 fprintf(stderr," ./redis-server - (read config from stdin)\n");
723fb69b
AO
10631 exit(1);
10632}
10633
bcfc686d 10634int main(int argc, char **argv) {
9651a787 10635 time_t start;
10636
bcfc686d 10637 initServerConfig();
10638 if (argc == 2) {
44efe66e 10639 if (strcmp(argv[1], "-v") == 0 ||
10640 strcmp(argv[1], "--version") == 0) version();
10641 if (strcmp(argv[1], "--help") == 0) usage();
bcfc686d 10642 resetServerSaveParams();
10643 loadServerConfig(argv[1]);
723fb69b
AO
10644 } else if ((argc > 2)) {
10645 usage();
bcfc686d 10646 } else {
10647 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'");
10648 }
bcfc686d 10649 if (server.daemonize) daemonize();
71c54b21 10650 initServer();
bcfc686d 10651 redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
10652#ifdef __linux__
10653 linuxOvercommitMemoryWarning();
10654#endif
9651a787 10655 start = time(NULL);
bcfc686d 10656 if (server.appendonly) {
10657 if (loadAppendOnlyFile(server.appendfilename) == REDIS_OK)
9651a787 10658 redisLog(REDIS_NOTICE,"DB loaded from append only file: %ld seconds",time(NULL)-start);
bcfc686d 10659 } else {
10660 if (rdbLoad(server.dbfilename) == REDIS_OK)
9651a787 10661 redisLog(REDIS_NOTICE,"DB loaded from disk: %ld seconds",time(NULL)-start);
bcfc686d 10662 }
bcfc686d 10663 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
d5d55fc3 10664 aeSetBeforeSleepProc(server.el,beforeSleep);
bcfc686d 10665 aeMain(server.el);
10666 aeDeleteEventLoop(server.el);
10667 return 0;
10668}
10669
10670/* ============================= Backtrace support ========================= */
10671
10672#ifdef HAVE_BACKTRACE
10673static char *findFuncName(void *pointer, unsigned long *offset);
10674
56906eef 10675static void *getMcontextEip(ucontext_t *uc) {
10676#if defined(__FreeBSD__)
10677 return (void*) uc->uc_mcontext.mc_eip;
10678#elif defined(__dietlibc__)
10679 return (void*) uc->uc_mcontext.eip;
06db1f50 10680#elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
da0a1620 10681 #if __x86_64__
10682 return (void*) uc->uc_mcontext->__ss.__rip;
10683 #else
56906eef 10684 return (void*) uc->uc_mcontext->__ss.__eip;
da0a1620 10685 #endif
06db1f50 10686#elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
cb7e07cc 10687 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
06db1f50 10688 return (void*) uc->uc_mcontext->__ss.__rip;
cbc59b38 10689 #else
10690 return (void*) uc->uc_mcontext->__ss.__eip;
e0a62c7f 10691 #endif
54bac49d 10692#elif defined(__i386__) || defined(__X86_64__) || defined(__x86_64__)
c04c9ac9 10693 return (void*) uc->uc_mcontext.gregs[REG_EIP]; /* Linux 32/64 bit */
b91cf5ef 10694#elif defined(__ia64__) /* Linux IA64 */
10695 return (void*) uc->uc_mcontext.sc_ip;
10696#else
10697 return NULL;
56906eef 10698#endif
10699}
10700
10701static void segvHandler(int sig, siginfo_t *info, void *secret) {
10702 void *trace[100];
10703 char **messages = NULL;
10704 int i, trace_size = 0;
10705 unsigned long offset=0;
56906eef 10706 ucontext_t *uc = (ucontext_t*) secret;
1c85b79f 10707 sds infostring;
56906eef 10708 REDIS_NOTUSED(info);
10709
10710 redisLog(REDIS_WARNING,
10711 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION, sig);
1c85b79f 10712 infostring = genRedisInfoString();
10713 redisLog(REDIS_WARNING, "%s",infostring);
10714 /* It's not safe to sdsfree() the returned string under memory
10715 * corruption conditions. Let it leak as we are going to abort */
e0a62c7f 10716
56906eef 10717 trace_size = backtrace(trace, 100);
de96dbfe 10718 /* overwrite sigaction with caller's address */
b91cf5ef 10719 if (getMcontextEip(uc) != NULL) {
10720 trace[1] = getMcontextEip(uc);
10721 }
56906eef 10722 messages = backtrace_symbols(trace, trace_size);
fe3bbfbe 10723
d76412d1 10724 for (i=1; i<trace_size; ++i) {
56906eef 10725 char *fn = findFuncName(trace[i], &offset), *p;
10726
10727 p = strchr(messages[i],'+');
10728 if (!fn || (p && ((unsigned long)strtol(p+1,NULL,10)) < offset)) {
10729 redisLog(REDIS_WARNING,"%s", messages[i]);
10730 } else {
10731 redisLog(REDIS_WARNING,"%d redis-server %p %s + %d", i, trace[i], fn, (unsigned int)offset);
10732 }
10733 }
b177fd30 10734 /* free(messages); Don't call free() with possibly corrupted memory. */
478c2c6f 10735 _exit(0);
fe3bbfbe 10736}
56906eef 10737
10738static void setupSigSegvAction(void) {
10739 struct sigaction act;
10740
10741 sigemptyset (&act.sa_mask);
10742 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
10743 * is used. Otherwise, sa_handler is used */
10744 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
10745 act.sa_sigaction = segvHandler;
10746 sigaction (SIGSEGV, &act, NULL);
10747 sigaction (SIGBUS, &act, NULL);
12fea928 10748 sigaction (SIGFPE, &act, NULL);
10749 sigaction (SIGILL, &act, NULL);
10750 sigaction (SIGBUS, &act, NULL);
e65fdc78 10751 return;
56906eef 10752}
e65fdc78 10753
bcfc686d 10754#include "staticsymbols.h"
10755/* This function try to convert a pointer into a function name. It's used in
10756 * oreder to provide a backtrace under segmentation fault that's able to
10757 * display functions declared as static (otherwise the backtrace is useless). */
10758static char *findFuncName(void *pointer, unsigned long *offset){
10759 int i, ret = -1;
10760 unsigned long off, minoff = 0;
ed9b544e 10761
bcfc686d 10762 /* Try to match against the Symbol with the smallest offset */
10763 for (i=0; symsTable[i].pointer; i++) {
10764 unsigned long lp = (unsigned long) pointer;
0bc03378 10765
bcfc686d 10766 if (lp != (unsigned long)-1 && lp >= symsTable[i].pointer) {
10767 off=lp-symsTable[i].pointer;
10768 if (ret < 0 || off < minoff) {
10769 minoff=off;
10770 ret=i;
10771 }
10772 }
0bc03378 10773 }
bcfc686d 10774 if (ret == -1) return NULL;
10775 *offset = minoff;
10776 return symsTable[ret].name;
0bc03378 10777}
bcfc686d 10778#else /* HAVE_BACKTRACE */
10779static void setupSigSegvAction(void) {
0bc03378 10780}
bcfc686d 10781#endif /* HAVE_BACKTRACE */
0bc03378 10782
ed9b544e 10783
ed9b544e 10784
bcfc686d 10785/* The End */
10786
10787
ed9b544e 10788