8 #include "solarisfixes.h"
21 #include <netinet/in.h>
24 #include "ae.h" /* Event driven programming library */
25 #include "sds.h" /* Dynamic safe strings */
26 #include "dict.h" /* Hash tables */
27 #include "adlist.h" /* Linked lists */
28 #include "zmalloc.h" /* total memory usage aware version of malloc/free */
29 #include "anet.h" /* Networking the easy way */
30 #include "zipmap.h" /* Compact string -> string data structure */
31 #include "ziplist.h" /* Compact list data structure */
32 #include "intset.h" /* Compact integer set structure */
33 #include "version.h" /* Version macro */
34 #include "util.h" /* Misc functions useful in many places */
40 /* Static server configuration */
41 #define REDIS_SERVERPORT 6379 /* TCP port */
42 #define REDIS_MAXIDLETIME (60*5) /* default client timeout */
43 #define REDIS_IOBUF_LEN 1024
44 #define REDIS_LOADBUF_LEN 1024
45 #define REDIS_DEFAULT_DBNUM 16
46 #define REDIS_CONFIGLINE_MAX 1024
47 #define REDIS_MAX_SYNC_TIME 60 /* Slave can't take more to sync */
48 #define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */
49 #define REDIS_MAX_WRITE_PER_EVENT (1024*64)
50 #define REDIS_REQUEST_MAX_SIZE (1024*1024*256) /* max bytes in inline command */
51 #define REDIS_SHARED_INTEGERS 10000
52 #define REDIS_REPLY_CHUNK_BYTES (5*1500) /* 5 TCP packets with default MTU */
53 #define REDIS_MAX_LOGMSG_LEN 1024 /* Default maximum length of syslog messages */
54 #define REDIS_AUTO_AOFREWRITE_PERC 100
55 #define REDIS_AUTO_AOFREWRITE_MIN_SIZE (1024*1024)
56 #define REDIS_SLOWLOG_LOG_SLOWER_THAN 10000
57 #define REDIS_SLOWLOG_MAX_LEN 64
59 /* Hash table parameters */
60 #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
62 /* Command flags. Please check the command table defined in the redis.c file
63 * for more information about the meaning of every flag. */
64 #define REDIS_CMD_WRITE 1 /* "w" flag */
65 #define REDIS_CMD_READONLY 2 /* "r" flag */
66 #define REDIS_CMD_DENYOOM 4 /* "m" flag */
67 #define REDIS_CMD_FORCE_REPLICATION 8 /* "f" flag */
68 #define REDIS_CMD_ADMIN 16 /* "a" flag */
69 #define REDIS_CMD_PUBSUB 32 /* "p" flag */
70 #define REDIS_CMD_NOSCRIPT 64 /* "s" flag */
71 #define REDIS_CMD_RANDOM 128 /* "R" flag */
74 #define REDIS_STRING 0
79 #define REDIS_VMPOINTER 8
81 /* Objects encoding. Some kind of objects like Strings and Hashes can be
82 * internally represented in multiple ways. The 'encoding' field of the object
83 * is set to one of this fields for this object. */
84 #define REDIS_ENCODING_RAW 0 /* Raw representation */
85 #define REDIS_ENCODING_INT 1 /* Encoded as integer */
86 #define REDIS_ENCODING_HT 2 /* Encoded as hash table */
87 #define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
88 #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */
89 #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
90 #define REDIS_ENCODING_INTSET 6 /* Encoded as intset */
91 #define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
93 /* Object types only used for dumping to disk */
94 #define REDIS_EXPIRETIME 253
95 #define REDIS_SELECTDB 254
98 /* Defines related to the dump file format. To store 32 bits lengths for short
99 * keys requires a lot of space, so we check the most significant 2 bits of
100 * the first byte to interpreter the length:
102 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
103 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
104 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
105 * 11|000000 this means: specially encoded object will follow. The six bits
106 * number specify the kind of object that follows.
107 * See the REDIS_RDB_ENC_* defines.
109 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
110 * values, will fit inside. */
111 #define REDIS_RDB_6BITLEN 0
112 #define REDIS_RDB_14BITLEN 1
113 #define REDIS_RDB_32BITLEN 2
114 #define REDIS_RDB_ENCVAL 3
115 #define REDIS_RDB_LENERR UINT_MAX
117 /* When a length of a string object stored on disk has the first two bits
118 * set, the remaining two bits specify a special encoding for the object
119 * accordingly to the following defines: */
120 #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
121 #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
122 #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
123 #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
126 #define REDIS_SLAVE 1 /* This client is a slave server */
127 #define REDIS_MASTER 2 /* This client is a master server */
128 #define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */
129 #define REDIS_MULTI 8 /* This client is in a MULTI context */
130 #define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */
131 #define REDIS_DIRTY_CAS 64 /* Watched keys modified. EXEC will fail. */
132 #define REDIS_CLOSE_AFTER_REPLY 128 /* Close after writing entire reply. */
133 #define REDIS_UNBLOCKED 256 /* This client was unblocked and is stored in
134 server.unblocked_clients */
135 #define REDIS_LUA_CLIENT 512 /* This is a non connected client used by Lua */
137 /* Client request types */
138 #define REDIS_REQ_INLINE 1
139 #define REDIS_REQ_MULTIBULK 2
141 /* Slave replication state - slave side */
142 #define REDIS_REPL_NONE 0 /* No active replication */
143 #define REDIS_REPL_CONNECT 1 /* Must connect to master */
144 #define REDIS_REPL_CONNECTING 2 /* Connecting to master */
145 #define REDIS_REPL_TRANSFER 3 /* Receiving .rdb from master */
146 #define REDIS_REPL_CONNECTED 4 /* Connected to master */
148 /* Synchronous read timeout - slave side */
149 #define REDIS_REPL_SYNCIO_TIMEOUT 5
151 /* Slave replication state - from the point of view of master
152 * Note that in SEND_BULK and ONLINE state the slave receives new updates
153 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
154 * to start the next background saving in order to send updates to it. */
155 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
156 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
157 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
158 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
160 /* List related stuff */
164 /* Sort operations */
165 #define REDIS_SORT_GET 0
166 #define REDIS_SORT_ASC 1
167 #define REDIS_SORT_DESC 2
168 #define REDIS_SORTKEY_MAX 1024
171 #define REDIS_DEBUG 0
172 #define REDIS_VERBOSE 1
173 #define REDIS_NOTICE 2
174 #define REDIS_WARNING 3
175 #define REDIS_LOG_RAW (1<<10) /* Modifier to log without timestamp */
177 /* Anti-warning macro... */
178 #define REDIS_NOTUSED(V) ((void) V)
180 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
181 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
183 /* Append only defines */
184 #define APPENDFSYNC_NO 0
185 #define APPENDFSYNC_ALWAYS 1
186 #define APPENDFSYNC_EVERYSEC 2
188 /* Zip structure related defaults */
189 #define REDIS_HASH_MAX_ZIPMAP_ENTRIES 512
190 #define REDIS_HASH_MAX_ZIPMAP_VALUE 64
191 #define REDIS_LIST_MAX_ZIPLIST_ENTRIES 512
192 #define REDIS_LIST_MAX_ZIPLIST_VALUE 64
193 #define REDIS_SET_MAX_INTSET_ENTRIES 512
194 #define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128
195 #define REDIS_ZSET_MAX_ZIPLIST_VALUE 64
197 /* Sets operations codes */
198 #define REDIS_OP_UNION 0
199 #define REDIS_OP_DIFF 1
200 #define REDIS_OP_INTER 2
202 /* Redis maxmemory strategies */
203 #define REDIS_MAXMEMORY_VOLATILE_LRU 0
204 #define REDIS_MAXMEMORY_VOLATILE_TTL 1
205 #define REDIS_MAXMEMORY_VOLATILE_RANDOM 2
206 #define REDIS_MAXMEMORY_ALLKEYS_LRU 3
207 #define REDIS_MAXMEMORY_ALLKEYS_RANDOM 4
208 #define REDIS_MAXMEMORY_NO_EVICTION 5
211 #define REDIS_LUA_TIME_LIMIT 60000 /* milliseconds */
213 /* We can print the stacktrace, so our assert is defined this way: */
214 #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
215 #define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
216 void _redisAssert(char *estr
, char *file
, int line
);
217 void _redisPanic(char *msg
, char *file
, int line
);
219 /*-----------------------------------------------------------------------------
221 *----------------------------------------------------------------------------*/
223 /* A redis object, that is a type able to hold a string / list / set */
225 /* The actual Redis Object */
226 #define REDIS_LRU_CLOCK_MAX ((1<<21)-1) /* Max value of obj->lru */
227 #define REDIS_LRU_CLOCK_RESOLUTION 10 /* LRU clock resolution in seconds */
228 typedef struct redisObject
{
230 unsigned notused
:2; /* Not used */
232 unsigned lru
:22; /* lru time (relative to server.lruclock) */
235 /* VM fields are only allocated if VM is active, otherwise the
236 * object allocation function will just allocate
237 * sizeof(redisObjct) minus sizeof(redisObjectVM), so using
238 * Redis without VM active will not have any overhead. */
241 /* The VM pointer structure - identifies an object in the swap file.
243 * This object is stored in place of the value
244 * object in the main key->value hash table representing a database.
245 * Note that the first fields (type, storage) are the same as the redisObject
246 * structure so that vmPointer strucuters can be accessed even when casted
247 * as redisObject structures.
249 * This is useful as we don't know if a value object is or not on disk, but we
250 * are always able to read obj->storage to check this. For vmPointer
251 * structures "type" is set to REDIS_VMPOINTER (even if without this field
252 * is still possible to check the kind of object from the value of 'storage').*/
253 typedef struct vmPointer
{
255 unsigned storage
:2; /* REDIS_VM_SWAPPED or REDIS_VM_LOADING */
257 unsigned int vtype
; /* type of the object stored in the swap file */
258 off_t page
; /* the page at witch the object is stored on disk */
259 off_t usedpages
; /* number of pages used on disk */
262 /* Macro used to initalize a Redis object allocated on the stack.
263 * Note that this macro is taken near the structure definition to make sure
264 * we'll update it when the structure is changed, to avoid bugs like
265 * bug #85 introduced exactly in this way. */
266 #define initStaticStringObject(_var,_ptr) do { \
268 _var.type = REDIS_STRING; \
269 _var.encoding = REDIS_ENCODING_RAW; \
273 typedef struct redisDb
{
274 dict
*dict
; /* The keyspace for this DB */
275 dict
*expires
; /* Timeout of keys with a timeout set */
276 dict
*blocking_keys
; /* Keys with clients waiting for data (BLPOP) */
277 dict
*watched_keys
; /* WATCHED keys for MULTI/EXEC CAS */
281 /* Client MULTI/EXEC state */
282 typedef struct multiCmd
{
285 struct redisCommand
*cmd
;
288 typedef struct multiState
{
289 multiCmd
*commands
; /* Array of MULTI commands */
290 int count
; /* Total number of MULTI commands */
293 typedef struct blockingState
{
294 robj
**keys
; /* The key we are waiting to terminate a blocking
295 * operation such as BLPOP. Otherwise NULL. */
296 int count
; /* Number of blocking keys */
297 time_t timeout
; /* Blocking operation timeout. If UNIX current time
298 * is >= timeout then the operation timed out. */
299 robj
*target
; /* The key that should receive the element,
303 /* With multiplexing we need to take per-clinet state.
304 * Clients are taken in a liked list. */
305 typedef struct redisClient
{
312 struct redisCommand
*cmd
;
314 int multibulklen
; /* number of multi bulk arguments left to read */
315 long bulklen
; /* length of bulk argument in multi bulk request */
318 time_t lastinteraction
; /* time of the last interaction, used for timeout */
319 int flags
; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */
320 int slaveseldb
; /* slave selected db, if this client is a slave */
321 int authenticated
; /* when requirepass is non-NULL */
322 int replstate
; /* replication state if this is a slave */
323 int repldbfd
; /* replication DB file descriptor */
324 long repldboff
; /* replication DB file offset */
325 off_t repldbsize
; /* replication DB file size */
326 multiState mstate
; /* MULTI/EXEC state */
327 blockingState bpop
; /* blocking state */
328 list
*io_keys
; /* Keys this client is waiting to be loaded from the
329 * swap file in order to continue. */
330 list
*watched_keys
; /* Keys WATCHED for MULTI/EXEC CAS */
331 dict
*pubsub_channels
; /* channels a client is interested in (SUBSCRIBE) */
332 list
*pubsub_patterns
; /* patterns a client is interested in (SUBSCRIBE) */
334 /* Response buffer */
336 char buf
[REDIS_REPLY_CHUNK_BYTES
];
344 struct sharedObjectsStruct
{
345 robj
*crlf
, *ok
, *err
, *emptybulk
, *czero
, *cone
, *cnegone
, *pong
, *space
,
346 *colon
, *nullbulk
, *nullmultibulk
, *queued
,
347 *emptymultibulk
, *wrongtypeerr
, *nokeyerr
, *syntaxerr
, *sameobjecterr
,
348 *outofrangeerr
, *noscripterr
, *loadingerr
, *plus
,
349 *select0
, *select1
, *select2
, *select3
, *select4
,
350 *select5
, *select6
, *select7
, *select8
, *select9
,
351 *messagebulk
, *pmessagebulk
, *subscribebulk
, *unsubscribebulk
, *mbulk3
,
352 *mbulk4
, *psubscribebulk
, *punsubscribebulk
,
353 *integers
[REDIS_SHARED_INTEGERS
];
356 /* ZSETs use a specialized version of Skiplists */
357 typedef struct zskiplistNode
{
360 struct zskiplistNode
*backward
;
361 struct zskiplistLevel
{
362 struct zskiplistNode
*forward
;
367 typedef struct zskiplist
{
368 struct zskiplistNode
*header
, *tail
;
369 unsigned long length
;
373 typedef struct zset
{
378 /*-----------------------------------------------------------------------------
379 * Redis cluster data structures
380 *----------------------------------------------------------------------------*/
382 #define REDIS_CLUSTER_SLOTS 4096
383 #define REDIS_CLUSTER_OK 0 /* Everything looks ok */
384 #define REDIS_CLUSTER_FAIL 1 /* The cluster can't work */
385 #define REDIS_CLUSTER_NEEDHELP 2 /* The cluster works, but needs some help */
386 #define REDIS_CLUSTER_NAMELEN 40 /* sha1 hex length */
387 #define REDIS_CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */
391 /* clusterLink encapsulates everything needed to talk with a remote node. */
392 typedef struct clusterLink
{
393 int fd
; /* TCP socket file descriptor */
394 sds sndbuf
; /* Packet send buffer */
395 sds rcvbuf
; /* Packet reception buffer */
396 struct clusterNode
*node
; /* Node related to this link if any, or NULL */
400 #define REDIS_NODE_MASTER 1 /* The node is a master */
401 #define REDIS_NODE_SLAVE 2 /* The node is a slave */
402 #define REDIS_NODE_PFAIL 4 /* Failure? Need acknowledge */
403 #define REDIS_NODE_FAIL 8 /* The node is believed to be malfunctioning */
404 #define REDIS_NODE_MYSELF 16 /* This node is myself */
405 #define REDIS_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */
406 #define REDIS_NODE_NOADDR 64 /* We don't know the address of this node */
407 #define REDIS_NODE_MEET 128 /* Send a MEET message to this node */
408 #define REDIS_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
411 char name
[REDIS_CLUSTER_NAMELEN
]; /* Node name, hex string, sha1-size */
412 int flags
; /* REDIS_NODE_... */
413 unsigned char slots
[REDIS_CLUSTER_SLOTS
/8]; /* slots handled by this node */
414 int numslaves
; /* Number of slave nodes, if this is a master */
415 struct clusterNode
**slaves
; /* pointers to slave nodes */
416 struct clusterNode
*slaveof
; /* pointer to the master node */
417 time_t ping_sent
; /* Unix time we sent latest ping */
418 time_t pong_received
; /* Unix time we received the pong */
419 char *configdigest
; /* Configuration digest of this node */
420 time_t configdigest_ts
; /* Configuration digest timestamp */
421 char ip
[16]; /* Latest known IP address of this node */
422 int port
; /* Latest known port of this node */
423 clusterLink
*link
; /* TCP/IP link with this node */
425 typedef struct clusterNode clusterNode
;
429 clusterNode
*myself
; /* This node */
430 int state
; /* REDIS_CLUSTER_OK, REDIS_CLUSTER_FAIL, ... */
432 dict
*nodes
; /* Hash table of name -> clusterNode structures */
433 clusterNode
*migrating_slots_to
[REDIS_CLUSTER_SLOTS
];
434 clusterNode
*importing_slots_from
[REDIS_CLUSTER_SLOTS
];
435 clusterNode
*slots
[REDIS_CLUSTER_SLOTS
];
436 zskiplist
*slots_to_keys
;
439 /* Redis cluster messages header */
441 /* Note that the PING, PONG and MEET messages are actually the same exact
442 * kind of packet. PONG is the reply to ping, in the extact format as a PING,
443 * while MEET is a special PING that forces the receiver to add the sender
444 * as a node (if it is not already in the list). */
445 #define CLUSTERMSG_TYPE_PING 0 /* Ping */
446 #define CLUSTERMSG_TYPE_PONG 1 /* Pong (reply to Ping) */
447 #define CLUSTERMSG_TYPE_MEET 2 /* Meet "let's join" message */
448 #define CLUSTERMSG_TYPE_FAIL 3 /* Mark node xxx as failing */
450 /* Initially we don't know our "name", but we'll find it once we connect
451 * to the first node, using the getsockname() function. Then we'll use this
452 * address for all the next messages. */
454 char nodename
[REDIS_CLUSTER_NAMELEN
];
456 uint32_t pong_received
;
457 char ip
[16]; /* IP address last time it was seen */
458 uint16_t port
; /* port last time it was seen */
460 uint32_t notused
; /* for 64 bit alignment */
461 } clusterMsgDataGossip
;
464 char nodename
[REDIS_CLUSTER_NAMELEN
];
465 } clusterMsgDataFail
;
467 union clusterMsgData
{
468 /* PING, MEET and PONG */
470 /* Array of N clusterMsgDataGossip structures */
471 clusterMsgDataGossip gossip
[1];
475 clusterMsgDataFail about
;
480 uint32_t totlen
; /* Total length of this message */
481 uint16_t type
; /* Message type */
482 uint16_t count
; /* Only used for some kind of messages. */
483 char sender
[REDIS_CLUSTER_NAMELEN
]; /* Name of the sender node */
484 unsigned char myslots
[REDIS_CLUSTER_SLOTS
/8];
485 char slaveof
[REDIS_CLUSTER_NAMELEN
];
486 char configdigest
[32];
487 uint16_t port
; /* Sender TCP base port */
488 unsigned char state
; /* Cluster state from the POV of the sender */
489 unsigned char notused
[5]; /* Reserved for future use. For alignment. */
490 union clusterMsgData data
;
493 /*-----------------------------------------------------------------------------
494 * Global server state
495 *----------------------------------------------------------------------------*/
500 dict
*commands
; /* Command table hahs table */
510 list
*slaves
, *monitors
;
511 char neterr
[ANET_ERR_LEN
];
512 /* RDB / AOF loading information */
514 off_t loading_total_bytes
;
515 off_t loading_loaded_bytes
;
516 time_t loading_start_time
;
517 /* Fast pointers to often looked up command */
518 struct redisCommand
*delCommand
, *multiCommand
;
519 int cronloops
; /* number of times the cron function run */
520 time_t lastsave
; /* Unix time of last save succeeede */
521 /* Fields used only for stats */
522 time_t stat_starttime
; /* server start time */
523 long long stat_numcommands
; /* number of processed commands */
524 long long stat_numconnections
; /* number of connections received */
525 long long stat_expiredkeys
; /* number of expired keys */
526 long long stat_evictedkeys
; /* number of evicted keys (maxmemory) */
527 long long stat_keyspace_hits
; /* number of successful lookups of keys */
528 long long stat_keyspace_misses
; /* number of failed lookups of keys */
529 size_t stat_peak_memory
; /* max used memory record */
530 long long stat_fork_time
; /* time needed to perform latets fork() */
532 long long slowlog_entry_id
;
533 long long slowlog_log_slower_than
;
534 unsigned long slowlog_max_len
;
542 int no_appendfsync_on_rewrite
;
543 int auto_aofrewrite_perc
; /* Rewrite AOF if % growth is > M and... */
544 off_t auto_aofrewrite_min_size
; /* the AOF file is at least N bytes. */
545 off_t auto_aofrewrite_base_size
;/* AOF size on latest startup or rewrite. */
546 off_t appendonly_current_size
; /* AOF current size. */
547 int aofrewrite_scheduled
; /* Rewrite once BGSAVE terminates. */
552 long long dirty
; /* changes to DB from the last save */
553 long long dirty_before_bgsave
; /* used to restore dirty on failed BGSAVE */
557 time_t aof_flush_postponed_start
;
559 pid_t bgsavechildpid
;
560 pid_t bgrewritechildpid
;
561 sds bgrewritebuf
; /* buffer taken by parent during oppend only rewrite */
562 sds aofbuf
; /* AOF buffer, written before entering the event loop */
563 struct saveparam
*saveparams
;
567 char *appendfilename
;
573 /* Replication related */
575 /* Slave specific fields */
579 redisClient
*master
; /* client that is master for this slave */
580 int repl_syncio_timeout
; /* timeout for synchronous I/O calls */
581 int replstate
; /* replication status if the instance is a slave */
582 off_t repl_transfer_left
; /* bytes left reading .rdb */
583 int repl_transfer_s
; /* slave -> master SYNC socket */
584 int repl_transfer_fd
; /* slave -> master SYNC temp file descriptor */
585 char *repl_transfer_tmpfile
; /* slave-> master SYNC temp file name */
586 time_t repl_transfer_lastio
; /* unix time of the latest read, for timeout */
587 int repl_serve_stale_data
; /* Serve stale data when link is down? */
588 time_t repl_down_since
; /* unix time at which link with master went down */
590 unsigned int maxclients
;
591 unsigned long long maxmemory
;
592 int maxmemory_policy
;
593 int maxmemory_samples
;
594 /* Blocked clients */
595 unsigned int bpop_blocked_clients
;
596 list
*unblocked_clients
; /* list of clients to unblock before next loop */
597 /* Sort parameters - qsort_r() is only available under BSD so we
598 * have to take this state global, in order to pass it to sortCompare() */
602 /* Zip structure config */
603 size_t hash_max_zipmap_entries
;
604 size_t hash_max_zipmap_value
;
605 size_t list_max_ziplist_entries
;
606 size_t list_max_ziplist_value
;
607 size_t set_max_intset_entries
;
608 size_t zset_max_ziplist_entries
;
609 size_t zset_max_ziplist_value
;
610 time_t unixtime
; /* Unix time sampled every second. */
612 dict
*pubsub_channels
; /* Map channels to list of subscribed clients */
613 list
*pubsub_patterns
; /* A list of pubsub_patterns */
615 unsigned lruclock
:22; /* clock incrementing every minute, for LRU */
616 unsigned lruclock_padding
:10;
619 clusterState cluster
;
621 lua_State
*lua
; /* The Lua interpreter. We use just one for all clients */
622 redisClient
*lua_client
; /* The "fake client" to query Redis from Lua */
623 dict
*lua_scripts
; /* A dictionary of SHA1 -> Lua scripts */
624 long long lua_time_limit
;
625 long long lua_time_start
;
626 int lua_random_dirty
; /* True if a random command was called during the
627 exection of the current script. */
630 typedef struct pubsubPattern
{
635 typedef void redisCommandProc(redisClient
*c
);
636 typedef int *redisGetKeysProc(struct redisCommand
*cmd
, robj
**argv
, int argc
, int *numkeys
, int flags
);
637 struct redisCommand
{
639 redisCommandProc
*proc
;
641 char *sflags
; /* Flags as string represenation, one char per flag. */
642 int flags
; /* The actual flags, obtained from the 'sflags' field. */
643 /* Use a function to determine keys arguments in a command line.
644 * Used for Redis Cluster redirect. */
645 redisGetKeysProc
*getkeys_proc
;
646 /* What keys should be loaded in background when calling this command? */
647 int firstkey
; /* The first argument that's a key (0 = no keys) */
648 int lastkey
; /* THe last argument that's a key */
649 int keystep
; /* The step between first and last key */
650 long long microseconds
, calls
;
653 struct redisFunctionSym
{
655 unsigned long pointer
;
658 typedef struct _redisSortObject
{
666 typedef struct _redisSortOperation
{
669 } redisSortOperation
;
671 /* Structure to hold list iteration abstraction. */
674 unsigned char encoding
;
675 unsigned char direction
; /* Iteration direction */
680 /* Structure for an entry while iterating over a list. */
682 listTypeIterator
*li
;
683 unsigned char *zi
; /* Entry in ziplist */
684 listNode
*ln
; /* Entry in linked list */
687 /* Structure to hold set iteration abstraction. */
691 int ii
; /* intset iterator */
695 /* Structure to hold hash iteration abstration. Note that iteration over
696 * hashes involves both fields and values. Because it is possible that
697 * not both are required, store pointers in the iterator to avoid
698 * unnecessary memory allocation for fields/values. */
702 unsigned char *zk
, *zv
;
703 unsigned int zklen
, zvlen
;
709 #define REDIS_HASH_KEY 1
710 #define REDIS_HASH_VALUE 2
712 /*-----------------------------------------------------------------------------
713 * Extern declarations
714 *----------------------------------------------------------------------------*/
716 extern struct redisServer server
;
717 extern struct sharedObjectsStruct shared
;
718 extern dictType setDictType
;
719 extern dictType zsetDictType
;
720 extern dictType clusterNodesDictType
;
721 extern dictType dbDictType
;
722 extern double R_Zero
, R_PosInf
, R_NegInf
, R_Nan
;
723 dictType hashDictType
;
725 /*-----------------------------------------------------------------------------
726 * Functions prototypes
727 *----------------------------------------------------------------------------*/
730 long long ustime(void);
732 /* networking.c -- Networking and Client related operations */
733 redisClient
*createClient(int fd
);
734 void closeTimedoutClients(void);
735 void freeClient(redisClient
*c
);
736 void resetClient(redisClient
*c
);
737 void sendReplyToClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
738 void addReply(redisClient
*c
, robj
*obj
);
739 void *addDeferredMultiBulkLength(redisClient
*c
);
740 void setDeferredMultiBulkLength(redisClient
*c
, void *node
, long length
);
741 void addReplySds(redisClient
*c
, sds s
);
742 void processInputBuffer(redisClient
*c
);
743 void acceptTcpHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
744 void acceptUnixHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
745 void readQueryFromClient(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
746 void addReplyBulk(redisClient
*c
, robj
*obj
);
747 void addReplyBulkCString(redisClient
*c
, char *s
);
748 void addReplyBulkCBuffer(redisClient
*c
, void *p
, size_t len
);
749 void addReplyBulkLongLong(redisClient
*c
, long long ll
);
750 void acceptHandler(aeEventLoop
*el
, int fd
, void *privdata
, int mask
);
751 void addReply(redisClient
*c
, robj
*obj
);
752 void addReplySds(redisClient
*c
, sds s
);
753 void addReplyError(redisClient
*c
, char *err
);
754 void addReplyStatus(redisClient
*c
, char *status
);
755 void addReplyDouble(redisClient
*c
, double d
);
756 void addReplyLongLong(redisClient
*c
, long long ll
);
757 void addReplyMultiBulkLen(redisClient
*c
, long length
);
758 void *dupClientReplyValue(void *o
);
759 void getClientsMaxBuffers(unsigned long *longest_output_list
,
760 unsigned long *biggest_input_buffer
);
761 void rewriteClientCommandVector(redisClient
*c
, int argc
, ...);
762 void rewriteClientCommandArgument(redisClient
*c
, int i
, robj
*newval
);
765 void addReplyErrorFormat(redisClient
*c
, const char *fmt
, ...)
766 __attribute__((format(printf
, 2, 3)));
767 void addReplyStatusFormat(redisClient
*c
, const char *fmt
, ...)
768 __attribute__((format(printf
, 2, 3)));
770 void addReplyErrorFormat(redisClient
*c
, const char *fmt
, ...);
771 void addReplyStatusFormat(redisClient
*c
, const char *fmt
, ...);
775 void listTypeTryConversion(robj
*subject
, robj
*value
);
776 void listTypePush(robj
*subject
, robj
*value
, int where
);
777 robj
*listTypePop(robj
*subject
, int where
);
778 unsigned long listTypeLength(robj
*subject
);
779 listTypeIterator
*listTypeInitIterator(robj
*subject
, int index
, unsigned char direction
);
780 void listTypeReleaseIterator(listTypeIterator
*li
);
781 int listTypeNext(listTypeIterator
*li
, listTypeEntry
*entry
);
782 robj
*listTypeGet(listTypeEntry
*entry
);
783 void listTypeInsert(listTypeEntry
*entry
, robj
*value
, int where
);
784 int listTypeEqual(listTypeEntry
*entry
, robj
*o
);
785 void listTypeDelete(listTypeEntry
*entry
);
786 void listTypeConvert(robj
*subject
, int enc
);
787 void unblockClientWaitingData(redisClient
*c
);
788 int handleClientsWaitingListPush(redisClient
*c
, robj
*key
, robj
*ele
);
789 void popGenericCommand(redisClient
*c
, int where
);
791 /* MULTI/EXEC/WATCH... */
792 void unwatchAllKeys(redisClient
*c
);
793 void initClientMultiState(redisClient
*c
);
794 void freeClientMultiState(redisClient
*c
);
795 void queueMultiCommand(redisClient
*c
);
796 void touchWatchedKey(redisDb
*db
, robj
*key
);
797 void touchWatchedKeysOnFlush(int dbid
);
799 /* Redis object implementation */
800 void decrRefCount(void *o
);
801 void incrRefCount(robj
*o
);
802 robj
*resetRefCount(robj
*obj
);
803 void freeStringObject(robj
*o
);
804 void freeListObject(robj
*o
);
805 void freeSetObject(robj
*o
);
806 void freeZsetObject(robj
*o
);
807 void freeHashObject(robj
*o
);
808 robj
*createObject(int type
, void *ptr
);
809 robj
*createStringObject(char *ptr
, size_t len
);
810 robj
*dupStringObject(robj
*o
);
811 int isObjectRepresentableAsLongLong(robj
*o
, long long *llongval
);
812 robj
*tryObjectEncoding(robj
*o
);
813 robj
*getDecodedObject(robj
*o
);
814 size_t stringObjectLen(robj
*o
);
815 robj
*createStringObjectFromLongLong(long long value
);
816 robj
*createListObject(void);
817 robj
*createZiplistObject(void);
818 robj
*createSetObject(void);
819 robj
*createIntsetObject(void);
820 robj
*createHashObject(void);
821 robj
*createZsetObject(void);
822 robj
*createZsetZiplistObject(void);
823 int getLongFromObjectOrReply(redisClient
*c
, robj
*o
, long *target
, const char *msg
);
824 int checkType(redisClient
*c
, robj
*o
, int type
);
825 int getLongLongFromObjectOrReply(redisClient
*c
, robj
*o
, long long *target
, const char *msg
);
826 int getDoubleFromObjectOrReply(redisClient
*c
, robj
*o
, double *target
, const char *msg
);
827 int getLongLongFromObject(robj
*o
, long long *target
);
828 char *strEncoding(int encoding
);
829 int compareStringObjects(robj
*a
, robj
*b
);
830 int equalStringObjects(robj
*a
, robj
*b
);
831 unsigned long estimateObjectIdleTime(robj
*o
);
833 /* Synchronous I/O with timeout */
834 int syncWrite(int fd
, char *ptr
, ssize_t size
, int timeout
);
835 int syncRead(int fd
, char *ptr
, ssize_t size
, int timeout
);
836 int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
);
839 void replicationFeedSlaves(list
*slaves
, int dictid
, robj
**argv
, int argc
);
840 void replicationFeedMonitors(list
*monitors
, int dictid
, robj
**argv
, int argc
);
841 void updateSlavesWaitingBgsave(int bgsaveerr
);
842 void replicationCron(void);
844 /* Generic persistence functions */
845 void startLoading(FILE *fp
);
846 void loadingProgress(off_t pos
);
847 void stopLoading(void);
849 /* RDB persistence */
852 /* AOF persistence */
853 void flushAppendOnlyFile(int force
);
854 void feedAppendOnlyFile(struct redisCommand
*cmd
, int dictid
, robj
**argv
, int argc
);
855 void aofRemoveTempFile(pid_t childpid
);
856 int rewriteAppendOnlyFileBackground(void);
857 int loadAppendOnlyFile(char *filename
);
858 void stopAppendOnly(void);
859 int startAppendOnly(void);
860 void backgroundRewriteDoneHandler(int exitcode
, int bysignal
);
862 /* Sorted sets data type */
864 /* Struct to hold a inclusive/exclusive range spec. */
867 int minex
, maxex
; /* are min or max exclusive? */
870 zskiplist
*zslCreate(void);
871 void zslFree(zskiplist
*zsl
);
872 zskiplistNode
*zslInsert(zskiplist
*zsl
, double score
, robj
*obj
);
873 unsigned char *zzlInsert(unsigned char *zl
, robj
*ele
, double score
);
874 int zslDelete(zskiplist
*zsl
, double score
, robj
*obj
);
875 zskiplistNode
*zslFirstInRange(zskiplist
*zsl
, zrangespec range
);
876 double zzlGetScore(unsigned char *sptr
);
877 void zzlNext(unsigned char *zl
, unsigned char **eptr
, unsigned char **sptr
);
878 void zzlPrev(unsigned char *zl
, unsigned char **eptr
, unsigned char **sptr
);
879 unsigned int zsetLength(robj
*zobj
);
880 void zsetConvert(robj
*zobj
, int encoding
);
883 void freeMemoryIfNeeded(void);
884 int processCommand(redisClient
*c
);
885 void setupSignalHandlers(void);
886 struct redisCommand
*lookupCommand(sds name
);
887 struct redisCommand
*lookupCommandByCString(char *s
);
888 void call(redisClient
*c
);
889 int prepareForShutdown();
890 void redisLog(int level
, const char *fmt
, ...);
891 void redisLogRaw(int level
, const char *msg
);
893 void updateDictResizePolicy(void);
894 int htNeedsResize(dict
*dict
);
895 void oom(const char *msg
);
896 void populateCommandTable(void);
897 void resetCommandTableStats(void);
900 robj
*setTypeCreate(robj
*value
);
901 int setTypeAdd(robj
*subject
, robj
*value
);
902 int setTypeRemove(robj
*subject
, robj
*value
);
903 int setTypeIsMember(robj
*subject
, robj
*value
);
904 setTypeIterator
*setTypeInitIterator(robj
*subject
);
905 void setTypeReleaseIterator(setTypeIterator
*si
);
906 int setTypeNext(setTypeIterator
*si
, robj
**objele
, int64_t *llele
);
907 robj
*setTypeNextObject(setTypeIterator
*si
);
908 int setTypeRandomElement(robj
*setobj
, robj
**objele
, int64_t *llele
);
909 unsigned long setTypeSize(robj
*subject
);
910 void setTypeConvert(robj
*subject
, int enc
);
913 void convertToRealHash(robj
*o
);
914 void hashTypeTryConversion(robj
*subject
, robj
**argv
, int start
, int end
);
915 void hashTypeTryObjectEncoding(robj
*subject
, robj
**o1
, robj
**o2
);
916 int hashTypeGet(robj
*o
, robj
*key
, robj
**objval
, unsigned char **v
, unsigned int *vlen
);
917 robj
*hashTypeGetObject(robj
*o
, robj
*key
);
918 int hashTypeExists(robj
*o
, robj
*key
);
919 int hashTypeSet(robj
*o
, robj
*key
, robj
*value
);
920 int hashTypeDelete(robj
*o
, robj
*key
);
921 unsigned long hashTypeLength(robj
*o
);
922 hashTypeIterator
*hashTypeInitIterator(robj
*subject
);
923 void hashTypeReleaseIterator(hashTypeIterator
*hi
);
924 int hashTypeNext(hashTypeIterator
*hi
);
925 int hashTypeCurrent(hashTypeIterator
*hi
, int what
, robj
**objval
, unsigned char **v
, unsigned int *vlen
);
926 robj
*hashTypeCurrentObject(hashTypeIterator
*hi
, int what
);
927 robj
*hashTypeLookupWriteOrCreate(redisClient
*c
, robj
*key
);
930 int pubsubUnsubscribeAllChannels(redisClient
*c
, int notify
);
931 int pubsubUnsubscribeAllPatterns(redisClient
*c
, int notify
);
932 void freePubsubPattern(void *p
);
933 int listMatchPubsubPattern(void *a
, void *b
);
936 void loadServerConfig(char *filename
);
937 void appendServerSaveParams(time_t seconds
, int changes
);
938 void resetServerSaveParams();
940 /* db.c -- Keyspace access API */
941 int removeExpire(redisDb
*db
, robj
*key
);
942 void propagateExpire(redisDb
*db
, robj
*key
);
943 int expireIfNeeded(redisDb
*db
, robj
*key
);
944 time_t getExpire(redisDb
*db
, robj
*key
);
945 void setExpire(redisDb
*db
, robj
*key
, time_t when
);
946 robj
*lookupKey(redisDb
*db
, robj
*key
);
947 robj
*lookupKeyRead(redisDb
*db
, robj
*key
);
948 robj
*lookupKeyWrite(redisDb
*db
, robj
*key
);
949 robj
*lookupKeyReadOrReply(redisClient
*c
, robj
*key
, robj
*reply
);
950 robj
*lookupKeyWriteOrReply(redisClient
*c
, robj
*key
, robj
*reply
);
951 void dbAdd(redisDb
*db
, robj
*key
, robj
*val
);
952 void dbOverwrite(redisDb
*db
, robj
*key
, robj
*val
);
953 void setKey(redisDb
*db
, robj
*key
, robj
*val
);
954 int dbExists(redisDb
*db
, robj
*key
);
955 robj
*dbRandomKey(redisDb
*db
);
956 int dbDelete(redisDb
*db
, robj
*key
);
958 int selectDb(redisClient
*c
, int id
);
959 void signalModifiedKey(redisDb
*db
, robj
*key
);
960 void signalFlushedDb(int dbid
);
961 unsigned int GetKeysInSlot(unsigned int hashslot
, robj
**keys
, unsigned int count
);
963 /* API to get key arguments from commands */
964 #define REDIS_GETKEYS_ALL 0
965 #define REDIS_GETKEYS_PRELOAD 1
966 int *getKeysFromCommand(struct redisCommand
*cmd
, robj
**argv
, int argc
, int *numkeys
, int flags
);
967 void getKeysFreeResult(int *result
);
968 int *noPreloadGetKeys(struct redisCommand
*cmd
,robj
**argv
, int argc
, int *numkeys
, int flags
);
969 int *renameGetKeys(struct redisCommand
*cmd
,robj
**argv
, int argc
, int *numkeys
, int flags
);
970 int *zunionInterGetKeys(struct redisCommand
*cmd
,robj
**argv
, int argc
, int *numkeys
, int flags
);
973 void clusterInit(void);
974 unsigned short crc16(const char *buf
, int len
);
975 unsigned int keyHashSlot(char *key
, int keylen
);
976 clusterNode
*createClusterNode(char *nodename
, int flags
);
977 int clusterAddNode(clusterNode
*node
);
978 void clusterCron(void);
979 clusterNode
*getNodeByQuery(redisClient
*c
, struct redisCommand
*cmd
, robj
**argv
, int argc
, int *hashslot
, int *ask
);
982 void scriptingInit(void);
985 char *redisGitSHA1(void);
986 char *redisGitDirty(void);
988 /* Commands prototypes */
989 void authCommand(redisClient
*c
);
990 void pingCommand(redisClient
*c
);
991 void echoCommand(redisClient
*c
);
992 void setCommand(redisClient
*c
);
993 void setnxCommand(redisClient
*c
);
994 void setexCommand(redisClient
*c
);
995 void getCommand(redisClient
*c
);
996 void delCommand(redisClient
*c
);
997 void existsCommand(redisClient
*c
);
998 void setbitCommand(redisClient
*c
);
999 void getbitCommand(redisClient
*c
);
1000 void setrangeCommand(redisClient
*c
);
1001 void getrangeCommand(redisClient
*c
);
1002 void incrCommand(redisClient
*c
);
1003 void decrCommand(redisClient
*c
);
1004 void incrbyCommand(redisClient
*c
);
1005 void decrbyCommand(redisClient
*c
);
1006 void selectCommand(redisClient
*c
);
1007 void randomkeyCommand(redisClient
*c
);
1008 void keysCommand(redisClient
*c
);
1009 void dbsizeCommand(redisClient
*c
);
1010 void lastsaveCommand(redisClient
*c
);
1011 void saveCommand(redisClient
*c
);
1012 void bgsaveCommand(redisClient
*c
);
1013 void bgrewriteaofCommand(redisClient
*c
);
1014 void shutdownCommand(redisClient
*c
);
1015 void moveCommand(redisClient
*c
);
1016 void renameCommand(redisClient
*c
);
1017 void renamenxCommand(redisClient
*c
);
1018 void lpushCommand(redisClient
*c
);
1019 void rpushCommand(redisClient
*c
);
1020 void lpushxCommand(redisClient
*c
);
1021 void rpushxCommand(redisClient
*c
);
1022 void linsertCommand(redisClient
*c
);
1023 void lpopCommand(redisClient
*c
);
1024 void rpopCommand(redisClient
*c
);
1025 void llenCommand(redisClient
*c
);
1026 void lindexCommand(redisClient
*c
);
1027 void lrangeCommand(redisClient
*c
);
1028 void ltrimCommand(redisClient
*c
);
1029 void typeCommand(redisClient
*c
);
1030 void lsetCommand(redisClient
*c
);
1031 void saddCommand(redisClient
*c
);
1032 void sremCommand(redisClient
*c
);
1033 void smoveCommand(redisClient
*c
);
1034 void sismemberCommand(redisClient
*c
);
1035 void scardCommand(redisClient
*c
);
1036 void spopCommand(redisClient
*c
);
1037 void srandmemberCommand(redisClient
*c
);
1038 void sinterCommand(redisClient
*c
);
1039 void sinterstoreCommand(redisClient
*c
);
1040 void sunionCommand(redisClient
*c
);
1041 void sunionstoreCommand(redisClient
*c
);
1042 void sdiffCommand(redisClient
*c
);
1043 void sdiffstoreCommand(redisClient
*c
);
1044 void syncCommand(redisClient
*c
);
1045 void flushdbCommand(redisClient
*c
);
1046 void flushallCommand(redisClient
*c
);
1047 void sortCommand(redisClient
*c
);
1048 void lremCommand(redisClient
*c
);
1049 void rpoplpushCommand(redisClient
*c
);
1050 void infoCommand(redisClient
*c
);
1051 void mgetCommand(redisClient
*c
);
1052 void monitorCommand(redisClient
*c
);
1053 void expireCommand(redisClient
*c
);
1054 void expireatCommand(redisClient
*c
);
1055 void getsetCommand(redisClient
*c
);
1056 void ttlCommand(redisClient
*c
);
1057 void persistCommand(redisClient
*c
);
1058 void slaveofCommand(redisClient
*c
);
1059 void debugCommand(redisClient
*c
);
1060 void msetCommand(redisClient
*c
);
1061 void msetnxCommand(redisClient
*c
);
1062 void zaddCommand(redisClient
*c
);
1063 void zincrbyCommand(redisClient
*c
);
1064 void zrangeCommand(redisClient
*c
);
1065 void zrangebyscoreCommand(redisClient
*c
);
1066 void zrevrangebyscoreCommand(redisClient
*c
);
1067 void zcountCommand(redisClient
*c
);
1068 void zrevrangeCommand(redisClient
*c
);
1069 void zcardCommand(redisClient
*c
);
1070 void zremCommand(redisClient
*c
);
1071 void zscoreCommand(redisClient
*c
);
1072 void zremrangebyscoreCommand(redisClient
*c
);
1073 void multiCommand(redisClient
*c
);
1074 void execCommand(redisClient
*c
);
1075 void discardCommand(redisClient
*c
);
1076 void blpopCommand(redisClient
*c
);
1077 void brpopCommand(redisClient
*c
);
1078 void brpoplpushCommand(redisClient
*c
);
1079 void appendCommand(redisClient
*c
);
1080 void strlenCommand(redisClient
*c
);
1081 void zrankCommand(redisClient
*c
);
1082 void zrevrankCommand(redisClient
*c
);
1083 void hsetCommand(redisClient
*c
);
1084 void hsetnxCommand(redisClient
*c
);
1085 void hgetCommand(redisClient
*c
);
1086 void hmsetCommand(redisClient
*c
);
1087 void hmgetCommand(redisClient
*c
);
1088 void hdelCommand(redisClient
*c
);
1089 void hlenCommand(redisClient
*c
);
1090 void zremrangebyrankCommand(redisClient
*c
);
1091 void zunionstoreCommand(redisClient
*c
);
1092 void zinterstoreCommand(redisClient
*c
);
1093 void hkeysCommand(redisClient
*c
);
1094 void hvalsCommand(redisClient
*c
);
1095 void hgetallCommand(redisClient
*c
);
1096 void hexistsCommand(redisClient
*c
);
1097 void configCommand(redisClient
*c
);
1098 void hincrbyCommand(redisClient
*c
);
1099 void subscribeCommand(redisClient
*c
);
1100 void unsubscribeCommand(redisClient
*c
);
1101 void psubscribeCommand(redisClient
*c
);
1102 void punsubscribeCommand(redisClient
*c
);
1103 void publishCommand(redisClient
*c
);
1104 void watchCommand(redisClient
*c
);
1105 void unwatchCommand(redisClient
*c
);
1106 void clusterCommand(redisClient
*c
);
1107 void restoreCommand(redisClient
*c
);
1108 void migrateCommand(redisClient
*c
);
1109 void dumpCommand(redisClient
*c
);
1110 void objectCommand(redisClient
*c
);
1111 void clientCommand(redisClient
*c
);
1112 void evalCommand(redisClient
*c
);
1113 void evalShaCommand(redisClient
*c
);
1115 #if defined(__GNUC__)
1116 void *calloc(size_t count
, size_t size
) __attribute__ ((deprecated
));
1117 void free(void *ptr
) __attribute__ ((deprecated
));
1118 void *malloc(size_t size
) __attribute__ ((deprecated
));
1119 void *realloc(void *ptr
, size_t size
) __attribute__ ((deprecated
));