2 * Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
30 #define REDIS_VERSION "1.3.11"
45 #endif /* HAVE_BACKTRACE */
53 #include <arpa/inet.h>
57 #include <sys/resource.h>
65 #include "solarisfixes.h"
69 #include "ae.h" /* Event driven programming library */
70 #include "sds.h" /* Dynamic safe strings */
71 #include "anet.h" /* Networking the easy way */
72 #include "dict.h" /* Hash tables */
73 #include "adlist.h" /* Linked lists */
74 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
75 #include "lzf.h" /* LZF compression library */
76 #include "pqsort.h" /* Partial qsort for SORT+LIMIT */
77 #include "zipmap.h" /* Compact dictionary-alike data structure */
78 #include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
84 /* Static server configuration */
85 #define REDIS_SERVERPORT 6379 /* TCP port */
86 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
87 #define REDIS_IOBUF_LEN 1024
88 #define REDIS_LOADBUF_LEN 1024
89 #define REDIS_STATIC_ARGS 8
90 #define REDIS_DEFAULT_DBNUM 16
91 #define REDIS_CONFIGLINE_MAX 1024
92 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
93 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
94 #define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */
95 #define REDIS_MAX_WRITE_PER_EVENT (1024*64)
96 #define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
98 /* If more then REDIS_WRITEV_THRESHOLD write packets are pending use writev */
99 #define REDIS_WRITEV_THRESHOLD 3
100 /* Max number of iovecs used for each writev call */
101 #define REDIS_WRITEV_IOVEC_COUNT 256
103 /* Hash table parameters */
104 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
107 #define REDIS_CMD_BULK 1 /* Bulk write command */
108 #define REDIS_CMD_INLINE 2 /* Inline command */
109 /* REDIS_CMD_DENYOOM reserves a longer comment: all the commands marked with
110 this flags will return an error when the 'maxmemory' option is set in the
111 config file and the server is using more than maxmemory bytes of memory.
112 In short this commands are denied on low memory conditions. */
113 #define REDIS_CMD_DENYOOM 4
114 #define REDIS_CMD_FORCE_REPLICATION 8 /* Force replication even if dirty is 0 */
117 #define REDIS_STRING 0
123 /* Objects encoding. Some kind of objects like Strings and Hashes can be
124 * internally represented in multiple ways. The 'encoding' field of the object
125 * is set to one of this fields for this object. */
126 #define REDIS_ENCODING_RAW 0 /* Raw representation */
127 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
128 #define REDIS_ENCODING_ZIPMAP 2 /* Encoded as zipmap */
129 #define REDIS_ENCODING_HT 3 /* Encoded as an hash table */
131 static char* strencoding
[] = {
132 "raw", "int", "zipmap", "hashtable"
135 /* Object types only used for dumping to disk */
136 #define REDIS_EXPIRETIME 253
137 #define REDIS_SELECTDB 254
138 #define REDIS_EOF 255
140 /* Defines related to the dump file format. To store 32 bits lengths for short
141 * keys requires a lot of space, so we check the most significant 2 bits of
142 * the first byte to interpreter the length:
144 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
145 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
146 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
147 * 11|000000 this means: specially encoded object will follow. The six bits
148 * number specify the kind of object that follows.
149 * See the REDIS_RDB_ENC_* defines.
151 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
152 * values, will fit inside. */
153 #define REDIS_RDB_6BITLEN 0
154 #define REDIS_RDB_14BITLEN 1
155 #define REDIS_RDB_32BITLEN 2
156 #define REDIS_RDB_ENCVAL 3
157 #define REDIS_RDB_LENERR UINT_MAX
159 /* When a length of a string object stored on disk has the first two bits
160 * set, the remaining two bits specify a special encoding for the object
161 * accordingly to the following defines: */
162 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
163 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
164 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
165 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
167 /* Virtual memory object->where field. */
168 #define REDIS_VM_MEMORY 0 /* The object is on memory */
169 #define REDIS_VM_SWAPPED 1 /* The object is on disk */
170 #define REDIS_VM_SWAPPING 2 /* Redis is swapping this object on disk */
171 #define REDIS_VM_LOADING 3 /* Redis is loading this object from disk */
173 /* Virtual memory static configuration stuff.
174 * Check vmFindContiguousPages() to know more about this magic numbers. */
175 #define REDIS_VM_MAX_NEAR_PAGES 65536
176 #define REDIS_VM_MAX_RANDOM_JUMP 4096
177 #define REDIS_VM_MAX_THREADS 32
178 #define REDIS_THREAD_STACK_SIZE (1024*1024*4)
179 /* The following is the *percentage* of completed I/O jobs to process when the
180 * handelr is called. While Virtual Memory I/O operations are performed by
181 * threads, this operations must be processed by the main thread when completed
182 * in order to take effect. */
183 #define REDIS_MAX_COMPLETED_JOBS_PROCESSED 1
186 #define REDIS_SLAVE 1 /* This client is a slave server */
187 #define REDIS_MASTER 2 /* This client is a master server */
188 #define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */
189 #define REDIS_MULTI 8 /* This client is in a MULTI context */
190 #define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */
191 #define REDIS_IO_WAIT 32 /* The client is waiting for Virtual Memory I/O */
193 /* Slave replication state - slave side */
194 #define REDIS_REPL_NONE 0 /* No active replication */
195 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
196 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
198 /* Slave replication state - from the point of view of master
199 * Note that in SEND_BULK and ONLINE state the slave receives new updates
200 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
201 * to start the next background saving in order to send updates to it. */
202 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
203 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
204 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
205 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
207 /* List related stuff */
211 /* Sort operations */
212 #define REDIS_SORT_GET 0
213 #define REDIS_SORT_ASC 1
214 #define REDIS_SORT_DESC 2
215 #define REDIS_SORTKEY_MAX 1024
218 #define REDIS_DEBUG 0
219 #define REDIS_VERBOSE 1
220 #define REDIS_NOTICE 2
221 #define REDIS_WARNING 3
223 /* Anti-warning macro... */
224 #define REDIS_NOTUSED(V) ((void) V)
226 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
227 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
229 /* Append only defines */
230 #define APPENDFSYNC_NO 0
231 #define APPENDFSYNC_ALWAYS 1
232 #define APPENDFSYNC_EVERYSEC 2
234 /* Hashes related defaults */
235 #define REDIS_HASH_MAX_ZIPMAP_ENTRIES 64
236 #define REDIS_HASH_MAX_ZIPMAP_VALUE 512
238 /* We can print the stacktrace, so our assert is defined this way: */
239 #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
240 #define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
241 static void _redisAssert(char *estr
, char *file
, int line
);
242 static void _redisPanic(char *msg
, char *file
, int line
);
244 /*================================= Data types ============================== */
246 /* A redis object, that is a type able to hold a string / list / set */
248 /* The VM object structure */
249 struct redisObjectVM
{
250 off_t page
; /* the page at witch the object is stored on disk */
251 off_t usedpages
; /* number of pages used on disk */
252 time_t atime
; /* Last access time */
255 /* The actual Redis Object */
256 typedef struct redisObject
{
259 unsigned char encoding
;
260 unsigned char storage
; /* If this object is a key, where is the value?
261 * REDIS_VM_MEMORY, REDIS_VM_SWAPPED, ... */
262 unsigned char vtype
; /* If this object is a key, and value is swapped out,
263 * this is the type of the swapped out object. */
265 /* VM fields, this are only allocated if VM is active, otherwise the
266 * object allocation function will just allocate
267 * sizeof(redisObjct) minus sizeof(redisObjectVM), so using
268 * Redis without VM active will not have any overhead. */
269 struct redisObjectVM vm
;
272 /* Macro used to initalize a Redis object allocated on the stack.
273 * Note that this macro is taken near the structure definition to make sure
274 * we'll update it when the structure is changed, to avoid bugs like
275 * bug #85 introduced exactly in this way. */
276 #define initStaticStringObject(_var,_ptr) do { \
278 _var.type = REDIS_STRING; \
279 _var.encoding = REDIS_ENCODING_RAW; \
281 if (server.vm_enabled) _var.storage = REDIS_VM_MEMORY; \
284 typedef struct redisDb
{
285 dict
*dict
; /* The keyspace for this DB */
286 dict
*expires
; /* Timeout of keys with a timeout set */
287 dict
*blockingkeys
; /* Keys with clients waiting for data (BLPOP) */
288 dict
*io_keys
; /* Keys with clients waiting for VM I/O */
292 /* Client MULTI/EXEC state */
293 typedef struct multiCmd
{
296 struct redisCommand
*cmd
;
299 typedef struct multiState
{
300 multiCmd
*commands
; /* Array of MULTI commands */
301 int count
; /* Total number of MULTI commands */
304 /* With multiplexing we need to take per-clinet state.
305 * Clients are taken in a liked list. */
306 typedef struct redisClient
{
311 robj
**argv
, **mbargv
;
313 int bulklen
; /* bulk read len. -1 if not in bulk read mode */
314 int multibulk
; /* multi bulk command format active */
317 time_t lastinteraction
; /* time of the last interaction, used for timeout */
318 int flags
; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */
319 int slaveseldb
; /* slave selected db, if this client is a slave */
320 int authenticated
; /* when requirepass is non-NULL */
321 int replstate
; /* replication state if this is a slave */
322 int repldbfd
; /* replication DB file descriptor */
323 long repldboff
; /* replication DB file offset */
324 off_t repldbsize
; /* replication DB file size */
325 multiState mstate
; /* MULTI/EXEC state */
326 robj
**blockingkeys
; /* The key we are waiting to terminate a blocking
327 * operation such as BLPOP. Otherwise NULL. */
328 int blockingkeysnum
; /* Number of blocking keys */
329 time_t blockingto
; /* Blocking operation timeout. If UNIX current time
330 * is >= blockingto then the operation timed out. */
331 list
*io_keys
; /* Keys this client is waiting to be loaded from the
332 * swap file in order to continue. */
333 dict
*pubsub_channels
; /* channels a client is interested in (SUBSCRIBE) */
334 list
*pubsub_patterns
; /* patterns a client is interested in (SUBSCRIBE) */
342 /* Global server state structure */
347 long long dirty
; /* changes to DB from the last save */
349 list
*slaves
, *monitors
;
350 char neterr
[ANET_ERR_LEN
];
352 int cronloops
; /* number of times the cron function run */
353 list
*objfreelist
; /* A list of freed objects to avoid malloc() */
354 time_t lastsave
; /* Unix time of last save succeeede */
355 /* Fields used only for stats */
356 time_t stat_starttime
; /* server start time */
357 long long stat_numcommands
; /* number of processed commands */
358 long long stat_numconnections
; /* number of connections received */
359 long long stat_expiredkeys
; /* number of expired keys */
372 pid_t bgsavechildpid
;
373 pid_t bgrewritechildpid
;
374 sds bgrewritebuf
; /* buffer taken by parent during oppend only rewrite */
375 sds aofbuf
; /* AOF buffer, written before entering the event loop */
376 struct saveparam
*saveparams
;
381 char *appendfilename
;
385 /* Replication related */
390 redisClient
*master
; /* client that is master for this slave */
392 unsigned int maxclients
;
393 unsigned long long maxmemory
;
394 unsigned int blpop_blocked_clients
;
395 unsigned int vm_blocked_clients
;
396 /* Sort parameters - qsort_r() is only available under BSD so we
397 * have to take this state global, in order to pass it to sortCompare() */
401 /* Virtual memory configuration */
406 unsigned long long vm_max_memory
;
408 size_t hash_max_zipmap_entries
;
409 size_t hash_max_zipmap_value
;
410 /* Virtual memory state */
413 off_t vm_next_page
; /* Next probably empty page */
414 off_t vm_near_pages
; /* Number of pages allocated sequentially */
415 unsigned char *vm_bitmap
; /* Bitmap of free/used pages */
416 time_t unixtime
; /* Unix time sampled every second. */
417 /* Virtual memory I/O threads stuff */
418 /* An I/O thread process an element taken from the io_jobs queue and
419 * put the result of the operation in the io_done list. While the
420 * job is being processed, it's put on io_processing queue. */
421 list
*io_newjobs
; /* List of VM I/O jobs yet to be processed */
422 list
*io_processing
; /* List of VM I/O jobs being processed */
423 list
*io_processed
; /* List of VM I/O jobs already processed */
424 list
*io_ready_clients
; /* Clients ready to be unblocked. All keys loaded */
425 pthread_mutex_t io_mutex
; /* lock to access io_jobs/io_done/io_thread_job */
426 pthread_mutex_t obj_freelist_mutex
; /* safe redis objects creation/free */
427 pthread_mutex_t io_swapfile_mutex
; /* So we can lseek + write */
428 pthread_attr_t io_threads_attr
; /* attributes for threads creation */
429 int io_active_threads
; /* Number of running I/O threads */
430 int vm_max_threads
; /* Max number of I/O threads running at the same time */
431 /* Our main thread is blocked on the event loop, locking for sockets ready
432 * to be read or written, so when a threaded I/O operation is ready to be
433 * processed by the main thread, the I/O thread will use a unix pipe to
434 * awake the main thread. The followings are the two pipe FDs. */
435 int io_ready_pipe_read
;
436 int io_ready_pipe_write
;
437 /* Virtual memory stats */
438 unsigned long long vm_stats_used_pages
;
439 unsigned long long vm_stats_swapped_objects
;
440 unsigned long long vm_stats_swapouts
;
441 unsigned long long vm_stats_swapins
;
443 dict
*pubsub_channels
; /* Map channels to list of subscribed clients */
444 list
*pubsub_patterns
; /* A list of pubsub_patterns */
449 typedef struct pubsubPattern
{
454 typedef void redisCommandProc(redisClient
*c
);
455 typedef void redisVmPreloadProc(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
);
456 struct redisCommand
{
458 redisCommandProc
*proc
;
461 /* Use a function to determine which keys need to be loaded
462 * in the background prior to executing this command. Takes precedence
463 * over vm_firstkey and others, ignored when NULL */
464 redisVmPreloadProc
*vm_preload_proc
;
465 /* What keys should be loaded in background when calling this command? */
466 int vm_firstkey
; /* The first argument that's a key (0 = no keys) */
467 int vm_lastkey
; /* THe last argument that's a key */
468 int vm_keystep
; /* The step between first and last key */
471 struct redisFunctionSym
{
473 unsigned long pointer
;
476 typedef struct _redisSortObject
{
484 typedef struct _redisSortOperation
{
487 } redisSortOperation
;
489 /* ZSETs use a specialized version of Skiplists */
491 typedef struct zskiplistNode
{
492 struct zskiplistNode
**forward
;
493 struct zskiplistNode
*backward
;
499 typedef struct zskiplist
{
500 struct zskiplistNode
*header
, *tail
;
501 unsigned long length
;
505 typedef struct zset
{
510 /* Our shared "common" objects */
512 #define REDIS_SHARED_INTEGERS 10000
513 struct sharedObjectsStruct
{
514 robj
*crlf
, *ok
, *err
, *emptybulk
, *czero
, *cone
, *pong
, *space
,
515 *colon
, *nullbulk
, *nullmultibulk
, *queued
,
516 *emptymultibulk
, *wrongtypeerr
, *nokeyerr
, *syntaxerr
, *sameobjecterr
,
517 *outofrangeerr
, *plus
,
518 *select0
, *select1
, *select2
, *select3
, *select4
,
519 *select5
, *select6
, *select7
, *select8
, *select9
,
520 *messagebulk
, *pmessagebulk
, *subscribebulk
, *unsubscribebulk
, *mbulk3
,
521 *mbulk4
, *psubscribebulk
, *punsubscribebulk
,
522 *integers
[REDIS_SHARED_INTEGERS
];
525 /* Global vars that are actally used as constants. The following double
526 * values are used for double on-disk serialization, and are initialized
527 * at runtime to avoid strange compiler optimizations. */
529 static double R_Zero
, R_PosInf
, R_NegInf
, R_Nan
;
531 /* VM threaded I/O request message */
532 #define REDIS_IOJOB_LOAD 0 /* Load from disk to memory */
533 #define REDIS_IOJOB_PREPARE_SWAP 1 /* Compute needed pages */
534 #define REDIS_IOJOB_DO_SWAP 2 /* Swap from memory to disk */
535 typedef struct iojob
{
536 int type
; /* Request type, REDIS_IOJOB_* */
537 redisDb
*db
;/* Redis database */
538 robj
*key
; /* This I/O request is about swapping this key */
539 robj
*val
; /* the value to swap for REDIS_IOREQ_*_SWAP, otherwise this
540 * field is populated by the I/O thread for REDIS_IOREQ_LOAD. */
541 off_t page
; /* Swap page where to read/write the object */
542 off_t pages
; /* Swap pages needed to save object. PREPARE_SWAP return val */
543 int canceled
; /* True if this command was canceled by blocking side of VM */
544 pthread_t thread
; /* ID of the thread processing this entry */
547 /*================================ Prototypes =============================== */
549 static void freeStringObject(robj
*o
);
550 static void freeListObject(robj
*o
);
551 static void freeSetObject(robj
*o
);
552 static void decrRefCount(void *o
);
553 static robj
*createObject(int type
, void *ptr
);
554 static void freeClient(redisClient
*c
);
555 static int rdbLoad(char *filename
);
556 static void addReply(redisClient
*c
, robj
*obj
);
557 static void addReplySds(redisClient
*c
, sds s
);
558 static void incrRefCount(robj
*o
);
559 static int rdbSaveBackground(char *filename
);
560 static robj
*createStringObject(char *ptr
, size_t len
);
561 static robj
*dupStringObject(robj
*o
);
562 static void replicationFeedSlaves(list
*slaves
, int dictid
, robj
**argv
, int argc
);
563 static void replicationFeedMonitors(list
*monitors
, int dictid
, robj
**argv
, int argc
);
564 static void flushAppendOnlyFile(void);
565 static void feedAppendOnlyFile(struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
);
566 static int syncWithMaster(void);
567 static robj
*tryObjectEncoding(robj
*o
);
568 static robj
*getDecodedObject(robj
*o
);
569 static int removeExpire(redisDb
*db
, robj
*key
);
570 static int expireIfNeeded(redisDb
*db
, robj
*key
);
571 static int deleteIfVolatile(redisDb
*db
, robj
*key
);
572 static int deleteIfSwapped(redisDb
*db
, robj
*key
);
573 static int deleteKey(redisDb
*db
, robj
*key
);
574 static time_t getExpire(redisDb
*db
, robj
*key
);
575 static int setExpire(redisDb
*db
, robj
*key
, time_t when
);
576 static void updateSlavesWaitingBgsave(int bgsaveerr
);
577 static void freeMemoryIfNeeded(void);
578 static int processCommand(redisClient
*c
);
579 static void setupSigSegvAction(void);
580 static void rdbRemoveTempFile(pid_t childpid
);
581 static void aofRemoveTempFile(pid_t childpid
);
582 static size_t stringObjectLen(robj
*o
);
583 static void processInputBuffer(redisClient
*c
);
584 static zskiplist
*zslCreate(void);
585 static void zslFree(zskiplist
*zsl
);
586 static void zslInsert(zskiplist
*zsl
, double score
, robj
*obj
);
587 static void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
588 static void initClientMultiState(redisClient
*c
);
589 static void freeClientMultiState(redisClient
*c
);
590 static void queueMultiCommand(redisClient
*c
, struct redisCommand
*cmd
);
591 static void unblockClientWaitingData(redisClient
*c
);
592 static int handleClientsWaitingListPush(redisClient
*c
, robj
*key
, robj
*ele
);
593 static void vmInit(void);
594 static void vmMarkPagesFree(off_t page
, off_t count
);
595 static robj
*vmLoadObject(robj
*key
);
596 static robj
*vmPreviewObject(robj
*key
);
597 static int vmSwapOneObjectBlocking(void);
598 static int vmSwapOneObjectThreaded(void);
599 static int vmCanSwapOut(void);
600 static int tryFreeOneObjectFromFreelist(void);
601 static void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
602 static void vmThreadedIOCompletedJob(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
603 static void vmCancelThreadedIOJob(robj
*o
);
604 static void lockThreadedIO(void);
605 static void unlockThreadedIO(void);
606 static int vmSwapObjectThreaded(robj
*key
, robj
*val
, redisDb
*db
);
607 static void freeIOJob(iojob
*j
);
608 static void queueIOJob(iojob
*j
);
609 static int vmWriteObjectOnSwap(robj
*o
, off_t page
);
610 static robj
*vmReadObjectFromSwap(off_t page
, int type
);
611 static void waitEmptyIOJobsQueue(void);
612 static void vmReopenSwapFile(void);
613 static int vmFreePage(off_t page
);
614 static void zunionInterBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
);
615 static void execBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
);
616 static int blockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
);
617 static int dontWaitForSwappedKey(redisClient
*c
, robj
*key
);
618 static void handleClientsBlockedOnSwappedKey(redisDb
*db
, robj
*key
);
619 static void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
620 static struct redisCommand
*lookupCommand(char *name
);
621 static void call(redisClient
*c
, struct redisCommand
*cmd
);
622 static void resetClient(redisClient
*c
);
623 static void convertToRealHash(robj
*o
);
624 static int pubsubUnsubscribeAllChannels(redisClient
*c
, int notify
);
625 static int pubsubUnsubscribeAllPatterns(redisClient
*c
, int notify
);
626 static void freePubsubPattern(void *p
);
627 static int listMatchPubsubPattern(void *a
, void *b
);
628 static int compareStringObjects(robj
*a
, robj
*b
);
629 static int equalStringObjects(robj
*a
, robj
*b
);
631 static int rewriteAppendOnlyFileBackground(void);
632 static int vmSwapObjectBlocking(robj
*key
, robj
*val
);
634 static void authCommand(redisClient
*c
);
635 static void pingCommand(redisClient
*c
);
636 static void echoCommand(redisClient
*c
);
637 static void setCommand(redisClient
*c
);
638 static void setnxCommand(redisClient
*c
);
639 static void setexCommand(redisClient
*c
);
640 static void getCommand(redisClient
*c
);
641 static void delCommand(redisClient
*c
);
642 static void existsCommand(redisClient
*c
);
643 static void incrCommand(redisClient
*c
);
644 static void decrCommand(redisClient
*c
);
645 static void incrbyCommand(redisClient
*c
);
646 static void decrbyCommand(redisClient
*c
);
647 static void selectCommand(redisClient
*c
);
648 static void randomkeyCommand(redisClient
*c
);
649 static void keysCommand(redisClient
*c
);
650 static void dbsizeCommand(redisClient
*c
);
651 static void lastsaveCommand(redisClient
*c
);
652 static void saveCommand(redisClient
*c
);
653 static void bgsaveCommand(redisClient
*c
);
654 static void bgrewriteaofCommand(redisClient
*c
);
655 static void shutdownCommand(redisClient
*c
);
656 static void moveCommand(redisClient
*c
);
657 static void renameCommand(redisClient
*c
);
658 static void renamenxCommand(redisClient
*c
);
659 static void lpushCommand(redisClient
*c
);
660 static void rpushCommand(redisClient
*c
);
661 static void lpopCommand(redisClient
*c
);
662 static void rpopCommand(redisClient
*c
);
663 static void llenCommand(redisClient
*c
);
664 static void lindexCommand(redisClient
*c
);
665 static void lrangeCommand(redisClient
*c
);
666 static void ltrimCommand(redisClient
*c
);
667 static void typeCommand(redisClient
*c
);
668 static void lsetCommand(redisClient
*c
);
669 static void saddCommand(redisClient
*c
);
670 static void sremCommand(redisClient
*c
);
671 static void smoveCommand(redisClient
*c
);
672 static void sismemberCommand(redisClient
*c
);
673 static void scardCommand(redisClient
*c
);
674 static void spopCommand(redisClient
*c
);
675 static void srandmemberCommand(redisClient
*c
);
676 static void sinterCommand(redisClient
*c
);
677 static void sinterstoreCommand(redisClient
*c
);
678 static void sunionCommand(redisClient
*c
);
679 static void sunionstoreCommand(redisClient
*c
);
680 static void sdiffCommand(redisClient
*c
);
681 static void sdiffstoreCommand(redisClient
*c
);
682 static void syncCommand(redisClient
*c
);
683 static void flushdbCommand(redisClient
*c
);
684 static void flushallCommand(redisClient
*c
);
685 static void sortCommand(redisClient
*c
);
686 static void lremCommand(redisClient
*c
);
687 static void rpoplpushcommand(redisClient
*c
);
688 static void infoCommand(redisClient
*c
);
689 static void mgetCommand(redisClient
*c
);
690 static void monitorCommand(redisClient
*c
);
691 static void expireCommand(redisClient
*c
);
692 static void expireatCommand(redisClient
*c
);
693 static void getsetCommand(redisClient
*c
);
694 static void ttlCommand(redisClient
*c
);
695 static void slaveofCommand(redisClient
*c
);
696 static void debugCommand(redisClient
*c
);
697 static void msetCommand(redisClient
*c
);
698 static void msetnxCommand(redisClient
*c
);
699 static void zaddCommand(redisClient
*c
);
700 static void zincrbyCommand(redisClient
*c
);
701 static void zrangeCommand(redisClient
*c
);
702 static void zrangebyscoreCommand(redisClient
*c
);
703 static void zcountCommand(redisClient
*c
);
704 static void zrevrangeCommand(redisClient
*c
);
705 static void zcardCommand(redisClient
*c
);
706 static void zremCommand(redisClient
*c
);
707 static void zscoreCommand(redisClient
*c
);
708 static void zremrangebyscoreCommand(redisClient
*c
);
709 static void multiCommand(redisClient
*c
);
710 static void execCommand(redisClient
*c
);
711 static void discardCommand(redisClient
*c
);
712 static void blpopCommand(redisClient
*c
);
713 static void brpopCommand(redisClient
*c
);
714 static void appendCommand(redisClient
*c
);
715 static void substrCommand(redisClient
*c
);
716 static void zrankCommand(redisClient
*c
);
717 static void zrevrankCommand(redisClient
*c
);
718 static void hsetCommand(redisClient
*c
);
719 static void hsetnxCommand(redisClient
*c
);
720 static void hgetCommand(redisClient
*c
);
721 static void hmsetCommand(redisClient
*c
);
722 static void hmgetCommand(redisClient
*c
);
723 static void hdelCommand(redisClient
*c
);
724 static void hlenCommand(redisClient
*c
);
725 static void zremrangebyrankCommand(redisClient
*c
);
726 static void zunionstoreCommand(redisClient
*c
);
727 static void zinterstoreCommand(redisClient
*c
);
728 static void hkeysCommand(redisClient
*c
);
729 static void hvalsCommand(redisClient
*c
);
730 static void hgetallCommand(redisClient
*c
);
731 static void hexistsCommand(redisClient
*c
);
732 static void configCommand(redisClient
*c
);
733 static void hincrbyCommand(redisClient
*c
);
734 static void subscribeCommand(redisClient
*c
);
735 static void unsubscribeCommand(redisClient
*c
);
736 static void psubscribeCommand(redisClient
*c
);
737 static void punsubscribeCommand(redisClient
*c
);
738 static void publishCommand(redisClient
*c
);
740 /*================================= Globals ================================= */
743 static struct redisServer server
; /* server global state */
744 static struct redisCommand cmdTable
[] = {
745 {"get",getCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
746 {"set",setCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,0,0,0},
747 {"setnx",setnxCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,0,0,0},
748 {"setex",setexCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,0,0,0},
749 {"append",appendCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
750 {"substr",substrCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
751 {"del",delCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
752 {"exists",existsCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
753 {"incr",incrCommand
,2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
754 {"decr",decrCommand
,2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
755 {"mget",mgetCommand
,-2,REDIS_CMD_INLINE
,NULL
,1,-1,1},
756 {"rpush",rpushCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
757 {"lpush",lpushCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
758 {"rpop",rpopCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
759 {"lpop",lpopCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
760 {"brpop",brpopCommand
,-3,REDIS_CMD_INLINE
,NULL
,1,1,1},
761 {"blpop",blpopCommand
,-3,REDIS_CMD_INLINE
,NULL
,1,1,1},
762 {"llen",llenCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
763 {"lindex",lindexCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
764 {"lset",lsetCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
765 {"lrange",lrangeCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
766 {"ltrim",ltrimCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
767 {"lrem",lremCommand
,4,REDIS_CMD_BULK
,NULL
,1,1,1},
768 {"rpoplpush",rpoplpushcommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,2,1},
769 {"sadd",saddCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
770 {"srem",sremCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
771 {"smove",smoveCommand
,4,REDIS_CMD_BULK
,NULL
,1,2,1},
772 {"sismember",sismemberCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
773 {"scard",scardCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
774 {"spop",spopCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
775 {"srandmember",srandmemberCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
776 {"sinter",sinterCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,-1,1},
777 {"sinterstore",sinterstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,2,-1,1},
778 {"sunion",sunionCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,-1,1},
779 {"sunionstore",sunionstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,2,-1,1},
780 {"sdiff",sdiffCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,-1,1},
781 {"sdiffstore",sdiffstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,2,-1,1},
782 {"smembers",sinterCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
783 {"zadd",zaddCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
784 {"zincrby",zincrbyCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
785 {"zrem",zremCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
786 {"zremrangebyscore",zremrangebyscoreCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
787 {"zremrangebyrank",zremrangebyrankCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
788 {"zunionstore",zunionstoreCommand
,-4,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,zunionInterBlockClientOnSwappedKeys
,0,0,0},
789 {"zinterstore",zinterstoreCommand
,-4,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,zunionInterBlockClientOnSwappedKeys
,0,0,0},
790 {"zrange",zrangeCommand
,-4,REDIS_CMD_INLINE
,NULL
,1,1,1},
791 {"zrangebyscore",zrangebyscoreCommand
,-4,REDIS_CMD_INLINE
,NULL
,1,1,1},
792 {"zcount",zcountCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
793 {"zrevrange",zrevrangeCommand
,-4,REDIS_CMD_INLINE
,NULL
,1,1,1},
794 {"zcard",zcardCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
795 {"zscore",zscoreCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
796 {"zrank",zrankCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
797 {"zrevrank",zrevrankCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
798 {"hset",hsetCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
799 {"hsetnx",hsetnxCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
800 {"hget",hgetCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
801 {"hmset",hmsetCommand
,-4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
802 {"hmget",hmgetCommand
,-3,REDIS_CMD_BULK
,NULL
,1,1,1},
803 {"hincrby",hincrbyCommand
,4,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
804 {"hdel",hdelCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
805 {"hlen",hlenCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
806 {"hkeys",hkeysCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
807 {"hvals",hvalsCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
808 {"hgetall",hgetallCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
809 {"hexists",hexistsCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
810 {"incrby",incrbyCommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
811 {"decrby",decrbyCommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
812 {"getset",getsetCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
813 {"mset",msetCommand
,-3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,-1,2},
814 {"msetnx",msetnxCommand
,-3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,-1,2},
815 {"randomkey",randomkeyCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
816 {"select",selectCommand
,2,REDIS_CMD_INLINE
,NULL
,0,0,0},
817 {"move",moveCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
818 {"rename",renameCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
819 {"renamenx",renamenxCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
820 {"expire",expireCommand
,3,REDIS_CMD_INLINE
,NULL
,0,0,0},
821 {"expireat",expireatCommand
,3,REDIS_CMD_INLINE
,NULL
,0,0,0},
822 {"keys",keysCommand
,2,REDIS_CMD_INLINE
,NULL
,0,0,0},
823 {"dbsize",dbsizeCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
824 {"auth",authCommand
,2,REDIS_CMD_INLINE
,NULL
,0,0,0},
825 {"ping",pingCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
826 {"echo",echoCommand
,2,REDIS_CMD_BULK
,NULL
,0,0,0},
827 {"save",saveCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
828 {"bgsave",bgsaveCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
829 {"bgrewriteaof",bgrewriteaofCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
830 {"shutdown",shutdownCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
831 {"lastsave",lastsaveCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
832 {"type",typeCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
833 {"multi",multiCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
834 {"exec",execCommand
,1,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,execBlockClientOnSwappedKeys
,0,0,0},
835 {"discard",discardCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
836 {"sync",syncCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
837 {"flushdb",flushdbCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
838 {"flushall",flushallCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
839 {"sort",sortCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
840 {"info",infoCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
841 {"monitor",monitorCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
842 {"ttl",ttlCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
843 {"slaveof",slaveofCommand
,3,REDIS_CMD_INLINE
,NULL
,0,0,0},
844 {"debug",debugCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
845 {"config",configCommand
,-2,REDIS_CMD_BULK
,NULL
,0,0,0},
846 {"subscribe",subscribeCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
847 {"unsubscribe",unsubscribeCommand
,-1,REDIS_CMD_INLINE
,NULL
,0,0,0},
848 {"psubscribe",psubscribeCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
849 {"punsubscribe",punsubscribeCommand
,-1,REDIS_CMD_INLINE
,NULL
,0,0,0},
850 {"publish",publishCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_FORCE_REPLICATION
,NULL
,0,0,0},
851 {NULL
,NULL
,0,0,NULL
,0,0,0}
854 /*============================ Utility functions ============================ */
856 /* Glob-style pattern matching. */
857 static int stringmatchlen(const char *pattern
, int patternLen
,
858 const char *string
, int stringLen
, int nocase
)
863 while (pattern
[1] == '*') {
868 return 1; /* match */
870 if (stringmatchlen(pattern
+1, patternLen
-1,
871 string
, stringLen
, nocase
))
872 return 1; /* match */
876 return 0; /* no match */
880 return 0; /* no match */
890 not = pattern
[0] == '^';
897 if (pattern
[0] == '\\') {
900 if (pattern
[0] == string
[0])
902 } else if (pattern
[0] == ']') {
904 } else if (patternLen
== 0) {
908 } else if (pattern
[1] == '-' && patternLen
>= 3) {
909 int start
= pattern
[0];
910 int end
= pattern
[2];
918 start
= tolower(start
);
924 if (c
>= start
&& c
<= end
)
928 if (pattern
[0] == string
[0])
931 if (tolower((int)pattern
[0]) == tolower((int)string
[0]))
941 return 0; /* no match */
947 if (patternLen
>= 2) {
954 if (pattern
[0] != string
[0])
955 return 0; /* no match */
957 if (tolower((int)pattern
[0]) != tolower((int)string
[0]))
958 return 0; /* no match */
966 if (stringLen
== 0) {
967 while(*pattern
== '*') {
974 if (patternLen
== 0 && stringLen
== 0)
979 static int stringmatch(const char *pattern
, const char *string
, int nocase
) {
980 return stringmatchlen(pattern
,strlen(pattern
),string
,strlen(string
),nocase
);
983 /* Convert a string representing an amount of memory into the number of
984 * bytes, so for instance memtoll("1Gi") will return 1073741824 that is
987 * On parsing error, if *err is not NULL, it's set to 1, otherwise it's
989 static long long memtoll(const char *p
, int *err
) {
992 long mul
; /* unit multiplier */
997 /* Search the first non digit character. */
1000 while(*u
&& isdigit(*u
)) u
++;
1001 if (*u
== '\0' || !strcasecmp(u
,"b")) {
1003 } else if (!strcasecmp(u
,"k")) {
1005 } else if (!strcasecmp(u
,"kb")) {
1007 } else if (!strcasecmp(u
,"m")) {
1009 } else if (!strcasecmp(u
,"mb")) {
1011 } else if (!strcasecmp(u
,"g")) {
1012 mul
= 1000L*1000*1000;
1013 } else if (!strcasecmp(u
,"gb")) {
1014 mul
= 1024L*1024*1024;
1020 if (digits
>= sizeof(buf
)) {
1024 memcpy(buf
,p
,digits
);
1026 val
= strtoll(buf
,NULL
,10);
1030 /* Convert a long long into a string. Returns the number of
1031 * characters needed to represent the number, that can be shorter if passed
1032 * buffer length is not enough to store the whole number. */
1033 static int ll2string(char *s
, size_t len
, long long value
) {
1035 unsigned long long v
;
1038 if (len
== 0) return 0;
1039 v
= (value
< 0) ? -value
: value
;
1040 p
= buf
+31; /* point to the last character */
1045 if (value
< 0) *p
-- = '-';
1048 if (l
+1 > len
) l
= len
-1; /* Make sure it fits, including the nul term */
1054 static void redisLog(int level
, const char *fmt
, ...) {
1058 fp
= (server
.logfile
== NULL
) ? stdout
: fopen(server
.logfile
,"a");
1062 if (level
>= server
.verbosity
) {
1068 strftime(buf
,64,"%d %b %H:%M:%S",localtime(&now
));
1069 fprintf(fp
,"[%d] %s %c ",(int)getpid(),buf
,c
[level
]);
1070 vfprintf(fp
, fmt
, ap
);
1076 if (server
.logfile
) fclose(fp
);
1079 /*====================== Hash table type implementation ==================== */
1081 /* This is an hash table type that uses the SDS dynamic strings libary as
1082 * keys and radis objects as values (objects can hold SDS strings,
1085 static void dictVanillaFree(void *privdata
, void *val
)
1087 DICT_NOTUSED(privdata
);
1091 static void dictListDestructor(void *privdata
, void *val
)
1093 DICT_NOTUSED(privdata
);
1094 listRelease((list
*)val
);
1097 static int sdsDictKeyCompare(void *privdata
, const void *key1
,
1101 DICT_NOTUSED(privdata
);
1103 l1
= sdslen((sds
)key1
);
1104 l2
= sdslen((sds
)key2
);
1105 if (l1
!= l2
) return 0;
1106 return memcmp(key1
, key2
, l1
) == 0;
1109 static void dictRedisObjectDestructor(void *privdata
, void *val
)
1111 DICT_NOTUSED(privdata
);
1113 if (val
== NULL
) return; /* Values of swapped out keys as set to NULL */
1117 static int dictObjKeyCompare(void *privdata
, const void *key1
,
1120 const robj
*o1
= key1
, *o2
= key2
;
1121 return sdsDictKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
1124 static unsigned int dictObjHash(const void *key
) {
1125 const robj
*o
= key
;
1126 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
1129 static int dictEncObjKeyCompare(void *privdata
, const void *key1
,
1132 robj
*o1
= (robj
*) key1
, *o2
= (robj
*) key2
;
1135 if (o1
->encoding
== REDIS_ENCODING_INT
&&
1136 o2
->encoding
== REDIS_ENCODING_INT
)
1137 return o1
->ptr
== o2
->ptr
;
1139 o1
= getDecodedObject(o1
);
1140 o2
= getDecodedObject(o2
);
1141 cmp
= sdsDictKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
1147 static unsigned int dictEncObjHash(const void *key
) {
1148 robj
*o
= (robj
*) key
;
1150 if (o
->encoding
== REDIS_ENCODING_RAW
) {
1151 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
1153 if (o
->encoding
== REDIS_ENCODING_INT
) {
1157 len
= ll2string(buf
,32,(long)o
->ptr
);
1158 return dictGenHashFunction((unsigned char*)buf
, len
);
1162 o
= getDecodedObject(o
);
1163 hash
= dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
1170 /* Sets type and expires */
1171 static dictType setDictType
= {
1172 dictEncObjHash
, /* hash function */
1175 dictEncObjKeyCompare
, /* key compare */
1176 dictRedisObjectDestructor
, /* key destructor */
1177 NULL
/* val destructor */
1180 /* Sorted sets hash (note: a skiplist is used in addition to the hash table) */
1181 static dictType zsetDictType
= {
1182 dictEncObjHash
, /* hash function */
1185 dictEncObjKeyCompare
, /* key compare */
1186 dictRedisObjectDestructor
, /* key destructor */
1187 dictVanillaFree
/* val destructor of malloc(sizeof(double)) */
1191 static dictType dbDictType
= {
1192 dictObjHash
, /* hash function */
1195 dictObjKeyCompare
, /* key compare */
1196 dictRedisObjectDestructor
, /* key destructor */
1197 dictRedisObjectDestructor
/* val destructor */
1201 static dictType keyptrDictType
= {
1202 dictObjHash
, /* hash function */
1205 dictObjKeyCompare
, /* key compare */
1206 dictRedisObjectDestructor
, /* key destructor */
1207 NULL
/* val destructor */
1210 /* Hash type hash table (note that small hashes are represented with zimpaps) */
1211 static dictType hashDictType
= {
1212 dictEncObjHash
, /* hash function */
1215 dictEncObjKeyCompare
, /* key compare */
1216 dictRedisObjectDestructor
, /* key destructor */
1217 dictRedisObjectDestructor
/* val destructor */
1220 /* Keylist hash table type has unencoded redis objects as keys and
1221 * lists as values. It's used for blocking operations (BLPOP) and to
1222 * map swapped keys to a list of clients waiting for this keys to be loaded. */
1223 static dictType keylistDictType
= {
1224 dictObjHash
, /* hash function */
1227 dictObjKeyCompare
, /* key compare */
1228 dictRedisObjectDestructor
, /* key destructor */
1229 dictListDestructor
/* val destructor */
1232 static void version();
1234 /* ========================= Random utility functions ======================= */
1236 /* Redis generally does not try to recover from out of memory conditions
1237 * when allocating objects or strings, it is not clear if it will be possible
1238 * to report this condition to the client since the networking layer itself
1239 * is based on heap allocation for send buffers, so we simply abort.
1240 * At least the code will be simpler to read... */
1241 static void oom(const char *msg
) {
1242 redisLog(REDIS_WARNING
, "%s: Out of memory\n",msg
);
1247 /* ====================== Redis server networking stuff ===================== */
1248 static void closeTimedoutClients(void) {
1251 time_t now
= time(NULL
);
1254 listRewind(server
.clients
,&li
);
1255 while ((ln
= listNext(&li
)) != NULL
) {
1256 c
= listNodeValue(ln
);
1257 if (server
.maxidletime
&&
1258 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
1259 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
1260 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
1261 listLength(c
->pubsub_patterns
) == 0 &&
1262 (now
- c
->lastinteraction
> server
.maxidletime
))
1264 redisLog(REDIS_VERBOSE
,"Closing idle client");
1266 } else if (c
->flags
& REDIS_BLOCKED
) {
1267 if (c
->blockingto
!= 0 && c
->blockingto
< now
) {
1268 addReply(c
,shared
.nullmultibulk
);
1269 unblockClientWaitingData(c
);
1275 static int htNeedsResize(dict
*dict
) {
1276 long long size
, used
;
1278 size
= dictSlots(dict
);
1279 used
= dictSize(dict
);
1280 return (size
&& used
&& size
> DICT_HT_INITIAL_SIZE
&&
1281 (used
*100/size
< REDIS_HT_MINFILL
));
1284 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
1285 * we resize the hash table to save memory */
1286 static void tryResizeHashTables(void) {
1289 for (j
= 0; j
< server
.dbnum
; j
++) {
1290 if (htNeedsResize(server
.db
[j
].dict
))
1291 dictResize(server
.db
[j
].dict
);
1292 if (htNeedsResize(server
.db
[j
].expires
))
1293 dictResize(server
.db
[j
].expires
);
1297 /* Our hash table implementation performs rehashing incrementally while
1298 * we write/read from the hash table. Still if the server is idle, the hash
1299 * table will use two tables for a long time. So we try to use 1 millisecond
1300 * of CPU time at every serverCron() loop in order to rehash some key. */
1301 static void incrementallyRehash(void) {
1304 for (j
= 0; j
< server
.dbnum
; j
++) {
1305 if (dictIsRehashing(server
.db
[j
].dict
)) {
1306 dictRehashMilliseconds(server
.db
[j
].dict
,1);
1307 break; /* already used our millisecond for this loop... */
1312 /* A background saving child (BGSAVE) terminated its work. Handle this. */
1313 void backgroundSaveDoneHandler(int statloc
) {
1314 int exitcode
= WEXITSTATUS(statloc
);
1315 int bysignal
= WIFSIGNALED(statloc
);
1317 if (!bysignal
&& exitcode
== 0) {
1318 redisLog(REDIS_NOTICE
,
1319 "Background saving terminated with success");
1321 server
.lastsave
= time(NULL
);
1322 } else if (!bysignal
&& exitcode
!= 0) {
1323 redisLog(REDIS_WARNING
, "Background saving error");
1325 redisLog(REDIS_WARNING
,
1326 "Background saving terminated by signal %d", WTERMSIG(statloc
));
1327 rdbRemoveTempFile(server
.bgsavechildpid
);
1329 server
.bgsavechildpid
= -1;
1330 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1331 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1332 updateSlavesWaitingBgsave(exitcode
== 0 ? REDIS_OK
: REDIS_ERR
);
1335 /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
1337 void backgroundRewriteDoneHandler(int statloc
) {
1338 int exitcode
= WEXITSTATUS(statloc
);
1339 int bysignal
= WIFSIGNALED(statloc
);
1341 if (!bysignal
&& exitcode
== 0) {
1345 redisLog(REDIS_NOTICE
,
1346 "Background append only file rewriting terminated with success");
1347 /* Now it's time to flush the differences accumulated by the parent */
1348 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) server
.bgrewritechildpid
);
1349 fd
= open(tmpfile
,O_WRONLY
|O_APPEND
);
1351 redisLog(REDIS_WARNING
, "Not able to open the temp append only file produced by the child: %s", strerror(errno
));
1354 /* Flush our data... */
1355 if (write(fd
,server
.bgrewritebuf
,sdslen(server
.bgrewritebuf
)) !=
1356 (signed) sdslen(server
.bgrewritebuf
)) {
1357 redisLog(REDIS_WARNING
, "Error or short write trying to flush the parent diff of the append log file in the child temp file: %s", strerror(errno
));
1361 redisLog(REDIS_NOTICE
,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server
.bgrewritebuf
));
1362 /* Now our work is to rename the temp file into the stable file. And
1363 * switch the file descriptor used by the server for append only. */
1364 if (rename(tmpfile
,server
.appendfilename
) == -1) {
1365 redisLog(REDIS_WARNING
,"Can't rename the temp append only file into the stable one: %s", strerror(errno
));
1369 /* Mission completed... almost */
1370 redisLog(REDIS_NOTICE
,"Append only file successfully rewritten.");
1371 if (server
.appendfd
!= -1) {
1372 /* If append only is actually enabled... */
1373 close(server
.appendfd
);
1374 server
.appendfd
= fd
;
1376 server
.appendseldb
= -1; /* Make sure it will issue SELECT */
1377 redisLog(REDIS_NOTICE
,"The new append only file was selected for future appends.");
1379 /* If append only is disabled we just generate a dump in this
1380 * format. Why not? */
1383 } else if (!bysignal
&& exitcode
!= 0) {
1384 redisLog(REDIS_WARNING
, "Background append only file rewriting error");
1386 redisLog(REDIS_WARNING
,
1387 "Background append only file rewriting terminated by signal %d",
1391 sdsfree(server
.bgrewritebuf
);
1392 server
.bgrewritebuf
= sdsempty();
1393 aofRemoveTempFile(server
.bgrewritechildpid
);
1394 server
.bgrewritechildpid
= -1;
1397 /* This function is called once a background process of some kind terminates,
1398 * as we want to avoid resizing the hash tables when there is a child in order
1399 * to play well with copy-on-write (otherwise when a resize happens lots of
1400 * memory pages are copied). The goal of this function is to update the ability
1401 * for dict.c to resize the hash tables accordingly to the fact we have o not
1402 * running childs. */
1403 static void updateDictResizePolicy(void) {
1404 if (server
.bgsavechildpid
== -1 && server
.bgrewritechildpid
== -1)
1407 dictDisableResize();
1410 static int serverCron(struct aeEventLoop
*eventLoop
, long long id
, void *clientData
) {
1411 int j
, loops
= server
.cronloops
++;
1412 REDIS_NOTUSED(eventLoop
);
1414 REDIS_NOTUSED(clientData
);
1416 /* We take a cached value of the unix time in the global state because
1417 * with virtual memory and aging there is to store the current time
1418 * in objects at every object access, and accuracy is not needed.
1419 * To access a global var is faster than calling time(NULL) */
1420 server
.unixtime
= time(NULL
);
1422 /* Show some info about non-empty databases */
1423 for (j
= 0; j
< server
.dbnum
; j
++) {
1424 long long size
, used
, vkeys
;
1426 size
= dictSlots(server
.db
[j
].dict
);
1427 used
= dictSize(server
.db
[j
].dict
);
1428 vkeys
= dictSize(server
.db
[j
].expires
);
1429 if (!(loops
% 50) && (used
|| vkeys
)) {
1430 redisLog(REDIS_VERBOSE
,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j
,used
,vkeys
,size
);
1431 /* dictPrintStats(server.dict); */
1435 /* We don't want to resize the hash tables while a bacground saving
1436 * is in progress: the saving child is created using fork() that is
1437 * implemented with a copy-on-write semantic in most modern systems, so
1438 * if we resize the HT while there is the saving child at work actually
1439 * a lot of memory movements in the parent will cause a lot of pages
1441 if (server
.bgsavechildpid
== -1 && server
.bgrewritechildpid
== -1) {
1442 if (!(loops
% 10)) tryResizeHashTables();
1443 if (server
.activerehashing
) incrementallyRehash();
1446 /* Show information about connected clients */
1447 if (!(loops
% 50)) {
1448 redisLog(REDIS_VERBOSE
,"%d clients connected (%d slaves), %zu bytes in use",
1449 listLength(server
.clients
)-listLength(server
.slaves
),
1450 listLength(server
.slaves
),
1451 zmalloc_used_memory());
1454 /* Close connections of timedout clients */
1455 if ((server
.maxidletime
&& !(loops
% 100)) || server
.blpop_blocked_clients
)
1456 closeTimedoutClients();
1458 /* Check if a background saving or AOF rewrite in progress terminated */
1459 if (server
.bgsavechildpid
!= -1 || server
.bgrewritechildpid
!= -1) {
1463 if ((pid
= wait3(&statloc
,WNOHANG
,NULL
)) != 0) {
1464 if (pid
== server
.bgsavechildpid
) {
1465 backgroundSaveDoneHandler(statloc
);
1467 backgroundRewriteDoneHandler(statloc
);
1469 updateDictResizePolicy();
1472 /* If there is not a background saving in progress check if
1473 * we have to save now */
1474 time_t now
= time(NULL
);
1475 for (j
= 0; j
< server
.saveparamslen
; j
++) {
1476 struct saveparam
*sp
= server
.saveparams
+j
;
1478 if (server
.dirty
>= sp
->changes
&&
1479 now
-server
.lastsave
> sp
->seconds
) {
1480 redisLog(REDIS_NOTICE
,"%d changes in %d seconds. Saving...",
1481 sp
->changes
, sp
->seconds
);
1482 rdbSaveBackground(server
.dbfilename
);
1488 /* Try to expire a few timed out keys. The algorithm used is adaptive and
1489 * will use few CPU cycles if there are few expiring keys, otherwise
1490 * it will get more aggressive to avoid that too much memory is used by
1491 * keys that can be removed from the keyspace. */
1492 for (j
= 0; j
< server
.dbnum
; j
++) {
1494 redisDb
*db
= server
.db
+j
;
1496 /* Continue to expire if at the end of the cycle more than 25%
1497 * of the keys were expired. */
1499 long num
= dictSize(db
->expires
);
1500 time_t now
= time(NULL
);
1503 if (num
> REDIS_EXPIRELOOKUPS_PER_CRON
)
1504 num
= REDIS_EXPIRELOOKUPS_PER_CRON
;
1509 if ((de
= dictGetRandomKey(db
->expires
)) == NULL
) break;
1510 t
= (time_t) dictGetEntryVal(de
);
1512 deleteKey(db
,dictGetEntryKey(de
));
1514 server
.stat_expiredkeys
++;
1517 } while (expired
> REDIS_EXPIRELOOKUPS_PER_CRON
/4);
1520 /* Swap a few keys on disk if we are over the memory limit and VM
1521 * is enbled. Try to free objects from the free list first. */
1522 if (vmCanSwapOut()) {
1523 while (server
.vm_enabled
&& zmalloc_used_memory() >
1524 server
.vm_max_memory
)
1528 if (tryFreeOneObjectFromFreelist() == REDIS_OK
) continue;
1529 retval
= (server
.vm_max_threads
== 0) ?
1530 vmSwapOneObjectBlocking() :
1531 vmSwapOneObjectThreaded();
1532 if (retval
== REDIS_ERR
&& !(loops
% 300) &&
1533 zmalloc_used_memory() >
1534 (server
.vm_max_memory
+server
.vm_max_memory
/10))
1536 redisLog(REDIS_WARNING
,"WARNING: vm-max-memory limit exceeded by more than 10%% but unable to swap more objects out!");
1538 /* Note that when using threade I/O we free just one object,
1539 * because anyway when the I/O thread in charge to swap this
1540 * object out will finish, the handler of completed jobs
1541 * will try to swap more objects if we are still out of memory. */
1542 if (retval
== REDIS_ERR
|| server
.vm_max_threads
> 0) break;
1546 /* Check if we should connect to a MASTER */
1547 if (server
.replstate
== REDIS_REPL_CONNECT
&& !(loops
% 10)) {
1548 redisLog(REDIS_NOTICE
,"Connecting to MASTER...");
1549 if (syncWithMaster() == REDIS_OK
) {
1550 redisLog(REDIS_NOTICE
,"MASTER <-> SLAVE sync succeeded");
1551 if (server
.appendonly
) rewriteAppendOnlyFileBackground();
1557 /* This function gets called every time Redis is entering the
1558 * main loop of the event driven library, that is, before to sleep
1559 * for ready file descriptors. */
1560 static void beforeSleep(struct aeEventLoop
*eventLoop
) {
1561 REDIS_NOTUSED(eventLoop
);
1563 /* Awake clients that got all the swapped keys they requested */
1564 if (server
.vm_enabled
&& listLength(server
.io_ready_clients
)) {
1568 listRewind(server
.io_ready_clients
,&li
);
1569 while((ln
= listNext(&li
))) {
1570 redisClient
*c
= ln
->value
;
1571 struct redisCommand
*cmd
;
1573 /* Resume the client. */
1574 listDelNode(server
.io_ready_clients
,ln
);
1575 c
->flags
&= (~REDIS_IO_WAIT
);
1576 server
.vm_blocked_clients
--;
1577 aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
1578 readQueryFromClient
, c
);
1579 cmd
= lookupCommand(c
->argv
[0]->ptr
);
1580 assert(cmd
!= NULL
);
1583 /* There may be more data to process in the input buffer. */
1584 if (c
->querybuf
&& sdslen(c
->querybuf
) > 0)
1585 processInputBuffer(c
);
1588 /* Write the AOF buffer on disk */
1589 flushAppendOnlyFile();
1592 static void createSharedObjects(void) {
1595 shared
.crlf
= createObject(REDIS_STRING
,sdsnew("\r\n"));
1596 shared
.ok
= createObject(REDIS_STRING
,sdsnew("+OK\r\n"));
1597 shared
.err
= createObject(REDIS_STRING
,sdsnew("-ERR\r\n"));
1598 shared
.emptybulk
= createObject(REDIS_STRING
,sdsnew("$0\r\n\r\n"));
1599 shared
.czero
= createObject(REDIS_STRING
,sdsnew(":0\r\n"));
1600 shared
.cone
= createObject(REDIS_STRING
,sdsnew(":1\r\n"));
1601 shared
.nullbulk
= createObject(REDIS_STRING
,sdsnew("$-1\r\n"));
1602 shared
.nullmultibulk
= createObject(REDIS_STRING
,sdsnew("*-1\r\n"));
1603 shared
.emptymultibulk
= createObject(REDIS_STRING
,sdsnew("*0\r\n"));
1604 shared
.pong
= createObject(REDIS_STRING
,sdsnew("+PONG\r\n"));
1605 shared
.queued
= createObject(REDIS_STRING
,sdsnew("+QUEUED\r\n"));
1606 shared
.wrongtypeerr
= createObject(REDIS_STRING
,sdsnew(
1607 "-ERR Operation against a key holding the wrong kind of value\r\n"));
1608 shared
.nokeyerr
= createObject(REDIS_STRING
,sdsnew(
1609 "-ERR no such key\r\n"));
1610 shared
.syntaxerr
= createObject(REDIS_STRING
,sdsnew(
1611 "-ERR syntax error\r\n"));
1612 shared
.sameobjecterr
= createObject(REDIS_STRING
,sdsnew(
1613 "-ERR source and destination objects are the same\r\n"));
1614 shared
.outofrangeerr
= createObject(REDIS_STRING
,sdsnew(
1615 "-ERR index out of range\r\n"));
1616 shared
.space
= createObject(REDIS_STRING
,sdsnew(" "));
1617 shared
.colon
= createObject(REDIS_STRING
,sdsnew(":"));
1618 shared
.plus
= createObject(REDIS_STRING
,sdsnew("+"));
1619 shared
.select0
= createStringObject("select 0\r\n",10);
1620 shared
.select1
= createStringObject("select 1\r\n",10);
1621 shared
.select2
= createStringObject("select 2\r\n",10);
1622 shared
.select3
= createStringObject("select 3\r\n",10);
1623 shared
.select4
= createStringObject("select 4\r\n",10);
1624 shared
.select5
= createStringObject("select 5\r\n",10);
1625 shared
.select6
= createStringObject("select 6\r\n",10);
1626 shared
.select7
= createStringObject("select 7\r\n",10);
1627 shared
.select8
= createStringObject("select 8\r\n",10);
1628 shared
.select9
= createStringObject("select 9\r\n",10);
1629 shared
.messagebulk
= createStringObject("$7\r\nmessage\r\n",13);
1630 shared
.pmessagebulk
= createStringObject("$8\r\npmessage\r\n",14);
1631 shared
.subscribebulk
= createStringObject("$9\r\nsubscribe\r\n",15);
1632 shared
.unsubscribebulk
= createStringObject("$11\r\nunsubscribe\r\n",18);
1633 shared
.psubscribebulk
= createStringObject("$10\r\npsubscribe\r\n",17);
1634 shared
.punsubscribebulk
= createStringObject("$12\r\npunsubscribe\r\n",19);
1635 shared
.mbulk3
= createStringObject("*3\r\n",4);
1636 shared
.mbulk4
= createStringObject("*4\r\n",4);
1637 for (j
= 0; j
< REDIS_SHARED_INTEGERS
; j
++) {
1638 shared
.integers
[j
] = createObject(REDIS_STRING
,(void*)(long)j
);
1639 shared
.integers
[j
]->encoding
= REDIS_ENCODING_INT
;
1643 static void appendServerSaveParams(time_t seconds
, int changes
) {
1644 server
.saveparams
= zrealloc(server
.saveparams
,sizeof(struct saveparam
)*(server
.saveparamslen
+1));
1645 server
.saveparams
[server
.saveparamslen
].seconds
= seconds
;
1646 server
.saveparams
[server
.saveparamslen
].changes
= changes
;
1647 server
.saveparamslen
++;
1650 static void resetServerSaveParams() {
1651 zfree(server
.saveparams
);
1652 server
.saveparams
= NULL
;
1653 server
.saveparamslen
= 0;
1656 static void initServerConfig() {
1657 server
.dbnum
= REDIS_DEFAULT_DBNUM
;
1658 server
.port
= REDIS_SERVERPORT
;
1659 server
.verbosity
= REDIS_VERBOSE
;
1660 server
.maxidletime
= REDIS_MAXIDLETIME
;
1661 server
.saveparams
= NULL
;
1662 server
.logfile
= NULL
; /* NULL = log on standard output */
1663 server
.bindaddr
= NULL
;
1664 server
.glueoutputbuf
= 1;
1665 server
.daemonize
= 0;
1666 server
.appendonly
= 0;
1667 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
1668 server
.lastfsync
= time(NULL
);
1669 server
.appendfd
= -1;
1670 server
.appendseldb
= -1; /* Make sure the first time will not match */
1671 server
.pidfile
= zstrdup("/var/run/redis.pid");
1672 server
.dbfilename
= zstrdup("dump.rdb");
1673 server
.appendfilename
= zstrdup("appendonly.aof");
1674 server
.requirepass
= NULL
;
1675 server
.rdbcompression
= 1;
1676 server
.activerehashing
= 1;
1677 server
.maxclients
= 0;
1678 server
.blpop_blocked_clients
= 0;
1679 server
.maxmemory
= 0;
1680 server
.vm_enabled
= 0;
1681 server
.vm_swap_file
= zstrdup("/tmp/redis-%p.vm");
1682 server
.vm_page_size
= 256; /* 256 bytes per page */
1683 server
.vm_pages
= 1024*1024*100; /* 104 millions of pages */
1684 server
.vm_max_memory
= 1024LL*1024*1024*1; /* 1 GB of RAM */
1685 server
.vm_max_threads
= 4;
1686 server
.vm_blocked_clients
= 0;
1687 server
.hash_max_zipmap_entries
= REDIS_HASH_MAX_ZIPMAP_ENTRIES
;
1688 server
.hash_max_zipmap_value
= REDIS_HASH_MAX_ZIPMAP_VALUE
;
1690 resetServerSaveParams();
1692 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1693 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1694 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1695 /* Replication related */
1697 server
.masterauth
= NULL
;
1698 server
.masterhost
= NULL
;
1699 server
.masterport
= 6379;
1700 server
.master
= NULL
;
1701 server
.replstate
= REDIS_REPL_NONE
;
1703 /* Double constants initialization */
1705 R_PosInf
= 1.0/R_Zero
;
1706 R_NegInf
= -1.0/R_Zero
;
1707 R_Nan
= R_Zero
/R_Zero
;
1710 static void initServer() {
1713 signal(SIGHUP
, SIG_IGN
);
1714 signal(SIGPIPE
, SIG_IGN
);
1715 setupSigSegvAction();
1717 server
.devnull
= fopen("/dev/null","w");
1718 if (server
.devnull
== NULL
) {
1719 redisLog(REDIS_WARNING
, "Can't open /dev/null: %s", server
.neterr
);
1722 server
.clients
= listCreate();
1723 server
.slaves
= listCreate();
1724 server
.monitors
= listCreate();
1725 server
.objfreelist
= listCreate();
1726 createSharedObjects();
1727 server
.el
= aeCreateEventLoop();
1728 server
.db
= zmalloc(sizeof(redisDb
)*server
.dbnum
);
1729 server
.fd
= anetTcpServer(server
.neterr
, server
.port
, server
.bindaddr
);
1730 if (server
.fd
== -1) {
1731 redisLog(REDIS_WARNING
, "Opening TCP port: %s", server
.neterr
);
1734 for (j
= 0; j
< server
.dbnum
; j
++) {
1735 server
.db
[j
].dict
= dictCreate(&dbDictType
,NULL
);
1736 server
.db
[j
].expires
= dictCreate(&keyptrDictType
,NULL
);
1737 server
.db
[j
].blockingkeys
= dictCreate(&keylistDictType
,NULL
);
1738 if (server
.vm_enabled
)
1739 server
.db
[j
].io_keys
= dictCreate(&keylistDictType
,NULL
);
1740 server
.db
[j
].id
= j
;
1742 server
.pubsub_channels
= dictCreate(&keylistDictType
,NULL
);
1743 server
.pubsub_patterns
= listCreate();
1744 listSetFreeMethod(server
.pubsub_patterns
,freePubsubPattern
);
1745 listSetMatchMethod(server
.pubsub_patterns
,listMatchPubsubPattern
);
1746 server
.cronloops
= 0;
1747 server
.bgsavechildpid
= -1;
1748 server
.bgrewritechildpid
= -1;
1749 server
.bgrewritebuf
= sdsempty();
1750 server
.aofbuf
= sdsempty();
1751 server
.lastsave
= time(NULL
);
1753 server
.stat_numcommands
= 0;
1754 server
.stat_numconnections
= 0;
1755 server
.stat_expiredkeys
= 0;
1756 server
.stat_starttime
= time(NULL
);
1757 server
.unixtime
= time(NULL
);
1758 aeCreateTimeEvent(server
.el
, 1, serverCron
, NULL
, NULL
);
1759 if (aeCreateFileEvent(server
.el
, server
.fd
, AE_READABLE
,
1760 acceptHandler
, NULL
) == AE_ERR
) oom("creating file event");
1762 if (server
.appendonly
) {
1763 server
.appendfd
= open(server
.appendfilename
,O_WRONLY
|O_APPEND
|O_CREAT
,0644);
1764 if (server
.appendfd
== -1) {
1765 redisLog(REDIS_WARNING
, "Can't open the append-only file: %s",
1771 if (server
.vm_enabled
) vmInit();
1774 /* Empty the whole database */
1775 static long long emptyDb() {
1777 long long removed
= 0;
1779 for (j
= 0; j
< server
.dbnum
; j
++) {
1780 removed
+= dictSize(server
.db
[j
].dict
);
1781 dictEmpty(server
.db
[j
].dict
);
1782 dictEmpty(server
.db
[j
].expires
);
1787 static int yesnotoi(char *s
) {
1788 if (!strcasecmp(s
,"yes")) return 1;
1789 else if (!strcasecmp(s
,"no")) return 0;
1793 /* I agree, this is a very rudimental way to load a configuration...
1794 will improve later if the config gets more complex */
1795 static void loadServerConfig(char *filename
) {
1797 char buf
[REDIS_CONFIGLINE_MAX
+1], *err
= NULL
;
1801 if (filename
[0] == '-' && filename
[1] == '\0')
1804 if ((fp
= fopen(filename
,"r")) == NULL
) {
1805 redisLog(REDIS_WARNING
, "Fatal error, can't open config file '%s'", filename
);
1810 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
) {
1816 line
= sdstrim(line
," \t\r\n");
1818 /* Skip comments and blank lines*/
1819 if (line
[0] == '#' || line
[0] == '\0') {
1824 /* Split into arguments */
1825 argv
= sdssplitlen(line
,sdslen(line
)," ",1,&argc
);
1826 sdstolower(argv
[0]);
1828 /* Execute config directives */
1829 if (!strcasecmp(argv
[0],"timeout") && argc
== 2) {
1830 server
.maxidletime
= atoi(argv
[1]);
1831 if (server
.maxidletime
< 0) {
1832 err
= "Invalid timeout value"; goto loaderr
;
1834 } else if (!strcasecmp(argv
[0],"port") && argc
== 2) {
1835 server
.port
= atoi(argv
[1]);
1836 if (server
.port
< 1 || server
.port
> 65535) {
1837 err
= "Invalid port"; goto loaderr
;
1839 } else if (!strcasecmp(argv
[0],"bind") && argc
== 2) {
1840 server
.bindaddr
= zstrdup(argv
[1]);
1841 } else if (!strcasecmp(argv
[0],"save") && argc
== 3) {
1842 int seconds
= atoi(argv
[1]);
1843 int changes
= atoi(argv
[2]);
1844 if (seconds
< 1 || changes
< 0) {
1845 err
= "Invalid save parameters"; goto loaderr
;
1847 appendServerSaveParams(seconds
,changes
);
1848 } else if (!strcasecmp(argv
[0],"dir") && argc
== 2) {
1849 if (chdir(argv
[1]) == -1) {
1850 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
1851 argv
[1], strerror(errno
));
1854 } else if (!strcasecmp(argv
[0],"loglevel") && argc
== 2) {
1855 if (!strcasecmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
1856 else if (!strcasecmp(argv
[1],"verbose")) server
.verbosity
= REDIS_VERBOSE
;
1857 else if (!strcasecmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
1858 else if (!strcasecmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
1860 err
= "Invalid log level. Must be one of debug, notice, warning";
1863 } else if (!strcasecmp(argv
[0],"logfile") && argc
== 2) {
1866 server
.logfile
= zstrdup(argv
[1]);
1867 if (!strcasecmp(server
.logfile
,"stdout")) {
1868 zfree(server
.logfile
);
1869 server
.logfile
= NULL
;
1871 if (server
.logfile
) {
1872 /* Test if we are able to open the file. The server will not
1873 * be able to abort just for this problem later... */
1874 logfp
= fopen(server
.logfile
,"a");
1875 if (logfp
== NULL
) {
1876 err
= sdscatprintf(sdsempty(),
1877 "Can't open the log file: %s", strerror(errno
));
1882 } else if (!strcasecmp(argv
[0],"databases") && argc
== 2) {
1883 server
.dbnum
= atoi(argv
[1]);
1884 if (server
.dbnum
< 1) {
1885 err
= "Invalid number of databases"; goto loaderr
;
1887 } else if (!strcasecmp(argv
[0],"include") && argc
== 2) {
1888 loadServerConfig(argv
[1]);
1889 } else if (!strcasecmp(argv
[0],"maxclients") && argc
== 2) {
1890 server
.maxclients
= atoi(argv
[1]);
1891 } else if (!strcasecmp(argv
[0],"maxmemory") && argc
== 2) {
1892 server
.maxmemory
= memtoll(argv
[1],NULL
);
1893 } else if (!strcasecmp(argv
[0],"slaveof") && argc
== 3) {
1894 server
.masterhost
= sdsnew(argv
[1]);
1895 server
.masterport
= atoi(argv
[2]);
1896 server
.replstate
= REDIS_REPL_CONNECT
;
1897 } else if (!strcasecmp(argv
[0],"masterauth") && argc
== 2) {
1898 server
.masterauth
= zstrdup(argv
[1]);
1899 } else if (!strcasecmp(argv
[0],"glueoutputbuf") && argc
== 2) {
1900 if ((server
.glueoutputbuf
= yesnotoi(argv
[1])) == -1) {
1901 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1903 } else if (!strcasecmp(argv
[0],"rdbcompression") && argc
== 2) {
1904 if ((server
.rdbcompression
= yesnotoi(argv
[1])) == -1) {
1905 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1907 } else if (!strcasecmp(argv
[0],"activerehashing") && argc
== 2) {
1908 if ((server
.activerehashing
= yesnotoi(argv
[1])) == -1) {
1909 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1911 } else if (!strcasecmp(argv
[0],"daemonize") && argc
== 2) {
1912 if ((server
.daemonize
= yesnotoi(argv
[1])) == -1) {
1913 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1915 } else if (!strcasecmp(argv
[0],"appendonly") && argc
== 2) {
1916 if ((server
.appendonly
= yesnotoi(argv
[1])) == -1) {
1917 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1919 } else if (!strcasecmp(argv
[0],"appendfilename") && argc
== 2) {
1920 zfree(server
.appendfilename
);
1921 server
.appendfilename
= zstrdup(argv
[1]);
1922 } else if (!strcasecmp(argv
[0],"appendfsync") && argc
== 2) {
1923 if (!strcasecmp(argv
[1],"no")) {
1924 server
.appendfsync
= APPENDFSYNC_NO
;
1925 } else if (!strcasecmp(argv
[1],"always")) {
1926 server
.appendfsync
= APPENDFSYNC_ALWAYS
;
1927 } else if (!strcasecmp(argv
[1],"everysec")) {
1928 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
1930 err
= "argument must be 'no', 'always' or 'everysec'";
1933 } else if (!strcasecmp(argv
[0],"requirepass") && argc
== 2) {
1934 server
.requirepass
= zstrdup(argv
[1]);
1935 } else if (!strcasecmp(argv
[0],"pidfile") && argc
== 2) {
1936 zfree(server
.pidfile
);
1937 server
.pidfile
= zstrdup(argv
[1]);
1938 } else if (!strcasecmp(argv
[0],"dbfilename") && argc
== 2) {
1939 zfree(server
.dbfilename
);
1940 server
.dbfilename
= zstrdup(argv
[1]);
1941 } else if (!strcasecmp(argv
[0],"vm-enabled") && argc
== 2) {
1942 if ((server
.vm_enabled
= yesnotoi(argv
[1])) == -1) {
1943 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1945 } else if (!strcasecmp(argv
[0],"vm-swap-file") && argc
== 2) {
1946 zfree(server
.vm_swap_file
);
1947 server
.vm_swap_file
= zstrdup(argv
[1]);
1948 } else if (!strcasecmp(argv
[0],"vm-max-memory") && argc
== 2) {
1949 server
.vm_max_memory
= memtoll(argv
[1],NULL
);
1950 } else if (!strcasecmp(argv
[0],"vm-page-size") && argc
== 2) {
1951 server
.vm_page_size
= memtoll(argv
[1], NULL
);
1952 } else if (!strcasecmp(argv
[0],"vm-pages") && argc
== 2) {
1953 server
.vm_pages
= memtoll(argv
[1], NULL
);
1954 } else if (!strcasecmp(argv
[0],"vm-max-threads") && argc
== 2) {
1955 server
.vm_max_threads
= strtoll(argv
[1], NULL
, 10);
1956 } else if (!strcasecmp(argv
[0],"hash-max-zipmap-entries") && argc
== 2){
1957 server
.hash_max_zipmap_entries
= memtoll(argv
[1], NULL
);
1958 } else if (!strcasecmp(argv
[0],"hash-max-zipmap-value") && argc
== 2){
1959 server
.hash_max_zipmap_value
= memtoll(argv
[1], NULL
);
1961 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
1963 for (j
= 0; j
< argc
; j
++)
1968 if (fp
!= stdin
) fclose(fp
);
1972 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
1973 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
1974 fprintf(stderr
, ">>> '%s'\n", line
);
1975 fprintf(stderr
, "%s\n", err
);
1979 static void freeClientArgv(redisClient
*c
) {
1982 for (j
= 0; j
< c
->argc
; j
++)
1983 decrRefCount(c
->argv
[j
]);
1984 for (j
= 0; j
< c
->mbargc
; j
++)
1985 decrRefCount(c
->mbargv
[j
]);
1990 static void freeClient(redisClient
*c
) {
1993 /* Note that if the client we are freeing is blocked into a blocking
1994 * call, we have to set querybuf to NULL *before* to call
1995 * unblockClientWaitingData() to avoid processInputBuffer() will get
1996 * called. Also it is important to remove the file events after
1997 * this, because this call adds the READABLE event. */
1998 sdsfree(c
->querybuf
);
2000 if (c
->flags
& REDIS_BLOCKED
)
2001 unblockClientWaitingData(c
);
2003 /* Unsubscribe from all the pubsub channels */
2004 pubsubUnsubscribeAllChannels(c
,0);
2005 pubsubUnsubscribeAllPatterns(c
,0);
2006 dictRelease(c
->pubsub_channels
);
2007 listRelease(c
->pubsub_patterns
);
2008 /* Obvious cleanup */
2009 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
2010 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
2011 listRelease(c
->reply
);
2014 /* Remove from the list of clients */
2015 ln
= listSearchKey(server
.clients
,c
);
2016 redisAssert(ln
!= NULL
);
2017 listDelNode(server
.clients
,ln
);
2018 /* Remove from the list of clients waiting for swapped keys */
2019 if (c
->flags
& REDIS_IO_WAIT
&& listLength(c
->io_keys
) == 0) {
2020 ln
= listSearchKey(server
.io_ready_clients
,c
);
2022 listDelNode(server
.io_ready_clients
,ln
);
2023 server
.vm_blocked_clients
--;
2026 while (server
.vm_enabled
&& listLength(c
->io_keys
)) {
2027 ln
= listFirst(c
->io_keys
);
2028 dontWaitForSwappedKey(c
,ln
->value
);
2030 listRelease(c
->io_keys
);
2031 /* Master/slave cleanup */
2032 if (c
->flags
& REDIS_SLAVE
) {
2033 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
2035 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
2036 ln
= listSearchKey(l
,c
);
2037 redisAssert(ln
!= NULL
);
2040 if (c
->flags
& REDIS_MASTER
) {
2041 server
.master
= NULL
;
2042 server
.replstate
= REDIS_REPL_CONNECT
;
2044 /* Release memory */
2047 freeClientMultiState(c
);
2051 #define GLUEREPLY_UP_TO (1024)
2052 static void glueReplyBuffersIfNeeded(redisClient
*c
) {
2054 char buf
[GLUEREPLY_UP_TO
];
2059 listRewind(c
->reply
,&li
);
2060 while((ln
= listNext(&li
))) {
2064 objlen
= sdslen(o
->ptr
);
2065 if (copylen
+ objlen
<= GLUEREPLY_UP_TO
) {
2066 memcpy(buf
+copylen
,o
->ptr
,objlen
);
2068 listDelNode(c
->reply
,ln
);
2070 if (copylen
== 0) return;
2074 /* Now the output buffer is empty, add the new single element */
2075 o
= createObject(REDIS_STRING
,sdsnewlen(buf
,copylen
));
2076 listAddNodeHead(c
->reply
,o
);
2079 static void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
2080 redisClient
*c
= privdata
;
2081 int nwritten
= 0, totwritten
= 0, objlen
;
2084 REDIS_NOTUSED(mask
);
2086 /* Use writev() if we have enough buffers to send */
2087 if (!server
.glueoutputbuf
&&
2088 listLength(c
->reply
) > REDIS_WRITEV_THRESHOLD
&&
2089 !(c
->flags
& REDIS_MASTER
))
2091 sendReplyToClientWritev(el
, fd
, privdata
, mask
);
2095 while(listLength(c
->reply
)) {
2096 if (server
.glueoutputbuf
&& listLength(c
->reply
) > 1)
2097 glueReplyBuffersIfNeeded(c
);
2099 o
= listNodeValue(listFirst(c
->reply
));
2100 objlen
= sdslen(o
->ptr
);
2103 listDelNode(c
->reply
,listFirst(c
->reply
));
2107 if (c
->flags
& REDIS_MASTER
) {
2108 /* Don't reply to a master */
2109 nwritten
= objlen
- c
->sentlen
;
2111 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
, objlen
- c
->sentlen
);
2112 if (nwritten
<= 0) break;
2114 c
->sentlen
+= nwritten
;
2115 totwritten
+= nwritten
;
2116 /* If we fully sent the object on head go to the next one */
2117 if (c
->sentlen
== objlen
) {
2118 listDelNode(c
->reply
,listFirst(c
->reply
));
2121 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
2122 * bytes, in a single threaded server it's a good idea to serve
2123 * other clients as well, even if a very large request comes from
2124 * super fast link that is always able to accept data (in real world
2125 * scenario think about 'KEYS *' against the loopback interfae) */
2126 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
2128 if (nwritten
== -1) {
2129 if (errno
== EAGAIN
) {
2132 redisLog(REDIS_VERBOSE
,
2133 "Error writing to client: %s", strerror(errno
));
2138 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
2139 if (listLength(c
->reply
) == 0) {
2141 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
2145 static void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
2147 redisClient
*c
= privdata
;
2148 int nwritten
= 0, totwritten
= 0, objlen
, willwrite
;
2150 struct iovec iov
[REDIS_WRITEV_IOVEC_COUNT
];
2151 int offset
, ion
= 0;
2153 REDIS_NOTUSED(mask
);
2156 while (listLength(c
->reply
)) {
2157 offset
= c
->sentlen
;
2161 /* fill-in the iov[] array */
2162 for(node
= listFirst(c
->reply
); node
; node
= listNextNode(node
)) {
2163 o
= listNodeValue(node
);
2164 objlen
= sdslen(o
->ptr
);
2166 if (totwritten
+ objlen
- offset
> REDIS_MAX_WRITE_PER_EVENT
)
2169 if(ion
== REDIS_WRITEV_IOVEC_COUNT
)
2170 break; /* no more iovecs */
2172 iov
[ion
].iov_base
= ((char*)o
->ptr
) + offset
;
2173 iov
[ion
].iov_len
= objlen
- offset
;
2174 willwrite
+= objlen
- offset
;
2175 offset
= 0; /* just for the first item */
2182 /* write all collected blocks at once */
2183 if((nwritten
= writev(fd
, iov
, ion
)) < 0) {
2184 if (errno
!= EAGAIN
) {
2185 redisLog(REDIS_VERBOSE
,
2186 "Error writing to client: %s", strerror(errno
));
2193 totwritten
+= nwritten
;
2194 offset
= c
->sentlen
;
2196 /* remove written robjs from c->reply */
2197 while (nwritten
&& listLength(c
->reply
)) {
2198 o
= listNodeValue(listFirst(c
->reply
));
2199 objlen
= sdslen(o
->ptr
);
2201 if(nwritten
>= objlen
- offset
) {
2202 listDelNode(c
->reply
, listFirst(c
->reply
));
2203 nwritten
-= objlen
- offset
;
2207 c
->sentlen
+= nwritten
;
2215 c
->lastinteraction
= time(NULL
);
2217 if (listLength(c
->reply
) == 0) {
2219 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
2223 static struct redisCommand
*lookupCommand(char *name
) {
2225 while(cmdTable
[j
].name
!= NULL
) {
2226 if (!strcasecmp(name
,cmdTable
[j
].name
)) return &cmdTable
[j
];
2232 /* resetClient prepare the client to process the next command */
2233 static void resetClient(redisClient
*c
) {
2239 /* Call() is the core of Redis execution of a command */
2240 static void call(redisClient
*c
, struct redisCommand
*cmd
) {
2243 dirty
= server
.dirty
;
2245 dirty
= server
.dirty
-dirty
;
2247 if (server
.appendonly
&& dirty
)
2248 feedAppendOnlyFile(cmd
,c
->db
->id
,c
->argv
,c
->argc
);
2249 if ((dirty
|| cmd
->flags
& REDIS_CMD_FORCE_REPLICATION
) &&
2250 listLength(server
.slaves
))
2251 replicationFeedSlaves(server
.slaves
,c
->db
->id
,c
->argv
,c
->argc
);
2252 if (listLength(server
.monitors
))
2253 replicationFeedMonitors(server
.monitors
,c
->db
->id
,c
->argv
,c
->argc
);
2254 server
.stat_numcommands
++;
2257 /* If this function gets called we already read a whole
2258 * command, argments are in the client argv/argc fields.
2259 * processCommand() execute the command or prepare the
2260 * server for a bulk read from the client.
2262 * If 1 is returned the client is still alive and valid and
2263 * and other operations can be performed by the caller. Otherwise
2264 * if 0 is returned the client was destroied (i.e. after QUIT). */
2265 static int processCommand(redisClient
*c
) {
2266 struct redisCommand
*cmd
;
2268 /* Free some memory if needed (maxmemory setting) */
2269 if (server
.maxmemory
) freeMemoryIfNeeded();
2271 /* Handle the multi bulk command type. This is an alternative protocol
2272 * supported by Redis in order to receive commands that are composed of
2273 * multiple binary-safe "bulk" arguments. The latency of processing is
2274 * a bit higher but this allows things like multi-sets, so if this
2275 * protocol is used only for MSET and similar commands this is a big win. */
2276 if (c
->multibulk
== 0 && c
->argc
== 1 && ((char*)(c
->argv
[0]->ptr
))[0] == '*') {
2277 c
->multibulk
= atoi(((char*)c
->argv
[0]->ptr
)+1);
2278 if (c
->multibulk
<= 0) {
2282 decrRefCount(c
->argv
[c
->argc
-1]);
2286 } else if (c
->multibulk
) {
2287 if (c
->bulklen
== -1) {
2288 if (((char*)c
->argv
[0]->ptr
)[0] != '$') {
2289 addReplySds(c
,sdsnew("-ERR multi bulk protocol error\r\n"));
2293 int bulklen
= atoi(((char*)c
->argv
[0]->ptr
)+1);
2294 decrRefCount(c
->argv
[0]);
2295 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
2297 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
2302 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
2306 c
->mbargv
= zrealloc(c
->mbargv
,(sizeof(robj
*))*(c
->mbargc
+1));
2307 c
->mbargv
[c
->mbargc
] = c
->argv
[0];
2311 if (c
->multibulk
== 0) {
2315 /* Here we need to swap the multi-bulk argc/argv with the
2316 * normal argc/argv of the client structure. */
2318 c
->argv
= c
->mbargv
;
2319 c
->mbargv
= auxargv
;
2322 c
->argc
= c
->mbargc
;
2323 c
->mbargc
= auxargc
;
2325 /* We need to set bulklen to something different than -1
2326 * in order for the code below to process the command without
2327 * to try to read the last argument of a bulk command as
2328 * a special argument. */
2330 /* continue below and process the command */
2337 /* -- end of multi bulk commands processing -- */
2339 /* The QUIT command is handled as a special case. Normal command
2340 * procs are unable to close the client connection safely */
2341 if (!strcasecmp(c
->argv
[0]->ptr
,"quit")) {
2346 /* Now lookup the command and check ASAP about trivial error conditions
2347 * such wrong arity, bad command name and so forth. */
2348 cmd
= lookupCommand(c
->argv
[0]->ptr
);
2351 sdscatprintf(sdsempty(), "-ERR unknown command '%s'\r\n",
2352 (char*)c
->argv
[0]->ptr
));
2355 } else if ((cmd
->arity
> 0 && cmd
->arity
!= c
->argc
) ||
2356 (c
->argc
< -cmd
->arity
)) {
2358 sdscatprintf(sdsempty(),
2359 "-ERR wrong number of arguments for '%s' command\r\n",
2363 } else if (cmd
->flags
& REDIS_CMD_BULK
&& c
->bulklen
== -1) {
2364 /* This is a bulk command, we have to read the last argument yet. */
2365 int bulklen
= atoi(c
->argv
[c
->argc
-1]->ptr
);
2367 decrRefCount(c
->argv
[c
->argc
-1]);
2368 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
2370 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
2375 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
2376 /* It is possible that the bulk read is already in the
2377 * buffer. Check this condition and handle it accordingly.
2378 * This is just a fast path, alternative to call processInputBuffer().
2379 * It's a good idea since the code is small and this condition
2380 * happens most of the times. */
2381 if ((signed)sdslen(c
->querybuf
) >= c
->bulklen
) {
2382 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
2384 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
2386 /* Otherwise return... there is to read the last argument
2387 * from the socket. */
2391 /* Let's try to encode the bulk object to save space. */
2392 if (cmd
->flags
& REDIS_CMD_BULK
)
2393 c
->argv
[c
->argc
-1] = tryObjectEncoding(c
->argv
[c
->argc
-1]);
2395 /* Check if the user is authenticated */
2396 if (server
.requirepass
&& !c
->authenticated
&& cmd
->proc
!= authCommand
) {
2397 addReplySds(c
,sdsnew("-ERR operation not permitted\r\n"));
2402 /* Handle the maxmemory directive */
2403 if (server
.maxmemory
&& (cmd
->flags
& REDIS_CMD_DENYOOM
) &&
2404 zmalloc_used_memory() > server
.maxmemory
)
2406 addReplySds(c
,sdsnew("-ERR command not allowed when used memory > 'maxmemory'\r\n"));
2411 /* Only allow SUBSCRIBE and UNSUBSCRIBE in the context of Pub/Sub */
2412 if ((dictSize(c
->pubsub_channels
) > 0 || listLength(c
->pubsub_patterns
) > 0)
2414 cmd
->proc
!= subscribeCommand
&& cmd
->proc
!= unsubscribeCommand
&&
2415 cmd
->proc
!= psubscribeCommand
&& cmd
->proc
!= punsubscribeCommand
) {
2416 addReplySds(c
,sdsnew("-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context\r\n"));
2421 /* Exec the command */
2422 if (c
->flags
& REDIS_MULTI
&& cmd
->proc
!= execCommand
&& cmd
->proc
!= discardCommand
) {
2423 queueMultiCommand(c
,cmd
);
2424 addReply(c
,shared
.queued
);
2426 if (server
.vm_enabled
&& server
.vm_max_threads
> 0 &&
2427 blockClientOnSwappedKeys(c
,cmd
)) return 1;
2431 /* Prepare the client for the next command */
2436 static void replicationFeedSlaves(list
*slaves
, int dictid
, robj
**argv
, int argc
) {
2441 /* We need 1+(ARGS*3) objects since commands are using the new protocol
2442 * and we one 1 object for the first "*<count>\r\n" multibulk count, then
2443 * for every additional object we have "$<count>\r\n" + object + "\r\n". */
2444 robj
*static_outv
[REDIS_STATIC_ARGS
*3+1];
2447 if (argc
<= REDIS_STATIC_ARGS
) {
2450 outv
= zmalloc(sizeof(robj
*)*(argc
*3+1));
2453 lenobj
= createObject(REDIS_STRING
,
2454 sdscatprintf(sdsempty(), "*%d\r\n", argc
));
2455 lenobj
->refcount
= 0;
2456 outv
[outc
++] = lenobj
;
2457 for (j
= 0; j
< argc
; j
++) {
2458 lenobj
= createObject(REDIS_STRING
,
2459 sdscatprintf(sdsempty(),"$%lu\r\n",
2460 (unsigned long) stringObjectLen(argv
[j
])));
2461 lenobj
->refcount
= 0;
2462 outv
[outc
++] = lenobj
;
2463 outv
[outc
++] = argv
[j
];
2464 outv
[outc
++] = shared
.crlf
;
2467 /* Increment all the refcounts at start and decrement at end in order to
2468 * be sure to free objects if there is no slave in a replication state
2469 * able to be feed with commands */
2470 for (j
= 0; j
< outc
; j
++) incrRefCount(outv
[j
]);
2471 listRewind(slaves
,&li
);
2472 while((ln
= listNext(&li
))) {
2473 redisClient
*slave
= ln
->value
;
2475 /* Don't feed slaves that are still waiting for BGSAVE to start */
2476 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
) continue;
2478 /* Feed all the other slaves, MONITORs and so on */
2479 if (slave
->slaveseldb
!= dictid
) {
2483 case 0: selectcmd
= shared
.select0
; break;
2484 case 1: selectcmd
= shared
.select1
; break;
2485 case 2: selectcmd
= shared
.select2
; break;
2486 case 3: selectcmd
= shared
.select3
; break;
2487 case 4: selectcmd
= shared
.select4
; break;
2488 case 5: selectcmd
= shared
.select5
; break;
2489 case 6: selectcmd
= shared
.select6
; break;
2490 case 7: selectcmd
= shared
.select7
; break;
2491 case 8: selectcmd
= shared
.select8
; break;
2492 case 9: selectcmd
= shared
.select9
; break;
2494 selectcmd
= createObject(REDIS_STRING
,
2495 sdscatprintf(sdsempty(),"select %d\r\n",dictid
));
2496 selectcmd
->refcount
= 0;
2499 addReply(slave
,selectcmd
);
2500 slave
->slaveseldb
= dictid
;
2502 for (j
= 0; j
< outc
; j
++) addReply(slave
,outv
[j
]);
2504 for (j
= 0; j
< outc
; j
++) decrRefCount(outv
[j
]);
2505 if (outv
!= static_outv
) zfree(outv
);
2508 static sds
sdscatrepr(sds s
, char *p
, size_t len
) {
2509 s
= sdscatlen(s
,"\"",1);
2514 s
= sdscatprintf(s
,"\\%c",*p
);
2516 case '\n': s
= sdscatlen(s
,"\\n",1); break;
2517 case '\r': s
= sdscatlen(s
,"\\r",1); break;
2518 case '\t': s
= sdscatlen(s
,"\\t",1); break;
2519 case '\a': s
= sdscatlen(s
,"\\a",1); break;
2520 case '\b': s
= sdscatlen(s
,"\\b",1); break;
2523 s
= sdscatprintf(s
,"%c",*p
);
2525 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
2530 return sdscatlen(s
,"\"",1);
2533 static void replicationFeedMonitors(list
*monitors
, int dictid
, robj
**argv
, int argc
) {
2537 sds cmdrepr
= sdsnew("+");
2541 gettimeofday(&tv
,NULL
);
2542 cmdrepr
= sdscatprintf(cmdrepr
,"%ld.%ld ",(long)tv
.tv_sec
,(long)tv
.tv_usec
);
2543 if (dictid
!= 0) cmdrepr
= sdscatprintf(cmdrepr
,"(db %d) ", dictid
);
2545 for (j
= 0; j
< argc
; j
++) {
2546 if (argv
[j
]->encoding
== REDIS_ENCODING_INT
) {
2547 cmdrepr
= sdscatprintf(cmdrepr
, "%ld", (long)argv
[j
]->ptr
);
2549 cmdrepr
= sdscatrepr(cmdrepr
,(char*)argv
[j
]->ptr
,
2550 sdslen(argv
[j
]->ptr
));
2553 cmdrepr
= sdscatlen(cmdrepr
," ",1);
2555 cmdrepr
= sdscatlen(cmdrepr
,"\r\n",2);
2556 cmdobj
= createObject(REDIS_STRING
,cmdrepr
);
2558 listRewind(monitors
,&li
);
2559 while((ln
= listNext(&li
))) {
2560 redisClient
*monitor
= ln
->value
;
2561 addReply(monitor
,cmdobj
);
2563 decrRefCount(cmdobj
);
2566 static void processInputBuffer(redisClient
*c
) {
2568 /* Before to process the input buffer, make sure the client is not
2569 * waitig for a blocking operation such as BLPOP. Note that the first
2570 * iteration the client is never blocked, otherwise the processInputBuffer
2571 * would not be called at all, but after the execution of the first commands
2572 * in the input buffer the client may be blocked, and the "goto again"
2573 * will try to reiterate. The following line will make it return asap. */
2574 if (c
->flags
& REDIS_BLOCKED
|| c
->flags
& REDIS_IO_WAIT
) return;
2575 if (c
->bulklen
== -1) {
2576 /* Read the first line of the query */
2577 char *p
= strchr(c
->querybuf
,'\n');
2584 query
= c
->querybuf
;
2585 c
->querybuf
= sdsempty();
2586 querylen
= 1+(p
-(query
));
2587 if (sdslen(query
) > querylen
) {
2588 /* leave data after the first line of the query in the buffer */
2589 c
->querybuf
= sdscatlen(c
->querybuf
,query
+querylen
,sdslen(query
)-querylen
);
2591 *p
= '\0'; /* remove "\n" */
2592 if (*(p
-1) == '\r') *(p
-1) = '\0'; /* and "\r" if any */
2593 sdsupdatelen(query
);
2595 /* Now we can split the query in arguments */
2596 argv
= sdssplitlen(query
,sdslen(query
)," ",1,&argc
);
2599 if (c
->argv
) zfree(c
->argv
);
2600 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
2602 for (j
= 0; j
< argc
; j
++) {
2603 if (sdslen(argv
[j
])) {
2604 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
2612 /* Execute the command. If the client is still valid
2613 * after processCommand() return and there is something
2614 * on the query buffer try to process the next command. */
2615 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
2617 /* Nothing to process, argc == 0. Just process the query
2618 * buffer if it's not empty or return to the caller */
2619 if (sdslen(c
->querybuf
)) goto again
;
2622 } else if (sdslen(c
->querybuf
) >= REDIS_REQUEST_MAX_SIZE
) {
2623 redisLog(REDIS_VERBOSE
, "Client protocol error");
2628 /* Bulk read handling. Note that if we are at this point
2629 the client already sent a command terminated with a newline,
2630 we are reading the bulk data that is actually the last
2631 argument of the command. */
2632 int qbl
= sdslen(c
->querybuf
);
2634 if (c
->bulklen
<= qbl
) {
2635 /* Copy everything but the final CRLF as final argument */
2636 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
2638 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
2639 /* Process the command. If the client is still valid after
2640 * the processing and there is more data in the buffer
2641 * try to parse it. */
2642 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
2648 static void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
2649 redisClient
*c
= (redisClient
*) privdata
;
2650 char buf
[REDIS_IOBUF_LEN
];
2653 REDIS_NOTUSED(mask
);
2655 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
2657 if (errno
== EAGAIN
) {
2660 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
2664 } else if (nread
== 0) {
2665 redisLog(REDIS_VERBOSE
, "Client closed connection");
2670 c
->querybuf
= sdscatlen(c
->querybuf
, buf
, nread
);
2671 c
->lastinteraction
= time(NULL
);
2675 processInputBuffer(c
);
2678 static int selectDb(redisClient
*c
, int id
) {
2679 if (id
< 0 || id
>= server
.dbnum
)
2681 c
->db
= &server
.db
[id
];
2685 static void *dupClientReplyValue(void *o
) {
2686 incrRefCount((robj
*)o
);
2690 static int listMatchObjects(void *a
, void *b
) {
2691 return equalStringObjects(a
,b
);
2694 static redisClient
*createClient(int fd
) {
2695 redisClient
*c
= zmalloc(sizeof(*c
));
2697 anetNonBlock(NULL
,fd
);
2698 anetTcpNoDelay(NULL
,fd
);
2699 if (!c
) return NULL
;
2702 c
->querybuf
= sdsempty();
2711 c
->lastinteraction
= time(NULL
);
2712 c
->authenticated
= 0;
2713 c
->replstate
= REDIS_REPL_NONE
;
2714 c
->reply
= listCreate();
2715 listSetFreeMethod(c
->reply
,decrRefCount
);
2716 listSetDupMethod(c
->reply
,dupClientReplyValue
);
2717 c
->blockingkeys
= NULL
;
2718 c
->blockingkeysnum
= 0;
2719 c
->io_keys
= listCreate();
2720 listSetFreeMethod(c
->io_keys
,decrRefCount
);
2721 c
->pubsub_channels
= dictCreate(&setDictType
,NULL
);
2722 c
->pubsub_patterns
= listCreate();
2723 listSetFreeMethod(c
->pubsub_patterns
,decrRefCount
);
2724 listSetMatchMethod(c
->pubsub_patterns
,listMatchObjects
);
2725 if (aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
2726 readQueryFromClient
, c
) == AE_ERR
) {
2730 listAddNodeTail(server
.clients
,c
);
2731 initClientMultiState(c
);
2735 static void addReply(redisClient
*c
, robj
*obj
) {
2736 if (listLength(c
->reply
) == 0 &&
2737 (c
->replstate
== REDIS_REPL_NONE
||
2738 c
->replstate
== REDIS_REPL_ONLINE
) &&
2739 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
2740 sendReplyToClient
, c
) == AE_ERR
) return;
2742 if (server
.vm_enabled
&& obj
->storage
!= REDIS_VM_MEMORY
) {
2743 obj
= dupStringObject(obj
);
2744 obj
->refcount
= 0; /* getDecodedObject() will increment the refcount */
2746 listAddNodeTail(c
->reply
,getDecodedObject(obj
));
2749 static void addReplySds(redisClient
*c
, sds s
) {
2750 robj
*o
= createObject(REDIS_STRING
,s
);
2755 static void addReplyDouble(redisClient
*c
, double d
) {
2758 snprintf(buf
,sizeof(buf
),"%.17g",d
);
2759 addReplySds(c
,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
2760 (unsigned long) strlen(buf
),buf
));
2763 static void addReplyLongLong(redisClient
*c
, long long ll
) {
2768 addReply(c
,shared
.czero
);
2770 } else if (ll
== 1) {
2771 addReply(c
,shared
.cone
);
2775 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
2778 addReplySds(c
,sdsnewlen(buf
,len
+3));
2781 static void addReplyUlong(redisClient
*c
, unsigned long ul
) {
2786 addReply(c
,shared
.czero
);
2788 } else if (ul
== 1) {
2789 addReply(c
,shared
.cone
);
2792 len
= snprintf(buf
,sizeof(buf
),":%lu\r\n",ul
);
2793 addReplySds(c
,sdsnewlen(buf
,len
));
2796 static void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
2800 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
2801 len
= sdslen(obj
->ptr
);
2803 long n
= (long)obj
->ptr
;
2805 /* Compute how many bytes will take this integer as a radix 10 string */
2811 while((n
= n
/10) != 0) {
2816 intlen
= ll2string(buf
+1,sizeof(buf
)-1,(long long)len
);
2817 buf
[intlen
+1] = '\r';
2818 buf
[intlen
+2] = '\n';
2819 addReplySds(c
,sdsnewlen(buf
,intlen
+3));
2822 static void addReplyBulk(redisClient
*c
, robj
*obj
) {
2823 addReplyBulkLen(c
,obj
);
2825 addReply(c
,shared
.crlf
);
2828 /* In the CONFIG command we need to add vanilla C string as bulk replies */
2829 static void addReplyBulkCString(redisClient
*c
, char *s
) {
2831 addReply(c
,shared
.nullbulk
);
2833 robj
*o
= createStringObject(s
,strlen(s
));
2839 static void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
2844 REDIS_NOTUSED(mask
);
2845 REDIS_NOTUSED(privdata
);
2847 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
2848 if (cfd
== AE_ERR
) {
2849 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
2852 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
2853 if ((c
= createClient(cfd
)) == NULL
) {
2854 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
2855 close(cfd
); /* May be already closed, just ingore errors */
2858 /* If maxclient directive is set and this is one client more... close the
2859 * connection. Note that we create the client instead to check before
2860 * for this condition, since now the socket is already set in nonblocking
2861 * mode and we can send an error for free using the Kernel I/O */
2862 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
2863 char *err
= "-ERR max number of clients reached\r\n";
2865 /* That's a best effort error message, don't check write errors */
2866 if (write(c
->fd
,err
,strlen(err
)) == -1) {
2867 /* Nothing to do, Just to avoid the warning... */
2872 server
.stat_numconnections
++;
2875 /* ======================= Redis objects implementation ===================== */
2877 static robj
*createObject(int type
, void *ptr
) {
2880 if (server
.vm_enabled
) pthread_mutex_lock(&server
.obj_freelist_mutex
);
2881 if (listLength(server
.objfreelist
)) {
2882 listNode
*head
= listFirst(server
.objfreelist
);
2883 o
= listNodeValue(head
);
2884 listDelNode(server
.objfreelist
,head
);
2885 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
2887 if (server
.vm_enabled
) {
2888 pthread_mutex_unlock(&server
.obj_freelist_mutex
);
2889 o
= zmalloc(sizeof(*o
));
2891 o
= zmalloc(sizeof(*o
)-sizeof(struct redisObjectVM
));
2895 o
->encoding
= REDIS_ENCODING_RAW
;
2898 if (server
.vm_enabled
) {
2899 /* Note that this code may run in the context of an I/O thread
2900 * and accessing to server.unixtime in theory is an error
2901 * (no locks). But in practice this is safe, and even if we read
2902 * garbage Redis will not fail, as it's just a statistical info */
2903 o
->vm
.atime
= server
.unixtime
;
2904 o
->storage
= REDIS_VM_MEMORY
;
2909 static robj
*createStringObject(char *ptr
, size_t len
) {
2910 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
2913 static robj
*createStringObjectFromLongLong(long long value
) {
2915 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
2916 incrRefCount(shared
.integers
[value
]);
2917 o
= shared
.integers
[value
];
2919 o
= createObject(REDIS_STRING
, NULL
);
2920 if (value
>= LONG_MIN
&& value
<= LONG_MAX
) {
2921 o
->encoding
= REDIS_ENCODING_INT
;
2922 o
->ptr
= (void*)((long)value
);
2924 o
= createObject(REDIS_STRING
,sdsfromlonglong(value
));
2930 static robj
*dupStringObject(robj
*o
) {
2931 assert(o
->encoding
== REDIS_ENCODING_RAW
);
2932 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
2935 static robj
*createListObject(void) {
2936 list
*l
= listCreate();
2938 listSetFreeMethod(l
,decrRefCount
);
2939 return createObject(REDIS_LIST
,l
);
2942 static robj
*createSetObject(void) {
2943 dict
*d
= dictCreate(&setDictType
,NULL
);
2944 return createObject(REDIS_SET
,d
);
2947 static robj
*createHashObject(void) {
2948 /* All the Hashes start as zipmaps. Will be automatically converted
2949 * into hash tables if there are enough elements or big elements
2951 unsigned char *zm
= zipmapNew();
2952 robj
*o
= createObject(REDIS_HASH
,zm
);
2953 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
2957 static robj
*createZsetObject(void) {
2958 zset
*zs
= zmalloc(sizeof(*zs
));
2960 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
2961 zs
->zsl
= zslCreate();
2962 return createObject(REDIS_ZSET
,zs
);
2965 static void freeStringObject(robj
*o
) {
2966 if (o
->encoding
== REDIS_ENCODING_RAW
) {
2971 static void freeListObject(robj
*o
) {
2972 listRelease((list
*) o
->ptr
);
2975 static void freeSetObject(robj
*o
) {
2976 dictRelease((dict
*) o
->ptr
);
2979 static void freeZsetObject(robj
*o
) {
2982 dictRelease(zs
->dict
);
2987 static void freeHashObject(robj
*o
) {
2988 switch (o
->encoding
) {
2989 case REDIS_ENCODING_HT
:
2990 dictRelease((dict
*) o
->ptr
);
2992 case REDIS_ENCODING_ZIPMAP
:
2996 redisPanic("Unknown hash encoding type");
3001 static void incrRefCount(robj
*o
) {
3005 static void decrRefCount(void *obj
) {
3008 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
3009 /* Object is a key of a swapped out value, or in the process of being
3011 if (server
.vm_enabled
&&
3012 (o
->storage
== REDIS_VM_SWAPPED
|| o
->storage
== REDIS_VM_LOADING
))
3014 if (o
->storage
== REDIS_VM_LOADING
) vmCancelThreadedIOJob(obj
);
3015 redisAssert(o
->type
== REDIS_STRING
);
3016 freeStringObject(o
);
3017 vmMarkPagesFree(o
->vm
.page
,o
->vm
.usedpages
);
3018 pthread_mutex_lock(&server
.obj_freelist_mutex
);
3019 if (listLength(server
.objfreelist
) > REDIS_OBJFREELIST_MAX
||
3020 !listAddNodeHead(server
.objfreelist
,o
))
3022 pthread_mutex_unlock(&server
.obj_freelist_mutex
);
3023 server
.vm_stats_swapped_objects
--;
3026 /* Object is in memory, or in the process of being swapped out. */
3027 if (--(o
->refcount
) == 0) {
3028 if (server
.vm_enabled
&& o
->storage
== REDIS_VM_SWAPPING
)
3029 vmCancelThreadedIOJob(obj
);
3031 case REDIS_STRING
: freeStringObject(o
); break;
3032 case REDIS_LIST
: freeListObject(o
); break;
3033 case REDIS_SET
: freeSetObject(o
); break;
3034 case REDIS_ZSET
: freeZsetObject(o
); break;
3035 case REDIS_HASH
: freeHashObject(o
); break;
3036 default: redisPanic("Unknown object type"); break;
3038 if (server
.vm_enabled
) pthread_mutex_lock(&server
.obj_freelist_mutex
);
3039 if (listLength(server
.objfreelist
) > REDIS_OBJFREELIST_MAX
||
3040 !listAddNodeHead(server
.objfreelist
,o
))
3042 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
3046 static robj
*lookupKey(redisDb
*db
, robj
*key
) {
3047 dictEntry
*de
= dictFind(db
->dict
,key
);
3049 robj
*key
= dictGetEntryKey(de
);
3050 robj
*val
= dictGetEntryVal(de
);
3052 if (server
.vm_enabled
) {
3053 if (key
->storage
== REDIS_VM_MEMORY
||
3054 key
->storage
== REDIS_VM_SWAPPING
)
3056 /* If we were swapping the object out, stop it, this key
3058 if (key
->storage
== REDIS_VM_SWAPPING
)
3059 vmCancelThreadedIOJob(key
);
3060 /* Update the access time of the key for the aging algorithm. */
3061 key
->vm
.atime
= server
.unixtime
;
3063 int notify
= (key
->storage
== REDIS_VM_LOADING
);
3065 /* Our value was swapped on disk. Bring it at home. */
3066 redisAssert(val
== NULL
);
3067 val
= vmLoadObject(key
);
3068 dictGetEntryVal(de
) = val
;
3070 /* Clients blocked by the VM subsystem may be waiting for
3072 if (notify
) handleClientsBlockedOnSwappedKey(db
,key
);
3081 static robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
3082 expireIfNeeded(db
,key
);
3083 return lookupKey(db
,key
);
3086 static robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
3087 deleteIfVolatile(db
,key
);
3088 return lookupKey(db
,key
);
3091 static robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
3092 robj
*o
= lookupKeyRead(c
->db
, key
);
3093 if (!o
) addReply(c
,reply
);
3097 static robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
3098 robj
*o
= lookupKeyWrite(c
->db
, key
);
3099 if (!o
) addReply(c
,reply
);
3103 static int checkType(redisClient
*c
, robj
*o
, int type
) {
3104 if (o
->type
!= type
) {
3105 addReply(c
,shared
.wrongtypeerr
);
3111 static int deleteKey(redisDb
*db
, robj
*key
) {
3114 /* We need to protect key from destruction: after the first dictDelete()
3115 * it may happen that 'key' is no longer valid if we don't increment
3116 * it's count. This may happen when we get the object reference directly
3117 * from the hash table with dictRandomKey() or dict iterators */
3119 if (dictSize(db
->expires
)) dictDelete(db
->expires
,key
);
3120 retval
= dictDelete(db
->dict
,key
);
3123 return retval
== DICT_OK
;
3126 /* Check if the nul-terminated string 's' can be represented by a long
3127 * (that is, is a number that fits into long without any other space or
3128 * character before or after the digits).
3130 * If so, the function returns REDIS_OK and *longval is set to the value
3131 * of the number. Otherwise REDIS_ERR is returned */
3132 static int isStringRepresentableAsLong(sds s
, long *longval
) {
3133 char buf
[32], *endptr
;
3137 value
= strtol(s
, &endptr
, 10);
3138 if (endptr
[0] != '\0') return REDIS_ERR
;
3139 slen
= ll2string(buf
,32,value
);
3141 /* If the number converted back into a string is not identical
3142 * then it's not possible to encode the string as integer */
3143 if (sdslen(s
) != (unsigned)slen
|| memcmp(buf
,s
,slen
)) return REDIS_ERR
;
3144 if (longval
) *longval
= value
;
3148 /* Try to encode a string object in order to save space */
3149 static robj
*tryObjectEncoding(robj
*o
) {
3153 if (o
->encoding
!= REDIS_ENCODING_RAW
)
3154 return o
; /* Already encoded */
3156 /* It's not safe to encode shared objects: shared objects can be shared
3157 * everywhere in the "object space" of Redis. Encoded objects can only
3158 * appear as "values" (and not, for instance, as keys) */
3159 if (o
->refcount
> 1) return o
;
3161 /* Currently we try to encode only strings */
3162 redisAssert(o
->type
== REDIS_STRING
);
3164 /* Check if we can represent this string as a long integer */
3165 if (isStringRepresentableAsLong(s
,&value
) == REDIS_ERR
) return o
;
3167 /* Ok, this object can be encoded */
3168 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
3170 incrRefCount(shared
.integers
[value
]);
3171 return shared
.integers
[value
];
3173 o
->encoding
= REDIS_ENCODING_INT
;
3175 o
->ptr
= (void*) value
;
3180 /* Get a decoded version of an encoded object (returned as a new object).
3181 * If the object is already raw-encoded just increment the ref count. */
3182 static robj
*getDecodedObject(robj
*o
) {
3185 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3189 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
3192 ll2string(buf
,32,(long)o
->ptr
);
3193 dec
= createStringObject(buf
,strlen(buf
));
3196 redisPanic("Unknown encoding type");
3200 /* Compare two string objects via strcmp() or alike.
3201 * Note that the objects may be integer-encoded. In such a case we
3202 * use ll2string() to get a string representation of the numbers on the stack
3203 * and compare the strings, it's much faster than calling getDecodedObject().
3205 * Important note: if objects are not integer encoded, but binary-safe strings,
3206 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
3208 static int compareStringObjects(robj
*a
, robj
*b
) {
3209 redisAssert(a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
3210 char bufa
[128], bufb
[128], *astr
, *bstr
;
3213 if (a
== b
) return 0;
3214 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
3215 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
3221 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
3222 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
3228 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
3231 /* Equal string objects return 1 if the two objects are the same from the
3232 * point of view of a string comparison, otherwise 0 is returned. Note that
3233 * this function is faster then checking for (compareStringObject(a,b) == 0)
3234 * because it can perform some more optimization. */
3235 static int equalStringObjects(robj
*a
, robj
*b
) {
3236 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
3237 return a
->ptr
== b
->ptr
;
3239 return compareStringObjects(a
,b
) == 0;
3243 static size_t stringObjectLen(robj
*o
) {
3244 redisAssert(o
->type
== REDIS_STRING
);
3245 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3246 return sdslen(o
->ptr
);
3250 return ll2string(buf
,32,(long)o
->ptr
);
3254 static int getDoubleFromObject(robj
*o
, double *target
) {
3261 redisAssert(o
->type
== REDIS_STRING
);
3262 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3263 value
= strtod(o
->ptr
, &eptr
);
3264 if (eptr
[0] != '\0') return REDIS_ERR
;
3265 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
3266 value
= (long)o
->ptr
;
3268 redisPanic("Unknown string encoding");
3276 static int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
3278 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
3280 addReplySds(c
, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg
));
3282 addReplySds(c
, sdsnew("-ERR value is not a double\r\n"));
3291 static int getLongLongFromObject(robj
*o
, long long *target
) {
3298 redisAssert(o
->type
== REDIS_STRING
);
3299 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3300 value
= strtoll(o
->ptr
, &eptr
, 10);
3301 if (eptr
[0] != '\0') return REDIS_ERR
;
3302 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
3303 value
= (long)o
->ptr
;
3305 redisPanic("Unknown string encoding");
3313 static int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
) {
3315 if (getLongLongFromObject(o
, &value
) != REDIS_OK
) {
3317 addReplySds(c
, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg
));
3319 addReplySds(c
, sdsnew("-ERR value is not an integer\r\n"));
3328 static int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
3331 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
3332 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
3334 addReplySds(c
, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg
));
3336 addReplySds(c
, sdsnew("-ERR value is out of range\r\n"));
3345 /*============================ RDB saving/loading =========================== */
3347 static int rdbSaveType(FILE *fp
, unsigned char type
) {
3348 if (fwrite(&type
,1,1,fp
) == 0) return -1;
3352 static int rdbSaveTime(FILE *fp
, time_t t
) {
3353 int32_t t32
= (int32_t) t
;
3354 if (fwrite(&t32
,4,1,fp
) == 0) return -1;
3358 /* check rdbLoadLen() comments for more info */
3359 static int rdbSaveLen(FILE *fp
, uint32_t len
) {
3360 unsigned char buf
[2];
3363 /* Save a 6 bit len */
3364 buf
[0] = (len
&0xFF)|(REDIS_RDB_6BITLEN
<<6);
3365 if (fwrite(buf
,1,1,fp
) == 0) return -1;
3366 } else if (len
< (1<<14)) {
3367 /* Save a 14 bit len */
3368 buf
[0] = ((len
>>8)&0xFF)|(REDIS_RDB_14BITLEN
<<6);
3370 if (fwrite(buf
,2,1,fp
) == 0) return -1;
3372 /* Save a 32 bit len */
3373 buf
[0] = (REDIS_RDB_32BITLEN
<<6);
3374 if (fwrite(buf
,1,1,fp
) == 0) return -1;
3376 if (fwrite(&len
,4,1,fp
) == 0) return -1;
3381 /* Encode 'value' as an integer if possible (if integer will fit the
3382 * supported range). If the function sucessful encoded the integer
3383 * then the (up to 5 bytes) encoded representation is written in the
3384 * string pointed by 'enc' and the length is returned. Otherwise
3386 static int rdbEncodeInteger(long long value
, unsigned char *enc
) {
3387 /* Finally check if it fits in our ranges */
3388 if (value
>= -(1<<7) && value
<= (1<<7)-1) {
3389 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT8
;
3390 enc
[1] = value
&0xFF;
3392 } else if (value
>= -(1<<15) && value
<= (1<<15)-1) {
3393 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT16
;
3394 enc
[1] = value
&0xFF;
3395 enc
[2] = (value
>>8)&0xFF;
3397 } else if (value
>= -((long long)1<<31) && value
<= ((long long)1<<31)-1) {
3398 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT32
;
3399 enc
[1] = value
&0xFF;
3400 enc
[2] = (value
>>8)&0xFF;
3401 enc
[3] = (value
>>16)&0xFF;
3402 enc
[4] = (value
>>24)&0xFF;
3409 /* String objects in the form "2391" "-100" without any space and with a
3410 * range of values that can fit in an 8, 16 or 32 bit signed value can be
3411 * encoded as integers to save space */
3412 static int rdbTryIntegerEncoding(char *s
, size_t len
, unsigned char *enc
) {
3414 char *endptr
, buf
[32];
3416 /* Check if it's possible to encode this value as a number */
3417 value
= strtoll(s
, &endptr
, 10);
3418 if (endptr
[0] != '\0') return 0;
3419 ll2string(buf
,32,value
);
3421 /* If the number converted back into a string is not identical
3422 * then it's not possible to encode the string as integer */
3423 if (strlen(buf
) != len
|| memcmp(buf
,s
,len
)) return 0;
3425 return rdbEncodeInteger(value
,enc
);
3428 static int rdbSaveLzfStringObject(FILE *fp
, unsigned char *s
, size_t len
) {
3429 size_t comprlen
, outlen
;
3433 /* We require at least four bytes compression for this to be worth it */
3434 if (len
<= 4) return 0;
3436 if ((out
= zmalloc(outlen
+1)) == NULL
) return 0;
3437 comprlen
= lzf_compress(s
, len
, out
, outlen
);
3438 if (comprlen
== 0) {
3442 /* Data compressed! Let's save it on disk */
3443 byte
= (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_LZF
;
3444 if (fwrite(&byte
,1,1,fp
) == 0) goto writeerr
;
3445 if (rdbSaveLen(fp
,comprlen
) == -1) goto writeerr
;
3446 if (rdbSaveLen(fp
,len
) == -1) goto writeerr
;
3447 if (fwrite(out
,comprlen
,1,fp
) == 0) goto writeerr
;
3456 /* Save a string objet as [len][data] on disk. If the object is a string
3457 * representation of an integer value we try to safe it in a special form */
3458 static int rdbSaveRawString(FILE *fp
, unsigned char *s
, size_t len
) {
3461 /* Try integer encoding */
3463 unsigned char buf
[5];
3464 if ((enclen
= rdbTryIntegerEncoding((char*)s
,len
,buf
)) > 0) {
3465 if (fwrite(buf
,enclen
,1,fp
) == 0) return -1;
3470 /* Try LZF compression - under 20 bytes it's unable to compress even
3471 * aaaaaaaaaaaaaaaaaa so skip it */
3472 if (server
.rdbcompression
&& len
> 20) {
3475 retval
= rdbSaveLzfStringObject(fp
,s
,len
);
3476 if (retval
== -1) return -1;
3477 if (retval
> 0) return 0;
3478 /* retval == 0 means data can't be compressed, save the old way */
3481 /* Store verbatim */
3482 if (rdbSaveLen(fp
,len
) == -1) return -1;
3483 if (len
&& fwrite(s
,len
,1,fp
) == 0) return -1;
3487 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
3488 static int rdbSaveStringObject(FILE *fp
, robj
*obj
) {
3491 /* Avoid to decode the object, then encode it again, if the
3492 * object is alrady integer encoded. */
3493 if (obj
->encoding
== REDIS_ENCODING_INT
) {
3494 long val
= (long) obj
->ptr
;
3495 unsigned char buf
[5];
3498 if ((enclen
= rdbEncodeInteger(val
,buf
)) > 0) {
3499 if (fwrite(buf
,enclen
,1,fp
) == 0) return -1;
3502 /* otherwise... fall throught and continue with the usual
3506 /* Avoid incr/decr ref count business when possible.
3507 * This plays well with copy-on-write given that we are probably
3508 * in a child process (BGSAVE). Also this makes sure key objects
3509 * of swapped objects are not incRefCount-ed (an assert does not allow
3510 * this in order to avoid bugs) */
3511 if (obj
->encoding
!= REDIS_ENCODING_RAW
) {
3512 obj
= getDecodedObject(obj
);
3513 retval
= rdbSaveRawString(fp
,obj
->ptr
,sdslen(obj
->ptr
));
3516 retval
= rdbSaveRawString(fp
,obj
->ptr
,sdslen(obj
->ptr
));
3521 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
3522 * 8 bit integer specifing the length of the representation.
3523 * This 8 bit integer has special values in order to specify the following
3529 static int rdbSaveDoubleValue(FILE *fp
, double val
) {
3530 unsigned char buf
[128];
3536 } else if (!isfinite(val
)) {
3538 buf
[0] = (val
< 0) ? 255 : 254;
3540 #if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
3541 /* Check if the float is in a safe range to be casted into a
3542 * long long. We are assuming that long long is 64 bit here.
3543 * Also we are assuming that there are no implementations around where
3544 * double has precision < 52 bit.
3546 * Under this assumptions we test if a double is inside an interval
3547 * where casting to long long is safe. Then using two castings we
3548 * make sure the decimal part is zero. If all this is true we use
3549 * integer printing function that is much faster. */
3550 double min
= -4503599627370495; /* (2^52)-1 */
3551 double max
= 4503599627370496; /* -(2^52) */
3552 if (val
> min
&& val
< max
&& val
== ((double)((long long)val
)))
3553 ll2string((char*)buf
+1,sizeof(buf
),(long long)val
);
3556 snprintf((char*)buf
+1,sizeof(buf
)-1,"%.17g",val
);
3557 buf
[0] = strlen((char*)buf
+1);
3560 if (fwrite(buf
,len
,1,fp
) == 0) return -1;
3564 /* Save a Redis object. */
3565 static int rdbSaveObject(FILE *fp
, robj
*o
) {
3566 if (o
->type
== REDIS_STRING
) {
3567 /* Save a string value */
3568 if (rdbSaveStringObject(fp
,o
) == -1) return -1;
3569 } else if (o
->type
== REDIS_LIST
) {
3570 /* Save a list value */
3571 list
*list
= o
->ptr
;
3575 if (rdbSaveLen(fp
,listLength(list
)) == -1) return -1;
3576 listRewind(list
,&li
);
3577 while((ln
= listNext(&li
))) {
3578 robj
*eleobj
= listNodeValue(ln
);
3580 if (rdbSaveStringObject(fp
,eleobj
) == -1) return -1;
3582 } else if (o
->type
== REDIS_SET
) {
3583 /* Save a set value */
3585 dictIterator
*di
= dictGetIterator(set
);
3588 if (rdbSaveLen(fp
,dictSize(set
)) == -1) return -1;
3589 while((de
= dictNext(di
)) != NULL
) {
3590 robj
*eleobj
= dictGetEntryKey(de
);
3592 if (rdbSaveStringObject(fp
,eleobj
) == -1) return -1;
3594 dictReleaseIterator(di
);
3595 } else if (o
->type
== REDIS_ZSET
) {
3596 /* Save a set value */
3598 dictIterator
*di
= dictGetIterator(zs
->dict
);
3601 if (rdbSaveLen(fp
,dictSize(zs
->dict
)) == -1) return -1;
3602 while((de
= dictNext(di
)) != NULL
) {
3603 robj
*eleobj
= dictGetEntryKey(de
);
3604 double *score
= dictGetEntryVal(de
);
3606 if (rdbSaveStringObject(fp
,eleobj
) == -1) return -1;
3607 if (rdbSaveDoubleValue(fp
,*score
) == -1) return -1;
3609 dictReleaseIterator(di
);
3610 } else if (o
->type
== REDIS_HASH
) {
3611 /* Save a hash value */
3612 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
3613 unsigned char *p
= zipmapRewind(o
->ptr
);
3614 unsigned int count
= zipmapLen(o
->ptr
);
3615 unsigned char *key
, *val
;
3616 unsigned int klen
, vlen
;
3618 if (rdbSaveLen(fp
,count
) == -1) return -1;
3619 while((p
= zipmapNext(p
,&key
,&klen
,&val
,&vlen
)) != NULL
) {
3620 if (rdbSaveRawString(fp
,key
,klen
) == -1) return -1;
3621 if (rdbSaveRawString(fp
,val
,vlen
) == -1) return -1;
3624 dictIterator
*di
= dictGetIterator(o
->ptr
);
3627 if (rdbSaveLen(fp
,dictSize((dict
*)o
->ptr
)) == -1) return -1;
3628 while((de
= dictNext(di
)) != NULL
) {
3629 robj
*key
= dictGetEntryKey(de
);
3630 robj
*val
= dictGetEntryVal(de
);
3632 if (rdbSaveStringObject(fp
,key
) == -1) return -1;
3633 if (rdbSaveStringObject(fp
,val
) == -1) return -1;
3635 dictReleaseIterator(di
);
3638 redisPanic("Unknown object type");
3643 /* Return the length the object will have on disk if saved with
3644 * the rdbSaveObject() function. Currently we use a trick to get
3645 * this length with very little changes to the code. In the future
3646 * we could switch to a faster solution. */
3647 static off_t
rdbSavedObjectLen(robj
*o
, FILE *fp
) {
3648 if (fp
== NULL
) fp
= server
.devnull
;
3650 assert(rdbSaveObject(fp
,o
) != 1);
3654 /* Return the number of pages required to save this object in the swap file */
3655 static off_t
rdbSavedObjectPages(robj
*o
, FILE *fp
) {
3656 off_t bytes
= rdbSavedObjectLen(o
,fp
);
3658 return (bytes
+(server
.vm_page_size
-1))/server
.vm_page_size
;
3661 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
3662 static int rdbSave(char *filename
) {
3663 dictIterator
*di
= NULL
;
3668 time_t now
= time(NULL
);
3670 /* Wait for I/O therads to terminate, just in case this is a
3671 * foreground-saving, to avoid seeking the swap file descriptor at the
3673 if (server
.vm_enabled
)
3674 waitEmptyIOJobsQueue();
3676 snprintf(tmpfile
,256,"temp-%d.rdb", (int) getpid());
3677 fp
= fopen(tmpfile
,"w");
3679 redisLog(REDIS_WARNING
, "Failed saving the DB: %s", strerror(errno
));
3682 if (fwrite("REDIS0001",9,1,fp
) == 0) goto werr
;
3683 for (j
= 0; j
< server
.dbnum
; j
++) {
3684 redisDb
*db
= server
.db
+j
;
3686 if (dictSize(d
) == 0) continue;
3687 di
= dictGetIterator(d
);
3693 /* Write the SELECT DB opcode */
3694 if (rdbSaveType(fp
,REDIS_SELECTDB
) == -1) goto werr
;
3695 if (rdbSaveLen(fp
,j
) == -1) goto werr
;
3697 /* Iterate this DB writing every entry */
3698 while((de
= dictNext(di
)) != NULL
) {
3699 robj
*key
= dictGetEntryKey(de
);
3700 robj
*o
= dictGetEntryVal(de
);
3701 time_t expiretime
= getExpire(db
,key
);
3703 /* Save the expire time */
3704 if (expiretime
!= -1) {
3705 /* If this key is already expired skip it */
3706 if (expiretime
< now
) continue;
3707 if (rdbSaveType(fp
,REDIS_EXPIRETIME
) == -1) goto werr
;
3708 if (rdbSaveTime(fp
,expiretime
) == -1) goto werr
;
3710 /* Save the key and associated value. This requires special
3711 * handling if the value is swapped out. */
3712 if (!server
.vm_enabled
|| key
->storage
== REDIS_VM_MEMORY
||
3713 key
->storage
== REDIS_VM_SWAPPING
) {
3714 /* Save type, key, value */
3715 if (rdbSaveType(fp
,o
->type
) == -1) goto werr
;
3716 if (rdbSaveStringObject(fp
,key
) == -1) goto werr
;
3717 if (rdbSaveObject(fp
,o
) == -1) goto werr
;
3719 /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
3721 /* Get a preview of the object in memory */
3722 po
= vmPreviewObject(key
);
3723 /* Save type, key, value */
3724 if (rdbSaveType(fp
,key
->vtype
) == -1) goto werr
;
3725 if (rdbSaveStringObject(fp
,key
) == -1) goto werr
;
3726 if (rdbSaveObject(fp
,po
) == -1) goto werr
;
3727 /* Remove the loaded object from memory */
3731 dictReleaseIterator(di
);
3734 if (rdbSaveType(fp
,REDIS_EOF
) == -1) goto werr
;
3736 /* Make sure data will not remain on the OS's output buffers */
3741 /* Use RENAME to make sure the DB file is changed atomically only
3742 * if the generate DB file is ok. */
3743 if (rename(tmpfile
,filename
) == -1) {
3744 redisLog(REDIS_WARNING
,"Error moving temp DB file on the final destination: %s", strerror(errno
));
3748 redisLog(REDIS_NOTICE
,"DB saved on disk");
3750 server
.lastsave
= time(NULL
);
3756 redisLog(REDIS_WARNING
,"Write error saving DB on disk: %s", strerror(errno
));
3757 if (di
) dictReleaseIterator(di
);
3761 static int rdbSaveBackground(char *filename
) {
3764 if (server
.bgsavechildpid
!= -1) return REDIS_ERR
;
3765 if (server
.vm_enabled
) waitEmptyIOJobsQueue();
3766 if ((childpid
= fork()) == 0) {
3768 if (server
.vm_enabled
) vmReopenSwapFile();
3770 if (rdbSave(filename
) == REDIS_OK
) {
3777 if (childpid
== -1) {
3778 redisLog(REDIS_WARNING
,"Can't save in background: fork: %s",
3782 redisLog(REDIS_NOTICE
,"Background saving started by pid %d",childpid
);
3783 server
.bgsavechildpid
= childpid
;
3784 updateDictResizePolicy();
3787 return REDIS_OK
; /* unreached */
3790 static void rdbRemoveTempFile(pid_t childpid
) {
3793 snprintf(tmpfile
,256,"temp-%d.rdb", (int) childpid
);
3797 static int rdbLoadType(FILE *fp
) {
3799 if (fread(&type
,1,1,fp
) == 0) return -1;
3803 static time_t rdbLoadTime(FILE *fp
) {
3805 if (fread(&t32
,4,1,fp
) == 0) return -1;
3806 return (time_t) t32
;
3809 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
3810 * of this file for a description of how this are stored on disk.
3812 * isencoded is set to 1 if the readed length is not actually a length but
3813 * an "encoding type", check the above comments for more info */
3814 static uint32_t rdbLoadLen(FILE *fp
, int *isencoded
) {
3815 unsigned char buf
[2];
3819 if (isencoded
) *isencoded
= 0;
3820 if (fread(buf
,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
3821 type
= (buf
[0]&0xC0)>>6;
3822 if (type
== REDIS_RDB_6BITLEN
) {
3823 /* Read a 6 bit len */
3825 } else if (type
== REDIS_RDB_ENCVAL
) {
3826 /* Read a 6 bit len encoding type */
3827 if (isencoded
) *isencoded
= 1;
3829 } else if (type
== REDIS_RDB_14BITLEN
) {
3830 /* Read a 14 bit len */
3831 if (fread(buf
+1,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
3832 return ((buf
[0]&0x3F)<<8)|buf
[1];
3834 /* Read a 32 bit len */
3835 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
3840 /* Load an integer-encoded object from file 'fp', with the specified
3841 * encoding type 'enctype'. If encode is true the function may return
3842 * an integer-encoded object as reply, otherwise the returned object
3843 * will always be encoded as a raw string. */
3844 static robj
*rdbLoadIntegerObject(FILE *fp
, int enctype
, int encode
) {
3845 unsigned char enc
[4];
3848 if (enctype
== REDIS_RDB_ENC_INT8
) {
3849 if (fread(enc
,1,1,fp
) == 0) return NULL
;
3850 val
= (signed char)enc
[0];
3851 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
3853 if (fread(enc
,2,1,fp
) == 0) return NULL
;
3854 v
= enc
[0]|(enc
[1]<<8);
3856 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
3858 if (fread(enc
,4,1,fp
) == 0) return NULL
;
3859 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
3862 val
= 0; /* anti-warning */
3863 redisPanic("Unknown RDB integer encoding type");
3866 return createStringObjectFromLongLong(val
);
3868 return createObject(REDIS_STRING
,sdsfromlonglong(val
));
3871 static robj
*rdbLoadLzfStringObject(FILE*fp
) {
3872 unsigned int len
, clen
;
3873 unsigned char *c
= NULL
;
3876 if ((clen
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
3877 if ((len
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
3878 if ((c
= zmalloc(clen
)) == NULL
) goto err
;
3879 if ((val
= sdsnewlen(NULL
,len
)) == NULL
) goto err
;
3880 if (fread(c
,clen
,1,fp
) == 0) goto err
;
3881 if (lzf_decompress(c
,clen
,val
,len
) == 0) goto err
;
3883 return createObject(REDIS_STRING
,val
);
3890 static robj
*rdbGenericLoadStringObject(FILE*fp
, int encode
) {
3895 len
= rdbLoadLen(fp
,&isencoded
);
3898 case REDIS_RDB_ENC_INT8
:
3899 case REDIS_RDB_ENC_INT16
:
3900 case REDIS_RDB_ENC_INT32
:
3901 return rdbLoadIntegerObject(fp
,len
,encode
);
3902 case REDIS_RDB_ENC_LZF
:
3903 return rdbLoadLzfStringObject(fp
);
3905 redisPanic("Unknown RDB encoding type");
3909 if (len
== REDIS_RDB_LENERR
) return NULL
;
3910 val
= sdsnewlen(NULL
,len
);
3911 if (len
&& fread(val
,len
,1,fp
) == 0) {
3915 return createObject(REDIS_STRING
,val
);
3918 static robj
*rdbLoadStringObject(FILE *fp
) {
3919 return rdbGenericLoadStringObject(fp
,0);
3922 static robj
*rdbLoadEncodedStringObject(FILE *fp
) {
3923 return rdbGenericLoadStringObject(fp
,1);
3926 /* For information about double serialization check rdbSaveDoubleValue() */
3927 static int rdbLoadDoubleValue(FILE *fp
, double *val
) {
3931 if (fread(&len
,1,1,fp
) == 0) return -1;
3933 case 255: *val
= R_NegInf
; return 0;
3934 case 254: *val
= R_PosInf
; return 0;
3935 case 253: *val
= R_Nan
; return 0;
3937 if (fread(buf
,len
,1,fp
) == 0) return -1;
3939 sscanf(buf
, "%lg", val
);
3944 /* Load a Redis object of the specified type from the specified file.
3945 * On success a newly allocated object is returned, otherwise NULL. */
3946 static robj
*rdbLoadObject(int type
, FILE *fp
) {
3949 redisLog(REDIS_DEBUG
,"LOADING OBJECT %d (at %d)\n",type
,ftell(fp
));
3950 if (type
== REDIS_STRING
) {
3951 /* Read string value */
3952 if ((o
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
3953 o
= tryObjectEncoding(o
);
3954 } else if (type
== REDIS_LIST
|| type
== REDIS_SET
) {
3955 /* Read list/set value */
3958 if ((listlen
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
3959 o
= (type
== REDIS_LIST
) ? createListObject() : createSetObject();
3960 /* It's faster to expand the dict to the right size asap in order
3961 * to avoid rehashing */
3962 if (type
== REDIS_SET
&& listlen
> DICT_HT_INITIAL_SIZE
)
3963 dictExpand(o
->ptr
,listlen
);
3964 /* Load every single element of the list/set */
3968 if ((ele
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
3969 ele
= tryObjectEncoding(ele
);
3970 if (type
== REDIS_LIST
) {
3971 listAddNodeTail((list
*)o
->ptr
,ele
);
3973 dictAdd((dict
*)o
->ptr
,ele
,NULL
);
3976 } else if (type
== REDIS_ZSET
) {
3977 /* Read list/set value */
3981 if ((zsetlen
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
3982 o
= createZsetObject();
3984 /* Load every single element of the list/set */
3987 double *score
= zmalloc(sizeof(double));
3989 if ((ele
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
3990 ele
= tryObjectEncoding(ele
);
3991 if (rdbLoadDoubleValue(fp
,score
) == -1) return NULL
;
3992 dictAdd(zs
->dict
,ele
,score
);
3993 zslInsert(zs
->zsl
,*score
,ele
);
3994 incrRefCount(ele
); /* added to skiplist */
3996 } else if (type
== REDIS_HASH
) {
3999 if ((hashlen
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
4000 o
= createHashObject();
4001 /* Too many entries? Use an hash table. */
4002 if (hashlen
> server
.hash_max_zipmap_entries
)
4003 convertToRealHash(o
);
4004 /* Load every key/value, then set it into the zipmap or hash
4005 * table, as needed. */
4009 if ((key
= rdbLoadStringObject(fp
)) == NULL
) return NULL
;
4010 if ((val
= rdbLoadStringObject(fp
)) == NULL
) return NULL
;
4011 /* If we are using a zipmap and there are too big values
4012 * the object is converted to real hash table encoding. */
4013 if (o
->encoding
!= REDIS_ENCODING_HT
&&
4014 (sdslen(key
->ptr
) > server
.hash_max_zipmap_value
||
4015 sdslen(val
->ptr
) > server
.hash_max_zipmap_value
))
4017 convertToRealHash(o
);
4020 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
4021 unsigned char *zm
= o
->ptr
;
4023 zm
= zipmapSet(zm
,key
->ptr
,sdslen(key
->ptr
),
4024 val
->ptr
,sdslen(val
->ptr
),NULL
);
4029 key
= tryObjectEncoding(key
);
4030 val
= tryObjectEncoding(val
);
4031 dictAdd((dict
*)o
->ptr
,key
,val
);
4035 redisPanic("Unknown object type");
4040 static int rdbLoad(char *filename
) {
4043 int type
, retval
, rdbver
;
4044 int swap_all_values
= 0;
4045 dict
*d
= server
.db
[0].dict
;
4046 redisDb
*db
= server
.db
+0;
4048 time_t expiretime
, now
= time(NULL
);
4049 long long loadedkeys
= 0;
4051 fp
= fopen(filename
,"r");
4052 if (!fp
) return REDIS_ERR
;
4053 if (fread(buf
,9,1,fp
) == 0) goto eoferr
;
4055 if (memcmp(buf
,"REDIS",5) != 0) {
4057 redisLog(REDIS_WARNING
,"Wrong signature trying to load DB from file");
4060 rdbver
= atoi(buf
+5);
4063 redisLog(REDIS_WARNING
,"Can't handle RDB format version %d",rdbver
);
4071 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
4072 if (type
== REDIS_EXPIRETIME
) {
4073 if ((expiretime
= rdbLoadTime(fp
)) == -1) goto eoferr
;
4074 /* We read the time so we need to read the object type again */
4075 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
4077 if (type
== REDIS_EOF
) break;
4078 /* Handle SELECT DB opcode as a special case */
4079 if (type
== REDIS_SELECTDB
) {
4080 if ((dbid
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
)
4082 if (dbid
>= (unsigned)server
.dbnum
) {
4083 redisLog(REDIS_WARNING
,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server
.dbnum
);
4086 db
= server
.db
+dbid
;
4091 if ((key
= rdbLoadStringObject(fp
)) == NULL
) goto eoferr
;
4093 if ((val
= rdbLoadObject(type
,fp
)) == NULL
) goto eoferr
;
4094 /* Check if the key already expired */
4095 if (expiretime
!= -1 && expiretime
< now
) {
4100 /* Add the new object in the hash table */
4101 retval
= dictAdd(d
,key
,val
);
4102 if (retval
== DICT_ERR
) {
4103 redisLog(REDIS_WARNING
,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key
->ptr
);
4107 /* Set the expire time if needed */
4108 if (expiretime
!= -1) setExpire(db
,key
,expiretime
);
4110 /* Handle swapping while loading big datasets when VM is on */
4112 /* If we detecter we are hopeless about fitting something in memory
4113 * we just swap every new key on disk. Directly...
4114 * Note that's important to check for this condition before resorting
4115 * to random sampling, otherwise we may try to swap already
4117 if (swap_all_values
) {
4118 dictEntry
*de
= dictFind(d
,key
);
4120 /* de may be NULL since the key already expired */
4122 key
= dictGetEntryKey(de
);
4123 val
= dictGetEntryVal(de
);
4125 if (vmSwapObjectBlocking(key
,val
) == REDIS_OK
) {
4126 dictGetEntryVal(de
) = NULL
;
4132 /* If we have still some hope of having some value fitting memory
4133 * then we try random sampling. */
4134 if (!swap_all_values
&& server
.vm_enabled
&& (loadedkeys
% 5000) == 0) {
4135 while (zmalloc_used_memory() > server
.vm_max_memory
) {
4136 if (vmSwapOneObjectBlocking() == REDIS_ERR
) break;
4138 if (zmalloc_used_memory() > server
.vm_max_memory
)
4139 swap_all_values
= 1; /* We are already using too much mem */
4145 eoferr
: /* unexpected end of file is handled here with a fatal exit */
4146 redisLog(REDIS_WARNING
,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
4148 return REDIS_ERR
; /* Just to avoid warning */
4151 /*================================== Commands =============================== */
4153 static void authCommand(redisClient
*c
) {
4154 if (!server
.requirepass
|| !strcmp(c
->argv
[1]->ptr
, server
.requirepass
)) {
4155 c
->authenticated
= 1;
4156 addReply(c
,shared
.ok
);
4158 c
->authenticated
= 0;
4159 addReplySds(c
,sdscatprintf(sdsempty(),"-ERR invalid password\r\n"));
4163 static void pingCommand(redisClient
*c
) {
4164 addReply(c
,shared
.pong
);
4167 static void echoCommand(redisClient
*c
) {
4168 addReplyBulk(c
,c
->argv
[1]);
4171 /*=================================== Strings =============================== */
4173 static void setGenericCommand(redisClient
*c
, int nx
, robj
*key
, robj
*val
, robj
*expire
) {
4175 long seconds
= 0; /* initialized to avoid an harmness warning */
4178 if (getLongFromObjectOrReply(c
, expire
, &seconds
, NULL
) != REDIS_OK
)
4181 addReplySds(c
,sdsnew("-ERR invalid expire time in SETEX\r\n"));
4186 if (nx
) deleteIfVolatile(c
->db
,key
);
4187 retval
= dictAdd(c
->db
->dict
,key
,val
);
4188 if (retval
== DICT_ERR
) {
4190 /* If the key is about a swapped value, we want a new key object
4191 * to overwrite the old. So we delete the old key in the database.
4192 * This will also make sure that swap pages about the old object
4193 * will be marked as free. */
4194 if (server
.vm_enabled
&& deleteIfSwapped(c
->db
,key
))
4196 dictReplace(c
->db
->dict
,key
,val
);
4199 addReply(c
,shared
.czero
);
4207 removeExpire(c
->db
,key
);
4208 if (expire
) setExpire(c
->db
,key
,time(NULL
)+seconds
);
4209 addReply(c
, nx
? shared
.cone
: shared
.ok
);
4212 static void setCommand(redisClient
*c
) {
4213 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[2],NULL
);
4216 static void setnxCommand(redisClient
*c
) {
4217 setGenericCommand(c
,1,c
->argv
[1],c
->argv
[2],NULL
);
4220 static void setexCommand(redisClient
*c
) {
4221 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[3],c
->argv
[2]);
4224 static int getGenericCommand(redisClient
*c
) {
4227 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
)
4230 if (o
->type
!= REDIS_STRING
) {
4231 addReply(c
,shared
.wrongtypeerr
);
4239 static void getCommand(redisClient
*c
) {
4240 getGenericCommand(c
);
4243 static void getsetCommand(redisClient
*c
) {
4244 if (getGenericCommand(c
) == REDIS_ERR
) return;
4245 if (dictAdd(c
->db
->dict
,c
->argv
[1],c
->argv
[2]) == DICT_ERR
) {
4246 dictReplace(c
->db
->dict
,c
->argv
[1],c
->argv
[2]);
4248 incrRefCount(c
->argv
[1]);
4250 incrRefCount(c
->argv
[2]);
4252 removeExpire(c
->db
,c
->argv
[1]);
4255 static void mgetCommand(redisClient
*c
) {
4258 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-1));
4259 for (j
= 1; j
< c
->argc
; j
++) {
4260 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
4262 addReply(c
,shared
.nullbulk
);
4264 if (o
->type
!= REDIS_STRING
) {
4265 addReply(c
,shared
.nullbulk
);
4273 static void msetGenericCommand(redisClient
*c
, int nx
) {
4274 int j
, busykeys
= 0;
4276 if ((c
->argc
% 2) == 0) {
4277 addReplySds(c
,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
4280 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
4281 * set nothing at all if at least one already key exists. */
4283 for (j
= 1; j
< c
->argc
; j
+= 2) {
4284 if (lookupKeyWrite(c
->db
,c
->argv
[j
]) != NULL
) {
4290 addReply(c
, shared
.czero
);
4294 for (j
= 1; j
< c
->argc
; j
+= 2) {
4297 c
->argv
[j
+1] = tryObjectEncoding(c
->argv
[j
+1]);
4298 retval
= dictAdd(c
->db
->dict
,c
->argv
[j
],c
->argv
[j
+1]);
4299 if (retval
== DICT_ERR
) {
4300 dictReplace(c
->db
->dict
,c
->argv
[j
],c
->argv
[j
+1]);
4301 incrRefCount(c
->argv
[j
+1]);
4303 incrRefCount(c
->argv
[j
]);
4304 incrRefCount(c
->argv
[j
+1]);
4306 removeExpire(c
->db
,c
->argv
[j
]);
4308 server
.dirty
+= (c
->argc
-1)/2;
4309 addReply(c
, nx
? shared
.cone
: shared
.ok
);
4312 static void msetCommand(redisClient
*c
) {
4313 msetGenericCommand(c
,0);
4316 static void msetnxCommand(redisClient
*c
) {
4317 msetGenericCommand(c
,1);
4320 static void incrDecrCommand(redisClient
*c
, long long incr
) {
4325 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4326 if (o
!= NULL
&& checkType(c
,o
,REDIS_STRING
)) return;
4327 if (getLongLongFromObjectOrReply(c
,o
,&value
,NULL
) != REDIS_OK
) return;
4330 o
= createStringObjectFromLongLong(value
);
4331 retval
= dictAdd(c
->db
->dict
,c
->argv
[1],o
);
4332 if (retval
== DICT_ERR
) {
4333 dictReplace(c
->db
->dict
,c
->argv
[1],o
);
4334 removeExpire(c
->db
,c
->argv
[1]);
4336 incrRefCount(c
->argv
[1]);
4339 addReply(c
,shared
.colon
);
4341 addReply(c
,shared
.crlf
);
4344 static void incrCommand(redisClient
*c
) {
4345 incrDecrCommand(c
,1);
4348 static void decrCommand(redisClient
*c
) {
4349 incrDecrCommand(c
,-1);
4352 static void incrbyCommand(redisClient
*c
) {
4355 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
4356 incrDecrCommand(c
,incr
);
4359 static void decrbyCommand(redisClient
*c
) {
4362 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
4363 incrDecrCommand(c
,-incr
);
4366 static void appendCommand(redisClient
*c
) {
4371 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4373 /* Create the key */
4374 retval
= dictAdd(c
->db
->dict
,c
->argv
[1],c
->argv
[2]);
4375 incrRefCount(c
->argv
[1]);
4376 incrRefCount(c
->argv
[2]);
4377 totlen
= stringObjectLen(c
->argv
[2]);
4381 de
= dictFind(c
->db
->dict
,c
->argv
[1]);
4384 o
= dictGetEntryVal(de
);
4385 if (o
->type
!= REDIS_STRING
) {
4386 addReply(c
,shared
.wrongtypeerr
);
4389 /* If the object is specially encoded or shared we have to make
4391 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
4392 robj
*decoded
= getDecodedObject(o
);
4394 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
4395 decrRefCount(decoded
);
4396 dictReplace(c
->db
->dict
,c
->argv
[1],o
);
4399 if (c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
) {
4400 o
->ptr
= sdscatlen(o
->ptr
,
4401 c
->argv
[2]->ptr
, sdslen(c
->argv
[2]->ptr
));
4403 o
->ptr
= sdscatprintf(o
->ptr
, "%ld",
4404 (unsigned long) c
->argv
[2]->ptr
);
4406 totlen
= sdslen(o
->ptr
);
4409 addReplySds(c
,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen
));
4412 static void substrCommand(redisClient
*c
) {
4414 long start
= atoi(c
->argv
[2]->ptr
);
4415 long end
= atoi(c
->argv
[3]->ptr
);
4416 size_t rangelen
, strlen
;
4419 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
4420 checkType(c
,o
,REDIS_STRING
)) return;
4422 o
= getDecodedObject(o
);
4423 strlen
= sdslen(o
->ptr
);
4425 /* convert negative indexes */
4426 if (start
< 0) start
= strlen
+start
;
4427 if (end
< 0) end
= strlen
+end
;
4428 if (start
< 0) start
= 0;
4429 if (end
< 0) end
= 0;
4431 /* indexes sanity checks */
4432 if (start
> end
|| (size_t)start
>= strlen
) {
4433 /* Out of range start or start > end result in null reply */
4434 addReply(c
,shared
.nullbulk
);
4438 if ((size_t)end
>= strlen
) end
= strlen
-1;
4439 rangelen
= (end
-start
)+1;
4441 /* Return the result */
4442 addReplySds(c
,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen
));
4443 range
= sdsnewlen((char*)o
->ptr
+start
,rangelen
);
4444 addReplySds(c
,range
);
4445 addReply(c
,shared
.crlf
);
4449 /* ========================= Type agnostic commands ========================= */
4451 static void delCommand(redisClient
*c
) {
4454 for (j
= 1; j
< c
->argc
; j
++) {
4455 if (deleteKey(c
->db
,c
->argv
[j
])) {
4460 addReplyLongLong(c
,deleted
);
4463 static void existsCommand(redisClient
*c
) {
4464 expireIfNeeded(c
->db
,c
->argv
[1]);
4465 if (dictFind(c
->db
->dict
,c
->argv
[1])) {
4466 addReply(c
, shared
.cone
);
4468 addReply(c
, shared
.czero
);
4472 static void selectCommand(redisClient
*c
) {
4473 int id
= atoi(c
->argv
[1]->ptr
);
4475 if (selectDb(c
,id
) == REDIS_ERR
) {
4476 addReplySds(c
,sdsnew("-ERR invalid DB index\r\n"));
4478 addReply(c
,shared
.ok
);
4482 static void randomkeyCommand(redisClient
*c
) {
4487 de
= dictGetRandomKey(c
->db
->dict
);
4488 if (!de
|| expireIfNeeded(c
->db
,dictGetEntryKey(de
)) == 0) break;
4492 addReply(c
,shared
.nullbulk
);
4496 key
= dictGetEntryKey(de
);
4497 if (server
.vm_enabled
) {
4498 key
= dupStringObject(key
);
4499 addReplyBulk(c
,key
);
4502 addReplyBulk(c
,key
);
4506 static void keysCommand(redisClient
*c
) {
4509 sds pattern
= c
->argv
[1]->ptr
;
4510 int plen
= sdslen(pattern
);
4511 unsigned long numkeys
= 0;
4512 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
4514 di
= dictGetIterator(c
->db
->dict
);
4516 decrRefCount(lenobj
);
4517 while((de
= dictNext(di
)) != NULL
) {
4518 robj
*keyobj
= dictGetEntryKey(de
);
4520 sds key
= keyobj
->ptr
;
4521 if ((pattern
[0] == '*' && pattern
[1] == '\0') ||
4522 stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
4523 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
4524 addReplyBulk(c
,keyobj
);
4529 dictReleaseIterator(di
);
4530 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",numkeys
);
4533 static void dbsizeCommand(redisClient
*c
) {
4535 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c
->db
->dict
)));
4538 static void lastsaveCommand(redisClient
*c
) {
4540 sdscatprintf(sdsempty(),":%lu\r\n",server
.lastsave
));
4543 static void typeCommand(redisClient
*c
) {
4547 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
4552 case REDIS_STRING
: type
= "+string"; break;
4553 case REDIS_LIST
: type
= "+list"; break;
4554 case REDIS_SET
: type
= "+set"; break;
4555 case REDIS_ZSET
: type
= "+zset"; break;
4556 case REDIS_HASH
: type
= "+hash"; break;
4557 default: type
= "+unknown"; break;
4560 addReplySds(c
,sdsnew(type
));
4561 addReply(c
,shared
.crlf
);
4564 static void saveCommand(redisClient
*c
) {
4565 if (server
.bgsavechildpid
!= -1) {
4566 addReplySds(c
,sdsnew("-ERR background save in progress\r\n"));
4569 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
4570 addReply(c
,shared
.ok
);
4572 addReply(c
,shared
.err
);
4576 static void bgsaveCommand(redisClient
*c
) {
4577 if (server
.bgsavechildpid
!= -1) {
4578 addReplySds(c
,sdsnew("-ERR background save already in progress\r\n"));
4581 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
4582 char *status
= "+Background saving started\r\n";
4583 addReplySds(c
,sdsnew(status
));
4585 addReply(c
,shared
.err
);
4589 static void shutdownCommand(redisClient
*c
) {
4590 redisLog(REDIS_WARNING
,"User requested shutdown, saving DB...");
4591 /* Kill the saving child if there is a background saving in progress.
4592 We want to avoid race conditions, for instance our saving child may
4593 overwrite the synchronous saving did by SHUTDOWN. */
4594 if (server
.bgsavechildpid
!= -1) {
4595 redisLog(REDIS_WARNING
,"There is a live saving child. Killing it!");
4596 kill(server
.bgsavechildpid
,SIGKILL
);
4597 rdbRemoveTempFile(server
.bgsavechildpid
);
4599 if (server
.appendonly
) {
4600 /* Append only file: fsync() the AOF and exit */
4601 fsync(server
.appendfd
);
4602 if (server
.vm_enabled
) unlink(server
.vm_swap_file
);
4605 /* Snapshotting. Perform a SYNC SAVE and exit */
4606 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
4607 if (server
.daemonize
)
4608 unlink(server
.pidfile
);
4609 redisLog(REDIS_WARNING
,"%zu bytes used at exit",zmalloc_used_memory());
4610 redisLog(REDIS_WARNING
,"Server exit now, bye bye...");
4613 /* Ooops.. error saving! The best we can do is to continue
4614 * operating. Note that if there was a background saving process,
4615 * in the next cron() Redis will be notified that the background
4616 * saving aborted, handling special stuff like slaves pending for
4617 * synchronization... */
4618 redisLog(REDIS_WARNING
,"Error trying to save the DB, can't exit");
4620 sdsnew("-ERR can't quit, problems saving the DB\r\n"));
4625 static void renameGenericCommand(redisClient
*c
, int nx
) {
4628 /* To use the same key as src and dst is probably an error */
4629 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
4630 addReply(c
,shared
.sameobjecterr
);
4634 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
4638 deleteIfVolatile(c
->db
,c
->argv
[2]);
4639 if (dictAdd(c
->db
->dict
,c
->argv
[2],o
) == DICT_ERR
) {
4642 addReply(c
,shared
.czero
);
4645 dictReplace(c
->db
->dict
,c
->argv
[2],o
);
4647 incrRefCount(c
->argv
[2]);
4649 deleteKey(c
->db
,c
->argv
[1]);
4651 addReply(c
,nx
? shared
.cone
: shared
.ok
);
4654 static void renameCommand(redisClient
*c
) {
4655 renameGenericCommand(c
,0);
4658 static void renamenxCommand(redisClient
*c
) {
4659 renameGenericCommand(c
,1);
4662 static void moveCommand(redisClient
*c
) {
4667 /* Obtain source and target DB pointers */
4670 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
4671 addReply(c
,shared
.outofrangeerr
);
4675 selectDb(c
,srcid
); /* Back to the source DB */
4677 /* If the user is moving using as target the same
4678 * DB as the source DB it is probably an error. */
4680 addReply(c
,shared
.sameobjecterr
);
4684 /* Check if the element exists and get a reference */
4685 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4687 addReply(c
,shared
.czero
);
4691 /* Try to add the element to the target DB */
4692 deleteIfVolatile(dst
,c
->argv
[1]);
4693 if (dictAdd(dst
->dict
,c
->argv
[1],o
) == DICT_ERR
) {
4694 addReply(c
,shared
.czero
);
4697 incrRefCount(c
->argv
[1]);
4700 /* OK! key moved, free the entry in the source DB */
4701 deleteKey(src
,c
->argv
[1]);
4703 addReply(c
,shared
.cone
);
4706 /* =================================== Lists ================================ */
4707 static void pushGenericCommand(redisClient
*c
, int where
) {
4711 lobj
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4713 if (handleClientsWaitingListPush(c
,c
->argv
[1],c
->argv
[2])) {
4714 addReply(c
,shared
.cone
);
4717 lobj
= createListObject();
4719 if (where
== REDIS_HEAD
) {
4720 listAddNodeHead(list
,c
->argv
[2]);
4722 listAddNodeTail(list
,c
->argv
[2]);
4724 dictAdd(c
->db
->dict
,c
->argv
[1],lobj
);
4725 incrRefCount(c
->argv
[1]);
4726 incrRefCount(c
->argv
[2]);
4728 if (lobj
->type
!= REDIS_LIST
) {
4729 addReply(c
,shared
.wrongtypeerr
);
4732 if (handleClientsWaitingListPush(c
,c
->argv
[1],c
->argv
[2])) {
4733 addReply(c
,shared
.cone
);
4737 if (where
== REDIS_HEAD
) {
4738 listAddNodeHead(list
,c
->argv
[2]);
4740 listAddNodeTail(list
,c
->argv
[2]);
4742 incrRefCount(c
->argv
[2]);
4745 addReplyLongLong(c
,listLength(list
));
4748 static void lpushCommand(redisClient
*c
) {
4749 pushGenericCommand(c
,REDIS_HEAD
);
4752 static void rpushCommand(redisClient
*c
) {
4753 pushGenericCommand(c
,REDIS_TAIL
);
4756 static void llenCommand(redisClient
*c
) {
4760 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
4761 checkType(c
,o
,REDIS_LIST
)) return;
4764 addReplyUlong(c
,listLength(l
));
4767 static void lindexCommand(redisClient
*c
) {
4769 int index
= atoi(c
->argv
[2]->ptr
);
4773 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
4774 checkType(c
,o
,REDIS_LIST
)) return;
4777 ln
= listIndex(list
, index
);
4779 addReply(c
,shared
.nullbulk
);
4781 robj
*ele
= listNodeValue(ln
);
4782 addReplyBulk(c
,ele
);
4786 static void lsetCommand(redisClient
*c
) {
4788 int index
= atoi(c
->argv
[2]->ptr
);
4792 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
||
4793 checkType(c
,o
,REDIS_LIST
)) return;
4796 ln
= listIndex(list
, index
);
4798 addReply(c
,shared
.outofrangeerr
);
4800 robj
*ele
= listNodeValue(ln
);
4803 listNodeValue(ln
) = c
->argv
[3];
4804 incrRefCount(c
->argv
[3]);
4805 addReply(c
,shared
.ok
);
4810 static void popGenericCommand(redisClient
*c
, int where
) {
4815 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
4816 checkType(c
,o
,REDIS_LIST
)) return;
4819 if (where
== REDIS_HEAD
)
4820 ln
= listFirst(list
);
4822 ln
= listLast(list
);
4825 addReply(c
,shared
.nullbulk
);
4827 robj
*ele
= listNodeValue(ln
);
4828 addReplyBulk(c
,ele
);
4829 listDelNode(list
,ln
);
4830 if (listLength(list
) == 0) deleteKey(c
->db
,c
->argv
[1]);
4835 static void lpopCommand(redisClient
*c
) {
4836 popGenericCommand(c
,REDIS_HEAD
);
4839 static void rpopCommand(redisClient
*c
) {
4840 popGenericCommand(c
,REDIS_TAIL
);
4843 static void lrangeCommand(redisClient
*c
) {
4845 int start
= atoi(c
->argv
[2]->ptr
);
4846 int end
= atoi(c
->argv
[3]->ptr
);
4853 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.emptymultibulk
)) == NULL
4854 || checkType(c
,o
,REDIS_LIST
)) return;
4856 llen
= listLength(list
);
4858 /* convert negative indexes */
4859 if (start
< 0) start
= llen
+start
;
4860 if (end
< 0) end
= llen
+end
;
4861 if (start
< 0) start
= 0;
4862 if (end
< 0) end
= 0;
4864 /* indexes sanity checks */
4865 if (start
> end
|| start
>= llen
) {
4866 /* Out of range start or start > end result in empty list */
4867 addReply(c
,shared
.emptymultibulk
);
4870 if (end
>= llen
) end
= llen
-1;
4871 rangelen
= (end
-start
)+1;
4873 /* Return the result in form of a multi-bulk reply */
4874 ln
= listIndex(list
, start
);
4875 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",rangelen
));
4876 for (j
= 0; j
< rangelen
; j
++) {
4877 ele
= listNodeValue(ln
);
4878 addReplyBulk(c
,ele
);
4883 static void ltrimCommand(redisClient
*c
) {
4885 int start
= atoi(c
->argv
[2]->ptr
);
4886 int end
= atoi(c
->argv
[3]->ptr
);
4888 int j
, ltrim
, rtrim
;
4892 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.ok
)) == NULL
||
4893 checkType(c
,o
,REDIS_LIST
)) return;
4895 llen
= listLength(list
);
4897 /* convert negative indexes */
4898 if (start
< 0) start
= llen
+start
;
4899 if (end
< 0) end
= llen
+end
;
4900 if (start
< 0) start
= 0;
4901 if (end
< 0) end
= 0;
4903 /* indexes sanity checks */
4904 if (start
> end
|| start
>= llen
) {
4905 /* Out of range start or start > end result in empty list */
4909 if (end
>= llen
) end
= llen
-1;
4914 /* Remove list elements to perform the trim */
4915 for (j
= 0; j
< ltrim
; j
++) {
4916 ln
= listFirst(list
);
4917 listDelNode(list
,ln
);
4919 for (j
= 0; j
< rtrim
; j
++) {
4920 ln
= listLast(list
);
4921 listDelNode(list
,ln
);
4923 if (listLength(list
) == 0) deleteKey(c
->db
,c
->argv
[1]);
4925 addReply(c
,shared
.ok
);
4928 static void lremCommand(redisClient
*c
) {
4931 listNode
*ln
, *next
;
4932 int toremove
= atoi(c
->argv
[2]->ptr
);
4936 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
4937 checkType(c
,o
,REDIS_LIST
)) return;
4941 toremove
= -toremove
;
4944 ln
= fromtail
? list
->tail
: list
->head
;
4946 robj
*ele
= listNodeValue(ln
);
4948 next
= fromtail
? ln
->prev
: ln
->next
;
4949 if (equalStringObjects(ele
,c
->argv
[3])) {
4950 listDelNode(list
,ln
);
4953 if (toremove
&& removed
== toremove
) break;
4957 if (listLength(list
) == 0) deleteKey(c
->db
,c
->argv
[1]);
4958 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",removed
));
4961 /* This is the semantic of this command:
4962 * RPOPLPUSH srclist dstlist:
4963 * IF LLEN(srclist) > 0
4964 * element = RPOP srclist
4965 * LPUSH dstlist element
4972 * The idea is to be able to get an element from a list in a reliable way
4973 * since the element is not just returned but pushed against another list
4974 * as well. This command was originally proposed by Ezra Zygmuntowicz.
4976 static void rpoplpushcommand(redisClient
*c
) {
4981 if ((sobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
4982 checkType(c
,sobj
,REDIS_LIST
)) return;
4983 srclist
= sobj
->ptr
;
4984 ln
= listLast(srclist
);
4987 addReply(c
,shared
.nullbulk
);
4989 robj
*dobj
= lookupKeyWrite(c
->db
,c
->argv
[2]);
4990 robj
*ele
= listNodeValue(ln
);
4993 if (dobj
&& dobj
->type
!= REDIS_LIST
) {
4994 addReply(c
,shared
.wrongtypeerr
);
4998 /* Add the element to the target list (unless it's directly
4999 * passed to some BLPOP-ing client */
5000 if (!handleClientsWaitingListPush(c
,c
->argv
[2],ele
)) {
5002 /* Create the list if the key does not exist */
5003 dobj
= createListObject();
5004 dictAdd(c
->db
->dict
,c
->argv
[2],dobj
);
5005 incrRefCount(c
->argv
[2]);
5007 dstlist
= dobj
->ptr
;
5008 listAddNodeHead(dstlist
,ele
);
5012 /* Send the element to the client as reply as well */
5013 addReplyBulk(c
,ele
);
5015 /* Finally remove the element from the source list */
5016 listDelNode(srclist
,ln
);
5017 if (listLength(srclist
) == 0) deleteKey(c
->db
,c
->argv
[1]);
5022 /* ==================================== Sets ================================ */
5024 static void saddCommand(redisClient
*c
) {
5027 set
= lookupKeyWrite(c
->db
,c
->argv
[1]);
5029 set
= createSetObject();
5030 dictAdd(c
->db
->dict
,c
->argv
[1],set
);
5031 incrRefCount(c
->argv
[1]);
5033 if (set
->type
!= REDIS_SET
) {
5034 addReply(c
,shared
.wrongtypeerr
);
5038 if (dictAdd(set
->ptr
,c
->argv
[2],NULL
) == DICT_OK
) {
5039 incrRefCount(c
->argv
[2]);
5041 addReply(c
,shared
.cone
);
5043 addReply(c
,shared
.czero
);
5047 static void sremCommand(redisClient
*c
) {
5050 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5051 checkType(c
,set
,REDIS_SET
)) return;
5053 if (dictDelete(set
->ptr
,c
->argv
[2]) == DICT_OK
) {
5055 if (htNeedsResize(set
->ptr
)) dictResize(set
->ptr
);
5056 if (dictSize((dict
*)set
->ptr
) == 0) deleteKey(c
->db
,c
->argv
[1]);
5057 addReply(c
,shared
.cone
);
5059 addReply(c
,shared
.czero
);
5063 static void smoveCommand(redisClient
*c
) {
5064 robj
*srcset
, *dstset
;
5066 srcset
= lookupKeyWrite(c
->db
,c
->argv
[1]);
5067 dstset
= lookupKeyWrite(c
->db
,c
->argv
[2]);
5069 /* If the source key does not exist return 0, if it's of the wrong type
5071 if (srcset
== NULL
|| srcset
->type
!= REDIS_SET
) {
5072 addReply(c
, srcset
? shared
.wrongtypeerr
: shared
.czero
);
5075 /* Error if the destination key is not a set as well */
5076 if (dstset
&& dstset
->type
!= REDIS_SET
) {
5077 addReply(c
,shared
.wrongtypeerr
);
5080 /* Remove the element from the source set */
5081 if (dictDelete(srcset
->ptr
,c
->argv
[3]) == DICT_ERR
) {
5082 /* Key not found in the src set! return zero */
5083 addReply(c
,shared
.czero
);
5086 if (dictSize((dict
*)srcset
->ptr
) == 0 && srcset
!= dstset
)
5087 deleteKey(c
->db
,c
->argv
[1]);
5089 /* Add the element to the destination set */
5091 dstset
= createSetObject();
5092 dictAdd(c
->db
->dict
,c
->argv
[2],dstset
);
5093 incrRefCount(c
->argv
[2]);
5095 if (dictAdd(dstset
->ptr
,c
->argv
[3],NULL
) == DICT_OK
)
5096 incrRefCount(c
->argv
[3]);
5097 addReply(c
,shared
.cone
);
5100 static void sismemberCommand(redisClient
*c
) {
5103 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5104 checkType(c
,set
,REDIS_SET
)) return;
5106 if (dictFind(set
->ptr
,c
->argv
[2]))
5107 addReply(c
,shared
.cone
);
5109 addReply(c
,shared
.czero
);
5112 static void scardCommand(redisClient
*c
) {
5116 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5117 checkType(c
,o
,REDIS_SET
)) return;
5120 addReplyUlong(c
,dictSize(s
));
5123 static void spopCommand(redisClient
*c
) {
5127 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
5128 checkType(c
,set
,REDIS_SET
)) return;
5130 de
= dictGetRandomKey(set
->ptr
);
5132 addReply(c
,shared
.nullbulk
);
5134 robj
*ele
= dictGetEntryKey(de
);
5136 addReplyBulk(c
,ele
);
5137 dictDelete(set
->ptr
,ele
);
5138 if (htNeedsResize(set
->ptr
)) dictResize(set
->ptr
);
5139 if (dictSize((dict
*)set
->ptr
) == 0) deleteKey(c
->db
,c
->argv
[1]);
5144 static void srandmemberCommand(redisClient
*c
) {
5148 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
5149 checkType(c
,set
,REDIS_SET
)) return;
5151 de
= dictGetRandomKey(set
->ptr
);
5153 addReply(c
,shared
.nullbulk
);
5155 robj
*ele
= dictGetEntryKey(de
);
5157 addReplyBulk(c
,ele
);
5161 static int qsortCompareSetsByCardinality(const void *s1
, const void *s2
) {
5162 dict
**d1
= (void*) s1
, **d2
= (void*) s2
;
5164 return dictSize(*d1
)-dictSize(*d2
);
5167 static void sinterGenericCommand(redisClient
*c
, robj
**setskeys
, unsigned long setsnum
, robj
*dstkey
) {
5168 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
5171 robj
*lenobj
= NULL
, *dstset
= NULL
;
5172 unsigned long j
, cardinality
= 0;
5174 for (j
= 0; j
< setsnum
; j
++) {
5178 lookupKeyWrite(c
->db
,setskeys
[j
]) :
5179 lookupKeyRead(c
->db
,setskeys
[j
]);
5183 if (deleteKey(c
->db
,dstkey
))
5185 addReply(c
,shared
.czero
);
5187 addReply(c
,shared
.emptymultibulk
);
5191 if (setobj
->type
!= REDIS_SET
) {
5193 addReply(c
,shared
.wrongtypeerr
);
5196 dv
[j
] = setobj
->ptr
;
5198 /* Sort sets from the smallest to largest, this will improve our
5199 * algorithm's performace */
5200 qsort(dv
,setsnum
,sizeof(dict
*),qsortCompareSetsByCardinality
);
5202 /* The first thing we should output is the total number of elements...
5203 * since this is a multi-bulk write, but at this stage we don't know
5204 * the intersection set size, so we use a trick, append an empty object
5205 * to the output list and save the pointer to later modify it with the
5208 lenobj
= createObject(REDIS_STRING
,NULL
);
5210 decrRefCount(lenobj
);
5212 /* If we have a target key where to store the resulting set
5213 * create this key with an empty set inside */
5214 dstset
= createSetObject();
5217 /* Iterate all the elements of the first (smallest) set, and test
5218 * the element against all the other sets, if at least one set does
5219 * not include the element it is discarded */
5220 di
= dictGetIterator(dv
[0]);
5222 while((de
= dictNext(di
)) != NULL
) {
5225 for (j
= 1; j
< setsnum
; j
++)
5226 if (dictFind(dv
[j
],dictGetEntryKey(de
)) == NULL
) break;
5228 continue; /* at least one set does not contain the member */
5229 ele
= dictGetEntryKey(de
);
5231 addReplyBulk(c
,ele
);
5234 dictAdd(dstset
->ptr
,ele
,NULL
);
5238 dictReleaseIterator(di
);
5241 /* Store the resulting set into the target, if the intersection
5242 * is not an empty set. */
5243 deleteKey(c
->db
,dstkey
);
5244 if (dictSize((dict
*)dstset
->ptr
) > 0) {
5245 dictAdd(c
->db
->dict
,dstkey
,dstset
);
5246 incrRefCount(dstkey
);
5247 addReplyLongLong(c
,dictSize((dict
*)dstset
->ptr
));
5249 decrRefCount(dstset
);
5250 addReply(c
,shared
.czero
);
5254 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",cardinality
);
5259 static void sinterCommand(redisClient
*c
) {
5260 sinterGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
);
5263 static void sinterstoreCommand(redisClient
*c
) {
5264 sinterGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1]);
5267 #define REDIS_OP_UNION 0
5268 #define REDIS_OP_DIFF 1
5269 #define REDIS_OP_INTER 2
5271 static void sunionDiffGenericCommand(redisClient
*c
, robj
**setskeys
, int setsnum
, robj
*dstkey
, int op
) {
5272 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
5275 robj
*dstset
= NULL
;
5276 int j
, cardinality
= 0;
5278 for (j
= 0; j
< setsnum
; j
++) {
5282 lookupKeyWrite(c
->db
,setskeys
[j
]) :
5283 lookupKeyRead(c
->db
,setskeys
[j
]);
5288 if (setobj
->type
!= REDIS_SET
) {
5290 addReply(c
,shared
.wrongtypeerr
);
5293 dv
[j
] = setobj
->ptr
;
5296 /* We need a temp set object to store our union. If the dstkey
5297 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
5298 * this set object will be the resulting object to set into the target key*/
5299 dstset
= createSetObject();
5301 /* Iterate all the elements of all the sets, add every element a single
5302 * time to the result set */
5303 for (j
= 0; j
< setsnum
; j
++) {
5304 if (op
== REDIS_OP_DIFF
&& j
== 0 && !dv
[j
]) break; /* result set is empty */
5305 if (!dv
[j
]) continue; /* non existing keys are like empty sets */
5307 di
= dictGetIterator(dv
[j
]);
5309 while((de
= dictNext(di
)) != NULL
) {
5312 /* dictAdd will not add the same element multiple times */
5313 ele
= dictGetEntryKey(de
);
5314 if (op
== REDIS_OP_UNION
|| j
== 0) {
5315 if (dictAdd(dstset
->ptr
,ele
,NULL
) == DICT_OK
) {
5319 } else if (op
== REDIS_OP_DIFF
) {
5320 if (dictDelete(dstset
->ptr
,ele
) == DICT_OK
) {
5325 dictReleaseIterator(di
);
5327 /* result set is empty? Exit asap. */
5328 if (op
== REDIS_OP_DIFF
&& cardinality
== 0) break;
5331 /* Output the content of the resulting set, if not in STORE mode */
5333 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",cardinality
));
5334 di
= dictGetIterator(dstset
->ptr
);
5335 while((de
= dictNext(di
)) != NULL
) {
5338 ele
= dictGetEntryKey(de
);
5339 addReplyBulk(c
,ele
);
5341 dictReleaseIterator(di
);
5342 decrRefCount(dstset
);
5344 /* If we have a target key where to store the resulting set
5345 * create this key with the result set inside */
5346 deleteKey(c
->db
,dstkey
);
5347 if (dictSize((dict
*)dstset
->ptr
) > 0) {
5348 dictAdd(c
->db
->dict
,dstkey
,dstset
);
5349 incrRefCount(dstkey
);
5350 addReplyLongLong(c
,dictSize((dict
*)dstset
->ptr
));
5352 decrRefCount(dstset
);
5353 addReply(c
,shared
.czero
);
5360 static void sunionCommand(redisClient
*c
) {
5361 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_UNION
);
5364 static void sunionstoreCommand(redisClient
*c
) {
5365 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_UNION
);
5368 static void sdiffCommand(redisClient
*c
) {
5369 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_DIFF
);
5372 static void sdiffstoreCommand(redisClient
*c
) {
5373 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_DIFF
);
5376 /* ==================================== ZSets =============================== */
5378 /* ZSETs are ordered sets using two data structures to hold the same elements
5379 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
5382 * The elements are added to an hash table mapping Redis objects to scores.
5383 * At the same time the elements are added to a skip list mapping scores
5384 * to Redis objects (so objects are sorted by scores in this "view"). */
5386 /* This skiplist implementation is almost a C translation of the original
5387 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
5388 * Alternative to Balanced Trees", modified in three ways:
5389 * a) this implementation allows for repeated values.
5390 * b) the comparison is not just by key (our 'score') but by satellite data.
5391 * c) there is a back pointer, so it's a doubly linked list with the back
5392 * pointers being only at "level 1". This allows to traverse the list
5393 * from tail to head, useful for ZREVRANGE. */
5395 static zskiplistNode
*zslCreateNode(int level
, double score
, robj
*obj
) {
5396 zskiplistNode
*zn
= zmalloc(sizeof(*zn
));
5398 zn
->forward
= zmalloc(sizeof(zskiplistNode
*) * level
);
5400 zn
->span
= zmalloc(sizeof(unsigned int) * (level
- 1));
5406 static zskiplist
*zslCreate(void) {
5410 zsl
= zmalloc(sizeof(*zsl
));
5413 zsl
->header
= zslCreateNode(ZSKIPLIST_MAXLEVEL
,0,NULL
);
5414 for (j
= 0; j
< ZSKIPLIST_MAXLEVEL
; j
++) {
5415 zsl
->header
->forward
[j
] = NULL
;
5417 /* span has space for ZSKIPLIST_MAXLEVEL-1 elements */
5418 if (j
< ZSKIPLIST_MAXLEVEL
-1)
5419 zsl
->header
->span
[j
] = 0;
5421 zsl
->header
->backward
= NULL
;
5426 static void zslFreeNode(zskiplistNode
*node
) {
5427 decrRefCount(node
->obj
);
5428 zfree(node
->forward
);
5433 static void zslFree(zskiplist
*zsl
) {
5434 zskiplistNode
*node
= zsl
->header
->forward
[0], *next
;
5436 zfree(zsl
->header
->forward
);
5437 zfree(zsl
->header
->span
);
5440 next
= node
->forward
[0];
5447 static int zslRandomLevel(void) {
5449 while ((random()&0xFFFF) < (ZSKIPLIST_P
* 0xFFFF))
5451 return (level
<ZSKIPLIST_MAXLEVEL
) ? level
: ZSKIPLIST_MAXLEVEL
;
5454 static void zslInsert(zskiplist
*zsl
, double score
, robj
*obj
) {
5455 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
5456 unsigned int rank
[ZSKIPLIST_MAXLEVEL
];
5460 for (i
= zsl
->level
-1; i
>= 0; i
--) {
5461 /* store rank that is crossed to reach the insert position */
5462 rank
[i
] = i
== (zsl
->level
-1) ? 0 : rank
[i
+1];
5464 while (x
->forward
[i
] &&
5465 (x
->forward
[i
]->score
< score
||
5466 (x
->forward
[i
]->score
== score
&&
5467 compareStringObjects(x
->forward
[i
]->obj
,obj
) < 0))) {
5468 rank
[i
] += i
> 0 ? x
->span
[i
-1] : 1;
5473 /* we assume the key is not already inside, since we allow duplicated
5474 * scores, and the re-insertion of score and redis object should never
5475 * happpen since the caller of zslInsert() should test in the hash table
5476 * if the element is already inside or not. */
5477 level
= zslRandomLevel();
5478 if (level
> zsl
->level
) {
5479 for (i
= zsl
->level
; i
< level
; i
++) {
5481 update
[i
] = zsl
->header
;
5482 update
[i
]->span
[i
-1] = zsl
->length
;
5486 x
= zslCreateNode(level
,score
,obj
);
5487 for (i
= 0; i
< level
; i
++) {
5488 x
->forward
[i
] = update
[i
]->forward
[i
];
5489 update
[i
]->forward
[i
] = x
;
5491 /* update span covered by update[i] as x is inserted here */
5493 x
->span
[i
-1] = update
[i
]->span
[i
-1] - (rank
[0] - rank
[i
]);
5494 update
[i
]->span
[i
-1] = (rank
[0] - rank
[i
]) + 1;
5498 /* increment span for untouched levels */
5499 for (i
= level
; i
< zsl
->level
; i
++) {
5500 update
[i
]->span
[i
-1]++;
5503 x
->backward
= (update
[0] == zsl
->header
) ? NULL
: update
[0];
5505 x
->forward
[0]->backward
= x
;
5511 /* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
5512 void zslDeleteNode(zskiplist
*zsl
, zskiplistNode
*x
, zskiplistNode
**update
) {
5514 for (i
= 0; i
< zsl
->level
; i
++) {
5515 if (update
[i
]->forward
[i
] == x
) {
5517 update
[i
]->span
[i
-1] += x
->span
[i
-1] - 1;
5519 update
[i
]->forward
[i
] = x
->forward
[i
];
5521 /* invariant: i > 0, because update[0]->forward[0]
5522 * is always equal to x */
5523 update
[i
]->span
[i
-1] -= 1;
5526 if (x
->forward
[0]) {
5527 x
->forward
[0]->backward
= x
->backward
;
5529 zsl
->tail
= x
->backward
;
5531 while(zsl
->level
> 1 && zsl
->header
->forward
[zsl
->level
-1] == NULL
)
5536 /* Delete an element with matching score/object from the skiplist. */
5537 static int zslDelete(zskiplist
*zsl
, double score
, robj
*obj
) {
5538 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
5542 for (i
= zsl
->level
-1; i
>= 0; i
--) {
5543 while (x
->forward
[i
] &&
5544 (x
->forward
[i
]->score
< score
||
5545 (x
->forward
[i
]->score
== score
&&
5546 compareStringObjects(x
->forward
[i
]->obj
,obj
) < 0)))
5550 /* We may have multiple elements with the same score, what we need
5551 * is to find the element with both the right score and object. */
5553 if (x
&& score
== x
->score
&& equalStringObjects(x
->obj
,obj
)) {
5554 zslDeleteNode(zsl
, x
, update
);
5558 return 0; /* not found */
5560 return 0; /* not found */
5563 /* Delete all the elements with score between min and max from the skiplist.
5564 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
5565 * Note that this function takes the reference to the hash table view of the
5566 * sorted set, in order to remove the elements from the hash table too. */
5567 static unsigned long zslDeleteRangeByScore(zskiplist
*zsl
, double min
, double max
, dict
*dict
) {
5568 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
5569 unsigned long removed
= 0;
5573 for (i
= zsl
->level
-1; i
>= 0; i
--) {
5574 while (x
->forward
[i
] && x
->forward
[i
]->score
< min
)
5578 /* We may have multiple elements with the same score, what we need
5579 * is to find the element with both the right score and object. */
5581 while (x
&& x
->score
<= max
) {
5582 zskiplistNode
*next
= x
->forward
[0];
5583 zslDeleteNode(zsl
, x
, update
);
5584 dictDelete(dict
,x
->obj
);
5589 return removed
; /* not found */
5592 /* Delete all the elements with rank between start and end from the skiplist.
5593 * Start and end are inclusive. Note that start and end need to be 1-based */
5594 static unsigned long zslDeleteRangeByRank(zskiplist
*zsl
, unsigned int start
, unsigned int end
, dict
*dict
) {
5595 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
5596 unsigned long traversed
= 0, removed
= 0;
5600 for (i
= zsl
->level
-1; i
>= 0; i
--) {
5601 while (x
->forward
[i
] && (traversed
+ (i
> 0 ? x
->span
[i
-1] : 1)) < start
) {
5602 traversed
+= i
> 0 ? x
->span
[i
-1] : 1;
5610 while (x
&& traversed
<= end
) {
5611 zskiplistNode
*next
= x
->forward
[0];
5612 zslDeleteNode(zsl
, x
, update
);
5613 dictDelete(dict
,x
->obj
);
5622 /* Find the first node having a score equal or greater than the specified one.
5623 * Returns NULL if there is no match. */
5624 static zskiplistNode
*zslFirstWithScore(zskiplist
*zsl
, double score
) {
5629 for (i
= zsl
->level
-1; i
>= 0; i
--) {
5630 while (x
->forward
[i
] && x
->forward
[i
]->score
< score
)
5633 /* We may have multiple elements with the same score, what we need
5634 * is to find the element with both the right score and object. */
5635 return x
->forward
[0];
5638 /* Find the rank for an element by both score and key.
5639 * Returns 0 when the element cannot be found, rank otherwise.
5640 * Note that the rank is 1-based due to the span of zsl->header to the
5642 static unsigned long zslGetRank(zskiplist
*zsl
, double score
, robj
*o
) {
5644 unsigned long rank
= 0;
5648 for (i
= zsl
->level
-1; i
>= 0; i
--) {
5649 while (x
->forward
[i
] &&
5650 (x
->forward
[i
]->score
< score
||
5651 (x
->forward
[i
]->score
== score
&&
5652 compareStringObjects(x
->forward
[i
]->obj
,o
) <= 0))) {
5653 rank
+= i
> 0 ? x
->span
[i
-1] : 1;
5657 /* x might be equal to zsl->header, so test if obj is non-NULL */
5658 if (x
->obj
&& equalStringObjects(x
->obj
,o
)) {
5665 /* Finds an element by its rank. The rank argument needs to be 1-based. */
5666 zskiplistNode
* zslGetElementByRank(zskiplist
*zsl
, unsigned long rank
) {
5668 unsigned long traversed
= 0;
5672 for (i
= zsl
->level
-1; i
>= 0; i
--) {
5673 while (x
->forward
[i
] && (traversed
+ (i
>0 ? x
->span
[i
-1] : 1)) <= rank
)
5675 traversed
+= i
> 0 ? x
->span
[i
-1] : 1;
5678 if (traversed
== rank
) {
5685 /* The actual Z-commands implementations */
5687 /* This generic command implements both ZADD and ZINCRBY.
5688 * scoreval is the score if the operation is a ZADD (doincrement == 0) or
5689 * the increment if the operation is a ZINCRBY (doincrement == 1). */
5690 static void zaddGenericCommand(redisClient
*c
, robj
*key
, robj
*ele
, double scoreval
, int doincrement
) {
5695 zsetobj
= lookupKeyWrite(c
->db
,key
);
5696 if (zsetobj
== NULL
) {
5697 zsetobj
= createZsetObject();
5698 dictAdd(c
->db
->dict
,key
,zsetobj
);
5701 if (zsetobj
->type
!= REDIS_ZSET
) {
5702 addReply(c
,shared
.wrongtypeerr
);
5708 /* Ok now since we implement both ZADD and ZINCRBY here the code
5709 * needs to handle the two different conditions. It's all about setting
5710 * '*score', that is, the new score to set, to the right value. */
5711 score
= zmalloc(sizeof(double));
5715 /* Read the old score. If the element was not present starts from 0 */
5716 de
= dictFind(zs
->dict
,ele
);
5718 double *oldscore
= dictGetEntryVal(de
);
5719 *score
= *oldscore
+ scoreval
;
5727 /* What follows is a simple remove and re-insert operation that is common
5728 * to both ZADD and ZINCRBY... */
5729 if (dictAdd(zs
->dict
,ele
,score
) == DICT_OK
) {
5730 /* case 1: New element */
5731 incrRefCount(ele
); /* added to hash */
5732 zslInsert(zs
->zsl
,*score
,ele
);
5733 incrRefCount(ele
); /* added to skiplist */
5736 addReplyDouble(c
,*score
);
5738 addReply(c
,shared
.cone
);
5743 /* case 2: Score update operation */
5744 de
= dictFind(zs
->dict
,ele
);
5745 redisAssert(de
!= NULL
);
5746 oldscore
= dictGetEntryVal(de
);
5747 if (*score
!= *oldscore
) {
5750 /* Remove and insert the element in the skip list with new score */
5751 deleted
= zslDelete(zs
->zsl
,*oldscore
,ele
);
5752 redisAssert(deleted
!= 0);
5753 zslInsert(zs
->zsl
,*score
,ele
);
5755 /* Update the score in the hash table */
5756 dictReplace(zs
->dict
,ele
,score
);
5762 addReplyDouble(c
,*score
);
5764 addReply(c
,shared
.czero
);
5768 static void zaddCommand(redisClient
*c
) {
5771 if (getDoubleFromObjectOrReply(c
, c
->argv
[2], &scoreval
, NULL
) != REDIS_OK
) return;
5772 zaddGenericCommand(c
,c
->argv
[1],c
->argv
[3],scoreval
,0);
5775 static void zincrbyCommand(redisClient
*c
) {
5778 if (getDoubleFromObjectOrReply(c
, c
->argv
[2], &scoreval
, NULL
) != REDIS_OK
) return;
5779 zaddGenericCommand(c
,c
->argv
[1],c
->argv
[3],scoreval
,1);
5782 static void zremCommand(redisClient
*c
) {
5789 if ((zsetobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5790 checkType(c
,zsetobj
,REDIS_ZSET
)) return;
5793 de
= dictFind(zs
->dict
,c
->argv
[2]);
5795 addReply(c
,shared
.czero
);
5798 /* Delete from the skiplist */
5799 oldscore
= dictGetEntryVal(de
);
5800 deleted
= zslDelete(zs
->zsl
,*oldscore
,c
->argv
[2]);
5801 redisAssert(deleted
!= 0);
5803 /* Delete from the hash table */
5804 dictDelete(zs
->dict
,c
->argv
[2]);
5805 if (htNeedsResize(zs
->dict
)) dictResize(zs
->dict
);
5806 if (dictSize(zs
->dict
) == 0) deleteKey(c
->db
,c
->argv
[1]);
5808 addReply(c
,shared
.cone
);
5811 static void zremrangebyscoreCommand(redisClient
*c
) {
5818 if ((getDoubleFromObjectOrReply(c
, c
->argv
[2], &min
, NULL
) != REDIS_OK
) ||
5819 (getDoubleFromObjectOrReply(c
, c
->argv
[3], &max
, NULL
) != REDIS_OK
)) return;
5821 if ((zsetobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5822 checkType(c
,zsetobj
,REDIS_ZSET
)) return;
5825 deleted
= zslDeleteRangeByScore(zs
->zsl
,min
,max
,zs
->dict
);
5826 if (htNeedsResize(zs
->dict
)) dictResize(zs
->dict
);
5827 if (dictSize(zs
->dict
) == 0) deleteKey(c
->db
,c
->argv
[1]);
5828 server
.dirty
+= deleted
;
5829 addReplyLongLong(c
,deleted
);
5832 static void zremrangebyrankCommand(redisClient
*c
) {
5840 if ((getLongFromObjectOrReply(c
, c
->argv
[2], &start
, NULL
) != REDIS_OK
) ||
5841 (getLongFromObjectOrReply(c
, c
->argv
[3], &end
, NULL
) != REDIS_OK
)) return;
5843 if ((zsetobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5844 checkType(c
,zsetobj
,REDIS_ZSET
)) return;
5846 llen
= zs
->zsl
->length
;
5848 /* convert negative indexes */
5849 if (start
< 0) start
= llen
+start
;
5850 if (end
< 0) end
= llen
+end
;
5851 if (start
< 0) start
= 0;
5852 if (end
< 0) end
= 0;
5854 /* indexes sanity checks */
5855 if (start
> end
|| start
>= llen
) {
5856 addReply(c
,shared
.czero
);
5859 if (end
>= llen
) end
= llen
-1;
5861 /* increment start and end because zsl*Rank functions
5862 * use 1-based rank */
5863 deleted
= zslDeleteRangeByRank(zs
->zsl
,start
+1,end
+1,zs
->dict
);
5864 if (htNeedsResize(zs
->dict
)) dictResize(zs
->dict
);
5865 if (dictSize(zs
->dict
) == 0) deleteKey(c
->db
,c
->argv
[1]);
5866 server
.dirty
+= deleted
;
5867 addReplyLongLong(c
, deleted
);
5875 static int qsortCompareZsetopsrcByCardinality(const void *s1
, const void *s2
) {
5876 zsetopsrc
*d1
= (void*) s1
, *d2
= (void*) s2
;
5877 unsigned long size1
, size2
;
5878 size1
= d1
->dict
? dictSize(d1
->dict
) : 0;
5879 size2
= d2
->dict
? dictSize(d2
->dict
) : 0;
5880 return size1
- size2
;
5883 #define REDIS_AGGR_SUM 1
5884 #define REDIS_AGGR_MIN 2
5885 #define REDIS_AGGR_MAX 3
5887 inline static void zunionInterAggregate(double *target
, double val
, int aggregate
) {
5888 if (aggregate
== REDIS_AGGR_SUM
) {
5889 *target
= *target
+ val
;
5890 } else if (aggregate
== REDIS_AGGR_MIN
) {
5891 *target
= val
< *target
? val
: *target
;
5892 } else if (aggregate
== REDIS_AGGR_MAX
) {
5893 *target
= val
> *target
? val
: *target
;
5896 redisPanic("Unknown ZUNION/INTER aggregate type");
5900 static void zunionInterGenericCommand(redisClient
*c
, robj
*dstkey
, int op
) {
5902 int aggregate
= REDIS_AGGR_SUM
;
5909 /* expect zsetnum input keys to be given */
5910 zsetnum
= atoi(c
->argv
[2]->ptr
);
5912 addReplySds(c
,sdsnew("-ERR at least 1 input key is needed for ZUNIONSTORE/ZINTERSTORE\r\n"));
5916 /* test if the expected number of keys would overflow */
5917 if (3+zsetnum
> c
->argc
) {
5918 addReply(c
,shared
.syntaxerr
);
5922 /* read keys to be used for input */
5923 src
= zmalloc(sizeof(zsetopsrc
) * zsetnum
);
5924 for (i
= 0, j
= 3; i
< zsetnum
; i
++, j
++) {
5925 robj
*zsetobj
= lookupKeyWrite(c
->db
,c
->argv
[j
]);
5929 if (zsetobj
->type
!= REDIS_ZSET
) {
5931 addReply(c
,shared
.wrongtypeerr
);
5934 src
[i
].dict
= ((zset
*)zsetobj
->ptr
)->dict
;
5937 /* default all weights to 1 */
5938 src
[i
].weight
= 1.0;
5941 /* parse optional extra arguments */
5943 int remaining
= c
->argc
- j
;
5946 if (remaining
>= (zsetnum
+ 1) && !strcasecmp(c
->argv
[j
]->ptr
,"weights")) {
5948 for (i
= 0; i
< zsetnum
; i
++, j
++, remaining
--) {
5949 if (getDoubleFromObjectOrReply(c
, c
->argv
[j
], &src
[i
].weight
, NULL
) != REDIS_OK
)
5952 } else if (remaining
>= 2 && !strcasecmp(c
->argv
[j
]->ptr
,"aggregate")) {
5954 if (!strcasecmp(c
->argv
[j
]->ptr
,"sum")) {
5955 aggregate
= REDIS_AGGR_SUM
;
5956 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"min")) {
5957 aggregate
= REDIS_AGGR_MIN
;
5958 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"max")) {
5959 aggregate
= REDIS_AGGR_MAX
;
5962 addReply(c
,shared
.syntaxerr
);
5968 addReply(c
,shared
.syntaxerr
);
5974 /* sort sets from the smallest to largest, this will improve our
5975 * algorithm's performance */
5976 qsort(src
,zsetnum
,sizeof(zsetopsrc
), qsortCompareZsetopsrcByCardinality
);
5978 dstobj
= createZsetObject();
5979 dstzset
= dstobj
->ptr
;
5981 if (op
== REDIS_OP_INTER
) {
5982 /* skip going over all entries if the smallest zset is NULL or empty */
5983 if (src
[0].dict
&& dictSize(src
[0].dict
) > 0) {
5984 /* precondition: as src[0].dict is non-empty and the zsets are ordered
5985 * from small to large, all src[i > 0].dict are non-empty too */
5986 di
= dictGetIterator(src
[0].dict
);
5987 while((de
= dictNext(di
)) != NULL
) {
5988 double *score
= zmalloc(sizeof(double)), value
;
5989 *score
= src
[0].weight
* (*(double*)dictGetEntryVal(de
));
5991 for (j
= 1; j
< zsetnum
; j
++) {
5992 dictEntry
*other
= dictFind(src
[j
].dict
,dictGetEntryKey(de
));
5994 value
= src
[j
].weight
* (*(double*)dictGetEntryVal(other
));
5995 zunionInterAggregate(score
, value
, aggregate
);
6001 /* skip entry when not present in every source dict */
6005 robj
*o
= dictGetEntryKey(de
);
6006 dictAdd(dstzset
->dict
,o
,score
);
6007 incrRefCount(o
); /* added to dictionary */
6008 zslInsert(dstzset
->zsl
,*score
,o
);
6009 incrRefCount(o
); /* added to skiplist */
6012 dictReleaseIterator(di
);
6014 } else if (op
== REDIS_OP_UNION
) {
6015 for (i
= 0; i
< zsetnum
; i
++) {
6016 if (!src
[i
].dict
) continue;
6018 di
= dictGetIterator(src
[i
].dict
);
6019 while((de
= dictNext(di
)) != NULL
) {
6020 /* skip key when already processed */
6021 if (dictFind(dstzset
->dict
,dictGetEntryKey(de
)) != NULL
) continue;
6023 double *score
= zmalloc(sizeof(double)), value
;
6024 *score
= src
[i
].weight
* (*(double*)dictGetEntryVal(de
));
6026 /* because the zsets are sorted by size, its only possible
6027 * for sets at larger indices to hold this entry */
6028 for (j
= (i
+1); j
< zsetnum
; j
++) {
6029 dictEntry
*other
= dictFind(src
[j
].dict
,dictGetEntryKey(de
));
6031 value
= src
[j
].weight
* (*(double*)dictGetEntryVal(other
));
6032 zunionInterAggregate(score
, value
, aggregate
);
6036 robj
*o
= dictGetEntryKey(de
);
6037 dictAdd(dstzset
->dict
,o
,score
);
6038 incrRefCount(o
); /* added to dictionary */
6039 zslInsert(dstzset
->zsl
,*score
,o
);
6040 incrRefCount(o
); /* added to skiplist */
6042 dictReleaseIterator(di
);
6045 /* unknown operator */
6046 redisAssert(op
== REDIS_OP_INTER
|| op
== REDIS_OP_UNION
);
6049 deleteKey(c
->db
,dstkey
);
6050 if (dstzset
->zsl
->length
) {
6051 dictAdd(c
->db
->dict
,dstkey
,dstobj
);
6052 incrRefCount(dstkey
);
6053 addReplyLongLong(c
, dstzset
->zsl
->length
);
6056 decrRefCount(dstobj
);
6057 addReply(c
, shared
.czero
);
6062 static void zunionstoreCommand(redisClient
*c
) {
6063 zunionInterGenericCommand(c
,c
->argv
[1], REDIS_OP_UNION
);
6066 static void zinterstoreCommand(redisClient
*c
) {
6067 zunionInterGenericCommand(c
,c
->argv
[1], REDIS_OP_INTER
);
6070 static void zrangeGenericCommand(redisClient
*c
, int reverse
) {
6082 if ((getLongFromObjectOrReply(c
, c
->argv
[2], &start
, NULL
) != REDIS_OK
) ||
6083 (getLongFromObjectOrReply(c
, c
->argv
[3], &end
, NULL
) != REDIS_OK
)) return;
6085 if (c
->argc
== 5 && !strcasecmp(c
->argv
[4]->ptr
,"withscores")) {
6087 } else if (c
->argc
>= 5) {
6088 addReply(c
,shared
.syntaxerr
);
6092 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.emptymultibulk
)) == NULL
6093 || checkType(c
,o
,REDIS_ZSET
)) return;
6098 /* convert negative indexes */
6099 if (start
< 0) start
= llen
+start
;
6100 if (end
< 0) end
= llen
+end
;
6101 if (start
< 0) start
= 0;
6102 if (end
< 0) end
= 0;
6104 /* indexes sanity checks */
6105 if (start
> end
|| start
>= llen
) {
6106 /* Out of range start or start > end result in empty list */
6107 addReply(c
,shared
.emptymultibulk
);
6110 if (end
>= llen
) end
= llen
-1;
6111 rangelen
= (end
-start
)+1;
6113 /* check if starting point is trivial, before searching
6114 * the element in log(N) time */
6116 ln
= start
== 0 ? zsl
->tail
: zslGetElementByRank(zsl
, llen
-start
);
6119 zsl
->header
->forward
[0] : zslGetElementByRank(zsl
, start
+1);
6122 /* Return the result in form of a multi-bulk reply */
6123 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",
6124 withscores
? (rangelen
*2) : rangelen
));
6125 for (j
= 0; j
< rangelen
; j
++) {
6127 addReplyBulk(c
,ele
);
6129 addReplyDouble(c
,ln
->score
);
6130 ln
= reverse
? ln
->backward
: ln
->forward
[0];
6134 static void zrangeCommand(redisClient
*c
) {
6135 zrangeGenericCommand(c
,0);
6138 static void zrevrangeCommand(redisClient
*c
) {
6139 zrangeGenericCommand(c
,1);
6142 /* This command implements both ZRANGEBYSCORE and ZCOUNT.
6143 * If justcount is non-zero, just the count is returned. */
6144 static void genericZrangebyscoreCommand(redisClient
*c
, int justcount
) {
6147 int minex
= 0, maxex
= 0; /* are min or max exclusive? */
6148 int offset
= 0, limit
= -1;
6152 /* Parse the min-max interval. If one of the values is prefixed
6153 * by the "(" character, it's considered "open". For instance
6154 * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max
6155 * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */
6156 if (((char*)c
->argv
[2]->ptr
)[0] == '(') {
6157 min
= strtod((char*)c
->argv
[2]->ptr
+1,NULL
);
6160 min
= strtod(c
->argv
[2]->ptr
,NULL
);
6162 if (((char*)c
->argv
[3]->ptr
)[0] == '(') {
6163 max
= strtod((char*)c
->argv
[3]->ptr
+1,NULL
);
6166 max
= strtod(c
->argv
[3]->ptr
,NULL
);
6169 /* Parse "WITHSCORES": note that if the command was called with
6170 * the name ZCOUNT then we are sure that c->argc == 4, so we'll never
6171 * enter the following paths to parse WITHSCORES and LIMIT. */
6172 if (c
->argc
== 5 || c
->argc
== 8) {
6173 if (strcasecmp(c
->argv
[c
->argc
-1]->ptr
,"withscores") == 0)
6178 if (c
->argc
!= (4 + withscores
) && c
->argc
!= (7 + withscores
))
6182 sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
6187 if (c
->argc
== (7 + withscores
) && strcasecmp(c
->argv
[4]->ptr
,"limit")) {
6188 addReply(c
,shared
.syntaxerr
);
6190 } else if (c
->argc
== (7 + withscores
)) {
6191 offset
= atoi(c
->argv
[5]->ptr
);
6192 limit
= atoi(c
->argv
[6]->ptr
);
6193 if (offset
< 0) offset
= 0;
6196 /* Ok, lookup the key and get the range */
6197 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
6199 addReply(c
,justcount
? shared
.czero
: shared
.emptymultibulk
);
6201 if (o
->type
!= REDIS_ZSET
) {
6202 addReply(c
,shared
.wrongtypeerr
);
6204 zset
*zsetobj
= o
->ptr
;
6205 zskiplist
*zsl
= zsetobj
->zsl
;
6207 robj
*ele
, *lenobj
= NULL
;
6208 unsigned long rangelen
= 0;
6210 /* Get the first node with the score >= min, or with
6211 * score > min if 'minex' is true. */
6212 ln
= zslFirstWithScore(zsl
,min
);
6213 while (minex
&& ln
&& ln
->score
== min
) ln
= ln
->forward
[0];
6216 /* No element matching the speciifed interval */
6217 addReply(c
,justcount
? shared
.czero
: shared
.emptymultibulk
);
6221 /* We don't know in advance how many matching elements there
6222 * are in the list, so we push this object that will represent
6223 * the multi-bulk length in the output buffer, and will "fix"
6226 lenobj
= createObject(REDIS_STRING
,NULL
);
6228 decrRefCount(lenobj
);
6231 while(ln
&& (maxex
? (ln
->score
< max
) : (ln
->score
<= max
))) {
6234 ln
= ln
->forward
[0];
6237 if (limit
== 0) break;
6240 addReplyBulk(c
,ele
);
6242 addReplyDouble(c
,ln
->score
);
6244 ln
= ln
->forward
[0];
6246 if (limit
> 0) limit
--;
6249 addReplyLongLong(c
,(long)rangelen
);
6251 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",
6252 withscores
? (rangelen
*2) : rangelen
);
6258 static void zrangebyscoreCommand(redisClient
*c
) {
6259 genericZrangebyscoreCommand(c
,0);
6262 static void zcountCommand(redisClient
*c
) {
6263 genericZrangebyscoreCommand(c
,1);
6266 static void zcardCommand(redisClient
*c
) {
6270 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6271 checkType(c
,o
,REDIS_ZSET
)) return;
6274 addReplyUlong(c
,zs
->zsl
->length
);
6277 static void zscoreCommand(redisClient
*c
) {
6282 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
6283 checkType(c
,o
,REDIS_ZSET
)) return;
6286 de
= dictFind(zs
->dict
,c
->argv
[2]);
6288 addReply(c
,shared
.nullbulk
);
6290 double *score
= dictGetEntryVal(de
);
6292 addReplyDouble(c
,*score
);
6296 static void zrankGenericCommand(redisClient
*c
, int reverse
) {
6304 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
6305 checkType(c
,o
,REDIS_ZSET
)) return;
6309 de
= dictFind(zs
->dict
,c
->argv
[2]);
6311 addReply(c
,shared
.nullbulk
);
6315 score
= dictGetEntryVal(de
);
6316 rank
= zslGetRank(zsl
, *score
, c
->argv
[2]);
6319 addReplyLongLong(c
, zsl
->length
- rank
);
6321 addReplyLongLong(c
, rank
-1);
6324 addReply(c
,shared
.nullbulk
);
6328 static void zrankCommand(redisClient
*c
) {
6329 zrankGenericCommand(c
, 0);
6332 static void zrevrankCommand(redisClient
*c
) {
6333 zrankGenericCommand(c
, 1);
6336 /* ========================= Hashes utility functions ======================= */
6337 #define REDIS_HASH_KEY 1
6338 #define REDIS_HASH_VALUE 2
6340 /* Check the length of a number of objects to see if we need to convert a
6341 * zipmap to a real hash. Note that we only check string encoded objects
6342 * as their string length can be queried in constant time. */
6343 static void hashTryConversion(robj
*subject
, robj
**argv
, int start
, int end
) {
6345 if (subject
->encoding
!= REDIS_ENCODING_ZIPMAP
) return;
6347 for (i
= start
; i
<= end
; i
++) {
6348 if (argv
[i
]->encoding
== REDIS_ENCODING_RAW
&&
6349 sdslen(argv
[i
]->ptr
) > server
.hash_max_zipmap_value
)
6351 convertToRealHash(subject
);
6357 /* Encode given objects in-place when the hash uses a dict. */
6358 static void hashTryObjectEncoding(robj
*subject
, robj
**o1
, robj
**o2
) {
6359 if (subject
->encoding
== REDIS_ENCODING_HT
) {
6360 if (o1
) *o1
= tryObjectEncoding(*o1
);
6361 if (o2
) *o2
= tryObjectEncoding(*o2
);
6365 /* Get the value from a hash identified by key. Returns either a string
6366 * object or NULL if the value cannot be found. The refcount of the object
6367 * is always increased by 1 when the value was found. */
6368 static robj
*hashGet(robj
*o
, robj
*key
) {
6370 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6373 key
= getDecodedObject(key
);
6374 if (zipmapGet(o
->ptr
,key
->ptr
,sdslen(key
->ptr
),&v
,&vlen
)) {
6375 value
= createStringObject((char*)v
,vlen
);
6379 dictEntry
*de
= dictFind(o
->ptr
,key
);
6381 value
= dictGetEntryVal(de
);
6382 incrRefCount(value
);
6388 /* Test if the key exists in the given hash. Returns 1 if the key
6389 * exists and 0 when it doesn't. */
6390 static int hashExists(robj
*o
, robj
*key
) {
6391 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6392 key
= getDecodedObject(key
);
6393 if (zipmapExists(o
->ptr
,key
->ptr
,sdslen(key
->ptr
))) {
6399 if (dictFind(o
->ptr
,key
) != NULL
) {
6406 /* Add an element, discard the old if the key already exists.
6407 * Return 0 on insert and 1 on update. */
6408 static int hashSet(robj
*o
, robj
*key
, robj
*value
) {
6410 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6411 key
= getDecodedObject(key
);
6412 value
= getDecodedObject(value
);
6413 o
->ptr
= zipmapSet(o
->ptr
,
6414 key
->ptr
,sdslen(key
->ptr
),
6415 value
->ptr
,sdslen(value
->ptr
), &update
);
6417 decrRefCount(value
);
6419 /* Check if the zipmap needs to be upgraded to a real hash table */
6420 if (zipmapLen(o
->ptr
) > server
.hash_max_zipmap_entries
)
6421 convertToRealHash(o
);
6423 if (dictReplace(o
->ptr
,key
,value
)) {
6430 incrRefCount(value
);
6435 /* Delete an element from a hash.
6436 * Return 1 on deleted and 0 on not found. */
6437 static int hashDelete(robj
*o
, robj
*key
) {
6439 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6440 key
= getDecodedObject(key
);
6441 o
->ptr
= zipmapDel(o
->ptr
,key
->ptr
,sdslen(key
->ptr
), &deleted
);
6444 deleted
= dictDelete((dict
*)o
->ptr
,key
) == DICT_OK
;
6445 /* Always check if the dictionary needs a resize after a delete. */
6446 if (deleted
&& htNeedsResize(o
->ptr
)) dictResize(o
->ptr
);
6451 /* Return the number of elements in a hash. */
6452 static unsigned long hashLength(robj
*o
) {
6453 return (o
->encoding
== REDIS_ENCODING_ZIPMAP
) ?
6454 zipmapLen((unsigned char*)o
->ptr
) : dictSize((dict
*)o
->ptr
);
6457 /* Structure to hold hash iteration abstration. Note that iteration over
6458 * hashes involves both fields and values. Because it is possible that
6459 * not both are required, store pointers in the iterator to avoid
6460 * unnecessary memory allocation for fields/values. */
6464 unsigned char *zk
, *zv
;
6465 unsigned int zklen
, zvlen
;
6471 static hashIterator
*hashInitIterator(robj
*subject
) {
6472 hashIterator
*hi
= zmalloc(sizeof(hashIterator
));
6473 hi
->encoding
= subject
->encoding
;
6474 if (hi
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6475 hi
->zi
= zipmapRewind(subject
->ptr
);
6476 } else if (hi
->encoding
== REDIS_ENCODING_HT
) {
6477 hi
->di
= dictGetIterator(subject
->ptr
);
6484 static void hashReleaseIterator(hashIterator
*hi
) {
6485 if (hi
->encoding
== REDIS_ENCODING_HT
) {
6486 dictReleaseIterator(hi
->di
);
6491 /* Move to the next entry in the hash. Return REDIS_OK when the next entry
6492 * could be found and REDIS_ERR when the iterator reaches the end. */
6493 static int hashNext(hashIterator
*hi
) {
6494 if (hi
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6495 if ((hi
->zi
= zipmapNext(hi
->zi
, &hi
->zk
, &hi
->zklen
,
6496 &hi
->zv
, &hi
->zvlen
)) == NULL
) return REDIS_ERR
;
6498 if ((hi
->de
= dictNext(hi
->di
)) == NULL
) return REDIS_ERR
;
6503 /* Get key or value object at current iteration position.
6504 * This increases the refcount of the field object by 1. */
6505 static robj
*hashCurrent(hashIterator
*hi
, int what
) {
6507 if (hi
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6508 if (what
& REDIS_HASH_KEY
) {
6509 o
= createStringObject((char*)hi
->zk
,hi
->zklen
);
6511 o
= createStringObject((char*)hi
->zv
,hi
->zvlen
);
6514 if (what
& REDIS_HASH_KEY
) {
6515 o
= dictGetEntryKey(hi
->de
);
6517 o
= dictGetEntryVal(hi
->de
);
6524 static robj
*hashLookupWriteOrCreate(redisClient
*c
, robj
*key
) {
6525 robj
*o
= lookupKeyWrite(c
->db
,key
);
6527 o
= createHashObject();
6528 dictAdd(c
->db
->dict
,key
,o
);
6531 if (o
->type
!= REDIS_HASH
) {
6532 addReply(c
,shared
.wrongtypeerr
);
6539 /* ============================= Hash commands ============================== */
6540 static void hsetCommand(redisClient
*c
) {
6544 if ((o
= hashLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
6545 hashTryConversion(o
,c
->argv
,2,3);
6546 hashTryObjectEncoding(o
,&c
->argv
[2], &c
->argv
[3]);
6547 update
= hashSet(o
,c
->argv
[2],c
->argv
[3]);
6548 addReply(c
, update
? shared
.czero
: shared
.cone
);
6552 static void hsetnxCommand(redisClient
*c
) {
6554 if ((o
= hashLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
6555 hashTryConversion(o
,c
->argv
,2,3);
6557 if (hashExists(o
, c
->argv
[2])) {
6558 addReply(c
, shared
.czero
);
6560 hashTryObjectEncoding(o
,&c
->argv
[2], &c
->argv
[3]);
6561 hashSet(o
,c
->argv
[2],c
->argv
[3]);
6562 addReply(c
, shared
.cone
);
6567 static void hmsetCommand(redisClient
*c
) {
6571 if ((c
->argc
% 2) == 1) {
6572 addReplySds(c
,sdsnew("-ERR wrong number of arguments for HMSET\r\n"));
6576 if ((o
= hashLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
6577 hashTryConversion(o
,c
->argv
,2,c
->argc
-1);
6578 for (i
= 2; i
< c
->argc
; i
+= 2) {
6579 hashTryObjectEncoding(o
,&c
->argv
[i
], &c
->argv
[i
+1]);
6580 hashSet(o
,c
->argv
[i
],c
->argv
[i
+1]);
6582 addReply(c
, shared
.ok
);
6586 static void hincrbyCommand(redisClient
*c
) {
6587 long long value
, incr
;
6588 robj
*o
, *current
, *new;
6590 if (getLongLongFromObjectOrReply(c
,c
->argv
[3],&incr
,NULL
) != REDIS_OK
) return;
6591 if ((o
= hashLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
6592 if ((current
= hashGet(o
,c
->argv
[2])) != NULL
) {
6593 if (getLongLongFromObjectOrReply(c
,current
,&value
,
6594 "hash value is not an integer") != REDIS_OK
) {
6595 decrRefCount(current
);
6598 decrRefCount(current
);
6604 new = createStringObjectFromLongLong(value
);
6605 hashTryObjectEncoding(o
,&c
->argv
[2],NULL
);
6606 hashSet(o
,c
->argv
[2],new);
6608 addReplyLongLong(c
,value
);
6612 static void hgetCommand(redisClient
*c
) {
6614 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
6615 checkType(c
,o
,REDIS_HASH
)) return;
6617 if ((value
= hashGet(o
,c
->argv
[2])) != NULL
) {
6618 addReplyBulk(c
,value
);
6619 decrRefCount(value
);
6621 addReply(c
,shared
.nullbulk
);
6625 static void hmgetCommand(redisClient
*c
) {
6628 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
6629 if (o
!= NULL
&& o
->type
!= REDIS_HASH
) {
6630 addReply(c
,shared
.wrongtypeerr
);
6633 /* Note the check for o != NULL happens inside the loop. This is
6634 * done because objects that cannot be found are considered to be
6635 * an empty hash. The reply should then be a series of NULLs. */
6636 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-2));
6637 for (i
= 2; i
< c
->argc
; i
++) {
6638 if (o
!= NULL
&& (value
= hashGet(o
,c
->argv
[i
])) != NULL
) {
6639 addReplyBulk(c
,value
);
6640 decrRefCount(value
);
6642 addReply(c
,shared
.nullbulk
);
6647 static void hdelCommand(redisClient
*c
) {
6649 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6650 checkType(c
,o
,REDIS_HASH
)) return;
6652 if (hashDelete(o
,c
->argv
[2])) {
6653 if (hashLength(o
) == 0) deleteKey(c
->db
,c
->argv
[1]);
6654 addReply(c
,shared
.cone
);
6657 addReply(c
,shared
.czero
);
6661 static void hlenCommand(redisClient
*c
) {
6663 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6664 checkType(c
,o
,REDIS_HASH
)) return;
6666 addReplyUlong(c
,hashLength(o
));
6669 static void genericHgetallCommand(redisClient
*c
, int flags
) {
6670 robj
*o
, *lenobj
, *obj
;
6671 unsigned long count
= 0;
6674 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.emptymultibulk
)) == NULL
6675 || checkType(c
,o
,REDIS_HASH
)) return;
6677 lenobj
= createObject(REDIS_STRING
,NULL
);
6679 decrRefCount(lenobj
);
6681 hi
= hashInitIterator(o
);
6682 while (hashNext(hi
) != REDIS_ERR
) {
6683 if (flags
& REDIS_HASH_KEY
) {
6684 obj
= hashCurrent(hi
,REDIS_HASH_KEY
);
6685 addReplyBulk(c
,obj
);
6689 if (flags
& REDIS_HASH_VALUE
) {
6690 obj
= hashCurrent(hi
,REDIS_HASH_VALUE
);
6691 addReplyBulk(c
,obj
);
6696 hashReleaseIterator(hi
);
6698 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",count
);
6701 static void hkeysCommand(redisClient
*c
) {
6702 genericHgetallCommand(c
,REDIS_HASH_KEY
);
6705 static void hvalsCommand(redisClient
*c
) {
6706 genericHgetallCommand(c
,REDIS_HASH_VALUE
);
6709 static void hgetallCommand(redisClient
*c
) {
6710 genericHgetallCommand(c
,REDIS_HASH_KEY
|REDIS_HASH_VALUE
);
6713 static void hexistsCommand(redisClient
*c
) {
6715 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6716 checkType(c
,o
,REDIS_HASH
)) return;
6718 addReply(c
, hashExists(o
,c
->argv
[2]) ? shared
.cone
: shared
.czero
);
6721 static void convertToRealHash(robj
*o
) {
6722 unsigned char *key
, *val
, *p
, *zm
= o
->ptr
;
6723 unsigned int klen
, vlen
;
6724 dict
*dict
= dictCreate(&hashDictType
,NULL
);
6726 assert(o
->type
== REDIS_HASH
&& o
->encoding
!= REDIS_ENCODING_HT
);
6727 p
= zipmapRewind(zm
);
6728 while((p
= zipmapNext(p
,&key
,&klen
,&val
,&vlen
)) != NULL
) {
6729 robj
*keyobj
, *valobj
;
6731 keyobj
= createStringObject((char*)key
,klen
);
6732 valobj
= createStringObject((char*)val
,vlen
);
6733 keyobj
= tryObjectEncoding(keyobj
);
6734 valobj
= tryObjectEncoding(valobj
);
6735 dictAdd(dict
,keyobj
,valobj
);
6737 o
->encoding
= REDIS_ENCODING_HT
;
6742 /* ========================= Non type-specific commands ==================== */
6744 static void flushdbCommand(redisClient
*c
) {
6745 server
.dirty
+= dictSize(c
->db
->dict
);
6746 dictEmpty(c
->db
->dict
);
6747 dictEmpty(c
->db
->expires
);
6748 addReply(c
,shared
.ok
);
6751 static void flushallCommand(redisClient
*c
) {
6752 server
.dirty
+= emptyDb();
6753 addReply(c
,shared
.ok
);
6754 if (server
.bgsavechildpid
!= -1) {
6755 kill(server
.bgsavechildpid
,SIGKILL
);
6756 rdbRemoveTempFile(server
.bgsavechildpid
);
6758 rdbSave(server
.dbfilename
);
6762 static redisSortOperation
*createSortOperation(int type
, robj
*pattern
) {
6763 redisSortOperation
*so
= zmalloc(sizeof(*so
));
6765 so
->pattern
= pattern
;
6769 /* Return the value associated to the key with a name obtained
6770 * substituting the first occurence of '*' in 'pattern' with 'subst'.
6771 * The returned object will always have its refcount increased by 1
6772 * when it is non-NULL. */
6773 static robj
*lookupKeyByPattern(redisDb
*db
, robj
*pattern
, robj
*subst
) {
6776 robj keyobj
, fieldobj
, *o
;
6777 int prefixlen
, sublen
, postfixlen
, fieldlen
;
6778 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
6782 char buf
[REDIS_SORTKEY_MAX
+1];
6783 } keyname
, fieldname
;
6785 /* If the pattern is "#" return the substitution object itself in order
6786 * to implement the "SORT ... GET #" feature. */
6787 spat
= pattern
->ptr
;
6788 if (spat
[0] == '#' && spat
[1] == '\0') {
6789 incrRefCount(subst
);
6793 /* The substitution object may be specially encoded. If so we create
6794 * a decoded object on the fly. Otherwise getDecodedObject will just
6795 * increment the ref count, that we'll decrement later. */
6796 subst
= getDecodedObject(subst
);
6799 if (sdslen(spat
)+sdslen(ssub
)-1 > REDIS_SORTKEY_MAX
) return NULL
;
6800 p
= strchr(spat
,'*');
6802 decrRefCount(subst
);
6806 /* Find out if we're dealing with a hash dereference. */
6807 if ((f
= strstr(p
+1, "->")) != NULL
) {
6808 fieldlen
= sdslen(spat
)-(f
-spat
);
6809 /* this also copies \0 character */
6810 memcpy(fieldname
.buf
,f
+2,fieldlen
-1);
6811 fieldname
.len
= fieldlen
-2;
6817 sublen
= sdslen(ssub
);
6818 postfixlen
= sdslen(spat
)-(prefixlen
+1)-fieldlen
;
6819 memcpy(keyname
.buf
,spat
,prefixlen
);
6820 memcpy(keyname
.buf
+prefixlen
,ssub
,sublen
);
6821 memcpy(keyname
.buf
+prefixlen
+sublen
,p
+1,postfixlen
);
6822 keyname
.buf
[prefixlen
+sublen
+postfixlen
] = '\0';
6823 keyname
.len
= prefixlen
+sublen
+postfixlen
;
6824 decrRefCount(subst
);
6826 /* Lookup substituted key */
6827 initStaticStringObject(keyobj
,((char*)&keyname
)+(sizeof(long)*2));
6828 o
= lookupKeyRead(db
,&keyobj
);
6829 if (o
== NULL
) return NULL
;
6832 if (o
->type
!= REDIS_HASH
|| fieldname
.len
< 1) return NULL
;
6834 /* Retrieve value from hash by the field name. This operation
6835 * already increases the refcount of the returned object. */
6836 initStaticStringObject(fieldobj
,((char*)&fieldname
)+(sizeof(long)*2));
6837 o
= hashGet(o
, &fieldobj
);
6839 if (o
->type
!= REDIS_STRING
) return NULL
;
6841 /* Every object that this function returns needs to have its refcount
6842 * increased. sortCommand decreases it again. */
6849 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
6850 * the additional parameter is not standard but a BSD-specific we have to
6851 * pass sorting parameters via the global 'server' structure */
6852 static int sortCompare(const void *s1
, const void *s2
) {
6853 const redisSortObject
*so1
= s1
, *so2
= s2
;
6856 if (!server
.sort_alpha
) {
6857 /* Numeric sorting. Here it's trivial as we precomputed scores */
6858 if (so1
->u
.score
> so2
->u
.score
) {
6860 } else if (so1
->u
.score
< so2
->u
.score
) {
6866 /* Alphanumeric sorting */
6867 if (server
.sort_bypattern
) {
6868 if (!so1
->u
.cmpobj
|| !so2
->u
.cmpobj
) {
6869 /* At least one compare object is NULL */
6870 if (so1
->u
.cmpobj
== so2
->u
.cmpobj
)
6872 else if (so1
->u
.cmpobj
== NULL
)
6877 /* We have both the objects, use strcoll */
6878 cmp
= strcoll(so1
->u
.cmpobj
->ptr
,so2
->u
.cmpobj
->ptr
);
6881 /* Compare elements directly. */
6882 cmp
= compareStringObjects(so1
->obj
,so2
->obj
);
6885 return server
.sort_desc
? -cmp
: cmp
;
6888 /* The SORT command is the most complex command in Redis. Warning: this code
6889 * is optimized for speed and a bit less for readability */
6890 static void sortCommand(redisClient
*c
) {
6893 int desc
= 0, alpha
= 0;
6894 int limit_start
= 0, limit_count
= -1, start
, end
;
6895 int j
, dontsort
= 0, vectorlen
;
6896 int getop
= 0; /* GET operation counter */
6897 robj
*sortval
, *sortby
= NULL
, *storekey
= NULL
;
6898 redisSortObject
*vector
; /* Resulting vector to sort */
6900 /* Lookup the key to sort. It must be of the right types */
6901 sortval
= lookupKeyRead(c
->db
,c
->argv
[1]);
6902 if (sortval
== NULL
) {
6903 addReply(c
,shared
.emptymultibulk
);
6906 if (sortval
->type
!= REDIS_SET
&& sortval
->type
!= REDIS_LIST
&&
6907 sortval
->type
!= REDIS_ZSET
)
6909 addReply(c
,shared
.wrongtypeerr
);
6913 /* Create a list of operations to perform for every sorted element.
6914 * Operations can be GET/DEL/INCR/DECR */
6915 operations
= listCreate();
6916 listSetFreeMethod(operations
,zfree
);
6919 /* Now we need to protect sortval incrementing its count, in the future
6920 * SORT may have options able to overwrite/delete keys during the sorting
6921 * and the sorted key itself may get destroied */
6922 incrRefCount(sortval
);
6924 /* The SORT command has an SQL-alike syntax, parse it */
6925 while(j
< c
->argc
) {
6926 int leftargs
= c
->argc
-j
-1;
6927 if (!strcasecmp(c
->argv
[j
]->ptr
,"asc")) {
6929 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"desc")) {
6931 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"alpha")) {
6933 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"limit") && leftargs
>= 2) {
6934 limit_start
= atoi(c
->argv
[j
+1]->ptr
);
6935 limit_count
= atoi(c
->argv
[j
+2]->ptr
);
6937 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"store") && leftargs
>= 1) {
6938 storekey
= c
->argv
[j
+1];
6940 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"by") && leftargs
>= 1) {
6941 sortby
= c
->argv
[j
+1];
6942 /* If the BY pattern does not contain '*', i.e. it is constant,
6943 * we don't need to sort nor to lookup the weight keys. */
6944 if (strchr(c
->argv
[j
+1]->ptr
,'*') == NULL
) dontsort
= 1;
6946 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
6947 listAddNodeTail(operations
,createSortOperation(
6948 REDIS_SORT_GET
,c
->argv
[j
+1]));
6952 decrRefCount(sortval
);
6953 listRelease(operations
);
6954 addReply(c
,shared
.syntaxerr
);
6960 /* Load the sorting vector with all the objects to sort */
6961 switch(sortval
->type
) {
6962 case REDIS_LIST
: vectorlen
= listLength((list
*)sortval
->ptr
); break;
6963 case REDIS_SET
: vectorlen
= dictSize((dict
*)sortval
->ptr
); break;
6964 case REDIS_ZSET
: vectorlen
= dictSize(((zset
*)sortval
->ptr
)->dict
); break;
6965 default: vectorlen
= 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
6967 vector
= zmalloc(sizeof(redisSortObject
)*vectorlen
);
6970 if (sortval
->type
== REDIS_LIST
) {
6971 list
*list
= sortval
->ptr
;
6975 listRewind(list
,&li
);
6976 while((ln
= listNext(&li
))) {
6977 robj
*ele
= ln
->value
;
6978 vector
[j
].obj
= ele
;
6979 vector
[j
].u
.score
= 0;
6980 vector
[j
].u
.cmpobj
= NULL
;
6988 if (sortval
->type
== REDIS_SET
) {
6991 zset
*zs
= sortval
->ptr
;
6995 di
= dictGetIterator(set
);
6996 while((setele
= dictNext(di
)) != NULL
) {
6997 vector
[j
].obj
= dictGetEntryKey(setele
);
6998 vector
[j
].u
.score
= 0;
6999 vector
[j
].u
.cmpobj
= NULL
;
7002 dictReleaseIterator(di
);
7004 redisAssert(j
== vectorlen
);
7006 /* Now it's time to load the right scores in the sorting vector */
7007 if (dontsort
== 0) {
7008 for (j
= 0; j
< vectorlen
; j
++) {
7011 /* lookup value to sort by */
7012 byval
= lookupKeyByPattern(c
->db
,sortby
,vector
[j
].obj
);
7013 if (!byval
) continue;
7015 /* use object itself to sort by */
7016 byval
= vector
[j
].obj
;
7020 if (sortby
) vector
[j
].u
.cmpobj
= getDecodedObject(byval
);
7022 if (byval
->encoding
== REDIS_ENCODING_RAW
) {
7023 vector
[j
].u
.score
= strtod(byval
->ptr
,NULL
);
7024 } else if (byval
->encoding
== REDIS_ENCODING_INT
) {
7025 /* Don't need to decode the object if it's
7026 * integer-encoded (the only encoding supported) so
7027 * far. We can just cast it */
7028 vector
[j
].u
.score
= (long)byval
->ptr
;
7030 redisAssert(1 != 1);
7034 /* when the object was retrieved using lookupKeyByPattern,
7035 * its refcount needs to be decreased. */
7037 decrRefCount(byval
);
7042 /* We are ready to sort the vector... perform a bit of sanity check
7043 * on the LIMIT option too. We'll use a partial version of quicksort. */
7044 start
= (limit_start
< 0) ? 0 : limit_start
;
7045 end
= (limit_count
< 0) ? vectorlen
-1 : start
+limit_count
-1;
7046 if (start
>= vectorlen
) {
7047 start
= vectorlen
-1;
7050 if (end
>= vectorlen
) end
= vectorlen
-1;
7052 if (dontsort
== 0) {
7053 server
.sort_desc
= desc
;
7054 server
.sort_alpha
= alpha
;
7055 server
.sort_bypattern
= sortby
? 1 : 0;
7056 if (sortby
&& (start
!= 0 || end
!= vectorlen
-1))
7057 pqsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
, start
,end
);
7059 qsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
);
7062 /* Send command output to the output buffer, performing the specified
7063 * GET/DEL/INCR/DECR operations if any. */
7064 outputlen
= getop
? getop
*(end
-start
+1) : end
-start
+1;
7065 if (storekey
== NULL
) {
7066 /* STORE option not specified, sent the sorting result to client */
7067 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",outputlen
));
7068 for (j
= start
; j
<= end
; j
++) {
7072 if (!getop
) addReplyBulk(c
,vector
[j
].obj
);
7073 listRewind(operations
,&li
);
7074 while((ln
= listNext(&li
))) {
7075 redisSortOperation
*sop
= ln
->value
;
7076 robj
*val
= lookupKeyByPattern(c
->db
,sop
->pattern
,
7079 if (sop
->type
== REDIS_SORT_GET
) {
7081 addReply(c
,shared
.nullbulk
);
7083 addReplyBulk(c
,val
);
7087 redisAssert(sop
->type
== REDIS_SORT_GET
); /* always fails */
7092 robj
*listObject
= createListObject();
7093 list
*listPtr
= (list
*) listObject
->ptr
;
7095 /* STORE option specified, set the sorting result as a List object */
7096 for (j
= start
; j
<= end
; j
++) {
7101 listAddNodeTail(listPtr
,vector
[j
].obj
);
7102 incrRefCount(vector
[j
].obj
);
7104 listRewind(operations
,&li
);
7105 while((ln
= listNext(&li
))) {
7106 redisSortOperation
*sop
= ln
->value
;
7107 robj
*val
= lookupKeyByPattern(c
->db
,sop
->pattern
,
7110 if (sop
->type
== REDIS_SORT_GET
) {
7112 listAddNodeTail(listPtr
,createStringObject("",0));
7114 /* We should do a incrRefCount on val because it is
7115 * added to the list, but also a decrRefCount because
7116 * it is returned by lookupKeyByPattern. This results
7117 * in doing nothing at all. */
7118 listAddNodeTail(listPtr
,val
);
7121 redisAssert(sop
->type
== REDIS_SORT_GET
); /* always fails */
7125 if (dictReplace(c
->db
->dict
,storekey
,listObject
)) {
7126 incrRefCount(storekey
);
7128 /* Note: we add 1 because the DB is dirty anyway since even if the
7129 * SORT result is empty a new key is set and maybe the old content
7131 server
.dirty
+= 1+outputlen
;
7132 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",outputlen
));
7136 decrRefCount(sortval
);
7137 listRelease(operations
);
7138 for (j
= 0; j
< vectorlen
; j
++) {
7139 if (alpha
&& vector
[j
].u
.cmpobj
)
7140 decrRefCount(vector
[j
].u
.cmpobj
);
7145 /* Convert an amount of bytes into a human readable string in the form
7146 * of 100B, 2G, 100M, 4K, and so forth. */
7147 static void bytesToHuman(char *s
, unsigned long long n
) {
7152 sprintf(s
,"%lluB",n
);
7154 } else if (n
< (1024*1024)) {
7155 d
= (double)n
/(1024);
7156 sprintf(s
,"%.2fK",d
);
7157 } else if (n
< (1024LL*1024*1024)) {
7158 d
= (double)n
/(1024*1024);
7159 sprintf(s
,"%.2fM",d
);
7160 } else if (n
< (1024LL*1024*1024*1024)) {
7161 d
= (double)n
/(1024LL*1024*1024);
7162 sprintf(s
,"%.2fG",d
);
7166 /* Create the string returned by the INFO command. This is decoupled
7167 * by the INFO command itself as we need to report the same information
7168 * on memory corruption problems. */
7169 static sds
genRedisInfoString(void) {
7171 time_t uptime
= time(NULL
)-server
.stat_starttime
;
7175 bytesToHuman(hmem
,zmalloc_used_memory());
7176 info
= sdscatprintf(sdsempty(),
7177 "redis_version:%s\r\n"
7179 "multiplexing_api:%s\r\n"
7180 "process_id:%ld\r\n"
7181 "uptime_in_seconds:%ld\r\n"
7182 "uptime_in_days:%ld\r\n"
7183 "connected_clients:%d\r\n"
7184 "connected_slaves:%d\r\n"
7185 "blocked_clients:%d\r\n"
7186 "used_memory:%zu\r\n"
7187 "used_memory_human:%s\r\n"
7188 "changes_since_last_save:%lld\r\n"
7189 "bgsave_in_progress:%d\r\n"
7190 "last_save_time:%ld\r\n"
7191 "bgrewriteaof_in_progress:%d\r\n"
7192 "total_connections_received:%lld\r\n"
7193 "total_commands_processed:%lld\r\n"
7194 "expired_keys:%lld\r\n"
7195 "hash_max_zipmap_entries:%zu\r\n"
7196 "hash_max_zipmap_value:%zu\r\n"
7197 "pubsub_channels:%ld\r\n"
7198 "pubsub_patterns:%u\r\n"
7202 (sizeof(long) == 8) ? "64" : "32",
7207 listLength(server
.clients
)-listLength(server
.slaves
),
7208 listLength(server
.slaves
),
7209 server
.blpop_blocked_clients
,
7210 zmalloc_used_memory(),
7213 server
.bgsavechildpid
!= -1,
7215 server
.bgrewritechildpid
!= -1,
7216 server
.stat_numconnections
,
7217 server
.stat_numcommands
,
7218 server
.stat_expiredkeys
,
7219 server
.hash_max_zipmap_entries
,
7220 server
.hash_max_zipmap_value
,
7221 dictSize(server
.pubsub_channels
),
7222 listLength(server
.pubsub_patterns
),
7223 server
.vm_enabled
!= 0,
7224 server
.masterhost
== NULL
? "master" : "slave"
7226 if (server
.masterhost
) {
7227 info
= sdscatprintf(info
,
7228 "master_host:%s\r\n"
7229 "master_port:%d\r\n"
7230 "master_link_status:%s\r\n"
7231 "master_last_io_seconds_ago:%d\r\n"
7234 (server
.replstate
== REDIS_REPL_CONNECTED
) ?
7236 server
.master
? ((int)(time(NULL
)-server
.master
->lastinteraction
)) : -1
7239 if (server
.vm_enabled
) {
7241 info
= sdscatprintf(info
,
7242 "vm_conf_max_memory:%llu\r\n"
7243 "vm_conf_page_size:%llu\r\n"
7244 "vm_conf_pages:%llu\r\n"
7245 "vm_stats_used_pages:%llu\r\n"
7246 "vm_stats_swapped_objects:%llu\r\n"
7247 "vm_stats_swappin_count:%llu\r\n"
7248 "vm_stats_swappout_count:%llu\r\n"
7249 "vm_stats_io_newjobs_len:%lu\r\n"
7250 "vm_stats_io_processing_len:%lu\r\n"
7251 "vm_stats_io_processed_len:%lu\r\n"
7252 "vm_stats_io_active_threads:%lu\r\n"
7253 "vm_stats_blocked_clients:%lu\r\n"
7254 ,(unsigned long long) server
.vm_max_memory
,
7255 (unsigned long long) server
.vm_page_size
,
7256 (unsigned long long) server
.vm_pages
,
7257 (unsigned long long) server
.vm_stats_used_pages
,
7258 (unsigned long long) server
.vm_stats_swapped_objects
,
7259 (unsigned long long) server
.vm_stats_swapins
,
7260 (unsigned long long) server
.vm_stats_swapouts
,
7261 (unsigned long) listLength(server
.io_newjobs
),
7262 (unsigned long) listLength(server
.io_processing
),
7263 (unsigned long) listLength(server
.io_processed
),
7264 (unsigned long) server
.io_active_threads
,
7265 (unsigned long) server
.vm_blocked_clients
7269 for (j
= 0; j
< server
.dbnum
; j
++) {
7270 long long keys
, vkeys
;
7272 keys
= dictSize(server
.db
[j
].dict
);
7273 vkeys
= dictSize(server
.db
[j
].expires
);
7274 if (keys
|| vkeys
) {
7275 info
= sdscatprintf(info
, "db%d:keys=%lld,expires=%lld\r\n",
7282 static void infoCommand(redisClient
*c
) {
7283 sds info
= genRedisInfoString();
7284 addReplySds(c
,sdscatprintf(sdsempty(),"$%lu\r\n",
7285 (unsigned long)sdslen(info
)));
7286 addReplySds(c
,info
);
7287 addReply(c
,shared
.crlf
);
7290 static void monitorCommand(redisClient
*c
) {
7291 /* ignore MONITOR if aleady slave or in monitor mode */
7292 if (c
->flags
& REDIS_SLAVE
) return;
7294 c
->flags
|= (REDIS_SLAVE
|REDIS_MONITOR
);
7296 listAddNodeTail(server
.monitors
,c
);
7297 addReply(c
,shared
.ok
);
7300 /* ================================= Expire ================================= */
7301 static int removeExpire(redisDb
*db
, robj
*key
) {
7302 if (dictDelete(db
->expires
,key
) == DICT_OK
) {
7309 static int setExpire(redisDb
*db
, robj
*key
, time_t when
) {
7310 if (dictAdd(db
->expires
,key
,(void*)when
) == DICT_ERR
) {
7318 /* Return the expire time of the specified key, or -1 if no expire
7319 * is associated with this key (i.e. the key is non volatile) */
7320 static time_t getExpire(redisDb
*db
, robj
*key
) {
7323 /* No expire? return ASAP */
7324 if (dictSize(db
->expires
) == 0 ||
7325 (de
= dictFind(db
->expires
,key
)) == NULL
) return -1;
7327 return (time_t) dictGetEntryVal(de
);
7330 static int expireIfNeeded(redisDb
*db
, robj
*key
) {
7334 /* No expire? return ASAP */
7335 if (dictSize(db
->expires
) == 0 ||
7336 (de
= dictFind(db
->expires
,key
)) == NULL
) return 0;
7338 /* Lookup the expire */
7339 when
= (time_t) dictGetEntryVal(de
);
7340 if (time(NULL
) <= when
) return 0;
7342 /* Delete the key */
7343 dictDelete(db
->expires
,key
);
7344 server
.stat_expiredkeys
++;
7345 return dictDelete(db
->dict
,key
) == DICT_OK
;
7348 static int deleteIfVolatile(redisDb
*db
, robj
*key
) {
7351 /* No expire? return ASAP */
7352 if (dictSize(db
->expires
) == 0 ||
7353 (de
= dictFind(db
->expires
,key
)) == NULL
) return 0;
7355 /* Delete the key */
7357 server
.stat_expiredkeys
++;
7358 dictDelete(db
->expires
,key
);
7359 return dictDelete(db
->dict
,key
) == DICT_OK
;
7362 static void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
7366 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
7370 de
= dictFind(c
->db
->dict
,key
);
7372 addReply(c
,shared
.czero
);
7376 if (deleteKey(c
->db
,key
)) server
.dirty
++;
7377 addReply(c
, shared
.cone
);
7380 time_t when
= time(NULL
)+seconds
;
7381 if (setExpire(c
->db
,key
,when
)) {
7382 addReply(c
,shared
.cone
);
7385 addReply(c
,shared
.czero
);
7391 static void expireCommand(redisClient
*c
) {
7392 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
7395 static void expireatCommand(redisClient
*c
) {
7396 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
7399 static void ttlCommand(redisClient
*c
) {
7403 expire
= getExpire(c
->db
,c
->argv
[1]);
7405 ttl
= (int) (expire
-time(NULL
));
7406 if (ttl
< 0) ttl
= -1;
7408 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",ttl
));
7411 /* ================================ MULTI/EXEC ============================== */
7413 /* Client state initialization for MULTI/EXEC */
7414 static void initClientMultiState(redisClient
*c
) {
7415 c
->mstate
.commands
= NULL
;
7416 c
->mstate
.count
= 0;
7419 /* Release all the resources associated with MULTI/EXEC state */
7420 static void freeClientMultiState(redisClient
*c
) {
7423 for (j
= 0; j
< c
->mstate
.count
; j
++) {
7425 multiCmd
*mc
= c
->mstate
.commands
+j
;
7427 for (i
= 0; i
< mc
->argc
; i
++)
7428 decrRefCount(mc
->argv
[i
]);
7431 zfree(c
->mstate
.commands
);
7434 /* Add a new command into the MULTI commands queue */
7435 static void queueMultiCommand(redisClient
*c
, struct redisCommand
*cmd
) {
7439 c
->mstate
.commands
= zrealloc(c
->mstate
.commands
,
7440 sizeof(multiCmd
)*(c
->mstate
.count
+1));
7441 mc
= c
->mstate
.commands
+c
->mstate
.count
;
7444 mc
->argv
= zmalloc(sizeof(robj
*)*c
->argc
);
7445 memcpy(mc
->argv
,c
->argv
,sizeof(robj
*)*c
->argc
);
7446 for (j
= 0; j
< c
->argc
; j
++)
7447 incrRefCount(mc
->argv
[j
]);
7451 static void multiCommand(redisClient
*c
) {
7452 c
->flags
|= REDIS_MULTI
;
7453 addReply(c
,shared
.ok
);
7456 static void discardCommand(redisClient
*c
) {
7457 if (!(c
->flags
& REDIS_MULTI
)) {
7458 addReplySds(c
,sdsnew("-ERR DISCARD without MULTI\r\n"));
7462 freeClientMultiState(c
);
7463 initClientMultiState(c
);
7464 c
->flags
&= (~REDIS_MULTI
);
7465 addReply(c
,shared
.ok
);
7468 /* Send a MULTI command to all the slaves and AOF file. Check the execCommand
7469 * implememntation for more information. */
7470 static void execCommandReplicateMulti(redisClient
*c
) {
7471 struct redisCommand
*cmd
;
7472 robj
*multistring
= createStringObject("MULTI",5);
7474 cmd
= lookupCommand("multi");
7475 if (server
.appendonly
)
7476 feedAppendOnlyFile(cmd
,c
->db
->id
,&multistring
,1);
7477 if (listLength(server
.slaves
))
7478 replicationFeedSlaves(server
.slaves
,c
->db
->id
,&multistring
,1);
7479 decrRefCount(multistring
);
7482 static void execCommand(redisClient
*c
) {
7487 if (!(c
->flags
& REDIS_MULTI
)) {
7488 addReplySds(c
,sdsnew("-ERR EXEC without MULTI\r\n"));
7492 /* Replicate a MULTI request now that we are sure the block is executed.
7493 * This way we'll deliver the MULTI/..../EXEC block as a whole and
7494 * both the AOF and the replication link will have the same consistency
7495 * and atomicity guarantees. */
7496 execCommandReplicateMulti(c
);
7498 /* Exec all the queued commands */
7499 orig_argv
= c
->argv
;
7500 orig_argc
= c
->argc
;
7501 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->mstate
.count
));
7502 for (j
= 0; j
< c
->mstate
.count
; j
++) {
7503 c
->argc
= c
->mstate
.commands
[j
].argc
;
7504 c
->argv
= c
->mstate
.commands
[j
].argv
;
7505 call(c
,c
->mstate
.commands
[j
].cmd
);
7507 c
->argv
= orig_argv
;
7508 c
->argc
= orig_argc
;
7509 freeClientMultiState(c
);
7510 initClientMultiState(c
);
7511 c
->flags
&= (~REDIS_MULTI
);
7512 /* Make sure the EXEC command is always replicated / AOF, since we
7513 * always send the MULTI command (we can't know beforehand if the
7514 * next operations will contain at least a modification to the DB). */
7518 /* =========================== Blocking Operations ========================= */
7520 /* Currently Redis blocking operations support is limited to list POP ops,
7521 * so the current implementation is not fully generic, but it is also not
7522 * completely specific so it will not require a rewrite to support new
7523 * kind of blocking operations in the future.
7525 * Still it's important to note that list blocking operations can be already
7526 * used as a notification mechanism in order to implement other blocking
7527 * operations at application level, so there must be a very strong evidence
7528 * of usefulness and generality before new blocking operations are implemented.
7530 * This is how the current blocking POP works, we use BLPOP as example:
7531 * - If the user calls BLPOP and the key exists and contains a non empty list
7532 * then LPOP is called instead. So BLPOP is semantically the same as LPOP
7533 * if there is not to block.
7534 * - If instead BLPOP is called and the key does not exists or the list is
7535 * empty we need to block. In order to do so we remove the notification for
7536 * new data to read in the client socket (so that we'll not serve new
7537 * requests if the blocking request is not served). Also we put the client
7538 * in a dictionary (db->blockingkeys) mapping keys to a list of clients
7539 * blocking for this keys.
7540 * - If a PUSH operation against a key with blocked clients waiting is
7541 * performed, we serve the first in the list: basically instead to push
7542 * the new element inside the list we return it to the (first / oldest)
7543 * blocking client, unblock the client, and remove it form the list.
7545 * The above comment and the source code should be enough in order to understand
7546 * the implementation and modify / fix it later.
7549 /* Set a client in blocking mode for the specified key, with the specified
7551 static void blockForKeys(redisClient
*c
, robj
**keys
, int numkeys
, time_t timeout
) {
7556 c
->blockingkeys
= zmalloc(sizeof(robj
*)*numkeys
);
7557 c
->blockingkeysnum
= numkeys
;
7558 c
->blockingto
= timeout
;
7559 for (j
= 0; j
< numkeys
; j
++) {
7560 /* Add the key in the client structure, to map clients -> keys */
7561 c
->blockingkeys
[j
] = keys
[j
];
7562 incrRefCount(keys
[j
]);
7564 /* And in the other "side", to map keys -> clients */
7565 de
= dictFind(c
->db
->blockingkeys
,keys
[j
]);
7569 /* For every key we take a list of clients blocked for it */
7571 retval
= dictAdd(c
->db
->blockingkeys
,keys
[j
],l
);
7572 incrRefCount(keys
[j
]);
7573 assert(retval
== DICT_OK
);
7575 l
= dictGetEntryVal(de
);
7577 listAddNodeTail(l
,c
);
7579 /* Mark the client as a blocked client */
7580 c
->flags
|= REDIS_BLOCKED
;
7581 server
.blpop_blocked_clients
++;
7584 /* Unblock a client that's waiting in a blocking operation such as BLPOP */
7585 static void unblockClientWaitingData(redisClient
*c
) {
7590 assert(c
->blockingkeys
!= NULL
);
7591 /* The client may wait for multiple keys, so unblock it for every key. */
7592 for (j
= 0; j
< c
->blockingkeysnum
; j
++) {
7593 /* Remove this client from the list of clients waiting for this key. */
7594 de
= dictFind(c
->db
->blockingkeys
,c
->blockingkeys
[j
]);
7596 l
= dictGetEntryVal(de
);
7597 listDelNode(l
,listSearchKey(l
,c
));
7598 /* If the list is empty we need to remove it to avoid wasting memory */
7599 if (listLength(l
) == 0)
7600 dictDelete(c
->db
->blockingkeys
,c
->blockingkeys
[j
]);
7601 decrRefCount(c
->blockingkeys
[j
]);
7603 /* Cleanup the client structure */
7604 zfree(c
->blockingkeys
);
7605 c
->blockingkeys
= NULL
;
7606 c
->flags
&= (~REDIS_BLOCKED
);
7607 server
.blpop_blocked_clients
--;
7608 /* We want to process data if there is some command waiting
7609 * in the input buffer. Note that this is safe even if
7610 * unblockClientWaitingData() gets called from freeClient() because
7611 * freeClient() will be smart enough to call this function
7612 * *after* c->querybuf was set to NULL. */
7613 if (c
->querybuf
&& sdslen(c
->querybuf
) > 0) processInputBuffer(c
);
7616 /* This should be called from any function PUSHing into lists.
7617 * 'c' is the "pushing client", 'key' is the key it is pushing data against,
7618 * 'ele' is the element pushed.
7620 * If the function returns 0 there was no client waiting for a list push
7623 * If the function returns 1 there was a client waiting for a list push
7624 * against this key, the element was passed to this client thus it's not
7625 * needed to actually add it to the list and the caller should return asap. */
7626 static int handleClientsWaitingListPush(redisClient
*c
, robj
*key
, robj
*ele
) {
7627 struct dictEntry
*de
;
7628 redisClient
*receiver
;
7632 de
= dictFind(c
->db
->blockingkeys
,key
);
7633 if (de
== NULL
) return 0;
7634 l
= dictGetEntryVal(de
);
7637 receiver
= ln
->value
;
7639 addReplySds(receiver
,sdsnew("*2\r\n"));
7640 addReplyBulk(receiver
,key
);
7641 addReplyBulk(receiver
,ele
);
7642 unblockClientWaitingData(receiver
);
7646 /* Blocking RPOP/LPOP */
7647 static void blockingPopGenericCommand(redisClient
*c
, int where
) {
7652 for (j
= 1; j
< c
->argc
-1; j
++) {
7653 o
= lookupKeyWrite(c
->db
,c
->argv
[j
]);
7655 if (o
->type
!= REDIS_LIST
) {
7656 addReply(c
,shared
.wrongtypeerr
);
7659 list
*list
= o
->ptr
;
7660 if (listLength(list
) != 0) {
7661 /* If the list contains elements fall back to the usual
7662 * non-blocking POP operation */
7663 robj
*argv
[2], **orig_argv
;
7666 /* We need to alter the command arguments before to call
7667 * popGenericCommand() as the command takes a single key. */
7668 orig_argv
= c
->argv
;
7669 orig_argc
= c
->argc
;
7670 argv
[1] = c
->argv
[j
];
7674 /* Also the return value is different, we need to output
7675 * the multi bulk reply header and the key name. The
7676 * "real" command will add the last element (the value)
7677 * for us. If this souds like an hack to you it's just
7678 * because it is... */
7679 addReplySds(c
,sdsnew("*2\r\n"));
7680 addReplyBulk(c
,argv
[1]);
7681 popGenericCommand(c
,where
);
7683 /* Fix the client structure with the original stuff */
7684 c
->argv
= orig_argv
;
7685 c
->argc
= orig_argc
;
7691 /* If the list is empty or the key does not exists we must block */
7692 timeout
= strtol(c
->argv
[c
->argc
-1]->ptr
,NULL
,10);
7693 if (timeout
> 0) timeout
+= time(NULL
);
7694 blockForKeys(c
,c
->argv
+1,c
->argc
-2,timeout
);
7697 static void blpopCommand(redisClient
*c
) {
7698 blockingPopGenericCommand(c
,REDIS_HEAD
);
7701 static void brpopCommand(redisClient
*c
) {
7702 blockingPopGenericCommand(c
,REDIS_TAIL
);
7705 /* =============================== Replication ============================= */
7707 static int syncWrite(int fd
, char *ptr
, ssize_t size
, int timeout
) {
7708 ssize_t nwritten
, ret
= size
;
7709 time_t start
= time(NULL
);
7713 if (aeWait(fd
,AE_WRITABLE
,1000) & AE_WRITABLE
) {
7714 nwritten
= write(fd
,ptr
,size
);
7715 if (nwritten
== -1) return -1;
7719 if ((time(NULL
)-start
) > timeout
) {
7727 static int syncRead(int fd
, char *ptr
, ssize_t size
, int timeout
) {
7728 ssize_t nread
, totread
= 0;
7729 time_t start
= time(NULL
);
7733 if (aeWait(fd
,AE_READABLE
,1000) & AE_READABLE
) {
7734 nread
= read(fd
,ptr
,size
);
7735 if (nread
== -1) return -1;
7740 if ((time(NULL
)-start
) > timeout
) {
7748 static int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
) {
7755 if (syncRead(fd
,&c
,1,timeout
) == -1) return -1;
7758 if (nread
&& *(ptr
-1) == '\r') *(ptr
-1) = '\0';
7769 static void syncCommand(redisClient
*c
) {
7770 /* ignore SYNC if aleady slave or in monitor mode */
7771 if (c
->flags
& REDIS_SLAVE
) return;
7773 /* SYNC can't be issued when the server has pending data to send to
7774 * the client about already issued commands. We need a fresh reply
7775 * buffer registering the differences between the BGSAVE and the current
7776 * dataset, so that we can copy to other slaves if needed. */
7777 if (listLength(c
->reply
) != 0) {
7778 addReplySds(c
,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
7782 redisLog(REDIS_NOTICE
,"Slave ask for synchronization");
7783 /* Here we need to check if there is a background saving operation
7784 * in progress, or if it is required to start one */
7785 if (server
.bgsavechildpid
!= -1) {
7786 /* Ok a background save is in progress. Let's check if it is a good
7787 * one for replication, i.e. if there is another slave that is
7788 * registering differences since the server forked to save */
7793 listRewind(server
.slaves
,&li
);
7794 while((ln
= listNext(&li
))) {
7796 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_END
) break;
7799 /* Perfect, the server is already registering differences for
7800 * another slave. Set the right state, and copy the buffer. */
7801 listRelease(c
->reply
);
7802 c
->reply
= listDup(slave
->reply
);
7803 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
7804 redisLog(REDIS_NOTICE
,"Waiting for end of BGSAVE for SYNC");
7806 /* No way, we need to wait for the next BGSAVE in order to
7807 * register differences */
7808 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_START
;
7809 redisLog(REDIS_NOTICE
,"Waiting for next BGSAVE for SYNC");
7812 /* Ok we don't have a BGSAVE in progress, let's start one */
7813 redisLog(REDIS_NOTICE
,"Starting BGSAVE for SYNC");
7814 if (rdbSaveBackground(server
.dbfilename
) != REDIS_OK
) {
7815 redisLog(REDIS_NOTICE
,"Replication failed, can't BGSAVE");
7816 addReplySds(c
,sdsnew("-ERR Unalbe to perform background save\r\n"));
7819 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
7822 c
->flags
|= REDIS_SLAVE
;
7824 listAddNodeTail(server
.slaves
,c
);
7828 static void sendBulkToSlave(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
7829 redisClient
*slave
= privdata
;
7831 REDIS_NOTUSED(mask
);
7832 char buf
[REDIS_IOBUF_LEN
];
7833 ssize_t nwritten
, buflen
;
7835 if (slave
->repldboff
== 0) {
7836 /* Write the bulk write count before to transfer the DB. In theory here
7837 * we don't know how much room there is in the output buffer of the
7838 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
7839 * operations) will never be smaller than the few bytes we need. */
7842 bulkcount
= sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
7844 if (write(fd
,bulkcount
,sdslen(bulkcount
)) != (signed)sdslen(bulkcount
))
7852 lseek(slave
->repldbfd
,slave
->repldboff
,SEEK_SET
);
7853 buflen
= read(slave
->repldbfd
,buf
,REDIS_IOBUF_LEN
);
7855 redisLog(REDIS_WARNING
,"Read error sending DB to slave: %s",
7856 (buflen
== 0) ? "premature EOF" : strerror(errno
));
7860 if ((nwritten
= write(fd
,buf
,buflen
)) == -1) {
7861 redisLog(REDIS_VERBOSE
,"Write error sending DB to slave: %s",
7866 slave
->repldboff
+= nwritten
;
7867 if (slave
->repldboff
== slave
->repldbsize
) {
7868 close(slave
->repldbfd
);
7869 slave
->repldbfd
= -1;
7870 aeDeleteFileEvent(server
.el
,slave
->fd
,AE_WRITABLE
);
7871 slave
->replstate
= REDIS_REPL_ONLINE
;
7872 if (aeCreateFileEvent(server
.el
, slave
->fd
, AE_WRITABLE
,
7873 sendReplyToClient
, slave
) == AE_ERR
) {
7877 addReplySds(slave
,sdsempty());
7878 redisLog(REDIS_NOTICE
,"Synchronization with slave succeeded");
7882 /* This function is called at the end of every backgrond saving.
7883 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
7884 * otherwise REDIS_ERR is passed to the function.
7886 * The goal of this function is to handle slaves waiting for a successful
7887 * background saving in order to perform non-blocking synchronization. */
7888 static void updateSlavesWaitingBgsave(int bgsaveerr
) {
7890 int startbgsave
= 0;
7893 listRewind(server
.slaves
,&li
);
7894 while((ln
= listNext(&li
))) {
7895 redisClient
*slave
= ln
->value
;
7897 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
) {
7899 slave
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
7900 } else if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_END
) {
7901 struct redis_stat buf
;
7903 if (bgsaveerr
!= REDIS_OK
) {
7905 redisLog(REDIS_WARNING
,"SYNC failed. BGSAVE child returned an error");
7908 if ((slave
->repldbfd
= open(server
.dbfilename
,O_RDONLY
)) == -1 ||
7909 redis_fstat(slave
->repldbfd
,&buf
) == -1) {
7911 redisLog(REDIS_WARNING
,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno
));
7914 slave
->repldboff
= 0;
7915 slave
->repldbsize
= buf
.st_size
;
7916 slave
->replstate
= REDIS_REPL_SEND_BULK
;
7917 aeDeleteFileEvent(server
.el
,slave
->fd
,AE_WRITABLE
);
7918 if (aeCreateFileEvent(server
.el
, slave
->fd
, AE_WRITABLE
, sendBulkToSlave
, slave
) == AE_ERR
) {
7925 if (rdbSaveBackground(server
.dbfilename
) != REDIS_OK
) {
7928 listRewind(server
.slaves
,&li
);
7929 redisLog(REDIS_WARNING
,"SYNC failed. BGSAVE failed");
7930 while((ln
= listNext(&li
))) {
7931 redisClient
*slave
= ln
->value
;
7933 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
)
7940 static int syncWithMaster(void) {
7941 char buf
[1024], tmpfile
[256], authcmd
[1024];
7943 int fd
= anetTcpConnect(NULL
,server
.masterhost
,server
.masterport
);
7944 int dfd
, maxtries
= 5;
7947 redisLog(REDIS_WARNING
,"Unable to connect to MASTER: %s",
7952 /* AUTH with the master if required. */
7953 if(server
.masterauth
) {
7954 snprintf(authcmd
, 1024, "AUTH %s\r\n", server
.masterauth
);
7955 if (syncWrite(fd
, authcmd
, strlen(server
.masterauth
)+7, 5) == -1) {
7957 redisLog(REDIS_WARNING
,"Unable to AUTH to MASTER: %s",
7961 /* Read the AUTH result. */
7962 if (syncReadLine(fd
,buf
,1024,3600) == -1) {
7964 redisLog(REDIS_WARNING
,"I/O error reading auth result from MASTER: %s",
7968 if (buf
[0] != '+') {
7970 redisLog(REDIS_WARNING
,"Cannot AUTH to MASTER, is the masterauth password correct?");
7975 /* Issue the SYNC command */
7976 if (syncWrite(fd
,"SYNC \r\n",7,5) == -1) {
7978 redisLog(REDIS_WARNING
,"I/O error writing to MASTER: %s",
7982 /* Read the bulk write count */
7983 if (syncReadLine(fd
,buf
,1024,3600) == -1) {
7985 redisLog(REDIS_WARNING
,"I/O error reading bulk count from MASTER: %s",
7989 if (buf
[0] != '$') {
7991 redisLog(REDIS_WARNING
,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
7994 dumpsize
= strtol(buf
+1,NULL
,10);
7995 redisLog(REDIS_NOTICE
,"Receiving %ld bytes data dump from MASTER",dumpsize
);
7996 /* Read the bulk write data on a temp file */
7998 snprintf(tmpfile
,256,
7999 "temp-%d.%ld.rdb",(int)time(NULL
),(long int)getpid());
8000 dfd
= open(tmpfile
,O_CREAT
|O_WRONLY
|O_EXCL
,0644);
8001 if (dfd
!= -1) break;
8006 redisLog(REDIS_WARNING
,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno
));
8010 int nread
, nwritten
;
8012 nread
= read(fd
,buf
,(dumpsize
< 1024)?dumpsize
:1024);
8014 redisLog(REDIS_WARNING
,"I/O error trying to sync with MASTER: %s",
8020 nwritten
= write(dfd
,buf
,nread
);
8021 if (nwritten
== -1) {
8022 redisLog(REDIS_WARNING
,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno
));
8030 if (rename(tmpfile
,server
.dbfilename
) == -1) {
8031 redisLog(REDIS_WARNING
,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno
));
8037 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
8038 redisLog(REDIS_WARNING
,"Failed trying to load the MASTER synchronization DB from disk");
8042 server
.master
= createClient(fd
);
8043 server
.master
->flags
|= REDIS_MASTER
;
8044 server
.master
->authenticated
= 1;
8045 server
.replstate
= REDIS_REPL_CONNECTED
;
8049 static void slaveofCommand(redisClient
*c
) {
8050 if (!strcasecmp(c
->argv
[1]->ptr
,"no") &&
8051 !strcasecmp(c
->argv
[2]->ptr
,"one")) {
8052 if (server
.masterhost
) {
8053 sdsfree(server
.masterhost
);
8054 server
.masterhost
= NULL
;
8055 if (server
.master
) freeClient(server
.master
);
8056 server
.replstate
= REDIS_REPL_NONE
;
8057 redisLog(REDIS_NOTICE
,"MASTER MODE enabled (user request)");
8060 sdsfree(server
.masterhost
);
8061 server
.masterhost
= sdsdup(c
->argv
[1]->ptr
);
8062 server
.masterport
= atoi(c
->argv
[2]->ptr
);
8063 if (server
.master
) freeClient(server
.master
);
8064 server
.replstate
= REDIS_REPL_CONNECT
;
8065 redisLog(REDIS_NOTICE
,"SLAVE OF %s:%d enabled (user request)",
8066 server
.masterhost
, server
.masterport
);
8068 addReply(c
,shared
.ok
);
8071 /* ============================ Maxmemory directive ======================== */
8073 /* Try to free one object form the pre-allocated objects free list.
8074 * This is useful under low mem conditions as by default we take 1 million
8075 * free objects allocated. On success REDIS_OK is returned, otherwise
8077 static int tryFreeOneObjectFromFreelist(void) {
8080 if (server
.vm_enabled
) pthread_mutex_lock(&server
.obj_freelist_mutex
);
8081 if (listLength(server
.objfreelist
)) {
8082 listNode
*head
= listFirst(server
.objfreelist
);
8083 o
= listNodeValue(head
);
8084 listDelNode(server
.objfreelist
,head
);
8085 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
8089 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
8094 /* This function gets called when 'maxmemory' is set on the config file to limit
8095 * the max memory used by the server, and we are out of memory.
8096 * This function will try to, in order:
8098 * - Free objects from the free list
8099 * - Try to remove keys with an EXPIRE set
8101 * It is not possible to free enough memory to reach used-memory < maxmemory
8102 * the server will start refusing commands that will enlarge even more the
8105 static void freeMemoryIfNeeded(void) {
8106 while (server
.maxmemory
&& zmalloc_used_memory() > server
.maxmemory
) {
8107 int j
, k
, freed
= 0;
8109 if (tryFreeOneObjectFromFreelist() == REDIS_OK
) continue;
8110 for (j
= 0; j
< server
.dbnum
; j
++) {
8112 robj
*minkey
= NULL
;
8113 struct dictEntry
*de
;
8115 if (dictSize(server
.db
[j
].expires
)) {
8117 /* From a sample of three keys drop the one nearest to
8118 * the natural expire */
8119 for (k
= 0; k
< 3; k
++) {
8122 de
= dictGetRandomKey(server
.db
[j
].expires
);
8123 t
= (time_t) dictGetEntryVal(de
);
8124 if (minttl
== -1 || t
< minttl
) {
8125 minkey
= dictGetEntryKey(de
);
8129 deleteKey(server
.db
+j
,minkey
);
8132 if (!freed
) return; /* nothing to free... */
8136 /* ============================== Append Only file ========================== */
8138 /* Write the append only file buffer on disk.
8140 * Since we are required to write the AOF before replying to the client,
8141 * and the only way the client socket can get a write is entering when the
8142 * the event loop, we accumulate all the AOF writes in a memory
8143 * buffer and write it on disk using this function just before entering
8144 * the event loop again. */
8145 static void flushAppendOnlyFile(void) {
8149 if (sdslen(server
.aofbuf
) == 0) return;
8151 /* We want to perform a single write. This should be guaranteed atomic
8152 * at least if the filesystem we are writing is a real physical one.
8153 * While this will save us against the server being killed I don't think
8154 * there is much to do about the whole server stopping for power problems
8156 nwritten
= write(server
.appendfd
,server
.aofbuf
,sdslen(server
.aofbuf
));
8157 if (nwritten
!= (signed)sdslen(server
.aofbuf
)) {
8158 /* Ooops, we are in troubles. The best thing to do for now is
8159 * aborting instead of giving the illusion that everything is
8160 * working as expected. */
8161 if (nwritten
== -1) {
8162 redisLog(REDIS_WARNING
,"Exiting on error writing to the append-only file: %s",strerror(errno
));
8164 redisLog(REDIS_WARNING
,"Exiting on short write while writing to the append-only file: %s",strerror(errno
));
8168 sdsfree(server
.aofbuf
);
8169 server
.aofbuf
= sdsempty();
8171 /* Fsync if needed */
8173 if (server
.appendfsync
== APPENDFSYNC_ALWAYS
||
8174 (server
.appendfsync
== APPENDFSYNC_EVERYSEC
&&
8175 now
-server
.lastfsync
> 1))
8177 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
8178 * flushing metadata. */
8179 aof_fsync(server
.appendfd
); /* Let's try to get this data on the disk */
8180 server
.lastfsync
= now
;
8184 static sds
catAppendOnlyGenericCommand(sds buf
, int argc
, robj
**argv
) {
8186 buf
= sdscatprintf(buf
,"*%d\r\n",argc
);
8187 for (j
= 0; j
< argc
; j
++) {
8188 robj
*o
= getDecodedObject(argv
[j
]);
8189 buf
= sdscatprintf(buf
,"$%lu\r\n",(unsigned long)sdslen(o
->ptr
));
8190 buf
= sdscatlen(buf
,o
->ptr
,sdslen(o
->ptr
));
8191 buf
= sdscatlen(buf
,"\r\n",2);
8197 static sds
catAppendOnlyExpireAtCommand(sds buf
, robj
*key
, robj
*seconds
) {
8202 /* Make sure we can use strtol */
8203 seconds
= getDecodedObject(seconds
);
8204 when
= time(NULL
)+strtol(seconds
->ptr
,NULL
,10);
8205 decrRefCount(seconds
);
8207 argv
[0] = createStringObject("EXPIREAT",8);
8209 argv
[2] = createObject(REDIS_STRING
,
8210 sdscatprintf(sdsempty(),"%ld",when
));
8211 buf
= catAppendOnlyGenericCommand(buf
, argc
, argv
);
8212 decrRefCount(argv
[0]);
8213 decrRefCount(argv
[2]);
8217 static void feedAppendOnlyFile(struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
) {
8218 sds buf
= sdsempty();
8221 /* The DB this command was targetting is not the same as the last command
8222 * we appendend. To issue a SELECT command is needed. */
8223 if (dictid
!= server
.appendseldb
) {
8226 snprintf(seldb
,sizeof(seldb
),"%d",dictid
);
8227 buf
= sdscatprintf(buf
,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
8228 (unsigned long)strlen(seldb
),seldb
);
8229 server
.appendseldb
= dictid
;
8232 if (cmd
->proc
== expireCommand
) {
8233 /* Translate EXPIRE into EXPIREAT */
8234 buf
= catAppendOnlyExpireAtCommand(buf
,argv
[1],argv
[2]);
8235 } else if (cmd
->proc
== setexCommand
) {
8236 /* Translate SETEX to SET and EXPIREAT */
8237 tmpargv
[0] = createStringObject("SET",3);
8238 tmpargv
[1] = argv
[1];
8239 tmpargv
[2] = argv
[3];
8240 buf
= catAppendOnlyGenericCommand(buf
,3,tmpargv
);
8241 decrRefCount(tmpargv
[0]);
8242 buf
= catAppendOnlyExpireAtCommand(buf
,argv
[1],argv
[2]);
8244 buf
= catAppendOnlyGenericCommand(buf
,argc
,argv
);
8247 /* Append to the AOF buffer. This will be flushed on disk just before
8248 * of re-entering the event loop, so before the client will get a
8249 * positive reply about the operation performed. */
8250 server
.aofbuf
= sdscatlen(server
.aofbuf
,buf
,sdslen(buf
));
8252 /* If a background append only file rewriting is in progress we want to
8253 * accumulate the differences between the child DB and the current one
8254 * in a buffer, so that when the child process will do its work we
8255 * can append the differences to the new append only file. */
8256 if (server
.bgrewritechildpid
!= -1)
8257 server
.bgrewritebuf
= sdscatlen(server
.bgrewritebuf
,buf
,sdslen(buf
));
8262 /* In Redis commands are always executed in the context of a client, so in
8263 * order to load the append only file we need to create a fake client. */
8264 static struct redisClient
*createFakeClient(void) {
8265 struct redisClient
*c
= zmalloc(sizeof(*c
));
8269 c
->querybuf
= sdsempty();
8273 /* We set the fake client as a slave waiting for the synchronization
8274 * so that Redis will not try to send replies to this client. */
8275 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_START
;
8276 c
->reply
= listCreate();
8277 listSetFreeMethod(c
->reply
,decrRefCount
);
8278 listSetDupMethod(c
->reply
,dupClientReplyValue
);
8279 initClientMultiState(c
);
8283 static void freeFakeClient(struct redisClient
*c
) {
8284 sdsfree(c
->querybuf
);
8285 listRelease(c
->reply
);
8286 freeClientMultiState(c
);
8290 /* Replay the append log file. On error REDIS_OK is returned. On non fatal
8291 * error (the append only file is zero-length) REDIS_ERR is returned. On
8292 * fatal error an error message is logged and the program exists. */
8293 int loadAppendOnlyFile(char *filename
) {
8294 struct redisClient
*fakeClient
;
8295 FILE *fp
= fopen(filename
,"r");
8296 struct redis_stat sb
;
8297 unsigned long long loadedkeys
= 0;
8298 int appendonly
= server
.appendonly
;
8300 if (redis_fstat(fileno(fp
),&sb
) != -1 && sb
.st_size
== 0)
8304 redisLog(REDIS_WARNING
,"Fatal error: can't open the append log file for reading: %s",strerror(errno
));
8308 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
8309 * to the same file we're about to read. */
8310 server
.appendonly
= 0;
8312 fakeClient
= createFakeClient();
8319 struct redisCommand
*cmd
;
8321 if (fgets(buf
,sizeof(buf
),fp
) == NULL
) {
8327 if (buf
[0] != '*') goto fmterr
;
8329 argv
= zmalloc(sizeof(robj
*)*argc
);
8330 for (j
= 0; j
< argc
; j
++) {
8331 if (fgets(buf
,sizeof(buf
),fp
) == NULL
) goto readerr
;
8332 if (buf
[0] != '$') goto fmterr
;
8333 len
= strtol(buf
+1,NULL
,10);
8334 argsds
= sdsnewlen(NULL
,len
);
8335 if (len
&& fread(argsds
,len
,1,fp
) == 0) goto fmterr
;
8336 argv
[j
] = createObject(REDIS_STRING
,argsds
);
8337 if (fread(buf
,2,1,fp
) == 0) goto fmterr
; /* discard CRLF */
8340 /* Command lookup */
8341 cmd
= lookupCommand(argv
[0]->ptr
);
8343 redisLog(REDIS_WARNING
,"Unknown command '%s' reading the append only file", argv
[0]->ptr
);
8346 /* Try object encoding */
8347 if (cmd
->flags
& REDIS_CMD_BULK
)
8348 argv
[argc
-1] = tryObjectEncoding(argv
[argc
-1]);
8349 /* Run the command in the context of a fake client */
8350 fakeClient
->argc
= argc
;
8351 fakeClient
->argv
= argv
;
8352 cmd
->proc(fakeClient
);
8353 /* Discard the reply objects list from the fake client */
8354 while(listLength(fakeClient
->reply
))
8355 listDelNode(fakeClient
->reply
,listFirst(fakeClient
->reply
));
8356 /* Clean up, ready for the next command */
8357 for (j
= 0; j
< argc
; j
++) decrRefCount(argv
[j
]);
8359 /* Handle swapping while loading big datasets when VM is on */
8361 if (server
.vm_enabled
&& (loadedkeys
% 5000) == 0) {
8362 while (zmalloc_used_memory() > server
.vm_max_memory
) {
8363 if (vmSwapOneObjectBlocking() == REDIS_ERR
) break;
8368 /* This point can only be reached when EOF is reached without errors.
8369 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
8370 if (fakeClient
->flags
& REDIS_MULTI
) goto readerr
;
8373 freeFakeClient(fakeClient
);
8374 server
.appendonly
= appendonly
;
8379 redisLog(REDIS_WARNING
,"Unexpected end of file reading the append only file");
8381 redisLog(REDIS_WARNING
,"Unrecoverable error reading the append only file: %s", strerror(errno
));
8385 redisLog(REDIS_WARNING
,"Bad file format reading the append only file");
8389 /* Write an object into a file in the bulk format $<count>\r\n<payload>\r\n */
8390 static int fwriteBulkObject(FILE *fp
, robj
*obj
) {
8394 /* Avoid the incr/decr ref count business if possible to help
8395 * copy-on-write (we are often in a child process when this function
8397 * Also makes sure that key objects don't get incrRefCount-ed when VM
8399 if (obj
->encoding
!= REDIS_ENCODING_RAW
) {
8400 obj
= getDecodedObject(obj
);
8403 snprintf(buf
,sizeof(buf
),"$%ld\r\n",(long)sdslen(obj
->ptr
));
8404 if (fwrite(buf
,strlen(buf
),1,fp
) == 0) goto err
;
8405 if (sdslen(obj
->ptr
) && fwrite(obj
->ptr
,sdslen(obj
->ptr
),1,fp
) == 0)
8407 if (fwrite("\r\n",2,1,fp
) == 0) goto err
;
8408 if (decrrc
) decrRefCount(obj
);
8411 if (decrrc
) decrRefCount(obj
);
8415 /* Write binary-safe string into a file in the bulkformat
8416 * $<count>\r\n<payload>\r\n */
8417 static int fwriteBulkString(FILE *fp
, char *s
, unsigned long len
) {
8420 snprintf(buf
,sizeof(buf
),"$%ld\r\n",(unsigned long)len
);
8421 if (fwrite(buf
,strlen(buf
),1,fp
) == 0) return 0;
8422 if (len
&& fwrite(s
,len
,1,fp
) == 0) return 0;
8423 if (fwrite("\r\n",2,1,fp
) == 0) return 0;
8427 /* Write a double value in bulk format $<count>\r\n<payload>\r\n */
8428 static int fwriteBulkDouble(FILE *fp
, double d
) {
8429 char buf
[128], dbuf
[128];
8431 snprintf(dbuf
,sizeof(dbuf
),"%.17g\r\n",d
);
8432 snprintf(buf
,sizeof(buf
),"$%lu\r\n",(unsigned long)strlen(dbuf
)-2);
8433 if (fwrite(buf
,strlen(buf
),1,fp
) == 0) return 0;
8434 if (fwrite(dbuf
,strlen(dbuf
),1,fp
) == 0) return 0;
8438 /* Write a long value in bulk format $<count>\r\n<payload>\r\n */
8439 static int fwriteBulkLong(FILE *fp
, long l
) {
8440 char buf
[128], lbuf
[128];
8442 snprintf(lbuf
,sizeof(lbuf
),"%ld\r\n",l
);
8443 snprintf(buf
,sizeof(buf
),"$%lu\r\n",(unsigned long)strlen(lbuf
)-2);
8444 if (fwrite(buf
,strlen(buf
),1,fp
) == 0) return 0;
8445 if (fwrite(lbuf
,strlen(lbuf
),1,fp
) == 0) return 0;
8449 /* Write a sequence of commands able to fully rebuild the dataset into
8450 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
8451 static int rewriteAppendOnlyFile(char *filename
) {
8452 dictIterator
*di
= NULL
;
8457 time_t now
= time(NULL
);
8459 /* Note that we have to use a different temp name here compared to the
8460 * one used by rewriteAppendOnlyFileBackground() function. */
8461 snprintf(tmpfile
,256,"temp-rewriteaof-%d.aof", (int) getpid());
8462 fp
= fopen(tmpfile
,"w");
8464 redisLog(REDIS_WARNING
, "Failed rewriting the append only file: %s", strerror(errno
));
8467 for (j
= 0; j
< server
.dbnum
; j
++) {
8468 char selectcmd
[] = "*2\r\n$6\r\nSELECT\r\n";
8469 redisDb
*db
= server
.db
+j
;
8471 if (dictSize(d
) == 0) continue;
8472 di
= dictGetIterator(d
);
8478 /* SELECT the new DB */
8479 if (fwrite(selectcmd
,sizeof(selectcmd
)-1,1,fp
) == 0) goto werr
;
8480 if (fwriteBulkLong(fp
,j
) == 0) goto werr
;
8482 /* Iterate this DB writing every entry */
8483 while((de
= dictNext(di
)) != NULL
) {
8488 key
= dictGetEntryKey(de
);
8489 /* If the value for this key is swapped, load a preview in memory.
8490 * We use a "swapped" flag to remember if we need to free the
8491 * value object instead to just increment the ref count anyway
8492 * in order to avoid copy-on-write of pages if we are forked() */
8493 if (!server
.vm_enabled
|| key
->storage
== REDIS_VM_MEMORY
||
8494 key
->storage
== REDIS_VM_SWAPPING
) {
8495 o
= dictGetEntryVal(de
);
8498 o
= vmPreviewObject(key
);
8501 expiretime
= getExpire(db
,key
);
8503 /* Save the key and associated value */
8504 if (o
->type
== REDIS_STRING
) {
8505 /* Emit a SET command */
8506 char cmd
[]="*3\r\n$3\r\nSET\r\n";
8507 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
8509 if (fwriteBulkObject(fp
,key
) == 0) goto werr
;
8510 if (fwriteBulkObject(fp
,o
) == 0) goto werr
;
8511 } else if (o
->type
== REDIS_LIST
) {
8512 /* Emit the RPUSHes needed to rebuild the list */
8513 list
*list
= o
->ptr
;
8517 listRewind(list
,&li
);
8518 while((ln
= listNext(&li
))) {
8519 char cmd
[]="*3\r\n$5\r\nRPUSH\r\n";
8520 robj
*eleobj
= listNodeValue(ln
);
8522 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
8523 if (fwriteBulkObject(fp
,key
) == 0) goto werr
;
8524 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
8526 } else if (o
->type
== REDIS_SET
) {
8527 /* Emit the SADDs needed to rebuild the set */
8529 dictIterator
*di
= dictGetIterator(set
);
8532 while((de
= dictNext(di
)) != NULL
) {
8533 char cmd
[]="*3\r\n$4\r\nSADD\r\n";
8534 robj
*eleobj
= dictGetEntryKey(de
);
8536 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
8537 if (fwriteBulkObject(fp
,key
) == 0) goto werr
;
8538 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
8540 dictReleaseIterator(di
);
8541 } else if (o
->type
== REDIS_ZSET
) {
8542 /* Emit the ZADDs needed to rebuild the sorted set */
8544 dictIterator
*di
= dictGetIterator(zs
->dict
);
8547 while((de
= dictNext(di
)) != NULL
) {
8548 char cmd
[]="*4\r\n$4\r\nZADD\r\n";
8549 robj
*eleobj
= dictGetEntryKey(de
);
8550 double *score
= dictGetEntryVal(de
);
8552 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
8553 if (fwriteBulkObject(fp
,key
) == 0) goto werr
;
8554 if (fwriteBulkDouble(fp
,*score
) == 0) goto werr
;
8555 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
8557 dictReleaseIterator(di
);
8558 } else if (o
->type
== REDIS_HASH
) {
8559 char cmd
[]="*4\r\n$4\r\nHSET\r\n";
8561 /* Emit the HSETs needed to rebuild the hash */
8562 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
8563 unsigned char *p
= zipmapRewind(o
->ptr
);
8564 unsigned char *field
, *val
;
8565 unsigned int flen
, vlen
;
8567 while((p
= zipmapNext(p
,&field
,&flen
,&val
,&vlen
)) != NULL
) {
8568 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
8569 if (fwriteBulkObject(fp
,key
) == 0) goto werr
;
8570 if (fwriteBulkString(fp
,(char*)field
,flen
) == -1)
8572 if (fwriteBulkString(fp
,(char*)val
,vlen
) == -1)
8576 dictIterator
*di
= dictGetIterator(o
->ptr
);
8579 while((de
= dictNext(di
)) != NULL
) {
8580 robj
*field
= dictGetEntryKey(de
);
8581 robj
*val
= dictGetEntryVal(de
);
8583 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
8584 if (fwriteBulkObject(fp
,key
) == 0) goto werr
;
8585 if (fwriteBulkObject(fp
,field
) == -1) return -1;
8586 if (fwriteBulkObject(fp
,val
) == -1) return -1;
8588 dictReleaseIterator(di
);
8591 redisPanic("Unknown object type");
8593 /* Save the expire time */
8594 if (expiretime
!= -1) {
8595 char cmd
[]="*3\r\n$8\r\nEXPIREAT\r\n";
8596 /* If this key is already expired skip it */
8597 if (expiretime
< now
) continue;
8598 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
8599 if (fwriteBulkObject(fp
,key
) == 0) goto werr
;
8600 if (fwriteBulkLong(fp
,expiretime
) == 0) goto werr
;
8602 if (swapped
) decrRefCount(o
);
8604 dictReleaseIterator(di
);
8607 /* Make sure data will not remain on the OS's output buffers */
8612 /* Use RENAME to make sure the DB file is changed atomically only
8613 * if the generate DB file is ok. */
8614 if (rename(tmpfile
,filename
) == -1) {
8615 redisLog(REDIS_WARNING
,"Error moving temp append only file on the final destination: %s", strerror(errno
));
8619 redisLog(REDIS_NOTICE
,"SYNC append only file rewrite performed");
8625 redisLog(REDIS_WARNING
,"Write error writing append only file on disk: %s", strerror(errno
));
8626 if (di
) dictReleaseIterator(di
);
8630 /* This is how rewriting of the append only file in background works:
8632 * 1) The user calls BGREWRITEAOF
8633 * 2) Redis calls this function, that forks():
8634 * 2a) the child rewrite the append only file in a temp file.
8635 * 2b) the parent accumulates differences in server.bgrewritebuf.
8636 * 3) When the child finished '2a' exists.
8637 * 4) The parent will trap the exit code, if it's OK, will append the
8638 * data accumulated into server.bgrewritebuf into the temp file, and
8639 * finally will rename(2) the temp file in the actual file name.
8640 * The the new file is reopened as the new append only file. Profit!
8642 static int rewriteAppendOnlyFileBackground(void) {
8645 if (server
.bgrewritechildpid
!= -1) return REDIS_ERR
;
8646 if (server
.vm_enabled
) waitEmptyIOJobsQueue();
8647 if ((childpid
= fork()) == 0) {
8651 if (server
.vm_enabled
) vmReopenSwapFile();
8653 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
8654 if (rewriteAppendOnlyFile(tmpfile
) == REDIS_OK
) {
8661 if (childpid
== -1) {
8662 redisLog(REDIS_WARNING
,
8663 "Can't rewrite append only file in background: fork: %s",
8667 redisLog(REDIS_NOTICE
,
8668 "Background append only file rewriting started by pid %d",childpid
);
8669 server
.bgrewritechildpid
= childpid
;
8670 updateDictResizePolicy();
8671 /* We set appendseldb to -1 in order to force the next call to the
8672 * feedAppendOnlyFile() to issue a SELECT command, so the differences
8673 * accumulated by the parent into server.bgrewritebuf will start
8674 * with a SELECT statement and it will be safe to merge. */
8675 server
.appendseldb
= -1;
8678 return REDIS_OK
; /* unreached */
8681 static void bgrewriteaofCommand(redisClient
*c
) {
8682 if (server
.bgrewritechildpid
!= -1) {
8683 addReplySds(c
,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
8686 if (rewriteAppendOnlyFileBackground() == REDIS_OK
) {
8687 char *status
= "+Background append only file rewriting started\r\n";
8688 addReplySds(c
,sdsnew(status
));
8690 addReply(c
,shared
.err
);
8694 static void aofRemoveTempFile(pid_t childpid
) {
8697 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) childpid
);
8701 /* Virtual Memory is composed mainly of two subsystems:
8702 * - Blocking Virutal Memory
8703 * - Threaded Virtual Memory I/O
8704 * The two parts are not fully decoupled, but functions are split among two
8705 * different sections of the source code (delimited by comments) in order to
8706 * make more clear what functionality is about the blocking VM and what about
8707 * the threaded (not blocking) VM.
8711 * Redis VM is a blocking VM (one that blocks reading swapped values from
8712 * disk into memory when a value swapped out is needed in memory) that is made
8713 * unblocking by trying to examine the command argument vector in order to
8714 * load in background values that will likely be needed in order to exec
8715 * the command. The command is executed only once all the relevant keys
8716 * are loaded into memory.
8718 * This basically is almost as simple of a blocking VM, but almost as parallel
8719 * as a fully non-blocking VM.
8722 /* =================== Virtual Memory - Blocking Side ====================== */
8724 static void vmInit(void) {
8730 if (server
.vm_max_threads
!= 0)
8731 zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */
8733 redisLog(REDIS_NOTICE
,"Using '%s' as swap file",server
.vm_swap_file
);
8734 /* Try to open the old swap file, otherwise create it */
8735 if ((server
.vm_fp
= fopen(server
.vm_swap_file
,"r+b")) == NULL
) {
8736 server
.vm_fp
= fopen(server
.vm_swap_file
,"w+b");
8738 if (server
.vm_fp
== NULL
) {
8739 redisLog(REDIS_WARNING
,
8740 "Can't open the swap file: %s. Exiting.",
8744 server
.vm_fd
= fileno(server
.vm_fp
);
8745 /* Lock the swap file for writing, this is useful in order to avoid
8746 * another instance to use the same swap file for a config error. */
8747 fl
.l_type
= F_WRLCK
;
8748 fl
.l_whence
= SEEK_SET
;
8749 fl
.l_start
= fl
.l_len
= 0;
8750 if (fcntl(server
.vm_fd
,F_SETLK
,&fl
) == -1) {
8751 redisLog(REDIS_WARNING
,
8752 "Can't lock the swap file at '%s': %s. Make sure it is not used by another Redis instance.", server
.vm_swap_file
, strerror(errno
));
8756 server
.vm_next_page
= 0;
8757 server
.vm_near_pages
= 0;
8758 server
.vm_stats_used_pages
= 0;
8759 server
.vm_stats_swapped_objects
= 0;
8760 server
.vm_stats_swapouts
= 0;
8761 server
.vm_stats_swapins
= 0;
8762 totsize
= server
.vm_pages
*server
.vm_page_size
;
8763 redisLog(REDIS_NOTICE
,"Allocating %lld bytes of swap file",totsize
);
8764 if (ftruncate(server
.vm_fd
,totsize
) == -1) {
8765 redisLog(REDIS_WARNING
,"Can't ftruncate swap file: %s. Exiting.",
8769 redisLog(REDIS_NOTICE
,"Swap file allocated with success");
8771 server
.vm_bitmap
= zmalloc((server
.vm_pages
+7)/8);
8772 redisLog(REDIS_VERBOSE
,"Allocated %lld bytes page table for %lld pages",
8773 (long long) (server
.vm_pages
+7)/8, server
.vm_pages
);
8774 memset(server
.vm_bitmap
,0,(server
.vm_pages
+7)/8);
8776 /* Initialize threaded I/O (used by Virtual Memory) */
8777 server
.io_newjobs
= listCreate();
8778 server
.io_processing
= listCreate();
8779 server
.io_processed
= listCreate();
8780 server
.io_ready_clients
= listCreate();
8781 pthread_mutex_init(&server
.io_mutex
,NULL
);
8782 pthread_mutex_init(&server
.obj_freelist_mutex
,NULL
);
8783 pthread_mutex_init(&server
.io_swapfile_mutex
,NULL
);
8784 server
.io_active_threads
= 0;
8785 if (pipe(pipefds
) == -1) {
8786 redisLog(REDIS_WARNING
,"Unable to intialized VM: pipe(2): %s. Exiting."
8790 server
.io_ready_pipe_read
= pipefds
[0];
8791 server
.io_ready_pipe_write
= pipefds
[1];
8792 redisAssert(anetNonBlock(NULL
,server
.io_ready_pipe_read
) != ANET_ERR
);
8793 /* LZF requires a lot of stack */
8794 pthread_attr_init(&server
.io_threads_attr
);
8795 pthread_attr_getstacksize(&server
.io_threads_attr
, &stacksize
);
8796 while (stacksize
< REDIS_THREAD_STACK_SIZE
) stacksize
*= 2;
8797 pthread_attr_setstacksize(&server
.io_threads_attr
, stacksize
);
8798 /* Listen for events in the threaded I/O pipe */
8799 if (aeCreateFileEvent(server
.el
, server
.io_ready_pipe_read
, AE_READABLE
,
8800 vmThreadedIOCompletedJob
, NULL
) == AE_ERR
)
8801 oom("creating file event");
8804 /* Mark the page as used */
8805 static void vmMarkPageUsed(off_t page
) {
8806 off_t byte
= page
/8;
8808 redisAssert(vmFreePage(page
) == 1);
8809 server
.vm_bitmap
[byte
] |= 1<<bit
;
8812 /* Mark N contiguous pages as used, with 'page' being the first. */
8813 static void vmMarkPagesUsed(off_t page
, off_t count
) {
8816 for (j
= 0; j
< count
; j
++)
8817 vmMarkPageUsed(page
+j
);
8818 server
.vm_stats_used_pages
+= count
;
8819 redisLog(REDIS_DEBUG
,"Mark USED pages: %lld pages at %lld\n",
8820 (long long)count
, (long long)page
);
8823 /* Mark the page as free */
8824 static void vmMarkPageFree(off_t page
) {
8825 off_t byte
= page
/8;
8827 redisAssert(vmFreePage(page
) == 0);
8828 server
.vm_bitmap
[byte
] &= ~(1<<bit
);
8831 /* Mark N contiguous pages as free, with 'page' being the first. */
8832 static void vmMarkPagesFree(off_t page
, off_t count
) {
8835 for (j
= 0; j
< count
; j
++)
8836 vmMarkPageFree(page
+j
);
8837 server
.vm_stats_used_pages
-= count
;
8838 redisLog(REDIS_DEBUG
,"Mark FREE pages: %lld pages at %lld\n",
8839 (long long)count
, (long long)page
);
8842 /* Test if the page is free */
8843 static int vmFreePage(off_t page
) {
8844 off_t byte
= page
/8;
8846 return (server
.vm_bitmap
[byte
] & (1<<bit
)) == 0;
8849 /* Find N contiguous free pages storing the first page of the cluster in *first.
8850 * Returns REDIS_OK if it was able to find N contiguous pages, otherwise
8851 * REDIS_ERR is returned.
8853 * This function uses a simple algorithm: we try to allocate
8854 * REDIS_VM_MAX_NEAR_PAGES sequentially, when we reach this limit we start
8855 * again from the start of the swap file searching for free spaces.
8857 * If it looks pretty clear that there are no free pages near our offset
8858 * we try to find less populated places doing a forward jump of
8859 * REDIS_VM_MAX_RANDOM_JUMP, then we start scanning again a few pages
8860 * without hurry, and then we jump again and so forth...
8862 * This function can be improved using a free list to avoid to guess
8863 * too much, since we could collect data about freed pages.
8865 * note: I implemented this function just after watching an episode of
8866 * Battlestar Galactica, where the hybrid was continuing to say "JUMP!"
8868 static int vmFindContiguousPages(off_t
*first
, off_t n
) {
8869 off_t base
, offset
= 0, since_jump
= 0, numfree
= 0;
8871 if (server
.vm_near_pages
== REDIS_VM_MAX_NEAR_PAGES
) {
8872 server
.vm_near_pages
= 0;
8873 server
.vm_next_page
= 0;
8875 server
.vm_near_pages
++; /* Yet another try for pages near to the old ones */
8876 base
= server
.vm_next_page
;
8878 while(offset
< server
.vm_pages
) {
8879 off_t
this = base
+offset
;
8881 /* If we overflow, restart from page zero */
8882 if (this >= server
.vm_pages
) {
8883 this -= server
.vm_pages
;
8885 /* Just overflowed, what we found on tail is no longer
8886 * interesting, as it's no longer contiguous. */
8890 if (vmFreePage(this)) {
8891 /* This is a free page */
8893 /* Already got N free pages? Return to the caller, with success */
8895 *first
= this-(n
-1);
8896 server
.vm_next_page
= this+1;
8897 redisLog(REDIS_DEBUG
, "FOUND CONTIGUOUS PAGES: %lld pages at %lld\n", (long long) n
, (long long) *first
);
8901 /* The current one is not a free page */
8905 /* Fast-forward if the current page is not free and we already
8906 * searched enough near this place. */
8908 if (!numfree
&& since_jump
>= REDIS_VM_MAX_RANDOM_JUMP
/4) {
8909 offset
+= random() % REDIS_VM_MAX_RANDOM_JUMP
;
8911 /* Note that even if we rewind after the jump, we are don't need
8912 * to make sure numfree is set to zero as we only jump *if* it
8913 * is set to zero. */
8915 /* Otherwise just check the next page */
8922 /* Write the specified object at the specified page of the swap file */
8923 static int vmWriteObjectOnSwap(robj
*o
, off_t page
) {
8924 if (server
.vm_enabled
) pthread_mutex_lock(&server
.io_swapfile_mutex
);
8925 if (fseeko(server
.vm_fp
,page
*server
.vm_page_size
,SEEK_SET
) == -1) {
8926 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.io_swapfile_mutex
);
8927 redisLog(REDIS_WARNING
,
8928 "Critical VM problem in vmWriteObjectOnSwap(): can't seek: %s",
8932 rdbSaveObject(server
.vm_fp
,o
);
8933 fflush(server
.vm_fp
);
8934 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.io_swapfile_mutex
);
8938 /* Swap the 'val' object relative to 'key' into disk. Store all the information
8939 * needed to later retrieve the object into the key object.
8940 * If we can't find enough contiguous empty pages to swap the object on disk
8941 * REDIS_ERR is returned. */
8942 static int vmSwapObjectBlocking(robj
*key
, robj
*val
) {
8943 off_t pages
= rdbSavedObjectPages(val
,NULL
);
8946 assert(key
->storage
== REDIS_VM_MEMORY
);
8947 assert(key
->refcount
== 1);
8948 if (vmFindContiguousPages(&page
,pages
) == REDIS_ERR
) return REDIS_ERR
;
8949 if (vmWriteObjectOnSwap(val
,page
) == REDIS_ERR
) return REDIS_ERR
;
8950 key
->vm
.page
= page
;
8951 key
->vm
.usedpages
= pages
;
8952 key
->storage
= REDIS_VM_SWAPPED
;
8953 key
->vtype
= val
->type
;
8954 decrRefCount(val
); /* Deallocate the object from memory. */
8955 vmMarkPagesUsed(page
,pages
);
8956 redisLog(REDIS_DEBUG
,"VM: object %s swapped out at %lld (%lld pages)",
8957 (unsigned char*) key
->ptr
,
8958 (unsigned long long) page
, (unsigned long long) pages
);
8959 server
.vm_stats_swapped_objects
++;
8960 server
.vm_stats_swapouts
++;
8964 static robj
*vmReadObjectFromSwap(off_t page
, int type
) {
8967 if (server
.vm_enabled
) pthread_mutex_lock(&server
.io_swapfile_mutex
);
8968 if (fseeko(server
.vm_fp
,page
*server
.vm_page_size
,SEEK_SET
) == -1) {
8969 redisLog(REDIS_WARNING
,
8970 "Unrecoverable VM problem in vmReadObjectFromSwap(): can't seek: %s",
8974 o
= rdbLoadObject(type
,server
.vm_fp
);
8976 redisLog(REDIS_WARNING
, "Unrecoverable VM problem in vmReadObjectFromSwap(): can't load object from swap file: %s", strerror(errno
));
8979 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.io_swapfile_mutex
);
8983 /* Load the value object relative to the 'key' object from swap to memory.
8984 * The newly allocated object is returned.
8986 * If preview is true the unserialized object is returned to the caller but
8987 * no changes are made to the key object, nor the pages are marked as freed */
8988 static robj
*vmGenericLoadObject(robj
*key
, int preview
) {
8991 redisAssert(key
->storage
== REDIS_VM_SWAPPED
|| key
->storage
== REDIS_VM_LOADING
);
8992 val
= vmReadObjectFromSwap(key
->vm
.page
,key
->vtype
);
8994 key
->storage
= REDIS_VM_MEMORY
;
8995 key
->vm
.atime
= server
.unixtime
;
8996 vmMarkPagesFree(key
->vm
.page
,key
->vm
.usedpages
);
8997 redisLog(REDIS_DEBUG
, "VM: object %s loaded from disk",
8998 (unsigned char*) key
->ptr
);
8999 server
.vm_stats_swapped_objects
--;
9001 redisLog(REDIS_DEBUG
, "VM: object %s previewed from disk",
9002 (unsigned char*) key
->ptr
);
9004 server
.vm_stats_swapins
++;
9008 /* Plain object loading, from swap to memory */
9009 static robj
*vmLoadObject(robj
*key
) {
9010 /* If we are loading the object in background, stop it, we
9011 * need to load this object synchronously ASAP. */
9012 if (key
->storage
== REDIS_VM_LOADING
)
9013 vmCancelThreadedIOJob(key
);
9014 return vmGenericLoadObject(key
,0);
9017 /* Just load the value on disk, without to modify the key.
9018 * This is useful when we want to perform some operation on the value
9019 * without to really bring it from swap to memory, like while saving the
9020 * dataset or rewriting the append only log. */
9021 static robj
*vmPreviewObject(robj
*key
) {
9022 return vmGenericLoadObject(key
,1);
9025 /* How a good candidate is this object for swapping?
9026 * The better candidate it is, the greater the returned value.
9028 * Currently we try to perform a fast estimation of the object size in
9029 * memory, and combine it with aging informations.
9031 * Basically swappability = idle-time * log(estimated size)
9033 * Bigger objects are preferred over smaller objects, but not
9034 * proportionally, this is why we use the logarithm. This algorithm is
9035 * just a first try and will probably be tuned later. */
9036 static double computeObjectSwappability(robj
*o
) {
9037 time_t age
= server
.unixtime
- o
->vm
.atime
;
9041 struct dictEntry
*de
;
9044 if (age
<= 0) return 0;
9047 if (o
->encoding
!= REDIS_ENCODING_RAW
) {
9050 asize
= sdslen(o
->ptr
)+sizeof(*o
)+sizeof(long)*2;
9055 listNode
*ln
= listFirst(l
);
9057 asize
= sizeof(list
);
9059 robj
*ele
= ln
->value
;
9062 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9063 (sizeof(*o
)+sdslen(ele
->ptr
)) :
9065 asize
+= (sizeof(listNode
)+elesize
)*listLength(l
);
9070 z
= (o
->type
== REDIS_ZSET
);
9071 d
= z
? ((zset
*)o
->ptr
)->dict
: o
->ptr
;
9073 asize
= sizeof(dict
)+(sizeof(struct dictEntry
*)*dictSlots(d
));
9074 if (z
) asize
+= sizeof(zset
)-sizeof(dict
);
9079 de
= dictGetRandomKey(d
);
9080 ele
= dictGetEntryKey(de
);
9081 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9082 (sizeof(*o
)+sdslen(ele
->ptr
)) :
9084 asize
+= (sizeof(struct dictEntry
)+elesize
)*dictSize(d
);
9085 if (z
) asize
+= sizeof(zskiplistNode
)*dictSize(d
);
9089 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
9090 unsigned char *p
= zipmapRewind((unsigned char*)o
->ptr
);
9091 unsigned int len
= zipmapLen((unsigned char*)o
->ptr
);
9092 unsigned int klen
, vlen
;
9093 unsigned char *key
, *val
;
9095 if ((p
= zipmapNext(p
,&key
,&klen
,&val
,&vlen
)) == NULL
) {
9099 asize
= len
*(klen
+vlen
+3);
9100 } else if (o
->encoding
== REDIS_ENCODING_HT
) {
9102 asize
= sizeof(dict
)+(sizeof(struct dictEntry
*)*dictSlots(d
));
9107 de
= dictGetRandomKey(d
);
9108 ele
= dictGetEntryKey(de
);
9109 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9110 (sizeof(*o
)+sdslen(ele
->ptr
)) :
9112 ele
= dictGetEntryVal(de
);
9113 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9114 (sizeof(*o
)+sdslen(ele
->ptr
)) :
9116 asize
+= (sizeof(struct dictEntry
)+elesize
)*dictSize(d
);
9121 return (double)age
*log(1+asize
);
9124 /* Try to swap an object that's a good candidate for swapping.
9125 * Returns REDIS_OK if the object was swapped, REDIS_ERR if it's not possible
9126 * to swap any object at all.
9128 * If 'usethreaded' is true, Redis will try to swap the object in background
9129 * using I/O threads. */
9130 static int vmSwapOneObject(int usethreads
) {
9132 struct dictEntry
*best
= NULL
;
9133 double best_swappability
= 0;
9134 redisDb
*best_db
= NULL
;
9137 for (j
= 0; j
< server
.dbnum
; j
++) {
9138 redisDb
*db
= server
.db
+j
;
9139 /* Why maxtries is set to 100?
9140 * Because this way (usually) we'll find 1 object even if just 1% - 2%
9141 * are swappable objects */
9144 if (dictSize(db
->dict
) == 0) continue;
9145 for (i
= 0; i
< 5; i
++) {
9147 double swappability
;
9149 if (maxtries
) maxtries
--;
9150 de
= dictGetRandomKey(db
->dict
);
9151 key
= dictGetEntryKey(de
);
9152 val
= dictGetEntryVal(de
);
9153 /* Only swap objects that are currently in memory.
9155 * Also don't swap shared objects if threaded VM is on, as we
9156 * try to ensure that the main thread does not touch the
9157 * object while the I/O thread is using it, but we can't
9158 * control other keys without adding additional mutex. */
9159 if (key
->storage
!= REDIS_VM_MEMORY
||
9160 (server
.vm_max_threads
!= 0 && val
->refcount
!= 1)) {
9161 if (maxtries
) i
--; /* don't count this try */
9164 swappability
= computeObjectSwappability(val
);
9165 if (!best
|| swappability
> best_swappability
) {
9167 best_swappability
= swappability
;
9172 if (best
== NULL
) return REDIS_ERR
;
9173 key
= dictGetEntryKey(best
);
9174 val
= dictGetEntryVal(best
);
9176 redisLog(REDIS_DEBUG
,"Key with best swappability: %s, %f",
9177 key
->ptr
, best_swappability
);
9179 /* Unshare the key if needed */
9180 if (key
->refcount
> 1) {
9181 robj
*newkey
= dupStringObject(key
);
9183 key
= dictGetEntryKey(best
) = newkey
;
9187 vmSwapObjectThreaded(key
,val
,best_db
);
9190 if (vmSwapObjectBlocking(key
,val
) == REDIS_OK
) {
9191 dictGetEntryVal(best
) = NULL
;
9199 static int vmSwapOneObjectBlocking() {
9200 return vmSwapOneObject(0);
9203 static int vmSwapOneObjectThreaded() {
9204 return vmSwapOneObject(1);
9207 /* Return true if it's safe to swap out objects in a given moment.
9208 * Basically we don't want to swap objects out while there is a BGSAVE
9209 * or a BGAEOREWRITE running in backgroud. */
9210 static int vmCanSwapOut(void) {
9211 return (server
.bgsavechildpid
== -1 && server
.bgrewritechildpid
== -1);
9214 /* Delete a key if swapped. Returns 1 if the key was found, was swapped
9215 * and was deleted. Otherwise 0 is returned. */
9216 static int deleteIfSwapped(redisDb
*db
, robj
*key
) {
9220 if ((de
= dictFind(db
->dict
,key
)) == NULL
) return 0;
9221 foundkey
= dictGetEntryKey(de
);
9222 if (foundkey
->storage
== REDIS_VM_MEMORY
) return 0;
9227 /* =================== Virtual Memory - Threaded I/O ======================= */
9229 static void freeIOJob(iojob
*j
) {
9230 if ((j
->type
== REDIS_IOJOB_PREPARE_SWAP
||
9231 j
->type
== REDIS_IOJOB_DO_SWAP
||
9232 j
->type
== REDIS_IOJOB_LOAD
) && j
->val
!= NULL
)
9233 decrRefCount(j
->val
);
9234 /* We don't decrRefCount the j->key field as we did't incremented
9235 * the count creating IO Jobs. This is because the key field here is
9236 * just used as an indentifier and if a key is removed the Job should
9237 * never be touched again. */
9241 /* Every time a thread finished a Job, it writes a byte into the write side
9242 * of an unix pipe in order to "awake" the main thread, and this function
9244 static void vmThreadedIOCompletedJob(aeEventLoop
*el
, int fd
, void *privdata
,
9248 int retval
, processed
= 0, toprocess
= -1, trytoswap
= 1;
9250 REDIS_NOTUSED(mask
);
9251 REDIS_NOTUSED(privdata
);
9253 /* For every byte we read in the read side of the pipe, there is one
9254 * I/O job completed to process. */
9255 while((retval
= read(fd
,buf
,1)) == 1) {
9259 struct dictEntry
*de
;
9261 redisLog(REDIS_DEBUG
,"Processing I/O completed job");
9263 /* Get the processed element (the oldest one) */
9265 assert(listLength(server
.io_processed
) != 0);
9266 if (toprocess
== -1) {
9267 toprocess
= (listLength(server
.io_processed
)*REDIS_MAX_COMPLETED_JOBS_PROCESSED
)/100;
9268 if (toprocess
<= 0) toprocess
= 1;
9270 ln
= listFirst(server
.io_processed
);
9272 listDelNode(server
.io_processed
,ln
);
9274 /* If this job is marked as canceled, just ignore it */
9279 /* Post process it in the main thread, as there are things we
9280 * can do just here to avoid race conditions and/or invasive locks */
9281 redisLog(REDIS_DEBUG
,"Job %p type: %d, key at %p (%s) refcount: %d\n", (void*) j
, j
->type
, (void*)j
->key
, (char*)j
->key
->ptr
, j
->key
->refcount
);
9282 de
= dictFind(j
->db
->dict
,j
->key
);
9284 key
= dictGetEntryKey(de
);
9285 if (j
->type
== REDIS_IOJOB_LOAD
) {
9288 /* Key loaded, bring it at home */
9289 key
->storage
= REDIS_VM_MEMORY
;
9290 key
->vm
.atime
= server
.unixtime
;
9291 vmMarkPagesFree(key
->vm
.page
,key
->vm
.usedpages
);
9292 redisLog(REDIS_DEBUG
, "VM: object %s loaded from disk (threaded)",
9293 (unsigned char*) key
->ptr
);
9294 server
.vm_stats_swapped_objects
--;
9295 server
.vm_stats_swapins
++;
9296 dictGetEntryVal(de
) = j
->val
;
9297 incrRefCount(j
->val
);
9300 /* Handle clients waiting for this key to be loaded. */
9301 handleClientsBlockedOnSwappedKey(db
,key
);
9302 } else if (j
->type
== REDIS_IOJOB_PREPARE_SWAP
) {
9303 /* Now we know the amount of pages required to swap this object.
9304 * Let's find some space for it, and queue this task again
9305 * rebranded as REDIS_IOJOB_DO_SWAP. */
9306 if (!vmCanSwapOut() ||
9307 vmFindContiguousPages(&j
->page
,j
->pages
) == REDIS_ERR
)
9309 /* Ooops... no space or we can't swap as there is
9310 * a fork()ed Redis trying to save stuff on disk. */
9312 key
->storage
= REDIS_VM_MEMORY
; /* undo operation */
9314 /* Note that we need to mark this pages as used now,
9315 * if the job will be canceled, we'll mark them as freed
9317 vmMarkPagesUsed(j
->page
,j
->pages
);
9318 j
->type
= REDIS_IOJOB_DO_SWAP
;
9323 } else if (j
->type
== REDIS_IOJOB_DO_SWAP
) {
9326 /* Key swapped. We can finally free some memory. */
9327 if (key
->storage
!= REDIS_VM_SWAPPING
) {
9328 printf("key->storage: %d\n",key
->storage
);
9329 printf("key->name: %s\n",(char*)key
->ptr
);
9330 printf("key->refcount: %d\n",key
->refcount
);
9331 printf("val: %p\n",(void*)j
->val
);
9332 printf("val->type: %d\n",j
->val
->type
);
9333 printf("val->ptr: %s\n",(char*)j
->val
->ptr
);
9335 redisAssert(key
->storage
== REDIS_VM_SWAPPING
);
9336 val
= dictGetEntryVal(de
);
9337 key
->vm
.page
= j
->page
;
9338 key
->vm
.usedpages
= j
->pages
;
9339 key
->storage
= REDIS_VM_SWAPPED
;
9340 key
->vtype
= j
->val
->type
;
9341 decrRefCount(val
); /* Deallocate the object from memory. */
9342 dictGetEntryVal(de
) = NULL
;
9343 redisLog(REDIS_DEBUG
,
9344 "VM: object %s swapped out at %lld (%lld pages) (threaded)",
9345 (unsigned char*) key
->ptr
,
9346 (unsigned long long) j
->page
, (unsigned long long) j
->pages
);
9347 server
.vm_stats_swapped_objects
++;
9348 server
.vm_stats_swapouts
++;
9350 /* Put a few more swap requests in queue if we are still
9352 if (trytoswap
&& vmCanSwapOut() &&
9353 zmalloc_used_memory() > server
.vm_max_memory
)
9358 more
= listLength(server
.io_newjobs
) <
9359 (unsigned) server
.vm_max_threads
;
9361 /* Don't waste CPU time if swappable objects are rare. */
9362 if (vmSwapOneObjectThreaded() == REDIS_ERR
) {
9370 if (processed
== toprocess
) return;
9372 if (retval
< 0 && errno
!= EAGAIN
) {
9373 redisLog(REDIS_WARNING
,
9374 "WARNING: read(2) error in vmThreadedIOCompletedJob() %s",
9379 static void lockThreadedIO(void) {
9380 pthread_mutex_lock(&server
.io_mutex
);
9383 static void unlockThreadedIO(void) {
9384 pthread_mutex_unlock(&server
.io_mutex
);
9387 /* Remove the specified object from the threaded I/O queue if still not
9388 * processed, otherwise make sure to flag it as canceled. */
9389 static void vmCancelThreadedIOJob(robj
*o
) {
9391 server
.io_newjobs
, /* 0 */
9392 server
.io_processing
, /* 1 */
9393 server
.io_processed
/* 2 */
9397 assert(o
->storage
== REDIS_VM_LOADING
|| o
->storage
== REDIS_VM_SWAPPING
);
9400 /* Search for a matching key in one of the queues */
9401 for (i
= 0; i
< 3; i
++) {
9405 listRewind(lists
[i
],&li
);
9406 while ((ln
= listNext(&li
)) != NULL
) {
9407 iojob
*job
= ln
->value
;
9409 if (job
->canceled
) continue; /* Skip this, already canceled. */
9410 if (job
->key
== o
) {
9411 redisLog(REDIS_DEBUG
,"*** CANCELED %p (%s) (type %d) (LIST ID %d)\n",
9412 (void*)job
, (char*)o
->ptr
, job
->type
, i
);
9413 /* Mark the pages as free since the swap didn't happened
9414 * or happened but is now discarded. */
9415 if (i
!= 1 && job
->type
== REDIS_IOJOB_DO_SWAP
)
9416 vmMarkPagesFree(job
->page
,job
->pages
);
9417 /* Cancel the job. It depends on the list the job is
9420 case 0: /* io_newjobs */
9421 /* If the job was yet not processed the best thing to do
9422 * is to remove it from the queue at all */
9424 listDelNode(lists
[i
],ln
);
9426 case 1: /* io_processing */
9427 /* Oh Shi- the thread is messing with the Job:
9429 * Probably it's accessing the object if this is a
9430 * PREPARE_SWAP or DO_SWAP job.
9431 * If it's a LOAD job it may be reading from disk and
9432 * if we don't wait for the job to terminate before to
9433 * cancel it, maybe in a few microseconds data can be
9434 * corrupted in this pages. So the short story is:
9436 * Better to wait for the job to move into the
9437 * next queue (processed)... */
9439 /* We try again and again until the job is completed. */
9441 /* But let's wait some time for the I/O thread
9442 * to finish with this job. After all this condition
9443 * should be very rare. */
9446 case 2: /* io_processed */
9447 /* The job was already processed, that's easy...
9448 * just mark it as canceled so that we'll ignore it
9449 * when processing completed jobs. */
9453 /* Finally we have to adjust the storage type of the object
9454 * in order to "UNDO" the operaiton. */
9455 if (o
->storage
== REDIS_VM_LOADING
)
9456 o
->storage
= REDIS_VM_SWAPPED
;
9457 else if (o
->storage
== REDIS_VM_SWAPPING
)
9458 o
->storage
= REDIS_VM_MEMORY
;
9465 assert(1 != 1); /* We should never reach this */
9468 static void *IOThreadEntryPoint(void *arg
) {
9473 pthread_detach(pthread_self());
9475 /* Get a new job to process */
9477 if (listLength(server
.io_newjobs
) == 0) {
9478 /* No new jobs in queue, exit. */
9479 redisLog(REDIS_DEBUG
,"Thread %ld exiting, nothing to do",
9480 (long) pthread_self());
9481 server
.io_active_threads
--;
9485 ln
= listFirst(server
.io_newjobs
);
9487 listDelNode(server
.io_newjobs
,ln
);
9488 /* Add the job in the processing queue */
9489 j
->thread
= pthread_self();
9490 listAddNodeTail(server
.io_processing
,j
);
9491 ln
= listLast(server
.io_processing
); /* We use ln later to remove it */
9493 redisLog(REDIS_DEBUG
,"Thread %ld got a new job (type %d): %p about key '%s'",
9494 (long) pthread_self(), j
->type
, (void*)j
, (char*)j
->key
->ptr
);
9496 /* Process the Job */
9497 if (j
->type
== REDIS_IOJOB_LOAD
) {
9498 j
->val
= vmReadObjectFromSwap(j
->page
,j
->key
->vtype
);
9499 } else if (j
->type
== REDIS_IOJOB_PREPARE_SWAP
) {
9500 FILE *fp
= fopen("/dev/null","w+");
9501 j
->pages
= rdbSavedObjectPages(j
->val
,fp
);
9503 } else if (j
->type
== REDIS_IOJOB_DO_SWAP
) {
9504 if (vmWriteObjectOnSwap(j
->val
,j
->page
) == REDIS_ERR
)
9508 /* Done: insert the job into the processed queue */
9509 redisLog(REDIS_DEBUG
,"Thread %ld completed the job: %p (key %s)",
9510 (long) pthread_self(), (void*)j
, (char*)j
->key
->ptr
);
9512 listDelNode(server
.io_processing
,ln
);
9513 listAddNodeTail(server
.io_processed
,j
);
9516 /* Signal the main thread there is new stuff to process */
9517 assert(write(server
.io_ready_pipe_write
,"x",1) == 1);
9519 return NULL
; /* never reached */
9522 static void spawnIOThread(void) {
9524 sigset_t mask
, omask
;
9528 sigaddset(&mask
,SIGCHLD
);
9529 sigaddset(&mask
,SIGHUP
);
9530 sigaddset(&mask
,SIGPIPE
);
9531 pthread_sigmask(SIG_SETMASK
, &mask
, &omask
);
9532 while ((err
= pthread_create(&thread
,&server
.io_threads_attr
,IOThreadEntryPoint
,NULL
)) != 0) {
9533 redisLog(REDIS_WARNING
,"Unable to spawn an I/O thread: %s",
9537 pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
9538 server
.io_active_threads
++;
9541 /* We need to wait for the last thread to exit before we are able to
9542 * fork() in order to BGSAVE or BGREWRITEAOF. */
9543 static void waitEmptyIOJobsQueue(void) {
9545 int io_processed_len
;
9548 if (listLength(server
.io_newjobs
) == 0 &&
9549 listLength(server
.io_processing
) == 0 &&
9550 server
.io_active_threads
== 0)
9555 /* While waiting for empty jobs queue condition we post-process some
9556 * finshed job, as I/O threads may be hanging trying to write against
9557 * the io_ready_pipe_write FD but there are so much pending jobs that
9559 io_processed_len
= listLength(server
.io_processed
);
9561 if (io_processed_len
) {
9562 vmThreadedIOCompletedJob(NULL
,server
.io_ready_pipe_read
,NULL
,0);
9563 usleep(1000); /* 1 millisecond */
9565 usleep(10000); /* 10 milliseconds */
9570 static void vmReopenSwapFile(void) {
9571 /* Note: we don't close the old one as we are in the child process
9572 * and don't want to mess at all with the original file object. */
9573 server
.vm_fp
= fopen(server
.vm_swap_file
,"r+b");
9574 if (server
.vm_fp
== NULL
) {
9575 redisLog(REDIS_WARNING
,"Can't re-open the VM swap file: %s. Exiting.",
9576 server
.vm_swap_file
);
9579 server
.vm_fd
= fileno(server
.vm_fp
);
9582 /* This function must be called while with threaded IO locked */
9583 static void queueIOJob(iojob
*j
) {
9584 redisLog(REDIS_DEBUG
,"Queued IO Job %p type %d about key '%s'\n",
9585 (void*)j
, j
->type
, (char*)j
->key
->ptr
);
9586 listAddNodeTail(server
.io_newjobs
,j
);
9587 if (server
.io_active_threads
< server
.vm_max_threads
)
9591 static int vmSwapObjectThreaded(robj
*key
, robj
*val
, redisDb
*db
) {
9594 assert(key
->storage
== REDIS_VM_MEMORY
);
9595 assert(key
->refcount
== 1);
9597 j
= zmalloc(sizeof(*j
));
9598 j
->type
= REDIS_IOJOB_PREPARE_SWAP
;
9604 j
->thread
= (pthread_t
) -1;
9605 key
->storage
= REDIS_VM_SWAPPING
;
9613 /* ============ Virtual Memory - Blocking clients on missing keys =========== */
9615 /* This function makes the clinet 'c' waiting for the key 'key' to be loaded.
9616 * If there is not already a job loading the key, it is craeted.
9617 * The key is added to the io_keys list in the client structure, and also
9618 * in the hash table mapping swapped keys to waiting clients, that is,
9619 * server.io_waited_keys. */
9620 static int waitForSwappedKey(redisClient
*c
, robj
*key
) {
9621 struct dictEntry
*de
;
9625 /* If the key does not exist or is already in RAM we don't need to
9626 * block the client at all. */
9627 de
= dictFind(c
->db
->dict
,key
);
9628 if (de
== NULL
) return 0;
9629 o
= dictGetEntryKey(de
);
9630 if (o
->storage
== REDIS_VM_MEMORY
) {
9632 } else if (o
->storage
== REDIS_VM_SWAPPING
) {
9633 /* We were swapping the key, undo it! */
9634 vmCancelThreadedIOJob(o
);
9638 /* OK: the key is either swapped, or being loaded just now. */
9640 /* Add the key to the list of keys this client is waiting for.
9641 * This maps clients to keys they are waiting for. */
9642 listAddNodeTail(c
->io_keys
,key
);
9645 /* Add the client to the swapped keys => clients waiting map. */
9646 de
= dictFind(c
->db
->io_keys
,key
);
9650 /* For every key we take a list of clients blocked for it */
9652 retval
= dictAdd(c
->db
->io_keys
,key
,l
);
9654 assert(retval
== DICT_OK
);
9656 l
= dictGetEntryVal(de
);
9658 listAddNodeTail(l
,c
);
9660 /* Are we already loading the key from disk? If not create a job */
9661 if (o
->storage
== REDIS_VM_SWAPPED
) {
9664 o
->storage
= REDIS_VM_LOADING
;
9665 j
= zmalloc(sizeof(*j
));
9666 j
->type
= REDIS_IOJOB_LOAD
;
9669 j
->key
->vtype
= o
->vtype
;
9670 j
->page
= o
->vm
.page
;
9673 j
->thread
= (pthread_t
) -1;
9681 /* Preload keys for any command with first, last and step values for
9682 * the command keys prototype, as defined in the command table. */
9683 static void waitForMultipleSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
) {
9685 if (cmd
->vm_firstkey
== 0) return;
9686 last
= cmd
->vm_lastkey
;
9687 if (last
< 0) last
= argc
+last
;
9688 for (j
= cmd
->vm_firstkey
; j
<= last
; j
+= cmd
->vm_keystep
) {
9689 redisAssert(j
< argc
);
9690 waitForSwappedKey(c
,argv
[j
]);
9694 /* Preload keys needed for the ZUNIONSTORE and ZINTERSTORE commands.
9695 * Note that the number of keys to preload is user-defined, so we need to
9696 * apply a sanity check against argc. */
9697 static void zunionInterBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
) {
9701 num
= atoi(argv
[2]->ptr
);
9702 if (num
> (argc
-3)) return;
9703 for (i
= 0; i
< num
; i
++) {
9704 waitForSwappedKey(c
,argv
[3+i
]);
9708 /* Preload keys needed to execute the entire MULTI/EXEC block.
9710 * This function is called by blockClientOnSwappedKeys when EXEC is issued,
9711 * and will block the client when any command requires a swapped out value. */
9712 static void execBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
) {
9714 struct redisCommand
*mcmd
;
9717 REDIS_NOTUSED(argc
);
9718 REDIS_NOTUSED(argv
);
9720 if (!(c
->flags
& REDIS_MULTI
)) return;
9721 for (i
= 0; i
< c
->mstate
.count
; i
++) {
9722 mcmd
= c
->mstate
.commands
[i
].cmd
;
9723 margc
= c
->mstate
.commands
[i
].argc
;
9724 margv
= c
->mstate
.commands
[i
].argv
;
9726 if (mcmd
->vm_preload_proc
!= NULL
) {
9727 mcmd
->vm_preload_proc(c
,mcmd
,margc
,margv
);
9729 waitForMultipleSwappedKeys(c
,mcmd
,margc
,margv
);
9734 /* Is this client attempting to run a command against swapped keys?
9735 * If so, block it ASAP, load the keys in background, then resume it.
9737 * The important idea about this function is that it can fail! If keys will
9738 * still be swapped when the client is resumed, this key lookups will
9739 * just block loading keys from disk. In practical terms this should only
9740 * happen with SORT BY command or if there is a bug in this function.
9742 * Return 1 if the client is marked as blocked, 0 if the client can
9743 * continue as the keys it is going to access appear to be in memory. */
9744 static int blockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
) {
9745 if (cmd
->vm_preload_proc
!= NULL
) {
9746 cmd
->vm_preload_proc(c
,cmd
,c
->argc
,c
->argv
);
9748 waitForMultipleSwappedKeys(c
,cmd
,c
->argc
,c
->argv
);
9751 /* If the client was blocked for at least one key, mark it as blocked. */
9752 if (listLength(c
->io_keys
)) {
9753 c
->flags
|= REDIS_IO_WAIT
;
9754 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
9755 server
.vm_blocked_clients
++;
9762 /* Remove the 'key' from the list of blocked keys for a given client.
9764 * The function returns 1 when there are no longer blocking keys after
9765 * the current one was removed (and the client can be unblocked). */
9766 static int dontWaitForSwappedKey(redisClient
*c
, robj
*key
) {
9770 struct dictEntry
*de
;
9772 /* Remove the key from the list of keys this client is waiting for. */
9773 listRewind(c
->io_keys
,&li
);
9774 while ((ln
= listNext(&li
)) != NULL
) {
9775 if (equalStringObjects(ln
->value
,key
)) {
9776 listDelNode(c
->io_keys
,ln
);
9782 /* Remove the client form the key => waiting clients map. */
9783 de
= dictFind(c
->db
->io_keys
,key
);
9785 l
= dictGetEntryVal(de
);
9786 ln
= listSearchKey(l
,c
);
9789 if (listLength(l
) == 0)
9790 dictDelete(c
->db
->io_keys
,key
);
9792 return listLength(c
->io_keys
) == 0;
9795 static void handleClientsBlockedOnSwappedKey(redisDb
*db
, robj
*key
) {
9796 struct dictEntry
*de
;
9801 de
= dictFind(db
->io_keys
,key
);
9804 l
= dictGetEntryVal(de
);
9805 len
= listLength(l
);
9806 /* Note: we can't use something like while(listLength(l)) as the list
9807 * can be freed by the calling function when we remove the last element. */
9810 redisClient
*c
= ln
->value
;
9812 if (dontWaitForSwappedKey(c
,key
)) {
9813 /* Put the client in the list of clients ready to go as we
9814 * loaded all the keys about it. */
9815 listAddNodeTail(server
.io_ready_clients
,c
);
9820 /* =========================== Remote Configuration ========================= */
9822 static void configSetCommand(redisClient
*c
) {
9823 robj
*o
= getDecodedObject(c
->argv
[3]);
9824 if (!strcasecmp(c
->argv
[2]->ptr
,"dbfilename")) {
9825 zfree(server
.dbfilename
);
9826 server
.dbfilename
= zstrdup(o
->ptr
);
9827 } else if (!strcasecmp(c
->argv
[2]->ptr
,"requirepass")) {
9828 zfree(server
.requirepass
);
9829 server
.requirepass
= zstrdup(o
->ptr
);
9830 } else if (!strcasecmp(c
->argv
[2]->ptr
,"masterauth")) {
9831 zfree(server
.masterauth
);
9832 server
.masterauth
= zstrdup(o
->ptr
);
9833 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory")) {
9834 server
.maxmemory
= strtoll(o
->ptr
, NULL
, 10);
9835 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendfsync")) {
9836 if (!strcasecmp(o
->ptr
,"no")) {
9837 server
.appendfsync
= APPENDFSYNC_NO
;
9838 } else if (!strcasecmp(o
->ptr
,"everysec")) {
9839 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
9840 } else if (!strcasecmp(o
->ptr
,"always")) {
9841 server
.appendfsync
= APPENDFSYNC_ALWAYS
;
9845 } else if (!strcasecmp(c
->argv
[2]->ptr
,"save")) {
9847 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
9849 /* Perform sanity check before setting the new config:
9850 * - Even number of args
9851 * - Seconds >= 1, changes >= 0 */
9853 sdsfreesplitres(v
,vlen
);
9856 for (j
= 0; j
< vlen
; j
++) {
9860 val
= strtoll(v
[j
], &eptr
, 10);
9861 if (eptr
[0] != '\0' ||
9862 ((j
& 1) == 0 && val
< 1) ||
9863 ((j
& 1) == 1 && val
< 0)) {
9864 sdsfreesplitres(v
,vlen
);
9868 /* Finally set the new config */
9869 resetServerSaveParams();
9870 for (j
= 0; j
< vlen
; j
+= 2) {
9874 seconds
= strtoll(v
[j
],NULL
,10);
9875 changes
= strtoll(v
[j
+1],NULL
,10);
9876 appendServerSaveParams(seconds
, changes
);
9878 sdsfreesplitres(v
,vlen
);
9880 addReplySds(c
,sdscatprintf(sdsempty(),
9881 "-ERR not supported CONFIG parameter %s\r\n",
9882 (char*)c
->argv
[2]->ptr
));
9887 addReply(c
,shared
.ok
);
9890 badfmt
: /* Bad format errors */
9891 addReplySds(c
,sdscatprintf(sdsempty(),
9892 "-ERR invalid argument '%s' for CONFIG SET '%s'\r\n",
9894 (char*)c
->argv
[2]->ptr
));
9898 static void configGetCommand(redisClient
*c
) {
9899 robj
*o
= getDecodedObject(c
->argv
[2]);
9900 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
9901 char *pattern
= o
->ptr
;
9905 decrRefCount(lenobj
);
9907 if (stringmatch(pattern
,"dbfilename",0)) {
9908 addReplyBulkCString(c
,"dbfilename");
9909 addReplyBulkCString(c
,server
.dbfilename
);
9912 if (stringmatch(pattern
,"requirepass",0)) {
9913 addReplyBulkCString(c
,"requirepass");
9914 addReplyBulkCString(c
,server
.requirepass
);
9917 if (stringmatch(pattern
,"masterauth",0)) {
9918 addReplyBulkCString(c
,"masterauth");
9919 addReplyBulkCString(c
,server
.masterauth
);
9922 if (stringmatch(pattern
,"maxmemory",0)) {
9925 snprintf(buf
,128,"%llu\n",server
.maxmemory
);
9926 addReplyBulkCString(c
,"maxmemory");
9927 addReplyBulkCString(c
,buf
);
9930 if (stringmatch(pattern
,"appendfsync",0)) {
9933 switch(server
.appendfsync
) {
9934 case APPENDFSYNC_NO
: policy
= "no"; break;
9935 case APPENDFSYNC_EVERYSEC
: policy
= "everysec"; break;
9936 case APPENDFSYNC_ALWAYS
: policy
= "always"; break;
9937 default: policy
= "unknown"; break; /* too harmless to panic */
9939 addReplyBulkCString(c
,"appendfsync");
9940 addReplyBulkCString(c
,policy
);
9943 if (stringmatch(pattern
,"save",0)) {
9944 sds buf
= sdsempty();
9947 for (j
= 0; j
< server
.saveparamslen
; j
++) {
9948 buf
= sdscatprintf(buf
,"%ld %d",
9949 server
.saveparams
[j
].seconds
,
9950 server
.saveparams
[j
].changes
);
9951 if (j
!= server
.saveparamslen
-1)
9952 buf
= sdscatlen(buf
," ",1);
9954 addReplyBulkCString(c
,"save");
9955 addReplyBulkCString(c
,buf
);
9960 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%d\r\n",matches
*2);
9963 static void configCommand(redisClient
*c
) {
9964 if (!strcasecmp(c
->argv
[1]->ptr
,"set")) {
9965 if (c
->argc
!= 4) goto badarity
;
9966 configSetCommand(c
);
9967 } else if (!strcasecmp(c
->argv
[1]->ptr
,"get")) {
9968 if (c
->argc
!= 3) goto badarity
;
9969 configGetCommand(c
);
9970 } else if (!strcasecmp(c
->argv
[1]->ptr
,"resetstat")) {
9971 if (c
->argc
!= 2) goto badarity
;
9972 server
.stat_numcommands
= 0;
9973 server
.stat_numconnections
= 0;
9974 server
.stat_expiredkeys
= 0;
9975 server
.stat_starttime
= time(NULL
);
9976 addReply(c
,shared
.ok
);
9978 addReplySds(c
,sdscatprintf(sdsempty(),
9979 "-ERR CONFIG subcommand must be one of GET, SET, RESETSTAT\r\n"));
9984 addReplySds(c
,sdscatprintf(sdsempty(),
9985 "-ERR Wrong number of arguments for CONFIG %s\r\n",
9986 (char*) c
->argv
[1]->ptr
));
9989 /* =========================== Pubsub implementation ======================== */
9991 static void freePubsubPattern(void *p
) {
9992 pubsubPattern
*pat
= p
;
9994 decrRefCount(pat
->pattern
);
9998 static int listMatchPubsubPattern(void *a
, void *b
) {
9999 pubsubPattern
*pa
= a
, *pb
= b
;
10001 return (pa
->client
== pb
->client
) &&
10002 (equalStringObjects(pa
->pattern
,pb
->pattern
));
10005 /* Subscribe a client to a channel. Returns 1 if the operation succeeded, or
10006 * 0 if the client was already subscribed to that channel. */
10007 static int pubsubSubscribeChannel(redisClient
*c
, robj
*channel
) {
10008 struct dictEntry
*de
;
10009 list
*clients
= NULL
;
10012 /* Add the channel to the client -> channels hash table */
10013 if (dictAdd(c
->pubsub_channels
,channel
,NULL
) == DICT_OK
) {
10015 incrRefCount(channel
);
10016 /* Add the client to the channel -> list of clients hash table */
10017 de
= dictFind(server
.pubsub_channels
,channel
);
10019 clients
= listCreate();
10020 dictAdd(server
.pubsub_channels
,channel
,clients
);
10021 incrRefCount(channel
);
10023 clients
= dictGetEntryVal(de
);
10025 listAddNodeTail(clients
,c
);
10027 /* Notify the client */
10028 addReply(c
,shared
.mbulk3
);
10029 addReply(c
,shared
.subscribebulk
);
10030 addReplyBulk(c
,channel
);
10031 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+listLength(c
->pubsub_patterns
));
10035 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10036 * 0 if the client was not subscribed to the specified channel. */
10037 static int pubsubUnsubscribeChannel(redisClient
*c
, robj
*channel
, int notify
) {
10038 struct dictEntry
*de
;
10043 /* Remove the channel from the client -> channels hash table */
10044 incrRefCount(channel
); /* channel may be just a pointer to the same object
10045 we have in the hash tables. Protect it... */
10046 if (dictDelete(c
->pubsub_channels
,channel
) == DICT_OK
) {
10048 /* Remove the client from the channel -> clients list hash table */
10049 de
= dictFind(server
.pubsub_channels
,channel
);
10050 assert(de
!= NULL
);
10051 clients
= dictGetEntryVal(de
);
10052 ln
= listSearchKey(clients
,c
);
10053 assert(ln
!= NULL
);
10054 listDelNode(clients
,ln
);
10055 if (listLength(clients
) == 0) {
10056 /* Free the list and associated hash entry at all if this was
10057 * the latest client, so that it will be possible to abuse
10058 * Redis PUBSUB creating millions of channels. */
10059 dictDelete(server
.pubsub_channels
,channel
);
10062 /* Notify the client */
10064 addReply(c
,shared
.mbulk3
);
10065 addReply(c
,shared
.unsubscribebulk
);
10066 addReplyBulk(c
,channel
);
10067 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+
10068 listLength(c
->pubsub_patterns
));
10071 decrRefCount(channel
); /* it is finally safe to release it */
10075 /* Subscribe a client to a pattern. Returns 1 if the operation succeeded, or 0 if the clinet was already subscribed to that pattern. */
10076 static int pubsubSubscribePattern(redisClient
*c
, robj
*pattern
) {
10079 if (listSearchKey(c
->pubsub_patterns
,pattern
) == NULL
) {
10081 pubsubPattern
*pat
;
10082 listAddNodeTail(c
->pubsub_patterns
,pattern
);
10083 incrRefCount(pattern
);
10084 pat
= zmalloc(sizeof(*pat
));
10085 pat
->pattern
= getDecodedObject(pattern
);
10087 listAddNodeTail(server
.pubsub_patterns
,pat
);
10089 /* Notify the client */
10090 addReply(c
,shared
.mbulk3
);
10091 addReply(c
,shared
.psubscribebulk
);
10092 addReplyBulk(c
,pattern
);
10093 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+listLength(c
->pubsub_patterns
));
10097 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10098 * 0 if the client was not subscribed to the specified channel. */
10099 static int pubsubUnsubscribePattern(redisClient
*c
, robj
*pattern
, int notify
) {
10104 incrRefCount(pattern
); /* Protect the object. May be the same we remove */
10105 if ((ln
= listSearchKey(c
->pubsub_patterns
,pattern
)) != NULL
) {
10107 listDelNode(c
->pubsub_patterns
,ln
);
10109 pat
.pattern
= pattern
;
10110 ln
= listSearchKey(server
.pubsub_patterns
,&pat
);
10111 listDelNode(server
.pubsub_patterns
,ln
);
10113 /* Notify the client */
10115 addReply(c
,shared
.mbulk3
);
10116 addReply(c
,shared
.punsubscribebulk
);
10117 addReplyBulk(c
,pattern
);
10118 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+
10119 listLength(c
->pubsub_patterns
));
10121 decrRefCount(pattern
);
10125 /* Unsubscribe from all the channels. Return the number of channels the
10126 * client was subscribed from. */
10127 static int pubsubUnsubscribeAllChannels(redisClient
*c
, int notify
) {
10128 dictIterator
*di
= dictGetIterator(c
->pubsub_channels
);
10132 while((de
= dictNext(di
)) != NULL
) {
10133 robj
*channel
= dictGetEntryKey(de
);
10135 count
+= pubsubUnsubscribeChannel(c
,channel
,notify
);
10137 dictReleaseIterator(di
);
10141 /* Unsubscribe from all the patterns. Return the number of patterns the
10142 * client was subscribed from. */
10143 static int pubsubUnsubscribeAllPatterns(redisClient
*c
, int notify
) {
10148 listRewind(c
->pubsub_patterns
,&li
);
10149 while ((ln
= listNext(&li
)) != NULL
) {
10150 robj
*pattern
= ln
->value
;
10152 count
+= pubsubUnsubscribePattern(c
,pattern
,notify
);
10157 /* Publish a message */
10158 static int pubsubPublishMessage(robj
*channel
, robj
*message
) {
10160 struct dictEntry
*de
;
10164 /* Send to clients listening for that channel */
10165 de
= dictFind(server
.pubsub_channels
,channel
);
10167 list
*list
= dictGetEntryVal(de
);
10171 listRewind(list
,&li
);
10172 while ((ln
= listNext(&li
)) != NULL
) {
10173 redisClient
*c
= ln
->value
;
10175 addReply(c
,shared
.mbulk3
);
10176 addReply(c
,shared
.messagebulk
);
10177 addReplyBulk(c
,channel
);
10178 addReplyBulk(c
,message
);
10182 /* Send to clients listening to matching channels */
10183 if (listLength(server
.pubsub_patterns
)) {
10184 listRewind(server
.pubsub_patterns
,&li
);
10185 channel
= getDecodedObject(channel
);
10186 while ((ln
= listNext(&li
)) != NULL
) {
10187 pubsubPattern
*pat
= ln
->value
;
10189 if (stringmatchlen((char*)pat
->pattern
->ptr
,
10190 sdslen(pat
->pattern
->ptr
),
10191 (char*)channel
->ptr
,
10192 sdslen(channel
->ptr
),0)) {
10193 addReply(pat
->client
,shared
.mbulk4
);
10194 addReply(pat
->client
,shared
.pmessagebulk
);
10195 addReplyBulk(pat
->client
,pat
->pattern
);
10196 addReplyBulk(pat
->client
,channel
);
10197 addReplyBulk(pat
->client
,message
);
10201 decrRefCount(channel
);
10206 static void subscribeCommand(redisClient
*c
) {
10209 for (j
= 1; j
< c
->argc
; j
++)
10210 pubsubSubscribeChannel(c
,c
->argv
[j
]);
10213 static void unsubscribeCommand(redisClient
*c
) {
10214 if (c
->argc
== 1) {
10215 pubsubUnsubscribeAllChannels(c
,1);
10220 for (j
= 1; j
< c
->argc
; j
++)
10221 pubsubUnsubscribeChannel(c
,c
->argv
[j
],1);
10225 static void psubscribeCommand(redisClient
*c
) {
10228 for (j
= 1; j
< c
->argc
; j
++)
10229 pubsubSubscribePattern(c
,c
->argv
[j
]);
10232 static void punsubscribeCommand(redisClient
*c
) {
10233 if (c
->argc
== 1) {
10234 pubsubUnsubscribeAllPatterns(c
,1);
10239 for (j
= 1; j
< c
->argc
; j
++)
10240 pubsubUnsubscribePattern(c
,c
->argv
[j
],1);
10244 static void publishCommand(redisClient
*c
) {
10245 int receivers
= pubsubPublishMessage(c
->argv
[1],c
->argv
[2]);
10246 addReplyLongLong(c
,receivers
);
10249 /* ================================= Debugging ============================== */
10251 /* Compute the sha1 of string at 's' with 'len' bytes long.
10252 * The SHA1 is then xored againt the string pointed by digest.
10253 * Since xor is commutative, this operation is used in order to
10254 * "add" digests relative to unordered elements.
10256 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
10257 static void xorDigest(unsigned char *digest
, void *ptr
, size_t len
) {
10259 unsigned char hash
[20], *s
= ptr
;
10263 SHA1Update(&ctx
,s
,len
);
10264 SHA1Final(hash
,&ctx
);
10266 for (j
= 0; j
< 20; j
++)
10267 digest
[j
] ^= hash
[j
];
10270 static void xorObjectDigest(unsigned char *digest
, robj
*o
) {
10271 o
= getDecodedObject(o
);
10272 xorDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
10276 /* This function instead of just computing the SHA1 and xoring it
10277 * against diget, also perform the digest of "digest" itself and
10278 * replace the old value with the new one.
10280 * So the final digest will be:
10282 * digest = SHA1(digest xor SHA1(data))
10284 * This function is used every time we want to preserve the order so
10285 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
10287 * Also note that mixdigest("foo") followed by mixdigest("bar")
10288 * will lead to a different digest compared to "fo", "obar".
10290 static void mixDigest(unsigned char *digest
, void *ptr
, size_t len
) {
10294 xorDigest(digest
,s
,len
);
10296 SHA1Update(&ctx
,digest
,20);
10297 SHA1Final(digest
,&ctx
);
10300 static void mixObjectDigest(unsigned char *digest
, robj
*o
) {
10301 o
= getDecodedObject(o
);
10302 mixDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
10306 /* Compute the dataset digest. Since keys, sets elements, hashes elements
10307 * are not ordered, we use a trick: every aggregate digest is the xor
10308 * of the digests of their elements. This way the order will not change
10309 * the result. For list instead we use a feedback entering the output digest
10310 * as input in order to ensure that a different ordered list will result in
10311 * a different digest. */
10312 static void computeDatasetDigest(unsigned char *final
) {
10313 unsigned char digest
[20];
10315 dictIterator
*di
= NULL
;
10320 memset(final
,0,20); /* Start with a clean result */
10322 for (j
= 0; j
< server
.dbnum
; j
++) {
10323 redisDb
*db
= server
.db
+j
;
10325 if (dictSize(db
->dict
) == 0) continue;
10326 di
= dictGetIterator(db
->dict
);
10328 /* hash the DB id, so the same dataset moved in a different
10329 * DB will lead to a different digest */
10331 mixDigest(final
,&aux
,sizeof(aux
));
10333 /* Iterate this DB writing every entry */
10334 while((de
= dictNext(di
)) != NULL
) {
10338 memset(digest
,0,20); /* This key-val digest */
10339 key
= dictGetEntryKey(de
);
10340 mixObjectDigest(digest
,key
);
10341 if (!server
.vm_enabled
|| key
->storage
== REDIS_VM_MEMORY
||
10342 key
->storage
== REDIS_VM_SWAPPING
) {
10343 o
= dictGetEntryVal(de
);
10346 o
= vmPreviewObject(key
);
10348 aux
= htonl(o
->type
);
10349 mixDigest(digest
,&aux
,sizeof(aux
));
10350 expiretime
= getExpire(db
,key
);
10352 /* Save the key and associated value */
10353 if (o
->type
== REDIS_STRING
) {
10354 mixObjectDigest(digest
,o
);
10355 } else if (o
->type
== REDIS_LIST
) {
10356 list
*list
= o
->ptr
;
10360 listRewind(list
,&li
);
10361 while((ln
= listNext(&li
))) {
10362 robj
*eleobj
= listNodeValue(ln
);
10364 mixObjectDigest(digest
,eleobj
);
10366 } else if (o
->type
== REDIS_SET
) {
10367 dict
*set
= o
->ptr
;
10368 dictIterator
*di
= dictGetIterator(set
);
10371 while((de
= dictNext(di
)) != NULL
) {
10372 robj
*eleobj
= dictGetEntryKey(de
);
10374 xorObjectDigest(digest
,eleobj
);
10376 dictReleaseIterator(di
);
10377 } else if (o
->type
== REDIS_ZSET
) {
10379 dictIterator
*di
= dictGetIterator(zs
->dict
);
10382 while((de
= dictNext(di
)) != NULL
) {
10383 robj
*eleobj
= dictGetEntryKey(de
);
10384 double *score
= dictGetEntryVal(de
);
10385 unsigned char eledigest
[20];
10387 snprintf(buf
,sizeof(buf
),"%.17g",*score
);
10388 memset(eledigest
,0,20);
10389 mixObjectDigest(eledigest
,eleobj
);
10390 mixDigest(eledigest
,buf
,strlen(buf
));
10391 xorDigest(digest
,eledigest
,20);
10393 dictReleaseIterator(di
);
10394 } else if (o
->type
== REDIS_HASH
) {
10398 hi
= hashInitIterator(o
);
10399 while (hashNext(hi
) != REDIS_ERR
) {
10400 unsigned char eledigest
[20];
10402 memset(eledigest
,0,20);
10403 obj
= hashCurrent(hi
,REDIS_HASH_KEY
);
10404 mixObjectDigest(eledigest
,obj
);
10406 obj
= hashCurrent(hi
,REDIS_HASH_VALUE
);
10407 mixObjectDigest(eledigest
,obj
);
10409 xorDigest(digest
,eledigest
,20);
10411 hashReleaseIterator(hi
);
10413 redisPanic("Unknown object type");
10416 /* If the key has an expire, add it to the mix */
10417 if (expiretime
!= -1) xorDigest(digest
,"!!expire!!",10);
10418 /* We can finally xor the key-val digest to the final digest */
10419 xorDigest(final
,digest
,20);
10421 dictReleaseIterator(di
);
10425 static void debugCommand(redisClient
*c
) {
10426 if (!strcasecmp(c
->argv
[1]->ptr
,"segfault")) {
10427 *((char*)-1) = 'x';
10428 } else if (!strcasecmp(c
->argv
[1]->ptr
,"reload")) {
10429 if (rdbSave(server
.dbfilename
) != REDIS_OK
) {
10430 addReply(c
,shared
.err
);
10434 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
10435 addReply(c
,shared
.err
);
10438 redisLog(REDIS_WARNING
,"DB reloaded by DEBUG RELOAD");
10439 addReply(c
,shared
.ok
);
10440 } else if (!strcasecmp(c
->argv
[1]->ptr
,"loadaof")) {
10442 if (loadAppendOnlyFile(server
.appendfilename
) != REDIS_OK
) {
10443 addReply(c
,shared
.err
);
10446 redisLog(REDIS_WARNING
,"Append Only File loaded by DEBUG LOADAOF");
10447 addReply(c
,shared
.ok
);
10448 } else if (!strcasecmp(c
->argv
[1]->ptr
,"object") && c
->argc
== 3) {
10449 dictEntry
*de
= dictFind(c
->db
->dict
,c
->argv
[2]);
10453 addReply(c
,shared
.nokeyerr
);
10456 key
= dictGetEntryKey(de
);
10457 val
= dictGetEntryVal(de
);
10458 if (!server
.vm_enabled
|| (key
->storage
== REDIS_VM_MEMORY
||
10459 key
->storage
== REDIS_VM_SWAPPING
)) {
10463 if (val
->encoding
< (sizeof(strencoding
)/sizeof(char*))) {
10464 strenc
= strencoding
[val
->encoding
];
10466 snprintf(buf
,64,"unknown encoding %d\n", val
->encoding
);
10469 addReplySds(c
,sdscatprintf(sdsempty(),
10470 "+Key at:%p refcount:%d, value at:%p refcount:%d "
10471 "encoding:%s serializedlength:%lld\r\n",
10472 (void*)key
, key
->refcount
, (void*)val
, val
->refcount
,
10473 strenc
, (long long) rdbSavedObjectLen(val
,NULL
)));
10475 addReplySds(c
,sdscatprintf(sdsempty(),
10476 "+Key at:%p refcount:%d, value swapped at: page %llu "
10477 "using %llu pages\r\n",
10478 (void*)key
, key
->refcount
, (unsigned long long) key
->vm
.page
,
10479 (unsigned long long) key
->vm
.usedpages
));
10481 } else if (!strcasecmp(c
->argv
[1]->ptr
,"swapin") && c
->argc
== 3) {
10482 lookupKeyRead(c
->db
,c
->argv
[2]);
10483 addReply(c
,shared
.ok
);
10484 } else if (!strcasecmp(c
->argv
[1]->ptr
,"swapout") && c
->argc
== 3) {
10485 dictEntry
*de
= dictFind(c
->db
->dict
,c
->argv
[2]);
10488 if (!server
.vm_enabled
) {
10489 addReplySds(c
,sdsnew("-ERR Virtual Memory is disabled\r\n"));
10493 addReply(c
,shared
.nokeyerr
);
10496 key
= dictGetEntryKey(de
);
10497 val
= dictGetEntryVal(de
);
10498 /* If the key is shared we want to create a copy */
10499 if (key
->refcount
> 1) {
10500 robj
*newkey
= dupStringObject(key
);
10502 key
= dictGetEntryKey(de
) = newkey
;
10505 if (key
->storage
!= REDIS_VM_MEMORY
) {
10506 addReplySds(c
,sdsnew("-ERR This key is not in memory\r\n"));
10507 } else if (vmSwapObjectBlocking(key
,val
) == REDIS_OK
) {
10508 dictGetEntryVal(de
) = NULL
;
10509 addReply(c
,shared
.ok
);
10511 addReply(c
,shared
.err
);
10513 } else if (!strcasecmp(c
->argv
[1]->ptr
,"populate") && c
->argc
== 3) {
10518 if (getLongFromObjectOrReply(c
, c
->argv
[2], &keys
, NULL
) != REDIS_OK
)
10520 for (j
= 0; j
< keys
; j
++) {
10521 snprintf(buf
,sizeof(buf
),"key:%lu",j
);
10522 key
= createStringObject(buf
,strlen(buf
));
10523 if (lookupKeyRead(c
->db
,key
) != NULL
) {
10527 snprintf(buf
,sizeof(buf
),"value:%lu",j
);
10528 val
= createStringObject(buf
,strlen(buf
));
10529 dictAdd(c
->db
->dict
,key
,val
);
10531 addReply(c
,shared
.ok
);
10532 } else if (!strcasecmp(c
->argv
[1]->ptr
,"digest") && c
->argc
== 2) {
10533 unsigned char digest
[20];
10534 sds d
= sdsnew("+");
10537 computeDatasetDigest(digest
);
10538 for (j
= 0; j
< 20; j
++)
10539 d
= sdscatprintf(d
, "%02x",digest
[j
]);
10541 d
= sdscatlen(d
,"\r\n",2);
10544 addReplySds(c
,sdsnew(
10545 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]\r\n"));
10549 static void _redisAssert(char *estr
, char *file
, int line
) {
10550 redisLog(REDIS_WARNING
,"=== ASSERTION FAILED ===");
10551 redisLog(REDIS_WARNING
,"==> %s:%d '%s' is not true\n",file
,line
,estr
);
10552 #ifdef HAVE_BACKTRACE
10553 redisLog(REDIS_WARNING
,"(forcing SIGSEGV in order to print the stack trace)");
10554 *((char*)-1) = 'x';
10558 static void _redisPanic(char *msg
, char *file
, int line
) {
10559 redisLog(REDIS_WARNING
,"!!! Software Failure. Press left mouse button to continue");
10560 redisLog(REDIS_WARNING
,"Guru Meditation: %s #%s:%d",msg
,file
,line
);
10561 #ifdef HAVE_BACKTRACE
10562 redisLog(REDIS_WARNING
,"(forcing SIGSEGV in order to print the stack trace)");
10563 *((char*)-1) = 'x';
10567 /* =================================== Main! ================================ */
10570 int linuxOvercommitMemoryValue(void) {
10571 FILE *fp
= fopen("/proc/sys/vm/overcommit_memory","r");
10574 if (!fp
) return -1;
10575 if (fgets(buf
,64,fp
) == NULL
) {
10584 void linuxOvercommitMemoryWarning(void) {
10585 if (linuxOvercommitMemoryValue() == 0) {
10586 redisLog(REDIS_WARNING
,"WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.");
10589 #endif /* __linux__ */
10591 static void daemonize(void) {
10595 if (fork() != 0) exit(0); /* parent exits */
10596 setsid(); /* create a new session */
10598 /* Every output goes to /dev/null. If Redis is daemonized but
10599 * the 'logfile' is set to 'stdout' in the configuration file
10600 * it will not log at all. */
10601 if ((fd
= open("/dev/null", O_RDWR
, 0)) != -1) {
10602 dup2(fd
, STDIN_FILENO
);
10603 dup2(fd
, STDOUT_FILENO
);
10604 dup2(fd
, STDERR_FILENO
);
10605 if (fd
> STDERR_FILENO
) close(fd
);
10607 /* Try to write the pid file */
10608 fp
= fopen(server
.pidfile
,"w");
10610 fprintf(fp
,"%d\n",getpid());
10615 static void version() {
10616 printf("Redis server version %s\n", REDIS_VERSION
);
10620 static void usage() {
10621 fprintf(stderr
,"Usage: ./redis-server [/path/to/redis.conf]\n");
10622 fprintf(stderr
," ./redis-server - (read config from stdin)\n");
10626 int main(int argc
, char **argv
) {
10629 initServerConfig();
10631 if (strcmp(argv
[1], "-v") == 0 ||
10632 strcmp(argv
[1], "--version") == 0) version();
10633 if (strcmp(argv
[1], "--help") == 0) usage();
10634 resetServerSaveParams();
10635 loadServerConfig(argv
[1]);
10636 } else if ((argc
> 2)) {
10639 redisLog(REDIS_WARNING
,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
10641 if (server
.daemonize
) daemonize();
10643 redisLog(REDIS_NOTICE
,"Server started, Redis version " REDIS_VERSION
);
10645 linuxOvercommitMemoryWarning();
10647 start
= time(NULL
);
10648 if (server
.appendonly
) {
10649 if (loadAppendOnlyFile(server
.appendfilename
) == REDIS_OK
)
10650 redisLog(REDIS_NOTICE
,"DB loaded from append only file: %ld seconds",time(NULL
)-start
);
10652 if (rdbLoad(server
.dbfilename
) == REDIS_OK
)
10653 redisLog(REDIS_NOTICE
,"DB loaded from disk: %ld seconds",time(NULL
)-start
);
10655 redisLog(REDIS_NOTICE
,"The server is now ready to accept connections on port %d", server
.port
);
10656 aeSetBeforeSleepProc(server
.el
,beforeSleep
);
10658 aeDeleteEventLoop(server
.el
);
10662 /* ============================= Backtrace support ========================= */
10664 #ifdef HAVE_BACKTRACE
10665 static char *findFuncName(void *pointer
, unsigned long *offset
);
10667 static void *getMcontextEip(ucontext_t
*uc
) {
10668 #if defined(__FreeBSD__)
10669 return (void*) uc
->uc_mcontext
.mc_eip
;
10670 #elif defined(__dietlibc__)
10671 return (void*) uc
->uc_mcontext
.eip
;
10672 #elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
10674 return (void*) uc
->uc_mcontext
->__ss
.__rip
;
10676 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
10678 #elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
10679 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
10680 return (void*) uc
->uc_mcontext
->__ss
.__rip
;
10682 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
10684 #elif defined(__i386__) || defined(__X86_64__) || defined(__x86_64__)
10685 return (void*) uc
->uc_mcontext
.gregs
[REG_EIP
]; /* Linux 32/64 bit */
10686 #elif defined(__ia64__) /* Linux IA64 */
10687 return (void*) uc
->uc_mcontext
.sc_ip
;
10693 static void segvHandler(int sig
, siginfo_t
*info
, void *secret
) {
10695 char **messages
= NULL
;
10696 int i
, trace_size
= 0;
10697 unsigned long offset
=0;
10698 ucontext_t
*uc
= (ucontext_t
*) secret
;
10700 REDIS_NOTUSED(info
);
10702 redisLog(REDIS_WARNING
,
10703 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION
, sig
);
10704 infostring
= genRedisInfoString();
10705 redisLog(REDIS_WARNING
, "%s",infostring
);
10706 /* It's not safe to sdsfree() the returned string under memory
10707 * corruption conditions. Let it leak as we are going to abort */
10709 trace_size
= backtrace(trace
, 100);
10710 /* overwrite sigaction with caller's address */
10711 if (getMcontextEip(uc
) != NULL
) {
10712 trace
[1] = getMcontextEip(uc
);
10714 messages
= backtrace_symbols(trace
, trace_size
);
10716 for (i
=1; i
<trace_size
; ++i
) {
10717 char *fn
= findFuncName(trace
[i
], &offset
), *p
;
10719 p
= strchr(messages
[i
],'+');
10720 if (!fn
|| (p
&& ((unsigned long)strtol(p
+1,NULL
,10)) < offset
)) {
10721 redisLog(REDIS_WARNING
,"%s", messages
[i
]);
10723 redisLog(REDIS_WARNING
,"%d redis-server %p %s + %d", i
, trace
[i
], fn
, (unsigned int)offset
);
10726 /* free(messages); Don't call free() with possibly corrupted memory. */
10730 static void setupSigSegvAction(void) {
10731 struct sigaction act
;
10733 sigemptyset (&act
.sa_mask
);
10734 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
10735 * is used. Otherwise, sa_handler is used */
10736 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
| SA_SIGINFO
;
10737 act
.sa_sigaction
= segvHandler
;
10738 sigaction (SIGSEGV
, &act
, NULL
);
10739 sigaction (SIGBUS
, &act
, NULL
);
10740 sigaction (SIGFPE
, &act
, NULL
);
10741 sigaction (SIGILL
, &act
, NULL
);
10742 sigaction (SIGBUS
, &act
, NULL
);
10746 #include "staticsymbols.h"
10747 /* This function try to convert a pointer into a function name. It's used in
10748 * oreder to provide a backtrace under segmentation fault that's able to
10749 * display functions declared as static (otherwise the backtrace is useless). */
10750 static char *findFuncName(void *pointer
, unsigned long *offset
){
10752 unsigned long off
, minoff
= 0;
10754 /* Try to match against the Symbol with the smallest offset */
10755 for (i
=0; symsTable
[i
].pointer
; i
++) {
10756 unsigned long lp
= (unsigned long) pointer
;
10758 if (lp
!= (unsigned long)-1 && lp
>= symsTable
[i
].pointer
) {
10759 off
=lp
-symsTable
[i
].pointer
;
10760 if (ret
< 0 || off
< minoff
) {
10766 if (ret
== -1) return NULL
;
10768 return symsTable
[ret
].name
;
10770 #else /* HAVE_BACKTRACE */
10771 static void setupSigSegvAction(void) {
10773 #endif /* HAVE_BACKTRACE */