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 "2.1.1"
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 "ziplist.h" /* Compact list data structure */
79 #include "sha1.h" /* SHA1 is used for DEBUG DIGEST */
85 /* Static server configuration */
86 #define REDIS_SERVERPORT 6379 /* TCP port */
87 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
88 #define REDIS_IOBUF_LEN 1024
89 #define REDIS_LOADBUF_LEN 1024
90 #define REDIS_STATIC_ARGS 8
91 #define REDIS_DEFAULT_DBNUM 16
92 #define REDIS_CONFIGLINE_MAX 1024
93 #define REDIS_OBJFREELIST_MAX 1000000 /* Max number of objects to cache */
94 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
95 #define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */
96 #define REDIS_MAX_WRITE_PER_EVENT (1024*64)
97 #define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
99 /* If more then REDIS_WRITEV_THRESHOLD write packets are pending use writev */
100 #define REDIS_WRITEV_THRESHOLD 3
101 /* Max number of iovecs used for each writev call */
102 #define REDIS_WRITEV_IOVEC_COUNT 256
104 /* Hash table parameters */
105 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
108 #define REDIS_CMD_BULK 1 /* Bulk write command */
109 #define REDIS_CMD_INLINE 2 /* Inline command */
110 /* REDIS_CMD_DENYOOM reserves a longer comment: all the commands marked with
111 this flags will return an error when the 'maxmemory' option is set in the
112 config file and the server is using more than maxmemory bytes of memory.
113 In short this commands are denied on low memory conditions. */
114 #define REDIS_CMD_DENYOOM 4
115 #define REDIS_CMD_FORCE_REPLICATION 8 /* Force replication even if dirty is 0 */
118 #define REDIS_STRING 0
123 #define REDIS_VMPOINTER 8
125 /* Objects encoding. Some kind of objects like Strings and Hashes can be
126 * internally represented in multiple ways. The 'encoding' field of the object
127 * is set to one of this fields for this object. */
128 #define REDIS_ENCODING_RAW 0 /* Raw representation */
129 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
130 #define REDIS_ENCODING_HT 2 /* Encoded as hash table */
131 #define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
132 #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */
133 #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
135 static char* strencoding
[] = {
136 "raw", "int", "hashtable", "zipmap", "linkedlist", "ziplist"
139 /* Object types only used for dumping to disk */
140 #define REDIS_EXPIRETIME 253
141 #define REDIS_SELECTDB 254
142 #define REDIS_EOF 255
144 /* Defines related to the dump file format. To store 32 bits lengths for short
145 * keys requires a lot of space, so we check the most significant 2 bits of
146 * the first byte to interpreter the length:
148 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
149 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
150 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
151 * 11|000000 this means: specially encoded object will follow. The six bits
152 * number specify the kind of object that follows.
153 * See the REDIS_RDB_ENC_* defines.
155 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
156 * values, will fit inside. */
157 #define REDIS_RDB_6BITLEN 0
158 #define REDIS_RDB_14BITLEN 1
159 #define REDIS_RDB_32BITLEN 2
160 #define REDIS_RDB_ENCVAL 3
161 #define REDIS_RDB_LENERR UINT_MAX
163 /* When a length of a string object stored on disk has the first two bits
164 * set, the remaining two bits specify a special encoding for the object
165 * accordingly to the following defines: */
166 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
167 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
168 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
169 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
171 /* Virtual memory object->where field. */
172 #define REDIS_VM_MEMORY 0 /* The object is on memory */
173 #define REDIS_VM_SWAPPED 1 /* The object is on disk */
174 #define REDIS_VM_SWAPPING 2 /* Redis is swapping this object on disk */
175 #define REDIS_VM_LOADING 3 /* Redis is loading this object from disk */
177 /* Virtual memory static configuration stuff.
178 * Check vmFindContiguousPages() to know more about this magic numbers. */
179 #define REDIS_VM_MAX_NEAR_PAGES 65536
180 #define REDIS_VM_MAX_RANDOM_JUMP 4096
181 #define REDIS_VM_MAX_THREADS 32
182 #define REDIS_THREAD_STACK_SIZE (1024*1024*4)
183 /* The following is the *percentage* of completed I/O jobs to process when the
184 * handelr is called. While Virtual Memory I/O operations are performed by
185 * threads, this operations must be processed by the main thread when completed
186 * in order to take effect. */
187 #define REDIS_MAX_COMPLETED_JOBS_PROCESSED 1
190 #define REDIS_SLAVE 1 /* This client is a slave server */
191 #define REDIS_MASTER 2 /* This client is a master server */
192 #define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */
193 #define REDIS_MULTI 8 /* This client is in a MULTI context */
194 #define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */
195 #define REDIS_IO_WAIT 32 /* The client is waiting for Virtual Memory I/O */
196 #define REDIS_DIRTY_CAS 64 /* Watched keys modified. EXEC will fail. */
198 /* Slave replication state - slave side */
199 #define REDIS_REPL_NONE 0 /* No active replication */
200 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
201 #define REDIS_REPL_CONNECTED 2 /* Connected to master */
203 /* Slave replication state - from the point of view of master
204 * Note that in SEND_BULK and ONLINE state the slave receives new updates
205 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
206 * to start the next background saving in order to send updates to it. */
207 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
208 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
209 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
210 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
212 /* List related stuff */
216 /* Sort operations */
217 #define REDIS_SORT_GET 0
218 #define REDIS_SORT_ASC 1
219 #define REDIS_SORT_DESC 2
220 #define REDIS_SORTKEY_MAX 1024
223 #define REDIS_DEBUG 0
224 #define REDIS_VERBOSE 1
225 #define REDIS_NOTICE 2
226 #define REDIS_WARNING 3
228 /* Anti-warning macro... */
229 #define REDIS_NOTUSED(V) ((void) V)
231 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
232 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
234 /* Append only defines */
235 #define APPENDFSYNC_NO 0
236 #define APPENDFSYNC_ALWAYS 1
237 #define APPENDFSYNC_EVERYSEC 2
239 /* Zip structure related defaults */
240 #define REDIS_HASH_MAX_ZIPMAP_ENTRIES 64
241 #define REDIS_HASH_MAX_ZIPMAP_VALUE 512
242 #define REDIS_LIST_MAX_ZIPLIST_ENTRIES 1024
243 #define REDIS_LIST_MAX_ZIPLIST_VALUE 32
245 /* We can print the stacktrace, so our assert is defined this way: */
246 #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
247 #define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
248 static void _redisAssert(char *estr
, char *file
, int line
);
249 static void _redisPanic(char *msg
, char *file
, int line
);
251 /*================================= Data types ============================== */
253 /* A redis object, that is a type able to hold a string / list / set */
255 /* The actual Redis Object */
256 typedef struct redisObject
{
258 unsigned storage
:2; /* REDIS_VM_MEMORY or REDIS_VM_SWAPPING */
260 unsigned lru
:22; /* lru time (relative to server.lruclock) */
263 /* VM fields are only allocated if VM is active, otherwise the
264 * object allocation function will just allocate
265 * sizeof(redisObjct) minus sizeof(redisObjectVM), so using
266 * Redis without VM active will not have any overhead. */
269 /* The VM pointer structure - identifies an object in the swap file.
271 * This object is stored in place of the value
272 * object in the main key->value hash table representing a database.
273 * Note that the first fields (type, storage) are the same as the redisObject
274 * structure so that vmPointer strucuters can be accessed even when casted
275 * as redisObject structures.
277 * This is useful as we don't know if a value object is or not on disk, but we
278 * are always able to read obj->storage to check this. For vmPointer
279 * structures "type" is set to REDIS_VMPOINTER (even if without this field
280 * is still possible to check the kind of object from the value of 'storage').*/
281 typedef struct vmPointer
{
283 unsigned storage
:2; /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
285 unsigned int vtype
; /* type of the object stored in the swap file */
286 off_t page
; /* the page at witch the object is stored on disk */
287 off_t usedpages
; /* number of pages used on disk */
290 /* Macro used to initalize a Redis object allocated on the stack.
291 * Note that this macro is taken near the structure definition to make sure
292 * we'll update it when the structure is changed, to avoid bugs like
293 * bug #85 introduced exactly in this way. */
294 #define initStaticStringObject(_var,_ptr) do { \
296 _var.type = REDIS_STRING; \
297 _var.encoding = REDIS_ENCODING_RAW; \
299 _var.storage = REDIS_VM_MEMORY; \
302 typedef struct redisDb
{
303 dict
*dict
; /* The keyspace for this DB */
304 dict
*expires
; /* Timeout of keys with a timeout set */
305 dict
*blocking_keys
; /* Keys with clients waiting for data (BLPOP) */
306 dict
*io_keys
; /* Keys with clients waiting for VM I/O */
307 dict
*watched_keys
; /* WATCHED keys for MULTI/EXEC CAS */
311 /* Client MULTI/EXEC state */
312 typedef struct multiCmd
{
315 struct redisCommand
*cmd
;
318 typedef struct multiState
{
319 multiCmd
*commands
; /* Array of MULTI commands */
320 int count
; /* Total number of MULTI commands */
323 /* With multiplexing we need to take per-clinet state.
324 * Clients are taken in a liked list. */
325 typedef struct redisClient
{
330 robj
**argv
, **mbargv
;
332 int bulklen
; /* bulk read len. -1 if not in bulk read mode */
333 int multibulk
; /* multi bulk command format active */
336 time_t lastinteraction
; /* time of the last interaction, used for timeout */
337 int flags
; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */
338 int slaveseldb
; /* slave selected db, if this client is a slave */
339 int authenticated
; /* when requirepass is non-NULL */
340 int replstate
; /* replication state if this is a slave */
341 int repldbfd
; /* replication DB file descriptor */
342 long repldboff
; /* replication DB file offset */
343 off_t repldbsize
; /* replication DB file size */
344 multiState mstate
; /* MULTI/EXEC state */
345 robj
**blocking_keys
; /* The key we are waiting to terminate a blocking
346 * operation such as BLPOP. Otherwise NULL. */
347 int blocking_keys_num
; /* Number of blocking keys */
348 time_t blockingto
; /* Blocking operation timeout. If UNIX current time
349 * is >= blockingto then the operation timed out. */
350 list
*io_keys
; /* Keys this client is waiting to be loaded from the
351 * swap file in order to continue. */
352 list
*watched_keys
; /* Keys WATCHED for MULTI/EXEC CAS */
353 dict
*pubsub_channels
; /* channels a client is interested in (SUBSCRIBE) */
354 list
*pubsub_patterns
; /* patterns a client is interested in (SUBSCRIBE) */
362 /* Global server state structure */
367 long long dirty
; /* changes to DB from the last save */
369 list
*slaves
, *monitors
;
370 char neterr
[ANET_ERR_LEN
];
372 int cronloops
; /* number of times the cron function run */
373 list
*objfreelist
; /* A list of freed objects to avoid malloc() */
374 time_t lastsave
; /* Unix time of last save succeeede */
375 /* Fields used only for stats */
376 time_t stat_starttime
; /* server start time */
377 long long stat_numcommands
; /* number of processed commands */
378 long long stat_numconnections
; /* number of connections received */
379 long long stat_expiredkeys
; /* number of expired keys */
388 int no_appendfsync_on_rewrite
;
394 pid_t bgsavechildpid
;
395 pid_t bgrewritechildpid
;
396 sds bgrewritebuf
; /* buffer taken by parent during oppend only rewrite */
397 sds aofbuf
; /* AOF buffer, written before entering the event loop */
398 struct saveparam
*saveparams
;
403 char *appendfilename
;
407 /* Replication related */
412 redisClient
*master
; /* client that is master for this slave */
414 unsigned int maxclients
;
415 unsigned long long maxmemory
;
416 unsigned int blpop_blocked_clients
;
417 unsigned int vm_blocked_clients
;
418 /* Sort parameters - qsort_r() is only available under BSD so we
419 * have to take this state global, in order to pass it to sortCompare() */
423 /* Virtual memory configuration */
428 unsigned long long vm_max_memory
;
429 /* Zip structure config */
430 size_t hash_max_zipmap_entries
;
431 size_t hash_max_zipmap_value
;
432 size_t list_max_ziplist_entries
;
433 size_t list_max_ziplist_value
;
434 /* Virtual memory state */
437 off_t vm_next_page
; /* Next probably empty page */
438 off_t vm_near_pages
; /* Number of pages allocated sequentially */
439 unsigned char *vm_bitmap
; /* Bitmap of free/used pages */
440 time_t unixtime
; /* Unix time sampled every second. */
441 /* Virtual memory I/O threads stuff */
442 /* An I/O thread process an element taken from the io_jobs queue and
443 * put the result of the operation in the io_done list. While the
444 * job is being processed, it's put on io_processing queue. */
445 list
*io_newjobs
; /* List of VM I/O jobs yet to be processed */
446 list
*io_processing
; /* List of VM I/O jobs being processed */
447 list
*io_processed
; /* List of VM I/O jobs already processed */
448 list
*io_ready_clients
; /* Clients ready to be unblocked. All keys loaded */
449 pthread_mutex_t io_mutex
; /* lock to access io_jobs/io_done/io_thread_job */
450 pthread_mutex_t obj_freelist_mutex
; /* safe redis objects creation/free */
451 pthread_mutex_t io_swapfile_mutex
; /* So we can lseek + write */
452 pthread_attr_t io_threads_attr
; /* attributes for threads creation */
453 int io_active_threads
; /* Number of running I/O threads */
454 int vm_max_threads
; /* Max number of I/O threads running at the same time */
455 /* Our main thread is blocked on the event loop, locking for sockets ready
456 * to be read or written, so when a threaded I/O operation is ready to be
457 * processed by the main thread, the I/O thread will use a unix pipe to
458 * awake the main thread. The followings are the two pipe FDs. */
459 int io_ready_pipe_read
;
460 int io_ready_pipe_write
;
461 /* Virtual memory stats */
462 unsigned long long vm_stats_used_pages
;
463 unsigned long long vm_stats_swapped_objects
;
464 unsigned long long vm_stats_swapouts
;
465 unsigned long long vm_stats_swapins
;
467 dict
*pubsub_channels
; /* Map channels to list of subscribed clients */
468 list
*pubsub_patterns
; /* A list of pubsub_patterns */
471 unsigned lruclock
:22; /* clock incrementing every minute, for LRU */
472 unsigned lruclock_padding
:10;
475 typedef struct pubsubPattern
{
480 typedef void redisCommandProc(redisClient
*c
);
481 typedef void redisVmPreloadProc(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
);
482 struct redisCommand
{
484 redisCommandProc
*proc
;
487 /* Use a function to determine which keys need to be loaded
488 * in the background prior to executing this command. Takes precedence
489 * over vm_firstkey and others, ignored when NULL */
490 redisVmPreloadProc
*vm_preload_proc
;
491 /* What keys should be loaded in background when calling this command? */
492 int vm_firstkey
; /* The first argument that's a key (0 = no keys) */
493 int vm_lastkey
; /* THe last argument that's a key */
494 int vm_keystep
; /* The step between first and last key */
497 struct redisFunctionSym
{
499 unsigned long pointer
;
502 typedef struct _redisSortObject
{
510 typedef struct _redisSortOperation
{
513 } redisSortOperation
;
515 /* ZSETs use a specialized version of Skiplists */
517 typedef struct zskiplistNode
{
518 struct zskiplistNode
**forward
;
519 struct zskiplistNode
*backward
;
525 typedef struct zskiplist
{
526 struct zskiplistNode
*header
, *tail
;
527 unsigned long length
;
531 typedef struct zset
{
536 /* Our shared "common" objects */
538 #define REDIS_SHARED_INTEGERS 10000
539 struct sharedObjectsStruct
{
540 robj
*crlf
, *ok
, *err
, *emptybulk
, *czero
, *cone
, *cnegone
, *pong
, *space
,
541 *colon
, *nullbulk
, *nullmultibulk
, *queued
,
542 *emptymultibulk
, *wrongtypeerr
, *nokeyerr
, *syntaxerr
, *sameobjecterr
,
543 *outofrangeerr
, *plus
,
544 *select0
, *select1
, *select2
, *select3
, *select4
,
545 *select5
, *select6
, *select7
, *select8
, *select9
,
546 *messagebulk
, *pmessagebulk
, *subscribebulk
, *unsubscribebulk
, *mbulk3
,
547 *mbulk4
, *psubscribebulk
, *punsubscribebulk
,
548 *integers
[REDIS_SHARED_INTEGERS
];
551 /* Global vars that are actally used as constants. The following double
552 * values are used for double on-disk serialization, and are initialized
553 * at runtime to avoid strange compiler optimizations. */
555 static double R_Zero
, R_PosInf
, R_NegInf
, R_Nan
;
557 /* VM threaded I/O request message */
558 #define REDIS_IOJOB_LOAD 0 /* Load from disk to memory */
559 #define REDIS_IOJOB_PREPARE_SWAP 1 /* Compute needed pages */
560 #define REDIS_IOJOB_DO_SWAP 2 /* Swap from memory to disk */
561 typedef struct iojob
{
562 int type
; /* Request type, REDIS_IOJOB_* */
563 redisDb
*db
;/* Redis database */
564 robj
*key
; /* This I/O request is about swapping this key */
565 robj
*id
; /* Unique identifier of this job:
566 this is the object to swap for REDIS_IOREQ_*_SWAP, or the
567 vmpointer objct for REDIS_IOREQ_LOAD. */
568 robj
*val
; /* the value to swap for REDIS_IOREQ_*_SWAP, otherwise this
569 * field is populated by the I/O thread for REDIS_IOREQ_LOAD. */
570 off_t page
; /* Swap page where to read/write the object */
571 off_t pages
; /* Swap pages needed to save object. PREPARE_SWAP return val */
572 int canceled
; /* True if this command was canceled by blocking side of VM */
573 pthread_t thread
; /* ID of the thread processing this entry */
576 /*================================ Prototypes =============================== */
577 char *redisGitSHA1(void);
578 char *redisGitDirty(void);
580 static void freeStringObject(robj
*o
);
581 static void freeListObject(robj
*o
);
582 static void freeSetObject(robj
*o
);
583 static void decrRefCount(void *o
);
584 static robj
*createObject(int type
, void *ptr
);
585 static void freeClient(redisClient
*c
);
586 static int rdbLoad(char *filename
);
587 static void addReply(redisClient
*c
, robj
*obj
);
588 static void addReplySds(redisClient
*c
, sds s
);
589 static void incrRefCount(robj
*o
);
590 static int rdbSaveBackground(char *filename
);
591 static robj
*createStringObject(char *ptr
, size_t len
);
592 static robj
*dupStringObject(robj
*o
);
593 static void replicationFeedSlaves(list
*slaves
, int dictid
, robj
**argv
, int argc
);
594 static void replicationFeedMonitors(list
*monitors
, int dictid
, robj
**argv
, int argc
);
595 static void flushAppendOnlyFile(void);
596 static void feedAppendOnlyFile(struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
);
597 static int syncWithMaster(void);
598 static robj
*tryObjectEncoding(robj
*o
);
599 static robj
*getDecodedObject(robj
*o
);
600 static int removeExpire(redisDb
*db
, robj
*key
);
601 static int expireIfNeeded(redisDb
*db
, robj
*key
);
602 static int deleteIfVolatile(redisDb
*db
, robj
*key
);
603 static int dbDelete(redisDb
*db
, robj
*key
);
604 static time_t getExpire(redisDb
*db
, robj
*key
);
605 static int setExpire(redisDb
*db
, robj
*key
, time_t when
);
606 static void updateSlavesWaitingBgsave(int bgsaveerr
);
607 static void freeMemoryIfNeeded(void);
608 static int processCommand(redisClient
*c
);
609 static void setupSigSegvAction(void);
610 static void rdbRemoveTempFile(pid_t childpid
);
611 static void aofRemoveTempFile(pid_t childpid
);
612 static size_t stringObjectLen(robj
*o
);
613 static void processInputBuffer(redisClient
*c
);
614 static zskiplist
*zslCreate(void);
615 static void zslFree(zskiplist
*zsl
);
616 static void zslInsert(zskiplist
*zsl
, double score
, robj
*obj
);
617 static void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
618 static void initClientMultiState(redisClient
*c
);
619 static void freeClientMultiState(redisClient
*c
);
620 static void queueMultiCommand(redisClient
*c
, struct redisCommand
*cmd
);
621 static void unblockClientWaitingData(redisClient
*c
);
622 static int handleClientsWaitingListPush(redisClient
*c
, robj
*key
, robj
*ele
);
623 static void vmInit(void);
624 static void vmMarkPagesFree(off_t page
, off_t count
);
625 static robj
*vmLoadObject(robj
*o
);
626 static robj
*vmPreviewObject(robj
*o
);
627 static int vmSwapOneObjectBlocking(void);
628 static int vmSwapOneObjectThreaded(void);
629 static int vmCanSwapOut(void);
630 static int tryFreeOneObjectFromFreelist(void);
631 static void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
632 static void vmThreadedIOCompletedJob(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
633 static void vmCancelThreadedIOJob(robj
*o
);
634 static void lockThreadedIO(void);
635 static void unlockThreadedIO(void);
636 static int vmSwapObjectThreaded(robj
*key
, robj
*val
, redisDb
*db
);
637 static void freeIOJob(iojob
*j
);
638 static void queueIOJob(iojob
*j
);
639 static int vmWriteObjectOnSwap(robj
*o
, off_t page
);
640 static robj
*vmReadObjectFromSwap(off_t page
, int type
);
641 static void waitEmptyIOJobsQueue(void);
642 static void vmReopenSwapFile(void);
643 static int vmFreePage(off_t page
);
644 static void zunionInterBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
);
645 static void execBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
);
646 static int blockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
);
647 static int dontWaitForSwappedKey(redisClient
*c
, robj
*key
);
648 static void handleClientsBlockedOnSwappedKey(redisDb
*db
, robj
*key
);
649 static void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
650 static struct redisCommand
*lookupCommand(char *name
);
651 static void call(redisClient
*c
, struct redisCommand
*cmd
);
652 static void resetClient(redisClient
*c
);
653 static void convertToRealHash(robj
*o
);
654 static void listTypeConvert(robj
*o
, int enc
);
655 static int pubsubUnsubscribeAllChannels(redisClient
*c
, int notify
);
656 static int pubsubUnsubscribeAllPatterns(redisClient
*c
, int notify
);
657 static void freePubsubPattern(void *p
);
658 static int listMatchPubsubPattern(void *a
, void *b
);
659 static int compareStringObjects(robj
*a
, robj
*b
);
660 static int equalStringObjects(robj
*a
, robj
*b
);
662 static int rewriteAppendOnlyFileBackground(void);
663 static vmpointer
*vmSwapObjectBlocking(robj
*val
);
664 static int prepareForShutdown();
665 static void touchWatchedKey(redisDb
*db
, robj
*key
);
666 static void touchWatchedKeysOnFlush(int dbid
);
667 static void unwatchAllKeys(redisClient
*c
);
669 static void authCommand(redisClient
*c
);
670 static void pingCommand(redisClient
*c
);
671 static void echoCommand(redisClient
*c
);
672 static void setCommand(redisClient
*c
);
673 static void setnxCommand(redisClient
*c
);
674 static void setexCommand(redisClient
*c
);
675 static void getCommand(redisClient
*c
);
676 static void delCommand(redisClient
*c
);
677 static void existsCommand(redisClient
*c
);
678 static void incrCommand(redisClient
*c
);
679 static void decrCommand(redisClient
*c
);
680 static void incrbyCommand(redisClient
*c
);
681 static void decrbyCommand(redisClient
*c
);
682 static void selectCommand(redisClient
*c
);
683 static void randomkeyCommand(redisClient
*c
);
684 static void keysCommand(redisClient
*c
);
685 static void dbsizeCommand(redisClient
*c
);
686 static void lastsaveCommand(redisClient
*c
);
687 static void saveCommand(redisClient
*c
);
688 static void bgsaveCommand(redisClient
*c
);
689 static void bgrewriteaofCommand(redisClient
*c
);
690 static void shutdownCommand(redisClient
*c
);
691 static void moveCommand(redisClient
*c
);
692 static void renameCommand(redisClient
*c
);
693 static void renamenxCommand(redisClient
*c
);
694 static void lpushCommand(redisClient
*c
);
695 static void rpushCommand(redisClient
*c
);
696 static void lpushxCommand(redisClient
*c
);
697 static void rpushxCommand(redisClient
*c
);
698 static void linsertCommand(redisClient
*c
);
699 static void lpopCommand(redisClient
*c
);
700 static void rpopCommand(redisClient
*c
);
701 static void llenCommand(redisClient
*c
);
702 static void lindexCommand(redisClient
*c
);
703 static void lrangeCommand(redisClient
*c
);
704 static void ltrimCommand(redisClient
*c
);
705 static void typeCommand(redisClient
*c
);
706 static void lsetCommand(redisClient
*c
);
707 static void saddCommand(redisClient
*c
);
708 static void sremCommand(redisClient
*c
);
709 static void smoveCommand(redisClient
*c
);
710 static void sismemberCommand(redisClient
*c
);
711 static void scardCommand(redisClient
*c
);
712 static void spopCommand(redisClient
*c
);
713 static void srandmemberCommand(redisClient
*c
);
714 static void sinterCommand(redisClient
*c
);
715 static void sinterstoreCommand(redisClient
*c
);
716 static void sunionCommand(redisClient
*c
);
717 static void sunionstoreCommand(redisClient
*c
);
718 static void sdiffCommand(redisClient
*c
);
719 static void sdiffstoreCommand(redisClient
*c
);
720 static void syncCommand(redisClient
*c
);
721 static void flushdbCommand(redisClient
*c
);
722 static void flushallCommand(redisClient
*c
);
723 static void sortCommand(redisClient
*c
);
724 static void lremCommand(redisClient
*c
);
725 static void rpoplpushcommand(redisClient
*c
);
726 static void infoCommand(redisClient
*c
);
727 static void mgetCommand(redisClient
*c
);
728 static void monitorCommand(redisClient
*c
);
729 static void expireCommand(redisClient
*c
);
730 static void expireatCommand(redisClient
*c
);
731 static void getsetCommand(redisClient
*c
);
732 static void ttlCommand(redisClient
*c
);
733 static void slaveofCommand(redisClient
*c
);
734 static void debugCommand(redisClient
*c
);
735 static void msetCommand(redisClient
*c
);
736 static void msetnxCommand(redisClient
*c
);
737 static void zaddCommand(redisClient
*c
);
738 static void zincrbyCommand(redisClient
*c
);
739 static void zrangeCommand(redisClient
*c
);
740 static void zrangebyscoreCommand(redisClient
*c
);
741 static void zcountCommand(redisClient
*c
);
742 static void zrevrangeCommand(redisClient
*c
);
743 static void zcardCommand(redisClient
*c
);
744 static void zremCommand(redisClient
*c
);
745 static void zscoreCommand(redisClient
*c
);
746 static void zremrangebyscoreCommand(redisClient
*c
);
747 static void multiCommand(redisClient
*c
);
748 static void execCommand(redisClient
*c
);
749 static void discardCommand(redisClient
*c
);
750 static void blpopCommand(redisClient
*c
);
751 static void brpopCommand(redisClient
*c
);
752 static void appendCommand(redisClient
*c
);
753 static void substrCommand(redisClient
*c
);
754 static void zrankCommand(redisClient
*c
);
755 static void zrevrankCommand(redisClient
*c
);
756 static void hsetCommand(redisClient
*c
);
757 static void hsetnxCommand(redisClient
*c
);
758 static void hgetCommand(redisClient
*c
);
759 static void hmsetCommand(redisClient
*c
);
760 static void hmgetCommand(redisClient
*c
);
761 static void hdelCommand(redisClient
*c
);
762 static void hlenCommand(redisClient
*c
);
763 static void zremrangebyrankCommand(redisClient
*c
);
764 static void zunionstoreCommand(redisClient
*c
);
765 static void zinterstoreCommand(redisClient
*c
);
766 static void hkeysCommand(redisClient
*c
);
767 static void hvalsCommand(redisClient
*c
);
768 static void hgetallCommand(redisClient
*c
);
769 static void hexistsCommand(redisClient
*c
);
770 static void configCommand(redisClient
*c
);
771 static void hincrbyCommand(redisClient
*c
);
772 static void subscribeCommand(redisClient
*c
);
773 static void unsubscribeCommand(redisClient
*c
);
774 static void psubscribeCommand(redisClient
*c
);
775 static void punsubscribeCommand(redisClient
*c
);
776 static void publishCommand(redisClient
*c
);
777 static void watchCommand(redisClient
*c
);
778 static void unwatchCommand(redisClient
*c
);
780 /*================================= Globals ================================= */
783 static struct redisServer server
; /* server global state */
784 static struct redisCommand
*commandTable
;
785 static struct redisCommand readonlyCommandTable
[] = {
786 {"get",getCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
787 {"set",setCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,0,0,0},
788 {"setnx",setnxCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,0,0,0},
789 {"setex",setexCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,0,0,0},
790 {"append",appendCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
791 {"substr",substrCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
792 {"del",delCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
793 {"exists",existsCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
794 {"incr",incrCommand
,2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
795 {"decr",decrCommand
,2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
796 {"mget",mgetCommand
,-2,REDIS_CMD_INLINE
,NULL
,1,-1,1},
797 {"rpush",rpushCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
798 {"lpush",lpushCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
799 {"rpushx",rpushxCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
800 {"lpushx",lpushxCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
801 {"linsert",linsertCommand
,5,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
802 {"rpop",rpopCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
803 {"lpop",lpopCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
804 {"brpop",brpopCommand
,-3,REDIS_CMD_INLINE
,NULL
,1,1,1},
805 {"blpop",blpopCommand
,-3,REDIS_CMD_INLINE
,NULL
,1,1,1},
806 {"llen",llenCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
807 {"lindex",lindexCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
808 {"lset",lsetCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
809 {"lrange",lrangeCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
810 {"ltrim",ltrimCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
811 {"lrem",lremCommand
,4,REDIS_CMD_BULK
,NULL
,1,1,1},
812 {"rpoplpush",rpoplpushcommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,2,1},
813 {"sadd",saddCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
814 {"srem",sremCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
815 {"smove",smoveCommand
,4,REDIS_CMD_BULK
,NULL
,1,2,1},
816 {"sismember",sismemberCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
817 {"scard",scardCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
818 {"spop",spopCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
819 {"srandmember",srandmemberCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
820 {"sinter",sinterCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,-1,1},
821 {"sinterstore",sinterstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,2,-1,1},
822 {"sunion",sunionCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,-1,1},
823 {"sunionstore",sunionstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,2,-1,1},
824 {"sdiff",sdiffCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,-1,1},
825 {"sdiffstore",sdiffstoreCommand
,-3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,2,-1,1},
826 {"smembers",sinterCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
827 {"zadd",zaddCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
828 {"zincrby",zincrbyCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
829 {"zrem",zremCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
830 {"zremrangebyscore",zremrangebyscoreCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
831 {"zremrangebyrank",zremrangebyrankCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
832 {"zunionstore",zunionstoreCommand
,-4,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,zunionInterBlockClientOnSwappedKeys
,0,0,0},
833 {"zinterstore",zinterstoreCommand
,-4,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,zunionInterBlockClientOnSwappedKeys
,0,0,0},
834 {"zrange",zrangeCommand
,-4,REDIS_CMD_INLINE
,NULL
,1,1,1},
835 {"zrangebyscore",zrangebyscoreCommand
,-4,REDIS_CMD_INLINE
,NULL
,1,1,1},
836 {"zcount",zcountCommand
,4,REDIS_CMD_INLINE
,NULL
,1,1,1},
837 {"zrevrange",zrevrangeCommand
,-4,REDIS_CMD_INLINE
,NULL
,1,1,1},
838 {"zcard",zcardCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
839 {"zscore",zscoreCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
840 {"zrank",zrankCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
841 {"zrevrank",zrevrankCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
842 {"hset",hsetCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
843 {"hsetnx",hsetnxCommand
,4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
844 {"hget",hgetCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
845 {"hmset",hmsetCommand
,-4,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
846 {"hmget",hmgetCommand
,-3,REDIS_CMD_BULK
,NULL
,1,1,1},
847 {"hincrby",hincrbyCommand
,4,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
848 {"hdel",hdelCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
849 {"hlen",hlenCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
850 {"hkeys",hkeysCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
851 {"hvals",hvalsCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
852 {"hgetall",hgetallCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
853 {"hexists",hexistsCommand
,3,REDIS_CMD_BULK
,NULL
,1,1,1},
854 {"incrby",incrbyCommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
855 {"decrby",decrbyCommand
,3,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
856 {"getset",getsetCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
857 {"mset",msetCommand
,-3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,-1,2},
858 {"msetnx",msetnxCommand
,-3,REDIS_CMD_BULK
|REDIS_CMD_DENYOOM
,NULL
,1,-1,2},
859 {"randomkey",randomkeyCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
860 {"select",selectCommand
,2,REDIS_CMD_INLINE
,NULL
,0,0,0},
861 {"move",moveCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
862 {"rename",renameCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
863 {"renamenx",renamenxCommand
,3,REDIS_CMD_INLINE
,NULL
,1,1,1},
864 {"expire",expireCommand
,3,REDIS_CMD_INLINE
,NULL
,0,0,0},
865 {"expireat",expireatCommand
,3,REDIS_CMD_INLINE
,NULL
,0,0,0},
866 {"keys",keysCommand
,2,REDIS_CMD_INLINE
,NULL
,0,0,0},
867 {"dbsize",dbsizeCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
868 {"auth",authCommand
,2,REDIS_CMD_INLINE
,NULL
,0,0,0},
869 {"ping",pingCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
870 {"echo",echoCommand
,2,REDIS_CMD_BULK
,NULL
,0,0,0},
871 {"save",saveCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
872 {"bgsave",bgsaveCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
873 {"bgrewriteaof",bgrewriteaofCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
874 {"shutdown",shutdownCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
875 {"lastsave",lastsaveCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
876 {"type",typeCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
877 {"multi",multiCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
878 {"exec",execCommand
,1,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,execBlockClientOnSwappedKeys
,0,0,0},
879 {"discard",discardCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
880 {"sync",syncCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
881 {"flushdb",flushdbCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
882 {"flushall",flushallCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
883 {"sort",sortCommand
,-2,REDIS_CMD_INLINE
|REDIS_CMD_DENYOOM
,NULL
,1,1,1},
884 {"info",infoCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
885 {"monitor",monitorCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0},
886 {"ttl",ttlCommand
,2,REDIS_CMD_INLINE
,NULL
,1,1,1},
887 {"slaveof",slaveofCommand
,3,REDIS_CMD_INLINE
,NULL
,0,0,0},
888 {"debug",debugCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
889 {"config",configCommand
,-2,REDIS_CMD_BULK
,NULL
,0,0,0},
890 {"subscribe",subscribeCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
891 {"unsubscribe",unsubscribeCommand
,-1,REDIS_CMD_INLINE
,NULL
,0,0,0},
892 {"psubscribe",psubscribeCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
893 {"punsubscribe",punsubscribeCommand
,-1,REDIS_CMD_INLINE
,NULL
,0,0,0},
894 {"publish",publishCommand
,3,REDIS_CMD_BULK
|REDIS_CMD_FORCE_REPLICATION
,NULL
,0,0,0},
895 {"watch",watchCommand
,-2,REDIS_CMD_INLINE
,NULL
,0,0,0},
896 {"unwatch",unwatchCommand
,1,REDIS_CMD_INLINE
,NULL
,0,0,0}
899 /*============================ Utility functions ============================ */
901 /* Glob-style pattern matching. */
902 static int stringmatchlen(const char *pattern
, int patternLen
,
903 const char *string
, int stringLen
, int nocase
)
908 while (pattern
[1] == '*') {
913 return 1; /* match */
915 if (stringmatchlen(pattern
+1, patternLen
-1,
916 string
, stringLen
, nocase
))
917 return 1; /* match */
921 return 0; /* no match */
925 return 0; /* no match */
935 not = pattern
[0] == '^';
942 if (pattern
[0] == '\\') {
945 if (pattern
[0] == string
[0])
947 } else if (pattern
[0] == ']') {
949 } else if (patternLen
== 0) {
953 } else if (pattern
[1] == '-' && patternLen
>= 3) {
954 int start
= pattern
[0];
955 int end
= pattern
[2];
963 start
= tolower(start
);
969 if (c
>= start
&& c
<= end
)
973 if (pattern
[0] == string
[0])
976 if (tolower((int)pattern
[0]) == tolower((int)string
[0]))
986 return 0; /* no match */
992 if (patternLen
>= 2) {
999 if (pattern
[0] != string
[0])
1000 return 0; /* no match */
1002 if (tolower((int)pattern
[0]) != tolower((int)string
[0]))
1003 return 0; /* no match */
1011 if (stringLen
== 0) {
1012 while(*pattern
== '*') {
1019 if (patternLen
== 0 && stringLen
== 0)
1024 static int stringmatch(const char *pattern
, const char *string
, int nocase
) {
1025 return stringmatchlen(pattern
,strlen(pattern
),string
,strlen(string
),nocase
);
1028 /* Convert a string representing an amount of memory into the number of
1029 * bytes, so for instance memtoll("1Gi") will return 1073741824 that is
1032 * On parsing error, if *err is not NULL, it's set to 1, otherwise it's
1034 static long long memtoll(const char *p
, int *err
) {
1037 long mul
; /* unit multiplier */
1039 unsigned int digits
;
1042 /* Search the first non digit character. */
1045 while(*u
&& isdigit(*u
)) u
++;
1046 if (*u
== '\0' || !strcasecmp(u
,"b")) {
1048 } else if (!strcasecmp(u
,"k")) {
1050 } else if (!strcasecmp(u
,"kb")) {
1052 } else if (!strcasecmp(u
,"m")) {
1054 } else if (!strcasecmp(u
,"mb")) {
1056 } else if (!strcasecmp(u
,"g")) {
1057 mul
= 1000L*1000*1000;
1058 } else if (!strcasecmp(u
,"gb")) {
1059 mul
= 1024L*1024*1024;
1065 if (digits
>= sizeof(buf
)) {
1069 memcpy(buf
,p
,digits
);
1071 val
= strtoll(buf
,NULL
,10);
1075 /* Convert a long long into a string. Returns the number of
1076 * characters needed to represent the number, that can be shorter if passed
1077 * buffer length is not enough to store the whole number. */
1078 static int ll2string(char *s
, size_t len
, long long value
) {
1080 unsigned long long v
;
1083 if (len
== 0) return 0;
1084 v
= (value
< 0) ? -value
: value
;
1085 p
= buf
+31; /* point to the last character */
1090 if (value
< 0) *p
-- = '-';
1093 if (l
+1 > len
) l
= len
-1; /* Make sure it fits, including the nul term */
1099 static void redisLog(int level
, const char *fmt
, ...) {
1103 fp
= (server
.logfile
== NULL
) ? stdout
: fopen(server
.logfile
,"a");
1107 if (level
>= server
.verbosity
) {
1113 strftime(buf
,64,"%d %b %H:%M:%S",localtime(&now
));
1114 fprintf(fp
,"[%d] %s %c ",(int)getpid(),buf
,c
[level
]);
1115 vfprintf(fp
, fmt
, ap
);
1121 if (server
.logfile
) fclose(fp
);
1124 /*====================== Hash table type implementation ==================== */
1126 /* This is an hash table type that uses the SDS dynamic strings libary as
1127 * keys and radis objects as values (objects can hold SDS strings,
1130 static void dictVanillaFree(void *privdata
, void *val
)
1132 DICT_NOTUSED(privdata
);
1136 static void dictListDestructor(void *privdata
, void *val
)
1138 DICT_NOTUSED(privdata
);
1139 listRelease((list
*)val
);
1142 static int dictSdsKeyCompare(void *privdata
, const void *key1
,
1146 DICT_NOTUSED(privdata
);
1148 l1
= sdslen((sds
)key1
);
1149 l2
= sdslen((sds
)key2
);
1150 if (l1
!= l2
) return 0;
1151 return memcmp(key1
, key2
, l1
) == 0;
1154 static void dictRedisObjectDestructor(void *privdata
, void *val
)
1156 DICT_NOTUSED(privdata
);
1158 if (val
== NULL
) return; /* Values of swapped out keys as set to NULL */
1162 static void dictSdsDestructor(void *privdata
, void *val
)
1164 DICT_NOTUSED(privdata
);
1169 static int dictObjKeyCompare(void *privdata
, const void *key1
,
1172 const robj
*o1
= key1
, *o2
= key2
;
1173 return dictSdsKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
1176 static unsigned int dictObjHash(const void *key
) {
1177 const robj
*o
= key
;
1178 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
1181 static unsigned int dictSdsHash(const void *key
) {
1182 return dictGenHashFunction((unsigned char*)key
, sdslen((char*)key
));
1185 static int dictEncObjKeyCompare(void *privdata
, const void *key1
,
1188 robj
*o1
= (robj
*) key1
, *o2
= (robj
*) key2
;
1191 if (o1
->encoding
== REDIS_ENCODING_INT
&&
1192 o2
->encoding
== REDIS_ENCODING_INT
)
1193 return o1
->ptr
== o2
->ptr
;
1195 o1
= getDecodedObject(o1
);
1196 o2
= getDecodedObject(o2
);
1197 cmp
= dictSdsKeyCompare(privdata
,o1
->ptr
,o2
->ptr
);
1203 static unsigned int dictEncObjHash(const void *key
) {
1204 robj
*o
= (robj
*) key
;
1206 if (o
->encoding
== REDIS_ENCODING_RAW
) {
1207 return dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
1209 if (o
->encoding
== REDIS_ENCODING_INT
) {
1213 len
= ll2string(buf
,32,(long)o
->ptr
);
1214 return dictGenHashFunction((unsigned char*)buf
, len
);
1218 o
= getDecodedObject(o
);
1219 hash
= dictGenHashFunction(o
->ptr
, sdslen((sds
)o
->ptr
));
1227 static dictType setDictType
= {
1228 dictEncObjHash
, /* hash function */
1231 dictEncObjKeyCompare
, /* key compare */
1232 dictRedisObjectDestructor
, /* key destructor */
1233 NULL
/* val destructor */
1236 /* Sorted sets hash (note: a skiplist is used in addition to the hash table) */
1237 static dictType zsetDictType
= {
1238 dictEncObjHash
, /* hash function */
1241 dictEncObjKeyCompare
, /* key compare */
1242 dictRedisObjectDestructor
, /* key destructor */
1243 dictVanillaFree
/* val destructor of malloc(sizeof(double)) */
1246 /* Db->dict, keys are sds strings, vals are Redis objects. */
1247 static dictType dbDictType
= {
1248 dictSdsHash
, /* hash function */
1251 dictSdsKeyCompare
, /* key compare */
1252 dictSdsDestructor
, /* key destructor */
1253 dictRedisObjectDestructor
/* val destructor */
1257 static dictType keyptrDictType
= {
1258 dictSdsHash
, /* hash function */
1261 dictSdsKeyCompare
, /* key compare */
1262 NULL
, /* key destructor */
1263 NULL
/* val destructor */
1266 /* Hash type hash table (note that small hashes are represented with zimpaps) */
1267 static dictType hashDictType
= {
1268 dictEncObjHash
, /* hash function */
1271 dictEncObjKeyCompare
, /* key compare */
1272 dictRedisObjectDestructor
, /* key destructor */
1273 dictRedisObjectDestructor
/* val destructor */
1276 /* Keylist hash table type has unencoded redis objects as keys and
1277 * lists as values. It's used for blocking operations (BLPOP) and to
1278 * map swapped keys to a list of clients waiting for this keys to be loaded. */
1279 static dictType keylistDictType
= {
1280 dictObjHash
, /* hash function */
1283 dictObjKeyCompare
, /* key compare */
1284 dictRedisObjectDestructor
, /* key destructor */
1285 dictListDestructor
/* val destructor */
1288 static void version();
1290 /* ========================= Random utility functions ======================= */
1292 /* Redis generally does not try to recover from out of memory conditions
1293 * when allocating objects or strings, it is not clear if it will be possible
1294 * to report this condition to the client since the networking layer itself
1295 * is based on heap allocation for send buffers, so we simply abort.
1296 * At least the code will be simpler to read... */
1297 static void oom(const char *msg
) {
1298 redisLog(REDIS_WARNING
, "%s: Out of memory\n",msg
);
1303 /* ====================== Redis server networking stuff ===================== */
1304 static void closeTimedoutClients(void) {
1307 time_t now
= time(NULL
);
1310 listRewind(server
.clients
,&li
);
1311 while ((ln
= listNext(&li
)) != NULL
) {
1312 c
= listNodeValue(ln
);
1313 if (server
.maxidletime
&&
1314 !(c
->flags
& REDIS_SLAVE
) && /* no timeout for slaves */
1315 !(c
->flags
& REDIS_MASTER
) && /* no timeout for masters */
1316 dictSize(c
->pubsub_channels
) == 0 && /* no timeout for pubsub */
1317 listLength(c
->pubsub_patterns
) == 0 &&
1318 (now
- c
->lastinteraction
> server
.maxidletime
))
1320 redisLog(REDIS_VERBOSE
,"Closing idle client");
1322 } else if (c
->flags
& REDIS_BLOCKED
) {
1323 if (c
->blockingto
!= 0 && c
->blockingto
< now
) {
1324 addReply(c
,shared
.nullmultibulk
);
1325 unblockClientWaitingData(c
);
1331 static int htNeedsResize(dict
*dict
) {
1332 long long size
, used
;
1334 size
= dictSlots(dict
);
1335 used
= dictSize(dict
);
1336 return (size
&& used
&& size
> DICT_HT_INITIAL_SIZE
&&
1337 (used
*100/size
< REDIS_HT_MINFILL
));
1340 /* If the percentage of used slots in the HT reaches REDIS_HT_MINFILL
1341 * we resize the hash table to save memory */
1342 static void tryResizeHashTables(void) {
1345 for (j
= 0; j
< server
.dbnum
; j
++) {
1346 if (htNeedsResize(server
.db
[j
].dict
))
1347 dictResize(server
.db
[j
].dict
);
1348 if (htNeedsResize(server
.db
[j
].expires
))
1349 dictResize(server
.db
[j
].expires
);
1353 /* Our hash table implementation performs rehashing incrementally while
1354 * we write/read from the hash table. Still if the server is idle, the hash
1355 * table will use two tables for a long time. So we try to use 1 millisecond
1356 * of CPU time at every serverCron() loop in order to rehash some key. */
1357 static void incrementallyRehash(void) {
1360 for (j
= 0; j
< server
.dbnum
; j
++) {
1361 if (dictIsRehashing(server
.db
[j
].dict
)) {
1362 dictRehashMilliseconds(server
.db
[j
].dict
,1);
1363 break; /* already used our millisecond for this loop... */
1368 /* A background saving child (BGSAVE) terminated its work. Handle this. */
1369 void backgroundSaveDoneHandler(int statloc
) {
1370 int exitcode
= WEXITSTATUS(statloc
);
1371 int bysignal
= WIFSIGNALED(statloc
);
1373 if (!bysignal
&& exitcode
== 0) {
1374 redisLog(REDIS_NOTICE
,
1375 "Background saving terminated with success");
1377 server
.lastsave
= time(NULL
);
1378 } else if (!bysignal
&& exitcode
!= 0) {
1379 redisLog(REDIS_WARNING
, "Background saving error");
1381 redisLog(REDIS_WARNING
,
1382 "Background saving terminated by signal %d", WTERMSIG(statloc
));
1383 rdbRemoveTempFile(server
.bgsavechildpid
);
1385 server
.bgsavechildpid
= -1;
1386 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1387 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1388 updateSlavesWaitingBgsave(exitcode
== 0 ? REDIS_OK
: REDIS_ERR
);
1391 /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
1393 void backgroundRewriteDoneHandler(int statloc
) {
1394 int exitcode
= WEXITSTATUS(statloc
);
1395 int bysignal
= WIFSIGNALED(statloc
);
1397 if (!bysignal
&& exitcode
== 0) {
1401 redisLog(REDIS_NOTICE
,
1402 "Background append only file rewriting terminated with success");
1403 /* Now it's time to flush the differences accumulated by the parent */
1404 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) server
.bgrewritechildpid
);
1405 fd
= open(tmpfile
,O_WRONLY
|O_APPEND
);
1407 redisLog(REDIS_WARNING
, "Not able to open the temp append only file produced by the child: %s", strerror(errno
));
1410 /* Flush our data... */
1411 if (write(fd
,server
.bgrewritebuf
,sdslen(server
.bgrewritebuf
)) !=
1412 (signed) sdslen(server
.bgrewritebuf
)) {
1413 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
));
1417 redisLog(REDIS_NOTICE
,"Parent diff flushed into the new append log file with success (%lu bytes)",sdslen(server
.bgrewritebuf
));
1418 /* Now our work is to rename the temp file into the stable file. And
1419 * switch the file descriptor used by the server for append only. */
1420 if (rename(tmpfile
,server
.appendfilename
) == -1) {
1421 redisLog(REDIS_WARNING
,"Can't rename the temp append only file into the stable one: %s", strerror(errno
));
1425 /* Mission completed... almost */
1426 redisLog(REDIS_NOTICE
,"Append only file successfully rewritten.");
1427 if (server
.appendfd
!= -1) {
1428 /* If append only is actually enabled... */
1429 close(server
.appendfd
);
1430 server
.appendfd
= fd
;
1431 if (server
.appendfsync
!= APPENDFSYNC_NO
) aof_fsync(fd
);
1432 server
.appendseldb
= -1; /* Make sure it will issue SELECT */
1433 redisLog(REDIS_NOTICE
,"The new append only file was selected for future appends.");
1435 /* If append only is disabled we just generate a dump in this
1436 * format. Why not? */
1439 } else if (!bysignal
&& exitcode
!= 0) {
1440 redisLog(REDIS_WARNING
, "Background append only file rewriting error");
1442 redisLog(REDIS_WARNING
,
1443 "Background append only file rewriting terminated by signal %d",
1447 sdsfree(server
.bgrewritebuf
);
1448 server
.bgrewritebuf
= sdsempty();
1449 aofRemoveTempFile(server
.bgrewritechildpid
);
1450 server
.bgrewritechildpid
= -1;
1453 /* This function is called once a background process of some kind terminates,
1454 * as we want to avoid resizing the hash tables when there is a child in order
1455 * to play well with copy-on-write (otherwise when a resize happens lots of
1456 * memory pages are copied). The goal of this function is to update the ability
1457 * for dict.c to resize the hash tables accordingly to the fact we have o not
1458 * running childs. */
1459 static void updateDictResizePolicy(void) {
1460 if (server
.bgsavechildpid
== -1 && server
.bgrewritechildpid
== -1)
1463 dictDisableResize();
1466 static int serverCron(struct aeEventLoop
*eventLoop
, long long id
, void *clientData
) {
1467 int j
, loops
= server
.cronloops
++;
1468 REDIS_NOTUSED(eventLoop
);
1470 REDIS_NOTUSED(clientData
);
1472 /* We take a cached value of the unix time in the global state because
1473 * with virtual memory and aging there is to store the current time
1474 * in objects at every object access, and accuracy is not needed.
1475 * To access a global var is faster than calling time(NULL) */
1476 server
.unixtime
= time(NULL
);
1477 /* We have just 21 bits per object for LRU information.
1478 * So we use an (eventually wrapping) LRU clock with minutes resolution.
1480 * When we need to select what object to swap, we compute the minimum
1481 * time distance between the current lruclock and the object last access
1482 * lruclock info. Even if clocks will wrap on overflow, there is
1483 * the interesting property that we are sure that at least
1484 * ABS(A-B) minutes passed between current time and timestamp B.
1486 * This is not precise but we don't need at all precision, but just
1487 * something statistically reasonable.
1489 server
.lruclock
= (time(NULL
)/60)&((1<<21)-1);
1491 /* We received a SIGTERM, shutting down here in a safe way, as it is
1492 * not ok doing so inside the signal handler. */
1493 if (server
.shutdown_asap
) {
1494 if (prepareForShutdown() == REDIS_OK
) exit(0);
1495 redisLog(REDIS_WARNING
,"SIGTERM received but errors trying to shut down the server, check the logs for more information");
1498 /* Show some info about non-empty databases */
1499 for (j
= 0; j
< server
.dbnum
; j
++) {
1500 long long size
, used
, vkeys
;
1502 size
= dictSlots(server
.db
[j
].dict
);
1503 used
= dictSize(server
.db
[j
].dict
);
1504 vkeys
= dictSize(server
.db
[j
].expires
);
1505 if (!(loops
% 50) && (used
|| vkeys
)) {
1506 redisLog(REDIS_VERBOSE
,"DB %d: %lld keys (%lld volatile) in %lld slots HT.",j
,used
,vkeys
,size
);
1507 /* dictPrintStats(server.dict); */
1511 /* We don't want to resize the hash tables while a bacground saving
1512 * is in progress: the saving child is created using fork() that is
1513 * implemented with a copy-on-write semantic in most modern systems, so
1514 * if we resize the HT while there is the saving child at work actually
1515 * a lot of memory movements in the parent will cause a lot of pages
1517 if (server
.bgsavechildpid
== -1 && server
.bgrewritechildpid
== -1) {
1518 if (!(loops
% 10)) tryResizeHashTables();
1519 if (server
.activerehashing
) incrementallyRehash();
1522 /* Show information about connected clients */
1523 if (!(loops
% 50)) {
1524 redisLog(REDIS_VERBOSE
,"%d clients connected (%d slaves), %zu bytes in use",
1525 listLength(server
.clients
)-listLength(server
.slaves
),
1526 listLength(server
.slaves
),
1527 zmalloc_used_memory());
1530 /* Close connections of timedout clients */
1531 if ((server
.maxidletime
&& !(loops
% 100)) || server
.blpop_blocked_clients
)
1532 closeTimedoutClients();
1534 /* Check if a background saving or AOF rewrite in progress terminated */
1535 if (server
.bgsavechildpid
!= -1 || server
.bgrewritechildpid
!= -1) {
1539 if ((pid
= wait3(&statloc
,WNOHANG
,NULL
)) != 0) {
1540 if (pid
== server
.bgsavechildpid
) {
1541 backgroundSaveDoneHandler(statloc
);
1543 backgroundRewriteDoneHandler(statloc
);
1545 updateDictResizePolicy();
1548 /* If there is not a background saving in progress check if
1549 * we have to save now */
1550 time_t now
= time(NULL
);
1551 for (j
= 0; j
< server
.saveparamslen
; j
++) {
1552 struct saveparam
*sp
= server
.saveparams
+j
;
1554 if (server
.dirty
>= sp
->changes
&&
1555 now
-server
.lastsave
> sp
->seconds
) {
1556 redisLog(REDIS_NOTICE
,"%d changes in %d seconds. Saving...",
1557 sp
->changes
, sp
->seconds
);
1558 rdbSaveBackground(server
.dbfilename
);
1564 /* Try to expire a few timed out keys. The algorithm used is adaptive and
1565 * will use few CPU cycles if there are few expiring keys, otherwise
1566 * it will get more aggressive to avoid that too much memory is used by
1567 * keys that can be removed from the keyspace. */
1568 for (j
= 0; j
< server
.dbnum
; j
++) {
1570 redisDb
*db
= server
.db
+j
;
1572 /* Continue to expire if at the end of the cycle more than 25%
1573 * of the keys were expired. */
1575 long num
= dictSize(db
->expires
);
1576 time_t now
= time(NULL
);
1579 if (num
> REDIS_EXPIRELOOKUPS_PER_CRON
)
1580 num
= REDIS_EXPIRELOOKUPS_PER_CRON
;
1585 if ((de
= dictGetRandomKey(db
->expires
)) == NULL
) break;
1586 t
= (time_t) dictGetEntryVal(de
);
1588 sds key
= dictGetEntryKey(de
);
1589 robj
*keyobj
= createStringObject(key
,sdslen(key
));
1591 dbDelete(db
,keyobj
);
1592 decrRefCount(keyobj
);
1594 server
.stat_expiredkeys
++;
1597 } while (expired
> REDIS_EXPIRELOOKUPS_PER_CRON
/4);
1600 /* Swap a few keys on disk if we are over the memory limit and VM
1601 * is enbled. Try to free objects from the free list first. */
1602 if (vmCanSwapOut()) {
1603 while (server
.vm_enabled
&& zmalloc_used_memory() >
1604 server
.vm_max_memory
)
1608 if (tryFreeOneObjectFromFreelist() == REDIS_OK
) continue;
1609 retval
= (server
.vm_max_threads
== 0) ?
1610 vmSwapOneObjectBlocking() :
1611 vmSwapOneObjectThreaded();
1612 if (retval
== REDIS_ERR
&& !(loops
% 300) &&
1613 zmalloc_used_memory() >
1614 (server
.vm_max_memory
+server
.vm_max_memory
/10))
1616 redisLog(REDIS_WARNING
,"WARNING: vm-max-memory limit exceeded by more than 10%% but unable to swap more objects out!");
1618 /* Note that when using threade I/O we free just one object,
1619 * because anyway when the I/O thread in charge to swap this
1620 * object out will finish, the handler of completed jobs
1621 * will try to swap more objects if we are still out of memory. */
1622 if (retval
== REDIS_ERR
|| server
.vm_max_threads
> 0) break;
1626 /* Check if we should connect to a MASTER */
1627 if (server
.replstate
== REDIS_REPL_CONNECT
&& !(loops
% 10)) {
1628 redisLog(REDIS_NOTICE
,"Connecting to MASTER...");
1629 if (syncWithMaster() == REDIS_OK
) {
1630 redisLog(REDIS_NOTICE
,"MASTER <-> SLAVE sync succeeded");
1631 if (server
.appendonly
) rewriteAppendOnlyFileBackground();
1637 /* This function gets called every time Redis is entering the
1638 * main loop of the event driven library, that is, before to sleep
1639 * for ready file descriptors. */
1640 static void beforeSleep(struct aeEventLoop
*eventLoop
) {
1641 REDIS_NOTUSED(eventLoop
);
1643 /* Awake clients that got all the swapped keys they requested */
1644 if (server
.vm_enabled
&& listLength(server
.io_ready_clients
)) {
1648 listRewind(server
.io_ready_clients
,&li
);
1649 while((ln
= listNext(&li
))) {
1650 redisClient
*c
= ln
->value
;
1651 struct redisCommand
*cmd
;
1653 /* Resume the client. */
1654 listDelNode(server
.io_ready_clients
,ln
);
1655 c
->flags
&= (~REDIS_IO_WAIT
);
1656 server
.vm_blocked_clients
--;
1657 aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
1658 readQueryFromClient
, c
);
1659 cmd
= lookupCommand(c
->argv
[0]->ptr
);
1660 assert(cmd
!= NULL
);
1663 /* There may be more data to process in the input buffer. */
1664 if (c
->querybuf
&& sdslen(c
->querybuf
) > 0)
1665 processInputBuffer(c
);
1668 /* Write the AOF buffer on disk */
1669 flushAppendOnlyFile();
1672 static void createSharedObjects(void) {
1675 shared
.crlf
= createObject(REDIS_STRING
,sdsnew("\r\n"));
1676 shared
.ok
= createObject(REDIS_STRING
,sdsnew("+OK\r\n"));
1677 shared
.err
= createObject(REDIS_STRING
,sdsnew("-ERR\r\n"));
1678 shared
.emptybulk
= createObject(REDIS_STRING
,sdsnew("$0\r\n\r\n"));
1679 shared
.czero
= createObject(REDIS_STRING
,sdsnew(":0\r\n"));
1680 shared
.cone
= createObject(REDIS_STRING
,sdsnew(":1\r\n"));
1681 shared
.cnegone
= createObject(REDIS_STRING
,sdsnew(":-1\r\n"));
1682 shared
.nullbulk
= createObject(REDIS_STRING
,sdsnew("$-1\r\n"));
1683 shared
.nullmultibulk
= createObject(REDIS_STRING
,sdsnew("*-1\r\n"));
1684 shared
.emptymultibulk
= createObject(REDIS_STRING
,sdsnew("*0\r\n"));
1685 shared
.pong
= createObject(REDIS_STRING
,sdsnew("+PONG\r\n"));
1686 shared
.queued
= createObject(REDIS_STRING
,sdsnew("+QUEUED\r\n"));
1687 shared
.wrongtypeerr
= createObject(REDIS_STRING
,sdsnew(
1688 "-ERR Operation against a key holding the wrong kind of value\r\n"));
1689 shared
.nokeyerr
= createObject(REDIS_STRING
,sdsnew(
1690 "-ERR no such key\r\n"));
1691 shared
.syntaxerr
= createObject(REDIS_STRING
,sdsnew(
1692 "-ERR syntax error\r\n"));
1693 shared
.sameobjecterr
= createObject(REDIS_STRING
,sdsnew(
1694 "-ERR source and destination objects are the same\r\n"));
1695 shared
.outofrangeerr
= createObject(REDIS_STRING
,sdsnew(
1696 "-ERR index out of range\r\n"));
1697 shared
.space
= createObject(REDIS_STRING
,sdsnew(" "));
1698 shared
.colon
= createObject(REDIS_STRING
,sdsnew(":"));
1699 shared
.plus
= createObject(REDIS_STRING
,sdsnew("+"));
1700 shared
.select0
= createStringObject("select 0\r\n",10);
1701 shared
.select1
= createStringObject("select 1\r\n",10);
1702 shared
.select2
= createStringObject("select 2\r\n",10);
1703 shared
.select3
= createStringObject("select 3\r\n",10);
1704 shared
.select4
= createStringObject("select 4\r\n",10);
1705 shared
.select5
= createStringObject("select 5\r\n",10);
1706 shared
.select6
= createStringObject("select 6\r\n",10);
1707 shared
.select7
= createStringObject("select 7\r\n",10);
1708 shared
.select8
= createStringObject("select 8\r\n",10);
1709 shared
.select9
= createStringObject("select 9\r\n",10);
1710 shared
.messagebulk
= createStringObject("$7\r\nmessage\r\n",13);
1711 shared
.pmessagebulk
= createStringObject("$8\r\npmessage\r\n",14);
1712 shared
.subscribebulk
= createStringObject("$9\r\nsubscribe\r\n",15);
1713 shared
.unsubscribebulk
= createStringObject("$11\r\nunsubscribe\r\n",18);
1714 shared
.psubscribebulk
= createStringObject("$10\r\npsubscribe\r\n",17);
1715 shared
.punsubscribebulk
= createStringObject("$12\r\npunsubscribe\r\n",19);
1716 shared
.mbulk3
= createStringObject("*3\r\n",4);
1717 shared
.mbulk4
= createStringObject("*4\r\n",4);
1718 for (j
= 0; j
< REDIS_SHARED_INTEGERS
; j
++) {
1719 shared
.integers
[j
] = createObject(REDIS_STRING
,(void*)(long)j
);
1720 shared
.integers
[j
]->encoding
= REDIS_ENCODING_INT
;
1724 static void appendServerSaveParams(time_t seconds
, int changes
) {
1725 server
.saveparams
= zrealloc(server
.saveparams
,sizeof(struct saveparam
)*(server
.saveparamslen
+1));
1726 server
.saveparams
[server
.saveparamslen
].seconds
= seconds
;
1727 server
.saveparams
[server
.saveparamslen
].changes
= changes
;
1728 server
.saveparamslen
++;
1731 static void resetServerSaveParams() {
1732 zfree(server
.saveparams
);
1733 server
.saveparams
= NULL
;
1734 server
.saveparamslen
= 0;
1737 static void initServerConfig() {
1738 server
.dbnum
= REDIS_DEFAULT_DBNUM
;
1739 server
.port
= REDIS_SERVERPORT
;
1740 server
.verbosity
= REDIS_VERBOSE
;
1741 server
.maxidletime
= REDIS_MAXIDLETIME
;
1742 server
.saveparams
= NULL
;
1743 server
.logfile
= NULL
; /* NULL = log on standard output */
1744 server
.bindaddr
= NULL
;
1745 server
.glueoutputbuf
= 1;
1746 server
.daemonize
= 0;
1747 server
.appendonly
= 0;
1748 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
1749 server
.no_appendfsync_on_rewrite
= 0;
1750 server
.lastfsync
= time(NULL
);
1751 server
.appendfd
= -1;
1752 server
.appendseldb
= -1; /* Make sure the first time will not match */
1753 server
.pidfile
= zstrdup("/var/run/redis.pid");
1754 server
.dbfilename
= zstrdup("dump.rdb");
1755 server
.appendfilename
= zstrdup("appendonly.aof");
1756 server
.requirepass
= NULL
;
1757 server
.rdbcompression
= 1;
1758 server
.activerehashing
= 1;
1759 server
.maxclients
= 0;
1760 server
.blpop_blocked_clients
= 0;
1761 server
.maxmemory
= 0;
1762 server
.vm_enabled
= 0;
1763 server
.vm_swap_file
= zstrdup("/tmp/redis-%p.vm");
1764 server
.vm_page_size
= 256; /* 256 bytes per page */
1765 server
.vm_pages
= 1024*1024*100; /* 104 millions of pages */
1766 server
.vm_max_memory
= 1024LL*1024*1024*1; /* 1 GB of RAM */
1767 server
.vm_max_threads
= 4;
1768 server
.vm_blocked_clients
= 0;
1769 server
.hash_max_zipmap_entries
= REDIS_HASH_MAX_ZIPMAP_ENTRIES
;
1770 server
.hash_max_zipmap_value
= REDIS_HASH_MAX_ZIPMAP_VALUE
;
1771 server
.list_max_ziplist_entries
= REDIS_LIST_MAX_ZIPLIST_ENTRIES
;
1772 server
.list_max_ziplist_value
= REDIS_LIST_MAX_ZIPLIST_VALUE
;
1773 server
.shutdown_asap
= 0;
1775 resetServerSaveParams();
1777 appendServerSaveParams(60*60,1); /* save after 1 hour and 1 change */
1778 appendServerSaveParams(300,100); /* save after 5 minutes and 100 changes */
1779 appendServerSaveParams(60,10000); /* save after 1 minute and 10000 changes */
1780 /* Replication related */
1782 server
.masterauth
= NULL
;
1783 server
.masterhost
= NULL
;
1784 server
.masterport
= 6379;
1785 server
.master
= NULL
;
1786 server
.replstate
= REDIS_REPL_NONE
;
1788 /* Double constants initialization */
1790 R_PosInf
= 1.0/R_Zero
;
1791 R_NegInf
= -1.0/R_Zero
;
1792 R_Nan
= R_Zero
/R_Zero
;
1795 static void initServer() {
1798 signal(SIGHUP
, SIG_IGN
);
1799 signal(SIGPIPE
, SIG_IGN
);
1800 setupSigSegvAction();
1802 server
.devnull
= fopen("/dev/null","w");
1803 if (server
.devnull
== NULL
) {
1804 redisLog(REDIS_WARNING
, "Can't open /dev/null: %s", server
.neterr
);
1807 server
.clients
= listCreate();
1808 server
.slaves
= listCreate();
1809 server
.monitors
= listCreate();
1810 server
.objfreelist
= listCreate();
1811 createSharedObjects();
1812 server
.el
= aeCreateEventLoop();
1813 server
.db
= zmalloc(sizeof(redisDb
)*server
.dbnum
);
1814 server
.fd
= anetTcpServer(server
.neterr
, server
.port
, server
.bindaddr
);
1815 if (server
.fd
== -1) {
1816 redisLog(REDIS_WARNING
, "Opening TCP port: %s", server
.neterr
);
1819 for (j
= 0; j
< server
.dbnum
; j
++) {
1820 server
.db
[j
].dict
= dictCreate(&dbDictType
,NULL
);
1821 server
.db
[j
].expires
= dictCreate(&keyptrDictType
,NULL
);
1822 server
.db
[j
].blocking_keys
= dictCreate(&keylistDictType
,NULL
);
1823 server
.db
[j
].watched_keys
= dictCreate(&keylistDictType
,NULL
);
1824 if (server
.vm_enabled
)
1825 server
.db
[j
].io_keys
= dictCreate(&keylistDictType
,NULL
);
1826 server
.db
[j
].id
= j
;
1828 server
.pubsub_channels
= dictCreate(&keylistDictType
,NULL
);
1829 server
.pubsub_patterns
= listCreate();
1830 listSetFreeMethod(server
.pubsub_patterns
,freePubsubPattern
);
1831 listSetMatchMethod(server
.pubsub_patterns
,listMatchPubsubPattern
);
1832 server
.cronloops
= 0;
1833 server
.bgsavechildpid
= -1;
1834 server
.bgrewritechildpid
= -1;
1835 server
.bgrewritebuf
= sdsempty();
1836 server
.aofbuf
= sdsempty();
1837 server
.lastsave
= time(NULL
);
1839 server
.stat_numcommands
= 0;
1840 server
.stat_numconnections
= 0;
1841 server
.stat_expiredkeys
= 0;
1842 server
.stat_starttime
= time(NULL
);
1843 server
.unixtime
= time(NULL
);
1844 aeCreateTimeEvent(server
.el
, 1, serverCron
, NULL
, NULL
);
1845 if (aeCreateFileEvent(server
.el
, server
.fd
, AE_READABLE
,
1846 acceptHandler
, NULL
) == AE_ERR
) oom("creating file event");
1848 if (server
.appendonly
) {
1849 server
.appendfd
= open(server
.appendfilename
,O_WRONLY
|O_APPEND
|O_CREAT
,0644);
1850 if (server
.appendfd
== -1) {
1851 redisLog(REDIS_WARNING
, "Can't open the append-only file: %s",
1857 if (server
.vm_enabled
) vmInit();
1860 /* Empty the whole database */
1861 static long long emptyDb() {
1863 long long removed
= 0;
1865 for (j
= 0; j
< server
.dbnum
; j
++) {
1866 removed
+= dictSize(server
.db
[j
].dict
);
1867 dictEmpty(server
.db
[j
].dict
);
1868 dictEmpty(server
.db
[j
].expires
);
1873 static int yesnotoi(char *s
) {
1874 if (!strcasecmp(s
,"yes")) return 1;
1875 else if (!strcasecmp(s
,"no")) return 0;
1879 /* I agree, this is a very rudimental way to load a configuration...
1880 will improve later if the config gets more complex */
1881 static void loadServerConfig(char *filename
) {
1883 char buf
[REDIS_CONFIGLINE_MAX
+1], *err
= NULL
;
1887 if (filename
[0] == '-' && filename
[1] == '\0')
1890 if ((fp
= fopen(filename
,"r")) == NULL
) {
1891 redisLog(REDIS_WARNING
, "Fatal error, can't open config file '%s'", filename
);
1896 while(fgets(buf
,REDIS_CONFIGLINE_MAX
+1,fp
) != NULL
) {
1902 line
= sdstrim(line
," \t\r\n");
1904 /* Skip comments and blank lines*/
1905 if (line
[0] == '#' || line
[0] == '\0') {
1910 /* Split into arguments */
1911 argv
= sdssplitlen(line
,sdslen(line
)," ",1,&argc
);
1912 sdstolower(argv
[0]);
1914 /* Execute config directives */
1915 if (!strcasecmp(argv
[0],"timeout") && argc
== 2) {
1916 server
.maxidletime
= atoi(argv
[1]);
1917 if (server
.maxidletime
< 0) {
1918 err
= "Invalid timeout value"; goto loaderr
;
1920 } else if (!strcasecmp(argv
[0],"port") && argc
== 2) {
1921 server
.port
= atoi(argv
[1]);
1922 if (server
.port
< 1 || server
.port
> 65535) {
1923 err
= "Invalid port"; goto loaderr
;
1925 } else if (!strcasecmp(argv
[0],"bind") && argc
== 2) {
1926 server
.bindaddr
= zstrdup(argv
[1]);
1927 } else if (!strcasecmp(argv
[0],"save") && argc
== 3) {
1928 int seconds
= atoi(argv
[1]);
1929 int changes
= atoi(argv
[2]);
1930 if (seconds
< 1 || changes
< 0) {
1931 err
= "Invalid save parameters"; goto loaderr
;
1933 appendServerSaveParams(seconds
,changes
);
1934 } else if (!strcasecmp(argv
[0],"dir") && argc
== 2) {
1935 if (chdir(argv
[1]) == -1) {
1936 redisLog(REDIS_WARNING
,"Can't chdir to '%s': %s",
1937 argv
[1], strerror(errno
));
1940 } else if (!strcasecmp(argv
[0],"loglevel") && argc
== 2) {
1941 if (!strcasecmp(argv
[1],"debug")) server
.verbosity
= REDIS_DEBUG
;
1942 else if (!strcasecmp(argv
[1],"verbose")) server
.verbosity
= REDIS_VERBOSE
;
1943 else if (!strcasecmp(argv
[1],"notice")) server
.verbosity
= REDIS_NOTICE
;
1944 else if (!strcasecmp(argv
[1],"warning")) server
.verbosity
= REDIS_WARNING
;
1946 err
= "Invalid log level. Must be one of debug, notice, warning";
1949 } else if (!strcasecmp(argv
[0],"logfile") && argc
== 2) {
1952 server
.logfile
= zstrdup(argv
[1]);
1953 if (!strcasecmp(server
.logfile
,"stdout")) {
1954 zfree(server
.logfile
);
1955 server
.logfile
= NULL
;
1957 if (server
.logfile
) {
1958 /* Test if we are able to open the file. The server will not
1959 * be able to abort just for this problem later... */
1960 logfp
= fopen(server
.logfile
,"a");
1961 if (logfp
== NULL
) {
1962 err
= sdscatprintf(sdsempty(),
1963 "Can't open the log file: %s", strerror(errno
));
1968 } else if (!strcasecmp(argv
[0],"databases") && argc
== 2) {
1969 server
.dbnum
= atoi(argv
[1]);
1970 if (server
.dbnum
< 1) {
1971 err
= "Invalid number of databases"; goto loaderr
;
1973 } else if (!strcasecmp(argv
[0],"include") && argc
== 2) {
1974 loadServerConfig(argv
[1]);
1975 } else if (!strcasecmp(argv
[0],"maxclients") && argc
== 2) {
1976 server
.maxclients
= atoi(argv
[1]);
1977 } else if (!strcasecmp(argv
[0],"maxmemory") && argc
== 2) {
1978 server
.maxmemory
= memtoll(argv
[1],NULL
);
1979 } else if (!strcasecmp(argv
[0],"slaveof") && argc
== 3) {
1980 server
.masterhost
= sdsnew(argv
[1]);
1981 server
.masterport
= atoi(argv
[2]);
1982 server
.replstate
= REDIS_REPL_CONNECT
;
1983 } else if (!strcasecmp(argv
[0],"masterauth") && argc
== 2) {
1984 server
.masterauth
= zstrdup(argv
[1]);
1985 } else if (!strcasecmp(argv
[0],"glueoutputbuf") && argc
== 2) {
1986 if ((server
.glueoutputbuf
= yesnotoi(argv
[1])) == -1) {
1987 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1989 } else if (!strcasecmp(argv
[0],"rdbcompression") && argc
== 2) {
1990 if ((server
.rdbcompression
= yesnotoi(argv
[1])) == -1) {
1991 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1993 } else if (!strcasecmp(argv
[0],"activerehashing") && argc
== 2) {
1994 if ((server
.activerehashing
= yesnotoi(argv
[1])) == -1) {
1995 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
1997 } else if (!strcasecmp(argv
[0],"daemonize") && argc
== 2) {
1998 if ((server
.daemonize
= yesnotoi(argv
[1])) == -1) {
1999 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
2001 } else if (!strcasecmp(argv
[0],"appendonly") && argc
== 2) {
2002 if ((server
.appendonly
= yesnotoi(argv
[1])) == -1) {
2003 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
2005 } else if (!strcasecmp(argv
[0],"appendfilename") && argc
== 2) {
2006 zfree(server
.appendfilename
);
2007 server
.appendfilename
= zstrdup(argv
[1]);
2008 } else if (!strcasecmp(argv
[0],"no-appendfsync-on-rewrite")
2010 if ((server
.no_appendfsync_on_rewrite
= yesnotoi(argv
[1])) == -1) {
2011 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
2013 } else if (!strcasecmp(argv
[0],"appendfsync") && argc
== 2) {
2014 if (!strcasecmp(argv
[1],"no")) {
2015 server
.appendfsync
= APPENDFSYNC_NO
;
2016 } else if (!strcasecmp(argv
[1],"always")) {
2017 server
.appendfsync
= APPENDFSYNC_ALWAYS
;
2018 } else if (!strcasecmp(argv
[1],"everysec")) {
2019 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
2021 err
= "argument must be 'no', 'always' or 'everysec'";
2024 } else if (!strcasecmp(argv
[0],"requirepass") && argc
== 2) {
2025 server
.requirepass
= zstrdup(argv
[1]);
2026 } else if (!strcasecmp(argv
[0],"pidfile") && argc
== 2) {
2027 zfree(server
.pidfile
);
2028 server
.pidfile
= zstrdup(argv
[1]);
2029 } else if (!strcasecmp(argv
[0],"dbfilename") && argc
== 2) {
2030 zfree(server
.dbfilename
);
2031 server
.dbfilename
= zstrdup(argv
[1]);
2032 } else if (!strcasecmp(argv
[0],"vm-enabled") && argc
== 2) {
2033 if ((server
.vm_enabled
= yesnotoi(argv
[1])) == -1) {
2034 err
= "argument must be 'yes' or 'no'"; goto loaderr
;
2036 } else if (!strcasecmp(argv
[0],"vm-swap-file") && argc
== 2) {
2037 zfree(server
.vm_swap_file
);
2038 server
.vm_swap_file
= zstrdup(argv
[1]);
2039 } else if (!strcasecmp(argv
[0],"vm-max-memory") && argc
== 2) {
2040 server
.vm_max_memory
= memtoll(argv
[1],NULL
);
2041 } else if (!strcasecmp(argv
[0],"vm-page-size") && argc
== 2) {
2042 server
.vm_page_size
= memtoll(argv
[1], NULL
);
2043 } else if (!strcasecmp(argv
[0],"vm-pages") && argc
== 2) {
2044 server
.vm_pages
= memtoll(argv
[1], NULL
);
2045 } else if (!strcasecmp(argv
[0],"vm-max-threads") && argc
== 2) {
2046 server
.vm_max_threads
= strtoll(argv
[1], NULL
, 10);
2047 } else if (!strcasecmp(argv
[0],"hash-max-zipmap-entries") && argc
== 2){
2048 server
.hash_max_zipmap_entries
= memtoll(argv
[1], NULL
);
2049 } else if (!strcasecmp(argv
[0],"hash-max-zipmap-value") && argc
== 2){
2050 server
.hash_max_zipmap_value
= memtoll(argv
[1], NULL
);
2051 } else if (!strcasecmp(argv
[0],"list-max-ziplist-entries") && argc
== 2){
2052 server
.list_max_ziplist_entries
= memtoll(argv
[1], NULL
);
2053 } else if (!strcasecmp(argv
[0],"list-max-ziplist-value") && argc
== 2){
2054 server
.list_max_ziplist_value
= memtoll(argv
[1], NULL
);
2056 err
= "Bad directive or wrong number of arguments"; goto loaderr
;
2058 for (j
= 0; j
< argc
; j
++)
2063 if (fp
!= stdin
) fclose(fp
);
2067 fprintf(stderr
, "\n*** FATAL CONFIG FILE ERROR ***\n");
2068 fprintf(stderr
, "Reading the configuration file, at line %d\n", linenum
);
2069 fprintf(stderr
, ">>> '%s'\n", line
);
2070 fprintf(stderr
, "%s\n", err
);
2074 static void freeClientArgv(redisClient
*c
) {
2077 for (j
= 0; j
< c
->argc
; j
++)
2078 decrRefCount(c
->argv
[j
]);
2079 for (j
= 0; j
< c
->mbargc
; j
++)
2080 decrRefCount(c
->mbargv
[j
]);
2085 static void freeClient(redisClient
*c
) {
2088 /* Note that if the client we are freeing is blocked into a blocking
2089 * call, we have to set querybuf to NULL *before* to call
2090 * unblockClientWaitingData() to avoid processInputBuffer() will get
2091 * called. Also it is important to remove the file events after
2092 * this, because this call adds the READABLE event. */
2093 sdsfree(c
->querybuf
);
2095 if (c
->flags
& REDIS_BLOCKED
)
2096 unblockClientWaitingData(c
);
2098 /* UNWATCH all the keys */
2100 listRelease(c
->watched_keys
);
2101 /* Unsubscribe from all the pubsub channels */
2102 pubsubUnsubscribeAllChannels(c
,0);
2103 pubsubUnsubscribeAllPatterns(c
,0);
2104 dictRelease(c
->pubsub_channels
);
2105 listRelease(c
->pubsub_patterns
);
2106 /* Obvious cleanup */
2107 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
2108 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
2109 listRelease(c
->reply
);
2112 /* Remove from the list of clients */
2113 ln
= listSearchKey(server
.clients
,c
);
2114 redisAssert(ln
!= NULL
);
2115 listDelNode(server
.clients
,ln
);
2116 /* Remove from the list of clients that are now ready to be restarted
2117 * after waiting for swapped keys */
2118 if (c
->flags
& REDIS_IO_WAIT
&& listLength(c
->io_keys
) == 0) {
2119 ln
= listSearchKey(server
.io_ready_clients
,c
);
2121 listDelNode(server
.io_ready_clients
,ln
);
2122 server
.vm_blocked_clients
--;
2125 /* Remove from the list of clients waiting for swapped keys */
2126 while (server
.vm_enabled
&& listLength(c
->io_keys
)) {
2127 ln
= listFirst(c
->io_keys
);
2128 dontWaitForSwappedKey(c
,ln
->value
);
2130 listRelease(c
->io_keys
);
2131 /* Master/slave cleanup */
2132 if (c
->flags
& REDIS_SLAVE
) {
2133 if (c
->replstate
== REDIS_REPL_SEND_BULK
&& c
->repldbfd
!= -1)
2135 list
*l
= (c
->flags
& REDIS_MONITOR
) ? server
.monitors
: server
.slaves
;
2136 ln
= listSearchKey(l
,c
);
2137 redisAssert(ln
!= NULL
);
2140 if (c
->flags
& REDIS_MASTER
) {
2141 server
.master
= NULL
;
2142 server
.replstate
= REDIS_REPL_CONNECT
;
2144 /* Release memory */
2147 freeClientMultiState(c
);
2151 #define GLUEREPLY_UP_TO (1024)
2152 static void glueReplyBuffersIfNeeded(redisClient
*c
) {
2154 char buf
[GLUEREPLY_UP_TO
];
2159 listRewind(c
->reply
,&li
);
2160 while((ln
= listNext(&li
))) {
2164 objlen
= sdslen(o
->ptr
);
2165 if (copylen
+ objlen
<= GLUEREPLY_UP_TO
) {
2166 memcpy(buf
+copylen
,o
->ptr
,objlen
);
2168 listDelNode(c
->reply
,ln
);
2170 if (copylen
== 0) return;
2174 /* Now the output buffer is empty, add the new single element */
2175 o
= createObject(REDIS_STRING
,sdsnewlen(buf
,copylen
));
2176 listAddNodeHead(c
->reply
,o
);
2179 static void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
2180 redisClient
*c
= privdata
;
2181 int nwritten
= 0, totwritten
= 0, objlen
;
2184 REDIS_NOTUSED(mask
);
2186 /* Use writev() if we have enough buffers to send */
2187 if (!server
.glueoutputbuf
&&
2188 listLength(c
->reply
) > REDIS_WRITEV_THRESHOLD
&&
2189 !(c
->flags
& REDIS_MASTER
))
2191 sendReplyToClientWritev(el
, fd
, privdata
, mask
);
2195 while(listLength(c
->reply
)) {
2196 if (server
.glueoutputbuf
&& listLength(c
->reply
) > 1)
2197 glueReplyBuffersIfNeeded(c
);
2199 o
= listNodeValue(listFirst(c
->reply
));
2200 objlen
= sdslen(o
->ptr
);
2203 listDelNode(c
->reply
,listFirst(c
->reply
));
2207 if (c
->flags
& REDIS_MASTER
) {
2208 /* Don't reply to a master */
2209 nwritten
= objlen
- c
->sentlen
;
2211 nwritten
= write(fd
, ((char*)o
->ptr
)+c
->sentlen
, objlen
- c
->sentlen
);
2212 if (nwritten
<= 0) break;
2214 c
->sentlen
+= nwritten
;
2215 totwritten
+= nwritten
;
2216 /* If we fully sent the object on head go to the next one */
2217 if (c
->sentlen
== objlen
) {
2218 listDelNode(c
->reply
,listFirst(c
->reply
));
2221 /* Note that we avoid to send more thank REDIS_MAX_WRITE_PER_EVENT
2222 * bytes, in a single threaded server it's a good idea to serve
2223 * other clients as well, even if a very large request comes from
2224 * super fast link that is always able to accept data (in real world
2225 * scenario think about 'KEYS *' against the loopback interfae) */
2226 if (totwritten
> REDIS_MAX_WRITE_PER_EVENT
) break;
2228 if (nwritten
== -1) {
2229 if (errno
== EAGAIN
) {
2232 redisLog(REDIS_VERBOSE
,
2233 "Error writing to client: %s", strerror(errno
));
2238 if (totwritten
> 0) c
->lastinteraction
= time(NULL
);
2239 if (listLength(c
->reply
) == 0) {
2241 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
2245 static void sendReplyToClientWritev(aeEventLoop
*el
, int fd
, void *privdata
, int mask
)
2247 redisClient
*c
= privdata
;
2248 int nwritten
= 0, totwritten
= 0, objlen
, willwrite
;
2250 struct iovec iov
[REDIS_WRITEV_IOVEC_COUNT
];
2251 int offset
, ion
= 0;
2253 REDIS_NOTUSED(mask
);
2256 while (listLength(c
->reply
)) {
2257 offset
= c
->sentlen
;
2261 /* fill-in the iov[] array */
2262 for(node
= listFirst(c
->reply
); node
; node
= listNextNode(node
)) {
2263 o
= listNodeValue(node
);
2264 objlen
= sdslen(o
->ptr
);
2266 if (totwritten
+ objlen
- offset
> REDIS_MAX_WRITE_PER_EVENT
)
2269 if(ion
== REDIS_WRITEV_IOVEC_COUNT
)
2270 break; /* no more iovecs */
2272 iov
[ion
].iov_base
= ((char*)o
->ptr
) + offset
;
2273 iov
[ion
].iov_len
= objlen
- offset
;
2274 willwrite
+= objlen
- offset
;
2275 offset
= 0; /* just for the first item */
2282 /* write all collected blocks at once */
2283 if((nwritten
= writev(fd
, iov
, ion
)) < 0) {
2284 if (errno
!= EAGAIN
) {
2285 redisLog(REDIS_VERBOSE
,
2286 "Error writing to client: %s", strerror(errno
));
2293 totwritten
+= nwritten
;
2294 offset
= c
->sentlen
;
2296 /* remove written robjs from c->reply */
2297 while (nwritten
&& listLength(c
->reply
)) {
2298 o
= listNodeValue(listFirst(c
->reply
));
2299 objlen
= sdslen(o
->ptr
);
2301 if(nwritten
>= objlen
- offset
) {
2302 listDelNode(c
->reply
, listFirst(c
->reply
));
2303 nwritten
-= objlen
- offset
;
2307 c
->sentlen
+= nwritten
;
2315 c
->lastinteraction
= time(NULL
);
2317 if (listLength(c
->reply
) == 0) {
2319 aeDeleteFileEvent(server
.el
,c
->fd
,AE_WRITABLE
);
2323 static int qsortRedisCommands(const void *r1
, const void *r2
) {
2325 ((struct redisCommand
*)r1
)->name
,
2326 ((struct redisCommand
*)r2
)->name
);
2329 static void sortCommandTable() {
2330 /* Copy and sort the read-only version of the command table */
2331 commandTable
= (struct redisCommand
*)malloc(sizeof(readonlyCommandTable
));
2332 memcpy(commandTable
,readonlyCommandTable
,sizeof(readonlyCommandTable
));
2334 sizeof(readonlyCommandTable
)/sizeof(struct redisCommand
),
2335 sizeof(struct redisCommand
),qsortRedisCommands
);
2338 static struct redisCommand
*lookupCommand(char *name
) {
2339 struct redisCommand tmp
= {name
,NULL
,0,0,NULL
,0,0,0};
2343 sizeof(readonlyCommandTable
)/sizeof(struct redisCommand
),
2344 sizeof(struct redisCommand
),
2345 qsortRedisCommands
);
2348 /* resetClient prepare the client to process the next command */
2349 static void resetClient(redisClient
*c
) {
2355 /* Call() is the core of Redis execution of a command */
2356 static void call(redisClient
*c
, struct redisCommand
*cmd
) {
2359 dirty
= server
.dirty
;
2361 dirty
= server
.dirty
-dirty
;
2363 if (server
.appendonly
&& dirty
)
2364 feedAppendOnlyFile(cmd
,c
->db
->id
,c
->argv
,c
->argc
);
2365 if ((dirty
|| cmd
->flags
& REDIS_CMD_FORCE_REPLICATION
) &&
2366 listLength(server
.slaves
))
2367 replicationFeedSlaves(server
.slaves
,c
->db
->id
,c
->argv
,c
->argc
);
2368 if (listLength(server
.monitors
))
2369 replicationFeedMonitors(server
.monitors
,c
->db
->id
,c
->argv
,c
->argc
);
2370 server
.stat_numcommands
++;
2373 /* If this function gets called we already read a whole
2374 * command, argments are in the client argv/argc fields.
2375 * processCommand() execute the command or prepare the
2376 * server for a bulk read from the client.
2378 * If 1 is returned the client is still alive and valid and
2379 * and other operations can be performed by the caller. Otherwise
2380 * if 0 is returned the client was destroied (i.e. after QUIT). */
2381 static int processCommand(redisClient
*c
) {
2382 struct redisCommand
*cmd
;
2384 /* Free some memory if needed (maxmemory setting) */
2385 if (server
.maxmemory
) freeMemoryIfNeeded();
2387 /* Handle the multi bulk command type. This is an alternative protocol
2388 * supported by Redis in order to receive commands that are composed of
2389 * multiple binary-safe "bulk" arguments. The latency of processing is
2390 * a bit higher but this allows things like multi-sets, so if this
2391 * protocol is used only for MSET and similar commands this is a big win. */
2392 if (c
->multibulk
== 0 && c
->argc
== 1 && ((char*)(c
->argv
[0]->ptr
))[0] == '*') {
2393 c
->multibulk
= atoi(((char*)c
->argv
[0]->ptr
)+1);
2394 if (c
->multibulk
<= 0) {
2398 decrRefCount(c
->argv
[c
->argc
-1]);
2402 } else if (c
->multibulk
) {
2403 if (c
->bulklen
== -1) {
2404 if (((char*)c
->argv
[0]->ptr
)[0] != '$') {
2405 addReplySds(c
,sdsnew("-ERR multi bulk protocol error\r\n"));
2409 int bulklen
= atoi(((char*)c
->argv
[0]->ptr
)+1);
2410 decrRefCount(c
->argv
[0]);
2411 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
2413 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
2418 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
2422 c
->mbargv
= zrealloc(c
->mbargv
,(sizeof(robj
*))*(c
->mbargc
+1));
2423 c
->mbargv
[c
->mbargc
] = c
->argv
[0];
2427 if (c
->multibulk
== 0) {
2431 /* Here we need to swap the multi-bulk argc/argv with the
2432 * normal argc/argv of the client structure. */
2434 c
->argv
= c
->mbargv
;
2435 c
->mbargv
= auxargv
;
2438 c
->argc
= c
->mbargc
;
2439 c
->mbargc
= auxargc
;
2441 /* We need to set bulklen to something different than -1
2442 * in order for the code below to process the command without
2443 * to try to read the last argument of a bulk command as
2444 * a special argument. */
2446 /* continue below and process the command */
2453 /* -- end of multi bulk commands processing -- */
2455 /* The QUIT command is handled as a special case. Normal command
2456 * procs are unable to close the client connection safely */
2457 if (!strcasecmp(c
->argv
[0]->ptr
,"quit")) {
2462 /* Now lookup the command and check ASAP about trivial error conditions
2463 * such wrong arity, bad command name and so forth. */
2464 cmd
= lookupCommand(c
->argv
[0]->ptr
);
2467 sdscatprintf(sdsempty(), "-ERR unknown command '%s'\r\n",
2468 (char*)c
->argv
[0]->ptr
));
2471 } else if ((cmd
->arity
> 0 && cmd
->arity
!= c
->argc
) ||
2472 (c
->argc
< -cmd
->arity
)) {
2474 sdscatprintf(sdsempty(),
2475 "-ERR wrong number of arguments for '%s' command\r\n",
2479 } else if (cmd
->flags
& REDIS_CMD_BULK
&& c
->bulklen
== -1) {
2480 /* This is a bulk command, we have to read the last argument yet. */
2481 int bulklen
= atoi(c
->argv
[c
->argc
-1]->ptr
);
2483 decrRefCount(c
->argv
[c
->argc
-1]);
2484 if (bulklen
< 0 || bulklen
> 1024*1024*1024) {
2486 addReplySds(c
,sdsnew("-ERR invalid bulk write count\r\n"));
2491 c
->bulklen
= bulklen
+2; /* add two bytes for CR+LF */
2492 /* It is possible that the bulk read is already in the
2493 * buffer. Check this condition and handle it accordingly.
2494 * This is just a fast path, alternative to call processInputBuffer().
2495 * It's a good idea since the code is small and this condition
2496 * happens most of the times. */
2497 if ((signed)sdslen(c
->querybuf
) >= c
->bulklen
) {
2498 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
2500 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
2502 /* Otherwise return... there is to read the last argument
2503 * from the socket. */
2507 /* Let's try to encode the bulk object to save space. */
2508 if (cmd
->flags
& REDIS_CMD_BULK
)
2509 c
->argv
[c
->argc
-1] = tryObjectEncoding(c
->argv
[c
->argc
-1]);
2511 /* Check if the user is authenticated */
2512 if (server
.requirepass
&& !c
->authenticated
&& cmd
->proc
!= authCommand
) {
2513 addReplySds(c
,sdsnew("-ERR operation not permitted\r\n"));
2518 /* Handle the maxmemory directive */
2519 if (server
.maxmemory
&& (cmd
->flags
& REDIS_CMD_DENYOOM
) &&
2520 zmalloc_used_memory() > server
.maxmemory
)
2522 addReplySds(c
,sdsnew("-ERR command not allowed when used memory > 'maxmemory'\r\n"));
2527 /* Only allow SUBSCRIBE and UNSUBSCRIBE in the context of Pub/Sub */
2528 if ((dictSize(c
->pubsub_channels
) > 0 || listLength(c
->pubsub_patterns
) > 0)
2530 cmd
->proc
!= subscribeCommand
&& cmd
->proc
!= unsubscribeCommand
&&
2531 cmd
->proc
!= psubscribeCommand
&& cmd
->proc
!= punsubscribeCommand
) {
2532 addReplySds(c
,sdsnew("-ERR only (P)SUBSCRIBE / (P)UNSUBSCRIBE / QUIT allowed in this context\r\n"));
2537 /* Exec the command */
2538 if (c
->flags
& REDIS_MULTI
&&
2539 cmd
->proc
!= execCommand
&& cmd
->proc
!= discardCommand
&&
2540 cmd
->proc
!= multiCommand
&& cmd
->proc
!= watchCommand
)
2542 queueMultiCommand(c
,cmd
);
2543 addReply(c
,shared
.queued
);
2545 if (server
.vm_enabled
&& server
.vm_max_threads
> 0 &&
2546 blockClientOnSwappedKeys(c
,cmd
)) return 1;
2550 /* Prepare the client for the next command */
2555 static void replicationFeedSlaves(list
*slaves
, int dictid
, robj
**argv
, int argc
) {
2560 /* We need 1+(ARGS*3) objects since commands are using the new protocol
2561 * and we one 1 object for the first "*<count>\r\n" multibulk count, then
2562 * for every additional object we have "$<count>\r\n" + object + "\r\n". */
2563 robj
*static_outv
[REDIS_STATIC_ARGS
*3+1];
2566 if (argc
<= REDIS_STATIC_ARGS
) {
2569 outv
= zmalloc(sizeof(robj
*)*(argc
*3+1));
2572 lenobj
= createObject(REDIS_STRING
,
2573 sdscatprintf(sdsempty(), "*%d\r\n", argc
));
2574 lenobj
->refcount
= 0;
2575 outv
[outc
++] = lenobj
;
2576 for (j
= 0; j
< argc
; j
++) {
2577 lenobj
= createObject(REDIS_STRING
,
2578 sdscatprintf(sdsempty(),"$%lu\r\n",
2579 (unsigned long) stringObjectLen(argv
[j
])));
2580 lenobj
->refcount
= 0;
2581 outv
[outc
++] = lenobj
;
2582 outv
[outc
++] = argv
[j
];
2583 outv
[outc
++] = shared
.crlf
;
2586 /* Increment all the refcounts at start and decrement at end in order to
2587 * be sure to free objects if there is no slave in a replication state
2588 * able to be feed with commands */
2589 for (j
= 0; j
< outc
; j
++) incrRefCount(outv
[j
]);
2590 listRewind(slaves
,&li
);
2591 while((ln
= listNext(&li
))) {
2592 redisClient
*slave
= ln
->value
;
2594 /* Don't feed slaves that are still waiting for BGSAVE to start */
2595 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
) continue;
2597 /* Feed all the other slaves, MONITORs and so on */
2598 if (slave
->slaveseldb
!= dictid
) {
2602 case 0: selectcmd
= shared
.select0
; break;
2603 case 1: selectcmd
= shared
.select1
; break;
2604 case 2: selectcmd
= shared
.select2
; break;
2605 case 3: selectcmd
= shared
.select3
; break;
2606 case 4: selectcmd
= shared
.select4
; break;
2607 case 5: selectcmd
= shared
.select5
; break;
2608 case 6: selectcmd
= shared
.select6
; break;
2609 case 7: selectcmd
= shared
.select7
; break;
2610 case 8: selectcmd
= shared
.select8
; break;
2611 case 9: selectcmd
= shared
.select9
; break;
2613 selectcmd
= createObject(REDIS_STRING
,
2614 sdscatprintf(sdsempty(),"select %d\r\n",dictid
));
2615 selectcmd
->refcount
= 0;
2618 addReply(slave
,selectcmd
);
2619 slave
->slaveseldb
= dictid
;
2621 for (j
= 0; j
< outc
; j
++) addReply(slave
,outv
[j
]);
2623 for (j
= 0; j
< outc
; j
++) decrRefCount(outv
[j
]);
2624 if (outv
!= static_outv
) zfree(outv
);
2627 static sds
sdscatrepr(sds s
, char *p
, size_t len
) {
2628 s
= sdscatlen(s
,"\"",1);
2633 s
= sdscatprintf(s
,"\\%c",*p
);
2635 case '\n': s
= sdscatlen(s
,"\\n",1); break;
2636 case '\r': s
= sdscatlen(s
,"\\r",1); break;
2637 case '\t': s
= sdscatlen(s
,"\\t",1); break;
2638 case '\a': s
= sdscatlen(s
,"\\a",1); break;
2639 case '\b': s
= sdscatlen(s
,"\\b",1); break;
2642 s
= sdscatprintf(s
,"%c",*p
);
2644 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
2649 return sdscatlen(s
,"\"",1);
2652 static void replicationFeedMonitors(list
*monitors
, int dictid
, robj
**argv
, int argc
) {
2656 sds cmdrepr
= sdsnew("+");
2660 gettimeofday(&tv
,NULL
);
2661 cmdrepr
= sdscatprintf(cmdrepr
,"%ld.%ld ",(long)tv
.tv_sec
,(long)tv
.tv_usec
);
2662 if (dictid
!= 0) cmdrepr
= sdscatprintf(cmdrepr
,"(db %d) ", dictid
);
2664 for (j
= 0; j
< argc
; j
++) {
2665 if (argv
[j
]->encoding
== REDIS_ENCODING_INT
) {
2666 cmdrepr
= sdscatprintf(cmdrepr
, "%ld", (long)argv
[j
]->ptr
);
2668 cmdrepr
= sdscatrepr(cmdrepr
,(char*)argv
[j
]->ptr
,
2669 sdslen(argv
[j
]->ptr
));
2672 cmdrepr
= sdscatlen(cmdrepr
," ",1);
2674 cmdrepr
= sdscatlen(cmdrepr
,"\r\n",2);
2675 cmdobj
= createObject(REDIS_STRING
,cmdrepr
);
2677 listRewind(monitors
,&li
);
2678 while((ln
= listNext(&li
))) {
2679 redisClient
*monitor
= ln
->value
;
2680 addReply(monitor
,cmdobj
);
2682 decrRefCount(cmdobj
);
2685 static void processInputBuffer(redisClient
*c
) {
2687 /* Before to process the input buffer, make sure the client is not
2688 * waitig for a blocking operation such as BLPOP. Note that the first
2689 * iteration the client is never blocked, otherwise the processInputBuffer
2690 * would not be called at all, but after the execution of the first commands
2691 * in the input buffer the client may be blocked, and the "goto again"
2692 * will try to reiterate. The following line will make it return asap. */
2693 if (c
->flags
& REDIS_BLOCKED
|| c
->flags
& REDIS_IO_WAIT
) return;
2694 if (c
->bulklen
== -1) {
2695 /* Read the first line of the query */
2696 char *p
= strchr(c
->querybuf
,'\n');
2703 query
= c
->querybuf
;
2704 c
->querybuf
= sdsempty();
2705 querylen
= 1+(p
-(query
));
2706 if (sdslen(query
) > querylen
) {
2707 /* leave data after the first line of the query in the buffer */
2708 c
->querybuf
= sdscatlen(c
->querybuf
,query
+querylen
,sdslen(query
)-querylen
);
2710 *p
= '\0'; /* remove "\n" */
2711 if (*(p
-1) == '\r') *(p
-1) = '\0'; /* and "\r" if any */
2712 sdsupdatelen(query
);
2714 /* Now we can split the query in arguments */
2715 argv
= sdssplitlen(query
,sdslen(query
)," ",1,&argc
);
2718 if (c
->argv
) zfree(c
->argv
);
2719 c
->argv
= zmalloc(sizeof(robj
*)*argc
);
2721 for (j
= 0; j
< argc
; j
++) {
2722 if (sdslen(argv
[j
])) {
2723 c
->argv
[c
->argc
] = createObject(REDIS_STRING
,argv
[j
]);
2731 /* Execute the command. If the client is still valid
2732 * after processCommand() return and there is something
2733 * on the query buffer try to process the next command. */
2734 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
2736 /* Nothing to process, argc == 0. Just process the query
2737 * buffer if it's not empty or return to the caller */
2738 if (sdslen(c
->querybuf
)) goto again
;
2741 } else if (sdslen(c
->querybuf
) >= REDIS_REQUEST_MAX_SIZE
) {
2742 redisLog(REDIS_VERBOSE
, "Client protocol error");
2747 /* Bulk read handling. Note that if we are at this point
2748 the client already sent a command terminated with a newline,
2749 we are reading the bulk data that is actually the last
2750 argument of the command. */
2751 int qbl
= sdslen(c
->querybuf
);
2753 if (c
->bulklen
<= qbl
) {
2754 /* Copy everything but the final CRLF as final argument */
2755 c
->argv
[c
->argc
] = createStringObject(c
->querybuf
,c
->bulklen
-2);
2757 c
->querybuf
= sdsrange(c
->querybuf
,c
->bulklen
,-1);
2758 /* Process the command. If the client is still valid after
2759 * the processing and there is more data in the buffer
2760 * try to parse it. */
2761 if (processCommand(c
) && sdslen(c
->querybuf
)) goto again
;
2767 static void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
2768 redisClient
*c
= (redisClient
*) privdata
;
2769 char buf
[REDIS_IOBUF_LEN
];
2772 REDIS_NOTUSED(mask
);
2774 nread
= read(fd
, buf
, REDIS_IOBUF_LEN
);
2776 if (errno
== EAGAIN
) {
2779 redisLog(REDIS_VERBOSE
, "Reading from client: %s",strerror(errno
));
2783 } else if (nread
== 0) {
2784 redisLog(REDIS_VERBOSE
, "Client closed connection");
2789 c
->querybuf
= sdscatlen(c
->querybuf
, buf
, nread
);
2790 c
->lastinteraction
= time(NULL
);
2794 processInputBuffer(c
);
2797 static int selectDb(redisClient
*c
, int id
) {
2798 if (id
< 0 || id
>= server
.dbnum
)
2800 c
->db
= &server
.db
[id
];
2804 static void *dupClientReplyValue(void *o
) {
2805 incrRefCount((robj
*)o
);
2809 static int listMatchObjects(void *a
, void *b
) {
2810 return equalStringObjects(a
,b
);
2813 static redisClient
*createClient(int fd
) {
2814 redisClient
*c
= zmalloc(sizeof(*c
));
2816 anetNonBlock(NULL
,fd
);
2817 anetTcpNoDelay(NULL
,fd
);
2818 if (!c
) return NULL
;
2821 c
->querybuf
= sdsempty();
2830 c
->lastinteraction
= time(NULL
);
2831 c
->authenticated
= 0;
2832 c
->replstate
= REDIS_REPL_NONE
;
2833 c
->reply
= listCreate();
2834 listSetFreeMethod(c
->reply
,decrRefCount
);
2835 listSetDupMethod(c
->reply
,dupClientReplyValue
);
2836 c
->blocking_keys
= NULL
;
2837 c
->blocking_keys_num
= 0;
2838 c
->io_keys
= listCreate();
2839 c
->watched_keys
= listCreate();
2840 listSetFreeMethod(c
->io_keys
,decrRefCount
);
2841 c
->pubsub_channels
= dictCreate(&setDictType
,NULL
);
2842 c
->pubsub_patterns
= listCreate();
2843 listSetFreeMethod(c
->pubsub_patterns
,decrRefCount
);
2844 listSetMatchMethod(c
->pubsub_patterns
,listMatchObjects
);
2845 if (aeCreateFileEvent(server
.el
, c
->fd
, AE_READABLE
,
2846 readQueryFromClient
, c
) == AE_ERR
) {
2850 listAddNodeTail(server
.clients
,c
);
2851 initClientMultiState(c
);
2855 static void addReply(redisClient
*c
, robj
*obj
) {
2856 if (listLength(c
->reply
) == 0 &&
2857 (c
->replstate
== REDIS_REPL_NONE
||
2858 c
->replstate
== REDIS_REPL_ONLINE
) &&
2859 aeCreateFileEvent(server
.el
, c
->fd
, AE_WRITABLE
,
2860 sendReplyToClient
, c
) == AE_ERR
) return;
2862 if (server
.vm_enabled
&& obj
->storage
!= REDIS_VM_MEMORY
) {
2863 obj
= dupStringObject(obj
);
2864 obj
->refcount
= 0; /* getDecodedObject() will increment the refcount */
2866 listAddNodeTail(c
->reply
,getDecodedObject(obj
));
2869 static void addReplySds(redisClient
*c
, sds s
) {
2870 robj
*o
= createObject(REDIS_STRING
,s
);
2875 static void addReplyDouble(redisClient
*c
, double d
) {
2878 snprintf(buf
,sizeof(buf
),"%.17g",d
);
2879 addReplySds(c
,sdscatprintf(sdsempty(),"$%lu\r\n%s\r\n",
2880 (unsigned long) strlen(buf
),buf
));
2883 static void addReplyLongLong(redisClient
*c
, long long ll
) {
2888 addReply(c
,shared
.czero
);
2890 } else if (ll
== 1) {
2891 addReply(c
,shared
.cone
);
2895 len
= ll2string(buf
+1,sizeof(buf
)-1,ll
);
2898 addReplySds(c
,sdsnewlen(buf
,len
+3));
2901 static void addReplyUlong(redisClient
*c
, unsigned long ul
) {
2906 addReply(c
,shared
.czero
);
2908 } else if (ul
== 1) {
2909 addReply(c
,shared
.cone
);
2912 len
= snprintf(buf
,sizeof(buf
),":%lu\r\n",ul
);
2913 addReplySds(c
,sdsnewlen(buf
,len
));
2916 static void addReplyBulkLen(redisClient
*c
, robj
*obj
) {
2920 if (obj
->encoding
== REDIS_ENCODING_RAW
) {
2921 len
= sdslen(obj
->ptr
);
2923 long n
= (long)obj
->ptr
;
2925 /* Compute how many bytes will take this integer as a radix 10 string */
2931 while((n
= n
/10) != 0) {
2936 intlen
= ll2string(buf
+1,sizeof(buf
)-1,(long long)len
);
2937 buf
[intlen
+1] = '\r';
2938 buf
[intlen
+2] = '\n';
2939 addReplySds(c
,sdsnewlen(buf
,intlen
+3));
2942 static void addReplyBulk(redisClient
*c
, robj
*obj
) {
2943 addReplyBulkLen(c
,obj
);
2945 addReply(c
,shared
.crlf
);
2948 static void addReplyBulkSds(redisClient
*c
, sds s
) {
2949 robj
*o
= createStringObject(s
, sdslen(s
));
2954 /* In the CONFIG command we need to add vanilla C string as bulk replies */
2955 static void addReplyBulkCString(redisClient
*c
, char *s
) {
2957 addReply(c
,shared
.nullbulk
);
2959 robj
*o
= createStringObject(s
,strlen(s
));
2965 static void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
2970 REDIS_NOTUSED(mask
);
2971 REDIS_NOTUSED(privdata
);
2973 cfd
= anetAccept(server
.neterr
, fd
, cip
, &cport
);
2974 if (cfd
== AE_ERR
) {
2975 redisLog(REDIS_VERBOSE
,"Accepting client connection: %s", server
.neterr
);
2978 redisLog(REDIS_VERBOSE
,"Accepted %s:%d", cip
, cport
);
2979 if ((c
= createClient(cfd
)) == NULL
) {
2980 redisLog(REDIS_WARNING
,"Error allocating resoures for the client");
2981 close(cfd
); /* May be already closed, just ingore errors */
2984 /* If maxclient directive is set and this is one client more... close the
2985 * connection. Note that we create the client instead to check before
2986 * for this condition, since now the socket is already set in nonblocking
2987 * mode and we can send an error for free using the Kernel I/O */
2988 if (server
.maxclients
&& listLength(server
.clients
) > server
.maxclients
) {
2989 char *err
= "-ERR max number of clients reached\r\n";
2991 /* That's a best effort error message, don't check write errors */
2992 if (write(c
->fd
,err
,strlen(err
)) == -1) {
2993 /* Nothing to do, Just to avoid the warning... */
2998 server
.stat_numconnections
++;
3001 /* ======================= Redis objects implementation ===================== */
3003 static robj
*createObject(int type
, void *ptr
) {
3006 if (server
.vm_enabled
) pthread_mutex_lock(&server
.obj_freelist_mutex
);
3007 if (listLength(server
.objfreelist
)) {
3008 listNode
*head
= listFirst(server
.objfreelist
);
3009 o
= listNodeValue(head
);
3010 listDelNode(server
.objfreelist
,head
);
3011 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
3013 if (server
.vm_enabled
)
3014 pthread_mutex_unlock(&server
.obj_freelist_mutex
);
3015 o
= zmalloc(sizeof(*o
));
3018 o
->encoding
= REDIS_ENCODING_RAW
;
3021 if (server
.vm_enabled
) {
3022 /* Note that this code may run in the context of an I/O thread
3023 * and accessing server.lruclock in theory is an error
3024 * (no locks). But in practice this is safe, and even if we read
3025 * garbage Redis will not fail. */
3026 o
->lru
= server
.lruclock
;
3027 o
->storage
= REDIS_VM_MEMORY
;
3032 static robj
*createStringObject(char *ptr
, size_t len
) {
3033 return createObject(REDIS_STRING
,sdsnewlen(ptr
,len
));
3036 static robj
*createStringObjectFromLongLong(long long value
) {
3038 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
3039 incrRefCount(shared
.integers
[value
]);
3040 o
= shared
.integers
[value
];
3042 if (value
>= LONG_MIN
&& value
<= LONG_MAX
) {
3043 o
= createObject(REDIS_STRING
, NULL
);
3044 o
->encoding
= REDIS_ENCODING_INT
;
3045 o
->ptr
= (void*)((long)value
);
3047 o
= createObject(REDIS_STRING
,sdsfromlonglong(value
));
3053 static robj
*dupStringObject(robj
*o
) {
3054 assert(o
->encoding
== REDIS_ENCODING_RAW
);
3055 return createStringObject(o
->ptr
,sdslen(o
->ptr
));
3058 static robj
*createListObject(void) {
3059 list
*l
= listCreate();
3060 robj
*o
= createObject(REDIS_LIST
,l
);
3061 listSetFreeMethod(l
,decrRefCount
);
3062 o
->encoding
= REDIS_ENCODING_LINKEDLIST
;
3066 static robj
*createZiplistObject(void) {
3067 unsigned char *zl
= ziplistNew();
3068 robj
*o
= createObject(REDIS_LIST
,zl
);
3069 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
3073 static robj
*createSetObject(void) {
3074 dict
*d
= dictCreate(&setDictType
,NULL
);
3075 return createObject(REDIS_SET
,d
);
3078 static robj
*createHashObject(void) {
3079 /* All the Hashes start as zipmaps. Will be automatically converted
3080 * into hash tables if there are enough elements or big elements
3082 unsigned char *zm
= zipmapNew();
3083 robj
*o
= createObject(REDIS_HASH
,zm
);
3084 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
3088 static robj
*createZsetObject(void) {
3089 zset
*zs
= zmalloc(sizeof(*zs
));
3091 zs
->dict
= dictCreate(&zsetDictType
,NULL
);
3092 zs
->zsl
= zslCreate();
3093 return createObject(REDIS_ZSET
,zs
);
3096 static void freeStringObject(robj
*o
) {
3097 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3102 static void freeListObject(robj
*o
) {
3103 switch (o
->encoding
) {
3104 case REDIS_ENCODING_LINKEDLIST
:
3105 listRelease((list
*) o
->ptr
);
3107 case REDIS_ENCODING_ZIPLIST
:
3111 redisPanic("Unknown list encoding type");
3115 static void freeSetObject(robj
*o
) {
3116 dictRelease((dict
*) o
->ptr
);
3119 static void freeZsetObject(robj
*o
) {
3122 dictRelease(zs
->dict
);
3127 static void freeHashObject(robj
*o
) {
3128 switch (o
->encoding
) {
3129 case REDIS_ENCODING_HT
:
3130 dictRelease((dict
*) o
->ptr
);
3132 case REDIS_ENCODING_ZIPMAP
:
3136 redisPanic("Unknown hash encoding type");
3141 static void incrRefCount(robj
*o
) {
3145 static void decrRefCount(void *obj
) {
3148 /* Object is a swapped out value, or in the process of being loaded. */
3149 if (server
.vm_enabled
&&
3150 (o
->storage
== REDIS_VM_SWAPPED
|| o
->storage
== REDIS_VM_LOADING
))
3152 vmpointer
*vp
= obj
;
3153 if (o
->storage
== REDIS_VM_LOADING
) vmCancelThreadedIOJob(o
);
3154 vmMarkPagesFree(vp
->page
,vp
->usedpages
);
3155 server
.vm_stats_swapped_objects
--;
3160 if (o
->refcount
<= 0) redisPanic("decrRefCount against refcount <= 0");
3161 /* Object is in memory, or in the process of being swapped out.
3163 * If the object is being swapped out, abort the operation on
3164 * decrRefCount even if the refcount does not drop to 0: the object
3165 * is referenced at least two times, as value of the key AND as
3166 * job->val in the iojob. So if we don't invalidate the iojob, when it is
3167 * done but the relevant key was removed in the meantime, the
3168 * complete jobs handler will not find the key about the job and the
3169 * assert will fail. */
3170 if (server
.vm_enabled
&& o
->storage
== REDIS_VM_SWAPPING
)
3171 vmCancelThreadedIOJob(o
);
3172 if (--(o
->refcount
) == 0) {
3174 case REDIS_STRING
: freeStringObject(o
); break;
3175 case REDIS_LIST
: freeListObject(o
); break;
3176 case REDIS_SET
: freeSetObject(o
); break;
3177 case REDIS_ZSET
: freeZsetObject(o
); break;
3178 case REDIS_HASH
: freeHashObject(o
); break;
3179 default: redisPanic("Unknown object type"); break;
3181 if (server
.vm_enabled
) pthread_mutex_lock(&server
.obj_freelist_mutex
);
3182 if (listLength(server
.objfreelist
) > REDIS_OBJFREELIST_MAX
||
3183 !listAddNodeHead(server
.objfreelist
,o
))
3185 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
3189 static int checkType(redisClient
*c
, robj
*o
, int type
) {
3190 if (o
->type
!= type
) {
3191 addReply(c
,shared
.wrongtypeerr
);
3197 /* Check if the nul-terminated string 's' can be represented by a long
3198 * (that is, is a number that fits into long without any other space or
3199 * character before or after the digits).
3201 * If so, the function returns REDIS_OK and *longval is set to the value
3202 * of the number. Otherwise REDIS_ERR is returned */
3203 static int isStringRepresentableAsLong(sds s
, long *longval
) {
3204 char buf
[32], *endptr
;
3208 value
= strtol(s
, &endptr
, 10);
3209 if (endptr
[0] != '\0') return REDIS_ERR
;
3210 slen
= ll2string(buf
,32,value
);
3212 /* If the number converted back into a string is not identical
3213 * then it's not possible to encode the string as integer */
3214 if (sdslen(s
) != (unsigned)slen
|| memcmp(buf
,s
,slen
)) return REDIS_ERR
;
3215 if (longval
) *longval
= value
;
3219 /* Try to encode a string object in order to save space */
3220 static robj
*tryObjectEncoding(robj
*o
) {
3224 if (o
->encoding
!= REDIS_ENCODING_RAW
)
3225 return o
; /* Already encoded */
3227 /* It's not safe to encode shared objects: shared objects can be shared
3228 * everywhere in the "object space" of Redis. Encoded objects can only
3229 * appear as "values" (and not, for instance, as keys) */
3230 if (o
->refcount
> 1) return o
;
3232 /* Currently we try to encode only strings */
3233 redisAssert(o
->type
== REDIS_STRING
);
3235 /* Check if we can represent this string as a long integer */
3236 if (isStringRepresentableAsLong(s
,&value
) == REDIS_ERR
) return o
;
3238 /* Ok, this object can be encoded */
3239 if (value
>= 0 && value
< REDIS_SHARED_INTEGERS
) {
3241 incrRefCount(shared
.integers
[value
]);
3242 return shared
.integers
[value
];
3244 o
->encoding
= REDIS_ENCODING_INT
;
3246 o
->ptr
= (void*) value
;
3251 /* Get a decoded version of an encoded object (returned as a new object).
3252 * If the object is already raw-encoded just increment the ref count. */
3253 static robj
*getDecodedObject(robj
*o
) {
3256 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3260 if (o
->type
== REDIS_STRING
&& o
->encoding
== REDIS_ENCODING_INT
) {
3263 ll2string(buf
,32,(long)o
->ptr
);
3264 dec
= createStringObject(buf
,strlen(buf
));
3267 redisPanic("Unknown encoding type");
3271 /* Compare two string objects via strcmp() or alike.
3272 * Note that the objects may be integer-encoded. In such a case we
3273 * use ll2string() to get a string representation of the numbers on the stack
3274 * and compare the strings, it's much faster than calling getDecodedObject().
3276 * Important note: if objects are not integer encoded, but binary-safe strings,
3277 * sdscmp() from sds.c will apply memcmp() so this function ca be considered
3279 static int compareStringObjects(robj
*a
, robj
*b
) {
3280 redisAssert(a
->type
== REDIS_STRING
&& b
->type
== REDIS_STRING
);
3281 char bufa
[128], bufb
[128], *astr
, *bstr
;
3284 if (a
== b
) return 0;
3285 if (a
->encoding
!= REDIS_ENCODING_RAW
) {
3286 ll2string(bufa
,sizeof(bufa
),(long) a
->ptr
);
3292 if (b
->encoding
!= REDIS_ENCODING_RAW
) {
3293 ll2string(bufb
,sizeof(bufb
),(long) b
->ptr
);
3299 return bothsds
? sdscmp(astr
,bstr
) : strcmp(astr
,bstr
);
3302 /* Equal string objects return 1 if the two objects are the same from the
3303 * point of view of a string comparison, otherwise 0 is returned. Note that
3304 * this function is faster then checking for (compareStringObject(a,b) == 0)
3305 * because it can perform some more optimization. */
3306 static int equalStringObjects(robj
*a
, robj
*b
) {
3307 if (a
->encoding
!= REDIS_ENCODING_RAW
&& b
->encoding
!= REDIS_ENCODING_RAW
){
3308 return a
->ptr
== b
->ptr
;
3310 return compareStringObjects(a
,b
) == 0;
3314 static size_t stringObjectLen(robj
*o
) {
3315 redisAssert(o
->type
== REDIS_STRING
);
3316 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3317 return sdslen(o
->ptr
);
3321 return ll2string(buf
,32,(long)o
->ptr
);
3325 static int getDoubleFromObject(robj
*o
, double *target
) {
3332 redisAssert(o
->type
== REDIS_STRING
);
3333 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3334 value
= strtod(o
->ptr
, &eptr
);
3335 if (eptr
[0] != '\0') return REDIS_ERR
;
3336 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
3337 value
= (long)o
->ptr
;
3339 redisPanic("Unknown string encoding");
3347 static int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
) {
3349 if (getDoubleFromObject(o
, &value
) != REDIS_OK
) {
3351 addReplySds(c
, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg
));
3353 addReplySds(c
, sdsnew("-ERR value is not a double\r\n"));
3362 static int getLongLongFromObject(robj
*o
, long long *target
) {
3369 redisAssert(o
->type
== REDIS_STRING
);
3370 if (o
->encoding
== REDIS_ENCODING_RAW
) {
3371 value
= strtoll(o
->ptr
, &eptr
, 10);
3372 if (eptr
[0] != '\0') return REDIS_ERR
;
3373 } else if (o
->encoding
== REDIS_ENCODING_INT
) {
3374 value
= (long)o
->ptr
;
3376 redisPanic("Unknown string encoding");
3384 static int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
) {
3386 if (getLongLongFromObject(o
, &value
) != REDIS_OK
) {
3388 addReplySds(c
, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg
));
3390 addReplySds(c
, sdsnew("-ERR value is not an integer\r\n"));
3399 static int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
) {
3402 if (getLongLongFromObjectOrReply(c
, o
, &value
, msg
) != REDIS_OK
) return REDIS_ERR
;
3403 if (value
< LONG_MIN
|| value
> LONG_MAX
) {
3405 addReplySds(c
, sdscatprintf(sdsempty(), "-ERR %s\r\n", msg
));
3407 addReplySds(c
, sdsnew("-ERR value is out of range\r\n"));
3416 /* =========================== Keyspace access API ========================== */
3418 static robj
*lookupKey(redisDb
*db
, robj
*key
) {
3419 dictEntry
*de
= dictFind(db
->dict
,key
->ptr
);
3421 robj
*val
= dictGetEntryVal(de
);
3423 if (server
.vm_enabled
) {
3424 if (val
->storage
== REDIS_VM_MEMORY
||
3425 val
->storage
== REDIS_VM_SWAPPING
)
3427 /* If we were swapping the object out, cancel the operation */
3428 if (val
->storage
== REDIS_VM_SWAPPING
)
3429 vmCancelThreadedIOJob(val
);
3430 /* Update the access time for the aging algorithm. */
3431 val
->lru
= server
.lruclock
;
3433 int notify
= (val
->storage
== REDIS_VM_LOADING
);
3435 /* Our value was swapped on disk. Bring it at home. */
3436 redisAssert(val
->type
== REDIS_VMPOINTER
);
3437 val
= vmLoadObject(val
);
3438 dictGetEntryVal(de
) = val
;
3440 /* Clients blocked by the VM subsystem may be waiting for
3442 if (notify
) handleClientsBlockedOnSwappedKey(db
,key
);
3451 static robj
*lookupKeyRead(redisDb
*db
, robj
*key
) {
3452 expireIfNeeded(db
,key
);
3453 return lookupKey(db
,key
);
3456 static robj
*lookupKeyWrite(redisDb
*db
, robj
*key
) {
3457 deleteIfVolatile(db
,key
);
3458 touchWatchedKey(db
,key
);
3459 return lookupKey(db
,key
);
3462 static robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
3463 robj
*o
= lookupKeyRead(c
->db
, key
);
3464 if (!o
) addReply(c
,reply
);
3468 static robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
) {
3469 robj
*o
= lookupKeyWrite(c
->db
, key
);
3470 if (!o
) addReply(c
,reply
);
3474 /* Add the key to the DB. If the key already exists REDIS_ERR is returned,
3475 * otherwise REDIS_OK is returned, and the caller should increment the
3476 * refcount of 'val'. */
3477 static int dbAdd(redisDb
*db
, robj
*key
, robj
*val
) {
3478 /* Perform a lookup before adding the key, as we need to copy the
3480 if (dictFind(db
->dict
, key
->ptr
) != NULL
) {
3483 sds copy
= sdsdup(key
->ptr
);
3484 dictAdd(db
->dict
, copy
, val
);
3489 /* If the key does not exist, this is just like dbAdd(). Otherwise
3490 * the value associated to the key is replaced with the new one.
3492 * On update (key already existed) 0 is returned. Otherwise 1. */
3493 static int dbReplace(redisDb
*db
, robj
*key
, robj
*val
) {
3494 if (dictFind(db
->dict
,key
->ptr
) == NULL
) {
3495 sds copy
= sdsdup(key
->ptr
);
3496 dictAdd(db
->dict
, copy
, val
);
3499 dictReplace(db
->dict
, key
->ptr
, val
);
3504 static int dbExists(redisDb
*db
, robj
*key
) {
3505 return dictFind(db
->dict
,key
->ptr
) != NULL
;
3508 /* Return a random key, in form of a Redis object.
3509 * If there are no keys, NULL is returned.
3511 * The function makes sure to return keys not already expired. */
3512 static robj
*dbRandomKey(redisDb
*db
) {
3513 struct dictEntry
*de
;
3519 de
= dictGetRandomKey(db
->dict
);
3520 if (de
== NULL
) return NULL
;
3522 key
= dictGetEntryKey(de
);
3523 keyobj
= createStringObject(key
,sdslen(key
));
3524 if (dictFind(db
->expires
,key
)) {
3525 if (expireIfNeeded(db
,keyobj
)) {
3526 decrRefCount(keyobj
);
3527 continue; /* search for another key. This expired. */
3534 /* Delete a key, value, and associated expiration entry if any, from the DB */
3535 static int dbDelete(redisDb
*db
, robj
*key
) {
3536 /* Deleting an entry from the expires dict will not free the sds of
3537 * the key, because it is shared with the main dictionary. */
3538 if (dictSize(db
->expires
) > 0) dictDelete(db
->expires
,key
->ptr
);
3539 return dictDelete(db
->dict
,key
->ptr
) == DICT_OK
;
3542 /*============================ RDB saving/loading =========================== */
3544 static int rdbSaveType(FILE *fp
, unsigned char type
) {
3545 if (fwrite(&type
,1,1,fp
) == 0) return -1;
3549 static int rdbSaveTime(FILE *fp
, time_t t
) {
3550 int32_t t32
= (int32_t) t
;
3551 if (fwrite(&t32
,4,1,fp
) == 0) return -1;
3555 /* check rdbLoadLen() comments for more info */
3556 static int rdbSaveLen(FILE *fp
, uint32_t len
) {
3557 unsigned char buf
[2];
3560 /* Save a 6 bit len */
3561 buf
[0] = (len
&0xFF)|(REDIS_RDB_6BITLEN
<<6);
3562 if (fwrite(buf
,1,1,fp
) == 0) return -1;
3563 } else if (len
< (1<<14)) {
3564 /* Save a 14 bit len */
3565 buf
[0] = ((len
>>8)&0xFF)|(REDIS_RDB_14BITLEN
<<6);
3567 if (fwrite(buf
,2,1,fp
) == 0) return -1;
3569 /* Save a 32 bit len */
3570 buf
[0] = (REDIS_RDB_32BITLEN
<<6);
3571 if (fwrite(buf
,1,1,fp
) == 0) return -1;
3573 if (fwrite(&len
,4,1,fp
) == 0) return -1;
3578 /* Encode 'value' as an integer if possible (if integer will fit the
3579 * supported range). If the function sucessful encoded the integer
3580 * then the (up to 5 bytes) encoded representation is written in the
3581 * string pointed by 'enc' and the length is returned. Otherwise
3583 static int rdbEncodeInteger(long long value
, unsigned char *enc
) {
3584 /* Finally check if it fits in our ranges */
3585 if (value
>= -(1<<7) && value
<= (1<<7)-1) {
3586 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT8
;
3587 enc
[1] = value
&0xFF;
3589 } else if (value
>= -(1<<15) && value
<= (1<<15)-1) {
3590 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT16
;
3591 enc
[1] = value
&0xFF;
3592 enc
[2] = (value
>>8)&0xFF;
3594 } else if (value
>= -((long long)1<<31) && value
<= ((long long)1<<31)-1) {
3595 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT32
;
3596 enc
[1] = value
&0xFF;
3597 enc
[2] = (value
>>8)&0xFF;
3598 enc
[3] = (value
>>16)&0xFF;
3599 enc
[4] = (value
>>24)&0xFF;
3606 /* String objects in the form "2391" "-100" without any space and with a
3607 * range of values that can fit in an 8, 16 or 32 bit signed value can be
3608 * encoded as integers to save space */
3609 static int rdbTryIntegerEncoding(char *s
, size_t len
, unsigned char *enc
) {
3611 char *endptr
, buf
[32];
3613 /* Check if it's possible to encode this value as a number */
3614 value
= strtoll(s
, &endptr
, 10);
3615 if (endptr
[0] != '\0') return 0;
3616 ll2string(buf
,32,value
);
3618 /* If the number converted back into a string is not identical
3619 * then it's not possible to encode the string as integer */
3620 if (strlen(buf
) != len
|| memcmp(buf
,s
,len
)) return 0;
3622 return rdbEncodeInteger(value
,enc
);
3625 static int rdbSaveLzfStringObject(FILE *fp
, unsigned char *s
, size_t len
) {
3626 size_t comprlen
, outlen
;
3630 /* We require at least four bytes compression for this to be worth it */
3631 if (len
<= 4) return 0;
3633 if ((out
= zmalloc(outlen
+1)) == NULL
) return 0;
3634 comprlen
= lzf_compress(s
, len
, out
, outlen
);
3635 if (comprlen
== 0) {
3639 /* Data compressed! Let's save it on disk */
3640 byte
= (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_LZF
;
3641 if (fwrite(&byte
,1,1,fp
) == 0) goto writeerr
;
3642 if (rdbSaveLen(fp
,comprlen
) == -1) goto writeerr
;
3643 if (rdbSaveLen(fp
,len
) == -1) goto writeerr
;
3644 if (fwrite(out
,comprlen
,1,fp
) == 0) goto writeerr
;
3653 /* Save a string objet as [len][data] on disk. If the object is a string
3654 * representation of an integer value we try to safe it in a special form */
3655 static int rdbSaveRawString(FILE *fp
, unsigned char *s
, size_t len
) {
3658 /* Try integer encoding */
3660 unsigned char buf
[5];
3661 if ((enclen
= rdbTryIntegerEncoding((char*)s
,len
,buf
)) > 0) {
3662 if (fwrite(buf
,enclen
,1,fp
) == 0) return -1;
3667 /* Try LZF compression - under 20 bytes it's unable to compress even
3668 * aaaaaaaaaaaaaaaaaa so skip it */
3669 if (server
.rdbcompression
&& len
> 20) {
3672 retval
= rdbSaveLzfStringObject(fp
,s
,len
);
3673 if (retval
== -1) return -1;
3674 if (retval
> 0) return 0;
3675 /* retval == 0 means data can't be compressed, save the old way */
3678 /* Store verbatim */
3679 if (rdbSaveLen(fp
,len
) == -1) return -1;
3680 if (len
&& fwrite(s
,len
,1,fp
) == 0) return -1;
3684 /* Save a long long value as either an encoded string or a string. */
3685 static int rdbSaveLongLongAsStringObject(FILE *fp
, long long value
) {
3686 unsigned char buf
[32];
3687 int enclen
= rdbEncodeInteger(value
,buf
);
3689 if (fwrite(buf
,enclen
,1,fp
) == 0) return -1;
3691 /* Encode as string */
3692 enclen
= ll2string((char*)buf
,32,value
);
3693 redisAssert(enclen
< 32);
3694 if (rdbSaveLen(fp
,enclen
) == -1) return -1;
3695 if (fwrite(buf
,enclen
,1,fp
) == 0) return -1;
3700 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
3701 static int rdbSaveStringObject(FILE *fp
, robj
*obj
) {
3702 /* Avoid to decode the object, then encode it again, if the
3703 * object is alrady integer encoded. */
3704 if (obj
->encoding
== REDIS_ENCODING_INT
) {
3705 return rdbSaveLongLongAsStringObject(fp
,(long)obj
->ptr
);
3707 redisAssert(obj
->encoding
== REDIS_ENCODING_RAW
);
3708 return rdbSaveRawString(fp
,obj
->ptr
,sdslen(obj
->ptr
));
3712 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
3713 * 8 bit integer specifing the length of the representation.
3714 * This 8 bit integer has special values in order to specify the following
3720 static int rdbSaveDoubleValue(FILE *fp
, double val
) {
3721 unsigned char buf
[128];
3727 } else if (!isfinite(val
)) {
3729 buf
[0] = (val
< 0) ? 255 : 254;
3731 #if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
3732 /* Check if the float is in a safe range to be casted into a
3733 * long long. We are assuming that long long is 64 bit here.
3734 * Also we are assuming that there are no implementations around where
3735 * double has precision < 52 bit.
3737 * Under this assumptions we test if a double is inside an interval
3738 * where casting to long long is safe. Then using two castings we
3739 * make sure the decimal part is zero. If all this is true we use
3740 * integer printing function that is much faster. */
3741 double min
= -4503599627370495; /* (2^52)-1 */
3742 double max
= 4503599627370496; /* -(2^52) */
3743 if (val
> min
&& val
< max
&& val
== ((double)((long long)val
)))
3744 ll2string((char*)buf
+1,sizeof(buf
),(long long)val
);
3747 snprintf((char*)buf
+1,sizeof(buf
)-1,"%.17g",val
);
3748 buf
[0] = strlen((char*)buf
+1);
3751 if (fwrite(buf
,len
,1,fp
) == 0) return -1;
3755 /* Save a Redis object. */
3756 static int rdbSaveObject(FILE *fp
, robj
*o
) {
3757 if (o
->type
== REDIS_STRING
) {
3758 /* Save a string value */
3759 if (rdbSaveStringObject(fp
,o
) == -1) return -1;
3760 } else if (o
->type
== REDIS_LIST
) {
3761 /* Save a list value */
3762 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
3764 unsigned char *vstr
;
3768 if (rdbSaveLen(fp
,ziplistLen(o
->ptr
)) == -1) return -1;
3769 p
= ziplistIndex(o
->ptr
,0);
3770 while(ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
3772 if (rdbSaveRawString(fp
,vstr
,vlen
) == -1)
3775 if (rdbSaveLongLongAsStringObject(fp
,vlong
) == -1)
3778 p
= ziplistNext(o
->ptr
,p
);
3780 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
3781 list
*list
= o
->ptr
;
3785 if (rdbSaveLen(fp
,listLength(list
)) == -1) return -1;
3786 listRewind(list
,&li
);
3787 while((ln
= listNext(&li
))) {
3788 robj
*eleobj
= listNodeValue(ln
);
3789 if (rdbSaveStringObject(fp
,eleobj
) == -1) return -1;
3792 redisPanic("Unknown list encoding");
3794 } else if (o
->type
== REDIS_SET
) {
3795 /* Save a set value */
3797 dictIterator
*di
= dictGetIterator(set
);
3800 if (rdbSaveLen(fp
,dictSize(set
)) == -1) return -1;
3801 while((de
= dictNext(di
)) != NULL
) {
3802 robj
*eleobj
= dictGetEntryKey(de
);
3804 if (rdbSaveStringObject(fp
,eleobj
) == -1) return -1;
3806 dictReleaseIterator(di
);
3807 } else if (o
->type
== REDIS_ZSET
) {
3808 /* Save a set value */
3810 dictIterator
*di
= dictGetIterator(zs
->dict
);
3813 if (rdbSaveLen(fp
,dictSize(zs
->dict
)) == -1) return -1;
3814 while((de
= dictNext(di
)) != NULL
) {
3815 robj
*eleobj
= dictGetEntryKey(de
);
3816 double *score
= dictGetEntryVal(de
);
3818 if (rdbSaveStringObject(fp
,eleobj
) == -1) return -1;
3819 if (rdbSaveDoubleValue(fp
,*score
) == -1) return -1;
3821 dictReleaseIterator(di
);
3822 } else if (o
->type
== REDIS_HASH
) {
3823 /* Save a hash value */
3824 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
3825 unsigned char *p
= zipmapRewind(o
->ptr
);
3826 unsigned int count
= zipmapLen(o
->ptr
);
3827 unsigned char *key
, *val
;
3828 unsigned int klen
, vlen
;
3830 if (rdbSaveLen(fp
,count
) == -1) return -1;
3831 while((p
= zipmapNext(p
,&key
,&klen
,&val
,&vlen
)) != NULL
) {
3832 if (rdbSaveRawString(fp
,key
,klen
) == -1) return -1;
3833 if (rdbSaveRawString(fp
,val
,vlen
) == -1) return -1;
3836 dictIterator
*di
= dictGetIterator(o
->ptr
);
3839 if (rdbSaveLen(fp
,dictSize((dict
*)o
->ptr
)) == -1) return -1;
3840 while((de
= dictNext(di
)) != NULL
) {
3841 robj
*key
= dictGetEntryKey(de
);
3842 robj
*val
= dictGetEntryVal(de
);
3844 if (rdbSaveStringObject(fp
,key
) == -1) return -1;
3845 if (rdbSaveStringObject(fp
,val
) == -1) return -1;
3847 dictReleaseIterator(di
);
3850 redisPanic("Unknown object type");
3855 /* Return the length the object will have on disk if saved with
3856 * the rdbSaveObject() function. Currently we use a trick to get
3857 * this length with very little changes to the code. In the future
3858 * we could switch to a faster solution. */
3859 static off_t
rdbSavedObjectLen(robj
*o
, FILE *fp
) {
3860 if (fp
== NULL
) fp
= server
.devnull
;
3862 assert(rdbSaveObject(fp
,o
) != 1);
3866 /* Return the number of pages required to save this object in the swap file */
3867 static off_t
rdbSavedObjectPages(robj
*o
, FILE *fp
) {
3868 off_t bytes
= rdbSavedObjectLen(o
,fp
);
3870 return (bytes
+(server
.vm_page_size
-1))/server
.vm_page_size
;
3873 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
3874 static int rdbSave(char *filename
) {
3875 dictIterator
*di
= NULL
;
3880 time_t now
= time(NULL
);
3882 /* Wait for I/O therads to terminate, just in case this is a
3883 * foreground-saving, to avoid seeking the swap file descriptor at the
3885 if (server
.vm_enabled
)
3886 waitEmptyIOJobsQueue();
3888 snprintf(tmpfile
,256,"temp-%d.rdb", (int) getpid());
3889 fp
= fopen(tmpfile
,"w");
3891 redisLog(REDIS_WARNING
, "Failed saving the DB: %s", strerror(errno
));
3894 if (fwrite("REDIS0001",9,1,fp
) == 0) goto werr
;
3895 for (j
= 0; j
< server
.dbnum
; j
++) {
3896 redisDb
*db
= server
.db
+j
;
3898 if (dictSize(d
) == 0) continue;
3899 di
= dictGetIterator(d
);
3905 /* Write the SELECT DB opcode */
3906 if (rdbSaveType(fp
,REDIS_SELECTDB
) == -1) goto werr
;
3907 if (rdbSaveLen(fp
,j
) == -1) goto werr
;
3909 /* Iterate this DB writing every entry */
3910 while((de
= dictNext(di
)) != NULL
) {
3911 sds keystr
= dictGetEntryKey(de
);
3912 robj key
, *o
= dictGetEntryVal(de
);
3915 initStaticStringObject(key
,keystr
);
3916 expiretime
= getExpire(db
,&key
);
3918 /* Save the expire time */
3919 if (expiretime
!= -1) {
3920 /* If this key is already expired skip it */
3921 if (expiretime
< now
) continue;
3922 if (rdbSaveType(fp
,REDIS_EXPIRETIME
) == -1) goto werr
;
3923 if (rdbSaveTime(fp
,expiretime
) == -1) goto werr
;
3925 /* Save the key and associated value. This requires special
3926 * handling if the value is swapped out. */
3927 if (!server
.vm_enabled
|| o
->storage
== REDIS_VM_MEMORY
||
3928 o
->storage
== REDIS_VM_SWAPPING
) {
3929 /* Save type, key, value */
3930 if (rdbSaveType(fp
,o
->type
) == -1) goto werr
;
3931 if (rdbSaveStringObject(fp
,&key
) == -1) goto werr
;
3932 if (rdbSaveObject(fp
,o
) == -1) goto werr
;
3934 /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
3936 /* Get a preview of the object in memory */
3937 po
= vmPreviewObject(o
);
3938 /* Save type, key, value */
3939 if (rdbSaveType(fp
,po
->type
) == -1) goto werr
;
3940 if (rdbSaveStringObject(fp
,&key
) == -1) goto werr
;
3941 if (rdbSaveObject(fp
,po
) == -1) goto werr
;
3942 /* Remove the loaded object from memory */
3946 dictReleaseIterator(di
);
3949 if (rdbSaveType(fp
,REDIS_EOF
) == -1) goto werr
;
3951 /* Make sure data will not remain on the OS's output buffers */
3956 /* Use RENAME to make sure the DB file is changed atomically only
3957 * if the generate DB file is ok. */
3958 if (rename(tmpfile
,filename
) == -1) {
3959 redisLog(REDIS_WARNING
,"Error moving temp DB file on the final destination: %s", strerror(errno
));
3963 redisLog(REDIS_NOTICE
,"DB saved on disk");
3965 server
.lastsave
= time(NULL
);
3971 redisLog(REDIS_WARNING
,"Write error saving DB on disk: %s", strerror(errno
));
3972 if (di
) dictReleaseIterator(di
);
3976 static int rdbSaveBackground(char *filename
) {
3979 if (server
.bgsavechildpid
!= -1) return REDIS_ERR
;
3980 if (server
.vm_enabled
) waitEmptyIOJobsQueue();
3981 if ((childpid
= fork()) == 0) {
3983 if (server
.vm_enabled
) vmReopenSwapFile();
3985 if (rdbSave(filename
) == REDIS_OK
) {
3992 if (childpid
== -1) {
3993 redisLog(REDIS_WARNING
,"Can't save in background: fork: %s",
3997 redisLog(REDIS_NOTICE
,"Background saving started by pid %d",childpid
);
3998 server
.bgsavechildpid
= childpid
;
3999 updateDictResizePolicy();
4002 return REDIS_OK
; /* unreached */
4005 static void rdbRemoveTempFile(pid_t childpid
) {
4008 snprintf(tmpfile
,256,"temp-%d.rdb", (int) childpid
);
4012 static int rdbLoadType(FILE *fp
) {
4014 if (fread(&type
,1,1,fp
) == 0) return -1;
4018 static time_t rdbLoadTime(FILE *fp
) {
4020 if (fread(&t32
,4,1,fp
) == 0) return -1;
4021 return (time_t) t32
;
4024 /* Load an encoded length from the DB, see the REDIS_RDB_* defines on the top
4025 * of this file for a description of how this are stored on disk.
4027 * isencoded is set to 1 if the readed length is not actually a length but
4028 * an "encoding type", check the above comments for more info */
4029 static uint32_t rdbLoadLen(FILE *fp
, int *isencoded
) {
4030 unsigned char buf
[2];
4034 if (isencoded
) *isencoded
= 0;
4035 if (fread(buf
,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
4036 type
= (buf
[0]&0xC0)>>6;
4037 if (type
== REDIS_RDB_6BITLEN
) {
4038 /* Read a 6 bit len */
4040 } else if (type
== REDIS_RDB_ENCVAL
) {
4041 /* Read a 6 bit len encoding type */
4042 if (isencoded
) *isencoded
= 1;
4044 } else if (type
== REDIS_RDB_14BITLEN
) {
4045 /* Read a 14 bit len */
4046 if (fread(buf
+1,1,1,fp
) == 0) return REDIS_RDB_LENERR
;
4047 return ((buf
[0]&0x3F)<<8)|buf
[1];
4049 /* Read a 32 bit len */
4050 if (fread(&len
,4,1,fp
) == 0) return REDIS_RDB_LENERR
;
4055 /* Load an integer-encoded object from file 'fp', with the specified
4056 * encoding type 'enctype'. If encode is true the function may return
4057 * an integer-encoded object as reply, otherwise the returned object
4058 * will always be encoded as a raw string. */
4059 static robj
*rdbLoadIntegerObject(FILE *fp
, int enctype
, int encode
) {
4060 unsigned char enc
[4];
4063 if (enctype
== REDIS_RDB_ENC_INT8
) {
4064 if (fread(enc
,1,1,fp
) == 0) return NULL
;
4065 val
= (signed char)enc
[0];
4066 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
4068 if (fread(enc
,2,1,fp
) == 0) return NULL
;
4069 v
= enc
[0]|(enc
[1]<<8);
4071 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
4073 if (fread(enc
,4,1,fp
) == 0) return NULL
;
4074 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
4077 val
= 0; /* anti-warning */
4078 redisPanic("Unknown RDB integer encoding type");
4081 return createStringObjectFromLongLong(val
);
4083 return createObject(REDIS_STRING
,sdsfromlonglong(val
));
4086 static robj
*rdbLoadLzfStringObject(FILE*fp
) {
4087 unsigned int len
, clen
;
4088 unsigned char *c
= NULL
;
4091 if ((clen
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
4092 if ((len
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
4093 if ((c
= zmalloc(clen
)) == NULL
) goto err
;
4094 if ((val
= sdsnewlen(NULL
,len
)) == NULL
) goto err
;
4095 if (fread(c
,clen
,1,fp
) == 0) goto err
;
4096 if (lzf_decompress(c
,clen
,val
,len
) == 0) goto err
;
4098 return createObject(REDIS_STRING
,val
);
4105 static robj
*rdbGenericLoadStringObject(FILE*fp
, int encode
) {
4110 len
= rdbLoadLen(fp
,&isencoded
);
4113 case REDIS_RDB_ENC_INT8
:
4114 case REDIS_RDB_ENC_INT16
:
4115 case REDIS_RDB_ENC_INT32
:
4116 return rdbLoadIntegerObject(fp
,len
,encode
);
4117 case REDIS_RDB_ENC_LZF
:
4118 return rdbLoadLzfStringObject(fp
);
4120 redisPanic("Unknown RDB encoding type");
4124 if (len
== REDIS_RDB_LENERR
) return NULL
;
4125 val
= sdsnewlen(NULL
,len
);
4126 if (len
&& fread(val
,len
,1,fp
) == 0) {
4130 return createObject(REDIS_STRING
,val
);
4133 static robj
*rdbLoadStringObject(FILE *fp
) {
4134 return rdbGenericLoadStringObject(fp
,0);
4137 static robj
*rdbLoadEncodedStringObject(FILE *fp
) {
4138 return rdbGenericLoadStringObject(fp
,1);
4141 /* For information about double serialization check rdbSaveDoubleValue() */
4142 static int rdbLoadDoubleValue(FILE *fp
, double *val
) {
4146 if (fread(&len
,1,1,fp
) == 0) return -1;
4148 case 255: *val
= R_NegInf
; return 0;
4149 case 254: *val
= R_PosInf
; return 0;
4150 case 253: *val
= R_Nan
; return 0;
4152 if (fread(buf
,len
,1,fp
) == 0) return -1;
4154 sscanf(buf
, "%lg", val
);
4159 /* Load a Redis object of the specified type from the specified file.
4160 * On success a newly allocated object is returned, otherwise NULL. */
4161 static robj
*rdbLoadObject(int type
, FILE *fp
) {
4162 robj
*o
, *ele
, *dec
;
4165 redisLog(REDIS_DEBUG
,"LOADING OBJECT %d (at %d)\n",type
,ftell(fp
));
4166 if (type
== REDIS_STRING
) {
4167 /* Read string value */
4168 if ((o
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
4169 o
= tryObjectEncoding(o
);
4170 } else if (type
== REDIS_LIST
) {
4171 /* Read list value */
4172 if ((len
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
4174 /* Use a real list when there are too many entries */
4175 if (len
> server
.list_max_ziplist_entries
) {
4176 o
= createListObject();
4178 o
= createZiplistObject();
4181 /* Load every single element of the list */
4183 if ((ele
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
4185 /* If we are using a ziplist and the value is too big, convert
4186 * the object to a real list. */
4187 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
&&
4188 ele
->encoding
== REDIS_ENCODING_RAW
&&
4189 sdslen(ele
->ptr
) > server
.list_max_ziplist_value
)
4190 listTypeConvert(o
,REDIS_ENCODING_LINKEDLIST
);
4192 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
4193 dec
= getDecodedObject(ele
);
4194 o
->ptr
= ziplistPush(o
->ptr
,dec
->ptr
,sdslen(dec
->ptr
),REDIS_TAIL
);
4198 ele
= tryObjectEncoding(ele
);
4199 listAddNodeTail(o
->ptr
,ele
);
4202 } else if (type
== REDIS_SET
) {
4203 /* Read list/set value */
4204 if ((len
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
4205 o
= createSetObject();
4206 /* It's faster to expand the dict to the right size asap in order
4207 * to avoid rehashing */
4208 if (len
> DICT_HT_INITIAL_SIZE
)
4209 dictExpand(o
->ptr
,len
);
4210 /* Load every single element of the list/set */
4212 if ((ele
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
4213 ele
= tryObjectEncoding(ele
);
4214 dictAdd((dict
*)o
->ptr
,ele
,NULL
);
4216 } else if (type
== REDIS_ZSET
) {
4217 /* Read list/set value */
4221 if ((zsetlen
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
4222 o
= createZsetObject();
4224 /* Load every single element of the list/set */
4227 double *score
= zmalloc(sizeof(double));
4229 if ((ele
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
4230 ele
= tryObjectEncoding(ele
);
4231 if (rdbLoadDoubleValue(fp
,score
) == -1) return NULL
;
4232 dictAdd(zs
->dict
,ele
,score
);
4233 zslInsert(zs
->zsl
,*score
,ele
);
4234 incrRefCount(ele
); /* added to skiplist */
4236 } else if (type
== REDIS_HASH
) {
4239 if ((hashlen
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
4240 o
= createHashObject();
4241 /* Too many entries? Use an hash table. */
4242 if (hashlen
> server
.hash_max_zipmap_entries
)
4243 convertToRealHash(o
);
4244 /* Load every key/value, then set it into the zipmap or hash
4245 * table, as needed. */
4249 if ((key
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
4250 if ((val
= rdbLoadEncodedStringObject(fp
)) == NULL
) return NULL
;
4251 /* If we are using a zipmap and there are too big values
4252 * the object is converted to real hash table encoding. */
4253 if (o
->encoding
!= REDIS_ENCODING_HT
&&
4254 ((key
->encoding
== REDIS_ENCODING_RAW
&&
4255 sdslen(key
->ptr
) > server
.hash_max_zipmap_value
) ||
4256 (val
->encoding
== REDIS_ENCODING_RAW
&&
4257 sdslen(val
->ptr
) > server
.hash_max_zipmap_value
)))
4259 convertToRealHash(o
);
4262 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
4263 unsigned char *zm
= o
->ptr
;
4264 robj
*deckey
, *decval
;
4266 /* We need raw string objects to add them to the zipmap */
4267 deckey
= getDecodedObject(key
);
4268 decval
= getDecodedObject(val
);
4269 zm
= zipmapSet(zm
,deckey
->ptr
,sdslen(deckey
->ptr
),
4270 decval
->ptr
,sdslen(decval
->ptr
),NULL
);
4272 decrRefCount(deckey
);
4273 decrRefCount(decval
);
4277 key
= tryObjectEncoding(key
);
4278 val
= tryObjectEncoding(val
);
4279 dictAdd((dict
*)o
->ptr
,key
,val
);
4283 redisPanic("Unknown object type");
4288 static int rdbLoad(char *filename
) {
4291 int type
, retval
, rdbver
;
4292 int swap_all_values
= 0;
4293 redisDb
*db
= server
.db
+0;
4295 time_t expiretime
, now
= time(NULL
);
4297 fp
= fopen(filename
,"r");
4298 if (!fp
) return REDIS_ERR
;
4299 if (fread(buf
,9,1,fp
) == 0) goto eoferr
;
4301 if (memcmp(buf
,"REDIS",5) != 0) {
4303 redisLog(REDIS_WARNING
,"Wrong signature trying to load DB from file");
4306 rdbver
= atoi(buf
+5);
4309 redisLog(REDIS_WARNING
,"Can't handle RDB format version %d",rdbver
);
4318 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
4319 if (type
== REDIS_EXPIRETIME
) {
4320 if ((expiretime
= rdbLoadTime(fp
)) == -1) goto eoferr
;
4321 /* We read the time so we need to read the object type again */
4322 if ((type
= rdbLoadType(fp
)) == -1) goto eoferr
;
4324 if (type
== REDIS_EOF
) break;
4325 /* Handle SELECT DB opcode as a special case */
4326 if (type
== REDIS_SELECTDB
) {
4327 if ((dbid
= rdbLoadLen(fp
,NULL
)) == REDIS_RDB_LENERR
)
4329 if (dbid
>= (unsigned)server
.dbnum
) {
4330 redisLog(REDIS_WARNING
,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server
.dbnum
);
4333 db
= server
.db
+dbid
;
4337 if ((key
= rdbLoadStringObject(fp
)) == NULL
) goto eoferr
;
4339 if ((val
= rdbLoadObject(type
,fp
)) == NULL
) goto eoferr
;
4340 /* Check if the key already expired */
4341 if (expiretime
!= -1 && expiretime
< now
) {
4346 /* Add the new object in the hash table */
4347 retval
= dbAdd(db
,key
,val
);
4348 if (retval
== REDIS_ERR
) {
4349 redisLog(REDIS_WARNING
,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key
->ptr
);
4352 /* Set the expire time if needed */
4353 if (expiretime
!= -1) setExpire(db
,key
,expiretime
);
4355 /* Handle swapping while loading big datasets when VM is on */
4357 /* If we detecter we are hopeless about fitting something in memory
4358 * we just swap every new key on disk. Directly...
4359 * Note that's important to check for this condition before resorting
4360 * to random sampling, otherwise we may try to swap already
4362 if (swap_all_values
) {
4363 dictEntry
*de
= dictFind(db
->dict
,key
->ptr
);
4365 /* de may be NULL since the key already expired */
4368 val
= dictGetEntryVal(de
);
4370 if (val
->refcount
== 1 &&
4371 (vp
= vmSwapObjectBlocking(val
)) != NULL
)
4372 dictGetEntryVal(de
) = vp
;
4379 /* Flush data on disk once 32 MB of additional RAM are used... */
4381 if ((zmalloc_used_memory() - server
.vm_max_memory
) > 1024*1024*32)
4384 /* If we have still some hope of having some value fitting memory
4385 * then we try random sampling. */
4386 if (!swap_all_values
&& server
.vm_enabled
&& force_swapout
) {
4387 while (zmalloc_used_memory() > server
.vm_max_memory
) {
4388 if (vmSwapOneObjectBlocking() == REDIS_ERR
) break;
4390 if (zmalloc_used_memory() > server
.vm_max_memory
)
4391 swap_all_values
= 1; /* We are already using too much mem */
4397 eoferr
: /* unexpected end of file is handled here with a fatal exit */
4398 redisLog(REDIS_WARNING
,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
4400 return REDIS_ERR
; /* Just to avoid warning */
4403 /*================================== Shutdown =============================== */
4404 static int prepareForShutdown() {
4405 redisLog(REDIS_WARNING
,"User requested shutdown, saving DB...");
4406 /* Kill the saving child if there is a background saving in progress.
4407 We want to avoid race conditions, for instance our saving child may
4408 overwrite the synchronous saving did by SHUTDOWN. */
4409 if (server
.bgsavechildpid
!= -1) {
4410 redisLog(REDIS_WARNING
,"There is a live saving child. Killing it!");
4411 kill(server
.bgsavechildpid
,SIGKILL
);
4412 rdbRemoveTempFile(server
.bgsavechildpid
);
4414 if (server
.appendonly
) {
4415 /* Append only file: fsync() the AOF and exit */
4416 aof_fsync(server
.appendfd
);
4417 if (server
.vm_enabled
) unlink(server
.vm_swap_file
);
4419 /* Snapshotting. Perform a SYNC SAVE and exit */
4420 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
4421 if (server
.daemonize
)
4422 unlink(server
.pidfile
);
4423 redisLog(REDIS_WARNING
,"%zu bytes used at exit",zmalloc_used_memory());
4425 /* Ooops.. error saving! The best we can do is to continue
4426 * operating. Note that if there was a background saving process,
4427 * in the next cron() Redis will be notified that the background
4428 * saving aborted, handling special stuff like slaves pending for
4429 * synchronization... */
4430 redisLog(REDIS_WARNING
,"Error trying to save the DB, can't exit");
4434 redisLog(REDIS_WARNING
,"Server exit now, bye bye...");
4438 /*================================== Commands =============================== */
4440 static void authCommand(redisClient
*c
) {
4441 if (!server
.requirepass
|| !strcmp(c
->argv
[1]->ptr
, server
.requirepass
)) {
4442 c
->authenticated
= 1;
4443 addReply(c
,shared
.ok
);
4445 c
->authenticated
= 0;
4446 addReplySds(c
,sdscatprintf(sdsempty(),"-ERR invalid password\r\n"));
4450 static void pingCommand(redisClient
*c
) {
4451 addReply(c
,shared
.pong
);
4454 static void echoCommand(redisClient
*c
) {
4455 addReplyBulk(c
,c
->argv
[1]);
4458 /*=================================== Strings =============================== */
4460 static void setGenericCommand(redisClient
*c
, int nx
, robj
*key
, robj
*val
, robj
*expire
) {
4462 long seconds
= 0; /* initialized to avoid an harmness warning */
4465 if (getLongFromObjectOrReply(c
, expire
, &seconds
, NULL
) != REDIS_OK
)
4468 addReplySds(c
,sdsnew("-ERR invalid expire time in SETEX\r\n"));
4473 touchWatchedKey(c
->db
,key
);
4474 if (nx
) deleteIfVolatile(c
->db
,key
);
4475 retval
= dbAdd(c
->db
,key
,val
);
4476 if (retval
== REDIS_ERR
) {
4478 dbReplace(c
->db
,key
,val
);
4481 addReply(c
,shared
.czero
);
4488 removeExpire(c
->db
,key
);
4489 if (expire
) setExpire(c
->db
,key
,time(NULL
)+seconds
);
4490 addReply(c
, nx
? shared
.cone
: shared
.ok
);
4493 static void setCommand(redisClient
*c
) {
4494 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[2],NULL
);
4497 static void setnxCommand(redisClient
*c
) {
4498 setGenericCommand(c
,1,c
->argv
[1],c
->argv
[2],NULL
);
4501 static void setexCommand(redisClient
*c
) {
4502 setGenericCommand(c
,0,c
->argv
[1],c
->argv
[3],c
->argv
[2]);
4505 static int getGenericCommand(redisClient
*c
) {
4508 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
)
4511 if (o
->type
!= REDIS_STRING
) {
4512 addReply(c
,shared
.wrongtypeerr
);
4520 static void getCommand(redisClient
*c
) {
4521 getGenericCommand(c
);
4524 static void getsetCommand(redisClient
*c
) {
4525 if (getGenericCommand(c
) == REDIS_ERR
) return;
4526 dbReplace(c
->db
,c
->argv
[1],c
->argv
[2]);
4527 incrRefCount(c
->argv
[2]);
4529 removeExpire(c
->db
,c
->argv
[1]);
4532 static void mgetCommand(redisClient
*c
) {
4535 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-1));
4536 for (j
= 1; j
< c
->argc
; j
++) {
4537 robj
*o
= lookupKeyRead(c
->db
,c
->argv
[j
]);
4539 addReply(c
,shared
.nullbulk
);
4541 if (o
->type
!= REDIS_STRING
) {
4542 addReply(c
,shared
.nullbulk
);
4550 static void msetGenericCommand(redisClient
*c
, int nx
) {
4551 int j
, busykeys
= 0;
4553 if ((c
->argc
% 2) == 0) {
4554 addReplySds(c
,sdsnew("-ERR wrong number of arguments for MSET\r\n"));
4557 /* Handle the NX flag. The MSETNX semantic is to return zero and don't
4558 * set nothing at all if at least one already key exists. */
4560 for (j
= 1; j
< c
->argc
; j
+= 2) {
4561 if (lookupKeyWrite(c
->db
,c
->argv
[j
]) != NULL
) {
4567 addReply(c
, shared
.czero
);
4571 for (j
= 1; j
< c
->argc
; j
+= 2) {
4572 c
->argv
[j
+1] = tryObjectEncoding(c
->argv
[j
+1]);
4573 dbReplace(c
->db
,c
->argv
[j
],c
->argv
[j
+1]);
4574 incrRefCount(c
->argv
[j
+1]);
4575 removeExpire(c
->db
,c
->argv
[j
]);
4577 server
.dirty
+= (c
->argc
-1)/2;
4578 addReply(c
, nx
? shared
.cone
: shared
.ok
);
4581 static void msetCommand(redisClient
*c
) {
4582 msetGenericCommand(c
,0);
4585 static void msetnxCommand(redisClient
*c
) {
4586 msetGenericCommand(c
,1);
4589 static void incrDecrCommand(redisClient
*c
, long long incr
) {
4593 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4594 if (o
!= NULL
&& checkType(c
,o
,REDIS_STRING
)) return;
4595 if (getLongLongFromObjectOrReply(c
,o
,&value
,NULL
) != REDIS_OK
) return;
4598 o
= createStringObjectFromLongLong(value
);
4599 dbReplace(c
->db
,c
->argv
[1],o
);
4601 addReply(c
,shared
.colon
);
4603 addReply(c
,shared
.crlf
);
4606 static void incrCommand(redisClient
*c
) {
4607 incrDecrCommand(c
,1);
4610 static void decrCommand(redisClient
*c
) {
4611 incrDecrCommand(c
,-1);
4614 static void incrbyCommand(redisClient
*c
) {
4617 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
4618 incrDecrCommand(c
,incr
);
4621 static void decrbyCommand(redisClient
*c
) {
4624 if (getLongLongFromObjectOrReply(c
, c
->argv
[2], &incr
, NULL
) != REDIS_OK
) return;
4625 incrDecrCommand(c
,-incr
);
4628 static void appendCommand(redisClient
*c
) {
4633 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4635 /* Create the key */
4636 retval
= dbAdd(c
->db
,c
->argv
[1],c
->argv
[2]);
4637 incrRefCount(c
->argv
[2]);
4638 totlen
= stringObjectLen(c
->argv
[2]);
4640 if (o
->type
!= REDIS_STRING
) {
4641 addReply(c
,shared
.wrongtypeerr
);
4644 /* If the object is specially encoded or shared we have to make
4646 if (o
->refcount
!= 1 || o
->encoding
!= REDIS_ENCODING_RAW
) {
4647 robj
*decoded
= getDecodedObject(o
);
4649 o
= createStringObject(decoded
->ptr
, sdslen(decoded
->ptr
));
4650 decrRefCount(decoded
);
4651 dbReplace(c
->db
,c
->argv
[1],o
);
4654 if (c
->argv
[2]->encoding
== REDIS_ENCODING_RAW
) {
4655 o
->ptr
= sdscatlen(o
->ptr
,
4656 c
->argv
[2]->ptr
, sdslen(c
->argv
[2]->ptr
));
4658 o
->ptr
= sdscatprintf(o
->ptr
, "%ld",
4659 (unsigned long) c
->argv
[2]->ptr
);
4661 totlen
= sdslen(o
->ptr
);
4664 addReplySds(c
,sdscatprintf(sdsempty(),":%lu\r\n",(unsigned long)totlen
));
4667 static void substrCommand(redisClient
*c
) {
4669 long start
= atoi(c
->argv
[2]->ptr
);
4670 long end
= atoi(c
->argv
[3]->ptr
);
4671 size_t rangelen
, strlen
;
4674 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
4675 checkType(c
,o
,REDIS_STRING
)) return;
4677 o
= getDecodedObject(o
);
4678 strlen
= sdslen(o
->ptr
);
4680 /* convert negative indexes */
4681 if (start
< 0) start
= strlen
+start
;
4682 if (end
< 0) end
= strlen
+end
;
4683 if (start
< 0) start
= 0;
4684 if (end
< 0) end
= 0;
4686 /* indexes sanity checks */
4687 if (start
> end
|| (size_t)start
>= strlen
) {
4688 /* Out of range start or start > end result in null reply */
4689 addReply(c
,shared
.nullbulk
);
4693 if ((size_t)end
>= strlen
) end
= strlen
-1;
4694 rangelen
= (end
-start
)+1;
4696 /* Return the result */
4697 addReplySds(c
,sdscatprintf(sdsempty(),"$%zu\r\n",rangelen
));
4698 range
= sdsnewlen((char*)o
->ptr
+start
,rangelen
);
4699 addReplySds(c
,range
);
4700 addReply(c
,shared
.crlf
);
4704 /* ========================= Type agnostic commands ========================= */
4706 static void delCommand(redisClient
*c
) {
4709 for (j
= 1; j
< c
->argc
; j
++) {
4710 if (dbDelete(c
->db
,c
->argv
[j
])) {
4711 touchWatchedKey(c
->db
,c
->argv
[j
]);
4716 addReplyLongLong(c
,deleted
);
4719 static void existsCommand(redisClient
*c
) {
4720 expireIfNeeded(c
->db
,c
->argv
[1]);
4721 if (dbExists(c
->db
,c
->argv
[1])) {
4722 addReply(c
, shared
.cone
);
4724 addReply(c
, shared
.czero
);
4728 static void selectCommand(redisClient
*c
) {
4729 int id
= atoi(c
->argv
[1]->ptr
);
4731 if (selectDb(c
,id
) == REDIS_ERR
) {
4732 addReplySds(c
,sdsnew("-ERR invalid DB index\r\n"));
4734 addReply(c
,shared
.ok
);
4738 static void randomkeyCommand(redisClient
*c
) {
4741 if ((key
= dbRandomKey(c
->db
)) == NULL
) {
4742 addReply(c
,shared
.nullbulk
);
4746 addReplyBulk(c
,key
);
4750 static void keysCommand(redisClient
*c
) {
4753 sds pattern
= c
->argv
[1]->ptr
;
4754 int plen
= sdslen(pattern
);
4755 unsigned long numkeys
= 0;
4756 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
4758 di
= dictGetIterator(c
->db
->dict
);
4760 decrRefCount(lenobj
);
4761 while((de
= dictNext(di
)) != NULL
) {
4762 sds key
= dictGetEntryKey(de
);
4765 if ((pattern
[0] == '*' && pattern
[1] == '\0') ||
4766 stringmatchlen(pattern
,plen
,key
,sdslen(key
),0)) {
4767 keyobj
= createStringObject(key
,sdslen(key
));
4768 if (expireIfNeeded(c
->db
,keyobj
) == 0) {
4769 addReplyBulk(c
,keyobj
);
4772 decrRefCount(keyobj
);
4775 dictReleaseIterator(di
);
4776 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",numkeys
);
4779 static void dbsizeCommand(redisClient
*c
) {
4781 sdscatprintf(sdsempty(),":%lu\r\n",dictSize(c
->db
->dict
)));
4784 static void lastsaveCommand(redisClient
*c
) {
4786 sdscatprintf(sdsempty(),":%lu\r\n",server
.lastsave
));
4789 static void typeCommand(redisClient
*c
) {
4793 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
4798 case REDIS_STRING
: type
= "+string"; break;
4799 case REDIS_LIST
: type
= "+list"; break;
4800 case REDIS_SET
: type
= "+set"; break;
4801 case REDIS_ZSET
: type
= "+zset"; break;
4802 case REDIS_HASH
: type
= "+hash"; break;
4803 default: type
= "+unknown"; break;
4806 addReplySds(c
,sdsnew(type
));
4807 addReply(c
,shared
.crlf
);
4810 static void saveCommand(redisClient
*c
) {
4811 if (server
.bgsavechildpid
!= -1) {
4812 addReplySds(c
,sdsnew("-ERR background save in progress\r\n"));
4815 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
4816 addReply(c
,shared
.ok
);
4818 addReply(c
,shared
.err
);
4822 static void bgsaveCommand(redisClient
*c
) {
4823 if (server
.bgsavechildpid
!= -1) {
4824 addReplySds(c
,sdsnew("-ERR background save already in progress\r\n"));
4827 if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
4828 char *status
= "+Background saving started\r\n";
4829 addReplySds(c
,sdsnew(status
));
4831 addReply(c
,shared
.err
);
4835 static void shutdownCommand(redisClient
*c
) {
4836 if (prepareForShutdown() == REDIS_OK
)
4838 addReplySds(c
, sdsnew("-ERR Errors trying to SHUTDOWN. Check logs.\r\n"));
4841 static void renameGenericCommand(redisClient
*c
, int nx
) {
4844 /* To use the same key as src and dst is probably an error */
4845 if (sdscmp(c
->argv
[1]->ptr
,c
->argv
[2]->ptr
) == 0) {
4846 addReply(c
,shared
.sameobjecterr
);
4850 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
)) == NULL
)
4854 deleteIfVolatile(c
->db
,c
->argv
[2]);
4855 if (dbAdd(c
->db
,c
->argv
[2],o
) == REDIS_ERR
) {
4858 addReply(c
,shared
.czero
);
4861 dbReplace(c
->db
,c
->argv
[2],o
);
4863 dbDelete(c
->db
,c
->argv
[1]);
4864 touchWatchedKey(c
->db
,c
->argv
[2]);
4866 addReply(c
,nx
? shared
.cone
: shared
.ok
);
4869 static void renameCommand(redisClient
*c
) {
4870 renameGenericCommand(c
,0);
4873 static void renamenxCommand(redisClient
*c
) {
4874 renameGenericCommand(c
,1);
4877 static void moveCommand(redisClient
*c
) {
4882 /* Obtain source and target DB pointers */
4885 if (selectDb(c
,atoi(c
->argv
[2]->ptr
)) == REDIS_ERR
) {
4886 addReply(c
,shared
.outofrangeerr
);
4890 selectDb(c
,srcid
); /* Back to the source DB */
4892 /* If the user is moving using as target the same
4893 * DB as the source DB it is probably an error. */
4895 addReply(c
,shared
.sameobjecterr
);
4899 /* Check if the element exists and get a reference */
4900 o
= lookupKeyWrite(c
->db
,c
->argv
[1]);
4902 addReply(c
,shared
.czero
);
4906 /* Try to add the element to the target DB */
4907 deleteIfVolatile(dst
,c
->argv
[1]);
4908 if (dbAdd(dst
,c
->argv
[1],o
) == REDIS_ERR
) {
4909 addReply(c
,shared
.czero
);
4914 /* OK! key moved, free the entry in the source DB */
4915 dbDelete(src
,c
->argv
[1]);
4917 addReply(c
,shared
.cone
);
4920 /* =================================== Lists ================================ */
4923 /* Check the argument length to see if it requires us to convert the ziplist
4924 * to a real list. Only check raw-encoded objects because integer encoded
4925 * objects are never too long. */
4926 static void listTypeTryConversion(robj
*subject
, robj
*value
) {
4927 if (subject
->encoding
!= REDIS_ENCODING_ZIPLIST
) return;
4928 if (value
->encoding
== REDIS_ENCODING_RAW
&&
4929 sdslen(value
->ptr
) > server
.list_max_ziplist_value
)
4930 listTypeConvert(subject
,REDIS_ENCODING_LINKEDLIST
);
4933 static void listTypePush(robj
*subject
, robj
*value
, int where
) {
4934 /* Check if we need to convert the ziplist */
4935 listTypeTryConversion(subject
,value
);
4936 if (subject
->encoding
== REDIS_ENCODING_ZIPLIST
&&
4937 ziplistLen(subject
->ptr
) >= server
.list_max_ziplist_entries
)
4938 listTypeConvert(subject
,REDIS_ENCODING_LINKEDLIST
);
4940 if (subject
->encoding
== REDIS_ENCODING_ZIPLIST
) {
4941 int pos
= (where
== REDIS_HEAD
) ? ZIPLIST_HEAD
: ZIPLIST_TAIL
;
4942 value
= getDecodedObject(value
);
4943 subject
->ptr
= ziplistPush(subject
->ptr
,value
->ptr
,sdslen(value
->ptr
),pos
);
4944 decrRefCount(value
);
4945 } else if (subject
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
4946 if (where
== REDIS_HEAD
) {
4947 listAddNodeHead(subject
->ptr
,value
);
4949 listAddNodeTail(subject
->ptr
,value
);
4951 incrRefCount(value
);
4953 redisPanic("Unknown list encoding");
4957 static robj
*listTypePop(robj
*subject
, int where
) {
4959 if (subject
->encoding
== REDIS_ENCODING_ZIPLIST
) {
4961 unsigned char *vstr
;
4964 int pos
= (where
== REDIS_HEAD
) ? 0 : -1;
4965 p
= ziplistIndex(subject
->ptr
,pos
);
4966 if (ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
4968 value
= createStringObject((char*)vstr
,vlen
);
4970 value
= createStringObjectFromLongLong(vlong
);
4972 /* We only need to delete an element when it exists */
4973 subject
->ptr
= ziplistDelete(subject
->ptr
,&p
);
4975 } else if (subject
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
4976 list
*list
= subject
->ptr
;
4978 if (where
== REDIS_HEAD
) {
4979 ln
= listFirst(list
);
4981 ln
= listLast(list
);
4984 value
= listNodeValue(ln
);
4985 incrRefCount(value
);
4986 listDelNode(list
,ln
);
4989 redisPanic("Unknown list encoding");
4994 static unsigned long listTypeLength(robj
*subject
) {
4995 if (subject
->encoding
== REDIS_ENCODING_ZIPLIST
) {
4996 return ziplistLen(subject
->ptr
);
4997 } else if (subject
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
4998 return listLength((list
*)subject
->ptr
);
5000 redisPanic("Unknown list encoding");
5004 /* Structure to hold set iteration abstraction. */
5007 unsigned char encoding
;
5008 unsigned char direction
; /* Iteration direction */
5013 /* Structure for an entry while iterating over a list. */
5015 listTypeIterator
*li
;
5016 unsigned char *zi
; /* Entry in ziplist */
5017 listNode
*ln
; /* Entry in linked list */
5020 /* Initialize an iterator at the specified index. */
5021 static listTypeIterator
*listTypeInitIterator(robj
*subject
, int index
, unsigned char direction
) {
5022 listTypeIterator
*li
= zmalloc(sizeof(listTypeIterator
));
5023 li
->subject
= subject
;
5024 li
->encoding
= subject
->encoding
;
5025 li
->direction
= direction
;
5026 if (li
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5027 li
->zi
= ziplistIndex(subject
->ptr
,index
);
5028 } else if (li
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5029 li
->ln
= listIndex(subject
->ptr
,index
);
5031 redisPanic("Unknown list encoding");
5036 /* Clean up the iterator. */
5037 static void listTypeReleaseIterator(listTypeIterator
*li
) {
5041 /* Stores pointer to current the entry in the provided entry structure
5042 * and advances the position of the iterator. Returns 1 when the current
5043 * entry is in fact an entry, 0 otherwise. */
5044 static int listTypeNext(listTypeIterator
*li
, listTypeEntry
*entry
) {
5045 /* Protect from converting when iterating */
5046 redisAssert(li
->subject
->encoding
== li
->encoding
);
5049 if (li
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5051 if (entry
->zi
!= NULL
) {
5052 if (li
->direction
== REDIS_TAIL
)
5053 li
->zi
= ziplistNext(li
->subject
->ptr
,li
->zi
);
5055 li
->zi
= ziplistPrev(li
->subject
->ptr
,li
->zi
);
5058 } else if (li
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5060 if (entry
->ln
!= NULL
) {
5061 if (li
->direction
== REDIS_TAIL
)
5062 li
->ln
= li
->ln
->next
;
5064 li
->ln
= li
->ln
->prev
;
5068 redisPanic("Unknown list encoding");
5073 /* Return entry or NULL at the current position of the iterator. */
5074 static robj
*listTypeGet(listTypeEntry
*entry
) {
5075 listTypeIterator
*li
= entry
->li
;
5077 if (li
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5078 unsigned char *vstr
;
5081 redisAssert(entry
->zi
!= NULL
);
5082 if (ziplistGet(entry
->zi
,&vstr
,&vlen
,&vlong
)) {
5084 value
= createStringObject((char*)vstr
,vlen
);
5086 value
= createStringObjectFromLongLong(vlong
);
5089 } else if (li
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5090 redisAssert(entry
->ln
!= NULL
);
5091 value
= listNodeValue(entry
->ln
);
5092 incrRefCount(value
);
5094 redisPanic("Unknown list encoding");
5099 static void listTypeInsert(listTypeEntry
*entry
, robj
*value
, int where
) {
5100 robj
*subject
= entry
->li
->subject
;
5101 if (entry
->li
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5102 value
= getDecodedObject(value
);
5103 if (where
== REDIS_TAIL
) {
5104 unsigned char *next
= ziplistNext(subject
->ptr
,entry
->zi
);
5106 /* When we insert after the current element, but the current element
5107 * is the tail of the list, we need to do a push. */
5109 subject
->ptr
= ziplistPush(subject
->ptr
,value
->ptr
,sdslen(value
->ptr
),REDIS_TAIL
);
5111 subject
->ptr
= ziplistInsert(subject
->ptr
,next
,value
->ptr
,sdslen(value
->ptr
));
5114 subject
->ptr
= ziplistInsert(subject
->ptr
,entry
->zi
,value
->ptr
,sdslen(value
->ptr
));
5116 decrRefCount(value
);
5117 } else if (entry
->li
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5118 if (where
== REDIS_TAIL
) {
5119 listInsertNode(subject
->ptr
,entry
->ln
,value
,AL_START_TAIL
);
5121 listInsertNode(subject
->ptr
,entry
->ln
,value
,AL_START_HEAD
);
5123 incrRefCount(value
);
5125 redisPanic("Unknown list encoding");
5129 /* Compare the given object with the entry at the current position. */
5130 static int listTypeEqual(listTypeEntry
*entry
, robj
*o
) {
5131 listTypeIterator
*li
= entry
->li
;
5132 if (li
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5133 redisAssert(o
->encoding
== REDIS_ENCODING_RAW
);
5134 return ziplistCompare(entry
->zi
,o
->ptr
,sdslen(o
->ptr
));
5135 } else if (li
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5136 return equalStringObjects(o
,listNodeValue(entry
->ln
));
5138 redisPanic("Unknown list encoding");
5142 /* Delete the element pointed to. */
5143 static void listTypeDelete(listTypeEntry
*entry
) {
5144 listTypeIterator
*li
= entry
->li
;
5145 if (li
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5146 unsigned char *p
= entry
->zi
;
5147 li
->subject
->ptr
= ziplistDelete(li
->subject
->ptr
,&p
);
5149 /* Update position of the iterator depending on the direction */
5150 if (li
->direction
== REDIS_TAIL
)
5153 li
->zi
= ziplistPrev(li
->subject
->ptr
,p
);
5154 } else if (entry
->li
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5156 if (li
->direction
== REDIS_TAIL
)
5157 next
= entry
->ln
->next
;
5159 next
= entry
->ln
->prev
;
5160 listDelNode(li
->subject
->ptr
,entry
->ln
);
5163 redisPanic("Unknown list encoding");
5167 static void listTypeConvert(robj
*subject
, int enc
) {
5168 listTypeIterator
*li
;
5169 listTypeEntry entry
;
5170 redisAssert(subject
->type
== REDIS_LIST
);
5172 if (enc
== REDIS_ENCODING_LINKEDLIST
) {
5173 list
*l
= listCreate();
5174 listSetFreeMethod(l
,decrRefCount
);
5176 /* listTypeGet returns a robj with incremented refcount */
5177 li
= listTypeInitIterator(subject
,0,REDIS_TAIL
);
5178 while (listTypeNext(li
,&entry
)) listAddNodeTail(l
,listTypeGet(&entry
));
5179 listTypeReleaseIterator(li
);
5181 subject
->encoding
= REDIS_ENCODING_LINKEDLIST
;
5182 zfree(subject
->ptr
);
5185 redisPanic("Unsupported list conversion");
5189 static void pushGenericCommand(redisClient
*c
, int where
) {
5190 robj
*lobj
= lookupKeyWrite(c
->db
,c
->argv
[1]);
5192 if (handleClientsWaitingListPush(c
,c
->argv
[1],c
->argv
[2])) {
5193 addReply(c
,shared
.cone
);
5196 lobj
= createZiplistObject();
5197 dbAdd(c
->db
,c
->argv
[1],lobj
);
5199 if (lobj
->type
!= REDIS_LIST
) {
5200 addReply(c
,shared
.wrongtypeerr
);
5203 if (handleClientsWaitingListPush(c
,c
->argv
[1],c
->argv
[2])) {
5204 addReply(c
,shared
.cone
);
5208 listTypePush(lobj
,c
->argv
[2],where
);
5209 addReplyLongLong(c
,listTypeLength(lobj
));
5213 static void lpushCommand(redisClient
*c
) {
5214 pushGenericCommand(c
,REDIS_HEAD
);
5217 static void rpushCommand(redisClient
*c
) {
5218 pushGenericCommand(c
,REDIS_TAIL
);
5221 static void pushxGenericCommand(redisClient
*c
, robj
*refval
, robj
*val
, int where
) {
5223 listTypeIterator
*iter
;
5224 listTypeEntry entry
;
5227 if ((subject
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5228 checkType(c
,subject
,REDIS_LIST
)) return;
5230 if (refval
!= NULL
) {
5231 /* Note: we expect refval to be string-encoded because it is *not* the
5232 * last argument of the multi-bulk LINSERT. */
5233 redisAssert(refval
->encoding
== REDIS_ENCODING_RAW
);
5235 /* We're not sure if this value can be inserted yet, but we cannot
5236 * convert the list inside the iterator. We don't want to loop over
5237 * the list twice (once to see if the value can be inserted and once
5238 * to do the actual insert), so we assume this value can be inserted
5239 * and convert the ziplist to a regular list if necessary. */
5240 listTypeTryConversion(subject
,val
);
5242 /* Seek refval from head to tail */
5243 iter
= listTypeInitIterator(subject
,0,REDIS_TAIL
);
5244 while (listTypeNext(iter
,&entry
)) {
5245 if (listTypeEqual(&entry
,refval
)) {
5246 listTypeInsert(&entry
,val
,where
);
5251 listTypeReleaseIterator(iter
);
5254 /* Check if the length exceeds the ziplist length threshold. */
5255 if (subject
->encoding
== REDIS_ENCODING_ZIPLIST
&&
5256 ziplistLen(subject
->ptr
) > server
.list_max_ziplist_entries
)
5257 listTypeConvert(subject
,REDIS_ENCODING_LINKEDLIST
);
5260 /* Notify client of a failed insert */
5261 addReply(c
,shared
.cnegone
);
5265 listTypePush(subject
,val
,where
);
5269 addReplyUlong(c
,listTypeLength(subject
));
5272 static void lpushxCommand(redisClient
*c
) {
5273 pushxGenericCommand(c
,NULL
,c
->argv
[2],REDIS_HEAD
);
5276 static void rpushxCommand(redisClient
*c
) {
5277 pushxGenericCommand(c
,NULL
,c
->argv
[2],REDIS_TAIL
);
5280 static void linsertCommand(redisClient
*c
) {
5281 if (strcasecmp(c
->argv
[2]->ptr
,"after") == 0) {
5282 pushxGenericCommand(c
,c
->argv
[3],c
->argv
[4],REDIS_TAIL
);
5283 } else if (strcasecmp(c
->argv
[2]->ptr
,"before") == 0) {
5284 pushxGenericCommand(c
,c
->argv
[3],c
->argv
[4],REDIS_HEAD
);
5286 addReply(c
,shared
.syntaxerr
);
5290 static void llenCommand(redisClient
*c
) {
5291 robj
*o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
);
5292 if (o
== NULL
|| checkType(c
,o
,REDIS_LIST
)) return;
5293 addReplyUlong(c
,listTypeLength(o
));
5296 static void lindexCommand(redisClient
*c
) {
5297 robj
*o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
);
5298 if (o
== NULL
|| checkType(c
,o
,REDIS_LIST
)) return;
5299 int index
= atoi(c
->argv
[2]->ptr
);
5302 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5304 unsigned char *vstr
;
5307 p
= ziplistIndex(o
->ptr
,index
);
5308 if (ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
5310 value
= createStringObject((char*)vstr
,vlen
);
5312 value
= createStringObjectFromLongLong(vlong
);
5314 addReplyBulk(c
,value
);
5315 decrRefCount(value
);
5317 addReply(c
,shared
.nullbulk
);
5319 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5320 listNode
*ln
= listIndex(o
->ptr
,index
);
5322 value
= listNodeValue(ln
);
5323 addReplyBulk(c
,value
);
5325 addReply(c
,shared
.nullbulk
);
5328 redisPanic("Unknown list encoding");
5332 static void lsetCommand(redisClient
*c
) {
5333 robj
*o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nokeyerr
);
5334 if (o
== NULL
|| checkType(c
,o
,REDIS_LIST
)) return;
5335 int index
= atoi(c
->argv
[2]->ptr
);
5336 robj
*value
= c
->argv
[3];
5338 listTypeTryConversion(o
,value
);
5339 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5340 unsigned char *p
, *zl
= o
->ptr
;
5341 p
= ziplistIndex(zl
,index
);
5343 addReply(c
,shared
.outofrangeerr
);
5345 o
->ptr
= ziplistDelete(o
->ptr
,&p
);
5346 value
= getDecodedObject(value
);
5347 o
->ptr
= ziplistInsert(o
->ptr
,p
,value
->ptr
,sdslen(value
->ptr
));
5348 decrRefCount(value
);
5349 addReply(c
,shared
.ok
);
5352 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5353 listNode
*ln
= listIndex(o
->ptr
,index
);
5355 addReply(c
,shared
.outofrangeerr
);
5357 decrRefCount((robj
*)listNodeValue(ln
));
5358 listNodeValue(ln
) = value
;
5359 incrRefCount(value
);
5360 addReply(c
,shared
.ok
);
5364 redisPanic("Unknown list encoding");
5368 static void popGenericCommand(redisClient
*c
, int where
) {
5369 robj
*o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
);
5370 if (o
== NULL
|| checkType(c
,o
,REDIS_LIST
)) return;
5372 robj
*value
= listTypePop(o
,where
);
5373 if (value
== NULL
) {
5374 addReply(c
,shared
.nullbulk
);
5376 addReplyBulk(c
,value
);
5377 decrRefCount(value
);
5378 if (listTypeLength(o
) == 0) dbDelete(c
->db
,c
->argv
[1]);
5383 static void lpopCommand(redisClient
*c
) {
5384 popGenericCommand(c
,REDIS_HEAD
);
5387 static void rpopCommand(redisClient
*c
) {
5388 popGenericCommand(c
,REDIS_TAIL
);
5391 static void lrangeCommand(redisClient
*c
) {
5393 int start
= atoi(c
->argv
[2]->ptr
);
5394 int end
= atoi(c
->argv
[3]->ptr
);
5397 listTypeEntry entry
;
5399 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.emptymultibulk
)) == NULL
5400 || checkType(c
,o
,REDIS_LIST
)) return;
5401 llen
= listTypeLength(o
);
5403 /* convert negative indexes */
5404 if (start
< 0) start
= llen
+start
;
5405 if (end
< 0) end
= llen
+end
;
5406 if (start
< 0) start
= 0;
5408 /* Invariant: start >= 0, so this test will be true when end < 0.
5409 * The range is empty when start > end or start >= length. */
5410 if (start
> end
|| start
>= llen
) {
5411 /* Out of range start or start > end result in empty list */
5412 addReply(c
,shared
.emptymultibulk
);
5415 if (end
>= llen
) end
= llen
-1;
5416 rangelen
= (end
-start
)+1;
5418 /* Return the result in form of a multi-bulk reply */
5419 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",rangelen
));
5420 listTypeIterator
*li
= listTypeInitIterator(o
,start
,REDIS_TAIL
);
5421 for (j
= 0; j
< rangelen
; j
++) {
5422 redisAssert(listTypeNext(li
,&entry
));
5423 value
= listTypeGet(&entry
);
5424 addReplyBulk(c
,value
);
5425 decrRefCount(value
);
5427 listTypeReleaseIterator(li
);
5430 static void ltrimCommand(redisClient
*c
) {
5432 int start
= atoi(c
->argv
[2]->ptr
);
5433 int end
= atoi(c
->argv
[3]->ptr
);
5435 int j
, ltrim
, rtrim
;
5439 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.ok
)) == NULL
||
5440 checkType(c
,o
,REDIS_LIST
)) return;
5441 llen
= listTypeLength(o
);
5443 /* convert negative indexes */
5444 if (start
< 0) start
= llen
+start
;
5445 if (end
< 0) end
= llen
+end
;
5446 if (start
< 0) start
= 0;
5448 /* Invariant: start >= 0, so this test will be true when end < 0.
5449 * The range is empty when start > end or start >= length. */
5450 if (start
> end
|| start
>= llen
) {
5451 /* Out of range start or start > end result in empty list */
5455 if (end
>= llen
) end
= llen
-1;
5460 /* Remove list elements to perform the trim */
5461 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
5462 o
->ptr
= ziplistDeleteRange(o
->ptr
,0,ltrim
);
5463 o
->ptr
= ziplistDeleteRange(o
->ptr
,-rtrim
,rtrim
);
5464 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
5466 for (j
= 0; j
< ltrim
; j
++) {
5467 ln
= listFirst(list
);
5468 listDelNode(list
,ln
);
5470 for (j
= 0; j
< rtrim
; j
++) {
5471 ln
= listLast(list
);
5472 listDelNode(list
,ln
);
5475 redisPanic("Unknown list encoding");
5477 if (listTypeLength(o
) == 0) dbDelete(c
->db
,c
->argv
[1]);
5479 addReply(c
,shared
.ok
);
5482 static void lremCommand(redisClient
*c
) {
5483 robj
*subject
, *obj
= c
->argv
[3];
5484 int toremove
= atoi(c
->argv
[2]->ptr
);
5486 listTypeEntry entry
;
5488 subject
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
);
5489 if (subject
== NULL
|| checkType(c
,subject
,REDIS_LIST
)) return;
5491 /* Make sure obj is raw when we're dealing with a ziplist */
5492 if (subject
->encoding
== REDIS_ENCODING_ZIPLIST
)
5493 obj
= getDecodedObject(obj
);
5495 listTypeIterator
*li
;
5497 toremove
= -toremove
;
5498 li
= listTypeInitIterator(subject
,-1,REDIS_HEAD
);
5500 li
= listTypeInitIterator(subject
,0,REDIS_TAIL
);
5503 while (listTypeNext(li
,&entry
)) {
5504 if (listTypeEqual(&entry
,obj
)) {
5505 listTypeDelete(&entry
);
5508 if (toremove
&& removed
== toremove
) break;
5511 listTypeReleaseIterator(li
);
5513 /* Clean up raw encoded object */
5514 if (subject
->encoding
== REDIS_ENCODING_ZIPLIST
)
5517 if (listTypeLength(subject
) == 0) dbDelete(c
->db
,c
->argv
[1]);
5518 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",removed
));
5521 /* This is the semantic of this command:
5522 * RPOPLPUSH srclist dstlist:
5523 * IF LLEN(srclist) > 0
5524 * element = RPOP srclist
5525 * LPUSH dstlist element
5532 * The idea is to be able to get an element from a list in a reliable way
5533 * since the element is not just returned but pushed against another list
5534 * as well. This command was originally proposed by Ezra Zygmuntowicz.
5536 static void rpoplpushcommand(redisClient
*c
) {
5538 if ((sobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
5539 checkType(c
,sobj
,REDIS_LIST
)) return;
5541 if (listTypeLength(sobj
) == 0) {
5542 addReply(c
,shared
.nullbulk
);
5544 robj
*dobj
= lookupKeyWrite(c
->db
,c
->argv
[2]);
5545 if (dobj
&& checkType(c
,dobj
,REDIS_LIST
)) return;
5546 value
= listTypePop(sobj
,REDIS_TAIL
);
5548 /* Add the element to the target list (unless it's directly
5549 * passed to some BLPOP-ing client */
5550 if (!handleClientsWaitingListPush(c
,c
->argv
[2],value
)) {
5551 /* Create the list if the key does not exist */
5553 dobj
= createZiplistObject();
5554 dbAdd(c
->db
,c
->argv
[2],dobj
);
5556 listTypePush(dobj
,value
,REDIS_HEAD
);
5559 /* Send the element to the client as reply as well */
5560 addReplyBulk(c
,value
);
5562 /* listTypePop returns an object with its refcount incremented */
5563 decrRefCount(value
);
5565 /* Delete the source list when it is empty */
5566 if (listTypeLength(sobj
) == 0) dbDelete(c
->db
,c
->argv
[1]);
5571 /* ==================================== Sets ================================ */
5573 static void saddCommand(redisClient
*c
) {
5576 set
= lookupKeyWrite(c
->db
,c
->argv
[1]);
5578 set
= createSetObject();
5579 dbAdd(c
->db
,c
->argv
[1],set
);
5581 if (set
->type
!= REDIS_SET
) {
5582 addReply(c
,shared
.wrongtypeerr
);
5586 if (dictAdd(set
->ptr
,c
->argv
[2],NULL
) == DICT_OK
) {
5587 incrRefCount(c
->argv
[2]);
5589 addReply(c
,shared
.cone
);
5591 addReply(c
,shared
.czero
);
5595 static void sremCommand(redisClient
*c
) {
5598 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5599 checkType(c
,set
,REDIS_SET
)) return;
5601 if (dictDelete(set
->ptr
,c
->argv
[2]) == DICT_OK
) {
5603 if (htNeedsResize(set
->ptr
)) dictResize(set
->ptr
);
5604 if (dictSize((dict
*)set
->ptr
) == 0) dbDelete(c
->db
,c
->argv
[1]);
5605 addReply(c
,shared
.cone
);
5607 addReply(c
,shared
.czero
);
5611 static void smoveCommand(redisClient
*c
) {
5612 robj
*srcset
, *dstset
;
5614 srcset
= lookupKeyWrite(c
->db
,c
->argv
[1]);
5615 dstset
= lookupKeyWrite(c
->db
,c
->argv
[2]);
5617 /* If the source key does not exist return 0, if it's of the wrong type
5619 if (srcset
== NULL
|| srcset
->type
!= REDIS_SET
) {
5620 addReply(c
, srcset
? shared
.wrongtypeerr
: shared
.czero
);
5623 /* Error if the destination key is not a set as well */
5624 if (dstset
&& dstset
->type
!= REDIS_SET
) {
5625 addReply(c
,shared
.wrongtypeerr
);
5628 /* Remove the element from the source set */
5629 if (dictDelete(srcset
->ptr
,c
->argv
[3]) == DICT_ERR
) {
5630 /* Key not found in the src set! return zero */
5631 addReply(c
,shared
.czero
);
5634 if (dictSize((dict
*)srcset
->ptr
) == 0 && srcset
!= dstset
)
5635 dbDelete(c
->db
,c
->argv
[1]);
5637 /* Add the element to the destination set */
5639 dstset
= createSetObject();
5640 dbAdd(c
->db
,c
->argv
[2],dstset
);
5642 if (dictAdd(dstset
->ptr
,c
->argv
[3],NULL
) == DICT_OK
)
5643 incrRefCount(c
->argv
[3]);
5644 addReply(c
,shared
.cone
);
5647 static void sismemberCommand(redisClient
*c
) {
5650 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5651 checkType(c
,set
,REDIS_SET
)) return;
5653 if (dictFind(set
->ptr
,c
->argv
[2]))
5654 addReply(c
,shared
.cone
);
5656 addReply(c
,shared
.czero
);
5659 static void scardCommand(redisClient
*c
) {
5663 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
5664 checkType(c
,o
,REDIS_SET
)) return;
5667 addReplyUlong(c
,dictSize(s
));
5670 static void spopCommand(redisClient
*c
) {
5674 if ((set
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
5675 checkType(c
,set
,REDIS_SET
)) return;
5677 de
= dictGetRandomKey(set
->ptr
);
5679 addReply(c
,shared
.nullbulk
);
5681 robj
*ele
= dictGetEntryKey(de
);
5683 addReplyBulk(c
,ele
);
5684 dictDelete(set
->ptr
,ele
);
5685 if (htNeedsResize(set
->ptr
)) dictResize(set
->ptr
);
5686 if (dictSize((dict
*)set
->ptr
) == 0) dbDelete(c
->db
,c
->argv
[1]);
5691 static void srandmemberCommand(redisClient
*c
) {
5695 if ((set
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
5696 checkType(c
,set
,REDIS_SET
)) return;
5698 de
= dictGetRandomKey(set
->ptr
);
5700 addReply(c
,shared
.nullbulk
);
5702 robj
*ele
= dictGetEntryKey(de
);
5704 addReplyBulk(c
,ele
);
5708 static int qsortCompareSetsByCardinality(const void *s1
, const void *s2
) {
5709 dict
**d1
= (void*) s1
, **d2
= (void*) s2
;
5711 return dictSize(*d1
)-dictSize(*d2
);
5714 static void sinterGenericCommand(redisClient
*c
, robj
**setskeys
, unsigned long setsnum
, robj
*dstkey
) {
5715 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
5718 robj
*lenobj
= NULL
, *dstset
= NULL
;
5719 unsigned long j
, cardinality
= 0;
5721 for (j
= 0; j
< setsnum
; j
++) {
5725 lookupKeyWrite(c
->db
,setskeys
[j
]) :
5726 lookupKeyRead(c
->db
,setskeys
[j
]);
5730 if (dbDelete(c
->db
,dstkey
))
5732 addReply(c
,shared
.czero
);
5734 addReply(c
,shared
.emptymultibulk
);
5738 if (setobj
->type
!= REDIS_SET
) {
5740 addReply(c
,shared
.wrongtypeerr
);
5743 dv
[j
] = setobj
->ptr
;
5745 /* Sort sets from the smallest to largest, this will improve our
5746 * algorithm's performace */
5747 qsort(dv
,setsnum
,sizeof(dict
*),qsortCompareSetsByCardinality
);
5749 /* The first thing we should output is the total number of elements...
5750 * since this is a multi-bulk write, but at this stage we don't know
5751 * the intersection set size, so we use a trick, append an empty object
5752 * to the output list and save the pointer to later modify it with the
5755 lenobj
= createObject(REDIS_STRING
,NULL
);
5757 decrRefCount(lenobj
);
5759 /* If we have a target key where to store the resulting set
5760 * create this key with an empty set inside */
5761 dstset
= createSetObject();
5764 /* Iterate all the elements of the first (smallest) set, and test
5765 * the element against all the other sets, if at least one set does
5766 * not include the element it is discarded */
5767 di
= dictGetIterator(dv
[0]);
5769 while((de
= dictNext(di
)) != NULL
) {
5772 for (j
= 1; j
< setsnum
; j
++)
5773 if (dictFind(dv
[j
],dictGetEntryKey(de
)) == NULL
) break;
5775 continue; /* at least one set does not contain the member */
5776 ele
= dictGetEntryKey(de
);
5778 addReplyBulk(c
,ele
);
5781 dictAdd(dstset
->ptr
,ele
,NULL
);
5785 dictReleaseIterator(di
);
5788 /* Store the resulting set into the target, if the intersection
5789 * is not an empty set. */
5790 dbDelete(c
->db
,dstkey
);
5791 if (dictSize((dict
*)dstset
->ptr
) > 0) {
5792 dbAdd(c
->db
,dstkey
,dstset
);
5793 addReplyLongLong(c
,dictSize((dict
*)dstset
->ptr
));
5795 decrRefCount(dstset
);
5796 addReply(c
,shared
.czero
);
5800 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",cardinality
);
5805 static void sinterCommand(redisClient
*c
) {
5806 sinterGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
);
5809 static void sinterstoreCommand(redisClient
*c
) {
5810 sinterGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1]);
5813 #define REDIS_OP_UNION 0
5814 #define REDIS_OP_DIFF 1
5815 #define REDIS_OP_INTER 2
5817 static void sunionDiffGenericCommand(redisClient
*c
, robj
**setskeys
, int setsnum
, robj
*dstkey
, int op
) {
5818 dict
**dv
= zmalloc(sizeof(dict
*)*setsnum
);
5821 robj
*dstset
= NULL
;
5822 int j
, cardinality
= 0;
5824 for (j
= 0; j
< setsnum
; j
++) {
5828 lookupKeyWrite(c
->db
,setskeys
[j
]) :
5829 lookupKeyRead(c
->db
,setskeys
[j
]);
5834 if (setobj
->type
!= REDIS_SET
) {
5836 addReply(c
,shared
.wrongtypeerr
);
5839 dv
[j
] = setobj
->ptr
;
5842 /* We need a temp set object to store our union. If the dstkey
5843 * is not NULL (that is, we are inside an SUNIONSTORE operation) then
5844 * this set object will be the resulting object to set into the target key*/
5845 dstset
= createSetObject();
5847 /* Iterate all the elements of all the sets, add every element a single
5848 * time to the result set */
5849 for (j
= 0; j
< setsnum
; j
++) {
5850 if (op
== REDIS_OP_DIFF
&& j
== 0 && !dv
[j
]) break; /* result set is empty */
5851 if (!dv
[j
]) continue; /* non existing keys are like empty sets */
5853 di
= dictGetIterator(dv
[j
]);
5855 while((de
= dictNext(di
)) != NULL
) {
5858 /* dictAdd will not add the same element multiple times */
5859 ele
= dictGetEntryKey(de
);
5860 if (op
== REDIS_OP_UNION
|| j
== 0) {
5861 if (dictAdd(dstset
->ptr
,ele
,NULL
) == DICT_OK
) {
5865 } else if (op
== REDIS_OP_DIFF
) {
5866 if (dictDelete(dstset
->ptr
,ele
) == DICT_OK
) {
5871 dictReleaseIterator(di
);
5873 /* result set is empty? Exit asap. */
5874 if (op
== REDIS_OP_DIFF
&& cardinality
== 0) break;
5877 /* Output the content of the resulting set, if not in STORE mode */
5879 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",cardinality
));
5880 di
= dictGetIterator(dstset
->ptr
);
5881 while((de
= dictNext(di
)) != NULL
) {
5884 ele
= dictGetEntryKey(de
);
5885 addReplyBulk(c
,ele
);
5887 dictReleaseIterator(di
);
5888 decrRefCount(dstset
);
5890 /* If we have a target key where to store the resulting set
5891 * create this key with the result set inside */
5892 dbDelete(c
->db
,dstkey
);
5893 if (dictSize((dict
*)dstset
->ptr
) > 0) {
5894 dbAdd(c
->db
,dstkey
,dstset
);
5895 addReplyLongLong(c
,dictSize((dict
*)dstset
->ptr
));
5897 decrRefCount(dstset
);
5898 addReply(c
,shared
.czero
);
5905 static void sunionCommand(redisClient
*c
) {
5906 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_UNION
);
5909 static void sunionstoreCommand(redisClient
*c
) {
5910 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_UNION
);
5913 static void sdiffCommand(redisClient
*c
) {
5914 sunionDiffGenericCommand(c
,c
->argv
+1,c
->argc
-1,NULL
,REDIS_OP_DIFF
);
5917 static void sdiffstoreCommand(redisClient
*c
) {
5918 sunionDiffGenericCommand(c
,c
->argv
+2,c
->argc
-2,c
->argv
[1],REDIS_OP_DIFF
);
5921 /* ==================================== ZSets =============================== */
5923 /* ZSETs are ordered sets using two data structures to hold the same elements
5924 * in order to get O(log(N)) INSERT and REMOVE operations into a sorted
5927 * The elements are added to an hash table mapping Redis objects to scores.
5928 * At the same time the elements are added to a skip list mapping scores
5929 * to Redis objects (so objects are sorted by scores in this "view"). */
5931 /* This skiplist implementation is almost a C translation of the original
5932 * algorithm described by William Pugh in "Skip Lists: A Probabilistic
5933 * Alternative to Balanced Trees", modified in three ways:
5934 * a) this implementation allows for repeated values.
5935 * b) the comparison is not just by key (our 'score') but by satellite data.
5936 * c) there is a back pointer, so it's a doubly linked list with the back
5937 * pointers being only at "level 1". This allows to traverse the list
5938 * from tail to head, useful for ZREVRANGE. */
5940 static zskiplistNode
*zslCreateNode(int level
, double score
, robj
*obj
) {
5941 zskiplistNode
*zn
= zmalloc(sizeof(*zn
));
5943 zn
->forward
= zmalloc(sizeof(zskiplistNode
*) * level
);
5945 zn
->span
= zmalloc(sizeof(unsigned int) * (level
- 1));
5953 static zskiplist
*zslCreate(void) {
5957 zsl
= zmalloc(sizeof(*zsl
));
5960 zsl
->header
= zslCreateNode(ZSKIPLIST_MAXLEVEL
,0,NULL
);
5961 for (j
= 0; j
< ZSKIPLIST_MAXLEVEL
; j
++) {
5962 zsl
->header
->forward
[j
] = NULL
;
5964 /* span has space for ZSKIPLIST_MAXLEVEL-1 elements */
5965 if (j
< ZSKIPLIST_MAXLEVEL
-1)
5966 zsl
->header
->span
[j
] = 0;
5968 zsl
->header
->backward
= NULL
;
5973 static void zslFreeNode(zskiplistNode
*node
) {
5974 decrRefCount(node
->obj
);
5975 zfree(node
->forward
);
5980 static void zslFree(zskiplist
*zsl
) {
5981 zskiplistNode
*node
= zsl
->header
->forward
[0], *next
;
5983 zfree(zsl
->header
->forward
);
5984 zfree(zsl
->header
->span
);
5987 next
= node
->forward
[0];
5994 static int zslRandomLevel(void) {
5996 while ((random()&0xFFFF) < (ZSKIPLIST_P
* 0xFFFF))
5998 return (level
<ZSKIPLIST_MAXLEVEL
) ? level
: ZSKIPLIST_MAXLEVEL
;
6001 static void zslInsert(zskiplist
*zsl
, double score
, robj
*obj
) {
6002 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
6003 unsigned int rank
[ZSKIPLIST_MAXLEVEL
];
6007 for (i
= zsl
->level
-1; i
>= 0; i
--) {
6008 /* store rank that is crossed to reach the insert position */
6009 rank
[i
] = i
== (zsl
->level
-1) ? 0 : rank
[i
+1];
6011 while (x
->forward
[i
] &&
6012 (x
->forward
[i
]->score
< score
||
6013 (x
->forward
[i
]->score
== score
&&
6014 compareStringObjects(x
->forward
[i
]->obj
,obj
) < 0))) {
6015 rank
[i
] += i
> 0 ? x
->span
[i
-1] : 1;
6020 /* we assume the key is not already inside, since we allow duplicated
6021 * scores, and the re-insertion of score and redis object should never
6022 * happpen since the caller of zslInsert() should test in the hash table
6023 * if the element is already inside or not. */
6024 level
= zslRandomLevel();
6025 if (level
> zsl
->level
) {
6026 for (i
= zsl
->level
; i
< level
; i
++) {
6028 update
[i
] = zsl
->header
;
6029 update
[i
]->span
[i
-1] = zsl
->length
;
6033 x
= zslCreateNode(level
,score
,obj
);
6034 for (i
= 0; i
< level
; i
++) {
6035 x
->forward
[i
] = update
[i
]->forward
[i
];
6036 update
[i
]->forward
[i
] = x
;
6038 /* update span covered by update[i] as x is inserted here */
6040 x
->span
[i
-1] = update
[i
]->span
[i
-1] - (rank
[0] - rank
[i
]);
6041 update
[i
]->span
[i
-1] = (rank
[0] - rank
[i
]) + 1;
6045 /* increment span for untouched levels */
6046 for (i
= level
; i
< zsl
->level
; i
++) {
6047 update
[i
]->span
[i
-1]++;
6050 x
->backward
= (update
[0] == zsl
->header
) ? NULL
: update
[0];
6052 x
->forward
[0]->backward
= x
;
6058 /* Internal function used by zslDelete, zslDeleteByScore and zslDeleteByRank */
6059 void zslDeleteNode(zskiplist
*zsl
, zskiplistNode
*x
, zskiplistNode
**update
) {
6061 for (i
= 0; i
< zsl
->level
; i
++) {
6062 if (update
[i
]->forward
[i
] == x
) {
6064 update
[i
]->span
[i
-1] += x
->span
[i
-1] - 1;
6066 update
[i
]->forward
[i
] = x
->forward
[i
];
6068 /* invariant: i > 0, because update[0]->forward[0]
6069 * is always equal to x */
6070 update
[i
]->span
[i
-1] -= 1;
6073 if (x
->forward
[0]) {
6074 x
->forward
[0]->backward
= x
->backward
;
6076 zsl
->tail
= x
->backward
;
6078 while(zsl
->level
> 1 && zsl
->header
->forward
[zsl
->level
-1] == NULL
)
6083 /* Delete an element with matching score/object from the skiplist. */
6084 static int zslDelete(zskiplist
*zsl
, double score
, robj
*obj
) {
6085 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
6089 for (i
= zsl
->level
-1; i
>= 0; i
--) {
6090 while (x
->forward
[i
] &&
6091 (x
->forward
[i
]->score
< score
||
6092 (x
->forward
[i
]->score
== score
&&
6093 compareStringObjects(x
->forward
[i
]->obj
,obj
) < 0)))
6097 /* We may have multiple elements with the same score, what we need
6098 * is to find the element with both the right score and object. */
6100 if (x
&& score
== x
->score
&& equalStringObjects(x
->obj
,obj
)) {
6101 zslDeleteNode(zsl
, x
, update
);
6105 return 0; /* not found */
6107 return 0; /* not found */
6110 /* Delete all the elements with score between min and max from the skiplist.
6111 * Min and mx are inclusive, so a score >= min || score <= max is deleted.
6112 * Note that this function takes the reference to the hash table view of the
6113 * sorted set, in order to remove the elements from the hash table too. */
6114 static unsigned long zslDeleteRangeByScore(zskiplist
*zsl
, double min
, double max
, dict
*dict
) {
6115 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
6116 unsigned long removed
= 0;
6120 for (i
= zsl
->level
-1; i
>= 0; i
--) {
6121 while (x
->forward
[i
] && x
->forward
[i
]->score
< min
)
6125 /* We may have multiple elements with the same score, what we need
6126 * is to find the element with both the right score and object. */
6128 while (x
&& x
->score
<= max
) {
6129 zskiplistNode
*next
= x
->forward
[0];
6130 zslDeleteNode(zsl
, x
, update
);
6131 dictDelete(dict
,x
->obj
);
6136 return removed
; /* not found */
6139 /* Delete all the elements with rank between start and end from the skiplist.
6140 * Start and end are inclusive. Note that start and end need to be 1-based */
6141 static unsigned long zslDeleteRangeByRank(zskiplist
*zsl
, unsigned int start
, unsigned int end
, dict
*dict
) {
6142 zskiplistNode
*update
[ZSKIPLIST_MAXLEVEL
], *x
;
6143 unsigned long traversed
= 0, removed
= 0;
6147 for (i
= zsl
->level
-1; i
>= 0; i
--) {
6148 while (x
->forward
[i
] && (traversed
+ (i
> 0 ? x
->span
[i
-1] : 1)) < start
) {
6149 traversed
+= i
> 0 ? x
->span
[i
-1] : 1;
6157 while (x
&& traversed
<= end
) {
6158 zskiplistNode
*next
= x
->forward
[0];
6159 zslDeleteNode(zsl
, x
, update
);
6160 dictDelete(dict
,x
->obj
);
6169 /* Find the first node having a score equal or greater than the specified one.
6170 * Returns NULL if there is no match. */
6171 static zskiplistNode
*zslFirstWithScore(zskiplist
*zsl
, double score
) {
6176 for (i
= zsl
->level
-1; i
>= 0; i
--) {
6177 while (x
->forward
[i
] && x
->forward
[i
]->score
< score
)
6180 /* We may have multiple elements with the same score, what we need
6181 * is to find the element with both the right score and object. */
6182 return x
->forward
[0];
6185 /* Find the rank for an element by both score and key.
6186 * Returns 0 when the element cannot be found, rank otherwise.
6187 * Note that the rank is 1-based due to the span of zsl->header to the
6189 static unsigned long zslistTypeGetRank(zskiplist
*zsl
, double score
, robj
*o
) {
6191 unsigned long rank
= 0;
6195 for (i
= zsl
->level
-1; i
>= 0; i
--) {
6196 while (x
->forward
[i
] &&
6197 (x
->forward
[i
]->score
< score
||
6198 (x
->forward
[i
]->score
== score
&&
6199 compareStringObjects(x
->forward
[i
]->obj
,o
) <= 0))) {
6200 rank
+= i
> 0 ? x
->span
[i
-1] : 1;
6204 /* x might be equal to zsl->header, so test if obj is non-NULL */
6205 if (x
->obj
&& equalStringObjects(x
->obj
,o
)) {
6212 /* Finds an element by its rank. The rank argument needs to be 1-based. */
6213 zskiplistNode
* zslistTypeGetElementByRank(zskiplist
*zsl
, unsigned long rank
) {
6215 unsigned long traversed
= 0;
6219 for (i
= zsl
->level
-1; i
>= 0; i
--) {
6220 while (x
->forward
[i
] && (traversed
+ (i
>0 ? x
->span
[i
-1] : 1)) <= rank
)
6222 traversed
+= i
> 0 ? x
->span
[i
-1] : 1;
6225 if (traversed
== rank
) {
6232 /* The actual Z-commands implementations */
6234 /* This generic command implements both ZADD and ZINCRBY.
6235 * scoreval is the score if the operation is a ZADD (doincrement == 0) or
6236 * the increment if the operation is a ZINCRBY (doincrement == 1). */
6237 static void zaddGenericCommand(redisClient
*c
, robj
*key
, robj
*ele
, double scoreval
, int doincrement
) {
6242 if (isnan(scoreval
)) {
6243 addReplySds(c
,sdsnew("-ERR provide score is Not A Number (nan)\r\n"));
6247 zsetobj
= lookupKeyWrite(c
->db
,key
);
6248 if (zsetobj
== NULL
) {
6249 zsetobj
= createZsetObject();
6250 dbAdd(c
->db
,key
,zsetobj
);
6252 if (zsetobj
->type
!= REDIS_ZSET
) {
6253 addReply(c
,shared
.wrongtypeerr
);
6259 /* Ok now since we implement both ZADD and ZINCRBY here the code
6260 * needs to handle the two different conditions. It's all about setting
6261 * '*score', that is, the new score to set, to the right value. */
6262 score
= zmalloc(sizeof(double));
6266 /* Read the old score. If the element was not present starts from 0 */
6267 de
= dictFind(zs
->dict
,ele
);
6269 double *oldscore
= dictGetEntryVal(de
);
6270 *score
= *oldscore
+ scoreval
;
6274 if (isnan(*score
)) {
6276 sdsnew("-ERR resulting score is Not A Number (nan)\r\n"));
6278 /* Note that we don't need to check if the zset may be empty and
6279 * should be removed here, as we can only obtain Nan as score if
6280 * there was already an element in the sorted set. */
6287 /* What follows is a simple remove and re-insert operation that is common
6288 * to both ZADD and ZINCRBY... */
6289 if (dictAdd(zs
->dict
,ele
,score
) == DICT_OK
) {
6290 /* case 1: New element */
6291 incrRefCount(ele
); /* added to hash */
6292 zslInsert(zs
->zsl
,*score
,ele
);
6293 incrRefCount(ele
); /* added to skiplist */
6296 addReplyDouble(c
,*score
);
6298 addReply(c
,shared
.cone
);
6303 /* case 2: Score update operation */
6304 de
= dictFind(zs
->dict
,ele
);
6305 redisAssert(de
!= NULL
);
6306 oldscore
= dictGetEntryVal(de
);
6307 if (*score
!= *oldscore
) {
6310 /* Remove and insert the element in the skip list with new score */
6311 deleted
= zslDelete(zs
->zsl
,*oldscore
,ele
);
6312 redisAssert(deleted
!= 0);
6313 zslInsert(zs
->zsl
,*score
,ele
);
6315 /* Update the score in the hash table */
6316 dictReplace(zs
->dict
,ele
,score
);
6322 addReplyDouble(c
,*score
);
6324 addReply(c
,shared
.czero
);
6328 static void zaddCommand(redisClient
*c
) {
6331 if (getDoubleFromObjectOrReply(c
, c
->argv
[2], &scoreval
, NULL
) != REDIS_OK
) return;
6332 zaddGenericCommand(c
,c
->argv
[1],c
->argv
[3],scoreval
,0);
6335 static void zincrbyCommand(redisClient
*c
) {
6338 if (getDoubleFromObjectOrReply(c
, c
->argv
[2], &scoreval
, NULL
) != REDIS_OK
) return;
6339 zaddGenericCommand(c
,c
->argv
[1],c
->argv
[3],scoreval
,1);
6342 static void zremCommand(redisClient
*c
) {
6349 if ((zsetobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6350 checkType(c
,zsetobj
,REDIS_ZSET
)) return;
6353 de
= dictFind(zs
->dict
,c
->argv
[2]);
6355 addReply(c
,shared
.czero
);
6358 /* Delete from the skiplist */
6359 oldscore
= dictGetEntryVal(de
);
6360 deleted
= zslDelete(zs
->zsl
,*oldscore
,c
->argv
[2]);
6361 redisAssert(deleted
!= 0);
6363 /* Delete from the hash table */
6364 dictDelete(zs
->dict
,c
->argv
[2]);
6365 if (htNeedsResize(zs
->dict
)) dictResize(zs
->dict
);
6366 if (dictSize(zs
->dict
) == 0) dbDelete(c
->db
,c
->argv
[1]);
6368 addReply(c
,shared
.cone
);
6371 static void zremrangebyscoreCommand(redisClient
*c
) {
6378 if ((getDoubleFromObjectOrReply(c
, c
->argv
[2], &min
, NULL
) != REDIS_OK
) ||
6379 (getDoubleFromObjectOrReply(c
, c
->argv
[3], &max
, NULL
) != REDIS_OK
)) return;
6381 if ((zsetobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6382 checkType(c
,zsetobj
,REDIS_ZSET
)) return;
6385 deleted
= zslDeleteRangeByScore(zs
->zsl
,min
,max
,zs
->dict
);
6386 if (htNeedsResize(zs
->dict
)) dictResize(zs
->dict
);
6387 if (dictSize(zs
->dict
) == 0) dbDelete(c
->db
,c
->argv
[1]);
6388 server
.dirty
+= deleted
;
6389 addReplyLongLong(c
,deleted
);
6392 static void zremrangebyrankCommand(redisClient
*c
) {
6400 if ((getLongFromObjectOrReply(c
, c
->argv
[2], &start
, NULL
) != REDIS_OK
) ||
6401 (getLongFromObjectOrReply(c
, c
->argv
[3], &end
, NULL
) != REDIS_OK
)) return;
6403 if ((zsetobj
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6404 checkType(c
,zsetobj
,REDIS_ZSET
)) return;
6406 llen
= zs
->zsl
->length
;
6408 /* convert negative indexes */
6409 if (start
< 0) start
= llen
+start
;
6410 if (end
< 0) end
= llen
+end
;
6411 if (start
< 0) start
= 0;
6413 /* Invariant: start >= 0, so this test will be true when end < 0.
6414 * The range is empty when start > end or start >= length. */
6415 if (start
> end
|| start
>= llen
) {
6416 addReply(c
,shared
.czero
);
6419 if (end
>= llen
) end
= llen
-1;
6421 /* increment start and end because zsl*Rank functions
6422 * use 1-based rank */
6423 deleted
= zslDeleteRangeByRank(zs
->zsl
,start
+1,end
+1,zs
->dict
);
6424 if (htNeedsResize(zs
->dict
)) dictResize(zs
->dict
);
6425 if (dictSize(zs
->dict
) == 0) dbDelete(c
->db
,c
->argv
[1]);
6426 server
.dirty
+= deleted
;
6427 addReplyLongLong(c
, deleted
);
6435 static int qsortCompareZsetopsrcByCardinality(const void *s1
, const void *s2
) {
6436 zsetopsrc
*d1
= (void*) s1
, *d2
= (void*) s2
;
6437 unsigned long size1
, size2
;
6438 size1
= d1
->dict
? dictSize(d1
->dict
) : 0;
6439 size2
= d2
->dict
? dictSize(d2
->dict
) : 0;
6440 return size1
- size2
;
6443 #define REDIS_AGGR_SUM 1
6444 #define REDIS_AGGR_MIN 2
6445 #define REDIS_AGGR_MAX 3
6446 #define zunionInterDictValue(_e) (dictGetEntryVal(_e) == NULL ? 1.0 : *(double*)dictGetEntryVal(_e))
6448 inline static void zunionInterAggregate(double *target
, double val
, int aggregate
) {
6449 if (aggregate
== REDIS_AGGR_SUM
) {
6450 *target
= *target
+ val
;
6451 } else if (aggregate
== REDIS_AGGR_MIN
) {
6452 *target
= val
< *target
? val
: *target
;
6453 } else if (aggregate
== REDIS_AGGR_MAX
) {
6454 *target
= val
> *target
? val
: *target
;
6457 redisPanic("Unknown ZUNION/INTER aggregate type");
6461 static void zunionInterGenericCommand(redisClient
*c
, robj
*dstkey
, int op
) {
6463 int aggregate
= REDIS_AGGR_SUM
;
6470 /* expect setnum input keys to be given */
6471 setnum
= atoi(c
->argv
[2]->ptr
);
6473 addReplySds(c
,sdsnew("-ERR at least 1 input key is needed for ZUNIONSTORE/ZINTERSTORE\r\n"));
6477 /* test if the expected number of keys would overflow */
6478 if (3+setnum
> c
->argc
) {
6479 addReply(c
,shared
.syntaxerr
);
6483 /* read keys to be used for input */
6484 src
= zmalloc(sizeof(zsetopsrc
) * setnum
);
6485 for (i
= 0, j
= 3; i
< setnum
; i
++, j
++) {
6486 robj
*obj
= lookupKeyWrite(c
->db
,c
->argv
[j
]);
6490 if (obj
->type
== REDIS_ZSET
) {
6491 src
[i
].dict
= ((zset
*)obj
->ptr
)->dict
;
6492 } else if (obj
->type
== REDIS_SET
) {
6493 src
[i
].dict
= (obj
->ptr
);
6496 addReply(c
,shared
.wrongtypeerr
);
6501 /* default all weights to 1 */
6502 src
[i
].weight
= 1.0;
6505 /* parse optional extra arguments */
6507 int remaining
= c
->argc
- j
;
6510 if (remaining
>= (setnum
+ 1) && !strcasecmp(c
->argv
[j
]->ptr
,"weights")) {
6512 for (i
= 0; i
< setnum
; i
++, j
++, remaining
--) {
6513 if (getDoubleFromObjectOrReply(c
, c
->argv
[j
], &src
[i
].weight
, NULL
) != REDIS_OK
)
6516 } else if (remaining
>= 2 && !strcasecmp(c
->argv
[j
]->ptr
,"aggregate")) {
6518 if (!strcasecmp(c
->argv
[j
]->ptr
,"sum")) {
6519 aggregate
= REDIS_AGGR_SUM
;
6520 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"min")) {
6521 aggregate
= REDIS_AGGR_MIN
;
6522 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"max")) {
6523 aggregate
= REDIS_AGGR_MAX
;
6526 addReply(c
,shared
.syntaxerr
);
6532 addReply(c
,shared
.syntaxerr
);
6538 /* sort sets from the smallest to largest, this will improve our
6539 * algorithm's performance */
6540 qsort(src
,setnum
,sizeof(zsetopsrc
),qsortCompareZsetopsrcByCardinality
);
6542 dstobj
= createZsetObject();
6543 dstzset
= dstobj
->ptr
;
6545 if (op
== REDIS_OP_INTER
) {
6546 /* skip going over all entries if the smallest zset is NULL or empty */
6547 if (src
[0].dict
&& dictSize(src
[0].dict
) > 0) {
6548 /* precondition: as src[0].dict is non-empty and the zsets are ordered
6549 * from small to large, all src[i > 0].dict are non-empty too */
6550 di
= dictGetIterator(src
[0].dict
);
6551 while((de
= dictNext(di
)) != NULL
) {
6552 double *score
= zmalloc(sizeof(double)), value
;
6553 *score
= src
[0].weight
* zunionInterDictValue(de
);
6555 for (j
= 1; j
< setnum
; j
++) {
6556 dictEntry
*other
= dictFind(src
[j
].dict
,dictGetEntryKey(de
));
6558 value
= src
[j
].weight
* zunionInterDictValue(other
);
6559 zunionInterAggregate(score
, value
, aggregate
);
6565 /* skip entry when not present in every source dict */
6569 robj
*o
= dictGetEntryKey(de
);
6570 dictAdd(dstzset
->dict
,o
,score
);
6571 incrRefCount(o
); /* added to dictionary */
6572 zslInsert(dstzset
->zsl
,*score
,o
);
6573 incrRefCount(o
); /* added to skiplist */
6576 dictReleaseIterator(di
);
6578 } else if (op
== REDIS_OP_UNION
) {
6579 for (i
= 0; i
< setnum
; i
++) {
6580 if (!src
[i
].dict
) continue;
6582 di
= dictGetIterator(src
[i
].dict
);
6583 while((de
= dictNext(di
)) != NULL
) {
6584 /* skip key when already processed */
6585 if (dictFind(dstzset
->dict
,dictGetEntryKey(de
)) != NULL
) continue;
6587 double *score
= zmalloc(sizeof(double)), value
;
6588 *score
= src
[i
].weight
* zunionInterDictValue(de
);
6590 /* because the zsets are sorted by size, its only possible
6591 * for sets at larger indices to hold this entry */
6592 for (j
= (i
+1); j
< setnum
; j
++) {
6593 dictEntry
*other
= dictFind(src
[j
].dict
,dictGetEntryKey(de
));
6595 value
= src
[j
].weight
* zunionInterDictValue(other
);
6596 zunionInterAggregate(score
, value
, aggregate
);
6600 robj
*o
= dictGetEntryKey(de
);
6601 dictAdd(dstzset
->dict
,o
,score
);
6602 incrRefCount(o
); /* added to dictionary */
6603 zslInsert(dstzset
->zsl
,*score
,o
);
6604 incrRefCount(o
); /* added to skiplist */
6606 dictReleaseIterator(di
);
6609 /* unknown operator */
6610 redisAssert(op
== REDIS_OP_INTER
|| op
== REDIS_OP_UNION
);
6613 dbDelete(c
->db
,dstkey
);
6614 if (dstzset
->zsl
->length
) {
6615 dbAdd(c
->db
,dstkey
,dstobj
);
6616 addReplyLongLong(c
, dstzset
->zsl
->length
);
6619 decrRefCount(dstobj
);
6620 addReply(c
, shared
.czero
);
6625 static void zunionstoreCommand(redisClient
*c
) {
6626 zunionInterGenericCommand(c
,c
->argv
[1], REDIS_OP_UNION
);
6629 static void zinterstoreCommand(redisClient
*c
) {
6630 zunionInterGenericCommand(c
,c
->argv
[1], REDIS_OP_INTER
);
6633 static void zrangeGenericCommand(redisClient
*c
, int reverse
) {
6645 if ((getLongFromObjectOrReply(c
, c
->argv
[2], &start
, NULL
) != REDIS_OK
) ||
6646 (getLongFromObjectOrReply(c
, c
->argv
[3], &end
, NULL
) != REDIS_OK
)) return;
6648 if (c
->argc
== 5 && !strcasecmp(c
->argv
[4]->ptr
,"withscores")) {
6650 } else if (c
->argc
>= 5) {
6651 addReply(c
,shared
.syntaxerr
);
6655 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.emptymultibulk
)) == NULL
6656 || checkType(c
,o
,REDIS_ZSET
)) return;
6661 /* convert negative indexes */
6662 if (start
< 0) start
= llen
+start
;
6663 if (end
< 0) end
= llen
+end
;
6664 if (start
< 0) start
= 0;
6666 /* Invariant: start >= 0, so this test will be true when end < 0.
6667 * The range is empty when start > end or start >= length. */
6668 if (start
> end
|| start
>= llen
) {
6669 addReply(c
,shared
.emptymultibulk
);
6672 if (end
>= llen
) end
= llen
-1;
6673 rangelen
= (end
-start
)+1;
6675 /* check if starting point is trivial, before searching
6676 * the element in log(N) time */
6678 ln
= start
== 0 ? zsl
->tail
: zslistTypeGetElementByRank(zsl
, llen
-start
);
6681 zsl
->header
->forward
[0] : zslistTypeGetElementByRank(zsl
, start
+1);
6684 /* Return the result in form of a multi-bulk reply */
6685 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",
6686 withscores
? (rangelen
*2) : rangelen
));
6687 for (j
= 0; j
< rangelen
; j
++) {
6689 addReplyBulk(c
,ele
);
6691 addReplyDouble(c
,ln
->score
);
6692 ln
= reverse
? ln
->backward
: ln
->forward
[0];
6696 static void zrangeCommand(redisClient
*c
) {
6697 zrangeGenericCommand(c
,0);
6700 static void zrevrangeCommand(redisClient
*c
) {
6701 zrangeGenericCommand(c
,1);
6704 /* This command implements both ZRANGEBYSCORE and ZCOUNT.
6705 * If justcount is non-zero, just the count is returned. */
6706 static void genericZrangebyscoreCommand(redisClient
*c
, int justcount
) {
6709 int minex
= 0, maxex
= 0; /* are min or max exclusive? */
6710 int offset
= 0, limit
= -1;
6714 /* Parse the min-max interval. If one of the values is prefixed
6715 * by the "(" character, it's considered "open". For instance
6716 * ZRANGEBYSCORE zset (1.5 (2.5 will match min < x < max
6717 * ZRANGEBYSCORE zset 1.5 2.5 will instead match min <= x <= max */
6718 if (((char*)c
->argv
[2]->ptr
)[0] == '(') {
6719 min
= strtod((char*)c
->argv
[2]->ptr
+1,NULL
);
6722 min
= strtod(c
->argv
[2]->ptr
,NULL
);
6724 if (((char*)c
->argv
[3]->ptr
)[0] == '(') {
6725 max
= strtod((char*)c
->argv
[3]->ptr
+1,NULL
);
6728 max
= strtod(c
->argv
[3]->ptr
,NULL
);
6731 /* Parse "WITHSCORES": note that if the command was called with
6732 * the name ZCOUNT then we are sure that c->argc == 4, so we'll never
6733 * enter the following paths to parse WITHSCORES and LIMIT. */
6734 if (c
->argc
== 5 || c
->argc
== 8) {
6735 if (strcasecmp(c
->argv
[c
->argc
-1]->ptr
,"withscores") == 0)
6740 if (c
->argc
!= (4 + withscores
) && c
->argc
!= (7 + withscores
))
6744 sdsnew("-ERR wrong number of arguments for ZRANGEBYSCORE\r\n"));
6749 if (c
->argc
== (7 + withscores
) && strcasecmp(c
->argv
[4]->ptr
,"limit")) {
6750 addReply(c
,shared
.syntaxerr
);
6752 } else if (c
->argc
== (7 + withscores
)) {
6753 offset
= atoi(c
->argv
[5]->ptr
);
6754 limit
= atoi(c
->argv
[6]->ptr
);
6755 if (offset
< 0) offset
= 0;
6758 /* Ok, lookup the key and get the range */
6759 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
6761 addReply(c
,justcount
? shared
.czero
: shared
.emptymultibulk
);
6763 if (o
->type
!= REDIS_ZSET
) {
6764 addReply(c
,shared
.wrongtypeerr
);
6766 zset
*zsetobj
= o
->ptr
;
6767 zskiplist
*zsl
= zsetobj
->zsl
;
6769 robj
*ele
, *lenobj
= NULL
;
6770 unsigned long rangelen
= 0;
6772 /* Get the first node with the score >= min, or with
6773 * score > min if 'minex' is true. */
6774 ln
= zslFirstWithScore(zsl
,min
);
6775 while (minex
&& ln
&& ln
->score
== min
) ln
= ln
->forward
[0];
6778 /* No element matching the speciifed interval */
6779 addReply(c
,justcount
? shared
.czero
: shared
.emptymultibulk
);
6783 /* We don't know in advance how many matching elements there
6784 * are in the list, so we push this object that will represent
6785 * the multi-bulk length in the output buffer, and will "fix"
6788 lenobj
= createObject(REDIS_STRING
,NULL
);
6790 decrRefCount(lenobj
);
6793 while(ln
&& (maxex
? (ln
->score
< max
) : (ln
->score
<= max
))) {
6796 ln
= ln
->forward
[0];
6799 if (limit
== 0) break;
6802 addReplyBulk(c
,ele
);
6804 addReplyDouble(c
,ln
->score
);
6806 ln
= ln
->forward
[0];
6808 if (limit
> 0) limit
--;
6811 addReplyLongLong(c
,(long)rangelen
);
6813 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",
6814 withscores
? (rangelen
*2) : rangelen
);
6820 static void zrangebyscoreCommand(redisClient
*c
) {
6821 genericZrangebyscoreCommand(c
,0);
6824 static void zcountCommand(redisClient
*c
) {
6825 genericZrangebyscoreCommand(c
,1);
6828 static void zcardCommand(redisClient
*c
) {
6832 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
6833 checkType(c
,o
,REDIS_ZSET
)) return;
6836 addReplyUlong(c
,zs
->zsl
->length
);
6839 static void zscoreCommand(redisClient
*c
) {
6844 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
6845 checkType(c
,o
,REDIS_ZSET
)) return;
6848 de
= dictFind(zs
->dict
,c
->argv
[2]);
6850 addReply(c
,shared
.nullbulk
);
6852 double *score
= dictGetEntryVal(de
);
6854 addReplyDouble(c
,*score
);
6858 static void zrankGenericCommand(redisClient
*c
, int reverse
) {
6866 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
6867 checkType(c
,o
,REDIS_ZSET
)) return;
6871 de
= dictFind(zs
->dict
,c
->argv
[2]);
6873 addReply(c
,shared
.nullbulk
);
6877 score
= dictGetEntryVal(de
);
6878 rank
= zslistTypeGetRank(zsl
, *score
, c
->argv
[2]);
6881 addReplyLongLong(c
, zsl
->length
- rank
);
6883 addReplyLongLong(c
, rank
-1);
6886 addReply(c
,shared
.nullbulk
);
6890 static void zrankCommand(redisClient
*c
) {
6891 zrankGenericCommand(c
, 0);
6894 static void zrevrankCommand(redisClient
*c
) {
6895 zrankGenericCommand(c
, 1);
6898 /* ========================= Hashes utility functions ======================= */
6899 #define REDIS_HASH_KEY 1
6900 #define REDIS_HASH_VALUE 2
6902 /* Check the length of a number of objects to see if we need to convert a
6903 * zipmap to a real hash. Note that we only check string encoded objects
6904 * as their string length can be queried in constant time. */
6905 static void hashTypeTryConversion(robj
*subject
, robj
**argv
, int start
, int end
) {
6907 if (subject
->encoding
!= REDIS_ENCODING_ZIPMAP
) return;
6909 for (i
= start
; i
<= end
; i
++) {
6910 if (argv
[i
]->encoding
== REDIS_ENCODING_RAW
&&
6911 sdslen(argv
[i
]->ptr
) > server
.hash_max_zipmap_value
)
6913 convertToRealHash(subject
);
6919 /* Encode given objects in-place when the hash uses a dict. */
6920 static void hashTypeTryObjectEncoding(robj
*subject
, robj
**o1
, robj
**o2
) {
6921 if (subject
->encoding
== REDIS_ENCODING_HT
) {
6922 if (o1
) *o1
= tryObjectEncoding(*o1
);
6923 if (o2
) *o2
= tryObjectEncoding(*o2
);
6927 /* Get the value from a hash identified by key. Returns either a string
6928 * object or NULL if the value cannot be found. The refcount of the object
6929 * is always increased by 1 when the value was found. */
6930 static robj
*hashTypeGet(robj
*o
, robj
*key
) {
6932 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6935 key
= getDecodedObject(key
);
6936 if (zipmapGet(o
->ptr
,key
->ptr
,sdslen(key
->ptr
),&v
,&vlen
)) {
6937 value
= createStringObject((char*)v
,vlen
);
6941 dictEntry
*de
= dictFind(o
->ptr
,key
);
6943 value
= dictGetEntryVal(de
);
6944 incrRefCount(value
);
6950 /* Test if the key exists in the given hash. Returns 1 if the key
6951 * exists and 0 when it doesn't. */
6952 static int hashTypeExists(robj
*o
, robj
*key
) {
6953 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6954 key
= getDecodedObject(key
);
6955 if (zipmapExists(o
->ptr
,key
->ptr
,sdslen(key
->ptr
))) {
6961 if (dictFind(o
->ptr
,key
) != NULL
) {
6968 /* Add an element, discard the old if the key already exists.
6969 * Return 0 on insert and 1 on update. */
6970 static int hashTypeSet(robj
*o
, robj
*key
, robj
*value
) {
6972 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
6973 key
= getDecodedObject(key
);
6974 value
= getDecodedObject(value
);
6975 o
->ptr
= zipmapSet(o
->ptr
,
6976 key
->ptr
,sdslen(key
->ptr
),
6977 value
->ptr
,sdslen(value
->ptr
), &update
);
6979 decrRefCount(value
);
6981 /* Check if the zipmap needs to be upgraded to a real hash table */
6982 if (zipmapLen(o
->ptr
) > server
.hash_max_zipmap_entries
)
6983 convertToRealHash(o
);
6985 if (dictReplace(o
->ptr
,key
,value
)) {
6992 incrRefCount(value
);
6997 /* Delete an element from a hash.
6998 * Return 1 on deleted and 0 on not found. */
6999 static int hashTypeDelete(robj
*o
, robj
*key
) {
7001 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
7002 key
= getDecodedObject(key
);
7003 o
->ptr
= zipmapDel(o
->ptr
,key
->ptr
,sdslen(key
->ptr
), &deleted
);
7006 deleted
= dictDelete((dict
*)o
->ptr
,key
) == DICT_OK
;
7007 /* Always check if the dictionary needs a resize after a delete. */
7008 if (deleted
&& htNeedsResize(o
->ptr
)) dictResize(o
->ptr
);
7013 /* Return the number of elements in a hash. */
7014 static unsigned long hashTypeLength(robj
*o
) {
7015 return (o
->encoding
== REDIS_ENCODING_ZIPMAP
) ?
7016 zipmapLen((unsigned char*)o
->ptr
) : dictSize((dict
*)o
->ptr
);
7019 /* Structure to hold hash iteration abstration. Note that iteration over
7020 * hashes involves both fields and values. Because it is possible that
7021 * not both are required, store pointers in the iterator to avoid
7022 * unnecessary memory allocation for fields/values. */
7026 unsigned char *zk
, *zv
;
7027 unsigned int zklen
, zvlen
;
7033 static hashTypeIterator
*hashTypeInitIterator(robj
*subject
) {
7034 hashTypeIterator
*hi
= zmalloc(sizeof(hashTypeIterator
));
7035 hi
->encoding
= subject
->encoding
;
7036 if (hi
->encoding
== REDIS_ENCODING_ZIPMAP
) {
7037 hi
->zi
= zipmapRewind(subject
->ptr
);
7038 } else if (hi
->encoding
== REDIS_ENCODING_HT
) {
7039 hi
->di
= dictGetIterator(subject
->ptr
);
7046 static void hashTypeReleaseIterator(hashTypeIterator
*hi
) {
7047 if (hi
->encoding
== REDIS_ENCODING_HT
) {
7048 dictReleaseIterator(hi
->di
);
7053 /* Move to the next entry in the hash. Return REDIS_OK when the next entry
7054 * could be found and REDIS_ERR when the iterator reaches the end. */
7055 static int hashTypeNext(hashTypeIterator
*hi
) {
7056 if (hi
->encoding
== REDIS_ENCODING_ZIPMAP
) {
7057 if ((hi
->zi
= zipmapNext(hi
->zi
, &hi
->zk
, &hi
->zklen
,
7058 &hi
->zv
, &hi
->zvlen
)) == NULL
) return REDIS_ERR
;
7060 if ((hi
->de
= dictNext(hi
->di
)) == NULL
) return REDIS_ERR
;
7065 /* Get key or value object at current iteration position.
7066 * This increases the refcount of the field object by 1. */
7067 static robj
*hashTypeCurrent(hashTypeIterator
*hi
, int what
) {
7069 if (hi
->encoding
== REDIS_ENCODING_ZIPMAP
) {
7070 if (what
& REDIS_HASH_KEY
) {
7071 o
= createStringObject((char*)hi
->zk
,hi
->zklen
);
7073 o
= createStringObject((char*)hi
->zv
,hi
->zvlen
);
7076 if (what
& REDIS_HASH_KEY
) {
7077 o
= dictGetEntryKey(hi
->de
);
7079 o
= dictGetEntryVal(hi
->de
);
7086 static robj
*hashTypeLookupWriteOrCreate(redisClient
*c
, robj
*key
) {
7087 robj
*o
= lookupKeyWrite(c
->db
,key
);
7089 o
= createHashObject();
7092 if (o
->type
!= REDIS_HASH
) {
7093 addReply(c
,shared
.wrongtypeerr
);
7100 /* ============================= Hash commands ============================== */
7101 static void hsetCommand(redisClient
*c
) {
7105 if ((o
= hashTypeLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
7106 hashTypeTryConversion(o
,c
->argv
,2,3);
7107 hashTypeTryObjectEncoding(o
,&c
->argv
[2], &c
->argv
[3]);
7108 update
= hashTypeSet(o
,c
->argv
[2],c
->argv
[3]);
7109 addReply(c
, update
? shared
.czero
: shared
.cone
);
7113 static void hsetnxCommand(redisClient
*c
) {
7115 if ((o
= hashTypeLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
7116 hashTypeTryConversion(o
,c
->argv
,2,3);
7118 if (hashTypeExists(o
, c
->argv
[2])) {
7119 addReply(c
, shared
.czero
);
7121 hashTypeTryObjectEncoding(o
,&c
->argv
[2], &c
->argv
[3]);
7122 hashTypeSet(o
,c
->argv
[2],c
->argv
[3]);
7123 addReply(c
, shared
.cone
);
7128 static void hmsetCommand(redisClient
*c
) {
7132 if ((c
->argc
% 2) == 1) {
7133 addReplySds(c
,sdsnew("-ERR wrong number of arguments for HMSET\r\n"));
7137 if ((o
= hashTypeLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
7138 hashTypeTryConversion(o
,c
->argv
,2,c
->argc
-1);
7139 for (i
= 2; i
< c
->argc
; i
+= 2) {
7140 hashTypeTryObjectEncoding(o
,&c
->argv
[i
], &c
->argv
[i
+1]);
7141 hashTypeSet(o
,c
->argv
[i
],c
->argv
[i
+1]);
7143 addReply(c
, shared
.ok
);
7147 static void hincrbyCommand(redisClient
*c
) {
7148 long long value
, incr
;
7149 robj
*o
, *current
, *new;
7151 if (getLongLongFromObjectOrReply(c
,c
->argv
[3],&incr
,NULL
) != REDIS_OK
) return;
7152 if ((o
= hashTypeLookupWriteOrCreate(c
,c
->argv
[1])) == NULL
) return;
7153 if ((current
= hashTypeGet(o
,c
->argv
[2])) != NULL
) {
7154 if (getLongLongFromObjectOrReply(c
,current
,&value
,
7155 "hash value is not an integer") != REDIS_OK
) {
7156 decrRefCount(current
);
7159 decrRefCount(current
);
7165 new = createStringObjectFromLongLong(value
);
7166 hashTypeTryObjectEncoding(o
,&c
->argv
[2],NULL
);
7167 hashTypeSet(o
,c
->argv
[2],new);
7169 addReplyLongLong(c
,value
);
7173 static void hgetCommand(redisClient
*c
) {
7175 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.nullbulk
)) == NULL
||
7176 checkType(c
,o
,REDIS_HASH
)) return;
7178 if ((value
= hashTypeGet(o
,c
->argv
[2])) != NULL
) {
7179 addReplyBulk(c
,value
);
7180 decrRefCount(value
);
7182 addReply(c
,shared
.nullbulk
);
7186 static void hmgetCommand(redisClient
*c
) {
7189 o
= lookupKeyRead(c
->db
,c
->argv
[1]);
7190 if (o
!= NULL
&& o
->type
!= REDIS_HASH
) {
7191 addReply(c
,shared
.wrongtypeerr
);
7194 /* Note the check for o != NULL happens inside the loop. This is
7195 * done because objects that cannot be found are considered to be
7196 * an empty hash. The reply should then be a series of NULLs. */
7197 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->argc
-2));
7198 for (i
= 2; i
< c
->argc
; i
++) {
7199 if (o
!= NULL
&& (value
= hashTypeGet(o
,c
->argv
[i
])) != NULL
) {
7200 addReplyBulk(c
,value
);
7201 decrRefCount(value
);
7203 addReply(c
,shared
.nullbulk
);
7208 static void hdelCommand(redisClient
*c
) {
7210 if ((o
= lookupKeyWriteOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
7211 checkType(c
,o
,REDIS_HASH
)) return;
7213 if (hashTypeDelete(o
,c
->argv
[2])) {
7214 if (hashTypeLength(o
) == 0) dbDelete(c
->db
,c
->argv
[1]);
7215 addReply(c
,shared
.cone
);
7218 addReply(c
,shared
.czero
);
7222 static void hlenCommand(redisClient
*c
) {
7224 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
7225 checkType(c
,o
,REDIS_HASH
)) return;
7227 addReplyUlong(c
,hashTypeLength(o
));
7230 static void genericHgetallCommand(redisClient
*c
, int flags
) {
7231 robj
*o
, *lenobj
, *obj
;
7232 unsigned long count
= 0;
7233 hashTypeIterator
*hi
;
7235 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.emptymultibulk
)) == NULL
7236 || checkType(c
,o
,REDIS_HASH
)) return;
7238 lenobj
= createObject(REDIS_STRING
,NULL
);
7240 decrRefCount(lenobj
);
7242 hi
= hashTypeInitIterator(o
);
7243 while (hashTypeNext(hi
) != REDIS_ERR
) {
7244 if (flags
& REDIS_HASH_KEY
) {
7245 obj
= hashTypeCurrent(hi
,REDIS_HASH_KEY
);
7246 addReplyBulk(c
,obj
);
7250 if (flags
& REDIS_HASH_VALUE
) {
7251 obj
= hashTypeCurrent(hi
,REDIS_HASH_VALUE
);
7252 addReplyBulk(c
,obj
);
7257 hashTypeReleaseIterator(hi
);
7259 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%lu\r\n",count
);
7262 static void hkeysCommand(redisClient
*c
) {
7263 genericHgetallCommand(c
,REDIS_HASH_KEY
);
7266 static void hvalsCommand(redisClient
*c
) {
7267 genericHgetallCommand(c
,REDIS_HASH_VALUE
);
7270 static void hgetallCommand(redisClient
*c
) {
7271 genericHgetallCommand(c
,REDIS_HASH_KEY
|REDIS_HASH_VALUE
);
7274 static void hexistsCommand(redisClient
*c
) {
7276 if ((o
= lookupKeyReadOrReply(c
,c
->argv
[1],shared
.czero
)) == NULL
||
7277 checkType(c
,o
,REDIS_HASH
)) return;
7279 addReply(c
, hashTypeExists(o
,c
->argv
[2]) ? shared
.cone
: shared
.czero
);
7282 static void convertToRealHash(robj
*o
) {
7283 unsigned char *key
, *val
, *p
, *zm
= o
->ptr
;
7284 unsigned int klen
, vlen
;
7285 dict
*dict
= dictCreate(&hashDictType
,NULL
);
7287 assert(o
->type
== REDIS_HASH
&& o
->encoding
!= REDIS_ENCODING_HT
);
7288 p
= zipmapRewind(zm
);
7289 while((p
= zipmapNext(p
,&key
,&klen
,&val
,&vlen
)) != NULL
) {
7290 robj
*keyobj
, *valobj
;
7292 keyobj
= createStringObject((char*)key
,klen
);
7293 valobj
= createStringObject((char*)val
,vlen
);
7294 keyobj
= tryObjectEncoding(keyobj
);
7295 valobj
= tryObjectEncoding(valobj
);
7296 dictAdd(dict
,keyobj
,valobj
);
7298 o
->encoding
= REDIS_ENCODING_HT
;
7303 /* ========================= Non type-specific commands ==================== */
7305 static void flushdbCommand(redisClient
*c
) {
7306 server
.dirty
+= dictSize(c
->db
->dict
);
7307 touchWatchedKeysOnFlush(c
->db
->id
);
7308 dictEmpty(c
->db
->dict
);
7309 dictEmpty(c
->db
->expires
);
7310 addReply(c
,shared
.ok
);
7313 static void flushallCommand(redisClient
*c
) {
7314 touchWatchedKeysOnFlush(-1);
7315 server
.dirty
+= emptyDb();
7316 addReply(c
,shared
.ok
);
7317 if (server
.bgsavechildpid
!= -1) {
7318 kill(server
.bgsavechildpid
,SIGKILL
);
7319 rdbRemoveTempFile(server
.bgsavechildpid
);
7321 rdbSave(server
.dbfilename
);
7325 static redisSortOperation
*createSortOperation(int type
, robj
*pattern
) {
7326 redisSortOperation
*so
= zmalloc(sizeof(*so
));
7328 so
->pattern
= pattern
;
7332 /* Return the value associated to the key with a name obtained
7333 * substituting the first occurence of '*' in 'pattern' with 'subst'.
7334 * The returned object will always have its refcount increased by 1
7335 * when it is non-NULL. */
7336 static robj
*lookupKeyByPattern(redisDb
*db
, robj
*pattern
, robj
*subst
) {
7339 robj keyobj
, fieldobj
, *o
;
7340 int prefixlen
, sublen
, postfixlen
, fieldlen
;
7341 /* Expoit the internal sds representation to create a sds string allocated on the stack in order to make this function faster */
7345 char buf
[REDIS_SORTKEY_MAX
+1];
7346 } keyname
, fieldname
;
7348 /* If the pattern is "#" return the substitution object itself in order
7349 * to implement the "SORT ... GET #" feature. */
7350 spat
= pattern
->ptr
;
7351 if (spat
[0] == '#' && spat
[1] == '\0') {
7352 incrRefCount(subst
);
7356 /* The substitution object may be specially encoded. If so we create
7357 * a decoded object on the fly. Otherwise getDecodedObject will just
7358 * increment the ref count, that we'll decrement later. */
7359 subst
= getDecodedObject(subst
);
7362 if (sdslen(spat
)+sdslen(ssub
)-1 > REDIS_SORTKEY_MAX
) return NULL
;
7363 p
= strchr(spat
,'*');
7365 decrRefCount(subst
);
7369 /* Find out if we're dealing with a hash dereference. */
7370 if ((f
= strstr(p
+1, "->")) != NULL
) {
7371 fieldlen
= sdslen(spat
)-(f
-spat
);
7372 /* this also copies \0 character */
7373 memcpy(fieldname
.buf
,f
+2,fieldlen
-1);
7374 fieldname
.len
= fieldlen
-2;
7380 sublen
= sdslen(ssub
);
7381 postfixlen
= sdslen(spat
)-(prefixlen
+1)-fieldlen
;
7382 memcpy(keyname
.buf
,spat
,prefixlen
);
7383 memcpy(keyname
.buf
+prefixlen
,ssub
,sublen
);
7384 memcpy(keyname
.buf
+prefixlen
+sublen
,p
+1,postfixlen
);
7385 keyname
.buf
[prefixlen
+sublen
+postfixlen
] = '\0';
7386 keyname
.len
= prefixlen
+sublen
+postfixlen
;
7387 decrRefCount(subst
);
7389 /* Lookup substituted key */
7390 initStaticStringObject(keyobj
,((char*)&keyname
)+(sizeof(long)*2));
7391 o
= lookupKeyRead(db
,&keyobj
);
7392 if (o
== NULL
) return NULL
;
7395 if (o
->type
!= REDIS_HASH
|| fieldname
.len
< 1) return NULL
;
7397 /* Retrieve value from hash by the field name. This operation
7398 * already increases the refcount of the returned object. */
7399 initStaticStringObject(fieldobj
,((char*)&fieldname
)+(sizeof(long)*2));
7400 o
= hashTypeGet(o
, &fieldobj
);
7402 if (o
->type
!= REDIS_STRING
) return NULL
;
7404 /* Every object that this function returns needs to have its refcount
7405 * increased. sortCommand decreases it again. */
7412 /* sortCompare() is used by qsort in sortCommand(). Given that qsort_r with
7413 * the additional parameter is not standard but a BSD-specific we have to
7414 * pass sorting parameters via the global 'server' structure */
7415 static int sortCompare(const void *s1
, const void *s2
) {
7416 const redisSortObject
*so1
= s1
, *so2
= s2
;
7419 if (!server
.sort_alpha
) {
7420 /* Numeric sorting. Here it's trivial as we precomputed scores */
7421 if (so1
->u
.score
> so2
->u
.score
) {
7423 } else if (so1
->u
.score
< so2
->u
.score
) {
7429 /* Alphanumeric sorting */
7430 if (server
.sort_bypattern
) {
7431 if (!so1
->u
.cmpobj
|| !so2
->u
.cmpobj
) {
7432 /* At least one compare object is NULL */
7433 if (so1
->u
.cmpobj
== so2
->u
.cmpobj
)
7435 else if (so1
->u
.cmpobj
== NULL
)
7440 /* We have both the objects, use strcoll */
7441 cmp
= strcoll(so1
->u
.cmpobj
->ptr
,so2
->u
.cmpobj
->ptr
);
7444 /* Compare elements directly. */
7445 cmp
= compareStringObjects(so1
->obj
,so2
->obj
);
7448 return server
.sort_desc
? -cmp
: cmp
;
7451 /* The SORT command is the most complex command in Redis. Warning: this code
7452 * is optimized for speed and a bit less for readability */
7453 static void sortCommand(redisClient
*c
) {
7455 unsigned int outputlen
= 0;
7456 int desc
= 0, alpha
= 0;
7457 int limit_start
= 0, limit_count
= -1, start
, end
;
7458 int j
, dontsort
= 0, vectorlen
;
7459 int getop
= 0; /* GET operation counter */
7460 robj
*sortval
, *sortby
= NULL
, *storekey
= NULL
;
7461 redisSortObject
*vector
; /* Resulting vector to sort */
7463 /* Lookup the key to sort. It must be of the right types */
7464 sortval
= lookupKeyRead(c
->db
,c
->argv
[1]);
7465 if (sortval
== NULL
) {
7466 addReply(c
,shared
.emptymultibulk
);
7469 if (sortval
->type
!= REDIS_SET
&& sortval
->type
!= REDIS_LIST
&&
7470 sortval
->type
!= REDIS_ZSET
)
7472 addReply(c
,shared
.wrongtypeerr
);
7476 /* Create a list of operations to perform for every sorted element.
7477 * Operations can be GET/DEL/INCR/DECR */
7478 operations
= listCreate();
7479 listSetFreeMethod(operations
,zfree
);
7482 /* Now we need to protect sortval incrementing its count, in the future
7483 * SORT may have options able to overwrite/delete keys during the sorting
7484 * and the sorted key itself may get destroied */
7485 incrRefCount(sortval
);
7487 /* The SORT command has an SQL-alike syntax, parse it */
7488 while(j
< c
->argc
) {
7489 int leftargs
= c
->argc
-j
-1;
7490 if (!strcasecmp(c
->argv
[j
]->ptr
,"asc")) {
7492 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"desc")) {
7494 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"alpha")) {
7496 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"limit") && leftargs
>= 2) {
7497 limit_start
= atoi(c
->argv
[j
+1]->ptr
);
7498 limit_count
= atoi(c
->argv
[j
+2]->ptr
);
7500 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"store") && leftargs
>= 1) {
7501 storekey
= c
->argv
[j
+1];
7503 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"by") && leftargs
>= 1) {
7504 sortby
= c
->argv
[j
+1];
7505 /* If the BY pattern does not contain '*', i.e. it is constant,
7506 * we don't need to sort nor to lookup the weight keys. */
7507 if (strchr(c
->argv
[j
+1]->ptr
,'*') == NULL
) dontsort
= 1;
7509 } else if (!strcasecmp(c
->argv
[j
]->ptr
,"get") && leftargs
>= 1) {
7510 listAddNodeTail(operations
,createSortOperation(
7511 REDIS_SORT_GET
,c
->argv
[j
+1]));
7515 decrRefCount(sortval
);
7516 listRelease(operations
);
7517 addReply(c
,shared
.syntaxerr
);
7523 /* Load the sorting vector with all the objects to sort */
7524 switch(sortval
->type
) {
7525 case REDIS_LIST
: vectorlen
= listTypeLength(sortval
); break;
7526 case REDIS_SET
: vectorlen
= dictSize((dict
*)sortval
->ptr
); break;
7527 case REDIS_ZSET
: vectorlen
= dictSize(((zset
*)sortval
->ptr
)->dict
); break;
7528 default: vectorlen
= 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
7530 vector
= zmalloc(sizeof(redisSortObject
)*vectorlen
);
7533 if (sortval
->type
== REDIS_LIST
) {
7534 listTypeIterator
*li
= listTypeInitIterator(sortval
,0,REDIS_TAIL
);
7535 listTypeEntry entry
;
7536 while(listTypeNext(li
,&entry
)) {
7537 vector
[j
].obj
= listTypeGet(&entry
);
7538 vector
[j
].u
.score
= 0;
7539 vector
[j
].u
.cmpobj
= NULL
;
7542 listTypeReleaseIterator(li
);
7548 if (sortval
->type
== REDIS_SET
) {
7551 zset
*zs
= sortval
->ptr
;
7555 di
= dictGetIterator(set
);
7556 while((setele
= dictNext(di
)) != NULL
) {
7557 vector
[j
].obj
= dictGetEntryKey(setele
);
7558 vector
[j
].u
.score
= 0;
7559 vector
[j
].u
.cmpobj
= NULL
;
7562 dictReleaseIterator(di
);
7564 redisAssert(j
== vectorlen
);
7566 /* Now it's time to load the right scores in the sorting vector */
7567 if (dontsort
== 0) {
7568 for (j
= 0; j
< vectorlen
; j
++) {
7571 /* lookup value to sort by */
7572 byval
= lookupKeyByPattern(c
->db
,sortby
,vector
[j
].obj
);
7573 if (!byval
) continue;
7575 /* use object itself to sort by */
7576 byval
= vector
[j
].obj
;
7580 if (sortby
) vector
[j
].u
.cmpobj
= getDecodedObject(byval
);
7582 if (byval
->encoding
== REDIS_ENCODING_RAW
) {
7583 vector
[j
].u
.score
= strtod(byval
->ptr
,NULL
);
7584 } else if (byval
->encoding
== REDIS_ENCODING_INT
) {
7585 /* Don't need to decode the object if it's
7586 * integer-encoded (the only encoding supported) so
7587 * far. We can just cast it */
7588 vector
[j
].u
.score
= (long)byval
->ptr
;
7590 redisAssert(1 != 1);
7594 /* when the object was retrieved using lookupKeyByPattern,
7595 * its refcount needs to be decreased. */
7597 decrRefCount(byval
);
7602 /* We are ready to sort the vector... perform a bit of sanity check
7603 * on the LIMIT option too. We'll use a partial version of quicksort. */
7604 start
= (limit_start
< 0) ? 0 : limit_start
;
7605 end
= (limit_count
< 0) ? vectorlen
-1 : start
+limit_count
-1;
7606 if (start
>= vectorlen
) {
7607 start
= vectorlen
-1;
7610 if (end
>= vectorlen
) end
= vectorlen
-1;
7612 if (dontsort
== 0) {
7613 server
.sort_desc
= desc
;
7614 server
.sort_alpha
= alpha
;
7615 server
.sort_bypattern
= sortby
? 1 : 0;
7616 if (sortby
&& (start
!= 0 || end
!= vectorlen
-1))
7617 pqsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
, start
,end
);
7619 qsort(vector
,vectorlen
,sizeof(redisSortObject
),sortCompare
);
7622 /* Send command output to the output buffer, performing the specified
7623 * GET/DEL/INCR/DECR operations if any. */
7624 outputlen
= getop
? getop
*(end
-start
+1) : end
-start
+1;
7625 if (storekey
== NULL
) {
7626 /* STORE option not specified, sent the sorting result to client */
7627 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",outputlen
));
7628 for (j
= start
; j
<= end
; j
++) {
7632 if (!getop
) addReplyBulk(c
,vector
[j
].obj
);
7633 listRewind(operations
,&li
);
7634 while((ln
= listNext(&li
))) {
7635 redisSortOperation
*sop
= ln
->value
;
7636 robj
*val
= lookupKeyByPattern(c
->db
,sop
->pattern
,
7639 if (sop
->type
== REDIS_SORT_GET
) {
7641 addReply(c
,shared
.nullbulk
);
7643 addReplyBulk(c
,val
);
7647 redisAssert(sop
->type
== REDIS_SORT_GET
); /* always fails */
7652 robj
*sobj
= createZiplistObject();
7654 /* STORE option specified, set the sorting result as a List object */
7655 for (j
= start
; j
<= end
; j
++) {
7660 listTypePush(sobj
,vector
[j
].obj
,REDIS_TAIL
);
7662 listRewind(operations
,&li
);
7663 while((ln
= listNext(&li
))) {
7664 redisSortOperation
*sop
= ln
->value
;
7665 robj
*val
= lookupKeyByPattern(c
->db
,sop
->pattern
,
7668 if (sop
->type
== REDIS_SORT_GET
) {
7669 if (!val
) val
= createStringObject("",0);
7671 /* listTypePush does an incrRefCount, so we should take care
7672 * care of the incremented refcount caused by either
7673 * lookupKeyByPattern or createStringObject("",0) */
7674 listTypePush(sobj
,val
,REDIS_TAIL
);
7678 redisAssert(sop
->type
== REDIS_SORT_GET
);
7683 dbReplace(c
->db
,storekey
,sobj
);
7684 /* Note: we add 1 because the DB is dirty anyway since even if the
7685 * SORT result is empty a new key is set and maybe the old content
7687 server
.dirty
+= 1+outputlen
;
7688 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",outputlen
));
7692 if (sortval
->type
== REDIS_LIST
)
7693 for (j
= 0; j
< vectorlen
; j
++)
7694 decrRefCount(vector
[j
].obj
);
7695 decrRefCount(sortval
);
7696 listRelease(operations
);
7697 for (j
= 0; j
< vectorlen
; j
++) {
7698 if (alpha
&& vector
[j
].u
.cmpobj
)
7699 decrRefCount(vector
[j
].u
.cmpobj
);
7704 /* Convert an amount of bytes into a human readable string in the form
7705 * of 100B, 2G, 100M, 4K, and so forth. */
7706 static void bytesToHuman(char *s
, unsigned long long n
) {
7711 sprintf(s
,"%lluB",n
);
7713 } else if (n
< (1024*1024)) {
7714 d
= (double)n
/(1024);
7715 sprintf(s
,"%.2fK",d
);
7716 } else if (n
< (1024LL*1024*1024)) {
7717 d
= (double)n
/(1024*1024);
7718 sprintf(s
,"%.2fM",d
);
7719 } else if (n
< (1024LL*1024*1024*1024)) {
7720 d
= (double)n
/(1024LL*1024*1024);
7721 sprintf(s
,"%.2fG",d
);
7725 /* Create the string returned by the INFO command. This is decoupled
7726 * by the INFO command itself as we need to report the same information
7727 * on memory corruption problems. */
7728 static sds
genRedisInfoString(void) {
7730 time_t uptime
= time(NULL
)-server
.stat_starttime
;
7734 bytesToHuman(hmem
,zmalloc_used_memory());
7735 info
= sdscatprintf(sdsempty(),
7736 "redis_version:%s\r\n"
7737 "redis_git_sha1:%s\r\n"
7738 "redis_git_dirty:%d\r\n"
7740 "multiplexing_api:%s\r\n"
7741 "process_id:%ld\r\n"
7742 "uptime_in_seconds:%ld\r\n"
7743 "uptime_in_days:%ld\r\n"
7744 "connected_clients:%d\r\n"
7745 "connected_slaves:%d\r\n"
7746 "blocked_clients:%d\r\n"
7747 "used_memory:%zu\r\n"
7748 "used_memory_human:%s\r\n"
7749 "changes_since_last_save:%lld\r\n"
7750 "bgsave_in_progress:%d\r\n"
7751 "last_save_time:%ld\r\n"
7752 "bgrewriteaof_in_progress:%d\r\n"
7753 "total_connections_received:%lld\r\n"
7754 "total_commands_processed:%lld\r\n"
7755 "expired_keys:%lld\r\n"
7756 "hash_max_zipmap_entries:%zu\r\n"
7757 "hash_max_zipmap_value:%zu\r\n"
7758 "pubsub_channels:%ld\r\n"
7759 "pubsub_patterns:%u\r\n"
7764 strtol(redisGitDirty(),NULL
,10) > 0,
7765 (sizeof(long) == 8) ? "64" : "32",
7770 listLength(server
.clients
)-listLength(server
.slaves
),
7771 listLength(server
.slaves
),
7772 server
.blpop_blocked_clients
,
7773 zmalloc_used_memory(),
7776 server
.bgsavechildpid
!= -1,
7778 server
.bgrewritechildpid
!= -1,
7779 server
.stat_numconnections
,
7780 server
.stat_numcommands
,
7781 server
.stat_expiredkeys
,
7782 server
.hash_max_zipmap_entries
,
7783 server
.hash_max_zipmap_value
,
7784 dictSize(server
.pubsub_channels
),
7785 listLength(server
.pubsub_patterns
),
7786 server
.vm_enabled
!= 0,
7787 server
.masterhost
== NULL
? "master" : "slave"
7789 if (server
.masterhost
) {
7790 info
= sdscatprintf(info
,
7791 "master_host:%s\r\n"
7792 "master_port:%d\r\n"
7793 "master_link_status:%s\r\n"
7794 "master_last_io_seconds_ago:%d\r\n"
7797 (server
.replstate
== REDIS_REPL_CONNECTED
) ?
7799 server
.master
? ((int)(time(NULL
)-server
.master
->lastinteraction
)) : -1
7802 if (server
.vm_enabled
) {
7804 info
= sdscatprintf(info
,
7805 "vm_conf_max_memory:%llu\r\n"
7806 "vm_conf_page_size:%llu\r\n"
7807 "vm_conf_pages:%llu\r\n"
7808 "vm_stats_used_pages:%llu\r\n"
7809 "vm_stats_swapped_objects:%llu\r\n"
7810 "vm_stats_swappin_count:%llu\r\n"
7811 "vm_stats_swappout_count:%llu\r\n"
7812 "vm_stats_io_newjobs_len:%lu\r\n"
7813 "vm_stats_io_processing_len:%lu\r\n"
7814 "vm_stats_io_processed_len:%lu\r\n"
7815 "vm_stats_io_active_threads:%lu\r\n"
7816 "vm_stats_blocked_clients:%lu\r\n"
7817 ,(unsigned long long) server
.vm_max_memory
,
7818 (unsigned long long) server
.vm_page_size
,
7819 (unsigned long long) server
.vm_pages
,
7820 (unsigned long long) server
.vm_stats_used_pages
,
7821 (unsigned long long) server
.vm_stats_swapped_objects
,
7822 (unsigned long long) server
.vm_stats_swapins
,
7823 (unsigned long long) server
.vm_stats_swapouts
,
7824 (unsigned long) listLength(server
.io_newjobs
),
7825 (unsigned long) listLength(server
.io_processing
),
7826 (unsigned long) listLength(server
.io_processed
),
7827 (unsigned long) server
.io_active_threads
,
7828 (unsigned long) server
.vm_blocked_clients
7832 for (j
= 0; j
< server
.dbnum
; j
++) {
7833 long long keys
, vkeys
;
7835 keys
= dictSize(server
.db
[j
].dict
);
7836 vkeys
= dictSize(server
.db
[j
].expires
);
7837 if (keys
|| vkeys
) {
7838 info
= sdscatprintf(info
, "db%d:keys=%lld,expires=%lld\r\n",
7845 static void infoCommand(redisClient
*c
) {
7846 sds info
= genRedisInfoString();
7847 addReplySds(c
,sdscatprintf(sdsempty(),"$%lu\r\n",
7848 (unsigned long)sdslen(info
)));
7849 addReplySds(c
,info
);
7850 addReply(c
,shared
.crlf
);
7853 static void monitorCommand(redisClient
*c
) {
7854 /* ignore MONITOR if aleady slave or in monitor mode */
7855 if (c
->flags
& REDIS_SLAVE
) return;
7857 c
->flags
|= (REDIS_SLAVE
|REDIS_MONITOR
);
7859 listAddNodeTail(server
.monitors
,c
);
7860 addReply(c
,shared
.ok
);
7863 /* ================================= Expire ================================= */
7864 static int removeExpire(redisDb
*db
, robj
*key
) {
7865 /* An expire may only be removed if there is a corresponding entry in the
7866 * main dict. Otherwise, the key will never be freed. */
7867 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
7868 if (dictDelete(db
->expires
,key
->ptr
) == DICT_OK
) {
7875 static int setExpire(redisDb
*db
, robj
*key
, time_t when
) {
7878 /* Reuse the sds from the main dict in the expire dict */
7879 redisAssert((de
= dictFind(db
->dict
,key
->ptr
)) != NULL
);
7880 if (dictAdd(db
->expires
,dictGetEntryKey(de
),(void*)when
) == DICT_ERR
) {
7887 /* Return the expire time of the specified key, or -1 if no expire
7888 * is associated with this key (i.e. the key is non volatile) */
7889 static time_t getExpire(redisDb
*db
, robj
*key
) {
7892 /* No expire? return ASAP */
7893 if (dictSize(db
->expires
) == 0 ||
7894 (de
= dictFind(db
->expires
,key
->ptr
)) == NULL
) return -1;
7896 /* The entry was found in the expire dict, this means it should also
7897 * be present in the main dict (safety check). */
7898 redisAssert(dictFind(db
->dict
,key
->ptr
) != NULL
);
7899 return (time_t) dictGetEntryVal(de
);
7902 static int expireIfNeeded(redisDb
*db
, robj
*key
) {
7903 time_t when
= getExpire(db
,key
);
7904 if (when
< 0) return 0;
7906 /* Return when this key has not expired */
7907 if (time(NULL
) <= when
) return 0;
7909 /* Delete the key */
7910 server
.stat_expiredkeys
++;
7912 return dbDelete(db
,key
);
7915 static int deleteIfVolatile(redisDb
*db
, robj
*key
) {
7916 if (getExpire(db
,key
) < 0) return 0;
7918 /* Delete the key */
7919 server
.stat_expiredkeys
++;
7921 return dbDelete(db
,key
);
7924 static void expireGenericCommand(redisClient
*c
, robj
*key
, robj
*param
, long offset
) {
7928 if (getLongFromObjectOrReply(c
, param
, &seconds
, NULL
) != REDIS_OK
) return;
7932 de
= dictFind(c
->db
->dict
,key
->ptr
);
7934 addReply(c
,shared
.czero
);
7938 if (dbDelete(c
->db
,key
)) server
.dirty
++;
7939 addReply(c
, shared
.cone
);
7942 time_t when
= time(NULL
)+seconds
;
7943 if (setExpire(c
->db
,key
,when
)) {
7944 addReply(c
,shared
.cone
);
7947 addReply(c
,shared
.czero
);
7953 static void expireCommand(redisClient
*c
) {
7954 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],0);
7957 static void expireatCommand(redisClient
*c
) {
7958 expireGenericCommand(c
,c
->argv
[1],c
->argv
[2],time(NULL
));
7961 static void ttlCommand(redisClient
*c
) {
7965 expire
= getExpire(c
->db
,c
->argv
[1]);
7967 ttl
= (int) (expire
-time(NULL
));
7968 if (ttl
< 0) ttl
= -1;
7970 addReplySds(c
,sdscatprintf(sdsempty(),":%d\r\n",ttl
));
7973 /* ================================ MULTI/EXEC ============================== */
7975 /* Client state initialization for MULTI/EXEC */
7976 static void initClientMultiState(redisClient
*c
) {
7977 c
->mstate
.commands
= NULL
;
7978 c
->mstate
.count
= 0;
7981 /* Release all the resources associated with MULTI/EXEC state */
7982 static void freeClientMultiState(redisClient
*c
) {
7985 for (j
= 0; j
< c
->mstate
.count
; j
++) {
7987 multiCmd
*mc
= c
->mstate
.commands
+j
;
7989 for (i
= 0; i
< mc
->argc
; i
++)
7990 decrRefCount(mc
->argv
[i
]);
7993 zfree(c
->mstate
.commands
);
7996 /* Add a new command into the MULTI commands queue */
7997 static void queueMultiCommand(redisClient
*c
, struct redisCommand
*cmd
) {
8001 c
->mstate
.commands
= zrealloc(c
->mstate
.commands
,
8002 sizeof(multiCmd
)*(c
->mstate
.count
+1));
8003 mc
= c
->mstate
.commands
+c
->mstate
.count
;
8006 mc
->argv
= zmalloc(sizeof(robj
*)*c
->argc
);
8007 memcpy(mc
->argv
,c
->argv
,sizeof(robj
*)*c
->argc
);
8008 for (j
= 0; j
< c
->argc
; j
++)
8009 incrRefCount(mc
->argv
[j
]);
8013 static void multiCommand(redisClient
*c
) {
8014 if (c
->flags
& REDIS_MULTI
) {
8015 addReplySds(c
,sdsnew("-ERR MULTI calls can not be nested\r\n"));
8018 c
->flags
|= REDIS_MULTI
;
8019 addReply(c
,shared
.ok
);
8022 static void discardCommand(redisClient
*c
) {
8023 if (!(c
->flags
& REDIS_MULTI
)) {
8024 addReplySds(c
,sdsnew("-ERR DISCARD without MULTI\r\n"));
8028 freeClientMultiState(c
);
8029 initClientMultiState(c
);
8030 c
->flags
&= (~REDIS_MULTI
);
8032 addReply(c
,shared
.ok
);
8035 /* Send a MULTI command to all the slaves and AOF file. Check the execCommand
8036 * implememntation for more information. */
8037 static void execCommandReplicateMulti(redisClient
*c
) {
8038 struct redisCommand
*cmd
;
8039 robj
*multistring
= createStringObject("MULTI",5);
8041 cmd
= lookupCommand("multi");
8042 if (server
.appendonly
)
8043 feedAppendOnlyFile(cmd
,c
->db
->id
,&multistring
,1);
8044 if (listLength(server
.slaves
))
8045 replicationFeedSlaves(server
.slaves
,c
->db
->id
,&multistring
,1);
8046 decrRefCount(multistring
);
8049 static void execCommand(redisClient
*c
) {
8054 if (!(c
->flags
& REDIS_MULTI
)) {
8055 addReplySds(c
,sdsnew("-ERR EXEC without MULTI\r\n"));
8059 /* Check if we need to abort the EXEC if some WATCHed key was touched.
8060 * A failed EXEC will return a multi bulk nil object. */
8061 if (c
->flags
& REDIS_DIRTY_CAS
) {
8062 freeClientMultiState(c
);
8063 initClientMultiState(c
);
8064 c
->flags
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
);
8066 addReply(c
,shared
.nullmultibulk
);
8070 /* Replicate a MULTI request now that we are sure the block is executed.
8071 * This way we'll deliver the MULTI/..../EXEC block as a whole and
8072 * both the AOF and the replication link will have the same consistency
8073 * and atomicity guarantees. */
8074 execCommandReplicateMulti(c
);
8076 /* Exec all the queued commands */
8077 unwatchAllKeys(c
); /* Unwatch ASAP otherwise we'll waste CPU cycles */
8078 orig_argv
= c
->argv
;
8079 orig_argc
= c
->argc
;
8080 addReplySds(c
,sdscatprintf(sdsempty(),"*%d\r\n",c
->mstate
.count
));
8081 for (j
= 0; j
< c
->mstate
.count
; j
++) {
8082 c
->argc
= c
->mstate
.commands
[j
].argc
;
8083 c
->argv
= c
->mstate
.commands
[j
].argv
;
8084 call(c
,c
->mstate
.commands
[j
].cmd
);
8086 c
->argv
= orig_argv
;
8087 c
->argc
= orig_argc
;
8088 freeClientMultiState(c
);
8089 initClientMultiState(c
);
8090 c
->flags
&= ~(REDIS_MULTI
|REDIS_DIRTY_CAS
);
8091 /* Make sure the EXEC command is always replicated / AOF, since we
8092 * always send the MULTI command (we can't know beforehand if the
8093 * next operations will contain at least a modification to the DB). */
8097 /* =========================== Blocking Operations ========================= */
8099 /* Currently Redis blocking operations support is limited to list POP ops,
8100 * so the current implementation is not fully generic, but it is also not
8101 * completely specific so it will not require a rewrite to support new
8102 * kind of blocking operations in the future.
8104 * Still it's important to note that list blocking operations can be already
8105 * used as a notification mechanism in order to implement other blocking
8106 * operations at application level, so there must be a very strong evidence
8107 * of usefulness and generality before new blocking operations are implemented.
8109 * This is how the current blocking POP works, we use BLPOP as example:
8110 * - If the user calls BLPOP and the key exists and contains a non empty list
8111 * then LPOP is called instead. So BLPOP is semantically the same as LPOP
8112 * if there is not to block.
8113 * - If instead BLPOP is called and the key does not exists or the list is
8114 * empty we need to block. In order to do so we remove the notification for
8115 * new data to read in the client socket (so that we'll not serve new
8116 * requests if the blocking request is not served). Also we put the client
8117 * in a dictionary (db->blocking_keys) mapping keys to a list of clients
8118 * blocking for this keys.
8119 * - If a PUSH operation against a key with blocked clients waiting is
8120 * performed, we serve the first in the list: basically instead to push
8121 * the new element inside the list we return it to the (first / oldest)
8122 * blocking client, unblock the client, and remove it form the list.
8124 * The above comment and the source code should be enough in order to understand
8125 * the implementation and modify / fix it later.
8128 /* Set a client in blocking mode for the specified key, with the specified
8130 static void blockForKeys(redisClient
*c
, robj
**keys
, int numkeys
, time_t timeout
) {
8135 c
->blocking_keys
= zmalloc(sizeof(robj
*)*numkeys
);
8136 c
->blocking_keys_num
= numkeys
;
8137 c
->blockingto
= timeout
;
8138 for (j
= 0; j
< numkeys
; j
++) {
8139 /* Add the key in the client structure, to map clients -> keys */
8140 c
->blocking_keys
[j
] = keys
[j
];
8141 incrRefCount(keys
[j
]);
8143 /* And in the other "side", to map keys -> clients */
8144 de
= dictFind(c
->db
->blocking_keys
,keys
[j
]);
8148 /* For every key we take a list of clients blocked for it */
8150 retval
= dictAdd(c
->db
->blocking_keys
,keys
[j
],l
);
8151 incrRefCount(keys
[j
]);
8152 assert(retval
== DICT_OK
);
8154 l
= dictGetEntryVal(de
);
8156 listAddNodeTail(l
,c
);
8158 /* Mark the client as a blocked client */
8159 c
->flags
|= REDIS_BLOCKED
;
8160 server
.blpop_blocked_clients
++;
8163 /* Unblock a client that's waiting in a blocking operation such as BLPOP */
8164 static void unblockClientWaitingData(redisClient
*c
) {
8169 assert(c
->blocking_keys
!= NULL
);
8170 /* The client may wait for multiple keys, so unblock it for every key. */
8171 for (j
= 0; j
< c
->blocking_keys_num
; j
++) {
8172 /* Remove this client from the list of clients waiting for this key. */
8173 de
= dictFind(c
->db
->blocking_keys
,c
->blocking_keys
[j
]);
8175 l
= dictGetEntryVal(de
);
8176 listDelNode(l
,listSearchKey(l
,c
));
8177 /* If the list is empty we need to remove it to avoid wasting memory */
8178 if (listLength(l
) == 0)
8179 dictDelete(c
->db
->blocking_keys
,c
->blocking_keys
[j
]);
8180 decrRefCount(c
->blocking_keys
[j
]);
8182 /* Cleanup the client structure */
8183 zfree(c
->blocking_keys
);
8184 c
->blocking_keys
= NULL
;
8185 c
->flags
&= (~REDIS_BLOCKED
);
8186 server
.blpop_blocked_clients
--;
8187 /* We want to process data if there is some command waiting
8188 * in the input buffer. Note that this is safe even if
8189 * unblockClientWaitingData() gets called from freeClient() because
8190 * freeClient() will be smart enough to call this function
8191 * *after* c->querybuf was set to NULL. */
8192 if (c
->querybuf
&& sdslen(c
->querybuf
) > 0) processInputBuffer(c
);
8195 /* This should be called from any function PUSHing into lists.
8196 * 'c' is the "pushing client", 'key' is the key it is pushing data against,
8197 * 'ele' is the element pushed.
8199 * If the function returns 0 there was no client waiting for a list push
8202 * If the function returns 1 there was a client waiting for a list push
8203 * against this key, the element was passed to this client thus it's not
8204 * needed to actually add it to the list and the caller should return asap. */
8205 static int handleClientsWaitingListPush(redisClient
*c
, robj
*key
, robj
*ele
) {
8206 struct dictEntry
*de
;
8207 redisClient
*receiver
;
8211 de
= dictFind(c
->db
->blocking_keys
,key
);
8212 if (de
== NULL
) return 0;
8213 l
= dictGetEntryVal(de
);
8216 receiver
= ln
->value
;
8218 addReplySds(receiver
,sdsnew("*2\r\n"));
8219 addReplyBulk(receiver
,key
);
8220 addReplyBulk(receiver
,ele
);
8221 unblockClientWaitingData(receiver
);
8225 /* Blocking RPOP/LPOP */
8226 static void blockingPopGenericCommand(redisClient
*c
, int where
) {
8231 for (j
= 1; j
< c
->argc
-1; j
++) {
8232 o
= lookupKeyWrite(c
->db
,c
->argv
[j
]);
8234 if (o
->type
!= REDIS_LIST
) {
8235 addReply(c
,shared
.wrongtypeerr
);
8238 if (listTypeLength(o
) != 0) {
8239 /* If the list contains elements fall back to the usual
8240 * non-blocking POP operation */
8241 robj
*argv
[2], **orig_argv
;
8244 /* We need to alter the command arguments before to call
8245 * popGenericCommand() as the command takes a single key. */
8246 orig_argv
= c
->argv
;
8247 orig_argc
= c
->argc
;
8248 argv
[1] = c
->argv
[j
];
8252 /* Also the return value is different, we need to output
8253 * the multi bulk reply header and the key name. The
8254 * "real" command will add the last element (the value)
8255 * for us. If this souds like an hack to you it's just
8256 * because it is... */
8257 addReplySds(c
,sdsnew("*2\r\n"));
8258 addReplyBulk(c
,argv
[1]);
8259 popGenericCommand(c
,where
);
8261 /* Fix the client structure with the original stuff */
8262 c
->argv
= orig_argv
;
8263 c
->argc
= orig_argc
;
8269 /* If the list is empty or the key does not exists we must block */
8270 timeout
= strtol(c
->argv
[c
->argc
-1]->ptr
,NULL
,10);
8271 if (timeout
> 0) timeout
+= time(NULL
);
8272 blockForKeys(c
,c
->argv
+1,c
->argc
-2,timeout
);
8275 static void blpopCommand(redisClient
*c
) {
8276 blockingPopGenericCommand(c
,REDIS_HEAD
);
8279 static void brpopCommand(redisClient
*c
) {
8280 blockingPopGenericCommand(c
,REDIS_TAIL
);
8283 /* =============================== Replication ============================= */
8285 static int syncWrite(int fd
, char *ptr
, ssize_t size
, int timeout
) {
8286 ssize_t nwritten
, ret
= size
;
8287 time_t start
= time(NULL
);
8291 if (aeWait(fd
,AE_WRITABLE
,1000) & AE_WRITABLE
) {
8292 nwritten
= write(fd
,ptr
,size
);
8293 if (nwritten
== -1) return -1;
8297 if ((time(NULL
)-start
) > timeout
) {
8305 static int syncRead(int fd
, char *ptr
, ssize_t size
, int timeout
) {
8306 ssize_t nread
, totread
= 0;
8307 time_t start
= time(NULL
);
8311 if (aeWait(fd
,AE_READABLE
,1000) & AE_READABLE
) {
8312 nread
= read(fd
,ptr
,size
);
8313 if (nread
== -1) return -1;
8318 if ((time(NULL
)-start
) > timeout
) {
8326 static int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
) {
8333 if (syncRead(fd
,&c
,1,timeout
) == -1) return -1;
8336 if (nread
&& *(ptr
-1) == '\r') *(ptr
-1) = '\0';
8347 static void syncCommand(redisClient
*c
) {
8348 /* ignore SYNC if aleady slave or in monitor mode */
8349 if (c
->flags
& REDIS_SLAVE
) return;
8351 /* SYNC can't be issued when the server has pending data to send to
8352 * the client about already issued commands. We need a fresh reply
8353 * buffer registering the differences between the BGSAVE and the current
8354 * dataset, so that we can copy to other slaves if needed. */
8355 if (listLength(c
->reply
) != 0) {
8356 addReplySds(c
,sdsnew("-ERR SYNC is invalid with pending input\r\n"));
8360 redisLog(REDIS_NOTICE
,"Slave ask for synchronization");
8361 /* Here we need to check if there is a background saving operation
8362 * in progress, or if it is required to start one */
8363 if (server
.bgsavechildpid
!= -1) {
8364 /* Ok a background save is in progress. Let's check if it is a good
8365 * one for replication, i.e. if there is another slave that is
8366 * registering differences since the server forked to save */
8371 listRewind(server
.slaves
,&li
);
8372 while((ln
= listNext(&li
))) {
8374 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_END
) break;
8377 /* Perfect, the server is already registering differences for
8378 * another slave. Set the right state, and copy the buffer. */
8379 listRelease(c
->reply
);
8380 c
->reply
= listDup(slave
->reply
);
8381 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
8382 redisLog(REDIS_NOTICE
,"Waiting for end of BGSAVE for SYNC");
8384 /* No way, we need to wait for the next BGSAVE in order to
8385 * register differences */
8386 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_START
;
8387 redisLog(REDIS_NOTICE
,"Waiting for next BGSAVE for SYNC");
8390 /* Ok we don't have a BGSAVE in progress, let's start one */
8391 redisLog(REDIS_NOTICE
,"Starting BGSAVE for SYNC");
8392 if (rdbSaveBackground(server
.dbfilename
) != REDIS_OK
) {
8393 redisLog(REDIS_NOTICE
,"Replication failed, can't BGSAVE");
8394 addReplySds(c
,sdsnew("-ERR Unalbe to perform background save\r\n"));
8397 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
8400 c
->flags
|= REDIS_SLAVE
;
8402 listAddNodeTail(server
.slaves
,c
);
8406 static void sendBulkToSlave(aeEventLoop
*el
, int fd
, void *privdata
, int mask
) {
8407 redisClient
*slave
= privdata
;
8409 REDIS_NOTUSED(mask
);
8410 char buf
[REDIS_IOBUF_LEN
];
8411 ssize_t nwritten
, buflen
;
8413 if (slave
->repldboff
== 0) {
8414 /* Write the bulk write count before to transfer the DB. In theory here
8415 * we don't know how much room there is in the output buffer of the
8416 * socket, but in pratice SO_SNDLOWAT (the minimum count for output
8417 * operations) will never be smaller than the few bytes we need. */
8420 bulkcount
= sdscatprintf(sdsempty(),"$%lld\r\n",(unsigned long long)
8422 if (write(fd
,bulkcount
,sdslen(bulkcount
)) != (signed)sdslen(bulkcount
))
8430 lseek(slave
->repldbfd
,slave
->repldboff
,SEEK_SET
);
8431 buflen
= read(slave
->repldbfd
,buf
,REDIS_IOBUF_LEN
);
8433 redisLog(REDIS_WARNING
,"Read error sending DB to slave: %s",
8434 (buflen
== 0) ? "premature EOF" : strerror(errno
));
8438 if ((nwritten
= write(fd
,buf
,buflen
)) == -1) {
8439 redisLog(REDIS_VERBOSE
,"Write error sending DB to slave: %s",
8444 slave
->repldboff
+= nwritten
;
8445 if (slave
->repldboff
== slave
->repldbsize
) {
8446 close(slave
->repldbfd
);
8447 slave
->repldbfd
= -1;
8448 aeDeleteFileEvent(server
.el
,slave
->fd
,AE_WRITABLE
);
8449 slave
->replstate
= REDIS_REPL_ONLINE
;
8450 if (aeCreateFileEvent(server
.el
, slave
->fd
, AE_WRITABLE
,
8451 sendReplyToClient
, slave
) == AE_ERR
) {
8455 addReplySds(slave
,sdsempty());
8456 redisLog(REDIS_NOTICE
,"Synchronization with slave succeeded");
8460 /* This function is called at the end of every backgrond saving.
8461 * The argument bgsaveerr is REDIS_OK if the background saving succeeded
8462 * otherwise REDIS_ERR is passed to the function.
8464 * The goal of this function is to handle slaves waiting for a successful
8465 * background saving in order to perform non-blocking synchronization. */
8466 static void updateSlavesWaitingBgsave(int bgsaveerr
) {
8468 int startbgsave
= 0;
8471 listRewind(server
.slaves
,&li
);
8472 while((ln
= listNext(&li
))) {
8473 redisClient
*slave
= ln
->value
;
8475 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
) {
8477 slave
->replstate
= REDIS_REPL_WAIT_BGSAVE_END
;
8478 } else if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_END
) {
8479 struct redis_stat buf
;
8481 if (bgsaveerr
!= REDIS_OK
) {
8483 redisLog(REDIS_WARNING
,"SYNC failed. BGSAVE child returned an error");
8486 if ((slave
->repldbfd
= open(server
.dbfilename
,O_RDONLY
)) == -1 ||
8487 redis_fstat(slave
->repldbfd
,&buf
) == -1) {
8489 redisLog(REDIS_WARNING
,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno
));
8492 slave
->repldboff
= 0;
8493 slave
->repldbsize
= buf
.st_size
;
8494 slave
->replstate
= REDIS_REPL_SEND_BULK
;
8495 aeDeleteFileEvent(server
.el
,slave
->fd
,AE_WRITABLE
);
8496 if (aeCreateFileEvent(server
.el
, slave
->fd
, AE_WRITABLE
, sendBulkToSlave
, slave
) == AE_ERR
) {
8503 if (rdbSaveBackground(server
.dbfilename
) != REDIS_OK
) {
8506 listRewind(server
.slaves
,&li
);
8507 redisLog(REDIS_WARNING
,"SYNC failed. BGSAVE failed");
8508 while((ln
= listNext(&li
))) {
8509 redisClient
*slave
= ln
->value
;
8511 if (slave
->replstate
== REDIS_REPL_WAIT_BGSAVE_START
)
8518 static int syncWithMaster(void) {
8519 char buf
[1024], tmpfile
[256], authcmd
[1024];
8521 int fd
= anetTcpConnect(NULL
,server
.masterhost
,server
.masterport
);
8522 int dfd
, maxtries
= 5;
8525 redisLog(REDIS_WARNING
,"Unable to connect to MASTER: %s",
8530 /* AUTH with the master if required. */
8531 if(server
.masterauth
) {
8532 snprintf(authcmd
, 1024, "AUTH %s\r\n", server
.masterauth
);
8533 if (syncWrite(fd
, authcmd
, strlen(server
.masterauth
)+7, 5) == -1) {
8535 redisLog(REDIS_WARNING
,"Unable to AUTH to MASTER: %s",
8539 /* Read the AUTH result. */
8540 if (syncReadLine(fd
,buf
,1024,3600) == -1) {
8542 redisLog(REDIS_WARNING
,"I/O error reading auth result from MASTER: %s",
8546 if (buf
[0] != '+') {
8548 redisLog(REDIS_WARNING
,"Cannot AUTH to MASTER, is the masterauth password correct?");
8553 /* Issue the SYNC command */
8554 if (syncWrite(fd
,"SYNC \r\n",7,5) == -1) {
8556 redisLog(REDIS_WARNING
,"I/O error writing to MASTER: %s",
8560 /* Read the bulk write count */
8561 if (syncReadLine(fd
,buf
,1024,3600) == -1) {
8563 redisLog(REDIS_WARNING
,"I/O error reading bulk count from MASTER: %s",
8567 if (buf
[0] != '$') {
8569 redisLog(REDIS_WARNING
,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
8572 dumpsize
= strtol(buf
+1,NULL
,10);
8573 redisLog(REDIS_NOTICE
,"Receiving %ld bytes data dump from MASTER",dumpsize
);
8574 /* Read the bulk write data on a temp file */
8576 snprintf(tmpfile
,256,
8577 "temp-%d.%ld.rdb",(int)time(NULL
),(long int)getpid());
8578 dfd
= open(tmpfile
,O_CREAT
|O_WRONLY
|O_EXCL
,0644);
8579 if (dfd
!= -1) break;
8584 redisLog(REDIS_WARNING
,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno
));
8588 int nread
, nwritten
;
8590 nread
= read(fd
,buf
,(dumpsize
< 1024)?dumpsize
:1024);
8592 redisLog(REDIS_WARNING
,"I/O error trying to sync with MASTER: %s",
8598 nwritten
= write(dfd
,buf
,nread
);
8599 if (nwritten
== -1) {
8600 redisLog(REDIS_WARNING
,"Write error writing to the DB dump file needed for MASTER <-> SLAVE synchrnonization: %s", strerror(errno
));
8608 if (rename(tmpfile
,server
.dbfilename
) == -1) {
8609 redisLog(REDIS_WARNING
,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno
));
8615 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
8616 redisLog(REDIS_WARNING
,"Failed trying to load the MASTER synchronization DB from disk");
8620 server
.master
= createClient(fd
);
8621 server
.master
->flags
|= REDIS_MASTER
;
8622 server
.master
->authenticated
= 1;
8623 server
.replstate
= REDIS_REPL_CONNECTED
;
8627 static void slaveofCommand(redisClient
*c
) {
8628 if (!strcasecmp(c
->argv
[1]->ptr
,"no") &&
8629 !strcasecmp(c
->argv
[2]->ptr
,"one")) {
8630 if (server
.masterhost
) {
8631 sdsfree(server
.masterhost
);
8632 server
.masterhost
= NULL
;
8633 if (server
.master
) freeClient(server
.master
);
8634 server
.replstate
= REDIS_REPL_NONE
;
8635 redisLog(REDIS_NOTICE
,"MASTER MODE enabled (user request)");
8638 sdsfree(server
.masterhost
);
8639 server
.masterhost
= sdsdup(c
->argv
[1]->ptr
);
8640 server
.masterport
= atoi(c
->argv
[2]->ptr
);
8641 if (server
.master
) freeClient(server
.master
);
8642 server
.replstate
= REDIS_REPL_CONNECT
;
8643 redisLog(REDIS_NOTICE
,"SLAVE OF %s:%d enabled (user request)",
8644 server
.masterhost
, server
.masterport
);
8646 addReply(c
,shared
.ok
);
8649 /* ============================ Maxmemory directive ======================== */
8651 /* Try to free one object form the pre-allocated objects free list.
8652 * This is useful under low mem conditions as by default we take 1 million
8653 * free objects allocated. On success REDIS_OK is returned, otherwise
8655 static int tryFreeOneObjectFromFreelist(void) {
8658 if (server
.vm_enabled
) pthread_mutex_lock(&server
.obj_freelist_mutex
);
8659 if (listLength(server
.objfreelist
)) {
8660 listNode
*head
= listFirst(server
.objfreelist
);
8661 o
= listNodeValue(head
);
8662 listDelNode(server
.objfreelist
,head
);
8663 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
8667 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.obj_freelist_mutex
);
8672 /* This function gets called when 'maxmemory' is set on the config file to limit
8673 * the max memory used by the server, and we are out of memory.
8674 * This function will try to, in order:
8676 * - Free objects from the free list
8677 * - Try to remove keys with an EXPIRE set
8679 * It is not possible to free enough memory to reach used-memory < maxmemory
8680 * the server will start refusing commands that will enlarge even more the
8683 static void freeMemoryIfNeeded(void) {
8684 while (server
.maxmemory
&& zmalloc_used_memory() > server
.maxmemory
) {
8685 int j
, k
, freed
= 0;
8687 if (tryFreeOneObjectFromFreelist() == REDIS_OK
) continue;
8688 for (j
= 0; j
< server
.dbnum
; j
++) {
8690 robj
*minkey
= NULL
;
8691 struct dictEntry
*de
;
8693 if (dictSize(server
.db
[j
].expires
)) {
8695 /* From a sample of three keys drop the one nearest to
8696 * the natural expire */
8697 for (k
= 0; k
< 3; k
++) {
8700 de
= dictGetRandomKey(server
.db
[j
].expires
);
8701 t
= (time_t) dictGetEntryVal(de
);
8702 if (minttl
== -1 || t
< minttl
) {
8703 minkey
= dictGetEntryKey(de
);
8707 dbDelete(server
.db
+j
,minkey
);
8710 if (!freed
) return; /* nothing to free... */
8714 /* ============================== Append Only file ========================== */
8716 /* Called when the user switches from "appendonly yes" to "appendonly no"
8717 * at runtime using the CONFIG command. */
8718 static void stopAppendOnly(void) {
8719 flushAppendOnlyFile();
8720 aof_fsync(server
.appendfd
);
8721 close(server
.appendfd
);
8723 server
.appendfd
= -1;
8724 server
.appendseldb
= -1;
8725 server
.appendonly
= 0;
8726 /* rewrite operation in progress? kill it, wait child exit */
8727 if (server
.bgsavechildpid
!= -1) {
8730 if (kill(server
.bgsavechildpid
,SIGKILL
) != -1)
8731 wait3(&statloc
,0,NULL
);
8732 /* reset the buffer accumulating changes while the child saves */
8733 sdsfree(server
.bgrewritebuf
);
8734 server
.bgrewritebuf
= sdsempty();
8735 server
.bgsavechildpid
= -1;
8739 /* Called when the user switches from "appendonly no" to "appendonly yes"
8740 * at runtime using the CONFIG command. */
8741 static int startAppendOnly(void) {
8742 server
.appendonly
= 1;
8743 server
.lastfsync
= time(NULL
);
8744 server
.appendfd
= open(server
.appendfilename
,O_WRONLY
|O_APPEND
|O_CREAT
,0644);
8745 if (server
.appendfd
== -1) {
8746 redisLog(REDIS_WARNING
,"Used tried to switch on AOF via CONFIG, but I can't open the AOF file: %s",strerror(errno
));
8749 if (rewriteAppendOnlyFileBackground() == REDIS_ERR
) {
8750 server
.appendonly
= 0;
8751 close(server
.appendfd
);
8752 redisLog(REDIS_WARNING
,"Used tried to switch on AOF via CONFIG, I can't trigger a background AOF rewrite operation. Check the above logs for more info about the error.",strerror(errno
));
8758 /* Write the append only file buffer on disk.
8760 * Since we are required to write the AOF before replying to the client,
8761 * and the only way the client socket can get a write is entering when the
8762 * the event loop, we accumulate all the AOF writes in a memory
8763 * buffer and write it on disk using this function just before entering
8764 * the event loop again. */
8765 static void flushAppendOnlyFile(void) {
8769 if (sdslen(server
.aofbuf
) == 0) return;
8771 /* We want to perform a single write. This should be guaranteed atomic
8772 * at least if the filesystem we are writing is a real physical one.
8773 * While this will save us against the server being killed I don't think
8774 * there is much to do about the whole server stopping for power problems
8776 nwritten
= write(server
.appendfd
,server
.aofbuf
,sdslen(server
.aofbuf
));
8777 if (nwritten
!= (signed)sdslen(server
.aofbuf
)) {
8778 /* Ooops, we are in troubles. The best thing to do for now is
8779 * aborting instead of giving the illusion that everything is
8780 * working as expected. */
8781 if (nwritten
== -1) {
8782 redisLog(REDIS_WARNING
,"Exiting on error writing to the append-only file: %s",strerror(errno
));
8784 redisLog(REDIS_WARNING
,"Exiting on short write while writing to the append-only file: %s",strerror(errno
));
8788 sdsfree(server
.aofbuf
);
8789 server
.aofbuf
= sdsempty();
8791 /* Don't Fsync if no-appendfsync-on-rewrite is set to yes and we have
8792 * childs performing heavy I/O on disk. */
8793 if (server
.no_appendfsync_on_rewrite
&&
8794 (server
.bgrewritechildpid
!= -1 || server
.bgsavechildpid
!= -1))
8796 /* Fsync if needed */
8798 if (server
.appendfsync
== APPENDFSYNC_ALWAYS
||
8799 (server
.appendfsync
== APPENDFSYNC_EVERYSEC
&&
8800 now
-server
.lastfsync
> 1))
8802 /* aof_fsync is defined as fdatasync() for Linux in order to avoid
8803 * flushing metadata. */
8804 aof_fsync(server
.appendfd
); /* Let's try to get this data on the disk */
8805 server
.lastfsync
= now
;
8809 static sds
catAppendOnlyGenericCommand(sds buf
, int argc
, robj
**argv
) {
8811 buf
= sdscatprintf(buf
,"*%d\r\n",argc
);
8812 for (j
= 0; j
< argc
; j
++) {
8813 robj
*o
= getDecodedObject(argv
[j
]);
8814 buf
= sdscatprintf(buf
,"$%lu\r\n",(unsigned long)sdslen(o
->ptr
));
8815 buf
= sdscatlen(buf
,o
->ptr
,sdslen(o
->ptr
));
8816 buf
= sdscatlen(buf
,"\r\n",2);
8822 static sds
catAppendOnlyExpireAtCommand(sds buf
, robj
*key
, robj
*seconds
) {
8827 /* Make sure we can use strtol */
8828 seconds
= getDecodedObject(seconds
);
8829 when
= time(NULL
)+strtol(seconds
->ptr
,NULL
,10);
8830 decrRefCount(seconds
);
8832 argv
[0] = createStringObject("EXPIREAT",8);
8834 argv
[2] = createObject(REDIS_STRING
,
8835 sdscatprintf(sdsempty(),"%ld",when
));
8836 buf
= catAppendOnlyGenericCommand(buf
, argc
, argv
);
8837 decrRefCount(argv
[0]);
8838 decrRefCount(argv
[2]);
8842 static void feedAppendOnlyFile(struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
) {
8843 sds buf
= sdsempty();
8846 /* The DB this command was targetting is not the same as the last command
8847 * we appendend. To issue a SELECT command is needed. */
8848 if (dictid
!= server
.appendseldb
) {
8851 snprintf(seldb
,sizeof(seldb
),"%d",dictid
);
8852 buf
= sdscatprintf(buf
,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
8853 (unsigned long)strlen(seldb
),seldb
);
8854 server
.appendseldb
= dictid
;
8857 if (cmd
->proc
== expireCommand
) {
8858 /* Translate EXPIRE into EXPIREAT */
8859 buf
= catAppendOnlyExpireAtCommand(buf
,argv
[1],argv
[2]);
8860 } else if (cmd
->proc
== setexCommand
) {
8861 /* Translate SETEX to SET and EXPIREAT */
8862 tmpargv
[0] = createStringObject("SET",3);
8863 tmpargv
[1] = argv
[1];
8864 tmpargv
[2] = argv
[3];
8865 buf
= catAppendOnlyGenericCommand(buf
,3,tmpargv
);
8866 decrRefCount(tmpargv
[0]);
8867 buf
= catAppendOnlyExpireAtCommand(buf
,argv
[1],argv
[2]);
8869 buf
= catAppendOnlyGenericCommand(buf
,argc
,argv
);
8872 /* Append to the AOF buffer. This will be flushed on disk just before
8873 * of re-entering the event loop, so before the client will get a
8874 * positive reply about the operation performed. */
8875 server
.aofbuf
= sdscatlen(server
.aofbuf
,buf
,sdslen(buf
));
8877 /* If a background append only file rewriting is in progress we want to
8878 * accumulate the differences between the child DB and the current one
8879 * in a buffer, so that when the child process will do its work we
8880 * can append the differences to the new append only file. */
8881 if (server
.bgrewritechildpid
!= -1)
8882 server
.bgrewritebuf
= sdscatlen(server
.bgrewritebuf
,buf
,sdslen(buf
));
8887 /* In Redis commands are always executed in the context of a client, so in
8888 * order to load the append only file we need to create a fake client. */
8889 static struct redisClient
*createFakeClient(void) {
8890 struct redisClient
*c
= zmalloc(sizeof(*c
));
8894 c
->querybuf
= sdsempty();
8898 /* We set the fake client as a slave waiting for the synchronization
8899 * so that Redis will not try to send replies to this client. */
8900 c
->replstate
= REDIS_REPL_WAIT_BGSAVE_START
;
8901 c
->reply
= listCreate();
8902 listSetFreeMethod(c
->reply
,decrRefCount
);
8903 listSetDupMethod(c
->reply
,dupClientReplyValue
);
8904 initClientMultiState(c
);
8908 static void freeFakeClient(struct redisClient
*c
) {
8909 sdsfree(c
->querybuf
);
8910 listRelease(c
->reply
);
8911 freeClientMultiState(c
);
8915 /* Replay the append log file. On error REDIS_OK is returned. On non fatal
8916 * error (the append only file is zero-length) REDIS_ERR is returned. On
8917 * fatal error an error message is logged and the program exists. */
8918 int loadAppendOnlyFile(char *filename
) {
8919 struct redisClient
*fakeClient
;
8920 FILE *fp
= fopen(filename
,"r");
8921 struct redis_stat sb
;
8922 int appendonly
= server
.appendonly
;
8924 if (redis_fstat(fileno(fp
),&sb
) != -1 && sb
.st_size
== 0)
8928 redisLog(REDIS_WARNING
,"Fatal error: can't open the append log file for reading: %s",strerror(errno
));
8932 /* Temporarily disable AOF, to prevent EXEC from feeding a MULTI
8933 * to the same file we're about to read. */
8934 server
.appendonly
= 0;
8936 fakeClient
= createFakeClient();
8943 struct redisCommand
*cmd
;
8946 if (fgets(buf
,sizeof(buf
),fp
) == NULL
) {
8952 if (buf
[0] != '*') goto fmterr
;
8954 argv
= zmalloc(sizeof(robj
*)*argc
);
8955 for (j
= 0; j
< argc
; j
++) {
8956 if (fgets(buf
,sizeof(buf
),fp
) == NULL
) goto readerr
;
8957 if (buf
[0] != '$') goto fmterr
;
8958 len
= strtol(buf
+1,NULL
,10);
8959 argsds
= sdsnewlen(NULL
,len
);
8960 if (len
&& fread(argsds
,len
,1,fp
) == 0) goto fmterr
;
8961 argv
[j
] = createObject(REDIS_STRING
,argsds
);
8962 if (fread(buf
,2,1,fp
) == 0) goto fmterr
; /* discard CRLF */
8965 /* Command lookup */
8966 cmd
= lookupCommand(argv
[0]->ptr
);
8968 redisLog(REDIS_WARNING
,"Unknown command '%s' reading the append only file", argv
[0]->ptr
);
8971 /* Try object encoding */
8972 if (cmd
->flags
& REDIS_CMD_BULK
)
8973 argv
[argc
-1] = tryObjectEncoding(argv
[argc
-1]);
8974 /* Run the command in the context of a fake client */
8975 fakeClient
->argc
= argc
;
8976 fakeClient
->argv
= argv
;
8977 cmd
->proc(fakeClient
);
8978 /* Discard the reply objects list from the fake client */
8979 while(listLength(fakeClient
->reply
))
8980 listDelNode(fakeClient
->reply
,listFirst(fakeClient
->reply
));
8981 /* Clean up, ready for the next command */
8982 for (j
= 0; j
< argc
; j
++) decrRefCount(argv
[j
]);
8984 /* Handle swapping while loading big datasets when VM is on */
8986 if ((zmalloc_used_memory() - server
.vm_max_memory
) > 1024*1024*32)
8989 if (server
.vm_enabled
&& force_swapout
) {
8990 while (zmalloc_used_memory() > server
.vm_max_memory
) {
8991 if (vmSwapOneObjectBlocking() == REDIS_ERR
) break;
8996 /* This point can only be reached when EOF is reached without errors.
8997 * If the client is in the middle of a MULTI/EXEC, log error and quit. */
8998 if (fakeClient
->flags
& REDIS_MULTI
) goto readerr
;
9001 freeFakeClient(fakeClient
);
9002 server
.appendonly
= appendonly
;
9007 redisLog(REDIS_WARNING
,"Unexpected end of file reading the append only file");
9009 redisLog(REDIS_WARNING
,"Unrecoverable error reading the append only file: %s", strerror(errno
));
9013 redisLog(REDIS_WARNING
,"Bad file format reading the append only file");
9017 /* Write binary-safe string into a file in the bulkformat
9018 * $<count>\r\n<payload>\r\n */
9019 static int fwriteBulkString(FILE *fp
, char *s
, unsigned long len
) {
9023 clen
= 1+ll2string(cbuf
+1,sizeof(cbuf
)-1,len
);
9024 cbuf
[clen
++] = '\r';
9025 cbuf
[clen
++] = '\n';
9026 if (fwrite(cbuf
,clen
,1,fp
) == 0) return 0;
9027 if (len
> 0 && fwrite(s
,len
,1,fp
) == 0) return 0;
9028 if (fwrite("\r\n",2,1,fp
) == 0) return 0;
9032 /* Write a double value in bulk format $<count>\r\n<payload>\r\n */
9033 static int fwriteBulkDouble(FILE *fp
, double d
) {
9034 char buf
[128], dbuf
[128];
9036 snprintf(dbuf
,sizeof(dbuf
),"%.17g\r\n",d
);
9037 snprintf(buf
,sizeof(buf
),"$%lu\r\n",(unsigned long)strlen(dbuf
)-2);
9038 if (fwrite(buf
,strlen(buf
),1,fp
) == 0) return 0;
9039 if (fwrite(dbuf
,strlen(dbuf
),1,fp
) == 0) return 0;
9043 /* Write a long value in bulk format $<count>\r\n<payload>\r\n */
9044 static int fwriteBulkLongLong(FILE *fp
, long long l
) {
9045 char bbuf
[128], lbuf
[128];
9046 unsigned int blen
, llen
;
9047 llen
= ll2string(lbuf
,32,l
);
9048 blen
= snprintf(bbuf
,sizeof(bbuf
),"$%u\r\n%s\r\n",llen
,lbuf
);
9049 if (fwrite(bbuf
,blen
,1,fp
) == 0) return 0;
9053 /* Delegate writing an object to writing a bulk string or bulk long long. */
9054 static int fwriteBulkObject(FILE *fp
, robj
*obj
) {
9055 /* Avoid using getDecodedObject to help copy-on-write (we are often
9056 * in a child process when this function is called). */
9057 if (obj
->encoding
== REDIS_ENCODING_INT
) {
9058 return fwriteBulkLongLong(fp
,(long)obj
->ptr
);
9059 } else if (obj
->encoding
== REDIS_ENCODING_RAW
) {
9060 return fwriteBulkString(fp
,obj
->ptr
,sdslen(obj
->ptr
));
9062 redisPanic("Unknown string encoding");
9066 /* Write a sequence of commands able to fully rebuild the dataset into
9067 * "filename". Used both by REWRITEAOF and BGREWRITEAOF. */
9068 static int rewriteAppendOnlyFile(char *filename
) {
9069 dictIterator
*di
= NULL
;
9074 time_t now
= time(NULL
);
9076 /* Note that we have to use a different temp name here compared to the
9077 * one used by rewriteAppendOnlyFileBackground() function. */
9078 snprintf(tmpfile
,256,"temp-rewriteaof-%d.aof", (int) getpid());
9079 fp
= fopen(tmpfile
,"w");
9081 redisLog(REDIS_WARNING
, "Failed rewriting the append only file: %s", strerror(errno
));
9084 for (j
= 0; j
< server
.dbnum
; j
++) {
9085 char selectcmd
[] = "*2\r\n$6\r\nSELECT\r\n";
9086 redisDb
*db
= server
.db
+j
;
9088 if (dictSize(d
) == 0) continue;
9089 di
= dictGetIterator(d
);
9095 /* SELECT the new DB */
9096 if (fwrite(selectcmd
,sizeof(selectcmd
)-1,1,fp
) == 0) goto werr
;
9097 if (fwriteBulkLongLong(fp
,j
) == 0) goto werr
;
9099 /* Iterate this DB writing every entry */
9100 while((de
= dictNext(di
)) != NULL
) {
9101 sds keystr
= dictGetEntryKey(de
);
9106 keystr
= dictGetEntryKey(de
);
9107 o
= dictGetEntryVal(de
);
9108 initStaticStringObject(key
,keystr
);
9109 /* If the value for this key is swapped, load a preview in memory.
9110 * We use a "swapped" flag to remember if we need to free the
9111 * value object instead to just increment the ref count anyway
9112 * in order to avoid copy-on-write of pages if we are forked() */
9113 if (!server
.vm_enabled
|| o
->storage
== REDIS_VM_MEMORY
||
9114 o
->storage
== REDIS_VM_SWAPPING
) {
9117 o
= vmPreviewObject(o
);
9120 expiretime
= getExpire(db
,&key
);
9122 /* Save the key and associated value */
9123 if (o
->type
== REDIS_STRING
) {
9124 /* Emit a SET command */
9125 char cmd
[]="*3\r\n$3\r\nSET\r\n";
9126 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9128 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9129 if (fwriteBulkObject(fp
,o
) == 0) goto werr
;
9130 } else if (o
->type
== REDIS_LIST
) {
9131 /* Emit the RPUSHes needed to rebuild the list */
9132 char cmd
[]="*3\r\n$5\r\nRPUSH\r\n";
9133 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
9134 unsigned char *zl
= o
->ptr
;
9135 unsigned char *p
= ziplistIndex(zl
,0);
9136 unsigned char *vstr
;
9140 while(ziplistGet(p
,&vstr
,&vlen
,&vlong
)) {
9141 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9142 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9144 if (fwriteBulkString(fp
,(char*)vstr
,vlen
) == 0)
9147 if (fwriteBulkLongLong(fp
,vlong
) == 0)
9150 p
= ziplistNext(zl
,p
);
9152 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
9153 list
*list
= o
->ptr
;
9157 listRewind(list
,&li
);
9158 while((ln
= listNext(&li
))) {
9159 robj
*eleobj
= listNodeValue(ln
);
9161 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9162 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9163 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
9166 redisPanic("Unknown list encoding");
9168 } else if (o
->type
== REDIS_SET
) {
9169 /* Emit the SADDs needed to rebuild the set */
9171 dictIterator
*di
= dictGetIterator(set
);
9174 while((de
= dictNext(di
)) != NULL
) {
9175 char cmd
[]="*3\r\n$4\r\nSADD\r\n";
9176 robj
*eleobj
= dictGetEntryKey(de
);
9178 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9179 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9180 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
9182 dictReleaseIterator(di
);
9183 } else if (o
->type
== REDIS_ZSET
) {
9184 /* Emit the ZADDs needed to rebuild the sorted set */
9186 dictIterator
*di
= dictGetIterator(zs
->dict
);
9189 while((de
= dictNext(di
)) != NULL
) {
9190 char cmd
[]="*4\r\n$4\r\nZADD\r\n";
9191 robj
*eleobj
= dictGetEntryKey(de
);
9192 double *score
= dictGetEntryVal(de
);
9194 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9195 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9196 if (fwriteBulkDouble(fp
,*score
) == 0) goto werr
;
9197 if (fwriteBulkObject(fp
,eleobj
) == 0) goto werr
;
9199 dictReleaseIterator(di
);
9200 } else if (o
->type
== REDIS_HASH
) {
9201 char cmd
[]="*4\r\n$4\r\nHSET\r\n";
9203 /* Emit the HSETs needed to rebuild the hash */
9204 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
9205 unsigned char *p
= zipmapRewind(o
->ptr
);
9206 unsigned char *field
, *val
;
9207 unsigned int flen
, vlen
;
9209 while((p
= zipmapNext(p
,&field
,&flen
,&val
,&vlen
)) != NULL
) {
9210 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9211 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9212 if (fwriteBulkString(fp
,(char*)field
,flen
) == -1)
9214 if (fwriteBulkString(fp
,(char*)val
,vlen
) == -1)
9218 dictIterator
*di
= dictGetIterator(o
->ptr
);
9221 while((de
= dictNext(di
)) != NULL
) {
9222 robj
*field
= dictGetEntryKey(de
);
9223 robj
*val
= dictGetEntryVal(de
);
9225 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9226 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9227 if (fwriteBulkObject(fp
,field
) == -1) return -1;
9228 if (fwriteBulkObject(fp
,val
) == -1) return -1;
9230 dictReleaseIterator(di
);
9233 redisPanic("Unknown object type");
9235 /* Save the expire time */
9236 if (expiretime
!= -1) {
9237 char cmd
[]="*3\r\n$8\r\nEXPIREAT\r\n";
9238 /* If this key is already expired skip it */
9239 if (expiretime
< now
) continue;
9240 if (fwrite(cmd
,sizeof(cmd
)-1,1,fp
) == 0) goto werr
;
9241 if (fwriteBulkObject(fp
,&key
) == 0) goto werr
;
9242 if (fwriteBulkLongLong(fp
,expiretime
) == 0) goto werr
;
9244 if (swapped
) decrRefCount(o
);
9246 dictReleaseIterator(di
);
9249 /* Make sure data will not remain on the OS's output buffers */
9251 aof_fsync(fileno(fp
));
9254 /* Use RENAME to make sure the DB file is changed atomically only
9255 * if the generate DB file is ok. */
9256 if (rename(tmpfile
,filename
) == -1) {
9257 redisLog(REDIS_WARNING
,"Error moving temp append only file on the final destination: %s", strerror(errno
));
9261 redisLog(REDIS_NOTICE
,"SYNC append only file rewrite performed");
9267 redisLog(REDIS_WARNING
,"Write error writing append only file on disk: %s", strerror(errno
));
9268 if (di
) dictReleaseIterator(di
);
9272 /* This is how rewriting of the append only file in background works:
9274 * 1) The user calls BGREWRITEAOF
9275 * 2) Redis calls this function, that forks():
9276 * 2a) the child rewrite the append only file in a temp file.
9277 * 2b) the parent accumulates differences in server.bgrewritebuf.
9278 * 3) When the child finished '2a' exists.
9279 * 4) The parent will trap the exit code, if it's OK, will append the
9280 * data accumulated into server.bgrewritebuf into the temp file, and
9281 * finally will rename(2) the temp file in the actual file name.
9282 * The the new file is reopened as the new append only file. Profit!
9284 static int rewriteAppendOnlyFileBackground(void) {
9287 if (server
.bgrewritechildpid
!= -1) return REDIS_ERR
;
9288 if (server
.vm_enabled
) waitEmptyIOJobsQueue();
9289 if ((childpid
= fork()) == 0) {
9293 if (server
.vm_enabled
) vmReopenSwapFile();
9295 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
9296 if (rewriteAppendOnlyFile(tmpfile
) == REDIS_OK
) {
9303 if (childpid
== -1) {
9304 redisLog(REDIS_WARNING
,
9305 "Can't rewrite append only file in background: fork: %s",
9309 redisLog(REDIS_NOTICE
,
9310 "Background append only file rewriting started by pid %d",childpid
);
9311 server
.bgrewritechildpid
= childpid
;
9312 updateDictResizePolicy();
9313 /* We set appendseldb to -1 in order to force the next call to the
9314 * feedAppendOnlyFile() to issue a SELECT command, so the differences
9315 * accumulated by the parent into server.bgrewritebuf will start
9316 * with a SELECT statement and it will be safe to merge. */
9317 server
.appendseldb
= -1;
9320 return REDIS_OK
; /* unreached */
9323 static void bgrewriteaofCommand(redisClient
*c
) {
9324 if (server
.bgrewritechildpid
!= -1) {
9325 addReplySds(c
,sdsnew("-ERR background append only file rewriting already in progress\r\n"));
9328 if (rewriteAppendOnlyFileBackground() == REDIS_OK
) {
9329 char *status
= "+Background append only file rewriting started\r\n";
9330 addReplySds(c
,sdsnew(status
));
9332 addReply(c
,shared
.err
);
9336 static void aofRemoveTempFile(pid_t childpid
) {
9339 snprintf(tmpfile
,256,"temp-rewriteaof-bg-%d.aof", (int) childpid
);
9343 /* Virtual Memory is composed mainly of two subsystems:
9344 * - Blocking Virutal Memory
9345 * - Threaded Virtual Memory I/O
9346 * The two parts are not fully decoupled, but functions are split among two
9347 * different sections of the source code (delimited by comments) in order to
9348 * make more clear what functionality is about the blocking VM and what about
9349 * the threaded (not blocking) VM.
9353 * Redis VM is a blocking VM (one that blocks reading swapped values from
9354 * disk into memory when a value swapped out is needed in memory) that is made
9355 * unblocking by trying to examine the command argument vector in order to
9356 * load in background values that will likely be needed in order to exec
9357 * the command. The command is executed only once all the relevant keys
9358 * are loaded into memory.
9360 * This basically is almost as simple of a blocking VM, but almost as parallel
9361 * as a fully non-blocking VM.
9364 /* =================== Virtual Memory - Blocking Side ====================== */
9366 /* Create a VM pointer object. This kind of objects are used in place of
9367 * values in the key -> value hash table, for swapped out objects. */
9368 static vmpointer
*createVmPointer(int vtype
) {
9369 vmpointer
*vp
= zmalloc(sizeof(vmpointer
));
9371 vp
->type
= REDIS_VMPOINTER
;
9372 vp
->storage
= REDIS_VM_SWAPPED
;
9377 static void vmInit(void) {
9383 if (server
.vm_max_threads
!= 0)
9384 zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */
9386 redisLog(REDIS_NOTICE
,"Using '%s' as swap file",server
.vm_swap_file
);
9387 /* Try to open the old swap file, otherwise create it */
9388 if ((server
.vm_fp
= fopen(server
.vm_swap_file
,"r+b")) == NULL
) {
9389 server
.vm_fp
= fopen(server
.vm_swap_file
,"w+b");
9391 if (server
.vm_fp
== NULL
) {
9392 redisLog(REDIS_WARNING
,
9393 "Can't open the swap file: %s. Exiting.",
9397 server
.vm_fd
= fileno(server
.vm_fp
);
9398 /* Lock the swap file for writing, this is useful in order to avoid
9399 * another instance to use the same swap file for a config error. */
9400 fl
.l_type
= F_WRLCK
;
9401 fl
.l_whence
= SEEK_SET
;
9402 fl
.l_start
= fl
.l_len
= 0;
9403 if (fcntl(server
.vm_fd
,F_SETLK
,&fl
) == -1) {
9404 redisLog(REDIS_WARNING
,
9405 "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
));
9409 server
.vm_next_page
= 0;
9410 server
.vm_near_pages
= 0;
9411 server
.vm_stats_used_pages
= 0;
9412 server
.vm_stats_swapped_objects
= 0;
9413 server
.vm_stats_swapouts
= 0;
9414 server
.vm_stats_swapins
= 0;
9415 totsize
= server
.vm_pages
*server
.vm_page_size
;
9416 redisLog(REDIS_NOTICE
,"Allocating %lld bytes of swap file",totsize
);
9417 if (ftruncate(server
.vm_fd
,totsize
) == -1) {
9418 redisLog(REDIS_WARNING
,"Can't ftruncate swap file: %s. Exiting.",
9422 redisLog(REDIS_NOTICE
,"Swap file allocated with success");
9424 server
.vm_bitmap
= zmalloc((server
.vm_pages
+7)/8);
9425 redisLog(REDIS_VERBOSE
,"Allocated %lld bytes page table for %lld pages",
9426 (long long) (server
.vm_pages
+7)/8, server
.vm_pages
);
9427 memset(server
.vm_bitmap
,0,(server
.vm_pages
+7)/8);
9429 /* Initialize threaded I/O (used by Virtual Memory) */
9430 server
.io_newjobs
= listCreate();
9431 server
.io_processing
= listCreate();
9432 server
.io_processed
= listCreate();
9433 server
.io_ready_clients
= listCreate();
9434 pthread_mutex_init(&server
.io_mutex
,NULL
);
9435 pthread_mutex_init(&server
.obj_freelist_mutex
,NULL
);
9436 pthread_mutex_init(&server
.io_swapfile_mutex
,NULL
);
9437 server
.io_active_threads
= 0;
9438 if (pipe(pipefds
) == -1) {
9439 redisLog(REDIS_WARNING
,"Unable to intialized VM: pipe(2): %s. Exiting."
9443 server
.io_ready_pipe_read
= pipefds
[0];
9444 server
.io_ready_pipe_write
= pipefds
[1];
9445 redisAssert(anetNonBlock(NULL
,server
.io_ready_pipe_read
) != ANET_ERR
);
9446 /* LZF requires a lot of stack */
9447 pthread_attr_init(&server
.io_threads_attr
);
9448 pthread_attr_getstacksize(&server
.io_threads_attr
, &stacksize
);
9449 while (stacksize
< REDIS_THREAD_STACK_SIZE
) stacksize
*= 2;
9450 pthread_attr_setstacksize(&server
.io_threads_attr
, stacksize
);
9451 /* Listen for events in the threaded I/O pipe */
9452 if (aeCreateFileEvent(server
.el
, server
.io_ready_pipe_read
, AE_READABLE
,
9453 vmThreadedIOCompletedJob
, NULL
) == AE_ERR
)
9454 oom("creating file event");
9457 /* Mark the page as used */
9458 static void vmMarkPageUsed(off_t page
) {
9459 off_t byte
= page
/8;
9461 redisAssert(vmFreePage(page
) == 1);
9462 server
.vm_bitmap
[byte
] |= 1<<bit
;
9465 /* Mark N contiguous pages as used, with 'page' being the first. */
9466 static void vmMarkPagesUsed(off_t page
, off_t count
) {
9469 for (j
= 0; j
< count
; j
++)
9470 vmMarkPageUsed(page
+j
);
9471 server
.vm_stats_used_pages
+= count
;
9472 redisLog(REDIS_DEBUG
,"Mark USED pages: %lld pages at %lld\n",
9473 (long long)count
, (long long)page
);
9476 /* Mark the page as free */
9477 static void vmMarkPageFree(off_t page
) {
9478 off_t byte
= page
/8;
9480 redisAssert(vmFreePage(page
) == 0);
9481 server
.vm_bitmap
[byte
] &= ~(1<<bit
);
9484 /* Mark N contiguous pages as free, with 'page' being the first. */
9485 static void vmMarkPagesFree(off_t page
, off_t count
) {
9488 for (j
= 0; j
< count
; j
++)
9489 vmMarkPageFree(page
+j
);
9490 server
.vm_stats_used_pages
-= count
;
9491 redisLog(REDIS_DEBUG
,"Mark FREE pages: %lld pages at %lld\n",
9492 (long long)count
, (long long)page
);
9495 /* Test if the page is free */
9496 static int vmFreePage(off_t page
) {
9497 off_t byte
= page
/8;
9499 return (server
.vm_bitmap
[byte
] & (1<<bit
)) == 0;
9502 /* Find N contiguous free pages storing the first page of the cluster in *first.
9503 * Returns REDIS_OK if it was able to find N contiguous pages, otherwise
9504 * REDIS_ERR is returned.
9506 * This function uses a simple algorithm: we try to allocate
9507 * REDIS_VM_MAX_NEAR_PAGES sequentially, when we reach this limit we start
9508 * again from the start of the swap file searching for free spaces.
9510 * If it looks pretty clear that there are no free pages near our offset
9511 * we try to find less populated places doing a forward jump of
9512 * REDIS_VM_MAX_RANDOM_JUMP, then we start scanning again a few pages
9513 * without hurry, and then we jump again and so forth...
9515 * This function can be improved using a free list to avoid to guess
9516 * too much, since we could collect data about freed pages.
9518 * note: I implemented this function just after watching an episode of
9519 * Battlestar Galactica, where the hybrid was continuing to say "JUMP!"
9521 static int vmFindContiguousPages(off_t
*first
, off_t n
) {
9522 off_t base
, offset
= 0, since_jump
= 0, numfree
= 0;
9524 if (server
.vm_near_pages
== REDIS_VM_MAX_NEAR_PAGES
) {
9525 server
.vm_near_pages
= 0;
9526 server
.vm_next_page
= 0;
9528 server
.vm_near_pages
++; /* Yet another try for pages near to the old ones */
9529 base
= server
.vm_next_page
;
9531 while(offset
< server
.vm_pages
) {
9532 off_t
this = base
+offset
;
9534 /* If we overflow, restart from page zero */
9535 if (this >= server
.vm_pages
) {
9536 this -= server
.vm_pages
;
9538 /* Just overflowed, what we found on tail is no longer
9539 * interesting, as it's no longer contiguous. */
9543 if (vmFreePage(this)) {
9544 /* This is a free page */
9546 /* Already got N free pages? Return to the caller, with success */
9548 *first
= this-(n
-1);
9549 server
.vm_next_page
= this+1;
9550 redisLog(REDIS_DEBUG
, "FOUND CONTIGUOUS PAGES: %lld pages at %lld\n", (long long) n
, (long long) *first
);
9554 /* The current one is not a free page */
9558 /* Fast-forward if the current page is not free and we already
9559 * searched enough near this place. */
9561 if (!numfree
&& since_jump
>= REDIS_VM_MAX_RANDOM_JUMP
/4) {
9562 offset
+= random() % REDIS_VM_MAX_RANDOM_JUMP
;
9564 /* Note that even if we rewind after the jump, we are don't need
9565 * to make sure numfree is set to zero as we only jump *if* it
9566 * is set to zero. */
9568 /* Otherwise just check the next page */
9575 /* Write the specified object at the specified page of the swap file */
9576 static int vmWriteObjectOnSwap(robj
*o
, off_t page
) {
9577 if (server
.vm_enabled
) pthread_mutex_lock(&server
.io_swapfile_mutex
);
9578 if (fseeko(server
.vm_fp
,page
*server
.vm_page_size
,SEEK_SET
) == -1) {
9579 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.io_swapfile_mutex
);
9580 redisLog(REDIS_WARNING
,
9581 "Critical VM problem in vmWriteObjectOnSwap(): can't seek: %s",
9585 rdbSaveObject(server
.vm_fp
,o
);
9586 fflush(server
.vm_fp
);
9587 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.io_swapfile_mutex
);
9591 /* Transfers the 'val' object to disk. Store all the information
9592 * a 'vmpointer' object containing all the information needed to load the
9593 * object back later is returned.
9595 * If we can't find enough contiguous empty pages to swap the object on disk
9596 * NULL is returned. */
9597 static vmpointer
*vmSwapObjectBlocking(robj
*val
) {
9598 off_t pages
= rdbSavedObjectPages(val
,NULL
);
9602 assert(val
->storage
== REDIS_VM_MEMORY
);
9603 assert(val
->refcount
== 1);
9604 if (vmFindContiguousPages(&page
,pages
) == REDIS_ERR
) return NULL
;
9605 if (vmWriteObjectOnSwap(val
,page
) == REDIS_ERR
) return NULL
;
9607 vp
= createVmPointer(val
->type
);
9609 vp
->usedpages
= pages
;
9610 decrRefCount(val
); /* Deallocate the object from memory. */
9611 vmMarkPagesUsed(page
,pages
);
9612 redisLog(REDIS_DEBUG
,"VM: object %p swapped out at %lld (%lld pages)",
9614 (unsigned long long) page
, (unsigned long long) pages
);
9615 server
.vm_stats_swapped_objects
++;
9616 server
.vm_stats_swapouts
++;
9620 static robj
*vmReadObjectFromSwap(off_t page
, int type
) {
9623 if (server
.vm_enabled
) pthread_mutex_lock(&server
.io_swapfile_mutex
);
9624 if (fseeko(server
.vm_fp
,page
*server
.vm_page_size
,SEEK_SET
) == -1) {
9625 redisLog(REDIS_WARNING
,
9626 "Unrecoverable VM problem in vmReadObjectFromSwap(): can't seek: %s",
9630 o
= rdbLoadObject(type
,server
.vm_fp
);
9632 redisLog(REDIS_WARNING
, "Unrecoverable VM problem in vmReadObjectFromSwap(): can't load object from swap file: %s", strerror(errno
));
9635 if (server
.vm_enabled
) pthread_mutex_unlock(&server
.io_swapfile_mutex
);
9639 /* Load the specified object from swap to memory.
9640 * The newly allocated object is returned.
9642 * If preview is true the unserialized object is returned to the caller but
9643 * the pages are not marked as freed, nor the vp object is freed. */
9644 static robj
*vmGenericLoadObject(vmpointer
*vp
, int preview
) {
9647 redisAssert(vp
->type
== REDIS_VMPOINTER
&&
9648 (vp
->storage
== REDIS_VM_SWAPPED
|| vp
->storage
== REDIS_VM_LOADING
));
9649 val
= vmReadObjectFromSwap(vp
->page
,vp
->vtype
);
9651 redisLog(REDIS_DEBUG
, "VM: object %p loaded from disk", (void*)vp
);
9652 vmMarkPagesFree(vp
->page
,vp
->usedpages
);
9654 server
.vm_stats_swapped_objects
--;
9656 redisLog(REDIS_DEBUG
, "VM: object %p previewed from disk", (void*)vp
);
9658 server
.vm_stats_swapins
++;
9662 /* Plain object loading, from swap to memory.
9664 * 'o' is actually a redisVmPointer structure that will be freed by the call.
9665 * The return value is the loaded object. */
9666 static robj
*vmLoadObject(robj
*o
) {
9667 /* If we are loading the object in background, stop it, we
9668 * need to load this object synchronously ASAP. */
9669 if (o
->storage
== REDIS_VM_LOADING
)
9670 vmCancelThreadedIOJob(o
);
9671 return vmGenericLoadObject((vmpointer
*)o
,0);
9674 /* Just load the value on disk, without to modify the key.
9675 * This is useful when we want to perform some operation on the value
9676 * without to really bring it from swap to memory, like while saving the
9677 * dataset or rewriting the append only log. */
9678 static robj
*vmPreviewObject(robj
*o
) {
9679 return vmGenericLoadObject((vmpointer
*)o
,1);
9682 /* How a good candidate is this object for swapping?
9683 * The better candidate it is, the greater the returned value.
9685 * Currently we try to perform a fast estimation of the object size in
9686 * memory, and combine it with aging informations.
9688 * Basically swappability = idle-time * log(estimated size)
9690 * Bigger objects are preferred over smaller objects, but not
9691 * proportionally, this is why we use the logarithm. This algorithm is
9692 * just a first try and will probably be tuned later. */
9693 static double computeObjectSwappability(robj
*o
) {
9694 /* actual age can be >= minage, but not < minage. As we use wrapping
9695 * 21 bit clocks with minutes resolution for the LRU. */
9696 time_t minage
= abs(server
.lruclock
- o
->lru
);
9697 long asize
= 0, elesize
;
9702 struct dictEntry
*de
;
9705 if (minage
<= 0) return 0;
9708 if (o
->encoding
!= REDIS_ENCODING_RAW
) {
9711 asize
= sdslen(o
->ptr
)+sizeof(*o
)+sizeof(long)*2;
9715 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
9716 asize
= sizeof(*o
)+ziplistSize(o
->ptr
);
9720 asize
= sizeof(list
);
9723 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9724 (sizeof(*o
)+sdslen(ele
->ptr
)) : sizeof(*o
);
9725 asize
+= (sizeof(listNode
)+elesize
)*listLength(l
);
9731 z
= (o
->type
== REDIS_ZSET
);
9732 d
= z
? ((zset
*)o
->ptr
)->dict
: o
->ptr
;
9734 asize
= sizeof(dict
)+(sizeof(struct dictEntry
*)*dictSlots(d
));
9735 if (z
) asize
+= sizeof(zset
)-sizeof(dict
);
9737 de
= dictGetRandomKey(d
);
9738 ele
= dictGetEntryKey(de
);
9739 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9740 (sizeof(*o
)+sdslen(ele
->ptr
)) : sizeof(*o
);
9741 asize
+= (sizeof(struct dictEntry
)+elesize
)*dictSize(d
);
9742 if (z
) asize
+= sizeof(zskiplistNode
)*dictSize(d
);
9746 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
9747 unsigned char *p
= zipmapRewind((unsigned char*)o
->ptr
);
9748 unsigned int len
= zipmapLen((unsigned char*)o
->ptr
);
9749 unsigned int klen
, vlen
;
9750 unsigned char *key
, *val
;
9752 if ((p
= zipmapNext(p
,&key
,&klen
,&val
,&vlen
)) == NULL
) {
9756 asize
= len
*(klen
+vlen
+3);
9757 } else if (o
->encoding
== REDIS_ENCODING_HT
) {
9759 asize
= sizeof(dict
)+(sizeof(struct dictEntry
*)*dictSlots(d
));
9761 de
= dictGetRandomKey(d
);
9762 ele
= dictGetEntryKey(de
);
9763 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9764 (sizeof(*o
)+sdslen(ele
->ptr
)) : sizeof(*o
);
9765 ele
= dictGetEntryVal(de
);
9766 elesize
= (ele
->encoding
== REDIS_ENCODING_RAW
) ?
9767 (sizeof(*o
)+sdslen(ele
->ptr
)) : sizeof(*o
);
9768 asize
+= (sizeof(struct dictEntry
)+elesize
)*dictSize(d
);
9773 return (double)minage
*log(1+asize
);
9776 /* Try to swap an object that's a good candidate for swapping.
9777 * Returns REDIS_OK if the object was swapped, REDIS_ERR if it's not possible
9778 * to swap any object at all.
9780 * If 'usethreaded' is true, Redis will try to swap the object in background
9781 * using I/O threads. */
9782 static int vmSwapOneObject(int usethreads
) {
9784 struct dictEntry
*best
= NULL
;
9785 double best_swappability
= 0;
9786 redisDb
*best_db
= NULL
;
9790 for (j
= 0; j
< server
.dbnum
; j
++) {
9791 redisDb
*db
= server
.db
+j
;
9792 /* Why maxtries is set to 100?
9793 * Because this way (usually) we'll find 1 object even if just 1% - 2%
9794 * are swappable objects */
9797 if (dictSize(db
->dict
) == 0) continue;
9798 for (i
= 0; i
< 5; i
++) {
9800 double swappability
;
9802 if (maxtries
) maxtries
--;
9803 de
= dictGetRandomKey(db
->dict
);
9804 val
= dictGetEntryVal(de
);
9805 /* Only swap objects that are currently in memory.
9807 * Also don't swap shared objects: not a good idea in general and
9808 * we need to ensure that the main thread does not touch the
9809 * object while the I/O thread is using it, but we can't
9810 * control other keys without adding additional mutex. */
9811 if (val
->storage
!= REDIS_VM_MEMORY
|| val
->refcount
!= 1) {
9812 if (maxtries
) i
--; /* don't count this try */
9815 swappability
= computeObjectSwappability(val
);
9816 if (!best
|| swappability
> best_swappability
) {
9818 best_swappability
= swappability
;
9823 if (best
== NULL
) return REDIS_ERR
;
9824 key
= dictGetEntryKey(best
);
9825 val
= dictGetEntryVal(best
);
9827 redisLog(REDIS_DEBUG
,"Key with best swappability: %s, %f",
9828 key
, best_swappability
);
9832 robj
*keyobj
= createStringObject(key
,sdslen(key
));
9833 vmSwapObjectThreaded(keyobj
,val
,best_db
);
9834 decrRefCount(keyobj
);
9839 if ((vp
= vmSwapObjectBlocking(val
)) != NULL
) {
9840 dictGetEntryVal(best
) = vp
;
9848 static int vmSwapOneObjectBlocking() {
9849 return vmSwapOneObject(0);
9852 static int vmSwapOneObjectThreaded() {
9853 return vmSwapOneObject(1);
9856 /* Return true if it's safe to swap out objects in a given moment.
9857 * Basically we don't want to swap objects out while there is a BGSAVE
9858 * or a BGAEOREWRITE running in backgroud. */
9859 static int vmCanSwapOut(void) {
9860 return (server
.bgsavechildpid
== -1 && server
.bgrewritechildpid
== -1);
9863 /* =================== Virtual Memory - Threaded I/O ======================= */
9865 static void freeIOJob(iojob
*j
) {
9866 if ((j
->type
== REDIS_IOJOB_PREPARE_SWAP
||
9867 j
->type
== REDIS_IOJOB_DO_SWAP
||
9868 j
->type
== REDIS_IOJOB_LOAD
) && j
->val
!= NULL
)
9870 /* we fix the storage type, otherwise decrRefCount() will try to
9871 * kill the I/O thread Job (that does no longer exists). */
9872 if (j
->val
->storage
== REDIS_VM_SWAPPING
)
9873 j
->val
->storage
= REDIS_VM_MEMORY
;
9874 decrRefCount(j
->val
);
9876 decrRefCount(j
->key
);
9880 /* Every time a thread finished a Job, it writes a byte into the write side
9881 * of an unix pipe in order to "awake" the main thread, and this function
9883 static void vmThreadedIOCompletedJob(aeEventLoop
*el
, int fd
, void *privdata
,
9887 int retval
, processed
= 0, toprocess
= -1, trytoswap
= 1;
9889 REDIS_NOTUSED(mask
);
9890 REDIS_NOTUSED(privdata
);
9892 /* For every byte we read in the read side of the pipe, there is one
9893 * I/O job completed to process. */
9894 while((retval
= read(fd
,buf
,1)) == 1) {
9897 struct dictEntry
*de
;
9899 redisLog(REDIS_DEBUG
,"Processing I/O completed job");
9901 /* Get the processed element (the oldest one) */
9903 assert(listLength(server
.io_processed
) != 0);
9904 if (toprocess
== -1) {
9905 toprocess
= (listLength(server
.io_processed
)*REDIS_MAX_COMPLETED_JOBS_PROCESSED
)/100;
9906 if (toprocess
<= 0) toprocess
= 1;
9908 ln
= listFirst(server
.io_processed
);
9910 listDelNode(server
.io_processed
,ln
);
9912 /* If this job is marked as canceled, just ignore it */
9917 /* Post process it in the main thread, as there are things we
9918 * can do just here to avoid race conditions and/or invasive locks */
9919 redisLog(REDIS_DEBUG
,"COMPLETED Job type: %d, ID %p, key: %s", j
->type
, (void*)j
->id
, (unsigned char*)j
->key
->ptr
);
9920 de
= dictFind(j
->db
->dict
,j
->key
->ptr
);
9921 redisAssert(de
!= NULL
);
9922 if (j
->type
== REDIS_IOJOB_LOAD
) {
9924 vmpointer
*vp
= dictGetEntryVal(de
);
9926 /* Key loaded, bring it at home */
9927 vmMarkPagesFree(vp
->page
,vp
->usedpages
);
9928 redisLog(REDIS_DEBUG
, "VM: object %s loaded from disk (threaded)",
9929 (unsigned char*) j
->key
->ptr
);
9930 server
.vm_stats_swapped_objects
--;
9931 server
.vm_stats_swapins
++;
9932 dictGetEntryVal(de
) = j
->val
;
9933 incrRefCount(j
->val
);
9935 /* Handle clients waiting for this key to be loaded. */
9936 handleClientsBlockedOnSwappedKey(db
,j
->key
);
9939 } else if (j
->type
== REDIS_IOJOB_PREPARE_SWAP
) {
9940 /* Now we know the amount of pages required to swap this object.
9941 * Let's find some space for it, and queue this task again
9942 * rebranded as REDIS_IOJOB_DO_SWAP. */
9943 if (!vmCanSwapOut() ||
9944 vmFindContiguousPages(&j
->page
,j
->pages
) == REDIS_ERR
)
9946 /* Ooops... no space or we can't swap as there is
9947 * a fork()ed Redis trying to save stuff on disk. */
9948 j
->val
->storage
= REDIS_VM_MEMORY
; /* undo operation */
9951 /* Note that we need to mark this pages as used now,
9952 * if the job will be canceled, we'll mark them as freed
9954 vmMarkPagesUsed(j
->page
,j
->pages
);
9955 j
->type
= REDIS_IOJOB_DO_SWAP
;
9960 } else if (j
->type
== REDIS_IOJOB_DO_SWAP
) {
9963 /* Key swapped. We can finally free some memory. */
9964 if (j
->val
->storage
!= REDIS_VM_SWAPPING
) {
9965 vmpointer
*vp
= (vmpointer
*) j
->id
;
9966 printf("storage: %d\n",vp
->storage
);
9967 printf("key->name: %s\n",(char*)j
->key
->ptr
);
9968 printf("val: %p\n",(void*)j
->val
);
9969 printf("val->type: %d\n",j
->val
->type
);
9970 printf("val->ptr: %s\n",(char*)j
->val
->ptr
);
9972 redisAssert(j
->val
->storage
== REDIS_VM_SWAPPING
);
9973 vp
= createVmPointer(j
->val
->type
);
9975 vp
->usedpages
= j
->pages
;
9976 dictGetEntryVal(de
) = vp
;
9977 /* Fix the storage otherwise decrRefCount will attempt to
9978 * remove the associated I/O job */
9979 j
->val
->storage
= REDIS_VM_MEMORY
;
9980 decrRefCount(j
->val
);
9981 redisLog(REDIS_DEBUG
,
9982 "VM: object %s swapped out at %lld (%lld pages) (threaded)",
9983 (unsigned char*) j
->key
->ptr
,
9984 (unsigned long long) j
->page
, (unsigned long long) j
->pages
);
9985 server
.vm_stats_swapped_objects
++;
9986 server
.vm_stats_swapouts
++;
9988 /* Put a few more swap requests in queue if we are still
9990 if (trytoswap
&& vmCanSwapOut() &&
9991 zmalloc_used_memory() > server
.vm_max_memory
)
9996 more
= listLength(server
.io_newjobs
) <
9997 (unsigned) server
.vm_max_threads
;
9999 /* Don't waste CPU time if swappable objects are rare. */
10000 if (vmSwapOneObjectThreaded() == REDIS_ERR
) {
10008 if (processed
== toprocess
) return;
10010 if (retval
< 0 && errno
!= EAGAIN
) {
10011 redisLog(REDIS_WARNING
,
10012 "WARNING: read(2) error in vmThreadedIOCompletedJob() %s",
10017 static void lockThreadedIO(void) {
10018 pthread_mutex_lock(&server
.io_mutex
);
10021 static void unlockThreadedIO(void) {
10022 pthread_mutex_unlock(&server
.io_mutex
);
10025 /* Remove the specified object from the threaded I/O queue if still not
10026 * processed, otherwise make sure to flag it as canceled. */
10027 static void vmCancelThreadedIOJob(robj
*o
) {
10029 server
.io_newjobs
, /* 0 */
10030 server
.io_processing
, /* 1 */
10031 server
.io_processed
/* 2 */
10035 assert(o
->storage
== REDIS_VM_LOADING
|| o
->storage
== REDIS_VM_SWAPPING
);
10038 /* Search for a matching object in one of the queues */
10039 for (i
= 0; i
< 3; i
++) {
10043 listRewind(lists
[i
],&li
);
10044 while ((ln
= listNext(&li
)) != NULL
) {
10045 iojob
*job
= ln
->value
;
10047 if (job
->canceled
) continue; /* Skip this, already canceled. */
10048 if (job
->id
== o
) {
10049 redisLog(REDIS_DEBUG
,"*** CANCELED %p (key %s) (type %d) (LIST ID %d)\n",
10050 (void*)job
, (char*)job
->key
->ptr
, job
->type
, i
);
10051 /* Mark the pages as free since the swap didn't happened
10052 * or happened but is now discarded. */
10053 if (i
!= 1 && job
->type
== REDIS_IOJOB_DO_SWAP
)
10054 vmMarkPagesFree(job
->page
,job
->pages
);
10055 /* Cancel the job. It depends on the list the job is
10058 case 0: /* io_newjobs */
10059 /* If the job was yet not processed the best thing to do
10060 * is to remove it from the queue at all */
10062 listDelNode(lists
[i
],ln
);
10064 case 1: /* io_processing */
10065 /* Oh Shi- the thread is messing with the Job:
10067 * Probably it's accessing the object if this is a
10068 * PREPARE_SWAP or DO_SWAP job.
10069 * If it's a LOAD job it may be reading from disk and
10070 * if we don't wait for the job to terminate before to
10071 * cancel it, maybe in a few microseconds data can be
10072 * corrupted in this pages. So the short story is:
10074 * Better to wait for the job to move into the
10075 * next queue (processed)... */
10077 /* We try again and again until the job is completed. */
10078 unlockThreadedIO();
10079 /* But let's wait some time for the I/O thread
10080 * to finish with this job. After all this condition
10081 * should be very rare. */
10084 case 2: /* io_processed */
10085 /* The job was already processed, that's easy...
10086 * just mark it as canceled so that we'll ignore it
10087 * when processing completed jobs. */
10091 /* Finally we have to adjust the storage type of the object
10092 * in order to "UNDO" the operaiton. */
10093 if (o
->storage
== REDIS_VM_LOADING
)
10094 o
->storage
= REDIS_VM_SWAPPED
;
10095 else if (o
->storage
== REDIS_VM_SWAPPING
)
10096 o
->storage
= REDIS_VM_MEMORY
;
10097 unlockThreadedIO();
10098 redisLog(REDIS_DEBUG
,"*** DONE");
10103 unlockThreadedIO();
10104 printf("Not found: %p\n", (void*)o
);
10105 redisAssert(1 != 1); /* We should never reach this */
10108 static void *IOThreadEntryPoint(void *arg
) {
10111 REDIS_NOTUSED(arg
);
10113 pthread_detach(pthread_self());
10115 /* Get a new job to process */
10117 if (listLength(server
.io_newjobs
) == 0) {
10118 /* No new jobs in queue, exit. */
10119 redisLog(REDIS_DEBUG
,"Thread %ld exiting, nothing to do",
10120 (long) pthread_self());
10121 server
.io_active_threads
--;
10122 unlockThreadedIO();
10125 ln
= listFirst(server
.io_newjobs
);
10127 listDelNode(server
.io_newjobs
,ln
);
10128 /* Add the job in the processing queue */
10129 j
->thread
= pthread_self();
10130 listAddNodeTail(server
.io_processing
,j
);
10131 ln
= listLast(server
.io_processing
); /* We use ln later to remove it */
10132 unlockThreadedIO();
10133 redisLog(REDIS_DEBUG
,"Thread %ld got a new job (type %d): %p about key '%s'",
10134 (long) pthread_self(), j
->type
, (void*)j
, (char*)j
->key
->ptr
);
10136 /* Process the Job */
10137 if (j
->type
== REDIS_IOJOB_LOAD
) {
10138 vmpointer
*vp
= (vmpointer
*)j
->id
;
10139 j
->val
= vmReadObjectFromSwap(j
->page
,vp
->vtype
);
10140 } else if (j
->type
== REDIS_IOJOB_PREPARE_SWAP
) {
10141 FILE *fp
= fopen("/dev/null","w+");
10142 j
->pages
= rdbSavedObjectPages(j
->val
,fp
);
10144 } else if (j
->type
== REDIS_IOJOB_DO_SWAP
) {
10145 if (vmWriteObjectOnSwap(j
->val
,j
->page
) == REDIS_ERR
)
10149 /* Done: insert the job into the processed queue */
10150 redisLog(REDIS_DEBUG
,"Thread %ld completed the job: %p (key %s)",
10151 (long) pthread_self(), (void*)j
, (char*)j
->key
->ptr
);
10153 listDelNode(server
.io_processing
,ln
);
10154 listAddNodeTail(server
.io_processed
,j
);
10155 unlockThreadedIO();
10157 /* Signal the main thread there is new stuff to process */
10158 assert(write(server
.io_ready_pipe_write
,"x",1) == 1);
10160 return NULL
; /* never reached */
10163 static void spawnIOThread(void) {
10165 sigset_t mask
, omask
;
10168 sigemptyset(&mask
);
10169 sigaddset(&mask
,SIGCHLD
);
10170 sigaddset(&mask
,SIGHUP
);
10171 sigaddset(&mask
,SIGPIPE
);
10172 pthread_sigmask(SIG_SETMASK
, &mask
, &omask
);
10173 while ((err
= pthread_create(&thread
,&server
.io_threads_attr
,IOThreadEntryPoint
,NULL
)) != 0) {
10174 redisLog(REDIS_WARNING
,"Unable to spawn an I/O thread: %s",
10178 pthread_sigmask(SIG_SETMASK
, &omask
, NULL
);
10179 server
.io_active_threads
++;
10182 /* We need to wait for the last thread to exit before we are able to
10183 * fork() in order to BGSAVE or BGREWRITEAOF. */
10184 static void waitEmptyIOJobsQueue(void) {
10186 int io_processed_len
;
10189 if (listLength(server
.io_newjobs
) == 0 &&
10190 listLength(server
.io_processing
) == 0 &&
10191 server
.io_active_threads
== 0)
10193 unlockThreadedIO();
10196 /* While waiting for empty jobs queue condition we post-process some
10197 * finshed job, as I/O threads may be hanging trying to write against
10198 * the io_ready_pipe_write FD but there are so much pending jobs that
10199 * it's blocking. */
10200 io_processed_len
= listLength(server
.io_processed
);
10201 unlockThreadedIO();
10202 if (io_processed_len
) {
10203 vmThreadedIOCompletedJob(NULL
,server
.io_ready_pipe_read
,NULL
,0);
10204 usleep(1000); /* 1 millisecond */
10206 usleep(10000); /* 10 milliseconds */
10211 static void vmReopenSwapFile(void) {
10212 /* Note: we don't close the old one as we are in the child process
10213 * and don't want to mess at all with the original file object. */
10214 server
.vm_fp
= fopen(server
.vm_swap_file
,"r+b");
10215 if (server
.vm_fp
== NULL
) {
10216 redisLog(REDIS_WARNING
,"Can't re-open the VM swap file: %s. Exiting.",
10217 server
.vm_swap_file
);
10220 server
.vm_fd
= fileno(server
.vm_fp
);
10223 /* This function must be called while with threaded IO locked */
10224 static void queueIOJob(iojob
*j
) {
10225 redisLog(REDIS_DEBUG
,"Queued IO Job %p type %d about key '%s'\n",
10226 (void*)j
, j
->type
, (char*)j
->key
->ptr
);
10227 listAddNodeTail(server
.io_newjobs
,j
);
10228 if (server
.io_active_threads
< server
.vm_max_threads
)
10232 static int vmSwapObjectThreaded(robj
*key
, robj
*val
, redisDb
*db
) {
10235 j
= zmalloc(sizeof(*j
));
10236 j
->type
= REDIS_IOJOB_PREPARE_SWAP
;
10240 j
->id
= j
->val
= val
;
10243 j
->thread
= (pthread_t
) -1;
10244 val
->storage
= REDIS_VM_SWAPPING
;
10248 unlockThreadedIO();
10252 /* ============ Virtual Memory - Blocking clients on missing keys =========== */
10254 /* This function makes the clinet 'c' waiting for the key 'key' to be loaded.
10255 * If there is not already a job loading the key, it is craeted.
10256 * The key is added to the io_keys list in the client structure, and also
10257 * in the hash table mapping swapped keys to waiting clients, that is,
10258 * server.io_waited_keys. */
10259 static int waitForSwappedKey(redisClient
*c
, robj
*key
) {
10260 struct dictEntry
*de
;
10264 /* If the key does not exist or is already in RAM we don't need to
10265 * block the client at all. */
10266 de
= dictFind(c
->db
->dict
,key
->ptr
);
10267 if (de
== NULL
) return 0;
10268 o
= dictGetEntryVal(de
);
10269 if (o
->storage
== REDIS_VM_MEMORY
) {
10271 } else if (o
->storage
== REDIS_VM_SWAPPING
) {
10272 /* We were swapping the key, undo it! */
10273 vmCancelThreadedIOJob(o
);
10277 /* OK: the key is either swapped, or being loaded just now. */
10279 /* Add the key to the list of keys this client is waiting for.
10280 * This maps clients to keys they are waiting for. */
10281 listAddNodeTail(c
->io_keys
,key
);
10284 /* Add the client to the swapped keys => clients waiting map. */
10285 de
= dictFind(c
->db
->io_keys
,key
);
10289 /* For every key we take a list of clients blocked for it */
10291 retval
= dictAdd(c
->db
->io_keys
,key
,l
);
10293 assert(retval
== DICT_OK
);
10295 l
= dictGetEntryVal(de
);
10297 listAddNodeTail(l
,c
);
10299 /* Are we already loading the key from disk? If not create a job */
10300 if (o
->storage
== REDIS_VM_SWAPPED
) {
10302 vmpointer
*vp
= (vmpointer
*)o
;
10304 o
->storage
= REDIS_VM_LOADING
;
10305 j
= zmalloc(sizeof(*j
));
10306 j
->type
= REDIS_IOJOB_LOAD
;
10311 j
->page
= vp
->page
;
10314 j
->thread
= (pthread_t
) -1;
10317 unlockThreadedIO();
10322 /* Preload keys for any command with first, last and step values for
10323 * the command keys prototype, as defined in the command table. */
10324 static void waitForMultipleSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
) {
10326 if (cmd
->vm_firstkey
== 0) return;
10327 last
= cmd
->vm_lastkey
;
10328 if (last
< 0) last
= argc
+last
;
10329 for (j
= cmd
->vm_firstkey
; j
<= last
; j
+= cmd
->vm_keystep
) {
10330 redisAssert(j
< argc
);
10331 waitForSwappedKey(c
,argv
[j
]);
10335 /* Preload keys needed for the ZUNIONSTORE and ZINTERSTORE commands.
10336 * Note that the number of keys to preload is user-defined, so we need to
10337 * apply a sanity check against argc. */
10338 static void zunionInterBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
) {
10340 REDIS_NOTUSED(cmd
);
10342 num
= atoi(argv
[2]->ptr
);
10343 if (num
> (argc
-3)) return;
10344 for (i
= 0; i
< num
; i
++) {
10345 waitForSwappedKey(c
,argv
[3+i
]);
10349 /* Preload keys needed to execute the entire MULTI/EXEC block.
10351 * This function is called by blockClientOnSwappedKeys when EXEC is issued,
10352 * and will block the client when any command requires a swapped out value. */
10353 static void execBlockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
, int argc
, robj
**argv
) {
10355 struct redisCommand
*mcmd
;
10357 REDIS_NOTUSED(cmd
);
10358 REDIS_NOTUSED(argc
);
10359 REDIS_NOTUSED(argv
);
10361 if (!(c
->flags
& REDIS_MULTI
)) return;
10362 for (i
= 0; i
< c
->mstate
.count
; i
++) {
10363 mcmd
= c
->mstate
.commands
[i
].cmd
;
10364 margc
= c
->mstate
.commands
[i
].argc
;
10365 margv
= c
->mstate
.commands
[i
].argv
;
10367 if (mcmd
->vm_preload_proc
!= NULL
) {
10368 mcmd
->vm_preload_proc(c
,mcmd
,margc
,margv
);
10370 waitForMultipleSwappedKeys(c
,mcmd
,margc
,margv
);
10375 /* Is this client attempting to run a command against swapped keys?
10376 * If so, block it ASAP, load the keys in background, then resume it.
10378 * The important idea about this function is that it can fail! If keys will
10379 * still be swapped when the client is resumed, this key lookups will
10380 * just block loading keys from disk. In practical terms this should only
10381 * happen with SORT BY command or if there is a bug in this function.
10383 * Return 1 if the client is marked as blocked, 0 if the client can
10384 * continue as the keys it is going to access appear to be in memory. */
10385 static int blockClientOnSwappedKeys(redisClient
*c
, struct redisCommand
*cmd
) {
10386 if (cmd
->vm_preload_proc
!= NULL
) {
10387 cmd
->vm_preload_proc(c
,cmd
,c
->argc
,c
->argv
);
10389 waitForMultipleSwappedKeys(c
,cmd
,c
->argc
,c
->argv
);
10392 /* If the client was blocked for at least one key, mark it as blocked. */
10393 if (listLength(c
->io_keys
)) {
10394 c
->flags
|= REDIS_IO_WAIT
;
10395 aeDeleteFileEvent(server
.el
,c
->fd
,AE_READABLE
);
10396 server
.vm_blocked_clients
++;
10403 /* Remove the 'key' from the list of blocked keys for a given client.
10405 * The function returns 1 when there are no longer blocking keys after
10406 * the current one was removed (and the client can be unblocked). */
10407 static int dontWaitForSwappedKey(redisClient
*c
, robj
*key
) {
10411 struct dictEntry
*de
;
10413 /* Remove the key from the list of keys this client is waiting for. */
10414 listRewind(c
->io_keys
,&li
);
10415 while ((ln
= listNext(&li
)) != NULL
) {
10416 if (equalStringObjects(ln
->value
,key
)) {
10417 listDelNode(c
->io_keys
,ln
);
10421 assert(ln
!= NULL
);
10423 /* Remove the client form the key => waiting clients map. */
10424 de
= dictFind(c
->db
->io_keys
,key
);
10425 assert(de
!= NULL
);
10426 l
= dictGetEntryVal(de
);
10427 ln
= listSearchKey(l
,c
);
10428 assert(ln
!= NULL
);
10430 if (listLength(l
) == 0)
10431 dictDelete(c
->db
->io_keys
,key
);
10433 return listLength(c
->io_keys
) == 0;
10436 /* Every time we now a key was loaded back in memory, we handle clients
10437 * waiting for this key if any. */
10438 static void handleClientsBlockedOnSwappedKey(redisDb
*db
, robj
*key
) {
10439 struct dictEntry
*de
;
10444 de
= dictFind(db
->io_keys
,key
);
10447 l
= dictGetEntryVal(de
);
10448 len
= listLength(l
);
10449 /* Note: we can't use something like while(listLength(l)) as the list
10450 * can be freed by the calling function when we remove the last element. */
10453 redisClient
*c
= ln
->value
;
10455 if (dontWaitForSwappedKey(c
,key
)) {
10456 /* Put the client in the list of clients ready to go as we
10457 * loaded all the keys about it. */
10458 listAddNodeTail(server
.io_ready_clients
,c
);
10463 /* =========================== Remote Configuration ========================= */
10465 static void configSetCommand(redisClient
*c
) {
10466 robj
*o
= getDecodedObject(c
->argv
[3]);
10469 if (!strcasecmp(c
->argv
[2]->ptr
,"dbfilename")) {
10470 zfree(server
.dbfilename
);
10471 server
.dbfilename
= zstrdup(o
->ptr
);
10472 } else if (!strcasecmp(c
->argv
[2]->ptr
,"requirepass")) {
10473 zfree(server
.requirepass
);
10474 server
.requirepass
= zstrdup(o
->ptr
);
10475 } else if (!strcasecmp(c
->argv
[2]->ptr
,"masterauth")) {
10476 zfree(server
.masterauth
);
10477 server
.masterauth
= zstrdup(o
->ptr
);
10478 } else if (!strcasecmp(c
->argv
[2]->ptr
,"maxmemory")) {
10479 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
10480 ll
< 0) goto badfmt
;
10481 server
.maxmemory
= ll
;
10482 } else if (!strcasecmp(c
->argv
[2]->ptr
,"timeout")) {
10483 if (getLongLongFromObject(o
,&ll
) == REDIS_ERR
||
10484 ll
< 0 || ll
> LONG_MAX
) goto badfmt
;
10485 server
.maxidletime
= ll
;
10486 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendfsync")) {
10487 if (!strcasecmp(o
->ptr
,"no")) {
10488 server
.appendfsync
= APPENDFSYNC_NO
;
10489 } else if (!strcasecmp(o
->ptr
,"everysec")) {
10490 server
.appendfsync
= APPENDFSYNC_EVERYSEC
;
10491 } else if (!strcasecmp(o
->ptr
,"always")) {
10492 server
.appendfsync
= APPENDFSYNC_ALWAYS
;
10496 } else if (!strcasecmp(c
->argv
[2]->ptr
,"no-appendfsync-on-rewrite")) {
10497 int yn
= yesnotoi(o
->ptr
);
10499 if (yn
== -1) goto badfmt
;
10500 server
.no_appendfsync_on_rewrite
= yn
;
10501 } else if (!strcasecmp(c
->argv
[2]->ptr
,"appendonly")) {
10502 int old
= server
.appendonly
;
10503 int new = yesnotoi(o
->ptr
);
10505 if (new == -1) goto badfmt
;
10510 if (startAppendOnly() == REDIS_ERR
) {
10511 addReplySds(c
,sdscatprintf(sdsempty(),
10512 "-ERR Unable to turn on AOF. Check server logs.\r\n"));
10518 } else if (!strcasecmp(c
->argv
[2]->ptr
,"save")) {
10520 sds
*v
= sdssplitlen(o
->ptr
,sdslen(o
->ptr
)," ",1,&vlen
);
10522 /* Perform sanity check before setting the new config:
10523 * - Even number of args
10524 * - Seconds >= 1, changes >= 0 */
10526 sdsfreesplitres(v
,vlen
);
10529 for (j
= 0; j
< vlen
; j
++) {
10533 val
= strtoll(v
[j
], &eptr
, 10);
10534 if (eptr
[0] != '\0' ||
10535 ((j
& 1) == 0 && val
< 1) ||
10536 ((j
& 1) == 1 && val
< 0)) {
10537 sdsfreesplitres(v
,vlen
);
10541 /* Finally set the new config */
10542 resetServerSaveParams();
10543 for (j
= 0; j
< vlen
; j
+= 2) {
10547 seconds
= strtoll(v
[j
],NULL
,10);
10548 changes
= strtoll(v
[j
+1],NULL
,10);
10549 appendServerSaveParams(seconds
, changes
);
10551 sdsfreesplitres(v
,vlen
);
10553 addReplySds(c
,sdscatprintf(sdsempty(),
10554 "-ERR not supported CONFIG parameter %s\r\n",
10555 (char*)c
->argv
[2]->ptr
));
10560 addReply(c
,shared
.ok
);
10563 badfmt
: /* Bad format errors */
10564 addReplySds(c
,sdscatprintf(sdsempty(),
10565 "-ERR invalid argument '%s' for CONFIG SET '%s'\r\n",
10567 (char*)c
->argv
[2]->ptr
));
10571 static void configGetCommand(redisClient
*c
) {
10572 robj
*o
= getDecodedObject(c
->argv
[2]);
10573 robj
*lenobj
= createObject(REDIS_STRING
,NULL
);
10574 char *pattern
= o
->ptr
;
10577 addReply(c
,lenobj
);
10578 decrRefCount(lenobj
);
10580 if (stringmatch(pattern
,"dbfilename",0)) {
10581 addReplyBulkCString(c
,"dbfilename");
10582 addReplyBulkCString(c
,server
.dbfilename
);
10585 if (stringmatch(pattern
,"requirepass",0)) {
10586 addReplyBulkCString(c
,"requirepass");
10587 addReplyBulkCString(c
,server
.requirepass
);
10590 if (stringmatch(pattern
,"masterauth",0)) {
10591 addReplyBulkCString(c
,"masterauth");
10592 addReplyBulkCString(c
,server
.masterauth
);
10595 if (stringmatch(pattern
,"maxmemory",0)) {
10598 ll2string(buf
,128,server
.maxmemory
);
10599 addReplyBulkCString(c
,"maxmemory");
10600 addReplyBulkCString(c
,buf
);
10603 if (stringmatch(pattern
,"timeout",0)) {
10606 ll2string(buf
,128,server
.maxidletime
);
10607 addReplyBulkCString(c
,"timeout");
10608 addReplyBulkCString(c
,buf
);
10611 if (stringmatch(pattern
,"appendonly",0)) {
10612 addReplyBulkCString(c
,"appendonly");
10613 addReplyBulkCString(c
,server
.appendonly
? "yes" : "no");
10616 if (stringmatch(pattern
,"no-appendfsync-on-rewrite",0)) {
10617 addReplyBulkCString(c
,"no-appendfsync-on-rewrite");
10618 addReplyBulkCString(c
,server
.no_appendfsync_on_rewrite
? "yes" : "no");
10621 if (stringmatch(pattern
,"appendfsync",0)) {
10624 switch(server
.appendfsync
) {
10625 case APPENDFSYNC_NO
: policy
= "no"; break;
10626 case APPENDFSYNC_EVERYSEC
: policy
= "everysec"; break;
10627 case APPENDFSYNC_ALWAYS
: policy
= "always"; break;
10628 default: policy
= "unknown"; break; /* too harmless to panic */
10630 addReplyBulkCString(c
,"appendfsync");
10631 addReplyBulkCString(c
,policy
);
10634 if (stringmatch(pattern
,"save",0)) {
10635 sds buf
= sdsempty();
10638 for (j
= 0; j
< server
.saveparamslen
; j
++) {
10639 buf
= sdscatprintf(buf
,"%ld %d",
10640 server
.saveparams
[j
].seconds
,
10641 server
.saveparams
[j
].changes
);
10642 if (j
!= server
.saveparamslen
-1)
10643 buf
= sdscatlen(buf
," ",1);
10645 addReplyBulkCString(c
,"save");
10646 addReplyBulkCString(c
,buf
);
10651 lenobj
->ptr
= sdscatprintf(sdsempty(),"*%d\r\n",matches
*2);
10654 static void configCommand(redisClient
*c
) {
10655 if (!strcasecmp(c
->argv
[1]->ptr
,"set")) {
10656 if (c
->argc
!= 4) goto badarity
;
10657 configSetCommand(c
);
10658 } else if (!strcasecmp(c
->argv
[1]->ptr
,"get")) {
10659 if (c
->argc
!= 3) goto badarity
;
10660 configGetCommand(c
);
10661 } else if (!strcasecmp(c
->argv
[1]->ptr
,"resetstat")) {
10662 if (c
->argc
!= 2) goto badarity
;
10663 server
.stat_numcommands
= 0;
10664 server
.stat_numconnections
= 0;
10665 server
.stat_expiredkeys
= 0;
10666 server
.stat_starttime
= time(NULL
);
10667 addReply(c
,shared
.ok
);
10669 addReplySds(c
,sdscatprintf(sdsempty(),
10670 "-ERR CONFIG subcommand must be one of GET, SET, RESETSTAT\r\n"));
10675 addReplySds(c
,sdscatprintf(sdsempty(),
10676 "-ERR Wrong number of arguments for CONFIG %s\r\n",
10677 (char*) c
->argv
[1]->ptr
));
10680 /* =========================== Pubsub implementation ======================== */
10682 static void freePubsubPattern(void *p
) {
10683 pubsubPattern
*pat
= p
;
10685 decrRefCount(pat
->pattern
);
10689 static int listMatchPubsubPattern(void *a
, void *b
) {
10690 pubsubPattern
*pa
= a
, *pb
= b
;
10692 return (pa
->client
== pb
->client
) &&
10693 (equalStringObjects(pa
->pattern
,pb
->pattern
));
10696 /* Subscribe a client to a channel. Returns 1 if the operation succeeded, or
10697 * 0 if the client was already subscribed to that channel. */
10698 static int pubsubSubscribeChannel(redisClient
*c
, robj
*channel
) {
10699 struct dictEntry
*de
;
10700 list
*clients
= NULL
;
10703 /* Add the channel to the client -> channels hash table */
10704 if (dictAdd(c
->pubsub_channels
,channel
,NULL
) == DICT_OK
) {
10706 incrRefCount(channel
);
10707 /* Add the client to the channel -> list of clients hash table */
10708 de
= dictFind(server
.pubsub_channels
,channel
);
10710 clients
= listCreate();
10711 dictAdd(server
.pubsub_channels
,channel
,clients
);
10712 incrRefCount(channel
);
10714 clients
= dictGetEntryVal(de
);
10716 listAddNodeTail(clients
,c
);
10718 /* Notify the client */
10719 addReply(c
,shared
.mbulk3
);
10720 addReply(c
,shared
.subscribebulk
);
10721 addReplyBulk(c
,channel
);
10722 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+listLength(c
->pubsub_patterns
));
10726 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10727 * 0 if the client was not subscribed to the specified channel. */
10728 static int pubsubUnsubscribeChannel(redisClient
*c
, robj
*channel
, int notify
) {
10729 struct dictEntry
*de
;
10734 /* Remove the channel from the client -> channels hash table */
10735 incrRefCount(channel
); /* channel may be just a pointer to the same object
10736 we have in the hash tables. Protect it... */
10737 if (dictDelete(c
->pubsub_channels
,channel
) == DICT_OK
) {
10739 /* Remove the client from the channel -> clients list hash table */
10740 de
= dictFind(server
.pubsub_channels
,channel
);
10741 assert(de
!= NULL
);
10742 clients
= dictGetEntryVal(de
);
10743 ln
= listSearchKey(clients
,c
);
10744 assert(ln
!= NULL
);
10745 listDelNode(clients
,ln
);
10746 if (listLength(clients
) == 0) {
10747 /* Free the list and associated hash entry at all if this was
10748 * the latest client, so that it will be possible to abuse
10749 * Redis PUBSUB creating millions of channels. */
10750 dictDelete(server
.pubsub_channels
,channel
);
10753 /* Notify the client */
10755 addReply(c
,shared
.mbulk3
);
10756 addReply(c
,shared
.unsubscribebulk
);
10757 addReplyBulk(c
,channel
);
10758 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+
10759 listLength(c
->pubsub_patterns
));
10762 decrRefCount(channel
); /* it is finally safe to release it */
10766 /* Subscribe a client to a pattern. Returns 1 if the operation succeeded, or 0 if the clinet was already subscribed to that pattern. */
10767 static int pubsubSubscribePattern(redisClient
*c
, robj
*pattern
) {
10770 if (listSearchKey(c
->pubsub_patterns
,pattern
) == NULL
) {
10772 pubsubPattern
*pat
;
10773 listAddNodeTail(c
->pubsub_patterns
,pattern
);
10774 incrRefCount(pattern
);
10775 pat
= zmalloc(sizeof(*pat
));
10776 pat
->pattern
= getDecodedObject(pattern
);
10778 listAddNodeTail(server
.pubsub_patterns
,pat
);
10780 /* Notify the client */
10781 addReply(c
,shared
.mbulk3
);
10782 addReply(c
,shared
.psubscribebulk
);
10783 addReplyBulk(c
,pattern
);
10784 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+listLength(c
->pubsub_patterns
));
10788 /* Unsubscribe a client from a channel. Returns 1 if the operation succeeded, or
10789 * 0 if the client was not subscribed to the specified channel. */
10790 static int pubsubUnsubscribePattern(redisClient
*c
, robj
*pattern
, int notify
) {
10795 incrRefCount(pattern
); /* Protect the object. May be the same we remove */
10796 if ((ln
= listSearchKey(c
->pubsub_patterns
,pattern
)) != NULL
) {
10798 listDelNode(c
->pubsub_patterns
,ln
);
10800 pat
.pattern
= pattern
;
10801 ln
= listSearchKey(server
.pubsub_patterns
,&pat
);
10802 listDelNode(server
.pubsub_patterns
,ln
);
10804 /* Notify the client */
10806 addReply(c
,shared
.mbulk3
);
10807 addReply(c
,shared
.punsubscribebulk
);
10808 addReplyBulk(c
,pattern
);
10809 addReplyLongLong(c
,dictSize(c
->pubsub_channels
)+
10810 listLength(c
->pubsub_patterns
));
10812 decrRefCount(pattern
);
10816 /* Unsubscribe from all the channels. Return the number of channels the
10817 * client was subscribed from. */
10818 static int pubsubUnsubscribeAllChannels(redisClient
*c
, int notify
) {
10819 dictIterator
*di
= dictGetIterator(c
->pubsub_channels
);
10823 while((de
= dictNext(di
)) != NULL
) {
10824 robj
*channel
= dictGetEntryKey(de
);
10826 count
+= pubsubUnsubscribeChannel(c
,channel
,notify
);
10828 dictReleaseIterator(di
);
10832 /* Unsubscribe from all the patterns. Return the number of patterns the
10833 * client was subscribed from. */
10834 static int pubsubUnsubscribeAllPatterns(redisClient
*c
, int notify
) {
10839 listRewind(c
->pubsub_patterns
,&li
);
10840 while ((ln
= listNext(&li
)) != NULL
) {
10841 robj
*pattern
= ln
->value
;
10843 count
+= pubsubUnsubscribePattern(c
,pattern
,notify
);
10848 /* Publish a message */
10849 static int pubsubPublishMessage(robj
*channel
, robj
*message
) {
10851 struct dictEntry
*de
;
10855 /* Send to clients listening for that channel */
10856 de
= dictFind(server
.pubsub_channels
,channel
);
10858 list
*list
= dictGetEntryVal(de
);
10862 listRewind(list
,&li
);
10863 while ((ln
= listNext(&li
)) != NULL
) {
10864 redisClient
*c
= ln
->value
;
10866 addReply(c
,shared
.mbulk3
);
10867 addReply(c
,shared
.messagebulk
);
10868 addReplyBulk(c
,channel
);
10869 addReplyBulk(c
,message
);
10873 /* Send to clients listening to matching channels */
10874 if (listLength(server
.pubsub_patterns
)) {
10875 listRewind(server
.pubsub_patterns
,&li
);
10876 channel
= getDecodedObject(channel
);
10877 while ((ln
= listNext(&li
)) != NULL
) {
10878 pubsubPattern
*pat
= ln
->value
;
10880 if (stringmatchlen((char*)pat
->pattern
->ptr
,
10881 sdslen(pat
->pattern
->ptr
),
10882 (char*)channel
->ptr
,
10883 sdslen(channel
->ptr
),0)) {
10884 addReply(pat
->client
,shared
.mbulk4
);
10885 addReply(pat
->client
,shared
.pmessagebulk
);
10886 addReplyBulk(pat
->client
,pat
->pattern
);
10887 addReplyBulk(pat
->client
,channel
);
10888 addReplyBulk(pat
->client
,message
);
10892 decrRefCount(channel
);
10897 static void subscribeCommand(redisClient
*c
) {
10900 for (j
= 1; j
< c
->argc
; j
++)
10901 pubsubSubscribeChannel(c
,c
->argv
[j
]);
10904 static void unsubscribeCommand(redisClient
*c
) {
10905 if (c
->argc
== 1) {
10906 pubsubUnsubscribeAllChannels(c
,1);
10911 for (j
= 1; j
< c
->argc
; j
++)
10912 pubsubUnsubscribeChannel(c
,c
->argv
[j
],1);
10916 static void psubscribeCommand(redisClient
*c
) {
10919 for (j
= 1; j
< c
->argc
; j
++)
10920 pubsubSubscribePattern(c
,c
->argv
[j
]);
10923 static void punsubscribeCommand(redisClient
*c
) {
10924 if (c
->argc
== 1) {
10925 pubsubUnsubscribeAllPatterns(c
,1);
10930 for (j
= 1; j
< c
->argc
; j
++)
10931 pubsubUnsubscribePattern(c
,c
->argv
[j
],1);
10935 static void publishCommand(redisClient
*c
) {
10936 int receivers
= pubsubPublishMessage(c
->argv
[1],c
->argv
[2]);
10937 addReplyLongLong(c
,receivers
);
10940 /* ===================== WATCH (CAS alike for MULTI/EXEC) ===================
10942 * The implementation uses a per-DB hash table mapping keys to list of clients
10943 * WATCHing those keys, so that given a key that is going to be modified
10944 * we can mark all the associated clients as dirty.
10946 * Also every client contains a list of WATCHed keys so that's possible to
10947 * un-watch such keys when the client is freed or when UNWATCH is called. */
10949 /* In the client->watched_keys list we need to use watchedKey structures
10950 * as in order to identify a key in Redis we need both the key name and the
10952 typedef struct watchedKey
{
10957 /* Watch for the specified key */
10958 static void watchForKey(redisClient
*c
, robj
*key
) {
10959 list
*clients
= NULL
;
10964 /* Check if we are already watching for this key */
10965 listRewind(c
->watched_keys
,&li
);
10966 while((ln
= listNext(&li
))) {
10967 wk
= listNodeValue(ln
);
10968 if (wk
->db
== c
->db
&& equalStringObjects(key
,wk
->key
))
10969 return; /* Key already watched */
10971 /* This key is not already watched in this DB. Let's add it */
10972 clients
= dictFetchValue(c
->db
->watched_keys
,key
);
10974 clients
= listCreate();
10975 dictAdd(c
->db
->watched_keys
,key
,clients
);
10978 listAddNodeTail(clients
,c
);
10979 /* Add the new key to the lits of keys watched by this client */
10980 wk
= zmalloc(sizeof(*wk
));
10984 listAddNodeTail(c
->watched_keys
,wk
);
10987 /* Unwatch all the keys watched by this client. To clean the EXEC dirty
10988 * flag is up to the caller. */
10989 static void unwatchAllKeys(redisClient
*c
) {
10993 if (listLength(c
->watched_keys
) == 0) return;
10994 listRewind(c
->watched_keys
,&li
);
10995 while((ln
= listNext(&li
))) {
10999 /* Lookup the watched key -> clients list and remove the client
11001 wk
= listNodeValue(ln
);
11002 clients
= dictFetchValue(wk
->db
->watched_keys
, wk
->key
);
11003 assert(clients
!= NULL
);
11004 listDelNode(clients
,listSearchKey(clients
,c
));
11005 /* Kill the entry at all if this was the only client */
11006 if (listLength(clients
) == 0)
11007 dictDelete(wk
->db
->watched_keys
, wk
->key
);
11008 /* Remove this watched key from the client->watched list */
11009 listDelNode(c
->watched_keys
,ln
);
11010 decrRefCount(wk
->key
);
11015 /* "Touch" a key, so that if this key is being WATCHed by some client the
11016 * next EXEC will fail. */
11017 static void touchWatchedKey(redisDb
*db
, robj
*key
) {
11022 if (dictSize(db
->watched_keys
) == 0) return;
11023 clients
= dictFetchValue(db
->watched_keys
, key
);
11024 if (!clients
) return;
11026 /* Mark all the clients watching this key as REDIS_DIRTY_CAS */
11027 /* Check if we are already watching for this key */
11028 listRewind(clients
,&li
);
11029 while((ln
= listNext(&li
))) {
11030 redisClient
*c
= listNodeValue(ln
);
11032 c
->flags
|= REDIS_DIRTY_CAS
;
11036 /* On FLUSHDB or FLUSHALL all the watched keys that are present before the
11037 * flush but will be deleted as effect of the flushing operation should
11038 * be touched. "dbid" is the DB that's getting the flush. -1 if it is
11039 * a FLUSHALL operation (all the DBs flushed). */
11040 static void touchWatchedKeysOnFlush(int dbid
) {
11044 /* For every client, check all the waited keys */
11045 listRewind(server
.clients
,&li1
);
11046 while((ln
= listNext(&li1
))) {
11047 redisClient
*c
= listNodeValue(ln
);
11048 listRewind(c
->watched_keys
,&li2
);
11049 while((ln
= listNext(&li2
))) {
11050 watchedKey
*wk
= listNodeValue(ln
);
11052 /* For every watched key matching the specified DB, if the
11053 * key exists, mark the client as dirty, as the key will be
11055 if (dbid
== -1 || wk
->db
->id
== dbid
) {
11056 if (dictFind(wk
->db
->dict
, wk
->key
->ptr
) != NULL
)
11057 c
->flags
|= REDIS_DIRTY_CAS
;
11063 static void watchCommand(redisClient
*c
) {
11066 if (c
->flags
& REDIS_MULTI
) {
11067 addReplySds(c
,sdsnew("-ERR WATCH inside MULTI is not allowed\r\n"));
11070 for (j
= 1; j
< c
->argc
; j
++)
11071 watchForKey(c
,c
->argv
[j
]);
11072 addReply(c
,shared
.ok
);
11075 static void unwatchCommand(redisClient
*c
) {
11077 c
->flags
&= (~REDIS_DIRTY_CAS
);
11078 addReply(c
,shared
.ok
);
11081 /* ================================= Debugging ============================== */
11083 /* Compute the sha1 of string at 's' with 'len' bytes long.
11084 * The SHA1 is then xored againt the string pointed by digest.
11085 * Since xor is commutative, this operation is used in order to
11086 * "add" digests relative to unordered elements.
11088 * So digest(a,b,c,d) will be the same of digest(b,a,c,d) */
11089 static void xorDigest(unsigned char *digest
, void *ptr
, size_t len
) {
11091 unsigned char hash
[20], *s
= ptr
;
11095 SHA1Update(&ctx
,s
,len
);
11096 SHA1Final(hash
,&ctx
);
11098 for (j
= 0; j
< 20; j
++)
11099 digest
[j
] ^= hash
[j
];
11102 static void xorObjectDigest(unsigned char *digest
, robj
*o
) {
11103 o
= getDecodedObject(o
);
11104 xorDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
11108 /* This function instead of just computing the SHA1 and xoring it
11109 * against diget, also perform the digest of "digest" itself and
11110 * replace the old value with the new one.
11112 * So the final digest will be:
11114 * digest = SHA1(digest xor SHA1(data))
11116 * This function is used every time we want to preserve the order so
11117 * that digest(a,b,c,d) will be different than digest(b,c,d,a)
11119 * Also note that mixdigest("foo") followed by mixdigest("bar")
11120 * will lead to a different digest compared to "fo", "obar".
11122 static void mixDigest(unsigned char *digest
, void *ptr
, size_t len
) {
11126 xorDigest(digest
,s
,len
);
11128 SHA1Update(&ctx
,digest
,20);
11129 SHA1Final(digest
,&ctx
);
11132 static void mixObjectDigest(unsigned char *digest
, robj
*o
) {
11133 o
= getDecodedObject(o
);
11134 mixDigest(digest
,o
->ptr
,sdslen(o
->ptr
));
11138 /* Compute the dataset digest. Since keys, sets elements, hashes elements
11139 * are not ordered, we use a trick: every aggregate digest is the xor
11140 * of the digests of their elements. This way the order will not change
11141 * the result. For list instead we use a feedback entering the output digest
11142 * as input in order to ensure that a different ordered list will result in
11143 * a different digest. */
11144 static void computeDatasetDigest(unsigned char *final
) {
11145 unsigned char digest
[20];
11147 dictIterator
*di
= NULL
;
11152 memset(final
,0,20); /* Start with a clean result */
11154 for (j
= 0; j
< server
.dbnum
; j
++) {
11155 redisDb
*db
= server
.db
+j
;
11157 if (dictSize(db
->dict
) == 0) continue;
11158 di
= dictGetIterator(db
->dict
);
11160 /* hash the DB id, so the same dataset moved in a different
11161 * DB will lead to a different digest */
11163 mixDigest(final
,&aux
,sizeof(aux
));
11165 /* Iterate this DB writing every entry */
11166 while((de
= dictNext(di
)) != NULL
) {
11171 memset(digest
,0,20); /* This key-val digest */
11172 key
= dictGetEntryKey(de
);
11173 keyobj
= createStringObject(key
,sdslen(key
));
11175 mixDigest(digest
,key
,sdslen(key
));
11177 /* Make sure the key is loaded if VM is active */
11178 o
= lookupKeyRead(db
,keyobj
);
11180 aux
= htonl(o
->type
);
11181 mixDigest(digest
,&aux
,sizeof(aux
));
11182 expiretime
= getExpire(db
,keyobj
);
11184 /* Save the key and associated value */
11185 if (o
->type
== REDIS_STRING
) {
11186 mixObjectDigest(digest
,o
);
11187 } else if (o
->type
== REDIS_LIST
) {
11188 listTypeIterator
*li
= listTypeInitIterator(o
,0,REDIS_TAIL
);
11189 listTypeEntry entry
;
11190 while(listTypeNext(li
,&entry
)) {
11191 robj
*eleobj
= listTypeGet(&entry
);
11192 mixObjectDigest(digest
,eleobj
);
11193 decrRefCount(eleobj
);
11195 listTypeReleaseIterator(li
);
11196 } else if (o
->type
== REDIS_SET
) {
11197 dict
*set
= o
->ptr
;
11198 dictIterator
*di
= dictGetIterator(set
);
11201 while((de
= dictNext(di
)) != NULL
) {
11202 robj
*eleobj
= dictGetEntryKey(de
);
11204 xorObjectDigest(digest
,eleobj
);
11206 dictReleaseIterator(di
);
11207 } else if (o
->type
== REDIS_ZSET
) {
11209 dictIterator
*di
= dictGetIterator(zs
->dict
);
11212 while((de
= dictNext(di
)) != NULL
) {
11213 robj
*eleobj
= dictGetEntryKey(de
);
11214 double *score
= dictGetEntryVal(de
);
11215 unsigned char eledigest
[20];
11217 snprintf(buf
,sizeof(buf
),"%.17g",*score
);
11218 memset(eledigest
,0,20);
11219 mixObjectDigest(eledigest
,eleobj
);
11220 mixDigest(eledigest
,buf
,strlen(buf
));
11221 xorDigest(digest
,eledigest
,20);
11223 dictReleaseIterator(di
);
11224 } else if (o
->type
== REDIS_HASH
) {
11225 hashTypeIterator
*hi
;
11228 hi
= hashTypeInitIterator(o
);
11229 while (hashTypeNext(hi
) != REDIS_ERR
) {
11230 unsigned char eledigest
[20];
11232 memset(eledigest
,0,20);
11233 obj
= hashTypeCurrent(hi
,REDIS_HASH_KEY
);
11234 mixObjectDigest(eledigest
,obj
);
11236 obj
= hashTypeCurrent(hi
,REDIS_HASH_VALUE
);
11237 mixObjectDigest(eledigest
,obj
);
11239 xorDigest(digest
,eledigest
,20);
11241 hashTypeReleaseIterator(hi
);
11243 redisPanic("Unknown object type");
11245 /* If the key has an expire, add it to the mix */
11246 if (expiretime
!= -1) xorDigest(digest
,"!!expire!!",10);
11247 /* We can finally xor the key-val digest to the final digest */
11248 xorDigest(final
,digest
,20);
11249 decrRefCount(keyobj
);
11251 dictReleaseIterator(di
);
11255 static void debugCommand(redisClient
*c
) {
11256 if (!strcasecmp(c
->argv
[1]->ptr
,"segfault")) {
11257 *((char*)-1) = 'x';
11258 } else if (!strcasecmp(c
->argv
[1]->ptr
,"reload")) {
11259 if (rdbSave(server
.dbfilename
) != REDIS_OK
) {
11260 addReply(c
,shared
.err
);
11264 if (rdbLoad(server
.dbfilename
) != REDIS_OK
) {
11265 addReply(c
,shared
.err
);
11268 redisLog(REDIS_WARNING
,"DB reloaded by DEBUG RELOAD");
11269 addReply(c
,shared
.ok
);
11270 } else if (!strcasecmp(c
->argv
[1]->ptr
,"loadaof")) {
11272 if (loadAppendOnlyFile(server
.appendfilename
) != REDIS_OK
) {
11273 addReply(c
,shared
.err
);
11276 redisLog(REDIS_WARNING
,"Append Only File loaded by DEBUG LOADAOF");
11277 addReply(c
,shared
.ok
);
11278 } else if (!strcasecmp(c
->argv
[1]->ptr
,"object") && c
->argc
== 3) {
11279 dictEntry
*de
= dictFind(c
->db
->dict
,c
->argv
[2]->ptr
);
11283 addReply(c
,shared
.nokeyerr
);
11286 val
= dictGetEntryVal(de
);
11287 if (!server
.vm_enabled
|| (val
->storage
== REDIS_VM_MEMORY
||
11288 val
->storage
== REDIS_VM_SWAPPING
)) {
11292 if (val
->encoding
< (sizeof(strencoding
)/sizeof(char*))) {
11293 strenc
= strencoding
[val
->encoding
];
11295 snprintf(buf
,64,"unknown encoding %d\n", val
->encoding
);
11298 addReplySds(c
,sdscatprintf(sdsempty(),
11299 "+Value at:%p refcount:%d "
11300 "encoding:%s serializedlength:%lld\r\n",
11301 (void*)val
, val
->refcount
,
11302 strenc
, (long long) rdbSavedObjectLen(val
,NULL
)));
11304 vmpointer
*vp
= (vmpointer
*) val
;
11305 addReplySds(c
,sdscatprintf(sdsempty(),
11306 "+Value swapped at: page %llu "
11307 "using %llu pages\r\n",
11308 (unsigned long long) vp
->page
,
11309 (unsigned long long) vp
->usedpages
));
11311 } else if (!strcasecmp(c
->argv
[1]->ptr
,"swapin") && c
->argc
== 3) {
11312 lookupKeyRead(c
->db
,c
->argv
[2]);
11313 addReply(c
,shared
.ok
);
11314 } else if (!strcasecmp(c
->argv
[1]->ptr
,"swapout") && c
->argc
== 3) {
11315 dictEntry
*de
= dictFind(c
->db
->dict
,c
->argv
[2]->ptr
);
11319 if (!server
.vm_enabled
) {
11320 addReplySds(c
,sdsnew("-ERR Virtual Memory is disabled\r\n"));
11324 addReply(c
,shared
.nokeyerr
);
11327 val
= dictGetEntryVal(de
);
11329 if (val
->storage
!= REDIS_VM_MEMORY
) {
11330 addReplySds(c
,sdsnew("-ERR This key is not in memory\r\n"));
11331 } else if (val
->refcount
!= 1) {
11332 addReplySds(c
,sdsnew("-ERR Object is shared\r\n"));
11333 } else if ((vp
= vmSwapObjectBlocking(val
)) != NULL
) {
11334 dictGetEntryVal(de
) = vp
;
11335 addReply(c
,shared
.ok
);
11337 addReply(c
,shared
.err
);
11339 } else if (!strcasecmp(c
->argv
[1]->ptr
,"populate") && c
->argc
== 3) {
11344 if (getLongFromObjectOrReply(c
, c
->argv
[2], &keys
, NULL
) != REDIS_OK
)
11346 for (j
= 0; j
< keys
; j
++) {
11347 snprintf(buf
,sizeof(buf
),"key:%lu",j
);
11348 key
= createStringObject(buf
,strlen(buf
));
11349 if (lookupKeyRead(c
->db
,key
) != NULL
) {
11353 snprintf(buf
,sizeof(buf
),"value:%lu",j
);
11354 val
= createStringObject(buf
,strlen(buf
));
11355 dbAdd(c
->db
,key
,val
);
11358 addReply(c
,shared
.ok
);
11359 } else if (!strcasecmp(c
->argv
[1]->ptr
,"digest") && c
->argc
== 2) {
11360 unsigned char digest
[20];
11361 sds d
= sdsnew("+");
11364 computeDatasetDigest(digest
);
11365 for (j
= 0; j
< 20; j
++)
11366 d
= sdscatprintf(d
, "%02x",digest
[j
]);
11368 d
= sdscatlen(d
,"\r\n",2);
11371 addReplySds(c
,sdsnew(
11372 "-ERR Syntax error, try DEBUG [SEGFAULT|OBJECT <key>|SWAPIN <key>|SWAPOUT <key>|RELOAD]\r\n"));
11376 static void _redisAssert(char *estr
, char *file
, int line
) {
11377 redisLog(REDIS_WARNING
,"=== ASSERTION FAILED ===");
11378 redisLog(REDIS_WARNING
,"==> %s:%d '%s' is not true",file
,line
,estr
);
11379 #ifdef HAVE_BACKTRACE
11380 redisLog(REDIS_WARNING
,"(forcing SIGSEGV in order to print the stack trace)");
11381 *((char*)-1) = 'x';
11385 static void _redisPanic(char *msg
, char *file
, int line
) {
11386 redisLog(REDIS_WARNING
,"!!! Software Failure. Press left mouse button to continue");
11387 redisLog(REDIS_WARNING
,"Guru Meditation: %s #%s:%d",msg
,file
,line
);
11388 #ifdef HAVE_BACKTRACE
11389 redisLog(REDIS_WARNING
,"(forcing SIGSEGV in order to print the stack trace)");
11390 *((char*)-1) = 'x';
11394 /* =================================== Main! ================================ */
11397 int linuxOvercommitMemoryValue(void) {
11398 FILE *fp
= fopen("/proc/sys/vm/overcommit_memory","r");
11401 if (!fp
) return -1;
11402 if (fgets(buf
,64,fp
) == NULL
) {
11411 void linuxOvercommitMemoryWarning(void) {
11412 if (linuxOvercommitMemoryValue() == 0) {
11413 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.");
11416 #endif /* __linux__ */
11418 static void daemonize(void) {
11422 if (fork() != 0) exit(0); /* parent exits */
11423 setsid(); /* create a new session */
11425 /* Every output goes to /dev/null. If Redis is daemonized but
11426 * the 'logfile' is set to 'stdout' in the configuration file
11427 * it will not log at all. */
11428 if ((fd
= open("/dev/null", O_RDWR
, 0)) != -1) {
11429 dup2(fd
, STDIN_FILENO
);
11430 dup2(fd
, STDOUT_FILENO
);
11431 dup2(fd
, STDERR_FILENO
);
11432 if (fd
> STDERR_FILENO
) close(fd
);
11434 /* Try to write the pid file */
11435 fp
= fopen(server
.pidfile
,"w");
11437 fprintf(fp
,"%d\n",getpid());
11442 static void version() {
11443 printf("Redis server version %s (%s:%d)\n", REDIS_VERSION
,
11444 redisGitSHA1(), atoi(redisGitDirty()) > 0);
11448 static void usage() {
11449 fprintf(stderr
,"Usage: ./redis-server [/path/to/redis.conf]\n");
11450 fprintf(stderr
," ./redis-server - (read config from stdin)\n");
11454 int main(int argc
, char **argv
) {
11457 initServerConfig();
11458 sortCommandTable();
11460 if (strcmp(argv
[1], "-v") == 0 ||
11461 strcmp(argv
[1], "--version") == 0) version();
11462 if (strcmp(argv
[1], "--help") == 0) usage();
11463 resetServerSaveParams();
11464 loadServerConfig(argv
[1]);
11465 } else if ((argc
> 2)) {
11468 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'");
11470 if (server
.daemonize
) daemonize();
11472 redisLog(REDIS_NOTICE
,"Server started, Redis version " REDIS_VERSION
);
11474 linuxOvercommitMemoryWarning();
11476 start
= time(NULL
);
11477 if (server
.appendonly
) {
11478 if (loadAppendOnlyFile(server
.appendfilename
) == REDIS_OK
)
11479 redisLog(REDIS_NOTICE
,"DB loaded from append only file: %ld seconds",time(NULL
)-start
);
11481 if (rdbLoad(server
.dbfilename
) == REDIS_OK
)
11482 redisLog(REDIS_NOTICE
,"DB loaded from disk: %ld seconds",time(NULL
)-start
);
11484 redisLog(REDIS_NOTICE
,"The server is now ready to accept connections on port %d", server
.port
);
11485 aeSetBeforeSleepProc(server
.el
,beforeSleep
);
11487 aeDeleteEventLoop(server
.el
);
11491 /* ============================= Backtrace support ========================= */
11493 #ifdef HAVE_BACKTRACE
11494 static char *findFuncName(void *pointer
, unsigned long *offset
);
11496 static void *getMcontextEip(ucontext_t
*uc
) {
11497 #if defined(__FreeBSD__)
11498 return (void*) uc
->uc_mcontext
.mc_eip
;
11499 #elif defined(__dietlibc__)
11500 return (void*) uc
->uc_mcontext
.eip
;
11501 #elif defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
11503 return (void*) uc
->uc_mcontext
->__ss
.__rip
;
11505 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
11507 #elif defined(__APPLE__) && defined(MAC_OS_X_VERSION_10_6)
11508 #if defined(_STRUCT_X86_THREAD_STATE64) && !defined(__i386__)
11509 return (void*) uc
->uc_mcontext
->__ss
.__rip
;
11511 return (void*) uc
->uc_mcontext
->__ss
.__eip
;
11513 #elif defined(__i386__) || defined(__X86_64__) || defined(__x86_64__)
11514 return (void*) uc
->uc_mcontext
.gregs
[REG_EIP
]; /* Linux 32/64 bit */
11515 #elif defined(__ia64__) /* Linux IA64 */
11516 return (void*) uc
->uc_mcontext
.sc_ip
;
11522 static void segvHandler(int sig
, siginfo_t
*info
, void *secret
) {
11524 char **messages
= NULL
;
11525 int i
, trace_size
= 0;
11526 unsigned long offset
=0;
11527 ucontext_t
*uc
= (ucontext_t
*) secret
;
11529 REDIS_NOTUSED(info
);
11531 redisLog(REDIS_WARNING
,
11532 "======= Ooops! Redis %s got signal: -%d- =======", REDIS_VERSION
, sig
);
11533 infostring
= genRedisInfoString();
11534 redisLog(REDIS_WARNING
, "%s",infostring
);
11535 /* It's not safe to sdsfree() the returned string under memory
11536 * corruption conditions. Let it leak as we are going to abort */
11538 trace_size
= backtrace(trace
, 100);
11539 /* overwrite sigaction with caller's address */
11540 if (getMcontextEip(uc
) != NULL
) {
11541 trace
[1] = getMcontextEip(uc
);
11543 messages
= backtrace_symbols(trace
, trace_size
);
11545 for (i
=1; i
<trace_size
; ++i
) {
11546 char *fn
= findFuncName(trace
[i
], &offset
), *p
;
11548 p
= strchr(messages
[i
],'+');
11549 if (!fn
|| (p
&& ((unsigned long)strtol(p
+1,NULL
,10)) < offset
)) {
11550 redisLog(REDIS_WARNING
,"%s", messages
[i
]);
11552 redisLog(REDIS_WARNING
,"%d redis-server %p %s + %d", i
, trace
[i
], fn
, (unsigned int)offset
);
11555 /* free(messages); Don't call free() with possibly corrupted memory. */
11559 static void sigtermHandler(int sig
) {
11560 REDIS_NOTUSED(sig
);
11562 redisLog(REDIS_WARNING
,"SIGTERM received, scheduling shutting down...");
11563 server
.shutdown_asap
= 1;
11566 static void setupSigSegvAction(void) {
11567 struct sigaction act
;
11569 sigemptyset (&act
.sa_mask
);
11570 /* When the SA_SIGINFO flag is set in sa_flags then sa_sigaction
11571 * is used. Otherwise, sa_handler is used */
11572 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
| SA_SIGINFO
;
11573 act
.sa_sigaction
= segvHandler
;
11574 sigaction (SIGSEGV
, &act
, NULL
);
11575 sigaction (SIGBUS
, &act
, NULL
);
11576 sigaction (SIGFPE
, &act
, NULL
);
11577 sigaction (SIGILL
, &act
, NULL
);
11578 sigaction (SIGBUS
, &act
, NULL
);
11580 act
.sa_flags
= SA_NODEFER
| SA_ONSTACK
| SA_RESETHAND
;
11581 act
.sa_handler
= sigtermHandler
;
11582 sigaction (SIGTERM
, &act
, NULL
);
11586 #include "staticsymbols.h"
11587 /* This function try to convert a pointer into a function name. It's used in
11588 * oreder to provide a backtrace under segmentation fault that's able to
11589 * display functions declared as static (otherwise the backtrace is useless). */
11590 static char *findFuncName(void *pointer
, unsigned long *offset
){
11592 unsigned long off
, minoff
= 0;
11594 /* Try to match against the Symbol with the smallest offset */
11595 for (i
=0; symsTable
[i
].pointer
; i
++) {
11596 unsigned long lp
= (unsigned long) pointer
;
11598 if (lp
!= (unsigned long)-1 && lp
>= symsTable
[i
].pointer
) {
11599 off
=lp
-symsTable
[i
].pointer
;
11600 if (ret
< 0 || off
< minoff
) {
11606 if (ret
== -1) return NULL
;
11608 return symsTable
[ret
].name
;
11610 #else /* HAVE_BACKTRACE */
11611 static void setupSigSegvAction(void) {
11613 #endif /* HAVE_BACKTRACE */