]> git.saurik.com Git - redis.git/blob - redis.c
44b115ea2f9b8eaf5c648866ef8d70c9916c6969
[redis.git] / redis.c
1 /*
2 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #define REDIS_VERSION "2.1.1"
31
32 #include "fmacros.h"
33 #include "config.h"
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>
41
42 #ifdef HAVE_BACKTRACE
43 #include <execinfo.h>
44 #include <ucontext.h>
45 #endif /* HAVE_BACKTRACE */
46
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>
58 #include <sys/uio.h>
59 #include <limits.h>
60 #include <float.h>
61 #include <math.h>
62 #include <pthread.h>
63
64 #if defined(__sun)
65 #include "solarisfixes.h"
66 #endif
67
68 #include "redis.h"
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 */
75 #include "lzf.h" /* LZF compression library */
76 #include "pqsort.h" /* Partial qsort for SORT+LIMIT */
77 #include "zipmap.h" /* Compact dictionary-alike data structure */
78 #include "ziplist.h" /* Compact list data structure */
79 #include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
80 #include "release.h" /* Release and/or git repository information */
81
82 /* Error codes */
83 #define REDIS_OK 0
84 #define REDIS_ERR -1
85
86 /* Static server configuration */
87 #define REDIS_SERVERPORT 6379 /* TCP port */
88 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
89 #define REDIS_IOBUF_LEN 1024
90 #define REDIS_LOADBUF_LEN 1024
91 #define REDIS_STATIC_ARGS 8
92 #define REDIS_DEFAULT_DBNUM 16
93 #define REDIS_CONFIGLINE_MAX 1024
94 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
95 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
96 #define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */
97 #define REDIS_MAX_WRITE_PER_EVENT (1024*64)
98 #define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
99
100 /* If more then REDIS_WRITEV_THRESHOLD write packets are pending use writev */
101 #define REDIS_WRITEV_THRESHOLD 3
102 /* Max number of iovecs used for each writev call */
103 #define REDIS_WRITEV_IOVEC_COUNT 256
104
105 /* Hash table parameters */
106 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
107
108 /* Command flags */
109 #define REDIS_CMD_BULK 1 /* Bulk write command */
110 #define REDIS_CMD_INLINE 2 /* Inline command */
111 /* REDIS_CMD_DENYOOM reserves a longer comment: all the commands marked with
112 this flags will return an error when the 'maxmemory' option is set in the
113 config file and the server is using more than maxmemory bytes of memory.
114 In short this commands are denied on low memory conditions. */
115 #define REDIS_CMD_DENYOOM 4
116 #define REDIS_CMD_FORCE_REPLICATION 8 /* Force replication even if dirty is 0 */
117
118 /* Object types */
119 #define REDIS_STRING 0
120 #define REDIS_LIST 1
121 #define REDIS_SET 2
122 #define REDIS_ZSET 3
123 #define REDIS_HASH 4
124 #define REDIS_VMPOINTER 8
125
126 /* Objects encoding. Some kind of objects like Strings and Hashes can be
127 * internally represented in multiple ways. The 'encoding' field of the object
128 * is set to one of this fields for this object. */
129 #define REDIS_ENCODING_RAW 0 /* Raw representation */
130 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
131 #define REDIS_ENCODING_HT 2 /* Encoded as hash table */
132 #define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
133 #define REDIS_ENCODING_LIST 4 /* Encoded as zipmap */
134 #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
135
136 static char* strencoding[] = {
137 "raw", "int", "hashtable", "zipmap", "list", "ziplist"
138 };
139
140 /* Object types only used for dumping to disk */
141 #define REDIS_EXPIRETIME 253
142 #define REDIS_SELECTDB 254
143 #define REDIS_EOF 255
144
145 /* Defines related to the dump file format. To store 32 bits lengths for short
146 * keys requires a lot of space, so we check the most significant 2 bits of
147 * the first byte to interpreter the length:
148 *
149 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
150 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
151 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
152 * 11|000000 this means: specially encoded object will follow. The six bits
153 * number specify the kind of object that follows.
154 * See the REDIS_RDB_ENC_* defines.
155 *
156 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
157 * values, will fit inside. */
158 #define REDIS_RDB_6BITLEN 0
159 #define REDIS_RDB_14BITLEN 1
160 #define REDIS_RDB_32BITLEN 2
161 #define REDIS_RDB_ENCVAL 3
162 #define REDIS_RDB_LENERR UINT_MAX
163
164 /* When a length of a string object stored on disk has the first two bits
165 * set, the remaining two bits specify a special encoding for the object
166 * accordingly to the following defines: */
167 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
168 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
169 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
170 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
171
172 /* Virtual memory object->where field. */
173 #define REDIS_VM_MEMORY 0 /* The object is on memory */
174 #define REDIS_VM_SWAPPED 1 /* The object is on disk */
175 #define REDIS_VM_SWAPPING 2 /* Redis is swapping this object on disk */
176 #define REDIS_VM_LOADING 3 /* Redis is loading this object from disk */
177
178 /* Virtual memory static configuration stuff.
179 * Check vmFindContiguousPages() to know more about this magic numbers. */
180 #define REDIS_VM_MAX_NEAR_PAGES 65536
181 #define REDIS_VM_MAX_RANDOM_JUMP 4096
182 #define REDIS_VM_MAX_THREADS 32
183 #define REDIS_THREAD_STACK_SIZE (1024*1024*4)
184 /* The following is the *percentage* of completed I/O jobs to process when the
185 * handelr is called. While Virtual Memory I/O operations are performed by
186 * threads, this operations must be processed by the main thread when completed
187 * in order to take effect. */
188 #define REDIS_MAX_COMPLETED_JOBS_PROCESSED 1
189
190 /* Client flags */
191 #define REDIS_SLAVE 1 /* This client is a slave server */
192 #define REDIS_MASTER 2 /* This client is a master server */
193 #define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */
194 #define REDIS_MULTI 8 /* This client is in a MULTI context */
195 #define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */
196 #define REDIS_IO_WAIT 32 /* The client is waiting for Virtual Memory I/O */
197 #define REDIS_DIRTY_CAS 64 /* Watched keys modified. EXEC will fail. */
198
199 /* Slave replication state - slave side */
200 #define REDIS_REPL_NONE 0 /* No active replication */
201 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
202 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
203
204 /* Slave replication state - from the point of view of master
205 * Note that in SEND_BULK and ONLINE state the slave receives new updates
206 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
207 * to start the next background saving in order to send updates to it. */
208 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
209 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
210 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
211 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
212
213 /* List related stuff */
214 #define REDIS_HEAD 0
215 #define REDIS_TAIL 1
216
217 /* Sort operations */
218 #define REDIS_SORT_GET 0
219 #define REDIS_SORT_ASC 1
220 #define REDIS_SORT_DESC 2
221 #define REDIS_SORTKEY_MAX 1024
222
223 /* Log levels */
224 #define REDIS_DEBUG 0
225 #define REDIS_VERBOSE 1
226 #define REDIS_NOTICE 2
227 #define REDIS_WARNING 3
228
229 /* Anti-warning macro... */
230 #define REDIS_NOTUSED(V) ((void) V)
231
232 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
233 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
234
235 /* Append only defines */
236 #define APPENDFSYNC_NO 0
237 #define APPENDFSYNC_ALWAYS 1
238 #define APPENDFSYNC_EVERYSEC 2
239
240 /* Zip structure related defaults */
241 #define REDIS_HASH_MAX_ZIPMAP_ENTRIES 64
242 #define REDIS_HASH_MAX_ZIPMAP_VALUE 512
243 #define REDIS_LIST_MAX_ZIPLIST_ENTRIES 1024
244 #define REDIS_LIST_MAX_ZIPLIST_VALUE 32
245
246 /* We can print the stacktrace, so our assert is defined this way: */
247 #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
248 #define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
249 static void _redisAssert(char *estr, char *file, int line);
250 static void _redisPanic(char *msg, char *file, int line);
251
252 /*================================= Data types ============================== */
253
254 /* A redis object, that is a type able to hold a string / list / set */
255
256 /* The actual Redis Object */
257 typedef struct redisObject {
258 unsigned type:4;
259 unsigned storage:2; /* REDIS_VM_MEMORY or REDIS_VM_SWAPPING */
260 unsigned encoding:4;
261 unsigned lru:22; /* lru time (relative to server.lruclock) */
262 int refcount;
263 void *ptr;
264 /* VM fields, this are only allocated if VM is active, otherwise the
265 * object allocation function will just allocate
266 * sizeof(redisObjct) minus sizeof(redisObjectVM), so using
267 * Redis without VM active will not have any overhead. */
268 } robj;
269
270 /* The VM pointer structure - identifies an object in the swap file.
271 *
272 * This object is stored in place of the value
273 * object in the main key->value hash table representing a database.
274 * Note that the first fields (type, storage) are the same as the redisObject
275 * structure so that vmPointer strucuters can be accessed even when casted
276 * as redisObject structures.
277 *
278 * This is useful as we don't know if a value object is or not on disk, but we
279 * are always able to read obj->storage to check this. For vmPointer
280 * structures "type" is set to REDIS_VMPOINTER (even if without this field
281 * is still possible to check the kind of object from the value of 'storage').*/
282 typedef struct vmPointer {
283 unsigned type:4;
284 unsigned storage:2; /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
285 unsigned notused:26;
286 unsigned int vtype; /* type of the object stored in the swap file */
287 off_t page; /* the page at witch the object is stored on disk */
288 off_t usedpages; /* number of pages used on disk */
289 } vmpointer;
290
291 /* Macro used to initalize a Redis object allocated on the stack.
292 * Note that this macro is taken near the structure definition to make sure
293 * we'll update it when the structure is changed, to avoid bugs like
294 * bug #85 introduced exactly in this way. */
295 #define initStaticStringObject(_var,_ptr) do { \
296 _var.refcount = 1; \
297 _var.type = REDIS_STRING; \
298 _var.encoding = REDIS_ENCODING_RAW; \
299 _var.ptr = _ptr; \
300 _var.storage = REDIS_VM_MEMORY; \
301 } while(0);
302
303 typedef struct redisDb {
304 dict *dict; /* The keyspace for this DB */
305 dict *expires; /* Timeout of keys with a timeout set */
306 dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
307 dict *io_keys; /* Keys with clients waiting for VM I/O */
308 dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
309 int id;
310 } redisDb;
311
312 /* Client MULTI/EXEC state */
313 typedef struct multiCmd {
314 robj **argv;
315 int argc;
316 struct redisCommand *cmd;
317 } multiCmd;
318
319 typedef struct multiState {
320 multiCmd *commands; /* Array of MULTI commands */
321 int count; /* Total number of MULTI commands */
322 } multiState;
323
324 /* With multiplexing we need to take per-clinet state.
325 * Clients are taken in a liked list. */
326 typedef struct redisClient {
327 int fd;
328 redisDb *db;
329 int dictid;
330 sds querybuf;
331 robj **argv, **mbargv;
332 int argc, mbargc;
333 int bulklen; /* bulk read len. -1 if not in bulk read mode */
334 int multibulk; /* multi bulk command format active */
335 list *reply;
336 int sentlen;
337 time_t lastinteraction; /* time of the last interaction, used for timeout */
338 int flags; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */
339 int slaveseldb; /* slave selected db, if this client is a slave */
340 int authenticated; /* when requirepass is non-NULL */
341 int replstate; /* replication state if this is a slave */
342 int repldbfd; /* replication DB file descriptor */
343 long repldboff; /* replication DB file offset */
344 off_t repldbsize; /* replication DB file size */
345 multiState mstate; /* MULTI/EXEC state */
346 robj **blocking_keys; /* The key we are waiting to terminate a blocking
347 * operation such as BLPOP. Otherwise NULL. */
348 int blocking_keys_num; /* Number of blocking keys */
349 time_t blockingto; /* Blocking operation timeout. If UNIX current time
350 * is >= blockingto then the operation timed out. */
351 list *io_keys; /* Keys this client is waiting to be loaded from the
352 * swap file in order to continue. */
353 list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */
354 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
355 list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */
356 } redisClient;
357
358 struct saveparam {
359 time_t seconds;
360 int changes;
361 };
362
363 /* Global server state structure */
364 struct redisServer {
365 int port;
366 int fd;
367 redisDb *db;
368 long long dirty; /* changes to DB from the last save */
369 list *clients;
370 list *slaves, *monitors;
371 char neterr[ANET_ERR_LEN];
372 aeEventLoop *el;
373 int cronloops; /* number of times the cron function run */
374 list *objfreelist; /* A list of freed objects to avoid malloc() */
375 time_t lastsave; /* Unix time of last save succeeede */
376 /* Fields used only for stats */
377 time_t stat_starttime; /* server start time */
378 long long stat_numcommands; /* number of processed commands */
379 long long stat_numconnections; /* number of connections received */
380 long long stat_expiredkeys; /* number of expired keys */
381 /* Configuration */
382 int verbosity;
383 int glueoutputbuf;
384 int maxidletime;
385 int dbnum;
386 int daemonize;
387 int appendonly;
388 int appendfsync;
389 int no_appendfsync_on_rewrite;
390 int shutdown_asap;
391 time_t lastfsync;
392 int appendfd;
393 int appendseldb;
394 char *pidfile;
395 pid_t bgsavechildpid;
396 pid_t bgrewritechildpid;
397 sds bgrewritebuf; /* buffer taken by parent during oppend only rewrite */
398 sds aofbuf; /* AOF buffer, written before entering the event loop */
399 struct saveparam *saveparams;
400 int saveparamslen;
401 char *logfile;
402 char *bindaddr;
403 char *dbfilename;
404 char *appendfilename;
405 char *requirepass;
406 int rdbcompression;
407 int activerehashing;
408 /* Replication related */
409 int isslave;
410 char *masterauth;
411 char *masterhost;
412 int masterport;
413 redisClient *master; /* client that is master for this slave */
414 int replstate;
415 unsigned int maxclients;
416 unsigned long long maxmemory;
417 unsigned int blpop_blocked_clients;
418 unsigned int vm_blocked_clients;
419 /* Sort parameters - qsort_r() is only available under BSD so we
420 * have to take this state global, in order to pass it to sortCompare() */
421 int sort_desc;
422 int sort_alpha;
423 int sort_bypattern;
424 /* Virtual memory configuration */
425 int vm_enabled;
426 char *vm_swap_file;
427 off_t vm_page_size;
428 off_t vm_pages;
429 unsigned long long vm_max_memory;
430 /* Zip structure config */
431 size_t hash_max_zipmap_entries;
432 size_t hash_max_zipmap_value;
433 size_t list_max_ziplist_entries;
434 size_t list_max_ziplist_value;
435 /* Virtual memory state */
436 FILE *vm_fp;
437 int vm_fd;
438 off_t vm_next_page; /* Next probably empty page */
439 off_t vm_near_pages; /* Number of pages allocated sequentially */
440 unsigned char *vm_bitmap; /* Bitmap of free/used pages */
441 time_t unixtime; /* Unix time sampled every second. */
442 /* Virtual memory I/O threads stuff */
443 /* An I/O thread process an element taken from the io_jobs queue and
444 * put the result of the operation in the io_done list. While the
445 * job is being processed, it's put on io_processing queue. */
446 list *io_newjobs; /* List of VM I/O jobs yet to be processed */
447 list *io_processing; /* List of VM I/O jobs being processed */
448 list *io_processed; /* List of VM I/O jobs already processed */
449 list *io_ready_clients; /* Clients ready to be unblocked. All keys loaded */
450 pthread_mutex_t io_mutex; /* lock to access io_jobs/io_done/io_thread_job */
451 pthread_mutex_t obj_freelist_mutex; /* safe redis objects creation/free */
452 pthread_mutex_t io_swapfile_mutex; /* So we can lseek + write */
453 pthread_attr_t io_threads_attr; /* attributes for threads creation */
454 int io_active_threads; /* Number of running I/O threads */
455 int vm_max_threads; /* Max number of I/O threads running at the same time */
456 /* Our main thread is blocked on the event loop, locking for sockets ready
457 * to be read or written, so when a threaded I/O operation is ready to be
458 * processed by the main thread, the I/O thread will use a unix pipe to
459 * awake the main thread. The followings are the two pipe FDs. */
460 int io_ready_pipe_read;
461 int io_ready_pipe_write;
462 /* Virtual memory stats */
463 unsigned long long vm_stats_used_pages;
464 unsigned long long vm_stats_swapped_objects;
465 unsigned long long vm_stats_swapouts;
466 unsigned long long vm_stats_swapins;
467 /* Pubsub */
468 dict *pubsub_channels; /* Map channels to list of subscribed clients */
469 list *pubsub_patterns; /* A list of pubsub_patterns */
470 /* Misc */
471 FILE *devnull;
472 unsigned lruclock:22; /* clock incrementing every minute, for LRU */
473 unsigned lruclock_padding:10;
474 };
475
476 typedef struct pubsubPattern {
477 redisClient *client;
478 robj *pattern;
479 } pubsubPattern;
480
481 typedef void redisCommandProc(redisClient *c);
482 typedef void redisVmPreloadProc(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
483 struct redisCommand {
484 char *name;
485 redisCommandProc *proc;
486 int arity;
487 int flags;
488 /* Use a function to determine which keys need to be loaded
489 * in the background prior to executing this command. Takes precedence
490 * over vm_firstkey and others, ignored when NULL */
491 redisVmPreloadProc *vm_preload_proc;
492 /* What keys should be loaded in background when calling this command? */
493 int vm_firstkey; /* The first argument that's a key (0 = no keys) */
494 int vm_lastkey; /* THe last argument that's a key */
495 int vm_keystep; /* The step between first and last key */
496 };
497
498 struct redisFunctionSym {
499 char *name;
500 unsigned long pointer;
501 };
502
503 typedef struct _redisSortObject {
504 robj *obj;
505 union {
506 double score;
507 robj *cmpobj;
508 } u;
509 } redisSortObject;
510
511 typedef struct _redisSortOperation {
512 int type;
513 robj *pattern;
514 } redisSortOperation;
515
516 /* ZSETs use a specialized version of Skiplists */
517
518 typedef struct zskiplistNode {
519 struct zskiplistNode **forward;
520 struct zskiplistNode *backward;
521 unsigned int *span;
522 double score;
523 robj *obj;
524 } zskiplistNode;
525
526 typedef struct zskiplist {
527 struct zskiplistNode *header, *tail;
528 unsigned long length;
529 int level;
530 } zskiplist;
531
532 typedef struct zset {
533 dict *dict;
534 zskiplist *zsl;
535 } zset;
536
537 /* Our shared "common" objects */
538
539 #define REDIS_SHARED_INTEGERS 10000
540 struct sharedObjectsStruct {
541 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
542 *colon, *nullbulk, *nullmultibulk, *queued,
543 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
544 *outofrangeerr, *plus,
545 *select0, *select1, *select2, *select3, *select4,
546 *select5, *select6, *select7, *select8, *select9,
547 *messagebulk, *pmessagebulk, *subscribebulk, *unsubscribebulk, *mbulk3,
548 *mbulk4, *psubscribebulk, *punsubscribebulk,
549 *integers[REDIS_SHARED_INTEGERS];
550 } shared;
551
552 /* Global vars that are actally used as constants. The following double
553 * values are used for double on-disk serialization, and are initialized
554 * at runtime to avoid strange compiler optimizations. */
555
556 static double R_Zero, R_PosInf, R_NegInf, R_Nan;
557
558 /* VM threaded I/O request message */
559 #define REDIS_IOJOB_LOAD 0 /* Load from disk to memory */
560 #define REDIS_IOJOB_PREPARE_SWAP 1 /* Compute needed pages */
561 #define REDIS_IOJOB_DO_SWAP 2 /* Swap from memory to disk */
562 typedef struct iojob {
563 int type; /* Request type, REDIS_IOJOB_* */
564 redisDb *db;/* Redis database */
565 robj *key; /* This I/O request is about swapping this key */
566 robj *id; /* Unique identifier of this job:
567 this is the object to swap for REDIS_IOREQ_*_SWAP, or the
568 vmpointer objct for REDIS_IOREQ_LOAD. */
569 robj *val; /* the value to swap for REDIS_IOREQ_*_SWAP, otherwise this
570 * field is populated by the I/O thread for REDIS_IOREQ_LOAD. */
571 off_t page; /* Swap page where to read/write the object */
572 off_t pages; /* Swap pages needed to save object. PREPARE_SWAP return val */
573 int canceled; /* True if this command was canceled by blocking side of VM */
574 pthread_t thread; /* ID of the thread processing this entry */
575 } iojob;
576
577 /*================================ Prototypes =============================== */
578
579 static void freeStringObject(robj *o);
580 static void freeListObject(robj *o);
581 static void freeSetObject(robj *o);
582 static void decrRefCount(void *o);
583 static robj *createObject(int type, void *ptr);
584 static void freeClient(redisClient *c);
585 static int rdbLoad(char *filename);
586 static void addReply(redisClient *c, robj *obj);
587 static void addReplySds(redisClient *c, sds s);
588 static void incrRefCount(robj *o);
589 static int rdbSaveBackground(char *filename);
590 static robj *createStringObject(char *ptr, size_t len);
591 static robj *dupStringObject(robj *o);
592 static void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
593 static void replicationFeedMonitors(list *monitors, int dictid, robj **argv, int argc);
594 static void flushAppendOnlyFile(void);
595 static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);
596 static int syncWithMaster(void);
597 static robj *tryObjectEncoding(robj *o);
598 static robj *getDecodedObject(robj *o);
599 static int removeExpire(redisDb *db, robj *key);
600 static int expireIfNeeded(redisDb *db, robj *key);
601 static int deleteIfVolatile(redisDb *db, robj *key);
602 static int dbDelete(redisDb *db, robj *key);
603 static time_t getExpire(redisDb *db, robj *key);
604 static int setExpire(redisDb *db, robj *key, time_t when);
605 static void updateSlavesWaitingBgsave(int bgsaveerr);
606 static void freeMemoryIfNeeded(void);
607 static int processCommand(redisClient *c);
608 static void setupSigSegvAction(void);
609 static void rdbRemoveTempFile(pid_t childpid);
610 static void aofRemoveTempFile(pid_t childpid);
611 static size_t stringObjectLen(robj *o);
612 static void processInputBuffer(redisClient *c);
613 static zskiplist *zslCreate(void);
614 static void zslFree(zskiplist *zsl);
615 static void zslInsert(zskiplist *zsl, double score, robj *obj);
616 static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask);
617 static void initClientMultiState(redisClient *c);
618 static void freeClientMultiState(redisClient *c);
619 static void queueMultiCommand(redisClient *c, struct redisCommand *cmd);
620 static void unblockClientWaitingData(redisClient *c);
621 static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele);
622 static void vmInit(void);
623 static void vmMarkPagesFree(off_t page, off_t count);
624 static robj *vmLoadObject(robj *o);
625 static robj *vmPreviewObject(robj *o);
626 static int vmSwapOneObjectBlocking(void);
627 static int vmSwapOneObjectThreaded(void);
628 static int vmCanSwapOut(void);
629 static int tryFreeOneObjectFromFreelist(void);
630 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);
631 static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, int mask);
632 static void vmCancelThreadedIOJob(robj *o);
633 static void lockThreadedIO(void);
634 static void unlockThreadedIO(void);
635 static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db);
636 static void freeIOJob(iojob *j);
637 static void queueIOJob(iojob *j);
638 static int vmWriteObjectOnSwap(robj *o, off_t page);
639 static robj *vmReadObjectFromSwap(off_t page, int type);
640 static void waitEmptyIOJobsQueue(void);
641 static void vmReopenSwapFile(void);
642 static int vmFreePage(off_t page);
643 static void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
644 static void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
645 static int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd);
646 static int dontWaitForSwappedKey(redisClient *c, robj *key);
647 static void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key);
648 static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask);
649 static struct redisCommand *lookupCommand(char *name);
650 static void call(redisClient *c, struct redisCommand *cmd);
651 static void resetClient(redisClient *c);
652 static void convertToRealHash(robj *o);
653 static void listTypeConvert(robj *o, int enc);
654 static int pubsubUnsubscribeAllChannels(redisClient *c, int notify);
655 static int pubsubUnsubscribeAllPatterns(redisClient *c, int notify);
656 static void freePubsubPattern(void *p);
657 static int listMatchPubsubPattern(void *a, void *b);
658 static int compareStringObjects(robj *a, robj *b);
659 static int equalStringObjects(robj *a, robj *b);
660 static void usage();
661 static int rewriteAppendOnlyFileBackground(void);
662 static vmpointer *vmSwapObjectBlocking(robj *val);
663 static int prepareForShutdown();
664 static void touchWatchedKey(redisDb *db, robj *key);
665 static void touchWatchedKeysOnFlush(int dbid);
666 static void unwatchAllKeys(redisClient *c);
667
668 static void authCommand(redisClient *c);
669 static void pingCommand(redisClient *c);
670 static void echoCommand(redisClient *c);
671 static void setCommand(redisClient *c);
672 static void setnxCommand(redisClient *c);
673 static void setexCommand(redisClient *c);
674 static void getCommand(redisClient *c);
675 static void delCommand(redisClient *c);
676 static void existsCommand(redisClient *c);
677 static void incrCommand(redisClient *c);
678 static void decrCommand(redisClient *c);
679 static void incrbyCommand(redisClient *c);
680 static void decrbyCommand(redisClient *c);
681 static void selectCommand(redisClient *c);
682 static void randomkeyCommand(redisClient *c);
683 static void keysCommand(redisClient *c);
684 static void dbsizeCommand(redisClient *c);
685 static void lastsaveCommand(redisClient *c);
686 static void saveCommand(redisClient *c);
687 static void bgsaveCommand(redisClient *c);
688 static void bgrewriteaofCommand(redisClient *c);
689 static void shutdownCommand(redisClient *c);
690 static void moveCommand(redisClient *c);
691 static void renameCommand(redisClient *c);
692 static void renamenxCommand(redisClient *c);
693 static void lpushCommand(redisClient *c);
694 static void rpushCommand(redisClient *c);
695 static void lpopCommand(redisClient *c);
696 static void rpopCommand(redisClient *c);
697 static void llenCommand(redisClient *c);
698 static void lindexCommand(redisClient *c);
699 static void lrangeCommand(redisClient *c);
700 static void ltrimCommand(redisClient *c);
701 static void typeCommand(redisClient *c);
702 static void lsetCommand(redisClient *c);
703 static void saddCommand(redisClient *c);
704 static void sremCommand(redisClient *c);
705 static void smoveCommand(redisClient *c);
706 static void sismemberCommand(redisClient *c);
707 static void scardCommand(redisClient *c);
708 static void spopCommand(redisClient *c);
709 static void srandmemberCommand(redisClient *c);
710 static void sinterCommand(redisClient *c);
711 static void sinterstoreCommand(redisClient *c);
712 static void sunionCommand(redisClient *c);
713 static void sunionstoreCommand(redisClient *c);
714 static void sdiffCommand(redisClient *c);
715 static void sdiffstoreCommand(redisClient *c);
716 static void syncCommand(redisClient *c);
717 static void flushdbCommand(redisClient *c);
718 static void flushallCommand(redisClient *c);
719 static void sortCommand(redisClient *c);
720 static void lremCommand(redisClient *c);
721 static void rpoplpushcommand(redisClient *c);
722 static void infoCommand(redisClient *c);
723 static void mgetCommand(redisClient *c);
724 static void monitorCommand(redisClient *c);
725 static void expireCommand(redisClient *c);
726 static void expireatCommand(redisClient *c);
727 static void getsetCommand(redisClient *c);
728 static void ttlCommand(redisClient *c);
729 static void slaveofCommand(redisClient *c);
730 static void debugCommand(redisClient *c);
731 static void msetCommand(redisClient *c);
732 static void msetnxCommand(redisClient *c);
733 static void zaddCommand(redisClient *c);
734 static void zincrbyCommand(redisClient *c);
735 static void zrangeCommand(redisClient *c);
736 static void zrangebyscoreCommand(redisClient *c);
737 static void zcountCommand(redisClient *c);
738 static void zrevrangeCommand(redisClient *c);
739 static void zcardCommand(redisClient *c);
740 static void zremCommand(redisClient *c);
741 static void zscoreCommand(redisClient *c);
742 static void zremrangebyscoreCommand(redisClient *c);
743 static void multiCommand(redisClient *c);
744 static void execCommand(redisClient *c);
745 static void discardCommand(redisClient *c);
746 static void blpopCommand(redisClient *c);
747 static void brpopCommand(redisClient *c);
748 static void appendCommand(redisClient *c);
749 static void substrCommand(redisClient *c);
750 static void zrankCommand(redisClient *c);
751 static void zrevrankCommand(redisClient *c);
752 static void hsetCommand(redisClient *c);
753 static void hsetnxCommand(redisClient *c);
754 static void hgetCommand(redisClient *c);
755 static void hmsetCommand(redisClient *c);
756 static void hmgetCommand(redisClient *c);
757 static void hdelCommand(redisClient *c);
758 static void hlenCommand(redisClient *c);
759 static void zremrangebyrankCommand(redisClient *c);
760 static void zunionstoreCommand(redisClient *c);
761 static void zinterstoreCommand(redisClient *c);
762 static void hkeysCommand(redisClient *c);
763 static void hvalsCommand(redisClient *c);
764 static void hgetallCommand(redisClient *c);
765 static void hexistsCommand(redisClient *c);
766 static void configCommand(redisClient *c);
767 static void hincrbyCommand(redisClient *c);
768 static void subscribeCommand(redisClient *c);
769 static void unsubscribeCommand(redisClient *c);
770 static void psubscribeCommand(redisClient *c);
771 static void punsubscribeCommand(redisClient *c);
772 static void publishCommand(redisClient *c);
773 static void watchCommand(redisClient *c);
774 static void unwatchCommand(redisClient *c);
775
776 /*================================= Globals ================================= */
777
778 /* Global vars */
779 static struct redisServer server; /* server global state */
780 static struct redisCommand *commandTable;
781 static struct redisCommand readonlyCommandTable[] = {
782 {"get",getCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
783 {"set",setCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
784 {"setnx",setnxCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
785 {"setex",setexCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
786 {"append",appendCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
787 {"substr",substrCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
788 {"del",delCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
789 {"exists",existsCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
790 {"incr",incrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
791 {"decr",decrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
792 {"mget",mgetCommand,-2,REDIS_CMD_INLINE,NULL,1,-1,1},
793 {"rpush",rpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
794 {"lpush",lpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
795 {"rpop",rpopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
796 {"lpop",lpopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
797 {"brpop",brpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
798 {"blpop",blpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
799 {"llen",llenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
800 {"lindex",lindexCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
801 {"lset",lsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
802 {"lrange",lrangeCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
803 {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
804 {"lrem",lremCommand,4,REDIS_CMD_BULK,NULL,1,1,1},
805 {"rpoplpush",rpoplpushcommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,2,1},
806 {"sadd",saddCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
807 {"srem",sremCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
808 {"smove",smoveCommand,4,REDIS_CMD_BULK,NULL,1,2,1},
809 {"sismember",sismemberCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
810 {"scard",scardCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
811 {"spop",spopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
812 {"srandmember",srandmemberCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
813 {"sinter",sinterCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
814 {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
815 {"sunion",sunionCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
816 {"sunionstore",sunionstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
817 {"sdiff",sdiffCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
818 {"sdiffstore",sdiffstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
819 {"smembers",sinterCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
820 {"zadd",zaddCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
821 {"zincrby",zincrbyCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
822 {"zrem",zremCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
823 {"zremrangebyscore",zremrangebyscoreCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
824 {"zremrangebyrank",zremrangebyrankCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
825 {"zunionstore",zunionstoreCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
826 {"zinterstore",zinterstoreCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
827 {"zrange",zrangeCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
828 {"zrangebyscore",zrangebyscoreCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
829 {"zcount",zcountCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
830 {"zrevrange",zrevrangeCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
831 {"zcard",zcardCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
832 {"zscore",zscoreCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
833 {"zrank",zrankCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
834 {"zrevrank",zrevrankCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
835 {"hset",hsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
836 {"hsetnx",hsetnxCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
837 {"hget",hgetCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
838 {"hmset",hmsetCommand,-4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
839 {"hmget",hmgetCommand,-3,REDIS_CMD_BULK,NULL,1,1,1},
840 {"hincrby",hincrbyCommand,4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
841 {"hdel",hdelCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
842 {"hlen",hlenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
843 {"hkeys",hkeysCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
844 {"hvals",hvalsCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
845 {"hgetall",hgetallCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
846 {"hexists",hexistsCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
847 {"incrby",incrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
848 {"decrby",decrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
849 {"getset",getsetCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
850 {"mset",msetCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,-1,2},
851 {"msetnx",msetnxCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,-1,2},
852 {"randomkey",randomkeyCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
853 {"select",selectCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
854 {"move",moveCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
855 {"rename",renameCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
856 {"renamenx",renamenxCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
857 {"expire",expireCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
858 {"expireat",expireatCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
859 {"keys",keysCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
860 {"dbsize",dbsizeCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
861 {"auth",authCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
862 {"ping",pingCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
863 {"echo",echoCommand,2,REDIS_CMD_BULK,NULL,0,0,0},
864 {"save",saveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
865 {"bgsave",bgsaveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
866 {"bgrewriteaof",bgrewriteaofCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
867 {"shutdown",shutdownCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
868 {"lastsave",lastsaveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
869 {"type",typeCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
870 {"multi",multiCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
871 {"exec",execCommand,1,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,execBlockClientOnSwappedKeys,0,0,0},
872 {"discard",discardCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
873 {"sync",syncCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
874 {"flushdb",flushdbCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
875 {"flushall",flushallCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
876 {"sort",sortCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
877 {"info",infoCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
878 {"monitor",monitorCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
879 {"ttl",ttlCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
880 {"slaveof",slaveofCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
881 {"debug",debugCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
882 {"config",configCommand,-2,REDIS_CMD_BULK,NULL,0,0,0},
883 {"subscribe",subscribeCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
884 {"unsubscribe",unsubscribeCommand,-1,REDIS_CMD_INLINE,NULL,0,0,0},
885 {"psubscribe",psubscribeCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
886 {"punsubscribe",punsubscribeCommand,-1,REDIS_CMD_INLINE,NULL,0,0,0},
887 {"publish",publishCommand,3,REDIS_CMD_BULK|REDIS_CMD_FORCE_REPLICATION,NULL,0,0,0},
888 {"watch",watchCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
889 {"unwatch",unwatchCommand,1,REDIS_CMD_INLINE,NULL,0,0,0}
890 };
891
892 /*============================ Utility functions ============================ */
893
894 /* Glob-style pattern matching. */
895 static int stringmatchlen(const char *pattern, int patternLen,
896 const char *string, int stringLen, int nocase)
897 {
898 while(patternLen) {
899 switch(pattern[0]) {
900 case '*':
901 while (pattern[1] == '*') {
902 pattern++;
903 patternLen--;
904 }
905 if (patternLen == 1)
906 return 1; /* match */
907 while(stringLen) {
908 if (stringmatchlen(pattern+1, patternLen-1,
909 string, stringLen, nocase))
910 return 1; /* match */
911 string++;
912 stringLen--;
913 }
914 return 0; /* no match */
915 break;
916 case '?':
917 if (stringLen == 0)
918 return 0; /* no match */
919 string++;
920 stringLen--;
921 break;
922 case '[':
923 {
924 int not, match;
925
926 pattern++;
927 patternLen--;
928 not = pattern[0] == '^';
929 if (not) {
930 pattern++;
931 patternLen--;
932 }
933 match = 0;
934 while(1) {
935 if (pattern[0] == '\\') {
936 pattern++;
937 patternLen--;
938 if (pattern[0] == string[0])
939 match = 1;
940 } else if (pattern[0] == ']') {
941 break;
942 } else if (patternLen == 0) {
943 pattern--;
944 patternLen++;
945 break;
946 } else if (pattern[1] == '-' && patternLen >= 3) {
947 int start = pattern[0];
948 int end = pattern[2];
949 int c = string[0];
950 if (start > end) {
951 int t = start;
952 start = end;
953 end = t;
954 }
955 if (nocase) {
956 start = tolower(start);
957 end = tolower(end);
958 c = tolower(c);
959 }
960 pattern += 2;
961 patternLen -= 2;
962 if (c >= start && c <= end)
963 match = 1;
964 } else {
965 if (!nocase) {
966 if (pattern[0] == string[0])
967 match = 1;
968 } else {
969 if (tolower((int)pattern[0]) == tolower((int)string[0]))
970 match = 1;
971 }
972 }
973 pattern++;
974 patternLen--;
975 }
976 if (not)
977 match = !match;
978 if (!match)
979 return 0; /* no match */
980 string++;
981 stringLen--;
982 break;
983 }
984 case '\\':
985 if (patternLen >= 2) {
986 pattern++;
987 patternLen--;
988 }
989 /* fall through */
990 default:
991 if (!nocase) {
992 if (pattern[0] != string[0])
993 return 0; /* no match */
994 } else {
995 if (tolower((int)pattern[0]) != tolower((int)string[0]))
996 return 0; /* no match */
997 }
998 string++;
999 stringLen--;
1000 break;
1001 }
1002 pattern++;
1003 patternLen--;
1004 if (stringLen == 0) {
1005 while(*pattern == '*') {
1006 pattern++;
1007 patternLen--;
1008 }
1009 break;
1010 }
1011 }
1012 if (patternLen == 0 && stringLen == 0)
1013 return 1;
1014 return 0;
1015 }
1016
1017 static int stringmatch(const char *pattern, const char *string, int nocase) {
1018 return stringmatchlen(pattern,strlen(pattern),string,strlen(string),nocase);
1019 }
1020
1021 /* Convert a string representing an amount of memory into the number of
1022 * bytes, so for instance memtoll("1Gi") will return 1073741824 that is
1023 * (1024*1024*1024).
1024 *
1025 * On parsing error, if *err is not NULL, it's set to 1, otherwise it's
1026 * set to 0 */
1027 static long long memtoll(const char *p, int *err) {
1028 const char *u;
1029 char buf[128];
1030 long mul; /* unit multiplier */
1031 long long val;
1032 unsigned int digits;
1033
1034 if (err) *err = 0;
1035 /* Search the first non digit character. */
1036 u = p;
1037 if (*u == '-') u++;
1038 while(*u && isdigit(*u)) u++;
1039 if (*u == '\0' || !strcasecmp(u,"b")) {
1040 mul = 1;
1041 } else if (!strcasecmp(u,"k")) {
1042 mul = 1000;
1043 } else if (!strcasecmp(u,"kb")) {
1044 mul = 1024;
1045 } else if (!strcasecmp(u,"m")) {
1046 mul = 1000*1000;
1047 } else if (!strcasecmp(u,"mb")) {
1048 mul = 1024*1024;
1049 } else if (!strcasecmp(u,"g")) {
1050 mul = 1000L*1000*1000;
1051 } else if (!strcasecmp(u,"gb")) {
1052 mul = 1024L*1024*1024;
1053 } else {
1054 if (err) *err = 1;
1055 mul = 1;
1056 }
1057 digits = u-p;
1058 if (digits >= sizeof(buf)) {
1059 if (err) *err = 1;
1060 return LLONG_MAX;
1061 }
1062 memcpy(buf,p,digits);
1063 buf[digits] = '\0';
1064 val = strtoll(buf,NULL,10);
1065 return val*mul;
1066 }
1067
1068 /* Convert a long long into a string. Returns the number of
1069 * characters needed to represent the number, that can be shorter if passed
1070 * buffer length is not enough to store the whole number. */
1071 static int ll2string(char *s, size_t len, long long value) {
1072 char buf[32], *p;
1073 unsigned long long v;
1074 size_t l;
1075
1076 if (len == 0) return 0;
1077 v = (value < 0) ? -value : value;
1078 p = buf+31; /* point to the last character */
1079 do {
1080 *p-- = '0'+(v%10);
1081 v /= 10;
1082 } while(v);
1083 if (value < 0) *p-- = '-';
1084 p++;
1085 l = 32-(p-buf);
1086 if (l+1 > len) l = len-1; /* Make sure it fits, including the nul term */
1087 memcpy(s,p,l);
1088 s[l] = '\0';
1089 return l;
1090 }
1091
1092 static void redisLog(int level, const char *fmt, ...) {
1093 va_list ap;
1094 FILE *fp;
1095
1096 fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
1097 if (!fp) return;
1098
1099 va_start(ap, fmt);
1100 if (level >= server.verbosity) {
1101 char *c = ".-*#";
1102 char buf[64];
1103 time_t now;
1104
1105 now = time(NULL);
1106 strftime(buf,64,"%d %b %H:%M:%S",localtime(&now));
1107 fprintf(fp,"[%d] %s %c ",(int)getpid(),buf,c[level]);
1108 vfprintf(fp, fmt, ap);
1109 fprintf(fp,"\n");
1110 fflush(fp);
1111 }
1112 va_end(ap);
1113
1114 if (server.logfile) fclose(fp);
1115 }
1116
1117 /*====================== Hash table type implementation ==================== */
1118
1119 /* This is an hash table type that uses the SDS dynamic strings libary as
1120 * keys and radis objects as values (objects can hold SDS strings,
1121 * lists, sets). */
1122
1123 static void dictVanillaFree(void *privdata, void *val)
1124 {
1125 DICT_NOTUSED(privdata);
1126 zfree(val);
1127 }
1128
1129 static void dictListDestructor(void *privdata, void *val)
1130 {
1131 DICT_NOTUSED(privdata);
1132 listRelease((list*)val);
1133 }
1134
1135 static int dictSdsKeyCompare(void *privdata, const void *key1,
1136 const void *key2)
1137 {
1138 int l1,l2;
1139 DICT_NOTUSED(privdata);
1140
1141 l1 = sdslen((sds)key1);
1142 l2 = sdslen((sds)key2);
1143 if (l1 != l2) return 0;
1144 return memcmp(key1, key2, l1) == 0;
1145 }
1146
1147 static void dictRedisObjectDestructor(void *privdata, void *val)
1148 {
1149 DICT_NOTUSED(privdata);
1150
1151 if (val == NULL) return; /* Values of swapped out keys as set to NULL */
1152 decrRefCount(val);
1153 }
1154
1155 static void dictSdsDestructor(void *privdata, void *val)
1156 {
1157 DICT_NOTUSED(privdata);
1158
1159 sdsfree(val);
1160 }
1161
1162 static int dictObjKeyCompare(void *privdata, const void *key1,
1163 const void *key2)
1164 {
1165 const robj *o1 = key1, *o2 = key2;
1166 return dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
1167 }
1168
1169 static unsigned int dictObjHash(const void *key) {
1170 const robj *o = key;
1171 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1172 }
1173
1174 static unsigned int dictSdsHash(const void *key) {
1175 return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
1176 }
1177
1178 static int dictEncObjKeyCompare(void *privdata, const void *key1,
1179 const void *key2)
1180 {
1181 robj *o1 = (robj*) key1, *o2 = (robj*) key2;
1182 int cmp;
1183
1184 if (o1->encoding == REDIS_ENCODING_INT &&
1185 o2->encoding == REDIS_ENCODING_INT)
1186 return o1->ptr == o2->ptr;
1187
1188 o1 = getDecodedObject(o1);
1189 o2 = getDecodedObject(o2);
1190 cmp = dictSdsKeyCompare(privdata,o1->ptr,o2->ptr);
1191 decrRefCount(o1);
1192 decrRefCount(o2);
1193 return cmp;
1194 }
1195
1196 static unsigned int dictEncObjHash(const void *key) {
1197 robj *o = (robj*) key;
1198
1199 if (o->encoding == REDIS_ENCODING_RAW) {
1200 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1201 } else {
1202 if (o->encoding == REDIS_ENCODING_INT) {
1203 char buf[32];
1204 int len;
1205
1206 len = ll2string(buf,32,(long)o->ptr);
1207 return dictGenHashFunction((unsigned char*)buf, len);
1208 } else {
1209 unsigned int hash;
1210
1211 o = getDecodedObject(o);
1212 hash = dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1213 decrRefCount(o);
1214 return hash;
1215 }
1216 }
1217 }
1218
1219 /* Sets type */
1220 static dictType setDictType = {
1221 dictEncObjHash, /* hash function */
1222 NULL, /* key dup */
1223 NULL, /* val dup */
1224 dictEncObjKeyCompare, /* key compare */
1225 dictRedisObjectDestructor, /* key destructor */
1226 NULL /* val destructor */
1227 };
1228
1229 /* Sorted sets hash (note: a skiplist is used in addition to the hash table) */
1230 static dictType zsetDictType = {
1231 dictEncObjHash, /* hash function */
1232 NULL, /* key dup */
1233 NULL, /* val dup */
1234 dictEncObjKeyCompare, /* key compare */
1235 dictRedisObjectDestructor, /* key destructor */
1236 dictVanillaFree /* val destructor of malloc(sizeof(double)) */
1237 };
1238
1239 /* Db->dict, keys are sds strings, vals are Redis objects. */
1240 static dictType dbDictType = {
1241 dictSdsHash, /* hash function */
1242 NULL, /* key dup */
1243 NULL, /* val dup */
1244 dictSdsKeyCompare, /* key compare */
1245 dictSdsDestructor, /* key destructor */
1246 dictRedisObjectDestructor /* val destructor */
1247 };
1248
1249 /* Db->expires */
1250 static dictType keyptrDictType = {
1251 dictSdsHash, /* hash function */
1252 NULL, /* key dup */
1253 NULL, /* val dup */
1254 dictSdsKeyCompare, /* key compare */
1255 dictSdsDestructor, /* key destructor */
1256 NULL /* val destructor */
1257 };
1258
1259 /* Hash type hash table (note that small hashes are represented with zimpaps) */
1260 static dictType hashDictType = {
1261 dictEncObjHash, /* hash function */
1262 NULL, /* key dup */
1263 NULL, /* val dup */
1264 dictEncObjKeyCompare, /* key compare */
1265 dictRedisObjectDestructor, /* key destructor */
1266 dictRedisObjectDestructor /* val destructor */
1267 };
1268
1269 /* Keylist hash table type has unencoded redis objects as keys and
1270 * lists as values. It's used for blocking operations (BLPOP) and to
1271 * map swapped keys to a list of clients waiting for this keys to be loaded. */
1272 static dictType keylistDictType = {
1273 dictObjHash, /* hash function */
1274 NULL, /* key dup */
1275 NULL, /* val dup */
1276 dictObjKeyCompare, /* key compare */
1277 dictRedisObjectDestructor, /* key destructor */
1278 dictListDestructor /* val destructor */
1279 };
1280
1281 static void version();
1282
1283 /* ========================= Random utility functions ======================= */
1284
1285 /* Redis generally does not try to recover from out of memory conditions
1286 * when allocating objects or strings, it is not clear if it will be possible
1287 * to report this condition to the client since the networking layer itself
1288 * is based on heap allocation for send buffers, so we simply abort.
1289 * At least the code will be simpler to read... */
1290 static void oom(const char *msg) {
1291 redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
1292 sleep(1);
1293 abort();
1294 }
1295
1296 /* ====================== Redis server networking stuff ===================== */
1297 static void closeTimedoutClients(void) {
1298 redisClient *c;
1299 listNode *ln;
1300 time_t now = time(NULL);
1301 listIter li;
1302
1303 listRewind(server.clients,&li);
1304 while ((ln = listNext(&li)) != NULL) {
1305 c = listNodeValue(ln);
1306 if (server.maxidletime &&
1307 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
1308 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
1309 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
1310 listLength(c->pubsub_patterns) == 0 &&
1311 (now - c->lastinteraction > server.maxidletime))
1312 {
1313 redisLog(REDIS_VERBOSE,"Closing idle client");
1314 freeClient(c);
1315 } else if (c->flags & REDIS_BLOCKED) {
1316 if (c->blockingto != 0 && c->blockingto < now) {
1317 addReply(c,shared.nullmultibulk);
1318 unblockClientWaitingData(c);
1319 }
1320 }
1321 }
1322 }
1323
1324 static int htNeedsResize(dict *dict) {
1325 long long size, used;
1326
1327 size = dictSlots(dict);
1328 used = dictSize(dict);
1329 return (size && used && size > DICT_HT_INITIAL_SIZE &&
1330 (used*100/size < REDIS_HT_MINFILL));
1331 }
1332
1333 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
1334 * we resize the hash table to save memory */
1335 static void tryResizeHashTables(void) {
1336 int j;
1337
1338 for (j = 0; j < server.dbnum; j++) {
1339 if (htNeedsResize(server.db[j].dict))
1340 dictResize(server.db[j].dict);
1341 if (htNeedsResize(server.db[j].expires))
1342 dictResize(server.db[j].expires);
1343 }
1344 }
1345
1346 /* Our hash table implementation performs rehashing incrementally while
1347 * we write/read from the hash table. Still if the server is idle, the hash
1348 * table will use two tables for a long time. So we try to use 1 millisecond
1349 * of CPU time at every serverCron() loop in order to rehash some key. */
1350 static void incrementallyRehash(void) {
1351 int j;
1352
1353 for (j = 0; j < server.dbnum; j++) {
1354 if (dictIsRehashing(server.db[j].dict)) {
1355 dictRehashMilliseconds(server.db[j].dict,1);
1356 break; /* already used our millisecond for this loop... */
1357 }
1358 }
1359 }
1360
1361 /* A background saving child (BGSAVE) terminated its work. Handle this. */
1362 void backgroundSaveDoneHandler(int statloc) {
1363 int exitcode = WEXITSTATUS(statloc);
1364 int bysignal = WIFSIGNALED(statloc);
1365
1366 if (!bysignal && exitcode == 0) {
1367 redisLog(REDIS_NOTICE,
1368 "Background saving terminated with success");
1369 server.dirty = 0;
1370 server.lastsave = time(NULL);
1371 } else if (!bysignal && exitcode != 0) {
1372 redisLog(REDIS_WARNING, "Background saving error");
1373 } else {
1374 redisLog(REDIS_WARNING,
1375 "Background saving terminated by signal %d", WTERMSIG(statloc));
1376 rdbRemoveTempFile(server.bgsavechildpid);
1377 }
1378 server.bgsavechildpid = -1;
1379 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1380 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1381 updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR);
1382 }
1383
1384 /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
1385 * Handle this. */
1386 void backgroundRewriteDoneHandler(int statloc) {
1387 int exitcode = WEXITSTATUS(statloc);
1388 int bysignal = WIFSIGNALED(statloc);
1389
1390 if (!bysignal && exitcode == 0) {
1391 int fd;
1392 char tmpfile[256];
1393
1394 redisLog(REDIS_NOTICE,
1395 "Background append only file rewriting terminated with success");
1396 /* Now it's time to flush the differences accumulated by the parent */
1397 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) server.bgrewritechildpid);
1398 fd = open(tmpfile,O_WRONLY|O_APPEND);
1399 if (fd == -1) {
1400 redisLog(REDIS_WARNING, "Not able to open the temp append only file produced by the child: %s", strerror(errno));
1401 goto cleanup;
1402 }
1403 /* Flush our data... */
1404 if (write(fd,server.bgrewritebuf,sdslen(server.bgrewritebuf)) !=
1405 (signed) sdslen(server.bgrewritebuf)) {
1406 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));
1407 close(fd);
1408 goto cleanup;
1409 }
1410 redisLog(REDIS_NOTICE,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server.bgrewritebuf));
1411 /* Now our work is to rename the temp file into the stable file. And
1412 * switch the file descriptor used by the server for append only. */
1413 if (rename(tmpfile,server.appendfilename) == -1) {
1414 redisLog(REDIS_WARNING,"Can't rename the temp append only file into the stable one: %s", strerror(errno));
1415 close(fd);
1416 goto cleanup;
1417 }
1418 /* Mission completed... almost */
1419 redisLog(REDIS_NOTICE,"Append only file successfully rewritten.");
1420 if (server.appendfd != -1) {
1421 /* If append only is actually enabled... */
1422 close(server.appendfd);
1423 server.appendfd = fd;
1424 if (server.appendfsync != APPENDFSYNC_NO) aof_fsync(fd);
1425 server.appendseldb = -1; /* Make sure it will issue SELECT */
1426 redisLog(REDIS_NOTICE,"The new append only file was selected for future appends.");
1427 } else {
1428 /* If append only is disabled we just generate a dump in this
1429 * format. Why not? */
1430 close(fd);
1431 }
1432 } else if (!bysignal && exitcode != 0) {
1433 redisLog(REDIS_WARNING, "Background append only file rewriting error");
1434 } else {
1435 redisLog(REDIS_WARNING,
1436 "Background append only file rewriting terminated by signal %d",
1437 WTERMSIG(statloc));
1438 }
1439 cleanup:
1440 sdsfree(server.bgrewritebuf);
1441 server.bgrewritebuf = sdsempty();
1442 aofRemoveTempFile(server.bgrewritechildpid);
1443 server.bgrewritechildpid = -1;
1444 }
1445
1446 /* This function is called once a background process of some kind terminates,
1447 * as we want to avoid resizing the hash tables when there is a child in order
1448 * to play well with copy-on-write (otherwise when a resize happens lots of
1449 * memory pages are copied). The goal of this function is to update the ability
1450 * for dict.c to resize the hash tables accordingly to the fact we have o not
1451 * running childs. */
1452 static void updateDictResizePolicy(void) {
1453 if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1)
1454 dictEnableResize();
1455 else
1456 dictDisableResize();
1457 }
1458
1459 static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
1460 int j, loops = server.cronloops++;
1461 REDIS_NOTUSED(eventLoop);
1462 REDIS_NOTUSED(id);
1463 REDIS_NOTUSED(clientData);
1464
1465 /* We take a cached value of the unix time in the global state because
1466 * with virtual memory and aging there is to store the current time
1467 * in objects at every object access, and accuracy is not needed.
1468 * To access a global var is faster than calling time(NULL) */
1469 server.unixtime = time(NULL);
1470 /* We have just 21 bits per object for LRU information.
1471 * So we use an (eventually wrapping) LRU clock with minutes resolution.
1472 *
1473 * When we need to select what object to swap, we compute the minimum
1474 * time distance between the current lruclock and the object last access
1475 * lruclock info. Even if clocks will wrap on overflow, there is
1476 * the interesting property that we are sure that at least
1477 * ABS(A-B) minutes passed between current time and timestamp B.
1478 *
1479 * This is not precise but we don't need at all precision, but just
1480 * something statistically reasonable.
1481 */
1482 server.lruclock = (time(NULL)/60)&((1<<21)-1);
1483
1484 /* We received a SIGTERM, shutting down here in a safe way, as it is
1485 * not ok doing so inside the signal handler. */
1486 if (server.shutdown_asap) {
1487 if (prepareForShutdown() == REDIS_OK) exit(0);
1488 redisLog(REDIS_WARNING,"SIGTERM received but errors trying to shut down the server, check the logs for more information");
1489 }
1490
1491 /* Show some info about non-empty databases */
1492 for (j = 0; j < server.dbnum; j++) {
1493 long long size, used, vkeys;
1494
1495 size = dictSlots(server.db[j].dict);
1496 used = dictSize(server.db[j].dict);
1497 vkeys = dictSize(server.db[j].expires);
1498 if (!(loops % 50) && (used || vkeys)) {
1499 redisLog(REDIS_VERBOSE,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j,used,vkeys,size);
1500 /* dictPrintStats(server.dict); */
1501 }
1502 }
1503
1504 /* We don't want to resize the hash tables while a bacground saving
1505 * is in progress: the saving child is created using fork() that is
1506 * implemented with a copy-on-write semantic in most modern systems, so
1507 * if we resize the HT while there is the saving child at work actually
1508 * a lot of memory movements in the parent will cause a lot of pages
1509 * copied. */
1510 if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1) {
1511 if (!(loops % 10)) tryResizeHashTables();
1512 if (server.activerehashing) incrementallyRehash();
1513 }
1514
1515 /* Show information about connected clients */
1516 if (!(loops % 50)) {
1517 redisLog(REDIS_VERBOSE,"%d clients connected (%d slaves), %zu bytes in use",
1518 listLength(server.clients)-listLength(server.slaves),
1519 listLength(server.slaves),
1520 zmalloc_used_memory());
1521 }
1522
1523 /* Close connections of timedout clients */
1524 if ((server.maxidletime && !(loops % 100)) || server.blpop_blocked_clients)
1525 closeTimedoutClients();
1526
1527 /* Check if a background saving or AOF rewrite in progress terminated */
1528 if (server.bgsavechildpid != -1 || server.bgrewritechildpid != -1) {
1529 int statloc;
1530 pid_t pid;
1531
1532 if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
1533 if (pid == server.bgsavechildpid) {
1534 backgroundSaveDoneHandler(statloc);
1535 } else {
1536 backgroundRewriteDoneHandler(statloc);
1537 }
1538 updateDictResizePolicy();
1539 }
1540 } else {
1541 /* If there is not a background saving in progress check if
1542 * we have to save now */
1543 time_t now = time(NULL);
1544 for (j = 0; j < server.saveparamslen; j++) {
1545 struct saveparam *sp = server.saveparams+j;
1546
1547 if (server.dirty >= sp->changes &&
1548 now-server.lastsave > sp->seconds) {
1549 redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
1550 sp->changes, sp->seconds);
1551 rdbSaveBackground(server.dbfilename);
1552 break;
1553 }
1554 }
1555 }
1556
1557 /* Try to expire a few timed out keys. The algorithm used is adaptive and
1558 * will use few CPU cycles if there are few expiring keys, otherwise
1559 * it will get more aggressive to avoid that too much memory is used by
1560 * keys that can be removed from the keyspace. */
1561 for (j = 0; j < server.dbnum; j++) {
1562 int expired;
1563 redisDb *db = server.db+j;
1564
1565 /* Continue to expire if at the end of the cycle more than 25%
1566 * of the keys were expired. */
1567 do {
1568 long num = dictSize(db->expires);
1569 time_t now = time(NULL);
1570
1571 expired = 0;
1572 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
1573 num = REDIS_EXPIRELOOKUPS_PER_CRON;
1574 while (num--) {
1575 dictEntry *de;
1576 time_t t;
1577
1578 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
1579 t = (time_t) dictGetEntryVal(de);
1580 if (now > t) {
1581 sds key = dictGetEntryKey(de);
1582 robj *keyobj = createStringObject(key,sdslen(key));
1583
1584 dbDelete(db,keyobj);
1585 decrRefCount(keyobj);
1586 expired++;
1587 server.stat_expiredkeys++;
1588 }
1589 }
1590 } while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4);
1591 }
1592
1593 /* Swap a few keys on disk if we are over the memory limit and VM
1594 * is enbled. Try to free objects from the free list first. */
1595 if (vmCanSwapOut()) {
1596 while (server.vm_enabled && zmalloc_used_memory() >
1597 server.vm_max_memory)
1598 {
1599 int retval;
1600
1601 if (tryFreeOneObjectFromFreelist() == REDIS_OK) continue;
1602 retval = (server.vm_max_threads == 0) ?
1603 vmSwapOneObjectBlocking() :
1604 vmSwapOneObjectThreaded();
1605 if (retval == REDIS_ERR && !(loops % 300) &&
1606 zmalloc_used_memory() >
1607 (server.vm_max_memory+server.vm_max_memory/10))
1608 {
1609 redisLog(REDIS_WARNING,"WARNING: vm-max-memory limit exceeded by more than 10%% but unable to swap more objects out!");
1610 }
1611 /* Note that when using threade I/O we free just one object,
1612 * because anyway when the I/O thread in charge to swap this
1613 * object out will finish, the handler of completed jobs
1614 * will try to swap more objects if we are still out of memory. */
1615 if (retval == REDIS_ERR || server.vm_max_threads > 0) break;
1616 }
1617 }
1618
1619 /* Check if we should connect to a MASTER */
1620 if (server.replstate == REDIS_REPL_CONNECT && !(loops % 10)) {
1621 redisLog(REDIS_NOTICE,"Connecting to MASTER...");
1622 if (syncWithMaster() == REDIS_OK) {
1623 redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync succeeded");
1624 if (server.appendonly) rewriteAppendOnlyFileBackground();
1625 }
1626 }
1627 return 100;
1628 }
1629
1630 /* This function gets called every time Redis is entering the
1631 * main loop of the event driven library, that is, before to sleep
1632 * for ready file descriptors. */
1633 static void beforeSleep(struct aeEventLoop *eventLoop) {
1634 REDIS_NOTUSED(eventLoop);
1635
1636 /* Awake clients that got all the swapped keys they requested */
1637 if (server.vm_enabled && listLength(server.io_ready_clients)) {
1638 listIter li;
1639 listNode *ln;
1640
1641 listRewind(server.io_ready_clients,&li);
1642 while((ln = listNext(&li))) {
1643 redisClient *c = ln->value;
1644 struct redisCommand *cmd;
1645
1646 /* Resume the client. */
1647 listDelNode(server.io_ready_clients,ln);
1648 c->flags &= (~REDIS_IO_WAIT);
1649 server.vm_blocked_clients--;
1650 aeCreateFileEvent(server.el, c->fd, AE_READABLE,
1651 readQueryFromClient, c);
1652 cmd = lookupCommand(c->argv[0]->ptr);
1653 assert(cmd != NULL);
1654 call(c,cmd);
1655 resetClient(c);
1656 /* There may be more data to process in the input buffer. */
1657 if (c->querybuf && sdslen(c->querybuf) > 0)
1658 processInputBuffer(c);
1659 }
1660 }
1661 /* Write the AOF buffer on disk */
1662 flushAppendOnlyFile();
1663 }
1664
1665 static void createSharedObjects(void) {
1666 int j;
1667
1668 shared.crlf = createObject(REDIS_STRING,sdsnew("\r\n"));
1669 shared.ok = createObject(REDIS_STRING,sdsnew("+OK\r\n"));
1670 shared.err = createObject(REDIS_STRING,sdsnew("-ERR\r\n"));
1671 shared.emptybulk = createObject(REDIS_STRING,sdsnew("$0\r\n\r\n"));
1672 shared.czero = createObject(REDIS_STRING,sdsnew(":0\r\n"));
1673 shared.cone = createObject(REDIS_STRING,sdsnew(":1\r\n"));
1674 shared.nullbulk = createObject(REDIS_STRING,sdsnew("$-1\r\n"));
1675 shared.nullmultibulk = createObject(REDIS_STRING,sdsnew("*-1\r\n"));
1676 shared.emptymultibulk = createObject(REDIS_STRING,sdsnew("*0\r\n"));
1677 shared.pong = createObject(REDIS_STRING,sdsnew("+PONG\r\n"));
1678 shared.queued = createObject(REDIS_STRING,sdsnew("+QUEUED\r\n"));
1679 shared.wrongtypeerr = createObject(REDIS_STRING,sdsnew(
1680 "-ERR Operation against a key holding the wrong kind of value\r\n"));
1681 shared.nokeyerr = createObject(REDIS_STRING,sdsnew(
1682 "-ERR no such key\r\n"));
1683 shared.syntaxerr = createObject(REDIS_STRING,sdsnew(
1684 "-ERR syntax error\r\n"));
1685 shared.sameobjecterr = createObject(REDIS_STRING,sdsnew(
1686 "-ERR source and destination objects are the same\r\n"));
1687 shared.outofrangeerr = createObject(REDIS_STRING,sdsnew(
1688 "-ERR index out of range\r\n"));
1689 shared.space = createObject(REDIS_STRING,sdsnew(" "));
1690 shared.colon = createObject(REDIS_STRING,sdsnew(":"));
1691 shared.plus = createObject(REDIS_STRING,sdsnew("+"));
1692 shared.select0 = createStringObject("select 0\r\n",10);
1693 shared.select1 = createStringObject("select 1\r\n",10);
1694 shared.select2 = createStringObject("select 2\r\n",10);
1695 shared.select3 = createStringObject("select 3\r\n",10);
1696 shared.select4 = createStringObject("select 4\r\n",10);
1697 shared.select5 = createStringObject("select 5\r\n",10);
1698 shared.select6 = createStringObject("select 6\r\n",10);
1699 shared.select7 = createStringObject("select 7\r\n",10);
1700 shared.select8 = createStringObject("select 8\r\n",10);
1701 shared.select9 = createStringObject("select 9\r\n",10);
1702 shared.messagebulk = createStringObject("$7\r\nmessage\r\n",13);
1703 shared.pmessagebulk = createStringObject("$8\r\npmessage\r\n",14);
1704 shared.subscribebulk = createStringObject("$9\r\nsubscribe\r\n",15);
1705 shared.unsubscribebulk = createStringObject("$11\r\nunsubscribe\r\n",18);
1706 shared.psubscribebulk = createStringObject("$10\r\npsubscribe\r\n",17);
1707 shared.punsubscribebulk = createStringObject("$12\r\npunsubscribe\r\n",19);
1708 shared.mbulk3 = createStringObject("*3\r\n",4);
1709 shared.mbulk4 = createStringObject("*4\r\n",4);
1710 for (j = 0; j < REDIS_SHARED_INTEGERS; j++) {
1711 shared.integers[j] = createObject(REDIS_STRING,(void*)(long)j);
1712 shared.integers[j]->encoding = REDIS_ENCODING_INT;
1713 }
1714 }
1715
1716 static void appendServerSaveParams(time_t seconds, int changes) {
1717 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
1718 server.saveparams[server.saveparamslen].seconds = seconds;
1719 server.saveparams[server.saveparamslen].changes = changes;
1720 server.saveparamslen++;
1721 }
1722
1723 static void resetServerSaveParams() {
1724 zfree(server.saveparams);
1725 server.saveparams = NULL;
1726 server.saveparamslen = 0;
1727 }
1728
1729 static void initServerConfig() {
1730 server.dbnum = REDIS_DEFAULT_DBNUM;
1731 server.port = REDIS_SERVERPORT;
1732 server.verbosity = REDIS_VERBOSE;
1733 server.maxidletime = REDIS_MAXIDLETIME;
1734 server.saveparams = NULL;
1735 server.logfile = NULL; /* NULL = log on standard output */
1736 server.bindaddr = NULL;
1737 server.glueoutputbuf = 1;
1738 server.daemonize = 0;
1739 server.appendonly = 0;
1740 server.appendfsync = APPENDFSYNC_EVERYSEC;
1741 server.no_appendfsync_on_rewrite = 0;
1742 server.lastfsync = time(NULL);
1743 server.appendfd = -1;
1744 server.appendseldb = -1; /* Make sure the first time will not match */
1745 server.pidfile = zstrdup("/var/run/redis.pid");
1746 server.dbfilename = zstrdup("dump.rdb");
1747 server.appendfilename = zstrdup("appendonly.aof");
1748 server.requirepass = NULL;
1749 server.rdbcompression = 1;
1750 server.activerehashing = 1;
1751 server.maxclients = 0;
1752 server.blpop_blocked_clients = 0;
1753 server.maxmemory = 0;
1754 server.vm_enabled = 0;
1755 server.vm_swap_file = zstrdup("/tmp/redis-%p.vm");
1756 server.vm_page_size = 256; /* 256 bytes per page */
1757 server.vm_pages = 1024*1024*100; /* 104 millions of pages */
1758 server.vm_max_memory = 1024LL*1024*1024*1; /* 1 GB of RAM */
1759 server.vm_max_threads = 4;
1760 server.vm_blocked_clients = 0;
1761 server.hash_max_zipmap_entries = REDIS_HASH_MAX_ZIPMAP_ENTRIES;
1762 server.hash_max_zipmap_value = REDIS_HASH_MAX_ZIPMAP_VALUE;
1763 server.list_max_ziplist_entries = REDIS_LIST_MAX_ZIPLIST_ENTRIES;
1764 server.list_max_ziplist_value = REDIS_LIST_MAX_ZIPLIST_VALUE;
1765 server.shutdown_asap = 0;
1766
1767 resetServerSaveParams();
1768
1769 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1770 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1771 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1772 /* Replication related */
1773 server.isslave = 0;
1774 server.masterauth = NULL;
1775 server.masterhost = NULL;
1776 server.masterport = 6379;
1777 server.master = NULL;
1778 server.replstate = REDIS_REPL_NONE;
1779
1780 /* Double constants initialization */
1781 R_Zero = 0.0;
1782 R_PosInf = 1.0/R_Zero;
1783 R_NegInf = -1.0/R_Zero;
1784 R_Nan = R_Zero/R_Zero;
1785 }
1786
1787 static void initServer() {
1788 int j;
1789
1790 signal(SIGHUP, SIG_IGN);
1791 signal(SIGPIPE, SIG_IGN);
1792 setupSigSegvAction();
1793
1794 server.devnull = fopen("/dev/null","w");
1795 if (server.devnull == NULL) {
1796 redisLog(REDIS_WARNING, "Can't open /dev/null: %s", server.neterr);
1797 exit(1);
1798 }
1799 server.clients = listCreate();
1800 server.slaves = listCreate();
1801 server.monitors = listCreate();
1802 server.objfreelist = listCreate();
1803 createSharedObjects();
1804 server.el = aeCreateEventLoop();
1805 server.db = zmalloc(sizeof(redisDb)*server.dbnum);
1806 server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr);
1807 if (server.fd == -1) {
1808 redisLog(REDIS_WARNING, "Opening TCP port: %s", server.neterr);
1809 exit(1);
1810 }
1811 for (j = 0; j < server.dbnum; j++) {
1812 server.db[j].dict = dictCreate(&dbDictType,NULL);
1813 server.db[j].expires = dictCreate(&keyptrDictType,NULL);
1814 server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL);
1815 server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
1816 if (server.vm_enabled)
1817 server.db[j].io_keys = dictCreate(&keylistDictType,NULL);
1818 server.db[j].id = j;
1819 }
1820 server.pubsub_channels = dictCreate(&keylistDictType,NULL);
1821 server.pubsub_patterns = listCreate();
1822 listSetFreeMethod(server.pubsub_patterns,freePubsubPattern);
1823 listSetMatchMethod(server.pubsub_patterns,listMatchPubsubPattern);
1824 server.cronloops = 0;
1825 server.bgsavechildpid = -1;
1826 server.bgrewritechildpid = -1;
1827 server.bgrewritebuf = sdsempty();
1828 server.aofbuf = sdsempty();
1829 server.lastsave = time(NULL);
1830 server.dirty = 0;
1831 server.stat_numcommands = 0;
1832 server.stat_numconnections = 0;
1833 server.stat_expiredkeys = 0;
1834 server.stat_starttime = time(NULL);
1835 server.unixtime = time(NULL);
1836 aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
1837 if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,
1838 acceptHandler, NULL) == AE_ERR) oom("creating file event");
1839
1840 if (server.appendonly) {
1841 server.appendfd = open(server.appendfilename,O_WRONLY|O_APPEND|O_CREAT,0644);
1842 if (server.appendfd == -1) {
1843 redisLog(REDIS_WARNING, "Can't open the append-only file: %s",
1844 strerror(errno));
1845 exit(1);
1846 }
1847 }
1848
1849 if (server.vm_enabled) vmInit();
1850 }
1851
1852 /* Empty the whole database */
1853 static long long emptyDb() {
1854 int j;
1855 long long removed = 0;
1856
1857 for (j = 0; j < server.dbnum; j++) {
1858 removed += dictSize(server.db[j].dict);
1859 dictEmpty(server.db[j].dict);
1860 dictEmpty(server.db[j].expires);
1861 }
1862 return removed;
1863 }
1864
1865 static int yesnotoi(char *s) {
1866 if (!strcasecmp(s,"yes")) return 1;
1867 else if (!strcasecmp(s,"no")) return 0;
1868 else return -1;
1869 }
1870
1871 /* I agree, this is a very rudimental way to load a configuration...
1872 will improve later if the config gets more complex */
1873 static void loadServerConfig(char *filename) {
1874 FILE *fp;
1875 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
1876 int linenum = 0;
1877 sds line = NULL;
1878
1879 if (filename[0] == '-' && filename[1] == '\0')
1880 fp = stdin;
1881 else {
1882 if ((fp = fopen(filename,"r")) == NULL) {
1883 redisLog(REDIS_WARNING, "Fatal error, can't open config file '%s'", filename);
1884 exit(1);
1885 }
1886 }
1887
1888 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
1889 sds *argv;
1890 int argc, j;
1891
1892 linenum++;
1893 line = sdsnew(buf);
1894 line = sdstrim(line," \t\r\n");
1895
1896 /* Skip comments and blank lines*/
1897 if (line[0] == '#' || line[0] == '\0') {
1898 sdsfree(line);
1899 continue;
1900 }
1901
1902 /* Split into arguments */
1903 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
1904 sdstolower(argv[0]);
1905
1906 /* Execute config directives */
1907 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
1908 server.maxidletime = atoi(argv[1]);
1909 if (server.maxidletime < 0) {
1910 err = "Invalid timeout value"; goto loaderr;
1911 }
1912 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
1913 server.port = atoi(argv[1]);
1914 if (server.port < 1 || server.port > 65535) {
1915 err = "Invalid port"; goto loaderr;
1916 }
1917 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
1918 server.bindaddr = zstrdup(argv[1]);
1919 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
1920 int seconds = atoi(argv[1]);
1921 int changes = atoi(argv[2]);
1922 if (seconds < 1 || changes < 0) {
1923 err = "Invalid save parameters"; goto loaderr;
1924 }
1925 appendServerSaveParams(seconds,changes);
1926 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
1927 if (chdir(argv[1]) == -1) {
1928 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
1929 argv[1], strerror(errno));
1930 exit(1);
1931 }
1932 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
1933 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
1934 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
1935 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
1936 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
1937 else {
1938 err = "Invalid log level. Must be one of debug, notice, warning";
1939 goto loaderr;
1940 }
1941 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
1942 FILE *logfp;
1943
1944 server.logfile = zstrdup(argv[1]);
1945 if (!strcasecmp(server.logfile,"stdout")) {
1946 zfree(server.logfile);
1947 server.logfile = NULL;
1948 }
1949 if (server.logfile) {
1950 /* Test if we are able to open the file. The server will not
1951 * be able to abort just for this problem later... */
1952 logfp = fopen(server.logfile,"a");
1953 if (logfp == NULL) {
1954 err = sdscatprintf(sdsempty(),
1955 "Can't open the log file: %s", strerror(errno));
1956 goto loaderr;
1957 }
1958 fclose(logfp);
1959 }
1960 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
1961 server.dbnum = atoi(argv[1]);
1962 if (server.dbnum < 1) {
1963 err = "Invalid number of databases"; goto loaderr;
1964 }
1965 } else if (!strcasecmp(argv[0],"include") && argc == 2) {
1966 loadServerConfig(argv[1]);
1967 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
1968 server.maxclients = atoi(argv[1]);
1969 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
1970 server.maxmemory = memtoll(argv[1],NULL);
1971 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
1972 server.masterhost = sdsnew(argv[1]);
1973 server.masterport = atoi(argv[2]);
1974 server.replstate = REDIS_REPL_CONNECT;
1975 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
1976 server.masterauth = zstrdup(argv[1]);
1977 } else if (!strcasecmp(argv[0],"glueoutputbuf") && argc == 2) {
1978 if ((server.glueoutputbuf = yesnotoi(argv[1])) == -1) {
1979 err = "argument must be 'yes' or 'no'"; goto loaderr;
1980 }
1981 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
1982 if ((server.rdbcompression = yesnotoi(argv[1])) == -1) {
1983 err = "argument must be 'yes' or 'no'"; goto loaderr;
1984 }
1985 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
1986 if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
1987 err = "argument must be 'yes' or 'no'"; goto loaderr;
1988 }
1989 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
1990 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
1991 err = "argument must be 'yes' or 'no'"; goto loaderr;
1992 }
1993 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
1994 if ((server.appendonly = yesnotoi(argv[1])) == -1) {
1995 err = "argument must be 'yes' or 'no'"; goto loaderr;
1996 }
1997 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
1998 zfree(server.appendfilename);
1999 server.appendfilename = zstrdup(argv[1]);
2000 } else if (!strcasecmp(argv[0],"no-appendfsync-on-rewrite")
2001 && argc == 2) {
2002 if ((server.no_appendfsync_on_rewrite= yesnotoi(argv[1])) == -1) {
2003 err = "argument must be 'yes' or 'no'"; goto loaderr;
2004 }
2005 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
2006 if (!strcasecmp(argv[1],"no")) {
2007 server.appendfsync = APPENDFSYNC_NO;
2008 } else if (!strcasecmp(argv[1],"always")) {
2009 server.appendfsync = APPENDFSYNC_ALWAYS;
2010 } else if (!strcasecmp(argv[1],"everysec")) {
2011 server.appendfsync = APPENDFSYNC_EVERYSEC;
2012 } else {
2013 err = "argument must be 'no', 'always' or 'everysec'";
2014 goto loaderr;
2015 }
2016 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
2017 server.requirepass = zstrdup(argv[1]);
2018 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
2019 zfree(server.pidfile);
2020 server.pidfile = zstrdup(argv[1]);
2021 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
2022 zfree(server.dbfilename);
2023 server.dbfilename = zstrdup(argv[1]);
2024 } else if (!strcasecmp(argv[0],"vm-enabled") && argc == 2) {
2025 if ((server.vm_enabled = yesnotoi(argv[1])) == -1) {
2026 err = "argument must be 'yes' or 'no'"; goto loaderr;
2027 }
2028 } else if (!strcasecmp(argv[0],"vm-swap-file") && argc == 2) {
2029 zfree(server.vm_swap_file);
2030 server.vm_swap_file = zstrdup(argv[1]);
2031 } else if (!strcasecmp(argv[0],"vm-max-memory") && argc == 2) {
2032 server.vm_max_memory = memtoll(argv[1],NULL);
2033 } else if (!strcasecmp(argv[0],"vm-page-size") && argc == 2) {
2034 server.vm_page_size = memtoll(argv[1], NULL);
2035 } else if (!strcasecmp(argv[0],"vm-pages") && argc == 2) {
2036 server.vm_pages = memtoll(argv[1], NULL);
2037 } else if (!strcasecmp(argv[0],"vm-max-threads") && argc == 2) {
2038 server.vm_max_threads = strtoll(argv[1], NULL, 10);
2039 } else if (!strcasecmp(argv[0],"hash-max-zipmap-entries") && argc == 2){
2040 server.hash_max_zipmap_entries = memtoll(argv[1], NULL);
2041 } else if (!strcasecmp(argv[0],"hash-max-zipmap-value") && argc == 2){
2042 server.hash_max_zipmap_value = memtoll(argv[1], NULL);
2043 } else if (!strcasecmp(argv[0],"list-max-ziplist-entries") && argc == 2){
2044 server.list_max_ziplist_entries = memtoll(argv[1], NULL);
2045 } else if (!strcasecmp(argv[0],"list-max-ziplist-value") && argc == 2){
2046 server.list_max_ziplist_value = memtoll(argv[1], NULL);
2047 } else {
2048 err = "Bad directive or wrong number of arguments"; goto loaderr;
2049 }
2050 for (j = 0; j < argc; j++)
2051 sdsfree(argv[j]);
2052 zfree(argv);
2053 sdsfree(line);
2054 }
2055 if (fp != stdin) fclose(fp);
2056 return;
2057
2058 loaderr:
2059 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
2060 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
2061 fprintf(stderr, ">>> '%s'\n", line);
2062 fprintf(stderr, "%s\n", err);
2063 exit(1);
2064 }
2065
2066 static void freeClientArgv(redisClient *c) {
2067 int j;
2068
2069 for (j = 0; j < c->argc; j++)
2070 decrRefCount(c->argv[j]);
2071 for (j = 0; j < c->mbargc; j++)
2072 decrRefCount(c->mbargv[j]);
2073 c->argc = 0;
2074 c->mbargc = 0;
2075 }
2076
2077 static void freeClient(redisClient *c) {
2078 listNode *ln;
2079
2080 /* Note that if the client we are freeing is blocked into a blocking
2081 * call, we have to set querybuf to NULL *before* to call
2082 * unblockClientWaitingData() to avoid processInputBuffer() will get
2083 * called. Also it is important to remove the file events after
2084 * this, because this call adds the READABLE event. */
2085 sdsfree(c->querybuf);
2086 c->querybuf = NULL;
2087 if (c->flags & REDIS_BLOCKED)
2088 unblockClientWaitingData(c);
2089
2090 /* UNWATCH all the keys */
2091 unwatchAllKeys(c);
2092 listRelease(c->watched_keys);
2093 /* Unsubscribe from all the pubsub channels */
2094 pubsubUnsubscribeAllChannels(c,0);
2095 pubsubUnsubscribeAllPatterns(c,0);
2096 dictRelease(c->pubsub_channels);
2097 listRelease(c->pubsub_patterns);
2098 /* Obvious cleanup */
2099 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
2100 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2101 listRelease(c->reply);
2102 freeClientArgv(c);
2103 close(c->fd);
2104 /* Remove from the list of clients */
2105 ln = listSearchKey(server.clients,c);
2106 redisAssert(ln != NULL);
2107 listDelNode(server.clients,ln);
2108 /* Remove from the list of clients that are now ready to be restarted
2109 * after waiting for swapped keys */
2110 if (c->flags & REDIS_IO_WAIT && listLength(c->io_keys) == 0) {
2111 ln = listSearchKey(server.io_ready_clients,c);
2112 if (ln) {
2113 listDelNode(server.io_ready_clients,ln);
2114 server.vm_blocked_clients--;
2115 }
2116 }
2117 /* Remove from the list of clients waiting for swapped keys */
2118 while (server.vm_enabled && listLength(c->io_keys)) {
2119 ln = listFirst(c->io_keys);
2120 dontWaitForSwappedKey(c,ln->value);
2121 }
2122 listRelease(c->io_keys);
2123 /* Master/slave cleanup */
2124 if (c->flags & REDIS_SLAVE) {
2125 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
2126 close(c->repldbfd);
2127 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
2128 ln = listSearchKey(l,c);
2129 redisAssert(ln != NULL);
2130 listDelNode(l,ln);
2131 }
2132 if (c->flags & REDIS_MASTER) {
2133 server.master = NULL;
2134 server.replstate = REDIS_REPL_CONNECT;
2135 }
2136 /* Release memory */
2137 zfree(c->argv);
2138 zfree(c->mbargv);
2139 freeClientMultiState(c);
2140 zfree(c);
2141 }
2142
2143 #define GLUEREPLY_UP_TO (1024)
2144 static void glueReplyBuffersIfNeeded(redisClient *c) {
2145 int copylen = 0;
2146 char buf[GLUEREPLY_UP_TO];
2147 listNode *ln;
2148 listIter li;
2149 robj *o;
2150
2151 listRewind(c->reply,&li);
2152 while((ln = listNext(&li))) {
2153 int objlen;
2154
2155 o = ln->value;
2156 objlen = sdslen(o->ptr);
2157 if (copylen + objlen <= GLUEREPLY_UP_TO) {
2158 memcpy(buf+copylen,o->ptr,objlen);
2159 copylen += objlen;
2160 listDelNode(c->reply,ln);
2161 } else {
2162 if (copylen == 0) return;
2163 break;
2164 }
2165 }
2166 /* Now the output buffer is empty, add the new single element */
2167 o = createObject(REDIS_STRING,sdsnewlen(buf,copylen));
2168 listAddNodeHead(c->reply,o);
2169 }
2170
2171 static void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
2172 redisClient *c = privdata;
2173 int nwritten = 0, totwritten = 0, objlen;
2174 robj *o;
2175 REDIS_NOTUSED(el);
2176 REDIS_NOTUSED(mask);
2177
2178 /* Use writev() if we have enough buffers to send */
2179 if (!server.glueoutputbuf &&
2180 listLength(c->reply) > REDIS_WRITEV_THRESHOLD &&
2181 !(c->flags & REDIS_MASTER))
2182 {
2183 sendReplyToClientWritev(el, fd, privdata, mask);
2184 return;
2185 }
2186
2187 while(listLength(c->reply)) {
2188 if (server.glueoutputbuf && listLength(c->reply) > 1)
2189 glueReplyBuffersIfNeeded(c);
2190
2191 o = listNodeValue(listFirst(c->reply));
2192 objlen = sdslen(o->ptr);
2193
2194 if (objlen == 0) {
2195 listDelNode(c->reply,listFirst(c->reply));
2196 continue;
2197 }
2198
2199 if (c->flags & REDIS_MASTER) {
2200 /* Don't reply to a master */
2201 nwritten = objlen - c->sentlen;
2202 } else {
2203 nwritten = write(fd, ((char*)o->ptr)+c->sentlen, objlen - c->sentlen);
2204 if (nwritten <= 0) break;
2205 }
2206 c->sentlen += nwritten;
2207 totwritten += nwritten;
2208 /* If we fully sent the object on head go to the next one */
2209 if (c->sentlen == objlen) {
2210 listDelNode(c->reply,listFirst(c->reply));
2211 c->sentlen = 0;
2212 }
2213 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
2214 * bytes, in a single threaded server it's a good idea to serve
2215 * other clients as well, even if a very large request comes from
2216 * super fast link that is always able to accept data (in real world
2217 * scenario think about 'KEYS *' against the loopback interfae) */
2218 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
2219 }
2220 if (nwritten == -1) {
2221 if (errno == EAGAIN) {
2222 nwritten = 0;
2223 } else {
2224 redisLog(REDIS_VERBOSE,
2225 "Error writing to client: %s", strerror(errno));
2226 freeClient(c);
2227 return;
2228 }
2229 }
2230 if (totwritten > 0) c->lastinteraction = time(NULL);
2231 if (listLength(c->reply) == 0) {
2232 c->sentlen = 0;
2233 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2234 }
2235 }
2236
2237 static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask)
2238 {
2239 redisClient *c = privdata;
2240 int nwritten = 0, totwritten = 0, objlen, willwrite;
2241 robj *o;
2242 struct iovec iov[REDIS_WRITEV_IOVEC_COUNT];
2243 int offset, ion = 0;
2244 REDIS_NOTUSED(el);
2245 REDIS_NOTUSED(mask);
2246
2247 listNode *node;
2248 while (listLength(c->reply)) {
2249 offset = c->sentlen;
2250 ion = 0;
2251 willwrite = 0;
2252
2253 /* fill-in the iov[] array */
2254 for(node = listFirst(c->reply); node; node = listNextNode(node)) {
2255 o = listNodeValue(node);
2256 objlen = sdslen(o->ptr);
2257
2258 if (totwritten + objlen - offset > REDIS_MAX_WRITE_PER_EVENT)
2259 break;
2260
2261 if(ion == REDIS_WRITEV_IOVEC_COUNT)
2262 break; /* no more iovecs */
2263
2264 iov[ion].iov_base = ((char*)o->ptr) + offset;
2265 iov[ion].iov_len = objlen - offset;
2266 willwrite += objlen - offset;
2267 offset = 0; /* just for the first item */
2268 ion++;
2269 }
2270
2271 if(willwrite == 0)
2272 break;
2273
2274 /* write all collected blocks at once */
2275 if((nwritten = writev(fd, iov, ion)) < 0) {
2276 if (errno != EAGAIN) {
2277 redisLog(REDIS_VERBOSE,
2278 "Error writing to client: %s", strerror(errno));
2279 freeClient(c);
2280 return;
2281 }
2282 break;
2283 }
2284
2285 totwritten += nwritten;
2286 offset = c->sentlen;
2287
2288 /* remove written robjs from c->reply */
2289 while (nwritten && listLength(c->reply)) {
2290 o = listNodeValue(listFirst(c->reply));
2291 objlen = sdslen(o->ptr);
2292
2293 if(nwritten >= objlen - offset) {
2294 listDelNode(c->reply, listFirst(c->reply));
2295 nwritten -= objlen - offset;
2296 c->sentlen = 0;
2297 } else {
2298 /* partial write */
2299 c->sentlen += nwritten;
2300 break;
2301 }
2302 offset = 0;
2303 }
2304 }
2305
2306 if (totwritten > 0)
2307 c->lastinteraction = time(NULL);
2308
2309 if (listLength(c->reply) == 0) {
2310 c->sentlen = 0;
2311 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2312 }
2313 }
2314
2315 static int qsortRedisCommands(const void *r1, const void *r2) {
2316 return strcasecmp(
2317 ((struct redisCommand*)r1)->name,
2318 ((struct redisCommand*)r2)->name);
2319 }
2320
2321 static void sortCommandTable() {
2322 /* Copy and sort the read-only version of the command table */
2323 commandTable = (struct redisCommand*)malloc(sizeof(readonlyCommandTable));
2324 memcpy(commandTable,readonlyCommandTable,sizeof(readonlyCommandTable));
2325 qsort(commandTable,
2326 sizeof(readonlyCommandTable)/sizeof(struct redisCommand),
2327 sizeof(struct redisCommand),qsortRedisCommands);
2328 }
2329
2330 static struct redisCommand *lookupCommand(char *name) {
2331 struct redisCommand tmp = {name,NULL,0,0,NULL,0,0,0};
2332 return bsearch(
2333 &tmp,
2334 commandTable,
2335 sizeof(readonlyCommandTable)/sizeof(struct redisCommand),
2336 sizeof(struct redisCommand),
2337 qsortRedisCommands);
2338 }
2339
2340 /* resetClient prepare the client to process the next command */
2341 static void resetClient(redisClient *c) {
2342 freeClientArgv(c);
2343 c->bulklen = -1;
2344 c->multibulk = 0;
2345 }
2346
2347 /* Call() is the core of Redis execution of a command */
2348 static void call(redisClient *c, struct redisCommand *cmd) {
2349 long long dirty;
2350
2351 dirty = server.dirty;
2352 cmd->proc(c);
2353 dirty = server.dirty-dirty;
2354
2355 if (server.appendonly && dirty)
2356 feedAppendOnlyFile(cmd,c->db->id,c->argv,c->argc);
2357 if ((dirty || cmd->flags & REDIS_CMD_FORCE_REPLICATION) &&
2358 listLength(server.slaves))
2359 replicationFeedSlaves(server.slaves,c->db->id,c->argv,c->argc);
2360 if (listLength(server.monitors))
2361 replicationFeedMonitors(server.monitors,c->db->id,c->argv,c->argc);
2362 server.stat_numcommands++;
2363 }
2364
2365 /* If this function gets called we already read a whole
2366 * command, argments are in the client argv/argc fields.
2367 * processCommand() execute the command or prepare the
2368 * server for a bulk read from the client.
2369 *
2370 * If 1 is returned the client is still alive and valid and
2371 * and other operations can be performed by the caller. Otherwise
2372 * if 0 is returned the client was destroied (i.e. after QUIT). */
2373 static int processCommand(redisClient *c) {
2374 struct redisCommand *cmd;
2375
2376 /* Free some memory if needed (maxmemory setting) */
2377 if (server.maxmemory) freeMemoryIfNeeded();
2378
2379 /* Handle the multi bulk command type. This is an alternative protocol
2380 * supported by Redis in order to receive commands that are composed of
2381 * multiple binary-safe "bulk" arguments. The latency of processing is
2382 * a bit higher but this allows things like multi-sets, so if this
2383 * protocol is used only for MSET and similar commands this is a big win. */
2384 if (c->multibulk == 0 && c->argc == 1 && ((char*)(c->argv[0]->ptr))[0] == '*') {
2385 c->multibulk = atoi(((char*)c->argv[0]->ptr)+1);
2386 if (c->multibulk <= 0) {
2387 resetClient(c);
2388 return 1;
2389 } else {
2390 decrRefCount(c->argv[c->argc-1]);
2391 c->argc--;
2392 return 1;
2393 }
2394 } else if (c->multibulk) {
2395 if (c->bulklen == -1) {
2396 if (((char*)c->argv[0]->ptr)[0] != '$') {
2397 addReplySds(c,sdsnew("-ERR multi bulk protocol error\r\n"));
2398 resetClient(c);
2399 return 1;
2400 } else {
2401 int bulklen = atoi(((char*)c->argv[0]->ptr)+1);
2402 decrRefCount(c->argv[0]);
2403 if (bulklen < 0 || bulklen > 1024*1024*1024) {
2404 c->argc--;
2405 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
2406 resetClient(c);
2407 return 1;
2408 }
2409 c->argc--;
2410 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
2411 return 1;
2412 }
2413 } else {
2414 c->mbargv = zrealloc(c->mbargv,(sizeof(robj*))*(c->mbargc+1));
2415 c->mbargv[c->mbargc] = c->argv[0];
2416 c->mbargc++;
2417 c->argc--;
2418 c->multibulk--;
2419 if (c->multibulk == 0) {
2420 robj **auxargv;
2421 int auxargc;
2422
2423 /* Here we need to swap the multi-bulk argc/argv with the
2424 * normal argc/argv of the client structure. */
2425 auxargv = c->argv;
2426 c->argv = c->mbargv;
2427 c->mbargv = auxargv;
2428
2429 auxargc = c->argc;
2430 c->argc = c->mbargc;
2431 c->mbargc = auxargc;
2432
2433 /* We need to set bulklen to something different than -1
2434 * in order for the code below to process the command without
2435 * to try to read the last argument of a bulk command as
2436 * a special argument. */
2437 c->bulklen = 0;
2438 /* continue below and process the command */
2439 } else {
2440 c->bulklen = -1;
2441 return 1;
2442 }
2443 }
2444 }
2445 /* -- end of multi bulk commands processing -- */
2446
2447 /* The QUIT command is handled as a special case. Normal command
2448 * procs are unable to close the client connection safely */
2449 if (!strcasecmp(c->argv[0]->ptr,"quit")) {
2450 freeClient(c);
2451 return 0;
2452 }
2453
2454 /* Now lookup the command and check ASAP about trivial error conditions
2455 * such wrong arity, bad command name and so forth. */
2456 cmd = lookupCommand(c->argv[0]->ptr);
2457 if (!cmd) {
2458 addReplySds(c,
2459 sdscatprintf(sdsempty(), "-ERR unknown command '%s'\r\n",
2460 (char*)c->argv[0]->ptr));
2461 resetClient(c);
2462 return 1;
2463 } else if ((cmd->arity > 0 && cmd->arity != c->argc) ||
2464 (c->argc < -cmd->arity)) {
2465 addReplySds(c,
2466 sdscatprintf(sdsempty(),
2467 "-ERR wrong number of arguments for '%s' command\r\n",
2468 cmd->name));
2469 resetClient(c);
2470 return 1;
2471 } else if (cmd->flags & REDIS_CMD_BULK && c->bulklen == -1) {
2472 /* This is a bulk command, we have to read the last argument yet. */
2473 int bulklen = atoi(c->argv[c->argc-1]->ptr);
2474
2475 decrRefCount(c->argv[c->argc-1]);
2476 if (bulklen < 0 || bulklen > 1024*1024*1024) {
2477 c->argc--;
2478 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
2479 resetClient(c);
2480 return 1;
2481 }
2482 c->argc--;
2483 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
2484 /* It is possible that the bulk read is already in the
2485 * buffer. Check this condition and handle it accordingly.
2486 * This is just a fast path, alternative to call processInputBuffer().
2487 * It's a good idea since the code is small and this condition
2488 * happens most of the times. */
2489 if ((signed)sdslen(c->querybuf) >= c->bulklen) {
2490 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2491 c->argc++;
2492 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
2493 } else {
2494 /* Otherwise return... there is to read the last argument
2495 * from the socket. */
2496 return 1;
2497 }
2498 }
2499 /* Let's try to encode the bulk object to save space. */
2500 if (cmd->flags & REDIS_CMD_BULK)
2501 c->argv[c->argc-1] = tryObjectEncoding(c->argv[c->argc-1]);
2502
2503 /* Check if the user is authenticated */
2504 if (server.requirepass && !c->authenticated && cmd->proc != authCommand) {
2505 addReplySds(c,sdsnew("-ERR operation not permitted\r\n"));
2506 resetClient(c);
2507 return 1;
2508 }
2509
2510 /* Handle the maxmemory directive */
2511 if (server.maxmemory && (cmd->flags & REDIS_CMD_DENYOOM) &&
2512 zmalloc_used_memory() > server.maxmemory)
2513 {
2514 addReplySds(c,sdsnew("-ERR command not allowed when used memory > 'maxmemory'\r\n"));
2515 resetClient(c);
2516 return 1;
2517 }
2518
2519 /* Only allow SUBSCRIBE and UNSUBSCRIBE in the context of Pub/Sub */
2520 if ((dictSize(c->pubsub_channels) > 0 || listLength(c->pubsub_patterns) > 0)
2521 &&
2522 cmd->proc != subscribeCommand && cmd->proc != unsubscribeCommand &&
2523 cmd->proc != psubscribeCommand && cmd->proc != punsubscribeCommand) {
2524 addReplySds(c,sdsnew("-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context\r\n"));
2525 resetClient(c);
2526 return 1;
2527 }
2528
2529 /* Exec the command */
2530 if (c->flags & REDIS_MULTI &&
2531 cmd->proc != execCommand && cmd->proc != discardCommand &&
2532 cmd->proc != multiCommand && cmd->proc != watchCommand)
2533 {
2534 queueMultiCommand(c,cmd);
2535 addReply(c,shared.queued);
2536 } else {
2537 if (server.vm_enabled && server.vm_max_threads > 0 &&
2538 blockClientOnSwappedKeys(c,cmd)) return 1;
2539 call(c,cmd);
2540 }
2541
2542 /* Prepare the client for the next command */
2543 resetClient(c);
2544 return 1;
2545 }
2546
2547 static void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
2548 listNode *ln;
2549 listIter li;
2550 int outc = 0, j;
2551 robj **outv;
2552 /* We need 1+(ARGS*3) objects since commands are using the new protocol
2553 * and we one 1 object for the first "*<count>\r\n" multibulk count, then
2554 * for every additional object we have "$<count>\r\n" + object + "\r\n". */
2555 robj *static_outv[REDIS_STATIC_ARGS*3+1];
2556 robj *lenobj;
2557
2558 if (argc <= REDIS_STATIC_ARGS) {
2559 outv = static_outv;
2560 } else {
2561 outv = zmalloc(sizeof(robj*)*(argc*3+1));
2562 }
2563
2564 lenobj = createObject(REDIS_STRING,
2565 sdscatprintf(sdsempty(), "*%d\r\n", argc));
2566 lenobj->refcount = 0;
2567 outv[outc++] = lenobj;
2568 for (j = 0; j < argc; j++) {
2569 lenobj = createObject(REDIS_STRING,
2570 sdscatprintf(sdsempty(),"$%lu\r\n",
2571 (unsigned long) stringObjectLen(argv[j])));
2572 lenobj->refcount = 0;
2573 outv[outc++] = lenobj;
2574 outv[outc++] = argv[j];
2575 outv[outc++] = shared.crlf;
2576 }
2577
2578 /* Increment all the refcounts at start and decrement at end in order to
2579 * be sure to free objects if there is no slave in a replication state
2580 * able to be feed with commands */
2581 for (j = 0; j < outc; j++) incrRefCount(outv[j]);
2582 listRewind(slaves,&li);
2583 while((ln = listNext(&li))) {
2584 redisClient *slave = ln->value;
2585
2586 /* Don't feed slaves that are still waiting for BGSAVE to start */
2587 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue;
2588
2589 /* Feed all the other slaves, MONITORs and so on */
2590 if (slave->slaveseldb != dictid) {
2591 robj *selectcmd;
2592
2593 switch(dictid) {
2594 case 0: selectcmd = shared.select0; break;
2595 case 1: selectcmd = shared.select1; break;
2596 case 2: selectcmd = shared.select2; break;
2597 case 3: selectcmd = shared.select3; break;
2598 case 4: selectcmd = shared.select4; break;
2599 case 5: selectcmd = shared.select5; break;
2600 case 6: selectcmd = shared.select6; break;
2601 case 7: selectcmd = shared.select7; break;
2602 case 8: selectcmd = shared.select8; break;
2603 case 9: selectcmd = shared.select9; break;
2604 default:
2605 selectcmd = createObject(REDIS_STRING,
2606 sdscatprintf(sdsempty(),"select %d\r\n",dictid));
2607 selectcmd->refcount = 0;
2608 break;
2609 }
2610 addReply(slave,selectcmd);
2611 slave->slaveseldb = dictid;
2612 }
2613 for (j = 0; j < outc; j++) addReply(slave,outv[j]);
2614 }
2615 for (j = 0; j < outc; j++) decrRefCount(outv[j]);
2616 if (outv != static_outv) zfree(outv);
2617 }
2618
2619 static sds sdscatrepr(sds s, char *p, size_t len) {
2620 s = sdscatlen(s,"\"",1);
2621 while(len--) {
2622 switch(*p) {
2623 case '\\':
2624 case '"':
2625 s = sdscatprintf(s,"\\%c",*p);
2626 break;
2627 case '\n': s = sdscatlen(s,"\\n",1); break;
2628 case '\r': s = sdscatlen(s,"\\r",1); break;
2629 case '\t': s = sdscatlen(s,"\\t",1); break;
2630 case '\a': s = sdscatlen(s,"\\a",1); break;
2631 case '\b': s = sdscatlen(s,"\\b",1); break;
2632 default:
2633 if (isprint(*p))
2634 s = sdscatprintf(s,"%c",*p);
2635 else
2636 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
2637 break;
2638 }
2639 p++;
2640 }
2641 return sdscatlen(s,"\"",1);
2642 }
2643
2644 static void replicationFeedMonitors(list *monitors, int dictid, robj **argv, int argc) {
2645 listNode *ln;
2646 listIter li;
2647 int j;
2648 sds cmdrepr = sdsnew("+");
2649 robj *cmdobj;
2650 struct timeval tv;
2651
2652 gettimeofday(&tv,NULL);
2653 cmdrepr = sdscatprintf(cmdrepr,"%ld.%ld ",(long)tv.tv_sec,(long)tv.tv_usec);
2654 if (dictid != 0) cmdrepr = sdscatprintf(cmdrepr,"(db %d) ", dictid);
2655
2656 for (j = 0; j < argc; j++) {
2657 if (argv[j]->encoding == REDIS_ENCODING_INT) {
2658 cmdrepr = sdscatprintf(cmdrepr, "%ld", (long)argv[j]->ptr);
2659 } else {
2660 cmdrepr = sdscatrepr(cmdrepr,(char*)argv[j]->ptr,
2661 sdslen(argv[j]->ptr));
2662 }
2663 if (j != argc-1)
2664 cmdrepr = sdscatlen(cmdrepr," ",1);
2665 }
2666 cmdrepr = sdscatlen(cmdrepr,"\r\n",2);
2667 cmdobj = createObject(REDIS_STRING,cmdrepr);
2668
2669 listRewind(monitors,&li);
2670 while((ln = listNext(&li))) {
2671 redisClient *monitor = ln->value;
2672 addReply(monitor,cmdobj);
2673 }
2674 decrRefCount(cmdobj);
2675 }
2676
2677 static void processInputBuffer(redisClient *c) {
2678 again:
2679 /* Before to process the input buffer, make sure the client is not
2680 * waitig for a blocking operation such as BLPOP. Note that the first
2681 * iteration the client is never blocked, otherwise the processInputBuffer
2682 * would not be called at all, but after the execution of the first commands
2683 * in the input buffer the client may be blocked, and the "goto again"
2684 * will try to reiterate. The following line will make it return asap. */
2685 if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return;
2686 if (c->bulklen == -1) {
2687 /* Read the first line of the query */
2688 char *p = strchr(c->querybuf,'\n');
2689 size_t querylen;
2690
2691 if (p) {
2692 sds query, *argv;
2693 int argc, j;
2694
2695 query = c->querybuf;
2696 c->querybuf = sdsempty();
2697 querylen = 1+(p-(query));
2698 if (sdslen(query) > querylen) {
2699 /* leave data after the first line of the query in the buffer */
2700 c->querybuf = sdscatlen(c->querybuf,query+querylen,sdslen(query)-querylen);
2701 }
2702 *p = '\0'; /* remove "\n" */
2703 if (*(p-1) == '\r') *(p-1) = '\0'; /* and "\r" if any */
2704 sdsupdatelen(query);
2705
2706 /* Now we can split the query in arguments */
2707 argv = sdssplitlen(query,sdslen(query)," ",1,&argc);
2708 sdsfree(query);
2709
2710 if (c->argv) zfree(c->argv);
2711 c->argv = zmalloc(sizeof(robj*)*argc);
2712
2713 for (j = 0; j < argc; j++) {
2714 if (sdslen(argv[j])) {
2715 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
2716 c->argc++;
2717 } else {
2718 sdsfree(argv[j]);
2719 }
2720 }
2721 zfree(argv);
2722 if (c->argc) {
2723 /* Execute the command. If the client is still valid
2724 * after processCommand() return and there is something
2725 * on the query buffer try to process the next command. */
2726 if (processCommand(c) && sdslen(c->querybuf)) goto again;
2727 } else {
2728 /* Nothing to process, argc == 0. Just process the query
2729 * buffer if it's not empty or return to the caller */
2730 if (sdslen(c->querybuf)) goto again;
2731 }
2732 return;
2733 } else if (sdslen(c->querybuf) >= REDIS_REQUEST_MAX_SIZE) {
2734 redisLog(REDIS_VERBOSE, "Client protocol error");
2735 freeClient(c);
2736 return;
2737 }
2738 } else {
2739 /* Bulk read handling. Note that if we are at this point
2740 the client already sent a command terminated with a newline,
2741 we are reading the bulk data that is actually the last
2742 argument of the command. */
2743 int qbl = sdslen(c->querybuf);
2744
2745 if (c->bulklen <= qbl) {
2746 /* Copy everything but the final CRLF as final argument */
2747 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2748 c->argc++;
2749 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
2750 /* Process the command. If the client is still valid after
2751 * the processing and there is more data in the buffer
2752 * try to parse it. */
2753 if (processCommand(c) && sdslen(c->querybuf)) goto again;
2754 return;
2755 }
2756 }
2757 }
2758
2759 static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
2760 redisClient *c = (redisClient*) privdata;
2761 char buf[REDIS_IOBUF_LEN];
2762 int nread;
2763 REDIS_NOTUSED(el);
2764 REDIS_NOTUSED(mask);
2765
2766 nread = read(fd, buf, REDIS_IOBUF_LEN);
2767 if (nread == -1) {
2768 if (errno == EAGAIN) {
2769 nread = 0;
2770 } else {
2771 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
2772 freeClient(c);
2773 return;
2774 }
2775 } else if (nread == 0) {
2776 redisLog(REDIS_VERBOSE, "Client closed connection");
2777 freeClient(c);
2778 return;
2779 }
2780 if (nread) {
2781 c->querybuf = sdscatlen(c->querybuf, buf, nread);
2782 c->lastinteraction = time(NULL);
2783 } else {
2784 return;
2785 }
2786 processInputBuffer(c);
2787 }
2788
2789 static int selectDb(redisClient *c, int id) {
2790 if (id < 0 || id >= server.dbnum)
2791 return REDIS_ERR;
2792 c->db = &server.db[id];
2793 return REDIS_OK;
2794 }
2795
2796 static void *dupClientReplyValue(void *o) {
2797 incrRefCount((robj*)o);
2798 return o;
2799 }
2800
2801 static int listMatchObjects(void *a, void *b) {
2802 return equalStringObjects(a,b);
2803 }
2804
2805 static redisClient *createClient(int fd) {
2806 redisClient *c = zmalloc(sizeof(*c));
2807
2808 anetNonBlock(NULL,fd);
2809 anetTcpNoDelay(NULL,fd);
2810 if (!c) return NULL;
2811 selectDb(c,0);
2812 c->fd = fd;
2813 c->querybuf = sdsempty();
2814 c->argc = 0;
2815 c->argv = NULL;
2816 c->bulklen = -1;
2817 c->multibulk = 0;
2818 c->mbargc = 0;
2819 c->mbargv = NULL;
2820 c->sentlen = 0;
2821 c->flags = 0;
2822 c->lastinteraction = time(NULL);
2823 c->authenticated = 0;
2824 c->replstate = REDIS_REPL_NONE;
2825 c->reply = listCreate();
2826 listSetFreeMethod(c->reply,decrRefCount);
2827 listSetDupMethod(c->reply,dupClientReplyValue);
2828 c->blocking_keys = NULL;
2829 c->blocking_keys_num = 0;
2830 c->io_keys = listCreate();
2831 c->watched_keys = listCreate();
2832 listSetFreeMethod(c->io_keys,decrRefCount);
2833 c->pubsub_channels = dictCreate(&setDictType,NULL);
2834 c->pubsub_patterns = listCreate();
2835 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
2836 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
2837 if (aeCreateFileEvent(server.el, c->fd, AE_READABLE,
2838 readQueryFromClient, c) == AE_ERR) {
2839 freeClient(c);
2840 return NULL;
2841 }
2842 listAddNodeTail(server.clients,c);
2843 initClientMultiState(c);
2844 return c;
2845 }
2846
2847 static void addReply(redisClient *c, robj *obj) {
2848 if (listLength(c->reply) == 0 &&
2849 (c->replstate == REDIS_REPL_NONE ||
2850 c->replstate == REDIS_REPL_ONLINE) &&
2851 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
2852 sendReplyToClient, c) == AE_ERR) return;
2853
2854 if (server.vm_enabled && obj->storage != REDIS_VM_MEMORY) {
2855 obj = dupStringObject(obj);
2856 obj->refcount = 0; /* getDecodedObject() will increment the refcount */
2857 }
2858 listAddNodeTail(c->reply,getDecodedObject(obj));
2859 }
2860
2861 static void addReplySds(redisClient *c, sds s) {
2862 robj *o = createObject(REDIS_STRING,s);
2863 addReply(c,o);
2864 decrRefCount(o);
2865 }
2866
2867 static void addReplyDouble(redisClient *c, double d) {
2868 char buf[128];
2869
2870 snprintf(buf,sizeof(buf),"%.17g",d);
2871 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
2872 (unsigned long) strlen(buf),buf));
2873 }
2874
2875 static void addReplyLongLong(redisClient *c, long long ll) {
2876 char buf[128];
2877 size_t len;
2878
2879 if (ll == 0) {
2880 addReply(c,shared.czero);
2881 return;
2882 } else if (ll == 1) {
2883 addReply(c,shared.cone);
2884 return;
2885 }
2886 buf[0] = ':';
2887 len = ll2string(buf+1,sizeof(buf)-1,ll);
2888 buf[len+1] = '\r';
2889 buf[len+2] = '\n';
2890 addReplySds(c,sdsnewlen(buf,len+3));
2891 }
2892
2893 static void addReplyUlong(redisClient *c, unsigned long ul) {
2894 char buf[128];
2895 size_t len;
2896
2897 if (ul == 0) {
2898 addReply(c,shared.czero);
2899 return;
2900 } else if (ul == 1) {
2901 addReply(c,shared.cone);
2902 return;
2903 }
2904 len = snprintf(buf,sizeof(buf),":%lu\r\n",ul);
2905 addReplySds(c,sdsnewlen(buf,len));
2906 }
2907
2908 static void addReplyBulkLen(redisClient *c, robj *obj) {
2909 size_t len, intlen;
2910 char buf[128];
2911
2912 if (obj->encoding == REDIS_ENCODING_RAW) {
2913 len = sdslen(obj->ptr);
2914 } else {
2915 long n = (long)obj->ptr;
2916
2917 /* Compute how many bytes will take this integer as a radix 10 string */
2918 len = 1;
2919 if (n < 0) {
2920 len++;
2921 n = -n;
2922 }
2923 while((n = n/10) != 0) {
2924 len++;
2925 }
2926 }
2927 buf[0] = '$';
2928 intlen = ll2string(buf+1,sizeof(buf)-1,(long long)len);
2929 buf[intlen+1] = '\r';
2930 buf[intlen+2] = '\n';
2931 addReplySds(c,sdsnewlen(buf,intlen+3));
2932 }
2933
2934 static void addReplyBulk(redisClient *c, robj *obj) {
2935 addReplyBulkLen(c,obj);
2936 addReply(c,obj);
2937 addReply(c,shared.crlf);
2938 }
2939
2940 static void addReplyBulkSds(redisClient *c, sds s) {
2941 robj *o = createStringObject(s, sdslen(s));
2942 addReplyBulk(c,o);
2943 decrRefCount(o);
2944 }
2945
2946 /* In the CONFIG command we need to add vanilla C string as bulk replies */
2947 static void addReplyBulkCString(redisClient *c, char *s) {
2948 if (s == NULL) {
2949 addReply(c,shared.nullbulk);
2950 } else {
2951 robj *o = createStringObject(s,strlen(s));
2952 addReplyBulk(c,o);
2953 decrRefCount(o);
2954 }
2955 }
2956
2957 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
2958 int cport, cfd;
2959 char cip[128];
2960 redisClient *c;
2961 REDIS_NOTUSED(el);
2962 REDIS_NOTUSED(mask);
2963 REDIS_NOTUSED(privdata);
2964
2965 cfd = anetAccept(server.neterr, fd, cip, &cport);
2966 if (cfd == AE_ERR) {
2967 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
2968 return;
2969 }
2970 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
2971 if ((c = createClient(cfd)) == NULL) {
2972 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
2973 close(cfd); /* May be already closed, just ingore errors */
2974 return;
2975 }
2976 /* If maxclient directive is set and this is one client more... close the
2977 * connection. Note that we create the client instead to check before
2978 * for this condition, since now the socket is already set in nonblocking
2979 * mode and we can send an error for free using the Kernel I/O */
2980 if (server.maxclients && listLength(server.clients) > server.maxclients) {
2981 char *err = "-ERR max number of clients reached\r\n";
2982
2983 /* That's a best effort error message, don't check write errors */
2984 if (write(c->fd,err,strlen(err)) == -1) {
2985 /* Nothing to do, Just to avoid the warning... */
2986 }
2987 freeClient(c);
2988 return;
2989 }
2990 server.stat_numconnections++;
2991 }
2992
2993 /* ======================= Redis objects implementation ===================== */
2994
2995 static robj *createObject(int type, void *ptr) {
2996 robj *o;
2997
2998 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
2999 if (listLength(server.objfreelist)) {
3000 listNode *head = listFirst(server.objfreelist);
3001 o = listNodeValue(head);
3002 listDelNode(server.objfreelist,head);
3003 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
3004 } else {
3005 if (server.vm_enabled)
3006 pthread_mutex_unlock(&server.obj_freelist_mutex);
3007 o = zmalloc(sizeof(*o));
3008 }
3009 o->type = type;
3010 o->encoding = REDIS_ENCODING_RAW;
3011 o->ptr = ptr;
3012 o->refcount = 1;
3013 if (server.vm_enabled) {
3014 /* Note that this code may run in the context of an I/O thread
3015 * and accessing server.lruclock in theory is an error
3016 * (no locks). But in practice this is safe, and even if we read
3017 * garbage Redis will not fail. */
3018 o->lru = server.lruclock;
3019 o->storage = REDIS_VM_MEMORY;
3020 }
3021 return o;
3022 }
3023
3024 static robj *createStringObject(char *ptr, size_t len) {
3025 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
3026 }
3027
3028 static robj *createStringObjectFromLongLong(long long value) {
3029 robj *o;
3030 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
3031 incrRefCount(shared.integers[value]);
3032 o = shared.integers[value];
3033 } else {
3034 if (value >= LONG_MIN && value <= LONG_MAX) {
3035 o = createObject(REDIS_STRING, NULL);
3036 o->encoding = REDIS_ENCODING_INT;
3037 o->ptr = (void*)((long)value);
3038 } else {
3039 o = createObject(REDIS_STRING,sdsfromlonglong(value));
3040 }
3041 }
3042 return o;
3043 }
3044
3045 static robj *dupStringObject(robj *o) {
3046 assert(o->encoding == REDIS_ENCODING_RAW);
3047 return createStringObject(o->ptr,sdslen(o->ptr));
3048 }
3049
3050 static robj *createListObject(void) {
3051 list *l = listCreate();
3052 robj *o = createObject(REDIS_LIST,l);
3053 listSetFreeMethod(l,decrRefCount);
3054 o->encoding = REDIS_ENCODING_LIST;
3055 return o;
3056 }
3057
3058 static robj *createZiplistObject(void) {
3059 unsigned char *zl = ziplistNew();
3060 robj *o = createObject(REDIS_LIST,zl);
3061 o->encoding = REDIS_ENCODING_ZIPLIST;
3062 return o;
3063 }
3064
3065 static robj *createSetObject(void) {
3066 dict *d = dictCreate(&setDictType,NULL);
3067 return createObject(REDIS_SET,d);
3068 }
3069
3070 static robj *createHashObject(void) {
3071 /* All the Hashes start as zipmaps. Will be automatically converted
3072 * into hash tables if there are enough elements or big elements
3073 * inside. */
3074 unsigned char *zm = zipmapNew();
3075 robj *o = createObject(REDIS_HASH,zm);
3076 o->encoding = REDIS_ENCODING_ZIPMAP;
3077 return o;
3078 }
3079
3080 static robj *createZsetObject(void) {
3081 zset *zs = zmalloc(sizeof(*zs));
3082
3083 zs->dict = dictCreate(&zsetDictType,NULL);
3084 zs->zsl = zslCreate();
3085 return createObject(REDIS_ZSET,zs);
3086 }
3087
3088 static void freeStringObject(robj *o) {
3089 if (o->encoding == REDIS_ENCODING_RAW) {
3090 sdsfree(o->ptr);
3091 }
3092 }
3093
3094 static void freeListObject(robj *o) {
3095 switch (o->encoding) {
3096 case REDIS_ENCODING_LIST:
3097 listRelease((list*) o->ptr);
3098 break;
3099 case REDIS_ENCODING_ZIPLIST:
3100 zfree(o->ptr);
3101 break;
3102 default:
3103 redisPanic("Unknown list encoding type");
3104 }
3105 }
3106
3107 static void freeSetObject(robj *o) {
3108 dictRelease((dict*) o->ptr);
3109 }
3110
3111 static void freeZsetObject(robj *o) {
3112 zset *zs = o->ptr;
3113
3114 dictRelease(zs->dict);
3115 zslFree(zs->zsl);
3116 zfree(zs);
3117 }
3118
3119 static void freeHashObject(robj *o) {
3120 switch (o->encoding) {
3121 case REDIS_ENCODING_HT:
3122 dictRelease((dict*) o->ptr);
3123 break;
3124 case REDIS_ENCODING_ZIPMAP:
3125 zfree(o->ptr);
3126 break;
3127 default:
3128 redisPanic("Unknown hash encoding type");
3129 break;
3130 }
3131 }
3132
3133 static void incrRefCount(robj *o) {
3134 o->refcount++;
3135 }
3136
3137 static void decrRefCount(void *obj) {
3138 robj *o = obj;
3139
3140 /* Object is a swapped out value, or in the process of being loaded. */
3141 if (server.vm_enabled &&
3142 (o->storage == REDIS_VM_SWAPPED || o->storage == REDIS_VM_LOADING))
3143 {
3144 vmpointer *vp = obj;
3145 if (o->storage == REDIS_VM_LOADING) vmCancelThreadedIOJob(o);
3146 vmMarkPagesFree(vp->page,vp->usedpages);
3147 server.vm_stats_swapped_objects--;
3148 zfree(vp);
3149 return;
3150 }
3151
3152 if (o->refcount <= 0) redisPanic("decrRefCount against refcount <= 0");
3153 /* Object is in memory, or in the process of being swapped out.
3154 *
3155 * If the object is being swapped out, abort the operation on
3156 * decrRefCount even if the refcount does not drop to 0: the object
3157 * is referenced at least two times, as value of the key AND as
3158 * job->val in the iojob. So if we don't invalidate the iojob, when it is
3159 * done but the relevant key was removed in the meantime, the
3160 * complete jobs handler will not find the key about the job and the
3161 * assert will fail. */
3162 if (server.vm_enabled && o->storage == REDIS_VM_SWAPPING)
3163 vmCancelThreadedIOJob(o);
3164 if (--(o->refcount) == 0) {
3165 switch(o->type) {
3166 case REDIS_STRING: freeStringObject(o); break;
3167 case REDIS_LIST: freeListObject(o); break;
3168 case REDIS_SET: freeSetObject(o); break;
3169 case REDIS_ZSET: freeZsetObject(o); break;
3170 case REDIS_HASH: freeHashObject(o); break;
3171 default: redisPanic("Unknown object type"); break;
3172 }
3173 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
3174 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
3175 !listAddNodeHead(server.objfreelist,o))
3176 zfree(o);
3177 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
3178 }
3179 }
3180
3181 static int checkType(redisClient *c, robj *o, int type) {
3182 if (o->type != type) {
3183 addReply(c,shared.wrongtypeerr);
3184 return 1;
3185 }
3186 return 0;
3187 }
3188
3189 /* Check if the nul-terminated string 's' can be represented by a long
3190 * (that is, is a number that fits into long without any other space or
3191 * character before or after the digits).
3192 *
3193 * If so, the function returns REDIS_OK and *longval is set to the value
3194 * of the number. Otherwise REDIS_ERR is returned */
3195 static int isStringRepresentableAsLong(sds s, long *longval) {
3196 char buf[32], *endptr;
3197 long value;
3198 int slen;
3199
3200 value = strtol(s, &endptr, 10);
3201 if (endptr[0] != '\0') return REDIS_ERR;
3202 slen = ll2string(buf,32,value);
3203
3204 /* If the number converted back into a string is not identical
3205 * then it's not possible to encode the string as integer */
3206 if (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR;
3207 if (longval) *longval = value;
3208 return REDIS_OK;
3209 }
3210
3211 /* Try to encode a string object in order to save space */
3212 static robj *tryObjectEncoding(robj *o) {
3213 long value;
3214 sds s = o->ptr;
3215
3216 if (o->encoding != REDIS_ENCODING_RAW)
3217 return o; /* Already encoded */
3218
3219 /* It's not safe to encode shared objects: shared objects can be shared
3220 * everywhere in the "object space" of Redis. Encoded objects can only
3221 * appear as "values" (and not, for instance, as keys) */
3222 if (o->refcount > 1) return o;
3223
3224 /* Currently we try to encode only strings */
3225 redisAssert(o->type == REDIS_STRING);
3226
3227 /* Check if we can represent this string as a long integer */
3228 if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return o;
3229
3230 /* Ok, this object can be encoded */
3231 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
3232 decrRefCount(o);
3233 incrRefCount(shared.integers[value]);
3234 return shared.integers[value];
3235 } else {
3236 o->encoding = REDIS_ENCODING_INT;
3237 sdsfree(o->ptr);
3238 o->ptr = (void*) value;
3239 return o;
3240 }
3241 }
3242
3243 /* Get a decoded version of an encoded object (returned as a new object).
3244 * If the object is already raw-encoded just increment the ref count. */
3245 static robj *getDecodedObject(robj *o) {
3246 robj *dec;
3247
3248 if (o->encoding == REDIS_ENCODING_RAW) {
3249 incrRefCount(o);
3250 return o;
3251 }
3252 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_INT) {
3253 char buf[32];
3254
3255 ll2string(buf,32,(long)o->ptr);
3256 dec = createStringObject(buf,strlen(buf));
3257 return dec;
3258 } else {
3259 redisPanic("Unknown encoding type");
3260 }
3261 }
3262
3263 /* Compare two string objects via strcmp() or alike.
3264 * Note that the objects may be integer-encoded. In such a case we
3265 * use ll2string() to get a string representation of the numbers on the stack
3266 * and compare the strings, it's much faster than calling getDecodedObject().
3267 *
3268 * Important note: if objects are not integer encoded, but binary-safe strings,
3269 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
3270 * binary safe. */
3271 static int compareStringObjects(robj *a, robj *b) {
3272 redisAssert(a->type == REDIS_STRING && b->type == REDIS_STRING);
3273 char bufa[128], bufb[128], *astr, *bstr;
3274 int bothsds = 1;
3275
3276 if (a == b) return 0;
3277 if (a->encoding != REDIS_ENCODING_RAW) {
3278 ll2string(bufa,sizeof(bufa),(long) a->ptr);
3279 astr = bufa;
3280 bothsds = 0;
3281 } else {
3282 astr = a->ptr;
3283 }
3284 if (b->encoding != REDIS_ENCODING_RAW) {
3285 ll2string(bufb,sizeof(bufb),(long) b->ptr);
3286 bstr = bufb;
3287 bothsds = 0;
3288 } else {
3289 bstr = b->ptr;
3290 }
3291 return bothsds ? sdscmp(astr,bstr) : strcmp(astr,bstr);
3292 }
3293
3294 /* Equal string objects return 1 if the two objects are the same from the
3295 * point of view of a string comparison, otherwise 0 is returned. Note that
3296 * this function is faster then checking for (compareStringObject(a,b) == 0)
3297 * because it can perform some more optimization. */
3298 static int equalStringObjects(robj *a, robj *b) {
3299 if (a->encoding != REDIS_ENCODING_RAW && b->encoding != REDIS_ENCODING_RAW){
3300 return a->ptr == b->ptr;
3301 } else {
3302 return compareStringObjects(a,b) == 0;
3303 }
3304 }
3305
3306 static size_t stringObjectLen(robj *o) {
3307 redisAssert(o->type == REDIS_STRING);
3308 if (o->encoding == REDIS_ENCODING_RAW) {
3309 return sdslen(o->ptr);
3310 } else {
3311 char buf[32];
3312
3313 return ll2string(buf,32,(long)o->ptr);
3314 }
3315 }
3316
3317 static int getDoubleFromObject(robj *o, double *target) {
3318 double value;
3319 char *eptr;
3320
3321 if (o == NULL) {
3322 value = 0;
3323 } else {
3324 redisAssert(o->type == REDIS_STRING);
3325 if (o->encoding == REDIS_ENCODING_RAW) {
3326 value = strtod(o->ptr, &eptr);
3327 if (eptr[0] != '\0') return REDIS_ERR;
3328 } else if (o->encoding == REDIS_ENCODING_INT) {
3329 value = (long)o->ptr;
3330 } else {
3331 redisPanic("Unknown string encoding");
3332 }
3333 }
3334
3335 *target = value;
3336 return REDIS_OK;
3337 }
3338
3339 static int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg) {
3340 double value;
3341 if (getDoubleFromObject(o, &value) != REDIS_OK) {
3342 if (msg != NULL) {
3343 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3344 } else {
3345 addReplySds(c, sdsnew("-ERR value is not a double\r\n"));
3346 }
3347 return REDIS_ERR;
3348 }
3349
3350 *target = value;
3351 return REDIS_OK;
3352 }
3353
3354 static int getLongLongFromObject(robj *o, long long *target) {
3355 long long value;
3356 char *eptr;
3357
3358 if (o == NULL) {
3359 value = 0;
3360 } else {
3361 redisAssert(o->type == REDIS_STRING);
3362 if (o->encoding == REDIS_ENCODING_RAW) {
3363 value = strtoll(o->ptr, &eptr, 10);
3364 if (eptr[0] != '\0') return REDIS_ERR;
3365 } else if (o->encoding == REDIS_ENCODING_INT) {
3366 value = (long)o->ptr;
3367 } else {
3368 redisPanic("Unknown string encoding");
3369 }
3370 }
3371
3372 *target = value;
3373 return REDIS_OK;
3374 }
3375
3376 static int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg) {
3377 long long value;
3378 if (getLongLongFromObject(o, &value) != REDIS_OK) {
3379 if (msg != NULL) {
3380 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3381 } else {
3382 addReplySds(c, sdsnew("-ERR value is not an integer\r\n"));
3383 }
3384 return REDIS_ERR;
3385 }
3386
3387 *target = value;
3388 return REDIS_OK;
3389 }
3390
3391 static int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg) {
3392 long long value;
3393
3394 if (getLongLongFromObjectOrReply(c, o, &value, msg) != REDIS_OK) return REDIS_ERR;
3395 if (value < LONG_MIN || value > LONG_MAX) {
3396 if (msg != NULL) {
3397 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3398 } else {
3399 addReplySds(c, sdsnew("-ERR value is out of range\r\n"));
3400 }
3401 return REDIS_ERR;
3402 }
3403
3404 *target = value;
3405 return REDIS_OK;
3406 }
3407
3408 /* =========================== Keyspace access API ========================== */
3409
3410 static robj *lookupKey(redisDb *db, robj *key) {
3411 dictEntry *de = dictFind(db->dict,key->ptr);
3412 if (de) {
3413 robj *val = dictGetEntryVal(de);
3414
3415 if (server.vm_enabled) {
3416 if (val->storage == REDIS_VM_MEMORY ||
3417 val->storage == REDIS_VM_SWAPPING)
3418 {
3419 /* If we were swapping the object out, cancel the operation */
3420 if (val->storage == REDIS_VM_SWAPPING)
3421 vmCancelThreadedIOJob(val);
3422 /* Update the access time for the aging algorithm. */
3423 val->lru = server.lruclock;
3424 } else {
3425 int notify = (val->storage == REDIS_VM_LOADING);
3426
3427 /* Our value was swapped on disk. Bring it at home. */
3428 redisAssert(val->type == REDIS_VMPOINTER);
3429 val = vmLoadObject(val);
3430 dictGetEntryVal(de) = val;
3431
3432 /* Clients blocked by the VM subsystem may be waiting for
3433 * this key... */
3434 if (notify) handleClientsBlockedOnSwappedKey(db,key);
3435 }
3436 }
3437 return val;
3438 } else {
3439 return NULL;
3440 }
3441 }
3442
3443 static robj *lookupKeyRead(redisDb *db, robj *key) {
3444 expireIfNeeded(db,key);
3445 return lookupKey(db,key);
3446 }
3447
3448 static robj *lookupKeyWrite(redisDb *db, robj *key) {
3449 deleteIfVolatile(db,key);
3450 touchWatchedKey(db,key);
3451 return lookupKey(db,key);
3452 }
3453
3454 static robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
3455 robj *o = lookupKeyRead(c->db, key);
3456 if (!o) addReply(c,reply);
3457 return o;
3458 }
3459
3460 static robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
3461 robj *o = lookupKeyWrite(c->db, key);
3462 if (!o) addReply(c,reply);
3463 return o;
3464 }
3465
3466 /* Add the key to the DB. If the key already exists REDIS_ERR is returned,
3467 * otherwise REDIS_OK is returned, and the caller should increment the
3468 * refcount of 'val'. */
3469 static int dbAdd(redisDb *db, robj *key, robj *val) {
3470 /* Perform a lookup before adding the key, as we need to copy the
3471 * key value. */
3472 if (dictFind(db->dict, key->ptr) != NULL) {
3473 return REDIS_ERR;
3474 } else {
3475 sds copy = sdsdup(key->ptr);
3476 dictAdd(db->dict, copy, val);
3477 return REDIS_OK;
3478 }
3479 }
3480
3481 /* If the key does not exist, this is just like dbAdd(). Otherwise
3482 * the value associated to the key is replaced with the new one.
3483 *
3484 * On update (key already existed) 0 is returned. Otherwise 1. */
3485 static int dbReplace(redisDb *db, robj *key, robj *val) {
3486 if (dictFind(db->dict,key->ptr) == NULL) {
3487 sds copy = sdsdup(key->ptr);
3488 dictAdd(db->dict, copy, val);
3489 return 1;
3490 } else {
3491 dictReplace(db->dict, key->ptr, val);
3492 return 0;
3493 }
3494 }
3495
3496 static int dbExists(redisDb *db, robj *key) {
3497 return dictFind(db->dict,key->ptr) != NULL;
3498 }
3499
3500 /* Return a random key, in form of a Redis object.
3501 * If there are no keys, NULL is returned.
3502 *
3503 * The function makes sure to return keys not already expired. */
3504 static robj *dbRandomKey(redisDb *db) {
3505 struct dictEntry *de;
3506
3507 while(1) {
3508 sds key;
3509 robj *keyobj;
3510
3511 de = dictGetRandomKey(db->dict);
3512 if (de == NULL) return NULL;
3513
3514 key = dictGetEntryKey(de);
3515 keyobj = createStringObject(key,sdslen(key));
3516 if (dictFind(db->expires,key)) {
3517 if (expireIfNeeded(db,keyobj)) {
3518 decrRefCount(keyobj);
3519 continue; /* search for another key. This expired. */
3520 }
3521 }
3522 return keyobj;
3523 }
3524 }
3525
3526 /* Delete a key, value, and associated expiration entry if any, from the DB */
3527 static int dbDelete(redisDb *db, robj *key) {
3528 int retval;
3529
3530 if (dictSize(db->expires)) dictDelete(db->expires,key->ptr);
3531 retval = dictDelete(db->dict,key->ptr);
3532
3533 return retval == DICT_OK;
3534 }
3535
3536 /*============================ RDB saving/loading =========================== */
3537
3538 static int rdbSaveType(FILE *fp, unsigned char type) {
3539 if (fwrite(&type,1,1,fp) == 0) return -1;
3540 return 0;
3541 }
3542
3543 static int rdbSaveTime(FILE *fp, time_t t) {
3544 int32_t t32 = (int32_t) t;
3545 if (fwrite(&t32,4,1,fp) == 0) return -1;
3546 return 0;
3547 }
3548
3549 /* check rdbLoadLen() comments for more info */
3550 static int rdbSaveLen(FILE *fp, uint32_t len) {
3551 unsigned char buf[2];
3552
3553 if (len < (1<<6)) {
3554 /* Save a 6 bit len */
3555 buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
3556 if (fwrite(buf,1,1,fp) == 0) return -1;
3557 } else if (len < (1<<14)) {
3558 /* Save a 14 bit len */
3559 buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
3560 buf[1] = len&0xFF;
3561 if (fwrite(buf,2,1,fp) == 0) return -1;
3562 } else {
3563 /* Save a 32 bit len */
3564 buf[0] = (REDIS_RDB_32BITLEN<<6);
3565 if (fwrite(buf,1,1,fp) == 0) return -1;
3566 len = htonl(len);
3567 if (fwrite(&len,4,1,fp) == 0) return -1;
3568 }
3569 return 0;
3570 }
3571
3572 /* Encode 'value' as an integer if possible (if integer will fit the
3573 * supported range). If the function sucessful encoded the integer
3574 * then the (up to 5 bytes) encoded representation is written in the
3575 * string pointed by 'enc' and the length is returned. Otherwise
3576 * 0 is returned. */
3577 static int rdbEncodeInteger(long long value, unsigned char *enc) {
3578 /* Finally check if it fits in our ranges */
3579 if (value >= -(1<<7) && value <= (1<<7)-1) {
3580 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
3581 enc[1] = value&0xFF;
3582 return 2;
3583 } else if (value >= -(1<<15) && value <= (1<<15)-1) {
3584 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
3585 enc[1] = value&0xFF;
3586 enc[2] = (value>>8)&0xFF;
3587 return 3;
3588 } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
3589 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
3590 enc[1] = value&0xFF;
3591 enc[2] = (value>>8)&0xFF;
3592 enc[3] = (value>>16)&0xFF;
3593 enc[4] = (value>>24)&0xFF;
3594 return 5;
3595 } else {
3596 return 0;
3597 }
3598 }
3599
3600 /* String objects in the form "2391" "-100" without any space and with a
3601 * range of values that can fit in an 8, 16 or 32 bit signed value can be
3602 * encoded as integers to save space */
3603 static int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {
3604 long long value;
3605 char *endptr, buf[32];
3606
3607 /* Check if it's possible to encode this value as a number */
3608 value = strtoll(s, &endptr, 10);
3609 if (endptr[0] != '\0') return 0;
3610 ll2string(buf,32,value);
3611
3612 /* If the number converted back into a string is not identical
3613 * then it's not possible to encode the string as integer */
3614 if (strlen(buf) != len || memcmp(buf,s,len)) return 0;
3615
3616 return rdbEncodeInteger(value,enc);
3617 }
3618
3619 static int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) {
3620 size_t comprlen, outlen;
3621 unsigned char byte;
3622 void *out;
3623
3624 /* We require at least four bytes compression for this to be worth it */
3625 if (len <= 4) return 0;
3626 outlen = len-4;
3627 if ((out = zmalloc(outlen+1)) == NULL) return 0;
3628 comprlen = lzf_compress(s, len, out, outlen);
3629 if (comprlen == 0) {
3630 zfree(out);
3631 return 0;
3632 }
3633 /* Data compressed! Let's save it on disk */
3634 byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
3635 if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
3636 if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
3637 if (rdbSaveLen(fp,len) == -1) goto writeerr;
3638 if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
3639 zfree(out);
3640 return comprlen;
3641
3642 writeerr:
3643 zfree(out);
3644 return -1;
3645 }
3646
3647 /* Save a string objet as [len][data] on disk. If the object is a string
3648 * representation of an integer value we try to safe it in a special form */
3649 static int rdbSaveRawString(FILE *fp, unsigned char *s, size_t len) {
3650 int enclen;
3651
3652 /* Try integer encoding */
3653 if (len <= 11) {
3654 unsigned char buf[5];
3655 if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) {
3656 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3657 return 0;
3658 }
3659 }
3660
3661 /* Try LZF compression - under 20 bytes it's unable to compress even
3662 * aaaaaaaaaaaaaaaaaa so skip it */
3663 if (server.rdbcompression && len > 20) {
3664 int retval;
3665
3666 retval = rdbSaveLzfStringObject(fp,s,len);
3667 if (retval == -1) return -1;
3668 if (retval > 0) return 0;
3669 /* retval == 0 means data can't be compressed, save the old way */
3670 }
3671
3672 /* Store verbatim */
3673 if (rdbSaveLen(fp,len) == -1) return -1;
3674 if (len && fwrite(s,len,1,fp) == 0) return -1;
3675 return 0;
3676 }
3677
3678 /* Save a long long value as either an encoded string or a string. */
3679 static int rdbSaveLongLongAsStringObject(FILE *fp, long long value) {
3680 unsigned char buf[32];
3681 int enclen = rdbEncodeInteger(value,buf);
3682 if (enclen > 0) {
3683 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3684 } else {
3685 /* Encode as string */
3686 enclen = ll2string((char*)buf,32,value);
3687 redisAssert(enclen < 32);
3688 if (rdbSaveLen(fp,enclen) == -1) return -1;
3689 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3690 }
3691 return 0;
3692 }
3693
3694 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
3695 static int rdbSaveStringObject(FILE *fp, robj *obj) {
3696 /* Avoid to decode the object, then encode it again, if the
3697 * object is alrady integer encoded. */
3698 if (obj->encoding == REDIS_ENCODING_INT) {
3699 return rdbSaveLongLongAsStringObject(fp,(long)obj->ptr);
3700 } else {
3701 redisAssert(obj->encoding == REDIS_ENCODING_RAW);
3702 return rdbSaveRawString(fp,obj->ptr,sdslen(obj->ptr));
3703 }
3704 }
3705
3706 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
3707 * 8 bit integer specifing the length of the representation.
3708 * This 8 bit integer has special values in order to specify the following
3709 * conditions:
3710 * 253: not a number
3711 * 254: + inf
3712 * 255: - inf
3713 */
3714 static int rdbSaveDoubleValue(FILE *fp, double val) {
3715 unsigned char buf[128];
3716 int len;
3717
3718 if (isnan(val)) {
3719 buf[0] = 253;
3720 len = 1;
3721 } else if (!isfinite(val)) {
3722 len = 1;
3723 buf[0] = (val < 0) ? 255 : 254;
3724 } else {
3725 #if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
3726 /* Check if the float is in a safe range to be casted into a
3727 * long long. We are assuming that long long is 64 bit here.
3728 * Also we are assuming that there are no implementations around where
3729 * double has precision < 52 bit.
3730 *
3731 * Under this assumptions we test if a double is inside an interval
3732 * where casting to long long is safe. Then using two castings we
3733 * make sure the decimal part is zero. If all this is true we use
3734 * integer printing function that is much faster. */
3735 double min = -4503599627370495; /* (2^52)-1 */
3736 double max = 4503599627370496; /* -(2^52) */
3737 if (val > min && val < max && val == ((double)((long long)val)))
3738 ll2string((char*)buf+1,sizeof(buf),(long long)val);
3739 else
3740 #endif
3741 snprintf((char*)buf+1,sizeof(buf)-1,"%.17g",val);
3742 buf[0] = strlen((char*)buf+1);
3743 len = buf[0]+1;
3744 }
3745 if (fwrite(buf,len,1,fp) == 0) return -1;
3746 return 0;
3747 }
3748
3749 /* Save a Redis object. */
3750 static int rdbSaveObject(FILE *fp, robj *o) {
3751 if (o->type == REDIS_STRING) {
3752 /* Save a string value */
3753 if (rdbSaveStringObject(fp,o) == -1) return -1;
3754 } else if (o->type == REDIS_LIST) {
3755 /* Save a list value */
3756 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
3757 unsigned char *p;
3758 unsigned char *vstr;
3759 unsigned int vlen;
3760 long long vlong;
3761
3762 if (rdbSaveLen(fp,ziplistLen(o->ptr)) == -1) return -1;
3763 p = ziplistIndex(o->ptr,0);
3764 while(ziplistGet(p,&vstr,&vlen,&vlong)) {
3765 if (vstr) {
3766 if (rdbSaveRawString(fp,vstr,vlen) == -1)
3767 return -1;
3768 } else {
3769 if (rdbSaveLongLongAsStringObject(fp,vlong) == -1)
3770 return -1;
3771 }
3772 p = ziplistNext(o->ptr,p);
3773 }
3774 } else if (o->encoding == REDIS_ENCODING_LIST) {
3775 list *list = o->ptr;
3776 listIter li;
3777 listNode *ln;
3778
3779 if (rdbSaveLen(fp,listLength(list)) == -1) return -1;
3780 listRewind(list,&li);
3781 while((ln = listNext(&li))) {
3782 robj *eleobj = listNodeValue(ln);
3783 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3784 }
3785 } else {
3786 redisPanic("Unknown list encoding");
3787 }
3788 } else if (o->type == REDIS_SET) {
3789 /* Save a set value */
3790 dict *set = o->ptr;
3791 dictIterator *di = dictGetIterator(set);
3792 dictEntry *de;
3793
3794 if (rdbSaveLen(fp,dictSize(set)) == -1) return -1;
3795 while((de = dictNext(di)) != NULL) {
3796 robj *eleobj = dictGetEntryKey(de);
3797
3798 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3799 }
3800 dictReleaseIterator(di);
3801 } else if (o->type == REDIS_ZSET) {
3802 /* Save a set value */
3803 zset *zs = o->ptr;
3804 dictIterator *di = dictGetIterator(zs->dict);
3805 dictEntry *de;
3806
3807 if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) return -1;
3808 while((de = dictNext(di)) != NULL) {
3809 robj *eleobj = dictGetEntryKey(de);
3810 double *score = dictGetEntryVal(de);
3811
3812 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3813 if (rdbSaveDoubleValue(fp,*score) == -1) return -1;
3814 }
3815 dictReleaseIterator(di);
3816 } else if (o->type == REDIS_HASH) {
3817 /* Save a hash value */
3818 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
3819 unsigned char *p = zipmapRewind(o->ptr);
3820 unsigned int count = zipmapLen(o->ptr);
3821 unsigned char *key, *val;
3822 unsigned int klen, vlen;
3823
3824 if (rdbSaveLen(fp,count) == -1) return -1;
3825 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
3826 if (rdbSaveRawString(fp,key,klen) == -1) return -1;
3827 if (rdbSaveRawString(fp,val,vlen) == -1) return -1;
3828 }
3829 } else {
3830 dictIterator *di = dictGetIterator(o->ptr);
3831 dictEntry *de;
3832
3833 if (rdbSaveLen(fp,dictSize((dict*)o->ptr)) == -1) return -1;
3834 while((de = dictNext(di)) != NULL) {
3835 robj *key = dictGetEntryKey(de);
3836 robj *val = dictGetEntryVal(de);
3837
3838 if (rdbSaveStringObject(fp,key) == -1) return -1;
3839 if (rdbSaveStringObject(fp,val) == -1) return -1;
3840 }
3841 dictReleaseIterator(di);
3842 }
3843 } else {
3844 redisPanic("Unknown object type");
3845 }
3846 return 0;
3847 }
3848
3849 /* Return the length the object will have on disk if saved with
3850 * the rdbSaveObject() function. Currently we use a trick to get
3851 * this length with very little changes to the code. In the future
3852 * we could switch to a faster solution. */
3853 static off_t rdbSavedObjectLen(robj *o, FILE *fp) {
3854 if (fp == NULL) fp = server.devnull;
3855 rewind(fp);
3856 assert(rdbSaveObject(fp,o) != 1);
3857 return ftello(fp);
3858 }
3859
3860 /* Return the number of pages required to save this object in the swap file */
3861 static off_t rdbSavedObjectPages(robj *o, FILE *fp) {
3862 off_t bytes = rdbSavedObjectLen(o,fp);
3863
3864 return (bytes+(server.vm_page_size-1))/server.vm_page_size;
3865 }
3866
3867 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
3868 static int rdbSave(char *filename) {
3869 dictIterator *di = NULL;
3870 dictEntry *de;
3871 FILE *fp;
3872 char tmpfile[256];
3873 int j;
3874 time_t now = time(NULL);
3875
3876 /* Wait for I/O therads to terminate, just in case this is a
3877 * foreground-saving, to avoid seeking the swap file descriptor at the
3878 * same time. */
3879 if (server.vm_enabled)
3880 waitEmptyIOJobsQueue();
3881
3882 snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
3883 fp = fopen(tmpfile,"w");
3884 if (!fp) {
3885 redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno));
3886 return REDIS_ERR;
3887 }
3888 if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
3889 for (j = 0; j < server.dbnum; j++) {
3890 redisDb *db = server.db+j;
3891 dict *d = db->dict;
3892 if (dictSize(d) == 0) continue;
3893 di = dictGetIterator(d);
3894 if (!di) {
3895 fclose(fp);
3896 return REDIS_ERR;
3897 }
3898
3899 /* Write the SELECT DB opcode */
3900 if (rdbSaveType(fp,REDIS_SELECTDB) == -1) goto werr;
3901 if (rdbSaveLen(fp,j) == -1) goto werr;
3902
3903 /* Iterate this DB writing every entry */
3904 while((de = dictNext(di)) != NULL) {
3905 sds keystr = dictGetEntryKey(de);
3906 robj key, *o = dictGetEntryVal(de);
3907 time_t expiretime;
3908
3909 initStaticStringObject(key,keystr);
3910 expiretime = getExpire(db,&key);
3911
3912 /* Save the expire time */
3913 if (expiretime != -1) {
3914 /* If this key is already expired skip it */
3915 if (expiretime < now) continue;
3916 if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr;
3917 if (rdbSaveTime(fp,expiretime) == -1) goto werr;
3918 }
3919 /* Save the key and associated value. This requires special
3920 * handling if the value is swapped out. */
3921 if (!server.vm_enabled || o->storage == REDIS_VM_MEMORY ||
3922 o->storage == REDIS_VM_SWAPPING) {
3923 /* Save type, key, value */
3924 if (rdbSaveType(fp,o->type) == -1) goto werr;
3925 if (rdbSaveStringObject(fp,&key) == -1) goto werr;
3926 if (rdbSaveObject(fp,o) == -1) goto werr;
3927 } else {
3928 /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
3929 robj *po;
3930 /* Get a preview of the object in memory */
3931 po = vmPreviewObject(o);
3932 /* Save type, key, value */
3933 if (rdbSaveType(fp,po->type) == -1) goto werr;
3934 if (rdbSaveStringObject(fp,&key) == -1) goto werr;
3935 if (rdbSaveObject(fp,po) == -1) goto werr;
3936 /* Remove the loaded object from memory */
3937 decrRefCount(po);
3938 }
3939 }
3940 dictReleaseIterator(di);
3941 }
3942 /* EOF opcode */
3943 if (rdbSaveType(fp,REDIS_EOF) == -1) goto werr;
3944
3945 /* Make sure data will not remain on the OS's output buffers */
3946 fflush(fp);
3947 fsync(fileno(fp));
3948 fclose(fp);
3949
3950 /* Use RENAME to make sure the DB file is changed atomically only
3951 * if the generate DB file is ok. */
3952 if (rename(tmpfile,filename) == -1) {
3953 redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
3954 unlink(tmpfile);
3955 return REDIS_ERR;
3956 }
3957 redisLog(REDIS_NOTICE,"DB saved on disk");
3958 server.dirty = 0;
3959 server.lastsave = time(NULL);
3960 return REDIS_OK;
3961
3962 werr:
3963 fclose(fp);
3964 unlink(tmpfile);
3965 redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
3966 if (di) dictReleaseIterator(di);
3967 return REDIS_ERR;
3968 }
3969
3970 static int rdbSaveBackground(char *filename) {
3971 pid_t childpid;
3972
3973 if (server.bgsavechildpid != -1) return REDIS_ERR;
3974 if (server.vm_enabled) waitEmptyIOJobsQueue();
3975 if ((childpid = fork()) == 0) {
3976 /* Child */
3977 if (server.vm_enabled) vmReopenSwapFile();
3978 close(server.fd);
3979 if (rdbSave(filename) == REDIS_OK) {
3980 _exit(0);
3981 } else {
3982 _exit(1);
3983 }
3984 } else {
3985 /* Parent */
3986 if (childpid == -1) {
3987 redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
3988 strerror(errno));
3989 return REDIS_ERR;
3990 }
3991 redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);
3992 server.bgsavechildpid = childpid;
3993 updateDictResizePolicy();
3994 return REDIS_OK;
3995 }
3996 return REDIS_OK; /* unreached */
3997 }
3998
3999 static void rdbRemoveTempFile(pid_t childpid) {
4000 char tmpfile[256];
4001
4002 snprintf(tmpfile,256,"temp-%d.rdb", (int) childpid);
4003 unlink(tmpfile);
4004 }
4005
4006 static int rdbLoadType(FILE *fp) {
4007 unsigned char type;
4008 if (fread(&type,1,1,fp) == 0) return -1;
4009 return type;
4010 }
4011
4012 static time_t rdbLoadTime(FILE *fp) {
4013 int32_t t32;
4014 if (fread(&t32,4,1,fp) == 0) return -1;
4015 return (time_t) t32;
4016 }
4017
4018 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
4019 * of this file for a description of how this are stored on disk.
4020 *
4021 * isencoded is set to 1 if the readed length is not actually a length but
4022 * an "encoding type", check the above comments for more info */
4023 static uint32_t rdbLoadLen(FILE *fp, int *isencoded) {
4024 unsigned char buf[2];
4025 uint32_t len;
4026 int type;
4027
4028 if (isencoded) *isencoded = 0;
4029 if (fread(buf,1,1,fp) == 0) return REDIS_RDB_LENERR;
4030 type = (buf[0]&0xC0)>>6;
4031 if (type == REDIS_RDB_6BITLEN) {
4032 /* Read a 6 bit len */
4033 return buf[0]&0x3F;
4034 } else if (type == REDIS_RDB_ENCVAL) {
4035 /* Read a 6 bit len encoding type */
4036 if (isencoded) *isencoded = 1;
4037 return buf[0]&0x3F;
4038 } else if (type == REDIS_RDB_14BITLEN) {
4039 /* Read a 14 bit len */
4040 if (fread(buf+1,1,1,fp) == 0) return REDIS_RDB_LENERR;
4041 return ((buf[0]&0x3F)<<8)|buf[1];
4042 } else {
4043 /* Read a 32 bit len */
4044 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
4045 return ntohl(len);
4046 }
4047 }
4048
4049 /* Load an integer-encoded object from file 'fp', with the specified
4050 * encoding type 'enctype'. If encode is true the function may return
4051 * an integer-encoded object as reply, otherwise the returned object
4052 * will always be encoded as a raw string. */
4053 static robj *rdbLoadIntegerObject(FILE *fp, int enctype, int encode) {
4054 unsigned char enc[4];
4055 long long val;
4056
4057 if (enctype == REDIS_RDB_ENC_INT8) {
4058 if (fread(enc,1,1,fp) == 0) return NULL;
4059 val = (signed char)enc[0];
4060 } else if (enctype == REDIS_RDB_ENC_INT16) {
4061 uint16_t v;
4062 if (fread(enc,2,1,fp) == 0) return NULL;
4063 v = enc[0]|(enc[1]<<8);
4064 val = (int16_t)v;
4065 } else if (enctype == REDIS_RDB_ENC_INT32) {
4066 uint32_t v;
4067 if (fread(enc,4,1,fp) == 0) return NULL;
4068 v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
4069 val = (int32_t)v;
4070 } else {
4071 val = 0; /* anti-warning */
4072 redisPanic("Unknown RDB integer encoding type");
4073 }
4074 if (encode)
4075 return createStringObjectFromLongLong(val);
4076 else
4077 return createObject(REDIS_STRING,sdsfromlonglong(val));
4078 }
4079
4080 static robj *rdbLoadLzfStringObject(FILE*fp) {
4081 unsigned int len, clen;
4082 unsigned char *c = NULL;
4083 sds val = NULL;
4084
4085 if ((clen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4086 if ((len = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4087 if ((c = zmalloc(clen)) == NULL) goto err;
4088 if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
4089 if (fread(c,clen,1,fp) == 0) goto err;
4090 if (lzf_decompress(c,clen,val,len) == 0) goto err;
4091 zfree(c);
4092 return createObject(REDIS_STRING,val);
4093 err:
4094 zfree(c);
4095 sdsfree(val);
4096 return NULL;
4097 }
4098
4099 static robj *rdbGenericLoadStringObject(FILE*fp, int encode) {
4100 int isencoded;
4101 uint32_t len;
4102 sds val;
4103
4104 len = rdbLoadLen(fp,&isencoded);
4105 if (isencoded) {
4106 switch(len) {
4107 case REDIS_RDB_ENC_INT8:
4108 case REDIS_RDB_ENC_INT16:
4109 case REDIS_RDB_ENC_INT32:
4110 return rdbLoadIntegerObject(fp,len,encode);
4111 case REDIS_RDB_ENC_LZF:
4112 return rdbLoadLzfStringObject(fp);
4113 default:
4114 redisPanic("Unknown RDB encoding type");
4115 }
4116 }
4117
4118 if (len == REDIS_RDB_LENERR) return NULL;
4119 val = sdsnewlen(NULL,len);
4120 if (len && fread(val,len,1,fp) == 0) {
4121 sdsfree(val);
4122 return NULL;
4123 }
4124 return createObject(REDIS_STRING,val);
4125 }
4126
4127 static robj *rdbLoadStringObject(FILE *fp) {
4128 return rdbGenericLoadStringObject(fp,0);
4129 }
4130
4131 static robj *rdbLoadEncodedStringObject(FILE *fp) {
4132 return rdbGenericLoadStringObject(fp,1);
4133 }
4134
4135 /* For information about double serialization check rdbSaveDoubleValue() */
4136 static int rdbLoadDoubleValue(FILE *fp, double *val) {
4137 char buf[128];
4138 unsigned char len;
4139
4140 if (fread(&len,1,1,fp) == 0) return -1;
4141 switch(len) {
4142 case 255: *val = R_NegInf; return 0;
4143 case 254: *val = R_PosInf; return 0;
4144 case 253: *val = R_Nan; return 0;
4145 default:
4146 if (fread(buf,len,1,fp) == 0) return -1;
4147 buf[len] = '\0';
4148 sscanf(buf, "%lg", val);
4149 return 0;
4150 }
4151 }
4152
4153 /* Load a Redis object of the specified type from the specified file.
4154 * On success a newly allocated object is returned, otherwise NULL. */
4155 static robj *rdbLoadObject(int type, FILE *fp) {
4156 robj *o, *ele, *dec;
4157 size_t len;
4158
4159 redisLog(REDIS_DEBUG,"LOADING OBJECT %d (at %d)\n",type,ftell(fp));
4160 if (type == REDIS_STRING) {
4161 /* Read string value */
4162 if ((o = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
4163 o = tryObjectEncoding(o);
4164 } else if (type == REDIS_LIST) {
4165 /* Read list value */
4166 if ((len = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4167
4168 /* Use a real list when there are too many entries */
4169 if (len > server.list_max_ziplist_entries) {
4170 o = createListObject();
4171 } else {
4172 o = createZiplistObject();
4173 }
4174
4175 /* Load every single element of the list */
4176 while(len--) {
4177 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
4178
4179 /* If we are using a ziplist and the value is too big, convert
4180 * the object to a real list. */
4181 if (o->encoding == REDIS_ENCODING_ZIPLIST &&
4182 ele->encoding == REDIS_ENCODING_RAW &&
4183 sdslen(ele->ptr) > server.list_max_ziplist_value)
4184 listTypeConvert(o,REDIS_ENCODING_LIST);
4185
4186 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
4187 dec = getDecodedObject(ele);
4188 o->ptr = ziplistPush(o->ptr,dec->ptr,sdslen(dec->ptr),REDIS_TAIL);
4189 decrRefCount(dec);
4190 decrRefCount(ele);
4191 } else {
4192 ele = tryObjectEncoding(ele);
4193 listAddNodeTail(o->ptr,ele);
4194 }
4195 }
4196 } else if (type == REDIS_SET) {
4197 /* Read list/set value */
4198 if ((len = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4199 o = createSetObject();
4200 /* It's faster to expand the dict to the right size asap in order
4201 * to avoid rehashing */
4202 if (len > DICT_HT_INITIAL_SIZE)
4203 dictExpand(o->ptr,len);
4204 /* Load every single element of the list/set */
4205 while(len--) {
4206 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
4207 ele = tryObjectEncoding(ele);
4208 dictAdd((dict*)o->ptr,ele,NULL);
4209 }
4210 } else if (type == REDIS_ZSET) {
4211 /* Read list/set value */
4212 size_t zsetlen;
4213 zset *zs;
4214
4215 if ((zsetlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4216 o = createZsetObject();
4217 zs = o->ptr;
4218 /* Load every single element of the list/set */
4219 while(zsetlen--) {
4220 robj *ele;
4221 double *score = zmalloc(sizeof(double));
4222
4223 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
4224 ele = tryObjectEncoding(ele);
4225 if (rdbLoadDoubleValue(fp,score) == -1) return NULL;
4226 dictAdd(zs->dict,ele,score);
4227 zslInsert(zs->zsl,*score,ele);
4228 incrRefCount(ele); /* added to skiplist */
4229 }
4230 } else if (type == REDIS_HASH) {
4231 size_t hashlen;
4232
4233 if ((hashlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4234 o = createHashObject();
4235 /* Too many entries? Use an hash table. */
4236 if (hashlen > server.hash_max_zipmap_entries)
4237 convertToRealHash(o);
4238 /* Load every key/value, then set it into the zipmap or hash
4239 * table, as needed. */
4240 while(hashlen--) {
4241 robj *key, *val;
4242
4243 if ((key = rdbLoadStringObject(fp)) == NULL) return NULL;
4244 if ((val = rdbLoadStringObject(fp)) == NULL) return NULL;
4245 /* If we are using a zipmap and there are too big values
4246 * the object is converted to real hash table encoding. */
4247 if (o->encoding != REDIS_ENCODING_HT &&
4248 (sdslen(key->ptr) > server.hash_max_zipmap_value ||
4249 sdslen(val->ptr) > server.hash_max_zipmap_value))
4250 {
4251 convertToRealHash(o);
4252 }
4253
4254 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
4255 unsigned char *zm = o->ptr;
4256
4257 zm = zipmapSet(zm,key->ptr,sdslen(key->ptr),
4258 val->ptr,sdslen(val->ptr),NULL);
4259 o->ptr = zm;
4260 decrRefCount(key);
4261 decrRefCount(val);
4262 } else {
4263 key = tryObjectEncoding(key);
4264 val = tryObjectEncoding(val);
4265 dictAdd((dict*)o->ptr,key,val);
4266 }
4267 }
4268 } else {
4269 redisPanic("Unknown object type");
4270 }
4271 return o;
4272 }
4273
4274 static int rdbLoad(char *filename) {
4275 FILE *fp;
4276 uint32_t dbid;
4277 int type, retval, rdbver;
4278 int swap_all_values = 0;
4279 redisDb *db = server.db+0;
4280 char buf[1024];
4281 time_t expiretime, now = time(NULL);
4282
4283 fp = fopen(filename,"r");
4284 if (!fp) return REDIS_ERR;
4285 if (fread(buf,9,1,fp) == 0) goto eoferr;
4286 buf[9] = '\0';
4287 if (memcmp(buf,"REDIS",5) != 0) {
4288 fclose(fp);
4289 redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
4290 return REDIS_ERR;
4291 }
4292 rdbver = atoi(buf+5);
4293 if (rdbver != 1) {
4294 fclose(fp);
4295 redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
4296 return REDIS_ERR;
4297 }
4298 while(1) {
4299 robj *key, *val;
4300 int force_swapout;
4301
4302 expiretime = -1;
4303 /* Read type. */
4304 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
4305 if (type == REDIS_EXPIRETIME) {
4306 if ((expiretime = rdbLoadTime(fp)) == -1) goto eoferr;
4307 /* We read the time so we need to read the object type again */
4308 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
4309 }
4310 if (type == REDIS_EOF) break;
4311 /* Handle SELECT DB opcode as a special case */
4312 if (type == REDIS_SELECTDB) {
4313 if ((dbid = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR)
4314 goto eoferr;
4315 if (dbid >= (unsigned)server.dbnum) {
4316 redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum);
4317 exit(1);
4318 }
4319 db = server.db+dbid;
4320 continue;
4321 }
4322 /* Read key */
4323 if ((key = rdbLoadStringObject(fp)) == NULL) goto eoferr;
4324 /* Read value */
4325 if ((val = rdbLoadObject(type,fp)) == NULL) goto eoferr;
4326 /* Check if the key already expired */
4327 if (expiretime != -1 && expiretime < now) {
4328 decrRefCount(key);
4329 decrRefCount(val);
4330 continue;
4331 }
4332 /* Add the new object in the hash table */
4333 retval = dbAdd(db,key,val);
4334 if (retval == REDIS_ERR) {
4335 redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key->ptr);
4336 exit(1);
4337 }
4338 /* Set the expire time if needed */
4339 if (expiretime != -1) setExpire(db,key,expiretime);
4340
4341 /* Handle swapping while loading big datasets when VM is on */
4342
4343 /* If we detecter we are hopeless about fitting something in memory
4344 * we just swap every new key on disk. Directly...
4345 * Note that's important to check for this condition before resorting
4346 * to random sampling, otherwise we may try to swap already
4347 * swapped keys. */
4348 if (swap_all_values) {
4349 dictEntry *de = dictFind(db->dict,key->ptr);
4350
4351 /* de may be NULL since the key already expired */
4352 if (de) {
4353 vmpointer *vp;
4354 val = dictGetEntryVal(de);
4355
4356 if (val->refcount == 1 &&
4357 (vp = vmSwapObjectBlocking(val)) != NULL)
4358 dictGetEntryVal(de) = vp;
4359 }
4360 decrRefCount(key);
4361 continue;
4362 }
4363 decrRefCount(key);
4364
4365 /* Flush data on disk once 32 MB of additional RAM are used... */
4366 force_swapout = 0;
4367 if ((zmalloc_used_memory() - server.vm_max_memory) > 1024*1024*32)
4368 force_swapout = 1;
4369
4370 /* If we have still some hope of having some value fitting memory
4371 * then we try random sampling. */
4372 if (!swap_all_values && server.vm_enabled && force_swapout) {
4373 while (zmalloc_used_memory() > server.vm_max_memory) {
4374 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
4375 }
4376 if (zmalloc_used_memory() > server.vm_max_memory)
4377 swap_all_values = 1; /* We are already using too much mem */
4378 }
4379 }
4380 fclose(fp);
4381 return REDIS_OK;
4382
4383 eoferr: /* unexpected end of file is handled here with a fatal exit */
4384 redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
4385 exit(1);
4386 return REDIS_ERR; /* Just to avoid warning */
4387 }
4388
4389 /*================================== Shutdown =============================== */
4390 static int prepareForShutdown() {
4391 redisLog(REDIS_WARNING,"User requested shutdown, saving DB...");
4392 /* Kill the saving child if there is a background saving in progress.
4393 We want to avoid race conditions, for instance our saving child may
4394 overwrite the synchronous saving did by SHUTDOWN. */
4395 if (server.bgsavechildpid != -1) {
4396 redisLog(REDIS_WARNING,"There is a live saving child. Killing it!");
4397 kill(server.bgsavechildpid,SIGKILL);
4398 rdbRemoveTempFile(server.bgsavechildpid);
4399 }
4400 if (server.appendonly) {
4401 /* Append only file: fsync() the AOF and exit */
4402 aof_fsync(server.appendfd);
4403 if (server.vm_enabled) unlink(server.vm_swap_file);
4404 } else {
4405 /* Snapshotting. Perform a SYNC SAVE and exit */
4406 if (rdbSave(server.dbfilename) == REDIS_OK) {
4407 if (server.daemonize)
4408 unlink(server.pidfile);
4409 redisLog(REDIS_WARNING,"%zu bytes used at exit",zmalloc_used_memory());
4410 } else {
4411 /* Ooops.. error saving! The best we can do is to continue
4412 * operating. Note that if there was a background saving process,
4413 * in the next cron() Redis will be notified that the background
4414 * saving aborted, handling special stuff like slaves pending for
4415 * synchronization... */
4416 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit");
4417 return REDIS_ERR;
4418 }
4419 }
4420 redisLog(REDIS_WARNING,"Server exit now, bye bye...");
4421 return REDIS_OK;
4422 }
4423
4424 /*================================== Commands =============================== */
4425
4426 static void authCommand(redisClient *c) {
4427 if (!server.requirepass || !strcmp(c->argv[1]->ptr, server.requirepass)) {
4428 c->authenticated = 1;
4429 addReply(c,shared.ok);
4430 } else {
4431 c->authenticated = 0;
4432 addReplySds(c,sdscatprintf(sdsempty(),"-ERR invalid password\r\n"));
4433 }
4434 }
4435
4436 static void pingCommand(redisClient *c) {
4437 addReply(c,shared.pong);
4438 }
4439
4440 static void echoCommand(redisClient *c) {
4441 addReplyBulk(c,c->argv[1]);
4442 }
4443
4444 /*=================================== Strings =============================== */
4445
4446 static void setGenericCommand(redisClient *c, int nx, robj *key, robj *val, robj *expire) {
4447 int retval;
4448 long seconds = 0; /* initialized to avoid an harmness warning */
4449
4450 if (expire) {
4451 if (getLongFromObjectOrReply(c, expire, &seconds, NULL) != REDIS_OK)
4452 return;
4453 if (seconds <= 0) {
4454 addReplySds(c,sdsnew("-ERR invalid expire time in SETEX\r\n"));
4455 return;
4456 }
4457 }
4458
4459 touchWatchedKey(c->db,key);
4460 if (nx) deleteIfVolatile(c->db,key);
4461 retval = dbAdd(c->db,key,val);
4462 if (retval == REDIS_ERR) {
4463 if (!nx) {
4464 dbReplace(c->db,key,val);
4465 incrRefCount(val);
4466 } else {
4467 addReply(c,shared.czero);
4468 return;
4469 }
4470 } else {
4471 incrRefCount(val);
4472 }
4473 server.dirty++;
4474 removeExpire(c->db,key);
4475 if (expire) setExpire(c->db,key,time(NULL)+seconds);
4476 addReply(c, nx ? shared.cone : shared.ok);
4477 }
4478
4479 static void setCommand(redisClient *c) {
4480 setGenericCommand(c,0,c->argv[1],c->argv[2],NULL);
4481 }
4482
4483 static void setnxCommand(redisClient *c) {
4484 setGenericCommand(c,1,c->argv[1],c->argv[2],NULL);
4485 }
4486
4487 static void setexCommand(redisClient *c) {
4488 setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
4489 }
4490
4491 static int getGenericCommand(redisClient *c) {
4492 robj *o;
4493
4494 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
4495 return REDIS_OK;
4496
4497 if (o->type != REDIS_STRING) {
4498 addReply(c,shared.wrongtypeerr);
4499 return REDIS_ERR;
4500 } else {
4501 addReplyBulk(c,o);
4502 return REDIS_OK;
4503 }
4504 }
4505
4506 static void getCommand(redisClient *c) {
4507 getGenericCommand(c);
4508 }
4509
4510 static void getsetCommand(redisClient *c) {
4511 if (getGenericCommand(c) == REDIS_ERR) return;
4512 dbReplace(c->db,c->argv[1],c->argv[2]);
4513 incrRefCount(c->argv[2]);
4514 server.dirty++;
4515 removeExpire(c->db,c->argv[1]);
4516 }
4517
4518 static void mgetCommand(redisClient *c) {
4519 int j;
4520
4521 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1));
4522 for (j = 1; j < c->argc; j++) {
4523 robj *o = lookupKeyRead(c->db,c->argv[j]);
4524 if (o == NULL) {
4525 addReply(c,shared.nullbulk);
4526 } else {
4527 if (o->type != REDIS_STRING) {
4528 addReply(c,shared.nullbulk);
4529 } else {
4530 addReplyBulk(c,o);
4531 }
4532 }
4533 }
4534 }
4535
4536 static void msetGenericCommand(redisClient *c, int nx) {
4537 int j, busykeys = 0;
4538
4539 if ((c->argc % 2) == 0) {
4540 addReplySds(c,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
4541 return;
4542 }
4543 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
4544 * set nothing at all if at least one already key exists. */
4545 if (nx) {
4546 for (j = 1; j < c->argc; j += 2) {
4547 if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
4548 busykeys++;
4549 }
4550 }
4551 }
4552 if (busykeys) {
4553 addReply(c, shared.czero);
4554 return;
4555 }
4556
4557 for (j = 1; j < c->argc; j += 2) {
4558 c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
4559 dbReplace(c->db,c->argv[j],c->argv[j+1]);
4560 incrRefCount(c->argv[j+1]);
4561 removeExpire(c->db,c->argv[j]);
4562 }
4563 server.dirty += (c->argc-1)/2;
4564 addReply(c, nx ? shared.cone : shared.ok);
4565 }
4566
4567 static void msetCommand(redisClient *c) {
4568 msetGenericCommand(c,0);
4569 }
4570
4571 static void msetnxCommand(redisClient *c) {
4572 msetGenericCommand(c,1);
4573 }
4574
4575 static void incrDecrCommand(redisClient *c, long long incr) {
4576 long long value;
4577 robj *o;
4578
4579 o = lookupKeyWrite(c->db,c->argv[1]);
4580 if (o != NULL && checkType(c,o,REDIS_STRING)) return;
4581 if (getLongLongFromObjectOrReply(c,o,&value,NULL) != REDIS_OK) return;
4582
4583 value += incr;
4584 o = createStringObjectFromLongLong(value);
4585 dbReplace(c->db,c->argv[1],o);
4586 server.dirty++;
4587 addReply(c,shared.colon);
4588 addReply(c,o);
4589 addReply(c,shared.crlf);
4590 }
4591
4592 static void incrCommand(redisClient *c) {
4593 incrDecrCommand(c,1);
4594 }
4595
4596 static void decrCommand(redisClient *c) {
4597 incrDecrCommand(c,-1);
4598 }
4599
4600 static void incrbyCommand(redisClient *c) {
4601 long long incr;
4602
4603 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
4604 incrDecrCommand(c,incr);
4605 }
4606
4607 static void decrbyCommand(redisClient *c) {
4608 long long incr;
4609
4610 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
4611 incrDecrCommand(c,-incr);
4612 }
4613
4614 static void appendCommand(redisClient *c) {
4615 int retval;
4616 size_t totlen;
4617 robj *o;
4618
4619 o = lookupKeyWrite(c->db,c->argv[1]);
4620 if (o == NULL) {
4621 /* Create the key */
4622 retval = dbAdd(c->db,c->argv[1],c->argv[2]);
4623 incrRefCount(c->argv[2]);
4624 totlen = stringObjectLen(c->argv[2]);
4625 } else {
4626 if (o->type != REDIS_STRING) {
4627 addReply(c,shared.wrongtypeerr);
4628 return;
4629 }
4630 /* If the object is specially encoded or shared we have to make
4631 * a copy */
4632 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
4633 robj *decoded = getDecodedObject(o);
4634
4635 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
4636 decrRefCount(decoded);
4637 dbReplace(c->db,c->argv[1],o);
4638 }
4639 /* APPEND! */
4640 if (c->argv[2]->encoding == REDIS_ENCODING_RAW) {
4641 o->ptr = sdscatlen(o->ptr,
4642 c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
4643 } else {
4644 o->ptr = sdscatprintf(o->ptr, "%ld",
4645 (unsigned long) c->argv[2]->ptr);
4646 }
4647 totlen = sdslen(o->ptr);
4648 }
4649 server.dirty++;
4650 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen));
4651 }
4652
4653 static void substrCommand(redisClient *c) {
4654 robj *o;
4655 long start = atoi(c->argv[2]->ptr);
4656 long end = atoi(c->argv[3]->ptr);
4657 size_t rangelen, strlen;
4658 sds range;
4659
4660 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4661 checkType(c,o,REDIS_STRING)) return;
4662
4663 o = getDecodedObject(o);
4664 strlen = sdslen(o->ptr);
4665
4666 /* convert negative indexes */
4667 if (start < 0) start = strlen+start;
4668 if (end < 0) end = strlen+end;
4669 if (start < 0) start = 0;
4670 if (end < 0) end = 0;
4671
4672 /* indexes sanity checks */
4673 if (start > end || (size_t)start >= strlen) {
4674 /* Out of range start or start > end result in null reply */
4675 addReply(c,shared.nullbulk);
4676 decrRefCount(o);
4677 return;
4678 }
4679 if ((size_t)end >= strlen) end = strlen-1;
4680 rangelen = (end-start)+1;
4681
4682 /* Return the result */
4683 addReplySds(c,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen));
4684 range = sdsnewlen((char*)o->ptr+start,rangelen);
4685 addReplySds(c,range);
4686 addReply(c,shared.crlf);
4687 decrRefCount(o);
4688 }
4689
4690 /* ========================= Type agnostic commands ========================= */
4691
4692 static void delCommand(redisClient *c) {
4693 int deleted = 0, j;
4694
4695 for (j = 1; j < c->argc; j++) {
4696 if (dbDelete(c->db,c->argv[j])) {
4697 touchWatchedKey(c->db,c->argv[j]);
4698 server.dirty++;
4699 deleted++;
4700 }
4701 }
4702 addReplyLongLong(c,deleted);
4703 }
4704
4705 static void existsCommand(redisClient *c) {
4706 expireIfNeeded(c->db,c->argv[1]);
4707 if (dbExists(c->db,c->argv[1])) {
4708 addReply(c, shared.cone);
4709 } else {
4710 addReply(c, shared.czero);
4711 }
4712 }
4713
4714 static void selectCommand(redisClient *c) {
4715 int id = atoi(c->argv[1]->ptr);
4716
4717 if (selectDb(c,id) == REDIS_ERR) {
4718 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
4719 } else {
4720 addReply(c,shared.ok);
4721 }
4722 }
4723
4724 static void randomkeyCommand(redisClient *c) {
4725 robj *key;
4726
4727 if ((key = dbRandomKey(c->db)) == NULL) {
4728 addReply(c,shared.nullbulk);
4729 return;
4730 }
4731
4732 addReplyBulk(c,key);
4733 decrRefCount(key);
4734 }
4735
4736 static void keysCommand(redisClient *c) {
4737 dictIterator *di;
4738 dictEntry *de;
4739 sds pattern = c->argv[1]->ptr;
4740 int plen = sdslen(pattern);
4741 unsigned long numkeys = 0;
4742 robj *lenobj = createObject(REDIS_STRING,NULL);
4743
4744 di = dictGetIterator(c->db->dict);
4745 addReply(c,lenobj);
4746 decrRefCount(lenobj);
4747 while((de = dictNext(di)) != NULL) {
4748 sds key = dictGetEntryKey(de);
4749 robj *keyobj;
4750
4751 if ((pattern[0] == '*' && pattern[1] == '\0') ||
4752 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
4753 keyobj = createStringObject(key,sdslen(key));
4754 if (expireIfNeeded(c->db,keyobj) == 0) {
4755 addReplyBulk(c,keyobj);
4756 numkeys++;
4757 }
4758 decrRefCount(keyobj);
4759 }
4760 }
4761 dictReleaseIterator(di);
4762 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",numkeys);
4763 }
4764
4765 static void dbsizeCommand(redisClient *c) {
4766 addReplySds(c,
4767 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
4768 }
4769
4770 static void lastsaveCommand(redisClient *c) {
4771 addReplySds(c,
4772 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
4773 }
4774
4775 static void typeCommand(redisClient *c) {
4776 robj *o;
4777 char *type;
4778
4779 o = lookupKeyRead(c->db,c->argv[1]);
4780 if (o == NULL) {
4781 type = "+none";
4782 } else {
4783 switch(o->type) {
4784 case REDIS_STRING: type = "+string"; break;
4785 case REDIS_LIST: type = "+list"; break;
4786 case REDIS_SET: type = "+set"; break;
4787 case REDIS_ZSET: type = "+zset"; break;
4788 case REDIS_HASH: type = "+hash"; break;
4789 default: type = "+unknown"; break;
4790 }
4791 }
4792 addReplySds(c,sdsnew(type));
4793 addReply(c,shared.crlf);
4794 }
4795
4796 static void saveCommand(redisClient *c) {
4797 if (server.bgsavechildpid != -1) {
4798 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
4799 return;
4800 }
4801 if (rdbSave(server.dbfilename) == REDIS_OK) {
4802 addReply(c,shared.ok);
4803 } else {
4804 addReply(c,shared.err);
4805 }
4806 }
4807
4808 static void bgsaveCommand(redisClient *c) {
4809 if (server.bgsavechildpid != -1) {
4810 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
4811 return;
4812 }
4813 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
4814 char *status = "+Background saving started\r\n";
4815 addReplySds(c,sdsnew(status));
4816 } else {
4817 addReply(c,shared.err);
4818 }
4819 }
4820
4821 static void shutdownCommand(redisClient *c) {
4822 if (prepareForShutdown() == REDIS_OK)
4823 exit(0);
4824 addReplySds(c, sdsnew("-ERR Errors trying to SHUTDOWN. Check logs.\r\n"));
4825 }
4826
4827 static void renameGenericCommand(redisClient *c, int nx) {
4828 robj *o;
4829
4830 /* To use the same key as src and dst is probably an error */
4831 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
4832 addReply(c,shared.sameobjecterr);
4833 return;
4834 }
4835
4836 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
4837 return;
4838
4839 incrRefCount(o);
4840 deleteIfVolatile(c->db,c->argv[2]);
4841 if (dbAdd(c->db,c->argv[2],o) == REDIS_ERR) {
4842 if (nx) {
4843 decrRefCount(o);
4844 addReply(c,shared.czero);
4845 return;
4846 }
4847 dbReplace(c->db,c->argv[2],o);
4848 }
4849 dbDelete(c->db,c->argv[1]);
4850 touchWatchedKey(c->db,c->argv[2]);
4851 server.dirty++;
4852 addReply(c,nx ? shared.cone : shared.ok);
4853 }
4854
4855 static void renameCommand(redisClient *c) {
4856 renameGenericCommand(c,0);
4857 }
4858
4859 static void renamenxCommand(redisClient *c) {
4860 renameGenericCommand(c,1);
4861 }
4862
4863 static void moveCommand(redisClient *c) {
4864 robj *o;
4865 redisDb *src, *dst;
4866 int srcid;
4867
4868 /* Obtain source and target DB pointers */
4869 src = c->db;
4870 srcid = c->db->id;
4871 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
4872 addReply(c,shared.outofrangeerr);
4873 return;
4874 }
4875 dst = c->db;
4876 selectDb(c,srcid); /* Back to the source DB */
4877
4878 /* If the user is moving using as target the same
4879 * DB as the source DB it is probably an error. */
4880 if (src == dst) {
4881 addReply(c,shared.sameobjecterr);
4882 return;
4883 }
4884
4885 /* Check if the element exists and get a reference */
4886 o = lookupKeyWrite(c->db,c->argv[1]);
4887 if (!o) {
4888 addReply(c,shared.czero);
4889 return;
4890 }
4891
4892 /* Try to add the element to the target DB */
4893 deleteIfVolatile(dst,c->argv[1]);
4894 if (dbAdd(dst,c->argv[1],o) == REDIS_ERR) {
4895 addReply(c,shared.czero);
4896 return;
4897 }
4898 incrRefCount(o);
4899
4900 /* OK! key moved, free the entry in the source DB */
4901 dbDelete(src,c->argv[1]);
4902 server.dirty++;
4903 addReply(c,shared.cone);
4904 }
4905
4906 /* =================================== Lists ================================ */
4907
4908
4909 /* Check the argument length to see if it requires us to convert the ziplist
4910 * to a real list. Only check raw-encoded objects because integer encoded
4911 * objects are never too long. */
4912 static void listTypeTryConversion(robj *subject, robj *value) {
4913 if (subject->encoding != REDIS_ENCODING_ZIPLIST) return;
4914 if (value->encoding == REDIS_ENCODING_RAW &&
4915 sdslen(value->ptr) > server.list_max_ziplist_value)
4916 listTypeConvert(subject,REDIS_ENCODING_LIST);
4917 }
4918
4919 static void listTypePush(robj *subject, robj *value, int where) {
4920 /* Check if we need to convert the ziplist */
4921 listTypeTryConversion(subject,value);
4922 if (subject->encoding == REDIS_ENCODING_ZIPLIST &&
4923 ziplistLen(subject->ptr) > server.list_max_ziplist_entries)
4924 listTypeConvert(subject,REDIS_ENCODING_LIST);
4925
4926 if (subject->encoding == REDIS_ENCODING_ZIPLIST) {
4927 int pos = (where == REDIS_HEAD) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
4928 value = getDecodedObject(value);
4929 subject->ptr = ziplistPush(subject->ptr,value->ptr,sdslen(value->ptr),pos);
4930 decrRefCount(value);
4931 } else if (subject->encoding == REDIS_ENCODING_LIST) {
4932 if (where == REDIS_HEAD) {
4933 listAddNodeHead(subject->ptr,value);
4934 } else {
4935 listAddNodeTail(subject->ptr,value);
4936 }
4937 incrRefCount(value);
4938 } else {
4939 redisPanic("Unknown list encoding");
4940 }
4941 }
4942
4943 static robj *listTypePop(robj *subject, int where) {
4944 robj *value = NULL;
4945 if (subject->encoding == REDIS_ENCODING_ZIPLIST) {
4946 unsigned char *p;
4947 unsigned char *vstr;
4948 unsigned int vlen;
4949 long long vlong;
4950 int pos = (where == REDIS_HEAD) ? 0 : -1;
4951 p = ziplistIndex(subject->ptr,pos);
4952 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
4953 if (vstr) {
4954 value = createStringObject((char*)vstr,vlen);
4955 } else {
4956 value = createStringObjectFromLongLong(vlong);
4957 }
4958 /* We only need to delete an element when it exists */
4959 subject->ptr = ziplistDelete(subject->ptr,&p);
4960 }
4961 } else if (subject->encoding == REDIS_ENCODING_LIST) {
4962 list *list = subject->ptr;
4963 listNode *ln;
4964 if (where == REDIS_HEAD) {
4965 ln = listFirst(list);
4966 } else {
4967 ln = listLast(list);
4968 }
4969 if (ln != NULL) {
4970 value = listNodeValue(ln);
4971 incrRefCount(value);
4972 listDelNode(list,ln);
4973 }
4974 } else {
4975 redisPanic("Unknown list encoding");
4976 }
4977 return value;
4978 }
4979
4980 static unsigned long listTypeLength(robj *subject) {
4981 if (subject->encoding == REDIS_ENCODING_ZIPLIST) {
4982 return ziplistLen(subject->ptr);
4983 } else if (subject->encoding == REDIS_ENCODING_LIST) {
4984 return listLength((list*)subject->ptr);
4985 } else {
4986 redisPanic("Unknown list encoding");
4987 }
4988 }
4989
4990 /* Structure to hold set iteration abstraction. */
4991 typedef struct {
4992 robj *subject;
4993 unsigned char encoding;
4994 unsigned char direction; /* Iteration direction */
4995 unsigned char *zi;
4996 listNode *ln;
4997 } listTypeIterator;
4998
4999 /* Structure for an entry while iterating over a list. */
5000 typedef struct {
5001 listTypeIterator *li;
5002 unsigned char *zi; /* Entry in ziplist */
5003 listNode *ln; /* Entry in linked list */
5004 } listTypeEntry;
5005
5006 /* Initialize an iterator at the specified index. */
5007 static listTypeIterator *listTypeInitIterator(robj *subject, int index, unsigned char direction) {
5008 listTypeIterator *li = zmalloc(sizeof(listTypeIterator));
5009 li->subject = subject;
5010 li->encoding = subject->encoding;
5011 li->direction = direction;
5012 if (li->encoding == REDIS_ENCODING_ZIPLIST) {
5013 li->zi = ziplistIndex(subject->ptr,index);
5014 } else if (li->encoding == REDIS_ENCODING_LIST) {
5015 li->ln = listIndex(subject->ptr,index);
5016 } else {
5017 redisPanic("Unknown list encoding");
5018 }
5019 return li;
5020 }
5021
5022 /* Clean up the iterator. */
5023 static void listTypeReleaseIterator(listTypeIterator *li) {
5024 zfree(li);
5025 }
5026
5027 /* Stores pointer to current the entry in the provided entry structure
5028 * and advances the position of the iterator. Returns 1 when the current
5029 * entry is in fact an entry, 0 otherwise. */
5030 static int listTypeNext(listTypeIterator *li, listTypeEntry *entry) {
5031 /* Protect from converting when iterating */
5032 redisAssert(li->subject->encoding == li->encoding);
5033
5034 entry->li = li;
5035 if (li->encoding == REDIS_ENCODING_ZIPLIST) {
5036 entry->zi = li->zi;
5037 if (entry->zi != NULL) {
5038 if (li->direction == REDIS_TAIL)
5039 li->zi = ziplistNext(li->subject->ptr,li->zi);
5040 else
5041 li->zi = ziplistPrev(li->subject->ptr,li->zi);
5042 return 1;
5043 }
5044 } else if (li->encoding == REDIS_ENCODING_LIST) {
5045 entry->ln = li->ln;
5046 if (entry->ln != NULL) {
5047 if (li->direction == REDIS_TAIL)
5048 li->ln = li->ln->next;
5049 else
5050 li->ln = li->ln->prev;
5051 return 1;
5052 }
5053 } else {
5054 redisPanic("Unknown list encoding");
5055 }
5056 return 0;
5057 }
5058
5059 /* Return entry or NULL at the current position of the iterator. */
5060 static robj *listTypeGet(listTypeEntry *entry) {
5061 listTypeIterator *li = entry->li;
5062 robj *value = NULL;
5063 if (li->encoding == REDIS_ENCODING_ZIPLIST) {
5064 unsigned char *vstr;
5065 unsigned int vlen;
5066 long long vlong;
5067 redisAssert(entry->zi != NULL);
5068 if (ziplistGet(entry->zi,&vstr,&vlen,&vlong)) {
5069 if (vstr) {
5070 value = createStringObject((char*)vstr,vlen);
5071 } else {
5072 value = createStringObjectFromLongLong(vlong);
5073 }
5074 }
5075 } else if (li->encoding == REDIS_ENCODING_LIST) {
5076 redisAssert(entry->ln != NULL);
5077 value = listNodeValue(entry->ln);
5078 incrRefCount(value);
5079 } else {
5080 redisPanic("Unknown list encoding");
5081 }
5082 return value;
5083 }
5084
5085 /* Compare the given object with the entry at the current position. */
5086 static int listTypeEqual(listTypeEntry *entry, robj *o) {
5087 listTypeIterator *li = entry->li;
5088 if (li->encoding == REDIS_ENCODING_ZIPLIST) {
5089 redisAssert(o->encoding == REDIS_ENCODING_RAW);
5090 return ziplistCompare(entry->zi,o->ptr,sdslen(o->ptr));
5091 } else if (li->encoding == REDIS_ENCODING_LIST) {
5092 return equalStringObjects(o,listNodeValue(entry->ln));
5093 } else {
5094 redisPanic("Unknown list encoding");
5095 }
5096 }
5097
5098 /* Delete the element pointed to. */
5099 static void listTypeDelete(listTypeEntry *entry) {
5100 listTypeIterator *li = entry->li;
5101 if (li->encoding == REDIS_ENCODING_ZIPLIST) {
5102 unsigned char *p = entry->zi;
5103 li->subject->ptr = ziplistDelete(li->subject->ptr,&p);
5104
5105 /* Update position of the iterator depending on the direction */
5106 if (li->direction == REDIS_TAIL)
5107 li->zi = p;
5108 else
5109 li->zi = ziplistPrev(li->subject->ptr,p);
5110 } else if (entry->li->encoding == REDIS_ENCODING_LIST) {
5111 listNode *next;
5112 if (li->direction == REDIS_TAIL)
5113 next = entry->ln->next;
5114 else
5115 next = entry->ln->prev;
5116 listDelNode(li->subject->ptr,entry->ln);
5117 li->ln = next;
5118 } else {
5119 redisPanic("Unknown list encoding");
5120 }
5121 }
5122
5123 static void listTypeConvert(robj *subject, int enc) {
5124 listTypeIterator *li;
5125 listTypeEntry entry;
5126 redisAssert(subject->type == REDIS_LIST);
5127
5128 if (enc == REDIS_ENCODING_LIST) {
5129 list *l = listCreate();
5130 listSetFreeMethod(l,decrRefCount);
5131
5132 /* listTypeGet returns a robj with incremented refcount */
5133 li = listTypeInitIterator(subject,0,REDIS_TAIL);
5134 while (listTypeNext(li,&entry)) listAddNodeTail(l,listTypeGet(&entry));
5135 listTypeReleaseIterator(li);
5136
5137 subject->encoding = REDIS_ENCODING_LIST;
5138 zfree(subject->ptr);
5139 subject->ptr = l;
5140 } else {
5141 redisPanic("Unsupported list conversion");
5142 }
5143 }
5144
5145 static void pushGenericCommand(redisClient *c, int where) {
5146 robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
5147 if (lobj == NULL) {
5148 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
5149 addReply(c,shared.cone);
5150 return;
5151 }
5152 lobj = createZiplistObject();
5153 dbAdd(c->db,c->argv[1],lobj);
5154 } else {
5155 if (lobj->type != REDIS_LIST) {
5156 addReply(c,shared.wrongtypeerr);
5157 return;
5158 }
5159 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
5160 addReply(c,shared.cone);
5161 return;
5162 }
5163 }
5164 listTypePush(lobj,c->argv[2],where);
5165 addReplyLongLong(c,listTypeLength(lobj));
5166 server.dirty++;
5167 }
5168
5169 static void lpushCommand(redisClient *c) {
5170 pushGenericCommand(c,REDIS_HEAD);
5171 }
5172
5173 static void rpushCommand(redisClient *c) {
5174 pushGenericCommand(c,REDIS_TAIL);
5175 }
5176
5177 static void llenCommand(redisClient *c) {
5178 robj *o = lookupKeyReadOrReply(c,c->argv[1],shared.czero);
5179 if (o == NULL || checkType(c,o,REDIS_LIST)) return;
5180 addReplyUlong(c,listTypeLength(o));
5181 }
5182
5183 static void lindexCommand(redisClient *c) {
5184 robj *o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk);
5185 if (o == NULL || checkType(c,o,REDIS_LIST)) return;
5186 int index = atoi(c->argv[2]->ptr);
5187 robj *value = NULL;
5188
5189 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
5190 unsigned char *p;
5191 unsigned char *vstr;
5192 unsigned int vlen;
5193 long long vlong;
5194 p = ziplistIndex(o->ptr,index);
5195 if (ziplistGet(p,&vstr,&vlen,&vlong)) {
5196 if (vstr) {
5197 value = createStringObject((char*)vstr,vlen);
5198 } else {
5199 value = createStringObjectFromLongLong(vlong);
5200 }
5201 addReplyBulk(c,value);
5202 decrRefCount(value);
5203 } else {
5204 addReply(c,shared.nullbulk);
5205 }
5206 } else if (o->encoding == REDIS_ENCODING_LIST) {
5207 listNode *ln = listIndex(o->ptr,index);
5208 if (ln != NULL) {
5209 value = listNodeValue(ln);
5210 addReplyBulk(c,value);
5211 } else {
5212 addReply(c,shared.nullbulk);
5213 }
5214 } else {
5215 redisPanic("Unknown list encoding");
5216 }
5217 }
5218
5219 static void lsetCommand(redisClient *c) {
5220 robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr);
5221 if (o == NULL || checkType(c,o,REDIS_LIST)) return;
5222 int index = atoi(c->argv[2]->ptr);
5223 robj *value = c->argv[3];
5224
5225 listTypeTryConversion(o,value);
5226 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
5227 unsigned char *p, *zl = o->ptr;
5228 p = ziplistIndex(zl,index);
5229 if (p == NULL) {
5230 addReply(c,shared.outofrangeerr);
5231 } else {
5232 o->ptr = ziplistDelete(o->ptr,&p);
5233 value = getDecodedObject(value);
5234 o->ptr = ziplistInsert(o->ptr,p,value->ptr,sdslen(value->ptr));
5235 decrRefCount(value);
5236 addReply(c,shared.ok);
5237 server.dirty++;
5238 }
5239 } else if (o->encoding == REDIS_ENCODING_LIST) {
5240 listNode *ln = listIndex(o->ptr,index);
5241 if (ln == NULL) {
5242 addReply(c,shared.outofrangeerr);
5243 } else {
5244 decrRefCount((robj*)listNodeValue(ln));
5245 listNodeValue(ln) = value;
5246 incrRefCount(value);
5247 addReply(c,shared.ok);
5248 server.dirty++;
5249 }
5250 } else {
5251 redisPanic("Unknown list encoding");
5252 }
5253 }
5254
5255 static void popGenericCommand(redisClient *c, int where) {
5256 robj *o = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk);
5257 if (o == NULL || checkType(c,o,REDIS_LIST)) return;
5258
5259 robj *value = listTypePop(o,where);
5260 if (value == NULL) {
5261 addReply(c,shared.nullbulk);
5262 } else {
5263 addReplyBulk(c,value);
5264 decrRefCount(value);
5265 if (listTypeLength(o) == 0) dbDelete(c->db,c->argv[1]);
5266 server.dirty++;
5267 }
5268 }
5269
5270 static void lpopCommand(redisClient *c) {
5271 popGenericCommand(c,REDIS_HEAD);
5272 }
5273
5274 static void rpopCommand(redisClient *c) {
5275 popGenericCommand(c,REDIS_TAIL);
5276 }
5277
5278 static void lrangeCommand(redisClient *c) {
5279 robj *o, *value;
5280 int start = atoi(c->argv[2]->ptr);
5281 int end = atoi(c->argv[3]->ptr);
5282 int llen;
5283 int rangelen, j;
5284 listTypeEntry entry;
5285
5286 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
5287 || checkType(c,o,REDIS_LIST)) return;
5288 llen = listTypeLength(o);
5289
5290 /* convert negative indexes */
5291 if (start < 0) start = llen+start;
5292 if (end < 0) end = llen+end;
5293 if (start < 0) start = 0;
5294 if (end < 0) end = 0;
5295
5296 /* indexes sanity checks */
5297 if (start > end || start >= llen) {
5298 /* Out of range start or start > end result in empty list */
5299 addReply(c,shared.emptymultibulk);
5300 return;
5301 }
5302 if (end >= llen) end = llen-1;
5303 rangelen = (end-start)+1;
5304
5305 /* Return the result in form of a multi-bulk reply */
5306 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen));
5307 listTypeIterator *li = listTypeInitIterator(o,start,REDIS_TAIL);
5308 for (j = 0; j < rangelen; j++) {
5309 redisAssert(listTypeNext(li,&entry));
5310 value = listTypeGet(&entry);
5311 addReplyBulk(c,value);
5312 decrRefCount(value);
5313 }
5314 listTypeReleaseIterator(li);
5315 }
5316
5317 static void ltrimCommand(redisClient *c) {
5318 robj *o;
5319 int start = atoi(c->argv[2]->ptr);
5320 int end = atoi(c->argv[3]->ptr);
5321 int llen;
5322 int j, ltrim, rtrim;
5323 list *list;
5324 listNode *ln;
5325
5326 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == NULL ||
5327 checkType(c,o,REDIS_LIST)) return;
5328 llen = listTypeLength(o);
5329
5330 /* convert negative indexes */
5331 if (start < 0) start = llen+start;
5332 if (end < 0) end = llen+end;
5333 if (start < 0) start = 0;
5334 if (end < 0) end = 0;
5335
5336 /* indexes sanity checks */
5337 if (start > end || start >= llen) {
5338 /* Out of range start or start > end result in empty list */
5339 ltrim = llen;
5340 rtrim = 0;
5341 } else {
5342 if (end >= llen) end = llen-1;
5343 ltrim = start;
5344 rtrim = llen-end-1;
5345 }
5346
5347 /* Remove list elements to perform the trim */
5348 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
5349 o->ptr = ziplistDeleteRange(o->ptr,0,ltrim);
5350 o->ptr = ziplistDeleteRange(o->ptr,-rtrim,rtrim);
5351 } else if (o->encoding == REDIS_ENCODING_LIST) {
5352 list = o->ptr;
5353 for (j = 0; j < ltrim; j++) {
5354 ln = listFirst(list);
5355 listDelNode(list,ln);
5356 }
5357 for (j = 0; j < rtrim; j++) {
5358 ln = listLast(list);
5359 listDelNode(list,ln);
5360 }
5361 } else {
5362 redisPanic("Unknown list encoding");
5363 }
5364 if (listTypeLength(o) == 0) dbDelete(c->db,c->argv[1]);
5365 server.dirty++;
5366 addReply(c,shared.ok);
5367 }
5368
5369 static void lremCommand(redisClient *c) {
5370 robj *subject, *obj = c->argv[3];
5371 int toremove = atoi(c->argv[2]->ptr);
5372 int removed = 0;
5373 listTypeEntry entry;
5374
5375 subject = lookupKeyWriteOrReply(c,c->argv[1],shared.czero);
5376 if (subject == NULL || checkType(c,subject,REDIS_LIST)) return;
5377
5378 /* Make sure obj is raw when we're dealing with a ziplist */
5379 if (subject->encoding == REDIS_ENCODING_ZIPLIST)
5380 obj = getDecodedObject(obj);
5381
5382 listTypeIterator *li;
5383 if (toremove < 0) {
5384 toremove = -toremove;
5385 li = listTypeInitIterator(subject,-1,REDIS_HEAD);
5386 } else {
5387 li = listTypeInitIterator(subject,0,REDIS_TAIL);
5388 }
5389
5390 while (listTypeNext(li,&entry)) {
5391 if (listTypeEqual(&entry,obj)) {
5392 listTypeDelete(&entry);
5393 server.dirty++;
5394 removed++;
5395 if (toremove && removed == toremove) break;
5396 }
5397 }
5398 listTypeReleaseIterator(li);
5399
5400 /* Clean up raw encoded object */
5401 if (subject->encoding == REDIS_ENCODING_ZIPLIST)
5402 decrRefCount(obj);
5403
5404 if (listTypeLength(subject) == 0) dbDelete(c->db,c->argv[1]);
5405 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
5406 }
5407
5408 /* This is the semantic of this command:
5409 * RPOPLPUSH srclist dstlist:
5410 * IF LLEN(srclist) > 0
5411 * element = RPOP srclist
5412 * LPUSH dstlist element
5413 * RETURN element
5414 * ELSE
5415 * RETURN nil
5416 * END
5417 * END
5418 *
5419 * The idea is to be able to get an element from a list in a reliable way
5420 * since the element is not just returned but pushed against another list
5421 * as well. This command was originally proposed by Ezra Zygmuntowicz.
5422 */
5423 static void rpoplpushcommand(redisClient *c) {
5424 robj *sobj, *value;
5425 if ((sobj = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5426 checkType(c,sobj,REDIS_LIST)) return;
5427
5428 if (listTypeLength(sobj) == 0) {
5429 addReply(c,shared.nullbulk);
5430 } else {
5431 robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
5432 if (dobj && checkType(c,dobj,REDIS_LIST)) return;
5433 value = listTypePop(sobj,REDIS_TAIL);
5434
5435 /* Add the element to the target list (unless it's directly
5436 * passed to some BLPOP-ing client */
5437 if (!handleClientsWaitingListPush(c,c->argv[2],value)) {
5438 /* Create the list if the key does not exist */
5439 if (!dobj) {
5440 dobj = createZiplistObject();
5441 dbAdd(c->db,c->argv[2],dobj);
5442 }
5443 listTypePush(dobj,value,REDIS_HEAD);
5444 }
5445
5446 /* Send the element to the client as reply as well */
5447 addReplyBulk(c,value);
5448
5449 /* listTypePop returns an object with its refcount incremented */
5450 decrRefCount(value);
5451
5452 /* Delete the source list when it is empty */
5453 if (listTypeLength(sobj) == 0) dbDelete(c->db,c->argv[1]);
5454 server.dirty++;
5455 }
5456 }
5457
5458 /* ==================================== Sets ================================ */
5459
5460 static void saddCommand(redisClient *c) {
5461 robj *set;
5462
5463 set = lookupKeyWrite(c->db,c->argv[1]);
5464 if (set == NULL) {
5465 set = createSetObject();
5466 dbAdd(c->db,c->argv[1],set);
5467 } else {
5468 if (set->type != REDIS_SET) {
5469 addReply(c,shared.wrongtypeerr);
5470 return;
5471 }
5472 }
5473 if (dictAdd(set->ptr,c->argv[2],NULL) == DICT_OK) {
5474 incrRefCount(c->argv[2]);
5475 server.dirty++;
5476 addReply(c,shared.cone);
5477 } else {
5478 addReply(c,shared.czero);
5479 }
5480 }
5481
5482 static void sremCommand(redisClient *c) {
5483 robj *set;
5484
5485 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5486 checkType(c,set,REDIS_SET)) return;
5487
5488 if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
5489 server.dirty++;
5490 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
5491 if (dictSize((dict*)set->ptr) == 0) dbDelete(c->db,c->argv[1]);
5492 addReply(c,shared.cone);
5493 } else {
5494 addReply(c,shared.czero);
5495 }
5496 }
5497
5498 static void smoveCommand(redisClient *c) {
5499 robj *srcset, *dstset;
5500
5501 srcset = lookupKeyWrite(c->db,c->argv[1]);
5502 dstset = lookupKeyWrite(c->db,c->argv[2]);
5503
5504 /* If the source key does not exist return 0, if it's of the wrong type
5505 * raise an error */
5506 if (srcset == NULL || srcset->type != REDIS_SET) {
5507 addReply(c, srcset ? shared.wrongtypeerr : shared.czero);
5508 return;
5509 }
5510 /* Error if the destination key is not a set as well */
5511 if (dstset && dstset->type != REDIS_SET) {
5512 addReply(c,shared.wrongtypeerr);
5513 return;
5514 }
5515 /* Remove the element from the source set */
5516 if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) {
5517 /* Key not found in the src set! return zero */
5518 addReply(c,shared.czero);
5519 return;
5520 }
5521 if (dictSize((dict*)srcset->ptr) == 0 && srcset != dstset)
5522 dbDelete(c->db,c->argv[1]);
5523 server.dirty++;
5524 /* Add the element to the destination set */
5525 if (!dstset) {
5526 dstset = createSetObject();
5527 dbAdd(c->db,c->argv[2],dstset);
5528 }
5529 if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK)
5530 incrRefCount(c->argv[3]);
5531 addReply(c,shared.cone);
5532 }
5533
5534 static void sismemberCommand(redisClient *c) {
5535 robj *set;
5536
5537 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5538 checkType(c,set,REDIS_SET)) return;
5539
5540 if (dictFind(set->ptr,c->argv[2]))
5541 addReply(c,shared.cone);
5542 else
5543 addReply(c,shared.czero);
5544 }
5545
5546 static void scardCommand(redisClient *c) {
5547 robj *o;
5548 dict *s;
5549
5550 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5551 checkType(c,o,REDIS_SET)) return;
5552
5553 s = o->ptr;
5554 addReplyUlong(c,dictSize(s));
5555 }
5556
5557 static void spopCommand(redisClient *c) {
5558 robj *set;
5559 dictEntry *de;
5560
5561 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5562 checkType(c,set,REDIS_SET)) return;
5563
5564 de = dictGetRandomKey(set->ptr);
5565 if (de == NULL) {
5566 addReply(c,shared.nullbulk);
5567 } else {
5568 robj *ele = dictGetEntryKey(de);
5569
5570 addReplyBulk(c,ele);
5571 dictDelete(set->ptr,ele);
5572 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
5573 if (dictSize((dict*)set->ptr) == 0) dbDelete(c->db,c->argv[1]);
5574 server.dirty++;
5575 }
5576 }
5577
5578 static void srandmemberCommand(redisClient *c) {
5579 robj *set;
5580 dictEntry *de;
5581
5582 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5583 checkType(c,set,REDIS_SET)) return;
5584
5585 de = dictGetRandomKey(set->ptr);
5586 if (de == NULL) {
5587 addReply(c,shared.nullbulk);
5588 } else {
5589 robj *ele = dictGetEntryKey(de);
5590
5591 addReplyBulk(c,ele);
5592 }
5593 }
5594
5595 static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
5596 dict **d1 = (void*) s1, **d2 = (void*) s2;
5597
5598 return dictSize(*d1)-dictSize(*d2);
5599 }
5600
5601 static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long setsnum, robj *dstkey) {
5602 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5603 dictIterator *di;
5604 dictEntry *de;
5605 robj *lenobj = NULL, *dstset = NULL;
5606 unsigned long j, cardinality = 0;
5607
5608 for (j = 0; j < setsnum; j++) {
5609 robj *setobj;
5610
5611 setobj = dstkey ?
5612 lookupKeyWrite(c->db,setskeys[j]) :
5613 lookupKeyRead(c->db,setskeys[j]);
5614 if (!setobj) {
5615 zfree(dv);
5616 if (dstkey) {
5617 if (dbDelete(c->db,dstkey))
5618 server.dirty++;
5619 addReply(c,shared.czero);
5620 } else {
5621 addReply(c,shared.emptymultibulk);
5622 }
5623 return;
5624 }
5625 if (setobj->type != REDIS_SET) {
5626 zfree(dv);
5627 addReply(c,shared.wrongtypeerr);
5628 return;
5629 }
5630 dv[j] = setobj->ptr;
5631 }
5632 /* Sort sets from the smallest to largest, this will improve our
5633 * algorithm's performace */
5634 qsort(dv,setsnum,sizeof(dict*),qsortCompareSetsByCardinality);
5635
5636 /* The first thing we should output is the total number of elements...
5637 * since this is a multi-bulk write, but at this stage we don't know
5638 * the intersection set size, so we use a trick, append an empty object
5639 * to the output list and save the pointer to later modify it with the
5640 * right length */
5641 if (!dstkey) {
5642 lenobj = createObject(REDIS_STRING,NULL);
5643 addReply(c,lenobj);
5644 decrRefCount(lenobj);
5645 } else {
5646 /* If we have a target key where to store the resulting set
5647 * create this key with an empty set inside */
5648 dstset = createSetObject();
5649 }
5650
5651 /* Iterate all the elements of the first (smallest) set, and test
5652 * the element against all the other sets, if at least one set does
5653 * not include the element it is discarded */
5654 di = dictGetIterator(dv[0]);
5655
5656 while((de = dictNext(di)) != NULL) {
5657 robj *ele;
5658
5659 for (j = 1; j < setsnum; j++)
5660 if (dictFind(dv[j],dictGetEntryKey(de)) == NULL) break;
5661 if (j != setsnum)
5662 continue; /* at least one set does not contain the member */
5663 ele = dictGetEntryKey(de);
5664 if (!dstkey) {
5665 addReplyBulk(c,ele);
5666 cardinality++;
5667 } else {
5668 dictAdd(dstset->ptr,ele,NULL);
5669 incrRefCount(ele);
5670 }
5671 }
5672 dictReleaseIterator(di);
5673
5674 if (dstkey) {
5675 /* Store the resulting set into the target, if the intersection
5676 * is not an empty set. */
5677 dbDelete(c->db,dstkey);
5678 if (dictSize((dict*)dstset->ptr) > 0) {
5679 dbAdd(c->db,dstkey,dstset);
5680 addReplyLongLong(c,dictSize((dict*)dstset->ptr));
5681 } else {
5682 decrRefCount(dstset);
5683 addReply(c,shared.czero);
5684 }
5685 server.dirty++;
5686 } else {
5687 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
5688 }
5689 zfree(dv);
5690 }
5691
5692 static void sinterCommand(redisClient *c) {
5693 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
5694 }
5695
5696 static void sinterstoreCommand(redisClient *c) {
5697 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
5698 }
5699
5700 #define REDIS_OP_UNION 0
5701 #define REDIS_OP_DIFF 1
5702 #define REDIS_OP_INTER 2
5703
5704 static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey, int op) {
5705 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5706 dictIterator *di;
5707 dictEntry *de;
5708 robj *dstset = NULL;
5709 int j, cardinality = 0;
5710
5711 for (j = 0; j < setsnum; j++) {
5712 robj *setobj;
5713
5714 setobj = dstkey ?
5715 lookupKeyWrite(c->db,setskeys[j]) :
5716 lookupKeyRead(c->db,setskeys[j]);
5717 if (!setobj) {
5718 dv[j] = NULL;
5719 continue;
5720 }
5721 if (setobj->type != REDIS_SET) {
5722 zfree(dv);
5723 addReply(c,shared.wrongtypeerr);
5724 return;
5725 }
5726 dv[j] = setobj->ptr;
5727 }
5728
5729 /* We need a temp set object to store our union. If the dstkey
5730 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
5731 * this set object will be the resulting object to set into the target key*/
5732 dstset = createSetObject();
5733
5734 /* Iterate all the elements of all the sets, add every element a single
5735 * time to the result set */
5736 for (j = 0; j < setsnum; j++) {
5737 if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */
5738 if (!dv[j]) continue; /* non existing keys are like empty sets */
5739
5740 di = dictGetIterator(dv[j]);
5741
5742 while((de = dictNext(di)) != NULL) {
5743 robj *ele;
5744
5745 /* dictAdd will not add the same element multiple times */
5746 ele = dictGetEntryKey(de);
5747 if (op == REDIS_OP_UNION || j == 0) {
5748 if (dictAdd(dstset->ptr,ele,NULL) == DICT_OK) {
5749 incrRefCount(ele);
5750 cardinality++;
5751 }
5752 } else if (op == REDIS_OP_DIFF) {
5753 if (dictDelete(dstset->ptr,ele) == DICT_OK) {
5754 cardinality--;
5755 }
5756 }
5757 }
5758 dictReleaseIterator(di);
5759
5760 /* result set is empty? Exit asap. */
5761 if (op == REDIS_OP_DIFF && cardinality == 0) break;
5762 }
5763
5764 /* Output the content of the resulting set, if not in STORE mode */
5765 if (!dstkey) {
5766 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
5767 di = dictGetIterator(dstset->ptr);
5768 while((de = dictNext(di)) != NULL) {
5769 robj *ele;
5770
5771 ele = dictGetEntryKey(de);
5772 addReplyBulk(c,ele);
5773 }
5774 dictReleaseIterator(di);
5775 decrRefCount(dstset);
5776 } else {
5777 /* If we have a target key where to store the resulting set
5778 * create this key with the result set inside */
5779 dbDelete(c->db,dstkey);
5780 if (dictSize((dict*)dstset->ptr) > 0) {
5781 dbAdd(c->db,dstkey,dstset);
5782 addReplyLongLong(c,dictSize((dict*)dstset->ptr));
5783 } else {
5784 decrRefCount(dstset);
5785 addReply(c,shared.czero);
5786 }
5787 server.dirty++;
5788 }
5789 zfree(dv);
5790 }
5791
5792 static void sunionCommand(redisClient *c) {
5793 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
5794 }
5795
5796 static void sunionstoreCommand(redisClient *c) {
5797 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
5798 }
5799
5800 static void sdiffCommand(redisClient *c) {
5801 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
5802 }
5803
5804 static void sdiffstoreCommand(redisClient *c) {
5805 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
5806 }
5807
5808 /* ==================================== ZSets =============================== */
5809
5810 /* ZSETs are ordered sets using two data structures to hold the same elements
5811 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
5812 * data structure.
5813 *
5814 * The elements are added to an hash table mapping Redis objects to scores.
5815 * At the same time the elements are added to a skip list mapping scores
5816 * to Redis objects (so objects are sorted by scores in this "view"). */
5817
5818 /* This skiplist implementation is almost a C translation of the original
5819 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
5820 * Alternative to Balanced Trees", modified in three ways:
5821 * a) this implementation allows for repeated values.
5822 * b) the comparison is not just by key (our 'score') but by satellite data.
5823 * c) there is a back pointer, so it's a doubly linked list with the back
5824 * pointers being only at "level 1". This allows to traverse the list
5825 * from tail to head, useful for ZREVRANGE. */
5826
5827 static zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
5828 zskiplistNode *zn = zmalloc(sizeof(*zn));
5829
5830 zn->forward = zmalloc(sizeof(zskiplistNode*) * level);
5831 if (level > 1)
5832 zn->span = zmalloc(sizeof(unsigned int) * (level - 1));
5833 else
5834 zn->span = NULL;
5835 zn->score = score;
5836 zn->obj = obj;
5837 return zn;
5838 }
5839
5840 static zskiplist *zslCreate(void) {
5841 int j;
5842 zskiplist *zsl;
5843
5844 zsl = zmalloc(sizeof(*zsl));
5845 zsl->level = 1;
5846 zsl->length = 0;
5847 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);
5848 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
5849 zsl->header->forward[j] = NULL;
5850
5851 /* span has space for ZSKIPLIST_MAXLEVEL-1 elements */
5852 if (j < ZSKIPLIST_MAXLEVEL-1)
5853 zsl->header->span[j] = 0;
5854 }
5855 zsl->header->backward = NULL;
5856 zsl->tail = NULL;
5857 return zsl;
5858 }
5859
5860 static void zslFreeNode(zskiplistNode *node) {
5861 decrRefCount(node->obj);
5862 zfree(node->forward);
5863 zfree(node->span);
5864 zfree(node);
5865 }
5866
5867 static void zslFree(zskiplist *zsl) {
5868 zskiplistNode *node = zsl->header->forward[0], *next;
5869
5870 zfree(zsl->header->forward);
5871 zfree(zsl->header->span);
5872 zfree(zsl->header);
5873 while(node) {
5874 next = node->forward[0];
5875 zslFreeNode(node);
5876 node = next;
5877 }
5878 zfree(zsl);
5879 }
5880
5881 static int zslRandomLevel(void) {
5882 int level = 1;
5883 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
5884 level += 1;
5885 return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;
5886 }
5887
5888 static void zslInsert(zskiplist *zsl, double score, robj *obj) {
5889 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5890 unsigned int rank[ZSKIPLIST_MAXLEVEL];
5891 int i, level;
5892
5893 x = zsl->header;
5894 for (i = zsl->level-1; i >= 0; i--) {
5895 /* store rank that is crossed to reach the insert position */
5896 rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
5897
5898 while (x->forward[i] &&
5899 (x->forward[i]->score < score ||
5900 (x->forward[i]->score == score &&
5901 compareStringObjects(x->forward[i]->obj,obj) < 0))) {
5902 rank[i] += i > 0 ? x->span[i-1] : 1;
5903 x = x->forward[i];
5904 }
5905 update[i] = x;
5906 }
5907 /* we assume the key is not already inside, since we allow duplicated
5908 * scores, and the re-insertion of score and redis object should never
5909 * happpen since the caller of zslInsert() should test in the hash table
5910 * if the element is already inside or not. */
5911 level = zslRandomLevel();
5912 if (level > zsl->level) {
5913 for (i = zsl->level; i < level; i++) {
5914 rank[i] = 0;
5915 update[i] = zsl->header;
5916 update[i]->span[i-1] = zsl->length;
5917 }
5918 zsl->level = level;
5919 }
5920 x = zslCreateNode(level,score,obj);
5921 for (i = 0; i < level; i++) {
5922 x->forward[i] = update[i]->forward[i];
5923 update[i]->forward[i] = x;
5924
5925 /* update span covered by update[i] as x is inserted here */
5926 if (i > 0) {
5927 x->span[i-1] = update[i]->span[i-1] - (rank[0] - rank[i]);
5928 update[i]->span[i-1] = (rank[0] - rank[i]) + 1;
5929 }
5930 }
5931
5932 /* increment span for untouched levels */
5933 for (i = level; i < zsl->level; i++) {
5934 update[i]->span[i-1]++;
5935 }
5936
5937 x->backward = (update[0] == zsl->header) ? NULL : update[0];
5938 if (x->forward[0])
5939 x->forward[0]->backward = x;
5940 else
5941 zsl->tail = x;
5942 zsl->length++;
5943 }
5944
5945 /* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
5946 void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
5947 int i;
5948 for (i = 0; i < zsl->level; i++) {
5949 if (update[i]->forward[i] == x) {
5950 if (i > 0) {
5951 update[i]->span[i-1] += x->span[i-1] - 1;
5952 }
5953 update[i]->forward[i] = x->forward[i];
5954 } else {
5955 /* invariant: i > 0, because update[0]->forward[0]
5956 * is always equal to x */
5957 update[i]->span[i-1] -= 1;
5958 }
5959 }
5960 if (x->forward[0]) {
5961 x->forward[0]->backward = x->backward;
5962 } else {
5963 zsl->tail = x->backward;
5964 }
5965 while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
5966 zsl->level--;
5967 zsl->length--;
5968 }
5969
5970 /* Delete an element with matching score/object from the skiplist. */
5971 static int zslDelete(zskiplist *zsl, double score, robj *obj) {
5972 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5973 int i;
5974
5975 x = zsl->header;
5976 for (i = zsl->level-1; i >= 0; i--) {
5977 while (x->forward[i] &&
5978 (x->forward[i]->score < score ||
5979 (x->forward[i]->score == score &&
5980 compareStringObjects(x->forward[i]->obj,obj) < 0)))
5981 x = x->forward[i];
5982 update[i] = x;
5983 }
5984 /* We may have multiple elements with the same score, what we need
5985 * is to find the element with both the right score and object. */
5986 x = x->forward[0];
5987 if (x && score == x->score && equalStringObjects(x->obj,obj)) {
5988 zslDeleteNode(zsl, x, update);
5989 zslFreeNode(x);
5990 return 1;
5991 } else {
5992 return 0; /* not found */
5993 }
5994 return 0; /* not found */
5995 }
5996
5997 /* Delete all the elements with score between min and max from the skiplist.
5998 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
5999 * Note that this function takes the reference to the hash table view of the
6000 * sorted set, in order to remove the elements from the hash table too. */
6001 static unsigned long zslDeleteRangeByScore(zskiplist *zsl, double min, double max, dict *dict) {
6002 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
6003 unsigned long removed = 0;
6004 int i;
6005
6006 x = zsl->header;
6007 for (i = zsl->level-1; i >= 0; i--) {
6008 while (x->forward[i] && x->forward[i]->score < min)
6009 x = x->forward[i];
6010 update[i] = x;
6011 }
6012 /* We may have multiple elements with the same score, what we need
6013 * is to find the element with both the right score and object. */
6014 x = x->forward[0];
6015 while (x && x->score <= max) {
6016 zskiplistNode *next = x->forward[0];
6017 zslDeleteNode(zsl, x, update);
6018 dictDelete(dict,x->obj);
6019 zslFreeNode(x);
6020 removed++;
6021 x = next;
6022 }
6023 return removed; /* not found */
6024 }
6025
6026 /* Delete all the elements with rank between start and end from the skiplist.
6027 * Start and end are inclusive. Note that start and end need to be 1-based */
6028 static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) {
6029 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
6030 unsigned long traversed = 0, removed = 0;
6031 int i;
6032
6033 x = zsl->header;
6034 for (i = zsl->level-1; i >= 0; i--) {
6035 while (x->forward[i] && (traversed + (i > 0 ? x->span[i-1] : 1)) < start) {
6036 traversed += i > 0 ? x->span[i-1] : 1;
6037 x = x->forward[i];
6038 }
6039 update[i] = x;
6040 }
6041
6042 traversed++;
6043 x = x->forward[0];
6044 while (x && traversed <= end) {
6045 zskiplistNode *next = x->forward[0];
6046 zslDeleteNode(zsl, x, update);
6047 dictDelete(dict,x->obj);
6048 zslFreeNode(x);
6049 removed++;
6050 traversed++;
6051 x = next;
6052 }
6053 return removed;
6054 }
6055
6056 /* Find the first node having a score equal or greater than the specified one.
6057 * Returns NULL if there is no match. */
6058 static zskiplistNode *zslFirstWithScore(zskiplist *zsl, double score) {
6059 zskiplistNode *x;
6060 int i;
6061
6062 x = zsl->header;
6063 for (i = zsl->level-1; i >= 0; i--) {
6064 while (x->forward[i] && x->forward[i]->score < score)
6065 x = x->forward[i];
6066 }
6067 /* We may have multiple elements with the same score, what we need
6068 * is to find the element with both the right score and object. */
6069 return x->forward[0];
6070 }
6071
6072 /* Find the rank for an element by both score and key.
6073 * Returns 0 when the element cannot be found, rank otherwise.
6074 * Note that the rank is 1-based due to the span of zsl->header to the
6075 * first element. */
6076 static unsigned long zslistTypeGetRank(zskiplist *zsl, double score, robj *o) {
6077 zskiplistNode *x;
6078 unsigned long rank = 0;
6079 int i;
6080
6081 x = zsl->header;
6082 for (i = zsl->level-1; i >= 0; i--) {
6083 while (x->forward[i] &&
6084 (x->forward[i]->score < score ||
6085 (x->forward[i]->score == score &&
6086 compareStringObjects(x->forward[i]->obj,o) <= 0))) {
6087 rank += i > 0 ? x->span[i-1] : 1;
6088 x = x->forward[i];
6089 }
6090
6091 /* x might be equal to zsl->header, so test if obj is non-NULL */
6092 if (x->obj && equalStringObjects(x->obj,o)) {
6093 return rank;
6094 }
6095 }
6096 return 0;
6097 }
6098
6099 /* Finds an element by its rank. The rank argument needs to be 1-based. */
6100 zskiplistNode* zslistTypeGetElementByRank(zskiplist *zsl, unsigned long rank) {
6101 zskiplistNode *x;
6102 unsigned long traversed = 0;
6103 int i;
6104
6105 x = zsl->header;
6106 for (i = zsl->level-1; i >= 0; i--) {
6107 while (x->forward[i] && (traversed + (i>0 ? x->span[i-1] : 1)) <= rank)
6108 {
6109 traversed += i > 0 ? x->span[i-1] : 1;
6110 x = x->forward[i];
6111 }
6112 if (traversed == rank) {
6113 return x;
6114 }
6115 }
6116 return NULL;
6117 }
6118
6119 /* The actual Z-commands implementations */
6120
6121 /* This generic command implements both ZADD and ZINCRBY.
6122 * scoreval is the score if the operation is a ZADD (doincrement == 0) or
6123 * the increment if the operation is a ZINCRBY (doincrement == 1). */
6124 static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scoreval, int doincrement) {
6125 robj *zsetobj;
6126 zset *zs;
6127 double *score;
6128
6129 if (isnan(scoreval)) {
6130 addReplySds(c,sdsnew("-ERR provide score is Not A Number (nan)\r\n"));
6131 return;
6132 }
6133
6134 zsetobj = lookupKeyWrite(c->db,key);
6135 if (zsetobj == NULL) {
6136 zsetobj = createZsetObject();
6137 dbAdd(c->db,key,zsetobj);
6138 } else {
6139 if (zsetobj->type != REDIS_ZSET) {
6140 addReply(c,shared.wrongtypeerr);
6141 return;
6142 }
6143 }
6144 zs = zsetobj->ptr;
6145
6146 /* Ok now since we implement both ZADD and ZINCRBY here the code
6147 * needs to handle the two different conditions. It's all about setting
6148 * '*score', that is, the new score to set, to the right value. */
6149 score = zmalloc(sizeof(double));
6150 if (doincrement) {
6151 dictEntry *de;
6152
6153 /* Read the old score. If the element was not present starts from 0 */
6154 de = dictFind(zs->dict,ele);
6155 if (de) {
6156 double *oldscore = dictGetEntryVal(de);
6157 *score = *oldscore + scoreval;
6158 } else {
6159 *score = scoreval;
6160 }
6161 if (isnan(*score)) {
6162 addReplySds(c,
6163 sdsnew("-ERR resulting score is Not A Number (nan)\r\n"));
6164 zfree(score);
6165 /* Note that we don't need to check if the zset may be empty and
6166 * should be removed here, as we can only obtain Nan as score if
6167 * there was already an element in the sorted set. */
6168 return;
6169 }
6170 } else {
6171 *score = scoreval;
6172 }
6173
6174 /* What follows is a simple remove and re-insert operation that is common
6175 * to both ZADD and ZINCRBY... */
6176 if (dictAdd(zs->dict,ele,score) == DICT_OK) {
6177 /* case 1: New element */
6178 incrRefCount(ele); /* added to hash */
6179 zslInsert(zs->zsl,*score,ele);
6180 incrRefCount(ele); /* added to skiplist */
6181 server.dirty++;
6182 if (doincrement)
6183 addReplyDouble(c,*score);
6184 else
6185 addReply(c,shared.cone);
6186 } else {
6187 dictEntry *de;
6188 double *oldscore;
6189
6190 /* case 2: Score update operation */
6191 de = dictFind(zs->dict,ele);
6192 redisAssert(de != NULL);
6193 oldscore = dictGetEntryVal(de);
6194 if (*score != *oldscore) {
6195 int deleted;
6196
6197 /* Remove and insert the element in the skip list with new score */
6198 deleted = zslDelete(zs->zsl,*oldscore,ele);
6199 redisAssert(deleted != 0);
6200 zslInsert(zs->zsl,*score,ele);
6201 incrRefCount(ele);
6202 /* Update the score in the hash table */
6203 dictReplace(zs->dict,ele,score);
6204 server.dirty++;
6205 } else {
6206 zfree(score);
6207 }
6208 if (doincrement)
6209 addReplyDouble(c,*score);
6210 else
6211 addReply(c,shared.czero);
6212 }
6213 }
6214
6215 static void zaddCommand(redisClient *c) {
6216 double scoreval;
6217
6218 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
6219 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,0);
6220 }
6221
6222 static void zincrbyCommand(redisClient *c) {
6223 double scoreval;
6224
6225 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
6226 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,1);
6227 }
6228
6229 static void zremCommand(redisClient *c) {
6230 robj *zsetobj;
6231 zset *zs;
6232 dictEntry *de;
6233 double *oldscore;
6234 int deleted;
6235
6236 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
6237 checkType(c,zsetobj,REDIS_ZSET)) return;
6238
6239 zs = zsetobj->ptr;
6240 de = dictFind(zs->dict,c->argv[2]);
6241 if (de == NULL) {
6242 addReply(c,shared.czero);
6243 return;
6244 }
6245 /* Delete from the skiplist */
6246 oldscore = dictGetEntryVal(de);
6247 deleted = zslDelete(zs->zsl,*oldscore,c->argv[2]);
6248 redisAssert(deleted != 0);
6249
6250 /* Delete from the hash table */
6251 dictDelete(zs->dict,c->argv[2]);
6252 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
6253 if (dictSize(zs->dict) == 0) dbDelete(c->db,c->argv[1]);
6254 server.dirty++;
6255 addReply(c,shared.cone);
6256 }
6257
6258 static void zremrangebyscoreCommand(redisClient *c) {
6259 double min;
6260 double max;
6261 long deleted;
6262 robj *zsetobj;
6263 zset *zs;
6264
6265 if ((getDoubleFromObjectOrReply(c, c->argv[2], &min, NULL) != REDIS_OK) ||
6266 (getDoubleFromObjectOrReply(c, c->argv[3], &max, NULL) != REDIS_OK)) return;
6267
6268 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
6269 checkType(c,zsetobj,REDIS_ZSET)) return;
6270
6271 zs = zsetobj->ptr;
6272 deleted = zslDeleteRangeByScore(zs->zsl,min,max,zs->dict);
6273 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
6274 if (dictSize(zs->dict) == 0) dbDelete(c->db,c->argv[1]);
6275 server.dirty += deleted;
6276 addReplyLongLong(c,deleted);
6277 }
6278
6279 static void zremrangebyrankCommand(redisClient *c) {
6280 long start;
6281 long end;
6282 int llen;
6283 long deleted;
6284 robj *zsetobj;
6285 zset *zs;
6286
6287 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
6288 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
6289
6290 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
6291 checkType(c,zsetobj,REDIS_ZSET)) return;
6292 zs = zsetobj->ptr;
6293 llen = zs->zsl->length;
6294
6295 /* convert negative indexes */
6296 if (start < 0) start = llen+start;
6297 if (end < 0) end = llen+end;
6298 if (start < 0) start = 0;
6299 if (end < 0) end = 0;
6300
6301 /* indexes sanity checks */
6302 if (start > end || start >= llen) {
6303 addReply(c,shared.czero);
6304 return;
6305 }
6306 if (end >= llen) end = llen-1;
6307
6308 /* increment start and end because zsl*Rank functions
6309 * use 1-based rank */
6310 deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
6311 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
6312 if (dictSize(zs->dict) == 0) dbDelete(c->db,c->argv[1]);
6313 server.dirty += deleted;
6314 addReplyLongLong(c, deleted);
6315 }
6316
6317 typedef struct {
6318 dict *dict;
6319 double weight;
6320 } zsetopsrc;
6321
6322 static int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) {
6323 zsetopsrc *d1 = (void*) s1, *d2 = (void*) s2;
6324 unsigned long size1, size2;
6325 size1 = d1->dict ? dictSize(d1->dict) : 0;
6326 size2 = d2->dict ? dictSize(d2->dict) : 0;
6327 return size1 - size2;
6328 }
6329
6330 #define REDIS_AGGR_SUM 1
6331 #define REDIS_AGGR_MIN 2
6332 #define REDIS_AGGR_MAX 3
6333 #define zunionInterDictValue(_e) (dictGetEntryVal(_e) == NULL ? 1.0 : *(double*)dictGetEntryVal(_e))
6334
6335 inline static void zunionInterAggregate(double *target, double val, int aggregate) {
6336 if (aggregate == REDIS_AGGR_SUM) {
6337 *target = *target + val;
6338 } else if (aggregate == REDIS_AGGR_MIN) {
6339 *target = val < *target ? val : *target;
6340 } else if (aggregate == REDIS_AGGR_MAX) {
6341 *target = val > *target ? val : *target;
6342 } else {
6343 /* safety net */
6344 redisPanic("Unknown ZUNION/INTER aggregate type");
6345 }
6346 }
6347
6348 static void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) {
6349 int i, j, setnum;
6350 int aggregate = REDIS_AGGR_SUM;
6351 zsetopsrc *src;
6352 robj *dstobj;
6353 zset *dstzset;
6354 dictIterator *di;
6355 dictEntry *de;
6356
6357 /* expect setnum input keys to be given */
6358 setnum = atoi(c->argv[2]->ptr);
6359 if (setnum < 1) {
6360 addReplySds(c,sdsnew("-ERR at least 1 input key is needed for ZUNIONSTORE/ZINTERSTORE\r\n"));
6361 return;
6362 }
6363
6364 /* test if the expected number of keys would overflow */
6365 if (3+setnum > c->argc) {
6366 addReply(c,shared.syntaxerr);
6367 return;
6368 }
6369
6370 /* read keys to be used for input */
6371 src = zmalloc(sizeof(zsetopsrc) * setnum);
6372 for (i = 0, j = 3; i < setnum; i++, j++) {
6373 robj *obj = lookupKeyWrite(c->db,c->argv[j]);
6374 if (!obj) {
6375 src[i].dict = NULL;
6376 } else {
6377 if (obj->type == REDIS_ZSET) {
6378 src[i].dict = ((zset*)obj->ptr)->dict;
6379 } else if (obj->type == REDIS_SET) {
6380 src[i].dict = (obj->ptr);
6381 } else {
6382 zfree(src);
6383 addReply(c,shared.wrongtypeerr);
6384 return;
6385 }
6386 }
6387
6388 /* default all weights to 1 */
6389 src[i].weight = 1.0;
6390 }
6391
6392 /* parse optional extra arguments */
6393 if (j < c->argc) {
6394 int remaining = c->argc - j;
6395
6396 while (remaining) {
6397 if (remaining >= (setnum + 1) && !strcasecmp(c->argv[j]->ptr,"weights")) {
6398 j++; remaining--;
6399 for (i = 0; i < setnum; i++, j++, remaining--) {
6400 if (getDoubleFromObjectOrReply(c, c->argv[j], &src[i].weight, NULL) != REDIS_OK)
6401 return;
6402 }
6403 } else if (remaining >= 2 && !strcasecmp(c->argv[j]->ptr,"aggregate")) {
6404 j++; remaining--;
6405 if (!strcasecmp(c->argv[j]->ptr,"sum")) {
6406 aggregate = REDIS_AGGR_SUM;
6407 } else if (!strcasecmp(c->argv[j]->ptr,"min")) {
6408 aggregate = REDIS_AGGR_MIN;
6409 } else if (!strcasecmp(c->argv[j]->ptr,"max")) {
6410 aggregate = REDIS_AGGR_MAX;
6411 } else {
6412 zfree(src);
6413 addReply(c,shared.syntaxerr);
6414 return;
6415 }
6416 j++; remaining--;
6417 } else {
6418 zfree(src);
6419 addReply(c,shared.syntaxerr);
6420 return;
6421 }
6422 }
6423 }
6424
6425 /* sort sets from the smallest to largest, this will improve our
6426 * algorithm's performance */
6427 qsort(src,setnum,sizeof(zsetopsrc),qsortCompareZsetopsrcByCardinality);
6428
6429 dstobj = createZsetObject();
6430 dstzset = dstobj->ptr;
6431
6432 if (op == REDIS_OP_INTER) {
6433 /* skip going over all entries if the smallest zset is NULL or empty */
6434 if (src[0].dict && dictSize(src[0].dict) > 0) {
6435 /* precondition: as src[0].dict is non-empty and the zsets are ordered
6436 * from small to large, all src[i > 0].dict are non-empty too */
6437 di = dictGetIterator(src[0].dict);
6438 while((de = dictNext(di)) != NULL) {
6439 double *score = zmalloc(sizeof(double)), value;
6440 *score = src[0].weight * zunionInterDictValue(de);
6441
6442 for (j = 1; j < setnum; j++) {
6443 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
6444 if (other) {
6445 value = src[j].weight * zunionInterDictValue(other);
6446 zunionInterAggregate(score, value, aggregate);
6447 } else {
6448 break;
6449 }
6450 }
6451
6452 /* skip entry when not present in every source dict */
6453 if (j != setnum) {
6454 zfree(score);
6455 } else {
6456 robj *o = dictGetEntryKey(de);
6457 dictAdd(dstzset->dict,o,score);
6458 incrRefCount(o); /* added to dictionary */
6459 zslInsert(dstzset->zsl,*score,o);
6460 incrRefCount(o); /* added to skiplist */
6461 }
6462 }
6463 dictReleaseIterator(di);
6464 }
6465 } else if (op == REDIS_OP_UNION) {
6466 for (i = 0; i < setnum; i++) {
6467 if (!src[i].dict) continue;
6468
6469 di = dictGetIterator(src[i].dict);
6470 while((de = dictNext(di)) != NULL) {
6471 /* skip key when already processed */
6472 if (dictFind(dstzset->dict,dictGetEntryKey(de)) != NULL) continue;
6473
6474 double *score = zmalloc(sizeof(double)), value;
6475 *score = src[i].weight * zunionInterDictValue(de);
6476
6477 /* because the zsets are sorted by size, its only possible
6478 * for sets at larger indices to hold this entry */
6479 for (j = (i+1); j < setnum; j++) {
6480 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
6481 if (other) {
6482 value = src[j].weight * zunionInterDictValue(other);
6483 zunionInterAggregate(score, value, aggregate);
6484 }
6485 }
6486
6487 robj *o = dictGetEntryKey(de);
6488 dictAdd(dstzset->dict,o,score);
6489 incrRefCount(o); /* added to dictionary */
6490 zslInsert(dstzset->zsl,*score,o);
6491 incrRefCount(o); /* added to skiplist */
6492 }
6493 dictReleaseIterator(di);
6494 }
6495 } else {
6496 /* unknown operator */
6497 redisAssert(op == REDIS_OP_INTER || op == REDIS_OP_UNION);
6498 }
6499
6500 dbDelete(c->db,dstkey);
6501 if (dstzset->zsl->length) {
6502 dbAdd(c->db,dstkey,dstobj);
6503 addReplyLongLong(c, dstzset->zsl->length);
6504 server.dirty++;
6505 } else {
6506 decrRefCount(dstobj);
6507 addReply(c, shared.czero);
6508 }
6509 zfree(src);
6510 }
6511
6512 static void zunionstoreCommand(redisClient *c) {
6513 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_UNION);
6514 }
6515
6516 static void zinterstoreCommand(redisClient *c) {
6517 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_INTER);
6518 }
6519
6520 static void zrangeGenericCommand(redisClient *c, int reverse) {
6521 robj *o;
6522 long start;
6523 long end;
6524 int withscores = 0;
6525 int llen;
6526 int rangelen, j;
6527 zset *zsetobj;
6528 zskiplist *zsl;
6529 zskiplistNode *ln;
6530 robj *ele;
6531
6532 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
6533 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
6534
6535 if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) {
6536 withscores = 1;
6537 } else if (c->argc >= 5) {
6538 addReply(c,shared.syntaxerr);
6539 return;
6540 }
6541
6542 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
6543 || checkType(c,o,REDIS_ZSET)) return;
6544 zsetobj = o->ptr;
6545 zsl = zsetobj->zsl;
6546 llen = zsl->length;
6547
6548 /* convert negative indexes */
6549 if (start < 0) start = llen+start;
6550 if (end < 0) end = llen+end;
6551 if (start < 0) start = 0;
6552 if (end < 0) end = 0;
6553
6554 /* indexes sanity checks */
6555 if (start > end || start >= llen) {
6556 /* Out of range start or start > end result in empty list */
6557 addReply(c,shared.emptymultibulk);
6558 return;
6559 }
6560 if (end >= llen) end = llen-1;
6561 rangelen = (end-start)+1;
6562
6563 /* check if starting point is trivial, before searching
6564 * the element in log(N) time */
6565 if (reverse) {
6566 ln = start == 0 ? zsl->tail : zslistTypeGetElementByRank(zsl, llen-start);
6567 } else {
6568 ln = start == 0 ?
6569 zsl->header->forward[0] : zslistTypeGetElementByRank(zsl, start+1);
6570 }
6571
6572 /* Return the result in form of a multi-bulk reply */
6573 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",
6574 withscores ? (rangelen*2) : rangelen));
6575 for (j = 0; j < rangelen; j++) {
6576 ele = ln->obj;
6577 addReplyBulk(c,ele);
6578 if (withscores)
6579 addReplyDouble(c,ln->score);
6580 ln = reverse ? ln->backward : ln->forward[0];
6581 }
6582 }
6583
6584 static void zrangeCommand(redisClient *c) {
6585 zrangeGenericCommand(c,0);
6586 }
6587
6588 static void zrevrangeCommand(redisClient *c) {
6589 zrangeGenericCommand(c,1);
6590 }
6591
6592 /* This command implements both ZRANGEBYSCORE and ZCOUNT.
6593 * If justcount is non-zero, just the count is returned. */
6594 static void genericZrangebyscoreCommand(redisClient *c, int justcount) {
6595 robj *o;
6596 double min, max;
6597 int minex = 0, maxex = 0; /* are min or max exclusive? */
6598 int offset = 0, limit = -1;
6599 int withscores = 0;
6600 int badsyntax = 0;
6601
6602 /* Parse the min-max interval. If one of the values is prefixed
6603 * by the "(" character, it's considered "open". For instance
6604 * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max
6605 * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */
6606 if (((char*)c->argv[2]->ptr)[0] == '(') {
6607 min = strtod((char*)c->argv[2]->ptr+1,NULL);
6608 minex = 1;
6609 } else {
6610 min = strtod(c->argv[2]->ptr,NULL);
6611 }
6612 if (((char*)c->argv[3]->ptr)[0] == '(') {
6613 max = strtod((char*)c->argv[3]->ptr+1,NULL);
6614 maxex = 1;
6615 } else {
6616 max = strtod(c->argv[3]->ptr,NULL);
6617 }
6618
6619 /* Parse "WITHSCORES": note that if the command was called with
6620 * the name ZCOUNT then we are sure that c->argc == 4, so we'll never
6621 * enter the following paths to parse WITHSCORES and LIMIT. */
6622 if (c->argc == 5 || c->argc == 8) {
6623 if (strcasecmp(c->argv[c->argc-1]->ptr,"withscores") == 0)
6624 withscores = 1;
6625 else
6626 badsyntax = 1;
6627 }
6628 if (c->argc != (4 + withscores) && c->argc != (7 + withscores))
6629 badsyntax = 1;
6630 if (badsyntax) {
6631 addReplySds(c,
6632 sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
6633 return;
6634 }
6635
6636 /* Parse "LIMIT" */
6637 if (c->argc == (7 + withscores) && strcasecmp(c->argv[4]->ptr,"limit")) {
6638 addReply(c,shared.syntaxerr);
6639 return;
6640 } else if (c->argc == (7 + withscores)) {
6641 offset = atoi(c->argv[5]->ptr);
6642 limit = atoi(c->argv[6]->ptr);
6643 if (offset < 0) offset = 0;
6644 }
6645
6646 /* Ok, lookup the key and get the range */
6647 o = lookupKeyRead(c->db,c->argv[1]);
6648 if (o == NULL) {
6649 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
6650 } else {
6651 if (o->type != REDIS_ZSET) {
6652 addReply(c,shared.wrongtypeerr);
6653 } else {
6654 zset *zsetobj = o->ptr;
6655 zskiplist *zsl = zsetobj->zsl;
6656 zskiplistNode *ln;
6657 robj *ele, *lenobj = NULL;
6658 unsigned long rangelen = 0;
6659
6660 /* Get the first node with the score >= min, or with
6661 * score > min if 'minex' is true. */
6662 ln = zslFirstWithScore(zsl,min);
6663 while (minex && ln && ln->score == min) ln = ln->forward[0];
6664
6665 if (ln == NULL) {
6666 /* No element matching the speciifed interval */
6667 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
6668 return;
6669 }
6670
6671 /* We don't know in advance how many matching elements there
6672 * are in the list, so we push this object that will represent
6673 * the multi-bulk length in the output buffer, and will "fix"
6674 * it later */
6675 if (!justcount) {
6676 lenobj = createObject(REDIS_STRING,NULL);
6677 addReply(c,lenobj);
6678 decrRefCount(lenobj);
6679 }
6680
6681 while(ln && (maxex ? (ln->score < max) : (ln->score <= max))) {
6682 if (offset) {
6683 offset--;
6684 ln = ln->forward[0];
6685 continue;
6686 }
6687 if (limit == 0) break;
6688 if (!justcount) {
6689 ele = ln->obj;
6690 addReplyBulk(c,ele);
6691 if (withscores)
6692 addReplyDouble(c,ln->score);
6693 }
6694 ln = ln->forward[0];
6695 rangelen++;
6696 if (limit > 0) limit--;
6697 }
6698 if (justcount) {
6699 addReplyLongLong(c,(long)rangelen);
6700 } else {
6701 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",
6702 withscores ? (rangelen*2) : rangelen);
6703 }
6704 }
6705 }
6706 }
6707
6708 static void zrangebyscoreCommand(redisClient *c) {
6709 genericZrangebyscoreCommand(c,0);
6710 }
6711
6712 static void zcountCommand(redisClient *c) {
6713 genericZrangebyscoreCommand(c,1);
6714 }
6715
6716 static void zcardCommand(redisClient *c) {
6717 robj *o;
6718 zset *zs;
6719
6720 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6721 checkType(c,o,REDIS_ZSET)) return;
6722
6723 zs = o->ptr;
6724 addReplyUlong(c,zs->zsl->length);
6725 }
6726
6727 static void zscoreCommand(redisClient *c) {
6728 robj *o;
6729 zset *zs;
6730 dictEntry *de;
6731
6732 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6733 checkType(c,o,REDIS_ZSET)) return;
6734
6735 zs = o->ptr;
6736 de = dictFind(zs->dict,c->argv[2]);
6737 if (!de) {
6738 addReply(c,shared.nullbulk);
6739 } else {
6740 double *score = dictGetEntryVal(de);
6741
6742 addReplyDouble(c,*score);
6743 }
6744 }
6745
6746 static void zrankGenericCommand(redisClient *c, int reverse) {
6747 robj *o;
6748 zset *zs;
6749 zskiplist *zsl;
6750 dictEntry *de;
6751 unsigned long rank;
6752 double *score;
6753
6754 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6755 checkType(c,o,REDIS_ZSET)) return;
6756
6757 zs = o->ptr;
6758 zsl = zs->zsl;
6759 de = dictFind(zs->dict,c->argv[2]);
6760 if (!de) {
6761 addReply(c,shared.nullbulk);
6762 return;
6763 }
6764
6765 score = dictGetEntryVal(de);
6766 rank = zslistTypeGetRank(zsl, *score, c->argv[2]);
6767 if (rank) {
6768 if (reverse) {
6769 addReplyLongLong(c, zsl->length - rank);
6770 } else {
6771 addReplyLongLong(c, rank-1);
6772 }
6773 } else {
6774 addReply(c,shared.nullbulk);
6775 }
6776 }
6777
6778 static void zrankCommand(redisClient *c) {
6779 zrankGenericCommand(c, 0);
6780 }
6781
6782 static void zrevrankCommand(redisClient *c) {
6783 zrankGenericCommand(c, 1);
6784 }
6785
6786 /* ========================= Hashes utility functions ======================= */
6787 #define REDIS_HASH_KEY 1
6788 #define REDIS_HASH_VALUE 2
6789
6790 /* Check the length of a number of objects to see if we need to convert a
6791 * zipmap to a real hash. Note that we only check string encoded objects
6792 * as their string length can be queried in constant time. */
6793 static void hashTypeTryConversion(robj *subject, robj **argv, int start, int end) {
6794 int i;
6795 if (subject->encoding != REDIS_ENCODING_ZIPMAP) return;
6796
6797 for (i = start; i <= end; i++) {
6798 if (argv[i]->encoding == REDIS_ENCODING_RAW &&
6799 sdslen(argv[i]->ptr) > server.hash_max_zipmap_value)
6800 {
6801 convertToRealHash(subject);
6802 return;
6803 }
6804 }
6805 }
6806
6807 /* Encode given objects in-place when the hash uses a dict. */
6808 static void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2) {
6809 if (subject->encoding == REDIS_ENCODING_HT) {
6810 if (o1) *o1 = tryObjectEncoding(*o1);
6811 if (o2) *o2 = tryObjectEncoding(*o2);
6812 }
6813 }
6814
6815 /* Get the value from a hash identified by key. Returns either a string
6816 * object or NULL if the value cannot be found. The refcount of the object
6817 * is always increased by 1 when the value was found. */
6818 static robj *hashTypeGet(robj *o, robj *key) {
6819 robj *value = NULL;
6820 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6821 unsigned char *v;
6822 unsigned int vlen;
6823 key = getDecodedObject(key);
6824 if (zipmapGet(o->ptr,key->ptr,sdslen(key->ptr),&v,&vlen)) {
6825 value = createStringObject((char*)v,vlen);
6826 }
6827 decrRefCount(key);
6828 } else {
6829 dictEntry *de = dictFind(o->ptr,key);
6830 if (de != NULL) {
6831 value = dictGetEntryVal(de);
6832 incrRefCount(value);
6833 }
6834 }
6835 return value;
6836 }
6837
6838 /* Test if the key exists in the given hash. Returns 1 if the key
6839 * exists and 0 when it doesn't. */
6840 static int hashTypeExists(robj *o, robj *key) {
6841 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6842 key = getDecodedObject(key);
6843 if (zipmapExists(o->ptr,key->ptr,sdslen(key->ptr))) {
6844 decrRefCount(key);
6845 return 1;
6846 }
6847 decrRefCount(key);
6848 } else {
6849 if (dictFind(o->ptr,key) != NULL) {
6850 return 1;
6851 }
6852 }
6853 return 0;
6854 }
6855
6856 /* Add an element, discard the old if the key already exists.
6857 * Return 0 on insert and 1 on update. */
6858 static int hashTypeSet(robj *o, robj *key, robj *value) {
6859 int update = 0;
6860 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6861 key = getDecodedObject(key);
6862 value = getDecodedObject(value);
6863 o->ptr = zipmapSet(o->ptr,
6864 key->ptr,sdslen(key->ptr),
6865 value->ptr,sdslen(value->ptr), &update);
6866 decrRefCount(key);
6867 decrRefCount(value);
6868
6869 /* Check if the zipmap needs to be upgraded to a real hash table */
6870 if (zipmapLen(o->ptr) > server.hash_max_zipmap_entries)
6871 convertToRealHash(o);
6872 } else {
6873 if (dictReplace(o->ptr,key,value)) {
6874 /* Insert */
6875 incrRefCount(key);
6876 } else {
6877 /* Update */
6878 update = 1;
6879 }
6880 incrRefCount(value);
6881 }
6882 return update;
6883 }
6884
6885 /* Delete an element from a hash.
6886 * Return 1 on deleted and 0 on not found. */
6887 static int hashTypeDelete(robj *o, robj *key) {
6888 int deleted = 0;
6889 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6890 key = getDecodedObject(key);
6891 o->ptr = zipmapDel(o->ptr,key->ptr,sdslen(key->ptr), &deleted);
6892 decrRefCount(key);
6893 } else {
6894 deleted = dictDelete((dict*)o->ptr,key) == DICT_OK;
6895 /* Always check if the dictionary needs a resize after a delete. */
6896 if (deleted && htNeedsResize(o->ptr)) dictResize(o->ptr);
6897 }
6898 return deleted;
6899 }
6900
6901 /* Return the number of elements in a hash. */
6902 static unsigned long hashTypeLength(robj *o) {
6903 return (o->encoding == REDIS_ENCODING_ZIPMAP) ?
6904 zipmapLen((unsigned char*)o->ptr) : dictSize((dict*)o->ptr);
6905 }
6906
6907 /* Structure to hold hash iteration abstration. Note that iteration over
6908 * hashes involves both fields and values. Because it is possible that
6909 * not both are required, store pointers in the iterator to avoid
6910 * unnecessary memory allocation for fields/values. */
6911 typedef struct {
6912 int encoding;
6913 unsigned char *zi;
6914 unsigned char *zk, *zv;
6915 unsigned int zklen, zvlen;
6916
6917 dictIterator *di;
6918 dictEntry *de;
6919 } hashTypeIterator;
6920
6921 static hashTypeIterator *hashTypeInitIterator(robj *subject) {
6922 hashTypeIterator *hi = zmalloc(sizeof(hashTypeIterator));
6923 hi->encoding = subject->encoding;
6924 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6925 hi->zi = zipmapRewind(subject->ptr);
6926 } else if (hi->encoding == REDIS_ENCODING_HT) {
6927 hi->di = dictGetIterator(subject->ptr);
6928 } else {
6929 redisAssert(NULL);
6930 }
6931 return hi;
6932 }
6933
6934 static void hashTypeReleaseIterator(hashTypeIterator *hi) {
6935 if (hi->encoding == REDIS_ENCODING_HT) {
6936 dictReleaseIterator(hi->di);
6937 }
6938 zfree(hi);
6939 }
6940
6941 /* Move to the next entry in the hash. Return REDIS_OK when the next entry
6942 * could be found and REDIS_ERR when the iterator reaches the end. */
6943 static int hashTypeNext(hashTypeIterator *hi) {
6944 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6945 if ((hi->zi = zipmapNext(hi->zi, &hi->zk, &hi->zklen,
6946 &hi->zv, &hi->zvlen)) == NULL) return REDIS_ERR;
6947 } else {
6948 if ((hi->de = dictNext(hi->di)) == NULL) return REDIS_ERR;
6949 }
6950 return REDIS_OK;
6951 }
6952
6953 /* Get key or value object at current iteration position.
6954 * This increases the refcount of the field object by 1. */
6955 static robj *hashTypeCurrent(hashTypeIterator *hi, int what) {
6956 robj *o;
6957 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6958 if (what & REDIS_HASH_KEY) {
6959 o = createStringObject((char*)hi->zk,hi->zklen);
6960 } else {
6961 o = createStringObject((char*)hi->zv,hi->zvlen);
6962 }
6963 } else {
6964 if (what & REDIS_HASH_KEY) {
6965 o = dictGetEntryKey(hi->de);
6966 } else {
6967 o = dictGetEntryVal(hi->de);
6968 }
6969 incrRefCount(o);
6970 }
6971 return o;
6972 }
6973
6974 static robj *hashTypeLookupWriteOrCreate(redisClient *c, robj *key) {
6975 robj *o = lookupKeyWrite(c->db,key);
6976 if (o == NULL) {
6977 o = createHashObject();
6978 dbAdd(c->db,key,o);
6979 } else {
6980 if (o->type != REDIS_HASH) {
6981 addReply(c,shared.wrongtypeerr);
6982 return NULL;
6983 }
6984 }
6985 return o;
6986 }
6987
6988 /* ============================= Hash commands ============================== */
6989 static void hsetCommand(redisClient *c) {
6990 int update;
6991 robj *o;
6992
6993 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6994 hashTypeTryConversion(o,c->argv,2,3);
6995 hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
6996 update = hashTypeSet(o,c->argv[2],c->argv[3]);
6997 addReply(c, update ? shared.czero : shared.cone);
6998 server.dirty++;
6999 }
7000
7001 static void hsetnxCommand(redisClient *c) {
7002 robj *o;
7003 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
7004 hashTypeTryConversion(o,c->argv,2,3);
7005
7006 if (hashTypeExists(o, c->argv[2])) {
7007 addReply(c, shared.czero);
7008 } else {
7009 hashTypeTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
7010 hashTypeSet(o,c->argv[2],c->argv[3]);
7011 addReply(c, shared.cone);
7012 server.dirty++;
7013 }
7014 }
7015
7016 static void hmsetCommand(redisClient *c) {
7017 int i;
7018 robj *o;
7019
7020 if ((c->argc % 2) == 1) {
7021 addReplySds(c,sdsnew("-ERR wrong number of arguments for HMSET\r\n"));
7022 return;
7023 }
7024
7025 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
7026 hashTypeTryConversion(o,c->argv,2,c->argc-1);
7027 for (i = 2; i < c->argc; i += 2) {
7028 hashTypeTryObjectEncoding(o,&c->argv[i], &c->argv[i+1]);
7029 hashTypeSet(o,c->argv[i],c->argv[i+1]);
7030 }
7031 addReply(c, shared.ok);
7032 server.dirty++;
7033 }
7034
7035 static void hincrbyCommand(redisClient *c) {
7036 long long value, incr;
7037 robj *o, *current, *new;
7038
7039 if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return;
7040 if ((o = hashTypeLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
7041 if ((current = hashTypeGet(o,c->argv[2])) != NULL) {
7042 if (getLongLongFromObjectOrReply(c,current,&value,
7043 "hash value is not an integer") != REDIS_OK) {
7044 decrRefCount(current);
7045 return;
7046 }
7047 decrRefCount(current);
7048 } else {
7049 value = 0;
7050 }
7051
7052 value += incr;
7053 new = createStringObjectFromLongLong(value);
7054 hashTypeTryObjectEncoding(o,&c->argv[2],NULL);
7055 hashTypeSet(o,c->argv[2],new);
7056 decrRefCount(new);
7057 addReplyLongLong(c,value);
7058 server.dirty++;
7059 }
7060
7061 static void hgetCommand(redisClient *c) {
7062 robj *o, *value;
7063 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
7064 checkType(c,o,REDIS_HASH)) return;
7065
7066 if ((value = hashTypeGet(o,c->argv[2])) != NULL) {
7067 addReplyBulk(c,value);
7068 decrRefCount(value);
7069 } else {
7070 addReply(c,shared.nullbulk);
7071 }
7072 }
7073
7074 static void hmgetCommand(redisClient *c) {
7075 int i;
7076 robj *o, *value;
7077 o = lookupKeyRead(c->db,c->argv[1]);
7078 if (o != NULL && o->type != REDIS_HASH) {
7079 addReply(c,shared.wrongtypeerr);
7080 }
7081
7082 /* Note the check for o != NULL happens inside the loop. This is
7083 * done because objects that cannot be found are considered to be
7084 * an empty hash. The reply should then be a series of NULLs. */
7085 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-2));
7086 for (i = 2; i < c->argc; i++) {
7087 if (o != NULL && (value = hashTypeGet(o,c->argv[i])) != NULL) {
7088 addReplyBulk(c,value);
7089 decrRefCount(value);
7090 } else {
7091 addReply(c,shared.nullbulk);
7092 }
7093 }
7094 }
7095
7096 static void hdelCommand(redisClient *c) {
7097 robj *o;
7098 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
7099 checkType(c,o,REDIS_HASH)) return;
7100
7101 if (hashTypeDelete(o,c->argv[2])) {
7102 if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]);
7103 addReply(c,shared.cone);
7104 server.dirty++;
7105 } else {
7106 addReply(c,shared.czero);
7107 }
7108 }
7109
7110 static void hlenCommand(redisClient *c) {
7111 robj *o;
7112 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
7113 checkType(c,o,REDIS_HASH)) return;
7114
7115 addReplyUlong(c,hashTypeLength(o));
7116 }
7117
7118 static void genericHgetallCommand(redisClient *c, int flags) {
7119 robj *o, *lenobj, *obj;
7120 unsigned long count = 0;
7121 hashTypeIterator *hi;
7122
7123 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
7124 || checkType(c,o,REDIS_HASH)) return;
7125
7126 lenobj = createObject(REDIS_STRING,NULL);
7127 addReply(c,lenobj);
7128 decrRefCount(lenobj);
7129
7130 hi = hashTypeInitIterator(o);
7131 while (hashTypeNext(hi) != REDIS_ERR) {
7132 if (flags & REDIS_HASH_KEY) {
7133 obj = hashTypeCurrent(hi,REDIS_HASH_KEY);
7134 addReplyBulk(c,obj);
7135 decrRefCount(obj);
7136 count++;
7137 }
7138 if (flags & REDIS_HASH_VALUE) {
7139 obj = hashTypeCurrent(hi,REDIS_HASH_VALUE);
7140 addReplyBulk(c,obj);
7141 decrRefCount(obj);
7142 count++;
7143 }
7144 }
7145 hashTypeReleaseIterator(hi);
7146
7147 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",count);
7148 }
7149
7150 static void hkeysCommand(redisClient *c) {
7151 genericHgetallCommand(c,REDIS_HASH_KEY);
7152 }
7153
7154 static void hvalsCommand(redisClient *c) {
7155 genericHgetallCommand(c,REDIS_HASH_VALUE);
7156 }
7157
7158 static void hgetallCommand(redisClient *c) {
7159 genericHgetallCommand(c,REDIS_HASH_KEY|REDIS_HASH_VALUE);
7160 }
7161
7162 static void hexistsCommand(redisClient *c) {
7163 robj *o;
7164 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
7165 checkType(c,o,REDIS_HASH)) return;
7166
7167 addReply(c, hashTypeExists(o,c->argv[2]) ? shared.cone : shared.czero);
7168 }
7169
7170 static void convertToRealHash(robj *o) {
7171 unsigned char *key, *val, *p, *zm = o->ptr;
7172 unsigned int klen, vlen;
7173 dict *dict = dictCreate(&hashDictType,NULL);
7174
7175 assert(o->type == REDIS_HASH && o->encoding != REDIS_ENCODING_HT);
7176 p = zipmapRewind(zm);
7177 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
7178 robj *keyobj, *valobj;
7179
7180 keyobj = createStringObject((char*)key,klen);
7181 valobj = createStringObject((char*)val,vlen);
7182 keyobj = tryObjectEncoding(keyobj);
7183 valobj = tryObjectEncoding(valobj);
7184 dictAdd(dict,keyobj,valobj);
7185 }
7186 o->encoding = REDIS_ENCODING_HT;
7187 o->ptr = dict;
7188 zfree(zm);
7189 }
7190
7191 /* ========================= Non type-specific commands ==================== */
7192
7193 static void flushdbCommand(redisClient *c) {
7194 server.dirty += dictSize(c->db->dict);
7195 touchWatchedKeysOnFlush(c->db->id);
7196 dictEmpty(c->db->dict);
7197 dictEmpty(c->db->expires);
7198 addReply(c,shared.ok);
7199 }
7200
7201 static void flushallCommand(redisClient *c) {
7202 touchWatchedKeysOnFlush(-1);
7203 server.dirty += emptyDb();
7204 addReply(c,shared.ok);
7205 if (server.bgsavechildpid != -1) {
7206 kill(server.bgsavechildpid,SIGKILL);
7207 rdbRemoveTempFile(server.bgsavechildpid);
7208 }
7209 rdbSave(server.dbfilename);
7210 server.dirty++;
7211 }
7212
7213 static redisSortOperation *createSortOperation(int type, robj *pattern) {
7214 redisSortOperation *so = zmalloc(sizeof(*so));
7215 so->type = type;
7216 so->pattern = pattern;
7217 return so;
7218 }
7219
7220 /* Return the value associated to the key with a name obtained
7221 * substituting the first occurence of '*' in 'pattern' with 'subst'.
7222 * The returned object will always have its refcount increased by 1
7223 * when it is non-NULL. */
7224 static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
7225 char *p, *f;
7226 sds spat, ssub;
7227 robj keyobj, fieldobj, *o;
7228 int prefixlen, sublen, postfixlen, fieldlen;
7229 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
7230 struct {
7231 long len;
7232 long free;
7233 char buf[REDIS_SORTKEY_MAX+1];
7234 } keyname, fieldname;
7235
7236 /* If the pattern is "#" return the substitution object itself in order
7237 * to implement the "SORT ... GET #" feature. */
7238 spat = pattern->ptr;
7239 if (spat[0] == '#' && spat[1] == '\0') {
7240 incrRefCount(subst);
7241 return subst;
7242 }
7243
7244 /* The substitution object may be specially encoded. If so we create
7245 * a decoded object on the fly. Otherwise getDecodedObject will just
7246 * increment the ref count, that we'll decrement later. */
7247 subst = getDecodedObject(subst);
7248
7249 ssub = subst->ptr;
7250 if (sdslen(spat)+sdslen(ssub)-1 > REDIS_SORTKEY_MAX) return NULL;
7251 p = strchr(spat,'*');
7252 if (!p) {
7253 decrRefCount(subst);
7254 return NULL;
7255 }
7256
7257 /* Find out if we're dealing with a hash dereference. */
7258 if ((f = strstr(p+1, "->")) != NULL) {
7259 fieldlen = sdslen(spat)-(f-spat);
7260 /* this also copies \0 character */
7261 memcpy(fieldname.buf,f+2,fieldlen-1);
7262 fieldname.len = fieldlen-2;
7263 } else {
7264 fieldlen = 0;
7265 }
7266
7267 prefixlen = p-spat;
7268 sublen = sdslen(ssub);
7269 postfixlen = sdslen(spat)-(prefixlen+1)-fieldlen;
7270 memcpy(keyname.buf,spat,prefixlen);
7271 memcpy(keyname.buf+prefixlen,ssub,sublen);
7272 memcpy(keyname.buf+prefixlen+sublen,p+1,postfixlen);
7273 keyname.buf[prefixlen+sublen+postfixlen] = '\0';
7274 keyname.len = prefixlen+sublen+postfixlen;
7275 decrRefCount(subst);
7276
7277 /* Lookup substituted key */
7278 initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2));
7279 o = lookupKeyRead(db,&keyobj);
7280 if (o == NULL) return NULL;
7281
7282 if (fieldlen > 0) {
7283 if (o->type != REDIS_HASH || fieldname.len < 1) return NULL;
7284
7285 /* Retrieve value from hash by the field name. This operation
7286 * already increases the refcount of the returned object. */
7287 initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(long)*2));
7288 o = hashTypeGet(o, &fieldobj);
7289 } else {
7290 if (o->type != REDIS_STRING) return NULL;
7291
7292 /* Every object that this function returns needs to have its refcount
7293 * increased. sortCommand decreases it again. */
7294 incrRefCount(o);
7295 }
7296
7297 return o;
7298 }
7299
7300 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
7301 * the additional parameter is not standard but a BSD-specific we have to
7302 * pass sorting parameters via the global 'server' structure */
7303 static int sortCompare(const void *s1, const void *s2) {
7304 const redisSortObject *so1 = s1, *so2 = s2;
7305 int cmp;
7306
7307 if (!server.sort_alpha) {
7308 /* Numeric sorting. Here it's trivial as we precomputed scores */
7309 if (so1->u.score > so2->u.score) {
7310 cmp = 1;
7311 } else if (so1->u.score < so2->u.score) {
7312 cmp = -1;
7313 } else {
7314 cmp = 0;
7315 }
7316 } else {
7317 /* Alphanumeric sorting */
7318 if (server.sort_bypattern) {
7319 if (!so1->u.cmpobj || !so2->u.cmpobj) {
7320 /* At least one compare object is NULL */
7321 if (so1->u.cmpobj == so2->u.cmpobj)
7322 cmp = 0;
7323 else if (so1->u.cmpobj == NULL)
7324 cmp = -1;
7325 else
7326 cmp = 1;
7327 } else {
7328 /* We have both the objects, use strcoll */
7329 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
7330 }
7331 } else {
7332 /* Compare elements directly. */
7333 cmp = compareStringObjects(so1->obj,so2->obj);
7334 }
7335 }
7336 return server.sort_desc ? -cmp : cmp;
7337 }
7338
7339 /* The SORT command is the most complex command in Redis. Warning: this code
7340 * is optimized for speed and a bit less for readability */
7341 static void sortCommand(redisClient *c) {
7342 list *operations;
7343 unsigned int outputlen = 0;
7344 int desc = 0, alpha = 0;
7345 int limit_start = 0, limit_count = -1, start, end;
7346 int j, dontsort = 0, vectorlen;
7347 int getop = 0; /* GET operation counter */
7348 robj *sortval, *sortby = NULL, *storekey = NULL;
7349 redisSortObject *vector; /* Resulting vector to sort */
7350
7351 /* Lookup the key to sort. It must be of the right types */
7352 sortval = lookupKeyRead(c->db,c->argv[1]);
7353 if (sortval == NULL) {
7354 addReply(c,shared.emptymultibulk);
7355 return;
7356 }
7357 if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
7358 sortval->type != REDIS_ZSET)
7359 {
7360 addReply(c,shared.wrongtypeerr);
7361 return;
7362 }
7363
7364 /* Create a list of operations to perform for every sorted element.
7365 * Operations can be GET/DEL/INCR/DECR */
7366 operations = listCreate();
7367 listSetFreeMethod(operations,zfree);
7368 j = 2;
7369
7370 /* Now we need to protect sortval incrementing its count, in the future
7371 * SORT may have options able to overwrite/delete keys during the sorting
7372 * and the sorted key itself may get destroied */
7373 incrRefCount(sortval);
7374
7375 /* The SORT command has an SQL-alike syntax, parse it */
7376 while(j < c->argc) {
7377 int leftargs = c->argc-j-1;
7378 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
7379 desc = 0;
7380 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
7381 desc = 1;
7382 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
7383 alpha = 1;
7384 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
7385 limit_start = atoi(c->argv[j+1]->ptr);
7386 limit_count = atoi(c->argv[j+2]->ptr);
7387 j+=2;
7388 } else if (!strcasecmp(c->argv[j]->ptr,"store") && leftargs >= 1) {
7389 storekey = c->argv[j+1];
7390 j++;
7391 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
7392 sortby = c->argv[j+1];
7393 /* If the BY pattern does not contain '*', i.e. it is constant,
7394 * we don't need to sort nor to lookup the weight keys. */
7395 if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
7396 j++;
7397 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
7398 listAddNodeTail(operations,createSortOperation(
7399 REDIS_SORT_GET,c->argv[j+1]));
7400 getop++;
7401 j++;
7402 } else {
7403 decrRefCount(sortval);
7404 listRelease(operations);
7405 addReply(c,shared.syntaxerr);
7406 return;
7407 }
7408 j++;
7409 }
7410
7411 /* Load the sorting vector with all the objects to sort */
7412 switch(sortval->type) {
7413 case REDIS_LIST: vectorlen = listTypeLength(sortval); break;
7414 case REDIS_SET: vectorlen = dictSize((dict*)sortval->ptr); break;
7415 case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
7416 default: vectorlen = 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
7417 }
7418 vector = zmalloc(sizeof(redisSortObject)*vectorlen);
7419 j = 0;
7420
7421 if (sortval->type == REDIS_LIST) {
7422 listTypeIterator *li = listTypeInitIterator(sortval,0,REDIS_TAIL);
7423 listTypeEntry entry;
7424 while(listTypeNext(li,&entry)) {
7425 vector[j].obj = listTypeGet(&entry);
7426 vector[j].u.score = 0;
7427 vector[j].u.cmpobj = NULL;
7428 j++;
7429 }
7430 listTypeReleaseIterator(li);
7431 } else {
7432 dict *set;
7433 dictIterator *di;
7434 dictEntry *setele;
7435
7436 if (sortval->type == REDIS_SET) {
7437 set = sortval->ptr;
7438 } else {
7439 zset *zs = sortval->ptr;
7440 set = zs->dict;
7441 }
7442
7443 di = dictGetIterator(set);
7444 while((setele = dictNext(di)) != NULL) {
7445 vector[j].obj = dictGetEntryKey(setele);
7446 vector[j].u.score = 0;
7447 vector[j].u.cmpobj = NULL;
7448 j++;
7449 }
7450 dictReleaseIterator(di);
7451 }
7452 redisAssert(j == vectorlen);
7453
7454 /* Now it's time to load the right scores in the sorting vector */
7455 if (dontsort == 0) {
7456 for (j = 0; j < vectorlen; j++) {
7457 robj *byval;
7458 if (sortby) {
7459 /* lookup value to sort by */
7460 byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
7461 if (!byval) continue;
7462 } else {
7463 /* use object itself to sort by */
7464 byval = vector[j].obj;
7465 }
7466
7467 if (alpha) {
7468 if (sortby) vector[j].u.cmpobj = getDecodedObject(byval);
7469 } else {
7470 if (byval->encoding == REDIS_ENCODING_RAW) {
7471 vector[j].u.score = strtod(byval->ptr,NULL);
7472 } else if (byval->encoding == REDIS_ENCODING_INT) {
7473 /* Don't need to decode the object if it's
7474 * integer-encoded (the only encoding supported) so
7475 * far. We can just cast it */
7476 vector[j].u.score = (long)byval->ptr;
7477 } else {
7478 redisAssert(1 != 1);
7479 }
7480 }
7481
7482 /* when the object was retrieved using lookupKeyByPattern,
7483 * its refcount needs to be decreased. */
7484 if (sortby) {
7485 decrRefCount(byval);
7486 }
7487 }
7488 }
7489
7490 /* We are ready to sort the vector... perform a bit of sanity check
7491 * on the LIMIT option too. We'll use a partial version of quicksort. */
7492 start = (limit_start < 0) ? 0 : limit_start;
7493 end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
7494 if (start >= vectorlen) {
7495 start = vectorlen-1;
7496 end = vectorlen-2;
7497 }
7498 if (end >= vectorlen) end = vectorlen-1;
7499
7500 if (dontsort == 0) {
7501 server.sort_desc = desc;
7502 server.sort_alpha = alpha;
7503 server.sort_bypattern = sortby ? 1 : 0;
7504 if (sortby && (start != 0 || end != vectorlen-1))
7505 pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
7506 else
7507 qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
7508 }
7509
7510 /* Send command output to the output buffer, performing the specified
7511 * GET/DEL/INCR/DECR operations if any. */
7512 outputlen = getop ? getop*(end-start+1) : end-start+1;
7513 if (storekey == NULL) {
7514 /* STORE option not specified, sent the sorting result to client */
7515 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen));
7516 for (j = start; j <= end; j++) {
7517 listNode *ln;
7518 listIter li;
7519
7520 if (!getop) addReplyBulk(c,vector[j].obj);
7521 listRewind(operations,&li);
7522 while((ln = listNext(&li))) {
7523 redisSortOperation *sop = ln->value;
7524 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7525 vector[j].obj);
7526
7527 if (sop->type == REDIS_SORT_GET) {
7528 if (!val) {
7529 addReply(c,shared.nullbulk);
7530 } else {
7531 addReplyBulk(c,val);
7532 decrRefCount(val);
7533 }
7534 } else {
7535 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
7536 }
7537 }
7538 }
7539 } else {
7540 robj *sobj = createZiplistObject();
7541
7542 /* STORE option specified, set the sorting result as a List object */
7543 for (j = start; j <= end; j++) {
7544 listNode *ln;
7545 listIter li;
7546
7547 if (!getop) {
7548 listTypePush(sobj,vector[j].obj,REDIS_TAIL);
7549 } else {
7550 listRewind(operations,&li);
7551 while((ln = listNext(&li))) {
7552 redisSortOperation *sop = ln->value;
7553 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7554 vector[j].obj);
7555
7556 if (sop->type == REDIS_SORT_GET) {
7557 if (!val) val = createStringObject("",0);
7558
7559 /* listTypePush does an incrRefCount, so we should take care
7560 * care of the incremented refcount caused by either
7561 * lookupKeyByPattern or createStringObject("",0) */
7562 listTypePush(sobj,val,REDIS_TAIL);
7563 decrRefCount(val);
7564 } else {
7565 /* always fails */
7566 redisAssert(sop->type == REDIS_SORT_GET);
7567 }
7568 }
7569 }
7570 }
7571 dbReplace(c->db,storekey,sobj);
7572 /* Note: we add 1 because the DB is dirty anyway since even if the
7573 * SORT result is empty a new key is set and maybe the old content
7574 * replaced. */
7575 server.dirty += 1+outputlen;
7576 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",outputlen));
7577 }
7578
7579 /* Cleanup */
7580 if (sortval->type == REDIS_LIST)
7581 for (j = 0; j < vectorlen; j++)
7582 decrRefCount(vector[j].obj);
7583 decrRefCount(sortval);
7584 listRelease(operations);
7585 for (j = 0; j < vectorlen; j++) {
7586 if (alpha && vector[j].u.cmpobj)
7587 decrRefCount(vector[j].u.cmpobj);
7588 }
7589 zfree(vector);
7590 }
7591
7592 /* Convert an amount of bytes into a human readable string in the form
7593 * of 100B, 2G, 100M, 4K, and so forth. */
7594 static void bytesToHuman(char *s, unsigned long long n) {
7595 double d;
7596
7597 if (n < 1024) {
7598 /* Bytes */
7599 sprintf(s,"%lluB",n);
7600 return;
7601 } else if (n < (1024*1024)) {
7602 d = (double)n/(1024);
7603 sprintf(s,"%.2fK",d);
7604 } else if (n < (1024LL*1024*1024)) {
7605 d = (double)n/(1024*1024);
7606 sprintf(s,"%.2fM",d);
7607 } else if (n < (1024LL*1024*1024*1024)) {
7608 d = (double)n/(1024LL*1024*1024);
7609 sprintf(s,"%.2fG",d);
7610 }
7611 }
7612
7613 /* Create the string returned by the INFO command. This is decoupled
7614 * by the INFO command itself as we need to report the same information
7615 * on memory corruption problems. */
7616 static sds genRedisInfoString(void) {
7617 sds info;
7618 time_t uptime = time(NULL)-server.stat_starttime;
7619 int j;
7620 char hmem[64];
7621
7622 bytesToHuman(hmem,zmalloc_used_memory());
7623 info = sdscatprintf(sdsempty(),
7624 "redis_version:%s\r\n"
7625 "redis_git_sha1:%s\r\n"
7626 "redis_git_dirty:%d\r\n"
7627 "arch_bits:%s\r\n"
7628 "multiplexing_api:%s\r\n"
7629 "process_id:%ld\r\n"
7630 "uptime_in_seconds:%ld\r\n"
7631 "uptime_in_days:%ld\r\n"
7632 "connected_clients:%d\r\n"
7633 "connected_slaves:%d\r\n"
7634 "blocked_clients:%d\r\n"
7635 "used_memory:%zu\r\n"
7636 "used_memory_human:%s\r\n"
7637 "changes_since_last_save:%lld\r\n"
7638 "bgsave_in_progress:%d\r\n"
7639 "last_save_time:%ld\r\n"
7640 "bgrewriteaof_in_progress:%d\r\n"
7641 "total_connections_received:%lld\r\n"
7642 "total_commands_processed:%lld\r\n"
7643 "expired_keys:%lld\r\n"
7644 "hash_max_zipmap_entries:%zu\r\n"
7645 "hash_max_zipmap_value:%zu\r\n"
7646 "pubsub_channels:%ld\r\n"
7647 "pubsub_patterns:%u\r\n"
7648 "vm_enabled:%d\r\n"
7649 "role:%s\r\n"
7650 ,REDIS_VERSION,
7651 REDIS_GIT_SHA1,
7652 strtol(REDIS_GIT_DIRTY,NULL,10) > 0,
7653 (sizeof(long) == 8) ? "64" : "32",
7654 aeGetApiName(),
7655 (long) getpid(),
7656 uptime,
7657 uptime/(3600*24),
7658 listLength(server.clients)-listLength(server.slaves),
7659 listLength(server.slaves),
7660 server.blpop_blocked_clients,
7661 zmalloc_used_memory(),
7662 hmem,
7663 server.dirty,
7664 server.bgsavechildpid != -1,
7665 server.lastsave,
7666 server.bgrewritechildpid != -1,
7667 server.stat_numconnections,
7668 server.stat_numcommands,
7669 server.stat_expiredkeys,
7670 server.hash_max_zipmap_entries,
7671 server.hash_max_zipmap_value,
7672 dictSize(server.pubsub_channels),
7673 listLength(server.pubsub_patterns),
7674 server.vm_enabled != 0,
7675 server.masterhost == NULL ? "master" : "slave"
7676 );
7677 if (server.masterhost) {
7678 info = sdscatprintf(info,
7679 "master_host:%s\r\n"
7680 "master_port:%d\r\n"
7681 "master_link_status:%s\r\n"
7682 "master_last_io_seconds_ago:%d\r\n"
7683 ,server.masterhost,
7684 server.masterport,
7685 (server.replstate == REDIS_REPL_CONNECTED) ?
7686 "up" : "down",
7687 server.master ? ((int)(time(NULL)-server.master->lastinteraction)) : -1
7688 );
7689 }
7690 if (server.vm_enabled) {
7691 lockThreadedIO();
7692 info = sdscatprintf(info,
7693 "vm_conf_max_memory:%llu\r\n"
7694 "vm_conf_page_size:%llu\r\n"
7695 "vm_conf_pages:%llu\r\n"
7696 "vm_stats_used_pages:%llu\r\n"
7697 "vm_stats_swapped_objects:%llu\r\n"
7698 "vm_stats_swappin_count:%llu\r\n"
7699 "vm_stats_swappout_count:%llu\r\n"
7700 "vm_stats_io_newjobs_len:%lu\r\n"
7701 "vm_stats_io_processing_len:%lu\r\n"
7702 "vm_stats_io_processed_len:%lu\r\n"
7703 "vm_stats_io_active_threads:%lu\r\n"
7704 "vm_stats_blocked_clients:%lu\r\n"
7705 ,(unsigned long long) server.vm_max_memory,
7706 (unsigned long long) server.vm_page_size,
7707 (unsigned long long) server.vm_pages,
7708 (unsigned long long) server.vm_stats_used_pages,
7709 (unsigned long long) server.vm_stats_swapped_objects,
7710 (unsigned long long) server.vm_stats_swapins,
7711 (unsigned long long) server.vm_stats_swapouts,
7712 (unsigned long) listLength(server.io_newjobs),
7713 (unsigned long) listLength(server.io_processing),
7714 (unsigned long) listLength(server.io_processed),
7715 (unsigned long) server.io_active_threads,
7716 (unsigned long) server.vm_blocked_clients
7717 );
7718 unlockThreadedIO();
7719 }
7720 for (j = 0; j < server.dbnum; j++) {
7721 long long keys, vkeys;
7722
7723 keys = dictSize(server.db[j].dict);
7724 vkeys = dictSize(server.db[j].expires);
7725 if (keys || vkeys) {
7726 info = sdscatprintf(info, "db%d:keys=%lld,expires=%lld\r\n",
7727 j, keys, vkeys);
7728 }
7729 }
7730 return info;
7731 }
7732
7733 static void infoCommand(redisClient *c) {
7734 sds info = genRedisInfoString();
7735 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
7736 (unsigned long)sdslen(info)));
7737 addReplySds(c,info);
7738 addReply(c,shared.crlf);
7739 }
7740
7741 static void monitorCommand(redisClient *c) {
7742 /* ignore MONITOR if aleady slave or in monitor mode */
7743 if (c->flags & REDIS_SLAVE) return;
7744
7745 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
7746 c->slaveseldb = 0;
7747 listAddNodeTail(server.monitors,c);
7748 addReply(c,shared.ok);
7749 }
7750
7751 /* ================================= Expire ================================= */
7752 static int removeExpire(redisDb *db, robj *key) {
7753 if (dictDelete(db->expires,key->ptr) == DICT_OK) {
7754 return 1;
7755 } else {
7756 return 0;
7757 }
7758 }
7759
7760 static int setExpire(redisDb *db, robj *key, time_t when) {
7761 sds copy = sdsdup(key->ptr);
7762 if (dictAdd(db->expires,copy,(void*)when) == DICT_ERR) {
7763 sdsfree(copy);
7764 return 0;
7765 } else {
7766 return 1;
7767 }
7768 }
7769
7770 /* Return the expire time of the specified key, or -1 if no expire
7771 * is associated with this key (i.e. the key is non volatile) */
7772 static time_t getExpire(redisDb *db, robj *key) {
7773 dictEntry *de;
7774
7775 /* No expire? return ASAP */
7776 if (dictSize(db->expires) == 0 ||
7777 (de = dictFind(db->expires,key->ptr)) == NULL) return -1;
7778
7779 return (time_t) dictGetEntryVal(de);
7780 }
7781
7782 static int expireIfNeeded(redisDb *db, robj *key) {
7783 time_t when;
7784 dictEntry *de;
7785
7786 /* No expire? return ASAP */
7787 if (dictSize(db->expires) == 0 ||
7788 (de = dictFind(db->expires,key->ptr)) == NULL) return 0;
7789
7790 /* Lookup the expire */
7791 when = (time_t) dictGetEntryVal(de);
7792 if (time(NULL) <= when) return 0;
7793
7794 /* Delete the key */
7795 dbDelete(db,key);
7796 server.stat_expiredkeys++;
7797 return 1;
7798 }
7799
7800 static int deleteIfVolatile(redisDb *db, robj *key) {
7801 dictEntry *de;
7802
7803 /* No expire? return ASAP */
7804 if (dictSize(db->expires) == 0 ||
7805 (de = dictFind(db->expires,key->ptr)) == NULL) return 0;
7806
7807 /* Delete the key */
7808 server.dirty++;
7809 server.stat_expiredkeys++;
7810 dictDelete(db->expires,key->ptr);
7811 return dictDelete(db->dict,key->ptr) == DICT_OK;
7812 }
7813
7814 static void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
7815 dictEntry *de;
7816 time_t seconds;
7817
7818 if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
7819
7820 seconds -= offset;
7821
7822 de = dictFind(c->db->dict,key->ptr);
7823 if (de == NULL) {
7824 addReply(c,shared.czero);
7825 return;
7826 }
7827 if (seconds <= 0) {
7828 if (dbDelete(c->db,key)) server.dirty++;
7829 addReply(c, shared.cone);
7830 return;
7831 } else {
7832 time_t when = time(NULL)+seconds;
7833 if (setExpire(c->db,key,when)) {
7834 addReply(c,shared.cone);
7835 server.dirty++;
7836 } else {
7837 addReply(c,shared.czero);
7838 }
7839 return;
7840 }
7841 }
7842
7843 static void expireCommand(redisClient *c) {
7844 expireGenericCommand(c,c->argv[1],c->argv[2],0);
7845 }
7846
7847 static void expireatCommand(redisClient *c) {
7848 expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
7849 }
7850
7851 static void ttlCommand(redisClient *c) {
7852 time_t expire;
7853 int ttl = -1;
7854
7855 expire = getExpire(c->db,c->argv[1]);
7856 if (expire != -1) {
7857 ttl = (int) (expire-time(NULL));
7858 if (ttl < 0) ttl = -1;
7859 }
7860 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",ttl));
7861 }
7862
7863 /* ================================ MULTI/EXEC ============================== */
7864
7865 /* Client state initialization for MULTI/EXEC */
7866 static void initClientMultiState(redisClient *c) {
7867 c->mstate.commands = NULL;
7868 c->mstate.count = 0;
7869 }
7870
7871 /* Release all the resources associated with MULTI/EXEC state */
7872 static void freeClientMultiState(redisClient *c) {
7873 int j;
7874
7875 for (j = 0; j < c->mstate.count; j++) {
7876 int i;
7877 multiCmd *mc = c->mstate.commands+j;
7878
7879 for (i = 0; i < mc->argc; i++)
7880 decrRefCount(mc->argv[i]);
7881 zfree(mc->argv);
7882 }
7883 zfree(c->mstate.commands);
7884 }
7885
7886 /* Add a new command into the MULTI commands queue */
7887 static void queueMultiCommand(redisClient *c, struct redisCommand *cmd) {
7888 multiCmd *mc;
7889 int j;
7890
7891 c->mstate.commands = zrealloc(c->mstate.commands,
7892 sizeof(multiCmd)*(c->mstate.count+1));
7893 mc = c->mstate.commands+c->mstate.count;
7894 mc->cmd = cmd;
7895 mc->argc = c->argc;
7896 mc->argv = zmalloc(sizeof(robj*)*c->argc);
7897 memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
7898 for (j = 0; j < c->argc; j++)
7899 incrRefCount(mc->argv[j]);
7900 c->mstate.count++;
7901 }
7902
7903 static void multiCommand(redisClient *c) {
7904 if (c->flags & REDIS_MULTI) {
7905 addReplySds(c,sdsnew("-ERR MULTI calls can not be nested\r\n"));
7906 return;
7907 }
7908 c->flags |= REDIS_MULTI;
7909 addReply(c,shared.ok);
7910 }
7911
7912 static void discardCommand(redisClient *c) {
7913 if (!(c->flags & REDIS_MULTI)) {
7914 addReplySds(c,sdsnew("-ERR DISCARD without MULTI\r\n"));
7915 return;
7916 }
7917
7918 freeClientMultiState(c);
7919 initClientMultiState(c);
7920 c->flags &= (~REDIS_MULTI);
7921 unwatchAllKeys(c);
7922 addReply(c,shared.ok);
7923 }
7924
7925 /* Send a MULTI command to all the slaves and AOF file. Check the execCommand
7926 * implememntation for more information. */
7927 static void execCommandReplicateMulti(redisClient *c) {
7928 struct redisCommand *cmd;
7929 robj *multistring = createStringObject("MULTI",5);
7930
7931 cmd = lookupCommand("multi");
7932 if (server.appendonly)
7933 feedAppendOnlyFile(cmd,c->db->id,&multistring,1);
7934 if (listLength(server.slaves))
7935 replicationFeedSlaves(server.slaves,c->db->id,&multistring,1);
7936 decrRefCount(multistring);
7937 }
7938
7939 static void execCommand(redisClient *c) {
7940 int j;
7941 robj **orig_argv;
7942 int orig_argc;
7943
7944 if (!(c->flags & REDIS_MULTI)) {
7945 addReplySds(c,sdsnew("-ERR EXEC without MULTI\r\n"));
7946 return;
7947 }
7948
7949 /* Check if we need to abort the EXEC if some WATCHed key was touched.
7950 * A failed EXEC will return a multi bulk nil object. */
7951 if (c->flags & REDIS_DIRTY_CAS) {
7952 freeClientMultiState(c);
7953 initClientMultiState(c);
7954 c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS);
7955 unwatchAllKeys(c);
7956 addReply(c,shared.nullmultibulk);
7957 return;
7958 }
7959
7960 /* Replicate a MULTI request now that we are sure the block is executed.
7961 * This way we'll deliver the MULTI/..../EXEC block as a whole and
7962 * both the AOF and the replication link will have the same consistency
7963 * and atomicity guarantees. */
7964 execCommandReplicateMulti(c);
7965
7966 /* Exec all the queued commands */
7967 unwatchAllKeys(c); /* Unwatch ASAP otherwise we'll waste CPU cycles */
7968 orig_argv = c->argv;
7969 orig_argc = c->argc;
7970 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->mstate.count));
7971 for (j = 0; j < c->mstate.count; j++) {
7972 c->argc = c->mstate.commands[j].argc;
7973 c->argv = c->mstate.commands[j].argv;
7974 call(c,c->mstate.commands[j].cmd);
7975 }
7976 c->argv = orig_argv;
7977 c->argc = orig_argc;
7978 freeClientMultiState(c);
7979 initClientMultiState(c);
7980 c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS);
7981 /* Make sure the EXEC command is always replicated / AOF, since we
7982 * always send the MULTI command (we can't know beforehand if the
7983 * next operations will contain at least a modification to the DB). */
7984 server.dirty++;
7985 }
7986
7987 /* =========================== Blocking Operations ========================= */
7988
7989 /* Currently Redis blocking operations support is limited to list POP ops,
7990 * so the current implementation is not fully generic, but it is also not
7991 * completely specific so it will not require a rewrite to support new
7992 * kind of blocking operations in the future.
7993 *
7994 * Still it's important to note that list blocking operations can be already
7995 * used as a notification mechanism in order to implement other blocking
7996 * operations at application level, so there must be a very strong evidence
7997 * of usefulness and generality before new blocking operations are implemented.
7998 *
7999 * This is how the current blocking POP works, we use BLPOP as example:
8000 * - If the user calls BLPOP and the key exists and contains a non empty list
8001 * then LPOP is called instead. So BLPOP is semantically the same as LPOP
8002 * if there is not to block.
8003 * - If instead BLPOP is called and the key does not exists or the list is
8004 * empty we need to block. In order to do so we remove the notification for
8005 * new data to read in the client socket (so that we'll not serve new
8006 * requests if the blocking request is not served). Also we put the client
8007 * in a dictionary (db->blocking_keys) mapping keys to a list of clients
8008 * blocking for this keys.
8009 * - If a PUSH operation against a key with blocked clients waiting is
8010 * performed, we serve the first in the list: basically instead to push
8011 * the new element inside the list we return it to the (first / oldest)
8012 * blocking client, unblock the client, and remove it form the list.
8013 *
8014 * The above comment and the source code should be enough in order to understand
8015 * the implementation and modify / fix it later.
8016 */
8017
8018 /* Set a client in blocking mode for the specified key, with the specified
8019 * timeout */
8020 static void blockForKeys(redisClient *c, robj **keys, int numkeys, time_t timeout) {
8021 dictEntry *de;
8022 list *l;
8023 int j;
8024
8025 c->blocking_keys = zmalloc(sizeof(robj*)*numkeys);
8026 c->blocking_keys_num = numkeys;
8027 c->blockingto = timeout;
8028 for (j = 0; j < numkeys; j++) {
8029 /* Add the key in the client structure, to map clients -> keys */
8030 c->blocking_keys[j] = keys[j];
8031 incrRefCount(keys[j]);
8032
8033 /* And in the other "side", to map keys -> clients */
8034 de = dictFind(c->db->blocking_keys,keys[j]);
8035 if (de == NULL) {
8036 int retval;
8037
8038 /* For every key we take a list of clients blocked for it */
8039 l = listCreate();
8040 retval = dictAdd(c->db->blocking_keys,keys[j],l);
8041 incrRefCount(keys[j]);
8042 assert(retval == DICT_OK);
8043 } else {
8044 l = dictGetEntryVal(de);
8045 }
8046 listAddNodeTail(l,c);
8047 }
8048 /* Mark the client as a blocked client */
8049 c->flags |= REDIS_BLOCKED;
8050 server.blpop_blocked_clients++;
8051 }
8052
8053 /* Unblock a client that's waiting in a blocking operation such as BLPOP */
8054 static void unblockClientWaitingData(redisClient *c) {
8055 dictEntry *de;
8056 list *l;
8057 int j;
8058
8059 assert(c->blocking_keys != NULL);
8060 /* The client may wait for multiple keys, so unblock it for every key. */
8061 for (j = 0; j < c->blocking_keys_num; j++) {
8062 /* Remove this client from the list of clients waiting for this key. */
8063 de = dictFind(c->db->blocking_keys,c->blocking_keys[j]);
8064 assert(de != NULL);
8065 l = dictGetEntryVal(de);
8066 listDelNode(l,listSearchKey(l,c));
8067 /* If the list is empty we need to remove it to avoid wasting memory */
8068 if (listLength(l) == 0)
8069 dictDelete(c->db->blocking_keys,c->blocking_keys[j]);
8070 decrRefCount(c->blocking_keys[j]);
8071 }
8072 /* Cleanup the client structure */
8073 zfree(c->blocking_keys);
8074 c->blocking_keys = NULL;
8075 c->flags &= (~REDIS_BLOCKED);
8076 server.blpop_blocked_clients--;
8077 /* We want to process data if there is some command waiting
8078 * in the input buffer. Note that this is safe even if
8079 * unblockClientWaitingData() gets called from freeClient() because
8080 * freeClient() will be smart enough to call this function
8081 * *after* c->querybuf was set to NULL. */
8082 if (c->querybuf && sdslen(c->querybuf) > 0) processInputBuffer(c);
8083 }
8084
8085 /* This should be called from any function PUSHing into lists.
8086 * 'c' is the "pushing client", 'key' is the key it is pushing data against,
8087 * 'ele' is the element pushed.
8088 *
8089 * If the function returns 0 there was no client waiting for a list push
8090 * against this key.
8091 *
8092 * If the function returns 1 there was a client waiting for a list push
8093 * against this key, the element was passed to this client thus it's not
8094 * needed to actually add it to the list and the caller should return asap. */
8095 static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele) {
8096 struct dictEntry *de;
8097 redisClient *receiver;
8098 list *l;
8099 listNode *ln;
8100
8101 de = dictFind(c->db->blocking_keys,key);
8102 if (de == NULL) return 0;
8103 l = dictGetEntryVal(de);
8104 ln = listFirst(l);
8105 assert(ln != NULL);
8106 receiver = ln->value;
8107
8108 addReplySds(receiver,sdsnew("*2\r\n"));
8109 addReplyBulk(receiver,key);
8110 addReplyBulk(receiver,ele);
8111 unblockClientWaitingData(receiver);
8112 return 1;
8113 }
8114
8115 /* Blocking RPOP/LPOP */
8116 static void blockingPopGenericCommand(redisClient *c, int where) {
8117 robj *o;
8118 time_t timeout;
8119 int j;
8120
8121 for (j = 1; j < c->argc-1; j++) {
8122 o = lookupKeyWrite(c->db,c->argv[j]);
8123 if (o != NULL) {
8124 if (o->type != REDIS_LIST) {
8125 addReply(c,shared.wrongtypeerr);
8126 return;
8127 } else {
8128 list *list = o->ptr;
8129 if (listLength(list) != 0) {
8130 /* If the list contains elements fall back to the usual
8131 * non-blocking POP operation */
8132 robj *argv[2], **orig_argv;
8133 int orig_argc;
8134
8135 /* We need to alter the command arguments before to call
8136 * popGenericCommand() as the command takes a single key. */
8137 orig_argv = c->argv;
8138 orig_argc = c->argc;
8139 argv[1] = c->argv[j];
8140 c->argv = argv;
8141 c->argc = 2;
8142
8143 /* Also the return value is different, we need to output
8144 * the multi bulk reply header and the key name. The
8145 * "real" command will add the last element (the value)
8146 * for us. If this souds like an hack to you it's just
8147 * because it is... */
8148 addReplySds(c,sdsnew("*2\r\n"));
8149 addReplyBulk(c,argv[1]);
8150 popGenericCommand(c,where);
8151
8152 /* Fix the client structure with the original stuff */
8153 c->argv = orig_argv;
8154 c->argc = orig_argc;
8155 return;
8156 }
8157 }
8158 }
8159 }
8160 /* If the list is empty or the key does not exists we must block */
8161 timeout = strtol(c->argv[c->argc-1]->ptr,NULL,10);
8162 if (timeout > 0) timeout += time(NULL);
8163 blockForKeys(c,c->argv+1,c->argc-2,timeout);
8164 }
8165
8166 static void blpopCommand(redisClient *c) {
8167 blockingPopGenericCommand(c,REDIS_HEAD);
8168 }
8169
8170 static void brpopCommand(redisClient *c) {
8171 blockingPopGenericCommand(c,REDIS_TAIL);
8172 }
8173
8174 /* =============================== Replication ============================= */
8175
8176 static int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
8177 ssize_t nwritten, ret = size;
8178 time_t start = time(NULL);
8179
8180 timeout++;
8181 while(size) {
8182 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
8183 nwritten = write(fd,ptr,size);
8184 if (nwritten == -1) return -1;
8185 ptr += nwritten;
8186 size -= nwritten;
8187 }
8188 if ((time(NULL)-start) > timeout) {
8189 errno = ETIMEDOUT;
8190 return -1;
8191 }
8192 }
8193 return ret;
8194 }
8195
8196 static int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
8197 ssize_t nread, totread = 0;
8198 time_t start = time(NULL);
8199
8200 timeout++;
8201 while(size) {
8202 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
8203 nread = read(fd,ptr,size);
8204 if (nread == -1) return -1;
8205 ptr += nread;
8206 size -= nread;
8207 totread += nread;
8208 }
8209 if ((time(NULL)-start) > timeout) {
8210 errno = ETIMEDOUT;
8211 return -1;
8212 }
8213 }
8214 return totread;
8215 }
8216
8217 static int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
8218 ssize_t nread = 0;
8219
8220 size--;
8221 while(size) {
8222 char c;
8223
8224 if (syncRead(fd,&c,1,timeout) == -1) return -1;
8225 if (c == '\n') {
8226 *ptr = '\0';
8227 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
8228 return nread;
8229 } else {
8230 *ptr++ = c;
8231 *ptr = '\0';
8232 nread++;
8233 }
8234 }
8235 return nread;
8236 }
8237
8238 static void syncCommand(redisClient *c) {
8239 /* ignore SYNC if aleady slave or in monitor mode */
8240 if (c->flags & REDIS_SLAVE) return;
8241
8242 /* SYNC can't be issued when the server has pending data to send to
8243 * the client about already issued commands. We need a fresh reply
8244 * buffer registering the differences between the BGSAVE and the current
8245 * dataset, so that we can copy to other slaves if needed. */
8246 if (listLength(c->reply) != 0) {
8247 addReplySds(c,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
8248 return;
8249 }
8250
8251 redisLog(REDIS_NOTICE,"Slave ask for synchronization");
8252 /* Here we need to check if there is a background saving operation
8253 * in progress, or if it is required to start one */
8254 if (server.bgsavechildpid != -1) {
8255 /* Ok a background save is in progress. Let's check if it is a good
8256 * one for replication, i.e. if there is another slave that is
8257 * registering differences since the server forked to save */
8258 redisClient *slave;
8259 listNode *ln;
8260 listIter li;
8261
8262 listRewind(server.slaves,&li);
8263 while((ln = listNext(&li))) {
8264 slave = ln->value;
8265 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;
8266 }
8267 if (ln) {
8268 /* Perfect, the server is already registering differences for
8269 * another slave. Set the right state, and copy the buffer. */
8270 listRelease(c->reply);
8271 c->reply = listDup(slave->reply);
8272 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
8273 redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
8274 } else {
8275 /* No way, we need to wait for the next BGSAVE in order to
8276 * register differences */
8277 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
8278 redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");
8279 }
8280 } else {
8281 /* Ok we don't have a BGSAVE in progress, let's start one */
8282 redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC");
8283 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
8284 redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");
8285 addReplySds(c,sdsnew("-ERR Unalbe to perform background save\r\n"));
8286 return;
8287 }
8288 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
8289 }
8290 c->repldbfd = -1;
8291 c->flags |= REDIS_SLAVE;
8292 c->slaveseldb = 0;
8293 listAddNodeTail(server.slaves,c);
8294 return;
8295 }
8296
8297 static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
8298 redisClient *slave = privdata;
8299 REDIS_NOTUSED(el);
8300 REDIS_NOTUSED(mask);
8301 char buf[REDIS_IOBUF_LEN];
8302 ssize_t nwritten, buflen;
8303
8304 if (slave->repldboff == 0) {
8305 /* Write the bulk write count before to transfer the DB. In theory here
8306 * we don't know how much room there is in the output buffer of the
8307 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
8308 * operations) will never be smaller than the few bytes we need. */
8309 sds bulkcount;
8310
8311 bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
8312 slave->repldbsize);
8313 if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount))
8314 {
8315 sdsfree(bulkcount);
8316 freeClient(slave);
8317 return;
8318 }
8319 sdsfree(bulkcount);
8320 }
8321 lseek(slave->repldbfd,slave->repldboff,SEEK_SET);
8322 buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);
8323 if (buflen <= 0) {
8324 redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",
8325 (buflen == 0) ? "premature EOF" : strerror(errno));
8326 freeClient(slave);
8327 return;
8328 }
8329 if ((nwritten = write(fd,buf,buflen)) == -1) {
8330 redisLog(REDIS_VERBOSE,"Write error sending DB to slave: %s",
8331 strerror(errno));
8332 freeClient(slave);
8333 return;
8334 }
8335 slave->repldboff += nwritten;
8336 if (slave->repldboff == slave->repldbsize) {
8337 close(slave->repldbfd);
8338 slave->repldbfd = -1;
8339 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
8340 slave->replstate = REDIS_REPL_ONLINE;
8341 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
8342 sendReplyToClient, slave) == AE_ERR) {
8343 freeClient(slave);
8344 return;
8345 }
8346 addReplySds(slave,sdsempty());
8347 redisLog(REDIS_NOTICE,"Synchronization with slave succeeded");
8348 }
8349 }
8350
8351 /* This function is called at the end of every backgrond saving.
8352 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
8353 * otherwise REDIS_ERR is passed to the function.
8354 *
8355 * The goal of this function is to handle slaves waiting for a successful
8356 * background saving in order to perform non-blocking synchronization. */
8357 static void updateSlavesWaitingBgsave(int bgsaveerr) {
8358 listNode *ln;
8359 int startbgsave = 0;
8360 listIter li;
8361
8362 listRewind(server.slaves,&li);
8363 while((ln = listNext(&li))) {
8364 redisClient *slave = ln->value;
8365
8366 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
8367 startbgsave = 1;
8368 slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
8369 } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
8370 struct redis_stat buf;
8371
8372 if (bgsaveerr != REDIS_OK) {
8373 freeClient(slave);
8374 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
8375 continue;
8376 }
8377 if ((slave->repldbfd = open(server.dbfilename,O_RDONLY)) == -1 ||
8378 redis_fstat(slave->repldbfd,&buf) == -1) {
8379 freeClient(slave);
8380 redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
8381 continue;
8382 }
8383 slave->repldboff = 0;
8384 slave->repldbsize = buf.st_size;
8385 slave->replstate = REDIS_REPL_SEND_BULK;
8386 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
8387 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {
8388 freeClient(slave);
8389 continue;
8390 }
8391 }
8392 }
8393 if (startbgsave) {
8394 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
8395 listIter li;
8396
8397 listRewind(server.slaves,&li);
8398 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
8399 while((ln = listNext(&li))) {
8400 redisClient *slave = ln->value;
8401
8402 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
8403 freeClient(slave);
8404 }
8405 }
8406 }
8407 }
8408
8409 static int syncWithMaster(void) {
8410 char buf[1024], tmpfile[256], authcmd[1024];
8411 long dumpsize;
8412 int fd = anetTcpConnect(NULL,server.masterhost,server.masterport);
8413 int dfd, maxtries = 5;
8414
8415 if (fd == -1) {
8416 redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",
8417 strerror(errno));
8418 return REDIS_ERR;
8419 }
8420
8421 /* AUTH with the master if required. */
8422 if(server.masterauth) {
8423 snprintf(authcmd, 1024, "AUTH %s\r\n", server.masterauth);
8424 if (syncWrite(fd, authcmd, strlen(server.masterauth)+7, 5) == -1) {
8425 close(fd);
8426 redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",
8427 strerror(errno));
8428 return REDIS_ERR;
8429 }
8430 /* Read the AUTH result. */
8431 if (syncReadLine(fd,buf,1024,3600) == -1) {
8432 close(fd);
8433 redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s",
8434 strerror(errno));
8435 return REDIS_ERR;
8436 }
8437 if (buf[0] != '+') {
8438 close(fd);
8439 redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?");
8440 return REDIS_ERR;
8441 }
8442 }
8443
8444 /* Issue the SYNC command */
8445 if (syncWrite(fd,"SYNC \r\n",7,5) == -1) {
8446 close(fd);
8447 redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",
8448 strerror(errno));
8449 return REDIS_ERR;
8450 }
8451 /* Read the bulk write count */
8452 if (syncReadLine(fd,buf,1024,3600) == -1) {
8453 close(fd);
8454 redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s",
8455 strerror(errno));
8456 return REDIS_ERR;
8457 }
8458 if (buf[0] != '$') {
8459 close(fd);
8460 redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
8461 return REDIS_ERR;
8462 }
8463 dumpsize = strtol(buf+1,NULL,10);
8464 redisLog(REDIS_NOTICE,"Receiving %ld bytes data dump from MASTER",dumpsize);
8465 /* Read the bulk write data on a temp file */
8466 while(maxtries--) {
8467 snprintf(tmpfile,256,
8468 "temp-%d.%ld.rdb",(int)time(NULL),(long int)getpid());
8469 dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);
8470 if (dfd != -1) break;
8471 sleep(1);
8472 }
8473 if (dfd == -1) {
8474 close(fd);
8475 redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
8476 return REDIS_ERR;
8477 }
8478 while(dumpsize) {
8479 int nread, nwritten;
8480
8481 nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024);
8482 if (nread == -1) {
8483 redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
8484 strerror(errno));
8485 close(fd);
8486 close(dfd);
8487 return REDIS_ERR;
8488 }
8489 nwritten = write(dfd,buf,nread);
8490 if (nwritten == -1) {
8491 redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
8492 close(fd);
8493 close(dfd);
8494 return REDIS_ERR;
8495 }
8496 dumpsize -= nread;
8497 }
8498 close(dfd);
8499 if (rename(tmpfile,server.dbfilename) == -1) {
8500 redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
8501 unlink(tmpfile);
8502 close(fd);
8503 return REDIS_ERR;
8504 }
8505 emptyDb();
8506 if (rdbLoad(server.dbfilename) != REDIS_OK) {
8507 redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
8508 close(fd);
8509 return REDIS_ERR;
8510 }
8511 server.master = createClient(fd);
8512 server.master->flags |= REDIS_MASTER;
8513 server.master->authenticated = 1;
8514 server.replstate = REDIS_REPL_CONNECTED;
8515 return REDIS_OK;
8516 }
8517
8518 static void slaveofCommand(redisClient *c) {
8519 if (!strcasecmp(c->argv[1]->ptr,"no") &&
8520 !strcasecmp(c->argv[2]->ptr,"one")) {
8521 if (server.masterhost) {
8522 sdsfree(server.masterhost);
8523 server.masterhost = NULL;
8524 if (server.master) freeClient(server.master);
8525 server.replstate = REDIS_REPL_NONE;
8526 redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
8527 }
8528 } else {
8529 sdsfree(server.masterhost);
8530 server.masterhost = sdsdup(c->argv[1]->ptr);
8531 server.masterport = atoi(c->argv[2]->ptr);
8532 if (server.master) freeClient(server.master);
8533 server.replstate = REDIS_REPL_CONNECT;
8534 redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
8535 server.masterhost, server.masterport);
8536 }
8537 addReply(c,shared.ok);
8538 }
8539
8540 /* ============================ Maxmemory directive ======================== */
8541
8542 /* Try to free one object form the pre-allocated objects free list.
8543 * This is useful under low mem conditions as by default we take 1 million
8544 * free objects allocated. On success REDIS_OK is returned, otherwise
8545 * REDIS_ERR. */
8546 static int tryFreeOneObjectFromFreelist(void) {
8547 robj *o;
8548
8549 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
8550 if (listLength(server.objfreelist)) {
8551 listNode *head = listFirst(server.objfreelist);
8552 o = listNodeValue(head);
8553 listDelNode(server.objfreelist,head);
8554 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8555 zfree(o);
8556 return REDIS_OK;
8557 } else {
8558 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8559 return REDIS_ERR;
8560 }
8561 }
8562
8563 /* This function gets called when 'maxmemory' is set on the config file to limit
8564 * the max memory used by the server, and we are out of memory.
8565 * This function will try to, in order:
8566 *
8567 * - Free objects from the free list
8568 * - Try to remove keys with an EXPIRE set
8569 *
8570 * It is not possible to free enough memory to reach used-memory < maxmemory
8571 * the server will start refusing commands that will enlarge even more the
8572 * memory usage.
8573 */
8574 static void freeMemoryIfNeeded(void) {
8575 while (server.maxmemory && zmalloc_used_memory() > server.maxmemory) {
8576 int j, k, freed = 0;
8577
8578 if (tryFreeOneObjectFromFreelist() == REDIS_OK) continue;
8579 for (j = 0; j < server.dbnum; j++) {
8580 int minttl = -1;
8581 robj *minkey = NULL;
8582 struct dictEntry *de;
8583
8584 if (dictSize(server.db[j].expires)) {
8585 freed = 1;
8586 /* From a sample of three keys drop the one nearest to
8587 * the natural expire */
8588 for (k = 0; k < 3; k++) {
8589 time_t t;
8590
8591 de = dictGetRandomKey(server.db[j].expires);
8592 t = (time_t) dictGetEntryVal(de);
8593 if (minttl == -1 || t < minttl) {
8594 minkey = dictGetEntryKey(de);
8595 minttl = t;
8596 }
8597 }
8598 dbDelete(server.db+j,minkey);
8599 }
8600 }
8601 if (!freed) return; /* nothing to free... */
8602 }
8603 }
8604
8605 /* ============================== Append Only file ========================== */
8606
8607 /* Called when the user switches from "appendonly yes" to "appendonly no"
8608 * at runtime using the CONFIG command. */
8609 static void stopAppendOnly(void) {
8610 flushAppendOnlyFile();
8611 aof_fsync(server.appendfd);
8612 close(server.appendfd);
8613
8614 server.appendfd = -1;
8615 server.appendseldb = -1;
8616 server.appendonly = 0;
8617 /* rewrite operation in progress? kill it, wait child exit */
8618 if (server.bgsavechildpid != -1) {
8619 int statloc;
8620
8621 if (kill(server.bgsavechildpid,SIGKILL) != -1)
8622 wait3(&statloc,0,NULL);
8623 /* reset the buffer accumulating changes while the child saves */
8624 sdsfree(server.bgrewritebuf);
8625 server.bgrewritebuf = sdsempty();
8626 server.bgsavechildpid = -1;
8627 }
8628 }
8629
8630 /* Called when the user switches from "appendonly no" to "appendonly yes"
8631 * at runtime using the CONFIG command. */
8632 static int startAppendOnly(void) {
8633 server.appendonly = 1;
8634 server.lastfsync = time(NULL);
8635 server.appendfd = open(server.appendfilename,O_WRONLY|O_APPEND|O_CREAT,0644);
8636 if (server.appendfd == -1) {
8637 redisLog(REDIS_WARNING,"Used tried to switch on AOF via CONFIG, but I can't open the AOF file: %s",strerror(errno));
8638 return REDIS_ERR;
8639 }
8640 if (rewriteAppendOnlyFileBackground() == REDIS_ERR) {
8641 server.appendonly = 0;
8642 close(server.appendfd);
8643 redisLog(REDIS_WARNING,"Used tried to switch on AOF via CONFIG, I can't trigger a background AOF rewrite operation. Check the above logs for more info about the error.",strerror(errno));
8644 return REDIS_ERR;
8645 }
8646 return REDIS_OK;
8647 }
8648
8649 /* Write the append only file buffer on disk.
8650 *
8651 * Since we are required to write the AOF before replying to the client,
8652 * and the only way the client socket can get a write is entering when the
8653 * the event loop, we accumulate all the AOF writes in a memory
8654 * buffer and write it on disk using this function just before entering
8655 * the event loop again. */
8656 static void flushAppendOnlyFile(void) {
8657 time_t now;
8658 ssize_t nwritten;
8659
8660 if (sdslen(server.aofbuf) == 0) return;
8661
8662 /* We want to perform a single write. This should be guaranteed atomic
8663 * at least if the filesystem we are writing is a real physical one.
8664 * While this will save us against the server being killed I don't think
8665 * there is much to do about the whole server stopping for power problems
8666 * or alike */
8667 nwritten = write(server.appendfd,server.aofbuf,sdslen(server.aofbuf));
8668 if (nwritten != (signed)sdslen(server.aofbuf)) {
8669 /* Ooops, we are in troubles. The best thing to do for now is
8670 * aborting instead of giving the illusion that everything is
8671 * working as expected. */
8672 if (nwritten == -1) {
8673 redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno));
8674 } else {
8675 redisLog(REDIS_WARNING,"Exiting on short write while writing to the append-only file: %s",strerror(errno));
8676 }
8677 exit(1);
8678 }
8679 sdsfree(server.aofbuf);
8680 server.aofbuf = sdsempty();
8681
8682 /* Don't Fsync if no-appendfsync-on-rewrite is set to yes and we have
8683 * childs performing heavy I/O on disk. */
8684 if (server.no_appendfsync_on_rewrite &&
8685 (server.bgrewritechildpid != -1 || server.bgsavechildpid != -1))
8686 return;
8687 /* Fsync if needed */
8688 now = time(NULL);
8689 if (server.appendfsync == APPENDFSYNC_ALWAYS ||
8690 (server.appendfsync == APPENDFSYNC_EVERYSEC &&
8691 now-server.lastfsync > 1))
8692 {
8693 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
8694 * flushing metadata. */
8695 aof_fsync(server.appendfd); /* Let's try to get this data on the disk */
8696 server.lastfsync = now;
8697 }
8698 }
8699
8700 static sds catAppendOnlyGenericCommand(sds buf, int argc, robj **argv) {
8701 int j;
8702 buf = sdscatprintf(buf,"*%d\r\n",argc);
8703 for (j = 0; j < argc; j++) {
8704 robj *o = getDecodedObject(argv[j]);
8705 buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
8706 buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
8707 buf = sdscatlen(buf,"\r\n",2);
8708 decrRefCount(o);
8709 }
8710 return buf;
8711 }
8712
8713 static sds catAppendOnlyExpireAtCommand(sds buf, robj *key, robj *seconds) {
8714 int argc = 3;
8715 long when;
8716 robj *argv[3];
8717
8718 /* Make sure we can use strtol */
8719 seconds = getDecodedObject(seconds);
8720 when = time(NULL)+strtol(seconds->ptr,NULL,10);
8721 decrRefCount(seconds);
8722
8723 argv[0] = createStringObject("EXPIREAT",8);
8724 argv[1] = key;
8725 argv[2] = createObject(REDIS_STRING,
8726 sdscatprintf(sdsempty(),"%ld",when));
8727 buf = catAppendOnlyGenericCommand(buf, argc, argv);
8728 decrRefCount(argv[0]);
8729 decrRefCount(argv[2]);
8730 return buf;
8731 }
8732
8733 static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
8734 sds buf = sdsempty();
8735 robj *tmpargv[3];
8736
8737 /* The DB this command was targetting is not the same as the last command
8738 * we appendend. To issue a SELECT command is needed. */
8739 if (dictid != server.appendseldb) {
8740 char seldb[64];
8741
8742 snprintf(seldb,sizeof(seldb),"%d",dictid);
8743 buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
8744 (unsigned long)strlen(seldb),seldb);
8745 server.appendseldb = dictid;
8746 }
8747
8748 if (cmd->proc == expireCommand) {
8749 /* Translate EXPIRE into EXPIREAT */
8750 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8751 } else if (cmd->proc == setexCommand) {
8752 /* Translate SETEX to SET and EXPIREAT */
8753 tmpargv[0] = createStringObject("SET",3);
8754 tmpargv[1] = argv[1];
8755 tmpargv[2] = argv[3];
8756 buf = catAppendOnlyGenericCommand(buf,3,tmpargv);
8757 decrRefCount(tmpargv[0]);
8758 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8759 } else {
8760 buf = catAppendOnlyGenericCommand(buf,argc,argv);
8761 }
8762
8763 /* Append to the AOF buffer. This will be flushed on disk just before
8764 * of re-entering the event loop, so before the client will get a
8765 * positive reply about the operation performed. */
8766 server.aofbuf = sdscatlen(server.aofbuf,buf,sdslen(buf));
8767
8768 /* If a background append only file rewriting is in progress we want to
8769 * accumulate the differences between the child DB and the current one
8770 * in a buffer, so that when the child process will do its work we
8771 * can append the differences to the new append only file. */
8772 if (server.bgrewritechildpid != -1)
8773 server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf));
8774
8775 sdsfree(buf);
8776 }
8777
8778 /* In Redis commands are always executed in the context of a client, so in
8779 * order to load the append only file we need to create a fake client. */
8780 static struct redisClient *createFakeClient(void) {
8781 struct redisClient *c = zmalloc(sizeof(*c));
8782
8783 selectDb(c,0);
8784 c->fd = -1;
8785 c->querybuf = sdsempty();
8786 c->argc = 0;
8787 c->argv = NULL;
8788 c->flags = 0;
8789 /* We set the fake client as a slave waiting for the synchronization
8790 * so that Redis will not try to send replies to this client. */
8791 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
8792 c->reply = listCreate();
8793 listSetFreeMethod(c->reply,decrRefCount);
8794 listSetDupMethod(c->reply,dupClientReplyValue);
8795 initClientMultiState(c);
8796 return c;
8797 }
8798
8799 static void freeFakeClient(struct redisClient *c) {
8800 sdsfree(c->querybuf);
8801 listRelease(c->reply);
8802 freeClientMultiState(c);
8803 zfree(c);
8804 }
8805
8806 /* Replay the append log file. On error REDIS_OK is returned. On non fatal
8807 * error (the append only file is zero-length) REDIS_ERR is returned. On
8808 * fatal error an error message is logged and the program exists. */
8809 int loadAppendOnlyFile(char *filename) {
8810 struct redisClient *fakeClient;
8811 FILE *fp = fopen(filename,"r");
8812 struct redis_stat sb;
8813 int appendonly = server.appendonly;
8814
8815 if (redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0)
8816 return REDIS_ERR;
8817
8818 if (fp == NULL) {
8819 redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
8820 exit(1);
8821 }
8822
8823 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
8824 * to the same file we're about to read. */
8825 server.appendonly = 0;
8826
8827 fakeClient = createFakeClient();
8828 while(1) {
8829 int argc, j;
8830 unsigned long len;
8831 robj **argv;
8832 char buf[128];
8833 sds argsds;
8834 struct redisCommand *cmd;
8835 int force_swapout;
8836
8837 if (fgets(buf,sizeof(buf),fp) == NULL) {
8838 if (feof(fp))
8839 break;
8840 else
8841 goto readerr;
8842 }
8843 if (buf[0] != '*') goto fmterr;
8844 argc = atoi(buf+1);
8845 argv = zmalloc(sizeof(robj*)*argc);
8846 for (j = 0; j < argc; j++) {
8847 if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr;
8848 if (buf[0] != '$') goto fmterr;
8849 len = strtol(buf+1,NULL,10);
8850 argsds = sdsnewlen(NULL,len);
8851 if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
8852 argv[j] = createObject(REDIS_STRING,argsds);
8853 if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
8854 }
8855
8856 /* Command lookup */
8857 cmd = lookupCommand(argv[0]->ptr);
8858 if (!cmd) {
8859 redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr);
8860 exit(1);
8861 }
8862 /* Try object encoding */
8863 if (cmd->flags & REDIS_CMD_BULK)
8864 argv[argc-1] = tryObjectEncoding(argv[argc-1]);
8865 /* Run the command in the context of a fake client */
8866 fakeClient->argc = argc;
8867 fakeClient->argv = argv;
8868 cmd->proc(fakeClient);
8869 /* Discard the reply objects list from the fake client */
8870 while(listLength(fakeClient->reply))
8871 listDelNode(fakeClient->reply,listFirst(fakeClient->reply));
8872 /* Clean up, ready for the next command */
8873 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
8874 zfree(argv);
8875 /* Handle swapping while loading big datasets when VM is on */
8876 force_swapout = 0;
8877 if ((zmalloc_used_memory() - server.vm_max_memory) > 1024*1024*32)
8878 force_swapout = 1;
8879
8880 if (server.vm_enabled && force_swapout) {
8881 while (zmalloc_used_memory() > server.vm_max_memory) {
8882 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
8883 }
8884 }
8885 }
8886
8887 /* This point can only be reached when EOF is reached without errors.
8888 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
8889 if (fakeClient->flags & REDIS_MULTI) goto readerr;
8890
8891 fclose(fp);
8892 freeFakeClient(fakeClient);
8893 server.appendonly = appendonly;
8894 return REDIS_OK;
8895
8896 readerr:
8897 if (feof(fp)) {
8898 redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file");
8899 } else {
8900 redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
8901 }
8902 exit(1);
8903 fmterr:
8904 redisLog(REDIS_WARNING,"Bad file format reading the append only file");
8905 exit(1);
8906 }
8907
8908 /* Write binary-safe string into a file in the bulkformat
8909 * $<count>\r\n<payload>\r\n */
8910 static int fwriteBulkString(FILE *fp, char *s, unsigned long len) {
8911 char cbuf[128];
8912 int clen;
8913 cbuf[0] = '$';
8914 clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,len);
8915 cbuf[clen++] = '\r';
8916 cbuf[clen++] = '\n';
8917 if (fwrite(cbuf,clen,1,fp) == 0) return 0;
8918 if (len > 0 && fwrite(s,len,1,fp) == 0) return 0;
8919 if (fwrite("\r\n",2,1,fp) == 0) return 0;
8920 return 1;
8921 }
8922
8923 /* Write a double value in bulk format $<count>\r\n<payload>\r\n */
8924 static int fwriteBulkDouble(FILE *fp, double d) {
8925 char buf[128], dbuf[128];
8926
8927 snprintf(dbuf,sizeof(dbuf),"%.17g\r\n",d);
8928 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(dbuf)-2);
8929 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8930 if (fwrite(dbuf,strlen(dbuf),1,fp) == 0) return 0;
8931 return 1;
8932 }
8933
8934 /* Write a long value in bulk format $<count>\r\n<payload>\r\n */
8935 static int fwriteBulkLongLong(FILE *fp, long long l) {
8936 char bbuf[128], lbuf[128];
8937 unsigned int blen, llen;
8938 llen = ll2string(lbuf,32,l);
8939 blen = snprintf(bbuf,sizeof(bbuf),"$%u\r\n%s\r\n",llen,lbuf);
8940 if (fwrite(bbuf,blen,1,fp) == 0) return 0;
8941 return 1;
8942 }
8943
8944 /* Delegate writing an object to writing a bulk string or bulk long long. */
8945 static int fwriteBulkObject(FILE *fp, robj *obj) {
8946 /* Avoid using getDecodedObject to help copy-on-write (we are often
8947 * in a child process when this function is called). */
8948 if (obj->encoding == REDIS_ENCODING_INT) {
8949 return fwriteBulkLongLong(fp,(long)obj->ptr);
8950 } else if (obj->encoding == REDIS_ENCODING_RAW) {
8951 return fwriteBulkString(fp,obj->ptr,sdslen(obj->ptr));
8952 } else {
8953 redisPanic("Unknown string encoding");
8954 }
8955 }
8956
8957 /* Write a sequence of commands able to fully rebuild the dataset into
8958 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
8959 static int rewriteAppendOnlyFile(char *filename) {
8960 dictIterator *di = NULL;
8961 dictEntry *de;
8962 FILE *fp;
8963 char tmpfile[256];
8964 int j;
8965 time_t now = time(NULL);
8966
8967 /* Note that we have to use a different temp name here compared to the
8968 * one used by rewriteAppendOnlyFileBackground() function. */
8969 snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid());
8970 fp = fopen(tmpfile,"w");
8971 if (!fp) {
8972 redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno));
8973 return REDIS_ERR;
8974 }
8975 for (j = 0; j < server.dbnum; j++) {
8976 char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
8977 redisDb *db = server.db+j;
8978 dict *d = db->dict;
8979 if (dictSize(d) == 0) continue;
8980 di = dictGetIterator(d);
8981 if (!di) {
8982 fclose(fp);
8983 return REDIS_ERR;
8984 }
8985
8986 /* SELECT the new DB */
8987 if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr;
8988 if (fwriteBulkLongLong(fp,j) == 0) goto werr;
8989
8990 /* Iterate this DB writing every entry */
8991 while((de = dictNext(di)) != NULL) {
8992 sds keystr = dictGetEntryKey(de);
8993 robj key, *o;
8994 time_t expiretime;
8995 int swapped;
8996
8997 keystr = dictGetEntryKey(de);
8998 o = dictGetEntryVal(de);
8999 initStaticStringObject(key,keystr);
9000 /* If the value for this key is swapped, load a preview in memory.
9001 * We use a "swapped" flag to remember if we need to free the
9002 * value object instead to just increment the ref count anyway
9003 * in order to avoid copy-on-write of pages if we are forked() */
9004 if (!server.vm_enabled || o->storage == REDIS_VM_MEMORY ||
9005 o->storage == REDIS_VM_SWAPPING) {
9006 swapped = 0;
9007 } else {
9008 o = vmPreviewObject(o);
9009 swapped = 1;
9010 }
9011 expiretime = getExpire(db,&key);
9012
9013 /* Save the key and associated value */
9014 if (o->type == REDIS_STRING) {
9015 /* Emit a SET command */
9016 char cmd[]="*3\r\n$3\r\nSET\r\n";
9017 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9018 /* Key and value */
9019 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9020 if (fwriteBulkObject(fp,o) == 0) goto werr;
9021 } else if (o->type == REDIS_LIST) {
9022 /* Emit the RPUSHes needed to rebuild the list */
9023 char cmd[]="*3\r\n$5\r\nRPUSH\r\n";
9024 if (o->encoding == REDIS_ENCODING_ZIPLIST) {
9025 unsigned char *zl = o->ptr;
9026 unsigned char *p = ziplistIndex(zl,0);
9027 unsigned char *vstr;
9028 unsigned int vlen;
9029 long long vlong;
9030
9031 while(ziplistGet(p,&vstr,&vlen,&vlong)) {
9032 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9033 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9034 if (vstr) {
9035 if (fwriteBulkString(fp,(char*)vstr,vlen) == 0)
9036 goto werr;
9037 } else {
9038 if (fwriteBulkLongLong(fp,vlong) == 0)
9039 goto werr;
9040 }
9041 p = ziplistNext(zl,p);
9042 }
9043 } else if (o->encoding == REDIS_ENCODING_LIST) {
9044 list *list = o->ptr;
9045 listNode *ln;
9046 listIter li;
9047
9048 listRewind(list,&li);
9049 while((ln = listNext(&li))) {
9050 robj *eleobj = listNodeValue(ln);
9051
9052 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9053 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9054 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9055 }
9056 } else {
9057 redisPanic("Unknown list encoding");
9058 }
9059 } else if (o->type == REDIS_SET) {
9060 /* Emit the SADDs needed to rebuild the set */
9061 dict *set = o->ptr;
9062 dictIterator *di = dictGetIterator(set);
9063 dictEntry *de;
9064
9065 while((de = dictNext(di)) != NULL) {
9066 char cmd[]="*3\r\n$4\r\nSADD\r\n";
9067 robj *eleobj = dictGetEntryKey(de);
9068
9069 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9070 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9071 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9072 }
9073 dictReleaseIterator(di);
9074 } else if (o->type == REDIS_ZSET) {
9075 /* Emit the ZADDs needed to rebuild the sorted set */
9076 zset *zs = o->ptr;
9077 dictIterator *di = dictGetIterator(zs->dict);
9078 dictEntry *de;
9079
9080 while((de = dictNext(di)) != NULL) {
9081 char cmd[]="*4\r\n$4\r\nZADD\r\n";
9082 robj *eleobj = dictGetEntryKey(de);
9083 double *score = dictGetEntryVal(de);
9084
9085 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9086 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9087 if (fwriteBulkDouble(fp,*score) == 0) goto werr;
9088 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
9089 }
9090 dictReleaseIterator(di);
9091 } else if (o->type == REDIS_HASH) {
9092 char cmd[]="*4\r\n$4\r\nHSET\r\n";
9093
9094 /* Emit the HSETs needed to rebuild the hash */
9095 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
9096 unsigned char *p = zipmapRewind(o->ptr);
9097 unsigned char *field, *val;
9098 unsigned int flen, vlen;
9099
9100 while((p = zipmapNext(p,&field,&flen,&val,&vlen)) != NULL) {
9101 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9102 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9103 if (fwriteBulkString(fp,(char*)field,flen) == -1)
9104 return -1;
9105 if (fwriteBulkString(fp,(char*)val,vlen) == -1)
9106 return -1;
9107 }
9108 } else {
9109 dictIterator *di = dictGetIterator(o->ptr);
9110 dictEntry *de;
9111
9112 while((de = dictNext(di)) != NULL) {
9113 robj *field = dictGetEntryKey(de);
9114 robj *val = dictGetEntryVal(de);
9115
9116 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9117 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9118 if (fwriteBulkObject(fp,field) == -1) return -1;
9119 if (fwriteBulkObject(fp,val) == -1) return -1;
9120 }
9121 dictReleaseIterator(di);
9122 }
9123 } else {
9124 redisPanic("Unknown object type");
9125 }
9126 /* Save the expire time */
9127 if (expiretime != -1) {
9128 char cmd[]="*3\r\n$8\r\nEXPIREAT\r\n";
9129 /* If this key is already expired skip it */
9130 if (expiretime < now) continue;
9131 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
9132 if (fwriteBulkObject(fp,&key) == 0) goto werr;
9133 if (fwriteBulkLongLong(fp,expiretime) == 0) goto werr;
9134 }
9135 if (swapped) decrRefCount(o);
9136 }
9137 dictReleaseIterator(di);
9138 }
9139
9140 /* Make sure data will not remain on the OS's output buffers */
9141 fflush(fp);
9142 aof_fsync(fileno(fp));
9143 fclose(fp);
9144
9145 /* Use RENAME to make sure the DB file is changed atomically only
9146 * if the generate DB file is ok. */
9147 if (rename(tmpfile,filename) == -1) {
9148 redisLog(REDIS_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno));
9149 unlink(tmpfile);
9150 return REDIS_ERR;
9151 }
9152 redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed");
9153 return REDIS_OK;
9154
9155 werr:
9156 fclose(fp);
9157 unlink(tmpfile);
9158 redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
9159 if (di) dictReleaseIterator(di);
9160 return REDIS_ERR;
9161 }
9162
9163 /* This is how rewriting of the append only file in background works:
9164 *
9165 * 1) The user calls BGREWRITEAOF
9166 * 2) Redis calls this function, that forks():
9167 * 2a) the child rewrite the append only file in a temp file.
9168 * 2b) the parent accumulates differences in server.bgrewritebuf.
9169 * 3) When the child finished '2a' exists.
9170 * 4) The parent will trap the exit code, if it's OK, will append the
9171 * data accumulated into server.bgrewritebuf into the temp file, and
9172 * finally will rename(2) the temp file in the actual file name.
9173 * The the new file is reopened as the new append only file. Profit!
9174 */
9175 static int rewriteAppendOnlyFileBackground(void) {
9176 pid_t childpid;
9177
9178 if (server.bgrewritechildpid != -1) return REDIS_ERR;
9179 if (server.vm_enabled) waitEmptyIOJobsQueue();
9180 if ((childpid = fork()) == 0) {
9181 /* Child */
9182 char tmpfile[256];
9183
9184 if (server.vm_enabled) vmReopenSwapFile();
9185 close(server.fd);
9186 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
9187 if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
9188 _exit(0);
9189 } else {
9190 _exit(1);
9191 }
9192 } else {
9193 /* Parent */
9194 if (childpid == -1) {
9195 redisLog(REDIS_WARNING,
9196 "Can't rewrite append only file in background: fork: %s",
9197 strerror(errno));
9198 return REDIS_ERR;
9199 }
9200 redisLog(REDIS_NOTICE,
9201 "Background append only file rewriting started by pid %d",childpid);
9202 server.bgrewritechildpid = childpid;
9203 updateDictResizePolicy();
9204 /* We set appendseldb to -1 in order to force the next call to the
9205 * feedAppendOnlyFile() to issue a SELECT command, so the differences
9206 * accumulated by the parent into server.bgrewritebuf will start
9207 * with a SELECT statement and it will be safe to merge. */
9208 server.appendseldb = -1;
9209 return REDIS_OK;
9210 }
9211 return REDIS_OK; /* unreached */
9212 }
9213
9214 static void bgrewriteaofCommand(redisClient *c) {
9215 if (server.bgrewritechildpid != -1) {
9216 addReplySds(c,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
9217 return;
9218 }
9219 if (rewriteAppendOnlyFileBackground() == REDIS_OK) {
9220 char *status = "+Background append only file rewriting started\r\n";
9221 addReplySds(c,sdsnew(status));
9222 } else {
9223 addReply(c,shared.err);
9224 }
9225 }
9226
9227 static void aofRemoveTempFile(pid_t childpid) {
9228 char tmpfile[256];
9229
9230 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid);
9231 unlink(tmpfile);
9232 }
9233
9234 /* Virtual Memory is composed mainly of two subsystems:
9235 * - Blocking Virutal Memory
9236 * - Threaded Virtual Memory I/O
9237 * The two parts are not fully decoupled, but functions are split among two
9238 * different sections of the source code (delimited by comments) in order to
9239 * make more clear what functionality is about the blocking VM and what about
9240 * the threaded (not blocking) VM.
9241 *
9242 * Redis VM design:
9243 *
9244 * Redis VM is a blocking VM (one that blocks reading swapped values from
9245 * disk into memory when a value swapped out is needed in memory) that is made
9246 * unblocking by trying to examine the command argument vector in order to
9247 * load in background values that will likely be needed in order to exec
9248 * the command. The command is executed only once all the relevant keys
9249 * are loaded into memory.
9250 *
9251 * This basically is almost as simple of a blocking VM, but almost as parallel
9252 * as a fully non-blocking VM.
9253 */
9254
9255 /* =================== Virtual Memory - Blocking Side ====================== */
9256
9257 /* Create a VM pointer object. This kind of objects are used in place of
9258 * values in the key -> value hash table, for swapped out objects. */
9259 static vmpointer *createVmPointer(int vtype) {
9260 vmpointer *vp = zmalloc(sizeof(vmpointer));
9261
9262 vp->type = REDIS_VMPOINTER;
9263 vp->storage = REDIS_VM_SWAPPED;
9264 vp->vtype = vtype;
9265 return vp;
9266 }
9267
9268 static void vmInit(void) {
9269 off_t totsize;
9270 int pipefds[2];
9271 size_t stacksize;
9272 struct flock fl;
9273
9274 if (server.vm_max_threads != 0)
9275 zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */
9276
9277 redisLog(REDIS_NOTICE,"Using '%s' as swap file",server.vm_swap_file);
9278 /* Try to open the old swap file, otherwise create it */
9279 if ((server.vm_fp = fopen(server.vm_swap_file,"r+b")) == NULL) {
9280 server.vm_fp = fopen(server.vm_swap_file,"w+b");
9281 }
9282 if (server.vm_fp == NULL) {
9283 redisLog(REDIS_WARNING,
9284 "Can't open the swap file: %s. Exiting.",
9285 strerror(errno));
9286 exit(1);
9287 }
9288 server.vm_fd = fileno(server.vm_fp);
9289 /* Lock the swap file for writing, this is useful in order to avoid
9290 * another instance to use the same swap file for a config error. */
9291 fl.l_type = F_WRLCK;
9292 fl.l_whence = SEEK_SET;
9293 fl.l_start = fl.l_len = 0;
9294 if (fcntl(server.vm_fd,F_SETLK,&fl) == -1) {
9295 redisLog(REDIS_WARNING,
9296 "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));
9297 exit(1);
9298 }
9299 /* Initialize */
9300 server.vm_next_page = 0;
9301 server.vm_near_pages = 0;
9302 server.vm_stats_used_pages = 0;
9303 server.vm_stats_swapped_objects = 0;
9304 server.vm_stats_swapouts = 0;
9305 server.vm_stats_swapins = 0;
9306 totsize = server.vm_pages*server.vm_page_size;
9307 redisLog(REDIS_NOTICE,"Allocating %lld bytes of swap file",totsize);
9308 if (ftruncate(server.vm_fd,totsize) == -1) {
9309 redisLog(REDIS_WARNING,"Can't ftruncate swap file: %s. Exiting.",
9310 strerror(errno));
9311 exit(1);
9312 } else {
9313 redisLog(REDIS_NOTICE,"Swap file allocated with success");
9314 }
9315 server.vm_bitmap = zmalloc((server.vm_pages+7)/8);
9316 redisLog(REDIS_VERBOSE,"Allocated %lld bytes page table for %lld pages",
9317 (long long) (server.vm_pages+7)/8, server.vm_pages);
9318 memset(server.vm_bitmap,0,(server.vm_pages+7)/8);
9319
9320 /* Initialize threaded I/O (used by Virtual Memory) */
9321 server.io_newjobs = listCreate();
9322 server.io_processing = listCreate();
9323 server.io_processed = listCreate();
9324 server.io_ready_clients = listCreate();
9325 pthread_mutex_init(&server.io_mutex,NULL);
9326 pthread_mutex_init(&server.obj_freelist_mutex,NULL);
9327 pthread_mutex_init(&server.io_swapfile_mutex,NULL);
9328 server.io_active_threads = 0;
9329 if (pipe(pipefds) == -1) {
9330 redisLog(REDIS_WARNING,"Unable to intialized VM: pipe(2): %s. Exiting."
9331 ,strerror(errno));
9332 exit(1);
9333 }
9334 server.io_ready_pipe_read = pipefds[0];
9335 server.io_ready_pipe_write = pipefds[1];
9336 redisAssert(anetNonBlock(NULL,server.io_ready_pipe_read) != ANET_ERR);
9337 /* LZF requires a lot of stack */
9338 pthread_attr_init(&server.io_threads_attr);
9339 pthread_attr_getstacksize(&server.io_threads_attr, &stacksize);
9340 while (stacksize < REDIS_THREAD_STACK_SIZE) stacksize *= 2;
9341 pthread_attr_setstacksize(&server.io_threads_attr, stacksize);
9342 /* Listen for events in the threaded I/O pipe */
9343 if (aeCreateFileEvent(server.el, server.io_ready_pipe_read, AE_READABLE,
9344 vmThreadedIOCompletedJob, NULL) == AE_ERR)
9345 oom("creating file event");
9346 }
9347
9348 /* Mark the page as used */
9349 static void vmMarkPageUsed(off_t page) {
9350 off_t byte = page/8;
9351 int bit = page&7;
9352 redisAssert(vmFreePage(page) == 1);
9353 server.vm_bitmap[byte] |= 1<<bit;
9354 }
9355
9356 /* Mark N contiguous pages as used, with 'page' being the first. */
9357 static void vmMarkPagesUsed(off_t page, off_t count) {
9358 off_t j;
9359
9360 for (j = 0; j < count; j++)
9361 vmMarkPageUsed(page+j);
9362 server.vm_stats_used_pages += count;
9363 redisLog(REDIS_DEBUG,"Mark USED pages: %lld pages at %lld\n",
9364 (long long)count, (long long)page);
9365 }
9366
9367 /* Mark the page as free */
9368 static void vmMarkPageFree(off_t page) {
9369 off_t byte = page/8;
9370 int bit = page&7;
9371 redisAssert(vmFreePage(page) == 0);
9372 server.vm_bitmap[byte] &= ~(1<<bit);
9373 }
9374
9375 /* Mark N contiguous pages as free, with 'page' being the first. */
9376 static void vmMarkPagesFree(off_t page, off_t count) {
9377 off_t j;
9378
9379 for (j = 0; j < count; j++)
9380 vmMarkPageFree(page+j);
9381 server.vm_stats_used_pages -= count;
9382 redisLog(REDIS_DEBUG,"Mark FREE pages: %lld pages at %lld\n",
9383 (long long)count, (long long)page);
9384 }
9385
9386 /* Test if the page is free */
9387 static int vmFreePage(off_t page) {
9388 off_t byte = page/8;
9389 int bit = page&7;
9390 return (server.vm_bitmap[byte] & (1<<bit)) == 0;
9391 }
9392
9393 /* Find N contiguous free pages storing the first page of the cluster in *first.
9394 * Returns REDIS_OK if it was able to find N contiguous pages, otherwise
9395 * REDIS_ERR is returned.
9396 *
9397 * This function uses a simple algorithm: we try to allocate
9398 * REDIS_VM_MAX_NEAR_PAGES sequentially, when we reach this limit we start
9399 * again from the start of the swap file searching for free spaces.
9400 *
9401 * If it looks pretty clear that there are no free pages near our offset
9402 * we try to find less populated places doing a forward jump of
9403 * REDIS_VM_MAX_RANDOM_JUMP, then we start scanning again a few pages
9404 * without hurry, and then we jump again and so forth...
9405 *
9406 * This function can be improved using a free list to avoid to guess
9407 * too much, since we could collect data about freed pages.
9408 *
9409 * note: I implemented this function just after watching an episode of
9410 * Battlestar Galactica, where the hybrid was continuing to say "JUMP!"
9411 */
9412 static int vmFindContiguousPages(off_t *first, off_t n) {
9413 off_t base, offset = 0, since_jump = 0, numfree = 0;
9414
9415 if (server.vm_near_pages == REDIS_VM_MAX_NEAR_PAGES) {
9416 server.vm_near_pages = 0;
9417 server.vm_next_page = 0;
9418 }
9419 server.vm_near_pages++; /* Yet another try for pages near to the old ones */
9420 base = server.vm_next_page;
9421
9422 while(offset < server.vm_pages) {
9423 off_t this = base+offset;
9424
9425 /* If we overflow, restart from page zero */
9426 if (this >= server.vm_pages) {
9427 this -= server.vm_pages;
9428 if (this == 0) {
9429 /* Just overflowed, what we found on tail is no longer
9430 * interesting, as it's no longer contiguous. */
9431 numfree = 0;
9432 }
9433 }
9434 if (vmFreePage(this)) {
9435 /* This is a free page */
9436 numfree++;
9437 /* Already got N free pages? Return to the caller, with success */
9438 if (numfree == n) {
9439 *first = this-(n-1);
9440 server.vm_next_page = this+1;
9441 redisLog(REDIS_DEBUG, "FOUND CONTIGUOUS PAGES: %lld pages at %lld\n", (long long) n, (long long) *first);
9442 return REDIS_OK;
9443 }
9444 } else {
9445 /* The current one is not a free page */
9446 numfree = 0;
9447 }
9448
9449 /* Fast-forward if the current page is not free and we already
9450 * searched enough near this place. */
9451 since_jump++;
9452 if (!numfree && since_jump >= REDIS_VM_MAX_RANDOM_JUMP/4) {
9453 offset += random() % REDIS_VM_MAX_RANDOM_JUMP;
9454 since_jump = 0;
9455 /* Note that even if we rewind after the jump, we are don't need
9456 * to make sure numfree is set to zero as we only jump *if* it
9457 * is set to zero. */
9458 } else {
9459 /* Otherwise just check the next page */
9460 offset++;
9461 }
9462 }
9463 return REDIS_ERR;
9464 }
9465
9466 /* Write the specified object at the specified page of the swap file */
9467 static int vmWriteObjectOnSwap(robj *o, off_t page) {
9468 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
9469 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
9470 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
9471 redisLog(REDIS_WARNING,
9472 "Critical VM problem in vmWriteObjectOnSwap(): can't seek: %s",
9473 strerror(errno));
9474 return REDIS_ERR;
9475 }
9476 rdbSaveObject(server.vm_fp,o);
9477 fflush(server.vm_fp);
9478 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
9479 return REDIS_OK;
9480 }
9481
9482 /* Transfers the 'val' object to disk. Store all the information
9483 * a 'vmpointer' object containing all the information needed to load the
9484 * object back later is returned.
9485 *
9486 * If we can't find enough contiguous empty pages to swap the object on disk
9487 * NULL is returned. */
9488 static vmpointer *vmSwapObjectBlocking(robj *val) {
9489 off_t pages = rdbSavedObjectPages(val,NULL);
9490 off_t page;
9491 vmpointer *vp;
9492
9493 assert(val->storage == REDIS_VM_MEMORY);
9494 assert(val->refcount == 1);
9495 if (vmFindContiguousPages(&page,pages) == REDIS_ERR) return NULL;
9496 if (vmWriteObjectOnSwap(val,page) == REDIS_ERR) return NULL;
9497
9498 vp = createVmPointer(val->type);
9499 vp->page = page;
9500 vp->usedpages = pages;
9501 decrRefCount(val); /* Deallocate the object from memory. */
9502 vmMarkPagesUsed(page,pages);
9503 redisLog(REDIS_DEBUG,"VM: object %p swapped out at %lld (%lld pages)",
9504 (void*) val,
9505 (unsigned long long) page, (unsigned long long) pages);
9506 server.vm_stats_swapped_objects++;
9507 server.vm_stats_swapouts++;
9508 return vp;
9509 }
9510
9511 static robj *vmReadObjectFromSwap(off_t page, int type) {
9512 robj *o;
9513
9514 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
9515 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
9516 redisLog(REDIS_WARNING,
9517 "Unrecoverable VM problem in vmReadObjectFromSwap(): can't seek: %s",
9518 strerror(errno));
9519 _exit(1);
9520 }
9521 o = rdbLoadObject(type,server.vm_fp);
9522 if (o == NULL) {
9523 redisLog(REDIS_WARNING, "Unrecoverable VM problem in vmReadObjectFromSwap(): can't load object from swap file: %s", strerror(errno));
9524 _exit(1);
9525 }
9526 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
9527 return o;
9528 }
9529
9530 /* Load the specified object from swap to memory.
9531 * The newly allocated object is returned.
9532 *
9533 * If preview is true the unserialized object is returned to the caller but
9534 * the pages are not marked as freed, nor the vp object is freed. */
9535 static robj *vmGenericLoadObject(vmpointer *vp, int preview) {
9536 robj *val;
9537
9538 redisAssert(vp->type == REDIS_VMPOINTER &&
9539 (vp->storage == REDIS_VM_SWAPPED || vp->storage == REDIS_VM_LOADING));
9540 val = vmReadObjectFromSwap(vp->page,vp->vtype);
9541 if (!preview) {
9542 redisLog(REDIS_DEBUG, "VM: object %p loaded from disk", (void*)vp);
9543 vmMarkPagesFree(vp->page,vp->usedpages);
9544 zfree(vp);
9545 server.vm_stats_swapped_objects--;
9546 } else {
9547 redisLog(REDIS_DEBUG, "VM: object %p previewed from disk", (void*)vp);
9548 }
9549 server.vm_stats_swapins++;
9550 return val;
9551 }
9552
9553 /* Plain object loading, from swap to memory.
9554 *
9555 * 'o' is actually a redisVmPointer structure that will be freed by the call.
9556 * The return value is the loaded object. */
9557 static robj *vmLoadObject(robj *o) {
9558 /* If we are loading the object in background, stop it, we
9559 * need to load this object synchronously ASAP. */
9560 if (o->storage == REDIS_VM_LOADING)
9561 vmCancelThreadedIOJob(o);
9562 return vmGenericLoadObject((vmpointer*)o,0);
9563 }
9564
9565 /* Just load the value on disk, without to modify the key.
9566 * This is useful when we want to perform some operation on the value
9567 * without to really bring it from swap to memory, like while saving the
9568 * dataset or rewriting the append only log. */
9569 static robj *vmPreviewObject(robj *o) {
9570 return vmGenericLoadObject((vmpointer*)o,1);
9571 }
9572
9573 /* How a good candidate is this object for swapping?
9574 * The better candidate it is, the greater the returned value.
9575 *
9576 * Currently we try to perform a fast estimation of the object size in
9577 * memory, and combine it with aging informations.
9578 *
9579 * Basically swappability = idle-time * log(estimated size)
9580 *
9581 * Bigger objects are preferred over smaller objects, but not
9582 * proportionally, this is why we use the logarithm. This algorithm is
9583 * just a first try and will probably be tuned later. */
9584 static double computeObjectSwappability(robj *o) {
9585 /* actual age can be >= minage, but not < minage. As we use wrapping
9586 * 21 bit clocks with minutes resolution for the LRU. */
9587 time_t minage = abs(server.lruclock - o->lru);
9588 long asize = 0;
9589 list *l;
9590 dict *d;
9591 struct dictEntry *de;
9592 int z;
9593
9594 if (minage <= 0) return 0;
9595 switch(o->type) {
9596 case REDIS_STRING:
9597 if (o->encoding != REDIS_ENCODING_RAW) {
9598 asize = sizeof(*o);
9599 } else {
9600 asize = sdslen(o->ptr)+sizeof(*o)+sizeof(long)*2;
9601 }
9602 break;
9603 case REDIS_LIST:
9604 l = o->ptr;
9605 listNode *ln = listFirst(l);
9606
9607 asize = sizeof(list);
9608 if (ln) {
9609 robj *ele = ln->value;
9610 long elesize;
9611
9612 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9613 (sizeof(*o)+sdslen(ele->ptr)) : sizeof(*o);
9614 asize += (sizeof(listNode)+elesize)*listLength(l);
9615 }
9616 break;
9617 case REDIS_SET:
9618 case REDIS_ZSET:
9619 z = (o->type == REDIS_ZSET);
9620 d = z ? ((zset*)o->ptr)->dict : o->ptr;
9621
9622 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9623 if (z) asize += sizeof(zset)-sizeof(dict);
9624 if (dictSize(d)) {
9625 long elesize;
9626 robj *ele;
9627
9628 de = dictGetRandomKey(d);
9629 ele = dictGetEntryKey(de);
9630 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9631 (sizeof(*o)+sdslen(ele->ptr)) : sizeof(*o);
9632 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9633 if (z) asize += sizeof(zskiplistNode)*dictSize(d);
9634 }
9635 break;
9636 case REDIS_HASH:
9637 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
9638 unsigned char *p = zipmapRewind((unsigned char*)o->ptr);
9639 unsigned int len = zipmapLen((unsigned char*)o->ptr);
9640 unsigned int klen, vlen;
9641 unsigned char *key, *val;
9642
9643 if ((p = zipmapNext(p,&key,&klen,&val,&vlen)) == NULL) {
9644 klen = 0;
9645 vlen = 0;
9646 }
9647 asize = len*(klen+vlen+3);
9648 } else if (o->encoding == REDIS_ENCODING_HT) {
9649 d = o->ptr;
9650 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9651 if (dictSize(d)) {
9652 long elesize;
9653 robj *ele;
9654
9655 de = dictGetRandomKey(d);
9656 ele = dictGetEntryKey(de);
9657 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9658 (sizeof(*o)+sdslen(ele->ptr)) : sizeof(*o);
9659 ele = dictGetEntryVal(de);
9660 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9661 (sizeof(*o)+sdslen(ele->ptr)) : sizeof(*o);
9662 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9663 }
9664 }
9665 break;
9666 }
9667 return (double)minage*log(1+asize);
9668 }
9669
9670 /* Try to swap an object that's a good candidate for swapping.
9671 * Returns REDIS_OK if the object was swapped, REDIS_ERR if it's not possible
9672 * to swap any object at all.
9673 *
9674 * If 'usethreaded' is true, Redis will try to swap the object in background
9675 * using I/O threads. */
9676 static int vmSwapOneObject(int usethreads) {
9677 int j, i;
9678 struct dictEntry *best = NULL;
9679 double best_swappability = 0;
9680 redisDb *best_db = NULL;
9681 robj *val;
9682 sds key;
9683
9684 for (j = 0; j < server.dbnum; j++) {
9685 redisDb *db = server.db+j;
9686 /* Why maxtries is set to 100?
9687 * Because this way (usually) we'll find 1 object even if just 1% - 2%
9688 * are swappable objects */
9689 int maxtries = 100;
9690
9691 if (dictSize(db->dict) == 0) continue;
9692 for (i = 0; i < 5; i++) {
9693 dictEntry *de;
9694 double swappability;
9695
9696 if (maxtries) maxtries--;
9697 de = dictGetRandomKey(db->dict);
9698 val = dictGetEntryVal(de);
9699 /* Only swap objects that are currently in memory.
9700 *
9701 * Also don't swap shared objects: not a good idea in general and
9702 * we need to ensure that the main thread does not touch the
9703 * object while the I/O thread is using it, but we can't
9704 * control other keys without adding additional mutex. */
9705 if (val->storage != REDIS_VM_MEMORY || val->refcount != 1) {
9706 if (maxtries) i--; /* don't count this try */
9707 continue;
9708 }
9709 swappability = computeObjectSwappability(val);
9710 if (!best || swappability > best_swappability) {
9711 best = de;
9712 best_swappability = swappability;
9713 best_db = db;
9714 }
9715 }
9716 }
9717 if (best == NULL) return REDIS_ERR;
9718 key = dictGetEntryKey(best);
9719 val = dictGetEntryVal(best);
9720
9721 redisLog(REDIS_DEBUG,"Key with best swappability: %s, %f",
9722 key, best_swappability);
9723
9724 /* Swap it */
9725 if (usethreads) {
9726 robj *keyobj = createStringObject(key,sdslen(key));
9727 vmSwapObjectThreaded(keyobj,val,best_db);
9728 decrRefCount(keyobj);
9729 return REDIS_OK;
9730 } else {
9731 vmpointer *vp;
9732
9733 if ((vp = vmSwapObjectBlocking(val)) != NULL) {
9734 dictGetEntryVal(best) = vp;
9735 return REDIS_OK;
9736 } else {
9737 return REDIS_ERR;
9738 }
9739 }
9740 }
9741
9742 static int vmSwapOneObjectBlocking() {
9743 return vmSwapOneObject(0);
9744 }
9745
9746 static int vmSwapOneObjectThreaded() {
9747 return vmSwapOneObject(1);
9748 }
9749
9750 /* Return true if it's safe to swap out objects in a given moment.
9751 * Basically we don't want to swap objects out while there is a BGSAVE
9752 * or a BGAEOREWRITE running in backgroud. */
9753 static int vmCanSwapOut(void) {
9754 return (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1);
9755 }
9756
9757 /* =================== Virtual Memory - Threaded I/O ======================= */
9758
9759 static void freeIOJob(iojob *j) {
9760 if ((j->type == REDIS_IOJOB_PREPARE_SWAP ||
9761 j->type == REDIS_IOJOB_DO_SWAP ||
9762 j->type == REDIS_IOJOB_LOAD) && j->val != NULL)
9763 {
9764 /* we fix the storage type, otherwise decrRefCount() will try to
9765 * kill the I/O thread Job (that does no longer exists). */
9766 if (j->val->storage == REDIS_VM_SWAPPING)
9767 j->val->storage = REDIS_VM_MEMORY;
9768 decrRefCount(j->val);
9769 }
9770 decrRefCount(j->key);
9771 zfree(j);
9772 }
9773
9774 /* Every time a thread finished a Job, it writes a byte into the write side
9775 * of an unix pipe in order to "awake" the main thread, and this function
9776 * is called. */
9777 static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata,
9778 int mask)
9779 {
9780 char buf[1];
9781 int retval, processed = 0, toprocess = -1, trytoswap = 1;
9782 REDIS_NOTUSED(el);
9783 REDIS_NOTUSED(mask);
9784 REDIS_NOTUSED(privdata);
9785
9786 /* For every byte we read in the read side of the pipe, there is one
9787 * I/O job completed to process. */
9788 while((retval = read(fd,buf,1)) == 1) {
9789 iojob *j;
9790 listNode *ln;
9791 struct dictEntry *de;
9792
9793 redisLog(REDIS_DEBUG,"Processing I/O completed job");
9794
9795 /* Get the processed element (the oldest one) */
9796 lockThreadedIO();
9797 assert(listLength(server.io_processed) != 0);
9798 if (toprocess == -1) {
9799 toprocess = (listLength(server.io_processed)*REDIS_MAX_COMPLETED_JOBS_PROCESSED)/100;
9800 if (toprocess <= 0) toprocess = 1;
9801 }
9802 ln = listFirst(server.io_processed);
9803 j = ln->value;
9804 listDelNode(server.io_processed,ln);
9805 unlockThreadedIO();
9806 /* If this job is marked as canceled, just ignore it */
9807 if (j->canceled) {
9808 freeIOJob(j);
9809 continue;
9810 }
9811 /* Post process it in the main thread, as there are things we
9812 * can do just here to avoid race conditions and/or invasive locks */
9813 redisLog(REDIS_DEBUG,"COMPLETED Job type: %d, ID %p, key: %s", j->type, (void*)j->id, (unsigned char*)j->key->ptr);
9814 de = dictFind(j->db->dict,j->key->ptr);
9815 redisAssert(de != NULL);
9816 if (j->type == REDIS_IOJOB_LOAD) {
9817 redisDb *db;
9818 vmpointer *vp = dictGetEntryVal(de);
9819
9820 /* Key loaded, bring it at home */
9821 vmMarkPagesFree(vp->page,vp->usedpages);
9822 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk (threaded)",
9823 (unsigned char*) j->key->ptr);
9824 server.vm_stats_swapped_objects--;
9825 server.vm_stats_swapins++;
9826 dictGetEntryVal(de) = j->val;
9827 incrRefCount(j->val);
9828 db = j->db;
9829 /* Handle clients waiting for this key to be loaded. */
9830 handleClientsBlockedOnSwappedKey(db,j->key);
9831 freeIOJob(j);
9832 zfree(vp);
9833 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
9834 /* Now we know the amount of pages required to swap this object.
9835 * Let's find some space for it, and queue this task again
9836 * rebranded as REDIS_IOJOB_DO_SWAP. */
9837 if (!vmCanSwapOut() ||
9838 vmFindContiguousPages(&j->page,j->pages) == REDIS_ERR)
9839 {
9840 /* Ooops... no space or we can't swap as there is
9841 * a fork()ed Redis trying to save stuff on disk. */
9842 j->val->storage = REDIS_VM_MEMORY; /* undo operation */
9843 freeIOJob(j);
9844 } else {
9845 /* Note that we need to mark this pages as used now,
9846 * if the job will be canceled, we'll mark them as freed
9847 * again. */
9848 vmMarkPagesUsed(j->page,j->pages);
9849 j->type = REDIS_IOJOB_DO_SWAP;
9850 lockThreadedIO();
9851 queueIOJob(j);
9852 unlockThreadedIO();
9853 }
9854 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
9855 vmpointer *vp;
9856
9857 /* Key swapped. We can finally free some memory. */
9858 if (j->val->storage != REDIS_VM_SWAPPING) {
9859 vmpointer *vp = (vmpointer*) j->id;
9860 printf("storage: %d\n",vp->storage);
9861 printf("key->name: %s\n",(char*)j->key->ptr);
9862 printf("val: %p\n",(void*)j->val);
9863 printf("val->type: %d\n",j->val->type);
9864 printf("val->ptr: %s\n",(char*)j->val->ptr);
9865 }
9866 redisAssert(j->val->storage == REDIS_VM_SWAPPING);
9867 vp = createVmPointer(j->val->type);
9868 vp->page = j->page;
9869 vp->usedpages = j->pages;
9870 dictGetEntryVal(de) = vp;
9871 /* Fix the storage otherwise decrRefCount will attempt to
9872 * remove the associated I/O job */
9873 j->val->storage = REDIS_VM_MEMORY;
9874 decrRefCount(j->val);
9875 redisLog(REDIS_DEBUG,
9876 "VM: object %s swapped out at %lld (%lld pages) (threaded)",
9877 (unsigned char*) j->key->ptr,
9878 (unsigned long long) j->page, (unsigned long long) j->pages);
9879 server.vm_stats_swapped_objects++;
9880 server.vm_stats_swapouts++;
9881 freeIOJob(j);
9882 /* Put a few more swap requests in queue if we are still
9883 * out of memory */
9884 if (trytoswap && vmCanSwapOut() &&
9885 zmalloc_used_memory() > server.vm_max_memory)
9886 {
9887 int more = 1;
9888 while(more) {
9889 lockThreadedIO();
9890 more = listLength(server.io_newjobs) <
9891 (unsigned) server.vm_max_threads;
9892 unlockThreadedIO();
9893 /* Don't waste CPU time if swappable objects are rare. */
9894 if (vmSwapOneObjectThreaded() == REDIS_ERR) {
9895 trytoswap = 0;
9896 break;
9897 }
9898 }
9899 }
9900 }
9901 processed++;
9902 if (processed == toprocess) return;
9903 }
9904 if (retval < 0 && errno != EAGAIN) {
9905 redisLog(REDIS_WARNING,
9906 "WARNING: read(2) error in vmThreadedIOCompletedJob() %s",
9907 strerror(errno));
9908 }
9909 }
9910
9911 static void lockThreadedIO(void) {
9912 pthread_mutex_lock(&server.io_mutex);
9913 }
9914
9915 static void unlockThreadedIO(void) {
9916 pthread_mutex_unlock(&server.io_mutex);
9917 }
9918
9919 /* Remove the specified object from the threaded I/O queue if still not
9920 * processed, otherwise make sure to flag it as canceled. */
9921 static void vmCancelThreadedIOJob(robj *o) {
9922 list *lists[3] = {
9923 server.io_newjobs, /* 0 */
9924 server.io_processing, /* 1 */
9925 server.io_processed /* 2 */
9926 };
9927 int i;
9928
9929 assert(o->storage == REDIS_VM_LOADING || o->storage == REDIS_VM_SWAPPING);
9930 again:
9931 lockThreadedIO();
9932 /* Search for a matching object in one of the queues */
9933 for (i = 0; i < 3; i++) {
9934 listNode *ln;
9935 listIter li;
9936
9937 listRewind(lists[i],&li);
9938 while ((ln = listNext(&li)) != NULL) {
9939 iojob *job = ln->value;
9940
9941 if (job->canceled) continue; /* Skip this, already canceled. */
9942 if (job->id == o) {
9943 redisLog(REDIS_DEBUG,"*** CANCELED %p (key %s) (type %d) (LIST ID %d)\n",
9944 (void*)job, (char*)job->key->ptr, job->type, i);
9945 /* Mark the pages as free since the swap didn't happened
9946 * or happened but is now discarded. */
9947 if (i != 1 && job->type == REDIS_IOJOB_DO_SWAP)
9948 vmMarkPagesFree(job->page,job->pages);
9949 /* Cancel the job. It depends on the list the job is
9950 * living in. */
9951 switch(i) {
9952 case 0: /* io_newjobs */
9953 /* If the job was yet not processed the best thing to do
9954 * is to remove it from the queue at all */
9955 freeIOJob(job);
9956 listDelNode(lists[i],ln);
9957 break;
9958 case 1: /* io_processing */
9959 /* Oh Shi- the thread is messing with the Job:
9960 *
9961 * Probably it's accessing the object if this is a
9962 * PREPARE_SWAP or DO_SWAP job.
9963 * If it's a LOAD job it may be reading from disk and
9964 * if we don't wait for the job to terminate before to
9965 * cancel it, maybe in a few microseconds data can be
9966 * corrupted in this pages. So the short story is:
9967 *
9968 * Better to wait for the job to move into the
9969 * next queue (processed)... */
9970
9971 /* We try again and again until the job is completed. */
9972 unlockThreadedIO();
9973 /* But let's wait some time for the I/O thread
9974 * to finish with this job. After all this condition
9975 * should be very rare. */
9976 usleep(1);
9977 goto again;
9978 case 2: /* io_processed */
9979 /* The job was already processed, that's easy...
9980 * just mark it as canceled so that we'll ignore it
9981 * when processing completed jobs. */
9982 job->canceled = 1;
9983 break;
9984 }
9985 /* Finally we have to adjust the storage type of the object
9986 * in order to "UNDO" the operaiton. */
9987 if (o->storage == REDIS_VM_LOADING)
9988 o->storage = REDIS_VM_SWAPPED;
9989 else if (o->storage == REDIS_VM_SWAPPING)
9990 o->storage = REDIS_VM_MEMORY;
9991 unlockThreadedIO();
9992 redisLog(REDIS_DEBUG,"*** DONE");
9993 return;
9994 }
9995 }
9996 }
9997 unlockThreadedIO();
9998 printf("Not found: %p\n", (void*)o);
9999 redisAssert(1 != 1); /* We should never reach this */
10000 }
10001
10002 static void *IOThreadEntryPoint(void *arg) {
10003 iojob *j;
10004 listNode *ln;
10005 REDIS_NOTUSED(arg);
10006
10007 pthread_detach(pthread_self());
10008 while(1) {
10009 /* Get a new job to process */
10010 lockThreadedIO();
10011 if (listLength(server.io_newjobs) == 0) {
10012 /* No new jobs in queue, exit. */
10013 redisLog(REDIS_DEBUG,"Thread %ld exiting, nothing to do",
10014 (long) pthread_self());
10015 server.io_active_threads--;
10016 unlockThreadedIO();
10017 return NULL;
10018 }
10019 ln = listFirst(server.io_newjobs);
10020 j = ln->value;
10021 listDelNode(server.io_newjobs,ln);
10022 /* Add the job in the processing queue */
10023 j->thread = pthread_self();
10024 listAddNodeTail(server.io_processing,j);
10025 ln = listLast(server.io_processing); /* We use ln later to remove it */
10026 unlockThreadedIO();
10027 redisLog(REDIS_DEBUG,"Thread %ld got a new job (type %d): %p about key '%s'",
10028 (long) pthread_self(), j->type, (void*)j, (char*)j->key->ptr);
10029
10030 /* Process the Job */
10031 if (j->type == REDIS_IOJOB_LOAD) {
10032 vmpointer *vp = (vmpointer*)j->id;
10033 j->val = vmReadObjectFromSwap(j->page,vp->vtype);
10034 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
10035 FILE *fp = fopen("/dev/null","w+");
10036 j->pages = rdbSavedObjectPages(j->val,fp);
10037 fclose(fp);
10038 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
10039 if (vmWriteObjectOnSwap(j->val,j->page) == REDIS_ERR)
10040 j->canceled = 1;
10041 }
10042
10043 /* Done: insert the job into the processed queue */
10044 redisLog(REDIS_DEBUG,"Thread %ld completed the job: %p (key %s)",
10045 (long) pthread_self(), (void*)j, (char*)j->key->ptr);
10046 lockThreadedIO();
10047 listDelNode(server.io_processing,ln);
10048 listAddNodeTail(server.io_processed,j);
10049 unlockThreadedIO();
10050
10051 /* Signal the main thread there is new stuff to process */
10052 assert(write(server.io_ready_pipe_write,"x",1) == 1);
10053 }
10054 return NULL; /* never reached */
10055 }
10056
10057 static void spawnIOThread(void) {
10058 pthread_t thread;
10059 sigset_t mask, omask;
10060 int err;
10061
10062 sigemptyset(&mask);
10063 sigaddset(&mask,SIGCHLD);
10064 sigaddset(&mask,SIGHUP);
10065 sigaddset(&mask,SIGPIPE);
10066 pthread_sigmask(SIG_SETMASK, &mask, &omask);
10067 while ((err = pthread_create(&thread,&server.io_threads_attr,IOThreadEntryPoint,NULL)) != 0) {
10068 redisLog(REDIS_WARNING,"Unable to spawn an I/O thread: %s",
10069 strerror(err));
10070 usleep(1000000);
10071 }
10072 pthread_sigmask(SIG_SETMASK, &omask, NULL);
10073 server.io_active_threads++;
10074 }
10075
10076 /* We need to wait for the last thread to exit before we are able to
10077 * fork() in order to BGSAVE or BGREWRITEAOF. */
10078 static void waitEmptyIOJobsQueue(void) {
10079 while(1) {
10080 int io_processed_len;
10081
10082 lockThreadedIO();
10083 if (listLength(server.io_newjobs) == 0 &&
10084 listLength(server.io_processing) == 0 &&
10085 server.io_active_threads == 0)
10086 {
10087 unlockThreadedIO();
10088 return;
10089 }
10090 /* While waiting for empty jobs queue condition we post-process some
10091 * finshed job, as I/O threads may be hanging trying to write against
10092 * the io_ready_pipe_write FD but there are so much pending jobs that
10093 * it's blocking. */
10094 io_processed_len = listLength(server.io_processed);
10095 unlockThreadedIO();
10096 if (io_processed_len) {
10097 vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read,NULL,0);
10098 usleep(1000); /* 1 millisecond */
10099 } else {
10100 usleep(10000); /* 10 milliseconds */
10101 }
10102 }
10103 }
10104
10105 static void vmReopenSwapFile(void) {
10106 /* Note: we don't close the old one as we are in the child process
10107 * and don't want to mess at all with the original file object. */
10108 server.vm_fp = fopen(server.vm_swap_file,"r+b");
10109 if (server.vm_fp == NULL) {
10110 redisLog(REDIS_WARNING,"Can't re-open the VM swap file: %s. Exiting.",
10111 server.vm_swap_file);
10112 _exit(1);
10113 }
10114 server.vm_fd = fileno(server.vm_fp);
10115 }
10116
10117 /* This function must be called while with threaded IO locked */
10118 static void queueIOJob(iojob *j) {
10119 redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n",
10120 (void*)j, j->type, (char*)j->key->ptr);
10121 listAddNodeTail(server.io_newjobs,j);
10122 if (server.io_active_threads < server.vm_max_threads)
10123 spawnIOThread();
10124 }
10125
10126 static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db) {
10127 iojob *j;
10128
10129 j = zmalloc(sizeof(*j));
10130 j->type = REDIS_IOJOB_PREPARE_SWAP;
10131 j->db = db;
10132 j->key = key;
10133 incrRefCount(key);
10134 j->id = j->val = val;
10135 incrRefCount(val);
10136 j->canceled = 0;
10137 j->thread = (pthread_t) -1;
10138 val->storage = REDIS_VM_SWAPPING;
10139
10140 lockThreadedIO();
10141 queueIOJob(j);
10142 unlockThreadedIO();
10143 return REDIS_OK;
10144 }
10145
10146 /* ============ Virtual Memory - Blocking clients on missing keys =========== */
10147
10148 /* This function makes the clinet 'c' waiting for the key 'key' to be loaded.
10149 * If there is not already a job loading the key, it is craeted.
10150 * The key is added to the io_keys list in the client structure, and also
10151 * in the hash table mapping swapped keys to waiting clients, that is,
10152 * server.io_waited_keys. */
10153 static int waitForSwappedKey(redisClient *c, robj *key) {
10154 struct dictEntry *de;
10155 robj *o;
10156 list *l;
10157
10158 /* If the key does not exist or is already in RAM we don't need to
10159 * block the client at all. */
10160 de = dictFind(c->db->dict,key->ptr);
10161 if (de == NULL) return 0;
10162 o = dictGetEntryVal(de);
10163 if (o->storage == REDIS_VM_MEMORY) {
10164 return 0;
10165 } else if (o->storage == REDIS_VM_SWAPPING) {
10166 /* We were swapping the key, undo it! */
10167 vmCancelThreadedIOJob(o);
10168 return 0;
10169 }
10170
10171 /* OK: the key is either swapped, or being loaded just now. */
10172
10173 /* Add the key to the list of keys this client is waiting for.
10174 * This maps clients to keys they are waiting for. */
10175 listAddNodeTail(c->io_keys,key);
10176 incrRefCount(key);
10177
10178 /* Add the client to the swapped keys => clients waiting map. */
10179 de = dictFind(c->db->io_keys,key);
10180 if (de == NULL) {
10181 int retval;
10182
10183 /* For every key we take a list of clients blocked for it */
10184 l = listCreate();
10185 retval = dictAdd(c->db->io_keys,key,l);
10186 incrRefCount(key);
10187 assert(retval == DICT_OK);
10188 } else {
10189 l = dictGetEntryVal(de);
10190 }
10191 listAddNodeTail(l,c);
10192
10193 /* Are we already loading the key from disk? If not create a job */
10194 if (o->storage == REDIS_VM_SWAPPED) {
10195 iojob *j;
10196 vmpointer *vp = (vmpointer*)o;
10197
10198 o->storage = REDIS_VM_LOADING;
10199 j = zmalloc(sizeof(*j));
10200 j->type = REDIS_IOJOB_LOAD;
10201 j->db = c->db;
10202 j->id = (robj*)vp;
10203 j->key = key;
10204 incrRefCount(key);
10205 j->page = vp->page;
10206 j->val = NULL;
10207 j->canceled = 0;
10208 j->thread = (pthread_t) -1;
10209 lockThreadedIO();
10210 queueIOJob(j);
10211 unlockThreadedIO();
10212 }
10213 return 1;
10214 }
10215
10216 /* Preload keys for any command with first, last and step values for
10217 * the command keys prototype, as defined in the command table. */
10218 static void waitForMultipleSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
10219 int j, last;
10220 if (cmd->vm_firstkey == 0) return;
10221 last = cmd->vm_lastkey;
10222 if (last < 0) last = argc+last;
10223 for (j = cmd->vm_firstkey; j <= last; j += cmd->vm_keystep) {
10224 redisAssert(j < argc);
10225 waitForSwappedKey(c,argv[j]);
10226 }
10227 }
10228
10229 /* Preload keys needed for the ZUNIONSTORE and ZINTERSTORE commands.
10230 * Note that the number of keys to preload is user-defined, so we need to
10231 * apply a sanity check against argc. */
10232 static void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
10233 int i, num;
10234 REDIS_NOTUSED(cmd);
10235
10236 num = atoi(argv[2]->ptr);
10237 if (num > (argc-3)) return;
10238 for (i = 0; i < num; i++) {
10239 waitForSwappedKey(c,argv[3+i]);
10240 }
10241 }
10242
10243 /* Preload keys needed to execute the entire MULTI/EXEC block.
10244 *
10245 * This function is called by blockClientOnSwappedKeys when EXEC is issued,
10246 * and will block the client when any command requires a swapped out value. */
10247 static void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
10248 int i, margc;
10249 struct redisCommand *mcmd;
10250 robj **margv;
10251 REDIS_NOTUSED(cmd);
10252 REDIS_NOTUSED(argc);
10253 REDIS_NOTUSED(argv);
10254
10255 if (!(c->flags & REDIS_MULTI)) return;
10256 for (i = 0; i < c->mstate.count; i++) {
10257 mcmd = c->mstate.commands[i].cmd;
10258 margc = c->mstate.commands[i].argc;
10259 margv = c->mstate.commands[i].argv;
10260
10261 if (mcmd->vm_preload_proc != NULL) {
10262 mcmd->vm_preload_proc(c,mcmd,margc,margv);
10263 } else {
10264 waitForMultipleSwappedKeys(c,mcmd,margc,margv);
10265 }
10266 }
10267 }
10268
10269 /* Is this client attempting to run a command against swapped keys?
10270 * If so, block it ASAP, load the keys in background, then resume it.
10271 *
10272 * The important idea about this function is that it can fail! If keys will
10273 * still be swapped when the client is resumed, this key lookups will
10274 * just block loading keys from disk. In practical terms this should only
10275 * happen with SORT BY command or if there is a bug in this function.
10276 *
10277 * Return 1 if the client is marked as blocked, 0 if the client can
10278 * continue as the keys it is going to access appear to be in memory. */
10279 static int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) {
10280 if (cmd->vm_preload_proc != NULL) {
10281 cmd->vm_preload_proc(c,cmd,c->argc,c->argv);
10282 } else {
10283 waitForMultipleSwappedKeys(c,cmd,c->argc,c->argv);
10284 }
10285
10286 /* If the client was blocked for at least one key, mark it as blocked. */
10287 if (listLength(c->io_keys)) {
10288 c->flags |= REDIS_IO_WAIT;
10289 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
10290 server.vm_blocked_clients++;
10291 return 1;
10292 } else {
10293 return 0;
10294 }
10295 }
10296
10297 /* Remove the 'key' from the list of blocked keys for a given client.
10298 *
10299 * The function returns 1 when there are no longer blocking keys after
10300 * the current one was removed (and the client can be unblocked). */
10301 static int dontWaitForSwappedKey(redisClient *c, robj *key) {
10302 list *l;
10303 listNode *ln;
10304 listIter li;
10305 struct dictEntry *de;
10306
10307 /* Remove the key from the list of keys this client is waiting for. */
10308 listRewind(c->io_keys,&li);
10309 while ((ln = listNext(&li)) != NULL) {
10310 if (equalStringObjects(ln->value,key)) {
10311 listDelNode(c->io_keys,ln);
10312 break;
10313 }
10314 }
10315 assert(ln != NULL);
10316
10317 /* Remove the client form the key => waiting clients map. */
10318 de = dictFind(c->db->io_keys,key);
10319 assert(de != NULL);
10320 l = dictGetEntryVal(de);
10321 ln = listSearchKey(l,c);
10322 assert(ln != NULL);
10323 listDelNode(l,ln);
10324 if (listLength(l) == 0)
10325 dictDelete(c->db->io_keys,key);
10326
10327 return listLength(c->io_keys) == 0;
10328 }
10329
10330 /* Every time we now a key was loaded back in memory, we handle clients
10331 * waiting for this key if any. */
10332 static void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key) {
10333 struct dictEntry *de;
10334 list *l;
10335 listNode *ln;
10336 int len;
10337
10338 de = dictFind(db->io_keys,key);
10339 if (!de) return;
10340
10341 l = dictGetEntryVal(de);
10342 len = listLength(l);
10343 /* Note: we can't use something like while(listLength(l)) as the list
10344 * can be freed by the calling function when we remove the last element. */
10345 while (len--) {
10346 ln = listFirst(l);
10347 redisClient *c = ln->value;
10348
10349 if (dontWaitForSwappedKey(c,key)) {
10350 /* Put the client in the list of clients ready to go as we
10351 * loaded all the keys about it. */
10352 listAddNodeTail(server.io_ready_clients,c);
10353 }
10354 }
10355 }
10356
10357 /* =========================== Remote Configuration ========================= */
10358
10359 static void configSetCommand(redisClient *c) {
10360 robj *o = getDecodedObject(c->argv[3]);
10361 long long ll;
10362
10363 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
10364 zfree(server.dbfilename);
10365 server.dbfilename = zstrdup(o->ptr);
10366 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
10367 zfree(server.requirepass);
10368 server.requirepass = zstrdup(o->ptr);
10369 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
10370 zfree(server.masterauth);
10371 server.masterauth = zstrdup(o->ptr);
10372 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
10373 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
10374 ll < 0) goto badfmt;
10375 server.maxmemory = ll;
10376 } else if (!strcasecmp(c->argv[2]->ptr,"timeout")) {
10377 if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
10378 ll < 0 || ll > LONG_MAX) goto badfmt;
10379 server.maxidletime = ll;
10380 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
10381 if (!strcasecmp(o->ptr,"no")) {
10382 server.appendfsync = APPENDFSYNC_NO;
10383 } else if (!strcasecmp(o->ptr,"everysec")) {
10384 server.appendfsync = APPENDFSYNC_EVERYSEC;
10385 } else if (!strcasecmp(o->ptr,"always")) {
10386 server.appendfsync = APPENDFSYNC_ALWAYS;
10387 } else {
10388 goto badfmt;
10389 }
10390 } else if (!strcasecmp(c->argv[2]->ptr,"no-appendfsync-on-rewrite")) {
10391 int yn = yesnotoi(o->ptr);
10392
10393 if (yn == -1) goto badfmt;
10394 server.no_appendfsync_on_rewrite = yn;
10395 } else if (!strcasecmp(c->argv[2]->ptr,"appendonly")) {
10396 int old = server.appendonly;
10397 int new = yesnotoi(o->ptr);
10398
10399 if (new == -1) goto badfmt;
10400 if (old != new) {
10401 if (new == 0) {
10402 stopAppendOnly();
10403 } else {
10404 if (startAppendOnly() == REDIS_ERR) {
10405 addReplySds(c,sdscatprintf(sdsempty(),
10406 "-ERR Unable to turn on AOF. Check server logs.\r\n"));
10407 decrRefCount(o);
10408 return;
10409 }
10410 }
10411 }
10412 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
10413 int vlen, j;
10414 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
10415
10416 /* Perform sanity check before setting the new config:
10417 * - Even number of args
10418 * - Seconds >= 1, changes >= 0 */
10419 if (vlen & 1) {
10420 sdsfreesplitres(v,vlen);
10421 goto badfmt;
10422 }
10423 for (j = 0; j < vlen; j++) {
10424 char *eptr;
10425 long val;
10426
10427 val = strtoll(v[j], &eptr, 10);
10428 if (eptr[0] != '\0' ||
10429 ((j & 1) == 0 && val < 1) ||
10430 ((j & 1) == 1 && val < 0)) {
10431 sdsfreesplitres(v,vlen);
10432 goto badfmt;
10433 }
10434 }
10435 /* Finally set the new config */
10436 resetServerSaveParams();
10437 for (j = 0; j < vlen; j += 2) {
10438 time_t seconds;
10439 int changes;
10440
10441 seconds = strtoll(v[j],NULL,10);
10442 changes = strtoll(v[j+1],NULL,10);
10443 appendServerSaveParams(seconds, changes);
10444 }
10445 sdsfreesplitres(v,vlen);
10446 } else {
10447 addReplySds(c,sdscatprintf(sdsempty(),
10448 "-ERR not supported CONFIG parameter %s\r\n",
10449 (char*)c->argv[2]->ptr));
10450 decrRefCount(o);
10451 return;
10452 }
10453 decrRefCount(o);
10454 addReply(c,shared.ok);
10455 return;
10456
10457 badfmt: /* Bad format errors */
10458 addReplySds(c,sdscatprintf(sdsempty(),
10459 "-ERR invalid argument '%s' for CONFIG SET '%s'\r\n",
10460 (char*)o->ptr,
10461 (char*)c->argv[2]->ptr));
10462 decrRefCount(o);
10463 }
10464
10465 static void configGetCommand(redisClient *c) {
10466 robj *o = getDecodedObject(c->argv[2]);
10467 robj *lenobj = createObject(REDIS_STRING,NULL);
10468 char *pattern = o->ptr;
10469 int matches = 0;
10470
10471 addReply(c,lenobj);
10472 decrRefCount(lenobj);
10473
10474 if (stringmatch(pattern,"dbfilename",0)) {
10475 addReplyBulkCString(c,"dbfilename");
10476 addReplyBulkCString(c,server.dbfilename);
10477 matches++;
10478 }
10479 if (stringmatch(pattern,"requirepass",0)) {
10480 addReplyBulkCString(c,"requirepass");
10481 addReplyBulkCString(c,server.requirepass);
10482 matches++;
10483 }
10484 if (stringmatch(pattern,"masterauth",0)) {
10485 addReplyBulkCString(c,"masterauth");
10486 addReplyBulkCString(c,server.masterauth);
10487 matches++;
10488 }
10489 if (stringmatch(pattern,"maxmemory",0)) {
10490 char buf[128];
10491
10492 ll2string(buf,128,server.maxmemory);
10493 addReplyBulkCString(c,"maxmemory");
10494 addReplyBulkCString(c,buf);
10495 matches++;
10496 }
10497 if (stringmatch(pattern,"timeout",0)) {
10498 char buf[128];
10499
10500 ll2string(buf,128,server.maxidletime);
10501 addReplyBulkCString(c,"timeout");
10502 addReplyBulkCString(c,buf);
10503 matches++;
10504 }
10505 if (stringmatch(pattern,"appendonly",0)) {
10506 addReplyBulkCString(c,"appendonly");
10507 addReplyBulkCString(c,server.appendonly ? "yes" : "no");
10508 matches++;
10509 }
10510 if (stringmatch(pattern,"no-appendfsync-on-rewrite",0)) {
10511 addReplyBulkCString(c,"no-appendfsync-on-rewrite");
10512 addReplyBulkCString(c,server.no_appendfsync_on_rewrite ? "yes" : "no");
10513 matches++;
10514 }
10515 if (stringmatch(pattern,"appendfsync",0)) {
10516 char *policy;
10517
10518 switch(server.appendfsync) {
10519 case APPENDFSYNC_NO: policy = "no"; break;
10520 case APPENDFSYNC_EVERYSEC: policy = "everysec"; break;
10521 case APPENDFSYNC_ALWAYS: policy = "always"; break;
10522 default: policy = "unknown"; break; /* too harmless to panic */
10523 }
10524 addReplyBulkCString(c,"appendfsync");
10525 addReplyBulkCString(c,policy);
10526 matches++;
10527 }
10528 if (stringmatch(pattern,"save",0)) {
10529 sds buf = sdsempty();
10530 int j;
10531
10532 for (j = 0; j < server.saveparamslen; j++) {
10533 buf = sdscatprintf(buf,"%ld %d",
10534 server.saveparams[j].seconds,
10535 server.saveparams[j].changes);
10536 if (j != server.saveparamslen-1)
10537 buf = sdscatlen(buf," ",1);
10538 }
10539 addReplyBulkCString(c,"save");
10540 addReplyBulkCString(c,buf);
10541 sdsfree(buf);
10542 matches++;
10543 }
10544 decrRefCount(o);
10545 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",matches*2);
10546 }
10547
10548 static void configCommand(redisClient *c) {
10549 if (!strcasecmp(c->argv[1]->ptr,"set")) {
10550 if (c->argc != 4) goto badarity;
10551 configSetCommand(c);
10552 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
10553 if (c->argc != 3) goto badarity;
10554 configGetCommand(c);
10555 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
10556 if (c->argc != 2) goto badarity;
10557 server.stat_numcommands = 0;
10558 server.stat_numconnections = 0;
10559 server.stat_expiredkeys = 0;
10560 server.stat_starttime = time(NULL);
10561 addReply(c,shared.ok);
10562 } else {
10563 addReplySds(c,sdscatprintf(sdsempty(),
10564 "-ERR CONFIG subcommand must be one of GET, SET, RESETSTAT\r\n"));
10565 }
10566 return;
10567
10568 badarity:
10569 addReplySds(c,sdscatprintf(sdsempty(),
10570 "-ERR Wrong number of arguments for CONFIG %s\r\n",
10571 (char*) c->argv[1]->ptr));
10572 }
10573
10574 /* =========================== Pubsub implementation ======================== */
10575
10576 static void freePubsubPattern(void *p) {
10577 pubsubPattern *pat = p;
10578
10579 decrRefCount(pat->pattern);
10580 zfree(pat);
10581 }
10582
10583 static int listMatchPubsubPattern(void *a, void *b) {
10584 pubsubPattern *pa = a, *pb = b;
10585
10586 return (pa->client == pb->client) &&
10587 (equalStringObjects(pa->pattern,pb->pattern));
10588 }
10589
10590 /* Subscribe a client to a channel. Returns 1 if the operation succeeded, or
10591 * 0 if the client was already subscribed to that channel. */
10592 static int pubsubSubscribeChannel(redisClient *c, robj *channel) {
10593 struct dictEntry *de;
10594 list *clients = NULL;
10595 int retval = 0;
10596
10597 /* Add the channel to the client -> channels hash table */
10598 if (dictAdd(c->pubsub_channels,channel,NULL) == DICT_OK) {
10599 retval = 1;
10600 incrRefCount(channel);
10601 /* Add the client to the channel -> list of clients hash table */
10602 de = dictFind(server.pubsub_channels,channel);
10603 if (de == NULL) {
10604 clients = listCreate();
10605 dictAdd(server.pubsub_channels,channel,clients);
10606 incrRefCount(channel);
10607 } else {
10608 clients = dictGetEntryVal(de);
10609 }
10610 listAddNodeTail(clients,c);
10611 }
10612 /* Notify the client */
10613 addReply(c,shared.mbulk3);
10614 addReply(c,shared.subscribebulk);
10615 addReplyBulk(c,channel);
10616 addReplyLongLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
10617 return retval;
10618 }
10619
10620 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10621 * 0 if the client was not subscribed to the specified channel. */
10622 static int pubsubUnsubscribeChannel(redisClient *c, robj *channel, int notify) {
10623 struct dictEntry *de;
10624 list *clients;
10625 listNode *ln;
10626 int retval = 0;
10627
10628 /* Remove the channel from the client -> channels hash table */
10629 incrRefCount(channel); /* channel may be just a pointer to the same object
10630 we have in the hash tables. Protect it... */
10631 if (dictDelete(c->pubsub_channels,channel) == DICT_OK) {
10632 retval = 1;
10633 /* Remove the client from the channel -> clients list hash table */
10634 de = dictFind(server.pubsub_channels,channel);
10635 assert(de != NULL);
10636 clients = dictGetEntryVal(de);
10637 ln = listSearchKey(clients,c);
10638 assert(ln != NULL);
10639 listDelNode(clients,ln);
10640 if (listLength(clients) == 0) {
10641 /* Free the list and associated hash entry at all if this was
10642 * the latest client, so that it will be possible to abuse
10643 * Redis PUBSUB creating millions of channels. */
10644 dictDelete(server.pubsub_channels,channel);
10645 }
10646 }
10647 /* Notify the client */
10648 if (notify) {
10649 addReply(c,shared.mbulk3);
10650 addReply(c,shared.unsubscribebulk);
10651 addReplyBulk(c,channel);
10652 addReplyLongLong(c,dictSize(c->pubsub_channels)+
10653 listLength(c->pubsub_patterns));
10654
10655 }
10656 decrRefCount(channel); /* it is finally safe to release it */
10657 return retval;
10658 }
10659
10660 /* Subscribe a client to a pattern. Returns 1 if the operation succeeded, or 0 if the clinet was already subscribed to that pattern. */
10661 static int pubsubSubscribePattern(redisClient *c, robj *pattern) {
10662 int retval = 0;
10663
10664 if (listSearchKey(c->pubsub_patterns,pattern) == NULL) {
10665 retval = 1;
10666 pubsubPattern *pat;
10667 listAddNodeTail(c->pubsub_patterns,pattern);
10668 incrRefCount(pattern);
10669 pat = zmalloc(sizeof(*pat));
10670 pat->pattern = getDecodedObject(pattern);
10671 pat->client = c;
10672 listAddNodeTail(server.pubsub_patterns,pat);
10673 }
10674 /* Notify the client */
10675 addReply(c,shared.mbulk3);
10676 addReply(c,shared.psubscribebulk);
10677 addReplyBulk(c,pattern);
10678 addReplyLongLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
10679 return retval;
10680 }
10681
10682 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10683 * 0 if the client was not subscribed to the specified channel. */
10684 static int pubsubUnsubscribePattern(redisClient *c, robj *pattern, int notify) {
10685 listNode *ln;
10686 pubsubPattern pat;
10687 int retval = 0;
10688
10689 incrRefCount(pattern); /* Protect the object. May be the same we remove */
10690 if ((ln = listSearchKey(c->pubsub_patterns,pattern)) != NULL) {
10691 retval = 1;
10692 listDelNode(c->pubsub_patterns,ln);
10693 pat.client = c;
10694 pat.pattern = pattern;
10695 ln = listSearchKey(server.pubsub_patterns,&pat);
10696 listDelNode(server.pubsub_patterns,ln);
10697 }
10698 /* Notify the client */
10699 if (notify) {
10700 addReply(c,shared.mbulk3);
10701 addReply(c,shared.punsubscribebulk);
10702 addReplyBulk(c,pattern);
10703 addReplyLongLong(c,dictSize(c->pubsub_channels)+
10704 listLength(c->pubsub_patterns));
10705 }
10706 decrRefCount(pattern);
10707 return retval;
10708 }
10709
10710 /* Unsubscribe from all the channels. Return the number of channels the
10711 * client was subscribed from. */
10712 static int pubsubUnsubscribeAllChannels(redisClient *c, int notify) {
10713 dictIterator *di = dictGetIterator(c->pubsub_channels);
10714 dictEntry *de;
10715 int count = 0;
10716
10717 while((de = dictNext(di)) != NULL) {
10718 robj *channel = dictGetEntryKey(de);
10719
10720 count += pubsubUnsubscribeChannel(c,channel,notify);
10721 }
10722 dictReleaseIterator(di);
10723 return count;
10724 }
10725
10726 /* Unsubscribe from all the patterns. Return the number of patterns the
10727 * client was subscribed from. */
10728 static int pubsubUnsubscribeAllPatterns(redisClient *c, int notify) {
10729 listNode *ln;
10730 listIter li;
10731 int count = 0;
10732
10733 listRewind(c->pubsub_patterns,&li);
10734 while ((ln = listNext(&li)) != NULL) {
10735 robj *pattern = ln->value;
10736
10737 count += pubsubUnsubscribePattern(c,pattern,notify);
10738 }
10739 return count;
10740 }
10741
10742 /* Publish a message */
10743 static int pubsubPublishMessage(robj *channel, robj *message) {
10744 int receivers = 0;
10745 struct dictEntry *de;
10746 listNode *ln;
10747 listIter li;
10748
10749 /* Send to clients listening for that channel */
10750 de = dictFind(server.pubsub_channels,channel);
10751 if (de) {
10752 list *list = dictGetEntryVal(de);
10753 listNode *ln;
10754 listIter li;
10755
10756 listRewind(list,&li);
10757 while ((ln = listNext(&li)) != NULL) {
10758 redisClient *c = ln->value;
10759
10760 addReply(c,shared.mbulk3);
10761 addReply(c,shared.messagebulk);
10762 addReplyBulk(c,channel);
10763 addReplyBulk(c,message);
10764 receivers++;
10765 }
10766 }
10767 /* Send to clients listening to matching channels */
10768 if (listLength(server.pubsub_patterns)) {
10769 listRewind(server.pubsub_patterns,&li);
10770 channel = getDecodedObject(channel);
10771 while ((ln = listNext(&li)) != NULL) {
10772 pubsubPattern *pat = ln->value;
10773
10774 if (stringmatchlen((char*)pat->pattern->ptr,
10775 sdslen(pat->pattern->ptr),
10776 (char*)channel->ptr,
10777 sdslen(channel->ptr),0)) {
10778 addReply(pat->client,shared.mbulk4);
10779 addReply(pat->client,shared.pmessagebulk);
10780 addReplyBulk(pat->client,pat->pattern);
10781 addReplyBulk(pat->client,channel);
10782 addReplyBulk(pat->client,message);
10783 receivers++;
10784 }
10785 }
10786 decrRefCount(channel);
10787 }
10788 return receivers;
10789 }
10790
10791 static void subscribeCommand(redisClient *c) {
10792 int j;
10793
10794 for (j = 1; j < c->argc; j++)
10795 pubsubSubscribeChannel(c,c->argv[j]);
10796 }
10797
10798 static void unsubscribeCommand(redisClient *c) {
10799 if (c->argc == 1) {
10800 pubsubUnsubscribeAllChannels(c,1);
10801 return;
10802 } else {
10803 int j;
10804
10805 for (j = 1; j < c->argc; j++)
10806 pubsubUnsubscribeChannel(c,c->argv[j],1);
10807 }
10808 }
10809
10810 static void psubscribeCommand(redisClient *c) {
10811 int j;
10812
10813 for (j = 1; j < c->argc; j++)
10814 pubsubSubscribePattern(c,c->argv[j]);
10815 }
10816
10817 static void punsubscribeCommand(redisClient *c) {
10818 if (c->argc == 1) {
10819 pubsubUnsubscribeAllPatterns(c,1);
10820 return;
10821 } else {
10822 int j;
10823
10824 for (j = 1; j < c->argc; j++)
10825 pubsubUnsubscribePattern(c,c->argv[j],1);
10826 }
10827 }
10828
10829 static void publishCommand(redisClient *c) {
10830 int receivers = pubsubPublishMessage(c->argv[1],c->argv[2]);
10831 addReplyLongLong(c,receivers);
10832 }
10833
10834 /* ===================== WATCH (CAS alike for MULTI/EXEC) ===================
10835 *
10836 * The implementation uses a per-DB hash table mapping keys to list of clients
10837 * WATCHing those keys, so that given a key that is going to be modified
10838 * we can mark all the associated clients as dirty.
10839 *
10840 * Also every client contains a list of WATCHed keys so that's possible to
10841 * un-watch such keys when the client is freed or when UNWATCH is called. */
10842
10843 /* In the client->watched_keys list we need to use watchedKey structures
10844 * as in order to identify a key in Redis we need both the key name and the
10845 * DB */
10846 typedef struct watchedKey {
10847 robj *key;
10848 redisDb *db;
10849 } watchedKey;
10850
10851 /* Watch for the specified key */
10852 static void watchForKey(redisClient *c, robj *key) {
10853 list *clients = NULL;
10854 listIter li;
10855 listNode *ln;
10856 watchedKey *wk;
10857
10858 /* Check if we are already watching for this key */
10859 listRewind(c->watched_keys,&li);
10860 while((ln = listNext(&li))) {
10861 wk = listNodeValue(ln);
10862 if (wk->db == c->db && equalStringObjects(key,wk->key))
10863 return; /* Key already watched */
10864 }
10865 /* This key is not already watched in this DB. Let's add it */
10866 clients = dictFetchValue(c->db->watched_keys,key);
10867 if (!clients) {
10868 clients = listCreate();
10869 dictAdd(c->db->watched_keys,key,clients);
10870 incrRefCount(key);
10871 }
10872 listAddNodeTail(clients,c);
10873 /* Add the new key to the lits of keys watched by this client */
10874 wk = zmalloc(sizeof(*wk));
10875 wk->key = key;
10876 wk->db = c->db;
10877 incrRefCount(key);
10878 listAddNodeTail(c->watched_keys,wk);
10879 }
10880
10881 /* Unwatch all the keys watched by this client. To clean the EXEC dirty
10882 * flag is up to the caller. */
10883 static void unwatchAllKeys(redisClient *c) {
10884 listIter li;
10885 listNode *ln;
10886
10887 if (listLength(c->watched_keys) == 0) return;
10888 listRewind(c->watched_keys,&li);
10889 while((ln = listNext(&li))) {
10890 list *clients;
10891 watchedKey *wk;
10892
10893 /* Lookup the watched key -> clients list and remove the client
10894 * from the list */
10895 wk = listNodeValue(ln);
10896 clients = dictFetchValue(wk->db->watched_keys, wk->key);
10897 assert(clients != NULL);
10898 listDelNode(clients,listSearchKey(clients,c));
10899 /* Kill the entry at all if this was the only client */
10900 if (listLength(clients) == 0)
10901 dictDelete(wk->db->watched_keys, wk->key);
10902 /* Remove this watched key from the client->watched list */
10903 listDelNode(c->watched_keys,ln);
10904 decrRefCount(wk->key);
10905 zfree(wk);
10906 }
10907 }
10908
10909 /* "Touch" a key, so that if this key is being WATCHed by some client the
10910 * next EXEC will fail. */
10911 static void touchWatchedKey(redisDb *db, robj *key) {
10912 list *clients;
10913 listIter li;
10914 listNode *ln;
10915
10916 if (dictSize(db->watched_keys) == 0) return;
10917 clients = dictFetchValue(db->watched_keys, key);
10918 if (!clients) return;
10919
10920 /* Mark all the clients watching this key as REDIS_DIRTY_CAS */
10921 /* Check if we are already watching for this key */
10922 listRewind(clients,&li);
10923 while((ln = listNext(&li))) {
10924 redisClient *c = listNodeValue(ln);
10925
10926 c->flags |= REDIS_DIRTY_CAS;
10927 }
10928 }
10929
10930 /* On FLUSHDB or FLUSHALL all the watched keys that are present before the
10931 * flush but will be deleted as effect of the flushing operation should
10932 * be touched. "dbid" is the DB that's getting the flush. -1 if it is
10933 * a FLUSHALL operation (all the DBs flushed). */
10934 static void touchWatchedKeysOnFlush(int dbid) {
10935 listIter li1, li2;
10936 listNode *ln;
10937
10938 /* For every client, check all the waited keys */
10939 listRewind(server.clients,&li1);
10940 while((ln = listNext(&li1))) {
10941 redisClient *c = listNodeValue(ln);
10942 listRewind(c->watched_keys,&li2);
10943 while((ln = listNext(&li2))) {
10944 watchedKey *wk = listNodeValue(ln);
10945
10946 /* For every watched key matching the specified DB, if the
10947 * key exists, mark the client as dirty, as the key will be
10948 * removed. */
10949 if (dbid == -1 || wk->db->id == dbid) {
10950 if (dictFind(wk->db->dict, wk->key->ptr) != NULL)
10951 c->flags |= REDIS_DIRTY_CAS;
10952 }
10953 }
10954 }
10955 }
10956
10957 static void watchCommand(redisClient *c) {
10958 int j;
10959
10960 if (c->flags & REDIS_MULTI) {
10961 addReplySds(c,sdsnew("-ERR WATCH inside MULTI is not allowed\r\n"));
10962 return;
10963 }
10964 for (j = 1; j < c->argc; j++)
10965 watchForKey(c,c->argv[j]);
10966 addReply(c,shared.ok);
10967 }
10968
10969 static void unwatchCommand(redisClient *c) {
10970 unwatchAllKeys(c);
10971 c->flags &= (~REDIS_DIRTY_CAS);
10972 addReply(c,shared.ok);
10973 }
10974
10975 /* ================================= Debugging ============================== */
10976
10977 /* Compute the sha1 of string at 's' with 'len' bytes long.
10978 * The SHA1 is then xored againt the string pointed by digest.
10979 * Since xor is commutative, this operation is used in order to
10980 * "add" digests relative to unordered elements.
10981 *
10982 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
10983 static void xorDigest(unsigned char *digest, void *ptr, size_t len) {
10984 SHA1_CTX ctx;
10985 unsigned char hash[20], *s = ptr;
10986 int j;
10987
10988 SHA1Init(&ctx);
10989 SHA1Update(&ctx,s,len);
10990 SHA1Final(hash,&ctx);
10991
10992 for (j = 0; j < 20; j++)
10993 digest[j] ^= hash[j];
10994 }
10995
10996 static void xorObjectDigest(unsigned char *digest, robj *o) {
10997 o = getDecodedObject(o);
10998 xorDigest(digest,o->ptr,sdslen(o->ptr));
10999 decrRefCount(o);
11000 }
11001
11002 /* This function instead of just computing the SHA1 and xoring it
11003 * against diget, also perform the digest of "digest" itself and
11004 * replace the old value with the new one.
11005 *
11006 * So the final digest will be:
11007 *
11008 * digest = SHA1(digest xor SHA1(data))
11009 *
11010 * This function is used every time we want to preserve the order so
11011 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
11012 *
11013 * Also note that mixdigest("foo") followed by mixdigest("bar")
11014 * will lead to a different digest compared to "fo", "obar".
11015 */
11016 static void mixDigest(unsigned char *digest, void *ptr, size_t len) {
11017 SHA1_CTX ctx;
11018 char *s = ptr;
11019
11020 xorDigest(digest,s,len);
11021 SHA1Init(&ctx);
11022 SHA1Update(&ctx,digest,20);
11023 SHA1Final(digest,&ctx);
11024 }
11025
11026 static void mixObjectDigest(unsigned char *digest, robj *o) {
11027 o = getDecodedObject(o);
11028 mixDigest(digest,o->ptr,sdslen(o->ptr));
11029 decrRefCount(o);
11030 }
11031
11032 /* Compute the dataset digest. Since keys, sets elements, hashes elements
11033 * are not ordered, we use a trick: every aggregate digest is the xor
11034 * of the digests of their elements. This way the order will not change
11035 * the result. For list instead we use a feedback entering the output digest
11036 * as input in order to ensure that a different ordered list will result in
11037 * a different digest. */
11038 static void computeDatasetDigest(unsigned char *final) {
11039 unsigned char digest[20];
11040 char buf[128];
11041 dictIterator *di = NULL;
11042 dictEntry *de;
11043 int j;
11044 uint32_t aux;
11045
11046 memset(final,0,20); /* Start with a clean result */
11047
11048 for (j = 0; j < server.dbnum; j++) {
11049 redisDb *db = server.db+j;
11050
11051 if (dictSize(db->dict) == 0) continue;
11052 di = dictGetIterator(db->dict);
11053
11054 /* hash the DB id, so the same dataset moved in a different
11055 * DB will lead to a different digest */
11056 aux = htonl(j);
11057 mixDigest(final,&aux,sizeof(aux));
11058
11059 /* Iterate this DB writing every entry */
11060 while((de = dictNext(di)) != NULL) {
11061 sds key;
11062 robj *keyobj, *o;
11063 time_t expiretime;
11064
11065 memset(digest,0,20); /* This key-val digest */
11066 key = dictGetEntryKey(de);
11067 keyobj = createStringObject(key,sdslen(key));
11068
11069 mixDigest(digest,key,sdslen(key));
11070
11071 /* Make sure the key is loaded if VM is active */
11072 o = lookupKeyRead(db,keyobj);
11073
11074 aux = htonl(o->type);
11075 mixDigest(digest,&aux,sizeof(aux));
11076 expiretime = getExpire(db,keyobj);
11077
11078 /* Save the key and associated value */
11079 if (o->type == REDIS_STRING) {
11080 mixObjectDigest(digest,o);
11081 } else if (o->type == REDIS_LIST) {
11082 listTypeIterator *li = listTypeInitIterator(o,0,REDIS_TAIL);
11083 listTypeEntry entry;
11084 while(listTypeNext(li,&entry)) {
11085 robj *eleobj = listTypeGet(&entry);
11086 mixObjectDigest(digest,eleobj);
11087 decrRefCount(eleobj);
11088 }
11089 listTypeReleaseIterator(li);
11090 } else if (o->type == REDIS_SET) {
11091 dict *set = o->ptr;
11092 dictIterator *di = dictGetIterator(set);
11093 dictEntry *de;
11094
11095 while((de = dictNext(di)) != NULL) {
11096 robj *eleobj = dictGetEntryKey(de);
11097
11098 xorObjectDigest(digest,eleobj);
11099 }
11100 dictReleaseIterator(di);
11101 } else if (o->type == REDIS_ZSET) {
11102 zset *zs = o->ptr;
11103 dictIterator *di = dictGetIterator(zs->dict);
11104 dictEntry *de;
11105
11106 while((de = dictNext(di)) != NULL) {
11107 robj *eleobj = dictGetEntryKey(de);
11108 double *score = dictGetEntryVal(de);
11109 unsigned char eledigest[20];
11110
11111 snprintf(buf,sizeof(buf),"%.17g",*score);
11112 memset(eledigest,0,20);
11113 mixObjectDigest(eledigest,eleobj);
11114 mixDigest(eledigest,buf,strlen(buf));
11115 xorDigest(digest,eledigest,20);
11116 }
11117 dictReleaseIterator(di);
11118 } else if (o->type == REDIS_HASH) {
11119 hashTypeIterator *hi;
11120 robj *obj;
11121
11122 hi = hashTypeInitIterator(o);
11123 while (hashTypeNext(hi) != REDIS_ERR) {
11124 unsigned char eledigest[20];
11125
11126 memset(eledigest,0,20);
11127 obj = hashTypeCurrent(hi,REDIS_HASH_KEY);
11128 mixObjectDigest(eledigest,obj);
11129 decrRefCount(obj);
11130 obj = hashTypeCurrent(hi,REDIS_HASH_VALUE);
11131 mixObjectDigest(eledigest,obj);
11132 decrRefCount(obj);
11133 xorDigest(digest,eledigest,20);
11134 }
11135 hashTypeReleaseIterator(hi);
11136 } else {
11137 redisPanic("Unknown object type");
11138 }
11139 /* If the key has an expire, add it to the mix */
11140 if (expiretime != -1) xorDigest(digest,"!!expire!!",10);
11141 /* We can finally xor the key-val digest to the final digest */
11142 xorDigest(final,digest,20);
11143 decrRefCount(keyobj);
11144 }
11145 dictReleaseIterator(di);
11146 }
11147 }
11148
11149 static void debugCommand(redisClient *c) {
11150 if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
11151 *((char*)-1) = 'x';
11152 } else if (!strcasecmp(c->argv[1]->ptr,"reload")) {
11153 if (rdbSave(server.dbfilename) != REDIS_OK) {
11154 addReply(c,shared.err);
11155 return;
11156 }
11157 emptyDb();
11158 if (rdbLoad(server.dbfilename) != REDIS_OK) {
11159 addReply(c,shared.err);
11160 return;
11161 }
11162 redisLog(REDIS_WARNING,"DB reloaded by DEBUG RELOAD");
11163 addReply(c,shared.ok);
11164 } else if (!strcasecmp(c->argv[1]->ptr,"loadaof")) {
11165 emptyDb();
11166 if (loadAppendOnlyFile(server.appendfilename) != REDIS_OK) {
11167 addReply(c,shared.err);
11168 return;
11169 }
11170 redisLog(REDIS_WARNING,"Append Only File loaded by DEBUG LOADAOF");
11171 addReply(c,shared.ok);
11172 } else if (!strcasecmp(c->argv[1]->ptr,"object") && c->argc == 3) {
11173 dictEntry *de = dictFind(c->db->dict,c->argv[2]->ptr);
11174 robj *val;
11175
11176 if (!de) {
11177 addReply(c,shared.nokeyerr);
11178 return;
11179 }
11180 val = dictGetEntryVal(de);
11181 if (!server.vm_enabled || (val->storage == REDIS_VM_MEMORY ||
11182 val->storage == REDIS_VM_SWAPPING)) {
11183 char *strenc;
11184 char buf[128];
11185
11186 if (val->encoding < (sizeof(strencoding)/sizeof(char*))) {
11187 strenc = strencoding[val->encoding];
11188 } else {
11189 snprintf(buf,64,"unknown encoding %d\n", val->encoding);
11190 strenc = buf;
11191 }
11192 addReplySds(c,sdscatprintf(sdsempty(),
11193 "+Value at:%p refcount:%d "
11194 "encoding:%s serializedlength:%lld\r\n",
11195 (void*)val, val->refcount,
11196 strenc, (long long) rdbSavedObjectLen(val,NULL)));
11197 } else {
11198 vmpointer *vp = (vmpointer*) val;
11199 addReplySds(c,sdscatprintf(sdsempty(),
11200 "+Value swapped at: page %llu "
11201 "using %llu pages\r\n",
11202 (unsigned long long) vp->page,
11203 (unsigned long long) vp->usedpages));
11204 }
11205 } else if (!strcasecmp(c->argv[1]->ptr,"swapin") && c->argc == 3) {
11206 lookupKeyRead(c->db,c->argv[2]);
11207 addReply(c,shared.ok);
11208 } else if (!strcasecmp(c->argv[1]->ptr,"swapout") && c->argc == 3) {
11209 dictEntry *de = dictFind(c->db->dict,c->argv[2]->ptr);
11210 robj *val;
11211 vmpointer *vp;
11212
11213 if (!server.vm_enabled) {
11214 addReplySds(c,sdsnew("-ERR Virtual Memory is disabled\r\n"));
11215 return;
11216 }
11217 if (!de) {
11218 addReply(c,shared.nokeyerr);
11219 return;
11220 }
11221 val = dictGetEntryVal(de);
11222 /* Swap it */
11223 if (val->storage != REDIS_VM_MEMORY) {
11224 addReplySds(c,sdsnew("-ERR This key is not in memory\r\n"));
11225 } else if (val->refcount != 1) {
11226 addReplySds(c,sdsnew("-ERR Object is shared\r\n"));
11227 } else if ((vp = vmSwapObjectBlocking(val)) != NULL) {
11228 dictGetEntryVal(de) = vp;
11229 addReply(c,shared.ok);
11230 } else {
11231 addReply(c,shared.err);
11232 }
11233 } else if (!strcasecmp(c->argv[1]->ptr,"populate") && c->argc == 3) {
11234 long keys, j;
11235 robj *key, *val;
11236 char buf[128];
11237
11238 if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != REDIS_OK)
11239 return;
11240 for (j = 0; j < keys; j++) {
11241 snprintf(buf,sizeof(buf),"key:%lu",j);
11242 key = createStringObject(buf,strlen(buf));
11243 if (lookupKeyRead(c->db,key) != NULL) {
11244 decrRefCount(key);
11245 continue;
11246 }
11247 snprintf(buf,sizeof(buf),"value:%lu",j);
11248 val = createStringObject(buf,strlen(buf));
11249 dbAdd(c->db,key,val);
11250 decrRefCount(key);
11251 }
11252 addReply(c,shared.ok);
11253 } else if (!strcasecmp(c->argv[1]->ptr,"digest") && c->argc == 2) {
11254 unsigned char digest[20];
11255 sds d = sdsnew("+");
11256 int j;
11257
11258 computeDatasetDigest(digest);
11259 for (j = 0; j < 20; j++)
11260 d = sdscatprintf(d, "%02x",digest[j]);
11261
11262 d = sdscatlen(d,"\r\n",2);
11263 addReplySds(c,d);
11264 } else {
11265 addReplySds(c,sdsnew(
11266 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]\r\n"));
11267 }
11268 }
11269
11270 static void _redisAssert(char *estr, char *file, int line) {
11271 redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
11272 redisLog(REDIS_WARNING,"==> %s:%d '%s' is not true",file,line,estr);
11273 #ifdef HAVE_BACKTRACE
11274 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
11275 *((char*)-1) = 'x';
11276 #endif
11277 }
11278
11279 static void _redisPanic(char *msg, char *file, int line) {
11280 redisLog(REDIS_WARNING,"!!! Software Failure. Press left mouse button to continue");
11281 redisLog(REDIS_WARNING,"Guru Meditation: %s #%s:%d",msg,file,line);
11282 #ifdef HAVE_BACKTRACE
11283 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
11284 *((char*)-1) = 'x';
11285 #endif
11286 }
11287
11288 /* =================================== Main! ================================ */
11289
11290 #ifdef __linux__
11291 int linuxOvercommitMemoryValue(void) {
11292 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
11293 char buf[64];
11294
11295 if (!fp) return -1;
11296 if (fgets(buf,64,fp) == NULL) {
11297 fclose(fp);
11298 return -1;
11299 }
11300 fclose(fp);
11301
11302 return atoi(buf);
11303 }
11304
11305 void linuxOvercommitMemoryWarning(void) {
11306 if (linuxOvercommitMemoryValue() == 0) {
11307 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.");
11308 }
11309 }
11310 #endif /* __linux__ */
11311
11312 static void daemonize(void) {
11313 int fd;
11314 FILE *fp;
11315
11316 if (fork() != 0) exit(0); /* parent exits */
11317 setsid(); /* create a new session */
11318
11319 /* Every output goes to /dev/null. If Redis is daemonized but
11320 * the 'logfile' is set to 'stdout' in the configuration file
11321 * it will not log at all. */
11322 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
11323 dup2(fd, STDIN_FILENO);
11324 dup2(fd, STDOUT_FILENO);
11325 dup2(fd, STDERR_FILENO);
11326 if (fd > STDERR_FILENO) close(fd);
11327 }
11328 /* Try to write the pid file */
11329 fp = fopen(server.pidfile,"w");
11330 if (fp) {
11331 fprintf(fp,"%d\n",getpid());
11332 fclose(fp);
11333 }
11334 }
11335
11336 static void version() {
11337 printf("Redis server version %s (%s:%d)\n", REDIS_VERSION,
11338 REDIS_GIT_SHA1, atoi(REDIS_GIT_DIRTY) > 0);
11339 exit(0);
11340 }
11341
11342 static void usage() {
11343 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf]\n");
11344 fprintf(stderr," ./redis-server - (read config from stdin)\n");
11345 exit(1);
11346 }
11347
11348 int main(int argc, char **argv) {
11349 time_t start;
11350
11351 initServerConfig();
11352 sortCommandTable();
11353 if (argc == 2) {
11354 if (strcmp(argv[1], "-v") == 0 ||
11355 strcmp(argv[1], "--version") == 0) version();
11356 if (strcmp(argv[1], "--help") == 0) usage();
11357 resetServerSaveParams();
11358 loadServerConfig(argv[1]);
11359 } else if ((argc > 2)) {
11360 usage();
11361 } else {
11362 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'");
11363 }
11364 if (server.daemonize) daemonize();
11365 initServer();
11366 redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
11367 #ifdef __linux__
11368 linuxOvercommitMemoryWarning();
11369 #endif
11370 start = time(NULL);
11371 if (server.appendonly) {
11372 if (loadAppendOnlyFile(server.appendfilename) == REDIS_OK)
11373 redisLog(REDIS_NOTICE,"DB loaded from append only file: %ld seconds",time(NULL)-start);
11374 } else {
11375 if (rdbLoad(server.dbfilename) == REDIS_OK)
11376 redisLog(REDIS_NOTICE,"DB loaded from disk: %ld seconds",time(NULL)-start);
11377 }
11378 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
11379 aeSetBeforeSleepProc(server.el,beforeSleep);
11380 aeMain(server.el);
11381 aeDeleteEventLoop(server.el);
11382 return 0;
11383 }
11384
11385 /* ============================= Backtrace support ========================= */
11386
11387 #ifdef HAVE_BACKTRACE
11388 static char *findFuncName(void *pointer, unsigned long *offset);
11389
11390 static void *getMcontextEip(ucontext_t *uc) {
11391 #if defined(__FreeBSD__)
11392 return (void*) uc->uc_mcontext.mc_eip;
11393 #elif defined(__dietlibc__)
11394 return (void*) uc->uc_mcontext.eip;
11395 #elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
11396 #if __x86_64__
11397 return (void*) uc->uc_mcontext->__ss.__rip;
11398 #else
11399 return (void*) uc->uc_mcontext->__ss.__eip;
11400 #endif
11401 #elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
11402 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
11403 return (void*) uc->uc_mcontext->__ss.__rip;
11404 #else
11405 return (void*) uc->uc_mcontext->__ss.__eip;
11406 #endif
11407 #elif defined(__i386__) || defined(__X86_64__) || defined(__x86_64__)
11408 return (void*) uc->uc_mcontext.gregs[REG_EIP]; /* Linux 32/64 bit */
11409 #elif defined(__ia64__) /* Linux IA64 */
11410 return (void*) uc->uc_mcontext.sc_ip;
11411 #else
11412 return NULL;
11413 #endif
11414 }
11415
11416 static void segvHandler(int sig, siginfo_t *info, void *secret) {
11417 void *trace[100];
11418 char **messages = NULL;
11419 int i, trace_size = 0;
11420 unsigned long offset=0;
11421 ucontext_t *uc = (ucontext_t*) secret;
11422 sds infostring;
11423 REDIS_NOTUSED(info);
11424
11425 redisLog(REDIS_WARNING,
11426 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION, sig);
11427 infostring = genRedisInfoString();
11428 redisLog(REDIS_WARNING, "%s",infostring);
11429 /* It's not safe to sdsfree() the returned string under memory
11430 * corruption conditions. Let it leak as we are going to abort */
11431
11432 trace_size = backtrace(trace, 100);
11433 /* overwrite sigaction with caller's address */
11434 if (getMcontextEip(uc) != NULL) {
11435 trace[1] = getMcontextEip(uc);
11436 }
11437 messages = backtrace_symbols(trace, trace_size);
11438
11439 for (i=1; i<trace_size; ++i) {
11440 char *fn = findFuncName(trace[i], &offset), *p;
11441
11442 p = strchr(messages[i],'+');
11443 if (!fn || (p && ((unsigned long)strtol(p+1,NULL,10)) < offset)) {
11444 redisLog(REDIS_WARNING,"%s", messages[i]);
11445 } else {
11446 redisLog(REDIS_WARNING,"%d redis-server %p %s + %d", i, trace[i], fn, (unsigned int)offset);
11447 }
11448 }
11449 /* free(messages); Don't call free() with possibly corrupted memory. */
11450 _exit(0);
11451 }
11452
11453 static void sigtermHandler(int sig) {
11454 REDIS_NOTUSED(sig);
11455
11456 redisLog(REDIS_WARNING,"SIGTERM received, scheduling shutting down...");
11457 server.shutdown_asap = 1;
11458 }
11459
11460 static void setupSigSegvAction(void) {
11461 struct sigaction act;
11462
11463 sigemptyset (&act.sa_mask);
11464 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
11465 * is used. Otherwise, sa_handler is used */
11466 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
11467 act.sa_sigaction = segvHandler;
11468 sigaction (SIGSEGV, &act, NULL);
11469 sigaction (SIGBUS, &act, NULL);
11470 sigaction (SIGFPE, &act, NULL);
11471 sigaction (SIGILL, &act, NULL);
11472 sigaction (SIGBUS, &act, NULL);
11473
11474 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
11475 act.sa_handler = sigtermHandler;
11476 sigaction (SIGTERM, &act, NULL);
11477 return;
11478 }
11479
11480 #include "staticsymbols.h"
11481 /* This function try to convert a pointer into a function name. It's used in
11482 * oreder to provide a backtrace under segmentation fault that's able to
11483 * display functions declared as static (otherwise the backtrace is useless). */
11484 static char *findFuncName(void *pointer, unsigned long *offset){
11485 int i, ret = -1;
11486 unsigned long off, minoff = 0;
11487
11488 /* Try to match against the Symbol with the smallest offset */
11489 for (i=0; symsTable[i].pointer; i++) {
11490 unsigned long lp = (unsigned long) pointer;
11491
11492 if (lp != (unsigned long)-1 && lp >= symsTable[i].pointer) {
11493 off=lp-symsTable[i].pointer;
11494 if (ret < 0 || off < minoff) {
11495 minoff=off;
11496 ret=i;
11497 }
11498 }
11499 }
11500 if (ret == -1) return NULL;
11501 *offset = minoff;
11502 return symsTable[ret].name;
11503 }
11504 #else /* HAVE_BACKTRACE */
11505 static void setupSigSegvAction(void) {
11506 }
11507 #endif /* HAVE_BACKTRACE */
11508
11509
11510
11511 /* The End */
11512
11513
11514