]> git.saurik.com Git - redis.git/blob - redis.c
redis version is now 1.3.11
[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 "1.3.11"
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 "sha1.h" /* SHA1 is used for DEBUG DIGEST */
79
80 /* Error codes */
81 #define REDIS_OK 0
82 #define REDIS_ERR -1
83
84 /* Static server configuration */
85 #define REDIS_SERVERPORT 6379 /* TCP port */
86 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
87 #define REDIS_IOBUF_LEN 1024
88 #define REDIS_LOADBUF_LEN 1024
89 #define REDIS_STATIC_ARGS 8
90 #define REDIS_DEFAULT_DBNUM 16
91 #define REDIS_CONFIGLINE_MAX 1024
92 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
93 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
94 #define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */
95 #define REDIS_MAX_WRITE_PER_EVENT (1024*64)
96 #define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
97
98 /* If more then REDIS_WRITEV_THRESHOLD write packets are pending use writev */
99 #define REDIS_WRITEV_THRESHOLD 3
100 /* Max number of iovecs used for each writev call */
101 #define REDIS_WRITEV_IOVEC_COUNT 256
102
103 /* Hash table parameters */
104 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
105
106 /* Command flags */
107 #define REDIS_CMD_BULK 1 /* Bulk write command */
108 #define REDIS_CMD_INLINE 2 /* Inline command */
109 /* REDIS_CMD_DENYOOM reserves a longer comment: all the commands marked with
110 this flags will return an error when the 'maxmemory' option is set in the
111 config file and the server is using more than maxmemory bytes of memory.
112 In short this commands are denied on low memory conditions. */
113 #define REDIS_CMD_DENYOOM 4
114 #define REDIS_CMD_FORCE_REPLICATION 8 /* Force replication even if dirty is 0 */
115
116 /* Object types */
117 #define REDIS_STRING 0
118 #define REDIS_LIST 1
119 #define REDIS_SET 2
120 #define REDIS_ZSET 3
121 #define REDIS_HASH 4
122
123 /* Objects encoding. Some kind of objects like Strings and Hashes can be
124 * internally represented in multiple ways. The 'encoding' field of the object
125 * is set to one of this fields for this object. */
126 #define REDIS_ENCODING_RAW 0 /* Raw representation */
127 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
128 #define REDIS_ENCODING_ZIPMAP 2 /* Encoded as zipmap */
129 #define REDIS_ENCODING_HT 3 /* Encoded as an hash table */
130
131 static char* strencoding[] = {
132 "raw", "int", "zipmap", "hashtable"
133 };
134
135 /* Object types only used for dumping to disk */
136 #define REDIS_EXPIRETIME 253
137 #define REDIS_SELECTDB 254
138 #define REDIS_EOF 255
139
140 /* Defines related to the dump file format. To store 32 bits lengths for short
141 * keys requires a lot of space, so we check the most significant 2 bits of
142 * the first byte to interpreter the length:
143 *
144 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
145 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
146 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
147 * 11|000000 this means: specially encoded object will follow. The six bits
148 * number specify the kind of object that follows.
149 * See the REDIS_RDB_ENC_* defines.
150 *
151 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
152 * values, will fit inside. */
153 #define REDIS_RDB_6BITLEN 0
154 #define REDIS_RDB_14BITLEN 1
155 #define REDIS_RDB_32BITLEN 2
156 #define REDIS_RDB_ENCVAL 3
157 #define REDIS_RDB_LENERR UINT_MAX
158
159 /* When a length of a string object stored on disk has the first two bits
160 * set, the remaining two bits specify a special encoding for the object
161 * accordingly to the following defines: */
162 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
163 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
164 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
165 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
166
167 /* Virtual memory object->where field. */
168 #define REDIS_VM_MEMORY 0 /* The object is on memory */
169 #define REDIS_VM_SWAPPED 1 /* The object is on disk */
170 #define REDIS_VM_SWAPPING 2 /* Redis is swapping this object on disk */
171 #define REDIS_VM_LOADING 3 /* Redis is loading this object from disk */
172
173 /* Virtual memory static configuration stuff.
174 * Check vmFindContiguousPages() to know more about this magic numbers. */
175 #define REDIS_VM_MAX_NEAR_PAGES 65536
176 #define REDIS_VM_MAX_RANDOM_JUMP 4096
177 #define REDIS_VM_MAX_THREADS 32
178 #define REDIS_THREAD_STACK_SIZE (1024*1024*4)
179 /* The following is the *percentage* of completed I/O jobs to process when the
180 * handelr is called. While Virtual Memory I/O operations are performed by
181 * threads, this operations must be processed by the main thread when completed
182 * in order to take effect. */
183 #define REDIS_MAX_COMPLETED_JOBS_PROCESSED 1
184
185 /* Client flags */
186 #define REDIS_SLAVE 1 /* This client is a slave server */
187 #define REDIS_MASTER 2 /* This client is a master server */
188 #define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */
189 #define REDIS_MULTI 8 /* This client is in a MULTI context */
190 #define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */
191 #define REDIS_IO_WAIT 32 /* The client is waiting for Virtual Memory I/O */
192
193 /* Slave replication state - slave side */
194 #define REDIS_REPL_NONE 0 /* No active replication */
195 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
196 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
197
198 /* Slave replication state - from the point of view of master
199 * Note that in SEND_BULK and ONLINE state the slave receives new updates
200 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
201 * to start the next background saving in order to send updates to it. */
202 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
203 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
204 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
205 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
206
207 /* List related stuff */
208 #define REDIS_HEAD 0
209 #define REDIS_TAIL 1
210
211 /* Sort operations */
212 #define REDIS_SORT_GET 0
213 #define REDIS_SORT_ASC 1
214 #define REDIS_SORT_DESC 2
215 #define REDIS_SORTKEY_MAX 1024
216
217 /* Log levels */
218 #define REDIS_DEBUG 0
219 #define REDIS_VERBOSE 1
220 #define REDIS_NOTICE 2
221 #define REDIS_WARNING 3
222
223 /* Anti-warning macro... */
224 #define REDIS_NOTUSED(V) ((void) V)
225
226 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
227 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
228
229 /* Append only defines */
230 #define APPENDFSYNC_NO 0
231 #define APPENDFSYNC_ALWAYS 1
232 #define APPENDFSYNC_EVERYSEC 2
233
234 /* Hashes related defaults */
235 #define REDIS_HASH_MAX_ZIPMAP_ENTRIES 64
236 #define REDIS_HASH_MAX_ZIPMAP_VALUE 512
237
238 /* We can print the stacktrace, so our assert is defined this way: */
239 #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
240 #define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
241 static void _redisAssert(char *estr, char *file, int line);
242 static void _redisPanic(char *msg, char *file, int line);
243
244 /*================================= Data types ============================== */
245
246 /* A redis object, that is a type able to hold a string / list / set */
247
248 /* The VM object structure */
249 struct redisObjectVM {
250 off_t page; /* the page at witch the object is stored on disk */
251 off_t usedpages; /* number of pages used on disk */
252 time_t atime; /* Last access time */
253 } vm;
254
255 /* The actual Redis Object */
256 typedef struct redisObject {
257 void *ptr;
258 unsigned char type;
259 unsigned char encoding;
260 unsigned char storage; /* If this object is a key, where is the value?
261 * REDIS_VM_MEMORY, REDIS_VM_SWAPPED, ... */
262 unsigned char vtype; /* If this object is a key, and value is swapped out,
263 * this is the type of the swapped out object. */
264 int refcount;
265 /* VM fields, this are only allocated if VM is active, otherwise the
266 * object allocation function will just allocate
267 * sizeof(redisObjct) minus sizeof(redisObjectVM), so using
268 * Redis without VM active will not have any overhead. */
269 struct redisObjectVM vm;
270 } robj;
271
272 /* Macro used to initalize a Redis object allocated on the stack.
273 * Note that this macro is taken near the structure definition to make sure
274 * we'll update it when the structure is changed, to avoid bugs like
275 * bug #85 introduced exactly in this way. */
276 #define initStaticStringObject(_var,_ptr) do { \
277 _var.refcount = 1; \
278 _var.type = REDIS_STRING; \
279 _var.encoding = REDIS_ENCODING_RAW; \
280 _var.ptr = _ptr; \
281 if (server.vm_enabled) _var.storage = REDIS_VM_MEMORY; \
282 } while(0);
283
284 typedef struct redisDb {
285 dict *dict; /* The keyspace for this DB */
286 dict *expires; /* Timeout of keys with a timeout set */
287 dict *blockingkeys; /* Keys with clients waiting for data (BLPOP) */
288 dict *io_keys; /* Keys with clients waiting for VM I/O */
289 int id;
290 } redisDb;
291
292 /* Client MULTI/EXEC state */
293 typedef struct multiCmd {
294 robj **argv;
295 int argc;
296 struct redisCommand *cmd;
297 } multiCmd;
298
299 typedef struct multiState {
300 multiCmd *commands; /* Array of MULTI commands */
301 int count; /* Total number of MULTI commands */
302 } multiState;
303
304 /* With multiplexing we need to take per-clinet state.
305 * Clients are taken in a liked list. */
306 typedef struct redisClient {
307 int fd;
308 redisDb *db;
309 int dictid;
310 sds querybuf;
311 robj **argv, **mbargv;
312 int argc, mbargc;
313 int bulklen; /* bulk read len. -1 if not in bulk read mode */
314 int multibulk; /* multi bulk command format active */
315 list *reply;
316 int sentlen;
317 time_t lastinteraction; /* time of the last interaction, used for timeout */
318 int flags; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */
319 int slaveseldb; /* slave selected db, if this client is a slave */
320 int authenticated; /* when requirepass is non-NULL */
321 int replstate; /* replication state if this is a slave */
322 int repldbfd; /* replication DB file descriptor */
323 long repldboff; /* replication DB file offset */
324 off_t repldbsize; /* replication DB file size */
325 multiState mstate; /* MULTI/EXEC state */
326 robj **blockingkeys; /* The key we are waiting to terminate a blocking
327 * operation such as BLPOP. Otherwise NULL. */
328 int blockingkeysnum; /* Number of blocking keys */
329 time_t blockingto; /* Blocking operation timeout. If UNIX current time
330 * is >= blockingto then the operation timed out. */
331 list *io_keys; /* Keys this client is waiting to be loaded from the
332 * swap file in order to continue. */
333 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
334 list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */
335 } redisClient;
336
337 struct saveparam {
338 time_t seconds;
339 int changes;
340 };
341
342 /* Global server state structure */
343 struct redisServer {
344 int port;
345 int fd;
346 redisDb *db;
347 long long dirty; /* changes to DB from the last save */
348 list *clients;
349 list *slaves, *monitors;
350 char neterr[ANET_ERR_LEN];
351 aeEventLoop *el;
352 int cronloops; /* number of times the cron function run */
353 list *objfreelist; /* A list of freed objects to avoid malloc() */
354 time_t lastsave; /* Unix time of last save succeeede */
355 /* Fields used only for stats */
356 time_t stat_starttime; /* server start time */
357 long long stat_numcommands; /* number of processed commands */
358 long long stat_numconnections; /* number of connections received */
359 long long stat_expiredkeys; /* number of expired keys */
360 /* Configuration */
361 int verbosity;
362 int glueoutputbuf;
363 int maxidletime;
364 int dbnum;
365 int daemonize;
366 int appendonly;
367 int appendfsync;
368 time_t lastfsync;
369 int appendfd;
370 int appendseldb;
371 char *pidfile;
372 pid_t bgsavechildpid;
373 pid_t bgrewritechildpid;
374 sds bgrewritebuf; /* buffer taken by parent during oppend only rewrite */
375 sds aofbuf; /* AOF buffer, written before entering the event loop */
376 struct saveparam *saveparams;
377 int saveparamslen;
378 char *logfile;
379 char *bindaddr;
380 char *dbfilename;
381 char *appendfilename;
382 char *requirepass;
383 int rdbcompression;
384 int activerehashing;
385 /* Replication related */
386 int isslave;
387 char *masterauth;
388 char *masterhost;
389 int masterport;
390 redisClient *master; /* client that is master for this slave */
391 int replstate;
392 unsigned int maxclients;
393 unsigned long long maxmemory;
394 unsigned int blpop_blocked_clients;
395 unsigned int vm_blocked_clients;
396 /* Sort parameters - qsort_r() is only available under BSD so we
397 * have to take this state global, in order to pass it to sortCompare() */
398 int sort_desc;
399 int sort_alpha;
400 int sort_bypattern;
401 /* Virtual memory configuration */
402 int vm_enabled;
403 char *vm_swap_file;
404 off_t vm_page_size;
405 off_t vm_pages;
406 unsigned long long vm_max_memory;
407 /* Hashes config */
408 size_t hash_max_zipmap_entries;
409 size_t hash_max_zipmap_value;
410 /* Virtual memory state */
411 FILE *vm_fp;
412 int vm_fd;
413 off_t vm_next_page; /* Next probably empty page */
414 off_t vm_near_pages; /* Number of pages allocated sequentially */
415 unsigned char *vm_bitmap; /* Bitmap of free/used pages */
416 time_t unixtime; /* Unix time sampled every second. */
417 /* Virtual memory I/O threads stuff */
418 /* An I/O thread process an element taken from the io_jobs queue and
419 * put the result of the operation in the io_done list. While the
420 * job is being processed, it's put on io_processing queue. */
421 list *io_newjobs; /* List of VM I/O jobs yet to be processed */
422 list *io_processing; /* List of VM I/O jobs being processed */
423 list *io_processed; /* List of VM I/O jobs already processed */
424 list *io_ready_clients; /* Clients ready to be unblocked. All keys loaded */
425 pthread_mutex_t io_mutex; /* lock to access io_jobs/io_done/io_thread_job */
426 pthread_mutex_t obj_freelist_mutex; /* safe redis objects creation/free */
427 pthread_mutex_t io_swapfile_mutex; /* So we can lseek + write */
428 pthread_attr_t io_threads_attr; /* attributes for threads creation */
429 int io_active_threads; /* Number of running I/O threads */
430 int vm_max_threads; /* Max number of I/O threads running at the same time */
431 /* Our main thread is blocked on the event loop, locking for sockets ready
432 * to be read or written, so when a threaded I/O operation is ready to be
433 * processed by the main thread, the I/O thread will use a unix pipe to
434 * awake the main thread. The followings are the two pipe FDs. */
435 int io_ready_pipe_read;
436 int io_ready_pipe_write;
437 /* Virtual memory stats */
438 unsigned long long vm_stats_used_pages;
439 unsigned long long vm_stats_swapped_objects;
440 unsigned long long vm_stats_swapouts;
441 unsigned long long vm_stats_swapins;
442 /* Pubsub */
443 dict *pubsub_channels; /* Map channels to list of subscribed clients */
444 list *pubsub_patterns; /* A list of pubsub_patterns */
445 /* Misc */
446 FILE *devnull;
447 };
448
449 typedef struct pubsubPattern {
450 redisClient *client;
451 robj *pattern;
452 } pubsubPattern;
453
454 typedef void redisCommandProc(redisClient *c);
455 typedef void redisVmPreloadProc(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
456 struct redisCommand {
457 char *name;
458 redisCommandProc *proc;
459 int arity;
460 int flags;
461 /* Use a function to determine which keys need to be loaded
462 * in the background prior to executing this command. Takes precedence
463 * over vm_firstkey and others, ignored when NULL */
464 redisVmPreloadProc *vm_preload_proc;
465 /* What keys should be loaded in background when calling this command? */
466 int vm_firstkey; /* The first argument that's a key (0 = no keys) */
467 int vm_lastkey; /* THe last argument that's a key */
468 int vm_keystep; /* The step between first and last key */
469 };
470
471 struct redisFunctionSym {
472 char *name;
473 unsigned long pointer;
474 };
475
476 typedef struct _redisSortObject {
477 robj *obj;
478 union {
479 double score;
480 robj *cmpobj;
481 } u;
482 } redisSortObject;
483
484 typedef struct _redisSortOperation {
485 int type;
486 robj *pattern;
487 } redisSortOperation;
488
489 /* ZSETs use a specialized version of Skiplists */
490
491 typedef struct zskiplistNode {
492 struct zskiplistNode **forward;
493 struct zskiplistNode *backward;
494 unsigned int *span;
495 double score;
496 robj *obj;
497 } zskiplistNode;
498
499 typedef struct zskiplist {
500 struct zskiplistNode *header, *tail;
501 unsigned long length;
502 int level;
503 } zskiplist;
504
505 typedef struct zset {
506 dict *dict;
507 zskiplist *zsl;
508 } zset;
509
510 /* Our shared "common" objects */
511
512 #define REDIS_SHARED_INTEGERS 10000
513 struct sharedObjectsStruct {
514 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
515 *colon, *nullbulk, *nullmultibulk, *queued,
516 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
517 *outofrangeerr, *plus,
518 *select0, *select1, *select2, *select3, *select4,
519 *select5, *select6, *select7, *select8, *select9,
520 *messagebulk, *pmessagebulk, *subscribebulk, *unsubscribebulk, *mbulk3,
521 *mbulk4, *psubscribebulk, *punsubscribebulk,
522 *integers[REDIS_SHARED_INTEGERS];
523 } shared;
524
525 /* Global vars that are actally used as constants. The following double
526 * values are used for double on-disk serialization, and are initialized
527 * at runtime to avoid strange compiler optimizations. */
528
529 static double R_Zero, R_PosInf, R_NegInf, R_Nan;
530
531 /* VM threaded I/O request message */
532 #define REDIS_IOJOB_LOAD 0 /* Load from disk to memory */
533 #define REDIS_IOJOB_PREPARE_SWAP 1 /* Compute needed pages */
534 #define REDIS_IOJOB_DO_SWAP 2 /* Swap from memory to disk */
535 typedef struct iojob {
536 int type; /* Request type, REDIS_IOJOB_* */
537 redisDb *db;/* Redis database */
538 robj *key; /* This I/O request is about swapping this key */
539 robj *val; /* the value to swap for REDIS_IOREQ_*_SWAP, otherwise this
540 * field is populated by the I/O thread for REDIS_IOREQ_LOAD. */
541 off_t page; /* Swap page where to read/write the object */
542 off_t pages; /* Swap pages needed to save object. PREPARE_SWAP return val */
543 int canceled; /* True if this command was canceled by blocking side of VM */
544 pthread_t thread; /* ID of the thread processing this entry */
545 } iojob;
546
547 /*================================ Prototypes =============================== */
548
549 static void freeStringObject(robj *o);
550 static void freeListObject(robj *o);
551 static void freeSetObject(robj *o);
552 static void decrRefCount(void *o);
553 static robj *createObject(int type, void *ptr);
554 static void freeClient(redisClient *c);
555 static int rdbLoad(char *filename);
556 static void addReply(redisClient *c, robj *obj);
557 static void addReplySds(redisClient *c, sds s);
558 static void incrRefCount(robj *o);
559 static int rdbSaveBackground(char *filename);
560 static robj *createStringObject(char *ptr, size_t len);
561 static robj *dupStringObject(robj *o);
562 static void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
563 static void replicationFeedMonitors(list *monitors, int dictid, robj **argv, int argc);
564 static void flushAppendOnlyFile(void);
565 static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);
566 static int syncWithMaster(void);
567 static robj *tryObjectEncoding(robj *o);
568 static robj *getDecodedObject(robj *o);
569 static int removeExpire(redisDb *db, robj *key);
570 static int expireIfNeeded(redisDb *db, robj *key);
571 static int deleteIfVolatile(redisDb *db, robj *key);
572 static int deleteIfSwapped(redisDb *db, robj *key);
573 static int deleteKey(redisDb *db, robj *key);
574 static time_t getExpire(redisDb *db, robj *key);
575 static int setExpire(redisDb *db, robj *key, time_t when);
576 static void updateSlavesWaitingBgsave(int bgsaveerr);
577 static void freeMemoryIfNeeded(void);
578 static int processCommand(redisClient *c);
579 static void setupSigSegvAction(void);
580 static void rdbRemoveTempFile(pid_t childpid);
581 static void aofRemoveTempFile(pid_t childpid);
582 static size_t stringObjectLen(robj *o);
583 static void processInputBuffer(redisClient *c);
584 static zskiplist *zslCreate(void);
585 static void zslFree(zskiplist *zsl);
586 static void zslInsert(zskiplist *zsl, double score, robj *obj);
587 static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask);
588 static void initClientMultiState(redisClient *c);
589 static void freeClientMultiState(redisClient *c);
590 static void queueMultiCommand(redisClient *c, struct redisCommand *cmd);
591 static void unblockClientWaitingData(redisClient *c);
592 static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele);
593 static void vmInit(void);
594 static void vmMarkPagesFree(off_t page, off_t count);
595 static robj *vmLoadObject(robj *key);
596 static robj *vmPreviewObject(robj *key);
597 static int vmSwapOneObjectBlocking(void);
598 static int vmSwapOneObjectThreaded(void);
599 static int vmCanSwapOut(void);
600 static int tryFreeOneObjectFromFreelist(void);
601 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);
602 static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, int mask);
603 static void vmCancelThreadedIOJob(robj *o);
604 static void lockThreadedIO(void);
605 static void unlockThreadedIO(void);
606 static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db);
607 static void freeIOJob(iojob *j);
608 static void queueIOJob(iojob *j);
609 static int vmWriteObjectOnSwap(robj *o, off_t page);
610 static robj *vmReadObjectFromSwap(off_t page, int type);
611 static void waitEmptyIOJobsQueue(void);
612 static void vmReopenSwapFile(void);
613 static int vmFreePage(off_t page);
614 static void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
615 static void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
616 static int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd);
617 static int dontWaitForSwappedKey(redisClient *c, robj *key);
618 static void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key);
619 static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask);
620 static struct redisCommand *lookupCommand(char *name);
621 static void call(redisClient *c, struct redisCommand *cmd);
622 static void resetClient(redisClient *c);
623 static void convertToRealHash(robj *o);
624 static int pubsubUnsubscribeAllChannels(redisClient *c, int notify);
625 static int pubsubUnsubscribeAllPatterns(redisClient *c, int notify);
626 static void freePubsubPattern(void *p);
627 static int listMatchPubsubPattern(void *a, void *b);
628 static int compareStringObjects(robj *a, robj *b);
629 static int equalStringObjects(robj *a, robj *b);
630 static void usage();
631 static int rewriteAppendOnlyFileBackground(void);
632 static int vmSwapObjectBlocking(robj *key, robj *val);
633
634 static void authCommand(redisClient *c);
635 static void pingCommand(redisClient *c);
636 static void echoCommand(redisClient *c);
637 static void setCommand(redisClient *c);
638 static void setnxCommand(redisClient *c);
639 static void setexCommand(redisClient *c);
640 static void getCommand(redisClient *c);
641 static void delCommand(redisClient *c);
642 static void existsCommand(redisClient *c);
643 static void incrCommand(redisClient *c);
644 static void decrCommand(redisClient *c);
645 static void incrbyCommand(redisClient *c);
646 static void decrbyCommand(redisClient *c);
647 static void selectCommand(redisClient *c);
648 static void randomkeyCommand(redisClient *c);
649 static void keysCommand(redisClient *c);
650 static void dbsizeCommand(redisClient *c);
651 static void lastsaveCommand(redisClient *c);
652 static void saveCommand(redisClient *c);
653 static void bgsaveCommand(redisClient *c);
654 static void bgrewriteaofCommand(redisClient *c);
655 static void shutdownCommand(redisClient *c);
656 static void moveCommand(redisClient *c);
657 static void renameCommand(redisClient *c);
658 static void renamenxCommand(redisClient *c);
659 static void lpushCommand(redisClient *c);
660 static void rpushCommand(redisClient *c);
661 static void lpopCommand(redisClient *c);
662 static void rpopCommand(redisClient *c);
663 static void llenCommand(redisClient *c);
664 static void lindexCommand(redisClient *c);
665 static void lrangeCommand(redisClient *c);
666 static void ltrimCommand(redisClient *c);
667 static void typeCommand(redisClient *c);
668 static void lsetCommand(redisClient *c);
669 static void saddCommand(redisClient *c);
670 static void sremCommand(redisClient *c);
671 static void smoveCommand(redisClient *c);
672 static void sismemberCommand(redisClient *c);
673 static void scardCommand(redisClient *c);
674 static void spopCommand(redisClient *c);
675 static void srandmemberCommand(redisClient *c);
676 static void sinterCommand(redisClient *c);
677 static void sinterstoreCommand(redisClient *c);
678 static void sunionCommand(redisClient *c);
679 static void sunionstoreCommand(redisClient *c);
680 static void sdiffCommand(redisClient *c);
681 static void sdiffstoreCommand(redisClient *c);
682 static void syncCommand(redisClient *c);
683 static void flushdbCommand(redisClient *c);
684 static void flushallCommand(redisClient *c);
685 static void sortCommand(redisClient *c);
686 static void lremCommand(redisClient *c);
687 static void rpoplpushcommand(redisClient *c);
688 static void infoCommand(redisClient *c);
689 static void mgetCommand(redisClient *c);
690 static void monitorCommand(redisClient *c);
691 static void expireCommand(redisClient *c);
692 static void expireatCommand(redisClient *c);
693 static void getsetCommand(redisClient *c);
694 static void ttlCommand(redisClient *c);
695 static void slaveofCommand(redisClient *c);
696 static void debugCommand(redisClient *c);
697 static void msetCommand(redisClient *c);
698 static void msetnxCommand(redisClient *c);
699 static void zaddCommand(redisClient *c);
700 static void zincrbyCommand(redisClient *c);
701 static void zrangeCommand(redisClient *c);
702 static void zrangebyscoreCommand(redisClient *c);
703 static void zcountCommand(redisClient *c);
704 static void zrevrangeCommand(redisClient *c);
705 static void zcardCommand(redisClient *c);
706 static void zremCommand(redisClient *c);
707 static void zscoreCommand(redisClient *c);
708 static void zremrangebyscoreCommand(redisClient *c);
709 static void multiCommand(redisClient *c);
710 static void execCommand(redisClient *c);
711 static void discardCommand(redisClient *c);
712 static void blpopCommand(redisClient *c);
713 static void brpopCommand(redisClient *c);
714 static void appendCommand(redisClient *c);
715 static void substrCommand(redisClient *c);
716 static void zrankCommand(redisClient *c);
717 static void zrevrankCommand(redisClient *c);
718 static void hsetCommand(redisClient *c);
719 static void hsetnxCommand(redisClient *c);
720 static void hgetCommand(redisClient *c);
721 static void hmsetCommand(redisClient *c);
722 static void hmgetCommand(redisClient *c);
723 static void hdelCommand(redisClient *c);
724 static void hlenCommand(redisClient *c);
725 static void zremrangebyrankCommand(redisClient *c);
726 static void zunionstoreCommand(redisClient *c);
727 static void zinterstoreCommand(redisClient *c);
728 static void hkeysCommand(redisClient *c);
729 static void hvalsCommand(redisClient *c);
730 static void hgetallCommand(redisClient *c);
731 static void hexistsCommand(redisClient *c);
732 static void configCommand(redisClient *c);
733 static void hincrbyCommand(redisClient *c);
734 static void subscribeCommand(redisClient *c);
735 static void unsubscribeCommand(redisClient *c);
736 static void psubscribeCommand(redisClient *c);
737 static void punsubscribeCommand(redisClient *c);
738 static void publishCommand(redisClient *c);
739
740 /*================================= Globals ================================= */
741
742 /* Global vars */
743 static struct redisServer server; /* server global state */
744 static struct redisCommand cmdTable[] = {
745 {"get",getCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
746 {"set",setCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
747 {"setnx",setnxCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
748 {"setex",setexCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,0,0,0},
749 {"append",appendCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
750 {"substr",substrCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
751 {"del",delCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
752 {"exists",existsCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
753 {"incr",incrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
754 {"decr",decrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
755 {"mget",mgetCommand,-2,REDIS_CMD_INLINE,NULL,1,-1,1},
756 {"rpush",rpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
757 {"lpush",lpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
758 {"rpop",rpopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
759 {"lpop",lpopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
760 {"brpop",brpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
761 {"blpop",blpopCommand,-3,REDIS_CMD_INLINE,NULL,1,1,1},
762 {"llen",llenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
763 {"lindex",lindexCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
764 {"lset",lsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
765 {"lrange",lrangeCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
766 {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
767 {"lrem",lremCommand,4,REDIS_CMD_BULK,NULL,1,1,1},
768 {"rpoplpush",rpoplpushcommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,2,1},
769 {"sadd",saddCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
770 {"srem",sremCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
771 {"smove",smoveCommand,4,REDIS_CMD_BULK,NULL,1,2,1},
772 {"sismember",sismemberCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
773 {"scard",scardCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
774 {"spop",spopCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
775 {"srandmember",srandmemberCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
776 {"sinter",sinterCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
777 {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
778 {"sunion",sunionCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
779 {"sunionstore",sunionstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
780 {"sdiff",sdiffCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,-1,1},
781 {"sdiffstore",sdiffstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,2,-1,1},
782 {"smembers",sinterCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
783 {"zadd",zaddCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
784 {"zincrby",zincrbyCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
785 {"zrem",zremCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
786 {"zremrangebyscore",zremrangebyscoreCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
787 {"zremrangebyrank",zremrangebyrankCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
788 {"zunionstore",zunionstoreCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
789 {"zinterstore",zinterstoreCommand,-4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0},
790 {"zrange",zrangeCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
791 {"zrangebyscore",zrangebyscoreCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
792 {"zcount",zcountCommand,4,REDIS_CMD_INLINE,NULL,1,1,1},
793 {"zrevrange",zrevrangeCommand,-4,REDIS_CMD_INLINE,NULL,1,1,1},
794 {"zcard",zcardCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
795 {"zscore",zscoreCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
796 {"zrank",zrankCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
797 {"zrevrank",zrevrankCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
798 {"hset",hsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
799 {"hsetnx",hsetnxCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
800 {"hget",hgetCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
801 {"hmset",hmsetCommand,-4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
802 {"hmget",hmgetCommand,-3,REDIS_CMD_BULK,NULL,1,1,1},
803 {"hincrby",hincrbyCommand,4,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
804 {"hdel",hdelCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
805 {"hlen",hlenCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
806 {"hkeys",hkeysCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
807 {"hvals",hvalsCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
808 {"hgetall",hgetallCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
809 {"hexists",hexistsCommand,3,REDIS_CMD_BULK,NULL,1,1,1},
810 {"incrby",incrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
811 {"decrby",decrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
812 {"getset",getsetCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,1,1},
813 {"mset",msetCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,-1,2},
814 {"msetnx",msetnxCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM,NULL,1,-1,2},
815 {"randomkey",randomkeyCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
816 {"select",selectCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
817 {"move",moveCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
818 {"rename",renameCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
819 {"renamenx",renamenxCommand,3,REDIS_CMD_INLINE,NULL,1,1,1},
820 {"expire",expireCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
821 {"expireat",expireatCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
822 {"keys",keysCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
823 {"dbsize",dbsizeCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
824 {"auth",authCommand,2,REDIS_CMD_INLINE,NULL,0,0,0},
825 {"ping",pingCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
826 {"echo",echoCommand,2,REDIS_CMD_BULK,NULL,0,0,0},
827 {"save",saveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
828 {"bgsave",bgsaveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
829 {"bgrewriteaof",bgrewriteaofCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
830 {"shutdown",shutdownCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
831 {"lastsave",lastsaveCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
832 {"type",typeCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
833 {"multi",multiCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
834 {"exec",execCommand,1,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,execBlockClientOnSwappedKeys,0,0,0},
835 {"discard",discardCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
836 {"sync",syncCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
837 {"flushdb",flushdbCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
838 {"flushall",flushallCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
839 {"sort",sortCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM,NULL,1,1,1},
840 {"info",infoCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
841 {"monitor",monitorCommand,1,REDIS_CMD_INLINE,NULL,0,0,0},
842 {"ttl",ttlCommand,2,REDIS_CMD_INLINE,NULL,1,1,1},
843 {"slaveof",slaveofCommand,3,REDIS_CMD_INLINE,NULL,0,0,0},
844 {"debug",debugCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
845 {"config",configCommand,-2,REDIS_CMD_BULK,NULL,0,0,0},
846 {"subscribe",subscribeCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
847 {"unsubscribe",unsubscribeCommand,-1,REDIS_CMD_INLINE,NULL,0,0,0},
848 {"psubscribe",psubscribeCommand,-2,REDIS_CMD_INLINE,NULL,0,0,0},
849 {"punsubscribe",punsubscribeCommand,-1,REDIS_CMD_INLINE,NULL,0,0,0},
850 {"publish",publishCommand,3,REDIS_CMD_BULK|REDIS_CMD_FORCE_REPLICATION,NULL,0,0,0},
851 {NULL,NULL,0,0,NULL,0,0,0}
852 };
853
854 /*============================ Utility functions ============================ */
855
856 /* Glob-style pattern matching. */
857 static int stringmatchlen(const char *pattern, int patternLen,
858 const char *string, int stringLen, int nocase)
859 {
860 while(patternLen) {
861 switch(pattern[0]) {
862 case '*':
863 while (pattern[1] == '*') {
864 pattern++;
865 patternLen--;
866 }
867 if (patternLen == 1)
868 return 1; /* match */
869 while(stringLen) {
870 if (stringmatchlen(pattern+1, patternLen-1,
871 string, stringLen, nocase))
872 return 1; /* match */
873 string++;
874 stringLen--;
875 }
876 return 0; /* no match */
877 break;
878 case '?':
879 if (stringLen == 0)
880 return 0; /* no match */
881 string++;
882 stringLen--;
883 break;
884 case '[':
885 {
886 int not, match;
887
888 pattern++;
889 patternLen--;
890 not = pattern[0] == '^';
891 if (not) {
892 pattern++;
893 patternLen--;
894 }
895 match = 0;
896 while(1) {
897 if (pattern[0] == '\\') {
898 pattern++;
899 patternLen--;
900 if (pattern[0] == string[0])
901 match = 1;
902 } else if (pattern[0] == ']') {
903 break;
904 } else if (patternLen == 0) {
905 pattern--;
906 patternLen++;
907 break;
908 } else if (pattern[1] == '-' && patternLen >= 3) {
909 int start = pattern[0];
910 int end = pattern[2];
911 int c = string[0];
912 if (start > end) {
913 int t = start;
914 start = end;
915 end = t;
916 }
917 if (nocase) {
918 start = tolower(start);
919 end = tolower(end);
920 c = tolower(c);
921 }
922 pattern += 2;
923 patternLen -= 2;
924 if (c >= start && c <= end)
925 match = 1;
926 } else {
927 if (!nocase) {
928 if (pattern[0] == string[0])
929 match = 1;
930 } else {
931 if (tolower((int)pattern[0]) == tolower((int)string[0]))
932 match = 1;
933 }
934 }
935 pattern++;
936 patternLen--;
937 }
938 if (not)
939 match = !match;
940 if (!match)
941 return 0; /* no match */
942 string++;
943 stringLen--;
944 break;
945 }
946 case '\\':
947 if (patternLen >= 2) {
948 pattern++;
949 patternLen--;
950 }
951 /* fall through */
952 default:
953 if (!nocase) {
954 if (pattern[0] != string[0])
955 return 0; /* no match */
956 } else {
957 if (tolower((int)pattern[0]) != tolower((int)string[0]))
958 return 0; /* no match */
959 }
960 string++;
961 stringLen--;
962 break;
963 }
964 pattern++;
965 patternLen--;
966 if (stringLen == 0) {
967 while(*pattern == '*') {
968 pattern++;
969 patternLen--;
970 }
971 break;
972 }
973 }
974 if (patternLen == 0 && stringLen == 0)
975 return 1;
976 return 0;
977 }
978
979 static int stringmatch(const char *pattern, const char *string, int nocase) {
980 return stringmatchlen(pattern,strlen(pattern),string,strlen(string),nocase);
981 }
982
983 /* Convert a string representing an amount of memory into the number of
984 * bytes, so for instance memtoll("1Gi") will return 1073741824 that is
985 * (1024*1024*1024).
986 *
987 * On parsing error, if *err is not NULL, it's set to 1, otherwise it's
988 * set to 0 */
989 static long long memtoll(const char *p, int *err) {
990 const char *u;
991 char buf[128];
992 long mul; /* unit multiplier */
993 long long val;
994 unsigned int digits;
995
996 if (err) *err = 0;
997 /* Search the first non digit character. */
998 u = p;
999 if (*u == '-') u++;
1000 while(*u && isdigit(*u)) u++;
1001 if (*u == '\0' || !strcasecmp(u,"b")) {
1002 mul = 1;
1003 } else if (!strcasecmp(u,"k")) {
1004 mul = 1000;
1005 } else if (!strcasecmp(u,"kb")) {
1006 mul = 1024;
1007 } else if (!strcasecmp(u,"m")) {
1008 mul = 1000*1000;
1009 } else if (!strcasecmp(u,"mb")) {
1010 mul = 1024*1024;
1011 } else if (!strcasecmp(u,"g")) {
1012 mul = 1000L*1000*1000;
1013 } else if (!strcasecmp(u,"gb")) {
1014 mul = 1024L*1024*1024;
1015 } else {
1016 if (err) *err = 1;
1017 mul = 1;
1018 }
1019 digits = u-p;
1020 if (digits >= sizeof(buf)) {
1021 if (err) *err = 1;
1022 return LLONG_MAX;
1023 }
1024 memcpy(buf,p,digits);
1025 buf[digits] = '\0';
1026 val = strtoll(buf,NULL,10);
1027 return val*mul;
1028 }
1029
1030 /* Convert a long long into a string. Returns the number of
1031 * characters needed to represent the number, that can be shorter if passed
1032 * buffer length is not enough to store the whole number. */
1033 static int ll2string(char *s, size_t len, long long value) {
1034 char buf[32], *p;
1035 unsigned long long v;
1036 size_t l;
1037
1038 if (len == 0) return 0;
1039 v = (value < 0) ? -value : value;
1040 p = buf+31; /* point to the last character */
1041 do {
1042 *p-- = '0'+(v%10);
1043 v /= 10;
1044 } while(v);
1045 if (value < 0) *p-- = '-';
1046 p++;
1047 l = 32-(p-buf);
1048 if (l+1 > len) l = len-1; /* Make sure it fits, including the nul term */
1049 memcpy(s,p,l);
1050 s[l] = '\0';
1051 return l;
1052 }
1053
1054 static void redisLog(int level, const char *fmt, ...) {
1055 va_list ap;
1056 FILE *fp;
1057
1058 fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
1059 if (!fp) return;
1060
1061 va_start(ap, fmt);
1062 if (level >= server.verbosity) {
1063 char *c = ".-*#";
1064 char buf[64];
1065 time_t now;
1066
1067 now = time(NULL);
1068 strftime(buf,64,"%d %b %H:%M:%S",localtime(&now));
1069 fprintf(fp,"[%d] %s %c ",(int)getpid(),buf,c[level]);
1070 vfprintf(fp, fmt, ap);
1071 fprintf(fp,"\n");
1072 fflush(fp);
1073 }
1074 va_end(ap);
1075
1076 if (server.logfile) fclose(fp);
1077 }
1078
1079 /*====================== Hash table type implementation ==================== */
1080
1081 /* This is an hash table type that uses the SDS dynamic strings libary as
1082 * keys and radis objects as values (objects can hold SDS strings,
1083 * lists, sets). */
1084
1085 static void dictVanillaFree(void *privdata, void *val)
1086 {
1087 DICT_NOTUSED(privdata);
1088 zfree(val);
1089 }
1090
1091 static void dictListDestructor(void *privdata, void *val)
1092 {
1093 DICT_NOTUSED(privdata);
1094 listRelease((list*)val);
1095 }
1096
1097 static int sdsDictKeyCompare(void *privdata, const void *key1,
1098 const void *key2)
1099 {
1100 int l1,l2;
1101 DICT_NOTUSED(privdata);
1102
1103 l1 = sdslen((sds)key1);
1104 l2 = sdslen((sds)key2);
1105 if (l1 != l2) return 0;
1106 return memcmp(key1, key2, l1) == 0;
1107 }
1108
1109 static void dictRedisObjectDestructor(void *privdata, void *val)
1110 {
1111 DICT_NOTUSED(privdata);
1112
1113 if (val == NULL) return; /* Values of swapped out keys as set to NULL */
1114 decrRefCount(val);
1115 }
1116
1117 static int dictObjKeyCompare(void *privdata, const void *key1,
1118 const void *key2)
1119 {
1120 const robj *o1 = key1, *o2 = key2;
1121 return sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
1122 }
1123
1124 static unsigned int dictObjHash(const void *key) {
1125 const robj *o = key;
1126 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1127 }
1128
1129 static int dictEncObjKeyCompare(void *privdata, const void *key1,
1130 const void *key2)
1131 {
1132 robj *o1 = (robj*) key1, *o2 = (robj*) key2;
1133 int cmp;
1134
1135 if (o1->encoding == REDIS_ENCODING_INT &&
1136 o2->encoding == REDIS_ENCODING_INT)
1137 return o1->ptr == o2->ptr;
1138
1139 o1 = getDecodedObject(o1);
1140 o2 = getDecodedObject(o2);
1141 cmp = sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
1142 decrRefCount(o1);
1143 decrRefCount(o2);
1144 return cmp;
1145 }
1146
1147 static unsigned int dictEncObjHash(const void *key) {
1148 robj *o = (robj*) key;
1149
1150 if (o->encoding == REDIS_ENCODING_RAW) {
1151 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1152 } else {
1153 if (o->encoding == REDIS_ENCODING_INT) {
1154 char buf[32];
1155 int len;
1156
1157 len = ll2string(buf,32,(long)o->ptr);
1158 return dictGenHashFunction((unsigned char*)buf, len);
1159 } else {
1160 unsigned int hash;
1161
1162 o = getDecodedObject(o);
1163 hash = dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
1164 decrRefCount(o);
1165 return hash;
1166 }
1167 }
1168 }
1169
1170 /* Sets type and expires */
1171 static dictType setDictType = {
1172 dictEncObjHash, /* hash function */
1173 NULL, /* key dup */
1174 NULL, /* val dup */
1175 dictEncObjKeyCompare, /* key compare */
1176 dictRedisObjectDestructor, /* key destructor */
1177 NULL /* val destructor */
1178 };
1179
1180 /* Sorted sets hash (note: a skiplist is used in addition to the hash table) */
1181 static dictType zsetDictType = {
1182 dictEncObjHash, /* hash function */
1183 NULL, /* key dup */
1184 NULL, /* val dup */
1185 dictEncObjKeyCompare, /* key compare */
1186 dictRedisObjectDestructor, /* key destructor */
1187 dictVanillaFree /* val destructor of malloc(sizeof(double)) */
1188 };
1189
1190 /* Db->dict */
1191 static dictType dbDictType = {
1192 dictObjHash, /* hash function */
1193 NULL, /* key dup */
1194 NULL, /* val dup */
1195 dictObjKeyCompare, /* key compare */
1196 dictRedisObjectDestructor, /* key destructor */
1197 dictRedisObjectDestructor /* val destructor */
1198 };
1199
1200 /* Db->expires */
1201 static dictType keyptrDictType = {
1202 dictObjHash, /* hash function */
1203 NULL, /* key dup */
1204 NULL, /* val dup */
1205 dictObjKeyCompare, /* key compare */
1206 dictRedisObjectDestructor, /* key destructor */
1207 NULL /* val destructor */
1208 };
1209
1210 /* Hash type hash table (note that small hashes are represented with zimpaps) */
1211 static dictType hashDictType = {
1212 dictEncObjHash, /* hash function */
1213 NULL, /* key dup */
1214 NULL, /* val dup */
1215 dictEncObjKeyCompare, /* key compare */
1216 dictRedisObjectDestructor, /* key destructor */
1217 dictRedisObjectDestructor /* val destructor */
1218 };
1219
1220 /* Keylist hash table type has unencoded redis objects as keys and
1221 * lists as values. It's used for blocking operations (BLPOP) and to
1222 * map swapped keys to a list of clients waiting for this keys to be loaded. */
1223 static dictType keylistDictType = {
1224 dictObjHash, /* hash function */
1225 NULL, /* key dup */
1226 NULL, /* val dup */
1227 dictObjKeyCompare, /* key compare */
1228 dictRedisObjectDestructor, /* key destructor */
1229 dictListDestructor /* val destructor */
1230 };
1231
1232 static void version();
1233
1234 /* ========================= Random utility functions ======================= */
1235
1236 /* Redis generally does not try to recover from out of memory conditions
1237 * when allocating objects or strings, it is not clear if it will be possible
1238 * to report this condition to the client since the networking layer itself
1239 * is based on heap allocation for send buffers, so we simply abort.
1240 * At least the code will be simpler to read... */
1241 static void oom(const char *msg) {
1242 redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
1243 sleep(1);
1244 abort();
1245 }
1246
1247 /* ====================== Redis server networking stuff ===================== */
1248 static void closeTimedoutClients(void) {
1249 redisClient *c;
1250 listNode *ln;
1251 time_t now = time(NULL);
1252 listIter li;
1253
1254 listRewind(server.clients,&li);
1255 while ((ln = listNext(&li)) != NULL) {
1256 c = listNodeValue(ln);
1257 if (server.maxidletime &&
1258 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
1259 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
1260 dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */
1261 listLength(c->pubsub_patterns) == 0 &&
1262 (now - c->lastinteraction > server.maxidletime))
1263 {
1264 redisLog(REDIS_VERBOSE,"Closing idle client");
1265 freeClient(c);
1266 } else if (c->flags & REDIS_BLOCKED) {
1267 if (c->blockingto != 0 && c->blockingto < now) {
1268 addReply(c,shared.nullmultibulk);
1269 unblockClientWaitingData(c);
1270 }
1271 }
1272 }
1273 }
1274
1275 static int htNeedsResize(dict *dict) {
1276 long long size, used;
1277
1278 size = dictSlots(dict);
1279 used = dictSize(dict);
1280 return (size && used && size > DICT_HT_INITIAL_SIZE &&
1281 (used*100/size < REDIS_HT_MINFILL));
1282 }
1283
1284 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
1285 * we resize the hash table to save memory */
1286 static void tryResizeHashTables(void) {
1287 int j;
1288
1289 for (j = 0; j < server.dbnum; j++) {
1290 if (htNeedsResize(server.db[j].dict))
1291 dictResize(server.db[j].dict);
1292 if (htNeedsResize(server.db[j].expires))
1293 dictResize(server.db[j].expires);
1294 }
1295 }
1296
1297 /* Our hash table implementation performs rehashing incrementally while
1298 * we write/read from the hash table. Still if the server is idle, the hash
1299 * table will use two tables for a long time. So we try to use 1 millisecond
1300 * of CPU time at every serverCron() loop in order to rehash some key. */
1301 static void incrementallyRehash(void) {
1302 int j;
1303
1304 for (j = 0; j < server.dbnum; j++) {
1305 if (dictIsRehashing(server.db[j].dict)) {
1306 dictRehashMilliseconds(server.db[j].dict,1);
1307 break; /* already used our millisecond for this loop... */
1308 }
1309 }
1310 }
1311
1312 /* A background saving child (BGSAVE) terminated its work. Handle this. */
1313 void backgroundSaveDoneHandler(int statloc) {
1314 int exitcode = WEXITSTATUS(statloc);
1315 int bysignal = WIFSIGNALED(statloc);
1316
1317 if (!bysignal && exitcode == 0) {
1318 redisLog(REDIS_NOTICE,
1319 "Background saving terminated with success");
1320 server.dirty = 0;
1321 server.lastsave = time(NULL);
1322 } else if (!bysignal && exitcode != 0) {
1323 redisLog(REDIS_WARNING, "Background saving error");
1324 } else {
1325 redisLog(REDIS_WARNING,
1326 "Background saving terminated by signal %d", WTERMSIG(statloc));
1327 rdbRemoveTempFile(server.bgsavechildpid);
1328 }
1329 server.bgsavechildpid = -1;
1330 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1331 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1332 updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR);
1333 }
1334
1335 /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
1336 * Handle this. */
1337 void backgroundRewriteDoneHandler(int statloc) {
1338 int exitcode = WEXITSTATUS(statloc);
1339 int bysignal = WIFSIGNALED(statloc);
1340
1341 if (!bysignal && exitcode == 0) {
1342 int fd;
1343 char tmpfile[256];
1344
1345 redisLog(REDIS_NOTICE,
1346 "Background append only file rewriting terminated with success");
1347 /* Now it's time to flush the differences accumulated by the parent */
1348 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) server.bgrewritechildpid);
1349 fd = open(tmpfile,O_WRONLY|O_APPEND);
1350 if (fd == -1) {
1351 redisLog(REDIS_WARNING, "Not able to open the temp append only file produced by the child: %s", strerror(errno));
1352 goto cleanup;
1353 }
1354 /* Flush our data... */
1355 if (write(fd,server.bgrewritebuf,sdslen(server.bgrewritebuf)) !=
1356 (signed) sdslen(server.bgrewritebuf)) {
1357 redisLog(REDIS_WARNING, "Error or short write trying to flush the parent diff of the append log file in the child temp file: %s", strerror(errno));
1358 close(fd);
1359 goto cleanup;
1360 }
1361 redisLog(REDIS_NOTICE,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server.bgrewritebuf));
1362 /* Now our work is to rename the temp file into the stable file. And
1363 * switch the file descriptor used by the server for append only. */
1364 if (rename(tmpfile,server.appendfilename) == -1) {
1365 redisLog(REDIS_WARNING,"Can't rename the temp append only file into the stable one: %s", strerror(errno));
1366 close(fd);
1367 goto cleanup;
1368 }
1369 /* Mission completed... almost */
1370 redisLog(REDIS_NOTICE,"Append only file successfully rewritten.");
1371 if (server.appendfd != -1) {
1372 /* If append only is actually enabled... */
1373 close(server.appendfd);
1374 server.appendfd = fd;
1375 fsync(fd);
1376 server.appendseldb = -1; /* Make sure it will issue SELECT */
1377 redisLog(REDIS_NOTICE,"The new append only file was selected for future appends.");
1378 } else {
1379 /* If append only is disabled we just generate a dump in this
1380 * format. Why not? */
1381 close(fd);
1382 }
1383 } else if (!bysignal && exitcode != 0) {
1384 redisLog(REDIS_WARNING, "Background append only file rewriting error");
1385 } else {
1386 redisLog(REDIS_WARNING,
1387 "Background append only file rewriting terminated by signal %d",
1388 WTERMSIG(statloc));
1389 }
1390 cleanup:
1391 sdsfree(server.bgrewritebuf);
1392 server.bgrewritebuf = sdsempty();
1393 aofRemoveTempFile(server.bgrewritechildpid);
1394 server.bgrewritechildpid = -1;
1395 }
1396
1397 /* This function is called once a background process of some kind terminates,
1398 * as we want to avoid resizing the hash tables when there is a child in order
1399 * to play well with copy-on-write (otherwise when a resize happens lots of
1400 * memory pages are copied). The goal of this function is to update the ability
1401 * for dict.c to resize the hash tables accordingly to the fact we have o not
1402 * running childs. */
1403 static void updateDictResizePolicy(void) {
1404 if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1)
1405 dictEnableResize();
1406 else
1407 dictDisableResize();
1408 }
1409
1410 static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
1411 int j, loops = server.cronloops++;
1412 REDIS_NOTUSED(eventLoop);
1413 REDIS_NOTUSED(id);
1414 REDIS_NOTUSED(clientData);
1415
1416 /* We take a cached value of the unix time in the global state because
1417 * with virtual memory and aging there is to store the current time
1418 * in objects at every object access, and accuracy is not needed.
1419 * To access a global var is faster than calling time(NULL) */
1420 server.unixtime = time(NULL);
1421
1422 /* Show some info about non-empty databases */
1423 for (j = 0; j < server.dbnum; j++) {
1424 long long size, used, vkeys;
1425
1426 size = dictSlots(server.db[j].dict);
1427 used = dictSize(server.db[j].dict);
1428 vkeys = dictSize(server.db[j].expires);
1429 if (!(loops % 50) && (used || vkeys)) {
1430 redisLog(REDIS_VERBOSE,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j,used,vkeys,size);
1431 /* dictPrintStats(server.dict); */
1432 }
1433 }
1434
1435 /* We don't want to resize the hash tables while a bacground saving
1436 * is in progress: the saving child is created using fork() that is
1437 * implemented with a copy-on-write semantic in most modern systems, so
1438 * if we resize the HT while there is the saving child at work actually
1439 * a lot of memory movements in the parent will cause a lot of pages
1440 * copied. */
1441 if (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1) {
1442 if (!(loops % 10)) tryResizeHashTables();
1443 if (server.activerehashing) incrementallyRehash();
1444 }
1445
1446 /* Show information about connected clients */
1447 if (!(loops % 50)) {
1448 redisLog(REDIS_VERBOSE,"%d clients connected (%d slaves), %zu bytes in use",
1449 listLength(server.clients)-listLength(server.slaves),
1450 listLength(server.slaves),
1451 zmalloc_used_memory());
1452 }
1453
1454 /* Close connections of timedout clients */
1455 if ((server.maxidletime && !(loops % 100)) || server.blpop_blocked_clients)
1456 closeTimedoutClients();
1457
1458 /* Check if a background saving or AOF rewrite in progress terminated */
1459 if (server.bgsavechildpid != -1 || server.bgrewritechildpid != -1) {
1460 int statloc;
1461 pid_t pid;
1462
1463 if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
1464 if (pid == server.bgsavechildpid) {
1465 backgroundSaveDoneHandler(statloc);
1466 } else {
1467 backgroundRewriteDoneHandler(statloc);
1468 }
1469 updateDictResizePolicy();
1470 }
1471 } else {
1472 /* If there is not a background saving in progress check if
1473 * we have to save now */
1474 time_t now = time(NULL);
1475 for (j = 0; j < server.saveparamslen; j++) {
1476 struct saveparam *sp = server.saveparams+j;
1477
1478 if (server.dirty >= sp->changes &&
1479 now-server.lastsave > sp->seconds) {
1480 redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
1481 sp->changes, sp->seconds);
1482 rdbSaveBackground(server.dbfilename);
1483 break;
1484 }
1485 }
1486 }
1487
1488 /* Try to expire a few timed out keys. The algorithm used is adaptive and
1489 * will use few CPU cycles if there are few expiring keys, otherwise
1490 * it will get more aggressive to avoid that too much memory is used by
1491 * keys that can be removed from the keyspace. */
1492 for (j = 0; j < server.dbnum; j++) {
1493 int expired;
1494 redisDb *db = server.db+j;
1495
1496 /* Continue to expire if at the end of the cycle more than 25%
1497 * of the keys were expired. */
1498 do {
1499 long num = dictSize(db->expires);
1500 time_t now = time(NULL);
1501
1502 expired = 0;
1503 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
1504 num = REDIS_EXPIRELOOKUPS_PER_CRON;
1505 while (num--) {
1506 dictEntry *de;
1507 time_t t;
1508
1509 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
1510 t = (time_t) dictGetEntryVal(de);
1511 if (now > t) {
1512 deleteKey(db,dictGetEntryKey(de));
1513 expired++;
1514 server.stat_expiredkeys++;
1515 }
1516 }
1517 } while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4);
1518 }
1519
1520 /* Swap a few keys on disk if we are over the memory limit and VM
1521 * is enbled. Try to free objects from the free list first. */
1522 if (vmCanSwapOut()) {
1523 while (server.vm_enabled && zmalloc_used_memory() >
1524 server.vm_max_memory)
1525 {
1526 int retval;
1527
1528 if (tryFreeOneObjectFromFreelist() == REDIS_OK) continue;
1529 retval = (server.vm_max_threads == 0) ?
1530 vmSwapOneObjectBlocking() :
1531 vmSwapOneObjectThreaded();
1532 if (retval == REDIS_ERR && !(loops % 300) &&
1533 zmalloc_used_memory() >
1534 (server.vm_max_memory+server.vm_max_memory/10))
1535 {
1536 redisLog(REDIS_WARNING,"WARNING: vm-max-memory limit exceeded by more than 10%% but unable to swap more objects out!");
1537 }
1538 /* Note that when using threade I/O we free just one object,
1539 * because anyway when the I/O thread in charge to swap this
1540 * object out will finish, the handler of completed jobs
1541 * will try to swap more objects if we are still out of memory. */
1542 if (retval == REDIS_ERR || server.vm_max_threads > 0) break;
1543 }
1544 }
1545
1546 /* Check if we should connect to a MASTER */
1547 if (server.replstate == REDIS_REPL_CONNECT && !(loops % 10)) {
1548 redisLog(REDIS_NOTICE,"Connecting to MASTER...");
1549 if (syncWithMaster() == REDIS_OK) {
1550 redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync succeeded");
1551 if (server.appendonly) rewriteAppendOnlyFileBackground();
1552 }
1553 }
1554 return 100;
1555 }
1556
1557 /* This function gets called every time Redis is entering the
1558 * main loop of the event driven library, that is, before to sleep
1559 * for ready file descriptors. */
1560 static void beforeSleep(struct aeEventLoop *eventLoop) {
1561 REDIS_NOTUSED(eventLoop);
1562
1563 /* Awake clients that got all the swapped keys they requested */
1564 if (server.vm_enabled && listLength(server.io_ready_clients)) {
1565 listIter li;
1566 listNode *ln;
1567
1568 listRewind(server.io_ready_clients,&li);
1569 while((ln = listNext(&li))) {
1570 redisClient *c = ln->value;
1571 struct redisCommand *cmd;
1572
1573 /* Resume the client. */
1574 listDelNode(server.io_ready_clients,ln);
1575 c->flags &= (~REDIS_IO_WAIT);
1576 server.vm_blocked_clients--;
1577 aeCreateFileEvent(server.el, c->fd, AE_READABLE,
1578 readQueryFromClient, c);
1579 cmd = lookupCommand(c->argv[0]->ptr);
1580 assert(cmd != NULL);
1581 call(c,cmd);
1582 resetClient(c);
1583 /* There may be more data to process in the input buffer. */
1584 if (c->querybuf && sdslen(c->querybuf) > 0)
1585 processInputBuffer(c);
1586 }
1587 }
1588 /* Write the AOF buffer on disk */
1589 flushAppendOnlyFile();
1590 }
1591
1592 static void createSharedObjects(void) {
1593 int j;
1594
1595 shared.crlf = createObject(REDIS_STRING,sdsnew("\r\n"));
1596 shared.ok = createObject(REDIS_STRING,sdsnew("+OK\r\n"));
1597 shared.err = createObject(REDIS_STRING,sdsnew("-ERR\r\n"));
1598 shared.emptybulk = createObject(REDIS_STRING,sdsnew("$0\r\n\r\n"));
1599 shared.czero = createObject(REDIS_STRING,sdsnew(":0\r\n"));
1600 shared.cone = createObject(REDIS_STRING,sdsnew(":1\r\n"));
1601 shared.nullbulk = createObject(REDIS_STRING,sdsnew("$-1\r\n"));
1602 shared.nullmultibulk = createObject(REDIS_STRING,sdsnew("*-1\r\n"));
1603 shared.emptymultibulk = createObject(REDIS_STRING,sdsnew("*0\r\n"));
1604 shared.pong = createObject(REDIS_STRING,sdsnew("+PONG\r\n"));
1605 shared.queued = createObject(REDIS_STRING,sdsnew("+QUEUED\r\n"));
1606 shared.wrongtypeerr = createObject(REDIS_STRING,sdsnew(
1607 "-ERR Operation against a key holding the wrong kind of value\r\n"));
1608 shared.nokeyerr = createObject(REDIS_STRING,sdsnew(
1609 "-ERR no such key\r\n"));
1610 shared.syntaxerr = createObject(REDIS_STRING,sdsnew(
1611 "-ERR syntax error\r\n"));
1612 shared.sameobjecterr = createObject(REDIS_STRING,sdsnew(
1613 "-ERR source and destination objects are the same\r\n"));
1614 shared.outofrangeerr = createObject(REDIS_STRING,sdsnew(
1615 "-ERR index out of range\r\n"));
1616 shared.space = createObject(REDIS_STRING,sdsnew(" "));
1617 shared.colon = createObject(REDIS_STRING,sdsnew(":"));
1618 shared.plus = createObject(REDIS_STRING,sdsnew("+"));
1619 shared.select0 = createStringObject("select 0\r\n",10);
1620 shared.select1 = createStringObject("select 1\r\n",10);
1621 shared.select2 = createStringObject("select 2\r\n",10);
1622 shared.select3 = createStringObject("select 3\r\n",10);
1623 shared.select4 = createStringObject("select 4\r\n",10);
1624 shared.select5 = createStringObject("select 5\r\n",10);
1625 shared.select6 = createStringObject("select 6\r\n",10);
1626 shared.select7 = createStringObject("select 7\r\n",10);
1627 shared.select8 = createStringObject("select 8\r\n",10);
1628 shared.select9 = createStringObject("select 9\r\n",10);
1629 shared.messagebulk = createStringObject("$7\r\nmessage\r\n",13);
1630 shared.pmessagebulk = createStringObject("$8\r\npmessage\r\n",14);
1631 shared.subscribebulk = createStringObject("$9\r\nsubscribe\r\n",15);
1632 shared.unsubscribebulk = createStringObject("$11\r\nunsubscribe\r\n",18);
1633 shared.psubscribebulk = createStringObject("$10\r\npsubscribe\r\n",17);
1634 shared.punsubscribebulk = createStringObject("$12\r\npunsubscribe\r\n",19);
1635 shared.mbulk3 = createStringObject("*3\r\n",4);
1636 shared.mbulk4 = createStringObject("*4\r\n",4);
1637 for (j = 0; j < REDIS_SHARED_INTEGERS; j++) {
1638 shared.integers[j] = createObject(REDIS_STRING,(void*)(long)j);
1639 shared.integers[j]->encoding = REDIS_ENCODING_INT;
1640 }
1641 }
1642
1643 static void appendServerSaveParams(time_t seconds, int changes) {
1644 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
1645 server.saveparams[server.saveparamslen].seconds = seconds;
1646 server.saveparams[server.saveparamslen].changes = changes;
1647 server.saveparamslen++;
1648 }
1649
1650 static void resetServerSaveParams() {
1651 zfree(server.saveparams);
1652 server.saveparams = NULL;
1653 server.saveparamslen = 0;
1654 }
1655
1656 static void initServerConfig() {
1657 server.dbnum = REDIS_DEFAULT_DBNUM;
1658 server.port = REDIS_SERVERPORT;
1659 server.verbosity = REDIS_VERBOSE;
1660 server.maxidletime = REDIS_MAXIDLETIME;
1661 server.saveparams = NULL;
1662 server.logfile = NULL; /* NULL = log on standard output */
1663 server.bindaddr = NULL;
1664 server.glueoutputbuf = 1;
1665 server.daemonize = 0;
1666 server.appendonly = 0;
1667 server.appendfsync = APPENDFSYNC_EVERYSEC;
1668 server.lastfsync = time(NULL);
1669 server.appendfd = -1;
1670 server.appendseldb = -1; /* Make sure the first time will not match */
1671 server.pidfile = zstrdup("/var/run/redis.pid");
1672 server.dbfilename = zstrdup("dump.rdb");
1673 server.appendfilename = zstrdup("appendonly.aof");
1674 server.requirepass = NULL;
1675 server.rdbcompression = 1;
1676 server.activerehashing = 1;
1677 server.maxclients = 0;
1678 server.blpop_blocked_clients = 0;
1679 server.maxmemory = 0;
1680 server.vm_enabled = 0;
1681 server.vm_swap_file = zstrdup("/tmp/redis-%p.vm");
1682 server.vm_page_size = 256; /* 256 bytes per page */
1683 server.vm_pages = 1024*1024*100; /* 104 millions of pages */
1684 server.vm_max_memory = 1024LL*1024*1024*1; /* 1 GB of RAM */
1685 server.vm_max_threads = 4;
1686 server.vm_blocked_clients = 0;
1687 server.hash_max_zipmap_entries = REDIS_HASH_MAX_ZIPMAP_ENTRIES;
1688 server.hash_max_zipmap_value = REDIS_HASH_MAX_ZIPMAP_VALUE;
1689
1690 resetServerSaveParams();
1691
1692 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1693 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1694 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1695 /* Replication related */
1696 server.isslave = 0;
1697 server.masterauth = NULL;
1698 server.masterhost = NULL;
1699 server.masterport = 6379;
1700 server.master = NULL;
1701 server.replstate = REDIS_REPL_NONE;
1702
1703 /* Double constants initialization */
1704 R_Zero = 0.0;
1705 R_PosInf = 1.0/R_Zero;
1706 R_NegInf = -1.0/R_Zero;
1707 R_Nan = R_Zero/R_Zero;
1708 }
1709
1710 static void initServer() {
1711 int j;
1712
1713 signal(SIGHUP, SIG_IGN);
1714 signal(SIGPIPE, SIG_IGN);
1715 setupSigSegvAction();
1716
1717 server.devnull = fopen("/dev/null","w");
1718 if (server.devnull == NULL) {
1719 redisLog(REDIS_WARNING, "Can't open /dev/null: %s", server.neterr);
1720 exit(1);
1721 }
1722 server.clients = listCreate();
1723 server.slaves = listCreate();
1724 server.monitors = listCreate();
1725 server.objfreelist = listCreate();
1726 createSharedObjects();
1727 server.el = aeCreateEventLoop();
1728 server.db = zmalloc(sizeof(redisDb)*server.dbnum);
1729 server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr);
1730 if (server.fd == -1) {
1731 redisLog(REDIS_WARNING, "Opening TCP port: %s", server.neterr);
1732 exit(1);
1733 }
1734 for (j = 0; j < server.dbnum; j++) {
1735 server.db[j].dict = dictCreate(&dbDictType,NULL);
1736 server.db[j].expires = dictCreate(&keyptrDictType,NULL);
1737 server.db[j].blockingkeys = dictCreate(&keylistDictType,NULL);
1738 if (server.vm_enabled)
1739 server.db[j].io_keys = dictCreate(&keylistDictType,NULL);
1740 server.db[j].id = j;
1741 }
1742 server.pubsub_channels = dictCreate(&keylistDictType,NULL);
1743 server.pubsub_patterns = listCreate();
1744 listSetFreeMethod(server.pubsub_patterns,freePubsubPattern);
1745 listSetMatchMethod(server.pubsub_patterns,listMatchPubsubPattern);
1746 server.cronloops = 0;
1747 server.bgsavechildpid = -1;
1748 server.bgrewritechildpid = -1;
1749 server.bgrewritebuf = sdsempty();
1750 server.aofbuf = sdsempty();
1751 server.lastsave = time(NULL);
1752 server.dirty = 0;
1753 server.stat_numcommands = 0;
1754 server.stat_numconnections = 0;
1755 server.stat_expiredkeys = 0;
1756 server.stat_starttime = time(NULL);
1757 server.unixtime = time(NULL);
1758 aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
1759 if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,
1760 acceptHandler, NULL) == AE_ERR) oom("creating file event");
1761
1762 if (server.appendonly) {
1763 server.appendfd = open(server.appendfilename,O_WRONLY|O_APPEND|O_CREAT,0644);
1764 if (server.appendfd == -1) {
1765 redisLog(REDIS_WARNING, "Can't open the append-only file: %s",
1766 strerror(errno));
1767 exit(1);
1768 }
1769 }
1770
1771 if (server.vm_enabled) vmInit();
1772 }
1773
1774 /* Empty the whole database */
1775 static long long emptyDb() {
1776 int j;
1777 long long removed = 0;
1778
1779 for (j = 0; j < server.dbnum; j++) {
1780 removed += dictSize(server.db[j].dict);
1781 dictEmpty(server.db[j].dict);
1782 dictEmpty(server.db[j].expires);
1783 }
1784 return removed;
1785 }
1786
1787 static int yesnotoi(char *s) {
1788 if (!strcasecmp(s,"yes")) return 1;
1789 else if (!strcasecmp(s,"no")) return 0;
1790 else return -1;
1791 }
1792
1793 /* I agree, this is a very rudimental way to load a configuration...
1794 will improve later if the config gets more complex */
1795 static void loadServerConfig(char *filename) {
1796 FILE *fp;
1797 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
1798 int linenum = 0;
1799 sds line = NULL;
1800
1801 if (filename[0] == '-' && filename[1] == '\0')
1802 fp = stdin;
1803 else {
1804 if ((fp = fopen(filename,"r")) == NULL) {
1805 redisLog(REDIS_WARNING, "Fatal error, can't open config file '%s'", filename);
1806 exit(1);
1807 }
1808 }
1809
1810 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
1811 sds *argv;
1812 int argc, j;
1813
1814 linenum++;
1815 line = sdsnew(buf);
1816 line = sdstrim(line," \t\r\n");
1817
1818 /* Skip comments and blank lines*/
1819 if (line[0] == '#' || line[0] == '\0') {
1820 sdsfree(line);
1821 continue;
1822 }
1823
1824 /* Split into arguments */
1825 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
1826 sdstolower(argv[0]);
1827
1828 /* Execute config directives */
1829 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
1830 server.maxidletime = atoi(argv[1]);
1831 if (server.maxidletime < 0) {
1832 err = "Invalid timeout value"; goto loaderr;
1833 }
1834 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
1835 server.port = atoi(argv[1]);
1836 if (server.port < 1 || server.port > 65535) {
1837 err = "Invalid port"; goto loaderr;
1838 }
1839 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
1840 server.bindaddr = zstrdup(argv[1]);
1841 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
1842 int seconds = atoi(argv[1]);
1843 int changes = atoi(argv[2]);
1844 if (seconds < 1 || changes < 0) {
1845 err = "Invalid save parameters"; goto loaderr;
1846 }
1847 appendServerSaveParams(seconds,changes);
1848 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
1849 if (chdir(argv[1]) == -1) {
1850 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
1851 argv[1], strerror(errno));
1852 exit(1);
1853 }
1854 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
1855 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
1856 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
1857 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
1858 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
1859 else {
1860 err = "Invalid log level. Must be one of debug, notice, warning";
1861 goto loaderr;
1862 }
1863 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
1864 FILE *logfp;
1865
1866 server.logfile = zstrdup(argv[1]);
1867 if (!strcasecmp(server.logfile,"stdout")) {
1868 zfree(server.logfile);
1869 server.logfile = NULL;
1870 }
1871 if (server.logfile) {
1872 /* Test if we are able to open the file. The server will not
1873 * be able to abort just for this problem later... */
1874 logfp = fopen(server.logfile,"a");
1875 if (logfp == NULL) {
1876 err = sdscatprintf(sdsempty(),
1877 "Can't open the log file: %s", strerror(errno));
1878 goto loaderr;
1879 }
1880 fclose(logfp);
1881 }
1882 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
1883 server.dbnum = atoi(argv[1]);
1884 if (server.dbnum < 1) {
1885 err = "Invalid number of databases"; goto loaderr;
1886 }
1887 } else if (!strcasecmp(argv[0],"include") && argc == 2) {
1888 loadServerConfig(argv[1]);
1889 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
1890 server.maxclients = atoi(argv[1]);
1891 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
1892 server.maxmemory = memtoll(argv[1],NULL);
1893 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
1894 server.masterhost = sdsnew(argv[1]);
1895 server.masterport = atoi(argv[2]);
1896 server.replstate = REDIS_REPL_CONNECT;
1897 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
1898 server.masterauth = zstrdup(argv[1]);
1899 } else if (!strcasecmp(argv[0],"glueoutputbuf") && argc == 2) {
1900 if ((server.glueoutputbuf = yesnotoi(argv[1])) == -1) {
1901 err = "argument must be 'yes' or 'no'"; goto loaderr;
1902 }
1903 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
1904 if ((server.rdbcompression = yesnotoi(argv[1])) == -1) {
1905 err = "argument must be 'yes' or 'no'"; goto loaderr;
1906 }
1907 } else if (!strcasecmp(argv[0],"activerehashing") && argc == 2) {
1908 if ((server.activerehashing = yesnotoi(argv[1])) == -1) {
1909 err = "argument must be 'yes' or 'no'"; goto loaderr;
1910 }
1911 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
1912 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
1913 err = "argument must be 'yes' or 'no'"; goto loaderr;
1914 }
1915 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
1916 if ((server.appendonly = yesnotoi(argv[1])) == -1) {
1917 err = "argument must be 'yes' or 'no'"; goto loaderr;
1918 }
1919 } else if (!strcasecmp(argv[0],"appendfilename") && argc == 2) {
1920 zfree(server.appendfilename);
1921 server.appendfilename = zstrdup(argv[1]);
1922 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
1923 if (!strcasecmp(argv[1],"no")) {
1924 server.appendfsync = APPENDFSYNC_NO;
1925 } else if (!strcasecmp(argv[1],"always")) {
1926 server.appendfsync = APPENDFSYNC_ALWAYS;
1927 } else if (!strcasecmp(argv[1],"everysec")) {
1928 server.appendfsync = APPENDFSYNC_EVERYSEC;
1929 } else {
1930 err = "argument must be 'no', 'always' or 'everysec'";
1931 goto loaderr;
1932 }
1933 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
1934 server.requirepass = zstrdup(argv[1]);
1935 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
1936 zfree(server.pidfile);
1937 server.pidfile = zstrdup(argv[1]);
1938 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
1939 zfree(server.dbfilename);
1940 server.dbfilename = zstrdup(argv[1]);
1941 } else if (!strcasecmp(argv[0],"vm-enabled") && argc == 2) {
1942 if ((server.vm_enabled = yesnotoi(argv[1])) == -1) {
1943 err = "argument must be 'yes' or 'no'"; goto loaderr;
1944 }
1945 } else if (!strcasecmp(argv[0],"vm-swap-file") && argc == 2) {
1946 zfree(server.vm_swap_file);
1947 server.vm_swap_file = zstrdup(argv[1]);
1948 } else if (!strcasecmp(argv[0],"vm-max-memory") && argc == 2) {
1949 server.vm_max_memory = memtoll(argv[1],NULL);
1950 } else if (!strcasecmp(argv[0],"vm-page-size") && argc == 2) {
1951 server.vm_page_size = memtoll(argv[1], NULL);
1952 } else if (!strcasecmp(argv[0],"vm-pages") && argc == 2) {
1953 server.vm_pages = memtoll(argv[1], NULL);
1954 } else if (!strcasecmp(argv[0],"vm-max-threads") && argc == 2) {
1955 server.vm_max_threads = strtoll(argv[1], NULL, 10);
1956 } else if (!strcasecmp(argv[0],"hash-max-zipmap-entries") && argc == 2){
1957 server.hash_max_zipmap_entries = memtoll(argv[1], NULL);
1958 } else if (!strcasecmp(argv[0],"hash-max-zipmap-value") && argc == 2){
1959 server.hash_max_zipmap_value = memtoll(argv[1], NULL);
1960 } else {
1961 err = "Bad directive or wrong number of arguments"; goto loaderr;
1962 }
1963 for (j = 0; j < argc; j++)
1964 sdsfree(argv[j]);
1965 zfree(argv);
1966 sdsfree(line);
1967 }
1968 if (fp != stdin) fclose(fp);
1969 return;
1970
1971 loaderr:
1972 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
1973 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
1974 fprintf(stderr, ">>> '%s'\n", line);
1975 fprintf(stderr, "%s\n", err);
1976 exit(1);
1977 }
1978
1979 static void freeClientArgv(redisClient *c) {
1980 int j;
1981
1982 for (j = 0; j < c->argc; j++)
1983 decrRefCount(c->argv[j]);
1984 for (j = 0; j < c->mbargc; j++)
1985 decrRefCount(c->mbargv[j]);
1986 c->argc = 0;
1987 c->mbargc = 0;
1988 }
1989
1990 static void freeClient(redisClient *c) {
1991 listNode *ln;
1992
1993 /* Note that if the client we are freeing is blocked into a blocking
1994 * call, we have to set querybuf to NULL *before* to call
1995 * unblockClientWaitingData() to avoid processInputBuffer() will get
1996 * called. Also it is important to remove the file events after
1997 * this, because this call adds the READABLE event. */
1998 sdsfree(c->querybuf);
1999 c->querybuf = NULL;
2000 if (c->flags & REDIS_BLOCKED)
2001 unblockClientWaitingData(c);
2002
2003 /* Unsubscribe from all the pubsub channels */
2004 pubsubUnsubscribeAllChannels(c,0);
2005 pubsubUnsubscribeAllPatterns(c,0);
2006 dictRelease(c->pubsub_channels);
2007 listRelease(c->pubsub_patterns);
2008 /* Obvious cleanup */
2009 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
2010 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2011 listRelease(c->reply);
2012 freeClientArgv(c);
2013 close(c->fd);
2014 /* Remove from the list of clients */
2015 ln = listSearchKey(server.clients,c);
2016 redisAssert(ln != NULL);
2017 listDelNode(server.clients,ln);
2018 /* Remove from the list of clients waiting for swapped keys */
2019 if (c->flags & REDIS_IO_WAIT && listLength(c->io_keys) == 0) {
2020 ln = listSearchKey(server.io_ready_clients,c);
2021 if (ln) {
2022 listDelNode(server.io_ready_clients,ln);
2023 server.vm_blocked_clients--;
2024 }
2025 }
2026 while (server.vm_enabled && listLength(c->io_keys)) {
2027 ln = listFirst(c->io_keys);
2028 dontWaitForSwappedKey(c,ln->value);
2029 }
2030 listRelease(c->io_keys);
2031 /* Master/slave cleanup */
2032 if (c->flags & REDIS_SLAVE) {
2033 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
2034 close(c->repldbfd);
2035 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
2036 ln = listSearchKey(l,c);
2037 redisAssert(ln != NULL);
2038 listDelNode(l,ln);
2039 }
2040 if (c->flags & REDIS_MASTER) {
2041 server.master = NULL;
2042 server.replstate = REDIS_REPL_CONNECT;
2043 }
2044 /* Release memory */
2045 zfree(c->argv);
2046 zfree(c->mbargv);
2047 freeClientMultiState(c);
2048 zfree(c);
2049 }
2050
2051 #define GLUEREPLY_UP_TO (1024)
2052 static void glueReplyBuffersIfNeeded(redisClient *c) {
2053 int copylen = 0;
2054 char buf[GLUEREPLY_UP_TO];
2055 listNode *ln;
2056 listIter li;
2057 robj *o;
2058
2059 listRewind(c->reply,&li);
2060 while((ln = listNext(&li))) {
2061 int objlen;
2062
2063 o = ln->value;
2064 objlen = sdslen(o->ptr);
2065 if (copylen + objlen <= GLUEREPLY_UP_TO) {
2066 memcpy(buf+copylen,o->ptr,objlen);
2067 copylen += objlen;
2068 listDelNode(c->reply,ln);
2069 } else {
2070 if (copylen == 0) return;
2071 break;
2072 }
2073 }
2074 /* Now the output buffer is empty, add the new single element */
2075 o = createObject(REDIS_STRING,sdsnewlen(buf,copylen));
2076 listAddNodeHead(c->reply,o);
2077 }
2078
2079 static void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
2080 redisClient *c = privdata;
2081 int nwritten = 0, totwritten = 0, objlen;
2082 robj *o;
2083 REDIS_NOTUSED(el);
2084 REDIS_NOTUSED(mask);
2085
2086 /* Use writev() if we have enough buffers to send */
2087 if (!server.glueoutputbuf &&
2088 listLength(c->reply) > REDIS_WRITEV_THRESHOLD &&
2089 !(c->flags & REDIS_MASTER))
2090 {
2091 sendReplyToClientWritev(el, fd, privdata, mask);
2092 return;
2093 }
2094
2095 while(listLength(c->reply)) {
2096 if (server.glueoutputbuf && listLength(c->reply) > 1)
2097 glueReplyBuffersIfNeeded(c);
2098
2099 o = listNodeValue(listFirst(c->reply));
2100 objlen = sdslen(o->ptr);
2101
2102 if (objlen == 0) {
2103 listDelNode(c->reply,listFirst(c->reply));
2104 continue;
2105 }
2106
2107 if (c->flags & REDIS_MASTER) {
2108 /* Don't reply to a master */
2109 nwritten = objlen - c->sentlen;
2110 } else {
2111 nwritten = write(fd, ((char*)o->ptr)+c->sentlen, objlen - c->sentlen);
2112 if (nwritten <= 0) break;
2113 }
2114 c->sentlen += nwritten;
2115 totwritten += nwritten;
2116 /* If we fully sent the object on head go to the next one */
2117 if (c->sentlen == objlen) {
2118 listDelNode(c->reply,listFirst(c->reply));
2119 c->sentlen = 0;
2120 }
2121 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
2122 * bytes, in a single threaded server it's a good idea to serve
2123 * other clients as well, even if a very large request comes from
2124 * super fast link that is always able to accept data (in real world
2125 * scenario think about 'KEYS *' against the loopback interfae) */
2126 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
2127 }
2128 if (nwritten == -1) {
2129 if (errno == EAGAIN) {
2130 nwritten = 0;
2131 } else {
2132 redisLog(REDIS_VERBOSE,
2133 "Error writing to client: %s", strerror(errno));
2134 freeClient(c);
2135 return;
2136 }
2137 }
2138 if (totwritten > 0) c->lastinteraction = time(NULL);
2139 if (listLength(c->reply) == 0) {
2140 c->sentlen = 0;
2141 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2142 }
2143 }
2144
2145 static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask)
2146 {
2147 redisClient *c = privdata;
2148 int nwritten = 0, totwritten = 0, objlen, willwrite;
2149 robj *o;
2150 struct iovec iov[REDIS_WRITEV_IOVEC_COUNT];
2151 int offset, ion = 0;
2152 REDIS_NOTUSED(el);
2153 REDIS_NOTUSED(mask);
2154
2155 listNode *node;
2156 while (listLength(c->reply)) {
2157 offset = c->sentlen;
2158 ion = 0;
2159 willwrite = 0;
2160
2161 /* fill-in the iov[] array */
2162 for(node = listFirst(c->reply); node; node = listNextNode(node)) {
2163 o = listNodeValue(node);
2164 objlen = sdslen(o->ptr);
2165
2166 if (totwritten + objlen - offset > REDIS_MAX_WRITE_PER_EVENT)
2167 break;
2168
2169 if(ion == REDIS_WRITEV_IOVEC_COUNT)
2170 break; /* no more iovecs */
2171
2172 iov[ion].iov_base = ((char*)o->ptr) + offset;
2173 iov[ion].iov_len = objlen - offset;
2174 willwrite += objlen - offset;
2175 offset = 0; /* just for the first item */
2176 ion++;
2177 }
2178
2179 if(willwrite == 0)
2180 break;
2181
2182 /* write all collected blocks at once */
2183 if((nwritten = writev(fd, iov, ion)) < 0) {
2184 if (errno != EAGAIN) {
2185 redisLog(REDIS_VERBOSE,
2186 "Error writing to client: %s", strerror(errno));
2187 freeClient(c);
2188 return;
2189 }
2190 break;
2191 }
2192
2193 totwritten += nwritten;
2194 offset = c->sentlen;
2195
2196 /* remove written robjs from c->reply */
2197 while (nwritten && listLength(c->reply)) {
2198 o = listNodeValue(listFirst(c->reply));
2199 objlen = sdslen(o->ptr);
2200
2201 if(nwritten >= objlen - offset) {
2202 listDelNode(c->reply, listFirst(c->reply));
2203 nwritten -= objlen - offset;
2204 c->sentlen = 0;
2205 } else {
2206 /* partial write */
2207 c->sentlen += nwritten;
2208 break;
2209 }
2210 offset = 0;
2211 }
2212 }
2213
2214 if (totwritten > 0)
2215 c->lastinteraction = time(NULL);
2216
2217 if (listLength(c->reply) == 0) {
2218 c->sentlen = 0;
2219 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
2220 }
2221 }
2222
2223 static struct redisCommand *lookupCommand(char *name) {
2224 int j = 0;
2225 while(cmdTable[j].name != NULL) {
2226 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
2227 j++;
2228 }
2229 return NULL;
2230 }
2231
2232 /* resetClient prepare the client to process the next command */
2233 static void resetClient(redisClient *c) {
2234 freeClientArgv(c);
2235 c->bulklen = -1;
2236 c->multibulk = 0;
2237 }
2238
2239 /* Call() is the core of Redis execution of a command */
2240 static void call(redisClient *c, struct redisCommand *cmd) {
2241 long long dirty;
2242
2243 dirty = server.dirty;
2244 cmd->proc(c);
2245 dirty = server.dirty-dirty;
2246
2247 if (server.appendonly && dirty)
2248 feedAppendOnlyFile(cmd,c->db->id,c->argv,c->argc);
2249 if ((dirty || cmd->flags & REDIS_CMD_FORCE_REPLICATION) &&
2250 listLength(server.slaves))
2251 replicationFeedSlaves(server.slaves,c->db->id,c->argv,c->argc);
2252 if (listLength(server.monitors))
2253 replicationFeedMonitors(server.monitors,c->db->id,c->argv,c->argc);
2254 server.stat_numcommands++;
2255 }
2256
2257 /* If this function gets called we already read a whole
2258 * command, argments are in the client argv/argc fields.
2259 * processCommand() execute the command or prepare the
2260 * server for a bulk read from the client.
2261 *
2262 * If 1 is returned the client is still alive and valid and
2263 * and other operations can be performed by the caller. Otherwise
2264 * if 0 is returned the client was destroied (i.e. after QUIT). */
2265 static int processCommand(redisClient *c) {
2266 struct redisCommand *cmd;
2267
2268 /* Free some memory if needed (maxmemory setting) */
2269 if (server.maxmemory) freeMemoryIfNeeded();
2270
2271 /* Handle the multi bulk command type. This is an alternative protocol
2272 * supported by Redis in order to receive commands that are composed of
2273 * multiple binary-safe "bulk" arguments. The latency of processing is
2274 * a bit higher but this allows things like multi-sets, so if this
2275 * protocol is used only for MSET and similar commands this is a big win. */
2276 if (c->multibulk == 0 && c->argc == 1 && ((char*)(c->argv[0]->ptr))[0] == '*') {
2277 c->multibulk = atoi(((char*)c->argv[0]->ptr)+1);
2278 if (c->multibulk <= 0) {
2279 resetClient(c);
2280 return 1;
2281 } else {
2282 decrRefCount(c->argv[c->argc-1]);
2283 c->argc--;
2284 return 1;
2285 }
2286 } else if (c->multibulk) {
2287 if (c->bulklen == -1) {
2288 if (((char*)c->argv[0]->ptr)[0] != '$') {
2289 addReplySds(c,sdsnew("-ERR multi bulk protocol error\r\n"));
2290 resetClient(c);
2291 return 1;
2292 } else {
2293 int bulklen = atoi(((char*)c->argv[0]->ptr)+1);
2294 decrRefCount(c->argv[0]);
2295 if (bulklen < 0 || bulklen > 1024*1024*1024) {
2296 c->argc--;
2297 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
2298 resetClient(c);
2299 return 1;
2300 }
2301 c->argc--;
2302 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
2303 return 1;
2304 }
2305 } else {
2306 c->mbargv = zrealloc(c->mbargv,(sizeof(robj*))*(c->mbargc+1));
2307 c->mbargv[c->mbargc] = c->argv[0];
2308 c->mbargc++;
2309 c->argc--;
2310 c->multibulk--;
2311 if (c->multibulk == 0) {
2312 robj **auxargv;
2313 int auxargc;
2314
2315 /* Here we need to swap the multi-bulk argc/argv with the
2316 * normal argc/argv of the client structure. */
2317 auxargv = c->argv;
2318 c->argv = c->mbargv;
2319 c->mbargv = auxargv;
2320
2321 auxargc = c->argc;
2322 c->argc = c->mbargc;
2323 c->mbargc = auxargc;
2324
2325 /* We need to set bulklen to something different than -1
2326 * in order for the code below to process the command without
2327 * to try to read the last argument of a bulk command as
2328 * a special argument. */
2329 c->bulklen = 0;
2330 /* continue below and process the command */
2331 } else {
2332 c->bulklen = -1;
2333 return 1;
2334 }
2335 }
2336 }
2337 /* -- end of multi bulk commands processing -- */
2338
2339 /* The QUIT command is handled as a special case. Normal command
2340 * procs are unable to close the client connection safely */
2341 if (!strcasecmp(c->argv[0]->ptr,"quit")) {
2342 freeClient(c);
2343 return 0;
2344 }
2345
2346 /* Now lookup the command and check ASAP about trivial error conditions
2347 * such wrong arity, bad command name and so forth. */
2348 cmd = lookupCommand(c->argv[0]->ptr);
2349 if (!cmd) {
2350 addReplySds(c,
2351 sdscatprintf(sdsempty(), "-ERR unknown command '%s'\r\n",
2352 (char*)c->argv[0]->ptr));
2353 resetClient(c);
2354 return 1;
2355 } else if ((cmd->arity > 0 && cmd->arity != c->argc) ||
2356 (c->argc < -cmd->arity)) {
2357 addReplySds(c,
2358 sdscatprintf(sdsempty(),
2359 "-ERR wrong number of arguments for '%s' command\r\n",
2360 cmd->name));
2361 resetClient(c);
2362 return 1;
2363 } else if (cmd->flags & REDIS_CMD_BULK && c->bulklen == -1) {
2364 /* This is a bulk command, we have to read the last argument yet. */
2365 int bulklen = atoi(c->argv[c->argc-1]->ptr);
2366
2367 decrRefCount(c->argv[c->argc-1]);
2368 if (bulklen < 0 || bulklen > 1024*1024*1024) {
2369 c->argc--;
2370 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
2371 resetClient(c);
2372 return 1;
2373 }
2374 c->argc--;
2375 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
2376 /* It is possible that the bulk read is already in the
2377 * buffer. Check this condition and handle it accordingly.
2378 * This is just a fast path, alternative to call processInputBuffer().
2379 * It's a good idea since the code is small and this condition
2380 * happens most of the times. */
2381 if ((signed)sdslen(c->querybuf) >= c->bulklen) {
2382 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2383 c->argc++;
2384 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
2385 } else {
2386 /* Otherwise return... there is to read the last argument
2387 * from the socket. */
2388 return 1;
2389 }
2390 }
2391 /* Let's try to encode the bulk object to save space. */
2392 if (cmd->flags & REDIS_CMD_BULK)
2393 c->argv[c->argc-1] = tryObjectEncoding(c->argv[c->argc-1]);
2394
2395 /* Check if the user is authenticated */
2396 if (server.requirepass && !c->authenticated && cmd->proc != authCommand) {
2397 addReplySds(c,sdsnew("-ERR operation not permitted\r\n"));
2398 resetClient(c);
2399 return 1;
2400 }
2401
2402 /* Handle the maxmemory directive */
2403 if (server.maxmemory && (cmd->flags & REDIS_CMD_DENYOOM) &&
2404 zmalloc_used_memory() > server.maxmemory)
2405 {
2406 addReplySds(c,sdsnew("-ERR command not allowed when used memory > 'maxmemory'\r\n"));
2407 resetClient(c);
2408 return 1;
2409 }
2410
2411 /* Only allow SUBSCRIBE and UNSUBSCRIBE in the context of Pub/Sub */
2412 if ((dictSize(c->pubsub_channels) > 0 || listLength(c->pubsub_patterns) > 0)
2413 &&
2414 cmd->proc != subscribeCommand && cmd->proc != unsubscribeCommand &&
2415 cmd->proc != psubscribeCommand && cmd->proc != punsubscribeCommand) {
2416 addReplySds(c,sdsnew("-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context\r\n"));
2417 resetClient(c);
2418 return 1;
2419 }
2420
2421 /* Exec the command */
2422 if (c->flags & REDIS_MULTI && cmd->proc != execCommand && cmd->proc != discardCommand) {
2423 queueMultiCommand(c,cmd);
2424 addReply(c,shared.queued);
2425 } else {
2426 if (server.vm_enabled && server.vm_max_threads > 0 &&
2427 blockClientOnSwappedKeys(c,cmd)) return 1;
2428 call(c,cmd);
2429 }
2430
2431 /* Prepare the client for the next command */
2432 resetClient(c);
2433 return 1;
2434 }
2435
2436 static void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) {
2437 listNode *ln;
2438 listIter li;
2439 int outc = 0, j;
2440 robj **outv;
2441 /* We need 1+(ARGS*3) objects since commands are using the new protocol
2442 * and we one 1 object for the first "*<count>\r\n" multibulk count, then
2443 * for every additional object we have "$<count>\r\n" + object + "\r\n". */
2444 robj *static_outv[REDIS_STATIC_ARGS*3+1];
2445 robj *lenobj;
2446
2447 if (argc <= REDIS_STATIC_ARGS) {
2448 outv = static_outv;
2449 } else {
2450 outv = zmalloc(sizeof(robj*)*(argc*3+1));
2451 }
2452
2453 lenobj = createObject(REDIS_STRING,
2454 sdscatprintf(sdsempty(), "*%d\r\n", argc));
2455 lenobj->refcount = 0;
2456 outv[outc++] = lenobj;
2457 for (j = 0; j < argc; j++) {
2458 lenobj = createObject(REDIS_STRING,
2459 sdscatprintf(sdsempty(),"$%lu\r\n",
2460 (unsigned long) stringObjectLen(argv[j])));
2461 lenobj->refcount = 0;
2462 outv[outc++] = lenobj;
2463 outv[outc++] = argv[j];
2464 outv[outc++] = shared.crlf;
2465 }
2466
2467 /* Increment all the refcounts at start and decrement at end in order to
2468 * be sure to free objects if there is no slave in a replication state
2469 * able to be feed with commands */
2470 for (j = 0; j < outc; j++) incrRefCount(outv[j]);
2471 listRewind(slaves,&li);
2472 while((ln = listNext(&li))) {
2473 redisClient *slave = ln->value;
2474
2475 /* Don't feed slaves that are still waiting for BGSAVE to start */
2476 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue;
2477
2478 /* Feed all the other slaves, MONITORs and so on */
2479 if (slave->slaveseldb != dictid) {
2480 robj *selectcmd;
2481
2482 switch(dictid) {
2483 case 0: selectcmd = shared.select0; break;
2484 case 1: selectcmd = shared.select1; break;
2485 case 2: selectcmd = shared.select2; break;
2486 case 3: selectcmd = shared.select3; break;
2487 case 4: selectcmd = shared.select4; break;
2488 case 5: selectcmd = shared.select5; break;
2489 case 6: selectcmd = shared.select6; break;
2490 case 7: selectcmd = shared.select7; break;
2491 case 8: selectcmd = shared.select8; break;
2492 case 9: selectcmd = shared.select9; break;
2493 default:
2494 selectcmd = createObject(REDIS_STRING,
2495 sdscatprintf(sdsempty(),"select %d\r\n",dictid));
2496 selectcmd->refcount = 0;
2497 break;
2498 }
2499 addReply(slave,selectcmd);
2500 slave->slaveseldb = dictid;
2501 }
2502 for (j = 0; j < outc; j++) addReply(slave,outv[j]);
2503 }
2504 for (j = 0; j < outc; j++) decrRefCount(outv[j]);
2505 if (outv != static_outv) zfree(outv);
2506 }
2507
2508 static sds sdscatrepr(sds s, char *p, size_t len) {
2509 s = sdscatlen(s,"\"",1);
2510 while(len--) {
2511 switch(*p) {
2512 case '\\':
2513 case '"':
2514 s = sdscatprintf(s,"\\%c",*p);
2515 break;
2516 case '\n': s = sdscatlen(s,"\\n",1); break;
2517 case '\r': s = sdscatlen(s,"\\r",1); break;
2518 case '\t': s = sdscatlen(s,"\\t",1); break;
2519 case '\a': s = sdscatlen(s,"\\a",1); break;
2520 case '\b': s = sdscatlen(s,"\\b",1); break;
2521 default:
2522 if (isprint(*p))
2523 s = sdscatprintf(s,"%c",*p);
2524 else
2525 s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
2526 break;
2527 }
2528 p++;
2529 }
2530 return sdscatlen(s,"\"",1);
2531 }
2532
2533 static void replicationFeedMonitors(list *monitors, int dictid, robj **argv, int argc) {
2534 listNode *ln;
2535 listIter li;
2536 int j;
2537 sds cmdrepr = sdsnew("+");
2538 robj *cmdobj;
2539 struct timeval tv;
2540
2541 gettimeofday(&tv,NULL);
2542 cmdrepr = sdscatprintf(cmdrepr,"%ld.%ld ",(long)tv.tv_sec,(long)tv.tv_usec);
2543 if (dictid != 0) cmdrepr = sdscatprintf(cmdrepr,"(db %d) ", dictid);
2544
2545 for (j = 0; j < argc; j++) {
2546 if (argv[j]->encoding == REDIS_ENCODING_INT) {
2547 cmdrepr = sdscatprintf(cmdrepr, "%ld", (long)argv[j]->ptr);
2548 } else {
2549 cmdrepr = sdscatrepr(cmdrepr,(char*)argv[j]->ptr,
2550 sdslen(argv[j]->ptr));
2551 }
2552 if (j != argc-1)
2553 cmdrepr = sdscatlen(cmdrepr," ",1);
2554 }
2555 cmdrepr = sdscatlen(cmdrepr,"\r\n",2);
2556 cmdobj = createObject(REDIS_STRING,cmdrepr);
2557
2558 listRewind(monitors,&li);
2559 while((ln = listNext(&li))) {
2560 redisClient *monitor = ln->value;
2561 addReply(monitor,cmdobj);
2562 }
2563 decrRefCount(cmdobj);
2564 }
2565
2566 static void processInputBuffer(redisClient *c) {
2567 again:
2568 /* Before to process the input buffer, make sure the client is not
2569 * waitig for a blocking operation such as BLPOP. Note that the first
2570 * iteration the client is never blocked, otherwise the processInputBuffer
2571 * would not be called at all, but after the execution of the first commands
2572 * in the input buffer the client may be blocked, and the "goto again"
2573 * will try to reiterate. The following line will make it return asap. */
2574 if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return;
2575 if (c->bulklen == -1) {
2576 /* Read the first line of the query */
2577 char *p = strchr(c->querybuf,'\n');
2578 size_t querylen;
2579
2580 if (p) {
2581 sds query, *argv;
2582 int argc, j;
2583
2584 query = c->querybuf;
2585 c->querybuf = sdsempty();
2586 querylen = 1+(p-(query));
2587 if (sdslen(query) > querylen) {
2588 /* leave data after the first line of the query in the buffer */
2589 c->querybuf = sdscatlen(c->querybuf,query+querylen,sdslen(query)-querylen);
2590 }
2591 *p = '\0'; /* remove "\n" */
2592 if (*(p-1) == '\r') *(p-1) = '\0'; /* and "\r" if any */
2593 sdsupdatelen(query);
2594
2595 /* Now we can split the query in arguments */
2596 argv = sdssplitlen(query,sdslen(query)," ",1,&argc);
2597 sdsfree(query);
2598
2599 if (c->argv) zfree(c->argv);
2600 c->argv = zmalloc(sizeof(robj*)*argc);
2601
2602 for (j = 0; j < argc; j++) {
2603 if (sdslen(argv[j])) {
2604 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
2605 c->argc++;
2606 } else {
2607 sdsfree(argv[j]);
2608 }
2609 }
2610 zfree(argv);
2611 if (c->argc) {
2612 /* Execute the command. If the client is still valid
2613 * after processCommand() return and there is something
2614 * on the query buffer try to process the next command. */
2615 if (processCommand(c) && sdslen(c->querybuf)) goto again;
2616 } else {
2617 /* Nothing to process, argc == 0. Just process the query
2618 * buffer if it's not empty or return to the caller */
2619 if (sdslen(c->querybuf)) goto again;
2620 }
2621 return;
2622 } else if (sdslen(c->querybuf) >= REDIS_REQUEST_MAX_SIZE) {
2623 redisLog(REDIS_VERBOSE, "Client protocol error");
2624 freeClient(c);
2625 return;
2626 }
2627 } else {
2628 /* Bulk read handling. Note that if we are at this point
2629 the client already sent a command terminated with a newline,
2630 we are reading the bulk data that is actually the last
2631 argument of the command. */
2632 int qbl = sdslen(c->querybuf);
2633
2634 if (c->bulklen <= qbl) {
2635 /* Copy everything but the final CRLF as final argument */
2636 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2637 c->argc++;
2638 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
2639 /* Process the command. If the client is still valid after
2640 * the processing and there is more data in the buffer
2641 * try to parse it. */
2642 if (processCommand(c) && sdslen(c->querybuf)) goto again;
2643 return;
2644 }
2645 }
2646 }
2647
2648 static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
2649 redisClient *c = (redisClient*) privdata;
2650 char buf[REDIS_IOBUF_LEN];
2651 int nread;
2652 REDIS_NOTUSED(el);
2653 REDIS_NOTUSED(mask);
2654
2655 nread = read(fd, buf, REDIS_IOBUF_LEN);
2656 if (nread == -1) {
2657 if (errno == EAGAIN) {
2658 nread = 0;
2659 } else {
2660 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
2661 freeClient(c);
2662 return;
2663 }
2664 } else if (nread == 0) {
2665 redisLog(REDIS_VERBOSE, "Client closed connection");
2666 freeClient(c);
2667 return;
2668 }
2669 if (nread) {
2670 c->querybuf = sdscatlen(c->querybuf, buf, nread);
2671 c->lastinteraction = time(NULL);
2672 } else {
2673 return;
2674 }
2675 processInputBuffer(c);
2676 }
2677
2678 static int selectDb(redisClient *c, int id) {
2679 if (id < 0 || id >= server.dbnum)
2680 return REDIS_ERR;
2681 c->db = &server.db[id];
2682 return REDIS_OK;
2683 }
2684
2685 static void *dupClientReplyValue(void *o) {
2686 incrRefCount((robj*)o);
2687 return o;
2688 }
2689
2690 static int listMatchObjects(void *a, void *b) {
2691 return equalStringObjects(a,b);
2692 }
2693
2694 static redisClient *createClient(int fd) {
2695 redisClient *c = zmalloc(sizeof(*c));
2696
2697 anetNonBlock(NULL,fd);
2698 anetTcpNoDelay(NULL,fd);
2699 if (!c) return NULL;
2700 selectDb(c,0);
2701 c->fd = fd;
2702 c->querybuf = sdsempty();
2703 c->argc = 0;
2704 c->argv = NULL;
2705 c->bulklen = -1;
2706 c->multibulk = 0;
2707 c->mbargc = 0;
2708 c->mbargv = NULL;
2709 c->sentlen = 0;
2710 c->flags = 0;
2711 c->lastinteraction = time(NULL);
2712 c->authenticated = 0;
2713 c->replstate = REDIS_REPL_NONE;
2714 c->reply = listCreate();
2715 listSetFreeMethod(c->reply,decrRefCount);
2716 listSetDupMethod(c->reply,dupClientReplyValue);
2717 c->blockingkeys = NULL;
2718 c->blockingkeysnum = 0;
2719 c->io_keys = listCreate();
2720 listSetFreeMethod(c->io_keys,decrRefCount);
2721 c->pubsub_channels = dictCreate(&setDictType,NULL);
2722 c->pubsub_patterns = listCreate();
2723 listSetFreeMethod(c->pubsub_patterns,decrRefCount);
2724 listSetMatchMethod(c->pubsub_patterns,listMatchObjects);
2725 if (aeCreateFileEvent(server.el, c->fd, AE_READABLE,
2726 readQueryFromClient, c) == AE_ERR) {
2727 freeClient(c);
2728 return NULL;
2729 }
2730 listAddNodeTail(server.clients,c);
2731 initClientMultiState(c);
2732 return c;
2733 }
2734
2735 static void addReply(redisClient *c, robj *obj) {
2736 if (listLength(c->reply) == 0 &&
2737 (c->replstate == REDIS_REPL_NONE ||
2738 c->replstate == REDIS_REPL_ONLINE) &&
2739 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
2740 sendReplyToClient, c) == AE_ERR) return;
2741
2742 if (server.vm_enabled && obj->storage != REDIS_VM_MEMORY) {
2743 obj = dupStringObject(obj);
2744 obj->refcount = 0; /* getDecodedObject() will increment the refcount */
2745 }
2746 listAddNodeTail(c->reply,getDecodedObject(obj));
2747 }
2748
2749 static void addReplySds(redisClient *c, sds s) {
2750 robj *o = createObject(REDIS_STRING,s);
2751 addReply(c,o);
2752 decrRefCount(o);
2753 }
2754
2755 static void addReplyDouble(redisClient *c, double d) {
2756 char buf[128];
2757
2758 snprintf(buf,sizeof(buf),"%.17g",d);
2759 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
2760 (unsigned long) strlen(buf),buf));
2761 }
2762
2763 static void addReplyLongLong(redisClient *c, long long ll) {
2764 char buf[128];
2765 size_t len;
2766
2767 if (ll == 0) {
2768 addReply(c,shared.czero);
2769 return;
2770 } else if (ll == 1) {
2771 addReply(c,shared.cone);
2772 return;
2773 }
2774 buf[0] = ':';
2775 len = ll2string(buf+1,sizeof(buf)-1,ll);
2776 buf[len+1] = '\r';
2777 buf[len+2] = '\n';
2778 addReplySds(c,sdsnewlen(buf,len+3));
2779 }
2780
2781 static void addReplyUlong(redisClient *c, unsigned long ul) {
2782 char buf[128];
2783 size_t len;
2784
2785 if (ul == 0) {
2786 addReply(c,shared.czero);
2787 return;
2788 } else if (ul == 1) {
2789 addReply(c,shared.cone);
2790 return;
2791 }
2792 len = snprintf(buf,sizeof(buf),":%lu\r\n",ul);
2793 addReplySds(c,sdsnewlen(buf,len));
2794 }
2795
2796 static void addReplyBulkLen(redisClient *c, robj *obj) {
2797 size_t len, intlen;
2798 char buf[128];
2799
2800 if (obj->encoding == REDIS_ENCODING_RAW) {
2801 len = sdslen(obj->ptr);
2802 } else {
2803 long n = (long)obj->ptr;
2804
2805 /* Compute how many bytes will take this integer as a radix 10 string */
2806 len = 1;
2807 if (n < 0) {
2808 len++;
2809 n = -n;
2810 }
2811 while((n = n/10) != 0) {
2812 len++;
2813 }
2814 }
2815 buf[0] = '$';
2816 intlen = ll2string(buf+1,sizeof(buf)-1,(long long)len);
2817 buf[intlen+1] = '\r';
2818 buf[intlen+2] = '\n';
2819 addReplySds(c,sdsnewlen(buf,intlen+3));
2820 }
2821
2822 static void addReplyBulk(redisClient *c, robj *obj) {
2823 addReplyBulkLen(c,obj);
2824 addReply(c,obj);
2825 addReply(c,shared.crlf);
2826 }
2827
2828 /* In the CONFIG command we need to add vanilla C string as bulk replies */
2829 static void addReplyBulkCString(redisClient *c, char *s) {
2830 if (s == NULL) {
2831 addReply(c,shared.nullbulk);
2832 } else {
2833 robj *o = createStringObject(s,strlen(s));
2834 addReplyBulk(c,o);
2835 decrRefCount(o);
2836 }
2837 }
2838
2839 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
2840 int cport, cfd;
2841 char cip[128];
2842 redisClient *c;
2843 REDIS_NOTUSED(el);
2844 REDIS_NOTUSED(mask);
2845 REDIS_NOTUSED(privdata);
2846
2847 cfd = anetAccept(server.neterr, fd, cip, &cport);
2848 if (cfd == AE_ERR) {
2849 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
2850 return;
2851 }
2852 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
2853 if ((c = createClient(cfd)) == NULL) {
2854 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
2855 close(cfd); /* May be already closed, just ingore errors */
2856 return;
2857 }
2858 /* If maxclient directive is set and this is one client more... close the
2859 * connection. Note that we create the client instead to check before
2860 * for this condition, since now the socket is already set in nonblocking
2861 * mode and we can send an error for free using the Kernel I/O */
2862 if (server.maxclients && listLength(server.clients) > server.maxclients) {
2863 char *err = "-ERR max number of clients reached\r\n";
2864
2865 /* That's a best effort error message, don't check write errors */
2866 if (write(c->fd,err,strlen(err)) == -1) {
2867 /* Nothing to do, Just to avoid the warning... */
2868 }
2869 freeClient(c);
2870 return;
2871 }
2872 server.stat_numconnections++;
2873 }
2874
2875 /* ======================= Redis objects implementation ===================== */
2876
2877 static robj *createObject(int type, void *ptr) {
2878 robj *o;
2879
2880 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
2881 if (listLength(server.objfreelist)) {
2882 listNode *head = listFirst(server.objfreelist);
2883 o = listNodeValue(head);
2884 listDelNode(server.objfreelist,head);
2885 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
2886 } else {
2887 if (server.vm_enabled) {
2888 pthread_mutex_unlock(&server.obj_freelist_mutex);
2889 o = zmalloc(sizeof(*o));
2890 } else {
2891 o = zmalloc(sizeof(*o)-sizeof(struct redisObjectVM));
2892 }
2893 }
2894 o->type = type;
2895 o->encoding = REDIS_ENCODING_RAW;
2896 o->ptr = ptr;
2897 o->refcount = 1;
2898 if (server.vm_enabled) {
2899 /* Note that this code may run in the context of an I/O thread
2900 * and accessing to server.unixtime in theory is an error
2901 * (no locks). But in practice this is safe, and even if we read
2902 * garbage Redis will not fail, as it's just a statistical info */
2903 o->vm.atime = server.unixtime;
2904 o->storage = REDIS_VM_MEMORY;
2905 }
2906 return o;
2907 }
2908
2909 static robj *createStringObject(char *ptr, size_t len) {
2910 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
2911 }
2912
2913 static robj *createStringObjectFromLongLong(long long value) {
2914 robj *o;
2915 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
2916 incrRefCount(shared.integers[value]);
2917 o = shared.integers[value];
2918 } else {
2919 o = createObject(REDIS_STRING, NULL);
2920 if (value >= LONG_MIN && value <= LONG_MAX) {
2921 o->encoding = REDIS_ENCODING_INT;
2922 o->ptr = (void*)((long)value);
2923 } else {
2924 o = createObject(REDIS_STRING,sdsfromlonglong(value));
2925 }
2926 }
2927 return o;
2928 }
2929
2930 static robj *dupStringObject(robj *o) {
2931 assert(o->encoding == REDIS_ENCODING_RAW);
2932 return createStringObject(o->ptr,sdslen(o->ptr));
2933 }
2934
2935 static robj *createListObject(void) {
2936 list *l = listCreate();
2937
2938 listSetFreeMethod(l,decrRefCount);
2939 return createObject(REDIS_LIST,l);
2940 }
2941
2942 static robj *createSetObject(void) {
2943 dict *d = dictCreate(&setDictType,NULL);
2944 return createObject(REDIS_SET,d);
2945 }
2946
2947 static robj *createHashObject(void) {
2948 /* All the Hashes start as zipmaps. Will be automatically converted
2949 * into hash tables if there are enough elements or big elements
2950 * inside. */
2951 unsigned char *zm = zipmapNew();
2952 robj *o = createObject(REDIS_HASH,zm);
2953 o->encoding = REDIS_ENCODING_ZIPMAP;
2954 return o;
2955 }
2956
2957 static robj *createZsetObject(void) {
2958 zset *zs = zmalloc(sizeof(*zs));
2959
2960 zs->dict = dictCreate(&zsetDictType,NULL);
2961 zs->zsl = zslCreate();
2962 return createObject(REDIS_ZSET,zs);
2963 }
2964
2965 static void freeStringObject(robj *o) {
2966 if (o->encoding == REDIS_ENCODING_RAW) {
2967 sdsfree(o->ptr);
2968 }
2969 }
2970
2971 static void freeListObject(robj *o) {
2972 listRelease((list*) o->ptr);
2973 }
2974
2975 static void freeSetObject(robj *o) {
2976 dictRelease((dict*) o->ptr);
2977 }
2978
2979 static void freeZsetObject(robj *o) {
2980 zset *zs = o->ptr;
2981
2982 dictRelease(zs->dict);
2983 zslFree(zs->zsl);
2984 zfree(zs);
2985 }
2986
2987 static void freeHashObject(robj *o) {
2988 switch (o->encoding) {
2989 case REDIS_ENCODING_HT:
2990 dictRelease((dict*) o->ptr);
2991 break;
2992 case REDIS_ENCODING_ZIPMAP:
2993 zfree(o->ptr);
2994 break;
2995 default:
2996 redisPanic("Unknown hash encoding type");
2997 break;
2998 }
2999 }
3000
3001 static void incrRefCount(robj *o) {
3002 o->refcount++;
3003 }
3004
3005 static void decrRefCount(void *obj) {
3006 robj *o = obj;
3007
3008 if (o->refcount <= 0) redisPanic("decrRefCount against refcount <= 0");
3009 /* Object is a key of a swapped out value, or in the process of being
3010 * loaded. */
3011 if (server.vm_enabled &&
3012 (o->storage == REDIS_VM_SWAPPED || o->storage == REDIS_VM_LOADING))
3013 {
3014 if (o->storage == REDIS_VM_LOADING) vmCancelThreadedIOJob(obj);
3015 redisAssert(o->type == REDIS_STRING);
3016 freeStringObject(o);
3017 vmMarkPagesFree(o->vm.page,o->vm.usedpages);
3018 pthread_mutex_lock(&server.obj_freelist_mutex);
3019 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
3020 !listAddNodeHead(server.objfreelist,o))
3021 zfree(o);
3022 pthread_mutex_unlock(&server.obj_freelist_mutex);
3023 server.vm_stats_swapped_objects--;
3024 return;
3025 }
3026 /* Object is in memory, or in the process of being swapped out. */
3027 if (--(o->refcount) == 0) {
3028 if (server.vm_enabled && o->storage == REDIS_VM_SWAPPING)
3029 vmCancelThreadedIOJob(obj);
3030 switch(o->type) {
3031 case REDIS_STRING: freeStringObject(o); break;
3032 case REDIS_LIST: freeListObject(o); break;
3033 case REDIS_SET: freeSetObject(o); break;
3034 case REDIS_ZSET: freeZsetObject(o); break;
3035 case REDIS_HASH: freeHashObject(o); break;
3036 default: redisPanic("Unknown object type"); break;
3037 }
3038 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
3039 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
3040 !listAddNodeHead(server.objfreelist,o))
3041 zfree(o);
3042 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
3043 }
3044 }
3045
3046 static robj *lookupKey(redisDb *db, robj *key) {
3047 dictEntry *de = dictFind(db->dict,key);
3048 if (de) {
3049 robj *key = dictGetEntryKey(de);
3050 robj *val = dictGetEntryVal(de);
3051
3052 if (server.vm_enabled) {
3053 if (key->storage == REDIS_VM_MEMORY ||
3054 key->storage == REDIS_VM_SWAPPING)
3055 {
3056 /* If we were swapping the object out, stop it, this key
3057 * was requested. */
3058 if (key->storage == REDIS_VM_SWAPPING)
3059 vmCancelThreadedIOJob(key);
3060 /* Update the access time of the key for the aging algorithm. */
3061 key->vm.atime = server.unixtime;
3062 } else {
3063 int notify = (key->storage == REDIS_VM_LOADING);
3064
3065 /* Our value was swapped on disk. Bring it at home. */
3066 redisAssert(val == NULL);
3067 val = vmLoadObject(key);
3068 dictGetEntryVal(de) = val;
3069
3070 /* Clients blocked by the VM subsystem may be waiting for
3071 * this key... */
3072 if (notify) handleClientsBlockedOnSwappedKey(db,key);
3073 }
3074 }
3075 return val;
3076 } else {
3077 return NULL;
3078 }
3079 }
3080
3081 static robj *lookupKeyRead(redisDb *db, robj *key) {
3082 expireIfNeeded(db,key);
3083 return lookupKey(db,key);
3084 }
3085
3086 static robj *lookupKeyWrite(redisDb *db, robj *key) {
3087 deleteIfVolatile(db,key);
3088 return lookupKey(db,key);
3089 }
3090
3091 static robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply) {
3092 robj *o = lookupKeyRead(c->db, key);
3093 if (!o) addReply(c,reply);
3094 return o;
3095 }
3096
3097 static robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply) {
3098 robj *o = lookupKeyWrite(c->db, key);
3099 if (!o) addReply(c,reply);
3100 return o;
3101 }
3102
3103 static int checkType(redisClient *c, robj *o, int type) {
3104 if (o->type != type) {
3105 addReply(c,shared.wrongtypeerr);
3106 return 1;
3107 }
3108 return 0;
3109 }
3110
3111 static int deleteKey(redisDb *db, robj *key) {
3112 int retval;
3113
3114 /* We need to protect key from destruction: after the first dictDelete()
3115 * it may happen that 'key' is no longer valid if we don't increment
3116 * it's count. This may happen when we get the object reference directly
3117 * from the hash table with dictRandomKey() or dict iterators */
3118 incrRefCount(key);
3119 if (dictSize(db->expires)) dictDelete(db->expires,key);
3120 retval = dictDelete(db->dict,key);
3121 decrRefCount(key);
3122
3123 return retval == DICT_OK;
3124 }
3125
3126 /* Check if the nul-terminated string 's' can be represented by a long
3127 * (that is, is a number that fits into long without any other space or
3128 * character before or after the digits).
3129 *
3130 * If so, the function returns REDIS_OK and *longval is set to the value
3131 * of the number. Otherwise REDIS_ERR is returned */
3132 static int isStringRepresentableAsLong(sds s, long *longval) {
3133 char buf[32], *endptr;
3134 long value;
3135 int slen;
3136
3137 value = strtol(s, &endptr, 10);
3138 if (endptr[0] != '\0') return REDIS_ERR;
3139 slen = ll2string(buf,32,value);
3140
3141 /* If the number converted back into a string is not identical
3142 * then it's not possible to encode the string as integer */
3143 if (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR;
3144 if (longval) *longval = value;
3145 return REDIS_OK;
3146 }
3147
3148 /* Try to encode a string object in order to save space */
3149 static robj *tryObjectEncoding(robj *o) {
3150 long value;
3151 sds s = o->ptr;
3152
3153 if (o->encoding != REDIS_ENCODING_RAW)
3154 return o; /* Already encoded */
3155
3156 /* It's not safe to encode shared objects: shared objects can be shared
3157 * everywhere in the "object space" of Redis. Encoded objects can only
3158 * appear as "values" (and not, for instance, as keys) */
3159 if (o->refcount > 1) return o;
3160
3161 /* Currently we try to encode only strings */
3162 redisAssert(o->type == REDIS_STRING);
3163
3164 /* Check if we can represent this string as a long integer */
3165 if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return o;
3166
3167 /* Ok, this object can be encoded */
3168 if (value >= 0 && value < REDIS_SHARED_INTEGERS) {
3169 decrRefCount(o);
3170 incrRefCount(shared.integers[value]);
3171 return shared.integers[value];
3172 } else {
3173 o->encoding = REDIS_ENCODING_INT;
3174 sdsfree(o->ptr);
3175 o->ptr = (void*) value;
3176 return o;
3177 }
3178 }
3179
3180 /* Get a decoded version of an encoded object (returned as a new object).
3181 * If the object is already raw-encoded just increment the ref count. */
3182 static robj *getDecodedObject(robj *o) {
3183 robj *dec;
3184
3185 if (o->encoding == REDIS_ENCODING_RAW) {
3186 incrRefCount(o);
3187 return o;
3188 }
3189 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_INT) {
3190 char buf[32];
3191
3192 ll2string(buf,32,(long)o->ptr);
3193 dec = createStringObject(buf,strlen(buf));
3194 return dec;
3195 } else {
3196 redisPanic("Unknown encoding type");
3197 }
3198 }
3199
3200 /* Compare two string objects via strcmp() or alike.
3201 * Note that the objects may be integer-encoded. In such a case we
3202 * use ll2string() to get a string representation of the numbers on the stack
3203 * and compare the strings, it's much faster than calling getDecodedObject().
3204 *
3205 * Important note: if objects are not integer encoded, but binary-safe strings,
3206 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
3207 * binary safe. */
3208 static int compareStringObjects(robj *a, robj *b) {
3209 redisAssert(a->type == REDIS_STRING && b->type == REDIS_STRING);
3210 char bufa[128], bufb[128], *astr, *bstr;
3211 int bothsds = 1;
3212
3213 if (a == b) return 0;
3214 if (a->encoding != REDIS_ENCODING_RAW) {
3215 ll2string(bufa,sizeof(bufa),(long) a->ptr);
3216 astr = bufa;
3217 bothsds = 0;
3218 } else {
3219 astr = a->ptr;
3220 }
3221 if (b->encoding != REDIS_ENCODING_RAW) {
3222 ll2string(bufb,sizeof(bufb),(long) b->ptr);
3223 bstr = bufb;
3224 bothsds = 0;
3225 } else {
3226 bstr = b->ptr;
3227 }
3228 return bothsds ? sdscmp(astr,bstr) : strcmp(astr,bstr);
3229 }
3230
3231 /* Equal string objects return 1 if the two objects are the same from the
3232 * point of view of a string comparison, otherwise 0 is returned. Note that
3233 * this function is faster then checking for (compareStringObject(a,b) == 0)
3234 * because it can perform some more optimization. */
3235 static int equalStringObjects(robj *a, robj *b) {
3236 if (a->encoding != REDIS_ENCODING_RAW && b->encoding != REDIS_ENCODING_RAW){
3237 return a->ptr == b->ptr;
3238 } else {
3239 return compareStringObjects(a,b) == 0;
3240 }
3241 }
3242
3243 static size_t stringObjectLen(robj *o) {
3244 redisAssert(o->type == REDIS_STRING);
3245 if (o->encoding == REDIS_ENCODING_RAW) {
3246 return sdslen(o->ptr);
3247 } else {
3248 char buf[32];
3249
3250 return ll2string(buf,32,(long)o->ptr);
3251 }
3252 }
3253
3254 static int getDoubleFromObject(robj *o, double *target) {
3255 double value;
3256 char *eptr;
3257
3258 if (o == NULL) {
3259 value = 0;
3260 } else {
3261 redisAssert(o->type == REDIS_STRING);
3262 if (o->encoding == REDIS_ENCODING_RAW) {
3263 value = strtod(o->ptr, &eptr);
3264 if (eptr[0] != '\0') return REDIS_ERR;
3265 } else if (o->encoding == REDIS_ENCODING_INT) {
3266 value = (long)o->ptr;
3267 } else {
3268 redisPanic("Unknown string encoding");
3269 }
3270 }
3271
3272 *target = value;
3273 return REDIS_OK;
3274 }
3275
3276 static int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg) {
3277 double value;
3278 if (getDoubleFromObject(o, &value) != REDIS_OK) {
3279 if (msg != NULL) {
3280 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3281 } else {
3282 addReplySds(c, sdsnew("-ERR value is not a double\r\n"));
3283 }
3284 return REDIS_ERR;
3285 }
3286
3287 *target = value;
3288 return REDIS_OK;
3289 }
3290
3291 static int getLongLongFromObject(robj *o, long long *target) {
3292 long long value;
3293 char *eptr;
3294
3295 if (o == NULL) {
3296 value = 0;
3297 } else {
3298 redisAssert(o->type == REDIS_STRING);
3299 if (o->encoding == REDIS_ENCODING_RAW) {
3300 value = strtoll(o->ptr, &eptr, 10);
3301 if (eptr[0] != '\0') return REDIS_ERR;
3302 } else if (o->encoding == REDIS_ENCODING_INT) {
3303 value = (long)o->ptr;
3304 } else {
3305 redisPanic("Unknown string encoding");
3306 }
3307 }
3308
3309 *target = value;
3310 return REDIS_OK;
3311 }
3312
3313 static int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg) {
3314 long long value;
3315 if (getLongLongFromObject(o, &value) != REDIS_OK) {
3316 if (msg != NULL) {
3317 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3318 } else {
3319 addReplySds(c, sdsnew("-ERR value is not an integer\r\n"));
3320 }
3321 return REDIS_ERR;
3322 }
3323
3324 *target = value;
3325 return REDIS_OK;
3326 }
3327
3328 static int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg) {
3329 long long value;
3330
3331 if (getLongLongFromObjectOrReply(c, o, &value, msg) != REDIS_OK) return REDIS_ERR;
3332 if (value < LONG_MIN || value > LONG_MAX) {
3333 if (msg != NULL) {
3334 addReplySds(c, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg));
3335 } else {
3336 addReplySds(c, sdsnew("-ERR value is out of range\r\n"));
3337 }
3338 return REDIS_ERR;
3339 }
3340
3341 *target = value;
3342 return REDIS_OK;
3343 }
3344
3345 /*============================ RDB saving/loading =========================== */
3346
3347 static int rdbSaveType(FILE *fp, unsigned char type) {
3348 if (fwrite(&type,1,1,fp) == 0) return -1;
3349 return 0;
3350 }
3351
3352 static int rdbSaveTime(FILE *fp, time_t t) {
3353 int32_t t32 = (int32_t) t;
3354 if (fwrite(&t32,4,1,fp) == 0) return -1;
3355 return 0;
3356 }
3357
3358 /* check rdbLoadLen() comments for more info */
3359 static int rdbSaveLen(FILE *fp, uint32_t len) {
3360 unsigned char buf[2];
3361
3362 if (len < (1<<6)) {
3363 /* Save a 6 bit len */
3364 buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
3365 if (fwrite(buf,1,1,fp) == 0) return -1;
3366 } else if (len < (1<<14)) {
3367 /* Save a 14 bit len */
3368 buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
3369 buf[1] = len&0xFF;
3370 if (fwrite(buf,2,1,fp) == 0) return -1;
3371 } else {
3372 /* Save a 32 bit len */
3373 buf[0] = (REDIS_RDB_32BITLEN<<6);
3374 if (fwrite(buf,1,1,fp) == 0) return -1;
3375 len = htonl(len);
3376 if (fwrite(&len,4,1,fp) == 0) return -1;
3377 }
3378 return 0;
3379 }
3380
3381 /* Encode 'value' as an integer if possible (if integer will fit the
3382 * supported range). If the function sucessful encoded the integer
3383 * then the (up to 5 bytes) encoded representation is written in the
3384 * string pointed by 'enc' and the length is returned. Otherwise
3385 * 0 is returned. */
3386 static int rdbEncodeInteger(long long value, unsigned char *enc) {
3387 /* Finally check if it fits in our ranges */
3388 if (value >= -(1<<7) && value <= (1<<7)-1) {
3389 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
3390 enc[1] = value&0xFF;
3391 return 2;
3392 } else if (value >= -(1<<15) && value <= (1<<15)-1) {
3393 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
3394 enc[1] = value&0xFF;
3395 enc[2] = (value>>8)&0xFF;
3396 return 3;
3397 } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
3398 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
3399 enc[1] = value&0xFF;
3400 enc[2] = (value>>8)&0xFF;
3401 enc[3] = (value>>16)&0xFF;
3402 enc[4] = (value>>24)&0xFF;
3403 return 5;
3404 } else {
3405 return 0;
3406 }
3407 }
3408
3409 /* String objects in the form "2391" "-100" without any space and with a
3410 * range of values that can fit in an 8, 16 or 32 bit signed value can be
3411 * encoded as integers to save space */
3412 static int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {
3413 long long value;
3414 char *endptr, buf[32];
3415
3416 /* Check if it's possible to encode this value as a number */
3417 value = strtoll(s, &endptr, 10);
3418 if (endptr[0] != '\0') return 0;
3419 ll2string(buf,32,value);
3420
3421 /* If the number converted back into a string is not identical
3422 * then it's not possible to encode the string as integer */
3423 if (strlen(buf) != len || memcmp(buf,s,len)) return 0;
3424
3425 return rdbEncodeInteger(value,enc);
3426 }
3427
3428 static int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) {
3429 size_t comprlen, outlen;
3430 unsigned char byte;
3431 void *out;
3432
3433 /* We require at least four bytes compression for this to be worth it */
3434 if (len <= 4) return 0;
3435 outlen = len-4;
3436 if ((out = zmalloc(outlen+1)) == NULL) return 0;
3437 comprlen = lzf_compress(s, len, out, outlen);
3438 if (comprlen == 0) {
3439 zfree(out);
3440 return 0;
3441 }
3442 /* Data compressed! Let's save it on disk */
3443 byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
3444 if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
3445 if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
3446 if (rdbSaveLen(fp,len) == -1) goto writeerr;
3447 if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
3448 zfree(out);
3449 return comprlen;
3450
3451 writeerr:
3452 zfree(out);
3453 return -1;
3454 }
3455
3456 /* Save a string objet as [len][data] on disk. If the object is a string
3457 * representation of an integer value we try to safe it in a special form */
3458 static int rdbSaveRawString(FILE *fp, unsigned char *s, size_t len) {
3459 int enclen;
3460
3461 /* Try integer encoding */
3462 if (len <= 11) {
3463 unsigned char buf[5];
3464 if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) {
3465 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3466 return 0;
3467 }
3468 }
3469
3470 /* Try LZF compression - under 20 bytes it's unable to compress even
3471 * aaaaaaaaaaaaaaaaaa so skip it */
3472 if (server.rdbcompression && len > 20) {
3473 int retval;
3474
3475 retval = rdbSaveLzfStringObject(fp,s,len);
3476 if (retval == -1) return -1;
3477 if (retval > 0) return 0;
3478 /* retval == 0 means data can't be compressed, save the old way */
3479 }
3480
3481 /* Store verbatim */
3482 if (rdbSaveLen(fp,len) == -1) return -1;
3483 if (len && fwrite(s,len,1,fp) == 0) return -1;
3484 return 0;
3485 }
3486
3487 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
3488 static int rdbSaveStringObject(FILE *fp, robj *obj) {
3489 int retval;
3490
3491 /* Avoid to decode the object, then encode it again, if the
3492 * object is alrady integer encoded. */
3493 if (obj->encoding == REDIS_ENCODING_INT) {
3494 long val = (long) obj->ptr;
3495 unsigned char buf[5];
3496 int enclen;
3497
3498 if ((enclen = rdbEncodeInteger(val,buf)) > 0) {
3499 if (fwrite(buf,enclen,1,fp) == 0) return -1;
3500 return 0;
3501 }
3502 /* otherwise... fall throught and continue with the usual
3503 * code path. */
3504 }
3505
3506 /* Avoid incr/decr ref count business when possible.
3507 * This plays well with copy-on-write given that we are probably
3508 * in a child process (BGSAVE). Also this makes sure key objects
3509 * of swapped objects are not incRefCount-ed (an assert does not allow
3510 * this in order to avoid bugs) */
3511 if (obj->encoding != REDIS_ENCODING_RAW) {
3512 obj = getDecodedObject(obj);
3513 retval = rdbSaveRawString(fp,obj->ptr,sdslen(obj->ptr));
3514 decrRefCount(obj);
3515 } else {
3516 retval = rdbSaveRawString(fp,obj->ptr,sdslen(obj->ptr));
3517 }
3518 return retval;
3519 }
3520
3521 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
3522 * 8 bit integer specifing the length of the representation.
3523 * This 8 bit integer has special values in order to specify the following
3524 * conditions:
3525 * 253: not a number
3526 * 254: + inf
3527 * 255: - inf
3528 */
3529 static int rdbSaveDoubleValue(FILE *fp, double val) {
3530 unsigned char buf[128];
3531 int len;
3532
3533 if (isnan(val)) {
3534 buf[0] = 253;
3535 len = 1;
3536 } else if (!isfinite(val)) {
3537 len = 1;
3538 buf[0] = (val < 0) ? 255 : 254;
3539 } else {
3540 #if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
3541 /* Check if the float is in a safe range to be casted into a
3542 * long long. We are assuming that long long is 64 bit here.
3543 * Also we are assuming that there are no implementations around where
3544 * double has precision < 52 bit.
3545 *
3546 * Under this assumptions we test if a double is inside an interval
3547 * where casting to long long is safe. Then using two castings we
3548 * make sure the decimal part is zero. If all this is true we use
3549 * integer printing function that is much faster. */
3550 double min = -4503599627370495; /* (2^52)-1 */
3551 double max = 4503599627370496; /* -(2^52) */
3552 if (val > min && val < max && val == ((double)((long long)val)))
3553 ll2string((char*)buf+1,sizeof(buf),(long long)val);
3554 else
3555 #endif
3556 snprintf((char*)buf+1,sizeof(buf)-1,"%.17g",val);
3557 buf[0] = strlen((char*)buf+1);
3558 len = buf[0]+1;
3559 }
3560 if (fwrite(buf,len,1,fp) == 0) return -1;
3561 return 0;
3562 }
3563
3564 /* Save a Redis object. */
3565 static int rdbSaveObject(FILE *fp, robj *o) {
3566 if (o->type == REDIS_STRING) {
3567 /* Save a string value */
3568 if (rdbSaveStringObject(fp,o) == -1) return -1;
3569 } else if (o->type == REDIS_LIST) {
3570 /* Save a list value */
3571 list *list = o->ptr;
3572 listIter li;
3573 listNode *ln;
3574
3575 if (rdbSaveLen(fp,listLength(list)) == -1) return -1;
3576 listRewind(list,&li);
3577 while((ln = listNext(&li))) {
3578 robj *eleobj = listNodeValue(ln);
3579
3580 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3581 }
3582 } else if (o->type == REDIS_SET) {
3583 /* Save a set value */
3584 dict *set = o->ptr;
3585 dictIterator *di = dictGetIterator(set);
3586 dictEntry *de;
3587
3588 if (rdbSaveLen(fp,dictSize(set)) == -1) return -1;
3589 while((de = dictNext(di)) != NULL) {
3590 robj *eleobj = dictGetEntryKey(de);
3591
3592 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3593 }
3594 dictReleaseIterator(di);
3595 } else if (o->type == REDIS_ZSET) {
3596 /* Save a set value */
3597 zset *zs = o->ptr;
3598 dictIterator *di = dictGetIterator(zs->dict);
3599 dictEntry *de;
3600
3601 if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) return -1;
3602 while((de = dictNext(di)) != NULL) {
3603 robj *eleobj = dictGetEntryKey(de);
3604 double *score = dictGetEntryVal(de);
3605
3606 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
3607 if (rdbSaveDoubleValue(fp,*score) == -1) return -1;
3608 }
3609 dictReleaseIterator(di);
3610 } else if (o->type == REDIS_HASH) {
3611 /* Save a hash value */
3612 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
3613 unsigned char *p = zipmapRewind(o->ptr);
3614 unsigned int count = zipmapLen(o->ptr);
3615 unsigned char *key, *val;
3616 unsigned int klen, vlen;
3617
3618 if (rdbSaveLen(fp,count) == -1) return -1;
3619 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
3620 if (rdbSaveRawString(fp,key,klen) == -1) return -1;
3621 if (rdbSaveRawString(fp,val,vlen) == -1) return -1;
3622 }
3623 } else {
3624 dictIterator *di = dictGetIterator(o->ptr);
3625 dictEntry *de;
3626
3627 if (rdbSaveLen(fp,dictSize((dict*)o->ptr)) == -1) return -1;
3628 while((de = dictNext(di)) != NULL) {
3629 robj *key = dictGetEntryKey(de);
3630 robj *val = dictGetEntryVal(de);
3631
3632 if (rdbSaveStringObject(fp,key) == -1) return -1;
3633 if (rdbSaveStringObject(fp,val) == -1) return -1;
3634 }
3635 dictReleaseIterator(di);
3636 }
3637 } else {
3638 redisPanic("Unknown object type");
3639 }
3640 return 0;
3641 }
3642
3643 /* Return the length the object will have on disk if saved with
3644 * the rdbSaveObject() function. Currently we use a trick to get
3645 * this length with very little changes to the code. In the future
3646 * we could switch to a faster solution. */
3647 static off_t rdbSavedObjectLen(robj *o, FILE *fp) {
3648 if (fp == NULL) fp = server.devnull;
3649 rewind(fp);
3650 assert(rdbSaveObject(fp,o) != 1);
3651 return ftello(fp);
3652 }
3653
3654 /* Return the number of pages required to save this object in the swap file */
3655 static off_t rdbSavedObjectPages(robj *o, FILE *fp) {
3656 off_t bytes = rdbSavedObjectLen(o,fp);
3657
3658 return (bytes+(server.vm_page_size-1))/server.vm_page_size;
3659 }
3660
3661 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
3662 static int rdbSave(char *filename) {
3663 dictIterator *di = NULL;
3664 dictEntry *de;
3665 FILE *fp;
3666 char tmpfile[256];
3667 int j;
3668 time_t now = time(NULL);
3669
3670 /* Wait for I/O therads to terminate, just in case this is a
3671 * foreground-saving, to avoid seeking the swap file descriptor at the
3672 * same time. */
3673 if (server.vm_enabled)
3674 waitEmptyIOJobsQueue();
3675
3676 snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
3677 fp = fopen(tmpfile,"w");
3678 if (!fp) {
3679 redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno));
3680 return REDIS_ERR;
3681 }
3682 if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
3683 for (j = 0; j < server.dbnum; j++) {
3684 redisDb *db = server.db+j;
3685 dict *d = db->dict;
3686 if (dictSize(d) == 0) continue;
3687 di = dictGetIterator(d);
3688 if (!di) {
3689 fclose(fp);
3690 return REDIS_ERR;
3691 }
3692
3693 /* Write the SELECT DB opcode */
3694 if (rdbSaveType(fp,REDIS_SELECTDB) == -1) goto werr;
3695 if (rdbSaveLen(fp,j) == -1) goto werr;
3696
3697 /* Iterate this DB writing every entry */
3698 while((de = dictNext(di)) != NULL) {
3699 robj *key = dictGetEntryKey(de);
3700 robj *o = dictGetEntryVal(de);
3701 time_t expiretime = getExpire(db,key);
3702
3703 /* Save the expire time */
3704 if (expiretime != -1) {
3705 /* If this key is already expired skip it */
3706 if (expiretime < now) continue;
3707 if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr;
3708 if (rdbSaveTime(fp,expiretime) == -1) goto werr;
3709 }
3710 /* Save the key and associated value. This requires special
3711 * handling if the value is swapped out. */
3712 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
3713 key->storage == REDIS_VM_SWAPPING) {
3714 /* Save type, key, value */
3715 if (rdbSaveType(fp,o->type) == -1) goto werr;
3716 if (rdbSaveStringObject(fp,key) == -1) goto werr;
3717 if (rdbSaveObject(fp,o) == -1) goto werr;
3718 } else {
3719 /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
3720 robj *po;
3721 /* Get a preview of the object in memory */
3722 po = vmPreviewObject(key);
3723 /* Save type, key, value */
3724 if (rdbSaveType(fp,key->vtype) == -1) goto werr;
3725 if (rdbSaveStringObject(fp,key) == -1) goto werr;
3726 if (rdbSaveObject(fp,po) == -1) goto werr;
3727 /* Remove the loaded object from memory */
3728 decrRefCount(po);
3729 }
3730 }
3731 dictReleaseIterator(di);
3732 }
3733 /* EOF opcode */
3734 if (rdbSaveType(fp,REDIS_EOF) == -1) goto werr;
3735
3736 /* Make sure data will not remain on the OS's output buffers */
3737 fflush(fp);
3738 fsync(fileno(fp));
3739 fclose(fp);
3740
3741 /* Use RENAME to make sure the DB file is changed atomically only
3742 * if the generate DB file is ok. */
3743 if (rename(tmpfile,filename) == -1) {
3744 redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
3745 unlink(tmpfile);
3746 return REDIS_ERR;
3747 }
3748 redisLog(REDIS_NOTICE,"DB saved on disk");
3749 server.dirty = 0;
3750 server.lastsave = time(NULL);
3751 return REDIS_OK;
3752
3753 werr:
3754 fclose(fp);
3755 unlink(tmpfile);
3756 redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
3757 if (di) dictReleaseIterator(di);
3758 return REDIS_ERR;
3759 }
3760
3761 static int rdbSaveBackground(char *filename) {
3762 pid_t childpid;
3763
3764 if (server.bgsavechildpid != -1) return REDIS_ERR;
3765 if (server.vm_enabled) waitEmptyIOJobsQueue();
3766 if ((childpid = fork()) == 0) {
3767 /* Child */
3768 if (server.vm_enabled) vmReopenSwapFile();
3769 close(server.fd);
3770 if (rdbSave(filename) == REDIS_OK) {
3771 _exit(0);
3772 } else {
3773 _exit(1);
3774 }
3775 } else {
3776 /* Parent */
3777 if (childpid == -1) {
3778 redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
3779 strerror(errno));
3780 return REDIS_ERR;
3781 }
3782 redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);
3783 server.bgsavechildpid = childpid;
3784 updateDictResizePolicy();
3785 return REDIS_OK;
3786 }
3787 return REDIS_OK; /* unreached */
3788 }
3789
3790 static void rdbRemoveTempFile(pid_t childpid) {
3791 char tmpfile[256];
3792
3793 snprintf(tmpfile,256,"temp-%d.rdb", (int) childpid);
3794 unlink(tmpfile);
3795 }
3796
3797 static int rdbLoadType(FILE *fp) {
3798 unsigned char type;
3799 if (fread(&type,1,1,fp) == 0) return -1;
3800 return type;
3801 }
3802
3803 static time_t rdbLoadTime(FILE *fp) {
3804 int32_t t32;
3805 if (fread(&t32,4,1,fp) == 0) return -1;
3806 return (time_t) t32;
3807 }
3808
3809 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
3810 * of this file for a description of how this are stored on disk.
3811 *
3812 * isencoded is set to 1 if the readed length is not actually a length but
3813 * an "encoding type", check the above comments for more info */
3814 static uint32_t rdbLoadLen(FILE *fp, int *isencoded) {
3815 unsigned char buf[2];
3816 uint32_t len;
3817 int type;
3818
3819 if (isencoded) *isencoded = 0;
3820 if (fread(buf,1,1,fp) == 0) return REDIS_RDB_LENERR;
3821 type = (buf[0]&0xC0)>>6;
3822 if (type == REDIS_RDB_6BITLEN) {
3823 /* Read a 6 bit len */
3824 return buf[0]&0x3F;
3825 } else if (type == REDIS_RDB_ENCVAL) {
3826 /* Read a 6 bit len encoding type */
3827 if (isencoded) *isencoded = 1;
3828 return buf[0]&0x3F;
3829 } else if (type == REDIS_RDB_14BITLEN) {
3830 /* Read a 14 bit len */
3831 if (fread(buf+1,1,1,fp) == 0) return REDIS_RDB_LENERR;
3832 return ((buf[0]&0x3F)<<8)|buf[1];
3833 } else {
3834 /* Read a 32 bit len */
3835 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
3836 return ntohl(len);
3837 }
3838 }
3839
3840 /* Load an integer-encoded object from file 'fp', with the specified
3841 * encoding type 'enctype'. If encode is true the function may return
3842 * an integer-encoded object as reply, otherwise the returned object
3843 * will always be encoded as a raw string. */
3844 static robj *rdbLoadIntegerObject(FILE *fp, int enctype, int encode) {
3845 unsigned char enc[4];
3846 long long val;
3847
3848 if (enctype == REDIS_RDB_ENC_INT8) {
3849 if (fread(enc,1,1,fp) == 0) return NULL;
3850 val = (signed char)enc[0];
3851 } else if (enctype == REDIS_RDB_ENC_INT16) {
3852 uint16_t v;
3853 if (fread(enc,2,1,fp) == 0) return NULL;
3854 v = enc[0]|(enc[1]<<8);
3855 val = (int16_t)v;
3856 } else if (enctype == REDIS_RDB_ENC_INT32) {
3857 uint32_t v;
3858 if (fread(enc,4,1,fp) == 0) return NULL;
3859 v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
3860 val = (int32_t)v;
3861 } else {
3862 val = 0; /* anti-warning */
3863 redisPanic("Unknown RDB integer encoding type");
3864 }
3865 if (encode)
3866 return createStringObjectFromLongLong(val);
3867 else
3868 return createObject(REDIS_STRING,sdsfromlonglong(val));
3869 }
3870
3871 static robj *rdbLoadLzfStringObject(FILE*fp) {
3872 unsigned int len, clen;
3873 unsigned char *c = NULL;
3874 sds val = NULL;
3875
3876 if ((clen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3877 if ((len = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3878 if ((c = zmalloc(clen)) == NULL) goto err;
3879 if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
3880 if (fread(c,clen,1,fp) == 0) goto err;
3881 if (lzf_decompress(c,clen,val,len) == 0) goto err;
3882 zfree(c);
3883 return createObject(REDIS_STRING,val);
3884 err:
3885 zfree(c);
3886 sdsfree(val);
3887 return NULL;
3888 }
3889
3890 static robj *rdbGenericLoadStringObject(FILE*fp, int encode) {
3891 int isencoded;
3892 uint32_t len;
3893 sds val;
3894
3895 len = rdbLoadLen(fp,&isencoded);
3896 if (isencoded) {
3897 switch(len) {
3898 case REDIS_RDB_ENC_INT8:
3899 case REDIS_RDB_ENC_INT16:
3900 case REDIS_RDB_ENC_INT32:
3901 return rdbLoadIntegerObject(fp,len,encode);
3902 case REDIS_RDB_ENC_LZF:
3903 return rdbLoadLzfStringObject(fp);
3904 default:
3905 redisPanic("Unknown RDB encoding type");
3906 }
3907 }
3908
3909 if (len == REDIS_RDB_LENERR) return NULL;
3910 val = sdsnewlen(NULL,len);
3911 if (len && fread(val,len,1,fp) == 0) {
3912 sdsfree(val);
3913 return NULL;
3914 }
3915 return createObject(REDIS_STRING,val);
3916 }
3917
3918 static robj *rdbLoadStringObject(FILE *fp) {
3919 return rdbGenericLoadStringObject(fp,0);
3920 }
3921
3922 static robj *rdbLoadEncodedStringObject(FILE *fp) {
3923 return rdbGenericLoadStringObject(fp,1);
3924 }
3925
3926 /* For information about double serialization check rdbSaveDoubleValue() */
3927 static int rdbLoadDoubleValue(FILE *fp, double *val) {
3928 char buf[128];
3929 unsigned char len;
3930
3931 if (fread(&len,1,1,fp) == 0) return -1;
3932 switch(len) {
3933 case 255: *val = R_NegInf; return 0;
3934 case 254: *val = R_PosInf; return 0;
3935 case 253: *val = R_Nan; return 0;
3936 default:
3937 if (fread(buf,len,1,fp) == 0) return -1;
3938 buf[len] = '\0';
3939 sscanf(buf, "%lg", val);
3940 return 0;
3941 }
3942 }
3943
3944 /* Load a Redis object of the specified type from the specified file.
3945 * On success a newly allocated object is returned, otherwise NULL. */
3946 static robj *rdbLoadObject(int type, FILE *fp) {
3947 robj *o;
3948
3949 redisLog(REDIS_DEBUG,"LOADING OBJECT %d (at %d)\n",type,ftell(fp));
3950 if (type == REDIS_STRING) {
3951 /* Read string value */
3952 if ((o = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
3953 o = tryObjectEncoding(o);
3954 } else if (type == REDIS_LIST || type == REDIS_SET) {
3955 /* Read list/set value */
3956 uint32_t listlen;
3957
3958 if ((listlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3959 o = (type == REDIS_LIST) ? createListObject() : createSetObject();
3960 /* It's faster to expand the dict to the right size asap in order
3961 * to avoid rehashing */
3962 if (type == REDIS_SET && listlen > DICT_HT_INITIAL_SIZE)
3963 dictExpand(o->ptr,listlen);
3964 /* Load every single element of the list/set */
3965 while(listlen--) {
3966 robj *ele;
3967
3968 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
3969 ele = tryObjectEncoding(ele);
3970 if (type == REDIS_LIST) {
3971 listAddNodeTail((list*)o->ptr,ele);
3972 } else {
3973 dictAdd((dict*)o->ptr,ele,NULL);
3974 }
3975 }
3976 } else if (type == REDIS_ZSET) {
3977 /* Read list/set value */
3978 size_t zsetlen;
3979 zset *zs;
3980
3981 if ((zsetlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3982 o = createZsetObject();
3983 zs = o->ptr;
3984 /* Load every single element of the list/set */
3985 while(zsetlen--) {
3986 robj *ele;
3987 double *score = zmalloc(sizeof(double));
3988
3989 if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
3990 ele = tryObjectEncoding(ele);
3991 if (rdbLoadDoubleValue(fp,score) == -1) return NULL;
3992 dictAdd(zs->dict,ele,score);
3993 zslInsert(zs->zsl,*score,ele);
3994 incrRefCount(ele); /* added to skiplist */
3995 }
3996 } else if (type == REDIS_HASH) {
3997 size_t hashlen;
3998
3999 if ((hashlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
4000 o = createHashObject();
4001 /* Too many entries? Use an hash table. */
4002 if (hashlen > server.hash_max_zipmap_entries)
4003 convertToRealHash(o);
4004 /* Load every key/value, then set it into the zipmap or hash
4005 * table, as needed. */
4006 while(hashlen--) {
4007 robj *key, *val;
4008
4009 if ((key = rdbLoadStringObject(fp)) == NULL) return NULL;
4010 if ((val = rdbLoadStringObject(fp)) == NULL) return NULL;
4011 /* If we are using a zipmap and there are too big values
4012 * the object is converted to real hash table encoding. */
4013 if (o->encoding != REDIS_ENCODING_HT &&
4014 (sdslen(key->ptr) > server.hash_max_zipmap_value ||
4015 sdslen(val->ptr) > server.hash_max_zipmap_value))
4016 {
4017 convertToRealHash(o);
4018 }
4019
4020 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
4021 unsigned char *zm = o->ptr;
4022
4023 zm = zipmapSet(zm,key->ptr,sdslen(key->ptr),
4024 val->ptr,sdslen(val->ptr),NULL);
4025 o->ptr = zm;
4026 decrRefCount(key);
4027 decrRefCount(val);
4028 } else {
4029 key = tryObjectEncoding(key);
4030 val = tryObjectEncoding(val);
4031 dictAdd((dict*)o->ptr,key,val);
4032 }
4033 }
4034 } else {
4035 redisPanic("Unknown object type");
4036 }
4037 return o;
4038 }
4039
4040 static int rdbLoad(char *filename) {
4041 FILE *fp;
4042 uint32_t dbid;
4043 int type, retval, rdbver;
4044 int swap_all_values = 0;
4045 dict *d = server.db[0].dict;
4046 redisDb *db = server.db+0;
4047 char buf[1024];
4048 time_t expiretime, now = time(NULL);
4049 long long loadedkeys = 0;
4050
4051 fp = fopen(filename,"r");
4052 if (!fp) return REDIS_ERR;
4053 if (fread(buf,9,1,fp) == 0) goto eoferr;
4054 buf[9] = '\0';
4055 if (memcmp(buf,"REDIS",5) != 0) {
4056 fclose(fp);
4057 redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
4058 return REDIS_ERR;
4059 }
4060 rdbver = atoi(buf+5);
4061 if (rdbver != 1) {
4062 fclose(fp);
4063 redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
4064 return REDIS_ERR;
4065 }
4066 while(1) {
4067 robj *key, *val;
4068
4069 expiretime = -1;
4070 /* Read type. */
4071 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
4072 if (type == REDIS_EXPIRETIME) {
4073 if ((expiretime = rdbLoadTime(fp)) == -1) goto eoferr;
4074 /* We read the time so we need to read the object type again */
4075 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
4076 }
4077 if (type == REDIS_EOF) break;
4078 /* Handle SELECT DB opcode as a special case */
4079 if (type == REDIS_SELECTDB) {
4080 if ((dbid = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR)
4081 goto eoferr;
4082 if (dbid >= (unsigned)server.dbnum) {
4083 redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum);
4084 exit(1);
4085 }
4086 db = server.db+dbid;
4087 d = db->dict;
4088 continue;
4089 }
4090 /* Read key */
4091 if ((key = rdbLoadStringObject(fp)) == NULL) goto eoferr;
4092 /* Read value */
4093 if ((val = rdbLoadObject(type,fp)) == NULL) goto eoferr;
4094 /* Check if the key already expired */
4095 if (expiretime != -1 && expiretime < now) {
4096 decrRefCount(key);
4097 decrRefCount(val);
4098 continue;
4099 }
4100 /* Add the new object in the hash table */
4101 retval = dictAdd(d,key,val);
4102 if (retval == DICT_ERR) {
4103 redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key->ptr);
4104 exit(1);
4105 }
4106 loadedkeys++;
4107 /* Set the expire time if needed */
4108 if (expiretime != -1) setExpire(db,key,expiretime);
4109
4110 /* Handle swapping while loading big datasets when VM is on */
4111
4112 /* If we detecter we are hopeless about fitting something in memory
4113 * we just swap every new key on disk. Directly...
4114 * Note that's important to check for this condition before resorting
4115 * to random sampling, otherwise we may try to swap already
4116 * swapped keys. */
4117 if (swap_all_values) {
4118 dictEntry *de = dictFind(d,key);
4119
4120 /* de may be NULL since the key already expired */
4121 if (de) {
4122 key = dictGetEntryKey(de);
4123 val = dictGetEntryVal(de);
4124
4125 if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
4126 dictGetEntryVal(de) = NULL;
4127 }
4128 }
4129 continue;
4130 }
4131
4132 /* If we have still some hope of having some value fitting memory
4133 * then we try random sampling. */
4134 if (!swap_all_values && server.vm_enabled && (loadedkeys % 5000) == 0) {
4135 while (zmalloc_used_memory() > server.vm_max_memory) {
4136 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
4137 }
4138 if (zmalloc_used_memory() > server.vm_max_memory)
4139 swap_all_values = 1; /* We are already using too much mem */
4140 }
4141 }
4142 fclose(fp);
4143 return REDIS_OK;
4144
4145 eoferr: /* unexpected end of file is handled here with a fatal exit */
4146 redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
4147 exit(1);
4148 return REDIS_ERR; /* Just to avoid warning */
4149 }
4150
4151 /*================================== Commands =============================== */
4152
4153 static void authCommand(redisClient *c) {
4154 if (!server.requirepass || !strcmp(c->argv[1]->ptr, server.requirepass)) {
4155 c->authenticated = 1;
4156 addReply(c,shared.ok);
4157 } else {
4158 c->authenticated = 0;
4159 addReplySds(c,sdscatprintf(sdsempty(),"-ERR invalid password\r\n"));
4160 }
4161 }
4162
4163 static void pingCommand(redisClient *c) {
4164 addReply(c,shared.pong);
4165 }
4166
4167 static void echoCommand(redisClient *c) {
4168 addReplyBulk(c,c->argv[1]);
4169 }
4170
4171 /*=================================== Strings =============================== */
4172
4173 static void setGenericCommand(redisClient *c, int nx, robj *key, robj *val, robj *expire) {
4174 int retval;
4175 long seconds = 0; /* initialized to avoid an harmness warning */
4176
4177 if (expire) {
4178 if (getLongFromObjectOrReply(c, expire, &seconds, NULL) != REDIS_OK)
4179 return;
4180 if (seconds <= 0) {
4181 addReplySds(c,sdsnew("-ERR invalid expire time in SETEX\r\n"));
4182 return;
4183 }
4184 }
4185
4186 if (nx) deleteIfVolatile(c->db,key);
4187 retval = dictAdd(c->db->dict,key,val);
4188 if (retval == DICT_ERR) {
4189 if (!nx) {
4190 /* If the key is about a swapped value, we want a new key object
4191 * to overwrite the old. So we delete the old key in the database.
4192 * This will also make sure that swap pages about the old object
4193 * will be marked as free. */
4194 if (server.vm_enabled && deleteIfSwapped(c->db,key))
4195 incrRefCount(key);
4196 dictReplace(c->db->dict,key,val);
4197 incrRefCount(val);
4198 } else {
4199 addReply(c,shared.czero);
4200 return;
4201 }
4202 } else {
4203 incrRefCount(key);
4204 incrRefCount(val);
4205 }
4206 server.dirty++;
4207 removeExpire(c->db,key);
4208 if (expire) setExpire(c->db,key,time(NULL)+seconds);
4209 addReply(c, nx ? shared.cone : shared.ok);
4210 }
4211
4212 static void setCommand(redisClient *c) {
4213 setGenericCommand(c,0,c->argv[1],c->argv[2],NULL);
4214 }
4215
4216 static void setnxCommand(redisClient *c) {
4217 setGenericCommand(c,1,c->argv[1],c->argv[2],NULL);
4218 }
4219
4220 static void setexCommand(redisClient *c) {
4221 setGenericCommand(c,0,c->argv[1],c->argv[3],c->argv[2]);
4222 }
4223
4224 static int getGenericCommand(redisClient *c) {
4225 robj *o;
4226
4227 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL)
4228 return REDIS_OK;
4229
4230 if (o->type != REDIS_STRING) {
4231 addReply(c,shared.wrongtypeerr);
4232 return REDIS_ERR;
4233 } else {
4234 addReplyBulk(c,o);
4235 return REDIS_OK;
4236 }
4237 }
4238
4239 static void getCommand(redisClient *c) {
4240 getGenericCommand(c);
4241 }
4242
4243 static void getsetCommand(redisClient *c) {
4244 if (getGenericCommand(c) == REDIS_ERR) return;
4245 if (dictAdd(c->db->dict,c->argv[1],c->argv[2]) == DICT_ERR) {
4246 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
4247 } else {
4248 incrRefCount(c->argv[1]);
4249 }
4250 incrRefCount(c->argv[2]);
4251 server.dirty++;
4252 removeExpire(c->db,c->argv[1]);
4253 }
4254
4255 static void mgetCommand(redisClient *c) {
4256 int j;
4257
4258 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1));
4259 for (j = 1; j < c->argc; j++) {
4260 robj *o = lookupKeyRead(c->db,c->argv[j]);
4261 if (o == NULL) {
4262 addReply(c,shared.nullbulk);
4263 } else {
4264 if (o->type != REDIS_STRING) {
4265 addReply(c,shared.nullbulk);
4266 } else {
4267 addReplyBulk(c,o);
4268 }
4269 }
4270 }
4271 }
4272
4273 static void msetGenericCommand(redisClient *c, int nx) {
4274 int j, busykeys = 0;
4275
4276 if ((c->argc % 2) == 0) {
4277 addReplySds(c,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
4278 return;
4279 }
4280 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
4281 * set nothing at all if at least one already key exists. */
4282 if (nx) {
4283 for (j = 1; j < c->argc; j += 2) {
4284 if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
4285 busykeys++;
4286 }
4287 }
4288 }
4289 if (busykeys) {
4290 addReply(c, shared.czero);
4291 return;
4292 }
4293
4294 for (j = 1; j < c->argc; j += 2) {
4295 int retval;
4296
4297 c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
4298 retval = dictAdd(c->db->dict,c->argv[j],c->argv[j+1]);
4299 if (retval == DICT_ERR) {
4300 dictReplace(c->db->dict,c->argv[j],c->argv[j+1]);
4301 incrRefCount(c->argv[j+1]);
4302 } else {
4303 incrRefCount(c->argv[j]);
4304 incrRefCount(c->argv[j+1]);
4305 }
4306 removeExpire(c->db,c->argv[j]);
4307 }
4308 server.dirty += (c->argc-1)/2;
4309 addReply(c, nx ? shared.cone : shared.ok);
4310 }
4311
4312 static void msetCommand(redisClient *c) {
4313 msetGenericCommand(c,0);
4314 }
4315
4316 static void msetnxCommand(redisClient *c) {
4317 msetGenericCommand(c,1);
4318 }
4319
4320 static void incrDecrCommand(redisClient *c, long long incr) {
4321 long long value;
4322 int retval;
4323 robj *o;
4324
4325 o = lookupKeyWrite(c->db,c->argv[1]);
4326 if (o != NULL && checkType(c,o,REDIS_STRING)) return;
4327 if (getLongLongFromObjectOrReply(c,o,&value,NULL) != REDIS_OK) return;
4328
4329 value += incr;
4330 o = createStringObjectFromLongLong(value);
4331 retval = dictAdd(c->db->dict,c->argv[1],o);
4332 if (retval == DICT_ERR) {
4333 dictReplace(c->db->dict,c->argv[1],o);
4334 removeExpire(c->db,c->argv[1]);
4335 } else {
4336 incrRefCount(c->argv[1]);
4337 }
4338 server.dirty++;
4339 addReply(c,shared.colon);
4340 addReply(c,o);
4341 addReply(c,shared.crlf);
4342 }
4343
4344 static void incrCommand(redisClient *c) {
4345 incrDecrCommand(c,1);
4346 }
4347
4348 static void decrCommand(redisClient *c) {
4349 incrDecrCommand(c,-1);
4350 }
4351
4352 static void incrbyCommand(redisClient *c) {
4353 long long incr;
4354
4355 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
4356 incrDecrCommand(c,incr);
4357 }
4358
4359 static void decrbyCommand(redisClient *c) {
4360 long long incr;
4361
4362 if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != REDIS_OK) return;
4363 incrDecrCommand(c,-incr);
4364 }
4365
4366 static void appendCommand(redisClient *c) {
4367 int retval;
4368 size_t totlen;
4369 robj *o;
4370
4371 o = lookupKeyWrite(c->db,c->argv[1]);
4372 if (o == NULL) {
4373 /* Create the key */
4374 retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
4375 incrRefCount(c->argv[1]);
4376 incrRefCount(c->argv[2]);
4377 totlen = stringObjectLen(c->argv[2]);
4378 } else {
4379 dictEntry *de;
4380
4381 de = dictFind(c->db->dict,c->argv[1]);
4382 assert(de != NULL);
4383
4384 o = dictGetEntryVal(de);
4385 if (o->type != REDIS_STRING) {
4386 addReply(c,shared.wrongtypeerr);
4387 return;
4388 }
4389 /* If the object is specially encoded or shared we have to make
4390 * a copy */
4391 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) {
4392 robj *decoded = getDecodedObject(o);
4393
4394 o = createStringObject(decoded->ptr, sdslen(decoded->ptr));
4395 decrRefCount(decoded);
4396 dictReplace(c->db->dict,c->argv[1],o);
4397 }
4398 /* APPEND! */
4399 if (c->argv[2]->encoding == REDIS_ENCODING_RAW) {
4400 o->ptr = sdscatlen(o->ptr,
4401 c->argv[2]->ptr, sdslen(c->argv[2]->ptr));
4402 } else {
4403 o->ptr = sdscatprintf(o->ptr, "%ld",
4404 (unsigned long) c->argv[2]->ptr);
4405 }
4406 totlen = sdslen(o->ptr);
4407 }
4408 server.dirty++;
4409 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen));
4410 }
4411
4412 static void substrCommand(redisClient *c) {
4413 robj *o;
4414 long start = atoi(c->argv[2]->ptr);
4415 long end = atoi(c->argv[3]->ptr);
4416 size_t rangelen, strlen;
4417 sds range;
4418
4419 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4420 checkType(c,o,REDIS_STRING)) return;
4421
4422 o = getDecodedObject(o);
4423 strlen = sdslen(o->ptr);
4424
4425 /* convert negative indexes */
4426 if (start < 0) start = strlen+start;
4427 if (end < 0) end = strlen+end;
4428 if (start < 0) start = 0;
4429 if (end < 0) end = 0;
4430
4431 /* indexes sanity checks */
4432 if (start > end || (size_t)start >= strlen) {
4433 /* Out of range start or start > end result in null reply */
4434 addReply(c,shared.nullbulk);
4435 decrRefCount(o);
4436 return;
4437 }
4438 if ((size_t)end >= strlen) end = strlen-1;
4439 rangelen = (end-start)+1;
4440
4441 /* Return the result */
4442 addReplySds(c,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen));
4443 range = sdsnewlen((char*)o->ptr+start,rangelen);
4444 addReplySds(c,range);
4445 addReply(c,shared.crlf);
4446 decrRefCount(o);
4447 }
4448
4449 /* ========================= Type agnostic commands ========================= */
4450
4451 static void delCommand(redisClient *c) {
4452 int deleted = 0, j;
4453
4454 for (j = 1; j < c->argc; j++) {
4455 if (deleteKey(c->db,c->argv[j])) {
4456 server.dirty++;
4457 deleted++;
4458 }
4459 }
4460 addReplyLongLong(c,deleted);
4461 }
4462
4463 static void existsCommand(redisClient *c) {
4464 expireIfNeeded(c->db,c->argv[1]);
4465 if (dictFind(c->db->dict,c->argv[1])) {
4466 addReply(c, shared.cone);
4467 } else {
4468 addReply(c, shared.czero);
4469 }
4470 }
4471
4472 static void selectCommand(redisClient *c) {
4473 int id = atoi(c->argv[1]->ptr);
4474
4475 if (selectDb(c,id) == REDIS_ERR) {
4476 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
4477 } else {
4478 addReply(c,shared.ok);
4479 }
4480 }
4481
4482 static void randomkeyCommand(redisClient *c) {
4483 dictEntry *de;
4484 robj *key;
4485
4486 while(1) {
4487 de = dictGetRandomKey(c->db->dict);
4488 if (!de || expireIfNeeded(c->db,dictGetEntryKey(de)) == 0) break;
4489 }
4490
4491 if (de == NULL) {
4492 addReply(c,shared.nullbulk);
4493 return;
4494 }
4495
4496 key = dictGetEntryKey(de);
4497 if (server.vm_enabled) {
4498 key = dupStringObject(key);
4499 addReplyBulk(c,key);
4500 decrRefCount(key);
4501 } else {
4502 addReplyBulk(c,key);
4503 }
4504 }
4505
4506 static void keysCommand(redisClient *c) {
4507 dictIterator *di;
4508 dictEntry *de;
4509 sds pattern = c->argv[1]->ptr;
4510 int plen = sdslen(pattern);
4511 unsigned long numkeys = 0;
4512 robj *lenobj = createObject(REDIS_STRING,NULL);
4513
4514 di = dictGetIterator(c->db->dict);
4515 addReply(c,lenobj);
4516 decrRefCount(lenobj);
4517 while((de = dictNext(di)) != NULL) {
4518 robj *keyobj = dictGetEntryKey(de);
4519
4520 sds key = keyobj->ptr;
4521 if ((pattern[0] == '*' && pattern[1] == '\0') ||
4522 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
4523 if (expireIfNeeded(c->db,keyobj) == 0) {
4524 addReplyBulk(c,keyobj);
4525 numkeys++;
4526 }
4527 }
4528 }
4529 dictReleaseIterator(di);
4530 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",numkeys);
4531 }
4532
4533 static void dbsizeCommand(redisClient *c) {
4534 addReplySds(c,
4535 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
4536 }
4537
4538 static void lastsaveCommand(redisClient *c) {
4539 addReplySds(c,
4540 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
4541 }
4542
4543 static void typeCommand(redisClient *c) {
4544 robj *o;
4545 char *type;
4546
4547 o = lookupKeyRead(c->db,c->argv[1]);
4548 if (o == NULL) {
4549 type = "+none";
4550 } else {
4551 switch(o->type) {
4552 case REDIS_STRING: type = "+string"; break;
4553 case REDIS_LIST: type = "+list"; break;
4554 case REDIS_SET: type = "+set"; break;
4555 case REDIS_ZSET: type = "+zset"; break;
4556 case REDIS_HASH: type = "+hash"; break;
4557 default: type = "+unknown"; break;
4558 }
4559 }
4560 addReplySds(c,sdsnew(type));
4561 addReply(c,shared.crlf);
4562 }
4563
4564 static void saveCommand(redisClient *c) {
4565 if (server.bgsavechildpid != -1) {
4566 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
4567 return;
4568 }
4569 if (rdbSave(server.dbfilename) == REDIS_OK) {
4570 addReply(c,shared.ok);
4571 } else {
4572 addReply(c,shared.err);
4573 }
4574 }
4575
4576 static void bgsaveCommand(redisClient *c) {
4577 if (server.bgsavechildpid != -1) {
4578 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
4579 return;
4580 }
4581 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
4582 char *status = "+Background saving started\r\n";
4583 addReplySds(c,sdsnew(status));
4584 } else {
4585 addReply(c,shared.err);
4586 }
4587 }
4588
4589 static void shutdownCommand(redisClient *c) {
4590 redisLog(REDIS_WARNING,"User requested shutdown, saving DB...");
4591 /* Kill the saving child if there is a background saving in progress.
4592 We want to avoid race conditions, for instance our saving child may
4593 overwrite the synchronous saving did by SHUTDOWN. */
4594 if (server.bgsavechildpid != -1) {
4595 redisLog(REDIS_WARNING,"There is a live saving child. Killing it!");
4596 kill(server.bgsavechildpid,SIGKILL);
4597 rdbRemoveTempFile(server.bgsavechildpid);
4598 }
4599 if (server.appendonly) {
4600 /* Append only file: fsync() the AOF and exit */
4601 fsync(server.appendfd);
4602 if (server.vm_enabled) unlink(server.vm_swap_file);
4603 exit(0);
4604 } else {
4605 /* Snapshotting. Perform a SYNC SAVE and exit */
4606 if (rdbSave(server.dbfilename) == REDIS_OK) {
4607 if (server.daemonize)
4608 unlink(server.pidfile);
4609 redisLog(REDIS_WARNING,"%zu bytes used at exit",zmalloc_used_memory());
4610 redisLog(REDIS_WARNING,"Server exit now, bye bye...");
4611 exit(0);
4612 } else {
4613 /* Ooops.. error saving! The best we can do is to continue
4614 * operating. Note that if there was a background saving process,
4615 * in the next cron() Redis will be notified that the background
4616 * saving aborted, handling special stuff like slaves pending for
4617 * synchronization... */
4618 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit");
4619 addReplySds(c,
4620 sdsnew("-ERR can't quit, problems saving the DB\r\n"));
4621 }
4622 }
4623 }
4624
4625 static void renameGenericCommand(redisClient *c, int nx) {
4626 robj *o;
4627
4628 /* To use the same key as src and dst is probably an error */
4629 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
4630 addReply(c,shared.sameobjecterr);
4631 return;
4632 }
4633
4634 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL)
4635 return;
4636
4637 incrRefCount(o);
4638 deleteIfVolatile(c->db,c->argv[2]);
4639 if (dictAdd(c->db->dict,c->argv[2],o) == DICT_ERR) {
4640 if (nx) {
4641 decrRefCount(o);
4642 addReply(c,shared.czero);
4643 return;
4644 }
4645 dictReplace(c->db->dict,c->argv[2],o);
4646 } else {
4647 incrRefCount(c->argv[2]);
4648 }
4649 deleteKey(c->db,c->argv[1]);
4650 server.dirty++;
4651 addReply(c,nx ? shared.cone : shared.ok);
4652 }
4653
4654 static void renameCommand(redisClient *c) {
4655 renameGenericCommand(c,0);
4656 }
4657
4658 static void renamenxCommand(redisClient *c) {
4659 renameGenericCommand(c,1);
4660 }
4661
4662 static void moveCommand(redisClient *c) {
4663 robj *o;
4664 redisDb *src, *dst;
4665 int srcid;
4666
4667 /* Obtain source and target DB pointers */
4668 src = c->db;
4669 srcid = c->db->id;
4670 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
4671 addReply(c,shared.outofrangeerr);
4672 return;
4673 }
4674 dst = c->db;
4675 selectDb(c,srcid); /* Back to the source DB */
4676
4677 /* If the user is moving using as target the same
4678 * DB as the source DB it is probably an error. */
4679 if (src == dst) {
4680 addReply(c,shared.sameobjecterr);
4681 return;
4682 }
4683
4684 /* Check if the element exists and get a reference */
4685 o = lookupKeyWrite(c->db,c->argv[1]);
4686 if (!o) {
4687 addReply(c,shared.czero);
4688 return;
4689 }
4690
4691 /* Try to add the element to the target DB */
4692 deleteIfVolatile(dst,c->argv[1]);
4693 if (dictAdd(dst->dict,c->argv[1],o) == DICT_ERR) {
4694 addReply(c,shared.czero);
4695 return;
4696 }
4697 incrRefCount(c->argv[1]);
4698 incrRefCount(o);
4699
4700 /* OK! key moved, free the entry in the source DB */
4701 deleteKey(src,c->argv[1]);
4702 server.dirty++;
4703 addReply(c,shared.cone);
4704 }
4705
4706 /* =================================== Lists ================================ */
4707 static void pushGenericCommand(redisClient *c, int where) {
4708 robj *lobj;
4709 list *list;
4710
4711 lobj = lookupKeyWrite(c->db,c->argv[1]);
4712 if (lobj == NULL) {
4713 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
4714 addReply(c,shared.cone);
4715 return;
4716 }
4717 lobj = createListObject();
4718 list = lobj->ptr;
4719 if (where == REDIS_HEAD) {
4720 listAddNodeHead(list,c->argv[2]);
4721 } else {
4722 listAddNodeTail(list,c->argv[2]);
4723 }
4724 dictAdd(c->db->dict,c->argv[1],lobj);
4725 incrRefCount(c->argv[1]);
4726 incrRefCount(c->argv[2]);
4727 } else {
4728 if (lobj->type != REDIS_LIST) {
4729 addReply(c,shared.wrongtypeerr);
4730 return;
4731 }
4732 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
4733 addReply(c,shared.cone);
4734 return;
4735 }
4736 list = lobj->ptr;
4737 if (where == REDIS_HEAD) {
4738 listAddNodeHead(list,c->argv[2]);
4739 } else {
4740 listAddNodeTail(list,c->argv[2]);
4741 }
4742 incrRefCount(c->argv[2]);
4743 }
4744 server.dirty++;
4745 addReplyLongLong(c,listLength(list));
4746 }
4747
4748 static void lpushCommand(redisClient *c) {
4749 pushGenericCommand(c,REDIS_HEAD);
4750 }
4751
4752 static void rpushCommand(redisClient *c) {
4753 pushGenericCommand(c,REDIS_TAIL);
4754 }
4755
4756 static void llenCommand(redisClient *c) {
4757 robj *o;
4758 list *l;
4759
4760 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
4761 checkType(c,o,REDIS_LIST)) return;
4762
4763 l = o->ptr;
4764 addReplyUlong(c,listLength(l));
4765 }
4766
4767 static void lindexCommand(redisClient *c) {
4768 robj *o;
4769 int index = atoi(c->argv[2]->ptr);
4770 list *list;
4771 listNode *ln;
4772
4773 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4774 checkType(c,o,REDIS_LIST)) return;
4775 list = o->ptr;
4776
4777 ln = listIndex(list, index);
4778 if (ln == NULL) {
4779 addReply(c,shared.nullbulk);
4780 } else {
4781 robj *ele = listNodeValue(ln);
4782 addReplyBulk(c,ele);
4783 }
4784 }
4785
4786 static void lsetCommand(redisClient *c) {
4787 robj *o;
4788 int index = atoi(c->argv[2]->ptr);
4789 list *list;
4790 listNode *ln;
4791
4792 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nokeyerr)) == NULL ||
4793 checkType(c,o,REDIS_LIST)) return;
4794 list = o->ptr;
4795
4796 ln = listIndex(list, index);
4797 if (ln == NULL) {
4798 addReply(c,shared.outofrangeerr);
4799 } else {
4800 robj *ele = listNodeValue(ln);
4801
4802 decrRefCount(ele);
4803 listNodeValue(ln) = c->argv[3];
4804 incrRefCount(c->argv[3]);
4805 addReply(c,shared.ok);
4806 server.dirty++;
4807 }
4808 }
4809
4810 static void popGenericCommand(redisClient *c, int where) {
4811 robj *o;
4812 list *list;
4813 listNode *ln;
4814
4815 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4816 checkType(c,o,REDIS_LIST)) return;
4817 list = o->ptr;
4818
4819 if (where == REDIS_HEAD)
4820 ln = listFirst(list);
4821 else
4822 ln = listLast(list);
4823
4824 if (ln == NULL) {
4825 addReply(c,shared.nullbulk);
4826 } else {
4827 robj *ele = listNodeValue(ln);
4828 addReplyBulk(c,ele);
4829 listDelNode(list,ln);
4830 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
4831 server.dirty++;
4832 }
4833 }
4834
4835 static void lpopCommand(redisClient *c) {
4836 popGenericCommand(c,REDIS_HEAD);
4837 }
4838
4839 static void rpopCommand(redisClient *c) {
4840 popGenericCommand(c,REDIS_TAIL);
4841 }
4842
4843 static void lrangeCommand(redisClient *c) {
4844 robj *o;
4845 int start = atoi(c->argv[2]->ptr);
4846 int end = atoi(c->argv[3]->ptr);
4847 int llen;
4848 int rangelen, j;
4849 list *list;
4850 listNode *ln;
4851 robj *ele;
4852
4853 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
4854 || checkType(c,o,REDIS_LIST)) return;
4855 list = o->ptr;
4856 llen = listLength(list);
4857
4858 /* convert negative indexes */
4859 if (start < 0) start = llen+start;
4860 if (end < 0) end = llen+end;
4861 if (start < 0) start = 0;
4862 if (end < 0) end = 0;
4863
4864 /* indexes sanity checks */
4865 if (start > end || start >= llen) {
4866 /* Out of range start or start > end result in empty list */
4867 addReply(c,shared.emptymultibulk);
4868 return;
4869 }
4870 if (end >= llen) end = llen-1;
4871 rangelen = (end-start)+1;
4872
4873 /* Return the result in form of a multi-bulk reply */
4874 ln = listIndex(list, start);
4875 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen));
4876 for (j = 0; j < rangelen; j++) {
4877 ele = listNodeValue(ln);
4878 addReplyBulk(c,ele);
4879 ln = ln->next;
4880 }
4881 }
4882
4883 static void ltrimCommand(redisClient *c) {
4884 robj *o;
4885 int start = atoi(c->argv[2]->ptr);
4886 int end = atoi(c->argv[3]->ptr);
4887 int llen;
4888 int j, ltrim, rtrim;
4889 list *list;
4890 listNode *ln;
4891
4892 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.ok)) == NULL ||
4893 checkType(c,o,REDIS_LIST)) return;
4894 list = o->ptr;
4895 llen = listLength(list);
4896
4897 /* convert negative indexes */
4898 if (start < 0) start = llen+start;
4899 if (end < 0) end = llen+end;
4900 if (start < 0) start = 0;
4901 if (end < 0) end = 0;
4902
4903 /* indexes sanity checks */
4904 if (start > end || start >= llen) {
4905 /* Out of range start or start > end result in empty list */
4906 ltrim = llen;
4907 rtrim = 0;
4908 } else {
4909 if (end >= llen) end = llen-1;
4910 ltrim = start;
4911 rtrim = llen-end-1;
4912 }
4913
4914 /* Remove list elements to perform the trim */
4915 for (j = 0; j < ltrim; j++) {
4916 ln = listFirst(list);
4917 listDelNode(list,ln);
4918 }
4919 for (j = 0; j < rtrim; j++) {
4920 ln = listLast(list);
4921 listDelNode(list,ln);
4922 }
4923 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
4924 server.dirty++;
4925 addReply(c,shared.ok);
4926 }
4927
4928 static void lremCommand(redisClient *c) {
4929 robj *o;
4930 list *list;
4931 listNode *ln, *next;
4932 int toremove = atoi(c->argv[2]->ptr);
4933 int removed = 0;
4934 int fromtail = 0;
4935
4936 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
4937 checkType(c,o,REDIS_LIST)) return;
4938 list = o->ptr;
4939
4940 if (toremove < 0) {
4941 toremove = -toremove;
4942 fromtail = 1;
4943 }
4944 ln = fromtail ? list->tail : list->head;
4945 while (ln) {
4946 robj *ele = listNodeValue(ln);
4947
4948 next = fromtail ? ln->prev : ln->next;
4949 if (equalStringObjects(ele,c->argv[3])) {
4950 listDelNode(list,ln);
4951 server.dirty++;
4952 removed++;
4953 if (toremove && removed == toremove) break;
4954 }
4955 ln = next;
4956 }
4957 if (listLength(list) == 0) deleteKey(c->db,c->argv[1]);
4958 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
4959 }
4960
4961 /* This is the semantic of this command:
4962 * RPOPLPUSH srclist dstlist:
4963 * IF LLEN(srclist) > 0
4964 * element = RPOP srclist
4965 * LPUSH dstlist element
4966 * RETURN element
4967 * ELSE
4968 * RETURN nil
4969 * END
4970 * END
4971 *
4972 * The idea is to be able to get an element from a list in a reliable way
4973 * since the element is not just returned but pushed against another list
4974 * as well. This command was originally proposed by Ezra Zygmuntowicz.
4975 */
4976 static void rpoplpushcommand(redisClient *c) {
4977 robj *sobj;
4978 list *srclist;
4979 listNode *ln;
4980
4981 if ((sobj = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
4982 checkType(c,sobj,REDIS_LIST)) return;
4983 srclist = sobj->ptr;
4984 ln = listLast(srclist);
4985
4986 if (ln == NULL) {
4987 addReply(c,shared.nullbulk);
4988 } else {
4989 robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
4990 robj *ele = listNodeValue(ln);
4991 list *dstlist;
4992
4993 if (dobj && dobj->type != REDIS_LIST) {
4994 addReply(c,shared.wrongtypeerr);
4995 return;
4996 }
4997
4998 /* Add the element to the target list (unless it's directly
4999 * passed to some BLPOP-ing client */
5000 if (!handleClientsWaitingListPush(c,c->argv[2],ele)) {
5001 if (dobj == NULL) {
5002 /* Create the list if the key does not exist */
5003 dobj = createListObject();
5004 dictAdd(c->db->dict,c->argv[2],dobj);
5005 incrRefCount(c->argv[2]);
5006 }
5007 dstlist = dobj->ptr;
5008 listAddNodeHead(dstlist,ele);
5009 incrRefCount(ele);
5010 }
5011
5012 /* Send the element to the client as reply as well */
5013 addReplyBulk(c,ele);
5014
5015 /* Finally remove the element from the source list */
5016 listDelNode(srclist,ln);
5017 if (listLength(srclist) == 0) deleteKey(c->db,c->argv[1]);
5018 server.dirty++;
5019 }
5020 }
5021
5022 /* ==================================== Sets ================================ */
5023
5024 static void saddCommand(redisClient *c) {
5025 robj *set;
5026
5027 set = lookupKeyWrite(c->db,c->argv[1]);
5028 if (set == NULL) {
5029 set = createSetObject();
5030 dictAdd(c->db->dict,c->argv[1],set);
5031 incrRefCount(c->argv[1]);
5032 } else {
5033 if (set->type != REDIS_SET) {
5034 addReply(c,shared.wrongtypeerr);
5035 return;
5036 }
5037 }
5038 if (dictAdd(set->ptr,c->argv[2],NULL) == DICT_OK) {
5039 incrRefCount(c->argv[2]);
5040 server.dirty++;
5041 addReply(c,shared.cone);
5042 } else {
5043 addReply(c,shared.czero);
5044 }
5045 }
5046
5047 static void sremCommand(redisClient *c) {
5048 robj *set;
5049
5050 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5051 checkType(c,set,REDIS_SET)) return;
5052
5053 if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
5054 server.dirty++;
5055 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
5056 if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
5057 addReply(c,shared.cone);
5058 } else {
5059 addReply(c,shared.czero);
5060 }
5061 }
5062
5063 static void smoveCommand(redisClient *c) {
5064 robj *srcset, *dstset;
5065
5066 srcset = lookupKeyWrite(c->db,c->argv[1]);
5067 dstset = lookupKeyWrite(c->db,c->argv[2]);
5068
5069 /* If the source key does not exist return 0, if it's of the wrong type
5070 * raise an error */
5071 if (srcset == NULL || srcset->type != REDIS_SET) {
5072 addReply(c, srcset ? shared.wrongtypeerr : shared.czero);
5073 return;
5074 }
5075 /* Error if the destination key is not a set as well */
5076 if (dstset && dstset->type != REDIS_SET) {
5077 addReply(c,shared.wrongtypeerr);
5078 return;
5079 }
5080 /* Remove the element from the source set */
5081 if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) {
5082 /* Key not found in the src set! return zero */
5083 addReply(c,shared.czero);
5084 return;
5085 }
5086 if (dictSize((dict*)srcset->ptr) == 0 && srcset != dstset)
5087 deleteKey(c->db,c->argv[1]);
5088 server.dirty++;
5089 /* Add the element to the destination set */
5090 if (!dstset) {
5091 dstset = createSetObject();
5092 dictAdd(c->db->dict,c->argv[2],dstset);
5093 incrRefCount(c->argv[2]);
5094 }
5095 if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK)
5096 incrRefCount(c->argv[3]);
5097 addReply(c,shared.cone);
5098 }
5099
5100 static void sismemberCommand(redisClient *c) {
5101 robj *set;
5102
5103 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5104 checkType(c,set,REDIS_SET)) return;
5105
5106 if (dictFind(set->ptr,c->argv[2]))
5107 addReply(c,shared.cone);
5108 else
5109 addReply(c,shared.czero);
5110 }
5111
5112 static void scardCommand(redisClient *c) {
5113 robj *o;
5114 dict *s;
5115
5116 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
5117 checkType(c,o,REDIS_SET)) return;
5118
5119 s = o->ptr;
5120 addReplyUlong(c,dictSize(s));
5121 }
5122
5123 static void spopCommand(redisClient *c) {
5124 robj *set;
5125 dictEntry *de;
5126
5127 if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5128 checkType(c,set,REDIS_SET)) return;
5129
5130 de = dictGetRandomKey(set->ptr);
5131 if (de == NULL) {
5132 addReply(c,shared.nullbulk);
5133 } else {
5134 robj *ele = dictGetEntryKey(de);
5135
5136 addReplyBulk(c,ele);
5137 dictDelete(set->ptr,ele);
5138 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
5139 if (dictSize((dict*)set->ptr) == 0) deleteKey(c->db,c->argv[1]);
5140 server.dirty++;
5141 }
5142 }
5143
5144 static void srandmemberCommand(redisClient *c) {
5145 robj *set;
5146 dictEntry *de;
5147
5148 if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
5149 checkType(c,set,REDIS_SET)) return;
5150
5151 de = dictGetRandomKey(set->ptr);
5152 if (de == NULL) {
5153 addReply(c,shared.nullbulk);
5154 } else {
5155 robj *ele = dictGetEntryKey(de);
5156
5157 addReplyBulk(c,ele);
5158 }
5159 }
5160
5161 static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
5162 dict **d1 = (void*) s1, **d2 = (void*) s2;
5163
5164 return dictSize(*d1)-dictSize(*d2);
5165 }
5166
5167 static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long setsnum, robj *dstkey) {
5168 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5169 dictIterator *di;
5170 dictEntry *de;
5171 robj *lenobj = NULL, *dstset = NULL;
5172 unsigned long j, cardinality = 0;
5173
5174 for (j = 0; j < setsnum; j++) {
5175 robj *setobj;
5176
5177 setobj = dstkey ?
5178 lookupKeyWrite(c->db,setskeys[j]) :
5179 lookupKeyRead(c->db,setskeys[j]);
5180 if (!setobj) {
5181 zfree(dv);
5182 if (dstkey) {
5183 if (deleteKey(c->db,dstkey))
5184 server.dirty++;
5185 addReply(c,shared.czero);
5186 } else {
5187 addReply(c,shared.emptymultibulk);
5188 }
5189 return;
5190 }
5191 if (setobj->type != REDIS_SET) {
5192 zfree(dv);
5193 addReply(c,shared.wrongtypeerr);
5194 return;
5195 }
5196 dv[j] = setobj->ptr;
5197 }
5198 /* Sort sets from the smallest to largest, this will improve our
5199 * algorithm's performace */
5200 qsort(dv,setsnum,sizeof(dict*),qsortCompareSetsByCardinality);
5201
5202 /* The first thing we should output is the total number of elements...
5203 * since this is a multi-bulk write, but at this stage we don't know
5204 * the intersection set size, so we use a trick, append an empty object
5205 * to the output list and save the pointer to later modify it with the
5206 * right length */
5207 if (!dstkey) {
5208 lenobj = createObject(REDIS_STRING,NULL);
5209 addReply(c,lenobj);
5210 decrRefCount(lenobj);
5211 } else {
5212 /* If we have a target key where to store the resulting set
5213 * create this key with an empty set inside */
5214 dstset = createSetObject();
5215 }
5216
5217 /* Iterate all the elements of the first (smallest) set, and test
5218 * the element against all the other sets, if at least one set does
5219 * not include the element it is discarded */
5220 di = dictGetIterator(dv[0]);
5221
5222 while((de = dictNext(di)) != NULL) {
5223 robj *ele;
5224
5225 for (j = 1; j < setsnum; j++)
5226 if (dictFind(dv[j],dictGetEntryKey(de)) == NULL) break;
5227 if (j != setsnum)
5228 continue; /* at least one set does not contain the member */
5229 ele = dictGetEntryKey(de);
5230 if (!dstkey) {
5231 addReplyBulk(c,ele);
5232 cardinality++;
5233 } else {
5234 dictAdd(dstset->ptr,ele,NULL);
5235 incrRefCount(ele);
5236 }
5237 }
5238 dictReleaseIterator(di);
5239
5240 if (dstkey) {
5241 /* Store the resulting set into the target, if the intersection
5242 * is not an empty set. */
5243 deleteKey(c->db,dstkey);
5244 if (dictSize((dict*)dstset->ptr) > 0) {
5245 dictAdd(c->db->dict,dstkey,dstset);
5246 incrRefCount(dstkey);
5247 addReplyLongLong(c,dictSize((dict*)dstset->ptr));
5248 } else {
5249 decrRefCount(dstset);
5250 addReply(c,shared.czero);
5251 }
5252 server.dirty++;
5253 } else {
5254 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
5255 }
5256 zfree(dv);
5257 }
5258
5259 static void sinterCommand(redisClient *c) {
5260 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
5261 }
5262
5263 static void sinterstoreCommand(redisClient *c) {
5264 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
5265 }
5266
5267 #define REDIS_OP_UNION 0
5268 #define REDIS_OP_DIFF 1
5269 #define REDIS_OP_INTER 2
5270
5271 static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey, int op) {
5272 dict **dv = zmalloc(sizeof(dict*)*setsnum);
5273 dictIterator *di;
5274 dictEntry *de;
5275 robj *dstset = NULL;
5276 int j, cardinality = 0;
5277
5278 for (j = 0; j < setsnum; j++) {
5279 robj *setobj;
5280
5281 setobj = dstkey ?
5282 lookupKeyWrite(c->db,setskeys[j]) :
5283 lookupKeyRead(c->db,setskeys[j]);
5284 if (!setobj) {
5285 dv[j] = NULL;
5286 continue;
5287 }
5288 if (setobj->type != REDIS_SET) {
5289 zfree(dv);
5290 addReply(c,shared.wrongtypeerr);
5291 return;
5292 }
5293 dv[j] = setobj->ptr;
5294 }
5295
5296 /* We need a temp set object to store our union. If the dstkey
5297 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
5298 * this set object will be the resulting object to set into the target key*/
5299 dstset = createSetObject();
5300
5301 /* Iterate all the elements of all the sets, add every element a single
5302 * time to the result set */
5303 for (j = 0; j < setsnum; j++) {
5304 if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */
5305 if (!dv[j]) continue; /* non existing keys are like empty sets */
5306
5307 di = dictGetIterator(dv[j]);
5308
5309 while((de = dictNext(di)) != NULL) {
5310 robj *ele;
5311
5312 /* dictAdd will not add the same element multiple times */
5313 ele = dictGetEntryKey(de);
5314 if (op == REDIS_OP_UNION || j == 0) {
5315 if (dictAdd(dstset->ptr,ele,NULL) == DICT_OK) {
5316 incrRefCount(ele);
5317 cardinality++;
5318 }
5319 } else if (op == REDIS_OP_DIFF) {
5320 if (dictDelete(dstset->ptr,ele) == DICT_OK) {
5321 cardinality--;
5322 }
5323 }
5324 }
5325 dictReleaseIterator(di);
5326
5327 /* result set is empty? Exit asap. */
5328 if (op == REDIS_OP_DIFF && cardinality == 0) break;
5329 }
5330
5331 /* Output the content of the resulting set, if not in STORE mode */
5332 if (!dstkey) {
5333 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
5334 di = dictGetIterator(dstset->ptr);
5335 while((de = dictNext(di)) != NULL) {
5336 robj *ele;
5337
5338 ele = dictGetEntryKey(de);
5339 addReplyBulk(c,ele);
5340 }
5341 dictReleaseIterator(di);
5342 decrRefCount(dstset);
5343 } else {
5344 /* If we have a target key where to store the resulting set
5345 * create this key with the result set inside */
5346 deleteKey(c->db,dstkey);
5347 if (dictSize((dict*)dstset->ptr) > 0) {
5348 dictAdd(c->db->dict,dstkey,dstset);
5349 incrRefCount(dstkey);
5350 addReplyLongLong(c,dictSize((dict*)dstset->ptr));
5351 } else {
5352 decrRefCount(dstset);
5353 addReply(c,shared.czero);
5354 }
5355 server.dirty++;
5356 }
5357 zfree(dv);
5358 }
5359
5360 static void sunionCommand(redisClient *c) {
5361 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
5362 }
5363
5364 static void sunionstoreCommand(redisClient *c) {
5365 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
5366 }
5367
5368 static void sdiffCommand(redisClient *c) {
5369 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
5370 }
5371
5372 static void sdiffstoreCommand(redisClient *c) {
5373 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
5374 }
5375
5376 /* ==================================== ZSets =============================== */
5377
5378 /* ZSETs are ordered sets using two data structures to hold the same elements
5379 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
5380 * data structure.
5381 *
5382 * The elements are added to an hash table mapping Redis objects to scores.
5383 * At the same time the elements are added to a skip list mapping scores
5384 * to Redis objects (so objects are sorted by scores in this "view"). */
5385
5386 /* This skiplist implementation is almost a C translation of the original
5387 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
5388 * Alternative to Balanced Trees", modified in three ways:
5389 * a) this implementation allows for repeated values.
5390 * b) the comparison is not just by key (our 'score') but by satellite data.
5391 * c) there is a back pointer, so it's a doubly linked list with the back
5392 * pointers being only at "level 1". This allows to traverse the list
5393 * from tail to head, useful for ZREVRANGE. */
5394
5395 static zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
5396 zskiplistNode *zn = zmalloc(sizeof(*zn));
5397
5398 zn->forward = zmalloc(sizeof(zskiplistNode*) * level);
5399 if (level > 0)
5400 zn->span = zmalloc(sizeof(unsigned int) * (level - 1));
5401 zn->score = score;
5402 zn->obj = obj;
5403 return zn;
5404 }
5405
5406 static zskiplist *zslCreate(void) {
5407 int j;
5408 zskiplist *zsl;
5409
5410 zsl = zmalloc(sizeof(*zsl));
5411 zsl->level = 1;
5412 zsl->length = 0;
5413 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);
5414 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++) {
5415 zsl->header->forward[j] = NULL;
5416
5417 /* span has space for ZSKIPLIST_MAXLEVEL-1 elements */
5418 if (j < ZSKIPLIST_MAXLEVEL-1)
5419 zsl->header->span[j] = 0;
5420 }
5421 zsl->header->backward = NULL;
5422 zsl->tail = NULL;
5423 return zsl;
5424 }
5425
5426 static void zslFreeNode(zskiplistNode *node) {
5427 decrRefCount(node->obj);
5428 zfree(node->forward);
5429 zfree(node->span);
5430 zfree(node);
5431 }
5432
5433 static void zslFree(zskiplist *zsl) {
5434 zskiplistNode *node = zsl->header->forward[0], *next;
5435
5436 zfree(zsl->header->forward);
5437 zfree(zsl->header->span);
5438 zfree(zsl->header);
5439 while(node) {
5440 next = node->forward[0];
5441 zslFreeNode(node);
5442 node = next;
5443 }
5444 zfree(zsl);
5445 }
5446
5447 static int zslRandomLevel(void) {
5448 int level = 1;
5449 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
5450 level += 1;
5451 return (level<ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;
5452 }
5453
5454 static void zslInsert(zskiplist *zsl, double score, robj *obj) {
5455 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5456 unsigned int rank[ZSKIPLIST_MAXLEVEL];
5457 int i, level;
5458
5459 x = zsl->header;
5460 for (i = zsl->level-1; i >= 0; i--) {
5461 /* store rank that is crossed to reach the insert position */
5462 rank[i] = i == (zsl->level-1) ? 0 : rank[i+1];
5463
5464 while (x->forward[i] &&
5465 (x->forward[i]->score < score ||
5466 (x->forward[i]->score == score &&
5467 compareStringObjects(x->forward[i]->obj,obj) < 0))) {
5468 rank[i] += i > 0 ? x->span[i-1] : 1;
5469 x = x->forward[i];
5470 }
5471 update[i] = x;
5472 }
5473 /* we assume the key is not already inside, since we allow duplicated
5474 * scores, and the re-insertion of score and redis object should never
5475 * happpen since the caller of zslInsert() should test in the hash table
5476 * if the element is already inside or not. */
5477 level = zslRandomLevel();
5478 if (level > zsl->level) {
5479 for (i = zsl->level; i < level; i++) {
5480 rank[i] = 0;
5481 update[i] = zsl->header;
5482 update[i]->span[i-1] = zsl->length;
5483 }
5484 zsl->level = level;
5485 }
5486 x = zslCreateNode(level,score,obj);
5487 for (i = 0; i < level; i++) {
5488 x->forward[i] = update[i]->forward[i];
5489 update[i]->forward[i] = x;
5490
5491 /* update span covered by update[i] as x is inserted here */
5492 if (i > 0) {
5493 x->span[i-1] = update[i]->span[i-1] - (rank[0] - rank[i]);
5494 update[i]->span[i-1] = (rank[0] - rank[i]) + 1;
5495 }
5496 }
5497
5498 /* increment span for untouched levels */
5499 for (i = level; i < zsl->level; i++) {
5500 update[i]->span[i-1]++;
5501 }
5502
5503 x->backward = (update[0] == zsl->header) ? NULL : update[0];
5504 if (x->forward[0])
5505 x->forward[0]->backward = x;
5506 else
5507 zsl->tail = x;
5508 zsl->length++;
5509 }
5510
5511 /* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
5512 void zslDeleteNode(zskiplist *zsl, zskiplistNode *x, zskiplistNode **update) {
5513 int i;
5514 for (i = 0; i < zsl->level; i++) {
5515 if (update[i]->forward[i] == x) {
5516 if (i > 0) {
5517 update[i]->span[i-1] += x->span[i-1] - 1;
5518 }
5519 update[i]->forward[i] = x->forward[i];
5520 } else {
5521 /* invariant: i > 0, because update[0]->forward[0]
5522 * is always equal to x */
5523 update[i]->span[i-1] -= 1;
5524 }
5525 }
5526 if (x->forward[0]) {
5527 x->forward[0]->backward = x->backward;
5528 } else {
5529 zsl->tail = x->backward;
5530 }
5531 while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
5532 zsl->level--;
5533 zsl->length--;
5534 }
5535
5536 /* Delete an element with matching score/object from the skiplist. */
5537 static int zslDelete(zskiplist *zsl, double score, robj *obj) {
5538 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5539 int i;
5540
5541 x = zsl->header;
5542 for (i = zsl->level-1; i >= 0; i--) {
5543 while (x->forward[i] &&
5544 (x->forward[i]->score < score ||
5545 (x->forward[i]->score == score &&
5546 compareStringObjects(x->forward[i]->obj,obj) < 0)))
5547 x = x->forward[i];
5548 update[i] = x;
5549 }
5550 /* We may have multiple elements with the same score, what we need
5551 * is to find the element with both the right score and object. */
5552 x = x->forward[0];
5553 if (x && score == x->score && equalStringObjects(x->obj,obj)) {
5554 zslDeleteNode(zsl, x, update);
5555 zslFreeNode(x);
5556 return 1;
5557 } else {
5558 return 0; /* not found */
5559 }
5560 return 0; /* not found */
5561 }
5562
5563 /* Delete all the elements with score between min and max from the skiplist.
5564 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
5565 * Note that this function takes the reference to the hash table view of the
5566 * sorted set, in order to remove the elements from the hash table too. */
5567 static unsigned long zslDeleteRangeByScore(zskiplist *zsl, double min, double max, dict *dict) {
5568 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5569 unsigned long removed = 0;
5570 int i;
5571
5572 x = zsl->header;
5573 for (i = zsl->level-1; i >= 0; i--) {
5574 while (x->forward[i] && x->forward[i]->score < min)
5575 x = x->forward[i];
5576 update[i] = x;
5577 }
5578 /* We may have multiple elements with the same score, what we need
5579 * is to find the element with both the right score and object. */
5580 x = x->forward[0];
5581 while (x && x->score <= max) {
5582 zskiplistNode *next = x->forward[0];
5583 zslDeleteNode(zsl, x, update);
5584 dictDelete(dict,x->obj);
5585 zslFreeNode(x);
5586 removed++;
5587 x = next;
5588 }
5589 return removed; /* not found */
5590 }
5591
5592 /* Delete all the elements with rank between start and end from the skiplist.
5593 * Start and end are inclusive. Note that start and end need to be 1-based */
5594 static unsigned long zslDeleteRangeByRank(zskiplist *zsl, unsigned int start, unsigned int end, dict *dict) {
5595 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
5596 unsigned long traversed = 0, removed = 0;
5597 int i;
5598
5599 x = zsl->header;
5600 for (i = zsl->level-1; i >= 0; i--) {
5601 while (x->forward[i] && (traversed + (i > 0 ? x->span[i-1] : 1)) < start) {
5602 traversed += i > 0 ? x->span[i-1] : 1;
5603 x = x->forward[i];
5604 }
5605 update[i] = x;
5606 }
5607
5608 traversed++;
5609 x = x->forward[0];
5610 while (x && traversed <= end) {
5611 zskiplistNode *next = x->forward[0];
5612 zslDeleteNode(zsl, x, update);
5613 dictDelete(dict,x->obj);
5614 zslFreeNode(x);
5615 removed++;
5616 traversed++;
5617 x = next;
5618 }
5619 return removed;
5620 }
5621
5622 /* Find the first node having a score equal or greater than the specified one.
5623 * Returns NULL if there is no match. */
5624 static zskiplistNode *zslFirstWithScore(zskiplist *zsl, double score) {
5625 zskiplistNode *x;
5626 int i;
5627
5628 x = zsl->header;
5629 for (i = zsl->level-1; i >= 0; i--) {
5630 while (x->forward[i] && x->forward[i]->score < score)
5631 x = x->forward[i];
5632 }
5633 /* We may have multiple elements with the same score, what we need
5634 * is to find the element with both the right score and object. */
5635 return x->forward[0];
5636 }
5637
5638 /* Find the rank for an element by both score and key.
5639 * Returns 0 when the element cannot be found, rank otherwise.
5640 * Note that the rank is 1-based due to the span of zsl->header to the
5641 * first element. */
5642 static unsigned long zslGetRank(zskiplist *zsl, double score, robj *o) {
5643 zskiplistNode *x;
5644 unsigned long rank = 0;
5645 int i;
5646
5647 x = zsl->header;
5648 for (i = zsl->level-1; i >= 0; i--) {
5649 while (x->forward[i] &&
5650 (x->forward[i]->score < score ||
5651 (x->forward[i]->score == score &&
5652 compareStringObjects(x->forward[i]->obj,o) <= 0))) {
5653 rank += i > 0 ? x->span[i-1] : 1;
5654 x = x->forward[i];
5655 }
5656
5657 /* x might be equal to zsl->header, so test if obj is non-NULL */
5658 if (x->obj && equalStringObjects(x->obj,o)) {
5659 return rank;
5660 }
5661 }
5662 return 0;
5663 }
5664
5665 /* Finds an element by its rank. The rank argument needs to be 1-based. */
5666 zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank) {
5667 zskiplistNode *x;
5668 unsigned long traversed = 0;
5669 int i;
5670
5671 x = zsl->header;
5672 for (i = zsl->level-1; i >= 0; i--) {
5673 while (x->forward[i] && (traversed + (i>0 ? x->span[i-1] : 1)) <= rank)
5674 {
5675 traversed += i > 0 ? x->span[i-1] : 1;
5676 x = x->forward[i];
5677 }
5678 if (traversed == rank) {
5679 return x;
5680 }
5681 }
5682 return NULL;
5683 }
5684
5685 /* The actual Z-commands implementations */
5686
5687 /* This generic command implements both ZADD and ZINCRBY.
5688 * scoreval is the score if the operation is a ZADD (doincrement == 0) or
5689 * the increment if the operation is a ZINCRBY (doincrement == 1). */
5690 static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scoreval, int doincrement) {
5691 robj *zsetobj;
5692 zset *zs;
5693 double *score;
5694
5695 zsetobj = lookupKeyWrite(c->db,key);
5696 if (zsetobj == NULL) {
5697 zsetobj = createZsetObject();
5698 dictAdd(c->db->dict,key,zsetobj);
5699 incrRefCount(key);
5700 } else {
5701 if (zsetobj->type != REDIS_ZSET) {
5702 addReply(c,shared.wrongtypeerr);
5703 return;
5704 }
5705 }
5706 zs = zsetobj->ptr;
5707
5708 /* Ok now since we implement both ZADD and ZINCRBY here the code
5709 * needs to handle the two different conditions. It's all about setting
5710 * '*score', that is, the new score to set, to the right value. */
5711 score = zmalloc(sizeof(double));
5712 if (doincrement) {
5713 dictEntry *de;
5714
5715 /* Read the old score. If the element was not present starts from 0 */
5716 de = dictFind(zs->dict,ele);
5717 if (de) {
5718 double *oldscore = dictGetEntryVal(de);
5719 *score = *oldscore + scoreval;
5720 } else {
5721 *score = scoreval;
5722 }
5723 } else {
5724 *score = scoreval;
5725 }
5726
5727 /* What follows is a simple remove and re-insert operation that is common
5728 * to both ZADD and ZINCRBY... */
5729 if (dictAdd(zs->dict,ele,score) == DICT_OK) {
5730 /* case 1: New element */
5731 incrRefCount(ele); /* added to hash */
5732 zslInsert(zs->zsl,*score,ele);
5733 incrRefCount(ele); /* added to skiplist */
5734 server.dirty++;
5735 if (doincrement)
5736 addReplyDouble(c,*score);
5737 else
5738 addReply(c,shared.cone);
5739 } else {
5740 dictEntry *de;
5741 double *oldscore;
5742
5743 /* case 2: Score update operation */
5744 de = dictFind(zs->dict,ele);
5745 redisAssert(de != NULL);
5746 oldscore = dictGetEntryVal(de);
5747 if (*score != *oldscore) {
5748 int deleted;
5749
5750 /* Remove and insert the element in the skip list with new score */
5751 deleted = zslDelete(zs->zsl,*oldscore,ele);
5752 redisAssert(deleted != 0);
5753 zslInsert(zs->zsl,*score,ele);
5754 incrRefCount(ele);
5755 /* Update the score in the hash table */
5756 dictReplace(zs->dict,ele,score);
5757 server.dirty++;
5758 } else {
5759 zfree(score);
5760 }
5761 if (doincrement)
5762 addReplyDouble(c,*score);
5763 else
5764 addReply(c,shared.czero);
5765 }
5766 }
5767
5768 static void zaddCommand(redisClient *c) {
5769 double scoreval;
5770
5771 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
5772 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,0);
5773 }
5774
5775 static void zincrbyCommand(redisClient *c) {
5776 double scoreval;
5777
5778 if (getDoubleFromObjectOrReply(c, c->argv[2], &scoreval, NULL) != REDIS_OK) return;
5779 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,1);
5780 }
5781
5782 static void zremCommand(redisClient *c) {
5783 robj *zsetobj;
5784 zset *zs;
5785 dictEntry *de;
5786 double *oldscore;
5787 int deleted;
5788
5789 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5790 checkType(c,zsetobj,REDIS_ZSET)) return;
5791
5792 zs = zsetobj->ptr;
5793 de = dictFind(zs->dict,c->argv[2]);
5794 if (de == NULL) {
5795 addReply(c,shared.czero);
5796 return;
5797 }
5798 /* Delete from the skiplist */
5799 oldscore = dictGetEntryVal(de);
5800 deleted = zslDelete(zs->zsl,*oldscore,c->argv[2]);
5801 redisAssert(deleted != 0);
5802
5803 /* Delete from the hash table */
5804 dictDelete(zs->dict,c->argv[2]);
5805 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
5806 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
5807 server.dirty++;
5808 addReply(c,shared.cone);
5809 }
5810
5811 static void zremrangebyscoreCommand(redisClient *c) {
5812 double min;
5813 double max;
5814 long deleted;
5815 robj *zsetobj;
5816 zset *zs;
5817
5818 if ((getDoubleFromObjectOrReply(c, c->argv[2], &min, NULL) != REDIS_OK) ||
5819 (getDoubleFromObjectOrReply(c, c->argv[3], &max, NULL) != REDIS_OK)) return;
5820
5821 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5822 checkType(c,zsetobj,REDIS_ZSET)) return;
5823
5824 zs = zsetobj->ptr;
5825 deleted = zslDeleteRangeByScore(zs->zsl,min,max,zs->dict);
5826 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
5827 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
5828 server.dirty += deleted;
5829 addReplyLongLong(c,deleted);
5830 }
5831
5832 static void zremrangebyrankCommand(redisClient *c) {
5833 long start;
5834 long end;
5835 int llen;
5836 long deleted;
5837 robj *zsetobj;
5838 zset *zs;
5839
5840 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
5841 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
5842
5843 if ((zsetobj = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
5844 checkType(c,zsetobj,REDIS_ZSET)) return;
5845 zs = zsetobj->ptr;
5846 llen = zs->zsl->length;
5847
5848 /* convert negative indexes */
5849 if (start < 0) start = llen+start;
5850 if (end < 0) end = llen+end;
5851 if (start < 0) start = 0;
5852 if (end < 0) end = 0;
5853
5854 /* indexes sanity checks */
5855 if (start > end || start >= llen) {
5856 addReply(c,shared.czero);
5857 return;
5858 }
5859 if (end >= llen) end = llen-1;
5860
5861 /* increment start and end because zsl*Rank functions
5862 * use 1-based rank */
5863 deleted = zslDeleteRangeByRank(zs->zsl,start+1,end+1,zs->dict);
5864 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
5865 if (dictSize(zs->dict) == 0) deleteKey(c->db,c->argv[1]);
5866 server.dirty += deleted;
5867 addReplyLongLong(c, deleted);
5868 }
5869
5870 typedef struct {
5871 dict *dict;
5872 double weight;
5873 } zsetopsrc;
5874
5875 static int qsortCompareZsetopsrcByCardinality(const void *s1, const void *s2) {
5876 zsetopsrc *d1 = (void*) s1, *d2 = (void*) s2;
5877 unsigned long size1, size2;
5878 size1 = d1->dict ? dictSize(d1->dict) : 0;
5879 size2 = d2->dict ? dictSize(d2->dict) : 0;
5880 return size1 - size2;
5881 }
5882
5883 #define REDIS_AGGR_SUM 1
5884 #define REDIS_AGGR_MIN 2
5885 #define REDIS_AGGR_MAX 3
5886
5887 inline static void zunionInterAggregate(double *target, double val, int aggregate) {
5888 if (aggregate == REDIS_AGGR_SUM) {
5889 *target = *target + val;
5890 } else if (aggregate == REDIS_AGGR_MIN) {
5891 *target = val < *target ? val : *target;
5892 } else if (aggregate == REDIS_AGGR_MAX) {
5893 *target = val > *target ? val : *target;
5894 } else {
5895 /* safety net */
5896 redisPanic("Unknown ZUNION/INTER aggregate type");
5897 }
5898 }
5899
5900 static void zunionInterGenericCommand(redisClient *c, robj *dstkey, int op) {
5901 int i, j, zsetnum;
5902 int aggregate = REDIS_AGGR_SUM;
5903 zsetopsrc *src;
5904 robj *dstobj;
5905 zset *dstzset;
5906 dictIterator *di;
5907 dictEntry *de;
5908
5909 /* expect zsetnum input keys to be given */
5910 zsetnum = atoi(c->argv[2]->ptr);
5911 if (zsetnum < 1) {
5912 addReplySds(c,sdsnew("-ERR at least 1 input key is needed for ZUNIONSTORE/ZINTERSTORE\r\n"));
5913 return;
5914 }
5915
5916 /* test if the expected number of keys would overflow */
5917 if (3+zsetnum > c->argc) {
5918 addReply(c,shared.syntaxerr);
5919 return;
5920 }
5921
5922 /* read keys to be used for input */
5923 src = zmalloc(sizeof(zsetopsrc) * zsetnum);
5924 for (i = 0, j = 3; i < zsetnum; i++, j++) {
5925 robj *zsetobj = lookupKeyWrite(c->db,c->argv[j]);
5926 if (!zsetobj) {
5927 src[i].dict = NULL;
5928 } else {
5929 if (zsetobj->type != REDIS_ZSET) {
5930 zfree(src);
5931 addReply(c,shared.wrongtypeerr);
5932 return;
5933 }
5934 src[i].dict = ((zset*)zsetobj->ptr)->dict;
5935 }
5936
5937 /* default all weights to 1 */
5938 src[i].weight = 1.0;
5939 }
5940
5941 /* parse optional extra arguments */
5942 if (j < c->argc) {
5943 int remaining = c->argc - j;
5944
5945 while (remaining) {
5946 if (remaining >= (zsetnum + 1) && !strcasecmp(c->argv[j]->ptr,"weights")) {
5947 j++; remaining--;
5948 for (i = 0; i < zsetnum; i++, j++, remaining--) {
5949 if (getDoubleFromObjectOrReply(c, c->argv[j], &src[i].weight, NULL) != REDIS_OK)
5950 return;
5951 }
5952 } else if (remaining >= 2 && !strcasecmp(c->argv[j]->ptr,"aggregate")) {
5953 j++; remaining--;
5954 if (!strcasecmp(c->argv[j]->ptr,"sum")) {
5955 aggregate = REDIS_AGGR_SUM;
5956 } else if (!strcasecmp(c->argv[j]->ptr,"min")) {
5957 aggregate = REDIS_AGGR_MIN;
5958 } else if (!strcasecmp(c->argv[j]->ptr,"max")) {
5959 aggregate = REDIS_AGGR_MAX;
5960 } else {
5961 zfree(src);
5962 addReply(c,shared.syntaxerr);
5963 return;
5964 }
5965 j++; remaining--;
5966 } else {
5967 zfree(src);
5968 addReply(c,shared.syntaxerr);
5969 return;
5970 }
5971 }
5972 }
5973
5974 /* sort sets from the smallest to largest, this will improve our
5975 * algorithm's performance */
5976 qsort(src,zsetnum,sizeof(zsetopsrc), qsortCompareZsetopsrcByCardinality);
5977
5978 dstobj = createZsetObject();
5979 dstzset = dstobj->ptr;
5980
5981 if (op == REDIS_OP_INTER) {
5982 /* skip going over all entries if the smallest zset is NULL or empty */
5983 if (src[0].dict && dictSize(src[0].dict) > 0) {
5984 /* precondition: as src[0].dict is non-empty and the zsets are ordered
5985 * from small to large, all src[i > 0].dict are non-empty too */
5986 di = dictGetIterator(src[0].dict);
5987 while((de = dictNext(di)) != NULL) {
5988 double *score = zmalloc(sizeof(double)), value;
5989 *score = src[0].weight * (*(double*)dictGetEntryVal(de));
5990
5991 for (j = 1; j < zsetnum; j++) {
5992 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
5993 if (other) {
5994 value = src[j].weight * (*(double*)dictGetEntryVal(other));
5995 zunionInterAggregate(score, value, aggregate);
5996 } else {
5997 break;
5998 }
5999 }
6000
6001 /* skip entry when not present in every source dict */
6002 if (j != zsetnum) {
6003 zfree(score);
6004 } else {
6005 robj *o = dictGetEntryKey(de);
6006 dictAdd(dstzset->dict,o,score);
6007 incrRefCount(o); /* added to dictionary */
6008 zslInsert(dstzset->zsl,*score,o);
6009 incrRefCount(o); /* added to skiplist */
6010 }
6011 }
6012 dictReleaseIterator(di);
6013 }
6014 } else if (op == REDIS_OP_UNION) {
6015 for (i = 0; i < zsetnum; i++) {
6016 if (!src[i].dict) continue;
6017
6018 di = dictGetIterator(src[i].dict);
6019 while((de = dictNext(di)) != NULL) {
6020 /* skip key when already processed */
6021 if (dictFind(dstzset->dict,dictGetEntryKey(de)) != NULL) continue;
6022
6023 double *score = zmalloc(sizeof(double)), value;
6024 *score = src[i].weight * (*(double*)dictGetEntryVal(de));
6025
6026 /* because the zsets are sorted by size, its only possible
6027 * for sets at larger indices to hold this entry */
6028 for (j = (i+1); j < zsetnum; j++) {
6029 dictEntry *other = dictFind(src[j].dict,dictGetEntryKey(de));
6030 if (other) {
6031 value = src[j].weight * (*(double*)dictGetEntryVal(other));
6032 zunionInterAggregate(score, value, aggregate);
6033 }
6034 }
6035
6036 robj *o = dictGetEntryKey(de);
6037 dictAdd(dstzset->dict,o,score);
6038 incrRefCount(o); /* added to dictionary */
6039 zslInsert(dstzset->zsl,*score,o);
6040 incrRefCount(o); /* added to skiplist */
6041 }
6042 dictReleaseIterator(di);
6043 }
6044 } else {
6045 /* unknown operator */
6046 redisAssert(op == REDIS_OP_INTER || op == REDIS_OP_UNION);
6047 }
6048
6049 deleteKey(c->db,dstkey);
6050 if (dstzset->zsl->length) {
6051 dictAdd(c->db->dict,dstkey,dstobj);
6052 incrRefCount(dstkey);
6053 addReplyLongLong(c, dstzset->zsl->length);
6054 server.dirty++;
6055 } else {
6056 decrRefCount(dstobj);
6057 addReply(c, shared.czero);
6058 }
6059 zfree(src);
6060 }
6061
6062 static void zunionstoreCommand(redisClient *c) {
6063 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_UNION);
6064 }
6065
6066 static void zinterstoreCommand(redisClient *c) {
6067 zunionInterGenericCommand(c,c->argv[1], REDIS_OP_INTER);
6068 }
6069
6070 static void zrangeGenericCommand(redisClient *c, int reverse) {
6071 robj *o;
6072 long start;
6073 long end;
6074 int withscores = 0;
6075 int llen;
6076 int rangelen, j;
6077 zset *zsetobj;
6078 zskiplist *zsl;
6079 zskiplistNode *ln;
6080 robj *ele;
6081
6082 if ((getLongFromObjectOrReply(c, c->argv[2], &start, NULL) != REDIS_OK) ||
6083 (getLongFromObjectOrReply(c, c->argv[3], &end, NULL) != REDIS_OK)) return;
6084
6085 if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) {
6086 withscores = 1;
6087 } else if (c->argc >= 5) {
6088 addReply(c,shared.syntaxerr);
6089 return;
6090 }
6091
6092 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
6093 || checkType(c,o,REDIS_ZSET)) return;
6094 zsetobj = o->ptr;
6095 zsl = zsetobj->zsl;
6096 llen = zsl->length;
6097
6098 /* convert negative indexes */
6099 if (start < 0) start = llen+start;
6100 if (end < 0) end = llen+end;
6101 if (start < 0) start = 0;
6102 if (end < 0) end = 0;
6103
6104 /* indexes sanity checks */
6105 if (start > end || start >= llen) {
6106 /* Out of range start or start > end result in empty list */
6107 addReply(c,shared.emptymultibulk);
6108 return;
6109 }
6110 if (end >= llen) end = llen-1;
6111 rangelen = (end-start)+1;
6112
6113 /* check if starting point is trivial, before searching
6114 * the element in log(N) time */
6115 if (reverse) {
6116 ln = start == 0 ? zsl->tail : zslGetElementByRank(zsl, llen-start);
6117 } else {
6118 ln = start == 0 ?
6119 zsl->header->forward[0] : zslGetElementByRank(zsl, start+1);
6120 }
6121
6122 /* Return the result in form of a multi-bulk reply */
6123 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",
6124 withscores ? (rangelen*2) : rangelen));
6125 for (j = 0; j < rangelen; j++) {
6126 ele = ln->obj;
6127 addReplyBulk(c,ele);
6128 if (withscores)
6129 addReplyDouble(c,ln->score);
6130 ln = reverse ? ln->backward : ln->forward[0];
6131 }
6132 }
6133
6134 static void zrangeCommand(redisClient *c) {
6135 zrangeGenericCommand(c,0);
6136 }
6137
6138 static void zrevrangeCommand(redisClient *c) {
6139 zrangeGenericCommand(c,1);
6140 }
6141
6142 /* This command implements both ZRANGEBYSCORE and ZCOUNT.
6143 * If justcount is non-zero, just the count is returned. */
6144 static void genericZrangebyscoreCommand(redisClient *c, int justcount) {
6145 robj *o;
6146 double min, max;
6147 int minex = 0, maxex = 0; /* are min or max exclusive? */
6148 int offset = 0, limit = -1;
6149 int withscores = 0;
6150 int badsyntax = 0;
6151
6152 /* Parse the min-max interval. If one of the values is prefixed
6153 * by the "(" character, it's considered "open". For instance
6154 * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max
6155 * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */
6156 if (((char*)c->argv[2]->ptr)[0] == '(') {
6157 min = strtod((char*)c->argv[2]->ptr+1,NULL);
6158 minex = 1;
6159 } else {
6160 min = strtod(c->argv[2]->ptr,NULL);
6161 }
6162 if (((char*)c->argv[3]->ptr)[0] == '(') {
6163 max = strtod((char*)c->argv[3]->ptr+1,NULL);
6164 maxex = 1;
6165 } else {
6166 max = strtod(c->argv[3]->ptr,NULL);
6167 }
6168
6169 /* Parse "WITHSCORES": note that if the command was called with
6170 * the name ZCOUNT then we are sure that c->argc == 4, so we'll never
6171 * enter the following paths to parse WITHSCORES and LIMIT. */
6172 if (c->argc == 5 || c->argc == 8) {
6173 if (strcasecmp(c->argv[c->argc-1]->ptr,"withscores") == 0)
6174 withscores = 1;
6175 else
6176 badsyntax = 1;
6177 }
6178 if (c->argc != (4 + withscores) && c->argc != (7 + withscores))
6179 badsyntax = 1;
6180 if (badsyntax) {
6181 addReplySds(c,
6182 sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
6183 return;
6184 }
6185
6186 /* Parse "LIMIT" */
6187 if (c->argc == (7 + withscores) && strcasecmp(c->argv[4]->ptr,"limit")) {
6188 addReply(c,shared.syntaxerr);
6189 return;
6190 } else if (c->argc == (7 + withscores)) {
6191 offset = atoi(c->argv[5]->ptr);
6192 limit = atoi(c->argv[6]->ptr);
6193 if (offset < 0) offset = 0;
6194 }
6195
6196 /* Ok, lookup the key and get the range */
6197 o = lookupKeyRead(c->db,c->argv[1]);
6198 if (o == NULL) {
6199 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
6200 } else {
6201 if (o->type != REDIS_ZSET) {
6202 addReply(c,shared.wrongtypeerr);
6203 } else {
6204 zset *zsetobj = o->ptr;
6205 zskiplist *zsl = zsetobj->zsl;
6206 zskiplistNode *ln;
6207 robj *ele, *lenobj = NULL;
6208 unsigned long rangelen = 0;
6209
6210 /* Get the first node with the score >= min, or with
6211 * score > min if 'minex' is true. */
6212 ln = zslFirstWithScore(zsl,min);
6213 while (minex && ln && ln->score == min) ln = ln->forward[0];
6214
6215 if (ln == NULL) {
6216 /* No element matching the speciifed interval */
6217 addReply(c,justcount ? shared.czero : shared.emptymultibulk);
6218 return;
6219 }
6220
6221 /* We don't know in advance how many matching elements there
6222 * are in the list, so we push this object that will represent
6223 * the multi-bulk length in the output buffer, and will "fix"
6224 * it later */
6225 if (!justcount) {
6226 lenobj = createObject(REDIS_STRING,NULL);
6227 addReply(c,lenobj);
6228 decrRefCount(lenobj);
6229 }
6230
6231 while(ln && (maxex ? (ln->score < max) : (ln->score <= max))) {
6232 if (offset) {
6233 offset--;
6234 ln = ln->forward[0];
6235 continue;
6236 }
6237 if (limit == 0) break;
6238 if (!justcount) {
6239 ele = ln->obj;
6240 addReplyBulk(c,ele);
6241 if (withscores)
6242 addReplyDouble(c,ln->score);
6243 }
6244 ln = ln->forward[0];
6245 rangelen++;
6246 if (limit > 0) limit--;
6247 }
6248 if (justcount) {
6249 addReplyLongLong(c,(long)rangelen);
6250 } else {
6251 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",
6252 withscores ? (rangelen*2) : rangelen);
6253 }
6254 }
6255 }
6256 }
6257
6258 static void zrangebyscoreCommand(redisClient *c) {
6259 genericZrangebyscoreCommand(c,0);
6260 }
6261
6262 static void zcountCommand(redisClient *c) {
6263 genericZrangebyscoreCommand(c,1);
6264 }
6265
6266 static void zcardCommand(redisClient *c) {
6267 robj *o;
6268 zset *zs;
6269
6270 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6271 checkType(c,o,REDIS_ZSET)) return;
6272
6273 zs = o->ptr;
6274 addReplyUlong(c,zs->zsl->length);
6275 }
6276
6277 static void zscoreCommand(redisClient *c) {
6278 robj *o;
6279 zset *zs;
6280 dictEntry *de;
6281
6282 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6283 checkType(c,o,REDIS_ZSET)) return;
6284
6285 zs = o->ptr;
6286 de = dictFind(zs->dict,c->argv[2]);
6287 if (!de) {
6288 addReply(c,shared.nullbulk);
6289 } else {
6290 double *score = dictGetEntryVal(de);
6291
6292 addReplyDouble(c,*score);
6293 }
6294 }
6295
6296 static void zrankGenericCommand(redisClient *c, int reverse) {
6297 robj *o;
6298 zset *zs;
6299 zskiplist *zsl;
6300 dictEntry *de;
6301 unsigned long rank;
6302 double *score;
6303
6304 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6305 checkType(c,o,REDIS_ZSET)) return;
6306
6307 zs = o->ptr;
6308 zsl = zs->zsl;
6309 de = dictFind(zs->dict,c->argv[2]);
6310 if (!de) {
6311 addReply(c,shared.nullbulk);
6312 return;
6313 }
6314
6315 score = dictGetEntryVal(de);
6316 rank = zslGetRank(zsl, *score, c->argv[2]);
6317 if (rank) {
6318 if (reverse) {
6319 addReplyLongLong(c, zsl->length - rank);
6320 } else {
6321 addReplyLongLong(c, rank-1);
6322 }
6323 } else {
6324 addReply(c,shared.nullbulk);
6325 }
6326 }
6327
6328 static void zrankCommand(redisClient *c) {
6329 zrankGenericCommand(c, 0);
6330 }
6331
6332 static void zrevrankCommand(redisClient *c) {
6333 zrankGenericCommand(c, 1);
6334 }
6335
6336 /* ========================= Hashes utility functions ======================= */
6337 #define REDIS_HASH_KEY 1
6338 #define REDIS_HASH_VALUE 2
6339
6340 /* Check the length of a number of objects to see if we need to convert a
6341 * zipmap to a real hash. Note that we only check string encoded objects
6342 * as their string length can be queried in constant time. */
6343 static void hashTryConversion(robj *subject, robj **argv, int start, int end) {
6344 int i;
6345 if (subject->encoding != REDIS_ENCODING_ZIPMAP) return;
6346
6347 for (i = start; i <= end; i++) {
6348 if (argv[i]->encoding == REDIS_ENCODING_RAW &&
6349 sdslen(argv[i]->ptr) > server.hash_max_zipmap_value)
6350 {
6351 convertToRealHash(subject);
6352 return;
6353 }
6354 }
6355 }
6356
6357 /* Encode given objects in-place when the hash uses a dict. */
6358 static void hashTryObjectEncoding(robj *subject, robj **o1, robj **o2) {
6359 if (subject->encoding == REDIS_ENCODING_HT) {
6360 if (o1) *o1 = tryObjectEncoding(*o1);
6361 if (o2) *o2 = tryObjectEncoding(*o2);
6362 }
6363 }
6364
6365 /* Get the value from a hash identified by key. Returns either a string
6366 * object or NULL if the value cannot be found. The refcount of the object
6367 * is always increased by 1 when the value was found. */
6368 static robj *hashGet(robj *o, robj *key) {
6369 robj *value = NULL;
6370 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6371 unsigned char *v;
6372 unsigned int vlen;
6373 key = getDecodedObject(key);
6374 if (zipmapGet(o->ptr,key->ptr,sdslen(key->ptr),&v,&vlen)) {
6375 value = createStringObject((char*)v,vlen);
6376 }
6377 decrRefCount(key);
6378 } else {
6379 dictEntry *de = dictFind(o->ptr,key);
6380 if (de != NULL) {
6381 value = dictGetEntryVal(de);
6382 incrRefCount(value);
6383 }
6384 }
6385 return value;
6386 }
6387
6388 /* Test if the key exists in the given hash. Returns 1 if the key
6389 * exists and 0 when it doesn't. */
6390 static int hashExists(robj *o, robj *key) {
6391 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6392 key = getDecodedObject(key);
6393 if (zipmapExists(o->ptr,key->ptr,sdslen(key->ptr))) {
6394 decrRefCount(key);
6395 return 1;
6396 }
6397 decrRefCount(key);
6398 } else {
6399 if (dictFind(o->ptr,key) != NULL) {
6400 return 1;
6401 }
6402 }
6403 return 0;
6404 }
6405
6406 /* Add an element, discard the old if the key already exists.
6407 * Return 0 on insert and 1 on update. */
6408 static int hashSet(robj *o, robj *key, robj *value) {
6409 int update = 0;
6410 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6411 key = getDecodedObject(key);
6412 value = getDecodedObject(value);
6413 o->ptr = zipmapSet(o->ptr,
6414 key->ptr,sdslen(key->ptr),
6415 value->ptr,sdslen(value->ptr), &update);
6416 decrRefCount(key);
6417 decrRefCount(value);
6418
6419 /* Check if the zipmap needs to be upgraded to a real hash table */
6420 if (zipmapLen(o->ptr) > server.hash_max_zipmap_entries)
6421 convertToRealHash(o);
6422 } else {
6423 if (dictReplace(o->ptr,key,value)) {
6424 /* Insert */
6425 incrRefCount(key);
6426 } else {
6427 /* Update */
6428 update = 1;
6429 }
6430 incrRefCount(value);
6431 }
6432 return update;
6433 }
6434
6435 /* Delete an element from a hash.
6436 * Return 1 on deleted and 0 on not found. */
6437 static int hashDelete(robj *o, robj *key) {
6438 int deleted = 0;
6439 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
6440 key = getDecodedObject(key);
6441 o->ptr = zipmapDel(o->ptr,key->ptr,sdslen(key->ptr), &deleted);
6442 decrRefCount(key);
6443 } else {
6444 deleted = dictDelete((dict*)o->ptr,key) == DICT_OK;
6445 /* Always check if the dictionary needs a resize after a delete. */
6446 if (deleted && htNeedsResize(o->ptr)) dictResize(o->ptr);
6447 }
6448 return deleted;
6449 }
6450
6451 /* Return the number of elements in a hash. */
6452 static unsigned long hashLength(robj *o) {
6453 return (o->encoding == REDIS_ENCODING_ZIPMAP) ?
6454 zipmapLen((unsigned char*)o->ptr) : dictSize((dict*)o->ptr);
6455 }
6456
6457 /* Structure to hold hash iteration abstration. Note that iteration over
6458 * hashes involves both fields and values. Because it is possible that
6459 * not both are required, store pointers in the iterator to avoid
6460 * unnecessary memory allocation for fields/values. */
6461 typedef struct {
6462 int encoding;
6463 unsigned char *zi;
6464 unsigned char *zk, *zv;
6465 unsigned int zklen, zvlen;
6466
6467 dictIterator *di;
6468 dictEntry *de;
6469 } hashIterator;
6470
6471 static hashIterator *hashInitIterator(robj *subject) {
6472 hashIterator *hi = zmalloc(sizeof(hashIterator));
6473 hi->encoding = subject->encoding;
6474 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6475 hi->zi = zipmapRewind(subject->ptr);
6476 } else if (hi->encoding == REDIS_ENCODING_HT) {
6477 hi->di = dictGetIterator(subject->ptr);
6478 } else {
6479 redisAssert(NULL);
6480 }
6481 return hi;
6482 }
6483
6484 static void hashReleaseIterator(hashIterator *hi) {
6485 if (hi->encoding == REDIS_ENCODING_HT) {
6486 dictReleaseIterator(hi->di);
6487 }
6488 zfree(hi);
6489 }
6490
6491 /* Move to the next entry in the hash. Return REDIS_OK when the next entry
6492 * could be found and REDIS_ERR when the iterator reaches the end. */
6493 static int hashNext(hashIterator *hi) {
6494 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6495 if ((hi->zi = zipmapNext(hi->zi, &hi->zk, &hi->zklen,
6496 &hi->zv, &hi->zvlen)) == NULL) return REDIS_ERR;
6497 } else {
6498 if ((hi->de = dictNext(hi->di)) == NULL) return REDIS_ERR;
6499 }
6500 return REDIS_OK;
6501 }
6502
6503 /* Get key or value object at current iteration position.
6504 * This increases the refcount of the field object by 1. */
6505 static robj *hashCurrent(hashIterator *hi, int what) {
6506 robj *o;
6507 if (hi->encoding == REDIS_ENCODING_ZIPMAP) {
6508 if (what & REDIS_HASH_KEY) {
6509 o = createStringObject((char*)hi->zk,hi->zklen);
6510 } else {
6511 o = createStringObject((char*)hi->zv,hi->zvlen);
6512 }
6513 } else {
6514 if (what & REDIS_HASH_KEY) {
6515 o = dictGetEntryKey(hi->de);
6516 } else {
6517 o = dictGetEntryVal(hi->de);
6518 }
6519 incrRefCount(o);
6520 }
6521 return o;
6522 }
6523
6524 static robj *hashLookupWriteOrCreate(redisClient *c, robj *key) {
6525 robj *o = lookupKeyWrite(c->db,key);
6526 if (o == NULL) {
6527 o = createHashObject();
6528 dictAdd(c->db->dict,key,o);
6529 incrRefCount(key);
6530 } else {
6531 if (o->type != REDIS_HASH) {
6532 addReply(c,shared.wrongtypeerr);
6533 return NULL;
6534 }
6535 }
6536 return o;
6537 }
6538
6539 /* ============================= Hash commands ============================== */
6540 static void hsetCommand(redisClient *c) {
6541 int update;
6542 robj *o;
6543
6544 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6545 hashTryConversion(o,c->argv,2,3);
6546 hashTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
6547 update = hashSet(o,c->argv[2],c->argv[3]);
6548 addReply(c, update ? shared.czero : shared.cone);
6549 server.dirty++;
6550 }
6551
6552 static void hsetnxCommand(redisClient *c) {
6553 robj *o;
6554 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6555 hashTryConversion(o,c->argv,2,3);
6556
6557 if (hashExists(o, c->argv[2])) {
6558 addReply(c, shared.czero);
6559 } else {
6560 hashTryObjectEncoding(o,&c->argv[2], &c->argv[3]);
6561 hashSet(o,c->argv[2],c->argv[3]);
6562 addReply(c, shared.cone);
6563 server.dirty++;
6564 }
6565 }
6566
6567 static void hmsetCommand(redisClient *c) {
6568 int i;
6569 robj *o;
6570
6571 if ((c->argc % 2) == 1) {
6572 addReplySds(c,sdsnew("-ERR wrong number of arguments for HMSET\r\n"));
6573 return;
6574 }
6575
6576 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6577 hashTryConversion(o,c->argv,2,c->argc-1);
6578 for (i = 2; i < c->argc; i += 2) {
6579 hashTryObjectEncoding(o,&c->argv[i], &c->argv[i+1]);
6580 hashSet(o,c->argv[i],c->argv[i+1]);
6581 }
6582 addReply(c, shared.ok);
6583 server.dirty++;
6584 }
6585
6586 static void hincrbyCommand(redisClient *c) {
6587 long long value, incr;
6588 robj *o, *current, *new;
6589
6590 if (getLongLongFromObjectOrReply(c,c->argv[3],&incr,NULL) != REDIS_OK) return;
6591 if ((o = hashLookupWriteOrCreate(c,c->argv[1])) == NULL) return;
6592 if ((current = hashGet(o,c->argv[2])) != NULL) {
6593 if (getLongLongFromObjectOrReply(c,current,&value,
6594 "hash value is not an integer") != REDIS_OK) {
6595 decrRefCount(current);
6596 return;
6597 }
6598 decrRefCount(current);
6599 } else {
6600 value = 0;
6601 }
6602
6603 value += incr;
6604 new = createStringObjectFromLongLong(value);
6605 hashTryObjectEncoding(o,&c->argv[2],NULL);
6606 hashSet(o,c->argv[2],new);
6607 decrRefCount(new);
6608 addReplyLongLong(c,value);
6609 server.dirty++;
6610 }
6611
6612 static void hgetCommand(redisClient *c) {
6613 robj *o, *value;
6614 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
6615 checkType(c,o,REDIS_HASH)) return;
6616
6617 if ((value = hashGet(o,c->argv[2])) != NULL) {
6618 addReplyBulk(c,value);
6619 decrRefCount(value);
6620 } else {
6621 addReply(c,shared.nullbulk);
6622 }
6623 }
6624
6625 static void hmgetCommand(redisClient *c) {
6626 int i;
6627 robj *o, *value;
6628 o = lookupKeyRead(c->db,c->argv[1]);
6629 if (o != NULL && o->type != REDIS_HASH) {
6630 addReply(c,shared.wrongtypeerr);
6631 }
6632
6633 /* Note the check for o != NULL happens inside the loop. This is
6634 * done because objects that cannot be found are considered to be
6635 * an empty hash. The reply should then be a series of NULLs. */
6636 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-2));
6637 for (i = 2; i < c->argc; i++) {
6638 if (o != NULL && (value = hashGet(o,c->argv[i])) != NULL) {
6639 addReplyBulk(c,value);
6640 decrRefCount(value);
6641 } else {
6642 addReply(c,shared.nullbulk);
6643 }
6644 }
6645 }
6646
6647 static void hdelCommand(redisClient *c) {
6648 robj *o;
6649 if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
6650 checkType(c,o,REDIS_HASH)) return;
6651
6652 if (hashDelete(o,c->argv[2])) {
6653 if (hashLength(o) == 0) deleteKey(c->db,c->argv[1]);
6654 addReply(c,shared.cone);
6655 server.dirty++;
6656 } else {
6657 addReply(c,shared.czero);
6658 }
6659 }
6660
6661 static void hlenCommand(redisClient *c) {
6662 robj *o;
6663 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6664 checkType(c,o,REDIS_HASH)) return;
6665
6666 addReplyUlong(c,hashLength(o));
6667 }
6668
6669 static void genericHgetallCommand(redisClient *c, int flags) {
6670 robj *o, *lenobj, *obj;
6671 unsigned long count = 0;
6672 hashIterator *hi;
6673
6674 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk)) == NULL
6675 || checkType(c,o,REDIS_HASH)) return;
6676
6677 lenobj = createObject(REDIS_STRING,NULL);
6678 addReply(c,lenobj);
6679 decrRefCount(lenobj);
6680
6681 hi = hashInitIterator(o);
6682 while (hashNext(hi) != REDIS_ERR) {
6683 if (flags & REDIS_HASH_KEY) {
6684 obj = hashCurrent(hi,REDIS_HASH_KEY);
6685 addReplyBulk(c,obj);
6686 decrRefCount(obj);
6687 count++;
6688 }
6689 if (flags & REDIS_HASH_VALUE) {
6690 obj = hashCurrent(hi,REDIS_HASH_VALUE);
6691 addReplyBulk(c,obj);
6692 decrRefCount(obj);
6693 count++;
6694 }
6695 }
6696 hashReleaseIterator(hi);
6697
6698 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",count);
6699 }
6700
6701 static void hkeysCommand(redisClient *c) {
6702 genericHgetallCommand(c,REDIS_HASH_KEY);
6703 }
6704
6705 static void hvalsCommand(redisClient *c) {
6706 genericHgetallCommand(c,REDIS_HASH_VALUE);
6707 }
6708
6709 static void hgetallCommand(redisClient *c) {
6710 genericHgetallCommand(c,REDIS_HASH_KEY|REDIS_HASH_VALUE);
6711 }
6712
6713 static void hexistsCommand(redisClient *c) {
6714 robj *o;
6715 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
6716 checkType(c,o,REDIS_HASH)) return;
6717
6718 addReply(c, hashExists(o,c->argv[2]) ? shared.cone : shared.czero);
6719 }
6720
6721 static void convertToRealHash(robj *o) {
6722 unsigned char *key, *val, *p, *zm = o->ptr;
6723 unsigned int klen, vlen;
6724 dict *dict = dictCreate(&hashDictType,NULL);
6725
6726 assert(o->type == REDIS_HASH && o->encoding != REDIS_ENCODING_HT);
6727 p = zipmapRewind(zm);
6728 while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
6729 robj *keyobj, *valobj;
6730
6731 keyobj = createStringObject((char*)key,klen);
6732 valobj = createStringObject((char*)val,vlen);
6733 keyobj = tryObjectEncoding(keyobj);
6734 valobj = tryObjectEncoding(valobj);
6735 dictAdd(dict,keyobj,valobj);
6736 }
6737 o->encoding = REDIS_ENCODING_HT;
6738 o->ptr = dict;
6739 zfree(zm);
6740 }
6741
6742 /* ========================= Non type-specific commands ==================== */
6743
6744 static void flushdbCommand(redisClient *c) {
6745 server.dirty += dictSize(c->db->dict);
6746 dictEmpty(c->db->dict);
6747 dictEmpty(c->db->expires);
6748 addReply(c,shared.ok);
6749 }
6750
6751 static void flushallCommand(redisClient *c) {
6752 server.dirty += emptyDb();
6753 addReply(c,shared.ok);
6754 if (server.bgsavechildpid != -1) {
6755 kill(server.bgsavechildpid,SIGKILL);
6756 rdbRemoveTempFile(server.bgsavechildpid);
6757 }
6758 rdbSave(server.dbfilename);
6759 server.dirty++;
6760 }
6761
6762 static redisSortOperation *createSortOperation(int type, robj *pattern) {
6763 redisSortOperation *so = zmalloc(sizeof(*so));
6764 so->type = type;
6765 so->pattern = pattern;
6766 return so;
6767 }
6768
6769 /* Return the value associated to the key with a name obtained
6770 * substituting the first occurence of '*' in 'pattern' with 'subst'.
6771 * The returned object will always have its refcount increased by 1
6772 * when it is non-NULL. */
6773 static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
6774 char *p, *f;
6775 sds spat, ssub;
6776 robj keyobj, fieldobj, *o;
6777 int prefixlen, sublen, postfixlen, fieldlen;
6778 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
6779 struct {
6780 long len;
6781 long free;
6782 char buf[REDIS_SORTKEY_MAX+1];
6783 } keyname, fieldname;
6784
6785 /* If the pattern is "#" return the substitution object itself in order
6786 * to implement the "SORT ... GET #" feature. */
6787 spat = pattern->ptr;
6788 if (spat[0] == '#' && spat[1] == '\0') {
6789 incrRefCount(subst);
6790 return subst;
6791 }
6792
6793 /* The substitution object may be specially encoded. If so we create
6794 * a decoded object on the fly. Otherwise getDecodedObject will just
6795 * increment the ref count, that we'll decrement later. */
6796 subst = getDecodedObject(subst);
6797
6798 ssub = subst->ptr;
6799 if (sdslen(spat)+sdslen(ssub)-1 > REDIS_SORTKEY_MAX) return NULL;
6800 p = strchr(spat,'*');
6801 if (!p) {
6802 decrRefCount(subst);
6803 return NULL;
6804 }
6805
6806 /* Find out if we're dealing with a hash dereference. */
6807 if ((f = strstr(p+1, "->")) != NULL) {
6808 fieldlen = sdslen(spat)-(f-spat);
6809 /* this also copies \0 character */
6810 memcpy(fieldname.buf,f+2,fieldlen-1);
6811 fieldname.len = fieldlen-2;
6812 } else {
6813 fieldlen = 0;
6814 }
6815
6816 prefixlen = p-spat;
6817 sublen = sdslen(ssub);
6818 postfixlen = sdslen(spat)-(prefixlen+1)-fieldlen;
6819 memcpy(keyname.buf,spat,prefixlen);
6820 memcpy(keyname.buf+prefixlen,ssub,sublen);
6821 memcpy(keyname.buf+prefixlen+sublen,p+1,postfixlen);
6822 keyname.buf[prefixlen+sublen+postfixlen] = '\0';
6823 keyname.len = prefixlen+sublen+postfixlen;
6824 decrRefCount(subst);
6825
6826 /* Lookup substituted key */
6827 initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2));
6828 o = lookupKeyRead(db,&keyobj);
6829 if (o == NULL) return NULL;
6830
6831 if (fieldlen > 0) {
6832 if (o->type != REDIS_HASH || fieldname.len < 1) return NULL;
6833
6834 /* Retrieve value from hash by the field name. This operation
6835 * already increases the refcount of the returned object. */
6836 initStaticStringObject(fieldobj,((char*)&fieldname)+(sizeof(long)*2));
6837 o = hashGet(o, &fieldobj);
6838 } else {
6839 if (o->type != REDIS_STRING) return NULL;
6840
6841 /* Every object that this function returns needs to have its refcount
6842 * increased. sortCommand decreases it again. */
6843 incrRefCount(o);
6844 }
6845
6846 return o;
6847 }
6848
6849 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
6850 * the additional parameter is not standard but a BSD-specific we have to
6851 * pass sorting parameters via the global 'server' structure */
6852 static int sortCompare(const void *s1, const void *s2) {
6853 const redisSortObject *so1 = s1, *so2 = s2;
6854 int cmp;
6855
6856 if (!server.sort_alpha) {
6857 /* Numeric sorting. Here it's trivial as we precomputed scores */
6858 if (so1->u.score > so2->u.score) {
6859 cmp = 1;
6860 } else if (so1->u.score < so2->u.score) {
6861 cmp = -1;
6862 } else {
6863 cmp = 0;
6864 }
6865 } else {
6866 /* Alphanumeric sorting */
6867 if (server.sort_bypattern) {
6868 if (!so1->u.cmpobj || !so2->u.cmpobj) {
6869 /* At least one compare object is NULL */
6870 if (so1->u.cmpobj == so2->u.cmpobj)
6871 cmp = 0;
6872 else if (so1->u.cmpobj == NULL)
6873 cmp = -1;
6874 else
6875 cmp = 1;
6876 } else {
6877 /* We have both the objects, use strcoll */
6878 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
6879 }
6880 } else {
6881 /* Compare elements directly. */
6882 cmp = compareStringObjects(so1->obj,so2->obj);
6883 }
6884 }
6885 return server.sort_desc ? -cmp : cmp;
6886 }
6887
6888 /* The SORT command is the most complex command in Redis. Warning: this code
6889 * is optimized for speed and a bit less for readability */
6890 static void sortCommand(redisClient *c) {
6891 list *operations;
6892 int outputlen = 0;
6893 int desc = 0, alpha = 0;
6894 int limit_start = 0, limit_count = -1, start, end;
6895 int j, dontsort = 0, vectorlen;
6896 int getop = 0; /* GET operation counter */
6897 robj *sortval, *sortby = NULL, *storekey = NULL;
6898 redisSortObject *vector; /* Resulting vector to sort */
6899
6900 /* Lookup the key to sort. It must be of the right types */
6901 sortval = lookupKeyRead(c->db,c->argv[1]);
6902 if (sortval == NULL) {
6903 addReply(c,shared.emptymultibulk);
6904 return;
6905 }
6906 if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
6907 sortval->type != REDIS_ZSET)
6908 {
6909 addReply(c,shared.wrongtypeerr);
6910 return;
6911 }
6912
6913 /* Create a list of operations to perform for every sorted element.
6914 * Operations can be GET/DEL/INCR/DECR */
6915 operations = listCreate();
6916 listSetFreeMethod(operations,zfree);
6917 j = 2;
6918
6919 /* Now we need to protect sortval incrementing its count, in the future
6920 * SORT may have options able to overwrite/delete keys during the sorting
6921 * and the sorted key itself may get destroied */
6922 incrRefCount(sortval);
6923
6924 /* The SORT command has an SQL-alike syntax, parse it */
6925 while(j < c->argc) {
6926 int leftargs = c->argc-j-1;
6927 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
6928 desc = 0;
6929 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
6930 desc = 1;
6931 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
6932 alpha = 1;
6933 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
6934 limit_start = atoi(c->argv[j+1]->ptr);
6935 limit_count = atoi(c->argv[j+2]->ptr);
6936 j+=2;
6937 } else if (!strcasecmp(c->argv[j]->ptr,"store") && leftargs >= 1) {
6938 storekey = c->argv[j+1];
6939 j++;
6940 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
6941 sortby = c->argv[j+1];
6942 /* If the BY pattern does not contain '*', i.e. it is constant,
6943 * we don't need to sort nor to lookup the weight keys. */
6944 if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
6945 j++;
6946 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
6947 listAddNodeTail(operations,createSortOperation(
6948 REDIS_SORT_GET,c->argv[j+1]));
6949 getop++;
6950 j++;
6951 } else {
6952 decrRefCount(sortval);
6953 listRelease(operations);
6954 addReply(c,shared.syntaxerr);
6955 return;
6956 }
6957 j++;
6958 }
6959
6960 /* Load the sorting vector with all the objects to sort */
6961 switch(sortval->type) {
6962 case REDIS_LIST: vectorlen = listLength((list*)sortval->ptr); break;
6963 case REDIS_SET: vectorlen = dictSize((dict*)sortval->ptr); break;
6964 case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
6965 default: vectorlen = 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
6966 }
6967 vector = zmalloc(sizeof(redisSortObject)*vectorlen);
6968 j = 0;
6969
6970 if (sortval->type == REDIS_LIST) {
6971 list *list = sortval->ptr;
6972 listNode *ln;
6973 listIter li;
6974
6975 listRewind(list,&li);
6976 while((ln = listNext(&li))) {
6977 robj *ele = ln->value;
6978 vector[j].obj = ele;
6979 vector[j].u.score = 0;
6980 vector[j].u.cmpobj = NULL;
6981 j++;
6982 }
6983 } else {
6984 dict *set;
6985 dictIterator *di;
6986 dictEntry *setele;
6987
6988 if (sortval->type == REDIS_SET) {
6989 set = sortval->ptr;
6990 } else {
6991 zset *zs = sortval->ptr;
6992 set = zs->dict;
6993 }
6994
6995 di = dictGetIterator(set);
6996 while((setele = dictNext(di)) != NULL) {
6997 vector[j].obj = dictGetEntryKey(setele);
6998 vector[j].u.score = 0;
6999 vector[j].u.cmpobj = NULL;
7000 j++;
7001 }
7002 dictReleaseIterator(di);
7003 }
7004 redisAssert(j == vectorlen);
7005
7006 /* Now it's time to load the right scores in the sorting vector */
7007 if (dontsort == 0) {
7008 for (j = 0; j < vectorlen; j++) {
7009 robj *byval;
7010 if (sortby) {
7011 /* lookup value to sort by */
7012 byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
7013 if (!byval) continue;
7014 } else {
7015 /* use object itself to sort by */
7016 byval = vector[j].obj;
7017 }
7018
7019 if (alpha) {
7020 if (sortby) vector[j].u.cmpobj = getDecodedObject(byval);
7021 } else {
7022 if (byval->encoding == REDIS_ENCODING_RAW) {
7023 vector[j].u.score = strtod(byval->ptr,NULL);
7024 } else if (byval->encoding == REDIS_ENCODING_INT) {
7025 /* Don't need to decode the object if it's
7026 * integer-encoded (the only encoding supported) so
7027 * far. We can just cast it */
7028 vector[j].u.score = (long)byval->ptr;
7029 } else {
7030 redisAssert(1 != 1);
7031 }
7032 }
7033
7034 /* when the object was retrieved using lookupKeyByPattern,
7035 * its refcount needs to be decreased. */
7036 if (sortby) {
7037 decrRefCount(byval);
7038 }
7039 }
7040 }
7041
7042 /* We are ready to sort the vector... perform a bit of sanity check
7043 * on the LIMIT option too. We'll use a partial version of quicksort. */
7044 start = (limit_start < 0) ? 0 : limit_start;
7045 end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
7046 if (start >= vectorlen) {
7047 start = vectorlen-1;
7048 end = vectorlen-2;
7049 }
7050 if (end >= vectorlen) end = vectorlen-1;
7051
7052 if (dontsort == 0) {
7053 server.sort_desc = desc;
7054 server.sort_alpha = alpha;
7055 server.sort_bypattern = sortby ? 1 : 0;
7056 if (sortby && (start != 0 || end != vectorlen-1))
7057 pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
7058 else
7059 qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
7060 }
7061
7062 /* Send command output to the output buffer, performing the specified
7063 * GET/DEL/INCR/DECR operations if any. */
7064 outputlen = getop ? getop*(end-start+1) : end-start+1;
7065 if (storekey == NULL) {
7066 /* STORE option not specified, sent the sorting result to client */
7067 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen));
7068 for (j = start; j <= end; j++) {
7069 listNode *ln;
7070 listIter li;
7071
7072 if (!getop) addReplyBulk(c,vector[j].obj);
7073 listRewind(operations,&li);
7074 while((ln = listNext(&li))) {
7075 redisSortOperation *sop = ln->value;
7076 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7077 vector[j].obj);
7078
7079 if (sop->type == REDIS_SORT_GET) {
7080 if (!val) {
7081 addReply(c,shared.nullbulk);
7082 } else {
7083 addReplyBulk(c,val);
7084 decrRefCount(val);
7085 }
7086 } else {
7087 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
7088 }
7089 }
7090 }
7091 } else {
7092 robj *listObject = createListObject();
7093 list *listPtr = (list*) listObject->ptr;
7094
7095 /* STORE option specified, set the sorting result as a List object */
7096 for (j = start; j <= end; j++) {
7097 listNode *ln;
7098 listIter li;
7099
7100 if (!getop) {
7101 listAddNodeTail(listPtr,vector[j].obj);
7102 incrRefCount(vector[j].obj);
7103 }
7104 listRewind(operations,&li);
7105 while((ln = listNext(&li))) {
7106 redisSortOperation *sop = ln->value;
7107 robj *val = lookupKeyByPattern(c->db,sop->pattern,
7108 vector[j].obj);
7109
7110 if (sop->type == REDIS_SORT_GET) {
7111 if (!val) {
7112 listAddNodeTail(listPtr,createStringObject("",0));
7113 } else {
7114 /* We should do a incrRefCount on val because it is
7115 * added to the list, but also a decrRefCount because
7116 * it is returned by lookupKeyByPattern. This results
7117 * in doing nothing at all. */
7118 listAddNodeTail(listPtr,val);
7119 }
7120 } else {
7121 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
7122 }
7123 }
7124 }
7125 if (dictReplace(c->db->dict,storekey,listObject)) {
7126 incrRefCount(storekey);
7127 }
7128 /* Note: we add 1 because the DB is dirty anyway since even if the
7129 * SORT result is empty a new key is set and maybe the old content
7130 * replaced. */
7131 server.dirty += 1+outputlen;
7132 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",outputlen));
7133 }
7134
7135 /* Cleanup */
7136 decrRefCount(sortval);
7137 listRelease(operations);
7138 for (j = 0; j < vectorlen; j++) {
7139 if (alpha && vector[j].u.cmpobj)
7140 decrRefCount(vector[j].u.cmpobj);
7141 }
7142 zfree(vector);
7143 }
7144
7145 /* Convert an amount of bytes into a human readable string in the form
7146 * of 100B, 2G, 100M, 4K, and so forth. */
7147 static void bytesToHuman(char *s, unsigned long long n) {
7148 double d;
7149
7150 if (n < 1024) {
7151 /* Bytes */
7152 sprintf(s,"%lluB",n);
7153 return;
7154 } else if (n < (1024*1024)) {
7155 d = (double)n/(1024);
7156 sprintf(s,"%.2fK",d);
7157 } else if (n < (1024LL*1024*1024)) {
7158 d = (double)n/(1024*1024);
7159 sprintf(s,"%.2fM",d);
7160 } else if (n < (1024LL*1024*1024*1024)) {
7161 d = (double)n/(1024LL*1024*1024);
7162 sprintf(s,"%.2fG",d);
7163 }
7164 }
7165
7166 /* Create the string returned by the INFO command. This is decoupled
7167 * by the INFO command itself as we need to report the same information
7168 * on memory corruption problems. */
7169 static sds genRedisInfoString(void) {
7170 sds info;
7171 time_t uptime = time(NULL)-server.stat_starttime;
7172 int j;
7173 char hmem[64];
7174
7175 bytesToHuman(hmem,zmalloc_used_memory());
7176 info = sdscatprintf(sdsempty(),
7177 "redis_version:%s\r\n"
7178 "arch_bits:%s\r\n"
7179 "multiplexing_api:%s\r\n"
7180 "process_id:%ld\r\n"
7181 "uptime_in_seconds:%ld\r\n"
7182 "uptime_in_days:%ld\r\n"
7183 "connected_clients:%d\r\n"
7184 "connected_slaves:%d\r\n"
7185 "blocked_clients:%d\r\n"
7186 "used_memory:%zu\r\n"
7187 "used_memory_human:%s\r\n"
7188 "changes_since_last_save:%lld\r\n"
7189 "bgsave_in_progress:%d\r\n"
7190 "last_save_time:%ld\r\n"
7191 "bgrewriteaof_in_progress:%d\r\n"
7192 "total_connections_received:%lld\r\n"
7193 "total_commands_processed:%lld\r\n"
7194 "expired_keys:%lld\r\n"
7195 "hash_max_zipmap_entries:%zu\r\n"
7196 "hash_max_zipmap_value:%zu\r\n"
7197 "pubsub_channels:%ld\r\n"
7198 "pubsub_patterns:%u\r\n"
7199 "vm_enabled:%d\r\n"
7200 "role:%s\r\n"
7201 ,REDIS_VERSION,
7202 (sizeof(long) == 8) ? "64" : "32",
7203 aeGetApiName(),
7204 (long) getpid(),
7205 uptime,
7206 uptime/(3600*24),
7207 listLength(server.clients)-listLength(server.slaves),
7208 listLength(server.slaves),
7209 server.blpop_blocked_clients,
7210 zmalloc_used_memory(),
7211 hmem,
7212 server.dirty,
7213 server.bgsavechildpid != -1,
7214 server.lastsave,
7215 server.bgrewritechildpid != -1,
7216 server.stat_numconnections,
7217 server.stat_numcommands,
7218 server.stat_expiredkeys,
7219 server.hash_max_zipmap_entries,
7220 server.hash_max_zipmap_value,
7221 dictSize(server.pubsub_channels),
7222 listLength(server.pubsub_patterns),
7223 server.vm_enabled != 0,
7224 server.masterhost == NULL ? "master" : "slave"
7225 );
7226 if (server.masterhost) {
7227 info = sdscatprintf(info,
7228 "master_host:%s\r\n"
7229 "master_port:%d\r\n"
7230 "master_link_status:%s\r\n"
7231 "master_last_io_seconds_ago:%d\r\n"
7232 ,server.masterhost,
7233 server.masterport,
7234 (server.replstate == REDIS_REPL_CONNECTED) ?
7235 "up" : "down",
7236 server.master ? ((int)(time(NULL)-server.master->lastinteraction)) : -1
7237 );
7238 }
7239 if (server.vm_enabled) {
7240 lockThreadedIO();
7241 info = sdscatprintf(info,
7242 "vm_conf_max_memory:%llu\r\n"
7243 "vm_conf_page_size:%llu\r\n"
7244 "vm_conf_pages:%llu\r\n"
7245 "vm_stats_used_pages:%llu\r\n"
7246 "vm_stats_swapped_objects:%llu\r\n"
7247 "vm_stats_swappin_count:%llu\r\n"
7248 "vm_stats_swappout_count:%llu\r\n"
7249 "vm_stats_io_newjobs_len:%lu\r\n"
7250 "vm_stats_io_processing_len:%lu\r\n"
7251 "vm_stats_io_processed_len:%lu\r\n"
7252 "vm_stats_io_active_threads:%lu\r\n"
7253 "vm_stats_blocked_clients:%lu\r\n"
7254 ,(unsigned long long) server.vm_max_memory,
7255 (unsigned long long) server.vm_page_size,
7256 (unsigned long long) server.vm_pages,
7257 (unsigned long long) server.vm_stats_used_pages,
7258 (unsigned long long) server.vm_stats_swapped_objects,
7259 (unsigned long long) server.vm_stats_swapins,
7260 (unsigned long long) server.vm_stats_swapouts,
7261 (unsigned long) listLength(server.io_newjobs),
7262 (unsigned long) listLength(server.io_processing),
7263 (unsigned long) listLength(server.io_processed),
7264 (unsigned long) server.io_active_threads,
7265 (unsigned long) server.vm_blocked_clients
7266 );
7267 unlockThreadedIO();
7268 }
7269 for (j = 0; j < server.dbnum; j++) {
7270 long long keys, vkeys;
7271
7272 keys = dictSize(server.db[j].dict);
7273 vkeys = dictSize(server.db[j].expires);
7274 if (keys || vkeys) {
7275 info = sdscatprintf(info, "db%d:keys=%lld,expires=%lld\r\n",
7276 j, keys, vkeys);
7277 }
7278 }
7279 return info;
7280 }
7281
7282 static void infoCommand(redisClient *c) {
7283 sds info = genRedisInfoString();
7284 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
7285 (unsigned long)sdslen(info)));
7286 addReplySds(c,info);
7287 addReply(c,shared.crlf);
7288 }
7289
7290 static void monitorCommand(redisClient *c) {
7291 /* ignore MONITOR if aleady slave or in monitor mode */
7292 if (c->flags & REDIS_SLAVE) return;
7293
7294 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
7295 c->slaveseldb = 0;
7296 listAddNodeTail(server.monitors,c);
7297 addReply(c,shared.ok);
7298 }
7299
7300 /* ================================= Expire ================================= */
7301 static int removeExpire(redisDb *db, robj *key) {
7302 if (dictDelete(db->expires,key) == DICT_OK) {
7303 return 1;
7304 } else {
7305 return 0;
7306 }
7307 }
7308
7309 static int setExpire(redisDb *db, robj *key, time_t when) {
7310 if (dictAdd(db->expires,key,(void*)when) == DICT_ERR) {
7311 return 0;
7312 } else {
7313 incrRefCount(key);
7314 return 1;
7315 }
7316 }
7317
7318 /* Return the expire time of the specified key, or -1 if no expire
7319 * is associated with this key (i.e. the key is non volatile) */
7320 static time_t getExpire(redisDb *db, robj *key) {
7321 dictEntry *de;
7322
7323 /* No expire? return ASAP */
7324 if (dictSize(db->expires) == 0 ||
7325 (de = dictFind(db->expires,key)) == NULL) return -1;
7326
7327 return (time_t) dictGetEntryVal(de);
7328 }
7329
7330 static int expireIfNeeded(redisDb *db, robj *key) {
7331 time_t when;
7332 dictEntry *de;
7333
7334 /* No expire? return ASAP */
7335 if (dictSize(db->expires) == 0 ||
7336 (de = dictFind(db->expires,key)) == NULL) return 0;
7337
7338 /* Lookup the expire */
7339 when = (time_t) dictGetEntryVal(de);
7340 if (time(NULL) <= when) return 0;
7341
7342 /* Delete the key */
7343 dictDelete(db->expires,key);
7344 server.stat_expiredkeys++;
7345 return dictDelete(db->dict,key) == DICT_OK;
7346 }
7347
7348 static int deleteIfVolatile(redisDb *db, robj *key) {
7349 dictEntry *de;
7350
7351 /* No expire? return ASAP */
7352 if (dictSize(db->expires) == 0 ||
7353 (de = dictFind(db->expires,key)) == NULL) return 0;
7354
7355 /* Delete the key */
7356 server.dirty++;
7357 server.stat_expiredkeys++;
7358 dictDelete(db->expires,key);
7359 return dictDelete(db->dict,key) == DICT_OK;
7360 }
7361
7362 static void expireGenericCommand(redisClient *c, robj *key, robj *param, long offset) {
7363 dictEntry *de;
7364 time_t seconds;
7365
7366 if (getLongFromObjectOrReply(c, param, &seconds, NULL) != REDIS_OK) return;
7367
7368 seconds -= offset;
7369
7370 de = dictFind(c->db->dict,key);
7371 if (de == NULL) {
7372 addReply(c,shared.czero);
7373 return;
7374 }
7375 if (seconds <= 0) {
7376 if (deleteKey(c->db,key)) server.dirty++;
7377 addReply(c, shared.cone);
7378 return;
7379 } else {
7380 time_t when = time(NULL)+seconds;
7381 if (setExpire(c->db,key,when)) {
7382 addReply(c,shared.cone);
7383 server.dirty++;
7384 } else {
7385 addReply(c,shared.czero);
7386 }
7387 return;
7388 }
7389 }
7390
7391 static void expireCommand(redisClient *c) {
7392 expireGenericCommand(c,c->argv[1],c->argv[2],0);
7393 }
7394
7395 static void expireatCommand(redisClient *c) {
7396 expireGenericCommand(c,c->argv[1],c->argv[2],time(NULL));
7397 }
7398
7399 static void ttlCommand(redisClient *c) {
7400 time_t expire;
7401 int ttl = -1;
7402
7403 expire = getExpire(c->db,c->argv[1]);
7404 if (expire != -1) {
7405 ttl = (int) (expire-time(NULL));
7406 if (ttl < 0) ttl = -1;
7407 }
7408 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",ttl));
7409 }
7410
7411 /* ================================ MULTI/EXEC ============================== */
7412
7413 /* Client state initialization for MULTI/EXEC */
7414 static void initClientMultiState(redisClient *c) {
7415 c->mstate.commands = NULL;
7416 c->mstate.count = 0;
7417 }
7418
7419 /* Release all the resources associated with MULTI/EXEC state */
7420 static void freeClientMultiState(redisClient *c) {
7421 int j;
7422
7423 for (j = 0; j < c->mstate.count; j++) {
7424 int i;
7425 multiCmd *mc = c->mstate.commands+j;
7426
7427 for (i = 0; i < mc->argc; i++)
7428 decrRefCount(mc->argv[i]);
7429 zfree(mc->argv);
7430 }
7431 zfree(c->mstate.commands);
7432 }
7433
7434 /* Add a new command into the MULTI commands queue */
7435 static void queueMultiCommand(redisClient *c, struct redisCommand *cmd) {
7436 multiCmd *mc;
7437 int j;
7438
7439 c->mstate.commands = zrealloc(c->mstate.commands,
7440 sizeof(multiCmd)*(c->mstate.count+1));
7441 mc = c->mstate.commands+c->mstate.count;
7442 mc->cmd = cmd;
7443 mc->argc = c->argc;
7444 mc->argv = zmalloc(sizeof(robj*)*c->argc);
7445 memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
7446 for (j = 0; j < c->argc; j++)
7447 incrRefCount(mc->argv[j]);
7448 c->mstate.count++;
7449 }
7450
7451 static void multiCommand(redisClient *c) {
7452 c->flags |= REDIS_MULTI;
7453 addReply(c,shared.ok);
7454 }
7455
7456 static void discardCommand(redisClient *c) {
7457 if (!(c->flags & REDIS_MULTI)) {
7458 addReplySds(c,sdsnew("-ERR DISCARD without MULTI\r\n"));
7459 return;
7460 }
7461
7462 freeClientMultiState(c);
7463 initClientMultiState(c);
7464 c->flags &= (~REDIS_MULTI);
7465 addReply(c,shared.ok);
7466 }
7467
7468 /* Send a MULTI command to all the slaves and AOF file. Check the execCommand
7469 * implememntation for more information. */
7470 static void execCommandReplicateMulti(redisClient *c) {
7471 struct redisCommand *cmd;
7472 robj *multistring = createStringObject("MULTI",5);
7473
7474 cmd = lookupCommand("multi");
7475 if (server.appendonly)
7476 feedAppendOnlyFile(cmd,c->db->id,&multistring,1);
7477 if (listLength(server.slaves))
7478 replicationFeedSlaves(server.slaves,c->db->id,&multistring,1);
7479 decrRefCount(multistring);
7480 }
7481
7482 static void execCommand(redisClient *c) {
7483 int j;
7484 robj **orig_argv;
7485 int orig_argc;
7486
7487 if (!(c->flags & REDIS_MULTI)) {
7488 addReplySds(c,sdsnew("-ERR EXEC without MULTI\r\n"));
7489 return;
7490 }
7491
7492 /* Replicate a MULTI request now that we are sure the block is executed.
7493 * This way we'll deliver the MULTI/..../EXEC block as a whole and
7494 * both the AOF and the replication link will have the same consistency
7495 * and atomicity guarantees. */
7496 execCommandReplicateMulti(c);
7497
7498 /* Exec all the queued commands */
7499 orig_argv = c->argv;
7500 orig_argc = c->argc;
7501 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->mstate.count));
7502 for (j = 0; j < c->mstate.count; j++) {
7503 c->argc = c->mstate.commands[j].argc;
7504 c->argv = c->mstate.commands[j].argv;
7505 call(c,c->mstate.commands[j].cmd);
7506 }
7507 c->argv = orig_argv;
7508 c->argc = orig_argc;
7509 freeClientMultiState(c);
7510 initClientMultiState(c);
7511 c->flags &= (~REDIS_MULTI);
7512 /* Make sure the EXEC command is always replicated / AOF, since we
7513 * always send the MULTI command (we can't know beforehand if the
7514 * next operations will contain at least a modification to the DB). */
7515 server.dirty++;
7516 }
7517
7518 /* =========================== Blocking Operations ========================= */
7519
7520 /* Currently Redis blocking operations support is limited to list POP ops,
7521 * so the current implementation is not fully generic, but it is also not
7522 * completely specific so it will not require a rewrite to support new
7523 * kind of blocking operations in the future.
7524 *
7525 * Still it's important to note that list blocking operations can be already
7526 * used as a notification mechanism in order to implement other blocking
7527 * operations at application level, so there must be a very strong evidence
7528 * of usefulness and generality before new blocking operations are implemented.
7529 *
7530 * This is how the current blocking POP works, we use BLPOP as example:
7531 * - If the user calls BLPOP and the key exists and contains a non empty list
7532 * then LPOP is called instead. So BLPOP is semantically the same as LPOP
7533 * if there is not to block.
7534 * - If instead BLPOP is called and the key does not exists or the list is
7535 * empty we need to block. In order to do so we remove the notification for
7536 * new data to read in the client socket (so that we'll not serve new
7537 * requests if the blocking request is not served). Also we put the client
7538 * in a dictionary (db->blockingkeys) mapping keys to a list of clients
7539 * blocking for this keys.
7540 * - If a PUSH operation against a key with blocked clients waiting is
7541 * performed, we serve the first in the list: basically instead to push
7542 * the new element inside the list we return it to the (first / oldest)
7543 * blocking client, unblock the client, and remove it form the list.
7544 *
7545 * The above comment and the source code should be enough in order to understand
7546 * the implementation and modify / fix it later.
7547 */
7548
7549 /* Set a client in blocking mode for the specified key, with the specified
7550 * timeout */
7551 static void blockForKeys(redisClient *c, robj **keys, int numkeys, time_t timeout) {
7552 dictEntry *de;
7553 list *l;
7554 int j;
7555
7556 c->blockingkeys = zmalloc(sizeof(robj*)*numkeys);
7557 c->blockingkeysnum = numkeys;
7558 c->blockingto = timeout;
7559 for (j = 0; j < numkeys; j++) {
7560 /* Add the key in the client structure, to map clients -> keys */
7561 c->blockingkeys[j] = keys[j];
7562 incrRefCount(keys[j]);
7563
7564 /* And in the other "side", to map keys -> clients */
7565 de = dictFind(c->db->blockingkeys,keys[j]);
7566 if (de == NULL) {
7567 int retval;
7568
7569 /* For every key we take a list of clients blocked for it */
7570 l = listCreate();
7571 retval = dictAdd(c->db->blockingkeys,keys[j],l);
7572 incrRefCount(keys[j]);
7573 assert(retval == DICT_OK);
7574 } else {
7575 l = dictGetEntryVal(de);
7576 }
7577 listAddNodeTail(l,c);
7578 }
7579 /* Mark the client as a blocked client */
7580 c->flags |= REDIS_BLOCKED;
7581 server.blpop_blocked_clients++;
7582 }
7583
7584 /* Unblock a client that's waiting in a blocking operation such as BLPOP */
7585 static void unblockClientWaitingData(redisClient *c) {
7586 dictEntry *de;
7587 list *l;
7588 int j;
7589
7590 assert(c->blockingkeys != NULL);
7591 /* The client may wait for multiple keys, so unblock it for every key. */
7592 for (j = 0; j < c->blockingkeysnum; j++) {
7593 /* Remove this client from the list of clients waiting for this key. */
7594 de = dictFind(c->db->blockingkeys,c->blockingkeys[j]);
7595 assert(de != NULL);
7596 l = dictGetEntryVal(de);
7597 listDelNode(l,listSearchKey(l,c));
7598 /* If the list is empty we need to remove it to avoid wasting memory */
7599 if (listLength(l) == 0)
7600 dictDelete(c->db->blockingkeys,c->blockingkeys[j]);
7601 decrRefCount(c->blockingkeys[j]);
7602 }
7603 /* Cleanup the client structure */
7604 zfree(c->blockingkeys);
7605 c->blockingkeys = NULL;
7606 c->flags &= (~REDIS_BLOCKED);
7607 server.blpop_blocked_clients--;
7608 /* We want to process data if there is some command waiting
7609 * in the input buffer. Note that this is safe even if
7610 * unblockClientWaitingData() gets called from freeClient() because
7611 * freeClient() will be smart enough to call this function
7612 * *after* c->querybuf was set to NULL. */
7613 if (c->querybuf && sdslen(c->querybuf) > 0) processInputBuffer(c);
7614 }
7615
7616 /* This should be called from any function PUSHing into lists.
7617 * 'c' is the "pushing client", 'key' is the key it is pushing data against,
7618 * 'ele' is the element pushed.
7619 *
7620 * If the function returns 0 there was no client waiting for a list push
7621 * against this key.
7622 *
7623 * If the function returns 1 there was a client waiting for a list push
7624 * against this key, the element was passed to this client thus it's not
7625 * needed to actually add it to the list and the caller should return asap. */
7626 static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele) {
7627 struct dictEntry *de;
7628 redisClient *receiver;
7629 list *l;
7630 listNode *ln;
7631
7632 de = dictFind(c->db->blockingkeys,key);
7633 if (de == NULL) return 0;
7634 l = dictGetEntryVal(de);
7635 ln = listFirst(l);
7636 assert(ln != NULL);
7637 receiver = ln->value;
7638
7639 addReplySds(receiver,sdsnew("*2\r\n"));
7640 addReplyBulk(receiver,key);
7641 addReplyBulk(receiver,ele);
7642 unblockClientWaitingData(receiver);
7643 return 1;
7644 }
7645
7646 /* Blocking RPOP/LPOP */
7647 static void blockingPopGenericCommand(redisClient *c, int where) {
7648 robj *o;
7649 time_t timeout;
7650 int j;
7651
7652 for (j = 1; j < c->argc-1; j++) {
7653 o = lookupKeyWrite(c->db,c->argv[j]);
7654 if (o != NULL) {
7655 if (o->type != REDIS_LIST) {
7656 addReply(c,shared.wrongtypeerr);
7657 return;
7658 } else {
7659 list *list = o->ptr;
7660 if (listLength(list) != 0) {
7661 /* If the list contains elements fall back to the usual
7662 * non-blocking POP operation */
7663 robj *argv[2], **orig_argv;
7664 int orig_argc;
7665
7666 /* We need to alter the command arguments before to call
7667 * popGenericCommand() as the command takes a single key. */
7668 orig_argv = c->argv;
7669 orig_argc = c->argc;
7670 argv[1] = c->argv[j];
7671 c->argv = argv;
7672 c->argc = 2;
7673
7674 /* Also the return value is different, we need to output
7675 * the multi bulk reply header and the key name. The
7676 * "real" command will add the last element (the value)
7677 * for us. If this souds like an hack to you it's just
7678 * because it is... */
7679 addReplySds(c,sdsnew("*2\r\n"));
7680 addReplyBulk(c,argv[1]);
7681 popGenericCommand(c,where);
7682
7683 /* Fix the client structure with the original stuff */
7684 c->argv = orig_argv;
7685 c->argc = orig_argc;
7686 return;
7687 }
7688 }
7689 }
7690 }
7691 /* If the list is empty or the key does not exists we must block */
7692 timeout = strtol(c->argv[c->argc-1]->ptr,NULL,10);
7693 if (timeout > 0) timeout += time(NULL);
7694 blockForKeys(c,c->argv+1,c->argc-2,timeout);
7695 }
7696
7697 static void blpopCommand(redisClient *c) {
7698 blockingPopGenericCommand(c,REDIS_HEAD);
7699 }
7700
7701 static void brpopCommand(redisClient *c) {
7702 blockingPopGenericCommand(c,REDIS_TAIL);
7703 }
7704
7705 /* =============================== Replication ============================= */
7706
7707 static int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
7708 ssize_t nwritten, ret = size;
7709 time_t start = time(NULL);
7710
7711 timeout++;
7712 while(size) {
7713 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
7714 nwritten = write(fd,ptr,size);
7715 if (nwritten == -1) return -1;
7716 ptr += nwritten;
7717 size -= nwritten;
7718 }
7719 if ((time(NULL)-start) > timeout) {
7720 errno = ETIMEDOUT;
7721 return -1;
7722 }
7723 }
7724 return ret;
7725 }
7726
7727 static int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
7728 ssize_t nread, totread = 0;
7729 time_t start = time(NULL);
7730
7731 timeout++;
7732 while(size) {
7733 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
7734 nread = read(fd,ptr,size);
7735 if (nread == -1) return -1;
7736 ptr += nread;
7737 size -= nread;
7738 totread += nread;
7739 }
7740 if ((time(NULL)-start) > timeout) {
7741 errno = ETIMEDOUT;
7742 return -1;
7743 }
7744 }
7745 return totread;
7746 }
7747
7748 static int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
7749 ssize_t nread = 0;
7750
7751 size--;
7752 while(size) {
7753 char c;
7754
7755 if (syncRead(fd,&c,1,timeout) == -1) return -1;
7756 if (c == '\n') {
7757 *ptr = '\0';
7758 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
7759 return nread;
7760 } else {
7761 *ptr++ = c;
7762 *ptr = '\0';
7763 nread++;
7764 }
7765 }
7766 return nread;
7767 }
7768
7769 static void syncCommand(redisClient *c) {
7770 /* ignore SYNC if aleady slave or in monitor mode */
7771 if (c->flags & REDIS_SLAVE) return;
7772
7773 /* SYNC can't be issued when the server has pending data to send to
7774 * the client about already issued commands. We need a fresh reply
7775 * buffer registering the differences between the BGSAVE and the current
7776 * dataset, so that we can copy to other slaves if needed. */
7777 if (listLength(c->reply) != 0) {
7778 addReplySds(c,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
7779 return;
7780 }
7781
7782 redisLog(REDIS_NOTICE,"Slave ask for synchronization");
7783 /* Here we need to check if there is a background saving operation
7784 * in progress, or if it is required to start one */
7785 if (server.bgsavechildpid != -1) {
7786 /* Ok a background save is in progress. Let's check if it is a good
7787 * one for replication, i.e. if there is another slave that is
7788 * registering differences since the server forked to save */
7789 redisClient *slave;
7790 listNode *ln;
7791 listIter li;
7792
7793 listRewind(server.slaves,&li);
7794 while((ln = listNext(&li))) {
7795 slave = ln->value;
7796 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;
7797 }
7798 if (ln) {
7799 /* Perfect, the server is already registering differences for
7800 * another slave. Set the right state, and copy the buffer. */
7801 listRelease(c->reply);
7802 c->reply = listDup(slave->reply);
7803 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7804 redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
7805 } else {
7806 /* No way, we need to wait for the next BGSAVE in order to
7807 * register differences */
7808 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
7809 redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");
7810 }
7811 } else {
7812 /* Ok we don't have a BGSAVE in progress, let's start one */
7813 redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC");
7814 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
7815 redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");
7816 addReplySds(c,sdsnew("-ERR Unalbe to perform background save\r\n"));
7817 return;
7818 }
7819 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7820 }
7821 c->repldbfd = -1;
7822 c->flags |= REDIS_SLAVE;
7823 c->slaveseldb = 0;
7824 listAddNodeTail(server.slaves,c);
7825 return;
7826 }
7827
7828 static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
7829 redisClient *slave = privdata;
7830 REDIS_NOTUSED(el);
7831 REDIS_NOTUSED(mask);
7832 char buf[REDIS_IOBUF_LEN];
7833 ssize_t nwritten, buflen;
7834
7835 if (slave->repldboff == 0) {
7836 /* Write the bulk write count before to transfer the DB. In theory here
7837 * we don't know how much room there is in the output buffer of the
7838 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
7839 * operations) will never be smaller than the few bytes we need. */
7840 sds bulkcount;
7841
7842 bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
7843 slave->repldbsize);
7844 if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount))
7845 {
7846 sdsfree(bulkcount);
7847 freeClient(slave);
7848 return;
7849 }
7850 sdsfree(bulkcount);
7851 }
7852 lseek(slave->repldbfd,slave->repldboff,SEEK_SET);
7853 buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);
7854 if (buflen <= 0) {
7855 redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",
7856 (buflen == 0) ? "premature EOF" : strerror(errno));
7857 freeClient(slave);
7858 return;
7859 }
7860 if ((nwritten = write(fd,buf,buflen)) == -1) {
7861 redisLog(REDIS_VERBOSE,"Write error sending DB to slave: %s",
7862 strerror(errno));
7863 freeClient(slave);
7864 return;
7865 }
7866 slave->repldboff += nwritten;
7867 if (slave->repldboff == slave->repldbsize) {
7868 close(slave->repldbfd);
7869 slave->repldbfd = -1;
7870 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
7871 slave->replstate = REDIS_REPL_ONLINE;
7872 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
7873 sendReplyToClient, slave) == AE_ERR) {
7874 freeClient(slave);
7875 return;
7876 }
7877 addReplySds(slave,sdsempty());
7878 redisLog(REDIS_NOTICE,"Synchronization with slave succeeded");
7879 }
7880 }
7881
7882 /* This function is called at the end of every backgrond saving.
7883 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
7884 * otherwise REDIS_ERR is passed to the function.
7885 *
7886 * The goal of this function is to handle slaves waiting for a successful
7887 * background saving in order to perform non-blocking synchronization. */
7888 static void updateSlavesWaitingBgsave(int bgsaveerr) {
7889 listNode *ln;
7890 int startbgsave = 0;
7891 listIter li;
7892
7893 listRewind(server.slaves,&li);
7894 while((ln = listNext(&li))) {
7895 redisClient *slave = ln->value;
7896
7897 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
7898 startbgsave = 1;
7899 slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
7900 } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
7901 struct redis_stat buf;
7902
7903 if (bgsaveerr != REDIS_OK) {
7904 freeClient(slave);
7905 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
7906 continue;
7907 }
7908 if ((slave->repldbfd = open(server.dbfilename,O_RDONLY)) == -1 ||
7909 redis_fstat(slave->repldbfd,&buf) == -1) {
7910 freeClient(slave);
7911 redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
7912 continue;
7913 }
7914 slave->repldboff = 0;
7915 slave->repldbsize = buf.st_size;
7916 slave->replstate = REDIS_REPL_SEND_BULK;
7917 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
7918 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {
7919 freeClient(slave);
7920 continue;
7921 }
7922 }
7923 }
7924 if (startbgsave) {
7925 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
7926 listIter li;
7927
7928 listRewind(server.slaves,&li);
7929 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
7930 while((ln = listNext(&li))) {
7931 redisClient *slave = ln->value;
7932
7933 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
7934 freeClient(slave);
7935 }
7936 }
7937 }
7938 }
7939
7940 static int syncWithMaster(void) {
7941 char buf[1024], tmpfile[256], authcmd[1024];
7942 long dumpsize;
7943 int fd = anetTcpConnect(NULL,server.masterhost,server.masterport);
7944 int dfd, maxtries = 5;
7945
7946 if (fd == -1) {
7947 redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",
7948 strerror(errno));
7949 return REDIS_ERR;
7950 }
7951
7952 /* AUTH with the master if required. */
7953 if(server.masterauth) {
7954 snprintf(authcmd, 1024, "AUTH %s\r\n", server.masterauth);
7955 if (syncWrite(fd, authcmd, strlen(server.masterauth)+7, 5) == -1) {
7956 close(fd);
7957 redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",
7958 strerror(errno));
7959 return REDIS_ERR;
7960 }
7961 /* Read the AUTH result. */
7962 if (syncReadLine(fd,buf,1024,3600) == -1) {
7963 close(fd);
7964 redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s",
7965 strerror(errno));
7966 return REDIS_ERR;
7967 }
7968 if (buf[0] != '+') {
7969 close(fd);
7970 redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?");
7971 return REDIS_ERR;
7972 }
7973 }
7974
7975 /* Issue the SYNC command */
7976 if (syncWrite(fd,"SYNC \r\n",7,5) == -1) {
7977 close(fd);
7978 redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",
7979 strerror(errno));
7980 return REDIS_ERR;
7981 }
7982 /* Read the bulk write count */
7983 if (syncReadLine(fd,buf,1024,3600) == -1) {
7984 close(fd);
7985 redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s",
7986 strerror(errno));
7987 return REDIS_ERR;
7988 }
7989 if (buf[0] != '$') {
7990 close(fd);
7991 redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
7992 return REDIS_ERR;
7993 }
7994 dumpsize = strtol(buf+1,NULL,10);
7995 redisLog(REDIS_NOTICE,"Receiving %ld bytes data dump from MASTER",dumpsize);
7996 /* Read the bulk write data on a temp file */
7997 while(maxtries--) {
7998 snprintf(tmpfile,256,
7999 "temp-%d.%ld.rdb",(int)time(NULL),(long int)getpid());
8000 dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);
8001 if (dfd != -1) break;
8002 sleep(1);
8003 }
8004 if (dfd == -1) {
8005 close(fd);
8006 redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
8007 return REDIS_ERR;
8008 }
8009 while(dumpsize) {
8010 int nread, nwritten;
8011
8012 nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024);
8013 if (nread == -1) {
8014 redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
8015 strerror(errno));
8016 close(fd);
8017 close(dfd);
8018 return REDIS_ERR;
8019 }
8020 nwritten = write(dfd,buf,nread);
8021 if (nwritten == -1) {
8022 redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
8023 close(fd);
8024 close(dfd);
8025 return REDIS_ERR;
8026 }
8027 dumpsize -= nread;
8028 }
8029 close(dfd);
8030 if (rename(tmpfile,server.dbfilename) == -1) {
8031 redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
8032 unlink(tmpfile);
8033 close(fd);
8034 return REDIS_ERR;
8035 }
8036 emptyDb();
8037 if (rdbLoad(server.dbfilename) != REDIS_OK) {
8038 redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
8039 close(fd);
8040 return REDIS_ERR;
8041 }
8042 server.master = createClient(fd);
8043 server.master->flags |= REDIS_MASTER;
8044 server.master->authenticated = 1;
8045 server.replstate = REDIS_REPL_CONNECTED;
8046 return REDIS_OK;
8047 }
8048
8049 static void slaveofCommand(redisClient *c) {
8050 if (!strcasecmp(c->argv[1]->ptr,"no") &&
8051 !strcasecmp(c->argv[2]->ptr,"one")) {
8052 if (server.masterhost) {
8053 sdsfree(server.masterhost);
8054 server.masterhost = NULL;
8055 if (server.master) freeClient(server.master);
8056 server.replstate = REDIS_REPL_NONE;
8057 redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
8058 }
8059 } else {
8060 sdsfree(server.masterhost);
8061 server.masterhost = sdsdup(c->argv[1]->ptr);
8062 server.masterport = atoi(c->argv[2]->ptr);
8063 if (server.master) freeClient(server.master);
8064 server.replstate = REDIS_REPL_CONNECT;
8065 redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
8066 server.masterhost, server.masterport);
8067 }
8068 addReply(c,shared.ok);
8069 }
8070
8071 /* ============================ Maxmemory directive ======================== */
8072
8073 /* Try to free one object form the pre-allocated objects free list.
8074 * This is useful under low mem conditions as by default we take 1 million
8075 * free objects allocated. On success REDIS_OK is returned, otherwise
8076 * REDIS_ERR. */
8077 static int tryFreeOneObjectFromFreelist(void) {
8078 robj *o;
8079
8080 if (server.vm_enabled) pthread_mutex_lock(&server.obj_freelist_mutex);
8081 if (listLength(server.objfreelist)) {
8082 listNode *head = listFirst(server.objfreelist);
8083 o = listNodeValue(head);
8084 listDelNode(server.objfreelist,head);
8085 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8086 zfree(o);
8087 return REDIS_OK;
8088 } else {
8089 if (server.vm_enabled) pthread_mutex_unlock(&server.obj_freelist_mutex);
8090 return REDIS_ERR;
8091 }
8092 }
8093
8094 /* This function gets called when 'maxmemory' is set on the config file to limit
8095 * the max memory used by the server, and we are out of memory.
8096 * This function will try to, in order:
8097 *
8098 * - Free objects from the free list
8099 * - Try to remove keys with an EXPIRE set
8100 *
8101 * It is not possible to free enough memory to reach used-memory < maxmemory
8102 * the server will start refusing commands that will enlarge even more the
8103 * memory usage.
8104 */
8105 static void freeMemoryIfNeeded(void) {
8106 while (server.maxmemory && zmalloc_used_memory() > server.maxmemory) {
8107 int j, k, freed = 0;
8108
8109 if (tryFreeOneObjectFromFreelist() == REDIS_OK) continue;
8110 for (j = 0; j < server.dbnum; j++) {
8111 int minttl = -1;
8112 robj *minkey = NULL;
8113 struct dictEntry *de;
8114
8115 if (dictSize(server.db[j].expires)) {
8116 freed = 1;
8117 /* From a sample of three keys drop the one nearest to
8118 * the natural expire */
8119 for (k = 0; k < 3; k++) {
8120 time_t t;
8121
8122 de = dictGetRandomKey(server.db[j].expires);
8123 t = (time_t) dictGetEntryVal(de);
8124 if (minttl == -1 || t < minttl) {
8125 minkey = dictGetEntryKey(de);
8126 minttl = t;
8127 }
8128 }
8129 deleteKey(server.db+j,minkey);
8130 }
8131 }
8132 if (!freed) return; /* nothing to free... */
8133 }
8134 }
8135
8136 /* ============================== Append Only file ========================== */
8137
8138 /* Write the append only file buffer on disk.
8139 *
8140 * Since we are required to write the AOF before replying to the client,
8141 * and the only way the client socket can get a write is entering when the
8142 * the event loop, we accumulate all the AOF writes in a memory
8143 * buffer and write it on disk using this function just before entering
8144 * the event loop again. */
8145 static void flushAppendOnlyFile(void) {
8146 time_t now;
8147 ssize_t nwritten;
8148
8149 if (sdslen(server.aofbuf) == 0) return;
8150
8151 /* We want to perform a single write. This should be guaranteed atomic
8152 * at least if the filesystem we are writing is a real physical one.
8153 * While this will save us against the server being killed I don't think
8154 * there is much to do about the whole server stopping for power problems
8155 * or alike */
8156 nwritten = write(server.appendfd,server.aofbuf,sdslen(server.aofbuf));
8157 if (nwritten != (signed)sdslen(server.aofbuf)) {
8158 /* Ooops, we are in troubles. The best thing to do for now is
8159 * aborting instead of giving the illusion that everything is
8160 * working as expected. */
8161 if (nwritten == -1) {
8162 redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno));
8163 } else {
8164 redisLog(REDIS_WARNING,"Exiting on short write while writing to the append-only file: %s",strerror(errno));
8165 }
8166 exit(1);
8167 }
8168 sdsfree(server.aofbuf);
8169 server.aofbuf = sdsempty();
8170
8171 /* Fsync if needed */
8172 now = time(NULL);
8173 if (server.appendfsync == APPENDFSYNC_ALWAYS ||
8174 (server.appendfsync == APPENDFSYNC_EVERYSEC &&
8175 now-server.lastfsync > 1))
8176 {
8177 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
8178 * flushing metadata. */
8179 aof_fsync(server.appendfd); /* Let's try to get this data on the disk */
8180 server.lastfsync = now;
8181 }
8182 }
8183
8184 static sds catAppendOnlyGenericCommand(sds buf, int argc, robj **argv) {
8185 int j;
8186 buf = sdscatprintf(buf,"*%d\r\n",argc);
8187 for (j = 0; j < argc; j++) {
8188 robj *o = getDecodedObject(argv[j]);
8189 buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
8190 buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
8191 buf = sdscatlen(buf,"\r\n",2);
8192 decrRefCount(o);
8193 }
8194 return buf;
8195 }
8196
8197 static sds catAppendOnlyExpireAtCommand(sds buf, robj *key, robj *seconds) {
8198 int argc = 3;
8199 long when;
8200 robj *argv[3];
8201
8202 /* Make sure we can use strtol */
8203 seconds = getDecodedObject(seconds);
8204 when = time(NULL)+strtol(seconds->ptr,NULL,10);
8205 decrRefCount(seconds);
8206
8207 argv[0] = createStringObject("EXPIREAT",8);
8208 argv[1] = key;
8209 argv[2] = createObject(REDIS_STRING,
8210 sdscatprintf(sdsempty(),"%ld",when));
8211 buf = catAppendOnlyGenericCommand(buf, argc, argv);
8212 decrRefCount(argv[0]);
8213 decrRefCount(argv[2]);
8214 return buf;
8215 }
8216
8217 static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
8218 sds buf = sdsempty();
8219 robj *tmpargv[3];
8220
8221 /* The DB this command was targetting is not the same as the last command
8222 * we appendend. To issue a SELECT command is needed. */
8223 if (dictid != server.appendseldb) {
8224 char seldb[64];
8225
8226 snprintf(seldb,sizeof(seldb),"%d",dictid);
8227 buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
8228 (unsigned long)strlen(seldb),seldb);
8229 server.appendseldb = dictid;
8230 }
8231
8232 if (cmd->proc == expireCommand) {
8233 /* Translate EXPIRE into EXPIREAT */
8234 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8235 } else if (cmd->proc == setexCommand) {
8236 /* Translate SETEX to SET and EXPIREAT */
8237 tmpargv[0] = createStringObject("SET",3);
8238 tmpargv[1] = argv[1];
8239 tmpargv[2] = argv[3];
8240 buf = catAppendOnlyGenericCommand(buf,3,tmpargv);
8241 decrRefCount(tmpargv[0]);
8242 buf = catAppendOnlyExpireAtCommand(buf,argv[1],argv[2]);
8243 } else {
8244 buf = catAppendOnlyGenericCommand(buf,argc,argv);
8245 }
8246
8247 /* Append to the AOF buffer. This will be flushed on disk just before
8248 * of re-entering the event loop, so before the client will get a
8249 * positive reply about the operation performed. */
8250 server.aofbuf = sdscatlen(server.aofbuf,buf,sdslen(buf));
8251
8252 /* If a background append only file rewriting is in progress we want to
8253 * accumulate the differences between the child DB and the current one
8254 * in a buffer, so that when the child process will do its work we
8255 * can append the differences to the new append only file. */
8256 if (server.bgrewritechildpid != -1)
8257 server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf));
8258
8259 sdsfree(buf);
8260 }
8261
8262 /* In Redis commands are always executed in the context of a client, so in
8263 * order to load the append only file we need to create a fake client. */
8264 static struct redisClient *createFakeClient(void) {
8265 struct redisClient *c = zmalloc(sizeof(*c));
8266
8267 selectDb(c,0);
8268 c->fd = -1;
8269 c->querybuf = sdsempty();
8270 c->argc = 0;
8271 c->argv = NULL;
8272 c->flags = 0;
8273 /* We set the fake client as a slave waiting for the synchronization
8274 * so that Redis will not try to send replies to this client. */
8275 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
8276 c->reply = listCreate();
8277 listSetFreeMethod(c->reply,decrRefCount);
8278 listSetDupMethod(c->reply,dupClientReplyValue);
8279 initClientMultiState(c);
8280 return c;
8281 }
8282
8283 static void freeFakeClient(struct redisClient *c) {
8284 sdsfree(c->querybuf);
8285 listRelease(c->reply);
8286 freeClientMultiState(c);
8287 zfree(c);
8288 }
8289
8290 /* Replay the append log file. On error REDIS_OK is returned. On non fatal
8291 * error (the append only file is zero-length) REDIS_ERR is returned. On
8292 * fatal error an error message is logged and the program exists. */
8293 int loadAppendOnlyFile(char *filename) {
8294 struct redisClient *fakeClient;
8295 FILE *fp = fopen(filename,"r");
8296 struct redis_stat sb;
8297 unsigned long long loadedkeys = 0;
8298 int appendonly = server.appendonly;
8299
8300 if (redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0)
8301 return REDIS_ERR;
8302
8303 if (fp == NULL) {
8304 redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
8305 exit(1);
8306 }
8307
8308 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
8309 * to the same file we're about to read. */
8310 server.appendonly = 0;
8311
8312 fakeClient = createFakeClient();
8313 while(1) {
8314 int argc, j;
8315 unsigned long len;
8316 robj **argv;
8317 char buf[128];
8318 sds argsds;
8319 struct redisCommand *cmd;
8320
8321 if (fgets(buf,sizeof(buf),fp) == NULL) {
8322 if (feof(fp))
8323 break;
8324 else
8325 goto readerr;
8326 }
8327 if (buf[0] != '*') goto fmterr;
8328 argc = atoi(buf+1);
8329 argv = zmalloc(sizeof(robj*)*argc);
8330 for (j = 0; j < argc; j++) {
8331 if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr;
8332 if (buf[0] != '$') goto fmterr;
8333 len = strtol(buf+1,NULL,10);
8334 argsds = sdsnewlen(NULL,len);
8335 if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
8336 argv[j] = createObject(REDIS_STRING,argsds);
8337 if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
8338 }
8339
8340 /* Command lookup */
8341 cmd = lookupCommand(argv[0]->ptr);
8342 if (!cmd) {
8343 redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr);
8344 exit(1);
8345 }
8346 /* Try object encoding */
8347 if (cmd->flags & REDIS_CMD_BULK)
8348 argv[argc-1] = tryObjectEncoding(argv[argc-1]);
8349 /* Run the command in the context of a fake client */
8350 fakeClient->argc = argc;
8351 fakeClient->argv = argv;
8352 cmd->proc(fakeClient);
8353 /* Discard the reply objects list from the fake client */
8354 while(listLength(fakeClient->reply))
8355 listDelNode(fakeClient->reply,listFirst(fakeClient->reply));
8356 /* Clean up, ready for the next command */
8357 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
8358 zfree(argv);
8359 /* Handle swapping while loading big datasets when VM is on */
8360 loadedkeys++;
8361 if (server.vm_enabled && (loadedkeys % 5000) == 0) {
8362 while (zmalloc_used_memory() > server.vm_max_memory) {
8363 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
8364 }
8365 }
8366 }
8367
8368 /* This point can only be reached when EOF is reached without errors.
8369 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
8370 if (fakeClient->flags & REDIS_MULTI) goto readerr;
8371
8372 fclose(fp);
8373 freeFakeClient(fakeClient);
8374 server.appendonly = appendonly;
8375 return REDIS_OK;
8376
8377 readerr:
8378 if (feof(fp)) {
8379 redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file");
8380 } else {
8381 redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
8382 }
8383 exit(1);
8384 fmterr:
8385 redisLog(REDIS_WARNING,"Bad file format reading the append only file");
8386 exit(1);
8387 }
8388
8389 /* Write an object into a file in the bulk format $<count>\r\n<payload>\r\n */
8390 static int fwriteBulkObject(FILE *fp, robj *obj) {
8391 char buf[128];
8392 int decrrc = 0;
8393
8394 /* Avoid the incr/decr ref count business if possible to help
8395 * copy-on-write (we are often in a child process when this function
8396 * is called).
8397 * Also makes sure that key objects don't get incrRefCount-ed when VM
8398 * is enabled */
8399 if (obj->encoding != REDIS_ENCODING_RAW) {
8400 obj = getDecodedObject(obj);
8401 decrrc = 1;
8402 }
8403 snprintf(buf,sizeof(buf),"$%ld\r\n",(long)sdslen(obj->ptr));
8404 if (fwrite(buf,strlen(buf),1,fp) == 0) goto err;
8405 if (sdslen(obj->ptr) && fwrite(obj->ptr,sdslen(obj->ptr),1,fp) == 0)
8406 goto err;
8407 if (fwrite("\r\n",2,1,fp) == 0) goto err;
8408 if (decrrc) decrRefCount(obj);
8409 return 1;
8410 err:
8411 if (decrrc) decrRefCount(obj);
8412 return 0;
8413 }
8414
8415 /* Write binary-safe string into a file in the bulkformat
8416 * $<count>\r\n<payload>\r\n */
8417 static int fwriteBulkString(FILE *fp, char *s, unsigned long len) {
8418 char buf[128];
8419
8420 snprintf(buf,sizeof(buf),"$%ld\r\n",(unsigned long)len);
8421 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8422 if (len && fwrite(s,len,1,fp) == 0) return 0;
8423 if (fwrite("\r\n",2,1,fp) == 0) return 0;
8424 return 1;
8425 }
8426
8427 /* Write a double value in bulk format $<count>\r\n<payload>\r\n */
8428 static int fwriteBulkDouble(FILE *fp, double d) {
8429 char buf[128], dbuf[128];
8430
8431 snprintf(dbuf,sizeof(dbuf),"%.17g\r\n",d);
8432 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(dbuf)-2);
8433 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8434 if (fwrite(dbuf,strlen(dbuf),1,fp) == 0) return 0;
8435 return 1;
8436 }
8437
8438 /* Write a long value in bulk format $<count>\r\n<payload>\r\n */
8439 static int fwriteBulkLong(FILE *fp, long l) {
8440 char buf[128], lbuf[128];
8441
8442 snprintf(lbuf,sizeof(lbuf),"%ld\r\n",l);
8443 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(lbuf)-2);
8444 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
8445 if (fwrite(lbuf,strlen(lbuf),1,fp) == 0) return 0;
8446 return 1;
8447 }
8448
8449 /* Write a sequence of commands able to fully rebuild the dataset into
8450 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
8451 static int rewriteAppendOnlyFile(char *filename) {
8452 dictIterator *di = NULL;
8453 dictEntry *de;
8454 FILE *fp;
8455 char tmpfile[256];
8456 int j;
8457 time_t now = time(NULL);
8458
8459 /* Note that we have to use a different temp name here compared to the
8460 * one used by rewriteAppendOnlyFileBackground() function. */
8461 snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid());
8462 fp = fopen(tmpfile,"w");
8463 if (!fp) {
8464 redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno));
8465 return REDIS_ERR;
8466 }
8467 for (j = 0; j < server.dbnum; j++) {
8468 char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
8469 redisDb *db = server.db+j;
8470 dict *d = db->dict;
8471 if (dictSize(d) == 0) continue;
8472 di = dictGetIterator(d);
8473 if (!di) {
8474 fclose(fp);
8475 return REDIS_ERR;
8476 }
8477
8478 /* SELECT the new DB */
8479 if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr;
8480 if (fwriteBulkLong(fp,j) == 0) goto werr;
8481
8482 /* Iterate this DB writing every entry */
8483 while((de = dictNext(di)) != NULL) {
8484 robj *key, *o;
8485 time_t expiretime;
8486 int swapped;
8487
8488 key = dictGetEntryKey(de);
8489 /* If the value for this key is swapped, load a preview in memory.
8490 * We use a "swapped" flag to remember if we need to free the
8491 * value object instead to just increment the ref count anyway
8492 * in order to avoid copy-on-write of pages if we are forked() */
8493 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
8494 key->storage == REDIS_VM_SWAPPING) {
8495 o = dictGetEntryVal(de);
8496 swapped = 0;
8497 } else {
8498 o = vmPreviewObject(key);
8499 swapped = 1;
8500 }
8501 expiretime = getExpire(db,key);
8502
8503 /* Save the key and associated value */
8504 if (o->type == REDIS_STRING) {
8505 /* Emit a SET command */
8506 char cmd[]="*3\r\n$3\r\nSET\r\n";
8507 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8508 /* Key and value */
8509 if (fwriteBulkObject(fp,key) == 0) goto werr;
8510 if (fwriteBulkObject(fp,o) == 0) goto werr;
8511 } else if (o->type == REDIS_LIST) {
8512 /* Emit the RPUSHes needed to rebuild the list */
8513 list *list = o->ptr;
8514 listNode *ln;
8515 listIter li;
8516
8517 listRewind(list,&li);
8518 while((ln = listNext(&li))) {
8519 char cmd[]="*3\r\n$5\r\nRPUSH\r\n";
8520 robj *eleobj = listNodeValue(ln);
8521
8522 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8523 if (fwriteBulkObject(fp,key) == 0) goto werr;
8524 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
8525 }
8526 } else if (o->type == REDIS_SET) {
8527 /* Emit the SADDs needed to rebuild the set */
8528 dict *set = o->ptr;
8529 dictIterator *di = dictGetIterator(set);
8530 dictEntry *de;
8531
8532 while((de = dictNext(di)) != NULL) {
8533 char cmd[]="*3\r\n$4\r\nSADD\r\n";
8534 robj *eleobj = dictGetEntryKey(de);
8535
8536 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8537 if (fwriteBulkObject(fp,key) == 0) goto werr;
8538 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
8539 }
8540 dictReleaseIterator(di);
8541 } else if (o->type == REDIS_ZSET) {
8542 /* Emit the ZADDs needed to rebuild the sorted set */
8543 zset *zs = o->ptr;
8544 dictIterator *di = dictGetIterator(zs->dict);
8545 dictEntry *de;
8546
8547 while((de = dictNext(di)) != NULL) {
8548 char cmd[]="*4\r\n$4\r\nZADD\r\n";
8549 robj *eleobj = dictGetEntryKey(de);
8550 double *score = dictGetEntryVal(de);
8551
8552 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8553 if (fwriteBulkObject(fp,key) == 0) goto werr;
8554 if (fwriteBulkDouble(fp,*score) == 0) goto werr;
8555 if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
8556 }
8557 dictReleaseIterator(di);
8558 } else if (o->type == REDIS_HASH) {
8559 char cmd[]="*4\r\n$4\r\nHSET\r\n";
8560
8561 /* Emit the HSETs needed to rebuild the hash */
8562 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
8563 unsigned char *p = zipmapRewind(o->ptr);
8564 unsigned char *field, *val;
8565 unsigned int flen, vlen;
8566
8567 while((p = zipmapNext(p,&field,&flen,&val,&vlen)) != NULL) {
8568 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8569 if (fwriteBulkObject(fp,key) == 0) goto werr;
8570 if (fwriteBulkString(fp,(char*)field,flen) == -1)
8571 return -1;
8572 if (fwriteBulkString(fp,(char*)val,vlen) == -1)
8573 return -1;
8574 }
8575 } else {
8576 dictIterator *di = dictGetIterator(o->ptr);
8577 dictEntry *de;
8578
8579 while((de = dictNext(di)) != NULL) {
8580 robj *field = dictGetEntryKey(de);
8581 robj *val = dictGetEntryVal(de);
8582
8583 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8584 if (fwriteBulkObject(fp,key) == 0) goto werr;
8585 if (fwriteBulkObject(fp,field) == -1) return -1;
8586 if (fwriteBulkObject(fp,val) == -1) return -1;
8587 }
8588 dictReleaseIterator(di);
8589 }
8590 } else {
8591 redisPanic("Unknown object type");
8592 }
8593 /* Save the expire time */
8594 if (expiretime != -1) {
8595 char cmd[]="*3\r\n$8\r\nEXPIREAT\r\n";
8596 /* If this key is already expired skip it */
8597 if (expiretime < now) continue;
8598 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
8599 if (fwriteBulkObject(fp,key) == 0) goto werr;
8600 if (fwriteBulkLong(fp,expiretime) == 0) goto werr;
8601 }
8602 if (swapped) decrRefCount(o);
8603 }
8604 dictReleaseIterator(di);
8605 }
8606
8607 /* Make sure data will not remain on the OS's output buffers */
8608 fflush(fp);
8609 fsync(fileno(fp));
8610 fclose(fp);
8611
8612 /* Use RENAME to make sure the DB file is changed atomically only
8613 * if the generate DB file is ok. */
8614 if (rename(tmpfile,filename) == -1) {
8615 redisLog(REDIS_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno));
8616 unlink(tmpfile);
8617 return REDIS_ERR;
8618 }
8619 redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed");
8620 return REDIS_OK;
8621
8622 werr:
8623 fclose(fp);
8624 unlink(tmpfile);
8625 redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
8626 if (di) dictReleaseIterator(di);
8627 return REDIS_ERR;
8628 }
8629
8630 /* This is how rewriting of the append only file in background works:
8631 *
8632 * 1) The user calls BGREWRITEAOF
8633 * 2) Redis calls this function, that forks():
8634 * 2a) the child rewrite the append only file in a temp file.
8635 * 2b) the parent accumulates differences in server.bgrewritebuf.
8636 * 3) When the child finished '2a' exists.
8637 * 4) The parent will trap the exit code, if it's OK, will append the
8638 * data accumulated into server.bgrewritebuf into the temp file, and
8639 * finally will rename(2) the temp file in the actual file name.
8640 * The the new file is reopened as the new append only file. Profit!
8641 */
8642 static int rewriteAppendOnlyFileBackground(void) {
8643 pid_t childpid;
8644
8645 if (server.bgrewritechildpid != -1) return REDIS_ERR;
8646 if (server.vm_enabled) waitEmptyIOJobsQueue();
8647 if ((childpid = fork()) == 0) {
8648 /* Child */
8649 char tmpfile[256];
8650
8651 if (server.vm_enabled) vmReopenSwapFile();
8652 close(server.fd);
8653 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
8654 if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
8655 _exit(0);
8656 } else {
8657 _exit(1);
8658 }
8659 } else {
8660 /* Parent */
8661 if (childpid == -1) {
8662 redisLog(REDIS_WARNING,
8663 "Can't rewrite append only file in background: fork: %s",
8664 strerror(errno));
8665 return REDIS_ERR;
8666 }
8667 redisLog(REDIS_NOTICE,
8668 "Background append only file rewriting started by pid %d",childpid);
8669 server.bgrewritechildpid = childpid;
8670 updateDictResizePolicy();
8671 /* We set appendseldb to -1 in order to force the next call to the
8672 * feedAppendOnlyFile() to issue a SELECT command, so the differences
8673 * accumulated by the parent into server.bgrewritebuf will start
8674 * with a SELECT statement and it will be safe to merge. */
8675 server.appendseldb = -1;
8676 return REDIS_OK;
8677 }
8678 return REDIS_OK; /* unreached */
8679 }
8680
8681 static void bgrewriteaofCommand(redisClient *c) {
8682 if (server.bgrewritechildpid != -1) {
8683 addReplySds(c,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
8684 return;
8685 }
8686 if (rewriteAppendOnlyFileBackground() == REDIS_OK) {
8687 char *status = "+Background append only file rewriting started\r\n";
8688 addReplySds(c,sdsnew(status));
8689 } else {
8690 addReply(c,shared.err);
8691 }
8692 }
8693
8694 static void aofRemoveTempFile(pid_t childpid) {
8695 char tmpfile[256];
8696
8697 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid);
8698 unlink(tmpfile);
8699 }
8700
8701 /* Virtual Memory is composed mainly of two subsystems:
8702 * - Blocking Virutal Memory
8703 * - Threaded Virtual Memory I/O
8704 * The two parts are not fully decoupled, but functions are split among two
8705 * different sections of the source code (delimited by comments) in order to
8706 * make more clear what functionality is about the blocking VM and what about
8707 * the threaded (not blocking) VM.
8708 *
8709 * Redis VM design:
8710 *
8711 * Redis VM is a blocking VM (one that blocks reading swapped values from
8712 * disk into memory when a value swapped out is needed in memory) that is made
8713 * unblocking by trying to examine the command argument vector in order to
8714 * load in background values that will likely be needed in order to exec
8715 * the command. The command is executed only once all the relevant keys
8716 * are loaded into memory.
8717 *
8718 * This basically is almost as simple of a blocking VM, but almost as parallel
8719 * as a fully non-blocking VM.
8720 */
8721
8722 /* =================== Virtual Memory - Blocking Side ====================== */
8723
8724 static void vmInit(void) {
8725 off_t totsize;
8726 int pipefds[2];
8727 size_t stacksize;
8728 struct flock fl;
8729
8730 if (server.vm_max_threads != 0)
8731 zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */
8732
8733 redisLog(REDIS_NOTICE,"Using '%s' as swap file",server.vm_swap_file);
8734 /* Try to open the old swap file, otherwise create it */
8735 if ((server.vm_fp = fopen(server.vm_swap_file,"r+b")) == NULL) {
8736 server.vm_fp = fopen(server.vm_swap_file,"w+b");
8737 }
8738 if (server.vm_fp == NULL) {
8739 redisLog(REDIS_WARNING,
8740 "Can't open the swap file: %s. Exiting.",
8741 strerror(errno));
8742 exit(1);
8743 }
8744 server.vm_fd = fileno(server.vm_fp);
8745 /* Lock the swap file for writing, this is useful in order to avoid
8746 * another instance to use the same swap file for a config error. */
8747 fl.l_type = F_WRLCK;
8748 fl.l_whence = SEEK_SET;
8749 fl.l_start = fl.l_len = 0;
8750 if (fcntl(server.vm_fd,F_SETLK,&fl) == -1) {
8751 redisLog(REDIS_WARNING,
8752 "Can't lock the swap file at '%s': %s. Make sure it is not used by another Redis instance.", server.vm_swap_file, strerror(errno));
8753 exit(1);
8754 }
8755 /* Initialize */
8756 server.vm_next_page = 0;
8757 server.vm_near_pages = 0;
8758 server.vm_stats_used_pages = 0;
8759 server.vm_stats_swapped_objects = 0;
8760 server.vm_stats_swapouts = 0;
8761 server.vm_stats_swapins = 0;
8762 totsize = server.vm_pages*server.vm_page_size;
8763 redisLog(REDIS_NOTICE,"Allocating %lld bytes of swap file",totsize);
8764 if (ftruncate(server.vm_fd,totsize) == -1) {
8765 redisLog(REDIS_WARNING,"Can't ftruncate swap file: %s. Exiting.",
8766 strerror(errno));
8767 exit(1);
8768 } else {
8769 redisLog(REDIS_NOTICE,"Swap file allocated with success");
8770 }
8771 server.vm_bitmap = zmalloc((server.vm_pages+7)/8);
8772 redisLog(REDIS_VERBOSE,"Allocated %lld bytes page table for %lld pages",
8773 (long long) (server.vm_pages+7)/8, server.vm_pages);
8774 memset(server.vm_bitmap,0,(server.vm_pages+7)/8);
8775
8776 /* Initialize threaded I/O (used by Virtual Memory) */
8777 server.io_newjobs = listCreate();
8778 server.io_processing = listCreate();
8779 server.io_processed = listCreate();
8780 server.io_ready_clients = listCreate();
8781 pthread_mutex_init(&server.io_mutex,NULL);
8782 pthread_mutex_init(&server.obj_freelist_mutex,NULL);
8783 pthread_mutex_init(&server.io_swapfile_mutex,NULL);
8784 server.io_active_threads = 0;
8785 if (pipe(pipefds) == -1) {
8786 redisLog(REDIS_WARNING,"Unable to intialized VM: pipe(2): %s. Exiting."
8787 ,strerror(errno));
8788 exit(1);
8789 }
8790 server.io_ready_pipe_read = pipefds[0];
8791 server.io_ready_pipe_write = pipefds[1];
8792 redisAssert(anetNonBlock(NULL,server.io_ready_pipe_read) != ANET_ERR);
8793 /* LZF requires a lot of stack */
8794 pthread_attr_init(&server.io_threads_attr);
8795 pthread_attr_getstacksize(&server.io_threads_attr, &stacksize);
8796 while (stacksize < REDIS_THREAD_STACK_SIZE) stacksize *= 2;
8797 pthread_attr_setstacksize(&server.io_threads_attr, stacksize);
8798 /* Listen for events in the threaded I/O pipe */
8799 if (aeCreateFileEvent(server.el, server.io_ready_pipe_read, AE_READABLE,
8800 vmThreadedIOCompletedJob, NULL) == AE_ERR)
8801 oom("creating file event");
8802 }
8803
8804 /* Mark the page as used */
8805 static void vmMarkPageUsed(off_t page) {
8806 off_t byte = page/8;
8807 int bit = page&7;
8808 redisAssert(vmFreePage(page) == 1);
8809 server.vm_bitmap[byte] |= 1<<bit;
8810 }
8811
8812 /* Mark N contiguous pages as used, with 'page' being the first. */
8813 static void vmMarkPagesUsed(off_t page, off_t count) {
8814 off_t j;
8815
8816 for (j = 0; j < count; j++)
8817 vmMarkPageUsed(page+j);
8818 server.vm_stats_used_pages += count;
8819 redisLog(REDIS_DEBUG,"Mark USED pages: %lld pages at %lld\n",
8820 (long long)count, (long long)page);
8821 }
8822
8823 /* Mark the page as free */
8824 static void vmMarkPageFree(off_t page) {
8825 off_t byte = page/8;
8826 int bit = page&7;
8827 redisAssert(vmFreePage(page) == 0);
8828 server.vm_bitmap[byte] &= ~(1<<bit);
8829 }
8830
8831 /* Mark N contiguous pages as free, with 'page' being the first. */
8832 static void vmMarkPagesFree(off_t page, off_t count) {
8833 off_t j;
8834
8835 for (j = 0; j < count; j++)
8836 vmMarkPageFree(page+j);
8837 server.vm_stats_used_pages -= count;
8838 redisLog(REDIS_DEBUG,"Mark FREE pages: %lld pages at %lld\n",
8839 (long long)count, (long long)page);
8840 }
8841
8842 /* Test if the page is free */
8843 static int vmFreePage(off_t page) {
8844 off_t byte = page/8;
8845 int bit = page&7;
8846 return (server.vm_bitmap[byte] & (1<<bit)) == 0;
8847 }
8848
8849 /* Find N contiguous free pages storing the first page of the cluster in *first.
8850 * Returns REDIS_OK if it was able to find N contiguous pages, otherwise
8851 * REDIS_ERR is returned.
8852 *
8853 * This function uses a simple algorithm: we try to allocate
8854 * REDIS_VM_MAX_NEAR_PAGES sequentially, when we reach this limit we start
8855 * again from the start of the swap file searching for free spaces.
8856 *
8857 * If it looks pretty clear that there are no free pages near our offset
8858 * we try to find less populated places doing a forward jump of
8859 * REDIS_VM_MAX_RANDOM_JUMP, then we start scanning again a few pages
8860 * without hurry, and then we jump again and so forth...
8861 *
8862 * This function can be improved using a free list to avoid to guess
8863 * too much, since we could collect data about freed pages.
8864 *
8865 * note: I implemented this function just after watching an episode of
8866 * Battlestar Galactica, where the hybrid was continuing to say "JUMP!"
8867 */
8868 static int vmFindContiguousPages(off_t *first, off_t n) {
8869 off_t base, offset = 0, since_jump = 0, numfree = 0;
8870
8871 if (server.vm_near_pages == REDIS_VM_MAX_NEAR_PAGES) {
8872 server.vm_near_pages = 0;
8873 server.vm_next_page = 0;
8874 }
8875 server.vm_near_pages++; /* Yet another try for pages near to the old ones */
8876 base = server.vm_next_page;
8877
8878 while(offset < server.vm_pages) {
8879 off_t this = base+offset;
8880
8881 /* If we overflow, restart from page zero */
8882 if (this >= server.vm_pages) {
8883 this -= server.vm_pages;
8884 if (this == 0) {
8885 /* Just overflowed, what we found on tail is no longer
8886 * interesting, as it's no longer contiguous. */
8887 numfree = 0;
8888 }
8889 }
8890 if (vmFreePage(this)) {
8891 /* This is a free page */
8892 numfree++;
8893 /* Already got N free pages? Return to the caller, with success */
8894 if (numfree == n) {
8895 *first = this-(n-1);
8896 server.vm_next_page = this+1;
8897 redisLog(REDIS_DEBUG, "FOUND CONTIGUOUS PAGES: %lld pages at %lld\n", (long long) n, (long long) *first);
8898 return REDIS_OK;
8899 }
8900 } else {
8901 /* The current one is not a free page */
8902 numfree = 0;
8903 }
8904
8905 /* Fast-forward if the current page is not free and we already
8906 * searched enough near this place. */
8907 since_jump++;
8908 if (!numfree && since_jump >= REDIS_VM_MAX_RANDOM_JUMP/4) {
8909 offset += random() % REDIS_VM_MAX_RANDOM_JUMP;
8910 since_jump = 0;
8911 /* Note that even if we rewind after the jump, we are don't need
8912 * to make sure numfree is set to zero as we only jump *if* it
8913 * is set to zero. */
8914 } else {
8915 /* Otherwise just check the next page */
8916 offset++;
8917 }
8918 }
8919 return REDIS_ERR;
8920 }
8921
8922 /* Write the specified object at the specified page of the swap file */
8923 static int vmWriteObjectOnSwap(robj *o, off_t page) {
8924 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
8925 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
8926 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8927 redisLog(REDIS_WARNING,
8928 "Critical VM problem in vmWriteObjectOnSwap(): can't seek: %s",
8929 strerror(errno));
8930 return REDIS_ERR;
8931 }
8932 rdbSaveObject(server.vm_fp,o);
8933 fflush(server.vm_fp);
8934 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8935 return REDIS_OK;
8936 }
8937
8938 /* Swap the 'val' object relative to 'key' into disk. Store all the information
8939 * needed to later retrieve the object into the key object.
8940 * If we can't find enough contiguous empty pages to swap the object on disk
8941 * REDIS_ERR is returned. */
8942 static int vmSwapObjectBlocking(robj *key, robj *val) {
8943 off_t pages = rdbSavedObjectPages(val,NULL);
8944 off_t page;
8945
8946 assert(key->storage == REDIS_VM_MEMORY);
8947 assert(key->refcount == 1);
8948 if (vmFindContiguousPages(&page,pages) == REDIS_ERR) return REDIS_ERR;
8949 if (vmWriteObjectOnSwap(val,page) == REDIS_ERR) return REDIS_ERR;
8950 key->vm.page = page;
8951 key->vm.usedpages = pages;
8952 key->storage = REDIS_VM_SWAPPED;
8953 key->vtype = val->type;
8954 decrRefCount(val); /* Deallocate the object from memory. */
8955 vmMarkPagesUsed(page,pages);
8956 redisLog(REDIS_DEBUG,"VM: object %s swapped out at %lld (%lld pages)",
8957 (unsigned char*) key->ptr,
8958 (unsigned long long) page, (unsigned long long) pages);
8959 server.vm_stats_swapped_objects++;
8960 server.vm_stats_swapouts++;
8961 return REDIS_OK;
8962 }
8963
8964 static robj *vmReadObjectFromSwap(off_t page, int type) {
8965 robj *o;
8966
8967 if (server.vm_enabled) pthread_mutex_lock(&server.io_swapfile_mutex);
8968 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
8969 redisLog(REDIS_WARNING,
8970 "Unrecoverable VM problem in vmReadObjectFromSwap(): can't seek: %s",
8971 strerror(errno));
8972 _exit(1);
8973 }
8974 o = rdbLoadObject(type,server.vm_fp);
8975 if (o == NULL) {
8976 redisLog(REDIS_WARNING, "Unrecoverable VM problem in vmReadObjectFromSwap(): can't load object from swap file: %s", strerror(errno));
8977 _exit(1);
8978 }
8979 if (server.vm_enabled) pthread_mutex_unlock(&server.io_swapfile_mutex);
8980 return o;
8981 }
8982
8983 /* Load the value object relative to the 'key' object from swap to memory.
8984 * The newly allocated object is returned.
8985 *
8986 * If preview is true the unserialized object is returned to the caller but
8987 * no changes are made to the key object, nor the pages are marked as freed */
8988 static robj *vmGenericLoadObject(robj *key, int preview) {
8989 robj *val;
8990
8991 redisAssert(key->storage == REDIS_VM_SWAPPED || key->storage == REDIS_VM_LOADING);
8992 val = vmReadObjectFromSwap(key->vm.page,key->vtype);
8993 if (!preview) {
8994 key->storage = REDIS_VM_MEMORY;
8995 key->vm.atime = server.unixtime;
8996 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
8997 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk",
8998 (unsigned char*) key->ptr);
8999 server.vm_stats_swapped_objects--;
9000 } else {
9001 redisLog(REDIS_DEBUG, "VM: object %s previewed from disk",
9002 (unsigned char*) key->ptr);
9003 }
9004 server.vm_stats_swapins++;
9005 return val;
9006 }
9007
9008 /* Plain object loading, from swap to memory */
9009 static robj *vmLoadObject(robj *key) {
9010 /* If we are loading the object in background, stop it, we
9011 * need to load this object synchronously ASAP. */
9012 if (key->storage == REDIS_VM_LOADING)
9013 vmCancelThreadedIOJob(key);
9014 return vmGenericLoadObject(key,0);
9015 }
9016
9017 /* Just load the value on disk, without to modify the key.
9018 * This is useful when we want to perform some operation on the value
9019 * without to really bring it from swap to memory, like while saving the
9020 * dataset or rewriting the append only log. */
9021 static robj *vmPreviewObject(robj *key) {
9022 return vmGenericLoadObject(key,1);
9023 }
9024
9025 /* How a good candidate is this object for swapping?
9026 * The better candidate it is, the greater the returned value.
9027 *
9028 * Currently we try to perform a fast estimation of the object size in
9029 * memory, and combine it with aging informations.
9030 *
9031 * Basically swappability = idle-time * log(estimated size)
9032 *
9033 * Bigger objects are preferred over smaller objects, but not
9034 * proportionally, this is why we use the logarithm. This algorithm is
9035 * just a first try and will probably be tuned later. */
9036 static double computeObjectSwappability(robj *o) {
9037 time_t age = server.unixtime - o->vm.atime;
9038 long asize = 0;
9039 list *l;
9040 dict *d;
9041 struct dictEntry *de;
9042 int z;
9043
9044 if (age <= 0) return 0;
9045 switch(o->type) {
9046 case REDIS_STRING:
9047 if (o->encoding != REDIS_ENCODING_RAW) {
9048 asize = sizeof(*o);
9049 } else {
9050 asize = sdslen(o->ptr)+sizeof(*o)+sizeof(long)*2;
9051 }
9052 break;
9053 case REDIS_LIST:
9054 l = o->ptr;
9055 listNode *ln = listFirst(l);
9056
9057 asize = sizeof(list);
9058 if (ln) {
9059 robj *ele = ln->value;
9060 long elesize;
9061
9062 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9063 (sizeof(*o)+sdslen(ele->ptr)) :
9064 sizeof(*o);
9065 asize += (sizeof(listNode)+elesize)*listLength(l);
9066 }
9067 break;
9068 case REDIS_SET:
9069 case REDIS_ZSET:
9070 z = (o->type == REDIS_ZSET);
9071 d = z ? ((zset*)o->ptr)->dict : o->ptr;
9072
9073 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9074 if (z) asize += sizeof(zset)-sizeof(dict);
9075 if (dictSize(d)) {
9076 long elesize;
9077 robj *ele;
9078
9079 de = dictGetRandomKey(d);
9080 ele = dictGetEntryKey(de);
9081 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9082 (sizeof(*o)+sdslen(ele->ptr)) :
9083 sizeof(*o);
9084 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9085 if (z) asize += sizeof(zskiplistNode)*dictSize(d);
9086 }
9087 break;
9088 case REDIS_HASH:
9089 if (o->encoding == REDIS_ENCODING_ZIPMAP) {
9090 unsigned char *p = zipmapRewind((unsigned char*)o->ptr);
9091 unsigned int len = zipmapLen((unsigned char*)o->ptr);
9092 unsigned int klen, vlen;
9093 unsigned char *key, *val;
9094
9095 if ((p = zipmapNext(p,&key,&klen,&val,&vlen)) == NULL) {
9096 klen = 0;
9097 vlen = 0;
9098 }
9099 asize = len*(klen+vlen+3);
9100 } else if (o->encoding == REDIS_ENCODING_HT) {
9101 d = o->ptr;
9102 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
9103 if (dictSize(d)) {
9104 long elesize;
9105 robj *ele;
9106
9107 de = dictGetRandomKey(d);
9108 ele = dictGetEntryKey(de);
9109 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9110 (sizeof(*o)+sdslen(ele->ptr)) :
9111 sizeof(*o);
9112 ele = dictGetEntryVal(de);
9113 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
9114 (sizeof(*o)+sdslen(ele->ptr)) :
9115 sizeof(*o);
9116 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
9117 }
9118 }
9119 break;
9120 }
9121 return (double)age*log(1+asize);
9122 }
9123
9124 /* Try to swap an object that's a good candidate for swapping.
9125 * Returns REDIS_OK if the object was swapped, REDIS_ERR if it's not possible
9126 * to swap any object at all.
9127 *
9128 * If 'usethreaded' is true, Redis will try to swap the object in background
9129 * using I/O threads. */
9130 static int vmSwapOneObject(int usethreads) {
9131 int j, i;
9132 struct dictEntry *best = NULL;
9133 double best_swappability = 0;
9134 redisDb *best_db = NULL;
9135 robj *key, *val;
9136
9137 for (j = 0; j < server.dbnum; j++) {
9138 redisDb *db = server.db+j;
9139 /* Why maxtries is set to 100?
9140 * Because this way (usually) we'll find 1 object even if just 1% - 2%
9141 * are swappable objects */
9142 int maxtries = 100;
9143
9144 if (dictSize(db->dict) == 0) continue;
9145 for (i = 0; i < 5; i++) {
9146 dictEntry *de;
9147 double swappability;
9148
9149 if (maxtries) maxtries--;
9150 de = dictGetRandomKey(db->dict);
9151 key = dictGetEntryKey(de);
9152 val = dictGetEntryVal(de);
9153 /* Only swap objects that are currently in memory.
9154 *
9155 * Also don't swap shared objects if threaded VM is on, as we
9156 * try to ensure that the main thread does not touch the
9157 * object while the I/O thread is using it, but we can't
9158 * control other keys without adding additional mutex. */
9159 if (key->storage != REDIS_VM_MEMORY ||
9160 (server.vm_max_threads != 0 && val->refcount != 1)) {
9161 if (maxtries) i--; /* don't count this try */
9162 continue;
9163 }
9164 swappability = computeObjectSwappability(val);
9165 if (!best || swappability > best_swappability) {
9166 best = de;
9167 best_swappability = swappability;
9168 best_db = db;
9169 }
9170 }
9171 }
9172 if (best == NULL) return REDIS_ERR;
9173 key = dictGetEntryKey(best);
9174 val = dictGetEntryVal(best);
9175
9176 redisLog(REDIS_DEBUG,"Key with best swappability: %s, %f",
9177 key->ptr, best_swappability);
9178
9179 /* Unshare the key if needed */
9180 if (key->refcount > 1) {
9181 robj *newkey = dupStringObject(key);
9182 decrRefCount(key);
9183 key = dictGetEntryKey(best) = newkey;
9184 }
9185 /* Swap it */
9186 if (usethreads) {
9187 vmSwapObjectThreaded(key,val,best_db);
9188 return REDIS_OK;
9189 } else {
9190 if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
9191 dictGetEntryVal(best) = NULL;
9192 return REDIS_OK;
9193 } else {
9194 return REDIS_ERR;
9195 }
9196 }
9197 }
9198
9199 static int vmSwapOneObjectBlocking() {
9200 return vmSwapOneObject(0);
9201 }
9202
9203 static int vmSwapOneObjectThreaded() {
9204 return vmSwapOneObject(1);
9205 }
9206
9207 /* Return true if it's safe to swap out objects in a given moment.
9208 * Basically we don't want to swap objects out while there is a BGSAVE
9209 * or a BGAEOREWRITE running in backgroud. */
9210 static int vmCanSwapOut(void) {
9211 return (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1);
9212 }
9213
9214 /* Delete a key if swapped. Returns 1 if the key was found, was swapped
9215 * and was deleted. Otherwise 0 is returned. */
9216 static int deleteIfSwapped(redisDb *db, robj *key) {
9217 dictEntry *de;
9218 robj *foundkey;
9219
9220 if ((de = dictFind(db->dict,key)) == NULL) return 0;
9221 foundkey = dictGetEntryKey(de);
9222 if (foundkey->storage == REDIS_VM_MEMORY) return 0;
9223 deleteKey(db,key);
9224 return 1;
9225 }
9226
9227 /* =================== Virtual Memory - Threaded I/O ======================= */
9228
9229 static void freeIOJob(iojob *j) {
9230 if ((j->type == REDIS_IOJOB_PREPARE_SWAP ||
9231 j->type == REDIS_IOJOB_DO_SWAP ||
9232 j->type == REDIS_IOJOB_LOAD) && j->val != NULL)
9233 decrRefCount(j->val);
9234 /* We don't decrRefCount the j->key field as we did't incremented
9235 * the count creating IO Jobs. This is because the key field here is
9236 * just used as an indentifier and if a key is removed the Job should
9237 * never be touched again. */
9238 zfree(j);
9239 }
9240
9241 /* Every time a thread finished a Job, it writes a byte into the write side
9242 * of an unix pipe in order to "awake" the main thread, and this function
9243 * is called. */
9244 static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata,
9245 int mask)
9246 {
9247 char buf[1];
9248 int retval, processed = 0, toprocess = -1, trytoswap = 1;
9249 REDIS_NOTUSED(el);
9250 REDIS_NOTUSED(mask);
9251 REDIS_NOTUSED(privdata);
9252
9253 /* For every byte we read in the read side of the pipe, there is one
9254 * I/O job completed to process. */
9255 while((retval = read(fd,buf,1)) == 1) {
9256 iojob *j;
9257 listNode *ln;
9258 robj *key;
9259 struct dictEntry *de;
9260
9261 redisLog(REDIS_DEBUG,"Processing I/O completed job");
9262
9263 /* Get the processed element (the oldest one) */
9264 lockThreadedIO();
9265 assert(listLength(server.io_processed) != 0);
9266 if (toprocess == -1) {
9267 toprocess = (listLength(server.io_processed)*REDIS_MAX_COMPLETED_JOBS_PROCESSED)/100;
9268 if (toprocess <= 0) toprocess = 1;
9269 }
9270 ln = listFirst(server.io_processed);
9271 j = ln->value;
9272 listDelNode(server.io_processed,ln);
9273 unlockThreadedIO();
9274 /* If this job is marked as canceled, just ignore it */
9275 if (j->canceled) {
9276 freeIOJob(j);
9277 continue;
9278 }
9279 /* Post process it in the main thread, as there are things we
9280 * can do just here to avoid race conditions and/or invasive locks */
9281 redisLog(REDIS_DEBUG,"Job %p type: %d, key at %p (%s) refcount: %d\n", (void*) j, j->type, (void*)j->key, (char*)j->key->ptr, j->key->refcount);
9282 de = dictFind(j->db->dict,j->key);
9283 assert(de != NULL);
9284 key = dictGetEntryKey(de);
9285 if (j->type == REDIS_IOJOB_LOAD) {
9286 redisDb *db;
9287
9288 /* Key loaded, bring it at home */
9289 key->storage = REDIS_VM_MEMORY;
9290 key->vm.atime = server.unixtime;
9291 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
9292 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk (threaded)",
9293 (unsigned char*) key->ptr);
9294 server.vm_stats_swapped_objects--;
9295 server.vm_stats_swapins++;
9296 dictGetEntryVal(de) = j->val;
9297 incrRefCount(j->val);
9298 db = j->db;
9299 freeIOJob(j);
9300 /* Handle clients waiting for this key to be loaded. */
9301 handleClientsBlockedOnSwappedKey(db,key);
9302 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
9303 /* Now we know the amount of pages required to swap this object.
9304 * Let's find some space for it, and queue this task again
9305 * rebranded as REDIS_IOJOB_DO_SWAP. */
9306 if (!vmCanSwapOut() ||
9307 vmFindContiguousPages(&j->page,j->pages) == REDIS_ERR)
9308 {
9309 /* Ooops... no space or we can't swap as there is
9310 * a fork()ed Redis trying to save stuff on disk. */
9311 freeIOJob(j);
9312 key->storage = REDIS_VM_MEMORY; /* undo operation */
9313 } else {
9314 /* Note that we need to mark this pages as used now,
9315 * if the job will be canceled, we'll mark them as freed
9316 * again. */
9317 vmMarkPagesUsed(j->page,j->pages);
9318 j->type = REDIS_IOJOB_DO_SWAP;
9319 lockThreadedIO();
9320 queueIOJob(j);
9321 unlockThreadedIO();
9322 }
9323 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
9324 robj *val;
9325
9326 /* Key swapped. We can finally free some memory. */
9327 if (key->storage != REDIS_VM_SWAPPING) {
9328 printf("key->storage: %d\n",key->storage);
9329 printf("key->name: %s\n",(char*)key->ptr);
9330 printf("key->refcount: %d\n",key->refcount);
9331 printf("val: %p\n",(void*)j->val);
9332 printf("val->type: %d\n",j->val->type);
9333 printf("val->ptr: %s\n",(char*)j->val->ptr);
9334 }
9335 redisAssert(key->storage == REDIS_VM_SWAPPING);
9336 val = dictGetEntryVal(de);
9337 key->vm.page = j->page;
9338 key->vm.usedpages = j->pages;
9339 key->storage = REDIS_VM_SWAPPED;
9340 key->vtype = j->val->type;
9341 decrRefCount(val); /* Deallocate the object from memory. */
9342 dictGetEntryVal(de) = NULL;
9343 redisLog(REDIS_DEBUG,
9344 "VM: object %s swapped out at %lld (%lld pages) (threaded)",
9345 (unsigned char*) key->ptr,
9346 (unsigned long long) j->page, (unsigned long long) j->pages);
9347 server.vm_stats_swapped_objects++;
9348 server.vm_stats_swapouts++;
9349 freeIOJob(j);
9350 /* Put a few more swap requests in queue if we are still
9351 * out of memory */
9352 if (trytoswap && vmCanSwapOut() &&
9353 zmalloc_used_memory() > server.vm_max_memory)
9354 {
9355 int more = 1;
9356 while(more) {
9357 lockThreadedIO();
9358 more = listLength(server.io_newjobs) <
9359 (unsigned) server.vm_max_threads;
9360 unlockThreadedIO();
9361 /* Don't waste CPU time if swappable objects are rare. */
9362 if (vmSwapOneObjectThreaded() == REDIS_ERR) {
9363 trytoswap = 0;
9364 break;
9365 }
9366 }
9367 }
9368 }
9369 processed++;
9370 if (processed == toprocess) return;
9371 }
9372 if (retval < 0 && errno != EAGAIN) {
9373 redisLog(REDIS_WARNING,
9374 "WARNING: read(2) error in vmThreadedIOCompletedJob() %s",
9375 strerror(errno));
9376 }
9377 }
9378
9379 static void lockThreadedIO(void) {
9380 pthread_mutex_lock(&server.io_mutex);
9381 }
9382
9383 static void unlockThreadedIO(void) {
9384 pthread_mutex_unlock(&server.io_mutex);
9385 }
9386
9387 /* Remove the specified object from the threaded I/O queue if still not
9388 * processed, otherwise make sure to flag it as canceled. */
9389 static void vmCancelThreadedIOJob(robj *o) {
9390 list *lists[3] = {
9391 server.io_newjobs, /* 0 */
9392 server.io_processing, /* 1 */
9393 server.io_processed /* 2 */
9394 };
9395 int i;
9396
9397 assert(o->storage == REDIS_VM_LOADING || o->storage == REDIS_VM_SWAPPING);
9398 again:
9399 lockThreadedIO();
9400 /* Search for a matching key in one of the queues */
9401 for (i = 0; i < 3; i++) {
9402 listNode *ln;
9403 listIter li;
9404
9405 listRewind(lists[i],&li);
9406 while ((ln = listNext(&li)) != NULL) {
9407 iojob *job = ln->value;
9408
9409 if (job->canceled) continue; /* Skip this, already canceled. */
9410 if (job->key == o) {
9411 redisLog(REDIS_DEBUG,"*** CANCELED %p (%s) (type %d) (LIST ID %d)\n",
9412 (void*)job, (char*)o->ptr, job->type, i);
9413 /* Mark the pages as free since the swap didn't happened
9414 * or happened but is now discarded. */
9415 if (i != 1 && job->type == REDIS_IOJOB_DO_SWAP)
9416 vmMarkPagesFree(job->page,job->pages);
9417 /* Cancel the job. It depends on the list the job is
9418 * living in. */
9419 switch(i) {
9420 case 0: /* io_newjobs */
9421 /* If the job was yet not processed the best thing to do
9422 * is to remove it from the queue at all */
9423 freeIOJob(job);
9424 listDelNode(lists[i],ln);
9425 break;
9426 case 1: /* io_processing */
9427 /* Oh Shi- the thread is messing with the Job:
9428 *
9429 * Probably it's accessing the object if this is a
9430 * PREPARE_SWAP or DO_SWAP job.
9431 * If it's a LOAD job it may be reading from disk and
9432 * if we don't wait for the job to terminate before to
9433 * cancel it, maybe in a few microseconds data can be
9434 * corrupted in this pages. So the short story is:
9435 *
9436 * Better to wait for the job to move into the
9437 * next queue (processed)... */
9438
9439 /* We try again and again until the job is completed. */
9440 unlockThreadedIO();
9441 /* But let's wait some time for the I/O thread
9442 * to finish with this job. After all this condition
9443 * should be very rare. */
9444 usleep(1);
9445 goto again;
9446 case 2: /* io_processed */
9447 /* The job was already processed, that's easy...
9448 * just mark it as canceled so that we'll ignore it
9449 * when processing completed jobs. */
9450 job->canceled = 1;
9451 break;
9452 }
9453 /* Finally we have to adjust the storage type of the object
9454 * in order to "UNDO" the operaiton. */
9455 if (o->storage == REDIS_VM_LOADING)
9456 o->storage = REDIS_VM_SWAPPED;
9457 else if (o->storage == REDIS_VM_SWAPPING)
9458 o->storage = REDIS_VM_MEMORY;
9459 unlockThreadedIO();
9460 return;
9461 }
9462 }
9463 }
9464 unlockThreadedIO();
9465 assert(1 != 1); /* We should never reach this */
9466 }
9467
9468 static void *IOThreadEntryPoint(void *arg) {
9469 iojob *j;
9470 listNode *ln;
9471 REDIS_NOTUSED(arg);
9472
9473 pthread_detach(pthread_self());
9474 while(1) {
9475 /* Get a new job to process */
9476 lockThreadedIO();
9477 if (listLength(server.io_newjobs) == 0) {
9478 /* No new jobs in queue, exit. */
9479 redisLog(REDIS_DEBUG,"Thread %ld exiting, nothing to do",
9480 (long) pthread_self());
9481 server.io_active_threads--;
9482 unlockThreadedIO();
9483 return NULL;
9484 }
9485 ln = listFirst(server.io_newjobs);
9486 j = ln->value;
9487 listDelNode(server.io_newjobs,ln);
9488 /* Add the job in the processing queue */
9489 j->thread = pthread_self();
9490 listAddNodeTail(server.io_processing,j);
9491 ln = listLast(server.io_processing); /* We use ln later to remove it */
9492 unlockThreadedIO();
9493 redisLog(REDIS_DEBUG,"Thread %ld got a new job (type %d): %p about key '%s'",
9494 (long) pthread_self(), j->type, (void*)j, (char*)j->key->ptr);
9495
9496 /* Process the Job */
9497 if (j->type == REDIS_IOJOB_LOAD) {
9498 j->val = vmReadObjectFromSwap(j->page,j->key->vtype);
9499 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
9500 FILE *fp = fopen("/dev/null","w+");
9501 j->pages = rdbSavedObjectPages(j->val,fp);
9502 fclose(fp);
9503 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
9504 if (vmWriteObjectOnSwap(j->val,j->page) == REDIS_ERR)
9505 j->canceled = 1;
9506 }
9507
9508 /* Done: insert the job into the processed queue */
9509 redisLog(REDIS_DEBUG,"Thread %ld completed the job: %p (key %s)",
9510 (long) pthread_self(), (void*)j, (char*)j->key->ptr);
9511 lockThreadedIO();
9512 listDelNode(server.io_processing,ln);
9513 listAddNodeTail(server.io_processed,j);
9514 unlockThreadedIO();
9515
9516 /* Signal the main thread there is new stuff to process */
9517 assert(write(server.io_ready_pipe_write,"x",1) == 1);
9518 }
9519 return NULL; /* never reached */
9520 }
9521
9522 static void spawnIOThread(void) {
9523 pthread_t thread;
9524 sigset_t mask, omask;
9525 int err;
9526
9527 sigemptyset(&mask);
9528 sigaddset(&mask,SIGCHLD);
9529 sigaddset(&mask,SIGHUP);
9530 sigaddset(&mask,SIGPIPE);
9531 pthread_sigmask(SIG_SETMASK, &mask, &omask);
9532 while ((err = pthread_create(&thread,&server.io_threads_attr,IOThreadEntryPoint,NULL)) != 0) {
9533 redisLog(REDIS_WARNING,"Unable to spawn an I/O thread: %s",
9534 strerror(err));
9535 usleep(1000000);
9536 }
9537 pthread_sigmask(SIG_SETMASK, &omask, NULL);
9538 server.io_active_threads++;
9539 }
9540
9541 /* We need to wait for the last thread to exit before we are able to
9542 * fork() in order to BGSAVE or BGREWRITEAOF. */
9543 static void waitEmptyIOJobsQueue(void) {
9544 while(1) {
9545 int io_processed_len;
9546
9547 lockThreadedIO();
9548 if (listLength(server.io_newjobs) == 0 &&
9549 listLength(server.io_processing) == 0 &&
9550 server.io_active_threads == 0)
9551 {
9552 unlockThreadedIO();
9553 return;
9554 }
9555 /* While waiting for empty jobs queue condition we post-process some
9556 * finshed job, as I/O threads may be hanging trying to write against
9557 * the io_ready_pipe_write FD but there are so much pending jobs that
9558 * it's blocking. */
9559 io_processed_len = listLength(server.io_processed);
9560 unlockThreadedIO();
9561 if (io_processed_len) {
9562 vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read,NULL,0);
9563 usleep(1000); /* 1 millisecond */
9564 } else {
9565 usleep(10000); /* 10 milliseconds */
9566 }
9567 }
9568 }
9569
9570 static void vmReopenSwapFile(void) {
9571 /* Note: we don't close the old one as we are in the child process
9572 * and don't want to mess at all with the original file object. */
9573 server.vm_fp = fopen(server.vm_swap_file,"r+b");
9574 if (server.vm_fp == NULL) {
9575 redisLog(REDIS_WARNING,"Can't re-open the VM swap file: %s. Exiting.",
9576 server.vm_swap_file);
9577 _exit(1);
9578 }
9579 server.vm_fd = fileno(server.vm_fp);
9580 }
9581
9582 /* This function must be called while with threaded IO locked */
9583 static void queueIOJob(iojob *j) {
9584 redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n",
9585 (void*)j, j->type, (char*)j->key->ptr);
9586 listAddNodeTail(server.io_newjobs,j);
9587 if (server.io_active_threads < server.vm_max_threads)
9588 spawnIOThread();
9589 }
9590
9591 static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db) {
9592 iojob *j;
9593
9594 assert(key->storage == REDIS_VM_MEMORY);
9595 assert(key->refcount == 1);
9596
9597 j = zmalloc(sizeof(*j));
9598 j->type = REDIS_IOJOB_PREPARE_SWAP;
9599 j->db = db;
9600 j->key = key;
9601 j->val = val;
9602 incrRefCount(val);
9603 j->canceled = 0;
9604 j->thread = (pthread_t) -1;
9605 key->storage = REDIS_VM_SWAPPING;
9606
9607 lockThreadedIO();
9608 queueIOJob(j);
9609 unlockThreadedIO();
9610 return REDIS_OK;
9611 }
9612
9613 /* ============ Virtual Memory - Blocking clients on missing keys =========== */
9614
9615 /* This function makes the clinet 'c' waiting for the key 'key' to be loaded.
9616 * If there is not already a job loading the key, it is craeted.
9617 * The key is added to the io_keys list in the client structure, and also
9618 * in the hash table mapping swapped keys to waiting clients, that is,
9619 * server.io_waited_keys. */
9620 static int waitForSwappedKey(redisClient *c, robj *key) {
9621 struct dictEntry *de;
9622 robj *o;
9623 list *l;
9624
9625 /* If the key does not exist or is already in RAM we don't need to
9626 * block the client at all. */
9627 de = dictFind(c->db->dict,key);
9628 if (de == NULL) return 0;
9629 o = dictGetEntryKey(de);
9630 if (o->storage == REDIS_VM_MEMORY) {
9631 return 0;
9632 } else if (o->storage == REDIS_VM_SWAPPING) {
9633 /* We were swapping the key, undo it! */
9634 vmCancelThreadedIOJob(o);
9635 return 0;
9636 }
9637
9638 /* OK: the key is either swapped, or being loaded just now. */
9639
9640 /* Add the key to the list of keys this client is waiting for.
9641 * This maps clients to keys they are waiting for. */
9642 listAddNodeTail(c->io_keys,key);
9643 incrRefCount(key);
9644
9645 /* Add the client to the swapped keys => clients waiting map. */
9646 de = dictFind(c->db->io_keys,key);
9647 if (de == NULL) {
9648 int retval;
9649
9650 /* For every key we take a list of clients blocked for it */
9651 l = listCreate();
9652 retval = dictAdd(c->db->io_keys,key,l);
9653 incrRefCount(key);
9654 assert(retval == DICT_OK);
9655 } else {
9656 l = dictGetEntryVal(de);
9657 }
9658 listAddNodeTail(l,c);
9659
9660 /* Are we already loading the key from disk? If not create a job */
9661 if (o->storage == REDIS_VM_SWAPPED) {
9662 iojob *j;
9663
9664 o->storage = REDIS_VM_LOADING;
9665 j = zmalloc(sizeof(*j));
9666 j->type = REDIS_IOJOB_LOAD;
9667 j->db = c->db;
9668 j->key = o;
9669 j->key->vtype = o->vtype;
9670 j->page = o->vm.page;
9671 j->val = NULL;
9672 j->canceled = 0;
9673 j->thread = (pthread_t) -1;
9674 lockThreadedIO();
9675 queueIOJob(j);
9676 unlockThreadedIO();
9677 }
9678 return 1;
9679 }
9680
9681 /* Preload keys for any command with first, last and step values for
9682 * the command keys prototype, as defined in the command table. */
9683 static void waitForMultipleSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
9684 int j, last;
9685 if (cmd->vm_firstkey == 0) return;
9686 last = cmd->vm_lastkey;
9687 if (last < 0) last = argc+last;
9688 for (j = cmd->vm_firstkey; j <= last; j += cmd->vm_keystep) {
9689 redisAssert(j < argc);
9690 waitForSwappedKey(c,argv[j]);
9691 }
9692 }
9693
9694 /* Preload keys needed for the ZUNIONSTORE and ZINTERSTORE commands.
9695 * Note that the number of keys to preload is user-defined, so we need to
9696 * apply a sanity check against argc. */
9697 static void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
9698 int i, num;
9699 REDIS_NOTUSED(cmd);
9700
9701 num = atoi(argv[2]->ptr);
9702 if (num > (argc-3)) return;
9703 for (i = 0; i < num; i++) {
9704 waitForSwappedKey(c,argv[3+i]);
9705 }
9706 }
9707
9708 /* Preload keys needed to execute the entire MULTI/EXEC block.
9709 *
9710 * This function is called by blockClientOnSwappedKeys when EXEC is issued,
9711 * and will block the client when any command requires a swapped out value. */
9712 static void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
9713 int i, margc;
9714 struct redisCommand *mcmd;
9715 robj **margv;
9716 REDIS_NOTUSED(cmd);
9717 REDIS_NOTUSED(argc);
9718 REDIS_NOTUSED(argv);
9719
9720 if (!(c->flags & REDIS_MULTI)) return;
9721 for (i = 0; i < c->mstate.count; i++) {
9722 mcmd = c->mstate.commands[i].cmd;
9723 margc = c->mstate.commands[i].argc;
9724 margv = c->mstate.commands[i].argv;
9725
9726 if (mcmd->vm_preload_proc != NULL) {
9727 mcmd->vm_preload_proc(c,mcmd,margc,margv);
9728 } else {
9729 waitForMultipleSwappedKeys(c,mcmd,margc,margv);
9730 }
9731 }
9732 }
9733
9734 /* Is this client attempting to run a command against swapped keys?
9735 * If so, block it ASAP, load the keys in background, then resume it.
9736 *
9737 * The important idea about this function is that it can fail! If keys will
9738 * still be swapped when the client is resumed, this key lookups will
9739 * just block loading keys from disk. In practical terms this should only
9740 * happen with SORT BY command or if there is a bug in this function.
9741 *
9742 * Return 1 if the client is marked as blocked, 0 if the client can
9743 * continue as the keys it is going to access appear to be in memory. */
9744 static int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) {
9745 if (cmd->vm_preload_proc != NULL) {
9746 cmd->vm_preload_proc(c,cmd,c->argc,c->argv);
9747 } else {
9748 waitForMultipleSwappedKeys(c,cmd,c->argc,c->argv);
9749 }
9750
9751 /* If the client was blocked for at least one key, mark it as blocked. */
9752 if (listLength(c->io_keys)) {
9753 c->flags |= REDIS_IO_WAIT;
9754 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
9755 server.vm_blocked_clients++;
9756 return 1;
9757 } else {
9758 return 0;
9759 }
9760 }
9761
9762 /* Remove the 'key' from the list of blocked keys for a given client.
9763 *
9764 * The function returns 1 when there are no longer blocking keys after
9765 * the current one was removed (and the client can be unblocked). */
9766 static int dontWaitForSwappedKey(redisClient *c, robj *key) {
9767 list *l;
9768 listNode *ln;
9769 listIter li;
9770 struct dictEntry *de;
9771
9772 /* Remove the key from the list of keys this client is waiting for. */
9773 listRewind(c->io_keys,&li);
9774 while ((ln = listNext(&li)) != NULL) {
9775 if (equalStringObjects(ln->value,key)) {
9776 listDelNode(c->io_keys,ln);
9777 break;
9778 }
9779 }
9780 assert(ln != NULL);
9781
9782 /* Remove the client form the key => waiting clients map. */
9783 de = dictFind(c->db->io_keys,key);
9784 assert(de != NULL);
9785 l = dictGetEntryVal(de);
9786 ln = listSearchKey(l,c);
9787 assert(ln != NULL);
9788 listDelNode(l,ln);
9789 if (listLength(l) == 0)
9790 dictDelete(c->db->io_keys,key);
9791
9792 return listLength(c->io_keys) == 0;
9793 }
9794
9795 static void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key) {
9796 struct dictEntry *de;
9797 list *l;
9798 listNode *ln;
9799 int len;
9800
9801 de = dictFind(db->io_keys,key);
9802 if (!de) return;
9803
9804 l = dictGetEntryVal(de);
9805 len = listLength(l);
9806 /* Note: we can't use something like while(listLength(l)) as the list
9807 * can be freed by the calling function when we remove the last element. */
9808 while (len--) {
9809 ln = listFirst(l);
9810 redisClient *c = ln->value;
9811
9812 if (dontWaitForSwappedKey(c,key)) {
9813 /* Put the client in the list of clients ready to go as we
9814 * loaded all the keys about it. */
9815 listAddNodeTail(server.io_ready_clients,c);
9816 }
9817 }
9818 }
9819
9820 /* =========================== Remote Configuration ========================= */
9821
9822 static void configSetCommand(redisClient *c) {
9823 robj *o = getDecodedObject(c->argv[3]);
9824 if (!strcasecmp(c->argv[2]->ptr,"dbfilename")) {
9825 zfree(server.dbfilename);
9826 server.dbfilename = zstrdup(o->ptr);
9827 } else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
9828 zfree(server.requirepass);
9829 server.requirepass = zstrdup(o->ptr);
9830 } else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
9831 zfree(server.masterauth);
9832 server.masterauth = zstrdup(o->ptr);
9833 } else if (!strcasecmp(c->argv[2]->ptr,"maxmemory")) {
9834 server.maxmemory = strtoll(o->ptr, NULL, 10);
9835 } else if (!strcasecmp(c->argv[2]->ptr,"appendfsync")) {
9836 if (!strcasecmp(o->ptr,"no")) {
9837 server.appendfsync = APPENDFSYNC_NO;
9838 } else if (!strcasecmp(o->ptr,"everysec")) {
9839 server.appendfsync = APPENDFSYNC_EVERYSEC;
9840 } else if (!strcasecmp(o->ptr,"always")) {
9841 server.appendfsync = APPENDFSYNC_ALWAYS;
9842 } else {
9843 goto badfmt;
9844 }
9845 } else if (!strcasecmp(c->argv[2]->ptr,"save")) {
9846 int vlen, j;
9847 sds *v = sdssplitlen(o->ptr,sdslen(o->ptr)," ",1,&vlen);
9848
9849 /* Perform sanity check before setting the new config:
9850 * - Even number of args
9851 * - Seconds >= 1, changes >= 0 */
9852 if (vlen & 1) {
9853 sdsfreesplitres(v,vlen);
9854 goto badfmt;
9855 }
9856 for (j = 0; j < vlen; j++) {
9857 char *eptr;
9858 long val;
9859
9860 val = strtoll(v[j], &eptr, 10);
9861 if (eptr[0] != '\0' ||
9862 ((j & 1) == 0 && val < 1) ||
9863 ((j & 1) == 1 && val < 0)) {
9864 sdsfreesplitres(v,vlen);
9865 goto badfmt;
9866 }
9867 }
9868 /* Finally set the new config */
9869 resetServerSaveParams();
9870 for (j = 0; j < vlen; j += 2) {
9871 time_t seconds;
9872 int changes;
9873
9874 seconds = strtoll(v[j],NULL,10);
9875 changes = strtoll(v[j+1],NULL,10);
9876 appendServerSaveParams(seconds, changes);
9877 }
9878 sdsfreesplitres(v,vlen);
9879 } else {
9880 addReplySds(c,sdscatprintf(sdsempty(),
9881 "-ERR not supported CONFIG parameter %s\r\n",
9882 (char*)c->argv[2]->ptr));
9883 decrRefCount(o);
9884 return;
9885 }
9886 decrRefCount(o);
9887 addReply(c,shared.ok);
9888 return;
9889
9890 badfmt: /* Bad format errors */
9891 addReplySds(c,sdscatprintf(sdsempty(),
9892 "-ERR invalid argument '%s' for CONFIG SET '%s'\r\n",
9893 (char*)o->ptr,
9894 (char*)c->argv[2]->ptr));
9895 decrRefCount(o);
9896 }
9897
9898 static void configGetCommand(redisClient *c) {
9899 robj *o = getDecodedObject(c->argv[2]);
9900 robj *lenobj = createObject(REDIS_STRING,NULL);
9901 char *pattern = o->ptr;
9902 int matches = 0;
9903
9904 addReply(c,lenobj);
9905 decrRefCount(lenobj);
9906
9907 if (stringmatch(pattern,"dbfilename",0)) {
9908 addReplyBulkCString(c,"dbfilename");
9909 addReplyBulkCString(c,server.dbfilename);
9910 matches++;
9911 }
9912 if (stringmatch(pattern,"requirepass",0)) {
9913 addReplyBulkCString(c,"requirepass");
9914 addReplyBulkCString(c,server.requirepass);
9915 matches++;
9916 }
9917 if (stringmatch(pattern,"masterauth",0)) {
9918 addReplyBulkCString(c,"masterauth");
9919 addReplyBulkCString(c,server.masterauth);
9920 matches++;
9921 }
9922 if (stringmatch(pattern,"maxmemory",0)) {
9923 char buf[128];
9924
9925 snprintf(buf,128,"%llu\n",server.maxmemory);
9926 addReplyBulkCString(c,"maxmemory");
9927 addReplyBulkCString(c,buf);
9928 matches++;
9929 }
9930 if (stringmatch(pattern,"appendfsync",0)) {
9931 char *policy;
9932
9933 switch(server.appendfsync) {
9934 case APPENDFSYNC_NO: policy = "no"; break;
9935 case APPENDFSYNC_EVERYSEC: policy = "everysec"; break;
9936 case APPENDFSYNC_ALWAYS: policy = "always"; break;
9937 default: policy = "unknown"; break; /* too harmless to panic */
9938 }
9939 addReplyBulkCString(c,"appendfsync");
9940 addReplyBulkCString(c,policy);
9941 matches++;
9942 }
9943 if (stringmatch(pattern,"save",0)) {
9944 sds buf = sdsempty();
9945 int j;
9946
9947 for (j = 0; j < server.saveparamslen; j++) {
9948 buf = sdscatprintf(buf,"%ld %d",
9949 server.saveparams[j].seconds,
9950 server.saveparams[j].changes);
9951 if (j != server.saveparamslen-1)
9952 buf = sdscatlen(buf," ",1);
9953 }
9954 addReplyBulkCString(c,"save");
9955 addReplyBulkCString(c,buf);
9956 sdsfree(buf);
9957 matches++;
9958 }
9959 decrRefCount(o);
9960 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",matches*2);
9961 }
9962
9963 static void configCommand(redisClient *c) {
9964 if (!strcasecmp(c->argv[1]->ptr,"set")) {
9965 if (c->argc != 4) goto badarity;
9966 configSetCommand(c);
9967 } else if (!strcasecmp(c->argv[1]->ptr,"get")) {
9968 if (c->argc != 3) goto badarity;
9969 configGetCommand(c);
9970 } else if (!strcasecmp(c->argv[1]->ptr,"resetstat")) {
9971 if (c->argc != 2) goto badarity;
9972 server.stat_numcommands = 0;
9973 server.stat_numconnections = 0;
9974 server.stat_expiredkeys = 0;
9975 server.stat_starttime = time(NULL);
9976 addReply(c,shared.ok);
9977 } else {
9978 addReplySds(c,sdscatprintf(sdsempty(),
9979 "-ERR CONFIG subcommand must be one of GET, SET, RESETSTAT\r\n"));
9980 }
9981 return;
9982
9983 badarity:
9984 addReplySds(c,sdscatprintf(sdsempty(),
9985 "-ERR Wrong number of arguments for CONFIG %s\r\n",
9986 (char*) c->argv[1]->ptr));
9987 }
9988
9989 /* =========================== Pubsub implementation ======================== */
9990
9991 static void freePubsubPattern(void *p) {
9992 pubsubPattern *pat = p;
9993
9994 decrRefCount(pat->pattern);
9995 zfree(pat);
9996 }
9997
9998 static int listMatchPubsubPattern(void *a, void *b) {
9999 pubsubPattern *pa = a, *pb = b;
10000
10001 return (pa->client == pb->client) &&
10002 (equalStringObjects(pa->pattern,pb->pattern));
10003 }
10004
10005 /* Subscribe a client to a channel. Returns 1 if the operation succeeded, or
10006 * 0 if the client was already subscribed to that channel. */
10007 static int pubsubSubscribeChannel(redisClient *c, robj *channel) {
10008 struct dictEntry *de;
10009 list *clients = NULL;
10010 int retval = 0;
10011
10012 /* Add the channel to the client -> channels hash table */
10013 if (dictAdd(c->pubsub_channels,channel,NULL) == DICT_OK) {
10014 retval = 1;
10015 incrRefCount(channel);
10016 /* Add the client to the channel -> list of clients hash table */
10017 de = dictFind(server.pubsub_channels,channel);
10018 if (de == NULL) {
10019 clients = listCreate();
10020 dictAdd(server.pubsub_channels,channel,clients);
10021 incrRefCount(channel);
10022 } else {
10023 clients = dictGetEntryVal(de);
10024 }
10025 listAddNodeTail(clients,c);
10026 }
10027 /* Notify the client */
10028 addReply(c,shared.mbulk3);
10029 addReply(c,shared.subscribebulk);
10030 addReplyBulk(c,channel);
10031 addReplyLongLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
10032 return retval;
10033 }
10034
10035 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10036 * 0 if the client was not subscribed to the specified channel. */
10037 static int pubsubUnsubscribeChannel(redisClient *c, robj *channel, int notify) {
10038 struct dictEntry *de;
10039 list *clients;
10040 listNode *ln;
10041 int retval = 0;
10042
10043 /* Remove the channel from the client -> channels hash table */
10044 incrRefCount(channel); /* channel may be just a pointer to the same object
10045 we have in the hash tables. Protect it... */
10046 if (dictDelete(c->pubsub_channels,channel) == DICT_OK) {
10047 retval = 1;
10048 /* Remove the client from the channel -> clients list hash table */
10049 de = dictFind(server.pubsub_channels,channel);
10050 assert(de != NULL);
10051 clients = dictGetEntryVal(de);
10052 ln = listSearchKey(clients,c);
10053 assert(ln != NULL);
10054 listDelNode(clients,ln);
10055 if (listLength(clients) == 0) {
10056 /* Free the list and associated hash entry at all if this was
10057 * the latest client, so that it will be possible to abuse
10058 * Redis PUBSUB creating millions of channels. */
10059 dictDelete(server.pubsub_channels,channel);
10060 }
10061 }
10062 /* Notify the client */
10063 if (notify) {
10064 addReply(c,shared.mbulk3);
10065 addReply(c,shared.unsubscribebulk);
10066 addReplyBulk(c,channel);
10067 addReplyLongLong(c,dictSize(c->pubsub_channels)+
10068 listLength(c->pubsub_patterns));
10069
10070 }
10071 decrRefCount(channel); /* it is finally safe to release it */
10072 return retval;
10073 }
10074
10075 /* Subscribe a client to a pattern. Returns 1 if the operation succeeded, or 0 if the clinet was already subscribed to that pattern. */
10076 static int pubsubSubscribePattern(redisClient *c, robj *pattern) {
10077 int retval = 0;
10078
10079 if (listSearchKey(c->pubsub_patterns,pattern) == NULL) {
10080 retval = 1;
10081 pubsubPattern *pat;
10082 listAddNodeTail(c->pubsub_patterns,pattern);
10083 incrRefCount(pattern);
10084 pat = zmalloc(sizeof(*pat));
10085 pat->pattern = getDecodedObject(pattern);
10086 pat->client = c;
10087 listAddNodeTail(server.pubsub_patterns,pat);
10088 }
10089 /* Notify the client */
10090 addReply(c,shared.mbulk3);
10091 addReply(c,shared.psubscribebulk);
10092 addReplyBulk(c,pattern);
10093 addReplyLongLong(c,dictSize(c->pubsub_channels)+listLength(c->pubsub_patterns));
10094 return retval;
10095 }
10096
10097 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10098 * 0 if the client was not subscribed to the specified channel. */
10099 static int pubsubUnsubscribePattern(redisClient *c, robj *pattern, int notify) {
10100 listNode *ln;
10101 pubsubPattern pat;
10102 int retval = 0;
10103
10104 incrRefCount(pattern); /* Protect the object. May be the same we remove */
10105 if ((ln = listSearchKey(c->pubsub_patterns,pattern)) != NULL) {
10106 retval = 1;
10107 listDelNode(c->pubsub_patterns,ln);
10108 pat.client = c;
10109 pat.pattern = pattern;
10110 ln = listSearchKey(server.pubsub_patterns,&pat);
10111 listDelNode(server.pubsub_patterns,ln);
10112 }
10113 /* Notify the client */
10114 if (notify) {
10115 addReply(c,shared.mbulk3);
10116 addReply(c,shared.punsubscribebulk);
10117 addReplyBulk(c,pattern);
10118 addReplyLongLong(c,dictSize(c->pubsub_channels)+
10119 listLength(c->pubsub_patterns));
10120 }
10121 decrRefCount(pattern);
10122 return retval;
10123 }
10124
10125 /* Unsubscribe from all the channels. Return the number of channels the
10126 * client was subscribed from. */
10127 static int pubsubUnsubscribeAllChannels(redisClient *c, int notify) {
10128 dictIterator *di = dictGetIterator(c->pubsub_channels);
10129 dictEntry *de;
10130 int count = 0;
10131
10132 while((de = dictNext(di)) != NULL) {
10133 robj *channel = dictGetEntryKey(de);
10134
10135 count += pubsubUnsubscribeChannel(c,channel,notify);
10136 }
10137 dictReleaseIterator(di);
10138 return count;
10139 }
10140
10141 /* Unsubscribe from all the patterns. Return the number of patterns the
10142 * client was subscribed from. */
10143 static int pubsubUnsubscribeAllPatterns(redisClient *c, int notify) {
10144 listNode *ln;
10145 listIter li;
10146 int count = 0;
10147
10148 listRewind(c->pubsub_patterns,&li);
10149 while ((ln = listNext(&li)) != NULL) {
10150 robj *pattern = ln->value;
10151
10152 count += pubsubUnsubscribePattern(c,pattern,notify);
10153 }
10154 return count;
10155 }
10156
10157 /* Publish a message */
10158 static int pubsubPublishMessage(robj *channel, robj *message) {
10159 int receivers = 0;
10160 struct dictEntry *de;
10161 listNode *ln;
10162 listIter li;
10163
10164 /* Send to clients listening for that channel */
10165 de = dictFind(server.pubsub_channels,channel);
10166 if (de) {
10167 list *list = dictGetEntryVal(de);
10168 listNode *ln;
10169 listIter li;
10170
10171 listRewind(list,&li);
10172 while ((ln = listNext(&li)) != NULL) {
10173 redisClient *c = ln->value;
10174
10175 addReply(c,shared.mbulk3);
10176 addReply(c,shared.messagebulk);
10177 addReplyBulk(c,channel);
10178 addReplyBulk(c,message);
10179 receivers++;
10180 }
10181 }
10182 /* Send to clients listening to matching channels */
10183 if (listLength(server.pubsub_patterns)) {
10184 listRewind(server.pubsub_patterns,&li);
10185 channel = getDecodedObject(channel);
10186 while ((ln = listNext(&li)) != NULL) {
10187 pubsubPattern *pat = ln->value;
10188
10189 if (stringmatchlen((char*)pat->pattern->ptr,
10190 sdslen(pat->pattern->ptr),
10191 (char*)channel->ptr,
10192 sdslen(channel->ptr),0)) {
10193 addReply(pat->client,shared.mbulk4);
10194 addReply(pat->client,shared.pmessagebulk);
10195 addReplyBulk(pat->client,pat->pattern);
10196 addReplyBulk(pat->client,channel);
10197 addReplyBulk(pat->client,message);
10198 receivers++;
10199 }
10200 }
10201 decrRefCount(channel);
10202 }
10203 return receivers;
10204 }
10205
10206 static void subscribeCommand(redisClient *c) {
10207 int j;
10208
10209 for (j = 1; j < c->argc; j++)
10210 pubsubSubscribeChannel(c,c->argv[j]);
10211 }
10212
10213 static void unsubscribeCommand(redisClient *c) {
10214 if (c->argc == 1) {
10215 pubsubUnsubscribeAllChannels(c,1);
10216 return;
10217 } else {
10218 int j;
10219
10220 for (j = 1; j < c->argc; j++)
10221 pubsubUnsubscribeChannel(c,c->argv[j],1);
10222 }
10223 }
10224
10225 static void psubscribeCommand(redisClient *c) {
10226 int j;
10227
10228 for (j = 1; j < c->argc; j++)
10229 pubsubSubscribePattern(c,c->argv[j]);
10230 }
10231
10232 static void punsubscribeCommand(redisClient *c) {
10233 if (c->argc == 1) {
10234 pubsubUnsubscribeAllPatterns(c,1);
10235 return;
10236 } else {
10237 int j;
10238
10239 for (j = 1; j < c->argc; j++)
10240 pubsubUnsubscribePattern(c,c->argv[j],1);
10241 }
10242 }
10243
10244 static void publishCommand(redisClient *c) {
10245 int receivers = pubsubPublishMessage(c->argv[1],c->argv[2]);
10246 addReplyLongLong(c,receivers);
10247 }
10248
10249 /* ================================= Debugging ============================== */
10250
10251 /* Compute the sha1 of string at 's' with 'len' bytes long.
10252 * The SHA1 is then xored againt the string pointed by digest.
10253 * Since xor is commutative, this operation is used in order to
10254 * "add" digests relative to unordered elements.
10255 *
10256 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
10257 static void xorDigest(unsigned char *digest, void *ptr, size_t len) {
10258 SHA1_CTX ctx;
10259 unsigned char hash[20], *s = ptr;
10260 int j;
10261
10262 SHA1Init(&ctx);
10263 SHA1Update(&ctx,s,len);
10264 SHA1Final(hash,&ctx);
10265
10266 for (j = 0; j < 20; j++)
10267 digest[j] ^= hash[j];
10268 }
10269
10270 static void xorObjectDigest(unsigned char *digest, robj *o) {
10271 o = getDecodedObject(o);
10272 xorDigest(digest,o->ptr,sdslen(o->ptr));
10273 decrRefCount(o);
10274 }
10275
10276 /* This function instead of just computing the SHA1 and xoring it
10277 * against diget, also perform the digest of "digest" itself and
10278 * replace the old value with the new one.
10279 *
10280 * So the final digest will be:
10281 *
10282 * digest = SHA1(digest xor SHA1(data))
10283 *
10284 * This function is used every time we want to preserve the order so
10285 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
10286 *
10287 * Also note that mixdigest("foo") followed by mixdigest("bar")
10288 * will lead to a different digest compared to "fo", "obar".
10289 */
10290 static void mixDigest(unsigned char *digest, void *ptr, size_t len) {
10291 SHA1_CTX ctx;
10292 char *s = ptr;
10293
10294 xorDigest(digest,s,len);
10295 SHA1Init(&ctx);
10296 SHA1Update(&ctx,digest,20);
10297 SHA1Final(digest,&ctx);
10298 }
10299
10300 static void mixObjectDigest(unsigned char *digest, robj *o) {
10301 o = getDecodedObject(o);
10302 mixDigest(digest,o->ptr,sdslen(o->ptr));
10303 decrRefCount(o);
10304 }
10305
10306 /* Compute the dataset digest. Since keys, sets elements, hashes elements
10307 * are not ordered, we use a trick: every aggregate digest is the xor
10308 * of the digests of their elements. This way the order will not change
10309 * the result. For list instead we use a feedback entering the output digest
10310 * as input in order to ensure that a different ordered list will result in
10311 * a different digest. */
10312 static void computeDatasetDigest(unsigned char *final) {
10313 unsigned char digest[20];
10314 char buf[128];
10315 dictIterator *di = NULL;
10316 dictEntry *de;
10317 int j;
10318 uint32_t aux;
10319
10320 memset(final,0,20); /* Start with a clean result */
10321
10322 for (j = 0; j < server.dbnum; j++) {
10323 redisDb *db = server.db+j;
10324
10325 if (dictSize(db->dict) == 0) continue;
10326 di = dictGetIterator(db->dict);
10327
10328 /* hash the DB id, so the same dataset moved in a different
10329 * DB will lead to a different digest */
10330 aux = htonl(j);
10331 mixDigest(final,&aux,sizeof(aux));
10332
10333 /* Iterate this DB writing every entry */
10334 while((de = dictNext(di)) != NULL) {
10335 robj *key, *o;
10336 time_t expiretime;
10337
10338 memset(digest,0,20); /* This key-val digest */
10339 key = dictGetEntryKey(de);
10340 mixObjectDigest(digest,key);
10341 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
10342 key->storage == REDIS_VM_SWAPPING) {
10343 o = dictGetEntryVal(de);
10344 incrRefCount(o);
10345 } else {
10346 o = vmPreviewObject(key);
10347 }
10348 aux = htonl(o->type);
10349 mixDigest(digest,&aux,sizeof(aux));
10350 expiretime = getExpire(db,key);
10351
10352 /* Save the key and associated value */
10353 if (o->type == REDIS_STRING) {
10354 mixObjectDigest(digest,o);
10355 } else if (o->type == REDIS_LIST) {
10356 list *list = o->ptr;
10357 listNode *ln;
10358 listIter li;
10359
10360 listRewind(list,&li);
10361 while((ln = listNext(&li))) {
10362 robj *eleobj = listNodeValue(ln);
10363
10364 mixObjectDigest(digest,eleobj);
10365 }
10366 } else if (o->type == REDIS_SET) {
10367 dict *set = o->ptr;
10368 dictIterator *di = dictGetIterator(set);
10369 dictEntry *de;
10370
10371 while((de = dictNext(di)) != NULL) {
10372 robj *eleobj = dictGetEntryKey(de);
10373
10374 xorObjectDigest(digest,eleobj);
10375 }
10376 dictReleaseIterator(di);
10377 } else if (o->type == REDIS_ZSET) {
10378 zset *zs = o->ptr;
10379 dictIterator *di = dictGetIterator(zs->dict);
10380 dictEntry *de;
10381
10382 while((de = dictNext(di)) != NULL) {
10383 robj *eleobj = dictGetEntryKey(de);
10384 double *score = dictGetEntryVal(de);
10385 unsigned char eledigest[20];
10386
10387 snprintf(buf,sizeof(buf),"%.17g",*score);
10388 memset(eledigest,0,20);
10389 mixObjectDigest(eledigest,eleobj);
10390 mixDigest(eledigest,buf,strlen(buf));
10391 xorDigest(digest,eledigest,20);
10392 }
10393 dictReleaseIterator(di);
10394 } else if (o->type == REDIS_HASH) {
10395 hashIterator *hi;
10396 robj *obj;
10397
10398 hi = hashInitIterator(o);
10399 while (hashNext(hi) != REDIS_ERR) {
10400 unsigned char eledigest[20];
10401
10402 memset(eledigest,0,20);
10403 obj = hashCurrent(hi,REDIS_HASH_KEY);
10404 mixObjectDigest(eledigest,obj);
10405 decrRefCount(obj);
10406 obj = hashCurrent(hi,REDIS_HASH_VALUE);
10407 mixObjectDigest(eledigest,obj);
10408 decrRefCount(obj);
10409 xorDigest(digest,eledigest,20);
10410 }
10411 hashReleaseIterator(hi);
10412 } else {
10413 redisPanic("Unknown object type");
10414 }
10415 decrRefCount(o);
10416 /* If the key has an expire, add it to the mix */
10417 if (expiretime != -1) xorDigest(digest,"!!expire!!",10);
10418 /* We can finally xor the key-val digest to the final digest */
10419 xorDigest(final,digest,20);
10420 }
10421 dictReleaseIterator(di);
10422 }
10423 }
10424
10425 static void debugCommand(redisClient *c) {
10426 if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
10427 *((char*)-1) = 'x';
10428 } else if (!strcasecmp(c->argv[1]->ptr,"reload")) {
10429 if (rdbSave(server.dbfilename) != REDIS_OK) {
10430 addReply(c,shared.err);
10431 return;
10432 }
10433 emptyDb();
10434 if (rdbLoad(server.dbfilename) != REDIS_OK) {
10435 addReply(c,shared.err);
10436 return;
10437 }
10438 redisLog(REDIS_WARNING,"DB reloaded by DEBUG RELOAD");
10439 addReply(c,shared.ok);
10440 } else if (!strcasecmp(c->argv[1]->ptr,"loadaof")) {
10441 emptyDb();
10442 if (loadAppendOnlyFile(server.appendfilename) != REDIS_OK) {
10443 addReply(c,shared.err);
10444 return;
10445 }
10446 redisLog(REDIS_WARNING,"Append Only File loaded by DEBUG LOADAOF");
10447 addReply(c,shared.ok);
10448 } else if (!strcasecmp(c->argv[1]->ptr,"object") && c->argc == 3) {
10449 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
10450 robj *key, *val;
10451
10452 if (!de) {
10453 addReply(c,shared.nokeyerr);
10454 return;
10455 }
10456 key = dictGetEntryKey(de);
10457 val = dictGetEntryVal(de);
10458 if (!server.vm_enabled || (key->storage == REDIS_VM_MEMORY ||
10459 key->storage == REDIS_VM_SWAPPING)) {
10460 char *strenc;
10461 char buf[128];
10462
10463 if (val->encoding < (sizeof(strencoding)/sizeof(char*))) {
10464 strenc = strencoding[val->encoding];
10465 } else {
10466 snprintf(buf,64,"unknown encoding %d\n", val->encoding);
10467 strenc = buf;
10468 }
10469 addReplySds(c,sdscatprintf(sdsempty(),
10470 "+Key at:%p refcount:%d, value at:%p refcount:%d "
10471 "encoding:%s serializedlength:%lld\r\n",
10472 (void*)key, key->refcount, (void*)val, val->refcount,
10473 strenc, (long long) rdbSavedObjectLen(val,NULL)));
10474 } else {
10475 addReplySds(c,sdscatprintf(sdsempty(),
10476 "+Key at:%p refcount:%d, value swapped at: page %llu "
10477 "using %llu pages\r\n",
10478 (void*)key, key->refcount, (unsigned long long) key->vm.page,
10479 (unsigned long long) key->vm.usedpages));
10480 }
10481 } else if (!strcasecmp(c->argv[1]->ptr,"swapin") && c->argc == 3) {
10482 lookupKeyRead(c->db,c->argv[2]);
10483 addReply(c,shared.ok);
10484 } else if (!strcasecmp(c->argv[1]->ptr,"swapout") && c->argc == 3) {
10485 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
10486 robj *key, *val;
10487
10488 if (!server.vm_enabled) {
10489 addReplySds(c,sdsnew("-ERR Virtual Memory is disabled\r\n"));
10490 return;
10491 }
10492 if (!de) {
10493 addReply(c,shared.nokeyerr);
10494 return;
10495 }
10496 key = dictGetEntryKey(de);
10497 val = dictGetEntryVal(de);
10498 /* If the key is shared we want to create a copy */
10499 if (key->refcount > 1) {
10500 robj *newkey = dupStringObject(key);
10501 decrRefCount(key);
10502 key = dictGetEntryKey(de) = newkey;
10503 }
10504 /* Swap it */
10505 if (key->storage != REDIS_VM_MEMORY) {
10506 addReplySds(c,sdsnew("-ERR This key is not in memory\r\n"));
10507 } else if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
10508 dictGetEntryVal(de) = NULL;
10509 addReply(c,shared.ok);
10510 } else {
10511 addReply(c,shared.err);
10512 }
10513 } else if (!strcasecmp(c->argv[1]->ptr,"populate") && c->argc == 3) {
10514 long keys, j;
10515 robj *key, *val;
10516 char buf[128];
10517
10518 if (getLongFromObjectOrReply(c, c->argv[2], &keys, NULL) != REDIS_OK)
10519 return;
10520 for (j = 0; j < keys; j++) {
10521 snprintf(buf,sizeof(buf),"key:%lu",j);
10522 key = createStringObject(buf,strlen(buf));
10523 if (lookupKeyRead(c->db,key) != NULL) {
10524 decrRefCount(key);
10525 continue;
10526 }
10527 snprintf(buf,sizeof(buf),"value:%lu",j);
10528 val = createStringObject(buf,strlen(buf));
10529 dictAdd(c->db->dict,key,val);
10530 }
10531 addReply(c,shared.ok);
10532 } else if (!strcasecmp(c->argv[1]->ptr,"digest") && c->argc == 2) {
10533 unsigned char digest[20];
10534 sds d = sdsnew("+");
10535 int j;
10536
10537 computeDatasetDigest(digest);
10538 for (j = 0; j < 20; j++)
10539 d = sdscatprintf(d, "%02x",digest[j]);
10540
10541 d = sdscatlen(d,"\r\n",2);
10542 addReplySds(c,d);
10543 } else {
10544 addReplySds(c,sdsnew(
10545 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]\r\n"));
10546 }
10547 }
10548
10549 static void _redisAssert(char *estr, char *file, int line) {
10550 redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
10551 redisLog(REDIS_WARNING,"==> %s:%d '%s' is not true\n",file,line,estr);
10552 #ifdef HAVE_BACKTRACE
10553 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
10554 *((char*)-1) = 'x';
10555 #endif
10556 }
10557
10558 static void _redisPanic(char *msg, char *file, int line) {
10559 redisLog(REDIS_WARNING,"!!! Software Failure. Press left mouse button to continue");
10560 redisLog(REDIS_WARNING,"Guru Meditation: %s #%s:%d",msg,file,line);
10561 #ifdef HAVE_BACKTRACE
10562 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
10563 *((char*)-1) = 'x';
10564 #endif
10565 }
10566
10567 /* =================================== Main! ================================ */
10568
10569 #ifdef __linux__
10570 int linuxOvercommitMemoryValue(void) {
10571 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
10572 char buf[64];
10573
10574 if (!fp) return -1;
10575 if (fgets(buf,64,fp) == NULL) {
10576 fclose(fp);
10577 return -1;
10578 }
10579 fclose(fp);
10580
10581 return atoi(buf);
10582 }
10583
10584 void linuxOvercommitMemoryWarning(void) {
10585 if (linuxOvercommitMemoryValue() == 0) {
10586 redisLog(REDIS_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.");
10587 }
10588 }
10589 #endif /* __linux__ */
10590
10591 static void daemonize(void) {
10592 int fd;
10593 FILE *fp;
10594
10595 if (fork() != 0) exit(0); /* parent exits */
10596 setsid(); /* create a new session */
10597
10598 /* Every output goes to /dev/null. If Redis is daemonized but
10599 * the 'logfile' is set to 'stdout' in the configuration file
10600 * it will not log at all. */
10601 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
10602 dup2(fd, STDIN_FILENO);
10603 dup2(fd, STDOUT_FILENO);
10604 dup2(fd, STDERR_FILENO);
10605 if (fd > STDERR_FILENO) close(fd);
10606 }
10607 /* Try to write the pid file */
10608 fp = fopen(server.pidfile,"w");
10609 if (fp) {
10610 fprintf(fp,"%d\n",getpid());
10611 fclose(fp);
10612 }
10613 }
10614
10615 static void version() {
10616 printf("Redis server version %s\n", REDIS_VERSION);
10617 exit(0);
10618 }
10619
10620 static void usage() {
10621 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf]\n");
10622 fprintf(stderr," ./redis-server - (read config from stdin)\n");
10623 exit(1);
10624 }
10625
10626 int main(int argc, char **argv) {
10627 time_t start;
10628
10629 initServerConfig();
10630 if (argc == 2) {
10631 if (strcmp(argv[1], "-v") == 0 ||
10632 strcmp(argv[1], "--version") == 0) version();
10633 if (strcmp(argv[1], "--help") == 0) usage();
10634 resetServerSaveParams();
10635 loadServerConfig(argv[1]);
10636 } else if ((argc > 2)) {
10637 usage();
10638 } else {
10639 redisLog(REDIS_WARNING,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
10640 }
10641 if (server.daemonize) daemonize();
10642 initServer();
10643 redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
10644 #ifdef __linux__
10645 linuxOvercommitMemoryWarning();
10646 #endif
10647 start = time(NULL);
10648 if (server.appendonly) {
10649 if (loadAppendOnlyFile(server.appendfilename) == REDIS_OK)
10650 redisLog(REDIS_NOTICE,"DB loaded from append only file: %ld seconds",time(NULL)-start);
10651 } else {
10652 if (rdbLoad(server.dbfilename) == REDIS_OK)
10653 redisLog(REDIS_NOTICE,"DB loaded from disk: %ld seconds",time(NULL)-start);
10654 }
10655 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
10656 aeSetBeforeSleepProc(server.el,beforeSleep);
10657 aeMain(server.el);
10658 aeDeleteEventLoop(server.el);
10659 return 0;
10660 }
10661
10662 /* ============================= Backtrace support ========================= */
10663
10664 #ifdef HAVE_BACKTRACE
10665 static char *findFuncName(void *pointer, unsigned long *offset);
10666
10667 static void *getMcontextEip(ucontext_t *uc) {
10668 #if defined(__FreeBSD__)
10669 return (void*) uc->uc_mcontext.mc_eip;
10670 #elif defined(__dietlibc__)
10671 return (void*) uc->uc_mcontext.eip;
10672 #elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
10673 #if __x86_64__
10674 return (void*) uc->uc_mcontext->__ss.__rip;
10675 #else
10676 return (void*) uc->uc_mcontext->__ss.__eip;
10677 #endif
10678 #elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
10679 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
10680 return (void*) uc->uc_mcontext->__ss.__rip;
10681 #else
10682 return (void*) uc->uc_mcontext->__ss.__eip;
10683 #endif
10684 #elif defined(__i386__) || defined(__X86_64__) || defined(__x86_64__)
10685 return (void*) uc->uc_mcontext.gregs[REG_EIP]; /* Linux 32/64 bit */
10686 #elif defined(__ia64__) /* Linux IA64 */
10687 return (void*) uc->uc_mcontext.sc_ip;
10688 #else
10689 return NULL;
10690 #endif
10691 }
10692
10693 static void segvHandler(int sig, siginfo_t *info, void *secret) {
10694 void *trace[100];
10695 char **messages = NULL;
10696 int i, trace_size = 0;
10697 unsigned long offset=0;
10698 ucontext_t *uc = (ucontext_t*) secret;
10699 sds infostring;
10700 REDIS_NOTUSED(info);
10701
10702 redisLog(REDIS_WARNING,
10703 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION, sig);
10704 infostring = genRedisInfoString();
10705 redisLog(REDIS_WARNING, "%s",infostring);
10706 /* It's not safe to sdsfree() the returned string under memory
10707 * corruption conditions. Let it leak as we are going to abort */
10708
10709 trace_size = backtrace(trace, 100);
10710 /* overwrite sigaction with caller's address */
10711 if (getMcontextEip(uc) != NULL) {
10712 trace[1] = getMcontextEip(uc);
10713 }
10714 messages = backtrace_symbols(trace, trace_size);
10715
10716 for (i=1; i<trace_size; ++i) {
10717 char *fn = findFuncName(trace[i], &offset), *p;
10718
10719 p = strchr(messages[i],'+');
10720 if (!fn || (p && ((unsigned long)strtol(p+1,NULL,10)) < offset)) {
10721 redisLog(REDIS_WARNING,"%s", messages[i]);
10722 } else {
10723 redisLog(REDIS_WARNING,"%d redis-server %p %s + %d", i, trace[i], fn, (unsigned int)offset);
10724 }
10725 }
10726 /* free(messages); Don't call free() with possibly corrupted memory. */
10727 _exit(0);
10728 }
10729
10730 static void setupSigSegvAction(void) {
10731 struct sigaction act;
10732
10733 sigemptyset (&act.sa_mask);
10734 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
10735 * is used. Otherwise, sa_handler is used */
10736 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
10737 act.sa_sigaction = segvHandler;
10738 sigaction (SIGSEGV, &act, NULL);
10739 sigaction (SIGBUS, &act, NULL);
10740 sigaction (SIGFPE, &act, NULL);
10741 sigaction (SIGILL, &act, NULL);
10742 sigaction (SIGBUS, &act, NULL);
10743 return;
10744 }
10745
10746 #include "staticsymbols.h"
10747 /* This function try to convert a pointer into a function name. It's used in
10748 * oreder to provide a backtrace under segmentation fault that's able to
10749 * display functions declared as static (otherwise the backtrace is useless). */
10750 static char *findFuncName(void *pointer, unsigned long *offset){
10751 int i, ret = -1;
10752 unsigned long off, minoff = 0;
10753
10754 /* Try to match against the Symbol with the smallest offset */
10755 for (i=0; symsTable[i].pointer; i++) {
10756 unsigned long lp = (unsigned long) pointer;
10757
10758 if (lp != (unsigned long)-1 && lp >= symsTable[i].pointer) {
10759 off=lp-symsTable[i].pointer;
10760 if (ret < 0 || off < minoff) {
10761 minoff=off;
10762 ret=i;
10763 }
10764 }
10765 }
10766 if (ret == -1) return NULL;
10767 *offset = minoff;
10768 return symsTable[ret].name;
10769 }
10770 #else /* HAVE_BACKTRACE */
10771 static void setupSigSegvAction(void) {
10772 }
10773 #endif /* HAVE_BACKTRACE */
10774
10775
10776
10777 /* The End */
10778
10779
10780