]>
Commit | Line | Data |
---|---|---|
1 | #ifndef __REDIS_H | |
2 | #define __REDIS_H | |
3 | ||
4 | #include "fmacros.h" | |
5 | #include "config.h" | |
6 | ||
7 | #if defined(__sun) | |
8 | #include "solarisfixes.h" | |
9 | #endif | |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <time.h> | |
15 | #include <limits.h> | |
16 | #include <unistd.h> | |
17 | #include <errno.h> | |
18 | #include <inttypes.h> | |
19 | #include <pthread.h> | |
20 | #include <syslog.h> | |
21 | #include <netinet/in.h> | |
22 | #include <lua.h> | |
23 | #include <signal.h> | |
24 | ||
25 | #include "ae.h" /* Event driven programming library */ | |
26 | #include "sds.h" /* Dynamic safe strings */ | |
27 | #include "dict.h" /* Hash tables */ | |
28 | #include "adlist.h" /* Linked lists */ | |
29 | #include "zmalloc.h" /* total memory usage aware version of malloc/free */ | |
30 | #include "anet.h" /* Networking the easy way */ | |
31 | #include "zipmap.h" /* Compact string -> string data structure */ | |
32 | #include "ziplist.h" /* Compact list data structure */ | |
33 | #include "intset.h" /* Compact integer set structure */ | |
34 | #include "version.h" /* Version macro */ | |
35 | #include "util.h" /* Misc functions useful in many places */ | |
36 | ||
37 | /* Error codes */ | |
38 | #define REDIS_OK 0 | |
39 | #define REDIS_ERR -1 | |
40 | ||
41 | /* Static server configuration */ | |
42 | #define REDIS_SERVERPORT 6379 /* TCP port */ | |
43 | #define REDIS_MAXIDLETIME 0 /* default client timeout: infinite */ | |
44 | #define REDIS_DEFAULT_DBNUM 16 | |
45 | #define REDIS_CONFIGLINE_MAX 1024 | |
46 | #define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */ | |
47 | #define REDIS_MAX_WRITE_PER_EVENT (1024*64) | |
48 | #define REDIS_SHARED_INTEGERS 10000 | |
49 | #define REDIS_SHARED_BULKHDR_LEN 32 | |
50 | #define REDIS_MAX_LOGMSG_LEN 1024 /* Default maximum length of syslog messages */ | |
51 | #define REDIS_AOF_REWRITE_PERC 100 | |
52 | #define REDIS_AOF_REWRITE_MIN_SIZE (1024*1024) | |
53 | #define REDIS_AOF_REWRITE_ITEMS_PER_CMD 64 | |
54 | #define REDIS_SLOWLOG_LOG_SLOWER_THAN 10000 | |
55 | #define REDIS_SLOWLOG_MAX_LEN 64 | |
56 | #define REDIS_MAX_CLIENTS 10000 | |
57 | ||
58 | #define REDIS_REPL_TIMEOUT 60 | |
59 | #define REDIS_REPL_PING_SLAVE_PERIOD 10 | |
60 | ||
61 | /* Protocol and I/O related defines */ | |
62 | #define REDIS_MAX_QUERYBUF_LEN (1024*1024*1024) /* 1GB max query buffer. */ | |
63 | #define REDIS_IOBUF_LEN (1024*16) /* Generic I/O buffer size */ | |
64 | #define REDIS_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */ | |
65 | #define REDIS_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */ | |
66 | #define REDIS_MBULK_BIG_ARG (1024*32) | |
67 | ||
68 | /* Hash table parameters */ | |
69 | #define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */ | |
70 | ||
71 | /* Command flags. Please check the command table defined in the redis.c file | |
72 | * for more information about the meaning of every flag. */ | |
73 | #define REDIS_CMD_WRITE 1 /* "w" flag */ | |
74 | #define REDIS_CMD_READONLY 2 /* "r" flag */ | |
75 | #define REDIS_CMD_DENYOOM 4 /* "m" flag */ | |
76 | #define REDIS_CMD_FORCE_REPLICATION 8 /* "f" flag */ | |
77 | #define REDIS_CMD_ADMIN 16 /* "a" flag */ | |
78 | #define REDIS_CMD_PUBSUB 32 /* "p" flag */ | |
79 | #define REDIS_CMD_NOSCRIPT 64 /* "s" flag */ | |
80 | #define REDIS_CMD_RANDOM 128 /* "R" flag */ | |
81 | #define REDIS_CMD_SORT_FOR_SCRIPT 256 /* "S" flag */ | |
82 | ||
83 | /* Object types */ | |
84 | #define REDIS_STRING 0 | |
85 | #define REDIS_LIST 1 | |
86 | #define REDIS_SET 2 | |
87 | #define REDIS_ZSET 3 | |
88 | #define REDIS_HASH 4 | |
89 | #define REDIS_VMPOINTER 8 | |
90 | ||
91 | /* Objects encoding. Some kind of objects like Strings and Hashes can be | |
92 | * internally represented in multiple ways. The 'encoding' field of the object | |
93 | * is set to one of this fields for this object. */ | |
94 | #define REDIS_ENCODING_RAW 0 /* Raw representation */ | |
95 | #define REDIS_ENCODING_INT 1 /* Encoded as integer */ | |
96 | #define REDIS_ENCODING_HT 2 /* Encoded as hash table */ | |
97 | #define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */ | |
98 | #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */ | |
99 | #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */ | |
100 | #define REDIS_ENCODING_INTSET 6 /* Encoded as intset */ | |
101 | #define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */ | |
102 | ||
103 | /* Defines related to the dump file format. To store 32 bits lengths for short | |
104 | * keys requires a lot of space, so we check the most significant 2 bits of | |
105 | * the first byte to interpreter the length: | |
106 | * | |
107 | * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte | |
108 | * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte | |
109 | * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow | |
110 | * 11|000000 this means: specially encoded object will follow. The six bits | |
111 | * number specify the kind of object that follows. | |
112 | * See the REDIS_RDB_ENC_* defines. | |
113 | * | |
114 | * Lenghts up to 63 are stored using a single byte, most DB keys, and may | |
115 | * values, will fit inside. */ | |
116 | #define REDIS_RDB_6BITLEN 0 | |
117 | #define REDIS_RDB_14BITLEN 1 | |
118 | #define REDIS_RDB_32BITLEN 2 | |
119 | #define REDIS_RDB_ENCVAL 3 | |
120 | #define REDIS_RDB_LENERR UINT_MAX | |
121 | ||
122 | /* When a length of a string object stored on disk has the first two bits | |
123 | * set, the remaining two bits specify a special encoding for the object | |
124 | * accordingly to the following defines: */ | |
125 | #define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */ | |
126 | #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */ | |
127 | #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */ | |
128 | #define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */ | |
129 | ||
130 | /* AOF states */ | |
131 | #define REDIS_AOF_OFF 0 /* AOF is off */ | |
132 | #define REDIS_AOF_ON 1 /* AOF is on */ | |
133 | #define REDIS_AOF_WAIT_REWRITE 2 /* AOF waits rewrite to start appending */ | |
134 | ||
135 | /* Client flags */ | |
136 | #define REDIS_SLAVE 1 /* This client is a slave server */ | |
137 | #define REDIS_MASTER 2 /* This client is a master server */ | |
138 | #define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */ | |
139 | #define REDIS_MULTI 8 /* This client is in a MULTI context */ | |
140 | #define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */ | |
141 | #define REDIS_DIRTY_CAS 64 /* Watched keys modified. EXEC will fail. */ | |
142 | #define REDIS_CLOSE_AFTER_REPLY 128 /* Close after writing entire reply. */ | |
143 | #define REDIS_UNBLOCKED 256 /* This client was unblocked and is stored in | |
144 | server.unblocked_clients */ | |
145 | #define REDIS_LUA_CLIENT 512 /* This is a non connected client used by Lua */ | |
146 | #define REDIS_ASKING 1024 /* Client issued the ASKING command */ | |
147 | #define REDIS_CLOSE_ASAP 2048 /* Close this client ASAP */ | |
148 | ||
149 | /* Client request types */ | |
150 | #define REDIS_REQ_INLINE 1 | |
151 | #define REDIS_REQ_MULTIBULK 2 | |
152 | ||
153 | /* Client classes for client limits, currently used only for | |
154 | * the max-client-output-buffer limit implementation. */ | |
155 | #define REDIS_CLIENT_LIMIT_CLASS_NORMAL 0 | |
156 | #define REDIS_CLIENT_LIMIT_CLASS_SLAVE 1 | |
157 | #define REDIS_CLIENT_LIMIT_CLASS_PUBSUB 2 | |
158 | #define REDIS_CLIENT_LIMIT_NUM_CLASSES 3 | |
159 | ||
160 | /* Slave replication state - slave side */ | |
161 | #define REDIS_REPL_NONE 0 /* No active replication */ | |
162 | #define REDIS_REPL_CONNECT 1 /* Must connect to master */ | |
163 | #define REDIS_REPL_CONNECTING 2 /* Connecting to master */ | |
164 | #define REDIS_REPL_TRANSFER 3 /* Receiving .rdb from master */ | |
165 | #define REDIS_REPL_CONNECTED 4 /* Connected to master */ | |
166 | ||
167 | /* Synchronous read timeout - slave side */ | |
168 | #define REDIS_REPL_SYNCIO_TIMEOUT 5 | |
169 | ||
170 | /* Slave replication state - from the point of view of master | |
171 | * Note that in SEND_BULK and ONLINE state the slave receives new updates | |
172 | * in its output queue. In the WAIT_BGSAVE state instead the server is waiting | |
173 | * to start the next background saving in order to send updates to it. */ | |
174 | #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */ | |
175 | #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */ | |
176 | #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */ | |
177 | #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */ | |
178 | ||
179 | /* List related stuff */ | |
180 | #define REDIS_HEAD 0 | |
181 | #define REDIS_TAIL 1 | |
182 | ||
183 | /* Sort operations */ | |
184 | #define REDIS_SORT_GET 0 | |
185 | #define REDIS_SORT_ASC 1 | |
186 | #define REDIS_SORT_DESC 2 | |
187 | #define REDIS_SORTKEY_MAX 1024 | |
188 | ||
189 | /* Log levels */ | |
190 | #define REDIS_DEBUG 0 | |
191 | #define REDIS_VERBOSE 1 | |
192 | #define REDIS_NOTICE 2 | |
193 | #define REDIS_WARNING 3 | |
194 | #define REDIS_LOG_RAW (1<<10) /* Modifier to log without timestamp */ | |
195 | ||
196 | /* Anti-warning macro... */ | |
197 | #define REDIS_NOTUSED(V) ((void) V) | |
198 | ||
199 | #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */ | |
200 | #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */ | |
201 | ||
202 | /* Append only defines */ | |
203 | #define AOF_FSYNC_NO 0 | |
204 | #define AOF_FSYNC_ALWAYS 1 | |
205 | #define AOF_FSYNC_EVERYSEC 2 | |
206 | ||
207 | /* Zip structure related defaults */ | |
208 | #define REDIS_HASH_MAX_ZIPMAP_ENTRIES 512 | |
209 | #define REDIS_HASH_MAX_ZIPMAP_VALUE 64 | |
210 | #define REDIS_LIST_MAX_ZIPLIST_ENTRIES 512 | |
211 | #define REDIS_LIST_MAX_ZIPLIST_VALUE 64 | |
212 | #define REDIS_SET_MAX_INTSET_ENTRIES 512 | |
213 | #define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128 | |
214 | #define REDIS_ZSET_MAX_ZIPLIST_VALUE 64 | |
215 | ||
216 | /* Sets operations codes */ | |
217 | #define REDIS_OP_UNION 0 | |
218 | #define REDIS_OP_DIFF 1 | |
219 | #define REDIS_OP_INTER 2 | |
220 | ||
221 | /* Redis maxmemory strategies */ | |
222 | #define REDIS_MAXMEMORY_VOLATILE_LRU 0 | |
223 | #define REDIS_MAXMEMORY_VOLATILE_TTL 1 | |
224 | #define REDIS_MAXMEMORY_VOLATILE_RANDOM 2 | |
225 | #define REDIS_MAXMEMORY_ALLKEYS_LRU 3 | |
226 | #define REDIS_MAXMEMORY_ALLKEYS_RANDOM 4 | |
227 | #define REDIS_MAXMEMORY_NO_EVICTION 5 | |
228 | ||
229 | /* Scripting */ | |
230 | #define REDIS_LUA_TIME_LIMIT 5000 /* milliseconds */ | |
231 | ||
232 | /* Units */ | |
233 | #define UNIT_SECONDS 0 | |
234 | #define UNIT_MILLISECONDS 1 | |
235 | ||
236 | /* SHUTDOWN flags */ | |
237 | #define REDIS_SHUTDOWN_SAVE 1 /* Force SAVE on SHUTDOWN even if no save | |
238 | points are configured. */ | |
239 | #define REDIS_SHUTDOWN_NOSAVE 2 /* Don't SAVE on SHUTDOWN. */ | |
240 | ||
241 | /* Command call flags, see call() function */ | |
242 | #define REDIS_CALL_NONE 0 | |
243 | #define REDIS_CALL_SLOWLOG 1 | |
244 | #define REDIS_CALL_STATS 2 | |
245 | #define REDIS_CALL_PROPAGATE 4 | |
246 | #define REDIS_CALL_FULL (REDIS_CALL_SLOWLOG | REDIS_CALL_STATS | REDIS_CALL_PROPAGATE) | |
247 | ||
248 | /* We can print the stacktrace, so our assert is defined this way: */ | |
249 | #define redisAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_redisAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1))) | |
250 | #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1))) | |
251 | #define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1) | |
252 | ||
253 | /*----------------------------------------------------------------------------- | |
254 | * Data types | |
255 | *----------------------------------------------------------------------------*/ | |
256 | ||
257 | /* A redis object, that is a type able to hold a string / list / set */ | |
258 | ||
259 | /* The actual Redis Object */ | |
260 | #define REDIS_LRU_CLOCK_MAX ((1<<21)-1) /* Max value of obj->lru */ | |
261 | #define REDIS_LRU_CLOCK_RESOLUTION 10 /* LRU clock resolution in seconds */ | |
262 | typedef struct redisObject { | |
263 | unsigned type:4; | |
264 | unsigned notused:2; /* Not used */ | |
265 | unsigned encoding:4; | |
266 | unsigned lru:22; /* lru time (relative to server.lruclock) */ | |
267 | int refcount; | |
268 | void *ptr; | |
269 | } robj; | |
270 | ||
271 | /* Macro used to initalize a Redis object allocated on the stack. | |
272 | * Note that this macro is taken near the structure definition to make sure | |
273 | * we'll update it when the structure is changed, to avoid bugs like | |
274 | * bug #85 introduced exactly in this way. */ | |
275 | #define initStaticStringObject(_var,_ptr) do { \ | |
276 | _var.refcount = 1; \ | |
277 | _var.type = REDIS_STRING; \ | |
278 | _var.encoding = REDIS_ENCODING_RAW; \ | |
279 | _var.ptr = _ptr; \ | |
280 | } while(0); | |
281 | ||
282 | typedef struct redisDb { | |
283 | dict *dict; /* The keyspace for this DB */ | |
284 | dict *expires; /* Timeout of keys with a timeout set */ | |
285 | dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */ | |
286 | dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */ | |
287 | int id; | |
288 | } redisDb; | |
289 | ||
290 | /* Client MULTI/EXEC state */ | |
291 | typedef struct multiCmd { | |
292 | robj **argv; | |
293 | int argc; | |
294 | struct redisCommand *cmd; | |
295 | } multiCmd; | |
296 | ||
297 | typedef struct multiState { | |
298 | multiCmd *commands; /* Array of MULTI commands */ | |
299 | int count; /* Total number of MULTI commands */ | |
300 | } multiState; | |
301 | ||
302 | typedef struct blockingState { | |
303 | robj **keys; /* The key we are waiting to terminate a blocking | |
304 | * operation such as BLPOP. Otherwise NULL. */ | |
305 | int count; /* Number of blocking keys */ | |
306 | time_t timeout; /* Blocking operation timeout. If UNIX current time | |
307 | * is >= timeout then the operation timed out. */ | |
308 | robj *target; /* The key that should receive the element, | |
309 | * for BRPOPLPUSH. */ | |
310 | } blockingState; | |
311 | ||
312 | /* With multiplexing we need to take per-clinet state. | |
313 | * Clients are taken in a liked list. */ | |
314 | typedef struct redisClient { | |
315 | int fd; | |
316 | redisDb *db; | |
317 | int dictid; | |
318 | sds querybuf; | |
319 | int argc; | |
320 | robj **argv; | |
321 | struct redisCommand *cmd, *lastcmd; | |
322 | int reqtype; | |
323 | int multibulklen; /* number of multi bulk arguments left to read */ | |
324 | long bulklen; /* length of bulk argument in multi bulk request */ | |
325 | list *reply; | |
326 | unsigned long reply_bytes; /* Tot bytes of objects in reply list */ | |
327 | int sentlen; | |
328 | time_t lastinteraction; /* time of the last interaction, used for timeout */ | |
329 | time_t obuf_soft_limit_reached_time; | |
330 | int flags; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */ | |
331 | int slaveseldb; /* slave selected db, if this client is a slave */ | |
332 | int authenticated; /* when requirepass is non-NULL */ | |
333 | int replstate; /* replication state if this is a slave */ | |
334 | int repldbfd; /* replication DB file descriptor */ | |
335 | long repldboff; /* replication DB file offset */ | |
336 | off_t repldbsize; /* replication DB file size */ | |
337 | multiState mstate; /* MULTI/EXEC state */ | |
338 | blockingState bpop; /* blocking state */ | |
339 | list *io_keys; /* Keys this client is waiting to be loaded from the | |
340 | * swap file in order to continue. */ | |
341 | list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */ | |
342 | dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */ | |
343 | list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */ | |
344 | ||
345 | /* Response buffer */ | |
346 | int bufpos; | |
347 | char buf[REDIS_REPLY_CHUNK_BYTES]; | |
348 | } redisClient; | |
349 | ||
350 | struct saveparam { | |
351 | time_t seconds; | |
352 | int changes; | |
353 | }; | |
354 | ||
355 | struct sharedObjectsStruct { | |
356 | robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *cnegone, *pong, *space, | |
357 | *colon, *nullbulk, *nullmultibulk, *queued, | |
358 | *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr, | |
359 | *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *plus, | |
360 | *select0, *select1, *select2, *select3, *select4, | |
361 | *select5, *select6, *select7, *select8, *select9, | |
362 | *messagebulk, *pmessagebulk, *subscribebulk, *unsubscribebulk, | |
363 | *psubscribebulk, *punsubscribebulk, *del, | |
364 | *integers[REDIS_SHARED_INTEGERS], | |
365 | *mbulkhdr[REDIS_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */ | |
366 | *bulkhdr[REDIS_SHARED_BULKHDR_LEN]; /* "$<value>\r\n" */ | |
367 | }; | |
368 | ||
369 | /* ZSETs use a specialized version of Skiplists */ | |
370 | typedef struct zskiplistNode { | |
371 | robj *obj; | |
372 | double score; | |
373 | struct zskiplistNode *backward; | |
374 | struct zskiplistLevel { | |
375 | struct zskiplistNode *forward; | |
376 | unsigned int span; | |
377 | } level[]; | |
378 | } zskiplistNode; | |
379 | ||
380 | typedef struct zskiplist { | |
381 | struct zskiplistNode *header, *tail; | |
382 | unsigned long length; | |
383 | int level; | |
384 | } zskiplist; | |
385 | ||
386 | typedef struct zset { | |
387 | dict *dict; | |
388 | zskiplist *zsl; | |
389 | } zset; | |
390 | ||
391 | typedef struct clientBufferLimitsConfig { | |
392 | unsigned long long hard_limit_bytes; | |
393 | unsigned long long soft_limit_bytes; | |
394 | time_t soft_limit_seconds; | |
395 | } clientBufferLimitsConfig; | |
396 | ||
397 | /*----------------------------------------------------------------------------- | |
398 | * Redis cluster data structures | |
399 | *----------------------------------------------------------------------------*/ | |
400 | ||
401 | #define REDIS_CLUSTER_SLOTS 4096 | |
402 | #define REDIS_CLUSTER_OK 0 /* Everything looks ok */ | |
403 | #define REDIS_CLUSTER_FAIL 1 /* The cluster can't work */ | |
404 | #define REDIS_CLUSTER_NEEDHELP 2 /* The cluster works, but needs some help */ | |
405 | #define REDIS_CLUSTER_NAMELEN 40 /* sha1 hex length */ | |
406 | #define REDIS_CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */ | |
407 | ||
408 | struct clusterNode; | |
409 | ||
410 | /* clusterLink encapsulates everything needed to talk with a remote node. */ | |
411 | typedef struct clusterLink { | |
412 | int fd; /* TCP socket file descriptor */ | |
413 | sds sndbuf; /* Packet send buffer */ | |
414 | sds rcvbuf; /* Packet reception buffer */ | |
415 | struct clusterNode *node; /* Node related to this link if any, or NULL */ | |
416 | } clusterLink; | |
417 | ||
418 | /* Node flags */ | |
419 | #define REDIS_NODE_MASTER 1 /* The node is a master */ | |
420 | #define REDIS_NODE_SLAVE 2 /* The node is a slave */ | |
421 | #define REDIS_NODE_PFAIL 4 /* Failure? Need acknowledge */ | |
422 | #define REDIS_NODE_FAIL 8 /* The node is believed to be malfunctioning */ | |
423 | #define REDIS_NODE_MYSELF 16 /* This node is myself */ | |
424 | #define REDIS_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */ | |
425 | #define REDIS_NODE_NOADDR 64 /* We don't know the address of this node */ | |
426 | #define REDIS_NODE_MEET 128 /* Send a MEET message to this node */ | |
427 | #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" | |
428 | ||
429 | struct clusterNode { | |
430 | char name[REDIS_CLUSTER_NAMELEN]; /* Node name, hex string, sha1-size */ | |
431 | int flags; /* REDIS_NODE_... */ | |
432 | unsigned char slots[REDIS_CLUSTER_SLOTS/8]; /* slots handled by this node */ | |
433 | int numslaves; /* Number of slave nodes, if this is a master */ | |
434 | struct clusterNode **slaves; /* pointers to slave nodes */ | |
435 | struct clusterNode *slaveof; /* pointer to the master node */ | |
436 | time_t ping_sent; /* Unix time we sent latest ping */ | |
437 | time_t pong_received; /* Unix time we received the pong */ | |
438 | char *configdigest; /* Configuration digest of this node */ | |
439 | time_t configdigest_ts; /* Configuration digest timestamp */ | |
440 | char ip[16]; /* Latest known IP address of this node */ | |
441 | int port; /* Latest known port of this node */ | |
442 | clusterLink *link; /* TCP/IP link with this node */ | |
443 | }; | |
444 | typedef struct clusterNode clusterNode; | |
445 | ||
446 | typedef struct { | |
447 | char *configfile; | |
448 | clusterNode *myself; /* This node */ | |
449 | int state; /* REDIS_CLUSTER_OK, REDIS_CLUSTER_FAIL, ... */ | |
450 | int node_timeout; | |
451 | dict *nodes; /* Hash table of name -> clusterNode structures */ | |
452 | clusterNode *migrating_slots_to[REDIS_CLUSTER_SLOTS]; | |
453 | clusterNode *importing_slots_from[REDIS_CLUSTER_SLOTS]; | |
454 | clusterNode *slots[REDIS_CLUSTER_SLOTS]; | |
455 | zskiplist *slots_to_keys; | |
456 | } clusterState; | |
457 | ||
458 | /* Redis cluster messages header */ | |
459 | ||
460 | /* Note that the PING, PONG and MEET messages are actually the same exact | |
461 | * kind of packet. PONG is the reply to ping, in the extact format as a PING, | |
462 | * while MEET is a special PING that forces the receiver to add the sender | |
463 | * as a node (if it is not already in the list). */ | |
464 | #define CLUSTERMSG_TYPE_PING 0 /* Ping */ | |
465 | #define CLUSTERMSG_TYPE_PONG 1 /* Pong (reply to Ping) */ | |
466 | #define CLUSTERMSG_TYPE_MEET 2 /* Meet "let's join" message */ | |
467 | #define CLUSTERMSG_TYPE_FAIL 3 /* Mark node xxx as failing */ | |
468 | #define CLUSTERMSG_TYPE_PUBLISH 4 /* Pub/Sub Publish propatagion */ | |
469 | ||
470 | /* Initially we don't know our "name", but we'll find it once we connect | |
471 | * to the first node, using the getsockname() function. Then we'll use this | |
472 | * address for all the next messages. */ | |
473 | typedef struct { | |
474 | char nodename[REDIS_CLUSTER_NAMELEN]; | |
475 | uint32_t ping_sent; | |
476 | uint32_t pong_received; | |
477 | char ip[16]; /* IP address last time it was seen */ | |
478 | uint16_t port; /* port last time it was seen */ | |
479 | uint16_t flags; | |
480 | uint32_t notused; /* for 64 bit alignment */ | |
481 | } clusterMsgDataGossip; | |
482 | ||
483 | typedef struct { | |
484 | char nodename[REDIS_CLUSTER_NAMELEN]; | |
485 | } clusterMsgDataFail; | |
486 | ||
487 | typedef struct { | |
488 | uint32_t channel_len; | |
489 | uint32_t message_len; | |
490 | unsigned char bulk_data[8]; /* defined as 8 just for alignment concerns. */ | |
491 | } clusterMsgDataPublish; | |
492 | ||
493 | union clusterMsgData { | |
494 | /* PING, MEET and PONG */ | |
495 | struct { | |
496 | /* Array of N clusterMsgDataGossip structures */ | |
497 | clusterMsgDataGossip gossip[1]; | |
498 | } ping; | |
499 | ||
500 | /* FAIL */ | |
501 | struct { | |
502 | clusterMsgDataFail about; | |
503 | } fail; | |
504 | ||
505 | /* PUBLISH */ | |
506 | struct { | |
507 | clusterMsgDataPublish msg; | |
508 | } publish; | |
509 | }; | |
510 | ||
511 | typedef struct { | |
512 | uint32_t totlen; /* Total length of this message */ | |
513 | uint16_t type; /* Message type */ | |
514 | uint16_t count; /* Only used for some kind of messages. */ | |
515 | char sender[REDIS_CLUSTER_NAMELEN]; /* Name of the sender node */ | |
516 | unsigned char myslots[REDIS_CLUSTER_SLOTS/8]; | |
517 | char slaveof[REDIS_CLUSTER_NAMELEN]; | |
518 | char configdigest[32]; | |
519 | uint16_t port; /* Sender TCP base port */ | |
520 | unsigned char state; /* Cluster state from the POV of the sender */ | |
521 | unsigned char notused[5]; /* Reserved for future use. For alignment. */ | |
522 | union clusterMsgData data; | |
523 | } clusterMsg; | |
524 | ||
525 | /*----------------------------------------------------------------------------- | |
526 | * Global server state | |
527 | *----------------------------------------------------------------------------*/ | |
528 | ||
529 | struct redisServer { | |
530 | /* General */ | |
531 | redisDb *db; | |
532 | dict *commands; /* Command table hahs table */ | |
533 | aeEventLoop *el; | |
534 | unsigned lruclock:22; /* Clock incrementing every minute, for LRU */ | |
535 | unsigned lruclock_padding:10; | |
536 | int shutdown_asap; /* SHUTDOWN needed ASAP */ | |
537 | int activerehashing; /* Incremental rehash in serverCron() */ | |
538 | char *requirepass; /* Pass for AUTH command, or NULL */ | |
539 | char *pidfile; /* PID file path */ | |
540 | int arch_bits; /* 32 or 64 depending on sizeof(long) */ | |
541 | /* Networking */ | |
542 | int port; /* TCP listening port */ | |
543 | char *bindaddr; /* Bind address or NULL */ | |
544 | char *unixsocket; /* UNIX socket path */ | |
545 | mode_t unixsocketperm; /* UNIX socket permission */ | |
546 | int ipfd; /* TCP socket file descriptor */ | |
547 | int sofd; /* Unix socket file descriptor */ | |
548 | int cfd; /* Cluster bus lisetning socket */ | |
549 | list *clients; /* List of active clients */ | |
550 | list *clients_to_close; /* Clients to close asynchronously */ | |
551 | list *slaves, *monitors; /* List of slaves and MONITORs */ | |
552 | redisClient *current_client; /* Current client, only used on crash report */ | |
553 | char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */ | |
554 | /* RDB / AOF loading information */ | |
555 | int loading; /* We are loading data from disk if true */ | |
556 | off_t loading_total_bytes; | |
557 | off_t loading_loaded_bytes; | |
558 | time_t loading_start_time; | |
559 | /* Fast pointers to often looked up command */ | |
560 | struct redisCommand *delCommand, *multiCommand; | |
561 | int cronloops; /* Number of times the cron function run */ | |
562 | time_t lastsave; /* Unix time of last save succeeede */ | |
563 | /* Fields used only for stats */ | |
564 | time_t stat_starttime; /* Server start time */ | |
565 | long long stat_numcommands; /* Number of processed commands */ | |
566 | long long stat_numconnections; /* Number of connections received */ | |
567 | long long stat_expiredkeys; /* Number of expired keys */ | |
568 | long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */ | |
569 | long long stat_keyspace_hits; /* Number of successful lookups of keys */ | |
570 | long long stat_keyspace_misses; /* Number of failed lookups of keys */ | |
571 | size_t stat_peak_memory; /* Max used memory record */ | |
572 | long long stat_fork_time; /* Time needed to perform latets fork() */ | |
573 | long long stat_rejected_conn; /* Clients rejected because of maxclients */ | |
574 | list *slowlog; /* SLOWLOG list of commands */ | |
575 | long long slowlog_entry_id; /* SLOWLOG current entry ID */ | |
576 | long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */ | |
577 | unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */ | |
578 | /* Configuration */ | |
579 | int verbosity; /* Loglevel in redis.conf */ | |
580 | int maxidletime; /* Client timeout in seconds */ | |
581 | size_t client_max_querybuf_len; /* Limit for client query buffer length */ | |
582 | int dbnum; /* Total number of configured DBs */ | |
583 | int daemonize; /* True if running as a daemon */ | |
584 | clientBufferLimitsConfig client_obuf_limits[REDIS_CLIENT_LIMIT_NUM_CLASSES]; | |
585 | /* AOF persistence */ | |
586 | int aof_state; /* REDIS_AOF_(ON|OFF|WAIT_REWRITE) */ | |
587 | int aof_fsync; /* Kind of fsync() policy */ | |
588 | char *aof_filename; /* Name of the AOF file */ | |
589 | int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */ | |
590 | int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */ | |
591 | off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */ | |
592 | off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */ | |
593 | off_t aof_current_size; /* AOF current size. */ | |
594 | int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */ | |
595 | pid_t aof_child_pid; /* PID if rewriting process */ | |
596 | sds aof_rewrite_buf; /* buffer taken by parent during oppend only rewrite */ | |
597 | sds aof_buf; /* AOF buffer, written before entering the event loop */ | |
598 | int aof_fd; /* File descriptor of currently selected AOF file */ | |
599 | int aof_selected_db; /* Currently selected DB in AOF */ | |
600 | time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */ | |
601 | time_t aof_last_fsync; /* UNIX time of last fsync() */ | |
602 | /* RDB persistence */ | |
603 | long long dirty; /* Changes to DB from the last save */ | |
604 | long long dirty_before_bgsave; /* Used to restore dirty on failed BGSAVE */ | |
605 | pid_t rdb_child_pid; /* PID of RDB saving child */ | |
606 | struct saveparam *saveparams; /* Save points array for RDB */ | |
607 | int saveparamslen; /* Number of saving points */ | |
608 | char *rdb_filename; /* Name of RDB file */ | |
609 | int rdb_compression; /* Use compression in RDB? */ | |
610 | /* Logging */ | |
611 | char *logfile; /* Path of log file */ | |
612 | int syslog_enabled; /* Is syslog enabled? */ | |
613 | char *syslog_ident; /* Syslog ident */ | |
614 | int syslog_facility; /* Syslog facility */ | |
615 | /* Slave specific fields */ | |
616 | char *masterauth; /* AUTH with this password with master */ | |
617 | char *masterhost; /* Hostname of master */ | |
618 | int masterport; /* Port of master */ | |
619 | int repl_ping_slave_period; /* Master pings the salve every N seconds */ | |
620 | int repl_timeout; /* Timeout after N seconds of master idle */ | |
621 | redisClient *master; /* Client that is master for this slave */ | |
622 | int repl_syncio_timeout; /* Timeout for synchronous I/O calls */ | |
623 | int repl_state; /* Replication status if the instance is a slave */ | |
624 | off_t repl_transfer_left; /* Bytes left reading .rdb */ | |
625 | int repl_transfer_s; /* Slave -> Master SYNC socket */ | |
626 | int repl_transfer_fd; /* Slave -> Master SYNC temp file descriptor */ | |
627 | char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */ | |
628 | time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */ | |
629 | int repl_serve_stale_data; /* Serve stale data when link is down? */ | |
630 | time_t repl_down_since; /* Unix time at which link with master went down */ | |
631 | /* Limits */ | |
632 | unsigned int maxclients; /* Max number of simultaneous clients */ | |
633 | unsigned long long maxmemory; /* Max number of memory bytes to use */ | |
634 | int maxmemory_policy; /* Policy for key evition */ | |
635 | int maxmemory_samples; /* Pricision of random sampling */ | |
636 | /* Blocked clients */ | |
637 | unsigned int bpop_blocked_clients; /* Number of clients blocked by lists */ | |
638 | list *unblocked_clients; /* list of clients to unblock before next loop */ | |
639 | /* Sort parameters - qsort_r() is only available under BSD so we | |
640 | * have to take this state global, in order to pass it to sortCompare() */ | |
641 | int sort_dontsort; | |
642 | int sort_desc; | |
643 | int sort_alpha; | |
644 | int sort_bypattern; | |
645 | /* Zip structure config, see redis.conf for more information */ | |
646 | size_t hash_max_zipmap_entries; | |
647 | size_t hash_max_zipmap_value; | |
648 | size_t list_max_ziplist_entries; | |
649 | size_t list_max_ziplist_value; | |
650 | size_t set_max_intset_entries; | |
651 | size_t zset_max_ziplist_entries; | |
652 | size_t zset_max_ziplist_value; | |
653 | time_t unixtime; /* Unix time sampled every second. */ | |
654 | /* Pubsub */ | |
655 | dict *pubsub_channels; /* Map channels to list of subscribed clients */ | |
656 | list *pubsub_patterns; /* A list of pubsub_patterns */ | |
657 | /* Cluster */ | |
658 | int cluster_enabled; /* Is cluster enabled? */ | |
659 | clusterState cluster; /* State of the cluster */ | |
660 | /* Scripting */ | |
661 | lua_State *lua; /* The Lua interpreter. We use just one for all clients */ | |
662 | redisClient *lua_client; /* The "fake client" to query Redis from Lua */ | |
663 | redisClient *lua_caller; /* The client running EVAL right now, or NULL */ | |
664 | dict *lua_scripts; /* A dictionary of SHA1 -> Lua scripts */ | |
665 | long long lua_time_limit; /* Script timeout in seconds */ | |
666 | long long lua_time_start; /* Start time of script */ | |
667 | int lua_write_dirty; /* True if a write command was called during the | |
668 | execution of the current script. */ | |
669 | int lua_random_dirty; /* True if a random command was called during the | |
670 | execution of the current script. */ | |
671 | int lua_timedout; /* True if we reached the time limit for script | |
672 | execution. */ | |
673 | int lua_kill; /* Kill the script if true. */ | |
674 | /* Assert & bug reportign */ | |
675 | char *assert_failed; | |
676 | char *assert_file; | |
677 | int assert_line; | |
678 | int bug_report_start; /* True if bug report header was already logged. */ | |
679 | }; | |
680 | ||
681 | typedef struct pubsubPattern { | |
682 | redisClient *client; | |
683 | robj *pattern; | |
684 | } pubsubPattern; | |
685 | ||
686 | typedef void redisCommandProc(redisClient *c); | |
687 | typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags); | |
688 | struct redisCommand { | |
689 | char *name; | |
690 | redisCommandProc *proc; | |
691 | int arity; | |
692 | char *sflags; /* Flags as string represenation, one char per flag. */ | |
693 | int flags; /* The actual flags, obtained from the 'sflags' field. */ | |
694 | /* Use a function to determine keys arguments in a command line. | |
695 | * Used for Redis Cluster redirect. */ | |
696 | redisGetKeysProc *getkeys_proc; | |
697 | /* What keys should be loaded in background when calling this command? */ | |
698 | int firstkey; /* The first argument that's a key (0 = no keys) */ | |
699 | int lastkey; /* THe last argument that's a key */ | |
700 | int keystep; /* The step between first and last key */ | |
701 | long long microseconds, calls; | |
702 | }; | |
703 | ||
704 | struct redisFunctionSym { | |
705 | char *name; | |
706 | unsigned long pointer; | |
707 | }; | |
708 | ||
709 | typedef struct _redisSortObject { | |
710 | robj *obj; | |
711 | union { | |
712 | double score; | |
713 | robj *cmpobj; | |
714 | } u; | |
715 | } redisSortObject; | |
716 | ||
717 | typedef struct _redisSortOperation { | |
718 | int type; | |
719 | robj *pattern; | |
720 | } redisSortOperation; | |
721 | ||
722 | /* Structure to hold list iteration abstraction. */ | |
723 | typedef struct { | |
724 | robj *subject; | |
725 | unsigned char encoding; | |
726 | unsigned char direction; /* Iteration direction */ | |
727 | unsigned char *zi; | |
728 | listNode *ln; | |
729 | } listTypeIterator; | |
730 | ||
731 | /* Structure for an entry while iterating over a list. */ | |
732 | typedef struct { | |
733 | listTypeIterator *li; | |
734 | unsigned char *zi; /* Entry in ziplist */ | |
735 | listNode *ln; /* Entry in linked list */ | |
736 | } listTypeEntry; | |
737 | ||
738 | /* Structure to hold set iteration abstraction. */ | |
739 | typedef struct { | |
740 | robj *subject; | |
741 | int encoding; | |
742 | int ii; /* intset iterator */ | |
743 | dictIterator *di; | |
744 | } setTypeIterator; | |
745 | ||
746 | /* Structure to hold hash iteration abstration. Note that iteration over | |
747 | * hashes involves both fields and values. Because it is possible that | |
748 | * not both are required, store pointers in the iterator to avoid | |
749 | * unnecessary memory allocation for fields/values. */ | |
750 | typedef struct { | |
751 | int encoding; | |
752 | unsigned char *zi; | |
753 | unsigned char *zk, *zv; | |
754 | unsigned int zklen, zvlen; | |
755 | ||
756 | dictIterator *di; | |
757 | dictEntry *de; | |
758 | } hashTypeIterator; | |
759 | ||
760 | #define REDIS_HASH_KEY 1 | |
761 | #define REDIS_HASH_VALUE 2 | |
762 | ||
763 | /*----------------------------------------------------------------------------- | |
764 | * Extern declarations | |
765 | *----------------------------------------------------------------------------*/ | |
766 | ||
767 | extern struct redisServer server; | |
768 | extern struct sharedObjectsStruct shared; | |
769 | extern dictType setDictType; | |
770 | extern dictType zsetDictType; | |
771 | extern dictType clusterNodesDictType; | |
772 | extern dictType dbDictType; | |
773 | extern double R_Zero, R_PosInf, R_NegInf, R_Nan; | |
774 | dictType hashDictType; | |
775 | ||
776 | /*----------------------------------------------------------------------------- | |
777 | * Functions prototypes | |
778 | *----------------------------------------------------------------------------*/ | |
779 | ||
780 | /* Utils */ | |
781 | long long ustime(void); | |
782 | long long mstime(void); | |
783 | ||
784 | /* networking.c -- Networking and Client related operations */ | |
785 | redisClient *createClient(int fd); | |
786 | void closeTimedoutClients(void); | |
787 | void freeClient(redisClient *c); | |
788 | void resetClient(redisClient *c); | |
789 | void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask); | |
790 | void addReply(redisClient *c, robj *obj); | |
791 | void *addDeferredMultiBulkLength(redisClient *c); | |
792 | void setDeferredMultiBulkLength(redisClient *c, void *node, long length); | |
793 | void addReplySds(redisClient *c, sds s); | |
794 | void processInputBuffer(redisClient *c); | |
795 | void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask); | |
796 | void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask); | |
797 | void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask); | |
798 | void addReplyBulk(redisClient *c, robj *obj); | |
799 | void addReplyBulkCString(redisClient *c, char *s); | |
800 | void addReplyBulkCBuffer(redisClient *c, void *p, size_t len); | |
801 | void addReplyBulkLongLong(redisClient *c, long long ll); | |
802 | void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask); | |
803 | void addReply(redisClient *c, robj *obj); | |
804 | void addReplySds(redisClient *c, sds s); | |
805 | void addReplyError(redisClient *c, char *err); | |
806 | void addReplyStatus(redisClient *c, char *status); | |
807 | void addReplyDouble(redisClient *c, double d); | |
808 | void addReplyLongLong(redisClient *c, long long ll); | |
809 | void addReplyMultiBulkLen(redisClient *c, long length); | |
810 | void copyClientOutputBuffer(redisClient *dst, redisClient *src); | |
811 | void *dupClientReplyValue(void *o); | |
812 | void getClientsMaxBuffers(unsigned long *longest_output_list, | |
813 | unsigned long *biggest_input_buffer); | |
814 | sds getClientInfoString(redisClient *client); | |
815 | sds getAllClientsInfoString(void); | |
816 | void rewriteClientCommandVector(redisClient *c, int argc, ...); | |
817 | void rewriteClientCommandArgument(redisClient *c, int i, robj *newval); | |
818 | unsigned long getClientOutputBufferMemoryUsage(redisClient *c); | |
819 | void freeClientsInAsyncFreeQueue(void); | |
820 | void asyncCloseClientOnOutputBufferLimitReached(redisClient *c); | |
821 | int getClientLimitClassByName(char *name); | |
822 | char *getClientLimitClassName(int class); | |
823 | ||
824 | #ifdef __GNUC__ | |
825 | void addReplyErrorFormat(redisClient *c, const char *fmt, ...) | |
826 | __attribute__((format(printf, 2, 3))); | |
827 | void addReplyStatusFormat(redisClient *c, const char *fmt, ...) | |
828 | __attribute__((format(printf, 2, 3))); | |
829 | #else | |
830 | void addReplyErrorFormat(redisClient *c, const char *fmt, ...); | |
831 | void addReplyStatusFormat(redisClient *c, const char *fmt, ...); | |
832 | #endif | |
833 | ||
834 | /* List data type */ | |
835 | void listTypeTryConversion(robj *subject, robj *value); | |
836 | void listTypePush(robj *subject, robj *value, int where); | |
837 | robj *listTypePop(robj *subject, int where); | |
838 | unsigned long listTypeLength(robj *subject); | |
839 | listTypeIterator *listTypeInitIterator(robj *subject, long index, unsigned char direction); | |
840 | void listTypeReleaseIterator(listTypeIterator *li); | |
841 | int listTypeNext(listTypeIterator *li, listTypeEntry *entry); | |
842 | robj *listTypeGet(listTypeEntry *entry); | |
843 | void listTypeInsert(listTypeEntry *entry, robj *value, int where); | |
844 | int listTypeEqual(listTypeEntry *entry, robj *o); | |
845 | void listTypeDelete(listTypeEntry *entry); | |
846 | void listTypeConvert(robj *subject, int enc); | |
847 | void unblockClientWaitingData(redisClient *c); | |
848 | int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele); | |
849 | void popGenericCommand(redisClient *c, int where); | |
850 | ||
851 | /* MULTI/EXEC/WATCH... */ | |
852 | void unwatchAllKeys(redisClient *c); | |
853 | void initClientMultiState(redisClient *c); | |
854 | void freeClientMultiState(redisClient *c); | |
855 | void queueMultiCommand(redisClient *c); | |
856 | void touchWatchedKey(redisDb *db, robj *key); | |
857 | void touchWatchedKeysOnFlush(int dbid); | |
858 | ||
859 | /* Redis object implementation */ | |
860 | void decrRefCount(void *o); | |
861 | void incrRefCount(robj *o); | |
862 | robj *resetRefCount(robj *obj); | |
863 | void freeStringObject(robj *o); | |
864 | void freeListObject(robj *o); | |
865 | void freeSetObject(robj *o); | |
866 | void freeZsetObject(robj *o); | |
867 | void freeHashObject(robj *o); | |
868 | robj *createObject(int type, void *ptr); | |
869 | robj *createStringObject(char *ptr, size_t len); | |
870 | robj *dupStringObject(robj *o); | |
871 | int isObjectRepresentableAsLongLong(robj *o, long long *llongval); | |
872 | robj *tryObjectEncoding(robj *o); | |
873 | robj *getDecodedObject(robj *o); | |
874 | size_t stringObjectLen(robj *o); | |
875 | robj *createStringObjectFromLongLong(long long value); | |
876 | robj *createStringObjectFromLongDouble(long double value); | |
877 | robj *createListObject(void); | |
878 | robj *createZiplistObject(void); | |
879 | robj *createSetObject(void); | |
880 | robj *createIntsetObject(void); | |
881 | robj *createHashObject(void); | |
882 | robj *createZsetObject(void); | |
883 | robj *createZsetZiplistObject(void); | |
884 | int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg); | |
885 | int checkType(redisClient *c, robj *o, int type); | |
886 | int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg); | |
887 | int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg); | |
888 | int getLongLongFromObject(robj *o, long long *target); | |
889 | int getLongDoubleFromObject(robj *o, long double *target); | |
890 | int getLongDoubleFromObjectOrReply(redisClient *c, robj *o, long double *target, const char *msg); | |
891 | char *strEncoding(int encoding); | |
892 | int compareStringObjects(robj *a, robj *b); | |
893 | int equalStringObjects(robj *a, robj *b); | |
894 | unsigned long estimateObjectIdleTime(robj *o); | |
895 | ||
896 | /* Synchronous I/O with timeout */ | |
897 | int syncWrite(int fd, char *ptr, ssize_t size, int timeout); | |
898 | int syncRead(int fd, char *ptr, ssize_t size, int timeout); | |
899 | int syncReadLine(int fd, char *ptr, ssize_t size, int timeout); | |
900 | ||
901 | /* Replication */ | |
902 | void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc); | |
903 | void replicationFeedMonitors(list *monitors, int dictid, robj **argv, int argc); | |
904 | void updateSlavesWaitingBgsave(int bgsaveerr); | |
905 | void replicationCron(void); | |
906 | ||
907 | /* Generic persistence functions */ | |
908 | void startLoading(FILE *fp); | |
909 | void loadingProgress(off_t pos); | |
910 | void stopLoading(void); | |
911 | ||
912 | /* RDB persistence */ | |
913 | #include "rdb.h" | |
914 | ||
915 | /* AOF persistence */ | |
916 | void flushAppendOnlyFile(int force); | |
917 | void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc); | |
918 | void aofRemoveTempFile(pid_t childpid); | |
919 | int rewriteAppendOnlyFileBackground(void); | |
920 | int loadAppendOnlyFile(char *filename); | |
921 | void stopAppendOnly(void); | |
922 | int startAppendOnly(void); | |
923 | void backgroundRewriteDoneHandler(int exitcode, int bysignal); | |
924 | ||
925 | /* Sorted sets data type */ | |
926 | ||
927 | /* Struct to hold a inclusive/exclusive range spec. */ | |
928 | typedef struct { | |
929 | double min, max; | |
930 | int minex, maxex; /* are min or max exclusive? */ | |
931 | } zrangespec; | |
932 | ||
933 | zskiplist *zslCreate(void); | |
934 | void zslFree(zskiplist *zsl); | |
935 | zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj); | |
936 | unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score); | |
937 | int zslDelete(zskiplist *zsl, double score, robj *obj); | |
938 | zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range); | |
939 | double zzlGetScore(unsigned char *sptr); | |
940 | void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); | |
941 | void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); | |
942 | unsigned int zsetLength(robj *zobj); | |
943 | void zsetConvert(robj *zobj, int encoding); | |
944 | ||
945 | /* Core functions */ | |
946 | void freeMemoryIfNeeded(void); | |
947 | int processCommand(redisClient *c); | |
948 | void setupSignalHandlers(void); | |
949 | struct redisCommand *lookupCommand(sds name); | |
950 | struct redisCommand *lookupCommandByCString(char *s); | |
951 | void call(redisClient *c, int flags); | |
952 | int prepareForShutdown(); | |
953 | void redisLog(int level, const char *fmt, ...); | |
954 | void redisLogRaw(int level, const char *msg); | |
955 | void usage(); | |
956 | void updateDictResizePolicy(void); | |
957 | int htNeedsResize(dict *dict); | |
958 | void oom(const char *msg); | |
959 | void populateCommandTable(void); | |
960 | void resetCommandTableStats(void); | |
961 | ||
962 | /* Set data type */ | |
963 | robj *setTypeCreate(robj *value); | |
964 | int setTypeAdd(robj *subject, robj *value); | |
965 | int setTypeRemove(robj *subject, robj *value); | |
966 | int setTypeIsMember(robj *subject, robj *value); | |
967 | setTypeIterator *setTypeInitIterator(robj *subject); | |
968 | void setTypeReleaseIterator(setTypeIterator *si); | |
969 | int setTypeNext(setTypeIterator *si, robj **objele, int64_t *llele); | |
970 | robj *setTypeNextObject(setTypeIterator *si); | |
971 | int setTypeRandomElement(robj *setobj, robj **objele, int64_t *llele); | |
972 | unsigned long setTypeSize(robj *subject); | |
973 | void setTypeConvert(robj *subject, int enc); | |
974 | ||
975 | /* Hash data type */ | |
976 | void convertToRealHash(robj *o); | |
977 | void hashTypeTryConversion(robj *subject, robj **argv, int start, int end); | |
978 | void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2); | |
979 | int hashTypeGet(robj *o, robj *key, robj **objval, unsigned char **v, unsigned int *vlen); | |
980 | robj *hashTypeGetObject(robj *o, robj *key); | |
981 | int hashTypeExists(robj *o, robj *key); | |
982 | int hashTypeSet(robj *o, robj *key, robj *value); | |
983 | int hashTypeDelete(robj *o, robj *key); | |
984 | unsigned long hashTypeLength(robj *o); | |
985 | hashTypeIterator *hashTypeInitIterator(robj *subject); | |
986 | void hashTypeReleaseIterator(hashTypeIterator *hi); | |
987 | int hashTypeNext(hashTypeIterator *hi); | |
988 | int hashTypeCurrent(hashTypeIterator *hi, int what, robj **objval, unsigned char **v, unsigned int *vlen); | |
989 | robj *hashTypeCurrentObject(hashTypeIterator *hi, int what); | |
990 | robj *hashTypeLookupWriteOrCreate(redisClient *c, robj *key); | |
991 | ||
992 | /* Pub / Sub */ | |
993 | int pubsubUnsubscribeAllChannels(redisClient *c, int notify); | |
994 | int pubsubUnsubscribeAllPatterns(redisClient *c, int notify); | |
995 | void freePubsubPattern(void *p); | |
996 | int listMatchPubsubPattern(void *a, void *b); | |
997 | int pubsubPublishMessage(robj *channel, robj *message); | |
998 | ||
999 | /* Configuration */ | |
1000 | void loadServerConfig(char *filename, char *options); | |
1001 | void appendServerSaveParams(time_t seconds, int changes); | |
1002 | void resetServerSaveParams(); | |
1003 | ||
1004 | /* db.c -- Keyspace access API */ | |
1005 | int removeExpire(redisDb *db, robj *key); | |
1006 | void propagateExpire(redisDb *db, robj *key); | |
1007 | int expireIfNeeded(redisDb *db, robj *key); | |
1008 | long long getExpire(redisDb *db, robj *key); | |
1009 | void setExpire(redisDb *db, robj *key, long long when); | |
1010 | robj *lookupKey(redisDb *db, robj *key); | |
1011 | robj *lookupKeyRead(redisDb *db, robj *key); | |
1012 | robj *lookupKeyWrite(redisDb *db, robj *key); | |
1013 | robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply); | |
1014 | robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply); | |
1015 | void dbAdd(redisDb *db, robj *key, robj *val); | |
1016 | void dbOverwrite(redisDb *db, robj *key, robj *val); | |
1017 | void setKey(redisDb *db, robj *key, robj *val); | |
1018 | int dbExists(redisDb *db, robj *key); | |
1019 | robj *dbRandomKey(redisDb *db); | |
1020 | int dbDelete(redisDb *db, robj *key); | |
1021 | long long emptyDb(); | |
1022 | int selectDb(redisClient *c, int id); | |
1023 | void signalModifiedKey(redisDb *db, robj *key); | |
1024 | void signalFlushedDb(int dbid); | |
1025 | unsigned int GetKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count); | |
1026 | ||
1027 | /* API to get key arguments from commands */ | |
1028 | #define REDIS_GETKEYS_ALL 0 | |
1029 | #define REDIS_GETKEYS_PRELOAD 1 | |
1030 | int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags); | |
1031 | void getKeysFreeResult(int *result); | |
1032 | int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); | |
1033 | int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); | |
1034 | int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags); | |
1035 | ||
1036 | /* Cluster */ | |
1037 | void clusterInit(void); | |
1038 | unsigned short crc16(const char *buf, int len); | |
1039 | unsigned int keyHashSlot(char *key, int keylen); | |
1040 | clusterNode *createClusterNode(char *nodename, int flags); | |
1041 | int clusterAddNode(clusterNode *node); | |
1042 | void clusterCron(void); | |
1043 | clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask); | |
1044 | void clusterPropagatePublish(robj *channel, robj *message); | |
1045 | ||
1046 | /* Scripting */ | |
1047 | void scriptingInit(void); | |
1048 | ||
1049 | /* Git SHA1 */ | |
1050 | char *redisGitSHA1(void); | |
1051 | char *redisGitDirty(void); | |
1052 | ||
1053 | /* Commands prototypes */ | |
1054 | void authCommand(redisClient *c); | |
1055 | void pingCommand(redisClient *c); | |
1056 | void echoCommand(redisClient *c); | |
1057 | void setCommand(redisClient *c); | |
1058 | void setnxCommand(redisClient *c); | |
1059 | void setexCommand(redisClient *c); | |
1060 | void psetexCommand(redisClient *c); | |
1061 | void getCommand(redisClient *c); | |
1062 | void delCommand(redisClient *c); | |
1063 | void existsCommand(redisClient *c); | |
1064 | void setbitCommand(redisClient *c); | |
1065 | void getbitCommand(redisClient *c); | |
1066 | void setrangeCommand(redisClient *c); | |
1067 | void getrangeCommand(redisClient *c); | |
1068 | void incrCommand(redisClient *c); | |
1069 | void decrCommand(redisClient *c); | |
1070 | void incrbyCommand(redisClient *c); | |
1071 | void decrbyCommand(redisClient *c); | |
1072 | void incrbyfloatCommand(redisClient *c); | |
1073 | void selectCommand(redisClient *c); | |
1074 | void randomkeyCommand(redisClient *c); | |
1075 | void keysCommand(redisClient *c); | |
1076 | void dbsizeCommand(redisClient *c); | |
1077 | void lastsaveCommand(redisClient *c); | |
1078 | void saveCommand(redisClient *c); | |
1079 | void bgsaveCommand(redisClient *c); | |
1080 | void bgrewriteaofCommand(redisClient *c); | |
1081 | void shutdownCommand(redisClient *c); | |
1082 | void moveCommand(redisClient *c); | |
1083 | void renameCommand(redisClient *c); | |
1084 | void renamenxCommand(redisClient *c); | |
1085 | void lpushCommand(redisClient *c); | |
1086 | void rpushCommand(redisClient *c); | |
1087 | void lpushxCommand(redisClient *c); | |
1088 | void rpushxCommand(redisClient *c); | |
1089 | void linsertCommand(redisClient *c); | |
1090 | void lpopCommand(redisClient *c); | |
1091 | void rpopCommand(redisClient *c); | |
1092 | void llenCommand(redisClient *c); | |
1093 | void lindexCommand(redisClient *c); | |
1094 | void lrangeCommand(redisClient *c); | |
1095 | void ltrimCommand(redisClient *c); | |
1096 | void typeCommand(redisClient *c); | |
1097 | void lsetCommand(redisClient *c); | |
1098 | void saddCommand(redisClient *c); | |
1099 | void sremCommand(redisClient *c); | |
1100 | void smoveCommand(redisClient *c); | |
1101 | void sismemberCommand(redisClient *c); | |
1102 | void scardCommand(redisClient *c); | |
1103 | void spopCommand(redisClient *c); | |
1104 | void srandmemberCommand(redisClient *c); | |
1105 | void sinterCommand(redisClient *c); | |
1106 | void sinterstoreCommand(redisClient *c); | |
1107 | void sunionCommand(redisClient *c); | |
1108 | void sunionstoreCommand(redisClient *c); | |
1109 | void sdiffCommand(redisClient *c); | |
1110 | void sdiffstoreCommand(redisClient *c); | |
1111 | void syncCommand(redisClient *c); | |
1112 | void flushdbCommand(redisClient *c); | |
1113 | void flushallCommand(redisClient *c); | |
1114 | void sortCommand(redisClient *c); | |
1115 | void lremCommand(redisClient *c); | |
1116 | void rpoplpushCommand(redisClient *c); | |
1117 | void infoCommand(redisClient *c); | |
1118 | void mgetCommand(redisClient *c); | |
1119 | void monitorCommand(redisClient *c); | |
1120 | void expireCommand(redisClient *c); | |
1121 | void expireatCommand(redisClient *c); | |
1122 | void pexpireCommand(redisClient *c); | |
1123 | void pexpireatCommand(redisClient *c); | |
1124 | void getsetCommand(redisClient *c); | |
1125 | void ttlCommand(redisClient *c); | |
1126 | void pttlCommand(redisClient *c); | |
1127 | void persistCommand(redisClient *c); | |
1128 | void slaveofCommand(redisClient *c); | |
1129 | void debugCommand(redisClient *c); | |
1130 | void msetCommand(redisClient *c); | |
1131 | void msetnxCommand(redisClient *c); | |
1132 | void zaddCommand(redisClient *c); | |
1133 | void zincrbyCommand(redisClient *c); | |
1134 | void zrangeCommand(redisClient *c); | |
1135 | void zrangebyscoreCommand(redisClient *c); | |
1136 | void zrevrangebyscoreCommand(redisClient *c); | |
1137 | void zcountCommand(redisClient *c); | |
1138 | void zrevrangeCommand(redisClient *c); | |
1139 | void zcardCommand(redisClient *c); | |
1140 | void zremCommand(redisClient *c); | |
1141 | void zscoreCommand(redisClient *c); | |
1142 | void zremrangebyscoreCommand(redisClient *c); | |
1143 | void multiCommand(redisClient *c); | |
1144 | void execCommand(redisClient *c); | |
1145 | void discardCommand(redisClient *c); | |
1146 | void blpopCommand(redisClient *c); | |
1147 | void brpopCommand(redisClient *c); | |
1148 | void brpoplpushCommand(redisClient *c); | |
1149 | void appendCommand(redisClient *c); | |
1150 | void strlenCommand(redisClient *c); | |
1151 | void zrankCommand(redisClient *c); | |
1152 | void zrevrankCommand(redisClient *c); | |
1153 | void hsetCommand(redisClient *c); | |
1154 | void hsetnxCommand(redisClient *c); | |
1155 | void hgetCommand(redisClient *c); | |
1156 | void hmsetCommand(redisClient *c); | |
1157 | void hmgetCommand(redisClient *c); | |
1158 | void hdelCommand(redisClient *c); | |
1159 | void hlenCommand(redisClient *c); | |
1160 | void zremrangebyrankCommand(redisClient *c); | |
1161 | void zunionstoreCommand(redisClient *c); | |
1162 | void zinterstoreCommand(redisClient *c); | |
1163 | void hkeysCommand(redisClient *c); | |
1164 | void hvalsCommand(redisClient *c); | |
1165 | void hgetallCommand(redisClient *c); | |
1166 | void hexistsCommand(redisClient *c); | |
1167 | void configCommand(redisClient *c); | |
1168 | void hincrbyCommand(redisClient *c); | |
1169 | void hincrbyfloatCommand(redisClient *c); | |
1170 | void subscribeCommand(redisClient *c); | |
1171 | void unsubscribeCommand(redisClient *c); | |
1172 | void psubscribeCommand(redisClient *c); | |
1173 | void punsubscribeCommand(redisClient *c); | |
1174 | void publishCommand(redisClient *c); | |
1175 | void watchCommand(redisClient *c); | |
1176 | void unwatchCommand(redisClient *c); | |
1177 | void clusterCommand(redisClient *c); | |
1178 | void restoreCommand(redisClient *c); | |
1179 | void migrateCommand(redisClient *c); | |
1180 | void askingCommand(redisClient *c); | |
1181 | void dumpCommand(redisClient *c); | |
1182 | void objectCommand(redisClient *c); | |
1183 | void clientCommand(redisClient *c); | |
1184 | void evalCommand(redisClient *c); | |
1185 | void evalShaCommand(redisClient *c); | |
1186 | void scriptCommand(redisClient *c); | |
1187 | ||
1188 | #if defined(__GNUC__) | |
1189 | void *calloc(size_t count, size_t size) __attribute__ ((deprecated)); | |
1190 | void free(void *ptr) __attribute__ ((deprecated)); | |
1191 | void *malloc(size_t size) __attribute__ ((deprecated)); | |
1192 | void *realloc(void *ptr, size_t size) __attribute__ ((deprecated)); | |
1193 | #endif | |
1194 | ||
1195 | /* Debugging stuff */ | |
1196 | void _redisAssertWithInfo(redisClient *c, robj *o, char *estr, char *file, int line); | |
1197 | void _redisAssert(char *estr, char *file, int line); | |
1198 | void _redisPanic(char *msg, char *file, int line); | |
1199 | void bugReportStart(void); | |
1200 | void redisLogObjectDebugInfo(robj *o); | |
1201 | void sigsegvHandler(int sig, siginfo_t *info, void *secret); | |
1202 | sds genRedisInfoString(char *section); | |
1203 | #endif |