]> git.saurik.com Git - redis.git/blob - redis.c
More work on non-blocking VM. Should work in a few days
[redis.git] / redis.c
1 /*
2 * Copyright (c) 2006-2009, 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.2"
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 #define __USE_POSIX199309
41 #include <signal.h>
42
43 #ifdef HAVE_BACKTRACE
44 #include <execinfo.h>
45 #include <ucontext.h>
46 #endif /* HAVE_BACKTRACE */
47
48 #include <sys/wait.h>
49 #include <errno.h>
50 #include <assert.h>
51 #include <ctype.h>
52 #include <stdarg.h>
53 #include <inttypes.h>
54 #include <arpa/inet.h>
55 #include <sys/stat.h>
56 #include <fcntl.h>
57 #include <sys/time.h>
58 #include <sys/resource.h>
59 #include <sys/uio.h>
60 #include <limits.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
78 /* Error codes */
79 #define REDIS_OK 0
80 #define REDIS_ERR -1
81
82 /* Static server configuration */
83 #define REDIS_SERVERPORT 6379 /* TCP port */
84 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
85 #define REDIS_IOBUF_LEN 1024
86 #define REDIS_LOADBUF_LEN 1024
87 #define REDIS_STATIC_ARGS 4
88 #define REDIS_DEFAULT_DBNUM 16
89 #define REDIS_CONFIGLINE_MAX 1024
90 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
91 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
92 #define REDIS_EXPIRELOOKUPS_PER_CRON 100 /* try to expire 100 keys/second */
93 #define REDIS_MAX_WRITE_PER_EVENT (1024*64)
94 #define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
95
96 /* If more then REDIS_WRITEV_THRESHOLD write packets are pending use writev */
97 #define REDIS_WRITEV_THRESHOLD 3
98 /* Max number of iovecs used for each writev call */
99 #define REDIS_WRITEV_IOVEC_COUNT 256
100
101 /* Hash table parameters */
102 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
103
104 /* Command flags */
105 #define REDIS_CMD_BULK 1 /* Bulk write command */
106 #define REDIS_CMD_INLINE 2 /* Inline command */
107 /* REDIS_CMD_DENYOOM reserves a longer comment: all the commands marked with
108 this flags will return an error when the 'maxmemory' option is set in the
109 config file and the server is using more than maxmemory bytes of memory.
110 In short this commands are denied on low memory conditions. */
111 #define REDIS_CMD_DENYOOM 4
112
113 /* Object types */
114 #define REDIS_STRING 0
115 #define REDIS_LIST 1
116 #define REDIS_SET 2
117 #define REDIS_ZSET 3
118 #define REDIS_HASH 4
119
120 /* Objects encoding */
121 #define REDIS_ENCODING_RAW 0 /* Raw representation */
122 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
123
124 /* Object types only used for dumping to disk */
125 #define REDIS_EXPIRETIME 253
126 #define REDIS_SELECTDB 254
127 #define REDIS_EOF 255
128
129 /* Defines related to the dump file format. To store 32 bits lengths for short
130 * keys requires a lot of space, so we check the most significant 2 bits of
131 * the first byte to interpreter the length:
132 *
133 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
134 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
135 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
136 * 11|000000 this means: specially encoded object will follow. The six bits
137 * number specify the kind of object that follows.
138 * See the REDIS_RDB_ENC_* defines.
139 *
140 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
141 * values, will fit inside. */
142 #define REDIS_RDB_6BITLEN 0
143 #define REDIS_RDB_14BITLEN 1
144 #define REDIS_RDB_32BITLEN 2
145 #define REDIS_RDB_ENCVAL 3
146 #define REDIS_RDB_LENERR UINT_MAX
147
148 /* When a length of a string object stored on disk has the first two bits
149 * set, the remaining two bits specify a special encoding for the object
150 * accordingly to the following defines: */
151 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
152 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
153 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
154 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
155
156 /* Virtual memory object->where field. */
157 #define REDIS_VM_MEMORY 0 /* The object is on memory */
158 #define REDIS_VM_SWAPPED 1 /* The object is on disk */
159 #define REDIS_VM_SWAPPING 2 /* Redis is swapping this object on disk */
160 #define REDIS_VM_LOADING 3 /* Redis is loading this object from disk */
161
162 /* Virtual memory static configuration stuff.
163 * Check vmFindContiguousPages() to know more about this magic numbers. */
164 #define REDIS_VM_MAX_NEAR_PAGES 65536
165 #define REDIS_VM_MAX_RANDOM_JUMP 4096
166 #define REDIS_VM_MAX_THREADS 32
167
168 /* Client flags */
169 #define REDIS_CLOSE 1 /* This client connection should be closed ASAP */
170 #define REDIS_SLAVE 2 /* This client is a slave server */
171 #define REDIS_MASTER 4 /* This client is a master server */
172 #define REDIS_MONITOR 8 /* This client is a slave monitor, see MONITOR */
173 #define REDIS_MULTI 16 /* This client is in a MULTI context */
174 #define REDIS_BLOCKED 32 /* The client is waiting in a blocking operation */
175 #define REDIS_IO_WAIT 64 /* The client is waiting for Virtual Memory I/O */
176
177 /* Slave replication state - slave side */
178 #define REDIS_REPL_NONE 0 /* No active replication */
179 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
180 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
181
182 /* Slave replication state - from the point of view of master
183 * Note that in SEND_BULK and ONLINE state the slave receives new updates
184 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
185 * to start the next background saving in order to send updates to it. */
186 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
187 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
188 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
189 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
190
191 /* List related stuff */
192 #define REDIS_HEAD 0
193 #define REDIS_TAIL 1
194
195 /* Sort operations */
196 #define REDIS_SORT_GET 0
197 #define REDIS_SORT_ASC 1
198 #define REDIS_SORT_DESC 2
199 #define REDIS_SORTKEY_MAX 1024
200
201 /* Log levels */
202 #define REDIS_DEBUG 0
203 #define REDIS_VERBOSE 1
204 #define REDIS_NOTICE 2
205 #define REDIS_WARNING 3
206
207 /* Anti-warning macro... */
208 #define REDIS_NOTUSED(V) ((void) V)
209
210 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
211 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
212
213 /* Append only defines */
214 #define APPENDFSYNC_NO 0
215 #define APPENDFSYNC_ALWAYS 1
216 #define APPENDFSYNC_EVERYSEC 2
217
218 /* We can print the stacktrace, so our assert is defined this way: */
219 #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e),exit(1)))
220 static void _redisAssert(char *estr);
221
222 /*================================= Data types ============================== */
223
224 /* A redis object, that is a type able to hold a string / list / set */
225
226 /* The VM object structure */
227 struct redisObjectVM {
228 off_t page; /* the page at witch the object is stored on disk */
229 off_t usedpages; /* number of pages used on disk */
230 time_t atime; /* Last access time */
231 } vm;
232
233 /* The actual Redis Object */
234 typedef struct redisObject {
235 void *ptr;
236 unsigned char type;
237 unsigned char encoding;
238 unsigned char storage; /* If this object is a key, where is the value?
239 * REDIS_VM_MEMORY, REDIS_VM_SWAPPED, ... */
240 unsigned char vtype; /* If this object is a key, and value is swapped out,
241 * this is the type of the swapped out object. */
242 int refcount;
243 /* VM fields, this are only allocated if VM is active, otherwise the
244 * object allocation function will just allocate
245 * sizeof(redisObjct) minus sizeof(redisObjectVM), so using
246 * Redis without VM active will not have any overhead. */
247 struct redisObjectVM vm;
248 } robj;
249
250 /* Macro used to initalize a Redis object allocated on the stack.
251 * Note that this macro is taken near the structure definition to make sure
252 * we'll update it when the structure is changed, to avoid bugs like
253 * bug #85 introduced exactly in this way. */
254 #define initStaticStringObject(_var,_ptr) do { \
255 _var.refcount = 1; \
256 _var.type = REDIS_STRING; \
257 _var.encoding = REDIS_ENCODING_RAW; \
258 _var.ptr = _ptr; \
259 if (server.vm_enabled) _var.storage = REDIS_VM_MEMORY; \
260 } while(0);
261
262 typedef struct redisDb {
263 dict *dict; /* The keyspace for this DB */
264 dict *expires; /* Timeout of keys with a timeout set */
265 dict *blockingkeys; /* Keys with clients waiting for data (BLPOP) */
266 int id;
267 } redisDb;
268
269 /* Client MULTI/EXEC state */
270 typedef struct multiCmd {
271 robj **argv;
272 int argc;
273 struct redisCommand *cmd;
274 } multiCmd;
275
276 typedef struct multiState {
277 multiCmd *commands; /* Array of MULTI commands */
278 int count; /* Total number of MULTI commands */
279 } multiState;
280
281 /* With multiplexing we need to take per-clinet state.
282 * Clients are taken in a liked list. */
283 typedef struct redisClient {
284 int fd;
285 redisDb *db;
286 int dictid;
287 sds querybuf;
288 robj **argv, **mbargv;
289 int argc, mbargc;
290 int bulklen; /* bulk read len. -1 if not in bulk read mode */
291 int multibulk; /* multi bulk command format active */
292 list *reply;
293 int sentlen;
294 time_t lastinteraction; /* time of the last interaction, used for timeout */
295 int flags; /* REDIS_CLOSE | REDIS_SLAVE | REDIS_MONITOR */
296 /* REDIS_MULTI */
297 int slaveseldb; /* slave selected db, if this client is a slave */
298 int authenticated; /* when requirepass is non-NULL */
299 int replstate; /* replication state if this is a slave */
300 int repldbfd; /* replication DB file descriptor */
301 long repldboff; /* replication DB file offset */
302 off_t repldbsize; /* replication DB file size */
303 multiState mstate; /* MULTI/EXEC state */
304 robj **blockingkeys; /* The key we waiting to terminate a blocking
305 * operation such as BLPOP. Otherwise NULL. */
306 int blockingkeysnum; /* Number of blocking keys */
307 time_t blockingto; /* Blocking operation timeout. If UNIX current time
308 * is >= blockingto then the operation timed out. */
309 list *io_keys; /* Keys this client is waiting to be loaded from the
310 * swap file in order to continue. */
311 } redisClient;
312
313 struct saveparam {
314 time_t seconds;
315 int changes;
316 };
317
318 /* Global server state structure */
319 struct redisServer {
320 int port;
321 int fd;
322 redisDb *db;
323 dict *sharingpool; /* Poll used for object sharing */
324 unsigned int sharingpoolsize;
325 long long dirty; /* changes to DB from the last save */
326 list *clients;
327 list *slaves, *monitors;
328 char neterr[ANET_ERR_LEN];
329 aeEventLoop *el;
330 int cronloops; /* number of times the cron function run */
331 list *objfreelist; /* A list of freed objects to avoid malloc() */
332 time_t lastsave; /* Unix time of last save succeeede */
333 size_t usedmemory; /* Used memory in megabytes */
334 /* Fields used only for stats */
335 time_t stat_starttime; /* server start time */
336 long long stat_numcommands; /* number of processed commands */
337 long long stat_numconnections; /* number of connections received */
338 /* Configuration */
339 int verbosity;
340 int glueoutputbuf;
341 int maxidletime;
342 int dbnum;
343 int daemonize;
344 int appendonly;
345 int appendfsync;
346 time_t lastfsync;
347 int appendfd;
348 int appendseldb;
349 char *pidfile;
350 pid_t bgsavechildpid;
351 pid_t bgrewritechildpid;
352 sds bgrewritebuf; /* buffer taken by parent during oppend only rewrite */
353 struct saveparam *saveparams;
354 int saveparamslen;
355 char *logfile;
356 char *bindaddr;
357 char *dbfilename;
358 char *appendfilename;
359 char *requirepass;
360 int shareobjects;
361 int rdbcompression;
362 /* Replication related */
363 int isslave;
364 char *masterauth;
365 char *masterhost;
366 int masterport;
367 redisClient *master; /* client that is master for this slave */
368 int replstate;
369 unsigned int maxclients;
370 unsigned long long maxmemory;
371 unsigned int blockedclients;
372 /* Sort parameters - qsort_r() is only available under BSD so we
373 * have to take this state global, in order to pass it to sortCompare() */
374 int sort_desc;
375 int sort_alpha;
376 int sort_bypattern;
377 /* Virtual memory configuration */
378 int vm_enabled;
379 off_t vm_page_size;
380 off_t vm_pages;
381 unsigned long long vm_max_memory;
382 /* Virtual memory state */
383 FILE *vm_fp;
384 int vm_fd;
385 off_t vm_next_page; /* Next probably empty page */
386 off_t vm_near_pages; /* Number of pages allocated sequentially */
387 unsigned char *vm_bitmap; /* Bitmap of free/used pages */
388 time_t unixtime; /* Unix time sampled every second. */
389 /* Virtual memory I/O threads stuff */
390 /* An I/O thread process an element taken from the io_jobs queue and
391 * put the result of the operation in the io_done list. While the
392 * job is being processed, it's put on io_processing queue. */
393 list *io_newjobs; /* List of VM I/O jobs yet to be processed */
394 list *io_processing; /* List of VM I/O jobs being processed */
395 list *io_processed; /* List of VM I/O jobs already processed */
396 list *io_clients; /* All the clients waiting for SWAP I/O operations */
397 pthread_mutex_t io_mutex; /* lock to access io_jobs/io_done/io_thread_job */
398 int io_active_threads; /* Number of running I/O threads */
399 int vm_max_threads; /* Max number of I/O threads running at the same time */
400 /* Our main thread is blocked on the event loop, locking for sockets ready
401 * to be read or written, so when a threaded I/O operation is ready to be
402 * processed by the main thread, the I/O thread will use a unix pipe to
403 * awake the main thread. The followings are the two pipe FDs. */
404 int io_ready_pipe_read;
405 int io_ready_pipe_write;
406 /* Virtual memory stats */
407 unsigned long long vm_stats_used_pages;
408 unsigned long long vm_stats_swapped_objects;
409 unsigned long long vm_stats_swapouts;
410 unsigned long long vm_stats_swapins;
411 FILE *devnull;
412 };
413
414 typedef void redisCommandProc(redisClient *c);
415 struct redisCommand {
416 char *name;
417 redisCommandProc *proc;
418 int arity;
419 int flags;
420 };
421
422 struct redisFunctionSym {
423 char *name;
424 unsigned long pointer;
425 };
426
427 typedef struct _redisSortObject {
428 robj *obj;
429 union {
430 double score;
431 robj *cmpobj;
432 } u;
433 } redisSortObject;
434
435 typedef struct _redisSortOperation {
436 int type;
437 robj *pattern;
438 } redisSortOperation;
439
440 /* ZSETs use a specialized version of Skiplists */
441
442 typedef struct zskiplistNode {
443 struct zskiplistNode **forward;
444 struct zskiplistNode *backward;
445 double score;
446 robj *obj;
447 } zskiplistNode;
448
449 typedef struct zskiplist {
450 struct zskiplistNode *header, *tail;
451 unsigned long length;
452 int level;
453 } zskiplist;
454
455 typedef struct zset {
456 dict *dict;
457 zskiplist *zsl;
458 } zset;
459
460 /* Our shared "common" objects */
461
462 struct sharedObjectsStruct {
463 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
464 *colon, *nullbulk, *nullmultibulk, *queued,
465 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
466 *outofrangeerr, *plus,
467 *select0, *select1, *select2, *select3, *select4,
468 *select5, *select6, *select7, *select8, *select9;
469 } shared;
470
471 /* Global vars that are actally used as constants. The following double
472 * values are used for double on-disk serialization, and are initialized
473 * at runtime to avoid strange compiler optimizations. */
474
475 static double R_Zero, R_PosInf, R_NegInf, R_Nan;
476
477 /* VM threaded I/O request message */
478 #define REDIS_IOJOB_LOAD 0 /* Load from disk to memory */
479 #define REDIS_IOJOB_PREPARE_SWAP 1 /* Compute needed pages */
480 #define REDIS_IOJOB_DO_SWAP 2 /* Swap from memory to disk */
481 typedef struct iojon {
482 int type; /* Request type, REDIS_IOJOB_* */
483 redisDb *db;/* Redis database */
484 robj *key; /* This I/O request is about swapping this key */
485 robj *val; /* the value to swap for REDIS_IOREQ_*_SWAP, otherwise this
486 * field is populated by the I/O thread for REDIS_IOREQ_LOAD. */
487 off_t page; /* Swap page where to read/write the object */
488 off_t pages; /* Swap pages needed to safe object. PREPARE_SWAP return val */
489 int canceled; /* True if this command was canceled by blocking side of VM */
490 pthread_t thread; /* ID of the thread processing this entry */
491 } iojob;
492
493 /*================================ Prototypes =============================== */
494
495 static void freeStringObject(robj *o);
496 static void freeListObject(robj *o);
497 static void freeSetObject(robj *o);
498 static void decrRefCount(void *o);
499 static robj *createObject(int type, void *ptr);
500 static void freeClient(redisClient *c);
501 static int rdbLoad(char *filename);
502 static void addReply(redisClient *c, robj *obj);
503 static void addReplySds(redisClient *c, sds s);
504 static void incrRefCount(robj *o);
505 static int rdbSaveBackground(char *filename);
506 static robj *createStringObject(char *ptr, size_t len);
507 static robj *dupStringObject(robj *o);
508 static void replicationFeedSlaves(list *slaves, struct redisCommand *cmd, int dictid, robj **argv, int argc);
509 static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);
510 static int syncWithMaster(void);
511 static robj *tryObjectSharing(robj *o);
512 static int tryObjectEncoding(robj *o);
513 static robj *getDecodedObject(robj *o);
514 static int removeExpire(redisDb *db, robj *key);
515 static int expireIfNeeded(redisDb *db, robj *key);
516 static int deleteIfVolatile(redisDb *db, robj *key);
517 static int deleteIfSwapped(redisDb *db, robj *key);
518 static int deleteKey(redisDb *db, robj *key);
519 static time_t getExpire(redisDb *db, robj *key);
520 static int setExpire(redisDb *db, robj *key, time_t when);
521 static void updateSlavesWaitingBgsave(int bgsaveerr);
522 static void freeMemoryIfNeeded(void);
523 static int processCommand(redisClient *c);
524 static void setupSigSegvAction(void);
525 static void rdbRemoveTempFile(pid_t childpid);
526 static void aofRemoveTempFile(pid_t childpid);
527 static size_t stringObjectLen(robj *o);
528 static void processInputBuffer(redisClient *c);
529 static zskiplist *zslCreate(void);
530 static void zslFree(zskiplist *zsl);
531 static void zslInsert(zskiplist *zsl, double score, robj *obj);
532 static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask);
533 static void initClientMultiState(redisClient *c);
534 static void freeClientMultiState(redisClient *c);
535 static void queueMultiCommand(redisClient *c, struct redisCommand *cmd);
536 static void unblockClient(redisClient *c);
537 static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele);
538 static void vmInit(void);
539 static void vmMarkPagesFree(off_t page, off_t count);
540 static robj *vmLoadObject(robj *key);
541 static robj *vmPreviewObject(robj *key);
542 static int vmSwapOneObjectBlocking(void);
543 static int vmSwapOneObjectThreaded(void);
544 static int vmCanSwapOut(void);
545 static void freeOneObjectFromFreelist(void);
546 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);
547 static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, int mask);
548 static void vmCancelThreadedIOJob(robj *o);
549 static void lockThreadedIO(void);
550 static void unlockThreadedIO(void);
551 static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db);
552 static void freeIOJob(iojob *j);
553 static void queueIOJob(iojob *j);
554
555 static void authCommand(redisClient *c);
556 static void pingCommand(redisClient *c);
557 static void echoCommand(redisClient *c);
558 static void setCommand(redisClient *c);
559 static void setnxCommand(redisClient *c);
560 static void getCommand(redisClient *c);
561 static void delCommand(redisClient *c);
562 static void existsCommand(redisClient *c);
563 static void incrCommand(redisClient *c);
564 static void decrCommand(redisClient *c);
565 static void incrbyCommand(redisClient *c);
566 static void decrbyCommand(redisClient *c);
567 static void selectCommand(redisClient *c);
568 static void randomkeyCommand(redisClient *c);
569 static void keysCommand(redisClient *c);
570 static void dbsizeCommand(redisClient *c);
571 static void lastsaveCommand(redisClient *c);
572 static void saveCommand(redisClient *c);
573 static void bgsaveCommand(redisClient *c);
574 static void bgrewriteaofCommand(redisClient *c);
575 static void shutdownCommand(redisClient *c);
576 static void moveCommand(redisClient *c);
577 static void renameCommand(redisClient *c);
578 static void renamenxCommand(redisClient *c);
579 static void lpushCommand(redisClient *c);
580 static void rpushCommand(redisClient *c);
581 static void lpopCommand(redisClient *c);
582 static void rpopCommand(redisClient *c);
583 static void llenCommand(redisClient *c);
584 static void lindexCommand(redisClient *c);
585 static void lrangeCommand(redisClient *c);
586 static void ltrimCommand(redisClient *c);
587 static void typeCommand(redisClient *c);
588 static void lsetCommand(redisClient *c);
589 static void saddCommand(redisClient *c);
590 static void sremCommand(redisClient *c);
591 static void smoveCommand(redisClient *c);
592 static void sismemberCommand(redisClient *c);
593 static void scardCommand(redisClient *c);
594 static void spopCommand(redisClient *c);
595 static void srandmemberCommand(redisClient *c);
596 static void sinterCommand(redisClient *c);
597 static void sinterstoreCommand(redisClient *c);
598 static void sunionCommand(redisClient *c);
599 static void sunionstoreCommand(redisClient *c);
600 static void sdiffCommand(redisClient *c);
601 static void sdiffstoreCommand(redisClient *c);
602 static void syncCommand(redisClient *c);
603 static void flushdbCommand(redisClient *c);
604 static void flushallCommand(redisClient *c);
605 static void sortCommand(redisClient *c);
606 static void lremCommand(redisClient *c);
607 static void rpoplpushcommand(redisClient *c);
608 static void infoCommand(redisClient *c);
609 static void mgetCommand(redisClient *c);
610 static void monitorCommand(redisClient *c);
611 static void expireCommand(redisClient *c);
612 static void expireatCommand(redisClient *c);
613 static void getsetCommand(redisClient *c);
614 static void ttlCommand(redisClient *c);
615 static void slaveofCommand(redisClient *c);
616 static void debugCommand(redisClient *c);
617 static void msetCommand(redisClient *c);
618 static void msetnxCommand(redisClient *c);
619 static void zaddCommand(redisClient *c);
620 static void zincrbyCommand(redisClient *c);
621 static void zrangeCommand(redisClient *c);
622 static void zrangebyscoreCommand(redisClient *c);
623 static void zrevrangeCommand(redisClient *c);
624 static void zcardCommand(redisClient *c);
625 static void zremCommand(redisClient *c);
626 static void zscoreCommand(redisClient *c);
627 static void zremrangebyscoreCommand(redisClient *c);
628 static void multiCommand(redisClient *c);
629 static void execCommand(redisClient *c);
630 static void blpopCommand(redisClient *c);
631 static void brpopCommand(redisClient *c);
632
633 /*================================= Globals ================================= */
634
635 /* Global vars */
636 static struct redisServer server; /* server global state */
637 static struct redisCommand cmdTable[] = {
638 {"get",getCommand,2,REDIS_CMD_INLINE},
639 {"set",setCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
640 {"setnx",setnxCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
641 {"del",delCommand,-2,REDIS_CMD_INLINE},
642 {"exists",existsCommand,2,REDIS_CMD_INLINE},
643 {"incr",incrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
644 {"decr",decrCommand,2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
645 {"mget",mgetCommand,-2,REDIS_CMD_INLINE},
646 {"rpush",rpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
647 {"lpush",lpushCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
648 {"rpop",rpopCommand,2,REDIS_CMD_INLINE},
649 {"lpop",lpopCommand,2,REDIS_CMD_INLINE},
650 {"brpop",brpopCommand,-3,REDIS_CMD_INLINE},
651 {"blpop",blpopCommand,-3,REDIS_CMD_INLINE},
652 {"llen",llenCommand,2,REDIS_CMD_INLINE},
653 {"lindex",lindexCommand,3,REDIS_CMD_INLINE},
654 {"lset",lsetCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
655 {"lrange",lrangeCommand,4,REDIS_CMD_INLINE},
656 {"ltrim",ltrimCommand,4,REDIS_CMD_INLINE},
657 {"lrem",lremCommand,4,REDIS_CMD_BULK},
658 {"rpoplpush",rpoplpushcommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
659 {"sadd",saddCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
660 {"srem",sremCommand,3,REDIS_CMD_BULK},
661 {"smove",smoveCommand,4,REDIS_CMD_BULK},
662 {"sismember",sismemberCommand,3,REDIS_CMD_BULK},
663 {"scard",scardCommand,2,REDIS_CMD_INLINE},
664 {"spop",spopCommand,2,REDIS_CMD_INLINE},
665 {"srandmember",srandmemberCommand,2,REDIS_CMD_INLINE},
666 {"sinter",sinterCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
667 {"sinterstore",sinterstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
668 {"sunion",sunionCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
669 {"sunionstore",sunionstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
670 {"sdiff",sdiffCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
671 {"sdiffstore",sdiffstoreCommand,-3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
672 {"smembers",sinterCommand,2,REDIS_CMD_INLINE},
673 {"zadd",zaddCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
674 {"zincrby",zincrbyCommand,4,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
675 {"zrem",zremCommand,3,REDIS_CMD_BULK},
676 {"zremrangebyscore",zremrangebyscoreCommand,4,REDIS_CMD_INLINE},
677 {"zrange",zrangeCommand,-4,REDIS_CMD_INLINE},
678 {"zrangebyscore",zrangebyscoreCommand,-4,REDIS_CMD_INLINE},
679 {"zrevrange",zrevrangeCommand,-4,REDIS_CMD_INLINE},
680 {"zcard",zcardCommand,2,REDIS_CMD_INLINE},
681 {"zscore",zscoreCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
682 {"incrby",incrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
683 {"decrby",decrbyCommand,3,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
684 {"getset",getsetCommand,3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
685 {"mset",msetCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
686 {"msetnx",msetnxCommand,-3,REDIS_CMD_BULK|REDIS_CMD_DENYOOM},
687 {"randomkey",randomkeyCommand,1,REDIS_CMD_INLINE},
688 {"select",selectCommand,2,REDIS_CMD_INLINE},
689 {"move",moveCommand,3,REDIS_CMD_INLINE},
690 {"rename",renameCommand,3,REDIS_CMD_INLINE},
691 {"renamenx",renamenxCommand,3,REDIS_CMD_INLINE},
692 {"expire",expireCommand,3,REDIS_CMD_INLINE},
693 {"expireat",expireatCommand,3,REDIS_CMD_INLINE},
694 {"keys",keysCommand,2,REDIS_CMD_INLINE},
695 {"dbsize",dbsizeCommand,1,REDIS_CMD_INLINE},
696 {"auth",authCommand,2,REDIS_CMD_INLINE},
697 {"ping",pingCommand,1,REDIS_CMD_INLINE},
698 {"echo",echoCommand,2,REDIS_CMD_BULK},
699 {"save",saveCommand,1,REDIS_CMD_INLINE},
700 {"bgsave",bgsaveCommand,1,REDIS_CMD_INLINE},
701 {"bgrewriteaof",bgrewriteaofCommand,1,REDIS_CMD_INLINE},
702 {"shutdown",shutdownCommand,1,REDIS_CMD_INLINE},
703 {"lastsave",lastsaveCommand,1,REDIS_CMD_INLINE},
704 {"type",typeCommand,2,REDIS_CMD_INLINE},
705 {"multi",multiCommand,1,REDIS_CMD_INLINE},
706 {"exec",execCommand,1,REDIS_CMD_INLINE},
707 {"sync",syncCommand,1,REDIS_CMD_INLINE},
708 {"flushdb",flushdbCommand,1,REDIS_CMD_INLINE},
709 {"flushall",flushallCommand,1,REDIS_CMD_INLINE},
710 {"sort",sortCommand,-2,REDIS_CMD_INLINE|REDIS_CMD_DENYOOM},
711 {"info",infoCommand,1,REDIS_CMD_INLINE},
712 {"monitor",monitorCommand,1,REDIS_CMD_INLINE},
713 {"ttl",ttlCommand,2,REDIS_CMD_INLINE},
714 {"slaveof",slaveofCommand,3,REDIS_CMD_INLINE},
715 {"debug",debugCommand,-2,REDIS_CMD_INLINE},
716 {NULL,NULL,0,0}
717 };
718
719 /*============================ Utility functions ============================ */
720
721 /* Glob-style pattern matching. */
722 int stringmatchlen(const char *pattern, int patternLen,
723 const char *string, int stringLen, int nocase)
724 {
725 while(patternLen) {
726 switch(pattern[0]) {
727 case '*':
728 while (pattern[1] == '*') {
729 pattern++;
730 patternLen--;
731 }
732 if (patternLen == 1)
733 return 1; /* match */
734 while(stringLen) {
735 if (stringmatchlen(pattern+1, patternLen-1,
736 string, stringLen, nocase))
737 return 1; /* match */
738 string++;
739 stringLen--;
740 }
741 return 0; /* no match */
742 break;
743 case '?':
744 if (stringLen == 0)
745 return 0; /* no match */
746 string++;
747 stringLen--;
748 break;
749 case '[':
750 {
751 int not, match;
752
753 pattern++;
754 patternLen--;
755 not = pattern[0] == '^';
756 if (not) {
757 pattern++;
758 patternLen--;
759 }
760 match = 0;
761 while(1) {
762 if (pattern[0] == '\\') {
763 pattern++;
764 patternLen--;
765 if (pattern[0] == string[0])
766 match = 1;
767 } else if (pattern[0] == ']') {
768 break;
769 } else if (patternLen == 0) {
770 pattern--;
771 patternLen++;
772 break;
773 } else if (pattern[1] == '-' && patternLen >= 3) {
774 int start = pattern[0];
775 int end = pattern[2];
776 int c = string[0];
777 if (start > end) {
778 int t = start;
779 start = end;
780 end = t;
781 }
782 if (nocase) {
783 start = tolower(start);
784 end = tolower(end);
785 c = tolower(c);
786 }
787 pattern += 2;
788 patternLen -= 2;
789 if (c >= start && c <= end)
790 match = 1;
791 } else {
792 if (!nocase) {
793 if (pattern[0] == string[0])
794 match = 1;
795 } else {
796 if (tolower((int)pattern[0]) == tolower((int)string[0]))
797 match = 1;
798 }
799 }
800 pattern++;
801 patternLen--;
802 }
803 if (not)
804 match = !match;
805 if (!match)
806 return 0; /* no match */
807 string++;
808 stringLen--;
809 break;
810 }
811 case '\\':
812 if (patternLen >= 2) {
813 pattern++;
814 patternLen--;
815 }
816 /* fall through */
817 default:
818 if (!nocase) {
819 if (pattern[0] != string[0])
820 return 0; /* no match */
821 } else {
822 if (tolower((int)pattern[0]) != tolower((int)string[0]))
823 return 0; /* no match */
824 }
825 string++;
826 stringLen--;
827 break;
828 }
829 pattern++;
830 patternLen--;
831 if (stringLen == 0) {
832 while(*pattern == '*') {
833 pattern++;
834 patternLen--;
835 }
836 break;
837 }
838 }
839 if (patternLen == 0 && stringLen == 0)
840 return 1;
841 return 0;
842 }
843
844 static void redisLog(int level, const char *fmt, ...) {
845 va_list ap;
846 FILE *fp;
847
848 fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
849 if (!fp) return;
850
851 va_start(ap, fmt);
852 if (level >= server.verbosity) {
853 char *c = ".-*";
854 char buf[64];
855 time_t now;
856
857 now = time(NULL);
858 strftime(buf,64,"%d %b %H:%M:%S",localtime(&now));
859 fprintf(fp,"%s %c ",buf,c[level]);
860 vfprintf(fp, fmt, ap);
861 fprintf(fp,"\n");
862 fflush(fp);
863 }
864 va_end(ap);
865
866 if (server.logfile) fclose(fp);
867 }
868
869 /*====================== Hash table type implementation ==================== */
870
871 /* This is an hash table type that uses the SDS dynamic strings libary as
872 * keys and radis objects as values (objects can hold SDS strings,
873 * lists, sets). */
874
875 static void dictVanillaFree(void *privdata, void *val)
876 {
877 DICT_NOTUSED(privdata);
878 zfree(val);
879 }
880
881 static void dictListDestructor(void *privdata, void *val)
882 {
883 DICT_NOTUSED(privdata);
884 listRelease((list*)val);
885 }
886
887 static int sdsDictKeyCompare(void *privdata, const void *key1,
888 const void *key2)
889 {
890 int l1,l2;
891 DICT_NOTUSED(privdata);
892
893 l1 = sdslen((sds)key1);
894 l2 = sdslen((sds)key2);
895 if (l1 != l2) return 0;
896 return memcmp(key1, key2, l1) == 0;
897 }
898
899 static void dictRedisObjectDestructor(void *privdata, void *val)
900 {
901 DICT_NOTUSED(privdata);
902
903 if (val == NULL) return; /* Values of swapped out keys as set to NULL */
904 decrRefCount(val);
905 }
906
907 static int dictObjKeyCompare(void *privdata, const void *key1,
908 const void *key2)
909 {
910 const robj *o1 = key1, *o2 = key2;
911 return sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
912 }
913
914 static unsigned int dictObjHash(const void *key) {
915 const robj *o = key;
916 return dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
917 }
918
919 static int dictEncObjKeyCompare(void *privdata, const void *key1,
920 const void *key2)
921 {
922 robj *o1 = (robj*) key1, *o2 = (robj*) key2;
923 int cmp;
924
925 o1 = getDecodedObject(o1);
926 o2 = getDecodedObject(o2);
927 cmp = sdsDictKeyCompare(privdata,o1->ptr,o2->ptr);
928 decrRefCount(o1);
929 decrRefCount(o2);
930 return cmp;
931 }
932
933 static unsigned int dictEncObjHash(const void *key) {
934 robj *o = (robj*) key;
935
936 o = getDecodedObject(o);
937 unsigned int hash = dictGenHashFunction(o->ptr, sdslen((sds)o->ptr));
938 decrRefCount(o);
939 return hash;
940 }
941
942 static dictType setDictType = {
943 dictEncObjHash, /* hash function */
944 NULL, /* key dup */
945 NULL, /* val dup */
946 dictEncObjKeyCompare, /* key compare */
947 dictRedisObjectDestructor, /* key destructor */
948 NULL /* val destructor */
949 };
950
951 static dictType zsetDictType = {
952 dictEncObjHash, /* hash function */
953 NULL, /* key dup */
954 NULL, /* val dup */
955 dictEncObjKeyCompare, /* key compare */
956 dictRedisObjectDestructor, /* key destructor */
957 dictVanillaFree /* val destructor of malloc(sizeof(double)) */
958 };
959
960 static dictType hashDictType = {
961 dictObjHash, /* hash function */
962 NULL, /* key dup */
963 NULL, /* val dup */
964 dictObjKeyCompare, /* key compare */
965 dictRedisObjectDestructor, /* key destructor */
966 dictRedisObjectDestructor /* val destructor */
967 };
968
969 /* Keylist hash table type has unencoded redis objects as keys and
970 * lists as values. It's used for blocking operations (BLPOP) */
971 static dictType keylistDictType = {
972 dictObjHash, /* hash function */
973 NULL, /* key dup */
974 NULL, /* val dup */
975 dictObjKeyCompare, /* key compare */
976 dictRedisObjectDestructor, /* key destructor */
977 dictListDestructor /* val destructor */
978 };
979
980 /* ========================= Random utility functions ======================= */
981
982 /* Redis generally does not try to recover from out of memory conditions
983 * when allocating objects or strings, it is not clear if it will be possible
984 * to report this condition to the client since the networking layer itself
985 * is based on heap allocation for send buffers, so we simply abort.
986 * At least the code will be simpler to read... */
987 static void oom(const char *msg) {
988 redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
989 sleep(1);
990 abort();
991 }
992
993 /* ====================== Redis server networking stuff ===================== */
994 static void closeTimedoutClients(void) {
995 redisClient *c;
996 listNode *ln;
997 time_t now = time(NULL);
998
999 listRewind(server.clients);
1000 while ((ln = listYield(server.clients)) != NULL) {
1001 c = listNodeValue(ln);
1002 if (server.maxidletime &&
1003 !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */
1004 !(c->flags & REDIS_MASTER) && /* no timeout for masters */
1005 (now - c->lastinteraction > server.maxidletime))
1006 {
1007 redisLog(REDIS_VERBOSE,"Closing idle client");
1008 freeClient(c);
1009 } else if (c->flags & REDIS_BLOCKED) {
1010 if (c->blockingto != 0 && c->blockingto < now) {
1011 addReply(c,shared.nullmultibulk);
1012 unblockClient(c);
1013 }
1014 }
1015 }
1016 }
1017
1018 static int htNeedsResize(dict *dict) {
1019 long long size, used;
1020
1021 size = dictSlots(dict);
1022 used = dictSize(dict);
1023 return (size && used && size > DICT_HT_INITIAL_SIZE &&
1024 (used*100/size < REDIS_HT_MINFILL));
1025 }
1026
1027 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
1028 * we resize the hash table to save memory */
1029 static void tryResizeHashTables(void) {
1030 int j;
1031
1032 for (j = 0; j < server.dbnum; j++) {
1033 if (htNeedsResize(server.db[j].dict)) {
1034 redisLog(REDIS_VERBOSE,"The hash table %d is too sparse, resize it...",j);
1035 dictResize(server.db[j].dict);
1036 redisLog(REDIS_VERBOSE,"Hash table %d resized.",j);
1037 }
1038 if (htNeedsResize(server.db[j].expires))
1039 dictResize(server.db[j].expires);
1040 }
1041 }
1042
1043 /* A background saving child (BGSAVE) terminated its work. Handle this. */
1044 void backgroundSaveDoneHandler(int statloc) {
1045 int exitcode = WEXITSTATUS(statloc);
1046 int bysignal = WIFSIGNALED(statloc);
1047
1048 if (!bysignal && exitcode == 0) {
1049 redisLog(REDIS_NOTICE,
1050 "Background saving terminated with success");
1051 server.dirty = 0;
1052 server.lastsave = time(NULL);
1053 } else if (!bysignal && exitcode != 0) {
1054 redisLog(REDIS_WARNING, "Background saving error");
1055 } else {
1056 redisLog(REDIS_WARNING,
1057 "Background saving terminated by signal");
1058 rdbRemoveTempFile(server.bgsavechildpid);
1059 }
1060 server.bgsavechildpid = -1;
1061 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1062 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1063 updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR);
1064 }
1065
1066 /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
1067 * Handle this. */
1068 void backgroundRewriteDoneHandler(int statloc) {
1069 int exitcode = WEXITSTATUS(statloc);
1070 int bysignal = WIFSIGNALED(statloc);
1071
1072 if (!bysignal && exitcode == 0) {
1073 int fd;
1074 char tmpfile[256];
1075
1076 redisLog(REDIS_NOTICE,
1077 "Background append only file rewriting terminated with success");
1078 /* Now it's time to flush the differences accumulated by the parent */
1079 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) server.bgrewritechildpid);
1080 fd = open(tmpfile,O_WRONLY|O_APPEND);
1081 if (fd == -1) {
1082 redisLog(REDIS_WARNING, "Not able to open the temp append only file produced by the child: %s", strerror(errno));
1083 goto cleanup;
1084 }
1085 /* Flush our data... */
1086 if (write(fd,server.bgrewritebuf,sdslen(server.bgrewritebuf)) !=
1087 (signed) sdslen(server.bgrewritebuf)) {
1088 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));
1089 close(fd);
1090 goto cleanup;
1091 }
1092 redisLog(REDIS_NOTICE,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server.bgrewritebuf));
1093 /* Now our work is to rename the temp file into the stable file. And
1094 * switch the file descriptor used by the server for append only. */
1095 if (rename(tmpfile,server.appendfilename) == -1) {
1096 redisLog(REDIS_WARNING,"Can't rename the temp append only file into the stable one: %s", strerror(errno));
1097 close(fd);
1098 goto cleanup;
1099 }
1100 /* Mission completed... almost */
1101 redisLog(REDIS_NOTICE,"Append only file successfully rewritten.");
1102 if (server.appendfd != -1) {
1103 /* If append only is actually enabled... */
1104 close(server.appendfd);
1105 server.appendfd = fd;
1106 fsync(fd);
1107 server.appendseldb = -1; /* Make sure it will issue SELECT */
1108 redisLog(REDIS_NOTICE,"The new append only file was selected for future appends.");
1109 } else {
1110 /* If append only is disabled we just generate a dump in this
1111 * format. Why not? */
1112 close(fd);
1113 }
1114 } else if (!bysignal && exitcode != 0) {
1115 redisLog(REDIS_WARNING, "Background append only file rewriting error");
1116 } else {
1117 redisLog(REDIS_WARNING,
1118 "Background append only file rewriting terminated by signal");
1119 }
1120 cleanup:
1121 sdsfree(server.bgrewritebuf);
1122 server.bgrewritebuf = sdsempty();
1123 aofRemoveTempFile(server.bgrewritechildpid);
1124 server.bgrewritechildpid = -1;
1125 }
1126
1127 static int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
1128 int j, loops = server.cronloops++;
1129 REDIS_NOTUSED(eventLoop);
1130 REDIS_NOTUSED(id);
1131 REDIS_NOTUSED(clientData);
1132
1133 /* We take a cached value of the unix time in the global state because
1134 * with virtual memory and aging there is to store the current time
1135 * in objects at every object access, and accuracy is not needed.
1136 * To access a global var is faster than calling time(NULL) */
1137 server.unixtime = time(NULL);
1138
1139 /* Update the global state with the amount of used memory */
1140 server.usedmemory = zmalloc_used_memory();
1141
1142 /* Show some info about non-empty databases */
1143 for (j = 0; j < server.dbnum; j++) {
1144 long long size, used, vkeys;
1145
1146 size = dictSlots(server.db[j].dict);
1147 used = dictSize(server.db[j].dict);
1148 vkeys = dictSize(server.db[j].expires);
1149 if (!(loops % 5) && (used || vkeys)) {
1150 redisLog(REDIS_VERBOSE,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j,used,vkeys,size);
1151 /* dictPrintStats(server.dict); */
1152 }
1153 }
1154
1155 /* We don't want to resize the hash tables while a bacground saving
1156 * is in progress: the saving child is created using fork() that is
1157 * implemented with a copy-on-write semantic in most modern systems, so
1158 * if we resize the HT while there is the saving child at work actually
1159 * a lot of memory movements in the parent will cause a lot of pages
1160 * copied. */
1161 if (server.bgsavechildpid == -1) tryResizeHashTables();
1162
1163 /* Show information about connected clients */
1164 if (!(loops % 5)) {
1165 redisLog(REDIS_VERBOSE,"%d clients connected (%d slaves), %zu bytes in use, %d shared objects",
1166 listLength(server.clients)-listLength(server.slaves),
1167 listLength(server.slaves),
1168 server.usedmemory,
1169 dictSize(server.sharingpool));
1170 }
1171
1172 /* Close connections of timedout clients */
1173 if ((server.maxidletime && !(loops % 10)) || server.blockedclients)
1174 closeTimedoutClients();
1175
1176 /* Check if a background saving or AOF rewrite in progress terminated */
1177 if (server.bgsavechildpid != -1 || server.bgrewritechildpid != -1) {
1178 int statloc;
1179 pid_t pid;
1180
1181 if ((pid = wait3(&statloc,WNOHANG,NULL)) != 0) {
1182 if (pid == server.bgsavechildpid) {
1183 backgroundSaveDoneHandler(statloc);
1184 } else {
1185 backgroundRewriteDoneHandler(statloc);
1186 }
1187 }
1188 } else {
1189 /* If there is not a background saving in progress check if
1190 * we have to save now */
1191 time_t now = time(NULL);
1192 for (j = 0; j < server.saveparamslen; j++) {
1193 struct saveparam *sp = server.saveparams+j;
1194
1195 if (server.dirty >= sp->changes &&
1196 now-server.lastsave > sp->seconds) {
1197 redisLog(REDIS_NOTICE,"%d changes in %d seconds. Saving...",
1198 sp->changes, sp->seconds);
1199 rdbSaveBackground(server.dbfilename);
1200 break;
1201 }
1202 }
1203 }
1204
1205 /* Try to expire a few timed out keys. The algorithm used is adaptive and
1206 * will use few CPU cycles if there are few expiring keys, otherwise
1207 * it will get more aggressive to avoid that too much memory is used by
1208 * keys that can be removed from the keyspace. */
1209 for (j = 0; j < server.dbnum; j++) {
1210 int expired;
1211 redisDb *db = server.db+j;
1212
1213 /* Continue to expire if at the end of the cycle more than 25%
1214 * of the keys were expired. */
1215 do {
1216 long num = dictSize(db->expires);
1217 time_t now = time(NULL);
1218
1219 expired = 0;
1220 if (num > REDIS_EXPIRELOOKUPS_PER_CRON)
1221 num = REDIS_EXPIRELOOKUPS_PER_CRON;
1222 while (num--) {
1223 dictEntry *de;
1224 time_t t;
1225
1226 if ((de = dictGetRandomKey(db->expires)) == NULL) break;
1227 t = (time_t) dictGetEntryVal(de);
1228 if (now > t) {
1229 deleteKey(db,dictGetEntryKey(de));
1230 expired++;
1231 }
1232 }
1233 } while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4);
1234 }
1235
1236 /* Swap a few keys on disk if we are over the memory limit and VM
1237 * is enbled. Try to free objects from the free list first. */
1238 if (vmCanSwapOut()) {
1239 while (server.vm_enabled && zmalloc_used_memory() >
1240 server.vm_max_memory)
1241 {
1242 if (listLength(server.objfreelist)) {
1243 freeOneObjectFromFreelist();
1244 } else {
1245 if (vmSwapOneObjectThreaded() == REDIS_ERR) {
1246 if ((loops % 30) == 0 && zmalloc_used_memory() >
1247 (server.vm_max_memory+server.vm_max_memory/10)) {
1248 redisLog(REDIS_WARNING,"WARNING: vm-max-memory limit exceeded by more than 10%% but unable to swap more objects out!");
1249 }
1250 }
1251 /* Note that we freed just one object, because anyway when
1252 * the I/O thread in charge to swap this object out will
1253 * do its work, the handler of completed jobs will try to swap
1254 * more objects if we are out of memory. */
1255 break;
1256 }
1257 }
1258 }
1259
1260 /* Check if we should connect to a MASTER */
1261 if (server.replstate == REDIS_REPL_CONNECT) {
1262 redisLog(REDIS_NOTICE,"Connecting to MASTER...");
1263 if (syncWithMaster() == REDIS_OK) {
1264 redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync succeeded");
1265 }
1266 }
1267 return 1000;
1268 }
1269
1270 static void createSharedObjects(void) {
1271 shared.crlf = createObject(REDIS_STRING,sdsnew("\r\n"));
1272 shared.ok = createObject(REDIS_STRING,sdsnew("+OK\r\n"));
1273 shared.err = createObject(REDIS_STRING,sdsnew("-ERR\r\n"));
1274 shared.emptybulk = createObject(REDIS_STRING,sdsnew("$0\r\n\r\n"));
1275 shared.czero = createObject(REDIS_STRING,sdsnew(":0\r\n"));
1276 shared.cone = createObject(REDIS_STRING,sdsnew(":1\r\n"));
1277 shared.nullbulk = createObject(REDIS_STRING,sdsnew("$-1\r\n"));
1278 shared.nullmultibulk = createObject(REDIS_STRING,sdsnew("*-1\r\n"));
1279 shared.emptymultibulk = createObject(REDIS_STRING,sdsnew("*0\r\n"));
1280 shared.pong = createObject(REDIS_STRING,sdsnew("+PONG\r\n"));
1281 shared.queued = createObject(REDIS_STRING,sdsnew("+QUEUED\r\n"));
1282 shared.wrongtypeerr = createObject(REDIS_STRING,sdsnew(
1283 "-ERR Operation against a key holding the wrong kind of value\r\n"));
1284 shared.nokeyerr = createObject(REDIS_STRING,sdsnew(
1285 "-ERR no such key\r\n"));
1286 shared.syntaxerr = createObject(REDIS_STRING,sdsnew(
1287 "-ERR syntax error\r\n"));
1288 shared.sameobjecterr = createObject(REDIS_STRING,sdsnew(
1289 "-ERR source and destination objects are the same\r\n"));
1290 shared.outofrangeerr = createObject(REDIS_STRING,sdsnew(
1291 "-ERR index out of range\r\n"));
1292 shared.space = createObject(REDIS_STRING,sdsnew(" "));
1293 shared.colon = createObject(REDIS_STRING,sdsnew(":"));
1294 shared.plus = createObject(REDIS_STRING,sdsnew("+"));
1295 shared.select0 = createStringObject("select 0\r\n",10);
1296 shared.select1 = createStringObject("select 1\r\n",10);
1297 shared.select2 = createStringObject("select 2\r\n",10);
1298 shared.select3 = createStringObject("select 3\r\n",10);
1299 shared.select4 = createStringObject("select 4\r\n",10);
1300 shared.select5 = createStringObject("select 5\r\n",10);
1301 shared.select6 = createStringObject("select 6\r\n",10);
1302 shared.select7 = createStringObject("select 7\r\n",10);
1303 shared.select8 = createStringObject("select 8\r\n",10);
1304 shared.select9 = createStringObject("select 9\r\n",10);
1305 }
1306
1307 static void appendServerSaveParams(time_t seconds, int changes) {
1308 server.saveparams = zrealloc(server.saveparams,sizeof(struct saveparam)*(server.saveparamslen+1));
1309 server.saveparams[server.saveparamslen].seconds = seconds;
1310 server.saveparams[server.saveparamslen].changes = changes;
1311 server.saveparamslen++;
1312 }
1313
1314 static void resetServerSaveParams() {
1315 zfree(server.saveparams);
1316 server.saveparams = NULL;
1317 server.saveparamslen = 0;
1318 }
1319
1320 static void initServerConfig() {
1321 server.dbnum = REDIS_DEFAULT_DBNUM;
1322 server.port = REDIS_SERVERPORT;
1323 server.verbosity = REDIS_VERBOSE;
1324 server.maxidletime = REDIS_MAXIDLETIME;
1325 server.saveparams = NULL;
1326 server.logfile = NULL; /* NULL = log on standard output */
1327 server.bindaddr = NULL;
1328 server.glueoutputbuf = 1;
1329 server.daemonize = 0;
1330 server.appendonly = 0;
1331 server.appendfsync = APPENDFSYNC_ALWAYS;
1332 server.lastfsync = time(NULL);
1333 server.appendfd = -1;
1334 server.appendseldb = -1; /* Make sure the first time will not match */
1335 server.pidfile = "/var/run/redis.pid";
1336 server.dbfilename = "dump.rdb";
1337 server.appendfilename = "appendonly.aof";
1338 server.requirepass = NULL;
1339 server.shareobjects = 0;
1340 server.rdbcompression = 1;
1341 server.sharingpoolsize = 1024;
1342 server.maxclients = 0;
1343 server.blockedclients = 0;
1344 server.maxmemory = 0;
1345 server.vm_enabled = 0;
1346 server.vm_page_size = 256; /* 256 bytes per page */
1347 server.vm_pages = 1024*1024*100; /* 104 millions of pages */
1348 server.vm_max_memory = 1024LL*1024*1024*1; /* 1 GB of RAM */
1349 server.vm_max_threads = 4;
1350
1351 resetServerSaveParams();
1352
1353 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1354 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1355 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1356 /* Replication related */
1357 server.isslave = 0;
1358 server.masterauth = NULL;
1359 server.masterhost = NULL;
1360 server.masterport = 6379;
1361 server.master = NULL;
1362 server.replstate = REDIS_REPL_NONE;
1363
1364 /* Double constants initialization */
1365 R_Zero = 0.0;
1366 R_PosInf = 1.0/R_Zero;
1367 R_NegInf = -1.0/R_Zero;
1368 R_Nan = R_Zero/R_Zero;
1369 }
1370
1371 static void initServer() {
1372 int j;
1373
1374 signal(SIGHUP, SIG_IGN);
1375 signal(SIGPIPE, SIG_IGN);
1376 setupSigSegvAction();
1377
1378 server.devnull = fopen("/dev/null","w");
1379 if (server.devnull == NULL) {
1380 redisLog(REDIS_WARNING, "Can't open /dev/null: %s", server.neterr);
1381 exit(1);
1382 }
1383 server.clients = listCreate();
1384 server.slaves = listCreate();
1385 server.monitors = listCreate();
1386 server.objfreelist = listCreate();
1387 createSharedObjects();
1388 server.el = aeCreateEventLoop();
1389 server.db = zmalloc(sizeof(redisDb)*server.dbnum);
1390 server.sharingpool = dictCreate(&setDictType,NULL);
1391 server.fd = anetTcpServer(server.neterr, server.port, server.bindaddr);
1392 if (server.fd == -1) {
1393 redisLog(REDIS_WARNING, "Opening TCP port: %s", server.neterr);
1394 exit(1);
1395 }
1396 for (j = 0; j < server.dbnum; j++) {
1397 server.db[j].dict = dictCreate(&hashDictType,NULL);
1398 server.db[j].expires = dictCreate(&setDictType,NULL);
1399 server.db[j].blockingkeys = dictCreate(&keylistDictType,NULL);
1400 server.db[j].id = j;
1401 }
1402 server.cronloops = 0;
1403 server.bgsavechildpid = -1;
1404 server.bgrewritechildpid = -1;
1405 server.bgrewritebuf = sdsempty();
1406 server.lastsave = time(NULL);
1407 server.dirty = 0;
1408 server.usedmemory = 0;
1409 server.stat_numcommands = 0;
1410 server.stat_numconnections = 0;
1411 server.stat_starttime = time(NULL);
1412 server.unixtime = time(NULL);
1413 aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
1414 if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,
1415 acceptHandler, NULL) == AE_ERR) oom("creating file event");
1416
1417 if (server.appendonly) {
1418 server.appendfd = open(server.appendfilename,O_WRONLY|O_APPEND|O_CREAT,0644);
1419 if (server.appendfd == -1) {
1420 redisLog(REDIS_WARNING, "Can't open the append-only file: %s",
1421 strerror(errno));
1422 exit(1);
1423 }
1424 }
1425
1426 if (server.vm_enabled) vmInit();
1427 }
1428
1429 /* Empty the whole database */
1430 static long long emptyDb() {
1431 int j;
1432 long long removed = 0;
1433
1434 for (j = 0; j < server.dbnum; j++) {
1435 removed += dictSize(server.db[j].dict);
1436 dictEmpty(server.db[j].dict);
1437 dictEmpty(server.db[j].expires);
1438 }
1439 return removed;
1440 }
1441
1442 static int yesnotoi(char *s) {
1443 if (!strcasecmp(s,"yes")) return 1;
1444 else if (!strcasecmp(s,"no")) return 0;
1445 else return -1;
1446 }
1447
1448 /* I agree, this is a very rudimental way to load a configuration...
1449 will improve later if the config gets more complex */
1450 static void loadServerConfig(char *filename) {
1451 FILE *fp;
1452 char buf[REDIS_CONFIGLINE_MAX+1], *err = NULL;
1453 int linenum = 0;
1454 sds line = NULL;
1455
1456 if (filename[0] == '-' && filename[1] == '\0')
1457 fp = stdin;
1458 else {
1459 if ((fp = fopen(filename,"r")) == NULL) {
1460 redisLog(REDIS_WARNING,"Fatal error, can't open config file");
1461 exit(1);
1462 }
1463 }
1464
1465 while(fgets(buf,REDIS_CONFIGLINE_MAX+1,fp) != NULL) {
1466 sds *argv;
1467 int argc, j;
1468
1469 linenum++;
1470 line = sdsnew(buf);
1471 line = sdstrim(line," \t\r\n");
1472
1473 /* Skip comments and blank lines*/
1474 if (line[0] == '#' || line[0] == '\0') {
1475 sdsfree(line);
1476 continue;
1477 }
1478
1479 /* Split into arguments */
1480 argv = sdssplitlen(line,sdslen(line)," ",1,&argc);
1481 sdstolower(argv[0]);
1482
1483 /* Execute config directives */
1484 if (!strcasecmp(argv[0],"timeout") && argc == 2) {
1485 server.maxidletime = atoi(argv[1]);
1486 if (server.maxidletime < 0) {
1487 err = "Invalid timeout value"; goto loaderr;
1488 }
1489 } else if (!strcasecmp(argv[0],"port") && argc == 2) {
1490 server.port = atoi(argv[1]);
1491 if (server.port < 1 || server.port > 65535) {
1492 err = "Invalid port"; goto loaderr;
1493 }
1494 } else if (!strcasecmp(argv[0],"bind") && argc == 2) {
1495 server.bindaddr = zstrdup(argv[1]);
1496 } else if (!strcasecmp(argv[0],"save") && argc == 3) {
1497 int seconds = atoi(argv[1]);
1498 int changes = atoi(argv[2]);
1499 if (seconds < 1 || changes < 0) {
1500 err = "Invalid save parameters"; goto loaderr;
1501 }
1502 appendServerSaveParams(seconds,changes);
1503 } else if (!strcasecmp(argv[0],"dir") && argc == 2) {
1504 if (chdir(argv[1]) == -1) {
1505 redisLog(REDIS_WARNING,"Can't chdir to '%s': %s",
1506 argv[1], strerror(errno));
1507 exit(1);
1508 }
1509 } else if (!strcasecmp(argv[0],"loglevel") && argc == 2) {
1510 if (!strcasecmp(argv[1],"debug")) server.verbosity = REDIS_DEBUG;
1511 else if (!strcasecmp(argv[1],"verbose")) server.verbosity = REDIS_VERBOSE;
1512 else if (!strcasecmp(argv[1],"notice")) server.verbosity = REDIS_NOTICE;
1513 else if (!strcasecmp(argv[1],"warning")) server.verbosity = REDIS_WARNING;
1514 else {
1515 err = "Invalid log level. Must be one of debug, notice, warning";
1516 goto loaderr;
1517 }
1518 } else if (!strcasecmp(argv[0],"logfile") && argc == 2) {
1519 FILE *logfp;
1520
1521 server.logfile = zstrdup(argv[1]);
1522 if (!strcasecmp(server.logfile,"stdout")) {
1523 zfree(server.logfile);
1524 server.logfile = NULL;
1525 }
1526 if (server.logfile) {
1527 /* Test if we are able to open the file. The server will not
1528 * be able to abort just for this problem later... */
1529 logfp = fopen(server.logfile,"a");
1530 if (logfp == NULL) {
1531 err = sdscatprintf(sdsempty(),
1532 "Can't open the log file: %s", strerror(errno));
1533 goto loaderr;
1534 }
1535 fclose(logfp);
1536 }
1537 } else if (!strcasecmp(argv[0],"databases") && argc == 2) {
1538 server.dbnum = atoi(argv[1]);
1539 if (server.dbnum < 1) {
1540 err = "Invalid number of databases"; goto loaderr;
1541 }
1542 } else if (!strcasecmp(argv[0],"maxclients") && argc == 2) {
1543 server.maxclients = atoi(argv[1]);
1544 } else if (!strcasecmp(argv[0],"maxmemory") && argc == 2) {
1545 server.maxmemory = strtoll(argv[1], NULL, 10);
1546 } else if (!strcasecmp(argv[0],"slaveof") && argc == 3) {
1547 server.masterhost = sdsnew(argv[1]);
1548 server.masterport = atoi(argv[2]);
1549 server.replstate = REDIS_REPL_CONNECT;
1550 } else if (!strcasecmp(argv[0],"masterauth") && argc == 2) {
1551 server.masterauth = zstrdup(argv[1]);
1552 } else if (!strcasecmp(argv[0],"glueoutputbuf") && argc == 2) {
1553 if ((server.glueoutputbuf = yesnotoi(argv[1])) == -1) {
1554 err = "argument must be 'yes' or 'no'"; goto loaderr;
1555 }
1556 } else if (!strcasecmp(argv[0],"shareobjects") && argc == 2) {
1557 if ((server.shareobjects = yesnotoi(argv[1])) == -1) {
1558 err = "argument must be 'yes' or 'no'"; goto loaderr;
1559 }
1560 } else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
1561 if ((server.rdbcompression = yesnotoi(argv[1])) == -1) {
1562 err = "argument must be 'yes' or 'no'"; goto loaderr;
1563 }
1564 } else if (!strcasecmp(argv[0],"shareobjectspoolsize") && argc == 2) {
1565 server.sharingpoolsize = atoi(argv[1]);
1566 if (server.sharingpoolsize < 1) {
1567 err = "invalid object sharing pool size"; goto loaderr;
1568 }
1569 } else if (!strcasecmp(argv[0],"daemonize") && argc == 2) {
1570 if ((server.daemonize = yesnotoi(argv[1])) == -1) {
1571 err = "argument must be 'yes' or 'no'"; goto loaderr;
1572 }
1573 } else if (!strcasecmp(argv[0],"appendonly") && argc == 2) {
1574 if ((server.appendonly = yesnotoi(argv[1])) == -1) {
1575 err = "argument must be 'yes' or 'no'"; goto loaderr;
1576 }
1577 } else if (!strcasecmp(argv[0],"appendfsync") && argc == 2) {
1578 if (!strcasecmp(argv[1],"no")) {
1579 server.appendfsync = APPENDFSYNC_NO;
1580 } else if (!strcasecmp(argv[1],"always")) {
1581 server.appendfsync = APPENDFSYNC_ALWAYS;
1582 } else if (!strcasecmp(argv[1],"everysec")) {
1583 server.appendfsync = APPENDFSYNC_EVERYSEC;
1584 } else {
1585 err = "argument must be 'no', 'always' or 'everysec'";
1586 goto loaderr;
1587 }
1588 } else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
1589 server.requirepass = zstrdup(argv[1]);
1590 } else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
1591 server.pidfile = zstrdup(argv[1]);
1592 } else if (!strcasecmp(argv[0],"dbfilename") && argc == 2) {
1593 server.dbfilename = zstrdup(argv[1]);
1594 } else if (!strcasecmp(argv[0],"vm-enabled") && argc == 2) {
1595 if ((server.vm_enabled = yesnotoi(argv[1])) == -1) {
1596 err = "argument must be 'yes' or 'no'"; goto loaderr;
1597 }
1598 } else if (!strcasecmp(argv[0],"vm-max-memory") && argc == 2) {
1599 server.vm_max_memory = strtoll(argv[1], NULL, 10);
1600 } else if (!strcasecmp(argv[0],"vm-page-size") && argc == 2) {
1601 server.vm_page_size = strtoll(argv[1], NULL, 10);
1602 } else if (!strcasecmp(argv[0],"vm-pages") && argc == 2) {
1603 server.vm_pages = strtoll(argv[1], NULL, 10);
1604 } else if (!strcasecmp(argv[0],"vm-max-threads") && argc == 2) {
1605 server.vm_max_threads = strtoll(argv[1], NULL, 10);
1606 } else {
1607 err = "Bad directive or wrong number of arguments"; goto loaderr;
1608 }
1609 for (j = 0; j < argc; j++)
1610 sdsfree(argv[j]);
1611 zfree(argv);
1612 sdsfree(line);
1613 }
1614 if (fp != stdin) fclose(fp);
1615 return;
1616
1617 loaderr:
1618 fprintf(stderr, "\n*** FATAL CONFIG FILE ERROR ***\n");
1619 fprintf(stderr, "Reading the configuration file, at line %d\n", linenum);
1620 fprintf(stderr, ">>> '%s'\n", line);
1621 fprintf(stderr, "%s\n", err);
1622 exit(1);
1623 }
1624
1625 static void freeClientArgv(redisClient *c) {
1626 int j;
1627
1628 for (j = 0; j < c->argc; j++)
1629 decrRefCount(c->argv[j]);
1630 for (j = 0; j < c->mbargc; j++)
1631 decrRefCount(c->mbargv[j]);
1632 c->argc = 0;
1633 c->mbargc = 0;
1634 }
1635
1636 static void freeClient(redisClient *c) {
1637 listNode *ln;
1638
1639 /* Note that if the client we are freeing is blocked into a blocking
1640 * call, we have to set querybuf to NULL *before* to call unblockClient()
1641 * to avoid processInputBuffer() will get called. Also it is important
1642 * to remove the file events after this, because this call adds
1643 * the READABLE event. */
1644 sdsfree(c->querybuf);
1645 c->querybuf = NULL;
1646 if (c->flags & REDIS_BLOCKED)
1647 unblockClient(c);
1648
1649 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
1650 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
1651 listRelease(c->reply);
1652 listRelease(c->io_keys);
1653 freeClientArgv(c);
1654 close(c->fd);
1655 /* Remove from the list of clients */
1656 ln = listSearchKey(server.clients,c);
1657 redisAssert(ln != NULL);
1658 listDelNode(server.clients,ln);
1659 /* Remove from the list of clients waiting for VM operations */
1660 if (server.vm_enabled && listLength(c->io_keys)) {
1661 ln = listSearchKey(server.io_clients,c);
1662 if (ln) listDelNode(server.io_clients,ln);
1663 listRelease(c->io_keys);
1664 }
1665 /* Other cleanup */
1666 if (c->flags & REDIS_SLAVE) {
1667 if (c->replstate == REDIS_REPL_SEND_BULK && c->repldbfd != -1)
1668 close(c->repldbfd);
1669 list *l = (c->flags & REDIS_MONITOR) ? server.monitors : server.slaves;
1670 ln = listSearchKey(l,c);
1671 redisAssert(ln != NULL);
1672 listDelNode(l,ln);
1673 }
1674 if (c->flags & REDIS_MASTER) {
1675 server.master = NULL;
1676 server.replstate = REDIS_REPL_CONNECT;
1677 }
1678 zfree(c->argv);
1679 zfree(c->mbargv);
1680 freeClientMultiState(c);
1681 zfree(c);
1682 }
1683
1684 #define GLUEREPLY_UP_TO (1024)
1685 static void glueReplyBuffersIfNeeded(redisClient *c) {
1686 int copylen = 0;
1687 char buf[GLUEREPLY_UP_TO];
1688 listNode *ln;
1689 robj *o;
1690
1691 listRewind(c->reply);
1692 while((ln = listYield(c->reply))) {
1693 int objlen;
1694
1695 o = ln->value;
1696 objlen = sdslen(o->ptr);
1697 if (copylen + objlen <= GLUEREPLY_UP_TO) {
1698 memcpy(buf+copylen,o->ptr,objlen);
1699 copylen += objlen;
1700 listDelNode(c->reply,ln);
1701 } else {
1702 if (copylen == 0) return;
1703 break;
1704 }
1705 }
1706 /* Now the output buffer is empty, add the new single element */
1707 o = createObject(REDIS_STRING,sdsnewlen(buf,copylen));
1708 listAddNodeHead(c->reply,o);
1709 }
1710
1711 static void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) {
1712 redisClient *c = privdata;
1713 int nwritten = 0, totwritten = 0, objlen;
1714 robj *o;
1715 REDIS_NOTUSED(el);
1716 REDIS_NOTUSED(mask);
1717
1718 /* Use writev() if we have enough buffers to send */
1719 if (!server.glueoutputbuf &&
1720 listLength(c->reply) > REDIS_WRITEV_THRESHOLD &&
1721 !(c->flags & REDIS_MASTER))
1722 {
1723 sendReplyToClientWritev(el, fd, privdata, mask);
1724 return;
1725 }
1726
1727 while(listLength(c->reply)) {
1728 if (server.glueoutputbuf && listLength(c->reply) > 1)
1729 glueReplyBuffersIfNeeded(c);
1730
1731 o = listNodeValue(listFirst(c->reply));
1732 objlen = sdslen(o->ptr);
1733
1734 if (objlen == 0) {
1735 listDelNode(c->reply,listFirst(c->reply));
1736 continue;
1737 }
1738
1739 if (c->flags & REDIS_MASTER) {
1740 /* Don't reply to a master */
1741 nwritten = objlen - c->sentlen;
1742 } else {
1743 nwritten = write(fd, ((char*)o->ptr)+c->sentlen, objlen - c->sentlen);
1744 if (nwritten <= 0) break;
1745 }
1746 c->sentlen += nwritten;
1747 totwritten += nwritten;
1748 /* If we fully sent the object on head go to the next one */
1749 if (c->sentlen == objlen) {
1750 listDelNode(c->reply,listFirst(c->reply));
1751 c->sentlen = 0;
1752 }
1753 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
1754 * bytes, in a single threaded server it's a good idea to serve
1755 * other clients as well, even if a very large request comes from
1756 * super fast link that is always able to accept data (in real world
1757 * scenario think about 'KEYS *' against the loopback interfae) */
1758 if (totwritten > REDIS_MAX_WRITE_PER_EVENT) break;
1759 }
1760 if (nwritten == -1) {
1761 if (errno == EAGAIN) {
1762 nwritten = 0;
1763 } else {
1764 redisLog(REDIS_VERBOSE,
1765 "Error writing to client: %s", strerror(errno));
1766 freeClient(c);
1767 return;
1768 }
1769 }
1770 if (totwritten > 0) c->lastinteraction = time(NULL);
1771 if (listLength(c->reply) == 0) {
1772 c->sentlen = 0;
1773 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
1774 }
1775 }
1776
1777 static void sendReplyToClientWritev(aeEventLoop *el, int fd, void *privdata, int mask)
1778 {
1779 redisClient *c = privdata;
1780 int nwritten = 0, totwritten = 0, objlen, willwrite;
1781 robj *o;
1782 struct iovec iov[REDIS_WRITEV_IOVEC_COUNT];
1783 int offset, ion = 0;
1784 REDIS_NOTUSED(el);
1785 REDIS_NOTUSED(mask);
1786
1787 listNode *node;
1788 while (listLength(c->reply)) {
1789 offset = c->sentlen;
1790 ion = 0;
1791 willwrite = 0;
1792
1793 /* fill-in the iov[] array */
1794 for(node = listFirst(c->reply); node; node = listNextNode(node)) {
1795 o = listNodeValue(node);
1796 objlen = sdslen(o->ptr);
1797
1798 if (totwritten + objlen - offset > REDIS_MAX_WRITE_PER_EVENT)
1799 break;
1800
1801 if(ion == REDIS_WRITEV_IOVEC_COUNT)
1802 break; /* no more iovecs */
1803
1804 iov[ion].iov_base = ((char*)o->ptr) + offset;
1805 iov[ion].iov_len = objlen - offset;
1806 willwrite += objlen - offset;
1807 offset = 0; /* just for the first item */
1808 ion++;
1809 }
1810
1811 if(willwrite == 0)
1812 break;
1813
1814 /* write all collected blocks at once */
1815 if((nwritten = writev(fd, iov, ion)) < 0) {
1816 if (errno != EAGAIN) {
1817 redisLog(REDIS_VERBOSE,
1818 "Error writing to client: %s", strerror(errno));
1819 freeClient(c);
1820 return;
1821 }
1822 break;
1823 }
1824
1825 totwritten += nwritten;
1826 offset = c->sentlen;
1827
1828 /* remove written robjs from c->reply */
1829 while (nwritten && listLength(c->reply)) {
1830 o = listNodeValue(listFirst(c->reply));
1831 objlen = sdslen(o->ptr);
1832
1833 if(nwritten >= objlen - offset) {
1834 listDelNode(c->reply, listFirst(c->reply));
1835 nwritten -= objlen - offset;
1836 c->sentlen = 0;
1837 } else {
1838 /* partial write */
1839 c->sentlen += nwritten;
1840 break;
1841 }
1842 offset = 0;
1843 }
1844 }
1845
1846 if (totwritten > 0)
1847 c->lastinteraction = time(NULL);
1848
1849 if (listLength(c->reply) == 0) {
1850 c->sentlen = 0;
1851 aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE);
1852 }
1853 }
1854
1855 static struct redisCommand *lookupCommand(char *name) {
1856 int j = 0;
1857 while(cmdTable[j].name != NULL) {
1858 if (!strcasecmp(name,cmdTable[j].name)) return &cmdTable[j];
1859 j++;
1860 }
1861 return NULL;
1862 }
1863
1864 /* resetClient prepare the client to process the next command */
1865 static void resetClient(redisClient *c) {
1866 freeClientArgv(c);
1867 c->bulklen = -1;
1868 c->multibulk = 0;
1869 }
1870
1871 /* Call() is the core of Redis execution of a command */
1872 static void call(redisClient *c, struct redisCommand *cmd) {
1873 long long dirty;
1874
1875 dirty = server.dirty;
1876 cmd->proc(c);
1877 if (server.appendonly && server.dirty-dirty)
1878 feedAppendOnlyFile(cmd,c->db->id,c->argv,c->argc);
1879 if (server.dirty-dirty && listLength(server.slaves))
1880 replicationFeedSlaves(server.slaves,cmd,c->db->id,c->argv,c->argc);
1881 if (listLength(server.monitors))
1882 replicationFeedSlaves(server.monitors,cmd,c->db->id,c->argv,c->argc);
1883 server.stat_numcommands++;
1884 }
1885
1886 /* If this function gets called we already read a whole
1887 * command, argments are in the client argv/argc fields.
1888 * processCommand() execute the command or prepare the
1889 * server for a bulk read from the client.
1890 *
1891 * If 1 is returned the client is still alive and valid and
1892 * and other operations can be performed by the caller. Otherwise
1893 * if 0 is returned the client was destroied (i.e. after QUIT). */
1894 static int processCommand(redisClient *c) {
1895 struct redisCommand *cmd;
1896
1897 /* Free some memory if needed (maxmemory setting) */
1898 if (server.maxmemory) freeMemoryIfNeeded();
1899
1900 /* Handle the multi bulk command type. This is an alternative protocol
1901 * supported by Redis in order to receive commands that are composed of
1902 * multiple binary-safe "bulk" arguments. The latency of processing is
1903 * a bit higher but this allows things like multi-sets, so if this
1904 * protocol is used only for MSET and similar commands this is a big win. */
1905 if (c->multibulk == 0 && c->argc == 1 && ((char*)(c->argv[0]->ptr))[0] == '*') {
1906 c->multibulk = atoi(((char*)c->argv[0]->ptr)+1);
1907 if (c->multibulk <= 0) {
1908 resetClient(c);
1909 return 1;
1910 } else {
1911 decrRefCount(c->argv[c->argc-1]);
1912 c->argc--;
1913 return 1;
1914 }
1915 } else if (c->multibulk) {
1916 if (c->bulklen == -1) {
1917 if (((char*)c->argv[0]->ptr)[0] != '$') {
1918 addReplySds(c,sdsnew("-ERR multi bulk protocol error\r\n"));
1919 resetClient(c);
1920 return 1;
1921 } else {
1922 int bulklen = atoi(((char*)c->argv[0]->ptr)+1);
1923 decrRefCount(c->argv[0]);
1924 if (bulklen < 0 || bulklen > 1024*1024*1024) {
1925 c->argc--;
1926 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
1927 resetClient(c);
1928 return 1;
1929 }
1930 c->argc--;
1931 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
1932 return 1;
1933 }
1934 } else {
1935 c->mbargv = zrealloc(c->mbargv,(sizeof(robj*))*(c->mbargc+1));
1936 c->mbargv[c->mbargc] = c->argv[0];
1937 c->mbargc++;
1938 c->argc--;
1939 c->multibulk--;
1940 if (c->multibulk == 0) {
1941 robj **auxargv;
1942 int auxargc;
1943
1944 /* Here we need to swap the multi-bulk argc/argv with the
1945 * normal argc/argv of the client structure. */
1946 auxargv = c->argv;
1947 c->argv = c->mbargv;
1948 c->mbargv = auxargv;
1949
1950 auxargc = c->argc;
1951 c->argc = c->mbargc;
1952 c->mbargc = auxargc;
1953
1954 /* We need to set bulklen to something different than -1
1955 * in order for the code below to process the command without
1956 * to try to read the last argument of a bulk command as
1957 * a special argument. */
1958 c->bulklen = 0;
1959 /* continue below and process the command */
1960 } else {
1961 c->bulklen = -1;
1962 return 1;
1963 }
1964 }
1965 }
1966 /* -- end of multi bulk commands processing -- */
1967
1968 /* The QUIT command is handled as a special case. Normal command
1969 * procs are unable to close the client connection safely */
1970 if (!strcasecmp(c->argv[0]->ptr,"quit")) {
1971 freeClient(c);
1972 return 0;
1973 }
1974 cmd = lookupCommand(c->argv[0]->ptr);
1975 if (!cmd) {
1976 addReplySds(c,
1977 sdscatprintf(sdsempty(), "-ERR unknown command '%s'\r\n",
1978 (char*)c->argv[0]->ptr));
1979 resetClient(c);
1980 return 1;
1981 } else if ((cmd->arity > 0 && cmd->arity != c->argc) ||
1982 (c->argc < -cmd->arity)) {
1983 addReplySds(c,
1984 sdscatprintf(sdsempty(),
1985 "-ERR wrong number of arguments for '%s' command\r\n",
1986 cmd->name));
1987 resetClient(c);
1988 return 1;
1989 } else if (server.maxmemory && cmd->flags & REDIS_CMD_DENYOOM && zmalloc_used_memory() > server.maxmemory) {
1990 addReplySds(c,sdsnew("-ERR command not allowed when used memory > 'maxmemory'\r\n"));
1991 resetClient(c);
1992 return 1;
1993 } else if (cmd->flags & REDIS_CMD_BULK && c->bulklen == -1) {
1994 int bulklen = atoi(c->argv[c->argc-1]->ptr);
1995
1996 decrRefCount(c->argv[c->argc-1]);
1997 if (bulklen < 0 || bulklen > 1024*1024*1024) {
1998 c->argc--;
1999 addReplySds(c,sdsnew("-ERR invalid bulk write count\r\n"));
2000 resetClient(c);
2001 return 1;
2002 }
2003 c->argc--;
2004 c->bulklen = bulklen+2; /* add two bytes for CR+LF */
2005 /* It is possible that the bulk read is already in the
2006 * buffer. Check this condition and handle it accordingly.
2007 * This is just a fast path, alternative to call processInputBuffer().
2008 * It's a good idea since the code is small and this condition
2009 * happens most of the times. */
2010 if ((signed)sdslen(c->querybuf) >= c->bulklen) {
2011 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2012 c->argc++;
2013 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
2014 } else {
2015 return 1;
2016 }
2017 }
2018 /* Let's try to share objects on the command arguments vector */
2019 if (server.shareobjects) {
2020 int j;
2021 for(j = 1; j < c->argc; j++)
2022 c->argv[j] = tryObjectSharing(c->argv[j]);
2023 }
2024 /* Let's try to encode the bulk object to save space. */
2025 if (cmd->flags & REDIS_CMD_BULK)
2026 tryObjectEncoding(c->argv[c->argc-1]);
2027
2028 /* Check if the user is authenticated */
2029 if (server.requirepass && !c->authenticated && cmd->proc != authCommand) {
2030 addReplySds(c,sdsnew("-ERR operation not permitted\r\n"));
2031 resetClient(c);
2032 return 1;
2033 }
2034
2035 /* Exec the command */
2036 if (c->flags & REDIS_MULTI && cmd->proc != execCommand) {
2037 queueMultiCommand(c,cmd);
2038 addReply(c,shared.queued);
2039 } else {
2040 call(c,cmd);
2041 }
2042
2043 /* Prepare the client for the next command */
2044 if (c->flags & REDIS_CLOSE) {
2045 freeClient(c);
2046 return 0;
2047 }
2048 resetClient(c);
2049 return 1;
2050 }
2051
2052 static void replicationFeedSlaves(list *slaves, struct redisCommand *cmd, int dictid, robj **argv, int argc) {
2053 listNode *ln;
2054 int outc = 0, j;
2055 robj **outv;
2056 /* (args*2)+1 is enough room for args, spaces, newlines */
2057 robj *static_outv[REDIS_STATIC_ARGS*2+1];
2058
2059 if (argc <= REDIS_STATIC_ARGS) {
2060 outv = static_outv;
2061 } else {
2062 outv = zmalloc(sizeof(robj*)*(argc*2+1));
2063 }
2064
2065 for (j = 0; j < argc; j++) {
2066 if (j != 0) outv[outc++] = shared.space;
2067 if ((cmd->flags & REDIS_CMD_BULK) && j == argc-1) {
2068 robj *lenobj;
2069
2070 lenobj = createObject(REDIS_STRING,
2071 sdscatprintf(sdsempty(),"%lu\r\n",
2072 (unsigned long) stringObjectLen(argv[j])));
2073 lenobj->refcount = 0;
2074 outv[outc++] = lenobj;
2075 }
2076 outv[outc++] = argv[j];
2077 }
2078 outv[outc++] = shared.crlf;
2079
2080 /* Increment all the refcounts at start and decrement at end in order to
2081 * be sure to free objects if there is no slave in a replication state
2082 * able to be feed with commands */
2083 for (j = 0; j < outc; j++) incrRefCount(outv[j]);
2084 listRewind(slaves);
2085 while((ln = listYield(slaves))) {
2086 redisClient *slave = ln->value;
2087
2088 /* Don't feed slaves that are still waiting for BGSAVE to start */
2089 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) continue;
2090
2091 /* Feed all the other slaves, MONITORs and so on */
2092 if (slave->slaveseldb != dictid) {
2093 robj *selectcmd;
2094
2095 switch(dictid) {
2096 case 0: selectcmd = shared.select0; break;
2097 case 1: selectcmd = shared.select1; break;
2098 case 2: selectcmd = shared.select2; break;
2099 case 3: selectcmd = shared.select3; break;
2100 case 4: selectcmd = shared.select4; break;
2101 case 5: selectcmd = shared.select5; break;
2102 case 6: selectcmd = shared.select6; break;
2103 case 7: selectcmd = shared.select7; break;
2104 case 8: selectcmd = shared.select8; break;
2105 case 9: selectcmd = shared.select9; break;
2106 default:
2107 selectcmd = createObject(REDIS_STRING,
2108 sdscatprintf(sdsempty(),"select %d\r\n",dictid));
2109 selectcmd->refcount = 0;
2110 break;
2111 }
2112 addReply(slave,selectcmd);
2113 slave->slaveseldb = dictid;
2114 }
2115 for (j = 0; j < outc; j++) addReply(slave,outv[j]);
2116 }
2117 for (j = 0; j < outc; j++) decrRefCount(outv[j]);
2118 if (outv != static_outv) zfree(outv);
2119 }
2120
2121 static void processInputBuffer(redisClient *c) {
2122 again:
2123 /* Before to process the input buffer, make sure the client is not
2124 * waitig for a blocking operation such as BLPOP. Note that the first
2125 * iteration the client is never blocked, otherwise the processInputBuffer
2126 * would not be called at all, but after the execution of the first commands
2127 * in the input buffer the client may be blocked, and the "goto again"
2128 * will try to reiterate. The following line will make it return asap. */
2129 if (c->flags & REDIS_BLOCKED || c->flags & REDIS_IO_WAIT) return;
2130 if (c->bulklen == -1) {
2131 /* Read the first line of the query */
2132 char *p = strchr(c->querybuf,'\n');
2133 size_t querylen;
2134
2135 if (p) {
2136 sds query, *argv;
2137 int argc, j;
2138
2139 query = c->querybuf;
2140 c->querybuf = sdsempty();
2141 querylen = 1+(p-(query));
2142 if (sdslen(query) > querylen) {
2143 /* leave data after the first line of the query in the buffer */
2144 c->querybuf = sdscatlen(c->querybuf,query+querylen,sdslen(query)-querylen);
2145 }
2146 *p = '\0'; /* remove "\n" */
2147 if (*(p-1) == '\r') *(p-1) = '\0'; /* and "\r" if any */
2148 sdsupdatelen(query);
2149
2150 /* Now we can split the query in arguments */
2151 argv = sdssplitlen(query,sdslen(query)," ",1,&argc);
2152 sdsfree(query);
2153
2154 if (c->argv) zfree(c->argv);
2155 c->argv = zmalloc(sizeof(robj*)*argc);
2156
2157 for (j = 0; j < argc; j++) {
2158 if (sdslen(argv[j])) {
2159 c->argv[c->argc] = createObject(REDIS_STRING,argv[j]);
2160 c->argc++;
2161 } else {
2162 sdsfree(argv[j]);
2163 }
2164 }
2165 zfree(argv);
2166 if (c->argc) {
2167 /* Execute the command. If the client is still valid
2168 * after processCommand() return and there is something
2169 * on the query buffer try to process the next command. */
2170 if (processCommand(c) && sdslen(c->querybuf)) goto again;
2171 } else {
2172 /* Nothing to process, argc == 0. Just process the query
2173 * buffer if it's not empty or return to the caller */
2174 if (sdslen(c->querybuf)) goto again;
2175 }
2176 return;
2177 } else if (sdslen(c->querybuf) >= REDIS_REQUEST_MAX_SIZE) {
2178 redisLog(REDIS_VERBOSE, "Client protocol error");
2179 freeClient(c);
2180 return;
2181 }
2182 } else {
2183 /* Bulk read handling. Note that if we are at this point
2184 the client already sent a command terminated with a newline,
2185 we are reading the bulk data that is actually the last
2186 argument of the command. */
2187 int qbl = sdslen(c->querybuf);
2188
2189 if (c->bulklen <= qbl) {
2190 /* Copy everything but the final CRLF as final argument */
2191 c->argv[c->argc] = createStringObject(c->querybuf,c->bulklen-2);
2192 c->argc++;
2193 c->querybuf = sdsrange(c->querybuf,c->bulklen,-1);
2194 /* Process the command. If the client is still valid after
2195 * the processing and there is more data in the buffer
2196 * try to parse it. */
2197 if (processCommand(c) && sdslen(c->querybuf)) goto again;
2198 return;
2199 }
2200 }
2201 }
2202
2203 static void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
2204 redisClient *c = (redisClient*) privdata;
2205 char buf[REDIS_IOBUF_LEN];
2206 int nread;
2207 REDIS_NOTUSED(el);
2208 REDIS_NOTUSED(mask);
2209
2210 nread = read(fd, buf, REDIS_IOBUF_LEN);
2211 if (nread == -1) {
2212 if (errno == EAGAIN) {
2213 nread = 0;
2214 } else {
2215 redisLog(REDIS_VERBOSE, "Reading from client: %s",strerror(errno));
2216 freeClient(c);
2217 return;
2218 }
2219 } else if (nread == 0) {
2220 redisLog(REDIS_VERBOSE, "Client closed connection");
2221 freeClient(c);
2222 return;
2223 }
2224 if (nread) {
2225 c->querybuf = sdscatlen(c->querybuf, buf, nread);
2226 c->lastinteraction = time(NULL);
2227 } else {
2228 return;
2229 }
2230 processInputBuffer(c);
2231 }
2232
2233 static int selectDb(redisClient *c, int id) {
2234 if (id < 0 || id >= server.dbnum)
2235 return REDIS_ERR;
2236 c->db = &server.db[id];
2237 return REDIS_OK;
2238 }
2239
2240 static void *dupClientReplyValue(void *o) {
2241 incrRefCount((robj*)o);
2242 return 0;
2243 }
2244
2245 static redisClient *createClient(int fd) {
2246 redisClient *c = zmalloc(sizeof(*c));
2247
2248 anetNonBlock(NULL,fd);
2249 anetTcpNoDelay(NULL,fd);
2250 if (!c) return NULL;
2251 selectDb(c,0);
2252 c->fd = fd;
2253 c->querybuf = sdsempty();
2254 c->argc = 0;
2255 c->argv = NULL;
2256 c->bulklen = -1;
2257 c->multibulk = 0;
2258 c->mbargc = 0;
2259 c->mbargv = NULL;
2260 c->sentlen = 0;
2261 c->flags = 0;
2262 c->lastinteraction = time(NULL);
2263 c->authenticated = 0;
2264 c->replstate = REDIS_REPL_NONE;
2265 c->reply = listCreate();
2266 listSetFreeMethod(c->reply,decrRefCount);
2267 listSetDupMethod(c->reply,dupClientReplyValue);
2268 c->blockingkeys = NULL;
2269 c->blockingkeysnum = 0;
2270 c->io_keys = listCreate();
2271 listSetFreeMethod(c->io_keys,decrRefCount);
2272 if (aeCreateFileEvent(server.el, c->fd, AE_READABLE,
2273 readQueryFromClient, c) == AE_ERR) {
2274 freeClient(c);
2275 return NULL;
2276 }
2277 listAddNodeTail(server.clients,c);
2278 initClientMultiState(c);
2279 return c;
2280 }
2281
2282 static void addReply(redisClient *c, robj *obj) {
2283 if (listLength(c->reply) == 0 &&
2284 (c->replstate == REDIS_REPL_NONE ||
2285 c->replstate == REDIS_REPL_ONLINE) &&
2286 aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
2287 sendReplyToClient, c) == AE_ERR) return;
2288
2289 if (server.vm_enabled && obj->storage != REDIS_VM_MEMORY) {
2290 obj = dupStringObject(obj);
2291 obj->refcount = 0; /* getDecodedObject() will increment the refcount */
2292 }
2293 listAddNodeTail(c->reply,getDecodedObject(obj));
2294 }
2295
2296 static void addReplySds(redisClient *c, sds s) {
2297 robj *o = createObject(REDIS_STRING,s);
2298 addReply(c,o);
2299 decrRefCount(o);
2300 }
2301
2302 static void addReplyDouble(redisClient *c, double d) {
2303 char buf[128];
2304
2305 snprintf(buf,sizeof(buf),"%.17g",d);
2306 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
2307 (unsigned long) strlen(buf),buf));
2308 }
2309
2310 static void addReplyBulkLen(redisClient *c, robj *obj) {
2311 size_t len;
2312
2313 if (obj->encoding == REDIS_ENCODING_RAW) {
2314 len = sdslen(obj->ptr);
2315 } else {
2316 long n = (long)obj->ptr;
2317
2318 /* Compute how many bytes will take this integer as a radix 10 string */
2319 len = 1;
2320 if (n < 0) {
2321 len++;
2322 n = -n;
2323 }
2324 while((n = n/10) != 0) {
2325 len++;
2326 }
2327 }
2328 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",(unsigned long)len));
2329 }
2330
2331 static void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
2332 int cport, cfd;
2333 char cip[128];
2334 redisClient *c;
2335 REDIS_NOTUSED(el);
2336 REDIS_NOTUSED(mask);
2337 REDIS_NOTUSED(privdata);
2338
2339 cfd = anetAccept(server.neterr, fd, cip, &cport);
2340 if (cfd == AE_ERR) {
2341 redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
2342 return;
2343 }
2344 redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
2345 if ((c = createClient(cfd)) == NULL) {
2346 redisLog(REDIS_WARNING,"Error allocating resoures for the client");
2347 close(cfd); /* May be already closed, just ingore errors */
2348 return;
2349 }
2350 /* If maxclient directive is set and this is one client more... close the
2351 * connection. Note that we create the client instead to check before
2352 * for this condition, since now the socket is already set in nonblocking
2353 * mode and we can send an error for free using the Kernel I/O */
2354 if (server.maxclients && listLength(server.clients) > server.maxclients) {
2355 char *err = "-ERR max number of clients reached\r\n";
2356
2357 /* That's a best effort error message, don't check write errors */
2358 if (write(c->fd,err,strlen(err)) == -1) {
2359 /* Nothing to do, Just to avoid the warning... */
2360 }
2361 freeClient(c);
2362 return;
2363 }
2364 server.stat_numconnections++;
2365 }
2366
2367 /* ======================= Redis objects implementation ===================== */
2368
2369 static robj *createObject(int type, void *ptr) {
2370 robj *o;
2371
2372 if (listLength(server.objfreelist)) {
2373 listNode *head = listFirst(server.objfreelist);
2374 o = listNodeValue(head);
2375 listDelNode(server.objfreelist,head);
2376 } else {
2377 if (server.vm_enabled) {
2378 o = zmalloc(sizeof(*o));
2379 } else {
2380 o = zmalloc(sizeof(*o)-sizeof(struct redisObjectVM));
2381 }
2382 }
2383 o->type = type;
2384 o->encoding = REDIS_ENCODING_RAW;
2385 o->ptr = ptr;
2386 o->refcount = 1;
2387 if (server.vm_enabled) {
2388 o->vm.atime = server.unixtime;
2389 o->storage = REDIS_VM_MEMORY;
2390 }
2391 return o;
2392 }
2393
2394 static robj *createStringObject(char *ptr, size_t len) {
2395 return createObject(REDIS_STRING,sdsnewlen(ptr,len));
2396 }
2397
2398 static robj *dupStringObject(robj *o) {
2399 assert(o->encoding == REDIS_ENCODING_RAW);
2400 return createStringObject(o->ptr,sdslen(o->ptr));
2401 }
2402
2403 static robj *createListObject(void) {
2404 list *l = listCreate();
2405
2406 listSetFreeMethod(l,decrRefCount);
2407 return createObject(REDIS_LIST,l);
2408 }
2409
2410 static robj *createSetObject(void) {
2411 dict *d = dictCreate(&setDictType,NULL);
2412 return createObject(REDIS_SET,d);
2413 }
2414
2415 static robj *createZsetObject(void) {
2416 zset *zs = zmalloc(sizeof(*zs));
2417
2418 zs->dict = dictCreate(&zsetDictType,NULL);
2419 zs->zsl = zslCreate();
2420 return createObject(REDIS_ZSET,zs);
2421 }
2422
2423 static void freeStringObject(robj *o) {
2424 if (o->encoding == REDIS_ENCODING_RAW) {
2425 sdsfree(o->ptr);
2426 }
2427 }
2428
2429 static void freeListObject(robj *o) {
2430 listRelease((list*) o->ptr);
2431 }
2432
2433 static void freeSetObject(robj *o) {
2434 dictRelease((dict*) o->ptr);
2435 }
2436
2437 static void freeZsetObject(robj *o) {
2438 zset *zs = o->ptr;
2439
2440 dictRelease(zs->dict);
2441 zslFree(zs->zsl);
2442 zfree(zs);
2443 }
2444
2445 static void freeHashObject(robj *o) {
2446 dictRelease((dict*) o->ptr);
2447 }
2448
2449 static void incrRefCount(robj *o) {
2450 redisAssert(!server.vm_enabled || o->storage == REDIS_VM_MEMORY);
2451 o->refcount++;
2452 }
2453
2454 static void decrRefCount(void *obj) {
2455 robj *o = obj;
2456
2457 /* Object is swapped out, or in the process of being loaded. */
2458 if (server.vm_enabled &&
2459 (o->storage == REDIS_VM_SWAPPED || o->storage == REDIS_VM_LOADING))
2460 {
2461 if (o->storage == REDIS_VM_SWAPPED || o->storage == REDIS_VM_LOADING) {
2462 redisAssert(o->refcount == 1);
2463 }
2464 if (o->storage == REDIS_VM_LOADING) vmCancelThreadedIOJob(obj);
2465 redisAssert(o->type == REDIS_STRING);
2466 freeStringObject(o);
2467 vmMarkPagesFree(o->vm.page,o->vm.usedpages);
2468 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
2469 !listAddNodeHead(server.objfreelist,o))
2470 zfree(o);
2471 server.vm_stats_swapped_objects--;
2472 return;
2473 }
2474 /* Object is in memory, or in the process of being swapped out. */
2475 if (--(o->refcount) == 0) {
2476 if (server.vm_enabled && o->storage == REDIS_VM_SWAPPING)
2477 vmCancelThreadedIOJob(obj);
2478 switch(o->type) {
2479 case REDIS_STRING: freeStringObject(o); break;
2480 case REDIS_LIST: freeListObject(o); break;
2481 case REDIS_SET: freeSetObject(o); break;
2482 case REDIS_ZSET: freeZsetObject(o); break;
2483 case REDIS_HASH: freeHashObject(o); break;
2484 default: redisAssert(0 != 0); break;
2485 }
2486 if (listLength(server.objfreelist) > REDIS_OBJFREELIST_MAX ||
2487 !listAddNodeHead(server.objfreelist,o))
2488 zfree(o);
2489 }
2490 }
2491
2492 static robj *lookupKey(redisDb *db, robj *key) {
2493 dictEntry *de = dictFind(db->dict,key);
2494 if (de) {
2495 robj *key = dictGetEntryKey(de);
2496 robj *val = dictGetEntryVal(de);
2497
2498 if (server.vm_enabled) {
2499 if (key->storage == REDIS_VM_MEMORY ||
2500 key->storage == REDIS_VM_SWAPPING)
2501 {
2502 /* If we were swapping the object out, stop it, this key
2503 * was requested. */
2504 if (key->storage == REDIS_VM_SWAPPING)
2505 vmCancelThreadedIOJob(key);
2506 /* Update the access time of the key for the aging algorithm. */
2507 key->vm.atime = server.unixtime;
2508 } else {
2509 /* Our value was swapped on disk. Bring it at home. */
2510 redisAssert(val == NULL);
2511 val = vmLoadObject(key);
2512 dictGetEntryVal(de) = val;
2513 }
2514 }
2515 return val;
2516 } else {
2517 return NULL;
2518 }
2519 }
2520
2521 static robj *lookupKeyRead(redisDb *db, robj *key) {
2522 expireIfNeeded(db,key);
2523 return lookupKey(db,key);
2524 }
2525
2526 static robj *lookupKeyWrite(redisDb *db, robj *key) {
2527 deleteIfVolatile(db,key);
2528 return lookupKey(db,key);
2529 }
2530
2531 static int deleteKey(redisDb *db, robj *key) {
2532 int retval;
2533
2534 /* We need to protect key from destruction: after the first dictDelete()
2535 * it may happen that 'key' is no longer valid if we don't increment
2536 * it's count. This may happen when we get the object reference directly
2537 * from the hash table with dictRandomKey() or dict iterators */
2538 incrRefCount(key);
2539 if (dictSize(db->expires)) dictDelete(db->expires,key);
2540 retval = dictDelete(db->dict,key);
2541 decrRefCount(key);
2542
2543 return retval == DICT_OK;
2544 }
2545
2546 /* Try to share an object against the shared objects pool */
2547 static robj *tryObjectSharing(robj *o) {
2548 struct dictEntry *de;
2549 unsigned long c;
2550
2551 if (o == NULL || server.shareobjects == 0) return o;
2552
2553 redisAssert(o->type == REDIS_STRING);
2554 de = dictFind(server.sharingpool,o);
2555 if (de) {
2556 robj *shared = dictGetEntryKey(de);
2557
2558 c = ((unsigned long) dictGetEntryVal(de))+1;
2559 dictGetEntryVal(de) = (void*) c;
2560 incrRefCount(shared);
2561 decrRefCount(o);
2562 return shared;
2563 } else {
2564 /* Here we are using a stream algorihtm: Every time an object is
2565 * shared we increment its count, everytime there is a miss we
2566 * recrement the counter of a random object. If this object reaches
2567 * zero we remove the object and put the current object instead. */
2568 if (dictSize(server.sharingpool) >=
2569 server.sharingpoolsize) {
2570 de = dictGetRandomKey(server.sharingpool);
2571 redisAssert(de != NULL);
2572 c = ((unsigned long) dictGetEntryVal(de))-1;
2573 dictGetEntryVal(de) = (void*) c;
2574 if (c == 0) {
2575 dictDelete(server.sharingpool,de->key);
2576 }
2577 } else {
2578 c = 0; /* If the pool is empty we want to add this object */
2579 }
2580 if (c == 0) {
2581 int retval;
2582
2583 retval = dictAdd(server.sharingpool,o,(void*)1);
2584 redisAssert(retval == DICT_OK);
2585 incrRefCount(o);
2586 }
2587 return o;
2588 }
2589 }
2590
2591 /* Check if the nul-terminated string 's' can be represented by a long
2592 * (that is, is a number that fits into long without any other space or
2593 * character before or after the digits).
2594 *
2595 * If so, the function returns REDIS_OK and *longval is set to the value
2596 * of the number. Otherwise REDIS_ERR is returned */
2597 static int isStringRepresentableAsLong(sds s, long *longval) {
2598 char buf[32], *endptr;
2599 long value;
2600 int slen;
2601
2602 value = strtol(s, &endptr, 10);
2603 if (endptr[0] != '\0') return REDIS_ERR;
2604 slen = snprintf(buf,32,"%ld",value);
2605
2606 /* If the number converted back into a string is not identical
2607 * then it's not possible to encode the string as integer */
2608 if (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR;
2609 if (longval) *longval = value;
2610 return REDIS_OK;
2611 }
2612
2613 /* Try to encode a string object in order to save space */
2614 static int tryObjectEncoding(robj *o) {
2615 long value;
2616 sds s = o->ptr;
2617
2618 if (o->encoding != REDIS_ENCODING_RAW)
2619 return REDIS_ERR; /* Already encoded */
2620
2621 /* It's not save to encode shared objects: shared objects can be shared
2622 * everywhere in the "object space" of Redis. Encoded objects can only
2623 * appear as "values" (and not, for instance, as keys) */
2624 if (o->refcount > 1) return REDIS_ERR;
2625
2626 /* Currently we try to encode only strings */
2627 redisAssert(o->type == REDIS_STRING);
2628
2629 /* Check if we can represent this string as a long integer */
2630 if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return REDIS_ERR;
2631
2632 /* Ok, this object can be encoded */
2633 o->encoding = REDIS_ENCODING_INT;
2634 sdsfree(o->ptr);
2635 o->ptr = (void*) value;
2636 return REDIS_OK;
2637 }
2638
2639 /* Get a decoded version of an encoded object (returned as a new object).
2640 * If the object is already raw-encoded just increment the ref count. */
2641 static robj *getDecodedObject(robj *o) {
2642 robj *dec;
2643
2644 if (o->encoding == REDIS_ENCODING_RAW) {
2645 incrRefCount(o);
2646 return o;
2647 }
2648 if (o->type == REDIS_STRING && o->encoding == REDIS_ENCODING_INT) {
2649 char buf[32];
2650
2651 snprintf(buf,32,"%ld",(long)o->ptr);
2652 dec = createStringObject(buf,strlen(buf));
2653 return dec;
2654 } else {
2655 redisAssert(1 != 1);
2656 }
2657 }
2658
2659 /* Compare two string objects via strcmp() or alike.
2660 * Note that the objects may be integer-encoded. In such a case we
2661 * use snprintf() to get a string representation of the numbers on the stack
2662 * and compare the strings, it's much faster than calling getDecodedObject().
2663 *
2664 * Important note: if objects are not integer encoded, but binary-safe strings,
2665 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
2666 * binary safe. */
2667 static int compareStringObjects(robj *a, robj *b) {
2668 redisAssert(a->type == REDIS_STRING && b->type == REDIS_STRING);
2669 char bufa[128], bufb[128], *astr, *bstr;
2670 int bothsds = 1;
2671
2672 if (a == b) return 0;
2673 if (a->encoding != REDIS_ENCODING_RAW) {
2674 snprintf(bufa,sizeof(bufa),"%ld",(long) a->ptr);
2675 astr = bufa;
2676 bothsds = 0;
2677 } else {
2678 astr = a->ptr;
2679 }
2680 if (b->encoding != REDIS_ENCODING_RAW) {
2681 snprintf(bufb,sizeof(bufb),"%ld",(long) b->ptr);
2682 bstr = bufb;
2683 bothsds = 0;
2684 } else {
2685 bstr = b->ptr;
2686 }
2687 return bothsds ? sdscmp(astr,bstr) : strcmp(astr,bstr);
2688 }
2689
2690 static size_t stringObjectLen(robj *o) {
2691 redisAssert(o->type == REDIS_STRING);
2692 if (o->encoding == REDIS_ENCODING_RAW) {
2693 return sdslen(o->ptr);
2694 } else {
2695 char buf[32];
2696
2697 return snprintf(buf,32,"%ld",(long)o->ptr);
2698 }
2699 }
2700
2701 /*============================ RDB saving/loading =========================== */
2702
2703 static int rdbSaveType(FILE *fp, unsigned char type) {
2704 if (fwrite(&type,1,1,fp) == 0) return -1;
2705 return 0;
2706 }
2707
2708 static int rdbSaveTime(FILE *fp, time_t t) {
2709 int32_t t32 = (int32_t) t;
2710 if (fwrite(&t32,4,1,fp) == 0) return -1;
2711 return 0;
2712 }
2713
2714 /* check rdbLoadLen() comments for more info */
2715 static int rdbSaveLen(FILE *fp, uint32_t len) {
2716 unsigned char buf[2];
2717
2718 if (len < (1<<6)) {
2719 /* Save a 6 bit len */
2720 buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
2721 if (fwrite(buf,1,1,fp) == 0) return -1;
2722 } else if (len < (1<<14)) {
2723 /* Save a 14 bit len */
2724 buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
2725 buf[1] = len&0xFF;
2726 if (fwrite(buf,2,1,fp) == 0) return -1;
2727 } else {
2728 /* Save a 32 bit len */
2729 buf[0] = (REDIS_RDB_32BITLEN<<6);
2730 if (fwrite(buf,1,1,fp) == 0) return -1;
2731 len = htonl(len);
2732 if (fwrite(&len,4,1,fp) == 0) return -1;
2733 }
2734 return 0;
2735 }
2736
2737 /* String objects in the form "2391" "-100" without any space and with a
2738 * range of values that can fit in an 8, 16 or 32 bit signed value can be
2739 * encoded as integers to save space */
2740 static int rdbTryIntegerEncoding(sds s, unsigned char *enc) {
2741 long long value;
2742 char *endptr, buf[32];
2743
2744 /* Check if it's possible to encode this value as a number */
2745 value = strtoll(s, &endptr, 10);
2746 if (endptr[0] != '\0') return 0;
2747 snprintf(buf,32,"%lld",value);
2748
2749 /* If the number converted back into a string is not identical
2750 * then it's not possible to encode the string as integer */
2751 if (strlen(buf) != sdslen(s) || memcmp(buf,s,sdslen(s))) return 0;
2752
2753 /* Finally check if it fits in our ranges */
2754 if (value >= -(1<<7) && value <= (1<<7)-1) {
2755 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8;
2756 enc[1] = value&0xFF;
2757 return 2;
2758 } else if (value >= -(1<<15) && value <= (1<<15)-1) {
2759 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16;
2760 enc[1] = value&0xFF;
2761 enc[2] = (value>>8)&0xFF;
2762 return 3;
2763 } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) {
2764 enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32;
2765 enc[1] = value&0xFF;
2766 enc[2] = (value>>8)&0xFF;
2767 enc[3] = (value>>16)&0xFF;
2768 enc[4] = (value>>24)&0xFF;
2769 return 5;
2770 } else {
2771 return 0;
2772 }
2773 }
2774
2775 static int rdbSaveLzfStringObject(FILE *fp, robj *obj) {
2776 unsigned int comprlen, outlen;
2777 unsigned char byte;
2778 void *out;
2779
2780 /* We require at least four bytes compression for this to be worth it */
2781 outlen = sdslen(obj->ptr)-4;
2782 if (outlen <= 0) return 0;
2783 if ((out = zmalloc(outlen+1)) == NULL) return 0;
2784 comprlen = lzf_compress(obj->ptr, sdslen(obj->ptr), out, outlen);
2785 if (comprlen == 0) {
2786 zfree(out);
2787 return 0;
2788 }
2789 /* Data compressed! Let's save it on disk */
2790 byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
2791 if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
2792 if (rdbSaveLen(fp,comprlen) == -1) goto writeerr;
2793 if (rdbSaveLen(fp,sdslen(obj->ptr)) == -1) goto writeerr;
2794 if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
2795 zfree(out);
2796 return comprlen;
2797
2798 writeerr:
2799 zfree(out);
2800 return -1;
2801 }
2802
2803 /* Save a string objet as [len][data] on disk. If the object is a string
2804 * representation of an integer value we try to safe it in a special form */
2805 static int rdbSaveStringObjectRaw(FILE *fp, robj *obj) {
2806 size_t len;
2807 int enclen;
2808
2809 len = sdslen(obj->ptr);
2810
2811 /* Try integer encoding */
2812 if (len <= 11) {
2813 unsigned char buf[5];
2814 if ((enclen = rdbTryIntegerEncoding(obj->ptr,buf)) > 0) {
2815 if (fwrite(buf,enclen,1,fp) == 0) return -1;
2816 return 0;
2817 }
2818 }
2819
2820 /* Try LZF compression - under 20 bytes it's unable to compress even
2821 * aaaaaaaaaaaaaaaaaa so skip it */
2822 if (server.rdbcompression && len > 20) {
2823 int retval;
2824
2825 retval = rdbSaveLzfStringObject(fp,obj);
2826 if (retval == -1) return -1;
2827 if (retval > 0) return 0;
2828 /* retval == 0 means data can't be compressed, save the old way */
2829 }
2830
2831 /* Store verbatim */
2832 if (rdbSaveLen(fp,len) == -1) return -1;
2833 if (len && fwrite(obj->ptr,len,1,fp) == 0) return -1;
2834 return 0;
2835 }
2836
2837 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
2838 static int rdbSaveStringObject(FILE *fp, robj *obj) {
2839 int retval;
2840
2841 if (obj->storage == REDIS_VM_MEMORY &&
2842 obj->encoding != REDIS_ENCODING_RAW)
2843 {
2844 obj = getDecodedObject(obj);
2845 retval = rdbSaveStringObjectRaw(fp,obj);
2846 decrRefCount(obj);
2847 } else {
2848 /* This is a fast path when we are sure the object is not encoded.
2849 * Note that's any *faster* actually as we needed to add the conditional
2850 * but because this may happen in a background process we don't want
2851 * to touch the object fields with incr/decrRefCount in order to
2852 * preveny copy on write of pages.
2853 *
2854 * Also incrRefCount() will have a failing assert() if we try to call
2855 * it against an object with storage != REDIS_VM_MEMORY. */
2856 retval = rdbSaveStringObjectRaw(fp,obj);
2857 }
2858 return retval;
2859 }
2860
2861 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
2862 * 8 bit integer specifing the length of the representation.
2863 * This 8 bit integer has special values in order to specify the following
2864 * conditions:
2865 * 253: not a number
2866 * 254: + inf
2867 * 255: - inf
2868 */
2869 static int rdbSaveDoubleValue(FILE *fp, double val) {
2870 unsigned char buf[128];
2871 int len;
2872
2873 if (isnan(val)) {
2874 buf[0] = 253;
2875 len = 1;
2876 } else if (!isfinite(val)) {
2877 len = 1;
2878 buf[0] = (val < 0) ? 255 : 254;
2879 } else {
2880 snprintf((char*)buf+1,sizeof(buf)-1,"%.17g",val);
2881 buf[0] = strlen((char*)buf+1);
2882 len = buf[0]+1;
2883 }
2884 if (fwrite(buf,len,1,fp) == 0) return -1;
2885 return 0;
2886 }
2887
2888 /* Save a Redis object. */
2889 static int rdbSaveObject(FILE *fp, robj *o) {
2890 if (o->type == REDIS_STRING) {
2891 /* Save a string value */
2892 if (rdbSaveStringObject(fp,o) == -1) return -1;
2893 } else if (o->type == REDIS_LIST) {
2894 /* Save a list value */
2895 list *list = o->ptr;
2896 listNode *ln;
2897
2898 listRewind(list);
2899 if (rdbSaveLen(fp,listLength(list)) == -1) return -1;
2900 while((ln = listYield(list))) {
2901 robj *eleobj = listNodeValue(ln);
2902
2903 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
2904 }
2905 } else if (o->type == REDIS_SET) {
2906 /* Save a set value */
2907 dict *set = o->ptr;
2908 dictIterator *di = dictGetIterator(set);
2909 dictEntry *de;
2910
2911 if (rdbSaveLen(fp,dictSize(set)) == -1) return -1;
2912 while((de = dictNext(di)) != NULL) {
2913 robj *eleobj = dictGetEntryKey(de);
2914
2915 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
2916 }
2917 dictReleaseIterator(di);
2918 } else if (o->type == REDIS_ZSET) {
2919 /* Save a set value */
2920 zset *zs = o->ptr;
2921 dictIterator *di = dictGetIterator(zs->dict);
2922 dictEntry *de;
2923
2924 if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) return -1;
2925 while((de = dictNext(di)) != NULL) {
2926 robj *eleobj = dictGetEntryKey(de);
2927 double *score = dictGetEntryVal(de);
2928
2929 if (rdbSaveStringObject(fp,eleobj) == -1) return -1;
2930 if (rdbSaveDoubleValue(fp,*score) == -1) return -1;
2931 }
2932 dictReleaseIterator(di);
2933 } else {
2934 redisAssert(0 != 0);
2935 }
2936 return 0;
2937 }
2938
2939 /* Return the length the object will have on disk if saved with
2940 * the rdbSaveObject() function. Currently we use a trick to get
2941 * this length with very little changes to the code. In the future
2942 * we could switch to a faster solution. */
2943 static off_t rdbSavedObjectLen(robj *o, FILE *fp) {
2944 if (fp == NULL) fp = server.devnull;
2945 rewind(fp);
2946 assert(rdbSaveObject(fp,o) != 1);
2947 return ftello(fp);
2948 }
2949
2950 /* Return the number of pages required to save this object in the swap file */
2951 static off_t rdbSavedObjectPages(robj *o, FILE *fp) {
2952 off_t bytes = rdbSavedObjectLen(o,fp);
2953
2954 return (bytes+(server.vm_page_size-1))/server.vm_page_size;
2955 }
2956
2957 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
2958 static int rdbSave(char *filename) {
2959 dictIterator *di = NULL;
2960 dictEntry *de;
2961 FILE *fp;
2962 char tmpfile[256];
2963 int j;
2964 time_t now = time(NULL);
2965
2966 snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
2967 fp = fopen(tmpfile,"w");
2968 if (!fp) {
2969 redisLog(REDIS_WARNING, "Failed saving the DB: %s", strerror(errno));
2970 return REDIS_ERR;
2971 }
2972 if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
2973 for (j = 0; j < server.dbnum; j++) {
2974 redisDb *db = server.db+j;
2975 dict *d = db->dict;
2976 if (dictSize(d) == 0) continue;
2977 di = dictGetIterator(d);
2978 if (!di) {
2979 fclose(fp);
2980 return REDIS_ERR;
2981 }
2982
2983 /* Write the SELECT DB opcode */
2984 if (rdbSaveType(fp,REDIS_SELECTDB) == -1) goto werr;
2985 if (rdbSaveLen(fp,j) == -1) goto werr;
2986
2987 /* Iterate this DB writing every entry */
2988 while((de = dictNext(di)) != NULL) {
2989 robj *key = dictGetEntryKey(de);
2990 robj *o = dictGetEntryVal(de);
2991 time_t expiretime = getExpire(db,key);
2992
2993 /* Save the expire time */
2994 if (expiretime != -1) {
2995 /* If this key is already expired skip it */
2996 if (expiretime < now) continue;
2997 if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr;
2998 if (rdbSaveTime(fp,expiretime) == -1) goto werr;
2999 }
3000 /* Save the key and associated value. This requires special
3001 * handling if the value is swapped out. */
3002 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
3003 key->storage == REDIS_VM_SWAPPING) {
3004 /* Save type, key, value */
3005 if (rdbSaveType(fp,o->type) == -1) goto werr;
3006 if (rdbSaveStringObject(fp,key) == -1) goto werr;
3007 if (rdbSaveObject(fp,o) == -1) goto werr;
3008 } else {
3009 /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
3010 robj *po;
3011 /* Get a preview of the object in memory */
3012 po = vmPreviewObject(key);
3013 /* Save type, key, value */
3014 if (rdbSaveType(fp,key->vtype) == -1) goto werr;
3015 if (rdbSaveStringObject(fp,key) == -1) goto werr;
3016 if (rdbSaveObject(fp,po) == -1) goto werr;
3017 /* Remove the loaded object from memory */
3018 decrRefCount(po);
3019 }
3020 }
3021 dictReleaseIterator(di);
3022 }
3023 /* EOF opcode */
3024 if (rdbSaveType(fp,REDIS_EOF) == -1) goto werr;
3025
3026 /* Make sure data will not remain on the OS's output buffers */
3027 fflush(fp);
3028 fsync(fileno(fp));
3029 fclose(fp);
3030
3031 /* Use RENAME to make sure the DB file is changed atomically only
3032 * if the generate DB file is ok. */
3033 if (rename(tmpfile,filename) == -1) {
3034 redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno));
3035 unlink(tmpfile);
3036 return REDIS_ERR;
3037 }
3038 redisLog(REDIS_NOTICE,"DB saved on disk");
3039 server.dirty = 0;
3040 server.lastsave = time(NULL);
3041 return REDIS_OK;
3042
3043 werr:
3044 fclose(fp);
3045 unlink(tmpfile);
3046 redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno));
3047 if (di) dictReleaseIterator(di);
3048 return REDIS_ERR;
3049 }
3050
3051 static int rdbSaveBackground(char *filename) {
3052 pid_t childpid;
3053
3054 if (server.bgsavechildpid != -1) return REDIS_ERR;
3055 if ((childpid = fork()) == 0) {
3056 /* Child */
3057 close(server.fd);
3058 if (rdbSave(filename) == REDIS_OK) {
3059 exit(0);
3060 } else {
3061 exit(1);
3062 }
3063 } else {
3064 /* Parent */
3065 if (childpid == -1) {
3066 redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
3067 strerror(errno));
3068 return REDIS_ERR;
3069 }
3070 redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid);
3071 server.bgsavechildpid = childpid;
3072 return REDIS_OK;
3073 }
3074 return REDIS_OK; /* unreached */
3075 }
3076
3077 static void rdbRemoveTempFile(pid_t childpid) {
3078 char tmpfile[256];
3079
3080 snprintf(tmpfile,256,"temp-%d.rdb", (int) childpid);
3081 unlink(tmpfile);
3082 }
3083
3084 static int rdbLoadType(FILE *fp) {
3085 unsigned char type;
3086 if (fread(&type,1,1,fp) == 0) return -1;
3087 return type;
3088 }
3089
3090 static time_t rdbLoadTime(FILE *fp) {
3091 int32_t t32;
3092 if (fread(&t32,4,1,fp) == 0) return -1;
3093 return (time_t) t32;
3094 }
3095
3096 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
3097 * of this file for a description of how this are stored on disk.
3098 *
3099 * isencoded is set to 1 if the readed length is not actually a length but
3100 * an "encoding type", check the above comments for more info */
3101 static uint32_t rdbLoadLen(FILE *fp, int *isencoded) {
3102 unsigned char buf[2];
3103 uint32_t len;
3104 int type;
3105
3106 if (isencoded) *isencoded = 0;
3107 if (fread(buf,1,1,fp) == 0) return REDIS_RDB_LENERR;
3108 type = (buf[0]&0xC0)>>6;
3109 if (type == REDIS_RDB_6BITLEN) {
3110 /* Read a 6 bit len */
3111 return buf[0]&0x3F;
3112 } else if (type == REDIS_RDB_ENCVAL) {
3113 /* Read a 6 bit len encoding type */
3114 if (isencoded) *isencoded = 1;
3115 return buf[0]&0x3F;
3116 } else if (type == REDIS_RDB_14BITLEN) {
3117 /* Read a 14 bit len */
3118 if (fread(buf+1,1,1,fp) == 0) return REDIS_RDB_LENERR;
3119 return ((buf[0]&0x3F)<<8)|buf[1];
3120 } else {
3121 /* Read a 32 bit len */
3122 if (fread(&len,4,1,fp) == 0) return REDIS_RDB_LENERR;
3123 return ntohl(len);
3124 }
3125 }
3126
3127 static robj *rdbLoadIntegerObject(FILE *fp, int enctype) {
3128 unsigned char enc[4];
3129 long long val;
3130
3131 if (enctype == REDIS_RDB_ENC_INT8) {
3132 if (fread(enc,1,1,fp) == 0) return NULL;
3133 val = (signed char)enc[0];
3134 } else if (enctype == REDIS_RDB_ENC_INT16) {
3135 uint16_t v;
3136 if (fread(enc,2,1,fp) == 0) return NULL;
3137 v = enc[0]|(enc[1]<<8);
3138 val = (int16_t)v;
3139 } else if (enctype == REDIS_RDB_ENC_INT32) {
3140 uint32_t v;
3141 if (fread(enc,4,1,fp) == 0) return NULL;
3142 v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24);
3143 val = (int32_t)v;
3144 } else {
3145 val = 0; /* anti-warning */
3146 redisAssert(0!=0);
3147 }
3148 return createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",val));
3149 }
3150
3151 static robj *rdbLoadLzfStringObject(FILE*fp) {
3152 unsigned int len, clen;
3153 unsigned char *c = NULL;
3154 sds val = NULL;
3155
3156 if ((clen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3157 if ((len = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3158 if ((c = zmalloc(clen)) == NULL) goto err;
3159 if ((val = sdsnewlen(NULL,len)) == NULL) goto err;
3160 if (fread(c,clen,1,fp) == 0) goto err;
3161 if (lzf_decompress(c,clen,val,len) == 0) goto err;
3162 zfree(c);
3163 return createObject(REDIS_STRING,val);
3164 err:
3165 zfree(c);
3166 sdsfree(val);
3167 return NULL;
3168 }
3169
3170 static robj *rdbLoadStringObject(FILE*fp) {
3171 int isencoded;
3172 uint32_t len;
3173 sds val;
3174
3175 len = rdbLoadLen(fp,&isencoded);
3176 if (isencoded) {
3177 switch(len) {
3178 case REDIS_RDB_ENC_INT8:
3179 case REDIS_RDB_ENC_INT16:
3180 case REDIS_RDB_ENC_INT32:
3181 return tryObjectSharing(rdbLoadIntegerObject(fp,len));
3182 case REDIS_RDB_ENC_LZF:
3183 return tryObjectSharing(rdbLoadLzfStringObject(fp));
3184 default:
3185 redisAssert(0!=0);
3186 }
3187 }
3188
3189 if (len == REDIS_RDB_LENERR) return NULL;
3190 val = sdsnewlen(NULL,len);
3191 if (len && fread(val,len,1,fp) == 0) {
3192 sdsfree(val);
3193 return NULL;
3194 }
3195 return tryObjectSharing(createObject(REDIS_STRING,val));
3196 }
3197
3198 /* For information about double serialization check rdbSaveDoubleValue() */
3199 static int rdbLoadDoubleValue(FILE *fp, double *val) {
3200 char buf[128];
3201 unsigned char len;
3202
3203 if (fread(&len,1,1,fp) == 0) return -1;
3204 switch(len) {
3205 case 255: *val = R_NegInf; return 0;
3206 case 254: *val = R_PosInf; return 0;
3207 case 253: *val = R_Nan; return 0;
3208 default:
3209 if (fread(buf,len,1,fp) == 0) return -1;
3210 buf[len] = '\0';
3211 sscanf(buf, "%lg", val);
3212 return 0;
3213 }
3214 }
3215
3216 /* Load a Redis object of the specified type from the specified file.
3217 * On success a newly allocated object is returned, otherwise NULL. */
3218 static robj *rdbLoadObject(int type, FILE *fp) {
3219 robj *o;
3220
3221 if (type == REDIS_STRING) {
3222 /* Read string value */
3223 if ((o = rdbLoadStringObject(fp)) == NULL) return NULL;
3224 tryObjectEncoding(o);
3225 } else if (type == REDIS_LIST || type == REDIS_SET) {
3226 /* Read list/set value */
3227 uint32_t listlen;
3228
3229 if ((listlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3230 o = (type == REDIS_LIST) ? createListObject() : createSetObject();
3231 /* Load every single element of the list/set */
3232 while(listlen--) {
3233 robj *ele;
3234
3235 if ((ele = rdbLoadStringObject(fp)) == NULL) return NULL;
3236 tryObjectEncoding(ele);
3237 if (type == REDIS_LIST) {
3238 listAddNodeTail((list*)o->ptr,ele);
3239 } else {
3240 dictAdd((dict*)o->ptr,ele,NULL);
3241 }
3242 }
3243 } else if (type == REDIS_ZSET) {
3244 /* Read list/set value */
3245 uint32_t zsetlen;
3246 zset *zs;
3247
3248 if ((zsetlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
3249 o = createZsetObject();
3250 zs = o->ptr;
3251 /* Load every single element of the list/set */
3252 while(zsetlen--) {
3253 robj *ele;
3254 double *score = zmalloc(sizeof(double));
3255
3256 if ((ele = rdbLoadStringObject(fp)) == NULL) return NULL;
3257 tryObjectEncoding(ele);
3258 if (rdbLoadDoubleValue(fp,score) == -1) return NULL;
3259 dictAdd(zs->dict,ele,score);
3260 zslInsert(zs->zsl,*score,ele);
3261 incrRefCount(ele); /* added to skiplist */
3262 }
3263 } else {
3264 redisAssert(0 != 0);
3265 }
3266 return o;
3267 }
3268
3269 static int rdbLoad(char *filename) {
3270 FILE *fp;
3271 robj *keyobj = NULL;
3272 uint32_t dbid;
3273 int type, retval, rdbver;
3274 dict *d = server.db[0].dict;
3275 redisDb *db = server.db+0;
3276 char buf[1024];
3277 time_t expiretime = -1, now = time(NULL);
3278 long long loadedkeys = 0;
3279
3280 fp = fopen(filename,"r");
3281 if (!fp) return REDIS_ERR;
3282 if (fread(buf,9,1,fp) == 0) goto eoferr;
3283 buf[9] = '\0';
3284 if (memcmp(buf,"REDIS",5) != 0) {
3285 fclose(fp);
3286 redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
3287 return REDIS_ERR;
3288 }
3289 rdbver = atoi(buf+5);
3290 if (rdbver != 1) {
3291 fclose(fp);
3292 redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
3293 return REDIS_ERR;
3294 }
3295 while(1) {
3296 robj *o;
3297
3298 /* Read type. */
3299 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
3300 if (type == REDIS_EXPIRETIME) {
3301 if ((expiretime = rdbLoadTime(fp)) == -1) goto eoferr;
3302 /* We read the time so we need to read the object type again */
3303 if ((type = rdbLoadType(fp)) == -1) goto eoferr;
3304 }
3305 if (type == REDIS_EOF) break;
3306 /* Handle SELECT DB opcode as a special case */
3307 if (type == REDIS_SELECTDB) {
3308 if ((dbid = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR)
3309 goto eoferr;
3310 if (dbid >= (unsigned)server.dbnum) {
3311 redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum);
3312 exit(1);
3313 }
3314 db = server.db+dbid;
3315 d = db->dict;
3316 continue;
3317 }
3318 /* Read key */
3319 if ((keyobj = rdbLoadStringObject(fp)) == NULL) goto eoferr;
3320 /* Read value */
3321 if ((o = rdbLoadObject(type,fp)) == NULL) goto eoferr;
3322 /* Add the new object in the hash table */
3323 retval = dictAdd(d,keyobj,o);
3324 if (retval == DICT_ERR) {
3325 redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", keyobj->ptr);
3326 exit(1);
3327 }
3328 /* Set the expire time if needed */
3329 if (expiretime != -1) {
3330 setExpire(db,keyobj,expiretime);
3331 /* Delete this key if already expired */
3332 if (expiretime < now) deleteKey(db,keyobj);
3333 expiretime = -1;
3334 }
3335 keyobj = o = NULL;
3336 /* Handle swapping while loading big datasets when VM is on */
3337 loadedkeys++;
3338 if (server.vm_enabled && (loadedkeys % 5000) == 0) {
3339 while (zmalloc_used_memory() > server.vm_max_memory) {
3340 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
3341 }
3342 }
3343 }
3344 fclose(fp);
3345 return REDIS_OK;
3346
3347 eoferr: /* unexpected end of file is handled here with a fatal exit */
3348 if (keyobj) decrRefCount(keyobj);
3349 redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
3350 exit(1);
3351 return REDIS_ERR; /* Just to avoid warning */
3352 }
3353
3354 /*================================== Commands =============================== */
3355
3356 static void authCommand(redisClient *c) {
3357 if (!server.requirepass || !strcmp(c->argv[1]->ptr, server.requirepass)) {
3358 c->authenticated = 1;
3359 addReply(c,shared.ok);
3360 } else {
3361 c->authenticated = 0;
3362 addReplySds(c,sdscatprintf(sdsempty(),"-ERR invalid password\r\n"));
3363 }
3364 }
3365
3366 static void pingCommand(redisClient *c) {
3367 addReply(c,shared.pong);
3368 }
3369
3370 static void echoCommand(redisClient *c) {
3371 addReplyBulkLen(c,c->argv[1]);
3372 addReply(c,c->argv[1]);
3373 addReply(c,shared.crlf);
3374 }
3375
3376 /*=================================== Strings =============================== */
3377
3378 static void setGenericCommand(redisClient *c, int nx) {
3379 int retval;
3380
3381 if (nx) deleteIfVolatile(c->db,c->argv[1]);
3382 retval = dictAdd(c->db->dict,c->argv[1],c->argv[2]);
3383 if (retval == DICT_ERR) {
3384 if (!nx) {
3385 /* If the key is about a swapped value, we want a new key object
3386 * to overwrite the old. So we delete the old key in the database.
3387 * This will also make sure that swap pages about the old object
3388 * will be marked as free. */
3389 if (deleteIfSwapped(c->db,c->argv[1]))
3390 incrRefCount(c->argv[1]);
3391 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
3392 incrRefCount(c->argv[2]);
3393 } else {
3394 addReply(c,shared.czero);
3395 return;
3396 }
3397 } else {
3398 incrRefCount(c->argv[1]);
3399 incrRefCount(c->argv[2]);
3400 }
3401 server.dirty++;
3402 removeExpire(c->db,c->argv[1]);
3403 addReply(c, nx ? shared.cone : shared.ok);
3404 }
3405
3406 static void setCommand(redisClient *c) {
3407 setGenericCommand(c,0);
3408 }
3409
3410 static void setnxCommand(redisClient *c) {
3411 setGenericCommand(c,1);
3412 }
3413
3414 static int getGenericCommand(redisClient *c) {
3415 robj *o = lookupKeyRead(c->db,c->argv[1]);
3416
3417 if (o == NULL) {
3418 addReply(c,shared.nullbulk);
3419 return REDIS_OK;
3420 } else {
3421 if (o->type != REDIS_STRING) {
3422 addReply(c,shared.wrongtypeerr);
3423 return REDIS_ERR;
3424 } else {
3425 addReplyBulkLen(c,o);
3426 addReply(c,o);
3427 addReply(c,shared.crlf);
3428 return REDIS_OK;
3429 }
3430 }
3431 }
3432
3433 static void getCommand(redisClient *c) {
3434 getGenericCommand(c);
3435 }
3436
3437 static void getsetCommand(redisClient *c) {
3438 if (getGenericCommand(c) == REDIS_ERR) return;
3439 if (dictAdd(c->db->dict,c->argv[1],c->argv[2]) == DICT_ERR) {
3440 dictReplace(c->db->dict,c->argv[1],c->argv[2]);
3441 } else {
3442 incrRefCount(c->argv[1]);
3443 }
3444 incrRefCount(c->argv[2]);
3445 server.dirty++;
3446 removeExpire(c->db,c->argv[1]);
3447 }
3448
3449 static void mgetCommand(redisClient *c) {
3450 int j;
3451
3452 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->argc-1));
3453 for (j = 1; j < c->argc; j++) {
3454 robj *o = lookupKeyRead(c->db,c->argv[j]);
3455 if (o == NULL) {
3456 addReply(c,shared.nullbulk);
3457 } else {
3458 if (o->type != REDIS_STRING) {
3459 addReply(c,shared.nullbulk);
3460 } else {
3461 addReplyBulkLen(c,o);
3462 addReply(c,o);
3463 addReply(c,shared.crlf);
3464 }
3465 }
3466 }
3467 }
3468
3469 static void msetGenericCommand(redisClient *c, int nx) {
3470 int j, busykeys = 0;
3471
3472 if ((c->argc % 2) == 0) {
3473 addReplySds(c,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
3474 return;
3475 }
3476 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
3477 * set nothing at all if at least one already key exists. */
3478 if (nx) {
3479 for (j = 1; j < c->argc; j += 2) {
3480 if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
3481 busykeys++;
3482 }
3483 }
3484 }
3485 if (busykeys) {
3486 addReply(c, shared.czero);
3487 return;
3488 }
3489
3490 for (j = 1; j < c->argc; j += 2) {
3491 int retval;
3492
3493 tryObjectEncoding(c->argv[j+1]);
3494 retval = dictAdd(c->db->dict,c->argv[j],c->argv[j+1]);
3495 if (retval == DICT_ERR) {
3496 dictReplace(c->db->dict,c->argv[j],c->argv[j+1]);
3497 incrRefCount(c->argv[j+1]);
3498 } else {
3499 incrRefCount(c->argv[j]);
3500 incrRefCount(c->argv[j+1]);
3501 }
3502 removeExpire(c->db,c->argv[j]);
3503 }
3504 server.dirty += (c->argc-1)/2;
3505 addReply(c, nx ? shared.cone : shared.ok);
3506 }
3507
3508 static void msetCommand(redisClient *c) {
3509 msetGenericCommand(c,0);
3510 }
3511
3512 static void msetnxCommand(redisClient *c) {
3513 msetGenericCommand(c,1);
3514 }
3515
3516 static void incrDecrCommand(redisClient *c, long long incr) {
3517 long long value;
3518 int retval;
3519 robj *o;
3520
3521 o = lookupKeyWrite(c->db,c->argv[1]);
3522 if (o == NULL) {
3523 value = 0;
3524 } else {
3525 if (o->type != REDIS_STRING) {
3526 value = 0;
3527 } else {
3528 char *eptr;
3529
3530 if (o->encoding == REDIS_ENCODING_RAW)
3531 value = strtoll(o->ptr, &eptr, 10);
3532 else if (o->encoding == REDIS_ENCODING_INT)
3533 value = (long)o->ptr;
3534 else
3535 redisAssert(1 != 1);
3536 }
3537 }
3538
3539 value += incr;
3540 o = createObject(REDIS_STRING,sdscatprintf(sdsempty(),"%lld",value));
3541 tryObjectEncoding(o);
3542 retval = dictAdd(c->db->dict,c->argv[1],o);
3543 if (retval == DICT_ERR) {
3544 dictReplace(c->db->dict,c->argv[1],o);
3545 removeExpire(c->db,c->argv[1]);
3546 } else {
3547 incrRefCount(c->argv[1]);
3548 }
3549 server.dirty++;
3550 addReply(c,shared.colon);
3551 addReply(c,o);
3552 addReply(c,shared.crlf);
3553 }
3554
3555 static void incrCommand(redisClient *c) {
3556 incrDecrCommand(c,1);
3557 }
3558
3559 static void decrCommand(redisClient *c) {
3560 incrDecrCommand(c,-1);
3561 }
3562
3563 static void incrbyCommand(redisClient *c) {
3564 long long incr = strtoll(c->argv[2]->ptr, NULL, 10);
3565 incrDecrCommand(c,incr);
3566 }
3567
3568 static void decrbyCommand(redisClient *c) {
3569 long long incr = strtoll(c->argv[2]->ptr, NULL, 10);
3570 incrDecrCommand(c,-incr);
3571 }
3572
3573 /* ========================= Type agnostic commands ========================= */
3574
3575 static void delCommand(redisClient *c) {
3576 int deleted = 0, j;
3577
3578 for (j = 1; j < c->argc; j++) {
3579 if (deleteKey(c->db,c->argv[j])) {
3580 server.dirty++;
3581 deleted++;
3582 }
3583 }
3584 switch(deleted) {
3585 case 0:
3586 addReply(c,shared.czero);
3587 break;
3588 case 1:
3589 addReply(c,shared.cone);
3590 break;
3591 default:
3592 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",deleted));
3593 break;
3594 }
3595 }
3596
3597 static void existsCommand(redisClient *c) {
3598 addReply(c,lookupKeyRead(c->db,c->argv[1]) ? shared.cone : shared.czero);
3599 }
3600
3601 static void selectCommand(redisClient *c) {
3602 int id = atoi(c->argv[1]->ptr);
3603
3604 if (selectDb(c,id) == REDIS_ERR) {
3605 addReplySds(c,sdsnew("-ERR invalid DB index\r\n"));
3606 } else {
3607 addReply(c,shared.ok);
3608 }
3609 }
3610
3611 static void randomkeyCommand(redisClient *c) {
3612 dictEntry *de;
3613
3614 while(1) {
3615 de = dictGetRandomKey(c->db->dict);
3616 if (!de || expireIfNeeded(c->db,dictGetEntryKey(de)) == 0) break;
3617 }
3618 if (de == NULL) {
3619 addReply(c,shared.plus);
3620 addReply(c,shared.crlf);
3621 } else {
3622 addReply(c,shared.plus);
3623 addReply(c,dictGetEntryKey(de));
3624 addReply(c,shared.crlf);
3625 }
3626 }
3627
3628 static void keysCommand(redisClient *c) {
3629 dictIterator *di;
3630 dictEntry *de;
3631 sds pattern = c->argv[1]->ptr;
3632 int plen = sdslen(pattern);
3633 unsigned long numkeys = 0, keyslen = 0;
3634 robj *lenobj = createObject(REDIS_STRING,NULL);
3635
3636 di = dictGetIterator(c->db->dict);
3637 addReply(c,lenobj);
3638 decrRefCount(lenobj);
3639 while((de = dictNext(di)) != NULL) {
3640 robj *keyobj = dictGetEntryKey(de);
3641
3642 sds key = keyobj->ptr;
3643 if ((pattern[0] == '*' && pattern[1] == '\0') ||
3644 stringmatchlen(pattern,plen,key,sdslen(key),0)) {
3645 if (expireIfNeeded(c->db,keyobj) == 0) {
3646 if (numkeys != 0)
3647 addReply(c,shared.space);
3648 addReply(c,keyobj);
3649 numkeys++;
3650 keyslen += sdslen(key);
3651 }
3652 }
3653 }
3654 dictReleaseIterator(di);
3655 lenobj->ptr = sdscatprintf(sdsempty(),"$%lu\r\n",keyslen+(numkeys ? (numkeys-1) : 0));
3656 addReply(c,shared.crlf);
3657 }
3658
3659 static void dbsizeCommand(redisClient *c) {
3660 addReplySds(c,
3661 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c->db->dict)));
3662 }
3663
3664 static void lastsaveCommand(redisClient *c) {
3665 addReplySds(c,
3666 sdscatprintf(sdsempty(),":%lu\r\n",server.lastsave));
3667 }
3668
3669 static void typeCommand(redisClient *c) {
3670 robj *o;
3671 char *type;
3672
3673 o = lookupKeyRead(c->db,c->argv[1]);
3674 if (o == NULL) {
3675 type = "+none";
3676 } else {
3677 switch(o->type) {
3678 case REDIS_STRING: type = "+string"; break;
3679 case REDIS_LIST: type = "+list"; break;
3680 case REDIS_SET: type = "+set"; break;
3681 case REDIS_ZSET: type = "+zset"; break;
3682 default: type = "unknown"; break;
3683 }
3684 }
3685 addReplySds(c,sdsnew(type));
3686 addReply(c,shared.crlf);
3687 }
3688
3689 static void saveCommand(redisClient *c) {
3690 if (server.bgsavechildpid != -1) {
3691 addReplySds(c,sdsnew("-ERR background save in progress\r\n"));
3692 return;
3693 }
3694 if (rdbSave(server.dbfilename) == REDIS_OK) {
3695 addReply(c,shared.ok);
3696 } else {
3697 addReply(c,shared.err);
3698 }
3699 }
3700
3701 static void bgsaveCommand(redisClient *c) {
3702 if (server.bgsavechildpid != -1) {
3703 addReplySds(c,sdsnew("-ERR background save already in progress\r\n"));
3704 return;
3705 }
3706 if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
3707 char *status = "+Background saving started\r\n";
3708 addReplySds(c,sdsnew(status));
3709 } else {
3710 addReply(c,shared.err);
3711 }
3712 }
3713
3714 static void shutdownCommand(redisClient *c) {
3715 redisLog(REDIS_WARNING,"User requested shutdown, saving DB...");
3716 /* Kill the saving child if there is a background saving in progress.
3717 We want to avoid race conditions, for instance our saving child may
3718 overwrite the synchronous saving did by SHUTDOWN. */
3719 if (server.bgsavechildpid != -1) {
3720 redisLog(REDIS_WARNING,"There is a live saving child. Killing it!");
3721 kill(server.bgsavechildpid,SIGKILL);
3722 rdbRemoveTempFile(server.bgsavechildpid);
3723 }
3724 if (server.appendonly) {
3725 /* Append only file: fsync() the AOF and exit */
3726 fsync(server.appendfd);
3727 exit(0);
3728 } else {
3729 /* Snapshotting. Perform a SYNC SAVE and exit */
3730 if (rdbSave(server.dbfilename) == REDIS_OK) {
3731 if (server.daemonize)
3732 unlink(server.pidfile);
3733 redisLog(REDIS_WARNING,"%zu bytes used at exit",zmalloc_used_memory());
3734 redisLog(REDIS_WARNING,"Server exit now, bye bye...");
3735 exit(0);
3736 } else {
3737 /* Ooops.. error saving! The best we can do is to continue operating.
3738 * Note that if there was a background saving process, in the next
3739 * cron() Redis will be notified that the background saving aborted,
3740 * handling special stuff like slaves pending for synchronization... */
3741 redisLog(REDIS_WARNING,"Error trying to save the DB, can't exit");
3742 addReplySds(c,sdsnew("-ERR can't quit, problems saving the DB\r\n"));
3743 }
3744 }
3745 }
3746
3747 static void renameGenericCommand(redisClient *c, int nx) {
3748 robj *o;
3749
3750 /* To use the same key as src and dst is probably an error */
3751 if (sdscmp(c->argv[1]->ptr,c->argv[2]->ptr) == 0) {
3752 addReply(c,shared.sameobjecterr);
3753 return;
3754 }
3755
3756 o = lookupKeyWrite(c->db,c->argv[1]);
3757 if (o == NULL) {
3758 addReply(c,shared.nokeyerr);
3759 return;
3760 }
3761 incrRefCount(o);
3762 deleteIfVolatile(c->db,c->argv[2]);
3763 if (dictAdd(c->db->dict,c->argv[2],o) == DICT_ERR) {
3764 if (nx) {
3765 decrRefCount(o);
3766 addReply(c,shared.czero);
3767 return;
3768 }
3769 dictReplace(c->db->dict,c->argv[2],o);
3770 } else {
3771 incrRefCount(c->argv[2]);
3772 }
3773 deleteKey(c->db,c->argv[1]);
3774 server.dirty++;
3775 addReply(c,nx ? shared.cone : shared.ok);
3776 }
3777
3778 static void renameCommand(redisClient *c) {
3779 renameGenericCommand(c,0);
3780 }
3781
3782 static void renamenxCommand(redisClient *c) {
3783 renameGenericCommand(c,1);
3784 }
3785
3786 static void moveCommand(redisClient *c) {
3787 robj *o;
3788 redisDb *src, *dst;
3789 int srcid;
3790
3791 /* Obtain source and target DB pointers */
3792 src = c->db;
3793 srcid = c->db->id;
3794 if (selectDb(c,atoi(c->argv[2]->ptr)) == REDIS_ERR) {
3795 addReply(c,shared.outofrangeerr);
3796 return;
3797 }
3798 dst = c->db;
3799 selectDb(c,srcid); /* Back to the source DB */
3800
3801 /* If the user is moving using as target the same
3802 * DB as the source DB it is probably an error. */
3803 if (src == dst) {
3804 addReply(c,shared.sameobjecterr);
3805 return;
3806 }
3807
3808 /* Check if the element exists and get a reference */
3809 o = lookupKeyWrite(c->db,c->argv[1]);
3810 if (!o) {
3811 addReply(c,shared.czero);
3812 return;
3813 }
3814
3815 /* Try to add the element to the target DB */
3816 deleteIfVolatile(dst,c->argv[1]);
3817 if (dictAdd(dst->dict,c->argv[1],o) == DICT_ERR) {
3818 addReply(c,shared.czero);
3819 return;
3820 }
3821 incrRefCount(c->argv[1]);
3822 incrRefCount(o);
3823
3824 /* OK! key moved, free the entry in the source DB */
3825 deleteKey(src,c->argv[1]);
3826 server.dirty++;
3827 addReply(c,shared.cone);
3828 }
3829
3830 /* =================================== Lists ================================ */
3831 static void pushGenericCommand(redisClient *c, int where) {
3832 robj *lobj;
3833 list *list;
3834
3835 lobj = lookupKeyWrite(c->db,c->argv[1]);
3836 if (lobj == NULL) {
3837 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
3838 addReply(c,shared.ok);
3839 return;
3840 }
3841 lobj = createListObject();
3842 list = lobj->ptr;
3843 if (where == REDIS_HEAD) {
3844 listAddNodeHead(list,c->argv[2]);
3845 } else {
3846 listAddNodeTail(list,c->argv[2]);
3847 }
3848 dictAdd(c->db->dict,c->argv[1],lobj);
3849 incrRefCount(c->argv[1]);
3850 incrRefCount(c->argv[2]);
3851 } else {
3852 if (lobj->type != REDIS_LIST) {
3853 addReply(c,shared.wrongtypeerr);
3854 return;
3855 }
3856 if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
3857 addReply(c,shared.ok);
3858 return;
3859 }
3860 list = lobj->ptr;
3861 if (where == REDIS_HEAD) {
3862 listAddNodeHead(list,c->argv[2]);
3863 } else {
3864 listAddNodeTail(list,c->argv[2]);
3865 }
3866 incrRefCount(c->argv[2]);
3867 }
3868 server.dirty++;
3869 addReply(c,shared.ok);
3870 }
3871
3872 static void lpushCommand(redisClient *c) {
3873 pushGenericCommand(c,REDIS_HEAD);
3874 }
3875
3876 static void rpushCommand(redisClient *c) {
3877 pushGenericCommand(c,REDIS_TAIL);
3878 }
3879
3880 static void llenCommand(redisClient *c) {
3881 robj *o;
3882 list *l;
3883
3884 o = lookupKeyRead(c->db,c->argv[1]);
3885 if (o == NULL) {
3886 addReply(c,shared.czero);
3887 return;
3888 } else {
3889 if (o->type != REDIS_LIST) {
3890 addReply(c,shared.wrongtypeerr);
3891 } else {
3892 l = o->ptr;
3893 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",listLength(l)));
3894 }
3895 }
3896 }
3897
3898 static void lindexCommand(redisClient *c) {
3899 robj *o;
3900 int index = atoi(c->argv[2]->ptr);
3901
3902 o = lookupKeyRead(c->db,c->argv[1]);
3903 if (o == NULL) {
3904 addReply(c,shared.nullbulk);
3905 } else {
3906 if (o->type != REDIS_LIST) {
3907 addReply(c,shared.wrongtypeerr);
3908 } else {
3909 list *list = o->ptr;
3910 listNode *ln;
3911
3912 ln = listIndex(list, index);
3913 if (ln == NULL) {
3914 addReply(c,shared.nullbulk);
3915 } else {
3916 robj *ele = listNodeValue(ln);
3917 addReplyBulkLen(c,ele);
3918 addReply(c,ele);
3919 addReply(c,shared.crlf);
3920 }
3921 }
3922 }
3923 }
3924
3925 static void lsetCommand(redisClient *c) {
3926 robj *o;
3927 int index = atoi(c->argv[2]->ptr);
3928
3929 o = lookupKeyWrite(c->db,c->argv[1]);
3930 if (o == NULL) {
3931 addReply(c,shared.nokeyerr);
3932 } else {
3933 if (o->type != REDIS_LIST) {
3934 addReply(c,shared.wrongtypeerr);
3935 } else {
3936 list *list = o->ptr;
3937 listNode *ln;
3938
3939 ln = listIndex(list, index);
3940 if (ln == NULL) {
3941 addReply(c,shared.outofrangeerr);
3942 } else {
3943 robj *ele = listNodeValue(ln);
3944
3945 decrRefCount(ele);
3946 listNodeValue(ln) = c->argv[3];
3947 incrRefCount(c->argv[3]);
3948 addReply(c,shared.ok);
3949 server.dirty++;
3950 }
3951 }
3952 }
3953 }
3954
3955 static void popGenericCommand(redisClient *c, int where) {
3956 robj *o;
3957
3958 o = lookupKeyWrite(c->db,c->argv[1]);
3959 if (o == NULL) {
3960 addReply(c,shared.nullbulk);
3961 } else {
3962 if (o->type != REDIS_LIST) {
3963 addReply(c,shared.wrongtypeerr);
3964 } else {
3965 list *list = o->ptr;
3966 listNode *ln;
3967
3968 if (where == REDIS_HEAD)
3969 ln = listFirst(list);
3970 else
3971 ln = listLast(list);
3972
3973 if (ln == NULL) {
3974 addReply(c,shared.nullbulk);
3975 } else {
3976 robj *ele = listNodeValue(ln);
3977 addReplyBulkLen(c,ele);
3978 addReply(c,ele);
3979 addReply(c,shared.crlf);
3980 listDelNode(list,ln);
3981 server.dirty++;
3982 }
3983 }
3984 }
3985 }
3986
3987 static void lpopCommand(redisClient *c) {
3988 popGenericCommand(c,REDIS_HEAD);
3989 }
3990
3991 static void rpopCommand(redisClient *c) {
3992 popGenericCommand(c,REDIS_TAIL);
3993 }
3994
3995 static void lrangeCommand(redisClient *c) {
3996 robj *o;
3997 int start = atoi(c->argv[2]->ptr);
3998 int end = atoi(c->argv[3]->ptr);
3999
4000 o = lookupKeyRead(c->db,c->argv[1]);
4001 if (o == NULL) {
4002 addReply(c,shared.nullmultibulk);
4003 } else {
4004 if (o->type != REDIS_LIST) {
4005 addReply(c,shared.wrongtypeerr);
4006 } else {
4007 list *list = o->ptr;
4008 listNode *ln;
4009 int llen = listLength(list);
4010 int rangelen, j;
4011 robj *ele;
4012
4013 /* convert negative indexes */
4014 if (start < 0) start = llen+start;
4015 if (end < 0) end = llen+end;
4016 if (start < 0) start = 0;
4017 if (end < 0) end = 0;
4018
4019 /* indexes sanity checks */
4020 if (start > end || start >= llen) {
4021 /* Out of range start or start > end result in empty list */
4022 addReply(c,shared.emptymultibulk);
4023 return;
4024 }
4025 if (end >= llen) end = llen-1;
4026 rangelen = (end-start)+1;
4027
4028 /* Return the result in form of a multi-bulk reply */
4029 ln = listIndex(list, start);
4030 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",rangelen));
4031 for (j = 0; j < rangelen; j++) {
4032 ele = listNodeValue(ln);
4033 addReplyBulkLen(c,ele);
4034 addReply(c,ele);
4035 addReply(c,shared.crlf);
4036 ln = ln->next;
4037 }
4038 }
4039 }
4040 }
4041
4042 static void ltrimCommand(redisClient *c) {
4043 robj *o;
4044 int start = atoi(c->argv[2]->ptr);
4045 int end = atoi(c->argv[3]->ptr);
4046
4047 o = lookupKeyWrite(c->db,c->argv[1]);
4048 if (o == NULL) {
4049 addReply(c,shared.ok);
4050 } else {
4051 if (o->type != REDIS_LIST) {
4052 addReply(c,shared.wrongtypeerr);
4053 } else {
4054 list *list = o->ptr;
4055 listNode *ln;
4056 int llen = listLength(list);
4057 int j, ltrim, rtrim;
4058
4059 /* convert negative indexes */
4060 if (start < 0) start = llen+start;
4061 if (end < 0) end = llen+end;
4062 if (start < 0) start = 0;
4063 if (end < 0) end = 0;
4064
4065 /* indexes sanity checks */
4066 if (start > end || start >= llen) {
4067 /* Out of range start or start > end result in empty list */
4068 ltrim = llen;
4069 rtrim = 0;
4070 } else {
4071 if (end >= llen) end = llen-1;
4072 ltrim = start;
4073 rtrim = llen-end-1;
4074 }
4075
4076 /* Remove list elements to perform the trim */
4077 for (j = 0; j < ltrim; j++) {
4078 ln = listFirst(list);
4079 listDelNode(list,ln);
4080 }
4081 for (j = 0; j < rtrim; j++) {
4082 ln = listLast(list);
4083 listDelNode(list,ln);
4084 }
4085 server.dirty++;
4086 addReply(c,shared.ok);
4087 }
4088 }
4089 }
4090
4091 static void lremCommand(redisClient *c) {
4092 robj *o;
4093
4094 o = lookupKeyWrite(c->db,c->argv[1]);
4095 if (o == NULL) {
4096 addReply(c,shared.czero);
4097 } else {
4098 if (o->type != REDIS_LIST) {
4099 addReply(c,shared.wrongtypeerr);
4100 } else {
4101 list *list = o->ptr;
4102 listNode *ln, *next;
4103 int toremove = atoi(c->argv[2]->ptr);
4104 int removed = 0;
4105 int fromtail = 0;
4106
4107 if (toremove < 0) {
4108 toremove = -toremove;
4109 fromtail = 1;
4110 }
4111 ln = fromtail ? list->tail : list->head;
4112 while (ln) {
4113 robj *ele = listNodeValue(ln);
4114
4115 next = fromtail ? ln->prev : ln->next;
4116 if (compareStringObjects(ele,c->argv[3]) == 0) {
4117 listDelNode(list,ln);
4118 server.dirty++;
4119 removed++;
4120 if (toremove && removed == toremove) break;
4121 }
4122 ln = next;
4123 }
4124 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",removed));
4125 }
4126 }
4127 }
4128
4129 /* This is the semantic of this command:
4130 * RPOPLPUSH srclist dstlist:
4131 * IF LLEN(srclist) > 0
4132 * element = RPOP srclist
4133 * LPUSH dstlist element
4134 * RETURN element
4135 * ELSE
4136 * RETURN nil
4137 * END
4138 * END
4139 *
4140 * The idea is to be able to get an element from a list in a reliable way
4141 * since the element is not just returned but pushed against another list
4142 * as well. This command was originally proposed by Ezra Zygmuntowicz.
4143 */
4144 static void rpoplpushcommand(redisClient *c) {
4145 robj *sobj;
4146
4147 sobj = lookupKeyWrite(c->db,c->argv[1]);
4148 if (sobj == NULL) {
4149 addReply(c,shared.nullbulk);
4150 } else {
4151 if (sobj->type != REDIS_LIST) {
4152 addReply(c,shared.wrongtypeerr);
4153 } else {
4154 list *srclist = sobj->ptr;
4155 listNode *ln = listLast(srclist);
4156
4157 if (ln == NULL) {
4158 addReply(c,shared.nullbulk);
4159 } else {
4160 robj *dobj = lookupKeyWrite(c->db,c->argv[2]);
4161 robj *ele = listNodeValue(ln);
4162 list *dstlist;
4163
4164 if (dobj && dobj->type != REDIS_LIST) {
4165 addReply(c,shared.wrongtypeerr);
4166 return;
4167 }
4168
4169 /* Add the element to the target list (unless it's directly
4170 * passed to some BLPOP-ing client */
4171 if (!handleClientsWaitingListPush(c,c->argv[2],ele)) {
4172 if (dobj == NULL) {
4173 /* Create the list if the key does not exist */
4174 dobj = createListObject();
4175 dictAdd(c->db->dict,c->argv[2],dobj);
4176 incrRefCount(c->argv[2]);
4177 }
4178 dstlist = dobj->ptr;
4179 listAddNodeHead(dstlist,ele);
4180 incrRefCount(ele);
4181 }
4182
4183 /* Send the element to the client as reply as well */
4184 addReplyBulkLen(c,ele);
4185 addReply(c,ele);
4186 addReply(c,shared.crlf);
4187
4188 /* Finally remove the element from the source list */
4189 listDelNode(srclist,ln);
4190 server.dirty++;
4191 }
4192 }
4193 }
4194 }
4195
4196
4197 /* ==================================== Sets ================================ */
4198
4199 static void saddCommand(redisClient *c) {
4200 robj *set;
4201
4202 set = lookupKeyWrite(c->db,c->argv[1]);
4203 if (set == NULL) {
4204 set = createSetObject();
4205 dictAdd(c->db->dict,c->argv[1],set);
4206 incrRefCount(c->argv[1]);
4207 } else {
4208 if (set->type != REDIS_SET) {
4209 addReply(c,shared.wrongtypeerr);
4210 return;
4211 }
4212 }
4213 if (dictAdd(set->ptr,c->argv[2],NULL) == DICT_OK) {
4214 incrRefCount(c->argv[2]);
4215 server.dirty++;
4216 addReply(c,shared.cone);
4217 } else {
4218 addReply(c,shared.czero);
4219 }
4220 }
4221
4222 static void sremCommand(redisClient *c) {
4223 robj *set;
4224
4225 set = lookupKeyWrite(c->db,c->argv[1]);
4226 if (set == NULL) {
4227 addReply(c,shared.czero);
4228 } else {
4229 if (set->type != REDIS_SET) {
4230 addReply(c,shared.wrongtypeerr);
4231 return;
4232 }
4233 if (dictDelete(set->ptr,c->argv[2]) == DICT_OK) {
4234 server.dirty++;
4235 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
4236 addReply(c,shared.cone);
4237 } else {
4238 addReply(c,shared.czero);
4239 }
4240 }
4241 }
4242
4243 static void smoveCommand(redisClient *c) {
4244 robj *srcset, *dstset;
4245
4246 srcset = lookupKeyWrite(c->db,c->argv[1]);
4247 dstset = lookupKeyWrite(c->db,c->argv[2]);
4248
4249 /* If the source key does not exist return 0, if it's of the wrong type
4250 * raise an error */
4251 if (srcset == NULL || srcset->type != REDIS_SET) {
4252 addReply(c, srcset ? shared.wrongtypeerr : shared.czero);
4253 return;
4254 }
4255 /* Error if the destination key is not a set as well */
4256 if (dstset && dstset->type != REDIS_SET) {
4257 addReply(c,shared.wrongtypeerr);
4258 return;
4259 }
4260 /* Remove the element from the source set */
4261 if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) {
4262 /* Key not found in the src set! return zero */
4263 addReply(c,shared.czero);
4264 return;
4265 }
4266 server.dirty++;
4267 /* Add the element to the destination set */
4268 if (!dstset) {
4269 dstset = createSetObject();
4270 dictAdd(c->db->dict,c->argv[2],dstset);
4271 incrRefCount(c->argv[2]);
4272 }
4273 if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK)
4274 incrRefCount(c->argv[3]);
4275 addReply(c,shared.cone);
4276 }
4277
4278 static void sismemberCommand(redisClient *c) {
4279 robj *set;
4280
4281 set = lookupKeyRead(c->db,c->argv[1]);
4282 if (set == NULL) {
4283 addReply(c,shared.czero);
4284 } else {
4285 if (set->type != REDIS_SET) {
4286 addReply(c,shared.wrongtypeerr);
4287 return;
4288 }
4289 if (dictFind(set->ptr,c->argv[2]))
4290 addReply(c,shared.cone);
4291 else
4292 addReply(c,shared.czero);
4293 }
4294 }
4295
4296 static void scardCommand(redisClient *c) {
4297 robj *o;
4298 dict *s;
4299
4300 o = lookupKeyRead(c->db,c->argv[1]);
4301 if (o == NULL) {
4302 addReply(c,shared.czero);
4303 return;
4304 } else {
4305 if (o->type != REDIS_SET) {
4306 addReply(c,shared.wrongtypeerr);
4307 } else {
4308 s = o->ptr;
4309 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
4310 dictSize(s)));
4311 }
4312 }
4313 }
4314
4315 static void spopCommand(redisClient *c) {
4316 robj *set;
4317 dictEntry *de;
4318
4319 set = lookupKeyWrite(c->db,c->argv[1]);
4320 if (set == NULL) {
4321 addReply(c,shared.nullbulk);
4322 } else {
4323 if (set->type != REDIS_SET) {
4324 addReply(c,shared.wrongtypeerr);
4325 return;
4326 }
4327 de = dictGetRandomKey(set->ptr);
4328 if (de == NULL) {
4329 addReply(c,shared.nullbulk);
4330 } else {
4331 robj *ele = dictGetEntryKey(de);
4332
4333 addReplyBulkLen(c,ele);
4334 addReply(c,ele);
4335 addReply(c,shared.crlf);
4336 dictDelete(set->ptr,ele);
4337 if (htNeedsResize(set->ptr)) dictResize(set->ptr);
4338 server.dirty++;
4339 }
4340 }
4341 }
4342
4343 static void srandmemberCommand(redisClient *c) {
4344 robj *set;
4345 dictEntry *de;
4346
4347 set = lookupKeyRead(c->db,c->argv[1]);
4348 if (set == NULL) {
4349 addReply(c,shared.nullbulk);
4350 } else {
4351 if (set->type != REDIS_SET) {
4352 addReply(c,shared.wrongtypeerr);
4353 return;
4354 }
4355 de = dictGetRandomKey(set->ptr);
4356 if (de == NULL) {
4357 addReply(c,shared.nullbulk);
4358 } else {
4359 robj *ele = dictGetEntryKey(de);
4360
4361 addReplyBulkLen(c,ele);
4362 addReply(c,ele);
4363 addReply(c,shared.crlf);
4364 }
4365 }
4366 }
4367
4368 static int qsortCompareSetsByCardinality(const void *s1, const void *s2) {
4369 dict **d1 = (void*) s1, **d2 = (void*) s2;
4370
4371 return dictSize(*d1)-dictSize(*d2);
4372 }
4373
4374 static void sinterGenericCommand(redisClient *c, robj **setskeys, unsigned long setsnum, robj *dstkey) {
4375 dict **dv = zmalloc(sizeof(dict*)*setsnum);
4376 dictIterator *di;
4377 dictEntry *de;
4378 robj *lenobj = NULL, *dstset = NULL;
4379 unsigned long j, cardinality = 0;
4380
4381 for (j = 0; j < setsnum; j++) {
4382 robj *setobj;
4383
4384 setobj = dstkey ?
4385 lookupKeyWrite(c->db,setskeys[j]) :
4386 lookupKeyRead(c->db,setskeys[j]);
4387 if (!setobj) {
4388 zfree(dv);
4389 if (dstkey) {
4390 if (deleteKey(c->db,dstkey))
4391 server.dirty++;
4392 addReply(c,shared.czero);
4393 } else {
4394 addReply(c,shared.nullmultibulk);
4395 }
4396 return;
4397 }
4398 if (setobj->type != REDIS_SET) {
4399 zfree(dv);
4400 addReply(c,shared.wrongtypeerr);
4401 return;
4402 }
4403 dv[j] = setobj->ptr;
4404 }
4405 /* Sort sets from the smallest to largest, this will improve our
4406 * algorithm's performace */
4407 qsort(dv,setsnum,sizeof(dict*),qsortCompareSetsByCardinality);
4408
4409 /* The first thing we should output is the total number of elements...
4410 * since this is a multi-bulk write, but at this stage we don't know
4411 * the intersection set size, so we use a trick, append an empty object
4412 * to the output list and save the pointer to later modify it with the
4413 * right length */
4414 if (!dstkey) {
4415 lenobj = createObject(REDIS_STRING,NULL);
4416 addReply(c,lenobj);
4417 decrRefCount(lenobj);
4418 } else {
4419 /* If we have a target key where to store the resulting set
4420 * create this key with an empty set inside */
4421 dstset = createSetObject();
4422 }
4423
4424 /* Iterate all the elements of the first (smallest) set, and test
4425 * the element against all the other sets, if at least one set does
4426 * not include the element it is discarded */
4427 di = dictGetIterator(dv[0]);
4428
4429 while((de = dictNext(di)) != NULL) {
4430 robj *ele;
4431
4432 for (j = 1; j < setsnum; j++)
4433 if (dictFind(dv[j],dictGetEntryKey(de)) == NULL) break;
4434 if (j != setsnum)
4435 continue; /* at least one set does not contain the member */
4436 ele = dictGetEntryKey(de);
4437 if (!dstkey) {
4438 addReplyBulkLen(c,ele);
4439 addReply(c,ele);
4440 addReply(c,shared.crlf);
4441 cardinality++;
4442 } else {
4443 dictAdd(dstset->ptr,ele,NULL);
4444 incrRefCount(ele);
4445 }
4446 }
4447 dictReleaseIterator(di);
4448
4449 if (dstkey) {
4450 /* Store the resulting set into the target */
4451 deleteKey(c->db,dstkey);
4452 dictAdd(c->db->dict,dstkey,dstset);
4453 incrRefCount(dstkey);
4454 }
4455
4456 if (!dstkey) {
4457 lenobj->ptr = sdscatprintf(sdsempty(),"*%lu\r\n",cardinality);
4458 } else {
4459 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
4460 dictSize((dict*)dstset->ptr)));
4461 server.dirty++;
4462 }
4463 zfree(dv);
4464 }
4465
4466 static void sinterCommand(redisClient *c) {
4467 sinterGenericCommand(c,c->argv+1,c->argc-1,NULL);
4468 }
4469
4470 static void sinterstoreCommand(redisClient *c) {
4471 sinterGenericCommand(c,c->argv+2,c->argc-2,c->argv[1]);
4472 }
4473
4474 #define REDIS_OP_UNION 0
4475 #define REDIS_OP_DIFF 1
4476
4477 static void sunionDiffGenericCommand(redisClient *c, robj **setskeys, int setsnum, robj *dstkey, int op) {
4478 dict **dv = zmalloc(sizeof(dict*)*setsnum);
4479 dictIterator *di;
4480 dictEntry *de;
4481 robj *dstset = NULL;
4482 int j, cardinality = 0;
4483
4484 for (j = 0; j < setsnum; j++) {
4485 robj *setobj;
4486
4487 setobj = dstkey ?
4488 lookupKeyWrite(c->db,setskeys[j]) :
4489 lookupKeyRead(c->db,setskeys[j]);
4490 if (!setobj) {
4491 dv[j] = NULL;
4492 continue;
4493 }
4494 if (setobj->type != REDIS_SET) {
4495 zfree(dv);
4496 addReply(c,shared.wrongtypeerr);
4497 return;
4498 }
4499 dv[j] = setobj->ptr;
4500 }
4501
4502 /* We need a temp set object to store our union. If the dstkey
4503 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
4504 * this set object will be the resulting object to set into the target key*/
4505 dstset = createSetObject();
4506
4507 /* Iterate all the elements of all the sets, add every element a single
4508 * time to the result set */
4509 for (j = 0; j < setsnum; j++) {
4510 if (op == REDIS_OP_DIFF && j == 0 && !dv[j]) break; /* result set is empty */
4511 if (!dv[j]) continue; /* non existing keys are like empty sets */
4512
4513 di = dictGetIterator(dv[j]);
4514
4515 while((de = dictNext(di)) != NULL) {
4516 robj *ele;
4517
4518 /* dictAdd will not add the same element multiple times */
4519 ele = dictGetEntryKey(de);
4520 if (op == REDIS_OP_UNION || j == 0) {
4521 if (dictAdd(dstset->ptr,ele,NULL) == DICT_OK) {
4522 incrRefCount(ele);
4523 cardinality++;
4524 }
4525 } else if (op == REDIS_OP_DIFF) {
4526 if (dictDelete(dstset->ptr,ele) == DICT_OK) {
4527 cardinality--;
4528 }
4529 }
4530 }
4531 dictReleaseIterator(di);
4532
4533 if (op == REDIS_OP_DIFF && cardinality == 0) break; /* result set is empty */
4534 }
4535
4536 /* Output the content of the resulting set, if not in STORE mode */
4537 if (!dstkey) {
4538 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",cardinality));
4539 di = dictGetIterator(dstset->ptr);
4540 while((de = dictNext(di)) != NULL) {
4541 robj *ele;
4542
4543 ele = dictGetEntryKey(de);
4544 addReplyBulkLen(c,ele);
4545 addReply(c,ele);
4546 addReply(c,shared.crlf);
4547 }
4548 dictReleaseIterator(di);
4549 } else {
4550 /* If we have a target key where to store the resulting set
4551 * create this key with the result set inside */
4552 deleteKey(c->db,dstkey);
4553 dictAdd(c->db->dict,dstkey,dstset);
4554 incrRefCount(dstkey);
4555 }
4556
4557 /* Cleanup */
4558 if (!dstkey) {
4559 decrRefCount(dstset);
4560 } else {
4561 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",
4562 dictSize((dict*)dstset->ptr)));
4563 server.dirty++;
4564 }
4565 zfree(dv);
4566 }
4567
4568 static void sunionCommand(redisClient *c) {
4569 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_UNION);
4570 }
4571
4572 static void sunionstoreCommand(redisClient *c) {
4573 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_UNION);
4574 }
4575
4576 static void sdiffCommand(redisClient *c) {
4577 sunionDiffGenericCommand(c,c->argv+1,c->argc-1,NULL,REDIS_OP_DIFF);
4578 }
4579
4580 static void sdiffstoreCommand(redisClient *c) {
4581 sunionDiffGenericCommand(c,c->argv+2,c->argc-2,c->argv[1],REDIS_OP_DIFF);
4582 }
4583
4584 /* ==================================== ZSets =============================== */
4585
4586 /* ZSETs are ordered sets using two data structures to hold the same elements
4587 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
4588 * data structure.
4589 *
4590 * The elements are added to an hash table mapping Redis objects to scores.
4591 * At the same time the elements are added to a skip list mapping scores
4592 * to Redis objects (so objects are sorted by scores in this "view"). */
4593
4594 /* This skiplist implementation is almost a C translation of the original
4595 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
4596 * Alternative to Balanced Trees", modified in three ways:
4597 * a) this implementation allows for repeated values.
4598 * b) the comparison is not just by key (our 'score') but by satellite data.
4599 * c) there is a back pointer, so it's a doubly linked list with the back
4600 * pointers being only at "level 1". This allows to traverse the list
4601 * from tail to head, useful for ZREVRANGE. */
4602
4603 static zskiplistNode *zslCreateNode(int level, double score, robj *obj) {
4604 zskiplistNode *zn = zmalloc(sizeof(*zn));
4605
4606 zn->forward = zmalloc(sizeof(zskiplistNode*) * level);
4607 zn->score = score;
4608 zn->obj = obj;
4609 return zn;
4610 }
4611
4612 static zskiplist *zslCreate(void) {
4613 int j;
4614 zskiplist *zsl;
4615
4616 zsl = zmalloc(sizeof(*zsl));
4617 zsl->level = 1;
4618 zsl->length = 0;
4619 zsl->header = zslCreateNode(ZSKIPLIST_MAXLEVEL,0,NULL);
4620 for (j = 0; j < ZSKIPLIST_MAXLEVEL; j++)
4621 zsl->header->forward[j] = NULL;
4622 zsl->header->backward = NULL;
4623 zsl->tail = NULL;
4624 return zsl;
4625 }
4626
4627 static void zslFreeNode(zskiplistNode *node) {
4628 decrRefCount(node->obj);
4629 zfree(node->forward);
4630 zfree(node);
4631 }
4632
4633 static void zslFree(zskiplist *zsl) {
4634 zskiplistNode *node = zsl->header->forward[0], *next;
4635
4636 zfree(zsl->header->forward);
4637 zfree(zsl->header);
4638 while(node) {
4639 next = node->forward[0];
4640 zslFreeNode(node);
4641 node = next;
4642 }
4643 zfree(zsl);
4644 }
4645
4646 static int zslRandomLevel(void) {
4647 int level = 1;
4648 while ((random()&0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
4649 level += 1;
4650 return level;
4651 }
4652
4653 static void zslInsert(zskiplist *zsl, double score, robj *obj) {
4654 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
4655 int i, level;
4656
4657 x = zsl->header;
4658 for (i = zsl->level-1; i >= 0; i--) {
4659 while (x->forward[i] &&
4660 (x->forward[i]->score < score ||
4661 (x->forward[i]->score == score &&
4662 compareStringObjects(x->forward[i]->obj,obj) < 0)))
4663 x = x->forward[i];
4664 update[i] = x;
4665 }
4666 /* we assume the key is not already inside, since we allow duplicated
4667 * scores, and the re-insertion of score and redis object should never
4668 * happpen since the caller of zslInsert() should test in the hash table
4669 * if the element is already inside or not. */
4670 level = zslRandomLevel();
4671 if (level > zsl->level) {
4672 for (i = zsl->level; i < level; i++)
4673 update[i] = zsl->header;
4674 zsl->level = level;
4675 }
4676 x = zslCreateNode(level,score,obj);
4677 for (i = 0; i < level; i++) {
4678 x->forward[i] = update[i]->forward[i];
4679 update[i]->forward[i] = x;
4680 }
4681 x->backward = (update[0] == zsl->header) ? NULL : update[0];
4682 if (x->forward[0])
4683 x->forward[0]->backward = x;
4684 else
4685 zsl->tail = x;
4686 zsl->length++;
4687 }
4688
4689 /* Delete an element with matching score/object from the skiplist. */
4690 static int zslDelete(zskiplist *zsl, double score, robj *obj) {
4691 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
4692 int i;
4693
4694 x = zsl->header;
4695 for (i = zsl->level-1; i >= 0; i--) {
4696 while (x->forward[i] &&
4697 (x->forward[i]->score < score ||
4698 (x->forward[i]->score == score &&
4699 compareStringObjects(x->forward[i]->obj,obj) < 0)))
4700 x = x->forward[i];
4701 update[i] = x;
4702 }
4703 /* We may have multiple elements with the same score, what we need
4704 * is to find the element with both the right score and object. */
4705 x = x->forward[0];
4706 if (x && score == x->score && compareStringObjects(x->obj,obj) == 0) {
4707 for (i = 0; i < zsl->level; i++) {
4708 if (update[i]->forward[i] != x) break;
4709 update[i]->forward[i] = x->forward[i];
4710 }
4711 if (x->forward[0]) {
4712 x->forward[0]->backward = (x->backward == zsl->header) ?
4713 NULL : x->backward;
4714 } else {
4715 zsl->tail = x->backward;
4716 }
4717 zslFreeNode(x);
4718 while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
4719 zsl->level--;
4720 zsl->length--;
4721 return 1;
4722 } else {
4723 return 0; /* not found */
4724 }
4725 return 0; /* not found */
4726 }
4727
4728 /* Delete all the elements with score between min and max from the skiplist.
4729 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
4730 * Note that this function takes the reference to the hash table view of the
4731 * sorted set, in order to remove the elements from the hash table too. */
4732 static unsigned long zslDeleteRange(zskiplist *zsl, double min, double max, dict *dict) {
4733 zskiplistNode *update[ZSKIPLIST_MAXLEVEL], *x;
4734 unsigned long removed = 0;
4735 int i;
4736
4737 x = zsl->header;
4738 for (i = zsl->level-1; i >= 0; i--) {
4739 while (x->forward[i] && x->forward[i]->score < min)
4740 x = x->forward[i];
4741 update[i] = x;
4742 }
4743 /* We may have multiple elements with the same score, what we need
4744 * is to find the element with both the right score and object. */
4745 x = x->forward[0];
4746 while (x && x->score <= max) {
4747 zskiplistNode *next;
4748
4749 for (i = 0; i < zsl->level; i++) {
4750 if (update[i]->forward[i] != x) break;
4751 update[i]->forward[i] = x->forward[i];
4752 }
4753 if (x->forward[0]) {
4754 x->forward[0]->backward = (x->backward == zsl->header) ?
4755 NULL : x->backward;
4756 } else {
4757 zsl->tail = x->backward;
4758 }
4759 next = x->forward[0];
4760 dictDelete(dict,x->obj);
4761 zslFreeNode(x);
4762 while(zsl->level > 1 && zsl->header->forward[zsl->level-1] == NULL)
4763 zsl->level--;
4764 zsl->length--;
4765 removed++;
4766 x = next;
4767 }
4768 return removed; /* not found */
4769 }
4770
4771 /* Find the first node having a score equal or greater than the specified one.
4772 * Returns NULL if there is no match. */
4773 static zskiplistNode *zslFirstWithScore(zskiplist *zsl, double score) {
4774 zskiplistNode *x;
4775 int i;
4776
4777 x = zsl->header;
4778 for (i = zsl->level-1; i >= 0; i--) {
4779 while (x->forward[i] && x->forward[i]->score < score)
4780 x = x->forward[i];
4781 }
4782 /* We may have multiple elements with the same score, what we need
4783 * is to find the element with both the right score and object. */
4784 return x->forward[0];
4785 }
4786
4787 /* The actual Z-commands implementations */
4788
4789 /* This generic command implements both ZADD and ZINCRBY.
4790 * scoreval is the score if the operation is a ZADD (doincrement == 0) or
4791 * the increment if the operation is a ZINCRBY (doincrement == 1). */
4792 static void zaddGenericCommand(redisClient *c, robj *key, robj *ele, double scoreval, int doincrement) {
4793 robj *zsetobj;
4794 zset *zs;
4795 double *score;
4796
4797 zsetobj = lookupKeyWrite(c->db,key);
4798 if (zsetobj == NULL) {
4799 zsetobj = createZsetObject();
4800 dictAdd(c->db->dict,key,zsetobj);
4801 incrRefCount(key);
4802 } else {
4803 if (zsetobj->type != REDIS_ZSET) {
4804 addReply(c,shared.wrongtypeerr);
4805 return;
4806 }
4807 }
4808 zs = zsetobj->ptr;
4809
4810 /* Ok now since we implement both ZADD and ZINCRBY here the code
4811 * needs to handle the two different conditions. It's all about setting
4812 * '*score', that is, the new score to set, to the right value. */
4813 score = zmalloc(sizeof(double));
4814 if (doincrement) {
4815 dictEntry *de;
4816
4817 /* Read the old score. If the element was not present starts from 0 */
4818 de = dictFind(zs->dict,ele);
4819 if (de) {
4820 double *oldscore = dictGetEntryVal(de);
4821 *score = *oldscore + scoreval;
4822 } else {
4823 *score = scoreval;
4824 }
4825 } else {
4826 *score = scoreval;
4827 }
4828
4829 /* What follows is a simple remove and re-insert operation that is common
4830 * to both ZADD and ZINCRBY... */
4831 if (dictAdd(zs->dict,ele,score) == DICT_OK) {
4832 /* case 1: New element */
4833 incrRefCount(ele); /* added to hash */
4834 zslInsert(zs->zsl,*score,ele);
4835 incrRefCount(ele); /* added to skiplist */
4836 server.dirty++;
4837 if (doincrement)
4838 addReplyDouble(c,*score);
4839 else
4840 addReply(c,shared.cone);
4841 } else {
4842 dictEntry *de;
4843 double *oldscore;
4844
4845 /* case 2: Score update operation */
4846 de = dictFind(zs->dict,ele);
4847 redisAssert(de != NULL);
4848 oldscore = dictGetEntryVal(de);
4849 if (*score != *oldscore) {
4850 int deleted;
4851
4852 /* Remove and insert the element in the skip list with new score */
4853 deleted = zslDelete(zs->zsl,*oldscore,ele);
4854 redisAssert(deleted != 0);
4855 zslInsert(zs->zsl,*score,ele);
4856 incrRefCount(ele);
4857 /* Update the score in the hash table */
4858 dictReplace(zs->dict,ele,score);
4859 server.dirty++;
4860 } else {
4861 zfree(score);
4862 }
4863 if (doincrement)
4864 addReplyDouble(c,*score);
4865 else
4866 addReply(c,shared.czero);
4867 }
4868 }
4869
4870 static void zaddCommand(redisClient *c) {
4871 double scoreval;
4872
4873 scoreval = strtod(c->argv[2]->ptr,NULL);
4874 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,0);
4875 }
4876
4877 static void zincrbyCommand(redisClient *c) {
4878 double scoreval;
4879
4880 scoreval = strtod(c->argv[2]->ptr,NULL);
4881 zaddGenericCommand(c,c->argv[1],c->argv[3],scoreval,1);
4882 }
4883
4884 static void zremCommand(redisClient *c) {
4885 robj *zsetobj;
4886 zset *zs;
4887
4888 zsetobj = lookupKeyWrite(c->db,c->argv[1]);
4889 if (zsetobj == NULL) {
4890 addReply(c,shared.czero);
4891 } else {
4892 dictEntry *de;
4893 double *oldscore;
4894 int deleted;
4895
4896 if (zsetobj->type != REDIS_ZSET) {
4897 addReply(c,shared.wrongtypeerr);
4898 return;
4899 }
4900 zs = zsetobj->ptr;
4901 de = dictFind(zs->dict,c->argv[2]);
4902 if (de == NULL) {
4903 addReply(c,shared.czero);
4904 return;
4905 }
4906 /* Delete from the skiplist */
4907 oldscore = dictGetEntryVal(de);
4908 deleted = zslDelete(zs->zsl,*oldscore,c->argv[2]);
4909 redisAssert(deleted != 0);
4910
4911 /* Delete from the hash table */
4912 dictDelete(zs->dict,c->argv[2]);
4913 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
4914 server.dirty++;
4915 addReply(c,shared.cone);
4916 }
4917 }
4918
4919 static void zremrangebyscoreCommand(redisClient *c) {
4920 double min = strtod(c->argv[2]->ptr,NULL);
4921 double max = strtod(c->argv[3]->ptr,NULL);
4922 robj *zsetobj;
4923 zset *zs;
4924
4925 zsetobj = lookupKeyWrite(c->db,c->argv[1]);
4926 if (zsetobj == NULL) {
4927 addReply(c,shared.czero);
4928 } else {
4929 long deleted;
4930
4931 if (zsetobj->type != REDIS_ZSET) {
4932 addReply(c,shared.wrongtypeerr);
4933 return;
4934 }
4935 zs = zsetobj->ptr;
4936 deleted = zslDeleteRange(zs->zsl,min,max,zs->dict);
4937 if (htNeedsResize(zs->dict)) dictResize(zs->dict);
4938 server.dirty += deleted;
4939 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",deleted));
4940 }
4941 }
4942
4943 static void zrangeGenericCommand(redisClient *c, int reverse) {
4944 robj *o;
4945 int start = atoi(c->argv[2]->ptr);
4946 int end = atoi(c->argv[3]->ptr);
4947 int withscores = 0;
4948
4949 if (c->argc == 5 && !strcasecmp(c->argv[4]->ptr,"withscores")) {
4950 withscores = 1;
4951 } else if (c->argc >= 5) {
4952 addReply(c,shared.syntaxerr);
4953 return;
4954 }
4955
4956 o = lookupKeyRead(c->db,c->argv[1]);
4957 if (o == NULL) {
4958 addReply(c,shared.nullmultibulk);
4959 } else {
4960 if (o->type != REDIS_ZSET) {
4961 addReply(c,shared.wrongtypeerr);
4962 } else {
4963 zset *zsetobj = o->ptr;
4964 zskiplist *zsl = zsetobj->zsl;
4965 zskiplistNode *ln;
4966
4967 int llen = zsl->length;
4968 int rangelen, j;
4969 robj *ele;
4970
4971 /* convert negative indexes */
4972 if (start < 0) start = llen+start;
4973 if (end < 0) end = llen+end;
4974 if (start < 0) start = 0;
4975 if (end < 0) end = 0;
4976
4977 /* indexes sanity checks */
4978 if (start > end || start >= llen) {
4979 /* Out of range start or start > end result in empty list */
4980 addReply(c,shared.emptymultibulk);
4981 return;
4982 }
4983 if (end >= llen) end = llen-1;
4984 rangelen = (end-start)+1;
4985
4986 /* Return the result in form of a multi-bulk reply */
4987 if (reverse) {
4988 ln = zsl->tail;
4989 while (start--)
4990 ln = ln->backward;
4991 } else {
4992 ln = zsl->header->forward[0];
4993 while (start--)
4994 ln = ln->forward[0];
4995 }
4996
4997 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",
4998 withscores ? (rangelen*2) : rangelen));
4999 for (j = 0; j < rangelen; j++) {
5000 ele = ln->obj;
5001 addReplyBulkLen(c,ele);
5002 addReply(c,ele);
5003 addReply(c,shared.crlf);
5004 if (withscores)
5005 addReplyDouble(c,ln->score);
5006 ln = reverse ? ln->backward : ln->forward[0];
5007 }
5008 }
5009 }
5010 }
5011
5012 static void zrangeCommand(redisClient *c) {
5013 zrangeGenericCommand(c,0);
5014 }
5015
5016 static void zrevrangeCommand(redisClient *c) {
5017 zrangeGenericCommand(c,1);
5018 }
5019
5020 static void zrangebyscoreCommand(redisClient *c) {
5021 robj *o;
5022 double min = strtod(c->argv[2]->ptr,NULL);
5023 double max = strtod(c->argv[3]->ptr,NULL);
5024 int offset = 0, limit = -1;
5025
5026 if (c->argc != 4 && c->argc != 7) {
5027 addReplySds(c,
5028 sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
5029 return;
5030 } else if (c->argc == 7 && strcasecmp(c->argv[4]->ptr,"limit")) {
5031 addReply(c,shared.syntaxerr);
5032 return;
5033 } else if (c->argc == 7) {
5034 offset = atoi(c->argv[5]->ptr);
5035 limit = atoi(c->argv[6]->ptr);
5036 if (offset < 0) offset = 0;
5037 }
5038
5039 o = lookupKeyRead(c->db,c->argv[1]);
5040 if (o == NULL) {
5041 addReply(c,shared.nullmultibulk);
5042 } else {
5043 if (o->type != REDIS_ZSET) {
5044 addReply(c,shared.wrongtypeerr);
5045 } else {
5046 zset *zsetobj = o->ptr;
5047 zskiplist *zsl = zsetobj->zsl;
5048 zskiplistNode *ln;
5049 robj *ele, *lenobj;
5050 unsigned int rangelen = 0;
5051
5052 /* Get the first node with the score >= min */
5053 ln = zslFirstWithScore(zsl,min);
5054 if (ln == NULL) {
5055 /* No element matching the speciifed interval */
5056 addReply(c,shared.emptymultibulk);
5057 return;
5058 }
5059
5060 /* We don't know in advance how many matching elements there
5061 * are in the list, so we push this object that will represent
5062 * the multi-bulk length in the output buffer, and will "fix"
5063 * it later */
5064 lenobj = createObject(REDIS_STRING,NULL);
5065 addReply(c,lenobj);
5066 decrRefCount(lenobj);
5067
5068 while(ln && ln->score <= max) {
5069 if (offset) {
5070 offset--;
5071 ln = ln->forward[0];
5072 continue;
5073 }
5074 if (limit == 0) break;
5075 ele = ln->obj;
5076 addReplyBulkLen(c,ele);
5077 addReply(c,ele);
5078 addReply(c,shared.crlf);
5079 ln = ln->forward[0];
5080 rangelen++;
5081 if (limit > 0) limit--;
5082 }
5083 lenobj->ptr = sdscatprintf(sdsempty(),"*%d\r\n",rangelen);
5084 }
5085 }
5086 }
5087
5088 static void zcardCommand(redisClient *c) {
5089 robj *o;
5090 zset *zs;
5091
5092 o = lookupKeyRead(c->db,c->argv[1]);
5093 if (o == NULL) {
5094 addReply(c,shared.czero);
5095 return;
5096 } else {
5097 if (o->type != REDIS_ZSET) {
5098 addReply(c,shared.wrongtypeerr);
5099 } else {
5100 zs = o->ptr;
5101 addReplySds(c,sdscatprintf(sdsempty(),":%lu\r\n",zs->zsl->length));
5102 }
5103 }
5104 }
5105
5106 static void zscoreCommand(redisClient *c) {
5107 robj *o;
5108 zset *zs;
5109
5110 o = lookupKeyRead(c->db,c->argv[1]);
5111 if (o == NULL) {
5112 addReply(c,shared.nullbulk);
5113 return;
5114 } else {
5115 if (o->type != REDIS_ZSET) {
5116 addReply(c,shared.wrongtypeerr);
5117 } else {
5118 dictEntry *de;
5119
5120 zs = o->ptr;
5121 de = dictFind(zs->dict,c->argv[2]);
5122 if (!de) {
5123 addReply(c,shared.nullbulk);
5124 } else {
5125 double *score = dictGetEntryVal(de);
5126
5127 addReplyDouble(c,*score);
5128 }
5129 }
5130 }
5131 }
5132
5133 /* ========================= Non type-specific commands ==================== */
5134
5135 static void flushdbCommand(redisClient *c) {
5136 server.dirty += dictSize(c->db->dict);
5137 dictEmpty(c->db->dict);
5138 dictEmpty(c->db->expires);
5139 addReply(c,shared.ok);
5140 }
5141
5142 static void flushallCommand(redisClient *c) {
5143 server.dirty += emptyDb();
5144 addReply(c,shared.ok);
5145 rdbSave(server.dbfilename);
5146 server.dirty++;
5147 }
5148
5149 static redisSortOperation *createSortOperation(int type, robj *pattern) {
5150 redisSortOperation *so = zmalloc(sizeof(*so));
5151 so->type = type;
5152 so->pattern = pattern;
5153 return so;
5154 }
5155
5156 /* Return the value associated to the key with a name obtained
5157 * substituting the first occurence of '*' in 'pattern' with 'subst' */
5158 static robj *lookupKeyByPattern(redisDb *db, robj *pattern, robj *subst) {
5159 char *p;
5160 sds spat, ssub;
5161 robj keyobj;
5162 int prefixlen, sublen, postfixlen;
5163 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
5164 struct {
5165 long len;
5166 long free;
5167 char buf[REDIS_SORTKEY_MAX+1];
5168 } keyname;
5169
5170 /* If the pattern is "#" return the substitution object itself in order
5171 * to implement the "SORT ... GET #" feature. */
5172 spat = pattern->ptr;
5173 if (spat[0] == '#' && spat[1] == '\0') {
5174 return subst;
5175 }
5176
5177 /* The substitution object may be specially encoded. If so we create
5178 * a decoded object on the fly. Otherwise getDecodedObject will just
5179 * increment the ref count, that we'll decrement later. */
5180 subst = getDecodedObject(subst);
5181
5182 ssub = subst->ptr;
5183 if (sdslen(spat)+sdslen(ssub)-1 > REDIS_SORTKEY_MAX) return NULL;
5184 p = strchr(spat,'*');
5185 if (!p) {
5186 decrRefCount(subst);
5187 return NULL;
5188 }
5189
5190 prefixlen = p-spat;
5191 sublen = sdslen(ssub);
5192 postfixlen = sdslen(spat)-(prefixlen+1);
5193 memcpy(keyname.buf,spat,prefixlen);
5194 memcpy(keyname.buf+prefixlen,ssub,sublen);
5195 memcpy(keyname.buf+prefixlen+sublen,p+1,postfixlen);
5196 keyname.buf[prefixlen+sublen+postfixlen] = '\0';
5197 keyname.len = prefixlen+sublen+postfixlen;
5198
5199 initStaticStringObject(keyobj,((char*)&keyname)+(sizeof(long)*2))
5200 decrRefCount(subst);
5201
5202 /* printf("lookup '%s' => %p\n", keyname.buf,de); */
5203 return lookupKeyRead(db,&keyobj);
5204 }
5205
5206 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
5207 * the additional parameter is not standard but a BSD-specific we have to
5208 * pass sorting parameters via the global 'server' structure */
5209 static int sortCompare(const void *s1, const void *s2) {
5210 const redisSortObject *so1 = s1, *so2 = s2;
5211 int cmp;
5212
5213 if (!server.sort_alpha) {
5214 /* Numeric sorting. Here it's trivial as we precomputed scores */
5215 if (so1->u.score > so2->u.score) {
5216 cmp = 1;
5217 } else if (so1->u.score < so2->u.score) {
5218 cmp = -1;
5219 } else {
5220 cmp = 0;
5221 }
5222 } else {
5223 /* Alphanumeric sorting */
5224 if (server.sort_bypattern) {
5225 if (!so1->u.cmpobj || !so2->u.cmpobj) {
5226 /* At least one compare object is NULL */
5227 if (so1->u.cmpobj == so2->u.cmpobj)
5228 cmp = 0;
5229 else if (so1->u.cmpobj == NULL)
5230 cmp = -1;
5231 else
5232 cmp = 1;
5233 } else {
5234 /* We have both the objects, use strcoll */
5235 cmp = strcoll(so1->u.cmpobj->ptr,so2->u.cmpobj->ptr);
5236 }
5237 } else {
5238 /* Compare elements directly */
5239 robj *dec1, *dec2;
5240
5241 dec1 = getDecodedObject(so1->obj);
5242 dec2 = getDecodedObject(so2->obj);
5243 cmp = strcoll(dec1->ptr,dec2->ptr);
5244 decrRefCount(dec1);
5245 decrRefCount(dec2);
5246 }
5247 }
5248 return server.sort_desc ? -cmp : cmp;
5249 }
5250
5251 /* The SORT command is the most complex command in Redis. Warning: this code
5252 * is optimized for speed and a bit less for readability */
5253 static void sortCommand(redisClient *c) {
5254 list *operations;
5255 int outputlen = 0;
5256 int desc = 0, alpha = 0;
5257 int limit_start = 0, limit_count = -1, start, end;
5258 int j, dontsort = 0, vectorlen;
5259 int getop = 0; /* GET operation counter */
5260 robj *sortval, *sortby = NULL, *storekey = NULL;
5261 redisSortObject *vector; /* Resulting vector to sort */
5262
5263 /* Lookup the key to sort. It must be of the right types */
5264 sortval = lookupKeyRead(c->db,c->argv[1]);
5265 if (sortval == NULL) {
5266 addReply(c,shared.nullmultibulk);
5267 return;
5268 }
5269 if (sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
5270 sortval->type != REDIS_ZSET)
5271 {
5272 addReply(c,shared.wrongtypeerr);
5273 return;
5274 }
5275
5276 /* Create a list of operations to perform for every sorted element.
5277 * Operations can be GET/DEL/INCR/DECR */
5278 operations = listCreate();
5279 listSetFreeMethod(operations,zfree);
5280 j = 2;
5281
5282 /* Now we need to protect sortval incrementing its count, in the future
5283 * SORT may have options able to overwrite/delete keys during the sorting
5284 * and the sorted key itself may get destroied */
5285 incrRefCount(sortval);
5286
5287 /* The SORT command has an SQL-alike syntax, parse it */
5288 while(j < c->argc) {
5289 int leftargs = c->argc-j-1;
5290 if (!strcasecmp(c->argv[j]->ptr,"asc")) {
5291 desc = 0;
5292 } else if (!strcasecmp(c->argv[j]->ptr,"desc")) {
5293 desc = 1;
5294 } else if (!strcasecmp(c->argv[j]->ptr,"alpha")) {
5295 alpha = 1;
5296 } else if (!strcasecmp(c->argv[j]->ptr,"limit") && leftargs >= 2) {
5297 limit_start = atoi(c->argv[j+1]->ptr);
5298 limit_count = atoi(c->argv[j+2]->ptr);
5299 j+=2;
5300 } else if (!strcasecmp(c->argv[j]->ptr,"store") && leftargs >= 1) {
5301 storekey = c->argv[j+1];
5302 j++;
5303 } else if (!strcasecmp(c->argv[j]->ptr,"by") && leftargs >= 1) {
5304 sortby = c->argv[j+1];
5305 /* If the BY pattern does not contain '*', i.e. it is constant,
5306 * we don't need to sort nor to lookup the weight keys. */
5307 if (strchr(c->argv[j+1]->ptr,'*') == NULL) dontsort = 1;
5308 j++;
5309 } else if (!strcasecmp(c->argv[j]->ptr,"get") && leftargs >= 1) {
5310 listAddNodeTail(operations,createSortOperation(
5311 REDIS_SORT_GET,c->argv[j+1]));
5312 getop++;
5313 j++;
5314 } else {
5315 decrRefCount(sortval);
5316 listRelease(operations);
5317 addReply(c,shared.syntaxerr);
5318 return;
5319 }
5320 j++;
5321 }
5322
5323 /* Load the sorting vector with all the objects to sort */
5324 switch(sortval->type) {
5325 case REDIS_LIST: vectorlen = listLength((list*)sortval->ptr); break;
5326 case REDIS_SET: vectorlen = dictSize((dict*)sortval->ptr); break;
5327 case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
5328 default: vectorlen = 0; redisAssert(0); /* Avoid GCC warning */
5329 }
5330 vector = zmalloc(sizeof(redisSortObject)*vectorlen);
5331 j = 0;
5332
5333 if (sortval->type == REDIS_LIST) {
5334 list *list = sortval->ptr;
5335 listNode *ln;
5336
5337 listRewind(list);
5338 while((ln = listYield(list))) {
5339 robj *ele = ln->value;
5340 vector[j].obj = ele;
5341 vector[j].u.score = 0;
5342 vector[j].u.cmpobj = NULL;
5343 j++;
5344 }
5345 } else {
5346 dict *set;
5347 dictIterator *di;
5348 dictEntry *setele;
5349
5350 if (sortval->type == REDIS_SET) {
5351 set = sortval->ptr;
5352 } else {
5353 zset *zs = sortval->ptr;
5354 set = zs->dict;
5355 }
5356
5357 di = dictGetIterator(set);
5358 while((setele = dictNext(di)) != NULL) {
5359 vector[j].obj = dictGetEntryKey(setele);
5360 vector[j].u.score = 0;
5361 vector[j].u.cmpobj = NULL;
5362 j++;
5363 }
5364 dictReleaseIterator(di);
5365 }
5366 redisAssert(j == vectorlen);
5367
5368 /* Now it's time to load the right scores in the sorting vector */
5369 if (dontsort == 0) {
5370 for (j = 0; j < vectorlen; j++) {
5371 if (sortby) {
5372 robj *byval;
5373
5374 byval = lookupKeyByPattern(c->db,sortby,vector[j].obj);
5375 if (!byval || byval->type != REDIS_STRING) continue;
5376 if (alpha) {
5377 vector[j].u.cmpobj = getDecodedObject(byval);
5378 } else {
5379 if (byval->encoding == REDIS_ENCODING_RAW) {
5380 vector[j].u.score = strtod(byval->ptr,NULL);
5381 } else {
5382 /* Don't need to decode the object if it's
5383 * integer-encoded (the only encoding supported) so
5384 * far. We can just cast it */
5385 if (byval->encoding == REDIS_ENCODING_INT) {
5386 vector[j].u.score = (long)byval->ptr;
5387 } else
5388 redisAssert(1 != 1);
5389 }
5390 }
5391 } else {
5392 if (!alpha) {
5393 if (vector[j].obj->encoding == REDIS_ENCODING_RAW)
5394 vector[j].u.score = strtod(vector[j].obj->ptr,NULL);
5395 else {
5396 if (vector[j].obj->encoding == REDIS_ENCODING_INT)
5397 vector[j].u.score = (long) vector[j].obj->ptr;
5398 else
5399 redisAssert(1 != 1);
5400 }
5401 }
5402 }
5403 }
5404 }
5405
5406 /* We are ready to sort the vector... perform a bit of sanity check
5407 * on the LIMIT option too. We'll use a partial version of quicksort. */
5408 start = (limit_start < 0) ? 0 : limit_start;
5409 end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
5410 if (start >= vectorlen) {
5411 start = vectorlen-1;
5412 end = vectorlen-2;
5413 }
5414 if (end >= vectorlen) end = vectorlen-1;
5415
5416 if (dontsort == 0) {
5417 server.sort_desc = desc;
5418 server.sort_alpha = alpha;
5419 server.sort_bypattern = sortby ? 1 : 0;
5420 if (sortby && (start != 0 || end != vectorlen-1))
5421 pqsort(vector,vectorlen,sizeof(redisSortObject),sortCompare, start,end);
5422 else
5423 qsort(vector,vectorlen,sizeof(redisSortObject),sortCompare);
5424 }
5425
5426 /* Send command output to the output buffer, performing the specified
5427 * GET/DEL/INCR/DECR operations if any. */
5428 outputlen = getop ? getop*(end-start+1) : end-start+1;
5429 if (storekey == NULL) {
5430 /* STORE option not specified, sent the sorting result to client */
5431 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",outputlen));
5432 for (j = start; j <= end; j++) {
5433 listNode *ln;
5434 if (!getop) {
5435 addReplyBulkLen(c,vector[j].obj);
5436 addReply(c,vector[j].obj);
5437 addReply(c,shared.crlf);
5438 }
5439 listRewind(operations);
5440 while((ln = listYield(operations))) {
5441 redisSortOperation *sop = ln->value;
5442 robj *val = lookupKeyByPattern(c->db,sop->pattern,
5443 vector[j].obj);
5444
5445 if (sop->type == REDIS_SORT_GET) {
5446 if (!val || val->type != REDIS_STRING) {
5447 addReply(c,shared.nullbulk);
5448 } else {
5449 addReplyBulkLen(c,val);
5450 addReply(c,val);
5451 addReply(c,shared.crlf);
5452 }
5453 } else {
5454 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
5455 }
5456 }
5457 }
5458 } else {
5459 robj *listObject = createListObject();
5460 list *listPtr = (list*) listObject->ptr;
5461
5462 /* STORE option specified, set the sorting result as a List object */
5463 for (j = start; j <= end; j++) {
5464 listNode *ln;
5465 if (!getop) {
5466 listAddNodeTail(listPtr,vector[j].obj);
5467 incrRefCount(vector[j].obj);
5468 }
5469 listRewind(operations);
5470 while((ln = listYield(operations))) {
5471 redisSortOperation *sop = ln->value;
5472 robj *val = lookupKeyByPattern(c->db,sop->pattern,
5473 vector[j].obj);
5474
5475 if (sop->type == REDIS_SORT_GET) {
5476 if (!val || val->type != REDIS_STRING) {
5477 listAddNodeTail(listPtr,createStringObject("",0));
5478 } else {
5479 listAddNodeTail(listPtr,val);
5480 incrRefCount(val);
5481 }
5482 } else {
5483 redisAssert(sop->type == REDIS_SORT_GET); /* always fails */
5484 }
5485 }
5486 }
5487 if (dictReplace(c->db->dict,storekey,listObject)) {
5488 incrRefCount(storekey);
5489 }
5490 /* Note: we add 1 because the DB is dirty anyway since even if the
5491 * SORT result is empty a new key is set and maybe the old content
5492 * replaced. */
5493 server.dirty += 1+outputlen;
5494 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",outputlen));
5495 }
5496
5497 /* Cleanup */
5498 decrRefCount(sortval);
5499 listRelease(operations);
5500 for (j = 0; j < vectorlen; j++) {
5501 if (sortby && alpha && vector[j].u.cmpobj)
5502 decrRefCount(vector[j].u.cmpobj);
5503 }
5504 zfree(vector);
5505 }
5506
5507 /* Convert an amount of bytes into a human readable string in the form
5508 * of 100B, 2G, 100M, 4K, and so forth. */
5509 static void bytesToHuman(char *s, unsigned long long n) {
5510 double d;
5511
5512 if (n < 1024) {
5513 /* Bytes */
5514 sprintf(s,"%lluB",n);
5515 return;
5516 } else if (n < (1024*1024)) {
5517 d = (double)n/(1024);
5518 sprintf(s,"%.2fK",d);
5519 } else if (n < (1024LL*1024*1024)) {
5520 d = (double)n/(1024*1024);
5521 sprintf(s,"%.2fM",d);
5522 } else if (n < (1024LL*1024*1024*1024)) {
5523 d = (double)n/(1024LL*1024*1024);
5524 sprintf(s,"%.2fM",d);
5525 }
5526 }
5527
5528 /* Create the string returned by the INFO command. This is decoupled
5529 * by the INFO command itself as we need to report the same information
5530 * on memory corruption problems. */
5531 static sds genRedisInfoString(void) {
5532 sds info;
5533 time_t uptime = time(NULL)-server.stat_starttime;
5534 int j;
5535 char hmem[64];
5536
5537 bytesToHuman(hmem,server.usedmemory);
5538 info = sdscatprintf(sdsempty(),
5539 "redis_version:%s\r\n"
5540 "arch_bits:%s\r\n"
5541 "multiplexing_api:%s\r\n"
5542 "process_id:%ld\r\n"
5543 "uptime_in_seconds:%ld\r\n"
5544 "uptime_in_days:%ld\r\n"
5545 "connected_clients:%d\r\n"
5546 "connected_slaves:%d\r\n"
5547 "blocked_clients:%d\r\n"
5548 "used_memory:%zu\r\n"
5549 "used_memory_human:%s\r\n"
5550 "changes_since_last_save:%lld\r\n"
5551 "bgsave_in_progress:%d\r\n"
5552 "last_save_time:%ld\r\n"
5553 "bgrewriteaof_in_progress:%d\r\n"
5554 "total_connections_received:%lld\r\n"
5555 "total_commands_processed:%lld\r\n"
5556 "vm_enabled:%d\r\n"
5557 "role:%s\r\n"
5558 ,REDIS_VERSION,
5559 (sizeof(long) == 8) ? "64" : "32",
5560 aeGetApiName(),
5561 (long) getpid(),
5562 uptime,
5563 uptime/(3600*24),
5564 listLength(server.clients)-listLength(server.slaves),
5565 listLength(server.slaves),
5566 server.blockedclients,
5567 server.usedmemory,
5568 hmem,
5569 server.dirty,
5570 server.bgsavechildpid != -1,
5571 server.lastsave,
5572 server.bgrewritechildpid != -1,
5573 server.stat_numconnections,
5574 server.stat_numcommands,
5575 server.vm_enabled != 0,
5576 server.masterhost == NULL ? "master" : "slave"
5577 );
5578 if (server.masterhost) {
5579 info = sdscatprintf(info,
5580 "master_host:%s\r\n"
5581 "master_port:%d\r\n"
5582 "master_link_status:%s\r\n"
5583 "master_last_io_seconds_ago:%d\r\n"
5584 ,server.masterhost,
5585 server.masterport,
5586 (server.replstate == REDIS_REPL_CONNECTED) ?
5587 "up" : "down",
5588 server.master ? ((int)(time(NULL)-server.master->lastinteraction)) : -1
5589 );
5590 }
5591 if (server.vm_enabled) {
5592 info = sdscatprintf(info,
5593 "vm_conf_max_memory:%llu\r\n"
5594 "vm_conf_page_size:%llu\r\n"
5595 "vm_conf_pages:%llu\r\n"
5596 "vm_stats_used_pages:%llu\r\n"
5597 "vm_stats_swapped_objects:%llu\r\n"
5598 "vm_stats_swappin_count:%llu\r\n"
5599 "vm_stats_swappout_count:%llu\r\n"
5600 "vm_stats_io_newjobs_len:%lu\r\n"
5601 "vm_stats_io_processing_len:%lu\r\n"
5602 "vm_stats_io_processed_len:%lu\r\n"
5603 "vm_stats_io_waiting_clients:%lu\r\n"
5604 ,(unsigned long long) server.vm_max_memory,
5605 (unsigned long long) server.vm_page_size,
5606 (unsigned long long) server.vm_pages,
5607 (unsigned long long) server.vm_stats_used_pages,
5608 (unsigned long long) server.vm_stats_swapped_objects,
5609 (unsigned long long) server.vm_stats_swapins,
5610 (unsigned long long) server.vm_stats_swapouts,
5611 (unsigned long) listLength(server.io_newjobs),
5612 (unsigned long) listLength(server.io_processing),
5613 (unsigned long) listLength(server.io_processed),
5614 (unsigned long) listLength(server.io_clients)
5615 );
5616 }
5617 for (j = 0; j < server.dbnum; j++) {
5618 long long keys, vkeys;
5619
5620 keys = dictSize(server.db[j].dict);
5621 vkeys = dictSize(server.db[j].expires);
5622 if (keys || vkeys) {
5623 info = sdscatprintf(info, "db%d:keys=%lld,expires=%lld\r\n",
5624 j, keys, vkeys);
5625 }
5626 }
5627 return info;
5628 }
5629
5630 static void infoCommand(redisClient *c) {
5631 sds info = genRedisInfoString();
5632 addReplySds(c,sdscatprintf(sdsempty(),"$%lu\r\n",
5633 (unsigned long)sdslen(info)));
5634 addReplySds(c,info);
5635 addReply(c,shared.crlf);
5636 }
5637
5638 static void monitorCommand(redisClient *c) {
5639 /* ignore MONITOR if aleady slave or in monitor mode */
5640 if (c->flags & REDIS_SLAVE) return;
5641
5642 c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
5643 c->slaveseldb = 0;
5644 listAddNodeTail(server.monitors,c);
5645 addReply(c,shared.ok);
5646 }
5647
5648 /* ================================= Expire ================================= */
5649 static int removeExpire(redisDb *db, robj *key) {
5650 if (dictDelete(db->expires,key) == DICT_OK) {
5651 return 1;
5652 } else {
5653 return 0;
5654 }
5655 }
5656
5657 static int setExpire(redisDb *db, robj *key, time_t when) {
5658 if (dictAdd(db->expires,key,(void*)when) == DICT_ERR) {
5659 return 0;
5660 } else {
5661 incrRefCount(key);
5662 return 1;
5663 }
5664 }
5665
5666 /* Return the expire time of the specified key, or -1 if no expire
5667 * is associated with this key (i.e. the key is non volatile) */
5668 static time_t getExpire(redisDb *db, robj *key) {
5669 dictEntry *de;
5670
5671 /* No expire? return ASAP */
5672 if (dictSize(db->expires) == 0 ||
5673 (de = dictFind(db->expires,key)) == NULL) return -1;
5674
5675 return (time_t) dictGetEntryVal(de);
5676 }
5677
5678 static int expireIfNeeded(redisDb *db, robj *key) {
5679 time_t when;
5680 dictEntry *de;
5681
5682 /* No expire? return ASAP */
5683 if (dictSize(db->expires) == 0 ||
5684 (de = dictFind(db->expires,key)) == NULL) return 0;
5685
5686 /* Lookup the expire */
5687 when = (time_t) dictGetEntryVal(de);
5688 if (time(NULL) <= when) return 0;
5689
5690 /* Delete the key */
5691 dictDelete(db->expires,key);
5692 return dictDelete(db->dict,key) == DICT_OK;
5693 }
5694
5695 static int deleteIfVolatile(redisDb *db, robj *key) {
5696 dictEntry *de;
5697
5698 /* No expire? return ASAP */
5699 if (dictSize(db->expires) == 0 ||
5700 (de = dictFind(db->expires,key)) == NULL) return 0;
5701
5702 /* Delete the key */
5703 server.dirty++;
5704 dictDelete(db->expires,key);
5705 return dictDelete(db->dict,key) == DICT_OK;
5706 }
5707
5708 static void expireGenericCommand(redisClient *c, robj *key, time_t seconds) {
5709 dictEntry *de;
5710
5711 de = dictFind(c->db->dict,key);
5712 if (de == NULL) {
5713 addReply(c,shared.czero);
5714 return;
5715 }
5716 if (seconds < 0) {
5717 if (deleteKey(c->db,key)) server.dirty++;
5718 addReply(c, shared.cone);
5719 return;
5720 } else {
5721 time_t when = time(NULL)+seconds;
5722 if (setExpire(c->db,key,when)) {
5723 addReply(c,shared.cone);
5724 server.dirty++;
5725 } else {
5726 addReply(c,shared.czero);
5727 }
5728 return;
5729 }
5730 }
5731
5732 static void expireCommand(redisClient *c) {
5733 expireGenericCommand(c,c->argv[1],strtol(c->argv[2]->ptr,NULL,10));
5734 }
5735
5736 static void expireatCommand(redisClient *c) {
5737 expireGenericCommand(c,c->argv[1],strtol(c->argv[2]->ptr,NULL,10)-time(NULL));
5738 }
5739
5740 static void ttlCommand(redisClient *c) {
5741 time_t expire;
5742 int ttl = -1;
5743
5744 expire = getExpire(c->db,c->argv[1]);
5745 if (expire != -1) {
5746 ttl = (int) (expire-time(NULL));
5747 if (ttl < 0) ttl = -1;
5748 }
5749 addReplySds(c,sdscatprintf(sdsempty(),":%d\r\n",ttl));
5750 }
5751
5752 /* ================================ MULTI/EXEC ============================== */
5753
5754 /* Client state initialization for MULTI/EXEC */
5755 static void initClientMultiState(redisClient *c) {
5756 c->mstate.commands = NULL;
5757 c->mstate.count = 0;
5758 }
5759
5760 /* Release all the resources associated with MULTI/EXEC state */
5761 static void freeClientMultiState(redisClient *c) {
5762 int j;
5763
5764 for (j = 0; j < c->mstate.count; j++) {
5765 int i;
5766 multiCmd *mc = c->mstate.commands+j;
5767
5768 for (i = 0; i < mc->argc; i++)
5769 decrRefCount(mc->argv[i]);
5770 zfree(mc->argv);
5771 }
5772 zfree(c->mstate.commands);
5773 }
5774
5775 /* Add a new command into the MULTI commands queue */
5776 static void queueMultiCommand(redisClient *c, struct redisCommand *cmd) {
5777 multiCmd *mc;
5778 int j;
5779
5780 c->mstate.commands = zrealloc(c->mstate.commands,
5781 sizeof(multiCmd)*(c->mstate.count+1));
5782 mc = c->mstate.commands+c->mstate.count;
5783 mc->cmd = cmd;
5784 mc->argc = c->argc;
5785 mc->argv = zmalloc(sizeof(robj*)*c->argc);
5786 memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
5787 for (j = 0; j < c->argc; j++)
5788 incrRefCount(mc->argv[j]);
5789 c->mstate.count++;
5790 }
5791
5792 static void multiCommand(redisClient *c) {
5793 c->flags |= REDIS_MULTI;
5794 addReply(c,shared.ok);
5795 }
5796
5797 static void execCommand(redisClient *c) {
5798 int j;
5799 robj **orig_argv;
5800 int orig_argc;
5801
5802 if (!(c->flags & REDIS_MULTI)) {
5803 addReplySds(c,sdsnew("-ERR EXEC without MULTI\r\n"));
5804 return;
5805 }
5806
5807 orig_argv = c->argv;
5808 orig_argc = c->argc;
5809 addReplySds(c,sdscatprintf(sdsempty(),"*%d\r\n",c->mstate.count));
5810 for (j = 0; j < c->mstate.count; j++) {
5811 c->argc = c->mstate.commands[j].argc;
5812 c->argv = c->mstate.commands[j].argv;
5813 call(c,c->mstate.commands[j].cmd);
5814 }
5815 c->argv = orig_argv;
5816 c->argc = orig_argc;
5817 freeClientMultiState(c);
5818 initClientMultiState(c);
5819 c->flags &= (~REDIS_MULTI);
5820 }
5821
5822 /* =========================== Blocking Operations ========================= */
5823
5824 /* Currently Redis blocking operations support is limited to list POP ops,
5825 * so the current implementation is not fully generic, but it is also not
5826 * completely specific so it will not require a rewrite to support new
5827 * kind of blocking operations in the future.
5828 *
5829 * Still it's important to note that list blocking operations can be already
5830 * used as a notification mechanism in order to implement other blocking
5831 * operations at application level, so there must be a very strong evidence
5832 * of usefulness and generality before new blocking operations are implemented.
5833 *
5834 * This is how the current blocking POP works, we use BLPOP as example:
5835 * - If the user calls BLPOP and the key exists and contains a non empty list
5836 * then LPOP is called instead. So BLPOP is semantically the same as LPOP
5837 * if there is not to block.
5838 * - If instead BLPOP is called and the key does not exists or the list is
5839 * empty we need to block. In order to do so we remove the notification for
5840 * new data to read in the client socket (so that we'll not serve new
5841 * requests if the blocking request is not served). Also we put the client
5842 * in a dictionary (db->blockingkeys) mapping keys to a list of clients
5843 * blocking for this keys.
5844 * - If a PUSH operation against a key with blocked clients waiting is
5845 * performed, we serve the first in the list: basically instead to push
5846 * the new element inside the list we return it to the (first / oldest)
5847 * blocking client, unblock the client, and remove it form the list.
5848 *
5849 * The above comment and the source code should be enough in order to understand
5850 * the implementation and modify / fix it later.
5851 */
5852
5853 /* Set a client in blocking mode for the specified key, with the specified
5854 * timeout */
5855 static void blockForKeys(redisClient *c, robj **keys, int numkeys, time_t timeout) {
5856 dictEntry *de;
5857 list *l;
5858 int j;
5859
5860 c->blockingkeys = zmalloc(sizeof(robj*)*numkeys);
5861 c->blockingkeysnum = numkeys;
5862 c->blockingto = timeout;
5863 for (j = 0; j < numkeys; j++) {
5864 /* Add the key in the client structure, to map clients -> keys */
5865 c->blockingkeys[j] = keys[j];
5866 incrRefCount(keys[j]);
5867
5868 /* And in the other "side", to map keys -> clients */
5869 de = dictFind(c->db->blockingkeys,keys[j]);
5870 if (de == NULL) {
5871 int retval;
5872
5873 /* For every key we take a list of clients blocked for it */
5874 l = listCreate();
5875 retval = dictAdd(c->db->blockingkeys,keys[j],l);
5876 incrRefCount(keys[j]);
5877 assert(retval == DICT_OK);
5878 } else {
5879 l = dictGetEntryVal(de);
5880 }
5881 listAddNodeTail(l,c);
5882 }
5883 /* Mark the client as a blocked client */
5884 c->flags |= REDIS_BLOCKED;
5885 aeDeleteFileEvent(server.el,c->fd,AE_READABLE);
5886 server.blockedclients++;
5887 }
5888
5889 /* Unblock a client that's waiting in a blocking operation such as BLPOP */
5890 static void unblockClient(redisClient *c) {
5891 dictEntry *de;
5892 list *l;
5893 int j;
5894
5895 assert(c->blockingkeys != NULL);
5896 /* The client may wait for multiple keys, so unblock it for every key. */
5897 for (j = 0; j < c->blockingkeysnum; j++) {
5898 /* Remove this client from the list of clients waiting for this key. */
5899 de = dictFind(c->db->blockingkeys,c->blockingkeys[j]);
5900 assert(de != NULL);
5901 l = dictGetEntryVal(de);
5902 listDelNode(l,listSearchKey(l,c));
5903 /* If the list is empty we need to remove it to avoid wasting memory */
5904 if (listLength(l) == 0)
5905 dictDelete(c->db->blockingkeys,c->blockingkeys[j]);
5906 decrRefCount(c->blockingkeys[j]);
5907 }
5908 /* Cleanup the client structure */
5909 zfree(c->blockingkeys);
5910 c->blockingkeys = NULL;
5911 c->flags &= (~REDIS_BLOCKED);
5912 server.blockedclients--;
5913 /* Ok now we are ready to get read events from socket, note that we
5914 * can't trap errors here as it's possible that unblockClients() is
5915 * called from freeClient() itself, and the only thing we can do
5916 * if we failed to register the READABLE event is to kill the client.
5917 * Still the following function should never fail in the real world as
5918 * we are sure the file descriptor is sane, and we exit on out of mem. */
5919 aeCreateFileEvent(server.el, c->fd, AE_READABLE, readQueryFromClient, c);
5920 /* As a final step we want to process data if there is some command waiting
5921 * in the input buffer. Note that this is safe even if unblockClient()
5922 * gets called from freeClient() because freeClient() will be smart
5923 * enough to call this function *after* c->querybuf was set to NULL. */
5924 if (c->querybuf && sdslen(c->querybuf) > 0) processInputBuffer(c);
5925 }
5926
5927 /* This should be called from any function PUSHing into lists.
5928 * 'c' is the "pushing client", 'key' is the key it is pushing data against,
5929 * 'ele' is the element pushed.
5930 *
5931 * If the function returns 0 there was no client waiting for a list push
5932 * against this key.
5933 *
5934 * If the function returns 1 there was a client waiting for a list push
5935 * against this key, the element was passed to this client thus it's not
5936 * needed to actually add it to the list and the caller should return asap. */
5937 static int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele) {
5938 struct dictEntry *de;
5939 redisClient *receiver;
5940 list *l;
5941 listNode *ln;
5942
5943 de = dictFind(c->db->blockingkeys,key);
5944 if (de == NULL) return 0;
5945 l = dictGetEntryVal(de);
5946 ln = listFirst(l);
5947 assert(ln != NULL);
5948 receiver = ln->value;
5949
5950 addReplySds(receiver,sdsnew("*2\r\n"));
5951 addReplyBulkLen(receiver,key);
5952 addReply(receiver,key);
5953 addReply(receiver,shared.crlf);
5954 addReplyBulkLen(receiver,ele);
5955 addReply(receiver,ele);
5956 addReply(receiver,shared.crlf);
5957 unblockClient(receiver);
5958 return 1;
5959 }
5960
5961 /* Blocking RPOP/LPOP */
5962 static void blockingPopGenericCommand(redisClient *c, int where) {
5963 robj *o;
5964 time_t timeout;
5965 int j;
5966
5967 for (j = 1; j < c->argc-1; j++) {
5968 o = lookupKeyWrite(c->db,c->argv[j]);
5969 if (o != NULL) {
5970 if (o->type != REDIS_LIST) {
5971 addReply(c,shared.wrongtypeerr);
5972 return;
5973 } else {
5974 list *list = o->ptr;
5975 if (listLength(list) != 0) {
5976 /* If the list contains elements fall back to the usual
5977 * non-blocking POP operation */
5978 robj *argv[2], **orig_argv;
5979 int orig_argc;
5980
5981 /* We need to alter the command arguments before to call
5982 * popGenericCommand() as the command takes a single key. */
5983 orig_argv = c->argv;
5984 orig_argc = c->argc;
5985 argv[1] = c->argv[j];
5986 c->argv = argv;
5987 c->argc = 2;
5988
5989 /* Also the return value is different, we need to output
5990 * the multi bulk reply header and the key name. The
5991 * "real" command will add the last element (the value)
5992 * for us. If this souds like an hack to you it's just
5993 * because it is... */
5994 addReplySds(c,sdsnew("*2\r\n"));
5995 addReplyBulkLen(c,argv[1]);
5996 addReply(c,argv[1]);
5997 addReply(c,shared.crlf);
5998 popGenericCommand(c,where);
5999
6000 /* Fix the client structure with the original stuff */
6001 c->argv = orig_argv;
6002 c->argc = orig_argc;
6003 return;
6004 }
6005 }
6006 }
6007 }
6008 /* If the list is empty or the key does not exists we must block */
6009 timeout = strtol(c->argv[c->argc-1]->ptr,NULL,10);
6010 if (timeout > 0) timeout += time(NULL);
6011 blockForKeys(c,c->argv+1,c->argc-2,timeout);
6012 }
6013
6014 static void blpopCommand(redisClient *c) {
6015 blockingPopGenericCommand(c,REDIS_HEAD);
6016 }
6017
6018 static void brpopCommand(redisClient *c) {
6019 blockingPopGenericCommand(c,REDIS_TAIL);
6020 }
6021
6022 /* =============================== Replication ============================= */
6023
6024 static int syncWrite(int fd, char *ptr, ssize_t size, int timeout) {
6025 ssize_t nwritten, ret = size;
6026 time_t start = time(NULL);
6027
6028 timeout++;
6029 while(size) {
6030 if (aeWait(fd,AE_WRITABLE,1000) & AE_WRITABLE) {
6031 nwritten = write(fd,ptr,size);
6032 if (nwritten == -1) return -1;
6033 ptr += nwritten;
6034 size -= nwritten;
6035 }
6036 if ((time(NULL)-start) > timeout) {
6037 errno = ETIMEDOUT;
6038 return -1;
6039 }
6040 }
6041 return ret;
6042 }
6043
6044 static int syncRead(int fd, char *ptr, ssize_t size, int timeout) {
6045 ssize_t nread, totread = 0;
6046 time_t start = time(NULL);
6047
6048 timeout++;
6049 while(size) {
6050 if (aeWait(fd,AE_READABLE,1000) & AE_READABLE) {
6051 nread = read(fd,ptr,size);
6052 if (nread == -1) return -1;
6053 ptr += nread;
6054 size -= nread;
6055 totread += nread;
6056 }
6057 if ((time(NULL)-start) > timeout) {
6058 errno = ETIMEDOUT;
6059 return -1;
6060 }
6061 }
6062 return totread;
6063 }
6064
6065 static int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
6066 ssize_t nread = 0;
6067
6068 size--;
6069 while(size) {
6070 char c;
6071
6072 if (syncRead(fd,&c,1,timeout) == -1) return -1;
6073 if (c == '\n') {
6074 *ptr = '\0';
6075 if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0';
6076 return nread;
6077 } else {
6078 *ptr++ = c;
6079 *ptr = '\0';
6080 nread++;
6081 }
6082 }
6083 return nread;
6084 }
6085
6086 static void syncCommand(redisClient *c) {
6087 /* ignore SYNC if aleady slave or in monitor mode */
6088 if (c->flags & REDIS_SLAVE) return;
6089
6090 /* SYNC can't be issued when the server has pending data to send to
6091 * the client about already issued commands. We need a fresh reply
6092 * buffer registering the differences between the BGSAVE and the current
6093 * dataset, so that we can copy to other slaves if needed. */
6094 if (listLength(c->reply) != 0) {
6095 addReplySds(c,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
6096 return;
6097 }
6098
6099 redisLog(REDIS_NOTICE,"Slave ask for synchronization");
6100 /* Here we need to check if there is a background saving operation
6101 * in progress, or if it is required to start one */
6102 if (server.bgsavechildpid != -1) {
6103 /* Ok a background save is in progress. Let's check if it is a good
6104 * one for replication, i.e. if there is another slave that is
6105 * registering differences since the server forked to save */
6106 redisClient *slave;
6107 listNode *ln;
6108
6109 listRewind(server.slaves);
6110 while((ln = listYield(server.slaves))) {
6111 slave = ln->value;
6112 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;
6113 }
6114 if (ln) {
6115 /* Perfect, the server is already registering differences for
6116 * another slave. Set the right state, and copy the buffer. */
6117 listRelease(c->reply);
6118 c->reply = listDup(slave->reply);
6119 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
6120 redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
6121 } else {
6122 /* No way, we need to wait for the next BGSAVE in order to
6123 * register differences */
6124 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
6125 redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");
6126 }
6127 } else {
6128 /* Ok we don't have a BGSAVE in progress, let's start one */
6129 redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC");
6130 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
6131 redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");
6132 addReplySds(c,sdsnew("-ERR Unalbe to perform background save\r\n"));
6133 return;
6134 }
6135 c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
6136 }
6137 c->repldbfd = -1;
6138 c->flags |= REDIS_SLAVE;
6139 c->slaveseldb = 0;
6140 listAddNodeTail(server.slaves,c);
6141 return;
6142 }
6143
6144 static void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
6145 redisClient *slave = privdata;
6146 REDIS_NOTUSED(el);
6147 REDIS_NOTUSED(mask);
6148 char buf[REDIS_IOBUF_LEN];
6149 ssize_t nwritten, buflen;
6150
6151 if (slave->repldboff == 0) {
6152 /* Write the bulk write count before to transfer the DB. In theory here
6153 * we don't know how much room there is in the output buffer of the
6154 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
6155 * operations) will never be smaller than the few bytes we need. */
6156 sds bulkcount;
6157
6158 bulkcount = sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
6159 slave->repldbsize);
6160 if (write(fd,bulkcount,sdslen(bulkcount)) != (signed)sdslen(bulkcount))
6161 {
6162 sdsfree(bulkcount);
6163 freeClient(slave);
6164 return;
6165 }
6166 sdsfree(bulkcount);
6167 }
6168 lseek(slave->repldbfd,slave->repldboff,SEEK_SET);
6169 buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);
6170 if (buflen <= 0) {
6171 redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",
6172 (buflen == 0) ? "premature EOF" : strerror(errno));
6173 freeClient(slave);
6174 return;
6175 }
6176 if ((nwritten = write(fd,buf,buflen)) == -1) {
6177 redisLog(REDIS_VERBOSE,"Write error sending DB to slave: %s",
6178 strerror(errno));
6179 freeClient(slave);
6180 return;
6181 }
6182 slave->repldboff += nwritten;
6183 if (slave->repldboff == slave->repldbsize) {
6184 close(slave->repldbfd);
6185 slave->repldbfd = -1;
6186 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
6187 slave->replstate = REDIS_REPL_ONLINE;
6188 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
6189 sendReplyToClient, slave) == AE_ERR) {
6190 freeClient(slave);
6191 return;
6192 }
6193 addReplySds(slave,sdsempty());
6194 redisLog(REDIS_NOTICE,"Synchronization with slave succeeded");
6195 }
6196 }
6197
6198 /* This function is called at the end of every backgrond saving.
6199 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
6200 * otherwise REDIS_ERR is passed to the function.
6201 *
6202 * The goal of this function is to handle slaves waiting for a successful
6203 * background saving in order to perform non-blocking synchronization. */
6204 static void updateSlavesWaitingBgsave(int bgsaveerr) {
6205 listNode *ln;
6206 int startbgsave = 0;
6207
6208 listRewind(server.slaves);
6209 while((ln = listYield(server.slaves))) {
6210 redisClient *slave = ln->value;
6211
6212 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
6213 startbgsave = 1;
6214 slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
6215 } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
6216 struct redis_stat buf;
6217
6218 if (bgsaveerr != REDIS_OK) {
6219 freeClient(slave);
6220 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");
6221 continue;
6222 }
6223 if ((slave->repldbfd = open(server.dbfilename,O_RDONLY)) == -1 ||
6224 redis_fstat(slave->repldbfd,&buf) == -1) {
6225 freeClient(slave);
6226 redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));
6227 continue;
6228 }
6229 slave->repldboff = 0;
6230 slave->repldbsize = buf.st_size;
6231 slave->replstate = REDIS_REPL_SEND_BULK;
6232 aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
6233 if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {
6234 freeClient(slave);
6235 continue;
6236 }
6237 }
6238 }
6239 if (startbgsave) {
6240 if (rdbSaveBackground(server.dbfilename) != REDIS_OK) {
6241 listRewind(server.slaves);
6242 redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");
6243 while((ln = listYield(server.slaves))) {
6244 redisClient *slave = ln->value;
6245
6246 if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)
6247 freeClient(slave);
6248 }
6249 }
6250 }
6251 }
6252
6253 static int syncWithMaster(void) {
6254 char buf[1024], tmpfile[256], authcmd[1024];
6255 int dumpsize;
6256 int fd = anetTcpConnect(NULL,server.masterhost,server.masterport);
6257 int dfd;
6258
6259 if (fd == -1) {
6260 redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",
6261 strerror(errno));
6262 return REDIS_ERR;
6263 }
6264
6265 /* AUTH with the master if required. */
6266 if(server.masterauth) {
6267 snprintf(authcmd, 1024, "AUTH %s\r\n", server.masterauth);
6268 if (syncWrite(fd, authcmd, strlen(server.masterauth)+7, 5) == -1) {
6269 close(fd);
6270 redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",
6271 strerror(errno));
6272 return REDIS_ERR;
6273 }
6274 /* Read the AUTH result. */
6275 if (syncReadLine(fd,buf,1024,3600) == -1) {
6276 close(fd);
6277 redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s",
6278 strerror(errno));
6279 return REDIS_ERR;
6280 }
6281 if (buf[0] != '+') {
6282 close(fd);
6283 redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?");
6284 return REDIS_ERR;
6285 }
6286 }
6287
6288 /* Issue the SYNC command */
6289 if (syncWrite(fd,"SYNC \r\n",7,5) == -1) {
6290 close(fd);
6291 redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",
6292 strerror(errno));
6293 return REDIS_ERR;
6294 }
6295 /* Read the bulk write count */
6296 if (syncReadLine(fd,buf,1024,3600) == -1) {
6297 close(fd);
6298 redisLog(REDIS_WARNING,"I/O error reading bulk count from MASTER: %s",
6299 strerror(errno));
6300 return REDIS_ERR;
6301 }
6302 if (buf[0] != '$') {
6303 close(fd);
6304 redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
6305 return REDIS_ERR;
6306 }
6307 dumpsize = atoi(buf+1);
6308 redisLog(REDIS_NOTICE,"Receiving %d bytes data dump from MASTER",dumpsize);
6309 /* Read the bulk write data on a temp file */
6310 snprintf(tmpfile,256,"temp-%d.%ld.rdb",(int)time(NULL),(long int)random());
6311 dfd = open(tmpfile,O_CREAT|O_WRONLY,0644);
6312 if (dfd == -1) {
6313 close(fd);
6314 redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));
6315 return REDIS_ERR;
6316 }
6317 while(dumpsize) {
6318 int nread, nwritten;
6319
6320 nread = read(fd,buf,(dumpsize < 1024)?dumpsize:1024);
6321 if (nread == -1) {
6322 redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
6323 strerror(errno));
6324 close(fd);
6325 close(dfd);
6326 return REDIS_ERR;
6327 }
6328 nwritten = write(dfd,buf,nread);
6329 if (nwritten == -1) {
6330 redisLog(REDIS_WARNING,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno));
6331 close(fd);
6332 close(dfd);
6333 return REDIS_ERR;
6334 }
6335 dumpsize -= nread;
6336 }
6337 close(dfd);
6338 if (rename(tmpfile,server.dbfilename) == -1) {
6339 redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
6340 unlink(tmpfile);
6341 close(fd);
6342 return REDIS_ERR;
6343 }
6344 emptyDb();
6345 if (rdbLoad(server.dbfilename) != REDIS_OK) {
6346 redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");
6347 close(fd);
6348 return REDIS_ERR;
6349 }
6350 server.master = createClient(fd);
6351 server.master->flags |= REDIS_MASTER;
6352 server.master->authenticated = 1;
6353 server.replstate = REDIS_REPL_CONNECTED;
6354 return REDIS_OK;
6355 }
6356
6357 static void slaveofCommand(redisClient *c) {
6358 if (!strcasecmp(c->argv[1]->ptr,"no") &&
6359 !strcasecmp(c->argv[2]->ptr,"one")) {
6360 if (server.masterhost) {
6361 sdsfree(server.masterhost);
6362 server.masterhost = NULL;
6363 if (server.master) freeClient(server.master);
6364 server.replstate = REDIS_REPL_NONE;
6365 redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
6366 }
6367 } else {
6368 sdsfree(server.masterhost);
6369 server.masterhost = sdsdup(c->argv[1]->ptr);
6370 server.masterport = atoi(c->argv[2]->ptr);
6371 if (server.master) freeClient(server.master);
6372 server.replstate = REDIS_REPL_CONNECT;
6373 redisLog(REDIS_NOTICE,"SLAVE OF %s:%d enabled (user request)",
6374 server.masterhost, server.masterport);
6375 }
6376 addReply(c,shared.ok);
6377 }
6378
6379 /* ============================ Maxmemory directive ======================== */
6380
6381 /* Free one object form the pre-allocated objects free list. This is useful
6382 * under low mem conditions as by default we take 1 million free objects
6383 * allocated. */
6384 static void freeOneObjectFromFreelist(void) {
6385 robj *o;
6386
6387 listNode *head = listFirst(server.objfreelist);
6388 o = listNodeValue(head);
6389 listDelNode(server.objfreelist,head);
6390 zfree(o);
6391 }
6392
6393 /* This function gets called when 'maxmemory' is set on the config file to limit
6394 * the max memory used by the server, and we are out of memory.
6395 * This function will try to, in order:
6396 *
6397 * - Free objects from the free list
6398 * - Try to remove keys with an EXPIRE set
6399 *
6400 * It is not possible to free enough memory to reach used-memory < maxmemory
6401 * the server will start refusing commands that will enlarge even more the
6402 * memory usage.
6403 */
6404 static void freeMemoryIfNeeded(void) {
6405 while (server.maxmemory && zmalloc_used_memory() > server.maxmemory) {
6406 if (listLength(server.objfreelist)) {
6407 freeOneObjectFromFreelist();
6408 } else {
6409 int j, k, freed = 0;
6410
6411 for (j = 0; j < server.dbnum; j++) {
6412 int minttl = -1;
6413 robj *minkey = NULL;
6414 struct dictEntry *de;
6415
6416 if (dictSize(server.db[j].expires)) {
6417 freed = 1;
6418 /* From a sample of three keys drop the one nearest to
6419 * the natural expire */
6420 for (k = 0; k < 3; k++) {
6421 time_t t;
6422
6423 de = dictGetRandomKey(server.db[j].expires);
6424 t = (time_t) dictGetEntryVal(de);
6425 if (minttl == -1 || t < minttl) {
6426 minkey = dictGetEntryKey(de);
6427 minttl = t;
6428 }
6429 }
6430 deleteKey(server.db+j,minkey);
6431 }
6432 }
6433 if (!freed) return; /* nothing to free... */
6434 }
6435 }
6436 }
6437
6438 /* ============================== Append Only file ========================== */
6439
6440 static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
6441 sds buf = sdsempty();
6442 int j;
6443 ssize_t nwritten;
6444 time_t now;
6445 robj *tmpargv[3];
6446
6447 /* The DB this command was targetting is not the same as the last command
6448 * we appendend. To issue a SELECT command is needed. */
6449 if (dictid != server.appendseldb) {
6450 char seldb[64];
6451
6452 snprintf(seldb,sizeof(seldb),"%d",dictid);
6453 buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
6454 (unsigned long)strlen(seldb),seldb);
6455 server.appendseldb = dictid;
6456 }
6457
6458 /* "Fix" the argv vector if the command is EXPIRE. We want to translate
6459 * EXPIREs into EXPIREATs calls */
6460 if (cmd->proc == expireCommand) {
6461 long when;
6462
6463 tmpargv[0] = createStringObject("EXPIREAT",8);
6464 tmpargv[1] = argv[1];
6465 incrRefCount(argv[1]);
6466 when = time(NULL)+strtol(argv[2]->ptr,NULL,10);
6467 tmpargv[2] = createObject(REDIS_STRING,
6468 sdscatprintf(sdsempty(),"%ld",when));
6469 argv = tmpargv;
6470 }
6471
6472 /* Append the actual command */
6473 buf = sdscatprintf(buf,"*%d\r\n",argc);
6474 for (j = 0; j < argc; j++) {
6475 robj *o = argv[j];
6476
6477 o = getDecodedObject(o);
6478 buf = sdscatprintf(buf,"$%lu\r\n",(unsigned long)sdslen(o->ptr));
6479 buf = sdscatlen(buf,o->ptr,sdslen(o->ptr));
6480 buf = sdscatlen(buf,"\r\n",2);
6481 decrRefCount(o);
6482 }
6483
6484 /* Free the objects from the modified argv for EXPIREAT */
6485 if (cmd->proc == expireCommand) {
6486 for (j = 0; j < 3; j++)
6487 decrRefCount(argv[j]);
6488 }
6489
6490 /* We want to perform a single write. This should be guaranteed atomic
6491 * at least if the filesystem we are writing is a real physical one.
6492 * While this will save us against the server being killed I don't think
6493 * there is much to do about the whole server stopping for power problems
6494 * or alike */
6495 nwritten = write(server.appendfd,buf,sdslen(buf));
6496 if (nwritten != (signed)sdslen(buf)) {
6497 /* Ooops, we are in troubles. The best thing to do for now is
6498 * to simply exit instead to give the illusion that everything is
6499 * working as expected. */
6500 if (nwritten == -1) {
6501 redisLog(REDIS_WARNING,"Exiting on error writing to the append-only file: %s",strerror(errno));
6502 } else {
6503 redisLog(REDIS_WARNING,"Exiting on short write while writing to the append-only file: %s",strerror(errno));
6504 }
6505 exit(1);
6506 }
6507 /* If a background append only file rewriting is in progress we want to
6508 * accumulate the differences between the child DB and the current one
6509 * in a buffer, so that when the child process will do its work we
6510 * can append the differences to the new append only file. */
6511 if (server.bgrewritechildpid != -1)
6512 server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf));
6513
6514 sdsfree(buf);
6515 now = time(NULL);
6516 if (server.appendfsync == APPENDFSYNC_ALWAYS ||
6517 (server.appendfsync == APPENDFSYNC_EVERYSEC &&
6518 now-server.lastfsync > 1))
6519 {
6520 fsync(server.appendfd); /* Let's try to get this data on the disk */
6521 server.lastfsync = now;
6522 }
6523 }
6524
6525 /* In Redis commands are always executed in the context of a client, so in
6526 * order to load the append only file we need to create a fake client. */
6527 static struct redisClient *createFakeClient(void) {
6528 struct redisClient *c = zmalloc(sizeof(*c));
6529
6530 selectDb(c,0);
6531 c->fd = -1;
6532 c->querybuf = sdsempty();
6533 c->argc = 0;
6534 c->argv = NULL;
6535 c->flags = 0;
6536 /* We set the fake client as a slave waiting for the synchronization
6537 * so that Redis will not try to send replies to this client. */
6538 c->replstate = REDIS_REPL_WAIT_BGSAVE_START;
6539 c->reply = listCreate();
6540 listSetFreeMethod(c->reply,decrRefCount);
6541 listSetDupMethod(c->reply,dupClientReplyValue);
6542 return c;
6543 }
6544
6545 static void freeFakeClient(struct redisClient *c) {
6546 sdsfree(c->querybuf);
6547 listRelease(c->reply);
6548 zfree(c);
6549 }
6550
6551 /* Replay the append log file. On error REDIS_OK is returned. On non fatal
6552 * error (the append only file is zero-length) REDIS_ERR is returned. On
6553 * fatal error an error message is logged and the program exists. */
6554 int loadAppendOnlyFile(char *filename) {
6555 struct redisClient *fakeClient;
6556 FILE *fp = fopen(filename,"r");
6557 struct redis_stat sb;
6558 unsigned long long loadedkeys = 0;
6559
6560 if (redis_fstat(fileno(fp),&sb) != -1 && sb.st_size == 0)
6561 return REDIS_ERR;
6562
6563 if (fp == NULL) {
6564 redisLog(REDIS_WARNING,"Fatal error: can't open the append log file for reading: %s",strerror(errno));
6565 exit(1);
6566 }
6567
6568 fakeClient = createFakeClient();
6569 while(1) {
6570 int argc, j;
6571 unsigned long len;
6572 robj **argv;
6573 char buf[128];
6574 sds argsds;
6575 struct redisCommand *cmd;
6576
6577 if (fgets(buf,sizeof(buf),fp) == NULL) {
6578 if (feof(fp))
6579 break;
6580 else
6581 goto readerr;
6582 }
6583 if (buf[0] != '*') goto fmterr;
6584 argc = atoi(buf+1);
6585 argv = zmalloc(sizeof(robj*)*argc);
6586 for (j = 0; j < argc; j++) {
6587 if (fgets(buf,sizeof(buf),fp) == NULL) goto readerr;
6588 if (buf[0] != '$') goto fmterr;
6589 len = strtol(buf+1,NULL,10);
6590 argsds = sdsnewlen(NULL,len);
6591 if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
6592 argv[j] = createObject(REDIS_STRING,argsds);
6593 if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
6594 }
6595
6596 /* Command lookup */
6597 cmd = lookupCommand(argv[0]->ptr);
6598 if (!cmd) {
6599 redisLog(REDIS_WARNING,"Unknown command '%s' reading the append only file", argv[0]->ptr);
6600 exit(1);
6601 }
6602 /* Try object sharing and encoding */
6603 if (server.shareobjects) {
6604 int j;
6605 for(j = 1; j < argc; j++)
6606 argv[j] = tryObjectSharing(argv[j]);
6607 }
6608 if (cmd->flags & REDIS_CMD_BULK)
6609 tryObjectEncoding(argv[argc-1]);
6610 /* Run the command in the context of a fake client */
6611 fakeClient->argc = argc;
6612 fakeClient->argv = argv;
6613 cmd->proc(fakeClient);
6614 /* Discard the reply objects list from the fake client */
6615 while(listLength(fakeClient->reply))
6616 listDelNode(fakeClient->reply,listFirst(fakeClient->reply));
6617 /* Clean up, ready for the next command */
6618 for (j = 0; j < argc; j++) decrRefCount(argv[j]);
6619 zfree(argv);
6620 /* Handle swapping while loading big datasets when VM is on */
6621 loadedkeys++;
6622 if (server.vm_enabled && (loadedkeys % 5000) == 0) {
6623 while (zmalloc_used_memory() > server.vm_max_memory) {
6624 if (vmSwapOneObjectBlocking() == REDIS_ERR) break;
6625 }
6626 }
6627 }
6628 fclose(fp);
6629 freeFakeClient(fakeClient);
6630 return REDIS_OK;
6631
6632 readerr:
6633 if (feof(fp)) {
6634 redisLog(REDIS_WARNING,"Unexpected end of file reading the append only file");
6635 } else {
6636 redisLog(REDIS_WARNING,"Unrecoverable error reading the append only file: %s", strerror(errno));
6637 }
6638 exit(1);
6639 fmterr:
6640 redisLog(REDIS_WARNING,"Bad file format reading the append only file");
6641 exit(1);
6642 }
6643
6644 /* Write an object into a file in the bulk format $<count>\r\n<payload>\r\n */
6645 static int fwriteBulk(FILE *fp, robj *obj) {
6646 char buf[128];
6647 int decrrc = 0;
6648
6649 if (obj->storage == REDIS_VM_MEMORY && obj->encoding != REDIS_ENCODING_RAW){
6650 obj = getDecodedObject(obj);
6651 decrrc = 1;
6652 }
6653 snprintf(buf,sizeof(buf),"$%ld\r\n",(long)sdslen(obj->ptr));
6654 if (fwrite(buf,strlen(buf),1,fp) == 0) goto err;
6655 if (sdslen(obj->ptr) && fwrite(obj->ptr,sdslen(obj->ptr),1,fp) == 0)
6656 goto err;
6657 if (fwrite("\r\n",2,1,fp) == 0) goto err;
6658 if (decrrc) decrRefCount(obj);
6659 return 1;
6660 err:
6661 if (decrrc) decrRefCount(obj);
6662 return 0;
6663 }
6664
6665 /* Write a double value in bulk format $<count>\r\n<payload>\r\n */
6666 static int fwriteBulkDouble(FILE *fp, double d) {
6667 char buf[128], dbuf[128];
6668
6669 snprintf(dbuf,sizeof(dbuf),"%.17g\r\n",d);
6670 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(dbuf)-2);
6671 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
6672 if (fwrite(dbuf,strlen(dbuf),1,fp) == 0) return 0;
6673 return 1;
6674 }
6675
6676 /* Write a long value in bulk format $<count>\r\n<payload>\r\n */
6677 static int fwriteBulkLong(FILE *fp, long l) {
6678 char buf[128], lbuf[128];
6679
6680 snprintf(lbuf,sizeof(lbuf),"%ld\r\n",l);
6681 snprintf(buf,sizeof(buf),"$%lu\r\n",(unsigned long)strlen(lbuf)-2);
6682 if (fwrite(buf,strlen(buf),1,fp) == 0) return 0;
6683 if (fwrite(lbuf,strlen(lbuf),1,fp) == 0) return 0;
6684 return 1;
6685 }
6686
6687 /* Write a sequence of commands able to fully rebuild the dataset into
6688 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
6689 static int rewriteAppendOnlyFile(char *filename) {
6690 dictIterator *di = NULL;
6691 dictEntry *de;
6692 FILE *fp;
6693 char tmpfile[256];
6694 int j;
6695 time_t now = time(NULL);
6696
6697 /* Note that we have to use a different temp name here compared to the
6698 * one used by rewriteAppendOnlyFileBackground() function. */
6699 snprintf(tmpfile,256,"temp-rewriteaof-%d.aof", (int) getpid());
6700 fp = fopen(tmpfile,"w");
6701 if (!fp) {
6702 redisLog(REDIS_WARNING, "Failed rewriting the append only file: %s", strerror(errno));
6703 return REDIS_ERR;
6704 }
6705 for (j = 0; j < server.dbnum; j++) {
6706 char selectcmd[] = "*2\r\n$6\r\nSELECT\r\n";
6707 redisDb *db = server.db+j;
6708 dict *d = db->dict;
6709 if (dictSize(d) == 0) continue;
6710 di = dictGetIterator(d);
6711 if (!di) {
6712 fclose(fp);
6713 return REDIS_ERR;
6714 }
6715
6716 /* SELECT the new DB */
6717 if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr;
6718 if (fwriteBulkLong(fp,j) == 0) goto werr;
6719
6720 /* Iterate this DB writing every entry */
6721 while((de = dictNext(di)) != NULL) {
6722 robj *key, *o;
6723 time_t expiretime;
6724 int swapped;
6725
6726 key = dictGetEntryKey(de);
6727 /* If the value for this key is swapped, load a preview in memory.
6728 * We use a "swapped" flag to remember if we need to free the
6729 * value object instead to just increment the ref count anyway
6730 * in order to avoid copy-on-write of pages if we are forked() */
6731 if (!server.vm_enabled || key->storage == REDIS_VM_MEMORY ||
6732 key->storage == REDIS_VM_SWAPPING) {
6733 o = dictGetEntryVal(de);
6734 swapped = 0;
6735 } else {
6736 o = vmPreviewObject(key);
6737 swapped = 1;
6738 }
6739 expiretime = getExpire(db,key);
6740
6741 /* Save the key and associated value */
6742 if (o->type == REDIS_STRING) {
6743 /* Emit a SET command */
6744 char cmd[]="*3\r\n$3\r\nSET\r\n";
6745 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
6746 /* Key and value */
6747 if (fwriteBulk(fp,key) == 0) goto werr;
6748 if (fwriteBulk(fp,o) == 0) goto werr;
6749 } else if (o->type == REDIS_LIST) {
6750 /* Emit the RPUSHes needed to rebuild the list */
6751 list *list = o->ptr;
6752 listNode *ln;
6753
6754 listRewind(list);
6755 while((ln = listYield(list))) {
6756 char cmd[]="*3\r\n$5\r\nRPUSH\r\n";
6757 robj *eleobj = listNodeValue(ln);
6758
6759 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
6760 if (fwriteBulk(fp,key) == 0) goto werr;
6761 if (fwriteBulk(fp,eleobj) == 0) goto werr;
6762 }
6763 } else if (o->type == REDIS_SET) {
6764 /* Emit the SADDs needed to rebuild the set */
6765 dict *set = o->ptr;
6766 dictIterator *di = dictGetIterator(set);
6767 dictEntry *de;
6768
6769 while((de = dictNext(di)) != NULL) {
6770 char cmd[]="*3\r\n$4\r\nSADD\r\n";
6771 robj *eleobj = dictGetEntryKey(de);
6772
6773 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
6774 if (fwriteBulk(fp,key) == 0) goto werr;
6775 if (fwriteBulk(fp,eleobj) == 0) goto werr;
6776 }
6777 dictReleaseIterator(di);
6778 } else if (o->type == REDIS_ZSET) {
6779 /* Emit the ZADDs needed to rebuild the sorted set */
6780 zset *zs = o->ptr;
6781 dictIterator *di = dictGetIterator(zs->dict);
6782 dictEntry *de;
6783
6784 while((de = dictNext(di)) != NULL) {
6785 char cmd[]="*4\r\n$4\r\nZADD\r\n";
6786 robj *eleobj = dictGetEntryKey(de);
6787 double *score = dictGetEntryVal(de);
6788
6789 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
6790 if (fwriteBulk(fp,key) == 0) goto werr;
6791 if (fwriteBulkDouble(fp,*score) == 0) goto werr;
6792 if (fwriteBulk(fp,eleobj) == 0) goto werr;
6793 }
6794 dictReleaseIterator(di);
6795 } else {
6796 redisAssert(0 != 0);
6797 }
6798 /* Save the expire time */
6799 if (expiretime != -1) {
6800 char cmd[]="*3\r\n$8\r\nEXPIREAT\r\n";
6801 /* If this key is already expired skip it */
6802 if (expiretime < now) continue;
6803 if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
6804 if (fwriteBulk(fp,key) == 0) goto werr;
6805 if (fwriteBulkLong(fp,expiretime) == 0) goto werr;
6806 }
6807 if (swapped) decrRefCount(o);
6808 }
6809 dictReleaseIterator(di);
6810 }
6811
6812 /* Make sure data will not remain on the OS's output buffers */
6813 fflush(fp);
6814 fsync(fileno(fp));
6815 fclose(fp);
6816
6817 /* Use RENAME to make sure the DB file is changed atomically only
6818 * if the generate DB file is ok. */
6819 if (rename(tmpfile,filename) == -1) {
6820 redisLog(REDIS_WARNING,"Error moving temp append only file on the final destination: %s", strerror(errno));
6821 unlink(tmpfile);
6822 return REDIS_ERR;
6823 }
6824 redisLog(REDIS_NOTICE,"SYNC append only file rewrite performed");
6825 return REDIS_OK;
6826
6827 werr:
6828 fclose(fp);
6829 unlink(tmpfile);
6830 redisLog(REDIS_WARNING,"Write error writing append only file on disk: %s", strerror(errno));
6831 if (di) dictReleaseIterator(di);
6832 return REDIS_ERR;
6833 }
6834
6835 /* This is how rewriting of the append only file in background works:
6836 *
6837 * 1) The user calls BGREWRITEAOF
6838 * 2) Redis calls this function, that forks():
6839 * 2a) the child rewrite the append only file in a temp file.
6840 * 2b) the parent accumulates differences in server.bgrewritebuf.
6841 * 3) When the child finished '2a' exists.
6842 * 4) The parent will trap the exit code, if it's OK, will append the
6843 * data accumulated into server.bgrewritebuf into the temp file, and
6844 * finally will rename(2) the temp file in the actual file name.
6845 * The the new file is reopened as the new append only file. Profit!
6846 */
6847 static int rewriteAppendOnlyFileBackground(void) {
6848 pid_t childpid;
6849
6850 if (server.bgrewritechildpid != -1) return REDIS_ERR;
6851 if ((childpid = fork()) == 0) {
6852 /* Child */
6853 char tmpfile[256];
6854 close(server.fd);
6855
6856 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
6857 if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
6858 exit(0);
6859 } else {
6860 exit(1);
6861 }
6862 } else {
6863 /* Parent */
6864 if (childpid == -1) {
6865 redisLog(REDIS_WARNING,
6866 "Can't rewrite append only file in background: fork: %s",
6867 strerror(errno));
6868 return REDIS_ERR;
6869 }
6870 redisLog(REDIS_NOTICE,
6871 "Background append only file rewriting started by pid %d",childpid);
6872 server.bgrewritechildpid = childpid;
6873 /* We set appendseldb to -1 in order to force the next call to the
6874 * feedAppendOnlyFile() to issue a SELECT command, so the differences
6875 * accumulated by the parent into server.bgrewritebuf will start
6876 * with a SELECT statement and it will be safe to merge. */
6877 server.appendseldb = -1;
6878 return REDIS_OK;
6879 }
6880 return REDIS_OK; /* unreached */
6881 }
6882
6883 static void bgrewriteaofCommand(redisClient *c) {
6884 if (server.bgrewritechildpid != -1) {
6885 addReplySds(c,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
6886 return;
6887 }
6888 if (rewriteAppendOnlyFileBackground() == REDIS_OK) {
6889 char *status = "+Background append only file rewriting started\r\n";
6890 addReplySds(c,sdsnew(status));
6891 } else {
6892 addReply(c,shared.err);
6893 }
6894 }
6895
6896 static void aofRemoveTempFile(pid_t childpid) {
6897 char tmpfile[256];
6898
6899 snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) childpid);
6900 unlink(tmpfile);
6901 }
6902
6903 /* Virtual Memory is composed mainly of two subsystems:
6904 * - Blocking Virutal Memory
6905 * - Threaded Virtual Memory I/O
6906 * The two parts are not fully decoupled, but functions are split among two
6907 * different sections of the source code (delimited by comments) in order to
6908 * make more clear what functionality is about the blocking VM and what about
6909 * the threaded (not blocking) VM.
6910 *
6911 * Redis VM design:
6912 *
6913 * Redis VM is a blocking VM (one that blocks reading swapped values from
6914 * disk into memory when a value swapped out is needed in memory) that is made
6915 * unblocking by trying to examine the command argument vector in order to
6916 * load in background values that will likely be needed in order to exec
6917 * the command. The command is executed only once all the relevant keys
6918 * are loaded into memory.
6919 *
6920 * This basically is almost as simple of a blocking VM, but almost as parallel
6921 * as a fully non-blocking VM.
6922 */
6923
6924 /* =================== Virtual Memory - Blocking Side ====================== */
6925 static void vmInit(void) {
6926 off_t totsize;
6927 int pipefds[2];
6928
6929 server.vm_fp = fopen("/tmp/redisvm","w+b");
6930 if (server.vm_fp == NULL) {
6931 redisLog(REDIS_WARNING,"Impossible to open the swap file. Exiting.");
6932 exit(1);
6933 }
6934 server.vm_fd = fileno(server.vm_fp);
6935 server.vm_next_page = 0;
6936 server.vm_near_pages = 0;
6937 server.vm_stats_used_pages = 0;
6938 server.vm_stats_swapped_objects = 0;
6939 server.vm_stats_swapouts = 0;
6940 server.vm_stats_swapins = 0;
6941 totsize = server.vm_pages*server.vm_page_size;
6942 redisLog(REDIS_NOTICE,"Allocating %lld bytes of swap file",totsize);
6943 if (ftruncate(server.vm_fd,totsize) == -1) {
6944 redisLog(REDIS_WARNING,"Can't ftruncate swap file: %s. Exiting.",
6945 strerror(errno));
6946 exit(1);
6947 } else {
6948 redisLog(REDIS_NOTICE,"Swap file allocated with success");
6949 }
6950 server.vm_bitmap = zmalloc((server.vm_pages+7)/8);
6951 redisLog(REDIS_VERBOSE,"Allocated %lld bytes page table for %lld pages",
6952 (long long) (server.vm_pages+7)/8, server.vm_pages);
6953 memset(server.vm_bitmap,0,(server.vm_pages+7)/8);
6954 /* Try to remove the swap file, so the OS will really delete it from the
6955 * file system when Redis exists. */
6956 unlink("/tmp/redisvm");
6957
6958 /* Initialize threaded I/O (used by Virtual Memory) */
6959 server.io_newjobs = listCreate();
6960 server.io_processing = listCreate();
6961 server.io_processed = listCreate();
6962 server.io_clients = listCreate();
6963 pthread_mutex_init(&server.io_mutex,NULL);
6964 server.io_active_threads = 0;
6965 if (pipe(pipefds) == -1) {
6966 redisLog(REDIS_WARNING,"Unable to intialized VM: pipe(2): %s. Exiting."
6967 ,strerror(errno));
6968 exit(1);
6969 }
6970 server.io_ready_pipe_read = pipefds[0];
6971 server.io_ready_pipe_write = pipefds[1];
6972 redisAssert(anetNonBlock(NULL,server.io_ready_pipe_read) != ANET_ERR);
6973 /* Listen for events in the threaded I/O pipe */
6974 if (aeCreateFileEvent(server.el, server.io_ready_pipe_read, AE_READABLE,
6975 vmThreadedIOCompletedJob, NULL) == AE_ERR)
6976 oom("creating file event");
6977 }
6978
6979 /* Mark the page as used */
6980 static void vmMarkPageUsed(off_t page) {
6981 off_t byte = page/8;
6982 int bit = page&7;
6983 server.vm_bitmap[byte] |= 1<<bit;
6984 redisLog(REDIS_DEBUG,"Mark used: %lld (byte:%lld bit:%d)\n",
6985 (long long)page, (long long)byte, bit);
6986 }
6987
6988 /* Mark N contiguous pages as used, with 'page' being the first. */
6989 static void vmMarkPagesUsed(off_t page, off_t count) {
6990 off_t j;
6991
6992 for (j = 0; j < count; j++)
6993 vmMarkPageUsed(page+j);
6994 server.vm_stats_used_pages += count;
6995 }
6996
6997 /* Mark the page as free */
6998 static void vmMarkPageFree(off_t page) {
6999 off_t byte = page/8;
7000 int bit = page&7;
7001 server.vm_bitmap[byte] &= ~(1<<bit);
7002 }
7003
7004 /* Mark N contiguous pages as free, with 'page' being the first. */
7005 static void vmMarkPagesFree(off_t page, off_t count) {
7006 off_t j;
7007
7008 for (j = 0; j < count; j++)
7009 vmMarkPageFree(page+j);
7010 server.vm_stats_used_pages -= count;
7011 }
7012
7013 /* Test if the page is free */
7014 static int vmFreePage(off_t page) {
7015 off_t byte = page/8;
7016 int bit = page&7;
7017 return (server.vm_bitmap[byte] & (1<<bit)) == 0;
7018 }
7019
7020 /* Find N contiguous free pages storing the first page of the cluster in *first.
7021 * Returns REDIS_OK if it was able to find N contiguous pages, otherwise
7022 * REDIS_ERR is returned.
7023 *
7024 * This function uses a simple algorithm: we try to allocate
7025 * REDIS_VM_MAX_NEAR_PAGES sequentially, when we reach this limit we start
7026 * again from the start of the swap file searching for free spaces.
7027 *
7028 * If it looks pretty clear that there are no free pages near our offset
7029 * we try to find less populated places doing a forward jump of
7030 * REDIS_VM_MAX_RANDOM_JUMP, then we start scanning again a few pages
7031 * without hurry, and then we jump again and so forth...
7032 *
7033 * This function can be improved using a free list to avoid to guess
7034 * too much, since we could collect data about freed pages.
7035 *
7036 * note: I implemented this function just after watching an episode of
7037 * Battlestar Galactica, where the hybrid was continuing to say "JUMP!"
7038 */
7039 static int vmFindContiguousPages(off_t *first, int n) {
7040 off_t base, offset = 0, since_jump = 0, numfree = 0;
7041
7042 if (server.vm_near_pages == REDIS_VM_MAX_NEAR_PAGES) {
7043 server.vm_near_pages = 0;
7044 server.vm_next_page = 0;
7045 }
7046 server.vm_near_pages++; /* Yet another try for pages near to the old ones */
7047 base = server.vm_next_page;
7048
7049 while(offset < server.vm_pages) {
7050 off_t this = base+offset;
7051
7052 redisLog(REDIS_DEBUG, "THIS: %lld (%c)\n", (long long) this, vmFreePage(this) ? 'F' : 'X');
7053 /* If we overflow, restart from page zero */
7054 if (this >= server.vm_pages) {
7055 this -= server.vm_pages;
7056 if (this == 0) {
7057 /* Just overflowed, what we found on tail is no longer
7058 * interesting, as it's no longer contiguous. */
7059 numfree = 0;
7060 }
7061 }
7062 if (vmFreePage(this)) {
7063 /* This is a free page */
7064 numfree++;
7065 /* Already got N free pages? Return to the caller, with success */
7066 if (numfree == n) {
7067 *first = this-(n-1);
7068 server.vm_next_page = this+1;
7069 return REDIS_OK;
7070 }
7071 } else {
7072 /* The current one is not a free page */
7073 numfree = 0;
7074 }
7075
7076 /* Fast-forward if the current page is not free and we already
7077 * searched enough near this place. */
7078 since_jump++;
7079 if (!numfree && since_jump >= REDIS_VM_MAX_RANDOM_JUMP/4) {
7080 offset += random() % REDIS_VM_MAX_RANDOM_JUMP;
7081 since_jump = 0;
7082 /* Note that even if we rewind after the jump, we are don't need
7083 * to make sure numfree is set to zero as we only jump *if* it
7084 * is set to zero. */
7085 } else {
7086 /* Otherwise just check the next page */
7087 offset++;
7088 }
7089 }
7090 return REDIS_ERR;
7091 }
7092
7093 /* Swap the 'val' object relative to 'key' into disk. Store all the information
7094 * needed to later retrieve the object into the key object.
7095 * If we can't find enough contiguous empty pages to swap the object on disk
7096 * REDIS_ERR is returned. */
7097 static int vmSwapObjectBlocking(robj *key, robj *val) {
7098 off_t pages = rdbSavedObjectPages(val,NULL);
7099 off_t page;
7100
7101 assert(key->storage == REDIS_VM_MEMORY);
7102 assert(key->refcount == 1);
7103 if (vmFindContiguousPages(&page,pages) == REDIS_ERR) return REDIS_ERR;
7104 if (fseeko(server.vm_fp,page*server.vm_page_size,SEEK_SET) == -1) {
7105 redisLog(REDIS_WARNING,
7106 "Critical VM problem in vmSwapObjectBlocking(): can't seek: %s",
7107 strerror(errno));
7108 return REDIS_ERR;
7109 }
7110 rdbSaveObject(server.vm_fp,val);
7111 key->vm.page = page;
7112 key->vm.usedpages = pages;
7113 key->storage = REDIS_VM_SWAPPED;
7114 key->vtype = val->type;
7115 decrRefCount(val); /* Deallocate the object from memory. */
7116 vmMarkPagesUsed(page,pages);
7117 redisLog(REDIS_DEBUG,"VM: object %s swapped out at %lld (%lld pages)",
7118 (unsigned char*) key->ptr,
7119 (unsigned long long) page, (unsigned long long) pages);
7120 server.vm_stats_swapped_objects++;
7121 server.vm_stats_swapouts++;
7122 fflush(server.vm_fp);
7123 return REDIS_OK;
7124 }
7125
7126 /* Load the value object relative to the 'key' object from swap to memory.
7127 * The newly allocated object is returned.
7128 *
7129 * If preview is true the unserialized object is returned to the caller but
7130 * no changes are made to the key object, nor the pages are marked as freed */
7131 static robj *vmGenericLoadObject(robj *key, int preview) {
7132 robj *val;
7133
7134 redisAssert(key->storage == REDIS_VM_SWAPPED);
7135 if (fseeko(server.vm_fp,key->vm.page*server.vm_page_size,SEEK_SET) == -1) {
7136 redisLog(REDIS_WARNING,
7137 "Unrecoverable VM problem in vmLoadObject(): can't seek: %s",
7138 strerror(errno));
7139 exit(1);
7140 }
7141 val = rdbLoadObject(key->vtype,server.vm_fp);
7142 if (val == NULL) {
7143 redisLog(REDIS_WARNING, "Unrecoverable VM problem in vmLoadObject(): can't load object from swap file: %s", strerror(errno));
7144 exit(1);
7145 }
7146 if (!preview) {
7147 key->storage = REDIS_VM_MEMORY;
7148 key->vm.atime = server.unixtime;
7149 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
7150 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk",
7151 (unsigned char*) key->ptr);
7152 server.vm_stats_swapped_objects--;
7153 } else {
7154 redisLog(REDIS_DEBUG, "VM: object %s previewed from disk",
7155 (unsigned char*) key->ptr);
7156 }
7157 server.vm_stats_swapins++;
7158 return val;
7159 }
7160
7161 /* Plain object loading, from swap to memory */
7162 static robj *vmLoadObject(robj *key) {
7163 /* If we are loading the object in background, stop it, we
7164 * need to load this object synchronously ASAP. */
7165 if (key->storage == REDIS_VM_LOADING)
7166 vmCancelThreadedIOJob(key);
7167 return vmGenericLoadObject(key,0);
7168 }
7169
7170 /* Just load the value on disk, without to modify the key.
7171 * This is useful when we want to perform some operation on the value
7172 * without to really bring it from swap to memory, like while saving the
7173 * dataset or rewriting the append only log. */
7174 static robj *vmPreviewObject(robj *key) {
7175 return vmGenericLoadObject(key,1);
7176 }
7177
7178 /* How a good candidate is this object for swapping?
7179 * The better candidate it is, the greater the returned value.
7180 *
7181 * Currently we try to perform a fast estimation of the object size in
7182 * memory, and combine it with aging informations.
7183 *
7184 * Basically swappability = idle-time * log(estimated size)
7185 *
7186 * Bigger objects are preferred over smaller objects, but not
7187 * proportionally, this is why we use the logarithm. This algorithm is
7188 * just a first try and will probably be tuned later. */
7189 static double computeObjectSwappability(robj *o) {
7190 time_t age = server.unixtime - o->vm.atime;
7191 long asize = 0;
7192 list *l;
7193 dict *d;
7194 struct dictEntry *de;
7195 int z;
7196
7197 if (age <= 0) return 0;
7198 switch(o->type) {
7199 case REDIS_STRING:
7200 if (o->encoding != REDIS_ENCODING_RAW) {
7201 asize = sizeof(*o);
7202 } else {
7203 asize = sdslen(o->ptr)+sizeof(*o)+sizeof(long)*2;
7204 }
7205 break;
7206 case REDIS_LIST:
7207 l = o->ptr;
7208 listNode *ln = listFirst(l);
7209
7210 asize = sizeof(list);
7211 if (ln) {
7212 robj *ele = ln->value;
7213 long elesize;
7214
7215 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
7216 (sizeof(*o)+sdslen(ele->ptr)) :
7217 sizeof(*o);
7218 asize += (sizeof(listNode)+elesize)*listLength(l);
7219 }
7220 break;
7221 case REDIS_SET:
7222 case REDIS_ZSET:
7223 z = (o->type == REDIS_ZSET);
7224 d = z ? ((zset*)o->ptr)->dict : o->ptr;
7225
7226 asize = sizeof(dict)+(sizeof(struct dictEntry*)*dictSlots(d));
7227 if (z) asize += sizeof(zset)-sizeof(dict);
7228 if (dictSize(d)) {
7229 long elesize;
7230 robj *ele;
7231
7232 de = dictGetRandomKey(d);
7233 ele = dictGetEntryKey(de);
7234 elesize = (ele->encoding == REDIS_ENCODING_RAW) ?
7235 (sizeof(*o)+sdslen(ele->ptr)) :
7236 sizeof(*o);
7237 asize += (sizeof(struct dictEntry)+elesize)*dictSize(d);
7238 if (z) asize += sizeof(zskiplistNode)*dictSize(d);
7239 }
7240 break;
7241 }
7242 return (double)asize*log(1+asize);
7243 }
7244
7245 /* Try to swap an object that's a good candidate for swapping.
7246 * Returns REDIS_OK if the object was swapped, REDIS_ERR if it's not possible
7247 * to swap any object at all.
7248 *
7249 * If 'usethreaded' is true, Redis will try to swap the object in background
7250 * using I/O threads. */
7251 static int vmSwapOneObject(int usethreads) {
7252 int j, i;
7253 struct dictEntry *best = NULL;
7254 double best_swappability = 0;
7255 redisDb *best_db = NULL;
7256 robj *key, *val;
7257
7258 for (j = 0; j < server.dbnum; j++) {
7259 redisDb *db = server.db+j;
7260 int maxtries = 1000;
7261
7262 if (dictSize(db->dict) == 0) continue;
7263 for (i = 0; i < 5; i++) {
7264 dictEntry *de;
7265 double swappability;
7266
7267 if (maxtries) maxtries--;
7268 de = dictGetRandomKey(db->dict);
7269 key = dictGetEntryKey(de);
7270 val = dictGetEntryVal(de);
7271 if (key->storage != REDIS_VM_MEMORY) {
7272 if (maxtries) i--; /* don't count this try */
7273 continue;
7274 }
7275 swappability = computeObjectSwappability(val);
7276 if (!best || swappability > best_swappability) {
7277 best = de;
7278 best_swappability = swappability;
7279 best_db = db;
7280 }
7281 }
7282 }
7283 if (best == NULL) {
7284 redisLog(REDIS_DEBUG,"No swappable key found!");
7285 return REDIS_ERR;
7286 }
7287 key = dictGetEntryKey(best);
7288 val = dictGetEntryVal(best);
7289
7290 redisLog(REDIS_DEBUG,"Key with best swappability: %s, %f",
7291 key->ptr, best_swappability);
7292
7293 /* Unshare the key if needed */
7294 if (key->refcount > 1) {
7295 robj *newkey = dupStringObject(key);
7296 decrRefCount(key);
7297 key = dictGetEntryKey(best) = newkey;
7298 }
7299 /* Swap it */
7300 if (usethreads) {
7301 vmSwapObjectThreaded(key,val,best_db);
7302 return REDIS_OK;
7303 } else {
7304 if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
7305 dictGetEntryVal(best) = NULL;
7306 return REDIS_OK;
7307 } else {
7308 return REDIS_ERR;
7309 }
7310 }
7311 }
7312
7313 static int vmSwapOneObjectBlocking() {
7314 return vmSwapOneObject(0);
7315 }
7316
7317 static int vmSwapOneObjectThreaded() {
7318 return vmSwapOneObject(1);
7319 }
7320
7321 /* Return true if it's safe to swap out objects in a given moment.
7322 * Basically we don't want to swap objects out while there is a BGSAVE
7323 * or a BGAEOREWRITE running in backgroud. */
7324 static int vmCanSwapOut(void) {
7325 return (server.bgsavechildpid == -1 && server.bgrewritechildpid == -1);
7326 }
7327
7328 /* Delete a key if swapped. Returns 1 if the key was found, was swapped
7329 * and was deleted. Otherwise 0 is returned. */
7330 static int deleteIfSwapped(redisDb *db, robj *key) {
7331 dictEntry *de;
7332 robj *foundkey;
7333
7334 if ((de = dictFind(db->dict,key)) == NULL) return 0;
7335 foundkey = dictGetEntryKey(de);
7336 if (foundkey->storage == REDIS_VM_MEMORY) return 0;
7337 deleteKey(db,key);
7338 return 1;
7339 }
7340
7341 /* =================== Virtual Memory - Threaded I/O ======================= */
7342
7343 static void freeIOJob(iojob *j) {
7344 if (j->type == REDIS_IOJOB_PREPARE_SWAP ||
7345 j->type == REDIS_IOJOB_DO_SWAP)
7346 decrRefCount(j->val);
7347 decrRefCount(j->key);
7348 zfree(j);
7349 }
7350
7351 /* Every time a thread finished a Job, it writes a byte into the write side
7352 * of an unix pipe in order to "awake" the main thread, and this function
7353 * is called. */
7354 static void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata,
7355 int mask)
7356 {
7357 char buf[1];
7358 int retval;
7359 REDIS_NOTUSED(el);
7360 REDIS_NOTUSED(mask);
7361 REDIS_NOTUSED(privdata);
7362
7363 /* For every byte we read in the read side of the pipe, there is one
7364 * I/O job completed to process. */
7365 while((retval = read(fd,buf,1)) == 1) {
7366 iojob *j;
7367 listNode *ln;
7368 robj *key;
7369 struct dictEntry *de;
7370
7371 redisLog(REDIS_DEBUG,"Processing I/O completed job");
7372 assert(listLength(server.io_processed) != 0);
7373
7374 /* Get the processed element (the oldest one) */
7375 lockThreadedIO();
7376 ln = listFirst(server.io_processed);
7377 j = ln->value;
7378 listDelNode(server.io_processed,ln);
7379 unlockThreadedIO();
7380 /* If this job is marked as canceled, just ignore it */
7381 if (j->canceled) {
7382 freeIOJob(j);
7383 continue;
7384 }
7385 /* Post process it in the main thread, as there are things we
7386 * can do just here to avoid race conditions and/or invasive locks */
7387 de = dictFind(j->db->dict,j->key);
7388 assert(de != NULL);
7389 key = dictGetEntryKey(de);
7390 if (j->type == REDIS_IOJOB_LOAD) {
7391 /* Key loaded, bring it at home */
7392 key->storage = REDIS_VM_MEMORY;
7393 key->vm.atime = server.unixtime;
7394 vmMarkPagesFree(key->vm.page,key->vm.usedpages);
7395 redisLog(REDIS_DEBUG, "VM: object %s loaded from disk (threaded)",
7396 (unsigned char*) key->ptr);
7397 server.vm_stats_swapped_objects--;
7398 server.vm_stats_swapins++;
7399 freeIOJob(j);
7400 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
7401 /* Now we know the amount of pages required to swap this object.
7402 * Let's find some space for it, and queue this task again
7403 * rebranded as REDIS_IOJOB_DO_SWAP. */
7404 if (vmFindContiguousPages(&j->page,j->pages) == REDIS_ERR) {
7405 /* Ooops... no space! */
7406 freeIOJob(j);
7407 } else {
7408 j->type = REDIS_IOJOB_DO_SWAP;
7409 lockThreadedIO();
7410 queueIOJob(j);
7411 unlockThreadedIO();
7412 }
7413 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
7414 robj *val;
7415
7416 /* Key swapped. We can finally free some memory. */
7417 val = dictGetEntryVal(de);
7418 key->vm.page = j->page;
7419 key->vm.usedpages = j->pages;
7420 key->storage = REDIS_VM_SWAPPED;
7421 key->vtype = j->val->type;
7422 decrRefCount(val); /* Deallocate the object from memory. */
7423 vmMarkPagesUsed(j->page,j->pages);
7424 redisLog(REDIS_DEBUG,
7425 "VM: object %s swapped out at %lld (%lld pages) (threaded)",
7426 (unsigned char*) key->ptr,
7427 (unsigned long long) j->page, (unsigned long long) j->pages);
7428 server.vm_stats_swapped_objects++;
7429 server.vm_stats_swapouts++;
7430 freeIOJob(j);
7431 }
7432 }
7433 if (retval < 0 && errno != EAGAIN) {
7434 redisLog(REDIS_WARNING,
7435 "WARNING: read(2) error in vmThreadedIOCompletedJob() %s",
7436 strerror(errno));
7437 }
7438 }
7439
7440 static void lockThreadedIO(void) {
7441 pthread_mutex_lock(&server.io_mutex);
7442 }
7443
7444 static void unlockThreadedIO(void) {
7445 pthread_mutex_unlock(&server.io_mutex);
7446 }
7447
7448 /* Remove the specified object from the threaded I/O queue if still not
7449 * processed, otherwise make sure to flag it as canceled. */
7450 static void vmCancelThreadedIOJob(robj *o) {
7451 list *lists[3] = {
7452 server.io_newjobs, server.io_processing, server.io_processed
7453 };
7454 int i;
7455
7456 assert(o->storage == REDIS_VM_LOADING || o->storage == REDIS_VM_SWAPPING);
7457 lockThreadedIO();
7458 /* Search for a matching key in one of the queues */
7459 for (i = 0; i < 3; i++) {
7460 listNode *ln;
7461
7462 listRewind(lists[i]);
7463 while ((ln = listYield(lists[i])) != NULL) {
7464 iojob *job = ln->value;
7465
7466 if (compareStringObjects(job->key,o) == 0) {
7467 switch(i) {
7468 case 0: /* io_newjobs */
7469 /* If the job was not yet processed the best thing to do
7470 * is to remove it from the queue at all */
7471 decrRefCount(job->key);
7472 if (job->type == REDIS_IOJOB_PREPARE_SWAP ||
7473 job->type == REDIS_IOJOB_DO_SWAP)
7474 decrRefCount(job->val);
7475 listDelNode(lists[i],ln);
7476 zfree(job);
7477 break;
7478 case 1: /* io_processing */
7479 case 2: /* io_processed */
7480 job->canceled = 1;
7481 break;
7482 }
7483 if (o->storage == REDIS_VM_LOADING)
7484 o->storage = REDIS_VM_SWAPPED;
7485 else if (o->storage == REDIS_VM_SWAPPING)
7486 o->storage = REDIS_VM_MEMORY;
7487 unlockThreadedIO();
7488 return;
7489 }
7490 }
7491 }
7492 unlockThreadedIO();
7493 assert(1 != 1); /* We should never reach this */
7494 }
7495
7496 static void *IOThreadEntryPoint(void *arg) {
7497 iojob *j;
7498 listNode *ln;
7499 REDIS_NOTUSED(arg);
7500
7501 pthread_detach(pthread_self());
7502 while(1) {
7503 /* Get a new job to process */
7504 lockThreadedIO();
7505 if (listLength(server.io_newjobs) == 0) {
7506 /* No new jobs in queue, exit. */
7507 printf("Thread %lld exiting, nothing to do\n",
7508 (long long) pthread_self());
7509 server.io_active_threads--;
7510 unlockThreadedIO();
7511 return NULL;
7512 }
7513 ln = listFirst(server.io_newjobs);
7514 j = ln->value;
7515 listDelNode(server.io_newjobs,ln);
7516 /* Add the job in the processing queue */
7517 j->thread = pthread_self();
7518 listAddNodeTail(server.io_processing,j);
7519 ln = listLast(server.io_processing); /* We use ln later to remove it */
7520 unlockThreadedIO();
7521 printf("Thread %lld got a new job: %p about key '%s'\n",
7522 (long long) pthread_self(), (void*)j, (char*)j->key->ptr);
7523
7524 /* Process the Job */
7525 if (j->type == REDIS_IOJOB_LOAD) {
7526 } else if (j->type == REDIS_IOJOB_PREPARE_SWAP) {
7527 FILE *fp = fopen("/dev/null","w+");
7528 j->pages = rdbSavedObjectPages(j->val,fp);
7529 fclose(fp);
7530 } else if (j->type == REDIS_IOJOB_DO_SWAP) {
7531 }
7532
7533 /* Done: insert the job into the processed queue */
7534 printf("Thread %lld completed the job: %p\n",
7535 (long long) pthread_self(), (void*)j);
7536 lockThreadedIO();
7537 listDelNode(server.io_processing,ln);
7538 listAddNodeTail(server.io_processed,j);
7539 unlockThreadedIO();
7540
7541 /* Signal the main thread there is new stuff to process */
7542 assert(write(server.io_ready_pipe_write,"x",1) == 1);
7543 }
7544 return NULL; /* never reached */
7545 }
7546
7547 static void spawnIOThread(void) {
7548 pthread_t thread;
7549
7550 pthread_create(&thread,NULL,IOThreadEntryPoint,NULL);
7551 server.io_active_threads++;
7552 }
7553
7554 /* This function must be called while with threaded IO locked */
7555 static void queueIOJob(iojob *j) {
7556 listAddNodeTail(server.io_newjobs,j);
7557 if (server.io_active_threads < server.vm_max_threads)
7558 spawnIOThread();
7559 }
7560
7561 static int vmSwapObjectThreaded(robj *key, robj *val, redisDb *db) {
7562 iojob *j;
7563
7564 assert(key->storage == REDIS_VM_MEMORY);
7565 assert(key->refcount == 1);
7566
7567 j = zmalloc(sizeof(*j));
7568 j->type = REDIS_IOJOB_PREPARE_SWAP;
7569 j->db = db;
7570 j->key = dupStringObject(key);
7571 j->val = val;
7572 incrRefCount(val);
7573 j->canceled = 0;
7574 j->thread = (pthread_t) -1;
7575
7576 lockThreadedIO();
7577 queueIOJob(j);
7578 unlockThreadedIO();
7579 return REDIS_OK;
7580 }
7581
7582 /* ================================= Debugging ============================== */
7583
7584 static void debugCommand(redisClient *c) {
7585 if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
7586 *((char*)-1) = 'x';
7587 } else if (!strcasecmp(c->argv[1]->ptr,"reload")) {
7588 if (rdbSave(server.dbfilename) != REDIS_OK) {
7589 addReply(c,shared.err);
7590 return;
7591 }
7592 emptyDb();
7593 if (rdbLoad(server.dbfilename) != REDIS_OK) {
7594 addReply(c,shared.err);
7595 return;
7596 }
7597 redisLog(REDIS_WARNING,"DB reloaded by DEBUG RELOAD");
7598 addReply(c,shared.ok);
7599 } else if (!strcasecmp(c->argv[1]->ptr,"loadaof")) {
7600 emptyDb();
7601 if (loadAppendOnlyFile(server.appendfilename) != REDIS_OK) {
7602 addReply(c,shared.err);
7603 return;
7604 }
7605 redisLog(REDIS_WARNING,"Append Only File loaded by DEBUG LOADAOF");
7606 addReply(c,shared.ok);
7607 } else if (!strcasecmp(c->argv[1]->ptr,"object") && c->argc == 3) {
7608 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
7609 robj *key, *val;
7610
7611 if (!de) {
7612 addReply(c,shared.nokeyerr);
7613 return;
7614 }
7615 key = dictGetEntryKey(de);
7616 val = dictGetEntryVal(de);
7617 if (server.vm_enabled && (key->storage == REDIS_VM_MEMORY ||
7618 key->storage == REDIS_VM_SWAPPING)) {
7619 addReplySds(c,sdscatprintf(sdsempty(),
7620 "+Key at:%p refcount:%d, value at:%p refcount:%d "
7621 "encoding:%d serializedlength:%lld\r\n",
7622 (void*)key, key->refcount, (void*)val, val->refcount,
7623 val->encoding, rdbSavedObjectLen(val,NULL)));
7624 } else {
7625 addReplySds(c,sdscatprintf(sdsempty(),
7626 "+Key at:%p refcount:%d, value swapped at: page %llu "
7627 "using %llu pages\r\n",
7628 (void*)key, key->refcount, (unsigned long long) key->vm.page,
7629 (unsigned long long) key->vm.usedpages));
7630 }
7631 } else if (!strcasecmp(c->argv[1]->ptr,"swapout") && c->argc == 3) {
7632 dictEntry *de = dictFind(c->db->dict,c->argv[2]);
7633 robj *key, *val;
7634
7635 if (!server.vm_enabled) {
7636 addReplySds(c,sdsnew("-ERR Virtual Memory is disabled\r\n"));
7637 return;
7638 }
7639 if (!de) {
7640 addReply(c,shared.nokeyerr);
7641 return;
7642 }
7643 key = dictGetEntryKey(de);
7644 val = dictGetEntryVal(de);
7645 /* If the key is shared we want to create a copy */
7646 if (key->refcount > 1) {
7647 robj *newkey = dupStringObject(key);
7648 decrRefCount(key);
7649 key = dictGetEntryKey(de) = newkey;
7650 }
7651 /* Swap it */
7652 if (key->storage != REDIS_VM_MEMORY) {
7653 addReplySds(c,sdsnew("-ERR This key is not in memory\r\n"));
7654 } else if (vmSwapObjectBlocking(key,val) == REDIS_OK) {
7655 dictGetEntryVal(de) = NULL;
7656 addReply(c,shared.ok);
7657 } else {
7658 addReply(c,shared.err);
7659 }
7660 } else {
7661 addReplySds(c,sdsnew(
7662 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPOUT <key>|RELOAD]\r\n"));
7663 }
7664 }
7665
7666 static void _redisAssert(char *estr) {
7667 redisLog(REDIS_WARNING,"=== ASSERTION FAILED ===");
7668 redisLog(REDIS_WARNING,"==> %s\n",estr);
7669 #ifdef HAVE_BACKTRACE
7670 redisLog(REDIS_WARNING,"(forcing SIGSEGV in order to print the stack trace)");
7671 *((char*)-1) = 'x';
7672 #endif
7673 }
7674
7675 /* =================================== Main! ================================ */
7676
7677 #ifdef __linux__
7678 int linuxOvercommitMemoryValue(void) {
7679 FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
7680 char buf[64];
7681
7682 if (!fp) return -1;
7683 if (fgets(buf,64,fp) == NULL) {
7684 fclose(fp);
7685 return -1;
7686 }
7687 fclose(fp);
7688
7689 return atoi(buf);
7690 }
7691
7692 void linuxOvercommitMemoryWarning(void) {
7693 if (linuxOvercommitMemoryValue() == 0) {
7694 redisLog(REDIS_WARNING,"WARNING overcommit_memory is set to 0! Background save may fail under low condition memory. 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.");
7695 }
7696 }
7697 #endif /* __linux__ */
7698
7699 static void daemonize(void) {
7700 int fd;
7701 FILE *fp;
7702
7703 if (fork() != 0) exit(0); /* parent exits */
7704 printf("New pid: %d\n", getpid());
7705 setsid(); /* create a new session */
7706
7707 /* Every output goes to /dev/null. If Redis is daemonized but
7708 * the 'logfile' is set to 'stdout' in the configuration file
7709 * it will not log at all. */
7710 if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
7711 dup2(fd, STDIN_FILENO);
7712 dup2(fd, STDOUT_FILENO);
7713 dup2(fd, STDERR_FILENO);
7714 if (fd > STDERR_FILENO) close(fd);
7715 }
7716 /* Try to write the pid file */
7717 fp = fopen(server.pidfile,"w");
7718 if (fp) {
7719 fprintf(fp,"%d\n",getpid());
7720 fclose(fp);
7721 }
7722 }
7723
7724 int main(int argc, char **argv) {
7725 initServerConfig();
7726 if (argc == 2) {
7727 resetServerSaveParams();
7728 loadServerConfig(argv[1]);
7729 } else if (argc > 2) {
7730 fprintf(stderr,"Usage: ./redis-server [/path/to/redis.conf]\n");
7731 exit(1);
7732 } else {
7733 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'");
7734 }
7735 if (server.daemonize) daemonize();
7736 initServer();
7737 redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
7738 #ifdef __linux__
7739 linuxOvercommitMemoryWarning();
7740 #endif
7741 if (server.appendonly) {
7742 if (loadAppendOnlyFile(server.appendfilename) == REDIS_OK)
7743 redisLog(REDIS_NOTICE,"DB loaded from append only file");
7744 } else {
7745 if (rdbLoad(server.dbfilename) == REDIS_OK)
7746 redisLog(REDIS_NOTICE,"DB loaded from disk");
7747 }
7748 redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
7749 aeMain(server.el);
7750 aeDeleteEventLoop(server.el);
7751 return 0;
7752 }
7753
7754 /* ============================= Backtrace support ========================= */
7755
7756 #ifdef HAVE_BACKTRACE
7757 static char *findFuncName(void *pointer, unsigned long *offset);
7758
7759 static void *getMcontextEip(ucontext_t *uc) {
7760 #if defined(__FreeBSD__)
7761 return (void*) uc->uc_mcontext.mc_eip;
7762 #elif defined(__dietlibc__)
7763 return (void*) uc->uc_mcontext.eip;
7764 #elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
7765 #if __x86_64__
7766 return (void*) uc->uc_mcontext->__ss.__rip;
7767 #else
7768 return (void*) uc->uc_mcontext->__ss.__eip;
7769 #endif
7770 #elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
7771 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
7772 return (void*) uc->uc_mcontext->__ss.__rip;
7773 #else
7774 return (void*) uc->uc_mcontext->__ss.__eip;
7775 #endif
7776 #elif defined(__i386__) || defined(__X86_64__) || defined(__x86_64__)
7777 return (void*) uc->uc_mcontext.gregs[REG_EIP]; /* Linux 32/64 bit */
7778 #elif defined(__ia64__) /* Linux IA64 */
7779 return (void*) uc->uc_mcontext.sc_ip;
7780 #else
7781 return NULL;
7782 #endif
7783 }
7784
7785 static void segvHandler(int sig, siginfo_t *info, void *secret) {
7786 void *trace[100];
7787 char **messages = NULL;
7788 int i, trace_size = 0;
7789 unsigned long offset=0;
7790 ucontext_t *uc = (ucontext_t*) secret;
7791 sds infostring;
7792 REDIS_NOTUSED(info);
7793
7794 redisLog(REDIS_WARNING,
7795 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION, sig);
7796 infostring = genRedisInfoString();
7797 redisLog(REDIS_WARNING, "%s",infostring);
7798 /* It's not safe to sdsfree() the returned string under memory
7799 * corruption conditions. Let it leak as we are going to abort */
7800
7801 trace_size = backtrace(trace, 100);
7802 /* overwrite sigaction with caller's address */
7803 if (getMcontextEip(uc) != NULL) {
7804 trace[1] = getMcontextEip(uc);
7805 }
7806 messages = backtrace_symbols(trace, trace_size);
7807
7808 for (i=1; i<trace_size; ++i) {
7809 char *fn = findFuncName(trace[i], &offset), *p;
7810
7811 p = strchr(messages[i],'+');
7812 if (!fn || (p && ((unsigned long)strtol(p+1,NULL,10)) < offset)) {
7813 redisLog(REDIS_WARNING,"%s", messages[i]);
7814 } else {
7815 redisLog(REDIS_WARNING,"%d redis-server %p %s + %d", i, trace[i], fn, (unsigned int)offset);
7816 }
7817 }
7818 /* free(messages); Don't call free() with possibly corrupted memory. */
7819 exit(0);
7820 }
7821
7822 static void setupSigSegvAction(void) {
7823 struct sigaction act;
7824
7825 sigemptyset (&act.sa_mask);
7826 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
7827 * is used. Otherwise, sa_handler is used */
7828 act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
7829 act.sa_sigaction = segvHandler;
7830 sigaction (SIGSEGV, &act, NULL);
7831 sigaction (SIGBUS, &act, NULL);
7832 sigaction (SIGFPE, &act, NULL);
7833 sigaction (SIGILL, &act, NULL);
7834 sigaction (SIGBUS, &act, NULL);
7835 return;
7836 }
7837
7838 #include "staticsymbols.h"
7839 /* This function try to convert a pointer into a function name. It's used in
7840 * oreder to provide a backtrace under segmentation fault that's able to
7841 * display functions declared as static (otherwise the backtrace is useless). */
7842 static char *findFuncName(void *pointer, unsigned long *offset){
7843 int i, ret = -1;
7844 unsigned long off, minoff = 0;
7845
7846 /* Try to match against the Symbol with the smallest offset */
7847 for (i=0; symsTable[i].pointer; i++) {
7848 unsigned long lp = (unsigned long) pointer;
7849
7850 if (lp != (unsigned long)-1 && lp >= symsTable[i].pointer) {
7851 off=lp-symsTable[i].pointer;
7852 if (ret < 0 || off < minoff) {
7853 minoff=off;
7854 ret=i;
7855 }
7856 }
7857 }
7858 if (ret == -1) return NULL;
7859 *offset = minoff;
7860 return symsTable[ret].name;
7861 }
7862 #else /* HAVE_BACKTRACE */
7863 static void setupSigSegvAction(void) {
7864 }
7865 #endif /* HAVE_BACKTRACE */
7866
7867
7868
7869 /* The End */
7870
7871
7872