· 6 years ago · Dec 24, 2019, 09:00 PM
1/*
2 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Redis nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef __REDIS_H
31#define __REDIS_H
32
33#include "fmacros.h"
34#include "config.h"
35#include "solarisfixes.h"
36#include "rio.h"
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <time.h>
42#include <limits.h>
43#include <unistd.h>
44#include <errno.h>
45#include <inttypes.h>
46#include <pthread.h>
47#include <syslog.h>
48#include <netinet/in.h>
49#include <lua.h>
50#include <signal.h>
51
52typedef long long mstime_t; /* millisecond time type. */
53
54#include "ae.h" /* Event driven programming library */
55#include "sds.h" /* Dynamic safe strings */
56#include "dict.h" /* Hash tables */
57#include "adlist.h" /* Linked lists */
58#include "zmalloc.h" /* total memory usage aware version of malloc/free */
59#include "anet.h" /* Networking the easy way */
60#include "ziplist.h" /* Compact list data structure */
61#include "intset.h" /* Compact integer set structure */
62#include "version.h" /* Version macro */
63#include "util.h" /* Misc functions useful in many places */
64#include "latency.h" /* Latency monitor API */
65#include "sparkline.h" /* ASCII graphs API */
66#include "quicklist.h" /* Lists are encoded as linked lists of
67 N-elements flat arrays */
68#include "rax.h" /* Radix tree */
69
70/* Following includes allow test functions to be called from Redis main() */
71#include "zipmap.h"
72#include "sha1.h"
73#include "endianconv.h"
74#include "crc64.h"
75
76/* Error codes */
77#define C_OK 0
78#define C_ERR -1
79
80/* Static server configuration */
81#define CONFIG_DEFAULT_DYNAMIC_HZ 1 /* Adapt hz to # of clients.*/
82#define CONFIG_DEFAULT_HZ 10 /* Time interrupt calls/sec. */
83#define CONFIG_MIN_HZ 1
84#define CONFIG_MAX_HZ 500
85#define MAX_CLIENTS_PER_CLOCK_TICK 200 /* HZ is adapted based on that. */
86#define CONFIG_DEFAULT_SERVER_PORT 6379 /* TCP port. */
87#define CONFIG_DEFAULT_TCP_BACKLOG 511 /* TCP listen backlog. */
88#define CONFIG_DEFAULT_CLIENT_TIMEOUT 0 /* Default client timeout: infinite */
89#define CONFIG_DEFAULT_DBNUM 16
90#define CONFIG_MAX_LINE 1024
91#define CRON_DBS_PER_CALL 16
92#define NET_MAX_WRITES_PER_EVENT (1024*64)
93#define PROTO_SHARED_SELECT_CMDS 10
94#define OBJ_SHARED_INTEGERS 10000
95#define OBJ_SHARED_BULKHDR_LEN 32
96#define LOG_MAX_LEN 1024 /* Default maximum length of syslog messages.*/
97#define AOF_REWRITE_PERC 100
98#define AOF_REWRITE_MIN_SIZE (64*1024*1024)
99#define AOF_REWRITE_ITEMS_PER_CMD 64
100#define AOF_READ_DIFF_INTERVAL_BYTES (1024*10)
101#define CONFIG_DEFAULT_SLOWLOG_LOG_SLOWER_THAN 10000
102#define CONFIG_DEFAULT_SLOWLOG_MAX_LEN 128
103#define CONFIG_DEFAULT_MAX_CLIENTS 10000
104#define CONFIG_AUTHPASS_MAX_LEN 512
105#define CONFIG_DEFAULT_SLAVE_PRIORITY 100
106#define CONFIG_DEFAULT_REPL_TIMEOUT 60
107#define CONFIG_DEFAULT_REPL_PING_SLAVE_PERIOD 10
108#define CONFIG_RUN_ID_SIZE 40
109#define RDB_EOF_MARK_SIZE 40
110#define CONFIG_DEFAULT_REPL_BACKLOG_SIZE (1024*1024) /* 1mb */
111#define CONFIG_DEFAULT_REPL_BACKLOG_TIME_LIMIT (60*60) /* 1 hour */
112#define CONFIG_REPL_BACKLOG_MIN_SIZE (1024*16) /* 16k */
113#define CONFIG_BGSAVE_RETRY_DELAY 5 /* Wait a few secs before trying again. */
114#define CONFIG_DEFAULT_PID_FILE "/var/run/redis.pid"
115#define CONFIG_DEFAULT_SYSLOG_IDENT "redis"
116#define CONFIG_DEFAULT_CLUSTER_CONFIG_FILE "nodes.conf"
117#define CONFIG_DEFAULT_CLUSTER_ANNOUNCE_IP NULL /* Auto detect. */
118#define CONFIG_DEFAULT_CLUSTER_ANNOUNCE_PORT 0 /* Use server.port */
119#define CONFIG_DEFAULT_CLUSTER_ANNOUNCE_BUS_PORT 0 /* Use +10000 offset. */
120#define CONFIG_DEFAULT_DAEMONIZE 0
121#define CONFIG_DEFAULT_UNIX_SOCKET_PERM 0
122#define CONFIG_DEFAULT_TCP_KEEPALIVE 300
123#define CONFIG_DEFAULT_PROTECTED_MODE 1
124#define CONFIG_DEFAULT_LOGFILE ""
125#define CONFIG_DEFAULT_SYSLOG_ENABLED 0
126#define CONFIG_DEFAULT_STOP_WRITES_ON_BGSAVE_ERROR 1
127#define CONFIG_DEFAULT_RDB_COMPRESSION 1
128#define CONFIG_DEFAULT_RDB_CHECKSUM 1
129#define CONFIG_DEFAULT_RDB_FILENAME "dump.rdb"
130#define CONFIG_DEFAULT_REPL_DISKLESS_SYNC 0
131#define CONFIG_DEFAULT_REPL_DISKLESS_SYNC_DELAY 5
132#define CONFIG_DEFAULT_SLAVE_SERVE_STALE_DATA 1
133#define CONFIG_DEFAULT_SLAVE_READ_ONLY 1
134#define CONFIG_DEFAULT_SLAVE_IGNORE_MAXMEMORY 1
135#define CONFIG_DEFAULT_SLAVE_ANNOUNCE_IP NULL
136#define CONFIG_DEFAULT_SLAVE_ANNOUNCE_PORT 0
137#define CONFIG_DEFAULT_REPL_DISABLE_TCP_NODELAY 0
138#define CONFIG_DEFAULT_MAXMEMORY 0
139#define CONFIG_DEFAULT_MAXMEMORY_SAMPLES 5
140#define CONFIG_DEFAULT_LFU_LOG_FACTOR 10
141#define CONFIG_DEFAULT_LFU_DECAY_TIME 1
142#define CONFIG_DEFAULT_AOF_FILENAME "appendonly.aof"
143#define CONFIG_DEFAULT_AOF_NO_FSYNC_ON_REWRITE 0
144#define CONFIG_DEFAULT_AOF_LOAD_TRUNCATED 1
145#define CONFIG_DEFAULT_AOF_USE_RDB_PREAMBLE 1
146#define CONFIG_DEFAULT_ACTIVE_REHASHING 1
147#define CONFIG_DEFAULT_AOF_REWRITE_INCREMENTAL_FSYNC 1
148#define CONFIG_DEFAULT_RDB_SAVE_INCREMENTAL_FSYNC 1
149#define CONFIG_DEFAULT_MIN_SLAVES_TO_WRITE 0
150#define CONFIG_DEFAULT_MIN_SLAVES_MAX_LAG 10
151#define NET_IP_STR_LEN 46 /* INET6_ADDRSTRLEN is 46, but we need to be sure */
152#define NET_PEER_ID_LEN (NET_IP_STR_LEN+32) /* Must be enough for ip:port */
153#define CONFIG_BINDADDR_MAX 16
154#define CONFIG_MIN_RESERVED_FDS 32
155#define CONFIG_DEFAULT_LATENCY_MONITOR_THRESHOLD 0
156#define CONFIG_DEFAULT_SLAVE_LAZY_FLUSH 0
157#define CONFIG_DEFAULT_LAZYFREE_LAZY_EVICTION 0
158#define CONFIG_DEFAULT_LAZYFREE_LAZY_EXPIRE 0
159#define CONFIG_DEFAULT_LAZYFREE_LAZY_SERVER_DEL 0
160#define CONFIG_DEFAULT_ALWAYS_SHOW_LOGO 0
161#define CONFIG_DEFAULT_ACTIVE_DEFRAG 0
162#define CONFIG_DEFAULT_DEFRAG_THRESHOLD_LOWER 10 /* don't defrag when fragmentation is below 10% */
163#define CONFIG_DEFAULT_DEFRAG_THRESHOLD_UPPER 100 /* maximum defrag force at 100% fragmentation */
164#define CONFIG_DEFAULT_DEFRAG_IGNORE_BYTES (100<<20) /* don't defrag if frag overhead is below 100mb */
165#define CONFIG_DEFAULT_DEFRAG_CYCLE_MIN 5 /* 5% CPU min (at lower threshold) */
166#define CONFIG_DEFAULT_DEFRAG_CYCLE_MAX 75 /* 75% CPU max (at upper threshold) */
167#define CONFIG_DEFAULT_DEFRAG_MAX_SCAN_FIELDS 1000 /* keys with more than 1000 fields will be processed separately */
168#define CONFIG_DEFAULT_PROTO_MAX_BULK_LEN (512ll*1024*1024) /* Bulk request max size */
169
170#define ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP 20 /* Loopkups per loop. */
171#define ACTIVE_EXPIRE_CYCLE_FAST_DURATION 1000 /* Microseconds */
172#define ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC 25 /* CPU max % for keys collection */
173#define ACTIVE_EXPIRE_CYCLE_SLOW 0
174#define ACTIVE_EXPIRE_CYCLE_FAST 1
175
176/* Instantaneous metrics tracking. */
177#define STATS_METRIC_SAMPLES 16 /* Number of samples per metric. */
178#define STATS_METRIC_COMMAND 0 /* Number of commands executed. */
179#define STATS_METRIC_NET_INPUT 1 /* Bytes read to network .*/
180#define STATS_METRIC_NET_OUTPUT 2 /* Bytes written to network. */
181#define STATS_METRIC_COUNT 3
182
183/* Protocol and I/O related defines */
184#define PROTO_MAX_QUERYBUF_LEN (1024*1024*1024) /* 1GB max query buffer. */
185#define PROTO_IOBUF_LEN (1024*16) /* Generic I/O buffer size */
186#define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */
187#define PROTO_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */
188#define PROTO_MBULK_BIG_ARG (1024*32)
189#define LONG_STR_SIZE 21 /* Bytes needed for long -> str + '\0' */
190#define REDIS_AUTOSYNC_BYTES (1024*1024*32) /* fdatasync every 32MB */
191
192#define LIMIT_PENDING_QUERYBUF (4*1024*1024) /* 4mb */
193
194/* When configuring the server eventloop, we setup it so that the total number
195 * of file descriptors we can handle are server.maxclients + RESERVED_FDS +
196 * a few more to stay safe. Since RESERVED_FDS defaults to 32, we add 96
197 * in order to make sure of not over provisioning more than 128 fds. */
198#define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)
199
200/* Hash table parameters */
201#define HASHTABLE_MIN_FILL 10 /* Minimal hash table fill 10% */
202
203/* Command flags. Please check the command table defined in the redis.c file
204 * for more information about the meaning of every flag. */
205#define CMD_WRITE (1<<0) /* "w" flag */
206#define CMD_READONLY (1<<1) /* "r" flag */
207#define CMD_DENYOOM (1<<2) /* "m" flag */
208#define CMD_MODULE (1<<3) /* Command exported by module. */
209#define CMD_ADMIN (1<<4) /* "a" flag */
210#define CMD_PUBSUB (1<<5) /* "p" flag */
211#define CMD_NOSCRIPT (1<<6) /* "s" flag */
212#define CMD_RANDOM (1<<7) /* "R" flag */
213#define CMD_SORT_FOR_SCRIPT (1<<8) /* "S" flag */
214#define CMD_LOADING (1<<9) /* "l" flag */
215#define CMD_STALE (1<<10) /* "t" flag */
216#define CMD_SKIP_MONITOR (1<<11) /* "M" flag */
217#define CMD_ASKING (1<<12) /* "k" flag */
218#define CMD_FAST (1<<13) /* "F" flag */
219#define CMD_MODULE_GETKEYS (1<<14) /* Use the modules getkeys interface. */
220#define CMD_MODULE_NO_CLUSTER (1<<15) /* Deny on Redis Cluster. */
221
222/* AOF states */
223#define AOF_OFF 0 /* AOF is off */
224#define AOF_ON 1 /* AOF is on */
225#define AOF_WAIT_REWRITE 2 /* AOF waits rewrite to start appending */
226
227/* Client flags */
228#define CLIENT_SLAVE (1<<0) /* This client is a slave server */
229#define CLIENT_MASTER (1<<1) /* This client is a master server */
230#define CLIENT_MONITOR (1<<2) /* This client is a slave monitor, see MONITOR */
231#define CLIENT_MULTI (1<<3) /* This client is in a MULTI context */
232#define CLIENT_BLOCKED (1<<4) /* The client is waiting in a blocking operation */
233#define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */
234#define CLIENT_CLOSE_AFTER_REPLY (1<<6) /* Close after writing entire reply. */
235#define CLIENT_UNBLOCKED (1<<7) /* This client was unblocked and is stored in
236 server.unblocked_clients */
237#define CLIENT_LUA (1<<8) /* This is a non connected client used by Lua */
238#define CLIENT_ASKING (1<<9) /* Client issued the ASKING command */
239#define CLIENT_CLOSE_ASAP (1<<10)/* Close this client ASAP */
240#define CLIENT_UNIX_SOCKET (1<<11) /* Client connected via Unix domain socket */
241#define CLIENT_DIRTY_EXEC (1<<12) /* EXEC will fail for errors while queueing */
242#define CLIENT_MASTER_FORCE_REPLY (1<<13) /* Queue replies even if is master */
243#define CLIENT_FORCE_AOF (1<<14) /* Force AOF propagation of current cmd. */
244#define CLIENT_FORCE_REPL (1<<15) /* Force replication of current cmd. */
245#define CLIENT_PRE_PSYNC (1<<16) /* Instance don't understand PSYNC. */
246#define CLIENT_READONLY (1<<17) /* Cluster client is in read-only state. */
247#define CLIENT_PUBSUB (1<<18) /* Client is in Pub/Sub mode. */
248#define CLIENT_PREVENT_AOF_PROP (1<<19) /* Don't propagate to AOF. */
249#define CLIENT_PREVENT_REPL_PROP (1<<20) /* Don't propagate to slaves. */
250#define CLIENT_PREVENT_PROP (CLIENT_PREVENT_AOF_PROP|CLIENT_PREVENT_REPL_PROP)
251#define CLIENT_PENDING_WRITE (1<<21) /* Client has output to send but a write
252 handler is yet not installed. */
253#define CLIENT_REPLY_OFF (1<<22) /* Don't send replies to client. */
254#define CLIENT_REPLY_SKIP_NEXT (1<<23) /* Set CLIENT_REPLY_SKIP for next cmd */
255#define CLIENT_REPLY_SKIP (1<<24) /* Don't send just this reply. */
256#define CLIENT_LUA_DEBUG (1<<25) /* Run EVAL in debug mode. */
257#define CLIENT_LUA_DEBUG_SYNC (1<<26) /* EVAL debugging without fork() */
258#define CLIENT_MODULE (1<<27) /* Non connected client used by some module. */
259#define CLIENT_PROTECTED (1<<28) /* Client should not be freed for now. */
260
261/* Client block type (btype field in client structure)
262 * if CLIENT_BLOCKED flag is set. */
263#define BLOCKED_NONE 0 /* Not blocked, no CLIENT_BLOCKED flag set. */
264#define BLOCKED_LIST 1 /* BLPOP & co. */
265#define BLOCKED_WAIT 2 /* WAIT for synchronous replication. */
266#define BLOCKED_MODULE 3 /* Blocked by a loadable module. */
267#define BLOCKED_STREAM 4 /* XREAD. */
268#define BLOCKED_ZSET 5 /* BZPOP et al. */
269#define BLOCKED_NUM 6 /* Number of blocked states. */
270
271/* Client request types */
272#define PROTO_REQ_INLINE 1
273#define PROTO_REQ_MULTIBULK 2
274
275/* Client classes for client limits, currently used only for
276 * the max-client-output-buffer limit implementation. */
277#define CLIENT_TYPE_NORMAL 0 /* Normal req-reply clients + MONITORs */
278#define CLIENT_TYPE_SLAVE 1 /* Slaves. */
279#define CLIENT_TYPE_PUBSUB 2 /* Clients subscribed to PubSub channels. */
280#define CLIENT_TYPE_MASTER 3 /* Master. */
281#define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output
282 buffer configuration. Just the first
283 three: normal, slave, pubsub. */
284
285/* Slave replication state. Used in server.repl_state for slaves to remember
286 * what to do next. */
287#define REPL_STATE_NONE 0 /* No active replication */
288#define REPL_STATE_CONNECT 1 /* Must connect to master */
289#define REPL_STATE_CONNECTING 2 /* Connecting to master */
290/* --- Handshake states, must be ordered --- */
291#define REPL_STATE_RECEIVE_PONG 3 /* Wait for PING reply */
292#define REPL_STATE_SEND_AUTH 4 /* Send AUTH to master */
293#define REPL_STATE_RECEIVE_AUTH 5 /* Wait for AUTH reply */
294#define REPL_STATE_SEND_PORT 6 /* Send REPLCONF listening-port */
295#define REPL_STATE_RECEIVE_PORT 7 /* Wait for REPLCONF reply */
296#define REPL_STATE_SEND_IP 8 /* Send REPLCONF ip-address */
297#define REPL_STATE_RECEIVE_IP 9 /* Wait for REPLCONF reply */
298#define REPL_STATE_SEND_CAPA 10 /* Send REPLCONF capa */
299#define REPL_STATE_RECEIVE_CAPA 11 /* Wait for REPLCONF reply */
300#define REPL_STATE_SEND_PSYNC 12 /* Send PSYNC */
301#define REPL_STATE_RECEIVE_PSYNC 13 /* Wait for PSYNC reply */
302/* --- End of handshake states --- */
303#define REPL_STATE_TRANSFER 14 /* Receiving .rdb from master */
304#define REPL_STATE_CONNECTED 15 /* Connected to master */
305
306/* State of slaves from the POV of the master. Used in client->replstate.
307 * In SEND_BULK and ONLINE state the slave receives new updates
308 * in its output queue. In the WAIT_BGSAVE states instead the server is waiting
309 * to start the next background saving in order to send updates to it. */
310#define SLAVE_STATE_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */
311#define SLAVE_STATE_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */
312#define SLAVE_STATE_SEND_BULK 8 /* Sending RDB file to slave. */
313#define SLAVE_STATE_ONLINE 9 /* RDB file transmitted, sending just updates. */
314
315/* Slave capabilities. */
316#define SLAVE_CAPA_NONE 0
317#define SLAVE_CAPA_EOF (1<<0) /* Can parse the RDB EOF streaming format. */
318#define SLAVE_CAPA_PSYNC2 (1<<1) /* Supports PSYNC2 protocol. */
319
320/* Synchronous read timeout - slave side */
321#define CONFIG_REPL_SYNCIO_TIMEOUT 5
322
323/* List related stuff */
324#define LIST_HEAD 0
325#define LIST_TAIL 1
326#define ZSET_MIN 0
327#define ZSET_MAX 1
328
329/* Sort operations */
330#define SORT_OP_GET 0
331
332/* Log levels */
333#define LL_DEBUG 0
334#define LL_VERBOSE 1
335#define LL_NOTICE 2
336#define LL_WARNING 3
337#define LL_RAW (1<<10) /* Modifier to log without timestamp */
338#define CONFIG_DEFAULT_VERBOSITY LL_NOTICE
339
340/* Supervision options */
341#define SUPERVISED_NONE 0
342#define SUPERVISED_AUTODETECT 1
343#define SUPERVISED_SYSTEMD 2
344#define SUPERVISED_UPSTART 3
345
346/* Anti-warning macro... */
347#define UNUSED(V) ((void) V)
348
349#define ZSKIPLIST_MAXLEVEL 64 /* Should be enough for 2^64 elements */
350#define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
351
352/* Append only defines */
353#define AOF_FSYNC_NO 0
354#define AOF_FSYNC_ALWAYS 1
355#define AOF_FSYNC_EVERYSEC 2
356#define CONFIG_DEFAULT_AOF_FSYNC AOF_FSYNC_EVERYSEC
357
358/* Zipped structures related defaults */
359#define OBJ_HASH_MAX_ZIPLIST_ENTRIES 512
360#define OBJ_HASH_MAX_ZIPLIST_VALUE 64
361#define OBJ_SET_MAX_INTSET_ENTRIES 512
362#define OBJ_ZSET_MAX_ZIPLIST_ENTRIES 128
363#define OBJ_ZSET_MAX_ZIPLIST_VALUE 64
364#define OBJ_STREAM_NODE_MAX_BYTES 4096
365#define OBJ_STREAM_NODE_MAX_ENTRIES 100
366
367/* List defaults */
368#define OBJ_LIST_MAX_ZIPLIST_SIZE -2
369#define OBJ_LIST_COMPRESS_DEPTH 0
370
371/* HyperLogLog defines */
372#define CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES 3000
373
374/* Sets operations codes */
375#define SET_OP_UNION 0
376#define SET_OP_DIFF 1
377#define SET_OP_INTER 2
378
379/* Redis maxmemory strategies. Instead of using just incremental number
380 * for this defines, we use a set of flags so that testing for certain
381 * properties common to multiple policies is faster. */
382#define MAXMEMORY_FLAG_LRU (1<<0)
383#define MAXMEMORY_FLAG_LFU (1<<1)
384#define MAXMEMORY_FLAG_ALLKEYS (1<<2)
385#define MAXMEMORY_FLAG_NO_SHARED_INTEGERS \
386 (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU)
387
388#define MAXMEMORY_VOLATILE_LRU ((0<<8)|MAXMEMORY_FLAG_LRU)
389#define MAXMEMORY_VOLATILE_LFU ((1<<8)|MAXMEMORY_FLAG_LFU)
390#define MAXMEMORY_VOLATILE_TTL (2<<8)
391#define MAXMEMORY_VOLATILE_RANDOM (3<<8)
392#define MAXMEMORY_ALLKEYS_LRU ((4<<8)|MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_ALLKEYS)
393#define MAXMEMORY_ALLKEYS_LFU ((5<<8)|MAXMEMORY_FLAG_LFU|MAXMEMORY_FLAG_ALLKEYS)
394#define MAXMEMORY_ALLKEYS_RANDOM ((6<<8)|MAXMEMORY_FLAG_ALLKEYS)
395#define MAXMEMORY_NO_EVICTION (7<<8)
396
397#define CONFIG_DEFAULT_MAXMEMORY_POLICY MAXMEMORY_NO_EVICTION
398
399/* Scripting */
400#define LUA_SCRIPT_TIME_LIMIT 5000 /* milliseconds */
401
402/* Units */
403#define UNIT_SECONDS 0
404#define UNIT_MILLISECONDS 1
405
406/* SHUTDOWN flags */
407#define SHUTDOWN_NOFLAGS 0 /* No flags. */
408#define SHUTDOWN_SAVE 1 /* Force SAVE on SHUTDOWN even if no save
409 points are configured. */
410#define SHUTDOWN_NOSAVE 2 /* Don't SAVE on SHUTDOWN. */
411
412/* Command call flags, see call() function */
413#define CMD_CALL_NONE 0
414#define CMD_CALL_SLOWLOG (1<<0)
415#define CMD_CALL_STATS (1<<1)
416#define CMD_CALL_PROPAGATE_AOF (1<<2)
417#define CMD_CALL_PROPAGATE_REPL (1<<3)
418#define CMD_CALL_PROPAGATE (CMD_CALL_PROPAGATE_AOF|CMD_CALL_PROPAGATE_REPL)
419#define CMD_CALL_FULL (CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_PROPAGATE)
420
421/* Command propagation flags, see propagate() function */
422#define PROPAGATE_NONE 0
423#define PROPAGATE_AOF 1
424#define PROPAGATE_REPL 2
425
426/* RDB active child save type. */
427#define RDB_CHILD_TYPE_NONE 0
428#define RDB_CHILD_TYPE_DISK 1 /* RDB is written to disk. */
429#define RDB_CHILD_TYPE_SOCKET 2 /* RDB is written to slave socket. */
430
431/* Keyspace changes notification classes. Every class is associated with a
432 * character for configuration purposes. */
433#define NOTIFY_KEYSPACE (1<<0) /* K */
434#define NOTIFY_KEYEVENT (1<<1) /* E */
435#define NOTIFY_GENERIC (1<<2) /* g */
436#define NOTIFY_STRING (1<<3) /* $ */
437#define NOTIFY_LIST (1<<4) /* l */
438#define NOTIFY_SET (1<<5) /* s */
439#define NOTIFY_HASH (1<<6) /* h */
440#define NOTIFY_ZSET (1<<7) /* z */
441#define NOTIFY_EXPIRED (1<<8) /* x */
442#define NOTIFY_EVICTED (1<<9) /* e */
443#define NOTIFY_STREAM (1<<10) /* t */
444#define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED | NOTIFY_STREAM) /* A flag */
445
446/* Get the first bind addr or NULL */
447#define NET_FIRST_BIND_ADDR (server.bindaddr_count ? server.bindaddr[0] : NULL)
448
449/* Using the following macro you can run code inside serverCron() with the
450 * specified period, specified in milliseconds.
451 * The actual resolution depends on server.hz. */
452#define run_with_period(_ms_) if ((_ms_ <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz))))
453
454/* We can print the stacktrace, so our assert is defined this way: */
455#define serverAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_serverAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1)))
456#define serverAssert(_e) ((_e)?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),_exit(1)))
457#define serverPanic(...) _serverPanic(__FILE__,__LINE__,__VA_ARGS__),_exit(1)
458
459/*-----------------------------------------------------------------------------
460 * Data types
461 *----------------------------------------------------------------------------*/
462
463/* A redis object, that is a type able to hold a string / list / set */
464
465/* The actual Redis Object */
466#define OBJ_STRING 0 /* String object. */
467#define OBJ_LIST 1 /* List object. */
468#define OBJ_SET 2 /* Set object. */
469#define OBJ_ZSET 3 /* Sorted set object. */
470#define OBJ_HASH 4 /* Hash object. */
471
472/* The "module" object type is a special one that signals that the object
473 * is one directly managed by a Redis module. In this case the value points
474 * to a moduleValue struct, which contains the object value (which is only
475 * handled by the module itself) and the RedisModuleType struct which lists
476 * function pointers in order to serialize, deserialize, AOF-rewrite and
477 * free the object.
478 *
479 * Inside the RDB file, module types are encoded as OBJ_MODULE followed
480 * by a 64 bit module type ID, which has a 54 bits module-specific signature
481 * in order to dispatch the loading to the right module, plus a 10 bits
482 * encoding version. */
483#define OBJ_MODULE 5 /* Module object. */
484#define OBJ_STREAM 6 /* Stream object. */
485
486/* Extract encver / signature from a module type ID. */
487#define REDISMODULE_TYPE_ENCVER_BITS 10
488#define REDISMODULE_TYPE_ENCVER_MASK ((1<<REDISMODULE_TYPE_ENCVER_BITS)-1)
489#define REDISMODULE_TYPE_ENCVER(id) (id & REDISMODULE_TYPE_ENCVER_MASK)
490#define REDISMODULE_TYPE_SIGN(id) ((id & ~((uint64_t)REDISMODULE_TYPE_ENCVER_MASK)) >>REDISMODULE_TYPE_ENCVER_BITS)
491
492struct RedisModule;
493struct RedisModuleIO;
494struct RedisModuleDigest;
495struct RedisModuleCtx;
496struct redisObject;
497
498/* Each module type implementation should export a set of methods in order
499 * to serialize and deserialize the value in the RDB file, rewrite the AOF
500 * log, create the digest for "DEBUG DIGEST", and free the value when a key
501 * is deleted. */
502typedef void *(*moduleTypeLoadFunc)(struct RedisModuleIO *io, int encver);
503typedef void (*moduleTypeSaveFunc)(struct RedisModuleIO *io, void *value);
504typedef void (*moduleTypeRewriteFunc)(struct RedisModuleIO *io, struct redisObject *key, void *value);
505typedef void (*moduleTypeDigestFunc)(struct RedisModuleDigest *digest, void *value);
506typedef size_t (*moduleTypeMemUsageFunc)(const void *value);
507typedef void (*moduleTypeFreeFunc)(void *value);
508
509/* The module type, which is referenced in each value of a given type, defines
510 * the methods and links to the module exporting the type. */
511typedef struct RedisModuleType {
512 uint64_t id; /* Higher 54 bits of type ID + 10 lower bits of encoding ver. */
513 struct RedisModule *module;
514 moduleTypeLoadFunc rdb_load;
515 moduleTypeSaveFunc rdb_save;
516 moduleTypeRewriteFunc aof_rewrite;
517 moduleTypeMemUsageFunc mem_usage;
518 moduleTypeDigestFunc digest;
519 moduleTypeFreeFunc free;
520 char name[10]; /* 9 bytes name + null term. Charset: A-Z a-z 0-9 _- */
521} moduleType;
522
523/* In Redis objects 'robj' structures of type OBJ_MODULE, the value pointer
524 * is set to the following structure, referencing the moduleType structure
525 * in order to work with the value, and at the same time providing a raw
526 * pointer to the value, as created by the module commands operating with
527 * the module type.
528 *
529 * So for example in order to free such a value, it is possible to use
530 * the following code:
531 *
532 * if (robj->type == OBJ_MODULE) {
533 * moduleValue *mt = robj->ptr;
534 * mt->type->free(mt->value);
535 * zfree(mt); // We need to release this in-the-middle struct as well.
536 * }
537 */
538typedef struct moduleValue {
539 moduleType *type;
540 void *value;
541} moduleValue;
542
543/* This is a wrapper for the 'rio' streams used inside rdb.c in Redis, so that
544 * the user does not have to take the total count of the written bytes nor
545 * to care about error conditions. */
546typedef struct RedisModuleIO {
547 size_t bytes; /* Bytes read / written so far. */
548 rio *rio; /* Rio stream. */
549 moduleType *type; /* Module type doing the operation. */
550 int error; /* True if error condition happened. */
551 int ver; /* Module serialization version: 1 (old),
552 * 2 (current version with opcodes annotation). */
553 struct RedisModuleCtx *ctx; /* Optional context, see RM_GetContextFromIO()*/
554 struct redisObject *key; /* Optional name of key processed */
555} RedisModuleIO;
556
557/* Macro to initialize an IO context. Note that the 'ver' field is populated
558 * inside rdb.c according to the version of the value to load. */
559#define moduleInitIOContext(iovar,mtype,rioptr,keyptr) do { \
560 iovar.rio = rioptr; \
561 iovar.type = mtype; \
562 iovar.bytes = 0; \
563 iovar.error = 0; \
564 iovar.ver = 0; \
565 iovar.key = keyptr; \
566 iovar.ctx = NULL; \
567} while(0);
568
569/* This is a structure used to export DEBUG DIGEST capabilities to Redis
570 * modules. We want to capture both the ordered and unordered elements of
571 * a data structure, so that a digest can be created in a way that correctly
572 * reflects the values. See the DEBUG DIGEST command implementation for more
573 * background. */
574typedef struct RedisModuleDigest {
575 unsigned char o[20]; /* Ordered elements. */
576 unsigned char x[20]; /* Xored elements. */
577} RedisModuleDigest;
578
579/* Just start with a digest composed of all zero bytes. */
580#define moduleInitDigestContext(mdvar) do { \
581 memset(mdvar.o,0,sizeof(mdvar.o)); \
582 memset(mdvar.x,0,sizeof(mdvar.x)); \
583} while(0);
584
585/* Objects encoding. Some kind of objects like Strings and Hashes can be
586 * internally represented in multiple ways. The 'encoding' field of the object
587 * is set to one of this fields for this object. */
588#define OBJ_ENCODING_RAW 0 /* Raw representation */
589#define OBJ_ENCODING_INT 1 /* Encoded as integer */
590#define OBJ_ENCODING_HT 2 /* Encoded as hash table */
591#define OBJ_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
592#define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
593#define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
594#define OBJ_ENCODING_INTSET 6 /* Encoded as intset */
595#define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
596#define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding */
597#define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */
598#define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */
599
600#define LRU_BITS 24
601#define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */
602#define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */
603
604#define OBJ_SHARED_REFCOUNT INT_MAX
605typedef struct redisObject {
606 unsigned type:4;
607 unsigned encoding:4;
608 unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
609 * LFU data (least significant 8 bits frequency
610 * and most significant 16 bits access time). */
611 int refcount;
612 void *ptr;
613} robj;
614
615/* Macro used to initialize a Redis object allocated on the stack.
616 * Note that this macro is taken near the structure definition to make sure
617 * we'll update it when the structure is changed, to avoid bugs like
618 * bug #85 introduced exactly in this way. */
619#define initStaticStringObject(_var,_ptr) do { \
620 _var.refcount = 1; \
621 _var.type = OBJ_STRING; \
622 _var.encoding = OBJ_ENCODING_RAW; \
623 _var.ptr = _ptr; \
624} while(0)
625
626struct evictionPoolEntry; /* Defined in evict.c */
627
628/* This structure is used in order to represent the output buffer of a client,
629 * which is actually a linked list of blocks like that, that is: client->reply. */
630typedef struct clientReplyBlock {
631 size_t size, used;
632 char buf[];
633} clientReplyBlock;
634
635/* Redis database representation. There are multiple databases identified
636 * by integers from 0 (the default database) up to the max configured
637 * database. The database number is the 'id' field in the structure. */
638typedef struct redisDb {
639 dict *dict; /* The keyspace for this DB */
640 dict *expires; /* Timeout of keys with a timeout set */
641 dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
642 dict *ready_keys; /* Blocked keys that received a PUSH */
643 dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
644 int id; /* Database ID */
645 long long avg_ttl; /* Average TTL, just for stats */
646 list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */
647} redisDb;
648
649/* Client MULTI/EXEC state */
650typedef struct multiCmd {
651 robj **argv;
652 int argc;
653 struct redisCommand *cmd;
654} multiCmd;
655
656typedef struct multiState {
657 multiCmd *commands; /* Array of MULTI commands */
658 int count; /* Total number of MULTI commands */
659 int cmd_flags; /* The accumulated command flags OR-ed together.
660 So if at least a command has a given flag, it
661 will be set in this field. */
662 int minreplicas; /* MINREPLICAS for synchronous replication */
663 time_t minreplicas_timeout; /* MINREPLICAS timeout as unixtime. */
664} multiState;
665
666/* This structure holds the blocking operation state for a client.
667 * The fields used depend on client->btype. */
668typedef struct blockingState {
669 /* Generic fields. */
670 mstime_t timeout; /* Blocking operation timeout. If UNIX current time
671 * is > timeout then the operation timed out. */
672
673 /* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM */
674 dict *keys; /* The keys we are waiting to terminate a blocking
675 * operation such as BLPOP or XREAD. Or NULL. */
676 robj *target; /* The key that should receive the element,
677 * for BRPOPLPUSH. */
678
679 /* BLOCK_STREAM */
680 size_t xread_count; /* XREAD COUNT option. */
681 robj *xread_group; /* XREADGROUP group name. */
682 robj *xread_consumer; /* XREADGROUP consumer name. */
683 mstime_t xread_retry_time, xread_retry_ttl;
684 int xread_group_noack;
685
686 /* BLOCKED_WAIT */
687 int numreplicas; /* Number of replicas we are waiting for ACK. */
688 long long reploffset; /* Replication offset to reach. */
689
690 /* BLOCKED_MODULE */
691 void *module_blocked_handle; /* RedisModuleBlockedClient structure.
692 which is opaque for the Redis core, only
693 handled in module.c. */
694} blockingState;
695
696/* The following structure represents a node in the server.ready_keys list,
697 * where we accumulate all the keys that had clients blocked with a blocking
698 * operation such as B[LR]POP, but received new data in the context of the
699 * last executed command.
700 *
701 * After the execution of every command or script, we run this list to check
702 * if as a result we should serve data to clients blocked, unblocking them.
703 * Note that server.ready_keys will not have duplicates as there dictionary
704 * also called ready_keys in every structure representing a Redis database,
705 * where we make sure to remember if a given key was already added in the
706 * server.ready_keys list. */
707typedef struct readyList {
708 redisDb *db;
709 robj *key;
710} readyList;
711
712/* With multiplexing we need to take per-client state.
713 * Clients are taken in a linked list. */
714typedef struct client {
715 uint64_t id; /* Client incremental unique ID. */
716 int fd; /* Client socket. */
717 redisDb *db; /* Pointer to currently SELECTed DB. */
718 robj *name; /* As set by CLIENT SETNAME. */
719 sds querybuf; /* Buffer we use to accumulate client queries. */
720 size_t qb_pos; /* The position we have read in querybuf. */
721 sds pending_querybuf; /* If this client is flagged as master, this buffer
722 represents the yet not applied portion of the
723 replication stream that we are receiving from
724 the master. */
725 size_t querybuf_peak; /* Recent (100ms or more) peak of querybuf size. */
726 int argc; /* Num of arguments of current command. */
727 robj **argv; /* Arguments of current command. */
728 struct redisCommand *cmd, *lastcmd; /* Last command executed. */
729 int reqtype; /* Request protocol type: PROTO_REQ_* */
730 int multibulklen; /* Number of multi bulk arguments left to read. */
731 long bulklen; /* Length of bulk argument in multi bulk request. */
732 list *reply; /* List of reply objects to send to the client. */
733 unsigned long long reply_bytes; /* Tot bytes of objects in reply list. */
734 size_t sentlen; /* Amount of bytes already sent in the current
735 buffer or object being sent. */
736 time_t ctime; /* Client creation time. */
737 time_t lastinteraction; /* Time of the last interaction, used for timeout */
738 time_t obuf_soft_limit_reached_time;
739 int flags; /* Client flags: CLIENT_* macros. */
740 int authenticated; /* When requirepass is non-NULL. */
741 int replstate; /* Replication state if this is a slave. */
742 int repl_put_online_on_ack; /* Install slave write handler on ACK. */
743 int repldbfd; /* Replication DB file descriptor. */
744 off_t repldboff; /* Replication DB file offset. */
745 off_t repldbsize; /* Replication DB file size. */
746 sds replpreamble; /* Replication DB preamble. */
747 long long read_reploff; /* Read replication offset if this is a master. */
748 long long reploff; /* Applied replication offset if this is a master. */
749 long long repl_ack_off; /* Replication ack offset, if this is a slave. */
750 long long repl_ack_time;/* Replication ack time, if this is a slave. */
751 long long psync_initial_offset; /* FULLRESYNC reply offset other slaves
752 copying this slave output buffer
753 should use. */
754 char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */
755 int slave_listening_port; /* As configured with: SLAVECONF listening-port */
756 char slave_ip[NET_IP_STR_LEN]; /* Optionally given by REPLCONF ip-address */
757 int slave_capa; /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */
758 multiState mstate; /* MULTI/EXEC state */
759 int btype; /* Type of blocking op if CLIENT_BLOCKED. */
760 blockingState bpop; /* blocking state */
761 long long woff; /* Last write global replication offset. */
762 list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */
763 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
764 list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */
765 sds peerid; /* Cached peer ID. */
766 listNode *client_list_node; /* list node in client list */
767
768 /* Response buffer */
769 int bufpos;
770 char buf[PROTO_REPLY_CHUNK_BYTES];
771} client;
772
773struct saveparam {
774 time_t seconds;
775 int changes;
776};
777
778struct moduleLoadQueueEntry {
779 sds path;
780 int argc;
781 robj **argv;
782};
783
784struct sharedObjectsStruct {
785 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *cnegone, *pong, *space,
786 *colon, *nullbulk, *nullmultibulk, *queued,
787 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
788 *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *bgsaveerr,
789 *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr,
790 *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
791 *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,
792 *rpop, *lpop, *lpush, *rpoplpush, *zpopmin, *zpopmax, *emptyscan,
793 *select[PROTO_SHARED_SELECT_CMDS],
794 *integers[OBJ_SHARED_INTEGERS],
795 *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
796 *bulkhdr[OBJ_SHARED_BULKHDR_LEN]; /* "$<value>\r\n" */
797 sds minstring, maxstring;
798};
799
800/* ZSETs use a specialized version of Skiplists */
801typedef struct zskiplistNode {
802 sds ele;
803 double score;
804 struct zskiplistNode *backward;
805 struct zskiplistLevel {
806 struct zskiplistNode *forward;
807 unsigned long span;
808 } level[];
809} zskiplistNode;
810
811typedef struct zskiplist {
812 struct zskiplistNode *header, *tail;
813 unsigned long length;
814 int level;
815} zskiplist;
816
817typedef struct zset {
818 dict *dict;
819 zskiplist *zsl;
820} zset;
821
822typedef struct clientBufferLimitsConfig {
823 unsigned long long hard_limit_bytes;
824 unsigned long long soft_limit_bytes;
825 time_t soft_limit_seconds;
826} clientBufferLimitsConfig;
827
828extern clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT];
829
830/* The redisOp structure defines a Redis Operation, that is an instance of
831 * a command with an argument vector, database ID, propagation target
832 * (PROPAGATE_*), and command pointer.
833 *
834 * Currently only used to additionally propagate more commands to AOF/Replication
835 * after the propagation of the executed command. */
836typedef struct redisOp {
837 robj **argv;
838 int argc, dbid, target;
839 struct redisCommand *cmd;
840} redisOp;
841
842/* Defines an array of Redis operations. There is an API to add to this
843 * structure in a easy way.
844 *
845 * redisOpArrayInit();
846 * redisOpArrayAppend();
847 * redisOpArrayFree();
848 */
849typedef struct redisOpArray {
850 redisOp *ops;
851 int numops;
852} redisOpArray;
853
854/* This structure is returned by the getMemoryOverheadData() function in
855 * order to return memory overhead information. */
856struct redisMemOverhead {
857 size_t peak_allocated;
858 size_t total_allocated;
859 size_t startup_allocated;
860 size_t repl_backlog;
861 size_t clients_slaves;
862 size_t clients_normal;
863 size_t aof_buffer;
864 size_t lua_caches;
865 size_t overhead_total;
866 size_t dataset;
867 size_t total_keys;
868 size_t bytes_per_key;
869 float dataset_perc;
870 float peak_perc;
871 float total_frag;
872 ssize_t total_frag_bytes;
873 float allocator_frag;
874 ssize_t allocator_frag_bytes;
875 float allocator_rss;
876 ssize_t allocator_rss_bytes;
877 float rss_extra;
878 size_t rss_extra_bytes;
879 size_t num_dbs;
880 struct {
881 size_t dbid;
882 size_t overhead_ht_main;
883 size_t overhead_ht_expires;
884 } *db;
885};
886
887/* This structure can be optionally passed to RDB save/load functions in
888 * order to implement additional functionalities, by storing and loading
889 * metadata to the RDB file.
890 *
891 * Currently the only use is to select a DB at load time, useful in
892 * replication in order to make sure that chained slaves (slaves of slaves)
893 * select the correct DB and are able to accept the stream coming from the
894 * top-level master. */
895typedef struct rdbSaveInfo {
896 /* Used saving and loading. */
897 int repl_stream_db; /* DB to select in server.master client. */
898
899 /* Used only loading. */
900 int repl_id_is_set; /* True if repl_id field is set. */
901 char repl_id[CONFIG_RUN_ID_SIZE+1]; /* Replication ID. */
902 long long repl_offset; /* Replication offset. */
903} rdbSaveInfo;
904
905#define RDB_SAVE_INFO_INIT {-1,0,"000000000000000000000000000000",-1}
906
907struct malloc_stats {
908 size_t zmalloc_used;
909 size_t process_rss;
910 size_t allocator_allocated;
911 size_t allocator_active;
912 size_t allocator_resident;
913};
914
915/*-----------------------------------------------------------------------------
916 * Global server state
917 *----------------------------------------------------------------------------*/
918
919struct clusterState;
920
921/* AIX defines hz to __hz, we don't use this define and in order to allow
922 * Redis build on AIX we need to undef it. */
923#ifdef _AIX
924#undef hz
925#endif
926
927#define CHILD_INFO_MAGIC 0xC17DDA7A12345678LL
928#define CHILD_INFO_TYPE_RDB 0
929#define CHILD_INFO_TYPE_AOF 1
930
931struct redisServer {
932 /* General */
933 pid_t pid; /* Main process pid. */
934 char *configfile; /* Absolute config file path, or NULL */
935 char *executable; /* Absolute executable file path. */
936 char **exec_argv; /* Executable argv vector (copy). */
937 int dynamic_hz; /* Change hz value depending on # of clients. */
938 int config_hz; /* Configured HZ value. May be different than
939 the actual 'hz' field value if dynamic-hz
940 is enabled. */
941 int hz; /* serverCron() calls frequency in hertz */
942 redisDb *db;
943 dict *commands; /* Command table */
944 dict *orig_commands; /* Command table before command renaming. */
945 aeEventLoop *el;
946 unsigned int lruclock; /* Clock for LRU eviction */
947 int shutdown_asap; /* SHUTDOWN needed ASAP */
948 int activerehashing; /* Incremental rehash in serverCron() */
949 int active_defrag_running; /* Active defragmentation running (holds current scan aggressiveness) */
950 char *requirepass; /* Pass for AUTH command, or NULL */
951 char *pidfile; /* PID file path */
952 int arch_bits; /* 32 or 64 depending on sizeof(long) */
953 int cronloops; /* Number of times the cron function run */
954 char runid[CONFIG_RUN_ID_SIZE+1]; /* ID always different at every exec. */
955 int sentinel_mode; /* True if this instance is a Sentinel. */
956 size_t initial_memory_usage; /* Bytes used after initialization. */
957 int always_show_logo; /* Show logo even for non-stdout logging. */
958 /* Modules */
959 dict *moduleapi; /* Exported core APIs dictionary for modules. */
960 dict *sharedapi; /* Like moduleapi but containing the APIs that
961 modules share with each other. */
962 list *loadmodule_queue; /* List of modules to load at startup. */
963 int module_blocked_pipe[2]; /* Pipe used to awake the event loop if a
964 client blocked on a module command needs
965 to be processed. */
966 /* Networking */
967 int port; /* TCP listening port */
968 int tcp_backlog; /* TCP listen() backlog */
969 char *bindaddr[CONFIG_BINDADDR_MAX]; /* Addresses we should bind to */
970 int bindaddr_count; /* Number of addresses in server.bindaddr[] */
971 char *unixsocket; /* UNIX socket path */
972 mode_t unixsocketperm; /* UNIX socket permission */
973 int ipfd[CONFIG_BINDADDR_MAX]; /* TCP socket file descriptors */
974 int ipfd_count; /* Used slots in ipfd[] */
975 int sofd; /* Unix socket file descriptor */
976 int cfd[CONFIG_BINDADDR_MAX];/* Cluster bus listening socket */
977 int cfd_count; /* Used slots in cfd[] */
978 list *clients; /* List of active clients */
979 list *clients_to_close; /* Clients to close asynchronously */
980 list *clients_pending_write; /* There is to write or install handler. */
981 list *slaves, *monitors; /* List of slaves and MONITORs */
982 client *current_client; /* Current client, only used on crash report */
983 rax *clients_index; /* Active clients dictionary by client ID. */
984 int clients_paused; /* True if clients are currently paused */
985 mstime_t clients_pause_end_time; /* Time when we undo clients_paused */
986 char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */
987 dict *migrate_cached_sockets;/* MIGRATE cached sockets */
988 uint64_t next_client_id; /* Next client unique ID. Incremental. */
989 int protected_mode; /* Don't accept external connections. */
990 /* RDB / AOF loading information */
991 int loading; /* We are loading data from disk if true */
992 off_t loading_total_bytes;
993 off_t loading_loaded_bytes;
994 time_t loading_start_time;
995 off_t loading_process_events_interval_bytes;
996 /* Fast pointers to often looked up command */
997 struct redisCommand *delCommand, *multiCommand, *lpushCommand,
998 *lpopCommand, *rpopCommand, *zpopminCommand,
999 *zpopmaxCommand, *sremCommand, *execCommand,
1000 *expireCommand, *pexpireCommand, *xclaimCommand,
1001 *xgroupCommand;
1002 /* Fields used only for stats */
1003 time_t stat_starttime; /* Server start time */
1004 long long stat_numcommands; /* Number of processed commands */
1005 long long stat_numconnections; /* Number of connections received */
1006 long long stat_expiredkeys; /* Number of expired keys */
1007 double stat_expired_stale_perc; /* Percentage of keys probably expired */
1008 long long stat_expired_time_cap_reached_count; /* Early expire cylce stops.*/
1009 long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */
1010 long long stat_keyspace_hits; /* Number of successful lookups of keys */
1011 long long stat_keyspace_misses; /* Number of failed lookups of keys */
1012 long long stat_active_defrag_hits; /* number of allocations moved */
1013 long long stat_active_defrag_misses; /* number of allocations scanned but not moved */
1014 long long stat_active_defrag_key_hits; /* number of keys with moved allocations */
1015 long long stat_active_defrag_key_misses;/* number of keys scanned and not moved */
1016 long long stat_active_defrag_scanned; /* number of dictEntries scanned */
1017 size_t stat_peak_memory; /* Max used memory record */
1018 long long stat_fork_time; /* Time needed to perform latest fork() */
1019 double stat_fork_rate; /* Fork rate in GB/sec. */
1020 long long stat_rejected_conn; /* Clients rejected because of maxclients */
1021 long long stat_sync_full; /* Number of full resyncs with slaves. */
1022 long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */
1023 long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */
1024 list *slowlog; /* SLOWLOG list of commands */
1025 long long slowlog_entry_id; /* SLOWLOG current entry ID */
1026 long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
1027 unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */
1028 struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */
1029 long long stat_net_input_bytes; /* Bytes read from network. */
1030 long long stat_net_output_bytes; /* Bytes written to network. */
1031 size_t stat_rdb_cow_bytes; /* Copy on write bytes during RDB saving. */
1032 size_t stat_aof_cow_bytes; /* Copy on write bytes during AOF rewrite. */
1033 /* The following two are used to track instantaneous metrics, like
1034 * number of operations per second, network traffic. */
1035 struct {
1036 long long last_sample_time; /* Timestamp of last sample in ms */
1037 long long last_sample_count;/* Count in last sample */
1038 long long samples[STATS_METRIC_SAMPLES];
1039 int idx;
1040 } inst_metric[STATS_METRIC_COUNT];
1041 /* Configuration */
1042 int verbosity; /* Loglevel in redis.conf */
1043 int maxidletime; /* Client timeout in seconds */
1044 int tcpkeepalive; /* Set SO_KEEPALIVE if non-zero. */
1045 int active_expire_enabled; /* Can be disabled for testing purposes. */
1046 int active_defrag_enabled;
1047 size_t active_defrag_ignore_bytes; /* minimum amount of fragmentation waste to start active defrag */
1048 int active_defrag_threshold_lower; /* minimum percentage of fragmentation to start active defrag */
1049 int active_defrag_threshold_upper; /* maximum percentage of fragmentation at which we use maximum effort */
1050 int active_defrag_cycle_min; /* minimal effort for defrag in CPU percentage */
1051 int active_defrag_cycle_max; /* maximal effort for defrag in CPU percentage */
1052 unsigned long active_defrag_max_scan_fields; /* maximum number of fields of set/hash/zset/list to process from within the main dict scan */
1053 size_t client_max_querybuf_len; /* Limit for client query buffer length */
1054 int dbnum; /* Total number of configured DBs */
1055 int supervised; /* 1 if supervised, 0 otherwise. */
1056 int supervised_mode; /* See SUPERVISED_* */
1057 int daemonize; /* True if running as a daemon */
1058 clientBufferLimitsConfig client_obuf_limits[CLIENT_TYPE_OBUF_COUNT];
1059 /* AOF persistence */
1060 int aof_state; /* AOF_(ON|OFF|WAIT_REWRITE) */
1061 int aof_fsync; /* Kind of fsync() policy */
1062 char *aof_filename; /* Name of the AOF file */
1063 int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */
1064 int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */
1065 off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */
1066 off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */
1067 off_t aof_current_size; /* AOF current size. */
1068 off_t aof_fsync_offset; /* AOF offset which is already synced to disk. */
1069 int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */
1070 pid_t aof_child_pid; /* PID if rewriting process */
1071 list *aof_rewrite_buf_blocks; /* Hold changes during an AOF rewrite. */
1072 sds aof_buf; /* AOF buffer, written before entering the event loop */
1073 int aof_fd; /* File descriptor of currently selected AOF file */
1074 int aof_selected_db; /* Currently selected DB in AOF */
1075 time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */
1076 time_t aof_last_fsync; /* UNIX time of last fsync() */
1077 time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */
1078 time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */
1079 int aof_lastbgrewrite_status; /* C_OK or C_ERR */
1080 unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */
1081 int aof_rewrite_incremental_fsync;/* fsync incrementally while aof rewriting? */
1082 int rdb_save_incremental_fsync; /* fsync incrementally while rdb saving? */
1083 int aof_last_write_status; /* C_OK or C_ERR */
1084 int aof_last_write_errno; /* Valid if aof_last_write_status is ERR */
1085 int aof_load_truncated; /* Don't stop on unexpected AOF EOF. */
1086 int aof_use_rdb_preamble; /* Use RDB preamble on AOF rewrites. */
1087 /* AOF pipes used to communicate between parent and child during rewrite. */
1088 int aof_pipe_write_data_to_child;
1089 int aof_pipe_read_data_from_parent;
1090 int aof_pipe_write_ack_to_parent;
1091 int aof_pipe_read_ack_from_child;
1092 int aof_pipe_write_ack_to_child;
1093 int aof_pipe_read_ack_from_parent;
1094 int aof_stop_sending_diff; /* If true stop sending accumulated diffs
1095 to child process. */
1096 sds aof_child_diff; /* AOF diff accumulator child side. */
1097 /* RDB persistence */
1098 long long dirty; /* Changes to DB from the last save */
1099 long long dirty_before_bgsave; /* Used to restore dirty on failed BGSAVE */
1100 pid_t rdb_child_pid; /* PID of RDB saving child */
1101 struct saveparam *saveparams; /* Save points array for RDB */
1102 int saveparamslen; /* Number of saving points */
1103 char *rdb_filename; /* Name of RDB file */
1104 int rdb_compression; /* Use compression in RDB? */
1105 int rdb_checksum; /* Use RDB checksum? */
1106 time_t lastsave; /* Unix time of last successful save */
1107 time_t lastbgsave_try; /* Unix time of last attempted bgsave */
1108 time_t rdb_save_time_last; /* Time used by last RDB save run. */
1109 time_t rdb_save_time_start; /* Current RDB save start time. */
1110 int rdb_bgsave_scheduled; /* BGSAVE when possible if true. */
1111 int rdb_child_type; /* Type of save by active child. */
1112 int lastbgsave_status; /* C_OK or C_ERR */
1113 int stop_writes_on_bgsave_err; /* Don't allow writes if can't BGSAVE */
1114 int rdb_pipe_write_result_to_parent; /* RDB pipes used to return the state */
1115 int rdb_pipe_read_result_from_child; /* of each slave in diskless SYNC. */
1116 /* Pipe and data structures for child -> parent info sharing. */
1117 int child_info_pipe[2]; /* Pipe used to write the child_info_data. */
1118 struct {
1119 int process_type; /* AOF or RDB child? */
1120 size_t cow_size; /* Copy on write size. */
1121 unsigned long long magic; /* Magic value to make sure data is valid. */
1122 } child_info_data;
1123 /* Propagation of commands in AOF / replication */
1124 redisOpArray also_propagate; /* Additional command to propagate. */
1125 /* Logging */
1126 char *logfile; /* Path of log file */
1127 int syslog_enabled; /* Is syslog enabled? */
1128 char *syslog_ident; /* Syslog ident */
1129 int syslog_facility; /* Syslog facility */
1130 /* Replication (master) */
1131 char replid[CONFIG_RUN_ID_SIZE+1]; /* My current replication ID. */
1132 char replid2[CONFIG_RUN_ID_SIZE+1]; /* replid inherited from master*/
1133 long long master_repl_offset; /* My current replication offset */
1134 long long second_replid_offset; /* Accept offsets up to this for replid2. */
1135 int slaveseldb; /* Last SELECTed DB in replication output */
1136 int repl_ping_slave_period; /* Master pings the slave every N seconds */
1137 char *repl_backlog; /* Replication backlog for partial syncs */
1138 long long repl_backlog_size; /* Backlog circular buffer size */
1139 long long repl_backlog_histlen; /* Backlog actual data length */
1140 long long repl_backlog_idx; /* Backlog circular buffer current offset,
1141 that is the next byte will'll write to.*/
1142 long long repl_backlog_off; /* Replication "master offset" of first
1143 byte in the replication backlog buffer.*/
1144 time_t repl_backlog_time_limit; /* Time without slaves after the backlog
1145 gets released. */
1146 time_t repl_no_slaves_since; /* We have no slaves since that time.
1147 Only valid if server.slaves len is 0. */
1148 int repl_min_slaves_to_write; /* Min number of slaves to write. */
1149 int repl_min_slaves_max_lag; /* Max lag of <count> slaves to write. */
1150 int repl_good_slaves_count; /* Number of slaves with lag <= max_lag. */
1151 int repl_diskless_sync; /* Send RDB to slaves sockets directly. */
1152 int repl_diskless_sync_delay; /* Delay to start a diskless repl BGSAVE. */
1153 /* Replication (slave) */
1154 char *masterauth; /* AUTH with this password with master */
1155 char *masterhost; /* Hostname of master */
1156 int masterport; /* Port of master */
1157 int repl_timeout; /* Timeout after N seconds of master idle */
1158 client *master; /* Client that is master for this slave */
1159 client *cached_master; /* Cached master to be reused for PSYNC. */
1160 int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
1161 int repl_state; /* Replication status if the instance is a slave */
1162 off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
1163 off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
1164 off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
1165 int repl_transfer_s; /* Slave -> Master SYNC socket */
1166 int repl_transfer_fd; /* Slave -> Master SYNC temp file descriptor */
1167 char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
1168 time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */
1169 int repl_serve_stale_data; /* Serve stale data when link is down? */
1170 int repl_slave_ro; /* Slave is read only? */
1171 int repl_slave_ignore_maxmemory; /* If true slaves do not evict. */
1172 time_t repl_down_since; /* Unix time at which link with master went down */
1173 int repl_disable_tcp_nodelay; /* Disable TCP_NODELAY after SYNC? */
1174 int slave_priority; /* Reported in INFO and used by Sentinel. */
1175 int slave_announce_port; /* Give the master this listening port. */
1176 char *slave_announce_ip; /* Give the master this ip address. */
1177 /* The following two fields is where we store master PSYNC replid/offset
1178 * while the PSYNC is in progress. At the end we'll copy the fields into
1179 * the server->master client structure. */
1180 char master_replid[CONFIG_RUN_ID_SIZE+1]; /* Master PSYNC runid. */
1181 long long master_initial_offset; /* Master PSYNC offset. */
1182 int repl_slave_lazy_flush; /* Lazy FLUSHALL before loading DB? */
1183 /* Replication script cache. */
1184 dict *repl_scriptcache_dict; /* SHA1 all slaves are aware of. */
1185 list *repl_scriptcache_fifo; /* First in, first out LRU eviction. */
1186 unsigned int repl_scriptcache_size; /* Max number of elements. */
1187 /* Synchronous replication. */
1188 list *clients_waiting_acks; /* Clients waiting in WAIT command. */
1189 int get_ack_from_slaves; /* If true we send REPLCONF GETACK. */
1190 /* Limits */
1191 unsigned int maxclients; /* Max number of simultaneous clients */
1192 unsigned long long maxmemory; /* Max number of memory bytes to use */
1193 int maxmemory_policy; /* Policy for key eviction */
1194 int maxmemory_samples; /* Pricision of random sampling */
1195 int lfu_log_factor; /* LFU logarithmic counter factor. */
1196 int lfu_decay_time; /* LFU counter decay factor. */
1197 long long proto_max_bulk_len; /* Protocol bulk length maximum size. */
1198 /* Blocked clients */
1199 unsigned int blocked_clients; /* # of clients executing a blocking cmd.*/
1200 unsigned int blocked_clients_by_type[BLOCKED_NUM];
1201 list *unblocked_clients; /* list of clients to unblock before next loop */
1202 list *ready_keys; /* List of readyList structures for BLPOP & co */
1203 /* Sort parameters - qsort_r() is only available under BSD so we
1204 * have to take this state global, in order to pass it to sortCompare() */
1205 int sort_desc;
1206 int sort_alpha;
1207 int sort_bypattern;
1208 int sort_store;
1209 /* Zip structure config, see redis.conf for more information */
1210 size_t hash_max_ziplist_entries;
1211 size_t hash_max_ziplist_value;
1212 size_t set_max_intset_entries;
1213 size_t zset_max_ziplist_entries;
1214 size_t zset_max_ziplist_value;
1215 size_t hll_sparse_max_bytes;
1216 size_t stream_node_max_bytes;
1217 int64_t stream_node_max_entries;
1218 /* List parameters */
1219 int list_max_ziplist_size;
1220 int list_compress_depth;
1221 /* time cache */
1222 time_t unixtime; /* Unix time sampled every cron cycle. */
1223 time_t timezone; /* Cached timezone. As set by tzset(). */
1224 int daylight_active; /* Currently in daylight saving time. */
1225 long long mstime; /* Like 'unixtime' but with milliseconds resolution. */
1226 /* Pubsub */
1227 dict *pubsub_channels; /* Map channels to list of subscribed clients */
1228 list *pubsub_patterns; /* A list of pubsub_patterns */
1229 int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an
1230 xor of NOTIFY_... flags. */
1231 /* Cluster */
1232 int cluster_enabled; /* Is cluster enabled? */
1233 mstime_t cluster_node_timeout; /* Cluster node timeout. */
1234 char *cluster_configfile; /* Cluster auto-generated config file name. */
1235 struct clusterState *cluster; /* State of the cluster */
1236 int cluster_migration_barrier; /* Cluster replicas migration barrier. */
1237 int cluster_slave_validity_factor; /* Slave max data age for failover. */
1238 int cluster_require_full_coverage; /* If true, put the cluster down if
1239 there is at least an uncovered slot.*/
1240 int cluster_slave_no_failover; /* Prevent slave from starting a failover
1241 if the master is in failure state. */
1242 char *cluster_announce_ip; /* IP address to announce on cluster bus. */
1243 int cluster_announce_port; /* base port to announce on cluster bus. */
1244 int cluster_announce_bus_port; /* bus port to announce on cluster bus. */
1245 int cluster_module_flags; /* Set of flags that Redis modules are able
1246 to set in order to suppress certain
1247 native Redis Cluster features. Check the
1248 REDISMODULE_CLUSTER_FLAG_*. */
1249 /* Scripting */
1250 lua_State *lua; /* The Lua interpreter. We use just one for all clients */
1251 client *lua_client; /* The "fake client" to query Redis from Lua */
1252 client *lua_caller; /* The client running EVAL right now, or NULL */
1253 dict *lua_scripts; /* A dictionary of SHA1 -> Lua scripts */
1254 unsigned long long lua_scripts_mem; /* Cached scripts' memory + oh */
1255 mstime_t lua_time_limit; /* Script timeout in milliseconds */
1256 mstime_t lua_time_start; /* Start time of script, milliseconds time */
1257 int lua_write_dirty; /* True if a write command was called during the
1258 execution of the current script. */
1259 int lua_random_dirty; /* True if a random command was called during the
1260 execution of the current script. */
1261 int lua_replicate_commands; /* True if we are doing single commands repl. */
1262 int lua_multi_emitted;/* True if we already proagated MULTI. */
1263 int lua_repl; /* Script replication flags for redis.set_repl(). */
1264 int lua_timedout; /* True if we reached the time limit for script
1265 execution. */
1266 int lua_kill; /* Kill the script if true. */
1267 int lua_always_replicate_commands; /* Default replication type. */
1268 /* Lazy free */
1269 int lazyfree_lazy_eviction;
1270 int lazyfree_lazy_expire;
1271 int lazyfree_lazy_server_del;
1272 /* Latency monitor */
1273 long long latency_monitor_threshold;
1274 dict *latency_events;
1275 /* Assert & bug reporting */
1276 const char *assert_failed;
1277 const char *assert_file;
1278 int assert_line;
1279 int bug_report_start; /* True if bug report header was already logged. */
1280 int watchdog_period; /* Software watchdog period in ms. 0 = off */
1281 /* System hardware info */
1282 size_t system_memory_size; /* Total memory in system as reported by OS */
1283
1284 /* Mutexes used to protect atomic variables when atomic builtins are
1285 * not available. */
1286 pthread_mutex_t lruclock_mutex;
1287 pthread_mutex_t next_client_id_mutex;
1288 pthread_mutex_t unixtime_mutex;
1289};
1290
1291typedef struct pubsubPattern {
1292 client *client;
1293 robj *pattern;
1294} pubsubPattern;
1295
1296typedef void redisCommandProc(client *c);
1297typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1298struct redisCommand {
1299 char *name;
1300 redisCommandProc *proc;
1301 int arity;
1302 char *sflags; /* Flags as string representation, one char per flag. */
1303 int flags; /* The actual flags, obtained from the 'sflags' field. */
1304 /* Use a function to determine keys arguments in a command line.
1305 * Used for Redis Cluster redirect. */
1306 redisGetKeysProc *getkeys_proc;
1307 /* What keys should be loaded in background when calling this command? */
1308 int firstkey; /* The first argument that's a key (0 = no keys) */
1309 int lastkey; /* The last argument that's a key */
1310 int keystep; /* The step between first and last key */
1311 long long microseconds, calls;
1312};
1313
1314struct redisFunctionSym {
1315 char *name;
1316 unsigned long pointer;
1317};
1318
1319typedef struct _redisSortObject {
1320 robj *obj;
1321 union {
1322 double score;
1323 robj *cmpobj;
1324 } u;
1325} redisSortObject;
1326
1327typedef struct _redisSortOperation {
1328 int type;
1329 robj *pattern;
1330} redisSortOperation;
1331
1332/* Structure to hold list iteration abstraction. */
1333typedef struct {
1334 robj *subject;
1335 unsigned char encoding;
1336 unsigned char direction; /* Iteration direction */
1337 quicklistIter *iter;
1338} listTypeIterator;
1339
1340/* Structure for an entry while iterating over a list. */
1341typedef struct {
1342 listTypeIterator *li;
1343 quicklistEntry entry; /* Entry in quicklist */
1344} listTypeEntry;
1345
1346/* Structure to hold set iteration abstraction. */
1347typedef struct {
1348 robj *subject;
1349 int encoding;
1350 int ii; /* intset iterator */
1351 dictIterator *di;
1352} setTypeIterator;
1353
1354/* Structure to hold hash iteration abstraction. Note that iteration over
1355 * hashes involves both fields and values. Because it is possible that
1356 * not both are required, store pointers in the iterator to avoid
1357 * unnecessary memory allocation for fields/values. */
1358typedef struct {
1359 robj *subject;
1360 int encoding;
1361
1362 unsigned char *fptr, *vptr;
1363
1364 dictIterator *di;
1365 dictEntry *de;
1366} hashTypeIterator;
1367
1368#include "stream.h" /* Stream data type header file. */
1369
1370#define OBJ_HASH_KEY 1
1371#define OBJ_HASH_VALUE 2
1372
1373/*-----------------------------------------------------------------------------
1374 * Extern declarations
1375 *----------------------------------------------------------------------------*/
1376
1377extern struct redisServer server;
1378extern struct sharedObjectsStruct shared;
1379extern dictType objectKeyPointerValueDictType;
1380extern dictType objectKeyHeapPointerValueDictType;
1381extern dictType setDictType;
1382extern dictType zsetDictType;
1383extern dictType clusterNodesDictType;
1384extern dictType clusterNodesBlackListDictType;
1385extern dictType dbDictType;
1386extern dictType shaScriptObjectDictType;
1387extern double R_Zero, R_PosInf, R_NegInf, R_Nan;
1388extern dictType hashDictType;
1389extern dictType replScriptCacheDictType;
1390extern dictType keyptrDictType;
1391extern dictType modulesDictType;
1392
1393/*-----------------------------------------------------------------------------
1394 * Functions prototypes
1395 *----------------------------------------------------------------------------*/
1396
1397/* Modules */
1398void moduleInitModulesSystem(void);
1399int moduleLoad(const char *path, void **argv, int argc);
1400void moduleLoadFromQueue(void);
1401int *moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1402moduleType *moduleTypeLookupModuleByID(uint64_t id);
1403void moduleTypeNameByID(char *name, uint64_t moduleid);
1404void moduleFreeContext(struct RedisModuleCtx *ctx);
1405void unblockClientFromModule(client *c);
1406void moduleHandleBlockedClients(void);
1407void moduleBlockedClientTimedOut(client *c);
1408void moduleBlockedClientPipeReadable(aeEventLoop *el, int fd, void *privdata, int mask);
1409size_t moduleCount(void);
1410void moduleAcquireGIL(void);
1411void moduleReleaseGIL(void);
1412void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);
1413void moduleCallCommandFilters(client *c);
1414
1415/* Utils */
1416long long ustime(void);
1417long long mstime(void);
1418void getRandomHexChars(char *p, size_t len);
1419void getRandomBytes(unsigned char *p, size_t len);
1420uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
1421void exitFromChild(int retcode);
1422size_t redisPopcount(void *s, long count);
1423void redisSetProcTitle(char *title);
1424
1425/* networking.c -- Networking and Client related operations */
1426client *createClient(int fd);
1427void closeTimedoutClients(void);
1428void freeClient(client *c);
1429void freeClientAsync(client *c);
1430void resetClient(client *c);
1431void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask);
1432void *addDeferredMultiBulkLength(client *c);
1433void setDeferredMultiBulkLength(client *c, void *node, long length);
1434void processInputBuffer(client *c);
1435void processInputBufferAndReplicate(client *c);
1436void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);
1437void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask);
1438void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask);
1439void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask);
1440void addReplyString(client *c, const char *s, size_t len);
1441void AddReplyFromClient(client *c, client *src);
1442void addReplyBulk(client *c, robj *obj);
1443void addReplyBulkCString(client *c, const char *s);
1444void addReplyBulkCBuffer(client *c, const void *p, size_t len);
1445void addReplyBulkLongLong(client *c, long long ll);
1446void addReply(client *c, robj *obj);
1447void addReplySds(client *c, sds s);
1448void addReplyBulkSds(client *c, sds s);
1449void addReplyError(client *c, const char *err);
1450void addReplyStatus(client *c, const char *status);
1451void addReplyDouble(client *c, double d);
1452void addReplyHumanLongDouble(client *c, long double d);
1453void addReplyLongLong(client *c, long long ll);
1454void addReplyMultiBulkLen(client *c, long length);
1455void addReplyHelp(client *c, const char **help);
1456void addReplySubcommandSyntaxError(client *c);
1457void copyClientOutputBuffer(client *dst, client *src);
1458size_t sdsZmallocSize(sds s);
1459size_t getStringObjectSdsUsedMemory(robj *o);
1460void freeClientReplyValue(void *o);
1461void *dupClientReplyValue(void *o);
1462void getClientsMaxBuffers(unsigned long *longest_output_list,
1463 unsigned long *biggest_input_buffer);
1464char *getClientPeerId(client *client);
1465sds catClientInfoString(sds s, client *client);
1466sds getAllClientsInfoString(int type);
1467void rewriteClientCommandVector(client *c, int argc, ...);
1468void rewriteClientCommandArgument(client *c, int i, robj *newval);
1469void replaceClientCommandVector(client *c, int argc, robj **argv);
1470unsigned long getClientOutputBufferMemoryUsage(client *c);
1471void freeClientsInAsyncFreeQueue(void);
1472void asyncCloseClientOnOutputBufferLimitReached(client *c);
1473int getClientType(client *c);
1474int getClientTypeByName(char *name);
1475char *getClientTypeName(int class);
1476void flushSlavesOutputBuffers(void);
1477void disconnectSlaves(void);
1478int listenToPort(int port, int *fds, int *count);
1479void pauseClients(mstime_t duration);
1480int clientsArePaused(void);
1481int processEventsWhileBlocked(void);
1482int handleClientsWithPendingWrites(void);
1483int clientHasPendingReplies(client *c);
1484void unlinkClient(client *c);
1485int writeToClient(int fd, client *c, int handler_installed);
1486void linkClient(client *c);
1487void protectClient(client *c);
1488void unprotectClient(client *c);
1489
1490#ifdef __GNUC__
1491void addReplyErrorFormat(client *c, const char *fmt, ...)
1492 __attribute__((format(printf, 2, 3)));
1493void addReplyStatusFormat(client *c, const char *fmt, ...)
1494 __attribute__((format(printf, 2, 3)));
1495#else
1496void addReplyErrorFormat(client *c, const char *fmt, ...);
1497void addReplyStatusFormat(client *c, const char *fmt, ...);
1498#endif
1499
1500/* List data type */
1501void listTypeTryConversion(robj *subject, robj *value);
1502void listTypePush(robj *subject, robj *value, int where);
1503robj *listTypePop(robj *subject, int where);
1504unsigned long listTypeLength(const robj *subject);
1505listTypeIterator *listTypeInitIterator(robj *subject, long index, unsigned char direction);
1506void listTypeReleaseIterator(listTypeIterator *li);
1507int listTypeNext(listTypeIterator *li, listTypeEntry *entry);
1508robj *listTypeGet(listTypeEntry *entry);
1509void listTypeInsert(listTypeEntry *entry, robj *value, int where);
1510int listTypeEqual(listTypeEntry *entry, robj *o);
1511void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry);
1512void listTypeConvert(robj *subject, int enc);
1513void unblockClientWaitingData(client *c);
1514void popGenericCommand(client *c, int where);
1515
1516/* MULTI/EXEC/WATCH... */
1517void unwatchAllKeys(client *c);
1518void initClientMultiState(client *c);
1519void freeClientMultiState(client *c);
1520void queueMultiCommand(client *c);
1521void touchWatchedKey(redisDb *db, robj *key);
1522void touchWatchedKeysOnFlush(int dbid);
1523void discardTransaction(client *c);
1524void flagTransaction(client *c);
1525void execCommandPropagateMulti(client *c);
1526
1527/* Redis object implementation */
1528void decrRefCount(robj *o);
1529void decrRefCountVoid(void *o);
1530void incrRefCount(robj *o);
1531robj *makeObjectShared(robj *o);
1532robj *resetRefCount(robj *obj);
1533void freeStringObject(robj *o);
1534void freeListObject(robj *o);
1535void freeSetObject(robj *o);
1536void freeZsetObject(robj *o);
1537void freeHashObject(robj *o);
1538robj *createObject(int type, void *ptr);
1539robj *createStringObject(const char *ptr, size_t len);
1540robj *createRawStringObject(const char *ptr, size_t len);
1541robj *createEmbeddedStringObject(const char *ptr, size_t len);
1542robj *dupStringObject(const robj *o);
1543int isSdsRepresentableAsLongLong(sds s, long long *llval);
1544int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
1545robj *tryObjectEncoding(robj *o);
1546robj *getDecodedObject(robj *o);
1547size_t stringObjectLen(robj *o);
1548robj *createStringObjectFromLongLong(long long value);
1549robj *createStringObjectFromLongLongForValue(long long value);
1550robj *createStringObjectFromLongDouble(long double value, int humanfriendly);
1551robj *createQuicklistObject(void);
1552robj *createZiplistObject(void);
1553robj *createSetObject(void);
1554robj *createIntsetObject(void);
1555robj *createHashObject(void);
1556robj *createZsetObject(void);
1557robj *createZsetZiplistObject(void);
1558robj *createStreamObject(void);
1559robj *createModuleObject(moduleType *mt, void *value);
1560int getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);
1561int checkType(client *c, robj *o, int type);
1562int getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg);
1563int getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg);
1564int getDoubleFromObject(const robj *o, double *target);
1565int getLongLongFromObject(robj *o, long long *target);
1566int getLongDoubleFromObject(robj *o, long double *target);
1567int getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg);
1568char *strEncoding(int encoding);
1569int compareStringObjects(robj *a, robj *b);
1570int collateStringObjects(robj *a, robj *b);
1571int equalStringObjects(robj *a, robj *b);
1572unsigned long long estimateObjectIdleTime(robj *o);
1573void trimStringObjectIfNeeded(robj *o);
1574#define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)
1575
1576/* Synchronous I/O with timeout */
1577ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout);
1578ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout);
1579ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);
1580
1581/* Replication */
1582void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
1583void replicationFeedSlavesFromMasterStream(list *slaves, char *buf, size_t buflen);
1584void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc);
1585void updateSlavesWaitingBgsave(int bgsaveerr, int type);
1586void replicationCron(void);
1587void replicationHandleMasterDisconnection(void);
1588void replicationCacheMaster(client *c);
1589void resizeReplicationBacklog(long long newsize);
1590void replicationSetMaster(char *ip, int port);
1591void replicationUnsetMaster(void);
1592void refreshGoodSlavesCount(void);
1593void replicationScriptCacheInit(void);
1594void replicationScriptCacheFlush(void);
1595void replicationScriptCacheAdd(sds sha1);
1596int replicationScriptCacheExists(sds sha1);
1597void processClientsWaitingReplicas(void);
1598void unblockClientWaitingReplicas(client *c);
1599int replicationCountAcksByOffset(long long offset);
1600void replicationSendNewlineToMaster(void);
1601long long replicationGetSlaveOffset(void);
1602char *replicationGetSlaveName(client *c);
1603long long getPsyncInitialOffset(void);
1604int replicationSetupSlaveForFullResync(client *slave, long long offset);
1605void changeReplicationId(void);
1606void clearReplicationId2(void);
1607void chopReplicationBacklog(void);
1608void replicationCacheMasterUsingMyself(void);
1609void feedReplicationBacklog(void *ptr, size_t len);
1610
1611/* Generic persistence functions */
1612void startLoading(FILE *fp);
1613void loadingProgress(off_t pos);
1614void stopLoading(void);
1615
1616#define DISK_ERROR_TYPE_AOF 1 /* Don't accept writes: AOF errors. */
1617#define DISK_ERROR_TYPE_RDB 2 /* Don't accept writes: RDB errors. */
1618#define DISK_ERROR_TYPE_NONE 0 /* No problems, we can accept writes. */
1619int writeCommandsDeniedByDiskError(void);
1620
1621/* RDB persistence */
1622#include "rdb.h"
1623int rdbSaveRio(rio *rdb, int *error, int flags, rdbSaveInfo *rsi);
1624
1625/* AOF persistence */
1626void flushAppendOnlyFile(int force);
1627void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);
1628void aofRemoveTempFile(pid_t childpid);
1629int rewriteAppendOnlyFileBackground(void);
1630int loadAppendOnlyFile(char *filename);
1631void stopAppendOnly(void);
1632int startAppendOnly(void);
1633void backgroundRewriteDoneHandler(int exitcode, int bysignal);
1634void aofRewriteBufferReset(void);
1635unsigned long aofRewriteBufferSize(void);
1636ssize_t aofReadDiffFromParent(void);
1637
1638/* Child info */
1639void openChildInfoPipe(void);
1640void closeChildInfoPipe(void);
1641void sendChildInfo(int process_type);
1642void receiveChildInfo(void);
1643
1644/* Sorted sets data type */
1645
1646/* Input flags. */
1647#define ZADD_NONE 0
1648#define ZADD_INCR (1<<0) /* Increment the score instead of setting it. */
1649#define ZADD_NX (1<<1) /* Don't touch elements not already existing. */
1650#define ZADD_XX (1<<2) /* Only touch elements already existing. */
1651
1652/* Output flags. */
1653#define ZADD_NOP (1<<3) /* Operation not performed because of conditionals.*/
1654#define ZADD_NAN (1<<4) /* Only touch elements already existing. */
1655#define ZADD_ADDED (1<<5) /* The element was new and was added. */
1656#define ZADD_UPDATED (1<<6) /* The element already existed, score updated. */
1657
1658/* Flags only used by the ZADD command but not by zsetAdd() API: */
1659#define ZADD_CH (1<<16) /* Return num of elements added or updated. */
1660
1661/* Struct to hold a inclusive/exclusive range spec by score comparison. */
1662typedef struct {
1663 double min, max;
1664 int minex, maxex; /* are min or max exclusive? */
1665} zrangespec;
1666
1667/* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */
1668typedef struct {
1669 sds min, max; /* May be set to shared.(minstring|maxstring) */
1670 int minex, maxex; /* are min or max exclusive? */
1671} zlexrangespec;
1672
1673zskiplist *zslCreate(void);
1674void zslFree(zskiplist *zsl);
1675zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele);
1676unsigned char *zzlInsert(unsigned char *zl, sds ele, double score);
1677int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node);
1678zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range);
1679zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range);
1680double zzlGetScore(unsigned char *sptr);
1681void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
1682void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
1683unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);
1684unsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range);
1685unsigned long zsetLength(const robj *zobj);
1686void zsetConvert(robj *zobj, int encoding);
1687void zsetConvertToZiplistIfNeeded(robj *zobj, size_t maxelelen);
1688int zsetScore(robj *zobj, sds member, double *score);
1689unsigned long zslGetRank(zskiplist *zsl, double score, sds o);
1690int zsetAdd(robj *zobj, double score, sds ele, int *flags, double *newscore);
1691long zsetRank(robj *zobj, sds ele, int reverse);
1692int zsetDel(robj *zobj, sds ele);
1693void genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, robj *countarg);
1694sds ziplistGetObject(unsigned char *sptr);
1695int zslValueGteMin(double value, zrangespec *spec);
1696int zslValueLteMax(double value, zrangespec *spec);
1697void zslFreeLexRange(zlexrangespec *spec);
1698int zslParseLexRange(robj *min, robj *max, zlexrangespec *spec);
1699unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range);
1700unsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range);
1701zskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec *range);
1702zskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec *range);
1703int zzlLexValueGteMin(unsigned char *p, zlexrangespec *spec);
1704int zzlLexValueLteMax(unsigned char *p, zlexrangespec *spec);
1705int zslLexValueGteMin(sds value, zlexrangespec *spec);
1706int zslLexValueLteMax(sds value, zlexrangespec *spec);
1707
1708/* Core functions */
1709int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *level);
1710size_t freeMemoryGetNotCountedMemory();
1711int freeMemoryIfNeeded(void);
1712int freeMemoryIfNeededAndSafe(void);
1713int processCommand(client *c);
1714void setupSignalHandlers(void);
1715struct redisCommand *lookupCommand(sds name);
1716struct redisCommand *lookupCommandByCString(char *s);
1717struct redisCommand *lookupCommandOrOriginal(sds name);
1718void call(client *c, int flags);
1719void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int flags);
1720void alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int target);
1721void forceCommandPropagation(client *c, int flags);
1722void preventCommandPropagation(client *c);
1723void preventCommandAOF(client *c);
1724void preventCommandReplication(client *c);
1725int prepareForShutdown();
1726#ifdef __GNUC__
1727void serverLog(int level, const char *fmt, ...)
1728 __attribute__((format(printf, 2, 3)));
1729#else
1730void serverLog(int level, const char *fmt, ...);
1731#endif
1732void serverLogRaw(int level, const char *msg);
1733void serverLogFromHandler(int level, const char *msg);
1734void usage(void);
1735void updateDictResizePolicy(void);
1736int htNeedsResize(dict *dict);
1737void populateCommandTable(void);
1738void resetCommandTableStats(void);
1739void adjustOpenFilesLimit(void);
1740void closeListeningSockets(int unlink_unix_socket);
1741void updateCachedTime(void);
1742void resetServerStats(void);
1743void activeDefragCycle(void);
1744unsigned int getLRUClock(void);
1745unsigned int LRU_CLOCK(void);
1746const char *evictPolicyToString(void);
1747struct redisMemOverhead *getMemoryOverheadData(void);
1748void freeMemoryOverheadData(struct redisMemOverhead *mh);
1749
1750#define RESTART_SERVER_NONE 0
1751#define RESTART_SERVER_GRACEFULLY (1<<0) /* Do proper shutdown. */
1752#define RESTART_SERVER_CONFIG_REWRITE (1<<1) /* CONFIG REWRITE before restart.*/
1753int restartServer(int flags, mstime_t delay);
1754
1755/* Set data type */
1756robj *setTypeCreate(sds value);
1757int setTypeAdd(robj *subject, sds value);
1758int setTypeRemove(robj *subject, sds value);
1759int setTypeIsMember(robj *subject, sds value);
1760setTypeIterator *setTypeInitIterator(robj *subject);
1761void setTypeReleaseIterator(setTypeIterator *si);
1762int setTypeNext(setTypeIterator *si, sds *sdsele, int64_t *llele);
1763sds setTypeNextObject(setTypeIterator *si);
1764int setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele);
1765unsigned long setTypeRandomElements(robj *set, unsigned long count, robj *aux_set);
1766unsigned long setTypeSize(const robj *subject);
1767void setTypeConvert(robj *subject, int enc);
1768
1769/* Hash data type */
1770#define HASH_SET_TAKE_FIELD (1<<0)
1771#define HASH_SET_TAKE_VALUE (1<<1)
1772#define HASH_SET_COPY 0
1773
1774void hashTypeConvert(robj *o, int enc);
1775void hashTypeTryConversion(robj *subject, robj **argv, int start, int end);
1776void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2);
1777int hashTypeExists(robj *o, sds key);
1778int hashTypeDelete(robj *o, sds key);
1779unsigned long hashTypeLength(const robj *o);
1780hashTypeIterator *hashTypeInitIterator(robj *subject);
1781void hashTypeReleaseIterator(hashTypeIterator *hi);
1782int hashTypeNext(hashTypeIterator *hi);
1783void hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,
1784 unsigned char **vstr,
1785 unsigned int *vlen,
1786 long long *vll);
1787sds hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what);
1788void hashTypeCurrentObject(hashTypeIterator *hi, int what, unsigned char **vstr, unsigned int *vlen, long long *vll);
1789sds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what);
1790robj *hashTypeLookupWriteOrCreate(client *c, robj *key);
1791robj *hashTypeGetValueObject(robj *o, sds field);
1792int hashTypeSet(robj *o, sds field, sds value, int flags);
1793
1794/* Pub / Sub */
1795int pubsubUnsubscribeAllChannels(client *c, int notify);
1796int pubsubUnsubscribeAllPatterns(client *c, int notify);
1797void freePubsubPattern(void *p);
1798int listMatchPubsubPattern(void *a, void *b);
1799int pubsubPublishMessage(robj *channel, robj *message);
1800
1801/* Keyspace events notification */
1802void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid);
1803int keyspaceEventsStringToFlags(char *classes);
1804sds keyspaceEventsFlagsToString(int flags);
1805
1806/* Configuration */
1807void loadServerConfig(char *filename, char *options);
1808void appendServerSaveParams(time_t seconds, int changes);
1809void resetServerSaveParams(void);
1810struct rewriteConfigState; /* Forward declaration to export API. */
1811void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force);
1812int rewriteConfig(char *path);
1813
1814/* db.c -- Keyspace access API */
1815int removeExpire(redisDb *db, robj *key);
1816void propagateExpire(redisDb *db, robj *key, int lazy);
1817int expireIfNeeded(redisDb *db, robj *key);
1818long long getExpire(redisDb *db, robj *key);
1819void setExpire(client *c, redisDb *db, robj *key, long long when);
1820robj *lookupKey(redisDb *db, robj *key, int flags);
1821robj *lookupKeyRead(redisDb *db, robj *key);
1822robj *lookupKeyWrite(redisDb *db, robj *key);
1823robj *lookupKeyReadOrReply(client *c, robj *key, robj *reply);
1824robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply);
1825robj *lookupKeyReadWithFlags(redisDb *db, robj *key, int flags);
1826robj *objectCommandLookup(client *c, robj *key);
1827robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply);
1828void objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
1829 long long lru_clock);
1830#define LOOKUP_NONE 0
1831#define LOOKUP_NOTOUCH (1<<0)
1832void dbAdd(redisDb *db, robj *key, robj *val);
1833void dbOverwrite(redisDb *db, robj *key, robj *val);
1834void setKey(redisDb *db, robj *key, robj *val);
1835int dbExists(redisDb *db, robj *key);
1836robj *dbRandomKey(redisDb *db);
1837int dbSyncDelete(redisDb *db, robj *key);
1838int dbDelete(redisDb *db, robj *key);
1839robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o);
1840
1841#define EMPTYDB_NO_FLAGS 0 /* No flags. */
1842#define EMPTYDB_ASYNC (1<<0) /* Reclaim memory in another thread. */
1843long long emptyDb(int dbnum, int flags, void(callback)(void*));
1844
1845int selectDb(client *c, int id);
1846void signalModifiedKey(redisDb *db, robj *key);
1847void signalFlushedDb(int dbid);
1848unsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count);
1849unsigned int countKeysInSlot(unsigned int hashslot);
1850unsigned int delKeysInSlot(unsigned int hashslot);
1851int verifyClusterConfigWithData(void);
1852void scanGenericCommand(client *c, robj *o, unsigned long cursor);
1853int parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor);
1854void slotToKeyAdd(robj *key);
1855void slotToKeyDel(robj *key);
1856void slotToKeyFlush(void);
1857int dbAsyncDelete(redisDb *db, robj *key);
1858void emptyDbAsync(redisDb *db);
1859void slotToKeyFlushAsync(void);
1860size_t lazyfreeGetPendingObjectsCount(void);
1861void freeObjAsync(robj *o);
1862
1863/* API to get key arguments from commands */
1864int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1865void getKeysFreeResult(int *result);
1866int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys);
1867int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1868int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1869int *migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1870int *georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1871int *xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
1872
1873/* Cluster */
1874void clusterInit(void);
1875unsigned short crc16(const char *buf, int len);
1876unsigned int keyHashSlot(char *key, int keylen);
1877void clusterCron(void);
1878void clusterPropagatePublish(robj *channel, robj *message);
1879void migrateCloseTimedoutSockets(void);
1880void clusterBeforeSleep(void);
1881int clusterSendModuleMessageToTarget(const char *target, uint64_t module_id, uint8_t type, unsigned char *payload, uint32_t len);
1882
1883/* Sentinel */
1884void initSentinelConfig(void);
1885void initSentinel(void);
1886void sentinelTimer(void);
1887char *sentinelHandleConfiguration(char **argv, int argc);
1888void sentinelIsRunning(void);
1889
1890/* redis-check-rdb & aof */
1891int redis_check_rdb(char *rdbfilename, FILE *fp);
1892int redis_check_rdb_main(int argc, char **argv, FILE *fp);
1893int redis_check_aof_main(int argc, char **argv);
1894
1895/* Scripting */
1896void scriptingInit(int setup);
1897int ldbRemoveChild(pid_t pid);
1898void ldbKillForkedSessions(void);
1899int ldbPendingChildren(void);
1900sds luaCreateFunction(client *c, lua_State *lua, robj *body);
1901
1902/* Blocked clients */
1903void processUnblockedClients(void);
1904void blockClient(client *c, int btype);
1905void unblockClient(client *c);
1906void queueClientForReprocessing(client *c);
1907void replyToBlockedClientTimedOut(client *c);
1908int getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit);
1909void disconnectAllBlockedClients(void);
1910void handleClientsBlockedOnKeys(void);
1911void signalKeyAsReady(redisDb *db, robj *key);
1912void blockForKeys(client *c, int btype, robj **keys, int numkeys, mstime_t timeout, robj *target, streamID *ids);
1913
1914/* expire.c -- Handling of expired keys */
1915void activeExpireCycle(int type);
1916void expireSlaveKeys(void);
1917void rememberSlaveKeyWithExpire(redisDb *db, robj *key);
1918void flushSlaveKeysWithExpireList(void);
1919size_t getSlaveKeyWithExpireCount(void);
1920
1921/* evict.c -- maxmemory handling and LRU eviction. */
1922void evictionPoolAlloc(void);
1923#define LFU_INIT_VAL 5
1924unsigned long LFUGetTimeInMinutes(void);
1925uint8_t LFULogIncr(uint8_t value);
1926unsigned long LFUDecrAndReturn(robj *o);
1927
1928/* Keys hashing / comparison functions for dict.c hash tables. */
1929uint64_t dictSdsHash(const void *key);
1930int dictSdsKeyCompare(void *privdata, const void *key1, const void *key2);
1931void dictSdsDestructor(void *privdata, void *val);
1932
1933/* Git SHA1 */
1934char *redisGitSHA1(void);
1935char *redisGitDirty(void);
1936uint64_t redisBuildId(void);
1937
1938/* Commands prototypes */
1939void authCommand(client *c);
1940void pingCommand(client *c);
1941void echoCommand(client *c);
1942void commandCommand(client *c);
1943void setCommand(client *c);
1944void setnxCommand(client *c);
1945void setexCommand(client *c);
1946void psetexCommand(client *c);
1947void getCommand(client *c);
1948void delCommand(client *c);
1949void unlinkCommand(client *c);
1950void existsCommand(client *c);
1951void setbitCommand(client *c);
1952void getbitCommand(client *c);
1953void bitfieldCommand(client *c);
1954void setrangeCommand(client *c);
1955void getrangeCommand(client *c);
1956void incrCommand(client *c);
1957void decrCommand(client *c);
1958void incrbyCommand(client *c);
1959void decrbyCommand(client *c);
1960void incrbyfloatCommand(client *c);
1961void selectCommand(client *c);
1962void swapdbCommand(client *c);
1963void randomkeyCommand(client *c);
1964void keysCommand(client *c);
1965void scanCommand(client *c);
1966void dbsizeCommand(client *c);
1967void lastsaveCommand(client *c);
1968void saveCommand(client *c);
1969void bgsaveCommand(client *c);
1970void bgrewriteaofCommand(client *c);
1971void shutdownCommand(client *c);
1972void moveCommand(client *c);
1973void renameCommand(client *c);
1974void renamenxCommand(client *c);
1975void lpushCommand(client *c);
1976void rpushCommand(client *c);
1977void lpushxCommand(client *c);
1978void rpushxCommand(client *c);
1979void linsertCommand(client *c);
1980void lpopCommand(client *c);
1981void rpopCommand(client *c);
1982void llenCommand(client *c);
1983void lindexCommand(client *c);
1984void lrangeCommand(client *c);
1985void ltrimCommand(client *c);
1986void typeCommand(client *c);
1987void lsetCommand(client *c);
1988void saddCommand(client *c);
1989void sremCommand(client *c);
1990void smoveCommand(client *c);
1991void sismemberCommand(client *c);
1992void scardCommand(client *c);
1993void spopCommand(client *c);
1994void srandmemberCommand(client *c);
1995void sinterCommand(client *c);
1996void sinterstoreCommand(client *c);
1997void sunionCommand(client *c);
1998void sunionstoreCommand(client *c);
1999void sdiffCommand(client *c);
2000void sdiffstoreCommand(client *c);
2001void sscanCommand(client *c);
2002void syncCommand(client *c);
2003void flushdbCommand(client *c);
2004void flushallCommand(client *c);
2005void sortCommand(client *c);
2006void lremCommand(client *c);
2007void rpoplpushCommand(client *c);
2008void infoCommand(client *c);
2009void mgetCommand(client *c);
2010void monitorCommand(client *c);
2011void expireCommand(client *c);
2012void expireatCommand(client *c);
2013void pexpireCommand(client *c);
2014void pexpireatCommand(client *c);
2015void getsetCommand(client *c);
2016void ttlCommand(client *c);
2017void touchCommand(client *c);
2018void pttlCommand(client *c);
2019void persistCommand(client *c);
2020void replicaofCommand(client *c);
2021void roleCommand(client *c);
2022void debugCommand(client *c);
2023void msetCommand(client *c);
2024void msetnxCommand(client *c);
2025void zaddCommand(client *c);
2026void zincrbyCommand(client *c);
2027void zrangeCommand(client *c);
2028void zrangebyscoreCommand(client *c);
2029void zrevrangebyscoreCommand(client *c);
2030void zrangebylexCommand(client *c);
2031void zrevrangebylexCommand(client *c);
2032void zcountCommand(client *c);
2033void zlexcountCommand(client *c);
2034void zrevrangeCommand(client *c);
2035void zcardCommand(client *c);
2036void zremCommand(client *c);
2037void zscoreCommand(client *c);
2038void zremrangebyscoreCommand(client *c);
2039void zremrangebylexCommand(client *c);
2040void zpopminCommand(client *c);
2041void zpopmaxCommand(client *c);
2042void bzpopminCommand(client *c);
2043void bzpopmaxCommand(client *c);
2044void multiCommand(client *c);
2045void execCommand(client *c);
2046void discardCommand(client *c);
2047void blpopCommand(client *c);
2048void brpopCommand(client *c);
2049void brpoplpushCommand(client *c);
2050void appendCommand(client *c);
2051void strlenCommand(client *c);
2052void zrankCommand(client *c);
2053void zrevrankCommand(client *c);
2054void hsetCommand(client *c);
2055void hsetnxCommand(client *c);
2056void hgetCommand(client *c);
2057void hmsetCommand(client *c);
2058void hmgetCommand(client *c);
2059void hdelCommand(client *c);
2060void hlenCommand(client *c);
2061void hstrlenCommand(client *c);
2062void zremrangebyrankCommand(client *c);
2063void zunionstoreCommand(client *c);
2064void zinterstoreCommand(client *c);
2065void zscanCommand(client *c);
2066void hkeysCommand(client *c);
2067void hvalsCommand(client *c);
2068void hgetallCommand(client *c);
2069void hexistsCommand(client *c);
2070void hscanCommand(client *c);
2071void configCommand(client *c);
2072void hincrbyCommand(client *c);
2073void hincrbyfloatCommand(client *c);
2074void subscribeCommand(client *c);
2075void unsubscribeCommand(client *c);
2076void psubscribeCommand(client *c);
2077void punsubscribeCommand(client *c);
2078void publishCommand(client *c);
2079void pubsubCommand(client *c);
2080void watchCommand(client *c);
2081void unwatchCommand(client *c);
2082void clusterCommand(client *c);
2083void restoreCommand(client *c);
2084void migrateCommand(client *c);
2085void askingCommand(client *c);
2086void readonlyCommand(client *c);
2087void readwriteCommand(client *c);
2088void dumpCommand(client *c);
2089void objectCommand(client *c);
2090void memoryCommand(client *c);
2091void clientCommand(client *c);
2092void evalCommand(client *c);
2093void evalShaCommand(client *c);
2094void scriptCommand(client *c);
2095void timeCommand(client *c);
2096void bitopCommand(client *c);
2097void bitcountCommand(client *c);
2098void bitposCommand(client *c);
2099void replconfCommand(client *c);
2100void waitCommand(client *c);
2101void geoencodeCommand(client *c);
2102void geodecodeCommand(client *c);
2103void georadiusbymemberCommand(client *c);
2104void georadiusbymemberroCommand(client *c);
2105void georadiusCommand(client *c);
2106void georadiusroCommand(client *c);
2107void geoaddCommand(client *c);
2108void geohashCommand(client *c);
2109void geoposCommand(client *c);
2110void geodistCommand(client *c);
2111void pfselftestCommand(client *c);
2112void pfaddCommand(client *c);
2113void pfcountCommand(client *c);
2114void pfmergeCommand(client *c);
2115void pfdebugCommand(client *c);
2116void latencyCommand(client *c);
2117void moduleCommand(client *c);
2118void securityWarningCommand(client *c);
2119void xaddCommand(client *c);
2120void xrangeCommand(client *c);
2121void xrevrangeCommand(client *c);
2122void xlenCommand(client *c);
2123void xreadCommand(client *c);
2124void xgroupCommand(client *c);
2125void xsetidCommand(client *c);
2126void xackCommand(client *c);
2127void xpendingCommand(client *c);
2128void xclaimCommand(client *c);
2129void xinfoCommand(client *c);
2130void xdelCommand(client *c);
2131void xtrimCommand(client *c);
2132void lolwutCommand(client *c);
2133
2134#if defined(__GNUC__)
2135void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
2136void free(void *ptr) __attribute__ ((deprecated));
2137void *malloc(size_t size) __attribute__ ((deprecated));
2138void *realloc(void *ptr, size_t size) __attribute__ ((deprecated));
2139#endif
2140
2141/* Debugging stuff */
2142void _serverAssertWithInfo(const client *c, const robj *o, const char *estr, const char *file, int line);
2143void _serverAssert(const char *estr, const char *file, int line);
2144void _serverPanic(const char *file, int line, const char *msg, ...);
2145void bugReportStart(void);
2146void serverLogObjectDebugInfo(const robj *o);
2147void sigsegvHandler(int sig, siginfo_t *info, void *secret);
2148sds genRedisInfoString(char *section);
2149void enableWatchdog(int period);
2150void disableWatchdog(void);
2151void watchdogScheduleSignal(int period);
2152void serverLogHexDump(int level, char *descr, void *value, size_t len);
2153int memtest_preserving_test(unsigned long *m, size_t bytes, int passes);
2154void mixDigest(unsigned char *digest, void *ptr, size_t len);
2155void xorDigest(unsigned char *digest, void *ptr, size_t len);
2156
2157#define redisDebug(fmt, ...) \
2158 printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
2159#define redisDebugMark() \
2160 printf("-- MARK %s:%d --\n", __FILE__, __LINE__)
2161
2162#endif