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#include "atomicvar.h"
38
39#include <assert.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <time.h>
44#include <limits.h>
45#include <unistd.h>
46#include <errno.h>
47#include <inttypes.h>
48#include <pthread.h>
49#include <syslog.h>
50#include <netinet/in.h>
51#include <sys/socket.h>
52#include <lua.h>
53#include <signal.h>
54#include "hdr_histogram.h"
55
56#ifdef HAVE_LIBSYSTEMD
57#include <systemd/sd-daemon.h>
58#endif
59
60#ifndef static_assert
61#define static_assert(expr, lit) extern char __static_assert_failure[(expr) ? 1:-1]
62#endif
63
64typedef long long mstime_t; /* millisecond time type. */
65typedef long long ustime_t; /* microsecond time type. */
66
67#include "ae.h" /* Event driven programming library */
68#include "sds.h" /* Dynamic safe strings */
69#include "dict.h" /* Hash tables */
70#include "adlist.h" /* Linked lists */
71#include "zmalloc.h" /* total memory usage aware version of malloc/free */
72#include "anet.h" /* Networking the easy way */
73#include "intset.h" /* Compact integer set structure */
74#include "version.h" /* Version macro */
75#include "util.h" /* Misc functions useful in many places */
76#include "latency.h" /* Latency monitor API */
77#include "sparkline.h" /* ASCII graphs API */
78#include "quicklist.h" /* Lists are encoded as linked lists of
79 N-elements flat arrays */
80#include "rax.h" /* Radix tree */
81#include "connection.h" /* Connection abstraction */
82
83#define REDISMODULE_CORE 1
84#include "redismodule.h" /* Redis modules API defines. */
85
86/* Following includes allow test functions to be called from Redis main() */
87#include "zipmap.h"
88#include "ziplist.h" /* Compact list data structure */
89#include "sha1.h"
90#include "endianconv.h"
91#include "crc64.h"
92
93/* min/max */
94#undef min
95#undef max
96#define min(a, b) ((a) < (b) ? (a) : (b))
97#define max(a, b) ((a) > (b) ? (a) : (b))
98
99/* Error codes */
100#define C_OK 0
101#define C_ERR -1
102
103/* Static server configuration */
104#define CONFIG_DEFAULT_HZ 10 /* Time interrupt calls/sec. */
105#define CONFIG_MIN_HZ 1
106#define CONFIG_MAX_HZ 500
107#define MAX_CLIENTS_PER_CLOCK_TICK 200 /* HZ is adapted based on that. */
108#define CRON_DBS_PER_CALL 16
109#define NET_MAX_WRITES_PER_EVENT (1024*64)
110#define PROTO_SHARED_SELECT_CMDS 10
111#define OBJ_SHARED_INTEGERS 10000
112#define OBJ_SHARED_BULKHDR_LEN 32
113#define OBJ_SHARED_HDR_STRLEN(_len_) (((_len_) < 10) ? 4 : 5) /* see shared.mbulkhdr etc. */
114#define LOG_MAX_LEN 1024 /* Default maximum length of syslog messages.*/
115#define AOF_REWRITE_ITEMS_PER_CMD 64
116#define AOF_ANNOTATION_LINE_MAX_LEN 1024
117#define CONFIG_RUN_ID_SIZE 40
118#define RDB_EOF_MARK_SIZE 40
119#define CONFIG_REPL_BACKLOG_MIN_SIZE (1024*16) /* 16k */
120#define CONFIG_BGSAVE_RETRY_DELAY 5 /* Wait a few secs before trying again. */
121#define CONFIG_DEFAULT_PID_FILE "/var/run/redis.pid"
122#define CONFIG_DEFAULT_BINDADDR_COUNT 2
123#define CONFIG_DEFAULT_BINDADDR { "*", "-::*" }
124#define NET_HOST_STR_LEN 256 /* Longest valid hostname */
125#define NET_IP_STR_LEN 46 /* INET6_ADDRSTRLEN is 46, but we need to be sure */
126#define NET_ADDR_STR_LEN (NET_IP_STR_LEN+32) /* Must be enough for ip:port */
127#define NET_HOST_PORT_STR_LEN (NET_HOST_STR_LEN+32) /* Must be enough for hostname:port */
128#define CONFIG_BINDADDR_MAX 16
129#define CONFIG_MIN_RESERVED_FDS 32
130#define CONFIG_DEFAULT_PROC_TITLE_TEMPLATE "{title} {listen-addr} {server-mode}"
131
132/* Bucket sizes for client eviction pools. Each bucket stores clients with
133 * memory usage of up to twice the size of the bucket below it. */
134#define CLIENT_MEM_USAGE_BUCKET_MIN_LOG 15 /* Bucket sizes start at up to 32KB (2^15) */
135#define CLIENT_MEM_USAGE_BUCKET_MAX_LOG 33 /* Bucket for largest clients: sizes above 4GB (2^32) */
136#define CLIENT_MEM_USAGE_BUCKETS (1+CLIENT_MEM_USAGE_BUCKET_MAX_LOG-CLIENT_MEM_USAGE_BUCKET_MIN_LOG)
137
138#define ACTIVE_EXPIRE_CYCLE_SLOW 0
139#define ACTIVE_EXPIRE_CYCLE_FAST 1
140
141/* Children process will exit with this status code to signal that the
142 * process terminated without an error: this is useful in order to kill
143 * a saving child (RDB or AOF one), without triggering in the parent the
144 * write protection that is normally turned on on write errors.
145 * Usually children that are terminated with SIGUSR1 will exit with this
146 * special code. */
147#define SERVER_CHILD_NOERROR_RETVAL 255
148
149/* Reading copy-on-write info is sometimes expensive and may slow down child
150 * processes that report it continuously. We measure the cost of obtaining it
151 * and hold back additional reading based on this factor. */
152#define CHILD_COW_DUTY_CYCLE 100
153
154/* Instantaneous metrics tracking. */
155#define STATS_METRIC_SAMPLES 16 /* Number of samples per metric. */
156#define STATS_METRIC_COMMAND 0 /* Number of commands executed. */
157#define STATS_METRIC_NET_INPUT 1 /* Bytes read to network. */
158#define STATS_METRIC_NET_OUTPUT 2 /* Bytes written to network. */
159#define STATS_METRIC_NET_INPUT_REPLICATION 3 /* Bytes read to network during replication. */
160#define STATS_METRIC_NET_OUTPUT_REPLICATION 4 /* Bytes written to network during replication. */
161#define STATS_METRIC_COUNT 5
162
163/* Protocol and I/O related defines */
164#define PROTO_IOBUF_LEN (1024*16) /* Generic I/O buffer size */
165#define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */
166#define PROTO_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */
167#define PROTO_MBULK_BIG_ARG (1024*32)
168#define PROTO_RESIZE_THRESHOLD (1024*32) /* Threshold for determining whether to resize query buffer */
169#define PROTO_REPLY_MIN_BYTES (1024) /* the lower limit on reply buffer size */
170#define REDIS_AUTOSYNC_BYTES (1024*1024*4) /* Sync file every 4MB. */
171
172#define REPLY_BUFFER_DEFAULT_PEAK_RESET_TIME 5000 /* 5 seconds */
173
174/* When configuring the server eventloop, we setup it so that the total number
175 * of file descriptors we can handle are server.maxclients + RESERVED_FDS +
176 * a few more to stay safe. Since RESERVED_FDS defaults to 32, we add 96
177 * in order to make sure of not over provisioning more than 128 fds. */
178#define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)
179
180/* OOM Score Adjustment classes. */
181#define CONFIG_OOM_MASTER 0
182#define CONFIG_OOM_REPLICA 1
183#define CONFIG_OOM_BGCHILD 2
184#define CONFIG_OOM_COUNT 3
185
186extern int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT];
187
188/* Hash table parameters */
189#define HASHTABLE_MIN_FILL 10 /* Minimal hash table fill 10% */
190#define HASHTABLE_MAX_LOAD_FACTOR 1.618 /* Maximum hash table load factor. */
191
192/* Command flags. Please check the definition of struct redisCommand in this file
193 * for more information about the meaning of every flag. */
194#define CMD_WRITE (1ULL<<0)
195#define CMD_READONLY (1ULL<<1)
196#define CMD_DENYOOM (1ULL<<2)
197#define CMD_MODULE (1ULL<<3) /* Command exported by module. */
198#define CMD_ADMIN (1ULL<<4)
199#define CMD_PUBSUB (1ULL<<5)
200#define CMD_NOSCRIPT (1ULL<<6)
201#define CMD_BLOCKING (1ULL<<8) /* Has potential to block. */
202#define CMD_LOADING (1ULL<<9)
203#define CMD_STALE (1ULL<<10)
204#define CMD_SKIP_MONITOR (1ULL<<11)
205#define CMD_SKIP_SLOWLOG (1ULL<<12)
206#define CMD_ASKING (1ULL<<13)
207#define CMD_FAST (1ULL<<14)
208#define CMD_NO_AUTH (1ULL<<15)
209#define CMD_MAY_REPLICATE (1ULL<<16)
210#define CMD_SENTINEL (1ULL<<17)
211#define CMD_ONLY_SENTINEL (1ULL<<18)
212#define CMD_NO_MANDATORY_KEYS (1ULL<<19)
213#define CMD_PROTECTED (1ULL<<20)
214#define CMD_MODULE_GETKEYS (1ULL<<21) /* Use the modules getkeys interface. */
215#define CMD_MODULE_NO_CLUSTER (1ULL<<22) /* Deny on Redis Cluster. */
216#define CMD_NO_ASYNC_LOADING (1ULL<<23)
217#define CMD_NO_MULTI (1ULL<<24)
218#define CMD_MOVABLE_KEYS (1ULL<<25) /* The legacy range spec doesn't cover all keys.
219 * Populated by populateCommandLegacyRangeSpec. */
220#define CMD_ALLOW_BUSY ((1ULL<<26))
221#define CMD_MODULE_GETCHANNELS (1ULL<<27) /* Use the modules getchannels interface. */
222
223/* Command flags that describe ACLs categories. */
224#define ACL_CATEGORY_KEYSPACE (1ULL<<0)
225#define ACL_CATEGORY_READ (1ULL<<1)
226#define ACL_CATEGORY_WRITE (1ULL<<2)
227#define ACL_CATEGORY_SET (1ULL<<3)
228#define ACL_CATEGORY_SORTEDSET (1ULL<<4)
229#define ACL_CATEGORY_LIST (1ULL<<5)
230#define ACL_CATEGORY_HASH (1ULL<<6)
231#define ACL_CATEGORY_STRING (1ULL<<7)
232#define ACL_CATEGORY_BITMAP (1ULL<<8)
233#define ACL_CATEGORY_HYPERLOGLOG (1ULL<<9)
234#define ACL_CATEGORY_GEO (1ULL<<10)
235#define ACL_CATEGORY_STREAM (1ULL<<11)
236#define ACL_CATEGORY_PUBSUB (1ULL<<12)
237#define ACL_CATEGORY_ADMIN (1ULL<<13)
238#define ACL_CATEGORY_FAST (1ULL<<14)
239#define ACL_CATEGORY_SLOW (1ULL<<15)
240#define ACL_CATEGORY_BLOCKING (1ULL<<16)
241#define ACL_CATEGORY_DANGEROUS (1ULL<<17)
242#define ACL_CATEGORY_CONNECTION (1ULL<<18)
243#define ACL_CATEGORY_TRANSACTION (1ULL<<19)
244#define ACL_CATEGORY_SCRIPTING (1ULL<<20)
245
246/* Key-spec flags *
247 * -------------- */
248/* The following refer what the command actually does with the value or metadata
249 * of the key, and not necessarily the user data or how it affects it.
250 * Each key-spec may must have exactly one of these. Any operation that's not
251 * distinctly deletion, overwrite or read-only would be marked as RW. */
252#define CMD_KEY_RO (1ULL<<0) /* Read-Only - Reads the value of the key, but
253 * doesn't necessarily returns it. */
254#define CMD_KEY_RW (1ULL<<1) /* Read-Write - Modifies the data stored in the
255 * value of the key or its metadata. */
256#define CMD_KEY_OW (1ULL<<2) /* Overwrite - Overwrites the data stored in
257 * the value of the key. */
258#define CMD_KEY_RM (1ULL<<3) /* Deletes the key. */
259/* The following refer to user data inside the value of the key, not the metadata
260 * like LRU, type, cardinality. It refers to the logical operation on the user's
261 * data (actual input strings / TTL), being used / returned / copied / changed,
262 * It doesn't refer to modification or returning of metadata (like type, count,
263 * presence of data). Any write that's not INSERT or DELETE, would be an UPDATE.
264 * Each key-spec may have one of the writes with or without access, or none: */
265#define CMD_KEY_ACCESS (1ULL<<4) /* Returns, copies or uses the user data from
266 * the value of the key. */
267#define CMD_KEY_UPDATE (1ULL<<5) /* Updates data to the value, new value may
268 * depend on the old value. */
269#define CMD_KEY_INSERT (1ULL<<6) /* Adds data to the value with no chance of
270 * modification or deletion of existing data. */
271#define CMD_KEY_DELETE (1ULL<<7) /* Explicitly deletes some content
272 * from the value of the key. */
273/* Other flags: */
274#define CMD_KEY_NOT_KEY (1ULL<<8) /* A 'fake' key that should be routed
275 * like a key in cluster mode but is
276 * excluded from other key checks. */
277#define CMD_KEY_INCOMPLETE (1ULL<<9) /* Means that the keyspec might not point
278 * out to all keys it should cover */
279#define CMD_KEY_VARIABLE_FLAGS (1ULL<<10) /* Means that some keys might have
280 * different flags depending on arguments */
281
282/* Key flags for when access type is unknown */
283#define CMD_KEY_FULL_ACCESS (CMD_KEY_RW | CMD_KEY_ACCESS | CMD_KEY_UPDATE)
284
285/* Channel flags share the same flag space as the key flags */
286#define CMD_CHANNEL_PATTERN (1ULL<<11) /* The argument is a channel pattern */
287#define CMD_CHANNEL_SUBSCRIBE (1ULL<<12) /* The command subscribes to channels */
288#define CMD_CHANNEL_UNSUBSCRIBE (1ULL<<13) /* The command unsubscribes to channels */
289#define CMD_CHANNEL_PUBLISH (1ULL<<14) /* The command publishes to channels. */
290
291/* AOF states */
292#define AOF_OFF 0 /* AOF is off */
293#define AOF_ON 1 /* AOF is on */
294#define AOF_WAIT_REWRITE 2 /* AOF waits rewrite to start appending */
295
296/* AOF return values for loadAppendOnlyFiles() and loadSingleAppendOnlyFile() */
297#define AOF_OK 0
298#define AOF_NOT_EXIST 1
299#define AOF_EMPTY 2
300#define AOF_OPEN_ERR 3
301#define AOF_FAILED 4
302#define AOF_TRUNCATED 5
303
304/* Command doc flags */
305#define CMD_DOC_NONE 0
306#define CMD_DOC_DEPRECATED (1<<0) /* Command is deprecated */
307#define CMD_DOC_SYSCMD (1<<1) /* System (internal) command */
308
309/* Client flags */
310#define CLIENT_SLAVE (1<<0) /* This client is a replica */
311#define CLIENT_MASTER (1<<1) /* This client is a master */
312#define CLIENT_MONITOR (1<<2) /* This client is a slave monitor, see MONITOR */
313#define CLIENT_MULTI (1<<3) /* This client is in a MULTI context */
314#define CLIENT_BLOCKED (1<<4) /* The client is waiting in a blocking operation */
315#define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */
316#define CLIENT_CLOSE_AFTER_REPLY (1<<6) /* Close after writing entire reply. */
317#define CLIENT_UNBLOCKED (1<<7) /* This client was unblocked and is stored in
318 server.unblocked_clients */
319#define CLIENT_SCRIPT (1<<8) /* This is a non connected client used by Lua */
320#define CLIENT_ASKING (1<<9) /* Client issued the ASKING command */
321#define CLIENT_CLOSE_ASAP (1<<10)/* Close this client ASAP */
322#define CLIENT_UNIX_SOCKET (1<<11) /* Client connected via Unix domain socket */
323#define CLIENT_DIRTY_EXEC (1<<12) /* EXEC will fail for errors while queueing */
324#define CLIENT_MASTER_FORCE_REPLY (1<<13) /* Queue replies even if is master */
325#define CLIENT_FORCE_AOF (1<<14) /* Force AOF propagation of current cmd. */
326#define CLIENT_FORCE_REPL (1<<15) /* Force replication of current cmd. */
327#define CLIENT_PRE_PSYNC (1<<16) /* Instance don't understand PSYNC. */
328#define CLIENT_READONLY (1<<17) /* Cluster client is in read-only state. */
329#define CLIENT_PUBSUB (1<<18) /* Client is in Pub/Sub mode. */
330#define CLIENT_PREVENT_AOF_PROP (1<<19) /* Don't propagate to AOF. */
331#define CLIENT_PREVENT_REPL_PROP (1<<20) /* Don't propagate to slaves. */
332#define CLIENT_PREVENT_PROP (CLIENT_PREVENT_AOF_PROP|CLIENT_PREVENT_REPL_PROP)
333#define CLIENT_PENDING_WRITE (1<<21) /* Client has output to send but a write
334 handler is yet not installed. */
335#define CLIENT_REPLY_OFF (1<<22) /* Don't send replies to client. */
336#define CLIENT_REPLY_SKIP_NEXT (1<<23) /* Set CLIENT_REPLY_SKIP for next cmd */
337#define CLIENT_REPLY_SKIP (1<<24) /* Don't send just this reply. */
338#define CLIENT_LUA_DEBUG (1<<25) /* Run EVAL in debug mode. */
339#define CLIENT_LUA_DEBUG_SYNC (1<<26) /* EVAL debugging without fork() */
340#define CLIENT_MODULE (1<<27) /* Non connected client used by some module. */
341#define CLIENT_PROTECTED (1<<28) /* Client should not be freed for now. */
342/* #define CLIENT_... (1<<29) currently unused, feel free to use in the future */
343#define CLIENT_PENDING_COMMAND (1<<30) /* Indicates the client has a fully
344 * parsed command ready for execution. */
345#define CLIENT_TRACKING (1ULL<<31) /* Client enabled keys tracking in order to
346 perform client side caching. */
347#define CLIENT_TRACKING_BROKEN_REDIR (1ULL<<32) /* Target client is invalid. */
348#define CLIENT_TRACKING_BCAST (1ULL<<33) /* Tracking in BCAST mode. */
349#define CLIENT_TRACKING_OPTIN (1ULL<<34) /* Tracking in opt-in mode. */
350#define CLIENT_TRACKING_OPTOUT (1ULL<<35) /* Tracking in opt-out mode. */
351#define CLIENT_TRACKING_CACHING (1ULL<<36) /* CACHING yes/no was given,
352 depending on optin/optout mode. */
353#define CLIENT_TRACKING_NOLOOP (1ULL<<37) /* Don't send invalidation messages
354 about writes performed by myself.*/
355#define CLIENT_IN_TO_TABLE (1ULL<<38) /* This client is in the timeout table. */
356#define CLIENT_PROTOCOL_ERROR (1ULL<<39) /* Protocol error chatting with it. */
357#define CLIENT_CLOSE_AFTER_COMMAND (1ULL<<40) /* Close after executing commands
358 * and writing entire reply. */
359#define CLIENT_DENY_BLOCKING (1ULL<<41) /* Indicate that the client should not be blocked.
360 currently, turned on inside MULTI, Lua, RM_Call,
361 and AOF client */
362#define CLIENT_REPL_RDBONLY (1ULL<<42) /* This client is a replica that only wants
363 RDB without replication buffer. */
364#define CLIENT_NO_EVICT (1ULL<<43) /* This client is protected against client
365 memory eviction. */
366
367/* Client block type (btype field in client structure)
368 * if CLIENT_BLOCKED flag is set. */
369#define BLOCKED_NONE 0 /* Not blocked, no CLIENT_BLOCKED flag set. */
370#define BLOCKED_LIST 1 /* BLPOP & co. */
371#define BLOCKED_WAIT 2 /* WAIT for synchronous replication. */
372#define BLOCKED_MODULE 3 /* Blocked by a loadable module. */
373#define BLOCKED_STREAM 4 /* XREAD. */
374#define BLOCKED_ZSET 5 /* BZPOP et al. */
375#define BLOCKED_POSTPONE 6 /* Blocked by processCommand, re-try processing later. */
376#define BLOCKED_SHUTDOWN 7 /* SHUTDOWN. */
377#define BLOCKED_NUM 8 /* Number of blocked states. */
378
379/* Client request types */
380#define PROTO_REQ_INLINE 1
381#define PROTO_REQ_MULTIBULK 2
382
383/* Client classes for client limits, currently used only for
384 * the max-client-output-buffer limit implementation. */
385#define CLIENT_TYPE_NORMAL 0 /* Normal req-reply clients + MONITORs */
386#define CLIENT_TYPE_SLAVE 1 /* Slaves. */
387#define CLIENT_TYPE_PUBSUB 2 /* Clients subscribed to PubSub channels. */
388#define CLIENT_TYPE_MASTER 3 /* Master. */
389#define CLIENT_TYPE_COUNT 4 /* Total number of client types. */
390#define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output
391 buffer configuration. Just the first
392 three: normal, slave, pubsub. */
393
394/* Slave replication state. Used in server.repl_state for slaves to remember
395 * what to do next. */
396typedef enum {
397 REPL_STATE_NONE = 0, /* No active replication */
398 REPL_STATE_CONNECT, /* Must connect to master */
399 REPL_STATE_CONNECTING, /* Connecting to master */
400 /* --- Handshake states, must be ordered --- */
401 REPL_STATE_RECEIVE_PING_REPLY, /* Wait for PING reply */
402 REPL_STATE_SEND_HANDSHAKE, /* Send handshake sequence to master */
403 REPL_STATE_RECEIVE_AUTH_REPLY, /* Wait for AUTH reply */
404 REPL_STATE_RECEIVE_PORT_REPLY, /* Wait for REPLCONF reply */
405 REPL_STATE_RECEIVE_IP_REPLY, /* Wait for REPLCONF reply */
406 REPL_STATE_RECEIVE_CAPA_REPLY, /* Wait for REPLCONF reply */
407 REPL_STATE_SEND_PSYNC, /* Send PSYNC */
408 REPL_STATE_RECEIVE_PSYNC_REPLY, /* Wait for PSYNC reply */
409 /* --- End of handshake states --- */
410 REPL_STATE_TRANSFER, /* Receiving .rdb from master */
411 REPL_STATE_CONNECTED, /* Connected to master */
412} repl_state;
413
414/* The state of an in progress coordinated failover */
415typedef enum {
416 NO_FAILOVER = 0, /* No failover in progress */
417 FAILOVER_WAIT_FOR_SYNC, /* Waiting for target replica to catch up */
418 FAILOVER_IN_PROGRESS /* Waiting for target replica to accept
419 * PSYNC FAILOVER request. */
420} failover_state;
421
422/* State of slaves from the POV of the master. Used in client->replstate.
423 * In SEND_BULK and ONLINE state the slave receives new updates
424 * in its output queue. In the WAIT_BGSAVE states instead the server is waiting
425 * to start the next background saving in order to send updates to it. */
426#define SLAVE_STATE_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */
427#define SLAVE_STATE_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */
428#define SLAVE_STATE_SEND_BULK 8 /* Sending RDB file to slave. */
429#define SLAVE_STATE_ONLINE 9 /* RDB file transmitted, sending just updates. */
430
431/* Slave capabilities. */
432#define SLAVE_CAPA_NONE 0
433#define SLAVE_CAPA_EOF (1<<0) /* Can parse the RDB EOF streaming format. */
434#define SLAVE_CAPA_PSYNC2 (1<<1) /* Supports PSYNC2 protocol. */
435
436/* Slave requirements */
437#define SLAVE_REQ_NONE 0
438#define SLAVE_REQ_RDB_EXCLUDE_DATA (1 << 0) /* Exclude data from RDB */
439#define SLAVE_REQ_RDB_EXCLUDE_FUNCTIONS (1 << 1) /* Exclude functions from RDB */
440/* Mask of all bits in the slave requirements bitfield that represent non-standard (filtered) RDB requirements */
441#define SLAVE_REQ_RDB_MASK (SLAVE_REQ_RDB_EXCLUDE_DATA | SLAVE_REQ_RDB_EXCLUDE_FUNCTIONS)
442
443/* Synchronous read timeout - slave side */
444#define CONFIG_REPL_SYNCIO_TIMEOUT 5
445
446/* The default number of replication backlog blocks to trim per call. */
447#define REPL_BACKLOG_TRIM_BLOCKS_PER_CALL 64
448
449/* In order to quickly find the requested offset for PSYNC requests,
450 * we index some nodes in the replication buffer linked list into a rax. */
451#define REPL_BACKLOG_INDEX_PER_BLOCKS 64
452
453/* List related stuff */
454#define LIST_HEAD 0
455#define LIST_TAIL 1
456#define ZSET_MIN 0
457#define ZSET_MAX 1
458
459/* Sort operations */
460#define SORT_OP_GET 0
461
462/* Log levels */
463#define LL_DEBUG 0
464#define LL_VERBOSE 1
465#define LL_NOTICE 2
466#define LL_WARNING 3
467#define LL_RAW (1<<10) /* Modifier to log without timestamp */
468
469/* Supervision options */
470#define SUPERVISED_NONE 0
471#define SUPERVISED_AUTODETECT 1
472#define SUPERVISED_SYSTEMD 2
473#define SUPERVISED_UPSTART 3
474
475/* Anti-warning macro... */
476#define UNUSED(V) ((void) V)
477
478#define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^64 elements */
479#define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
480
481/* Append only defines */
482#define AOF_FSYNC_NO 0
483#define AOF_FSYNC_ALWAYS 1
484#define AOF_FSYNC_EVERYSEC 2
485
486/* Replication diskless load defines */
487#define REPL_DISKLESS_LOAD_DISABLED 0
488#define REPL_DISKLESS_LOAD_WHEN_DB_EMPTY 1
489#define REPL_DISKLESS_LOAD_SWAPDB 2
490
491/* TLS Client Authentication */
492#define TLS_CLIENT_AUTH_NO 0
493#define TLS_CLIENT_AUTH_YES 1
494#define TLS_CLIENT_AUTH_OPTIONAL 2
495
496/* Sanitize dump payload */
497#define SANITIZE_DUMP_NO 0
498#define SANITIZE_DUMP_YES 1
499#define SANITIZE_DUMP_CLIENTS 2
500
501/* Enable protected config/command */
502#define PROTECTED_ACTION_ALLOWED_NO 0
503#define PROTECTED_ACTION_ALLOWED_YES 1
504#define PROTECTED_ACTION_ALLOWED_LOCAL 2
505
506/* Sets operations codes */
507#define SET_OP_UNION 0
508#define SET_OP_DIFF 1
509#define SET_OP_INTER 2
510
511/* oom-score-adj defines */
512#define OOM_SCORE_ADJ_NO 0
513#define OOM_SCORE_RELATIVE 1
514#define OOM_SCORE_ADJ_ABSOLUTE 2
515
516/* Redis maxmemory strategies. Instead of using just incremental number
517 * for this defines, we use a set of flags so that testing for certain
518 * properties common to multiple policies is faster. */
519#define MAXMEMORY_FLAG_LRU (1<<0)
520#define MAXMEMORY_FLAG_LFU (1<<1)
521#define MAXMEMORY_FLAG_ALLKEYS (1<<2)
522#define MAXMEMORY_FLAG_NO_SHARED_INTEGERS \
523 (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU)
524
525#define MAXMEMORY_VOLATILE_LRU ((0<<8)|MAXMEMORY_FLAG_LRU)
526#define MAXMEMORY_VOLATILE_LFU ((1<<8)|MAXMEMORY_FLAG_LFU)
527#define MAXMEMORY_VOLATILE_TTL (2<<8)
528#define MAXMEMORY_VOLATILE_RANDOM (3<<8)
529#define MAXMEMORY_ALLKEYS_LRU ((4<<8)|MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_ALLKEYS)
530#define MAXMEMORY_ALLKEYS_LFU ((5<<8)|MAXMEMORY_FLAG_LFU|MAXMEMORY_FLAG_ALLKEYS)
531#define MAXMEMORY_ALLKEYS_RANDOM ((6<<8)|MAXMEMORY_FLAG_ALLKEYS)
532#define MAXMEMORY_NO_EVICTION (7<<8)
533
534/* Units */
535#define UNIT_SECONDS 0
536#define UNIT_MILLISECONDS 1
537
538/* SHUTDOWN flags */
539#define SHUTDOWN_NOFLAGS 0 /* No flags. */
540#define SHUTDOWN_SAVE 1 /* Force SAVE on SHUTDOWN even if no save
541 points are configured. */
542#define SHUTDOWN_NOSAVE 2 /* Don't SAVE on SHUTDOWN. */
543#define SHUTDOWN_NOW 4 /* Don't wait for replicas to catch up. */
544#define SHUTDOWN_FORCE 8 /* Don't let errors prevent shutdown. */
545
546/* Command call flags, see call() function */
547#define CMD_CALL_NONE 0
548#define CMD_CALL_SLOWLOG (1<<0)
549#define CMD_CALL_STATS (1<<1)
550#define CMD_CALL_PROPAGATE_AOF (1<<2)
551#define CMD_CALL_PROPAGATE_REPL (1<<3)
552#define CMD_CALL_PROPAGATE (CMD_CALL_PROPAGATE_AOF|CMD_CALL_PROPAGATE_REPL)
553#define CMD_CALL_FULL (CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_PROPAGATE)
554#define CMD_CALL_FROM_MODULE (1<<4) /* From RM_Call */
555
556/* Command propagation flags, see propagateNow() function */
557#define PROPAGATE_NONE 0
558#define PROPAGATE_AOF 1
559#define PROPAGATE_REPL 2
560
561/* Client pause types, larger types are more restrictive
562 * pause types than smaller pause types. */
563typedef enum {
564 CLIENT_PAUSE_OFF = 0, /* Pause no commands */
565 CLIENT_PAUSE_WRITE, /* Pause write commands */
566 CLIENT_PAUSE_ALL /* Pause all commands */
567} pause_type;
568
569/* Client pause purposes. Each purpose has its own end time and pause type. */
570typedef enum {
571 PAUSE_BY_CLIENT_COMMAND = 0,
572 PAUSE_DURING_SHUTDOWN,
573 PAUSE_DURING_FAILOVER,
574 NUM_PAUSE_PURPOSES /* This value is the number of purposes above. */
575} pause_purpose;
576
577typedef struct {
578 pause_type type;
579 mstime_t end;
580} pause_event;
581
582/* Ways that a clusters endpoint can be described */
583typedef enum {
584 CLUSTER_ENDPOINT_TYPE_IP = 0, /* Show IP address */
585 CLUSTER_ENDPOINT_TYPE_HOSTNAME, /* Show hostname */
586 CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT /* Show NULL or empty */
587} cluster_endpoint_type;
588
589/* RDB active child save type. */
590#define RDB_CHILD_TYPE_NONE 0
591#define RDB_CHILD_TYPE_DISK 1 /* RDB is written to disk. */
592#define RDB_CHILD_TYPE_SOCKET 2 /* RDB is written to slave socket. */
593
594/* Keyspace changes notification classes. Every class is associated with a
595 * character for configuration purposes. */
596#define NOTIFY_KEYSPACE (1<<0) /* K */
597#define NOTIFY_KEYEVENT (1<<1) /* E */
598#define NOTIFY_GENERIC (1<<2) /* g */
599#define NOTIFY_STRING (1<<3) /* $ */
600#define NOTIFY_LIST (1<<4) /* l */
601#define NOTIFY_SET (1<<5) /* s */
602#define NOTIFY_HASH (1<<6) /* h */
603#define NOTIFY_ZSET (1<<7) /* z */
604#define NOTIFY_EXPIRED (1<<8) /* x */
605#define NOTIFY_EVICTED (1<<9) /* e */
606#define NOTIFY_STREAM (1<<10) /* t */
607#define NOTIFY_KEY_MISS (1<<11) /* m (Note: This one is excluded from NOTIFY_ALL on purpose) */
608#define NOTIFY_LOADED (1<<12) /* module only key space notification, indicate a key loaded from rdb */
609#define NOTIFY_MODULE (1<<13) /* d, module key space notification */
610#define NOTIFY_NEW (1<<14) /* n, new key notification */
611#define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED | NOTIFY_STREAM | NOTIFY_MODULE) /* A flag */
612
613/* Using the following macro you can run code inside serverCron() with the
614 * specified period, specified in milliseconds.
615 * The actual resolution depends on server.hz. */
616#define run_with_period(_ms_) if ((_ms_ <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz))))
617
618/* We can print the stacktrace, so our assert is defined this way: */
619#define serverAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_serverAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),redis_unreachable()))
620#define serverAssert(_e) ((_e)?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),redis_unreachable()))
621#define serverPanic(...) _serverPanic(__FILE__,__LINE__,__VA_ARGS__),redis_unreachable()
622
623/* latency histogram per command init settings */
624#define LATENCY_HISTOGRAM_MIN_VALUE 1L /* >= 1 nanosec */
625#define LATENCY_HISTOGRAM_MAX_VALUE 1000000000L /* <= 1 secs */
626#define LATENCY_HISTOGRAM_PRECISION 2 /* Maintain a value precision of 2 significant digits across LATENCY_HISTOGRAM_MIN_VALUE and LATENCY_HISTOGRAM_MAX_VALUE range.
627 * Value quantization within the range will thus be no larger than 1/100th (or 1%) of any value.
628 * The total size per histogram should sit around 40 KiB Bytes. */
629
630/* Busy module flags, see busy_module_yield_flags */
631#define BUSY_MODULE_YIELD_NONE (0)
632#define BUSY_MODULE_YIELD_EVENTS (1<<0)
633#define BUSY_MODULE_YIELD_CLIENTS (1<<1)
634
635/*-----------------------------------------------------------------------------
636 * Data types
637 *----------------------------------------------------------------------------*/
638
639/* A redis object, that is a type able to hold a string / list / set */
640
641/* The actual Redis Object */
642#define OBJ_STRING 0 /* String object. */
643#define OBJ_LIST 1 /* List object. */
644#define OBJ_SET 2 /* Set object. */
645#define OBJ_ZSET 3 /* Sorted set object. */
646#define OBJ_HASH 4 /* Hash object. */
647
648/* The "module" object type is a special one that signals that the object
649 * is one directly managed by a Redis module. In this case the value points
650 * to a moduleValue struct, which contains the object value (which is only
651 * handled by the module itself) and the RedisModuleType struct which lists
652 * function pointers in order to serialize, deserialize, AOF-rewrite and
653 * free the object.
654 *
655 * Inside the RDB file, module types are encoded as OBJ_MODULE followed
656 * by a 64 bit module type ID, which has a 54 bits module-specific signature
657 * in order to dispatch the loading to the right module, plus a 10 bits
658 * encoding version. */
659#define OBJ_MODULE 5 /* Module object. */
660#define OBJ_STREAM 6 /* Stream object. */
661
662/* Extract encver / signature from a module type ID. */
663#define REDISMODULE_TYPE_ENCVER_BITS 10
664#define REDISMODULE_TYPE_ENCVER_MASK ((1<<REDISMODULE_TYPE_ENCVER_BITS)-1)
665#define REDISMODULE_TYPE_ENCVER(id) (id & REDISMODULE_TYPE_ENCVER_MASK)
666#define REDISMODULE_TYPE_SIGN(id) ((id & ~((uint64_t)REDISMODULE_TYPE_ENCVER_MASK)) >>REDISMODULE_TYPE_ENCVER_BITS)
667
668/* Bit flags for moduleTypeAuxSaveFunc */
669#define REDISMODULE_AUX_BEFORE_RDB (1<<0)
670#define REDISMODULE_AUX_AFTER_RDB (1<<1)
671
672struct RedisModule;
673struct RedisModuleIO;
674struct RedisModuleDigest;
675struct RedisModuleCtx;
676struct moduleLoadQueueEntry;
677struct redisObject;
678struct RedisModuleDefragCtx;
679struct RedisModuleInfoCtx;
680struct RedisModuleKeyOptCtx;
681struct RedisModuleCommand;
682
683/* Each module type implementation should export a set of methods in order
684 * to serialize and deserialize the value in the RDB file, rewrite the AOF
685 * log, create the digest for "DEBUG DIGEST", and free the value when a key
686 * is deleted. */
687typedef void *(*moduleTypeLoadFunc)(struct RedisModuleIO *io, int encver);
688typedef void (*moduleTypeSaveFunc)(struct RedisModuleIO *io, void *value);
689typedef int (*moduleTypeAuxLoadFunc)(struct RedisModuleIO *rdb, int encver, int when);
690typedef void (*moduleTypeAuxSaveFunc)(struct RedisModuleIO *rdb, int when);
691typedef void (*moduleTypeRewriteFunc)(struct RedisModuleIO *io, struct redisObject *key, void *value);
692typedef void (*moduleTypeDigestFunc)(struct RedisModuleDigest *digest, void *value);
693typedef size_t (*moduleTypeMemUsageFunc)(const void *value);
694typedef void (*moduleTypeFreeFunc)(void *value);
695typedef size_t (*moduleTypeFreeEffortFunc)(struct redisObject *key, const void *value);
696typedef void (*moduleTypeUnlinkFunc)(struct redisObject *key, void *value);
697typedef void *(*moduleTypeCopyFunc)(struct redisObject *fromkey, struct redisObject *tokey, const void *value);
698typedef int (*moduleTypeDefragFunc)(struct RedisModuleDefragCtx *ctx, struct redisObject *key, void **value);
699typedef void (*RedisModuleInfoFunc)(struct RedisModuleInfoCtx *ctx, int for_crash_report);
700typedef void (*RedisModuleDefragFunc)(struct RedisModuleDefragCtx *ctx);
701typedef size_t (*moduleTypeMemUsageFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value, size_t sample_size);
702typedef void (*moduleTypeFreeFunc2)(struct RedisModuleKeyOptCtx *ctx, void *value);
703typedef size_t (*moduleTypeFreeEffortFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value);
704typedef void (*moduleTypeUnlinkFunc2)(struct RedisModuleKeyOptCtx *ctx, void *value);
705typedef void *(*moduleTypeCopyFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value);
706
707/* This callback type is called by moduleNotifyUserChanged() every time
708 * a user authenticated via the module API is associated with a different
709 * user or gets disconnected. This needs to be exposed since you can't cast
710 * a function pointer to (void *). */
711typedef void (*RedisModuleUserChangedFunc) (uint64_t client_id, void *privdata);
712
713
714/* The module type, which is referenced in each value of a given type, defines
715 * the methods and links to the module exporting the type. */
716typedef struct RedisModuleType {
717 uint64_t id; /* Higher 54 bits of type ID + 10 lower bits of encoding ver. */
718 struct RedisModule *module;
719 moduleTypeLoadFunc rdb_load;
720 moduleTypeSaveFunc rdb_save;
721 moduleTypeRewriteFunc aof_rewrite;
722 moduleTypeMemUsageFunc mem_usage;
723 moduleTypeDigestFunc digest;
724 moduleTypeFreeFunc free;
725 moduleTypeFreeEffortFunc free_effort;
726 moduleTypeUnlinkFunc unlink;
727 moduleTypeCopyFunc copy;
728 moduleTypeDefragFunc defrag;
729 moduleTypeAuxLoadFunc aux_load;
730 moduleTypeAuxSaveFunc aux_save;
731 moduleTypeMemUsageFunc2 mem_usage2;
732 moduleTypeFreeEffortFunc2 free_effort2;
733 moduleTypeUnlinkFunc2 unlink2;
734 moduleTypeCopyFunc2 copy2;
735 int aux_save_triggers;
736 char name[10]; /* 9 bytes name + null term. Charset: A-Z a-z 0-9 _- */
737} moduleType;
738
739/* In Redis objects 'robj' structures of type OBJ_MODULE, the value pointer
740 * is set to the following structure, referencing the moduleType structure
741 * in order to work with the value, and at the same time providing a raw
742 * pointer to the value, as created by the module commands operating with
743 * the module type.
744 *
745 * So for example in order to free such a value, it is possible to use
746 * the following code:
747 *
748 * if (robj->type == OBJ_MODULE) {
749 * moduleValue *mt = robj->ptr;
750 * mt->type->free(mt->value);
751 * zfree(mt); // We need to release this in-the-middle struct as well.
752 * }
753 */
754typedef struct moduleValue {
755 moduleType *type;
756 void *value;
757} moduleValue;
758
759/* This structure represents a module inside the system. */
760struct RedisModule {
761 void *handle; /* Module dlopen() handle. */
762 char *name; /* Module name. */
763 int ver; /* Module version. We use just progressive integers. */
764 int apiver; /* Module API version as requested during initialization.*/
765 list *types; /* Module data types. */
766 list *usedby; /* List of modules using APIs from this one. */
767 list *using; /* List of modules we use some APIs of. */
768 list *filters; /* List of filters the module has registered. */
769 list *module_configs; /* List of configurations the module has registered */
770 int configs_initialized; /* Have the module configurations been initialized? */
771 int in_call; /* RM_Call() nesting level */
772 int in_hook; /* Hooks callback nesting level for this module (0 or 1). */
773 int options; /* Module options and capabilities. */
774 int blocked_clients; /* Count of RedisModuleBlockedClient in this module. */
775 RedisModuleInfoFunc info_cb; /* Callback for module to add INFO fields. */
776 RedisModuleDefragFunc defrag_cb; /* Callback for global data defrag. */
777 struct moduleLoadQueueEntry *loadmod; /* Module load arguments for config rewrite. */
778};
779typedef struct RedisModule RedisModule;
780
781/* This is a wrapper for the 'rio' streams used inside rdb.c in Redis, so that
782 * the user does not have to take the total count of the written bytes nor
783 * to care about error conditions. */
784typedef struct RedisModuleIO {
785 size_t bytes; /* Bytes read / written so far. */
786 rio *rio; /* Rio stream. */
787 moduleType *type; /* Module type doing the operation. */
788 int error; /* True if error condition happened. */
789 int ver; /* Module serialization version: 1 (old),
790 * 2 (current version with opcodes annotation). */
791 struct RedisModuleCtx *ctx; /* Optional context, see RM_GetContextFromIO()*/
792 struct redisObject *key; /* Optional name of key processed */
793 int dbid; /* The dbid of the key being processed, -1 when unknown. */
794} RedisModuleIO;
795
796/* Macro to initialize an IO context. Note that the 'ver' field is populated
797 * inside rdb.c according to the version of the value to load. */
798#define moduleInitIOContext(iovar,mtype,rioptr,keyptr,db) do { \
799 iovar.rio = rioptr; \
800 iovar.type = mtype; \
801 iovar.bytes = 0; \
802 iovar.error = 0; \
803 iovar.ver = 0; \
804 iovar.key = keyptr; \
805 iovar.dbid = db; \
806 iovar.ctx = NULL; \
807} while(0)
808
809/* This is a structure used to export DEBUG DIGEST capabilities to Redis
810 * modules. We want to capture both the ordered and unordered elements of
811 * a data structure, so that a digest can be created in a way that correctly
812 * reflects the values. See the DEBUG DIGEST command implementation for more
813 * background. */
814typedef struct RedisModuleDigest {
815 unsigned char o[20]; /* Ordered elements. */
816 unsigned char x[20]; /* Xored elements. */
817 struct redisObject *key; /* Optional name of key processed */
818 int dbid; /* The dbid of the key being processed */
819} RedisModuleDigest;
820
821/* Just start with a digest composed of all zero bytes. */
822#define moduleInitDigestContext(mdvar) do { \
823 memset(mdvar.o,0,sizeof(mdvar.o)); \
824 memset(mdvar.x,0,sizeof(mdvar.x)); \
825} while(0)
826
827/* Objects encoding. Some kind of objects like Strings and Hashes can be
828 * internally represented in multiple ways. The 'encoding' field of the object
829 * is set to one of this fields for this object. */
830#define OBJ_ENCODING_RAW 0 /* Raw representation */
831#define OBJ_ENCODING_INT 1 /* Encoded as integer */
832#define OBJ_ENCODING_HT 2 /* Encoded as hash table */
833#define OBJ_ENCODING_ZIPMAP 3 /* No longer used: old hash encoding. */
834#define OBJ_ENCODING_LINKEDLIST 4 /* No longer used: old list encoding. */
835#define OBJ_ENCODING_ZIPLIST 5 /* No longer used: old list/hash/zset encoding. */
836#define OBJ_ENCODING_INTSET 6 /* Encoded as intset */
837#define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
838#define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding */
839#define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of listpacks */
840#define OBJ_ENCODING_STREAM 10 /* Encoded as a radix tree of listpacks */
841#define OBJ_ENCODING_LISTPACK 11 /* Encoded as a listpack */
842
843#define LRU_BITS 24
844#define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */
845#define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */
846
847#define OBJ_SHARED_REFCOUNT INT_MAX /* Global object never destroyed. */
848#define OBJ_STATIC_REFCOUNT (INT_MAX-1) /* Object allocated in the stack. */
849#define OBJ_FIRST_SPECIAL_REFCOUNT OBJ_STATIC_REFCOUNT
850typedef struct redisObject {
851 unsigned type:4;
852 unsigned encoding:4;
853 unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
854 * LFU data (least significant 8 bits frequency
855 * and most significant 16 bits access time). */
856 int refcount;
857 void *ptr;
858} robj;
859
860/* The a string name for an object's type as listed above
861 * Native types are checked against the OBJ_STRING, OBJ_LIST, OBJ_* defines,
862 * and Module types have their registered name returned. */
863char *getObjectTypeName(robj*);
864
865/* Macro used to initialize a Redis object allocated on the stack.
866 * Note that this macro is taken near the structure definition to make sure
867 * we'll update it when the structure is changed, to avoid bugs like
868 * bug #85 introduced exactly in this way. */
869#define initStaticStringObject(_var,_ptr) do { \
870 _var.refcount = OBJ_STATIC_REFCOUNT; \
871 _var.type = OBJ_STRING; \
872 _var.encoding = OBJ_ENCODING_RAW; \
873 _var.ptr = _ptr; \
874} while(0)
875
876struct evictionPoolEntry; /* Defined in evict.c */
877
878/* This structure is used in order to represent the output buffer of a client,
879 * which is actually a linked list of blocks like that, that is: client->reply. */
880typedef struct clientReplyBlock {
881 size_t size, used;
882 char buf[];
883} clientReplyBlock;
884
885/* Replication buffer blocks is the list of replBufBlock.
886 *
887 * +--------------+ +--------------+ +--------------+
888 * | refcount = 1 | ... | refcount = 0 | ... | refcount = 2 |
889 * +--------------+ +--------------+ +--------------+
890 * | / \
891 * | / \
892 * | / \
893 * Repl Backlog Replia_A Replia_B
894 *
895 * Each replica or replication backlog increments only the refcount of the
896 * 'ref_repl_buf_node' which it points to. So when replica walks to the next
897 * node, it should first increase the next node's refcount, and when we trim
898 * the replication buffer nodes, we remove node always from the head node which
899 * refcount is 0. If the refcount of the head node is not 0, we must stop
900 * trimming and never iterate the next node. */
901
902/* Similar with 'clientReplyBlock', it is used for shared buffers between
903 * all replica clients and replication backlog. */
904typedef struct replBufBlock {
905 int refcount; /* Number of replicas or repl backlog using. */
906 long long id; /* The unique incremental number. */
907 long long repl_offset; /* Start replication offset of the block. */
908 size_t size, used;
909 char buf[];
910} replBufBlock;
911
912/* Opaque type for the Slot to Key API. */
913typedef struct clusterSlotToKeyMapping clusterSlotToKeyMapping;
914
915/* Redis database representation. There are multiple databases identified
916 * by integers from 0 (the default database) up to the max configured
917 * database. The database number is the 'id' field in the structure. */
918typedef struct redisDb {
919 dict *dict; /* The keyspace for this DB */
920 dict *expires; /* Timeout of keys with a timeout set */
921 dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
922 dict *ready_keys; /* Blocked keys that received a PUSH */
923 dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
924 int id; /* Database ID */
925 long long avg_ttl; /* Average TTL, just for stats */
926 unsigned long expires_cursor; /* Cursor of the active expire cycle. */
927 list *defrag_later; /* List of key names to attempt to defrag one by one, gradually. */
928 clusterSlotToKeyMapping *slots_to_keys; /* Array of slots to keys. Only used in cluster mode (db 0). */
929} redisDb;
930
931/* forward declaration for functions ctx */
932typedef struct functionsLibCtx functionsLibCtx;
933
934/* Holding object that need to be populated during
935 * rdb loading. On loading end it is possible to decide
936 * whether not to set those objects on their rightful place.
937 * For example: dbarray need to be set as main database on
938 * successful loading and dropped on failure. */
939typedef struct rdbLoadingCtx {
940 redisDb* dbarray;
941 functionsLibCtx* functions_lib_ctx;
942}rdbLoadingCtx;
943
944/* Client MULTI/EXEC state */
945typedef struct multiCmd {
946 robj **argv;
947 int argv_len;
948 int argc;
949 struct redisCommand *cmd;
950} multiCmd;
951
952typedef struct multiState {
953 multiCmd *commands; /* Array of MULTI commands */
954 int count; /* Total number of MULTI commands */
955 int cmd_flags; /* The accumulated command flags OR-ed together.
956 So if at least a command has a given flag, it
957 will be set in this field. */
958 int cmd_inv_flags; /* Same as cmd_flags, OR-ing the ~flags. so that it
959 is possible to know if all the commands have a
960 certain flag. */
961 size_t argv_len_sums; /* mem used by all commands arguments */
962 int alloc_count; /* total number of multiCmd struct memory reserved. */
963} multiState;
964
965/* This structure holds the blocking operation state for a client.
966 * The fields used depend on client->btype. */
967typedef struct blockingState {
968 /* Generic fields. */
969 long count; /* Elements to pop if count was specified (BLMPOP/BZMPOP), -1 otherwise. */
970 mstime_t timeout; /* Blocking operation timeout. If UNIX current time
971 * is > timeout then the operation timed out. */
972
973 /* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM */
974 dict *keys; /* The keys we are waiting to terminate a blocking
975 * operation such as BLPOP or XREAD. Or NULL. */
976 robj *target; /* The key that should receive the element,
977 * for BLMOVE. */
978 struct blockPos {
979 int wherefrom; /* Where to pop from */
980 int whereto; /* Where to push to */
981 } blockpos; /* The positions in the src/dst lists/zsets
982 * where we want to pop/push an element
983 * for BLPOP, BRPOP, BLMOVE and BZMPOP. */
984
985 /* BLOCK_STREAM */
986 size_t xread_count; /* XREAD COUNT option. */
987 robj *xread_group; /* XREADGROUP group name. */
988 robj *xread_consumer; /* XREADGROUP consumer name. */
989 int xread_group_noack;
990
991 /* BLOCKED_WAIT */
992 int numreplicas; /* Number of replicas we are waiting for ACK. */
993 long long reploffset; /* Replication offset to reach. */
994
995 /* BLOCKED_MODULE */
996 void *module_blocked_handle; /* RedisModuleBlockedClient structure.
997 which is opaque for the Redis core, only
998 handled in module.c. */
999} blockingState;
1000
1001/* The following structure represents a node in the server.ready_keys list,
1002 * where we accumulate all the keys that had clients blocked with a blocking
1003 * operation such as B[LR]POP, but received new data in the context of the
1004 * last executed command.
1005 *
1006 * After the execution of every command or script, we run this list to check
1007 * if as a result we should serve data to clients blocked, unblocking them.
1008 * Note that server.ready_keys will not have duplicates as there dictionary
1009 * also called ready_keys in every structure representing a Redis database,
1010 * where we make sure to remember if a given key was already added in the
1011 * server.ready_keys list. */
1012typedef struct readyList {
1013 redisDb *db;
1014 robj *key;
1015} readyList;
1016
1017/* This structure represents a Redis user. This is useful for ACLs, the
1018 * user is associated to the connection after the connection is authenticated.
1019 * If there is no associated user, the connection uses the default user. */
1020#define USER_COMMAND_BITS_COUNT 1024 /* The total number of command bits
1021 in the user structure. The last valid
1022 command ID we can set in the user
1023 is USER_COMMAND_BITS_COUNT-1. */
1024#define USER_FLAG_ENABLED (1<<0) /* The user is active. */
1025#define USER_FLAG_DISABLED (1<<1) /* The user is disabled. */
1026#define USER_FLAG_NOPASS (1<<2) /* The user requires no password, any
1027 provided password will work. For the
1028 default user, this also means that
1029 no AUTH is needed, and every
1030 connection is immediately
1031 authenticated. */
1032#define USER_FLAG_SANITIZE_PAYLOAD (1<<3) /* The user require a deep RESTORE
1033 * payload sanitization. */
1034#define USER_FLAG_SANITIZE_PAYLOAD_SKIP (1<<4) /* The user should skip the
1035 * deep sanitization of RESTORE
1036 * payload. */
1037
1038#define SELECTOR_FLAG_ROOT (1<<0) /* This is the root user permission
1039 * selector. */
1040#define SELECTOR_FLAG_ALLKEYS (1<<1) /* The user can mention any key. */
1041#define SELECTOR_FLAG_ALLCOMMANDS (1<<2) /* The user can run all commands. */
1042#define SELECTOR_FLAG_ALLCHANNELS (1<<3) /* The user can mention any Pub/Sub
1043 channel. */
1044
1045typedef struct {
1046 sds name; /* The username as an SDS string. */
1047 uint32_t flags; /* See USER_FLAG_* */
1048 list *passwords; /* A list of SDS valid passwords for this user. */
1049 list *selectors; /* A list of selectors this user validates commands
1050 against. This list will always contain at least
1051 one selector for backwards compatibility. */
1052} user;
1053
1054/* With multiplexing we need to take per-client state.
1055 * Clients are taken in a linked list. */
1056
1057#define CLIENT_ID_AOF (UINT64_MAX) /* Reserved ID for the AOF client. If you
1058 need more reserved IDs use UINT64_MAX-1,
1059 -2, ... and so forth. */
1060
1061/* Replication backlog is not separate memory, it just is one consumer of
1062 * the global replication buffer. This structure records the reference of
1063 * replication buffers. Since the replication buffer block list may be very long,
1064 * it would cost much time to search replication offset on partial resync, so
1065 * we use one rax tree to index some blocks every REPL_BACKLOG_INDEX_PER_BLOCKS
1066 * to make searching offset from replication buffer blocks list faster. */
1067typedef struct replBacklog {
1068 listNode *ref_repl_buf_node; /* Referenced node of replication buffer blocks,
1069 * see the definition of replBufBlock. */
1070 size_t unindexed_count; /* The count from last creating index block. */
1071 rax *blocks_index; /* The index of recorded blocks of replication
1072 * buffer for quickly searching replication
1073 * offset on partial resynchronization. */
1074 long long histlen; /* Backlog actual data length */
1075 long long offset; /* Replication "master offset" of first
1076 * byte in the replication backlog buffer.*/
1077} replBacklog;
1078
1079typedef struct {
1080 list *clients;
1081 size_t mem_usage_sum;
1082} clientMemUsageBucket;
1083
1084typedef struct client {
1085 uint64_t id; /* Client incremental unique ID. */
1086 uint64_t flags; /* Client flags: CLIENT_* macros. */
1087 connection *conn;
1088 int resp; /* RESP protocol version. Can be 2 or 3. */
1089 redisDb *db; /* Pointer to currently SELECTed DB. */
1090 robj *name; /* As set by CLIENT SETNAME. */
1091 sds querybuf; /* Buffer we use to accumulate client queries. */
1092 size_t qb_pos; /* The position we have read in querybuf. */
1093 size_t querybuf_peak; /* Recent (100ms or more) peak of querybuf size. */
1094 int argc; /* Num of arguments of current command. */
1095 robj **argv; /* Arguments of current command. */
1096 int argv_len; /* Size of argv array (may be more than argc) */
1097 int original_argc; /* Num of arguments of original command if arguments were rewritten. */
1098 robj **original_argv; /* Arguments of original command if arguments were rewritten. */
1099 size_t argv_len_sum; /* Sum of lengths of objects in argv list. */
1100 struct redisCommand *cmd, *lastcmd; /* Last command executed. */
1101 struct redisCommand *realcmd; /* The original command that was executed by the client,
1102 Used to update error stats in case the c->cmd was modified
1103 during the command invocation (like on GEOADD for example). */
1104 user *user; /* User associated with this connection. If the
1105 user is set to NULL the connection can do
1106 anything (admin). */
1107 int reqtype; /* Request protocol type: PROTO_REQ_* */
1108 int multibulklen; /* Number of multi bulk arguments left to read. */
1109 long bulklen; /* Length of bulk argument in multi bulk request. */
1110 list *reply; /* List of reply objects to send to the client. */
1111 unsigned long long reply_bytes; /* Tot bytes of objects in reply list. */
1112 list *deferred_reply_errors; /* Used for module thread safe contexts. */
1113 size_t sentlen; /* Amount of bytes already sent in the current
1114 buffer or object being sent. */
1115 time_t ctime; /* Client creation time. */
1116 long duration; /* Current command duration. Used for measuring latency of blocking/non-blocking cmds */
1117 int slot; /* The slot the client is executing against. Set to -1 if no slot is being used */
1118 time_t lastinteraction; /* Time of the last interaction, used for timeout */
1119 time_t obuf_soft_limit_reached_time;
1120 int authenticated; /* Needed when the default user requires auth. */
1121 int replstate; /* Replication state if this is a slave. */
1122 int repl_start_cmd_stream_on_ack; /* Install slave write handler on first ACK. */
1123 int repldbfd; /* Replication DB file descriptor. */
1124 off_t repldboff; /* Replication DB file offset. */
1125 off_t repldbsize; /* Replication DB file size. */
1126 sds replpreamble; /* Replication DB preamble. */
1127 long long read_reploff; /* Read replication offset if this is a master. */
1128 long long reploff; /* Applied replication offset if this is a master. */
1129 long long repl_applied; /* Applied replication data count in querybuf, if this is a replica. */
1130 long long repl_ack_off; /* Replication ack offset, if this is a slave. */
1131 long long repl_ack_time;/* Replication ack time, if this is a slave. */
1132 long long repl_last_partial_write; /* The last time the server did a partial write from the RDB child pipe to this replica */
1133 long long psync_initial_offset; /* FULLRESYNC reply offset other slaves
1134 copying this slave output buffer
1135 should use. */
1136 char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */
1137 int slave_listening_port; /* As configured with: REPLCONF listening-port */
1138 char *slave_addr; /* Optionally given by REPLCONF ip-address */
1139 int slave_capa; /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */
1140 int slave_req; /* Slave requirements: SLAVE_REQ_* */
1141 multiState mstate; /* MULTI/EXEC state */
1142 int btype; /* Type of blocking op if CLIENT_BLOCKED. */
1143 blockingState bpop; /* blocking state */
1144 long long woff; /* Last write global replication offset. */
1145 list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */
1146 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
1147 list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */
1148 dict *pubsubshard_channels; /* shard level channels a client is interested in (SSUBSCRIBE) */
1149 sds peerid; /* Cached peer ID. */
1150 sds sockname; /* Cached connection target address. */
1151 listNode *client_list_node; /* list node in client list */
1152 listNode *postponed_list_node; /* list node within the postponed list */
1153 listNode *pending_read_list_node; /* list node in clients pending read list */
1154 RedisModuleUserChangedFunc auth_callback; /* Module callback to execute
1155 * when the authenticated user
1156 * changes. */
1157 void *auth_callback_privdata; /* Private data that is passed when the auth
1158 * changed callback is executed. Opaque for
1159 * Redis Core. */
1160 void *auth_module; /* The module that owns the callback, which is used
1161 * to disconnect the client if the module is
1162 * unloaded for cleanup. Opaque for Redis Core.*/
1163
1164 /* If this client is in tracking mode and this field is non zero,
1165 * invalidation messages for keys fetched by this client will be send to
1166 * the specified client ID. */
1167 uint64_t client_tracking_redirection;
1168 rax *client_tracking_prefixes; /* A dictionary of prefixes we are already
1169 subscribed to in BCAST mode, in the
1170 context of client side caching. */
1171 /* In updateClientMemUsage() we track the memory usage of
1172 * each client and add it to the sum of all the clients of a given type,
1173 * however we need to remember what was the old contribution of each
1174 * client, and in which category the client was, in order to remove it
1175 * before adding it the new value. */
1176 size_t last_memory_usage;
1177 int last_memory_type;
1178
1179 listNode *mem_usage_bucket_node;
1180 clientMemUsageBucket *mem_usage_bucket;
1181
1182 listNode *ref_repl_buf_node; /* Referenced node of replication buffer blocks,
1183 * see the definition of replBufBlock. */
1184 size_t ref_block_pos; /* Access position of referenced buffer block,
1185 * i.e. the next offset to send. */
1186
1187 /* Response buffer */
1188 size_t buf_peak; /* Peak used size of buffer in last 5 sec interval. */
1189 mstime_t buf_peak_last_reset_time; /* keeps the last time the buffer peak value was reset */
1190 int bufpos;
1191 size_t buf_usable_size; /* Usable size of buffer. */
1192 char *buf;
1193} client;
1194
1195struct saveparam {
1196 time_t seconds;
1197 int changes;
1198};
1199
1200struct moduleLoadQueueEntry {
1201 sds path;
1202 int argc;
1203 robj **argv;
1204};
1205
1206struct sentinelLoadQueueEntry {
1207 int argc;
1208 sds *argv;
1209 int linenum;
1210 sds line;
1211};
1212
1213struct sentinelConfig {
1214 list *pre_monitor_cfg;
1215 list *monitor_cfg;
1216 list *post_monitor_cfg;
1217};
1218
1219struct sharedObjectsStruct {
1220 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
1221 *queued, *null[4], *nullarray[4], *emptymap[4], *emptyset[4],
1222 *emptyarray, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
1223 *outofrangeerr, *noscripterr, *loadingerr,
1224 *slowevalerr, *slowscripterr, *slowmoduleerr, *bgsaveerr,
1225 *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr,
1226 *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
1227 *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,
1228 *rpop, *lpop, *lpush, *rpoplpush, *lmove, *blmove, *zpopmin, *zpopmax,
1229 *emptyscan, *multi, *exec, *left, *right, *hset, *srem, *xgroup, *xclaim,
1230 *script, *replconf, *eval, *persist, *set, *pexpireat, *pexpire,
1231 *time, *pxat, *absttl, *retrycount, *force, *justid, *entriesread,
1232 *lastid, *ping, *setid, *keepttl, *load, *createconsumer,
1233 *getack, *special_asterick, *special_equals, *default_username, *redacted,
1234 *ssubscribebulk,*sunsubscribebulk, *smessagebulk,
1235 *select[PROTO_SHARED_SELECT_CMDS],
1236 *integers[OBJ_SHARED_INTEGERS],
1237 *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
1238 *bulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "$<value>\r\n" */
1239 *maphdr[OBJ_SHARED_BULKHDR_LEN], /* "%<value>\r\n" */
1240 *sethdr[OBJ_SHARED_BULKHDR_LEN]; /* "~<value>\r\n" */
1241 sds minstring, maxstring;
1242};
1243
1244/* ZSETs use a specialized version of Skiplists */
1245typedef struct zskiplistNode {
1246 sds ele;
1247 double score;
1248 struct zskiplistNode *backward;
1249 struct zskiplistLevel {
1250 struct zskiplistNode *forward;
1251 unsigned long span;
1252 } level[];
1253} zskiplistNode;
1254
1255typedef struct zskiplist {
1256 struct zskiplistNode *header, *tail;
1257 unsigned long length;
1258 int level;
1259} zskiplist;
1260
1261typedef struct zset {
1262 dict *dict;
1263 zskiplist *zsl;
1264} zset;
1265
1266typedef struct clientBufferLimitsConfig {
1267 unsigned long long hard_limit_bytes;
1268 unsigned long long soft_limit_bytes;
1269 time_t soft_limit_seconds;
1270} clientBufferLimitsConfig;
1271
1272extern clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT];
1273
1274/* The redisOp structure defines a Redis Operation, that is an instance of
1275 * a command with an argument vector, database ID, propagation target
1276 * (PROPAGATE_*), and command pointer.
1277 *
1278 * Currently only used to additionally propagate more commands to AOF/Replication
1279 * after the propagation of the executed command. */
1280typedef struct redisOp {
1281 robj **argv;
1282 int argc, dbid, target;
1283} redisOp;
1284
1285/* Defines an array of Redis operations. There is an API to add to this
1286 * structure in an easy way.
1287 *
1288 * redisOpArrayInit();
1289 * redisOpArrayAppend();
1290 * redisOpArrayFree();
1291 */
1292typedef struct redisOpArray {
1293 redisOp *ops;
1294 int numops;
1295 int capacity;
1296} redisOpArray;
1297
1298/* This structure is returned by the getMemoryOverheadData() function in
1299 * order to return memory overhead information. */
1300struct redisMemOverhead {
1301 size_t peak_allocated;
1302 size_t total_allocated;
1303 size_t startup_allocated;
1304 size_t repl_backlog;
1305 size_t clients_slaves;
1306 size_t clients_normal;
1307 size_t cluster_links;
1308 size_t aof_buffer;
1309 size_t lua_caches;
1310 size_t functions_caches;
1311 size_t overhead_total;
1312 size_t dataset;
1313 size_t total_keys;
1314 size_t bytes_per_key;
1315 float dataset_perc;
1316 float peak_perc;
1317 float total_frag;
1318 ssize_t total_frag_bytes;
1319 float allocator_frag;
1320 ssize_t allocator_frag_bytes;
1321 float allocator_rss;
1322 ssize_t allocator_rss_bytes;
1323 float rss_extra;
1324 size_t rss_extra_bytes;
1325 size_t num_dbs;
1326 struct {
1327 size_t dbid;
1328 size_t overhead_ht_main;
1329 size_t overhead_ht_expires;
1330 size_t overhead_ht_slot_to_keys;
1331 } *db;
1332};
1333
1334/* Replication error behavior determines the replica behavior
1335 * when it receives an error over the replication stream. In
1336 * either case the error is logged. */
1337typedef enum {
1338 PROPAGATION_ERR_BEHAVIOR_IGNORE = 0,
1339 PROPAGATION_ERR_BEHAVIOR_PANIC,
1340 PROPAGATION_ERR_BEHAVIOR_PANIC_ON_REPLICAS
1341} replicationErrorBehavior;
1342
1343/* This structure can be optionally passed to RDB save/load functions in
1344 * order to implement additional functionalities, by storing and loading
1345 * metadata to the RDB file.
1346 *
1347 * For example, to use select a DB at load time, useful in
1348 * replication in order to make sure that chained slaves (slaves of slaves)
1349 * select the correct DB and are able to accept the stream coming from the
1350 * top-level master. */
1351typedef struct rdbSaveInfo {
1352 /* Used saving and loading. */
1353 int repl_stream_db; /* DB to select in server.master client. */
1354
1355 /* Used only loading. */
1356 int repl_id_is_set; /* True if repl_id field is set. */
1357 char repl_id[CONFIG_RUN_ID_SIZE+1]; /* Replication ID. */
1358 long long repl_offset; /* Replication offset. */
1359} rdbSaveInfo;
1360
1361#define RDB_SAVE_INFO_INIT {-1,0,"0000000000000000000000000000000000000000",-1}
1362
1363struct malloc_stats {
1364 size_t zmalloc_used;
1365 size_t process_rss;
1366 size_t allocator_allocated;
1367 size_t allocator_active;
1368 size_t allocator_resident;
1369};
1370
1371typedef struct socketFds {
1372 int fd[CONFIG_BINDADDR_MAX];
1373 int count;
1374} socketFds;
1375
1376/*-----------------------------------------------------------------------------
1377 * TLS Context Configuration
1378 *----------------------------------------------------------------------------*/
1379
1380typedef struct redisTLSContextConfig {
1381 char *cert_file; /* Server side and optionally client side cert file name */
1382 char *key_file; /* Private key filename for cert_file */
1383 char *key_file_pass; /* Optional password for key_file */
1384 char *client_cert_file; /* Certificate to use as a client; if none, use cert_file */
1385 char *client_key_file; /* Private key filename for client_cert_file */
1386 char *client_key_file_pass; /* Optional password for client_key_file */
1387 char *dh_params_file;
1388 char *ca_cert_file;
1389 char *ca_cert_dir;
1390 char *protocols;
1391 char *ciphers;
1392 char *ciphersuites;
1393 int prefer_server_ciphers;
1394 int session_caching;
1395 int session_cache_size;
1396 int session_cache_timeout;
1397} redisTLSContextConfig;
1398
1399/*-----------------------------------------------------------------------------
1400 * AOF manifest definition
1401 *----------------------------------------------------------------------------*/
1402typedef enum {
1403 AOF_FILE_TYPE_BASE = 'b', /* BASE file */
1404 AOF_FILE_TYPE_HIST = 'h', /* HISTORY file */
1405 AOF_FILE_TYPE_INCR = 'i', /* INCR file */
1406} aof_file_type;
1407
1408typedef struct {
1409 sds file_name; /* file name */
1410 long long file_seq; /* file sequence */
1411 aof_file_type file_type; /* file type */
1412} aofInfo;
1413
1414typedef struct {
1415 aofInfo *base_aof_info; /* BASE file information. NULL if there is no BASE file. */
1416 list *incr_aof_list; /* INCR AOFs list. We may have multiple INCR AOF when rewrite fails. */
1417 list *history_aof_list; /* HISTORY AOF list. When the AOFRW success, The aofInfo contained in
1418 `base_aof_info` and `incr_aof_list` will be moved to this list. We
1419 will delete these AOF files when AOFRW finish. */
1420 long long curr_base_file_seq; /* The sequence number used by the current BASE file. */
1421 long long curr_incr_file_seq; /* The sequence number used by the current INCR file. */
1422 int dirty; /* 1 Indicates that the aofManifest in the memory is inconsistent with
1423 disk, we need to persist it immediately. */
1424} aofManifest;
1425
1426/*-----------------------------------------------------------------------------
1427 * Global server state
1428 *----------------------------------------------------------------------------*/
1429
1430/* AIX defines hz to __hz, we don't use this define and in order to allow
1431 * Redis build on AIX we need to undef it. */
1432#ifdef _AIX
1433#undef hz
1434#endif
1435
1436#define CHILD_TYPE_NONE 0
1437#define CHILD_TYPE_RDB 1
1438#define CHILD_TYPE_AOF 2
1439#define CHILD_TYPE_LDB 3
1440#define CHILD_TYPE_MODULE 4
1441
1442typedef enum childInfoType {
1443 CHILD_INFO_TYPE_CURRENT_INFO,
1444 CHILD_INFO_TYPE_AOF_COW_SIZE,
1445 CHILD_INFO_TYPE_RDB_COW_SIZE,
1446 CHILD_INFO_TYPE_MODULE_COW_SIZE
1447} childInfoType;
1448
1449struct redisServer {
1450 /* General */
1451 pid_t pid; /* Main process pid. */
1452 pthread_t main_thread_id; /* Main thread id */
1453 char *configfile; /* Absolute config file path, or NULL */
1454 char *executable; /* Absolute executable file path. */
1455 char **exec_argv; /* Executable argv vector (copy). */
1456 int dynamic_hz; /* Change hz value depending on # of clients. */
1457 int config_hz; /* Configured HZ value. May be different than
1458 the actual 'hz' field value if dynamic-hz
1459 is enabled. */
1460 mode_t umask; /* The umask value of the process on startup */
1461 int hz; /* serverCron() calls frequency in hertz */
1462 int in_fork_child; /* indication that this is a fork child */
1463 redisDb *db;
1464 dict *commands; /* Command table */
1465 dict *orig_commands; /* Command table before command renaming. */
1466 aeEventLoop *el;
1467 rax *errors; /* Errors table */
1468 redisAtomic unsigned int lruclock; /* Clock for LRU eviction */
1469 volatile sig_atomic_t shutdown_asap; /* Shutdown ordered by signal handler. */
1470 mstime_t shutdown_mstime; /* Timestamp to limit graceful shutdown. */
1471 int last_sig_received; /* Indicates the last SIGNAL received, if any (e.g., SIGINT or SIGTERM). */
1472 int shutdown_flags; /* Flags passed to prepareForShutdown(). */
1473 int activerehashing; /* Incremental rehash in serverCron() */
1474 int active_defrag_running; /* Active defragmentation running (holds current scan aggressiveness) */
1475 char *pidfile; /* PID file path */
1476 int arch_bits; /* 32 or 64 depending on sizeof(long) */
1477 int cronloops; /* Number of times the cron function run */
1478 char runid[CONFIG_RUN_ID_SIZE+1]; /* ID always different at every exec. */
1479 int sentinel_mode; /* True if this instance is a Sentinel. */
1480 size_t initial_memory_usage; /* Bytes used after initialization. */
1481 int ; /* Show logo even for non-stdout logging. */
1482 int in_exec; /* Are we inside EXEC? */
1483 int busy_module_yield_flags; /* Are we inside a busy module? (triggered by RM_Yield). see BUSY_MODULE_YIELD_ flags. */
1484 const char *busy_module_yield_reply; /* When non-null, we are inside RM_Yield. */
1485 int core_propagates; /* Is the core (in oppose to the module subsystem) is in charge of calling propagatePendingCommands? */
1486 int propagate_no_multi; /* True if propagatePendingCommands should avoid wrapping command in MULTI/EXEC */
1487 int module_ctx_nesting; /* moduleCreateContext() nesting level */
1488 char *ignore_warnings; /* Config: warnings that should be ignored. */
1489 int client_pause_in_transaction; /* Was a client pause executed during this Exec? */
1490 int thp_enabled; /* If true, THP is enabled. */
1491 size_t page_size; /* The page size of OS. */
1492 /* Modules */
1493 dict *moduleapi; /* Exported core APIs dictionary for modules. */
1494 dict *sharedapi; /* Like moduleapi but containing the APIs that
1495 modules share with each other. */
1496 dict *module_configs_queue; /* Dict that stores module configurations from .conf file until after modules are loaded during startup or arguments to loadex. */
1497 list *loadmodule_queue; /* List of modules to load at startup. */
1498 int module_pipe[2]; /* Pipe used to awake the event loop by module threads. */
1499 pid_t child_pid; /* PID of current child */
1500 int child_type; /* Type of current child */
1501 /* Networking */
1502 int port; /* TCP listening port */
1503 int tls_port; /* TLS listening port */
1504 int tcp_backlog; /* TCP listen() backlog */
1505 char *bindaddr[CONFIG_BINDADDR_MAX]; /* Addresses we should bind to */
1506 int bindaddr_count; /* Number of addresses in server.bindaddr[] */
1507 char *bind_source_addr; /* Source address to bind on for outgoing connections */
1508 char *unixsocket; /* UNIX socket path */
1509 unsigned int unixsocketperm; /* UNIX socket permission (see mode_t) */
1510 socketFds ipfd; /* TCP socket file descriptors */
1511 socketFds tlsfd; /* TLS socket file descriptors */
1512 int sofd; /* Unix socket file descriptor */
1513 uint32_t socket_mark_id; /* ID for listen socket marking */
1514 socketFds cfd; /* Cluster bus listening socket */
1515 list *clients; /* List of active clients */
1516 list *clients_to_close; /* Clients to close asynchronously */
1517 list *clients_pending_write; /* There is to write or install handler. */
1518 list *clients_pending_read; /* Client has pending read socket buffers. */
1519 list *slaves, *monitors; /* List of slaves and MONITORs */
1520 client *current_client; /* Current client executing the command. */
1521
1522 /* Stuff for client mem eviction */
1523 clientMemUsageBucket client_mem_usage_buckets[CLIENT_MEM_USAGE_BUCKETS];
1524
1525 rax *clients_timeout_table; /* Radix tree for blocked clients timeouts. */
1526 long fixed_time_expire; /* If > 0, expire keys against server.mstime. */
1527 int in_nested_call; /* If > 0, in a nested call of a call */
1528 rax *clients_index; /* Active clients dictionary by client ID. */
1529 pause_type client_pause_type; /* True if clients are currently paused */
1530 list *postponed_clients; /* List of postponed clients */
1531 mstime_t client_pause_end_time; /* Time when we undo clients_paused */
1532 pause_event *client_pause_per_purpose[NUM_PAUSE_PURPOSES];
1533 char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */
1534 dict *migrate_cached_sockets;/* MIGRATE cached sockets */
1535 redisAtomic uint64_t next_client_id; /* Next client unique ID. Incremental. */
1536 int protected_mode; /* Don't accept external connections. */
1537 int io_threads_num; /* Number of IO threads to use. */
1538 int io_threads_do_reads; /* Read and parse from IO threads? */
1539 int io_threads_active; /* Is IO threads currently active? */
1540 long long events_processed_while_blocked; /* processEventsWhileBlocked() */
1541 int enable_protected_configs; /* Enable the modification of protected configs, see PROTECTED_ACTION_ALLOWED_* */
1542 int enable_debug_cmd; /* Enable DEBUG commands, see PROTECTED_ACTION_ALLOWED_* */
1543 int enable_module_cmd; /* Enable MODULE commands, see PROTECTED_ACTION_ALLOWED_* */
1544
1545 /* RDB / AOF loading information */
1546 volatile sig_atomic_t loading; /* We are loading data from disk if true */
1547 volatile sig_atomic_t async_loading; /* We are loading data without blocking the db being served */
1548 off_t loading_total_bytes;
1549 off_t loading_rdb_used_mem;
1550 off_t loading_loaded_bytes;
1551 time_t loading_start_time;
1552 off_t loading_process_events_interval_bytes;
1553 /* Fields used only for stats */
1554 time_t stat_starttime; /* Server start time */
1555 long long stat_numcommands; /* Number of processed commands */
1556 long long stat_numconnections; /* Number of connections received */
1557 long long stat_expiredkeys; /* Number of expired keys */
1558 double stat_expired_stale_perc; /* Percentage of keys probably expired */
1559 long long stat_expired_time_cap_reached_count; /* Early expire cycle stops.*/
1560 long long stat_expire_cycle_time_used; /* Cumulative microseconds used. */
1561 long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */
1562 long long stat_evictedclients; /* Number of evicted clients */
1563 long long stat_total_eviction_exceeded_time; /* Total time over the memory limit, unit us */
1564 monotime stat_last_eviction_exceeded_time; /* Timestamp of current eviction start, unit us */
1565 long long stat_keyspace_hits; /* Number of successful lookups of keys */
1566 long long stat_keyspace_misses; /* Number of failed lookups of keys */
1567 long long stat_active_defrag_hits; /* number of allocations moved */
1568 long long stat_active_defrag_misses; /* number of allocations scanned but not moved */
1569 long long stat_active_defrag_key_hits; /* number of keys with moved allocations */
1570 long long stat_active_defrag_key_misses;/* number of keys scanned and not moved */
1571 long long stat_active_defrag_scanned; /* number of dictEntries scanned */
1572 long long stat_total_active_defrag_time; /* Total time memory fragmentation over the limit, unit us */
1573 monotime stat_last_active_defrag_time; /* Timestamp of current active defrag start */
1574 size_t stat_peak_memory; /* Max used memory record */
1575 long long stat_aof_rewrites; /* number of aof file rewrites performed */
1576 long long stat_aofrw_consecutive_failures; /* The number of consecutive failures of aofrw */
1577 long long stat_rdb_saves; /* number of rdb saves performed */
1578 long long stat_fork_time; /* Time needed to perform latest fork() */
1579 double stat_fork_rate; /* Fork rate in GB/sec. */
1580 long long stat_total_forks; /* Total count of fork. */
1581 long long stat_rejected_conn; /* Clients rejected because of maxclients */
1582 long long stat_sync_full; /* Number of full resyncs with slaves. */
1583 long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */
1584 long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */
1585 list *slowlog; /* SLOWLOG list of commands */
1586 long long slowlog_entry_id; /* SLOWLOG current entry ID */
1587 long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
1588 unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */
1589 struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */
1590 redisAtomic long long stat_net_input_bytes; /* Bytes read from network. */
1591 redisAtomic long long stat_net_output_bytes; /* Bytes written to network. */
1592 redisAtomic long long stat_net_repl_input_bytes; /* Bytes read during replication, added to stat_net_input_bytes in 'info'. */
1593 redisAtomic long long stat_net_repl_output_bytes; /* Bytes written during replication, added to stat_net_output_bytes in 'info'. */
1594 size_t stat_current_cow_peak; /* Peak size of copy on write bytes. */
1595 size_t stat_current_cow_bytes; /* Copy on write bytes while child is active. */
1596 monotime stat_current_cow_updated; /* Last update time of stat_current_cow_bytes */
1597 size_t stat_current_save_keys_processed; /* Processed keys while child is active. */
1598 size_t stat_current_save_keys_total; /* Number of keys when child started. */
1599 size_t stat_rdb_cow_bytes; /* Copy on write bytes during RDB saving. */
1600 size_t stat_aof_cow_bytes; /* Copy on write bytes during AOF rewrite. */
1601 size_t stat_module_cow_bytes; /* Copy on write bytes during module fork. */
1602 double stat_module_progress; /* Module save progress. */
1603 size_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */
1604 size_t stat_cluster_links_memory; /* Mem usage by cluster links */
1605 long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */
1606 long long stat_total_error_replies; /* Total number of issued error replies ( command + rejected errors ) */
1607 long long stat_dump_payload_sanitizations; /* Number deep dump payloads integrity validations. */
1608 long long stat_io_reads_processed; /* Number of read events processed by IO / Main threads */
1609 long long stat_io_writes_processed; /* Number of write events processed by IO / Main threads */
1610 redisAtomic long long stat_total_reads_processed; /* Total number of read events processed */
1611 redisAtomic long long stat_total_writes_processed; /* Total number of write events processed */
1612 /* The following two are used to track instantaneous metrics, like
1613 * number of operations per second, network traffic. */
1614 struct {
1615 long long last_sample_time; /* Timestamp of last sample in ms */
1616 long long last_sample_count;/* Count in last sample */
1617 long long samples[STATS_METRIC_SAMPLES];
1618 int idx;
1619 } inst_metric[STATS_METRIC_COUNT];
1620 long long stat_reply_buffer_shrinks; /* Total number of output buffer shrinks */
1621 long long stat_reply_buffer_expands; /* Total number of output buffer expands */
1622
1623 /* Configuration */
1624 int verbosity; /* Loglevel in redis.conf */
1625 int maxidletime; /* Client timeout in seconds */
1626 int tcpkeepalive; /* Set SO_KEEPALIVE if non-zero. */
1627 int active_expire_enabled; /* Can be disabled for testing purposes. */
1628 int active_expire_effort; /* From 1 (default) to 10, active effort. */
1629 int active_defrag_enabled;
1630 int sanitize_dump_payload; /* Enables deep sanitization for ziplist and listpack in RDB and RESTORE. */
1631 int skip_checksum_validation; /* Disable checksum validation for RDB and RESTORE payload. */
1632 int jemalloc_bg_thread; /* Enable jemalloc background thread */
1633 size_t active_defrag_ignore_bytes; /* minimum amount of fragmentation waste to start active defrag */
1634 int active_defrag_threshold_lower; /* minimum percentage of fragmentation to start active defrag */
1635 int active_defrag_threshold_upper; /* maximum percentage of fragmentation at which we use maximum effort */
1636 int active_defrag_cycle_min; /* minimal effort for defrag in CPU percentage */
1637 int active_defrag_cycle_max; /* maximal effort for defrag in CPU percentage */
1638 unsigned long active_defrag_max_scan_fields; /* maximum number of fields of set/hash/zset/list to process from within the main dict scan */
1639 size_t client_max_querybuf_len; /* Limit for client query buffer length */
1640 int dbnum; /* Total number of configured DBs */
1641 int supervised; /* 1 if supervised, 0 otherwise. */
1642 int supervised_mode; /* See SUPERVISED_* */
1643 int daemonize; /* True if running as a daemon */
1644 int set_proc_title; /* True if change proc title */
1645 char *proc_title_template; /* Process title template format */
1646 clientBufferLimitsConfig client_obuf_limits[CLIENT_TYPE_OBUF_COUNT];
1647 int pause_cron; /* Don't run cron tasks (debug) */
1648 int latency_tracking_enabled; /* 1 if extended latency tracking is enabled, 0 otherwise. */
1649 double *latency_tracking_info_percentiles; /* Extended latency tracking info output percentile list configuration. */
1650 int latency_tracking_info_percentiles_len;
1651 /* AOF persistence */
1652 int aof_enabled; /* AOF configuration */
1653 int aof_state; /* AOF_(ON|OFF|WAIT_REWRITE) */
1654 int aof_fsync; /* Kind of fsync() policy */
1655 char *aof_filename; /* Basename of the AOF file and manifest file */
1656 char *aof_dirname; /* Name of the AOF directory */
1657 int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */
1658 int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */
1659 off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */
1660 off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */
1661 off_t aof_current_size; /* AOF current size (Including BASE + INCRs). */
1662 off_t aof_last_incr_size; /* The size of the latest incr AOF. */
1663 off_t aof_fsync_offset; /* AOF offset which is already synced to disk. */
1664 int aof_flush_sleep; /* Micros to sleep before flush. (used by tests) */
1665 int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */
1666 sds aof_buf; /* AOF buffer, written before entering the event loop */
1667 int aof_fd; /* File descriptor of currently selected AOF file */
1668 int aof_selected_db; /* Currently selected DB in AOF */
1669 time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */
1670 time_t aof_last_fsync; /* UNIX time of last fsync() */
1671 time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */
1672 time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */
1673 time_t aof_cur_timestamp; /* Current record timestamp in AOF */
1674 int aof_timestamp_enabled; /* Enable record timestamp in AOF */
1675 int aof_lastbgrewrite_status; /* C_OK or C_ERR */
1676 unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */
1677 int aof_rewrite_incremental_fsync;/* fsync incrementally while aof rewriting? */
1678 int rdb_save_incremental_fsync; /* fsync incrementally while rdb saving? */
1679 int aof_last_write_status; /* C_OK or C_ERR */
1680 int aof_last_write_errno; /* Valid if aof write/fsync status is ERR */
1681 int aof_load_truncated; /* Don't stop on unexpected AOF EOF. */
1682 int aof_use_rdb_preamble; /* Specify base AOF to use RDB encoding on AOF rewrites. */
1683 redisAtomic int aof_bio_fsync_status; /* Status of AOF fsync in bio job. */
1684 redisAtomic int aof_bio_fsync_errno; /* Errno of AOF fsync in bio job. */
1685 aofManifest *aof_manifest; /* Used to track AOFs. */
1686 int aof_disable_auto_gc; /* If disable automatically deleting HISTORY type AOFs?
1687 default no. (for testings). */
1688
1689 /* RDB persistence */
1690 long long dirty; /* Changes to DB from the last save */
1691 long long dirty_before_bgsave; /* Used to restore dirty on failed BGSAVE */
1692 long long rdb_last_load_keys_expired; /* number of expired keys when loading RDB */
1693 long long rdb_last_load_keys_loaded; /* number of loaded keys when loading RDB */
1694 struct saveparam *saveparams; /* Save points array for RDB */
1695 int saveparamslen; /* Number of saving points */
1696 char *rdb_filename; /* Name of RDB file */
1697 int rdb_compression; /* Use compression in RDB? */
1698 int rdb_checksum; /* Use RDB checksum? */
1699 int rdb_del_sync_files; /* Remove RDB files used only for SYNC if
1700 the instance does not use persistence. */
1701 time_t lastsave; /* Unix time of last successful save */
1702 time_t lastbgsave_try; /* Unix time of last attempted bgsave */
1703 time_t rdb_save_time_last; /* Time used by last RDB save run. */
1704 time_t rdb_save_time_start; /* Current RDB save start time. */
1705 int rdb_bgsave_scheduled; /* BGSAVE when possible if true. */
1706 int rdb_child_type; /* Type of save by active child. */
1707 int lastbgsave_status; /* C_OK or C_ERR */
1708 int stop_writes_on_bgsave_err; /* Don't allow writes if can't BGSAVE */
1709 int rdb_pipe_read; /* RDB pipe used to transfer the rdb data */
1710 /* to the parent process in diskless repl. */
1711 int rdb_child_exit_pipe; /* Used by the diskless parent allow child exit. */
1712 connection **rdb_pipe_conns; /* Connections which are currently the */
1713 int rdb_pipe_numconns; /* target of diskless rdb fork child. */
1714 int rdb_pipe_numconns_writing; /* Number of rdb conns with pending writes. */
1715 char *rdb_pipe_buff; /* In diskless replication, this buffer holds data */
1716 int rdb_pipe_bufflen; /* that was read from the rdb pipe. */
1717 int rdb_key_save_delay; /* Delay in microseconds between keys while
1718 * writing the RDB. (for testings). negative
1719 * value means fractions of microseconds (on average). */
1720 int key_load_delay; /* Delay in microseconds between keys while
1721 * loading aof or rdb. (for testings). negative
1722 * value means fractions of microseconds (on average). */
1723 /* Pipe and data structures for child -> parent info sharing. */
1724 int child_info_pipe[2]; /* Pipe used to write the child_info_data. */
1725 int child_info_nread; /* Num of bytes of the last read from pipe */
1726 /* Propagation of commands in AOF / replication */
1727 redisOpArray also_propagate; /* Additional command to propagate. */
1728 int replication_allowed; /* Are we allowed to replicate? */
1729 /* Logging */
1730 char *logfile; /* Path of log file */
1731 int syslog_enabled; /* Is syslog enabled? */
1732 char *syslog_ident; /* Syslog ident */
1733 int syslog_facility; /* Syslog facility */
1734 int crashlog_enabled; /* Enable signal handler for crashlog.
1735 * disable for clean core dumps. */
1736 int memcheck_enabled; /* Enable memory check on crash. */
1737 int use_exit_on_panic; /* Use exit() on panic and assert rather than
1738 * abort(). useful for Valgrind. */
1739 /* Shutdown */
1740 int shutdown_timeout; /* Graceful shutdown time limit in seconds. */
1741 int shutdown_on_sigint; /* Shutdown flags configured for SIGINT. */
1742 int shutdown_on_sigterm; /* Shutdown flags configured for SIGTERM. */
1743
1744 /* Replication (master) */
1745 char replid[CONFIG_RUN_ID_SIZE+1]; /* My current replication ID. */
1746 char replid2[CONFIG_RUN_ID_SIZE+1]; /* replid inherited from master*/
1747 long long master_repl_offset; /* My current replication offset */
1748 long long second_replid_offset; /* Accept offsets up to this for replid2. */
1749 int slaveseldb; /* Last SELECTed DB in replication output */
1750 int repl_ping_slave_period; /* Master pings the slave every N seconds */
1751 replBacklog *repl_backlog; /* Replication backlog for partial syncs */
1752 long long repl_backlog_size; /* Backlog circular buffer size */
1753 time_t repl_backlog_time_limit; /* Time without slaves after the backlog
1754 gets released. */
1755 time_t repl_no_slaves_since; /* We have no slaves since that time.
1756 Only valid if server.slaves len is 0. */
1757 int repl_min_slaves_to_write; /* Min number of slaves to write. */
1758 int repl_min_slaves_max_lag; /* Max lag of <count> slaves to write. */
1759 int repl_good_slaves_count; /* Number of slaves with lag <= max_lag. */
1760 int repl_diskless_sync; /* Master send RDB to slaves sockets directly. */
1761 int repl_diskless_load; /* Slave parse RDB directly from the socket.
1762 * see REPL_DISKLESS_LOAD_* enum */
1763 int repl_diskless_sync_delay; /* Delay to start a diskless repl BGSAVE. */
1764 int repl_diskless_sync_max_replicas;/* Max replicas for diskless repl BGSAVE
1765 * delay (start sooner if they all connect). */
1766 size_t repl_buffer_mem; /* The memory of replication buffer. */
1767 list *repl_buffer_blocks; /* Replication buffers blocks list
1768 * (serving replica clients and repl backlog) */
1769 /* Replication (slave) */
1770 char *masteruser; /* AUTH with this user and masterauth with master */
1771 sds masterauth; /* AUTH with this password with master */
1772 char *masterhost; /* Hostname of master */
1773 int masterport; /* Port of master */
1774 int repl_timeout; /* Timeout after N seconds of master idle */
1775 client *master; /* Client that is master for this slave */
1776 client *cached_master; /* Cached master to be reused for PSYNC. */
1777 int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
1778 int repl_state; /* Replication status if the instance is a slave */
1779 off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
1780 off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
1781 off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
1782 connection *repl_transfer_s; /* Slave -> Master SYNC connection */
1783 int repl_transfer_fd; /* Slave -> Master SYNC temp file descriptor */
1784 char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
1785 time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */
1786 int repl_serve_stale_data; /* Serve stale data when link is down? */
1787 int repl_slave_ro; /* Slave is read only? */
1788 int repl_slave_ignore_maxmemory; /* If true slaves do not evict. */
1789 time_t repl_down_since; /* Unix time at which link with master went down */
1790 int repl_disable_tcp_nodelay; /* Disable TCP_NODELAY after SYNC? */
1791 int slave_priority; /* Reported in INFO and used by Sentinel. */
1792 int replica_announced; /* If true, replica is announced by Sentinel */
1793 int slave_announce_port; /* Give the master this listening port. */
1794 char *slave_announce_ip; /* Give the master this ip address. */
1795 int propagation_error_behavior; /* Configures the behavior of the replica
1796 * when it receives an error on the replication stream */
1797 int repl_ignore_disk_write_error; /* Configures whether replicas panic when unable to
1798 * persist writes to AOF. */
1799 /* The following two fields is where we store master PSYNC replid/offset
1800 * while the PSYNC is in progress. At the end we'll copy the fields into
1801 * the server->master client structure. */
1802 char master_replid[CONFIG_RUN_ID_SIZE+1]; /* Master PSYNC runid. */
1803 long long master_initial_offset; /* Master PSYNC offset. */
1804 int repl_slave_lazy_flush; /* Lazy FLUSHALL before loading DB? */
1805 /* Synchronous replication. */
1806 list *clients_waiting_acks; /* Clients waiting in WAIT command. */
1807 int get_ack_from_slaves; /* If true we send REPLCONF GETACK. */
1808 /* Limits */
1809 unsigned int maxclients; /* Max number of simultaneous clients */
1810 unsigned long long maxmemory; /* Max number of memory bytes to use */
1811 ssize_t maxmemory_clients; /* Memory limit for total client buffers */
1812 int maxmemory_policy; /* Policy for key eviction */
1813 int maxmemory_samples; /* Precision of random sampling */
1814 int maxmemory_eviction_tenacity;/* Aggressiveness of eviction processing */
1815 int lfu_log_factor; /* LFU logarithmic counter factor. */
1816 int lfu_decay_time; /* LFU counter decay factor. */
1817 long long proto_max_bulk_len; /* Protocol bulk length maximum size. */
1818 int oom_score_adj_values[CONFIG_OOM_COUNT]; /* Linux oom_score_adj configuration */
1819 int oom_score_adj; /* If true, oom_score_adj is managed */
1820 int disable_thp; /* If true, disable THP by syscall */
1821 /* Blocked clients */
1822 unsigned int blocked_clients; /* # of clients executing a blocking cmd.*/
1823 unsigned int blocked_clients_by_type[BLOCKED_NUM];
1824 list *unblocked_clients; /* list of clients to unblock before next loop */
1825 list *ready_keys; /* List of readyList structures for BLPOP & co */
1826 /* Client side caching. */
1827 unsigned int tracking_clients; /* # of clients with tracking enabled.*/
1828 size_t tracking_table_max_keys; /* Max number of keys in tracking table. */
1829 list *tracking_pending_keys; /* tracking invalidation keys pending to flush */
1830 /* Sort parameters - qsort_r() is only available under BSD so we
1831 * have to take this state global, in order to pass it to sortCompare() */
1832 int sort_desc;
1833 int sort_alpha;
1834 int sort_bypattern;
1835 int sort_store;
1836 /* Zip structure config, see redis.conf for more information */
1837 size_t hash_max_listpack_entries;
1838 size_t hash_max_listpack_value;
1839 size_t set_max_intset_entries;
1840 size_t zset_max_listpack_entries;
1841 size_t zset_max_listpack_value;
1842 size_t hll_sparse_max_bytes;
1843 size_t stream_node_max_bytes;
1844 long long stream_node_max_entries;
1845 /* List parameters */
1846 int list_max_listpack_size;
1847 int list_compress_depth;
1848 /* time cache */
1849 redisAtomic time_t unixtime; /* Unix time sampled every cron cycle. */
1850 time_t timezone; /* Cached timezone. As set by tzset(). */
1851 int daylight_active; /* Currently in daylight saving time. */
1852 mstime_t mstime; /* 'unixtime' in milliseconds. */
1853 ustime_t ustime; /* 'unixtime' in microseconds. */
1854 size_t blocking_op_nesting; /* Nesting level of blocking operation, used to reset blocked_last_cron. */
1855 long long blocked_last_cron; /* Indicate the mstime of the last time we did cron jobs from a blocking operation */
1856 /* Pubsub */
1857 dict *pubsub_channels; /* Map channels to list of subscribed clients */
1858 dict *pubsub_patterns; /* A dict of pubsub_patterns */
1859 int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an
1860 xor of NOTIFY_... flags. */
1861 dict *pubsubshard_channels; /* Map shard channels to list of subscribed clients */
1862 /* Cluster */
1863 int cluster_enabled; /* Is cluster enabled? */
1864 int cluster_port; /* Set the cluster port for a node. */
1865 mstime_t cluster_node_timeout; /* Cluster node timeout. */
1866 char *cluster_configfile; /* Cluster auto-generated config file name. */
1867 struct clusterState *cluster; /* State of the cluster */
1868 int cluster_migration_barrier; /* Cluster replicas migration barrier. */
1869 int cluster_allow_replica_migration; /* Automatic replica migrations to orphaned masters and from empty masters */
1870 int cluster_slave_validity_factor; /* Slave max data age for failover. */
1871 int cluster_require_full_coverage; /* If true, put the cluster down if
1872 there is at least an uncovered slot.*/
1873 int cluster_slave_no_failover; /* Prevent slave from starting a failover
1874 if the master is in failure state. */
1875 char *cluster_announce_ip; /* IP address to announce on cluster bus. */
1876 char *cluster_announce_hostname; /* hostname to announce on cluster bus. */
1877 int cluster_preferred_endpoint_type; /* Use the announced hostname when available. */
1878 int cluster_announce_port; /* base port to announce on cluster bus. */
1879 int cluster_announce_tls_port; /* TLS port to announce on cluster bus. */
1880 int cluster_announce_bus_port; /* bus port to announce on cluster bus. */
1881 int cluster_module_flags; /* Set of flags that Redis modules are able
1882 to set in order to suppress certain
1883 native Redis Cluster features. Check the
1884 REDISMODULE_CLUSTER_FLAG_*. */
1885 int cluster_allow_reads_when_down; /* Are reads allowed when the cluster
1886 is down? */
1887 int cluster_config_file_lock_fd; /* cluster config fd, will be flock */
1888 unsigned long long cluster_link_sendbuf_limit_bytes; /* Memory usage limit on individual link send buffers*/
1889 int cluster_drop_packet_filter; /* Debug config that allows tactically
1890 * dropping packets of a specific type */
1891 /* Scripting */
1892 client *script_caller; /* The client running script right now, or NULL */
1893 mstime_t busy_reply_threshold; /* Script / module timeout in milliseconds */
1894 int pre_command_oom_state; /* OOM before command (script?) was started */
1895 int script_disable_deny_script; /* Allow running commands marked "no-script" inside a script. */
1896 /* Lazy free */
1897 int lazyfree_lazy_eviction;
1898 int lazyfree_lazy_expire;
1899 int lazyfree_lazy_server_del;
1900 int lazyfree_lazy_user_del;
1901 int lazyfree_lazy_user_flush;
1902 /* Latency monitor */
1903 long long latency_monitor_threshold;
1904 dict *latency_events;
1905 /* ACLs */
1906 char *acl_filename; /* ACL Users file. NULL if not configured. */
1907 unsigned long acllog_max_len; /* Maximum length of the ACL LOG list. */
1908 sds requirepass; /* Remember the cleartext password set with
1909 the old "requirepass" directive for
1910 backward compatibility with Redis <= 5. */
1911 int acl_pubsub_default; /* Default ACL pub/sub channels flag */
1912 /* Assert & bug reporting */
1913 int watchdog_period; /* Software watchdog period in ms. 0 = off */
1914 /* System hardware info */
1915 size_t system_memory_size; /* Total memory in system as reported by OS */
1916 /* TLS Configuration */
1917 int tls_cluster;
1918 int tls_replication;
1919 int tls_auth_clients;
1920 redisTLSContextConfig tls_ctx_config;
1921 /* cpu affinity */
1922 char *server_cpulist; /* cpu affinity list of redis server main/io thread. */
1923 char *bio_cpulist; /* cpu affinity list of bio thread. */
1924 char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */
1925 char *bgsave_cpulist; /* cpu affinity list of bgsave process. */
1926 /* Sentinel config */
1927 struct sentinelConfig *sentinel_config; /* sentinel config to load at startup time. */
1928 /* Coordinate failover info */
1929 mstime_t failover_end_time; /* Deadline for failover command. */
1930 int force_failover; /* If true then failover will be forced at the
1931 * deadline, otherwise failover is aborted. */
1932 char *target_replica_host; /* Failover target host. If null during a
1933 * failover then any replica can be used. */
1934 int target_replica_port; /* Failover target port */
1935 int failover_state; /* Failover state */
1936 int cluster_allow_pubsubshard_when_down; /* Is pubsubshard allowed when the cluster
1937 is down, doesn't affect pubsub global. */
1938 long reply_buffer_peak_reset_time; /* The amount of time (in milliseconds) to wait between reply buffer peak resets */
1939 int reply_buffer_resizing_enabled; /* Is reply buffer resizing enabled (1 by default) */
1940};
1941
1942#define MAX_KEYS_BUFFER 256
1943
1944typedef struct {
1945 int pos; /* The position of the key within the client array */
1946 int flags; /* The flags associated with the key access, see
1947 CMD_KEY_* for more information */
1948} keyReference;
1949
1950/* A result structure for the various getkeys function calls. It lists the
1951 * keys as indices to the provided argv. This functionality is also re-used
1952 * for returning channel information.
1953 */
1954typedef struct {
1955 keyReference keysbuf[MAX_KEYS_BUFFER]; /* Pre-allocated buffer, to save heap allocations */
1956 keyReference *keys; /* Key indices array, points to keysbuf or heap */
1957 int numkeys; /* Number of key indices return */
1958 int size; /* Available array size */
1959} getKeysResult;
1960#define GETKEYS_RESULT_INIT { {{0}}, NULL, 0, MAX_KEYS_BUFFER }
1961
1962/* Key specs definitions.
1963 *
1964 * Brief: This is a scheme that tries to describe the location
1965 * of key arguments better than the old [first,last,step] scheme
1966 * which is limited and doesn't fit many commands.
1967 *
1968 * There are two steps:
1969 * 1. begin_search (BS): in which index should we start searching for keys?
1970 * 2. find_keys (FK): relative to the output of BS, how can we will which args are keys?
1971 *
1972 * There are two types of BS:
1973 * 1. index: key args start at a constant index
1974 * 2. keyword: key args start just after a specific keyword
1975 *
1976 * There are two kinds of FK:
1977 * 1. range: keys end at a specific index (or relative to the last argument)
1978 * 2. keynum: there's an arg that contains the number of key args somewhere before the keys themselves
1979 */
1980
1981/* Must be synced with generate-command-code.py */
1982typedef enum {
1983 KSPEC_BS_INVALID = 0, /* Must be 0 */
1984 KSPEC_BS_UNKNOWN,
1985 KSPEC_BS_INDEX,
1986 KSPEC_BS_KEYWORD
1987} kspec_bs_type;
1988
1989/* Must be synced with generate-command-code.py */
1990typedef enum {
1991 KSPEC_FK_INVALID = 0, /* Must be 0 */
1992 KSPEC_FK_UNKNOWN,
1993 KSPEC_FK_RANGE,
1994 KSPEC_FK_KEYNUM
1995} kspec_fk_type;
1996
1997typedef struct {
1998 /* Declarative data */
1999 const char *notes;
2000 uint64_t flags;
2001 kspec_bs_type begin_search_type;
2002 union {
2003 struct {
2004 /* The index from which we start the search for keys */
2005 int pos;
2006 } index;
2007 struct {
2008 /* The keyword that indicates the beginning of key args */
2009 const char *keyword;
2010 /* An index in argv from which to start searching.
2011 * Can be negative, which means start search from the end, in reverse
2012 * (Example: -2 means to start in reverse from the penultimate arg) */
2013 int startfrom;
2014 } keyword;
2015 } bs;
2016 kspec_fk_type find_keys_type;
2017 union {
2018 /* NOTE: Indices in this struct are relative to the result of the begin_search step!
2019 * These are: range.lastkey, keynum.keynumidx, keynum.firstkey */
2020 struct {
2021 /* Index of the last key.
2022 * Can be negative, in which case it's not relative. -1 indicating till the last argument,
2023 * -2 one before the last and so on. */
2024 int lastkey;
2025 /* How many args should we skip after finding a key, in order to find the next one. */
2026 int keystep;
2027 /* If lastkey is -1, we use limit to stop the search by a factor. 0 and 1 mean no limit.
2028 * 2 means 1/2 of the remaining args, 3 means 1/3, and so on. */
2029 int limit;
2030 } range;
2031 struct {
2032 /* Index of the argument containing the number of keys to come */
2033 int keynumidx;
2034 /* Index of the fist key (Usually it's just after keynumidx, in
2035 * which case it should be set to keynumidx+1). */
2036 int firstkey;
2037 /* How many args should we skip after finding a key, in order to find the next one. */
2038 int keystep;
2039 } keynum;
2040 } fk;
2041} keySpec;
2042
2043/* Number of static key specs */
2044#define STATIC_KEY_SPECS_NUM 4
2045
2046/* Must be synced with ARG_TYPE_STR and generate-command-code.py */
2047typedef enum {
2048 ARG_TYPE_STRING,
2049 ARG_TYPE_INTEGER,
2050 ARG_TYPE_DOUBLE,
2051 ARG_TYPE_KEY, /* A string, but represents a keyname */
2052 ARG_TYPE_PATTERN,
2053 ARG_TYPE_UNIX_TIME,
2054 ARG_TYPE_PURE_TOKEN,
2055 ARG_TYPE_ONEOF, /* Has subargs */
2056 ARG_TYPE_BLOCK /* Has subargs */
2057} redisCommandArgType;
2058
2059#define CMD_ARG_NONE (0)
2060#define CMD_ARG_OPTIONAL (1<<0)
2061#define CMD_ARG_MULTIPLE (1<<1)
2062#define CMD_ARG_MULTIPLE_TOKEN (1<<2)
2063
2064typedef struct redisCommandArg {
2065 const char *name;
2066 redisCommandArgType type;
2067 int key_spec_index;
2068 const char *token;
2069 const char *summary;
2070 const char *since;
2071 int flags;
2072 const char *deprecated_since;
2073 struct redisCommandArg *subargs;
2074 /* runtime populated data */
2075 int num_args;
2076} redisCommandArg;
2077
2078/* Must be synced with RESP2_TYPE_STR and generate-command-code.py */
2079typedef enum {
2080 RESP2_SIMPLE_STRING,
2081 RESP2_ERROR,
2082 RESP2_INTEGER,
2083 RESP2_BULK_STRING,
2084 RESP2_NULL_BULK_STRING,
2085 RESP2_ARRAY,
2086 RESP2_NULL_ARRAY,
2087} redisCommandRESP2Type;
2088
2089/* Must be synced with RESP3_TYPE_STR and generate-command-code.py */
2090typedef enum {
2091 RESP3_SIMPLE_STRING,
2092 RESP3_ERROR,
2093 RESP3_INTEGER,
2094 RESP3_DOUBLE,
2095 RESP3_BULK_STRING,
2096 RESP3_ARRAY,
2097 RESP3_MAP,
2098 RESP3_SET,
2099 RESP3_BOOL,
2100 RESP3_NULL,
2101} redisCommandRESP3Type;
2102
2103typedef struct {
2104 const char *since;
2105 const char *changes;
2106} commandHistory;
2107
2108/* Must be synced with COMMAND_GROUP_STR and generate-command-code.py */
2109typedef enum {
2110 COMMAND_GROUP_GENERIC,
2111 COMMAND_GROUP_STRING,
2112 COMMAND_GROUP_LIST,
2113 COMMAND_GROUP_SET,
2114 COMMAND_GROUP_SORTED_SET,
2115 COMMAND_GROUP_HASH,
2116 COMMAND_GROUP_PUBSUB,
2117 COMMAND_GROUP_TRANSACTIONS,
2118 COMMAND_GROUP_CONNECTION,
2119 COMMAND_GROUP_SERVER,
2120 COMMAND_GROUP_SCRIPTING,
2121 COMMAND_GROUP_HYPERLOGLOG,
2122 COMMAND_GROUP_CLUSTER,
2123 COMMAND_GROUP_SENTINEL,
2124 COMMAND_GROUP_GEO,
2125 COMMAND_GROUP_STREAM,
2126 COMMAND_GROUP_BITMAP,
2127 COMMAND_GROUP_MODULE,
2128} redisCommandGroup;
2129
2130typedef void redisCommandProc(client *c);
2131typedef int redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2132
2133/* Redis command structure.
2134 *
2135 * Note that the command table is in commands.c and it is auto-generated.
2136 *
2137 * This is the meaning of the flags:
2138 *
2139 * CMD_WRITE: Write command (may modify the key space).
2140 *
2141 * CMD_READONLY: Commands just reading from keys without changing the content.
2142 * Note that commands that don't read from the keyspace such as
2143 * TIME, SELECT, INFO, administrative commands, and connection
2144 * or transaction related commands (multi, exec, discard, ...)
2145 * are not flagged as read-only commands, since they affect the
2146 * server or the connection in other ways.
2147 *
2148 * CMD_DENYOOM: May increase memory usage once called. Don't allow if out
2149 * of memory.
2150 *
2151 * CMD_ADMIN: Administrative command, like SAVE or SHUTDOWN.
2152 *
2153 * CMD_PUBSUB: Pub/Sub related command.
2154 *
2155 * CMD_NOSCRIPT: Command not allowed in scripts.
2156 *
2157 * CMD_BLOCKING: The command has the potential to block the client.
2158 *
2159 * CMD_LOADING: Allow the command while loading the database.
2160 *
2161 * CMD_NO_ASYNC_LOADING: Deny during async loading (when a replica uses diskless
2162 * sync swapdb, and allows access to the old dataset)
2163 *
2164 * CMD_STALE: Allow the command while a slave has stale data but is not
2165 * allowed to serve this data. Normally no command is accepted
2166 * in this condition but just a few.
2167 *
2168 * CMD_SKIP_MONITOR: Do not automatically propagate the command on MONITOR.
2169 *
2170 * CMD_SKIP_SLOWLOG: Do not automatically propagate the command to the slowlog.
2171 *
2172 * CMD_ASKING: Perform an implicit ASKING for this command, so the
2173 * command will be accepted in cluster mode if the slot is marked
2174 * as 'importing'.
2175 *
2176 * CMD_FAST: Fast command: O(1) or O(log(N)) command that should never
2177 * delay its execution as long as the kernel scheduler is giving
2178 * us time. Note that commands that may trigger a DEL as a side
2179 * effect (like SET) are not fast commands.
2180 *
2181 * CMD_NO_AUTH: Command doesn't require authentication
2182 *
2183 * CMD_MAY_REPLICATE: Command may produce replication traffic, but should be
2184 * allowed under circumstances where write commands are disallowed.
2185 * Examples include PUBLISH, which replicates pubsub messages,and
2186 * EVAL, which may execute write commands, which are replicated,
2187 * or may just execute read commands. A command can not be marked
2188 * both CMD_WRITE and CMD_MAY_REPLICATE
2189 *
2190 * CMD_SENTINEL: This command is present in sentinel mode.
2191 *
2192 * CMD_ONLY_SENTINEL: This command is present only when in sentinel mode.
2193 * And should be removed from redis.
2194 *
2195 * CMD_NO_MANDATORY_KEYS: This key arguments for this command are optional.
2196 *
2197 * CMD_NO_MULTI: The command is not allowed inside a transaction
2198 *
2199 * The following additional flags are only used in order to put commands
2200 * in a specific ACL category. Commands can have multiple ACL categories.
2201 * See redis.conf for the exact meaning of each.
2202 *
2203 * @keyspace, @read, @write, @set, @sortedset, @list, @hash, @string, @bitmap,
2204 * @hyperloglog, @stream, @admin, @fast, @slow, @pubsub, @blocking, @dangerous,
2205 * @connection, @transaction, @scripting, @geo.
2206 *
2207 * Note that:
2208 *
2209 * 1) The read-only flag implies the @read ACL category.
2210 * 2) The write flag implies the @write ACL category.
2211 * 3) The fast flag implies the @fast ACL category.
2212 * 4) The admin flag implies the @admin and @dangerous ACL category.
2213 * 5) The pub-sub flag implies the @pubsub ACL category.
2214 * 6) The lack of fast flag implies the @slow ACL category.
2215 * 7) The non obvious "keyspace" category includes the commands
2216 * that interact with keys without having anything to do with
2217 * specific data structures, such as: DEL, RENAME, MOVE, SELECT,
2218 * TYPE, EXPIRE*, PEXPIRE*, TTL, PTTL, ...
2219 */
2220struct redisCommand {
2221 /* Declarative data */
2222 const char *declared_name; /* A string representing the command declared_name.
2223 * It is a const char * for native commands and SDS for module commands. */
2224 const char *summary; /* Summary of the command (optional). */
2225 const char *complexity; /* Complexity description (optional). */
2226 const char *since; /* Debut version of the command (optional). */
2227 int doc_flags; /* Flags for documentation (see CMD_DOC_*). */
2228 const char *replaced_by; /* In case the command is deprecated, this is the successor command. */
2229 const char *deprecated_since; /* In case the command is deprecated, when did it happen? */
2230 redisCommandGroup group; /* Command group */
2231 commandHistory *history; /* History of the command */
2232 const char **tips; /* An array of strings that are meant to be tips for clients/proxies regarding this command */
2233 redisCommandProc *proc; /* Command implementation */
2234 int arity; /* Number of arguments, it is possible to use -N to say >= N */
2235 uint64_t flags; /* Command flags, see CMD_*. */
2236 uint64_t acl_categories; /* ACl categories, see ACL_CATEGORY_*. */
2237 keySpec key_specs_static[STATIC_KEY_SPECS_NUM]; /* Key specs. See keySpec */
2238 /* Use a function to determine keys arguments in a command line.
2239 * Used for Redis Cluster redirect (may be NULL) */
2240 redisGetKeysProc *getkeys_proc;
2241 /* Array of subcommands (may be NULL) */
2242 struct redisCommand *subcommands;
2243 /* Array of arguments (may be NULL) */
2244 struct redisCommandArg *args;
2245
2246 /* Runtime populated data */
2247 long long microseconds, calls, rejected_calls, failed_calls;
2248 int id; /* Command ID. This is a progressive ID starting from 0 that
2249 is assigned at runtime, and is used in order to check
2250 ACLs. A connection is able to execute a given command if
2251 the user associated to the connection has this command
2252 bit set in the bitmap of allowed commands. */
2253 sds fullname; /* A SDS string representing the command fullname. */
2254 struct hdr_histogram* latency_histogram; /*points to the command latency command histogram (unit of time nanosecond) */
2255 keySpec *key_specs;
2256 keySpec legacy_range_key_spec; /* The legacy (first,last,step) key spec is
2257 * still maintained (if applicable) so that
2258 * we can still support the reply format of
2259 * COMMAND INFO and COMMAND GETKEYS */
2260 int num_args;
2261 int num_history;
2262 int num_tips;
2263 int key_specs_num;
2264 int key_specs_max;
2265 dict *subcommands_dict; /* A dictionary that holds the subcommands, the key is the subcommand sds name
2266 * (not the fullname), and the value is the redisCommand structure pointer. */
2267 struct redisCommand *parent;
2268 struct RedisModuleCommand *module_cmd; /* A pointer to the module command data (NULL if native command) */
2269};
2270
2271struct redisError {
2272 long long count;
2273};
2274
2275struct redisFunctionSym {
2276 char *name;
2277 unsigned long pointer;
2278};
2279
2280typedef struct _redisSortObject {
2281 robj *obj;
2282 union {
2283 double score;
2284 robj *cmpobj;
2285 } u;
2286} redisSortObject;
2287
2288typedef struct _redisSortOperation {
2289 int type;
2290 robj *pattern;
2291} redisSortOperation;
2292
2293/* Structure to hold list iteration abstraction. */
2294typedef struct {
2295 robj *subject;
2296 unsigned char encoding;
2297 unsigned char direction; /* Iteration direction */
2298 quicklistIter *iter;
2299} listTypeIterator;
2300
2301/* Structure for an entry while iterating over a list. */
2302typedef struct {
2303 listTypeIterator *li;
2304 quicklistEntry entry; /* Entry in quicklist */
2305} listTypeEntry;
2306
2307/* Structure to hold set iteration abstraction. */
2308typedef struct {
2309 robj *subject;
2310 int encoding;
2311 int ii; /* intset iterator */
2312 dictIterator *di;
2313} setTypeIterator;
2314
2315/* Structure to hold hash iteration abstraction. Note that iteration over
2316 * hashes involves both fields and values. Because it is possible that
2317 * not both are required, store pointers in the iterator to avoid
2318 * unnecessary memory allocation for fields/values. */
2319typedef struct {
2320 robj *subject;
2321 int encoding;
2322
2323 unsigned char *fptr, *vptr;
2324
2325 dictIterator *di;
2326 dictEntry *de;
2327} hashTypeIterator;
2328
2329#include "stream.h" /* Stream data type header file. */
2330
2331#define OBJ_HASH_KEY 1
2332#define OBJ_HASH_VALUE 2
2333
2334#define IO_THREADS_OP_IDLE 0
2335#define IO_THREADS_OP_READ 1
2336#define IO_THREADS_OP_WRITE 2
2337extern int io_threads_op;
2338
2339/*-----------------------------------------------------------------------------
2340 * Extern declarations
2341 *----------------------------------------------------------------------------*/
2342
2343extern struct redisServer server;
2344extern struct sharedObjectsStruct shared;
2345extern dictType objectKeyPointerValueDictType;
2346extern dictType objectKeyHeapPointerValueDictType;
2347extern dictType setDictType;
2348extern dictType BenchmarkDictType;
2349extern dictType zsetDictType;
2350extern dictType dbDictType;
2351extern double R_Zero, R_PosInf, R_NegInf, R_Nan;
2352extern dictType hashDictType;
2353extern dictType stringSetDictType;
2354extern dictType externalStringType;
2355extern dictType sdsHashDictType;
2356extern dictType dbExpiresDictType;
2357extern dictType modulesDictType;
2358extern dictType sdsReplyDictType;
2359extern dict *modules;
2360
2361/*-----------------------------------------------------------------------------
2362 * Functions prototypes
2363 *----------------------------------------------------------------------------*/
2364
2365/* Command metadata */
2366void populateCommandLegacyRangeSpec(struct redisCommand *c);
2367int populateArgsStructure(struct redisCommandArg *args);
2368
2369/* Modules */
2370void moduleInitModulesSystem(void);
2371void moduleInitModulesSystemLast(void);
2372void modulesCron(void);
2373int moduleLoad(const char *path, void **argv, int argc, int is_loadex);
2374int moduleUnload(sds name);
2375void moduleLoadFromQueue(void);
2376int moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2377int moduleGetCommandChannelsViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2378moduleType *moduleTypeLookupModuleByID(uint64_t id);
2379void moduleTypeNameByID(char *name, uint64_t moduleid);
2380const char *moduleTypeModuleName(moduleType *mt);
2381const char *moduleNameFromCommand(struct redisCommand *cmd);
2382void moduleFreeContext(struct RedisModuleCtx *ctx);
2383void unblockClientFromModule(client *c);
2384void moduleHandleBlockedClients(void);
2385void moduleBlockedClientTimedOut(client *c);
2386void modulePipeReadable(aeEventLoop *el, int fd, void *privdata, int mask);
2387size_t moduleCount(void);
2388void moduleAcquireGIL(void);
2389int moduleTryAcquireGIL(void);
2390void moduleReleaseGIL(void);
2391void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);
2392void moduleCallCommandFilters(client *c);
2393void ModuleForkDoneHandler(int exitcode, int bysignal);
2394int TerminateModuleForkChild(int child_pid, int wait);
2395ssize_t rdbSaveModulesAux(rio *rdb, int when);
2396int moduleAllDatatypesHandleErrors();
2397int moduleAllModulesHandleReplAsyncLoad();
2398sds modulesCollectInfo(sds info, dict *sections_dict, int for_crash_report, int sections);
2399void moduleFireServerEvent(uint64_t eid, int subid, void *data);
2400void processModuleLoadingProgressEvent(int is_aof);
2401int moduleTryServeClientBlockedOnKey(client *c, robj *key);
2402void moduleUnblockClient(client *c);
2403int moduleBlockedClientMayTimeout(client *c);
2404int moduleClientIsBlockedOnKeys(client *c);
2405void moduleNotifyUserChanged(client *c);
2406void moduleNotifyKeyUnlink(robj *key, robj *val, int dbid);
2407size_t moduleGetFreeEffort(robj *key, robj *val, int dbid);
2408size_t moduleGetMemUsage(robj *key, robj *val, size_t sample_size, int dbid);
2409robj *moduleTypeDupOrReply(client *c, robj *fromkey, robj *tokey, int todb, robj *value);
2410int moduleDefragValue(robj *key, robj *obj, long *defragged, int dbid);
2411int moduleLateDefrag(robj *key, robj *value, unsigned long *cursor, long long endtime, long long *defragged, int dbid);
2412long moduleDefragGlobals(void);
2413void *moduleGetHandleByName(char *modulename);
2414int moduleIsModuleCommand(void *module_handle, struct redisCommand *cmd);
2415
2416/* Utils */
2417long long ustime(void);
2418long long mstime(void);
2419void getRandomHexChars(char *p, size_t len);
2420void getRandomBytes(unsigned char *p, size_t len);
2421uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
2422void exitFromChild(int retcode);
2423long long redisPopcount(void *s, long count);
2424int redisSetProcTitle(char *title);
2425int validateProcTitleTemplate(const char *template);
2426int redisCommunicateSystemd(const char *sd_notify_msg);
2427void redisSetCpuAffinity(const char *cpulist);
2428
2429/* afterErrorReply flags */
2430#define ERR_REPLY_FLAG_NO_STATS_UPDATE (1ULL<<0) /* Indicating that we should not update
2431 error stats after sending error reply */
2432/* networking.c -- Networking and Client related operations */
2433client *createClient(connection *conn);
2434void freeClient(client *c);
2435void freeClientAsync(client *c);
2436void logInvalidUseAndFreeClientAsync(client *c, const char *fmt, ...);
2437int beforeNextClient(client *c);
2438void clearClientConnectionState(client *c);
2439void resetClient(client *c);
2440void freeClientOriginalArgv(client *c);
2441void freeClientArgv(client *c);
2442void sendReplyToClient(connection *conn);
2443void *addReplyDeferredLen(client *c);
2444void setDeferredArrayLen(client *c, void *node, long length);
2445void setDeferredMapLen(client *c, void *node, long length);
2446void setDeferredSetLen(client *c, void *node, long length);
2447void setDeferredAttributeLen(client *c, void *node, long length);
2448void setDeferredPushLen(client *c, void *node, long length);
2449int processInputBuffer(client *c);
2450void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask);
2451void acceptTLSHandler(aeEventLoop *el, int fd, void *privdata, int mask);
2452void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask);
2453void readQueryFromClient(connection *conn);
2454int prepareClientToWrite(client *c);
2455void addReplyNull(client *c);
2456void addReplyNullArray(client *c);
2457void addReplyBool(client *c, int b);
2458void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext);
2459void addReplyProto(client *c, const char *s, size_t len);
2460void AddReplyFromClient(client *c, client *src);
2461void addReplyBulk(client *c, robj *obj);
2462void addReplyBulkCString(client *c, const char *s);
2463void addReplyBulkCBuffer(client *c, const void *p, size_t len);
2464void addReplyBulkLongLong(client *c, long long ll);
2465void addReply(client *c, robj *obj);
2466void addReplySds(client *c, sds s);
2467void addReplyBulkSds(client *c, sds s);
2468void setDeferredReplyBulkSds(client *c, void *node, sds s);
2469void addReplyErrorObject(client *c, robj *err);
2470void addReplyOrErrorObject(client *c, robj *reply);
2471void afterErrorReply(client *c, const char *s, size_t len, int flags);
2472void addReplyErrorSdsEx(client *c, sds err, int flags);
2473void addReplyErrorSds(client *c, sds err);
2474void addReplyError(client *c, const char *err);
2475void addReplyErrorArity(client *c);
2476void addReplyErrorExpireTime(client *c);
2477void addReplyStatus(client *c, const char *status);
2478void addReplyDouble(client *c, double d);
2479void addReplyLongLongWithPrefix(client *c, long long ll, char prefix);
2480void addReplyBigNum(client *c, const char* num, size_t len);
2481void addReplyHumanLongDouble(client *c, long double d);
2482void addReplyLongLong(client *c, long long ll);
2483void addReplyArrayLen(client *c, long length);
2484void addReplyMapLen(client *c, long length);
2485void addReplySetLen(client *c, long length);
2486void addReplyAttributeLen(client *c, long length);
2487void addReplyPushLen(client *c, long length);
2488void addReplyHelp(client *c, const char **help);
2489void addReplySubcommandSyntaxError(client *c);
2490void addReplyLoadedModules(client *c);
2491void copyReplicaOutputBuffer(client *dst, client *src);
2492void addListRangeReply(client *c, robj *o, long start, long end, int reverse);
2493void deferredAfterErrorReply(client *c, list *errors);
2494size_t sdsZmallocSize(sds s);
2495size_t getStringObjectSdsUsedMemory(robj *o);
2496void freeClientReplyValue(void *o);
2497void *dupClientReplyValue(void *o);
2498char *getClientPeerId(client *client);
2499char *getClientSockName(client *client);
2500sds catClientInfoString(sds s, client *client);
2501sds getAllClientsInfoString(int type);
2502int clientSetName(client *c, robj *name);
2503void rewriteClientCommandVector(client *c, int argc, ...);
2504void rewriteClientCommandArgument(client *c, int i, robj *newval);
2505void replaceClientCommandVector(client *c, int argc, robj **argv);
2506void redactClientCommandArgument(client *c, int argc);
2507size_t getClientOutputBufferMemoryUsage(client *c);
2508size_t getClientMemoryUsage(client *c, size_t *output_buffer_mem_usage);
2509int freeClientsInAsyncFreeQueue(void);
2510int closeClientOnOutputBufferLimitReached(client *c, int async);
2511int getClientType(client *c);
2512int getClientTypeByName(char *name);
2513char *getClientTypeName(int class);
2514void flushSlavesOutputBuffers(void);
2515void disconnectSlaves(void);
2516void evictClients(void);
2517int listenToPort(int port, socketFds *fds);
2518void pauseClients(pause_purpose purpose, mstime_t end, pause_type type);
2519void unpauseClients(pause_purpose purpose);
2520int areClientsPaused(void);
2521int checkClientPauseTimeoutAndReturnIfPaused(void);
2522void unblockPostponedClients();
2523void processEventsWhileBlocked(void);
2524void whileBlockedCron();
2525void blockingOperationStarts();
2526void blockingOperationEnds();
2527int handleClientsWithPendingWrites(void);
2528int handleClientsWithPendingWritesUsingThreads(void);
2529int handleClientsWithPendingReadsUsingThreads(void);
2530int stopThreadedIOIfNeeded(void);
2531int clientHasPendingReplies(client *c);
2532int islocalClient(client *c);
2533int updateClientMemUsage(client *c);
2534void updateClientMemUsageBucket(client *c);
2535void unlinkClient(client *c);
2536int writeToClient(client *c, int handler_installed);
2537void linkClient(client *c);
2538void protectClient(client *c);
2539void unprotectClient(client *c);
2540void initThreadedIO(void);
2541client *lookupClientByID(uint64_t id);
2542int authRequired(client *c);
2543void putClientInPendingWriteQueue(client *c);
2544
2545#ifdef __GNUC__
2546void addReplyErrorFormatEx(client *c, int flags, const char *fmt, ...)
2547 __attribute__((format(printf, 3, 4)));
2548void addReplyErrorFormat(client *c, const char *fmt, ...)
2549 __attribute__((format(printf, 2, 3)));
2550void addReplyStatusFormat(client *c, const char *fmt, ...)
2551 __attribute__((format(printf, 2, 3)));
2552#else
2553void addReplyErrorFormatEx(client *c, int flags, const char *fmt, ...);
2554void addReplyErrorFormat(client *c, const char *fmt, ...);
2555void addReplyStatusFormat(client *c, const char *fmt, ...);
2556#endif
2557
2558/* Client side caching (tracking mode) */
2559void enableTracking(client *c, uint64_t redirect_to, uint64_t options, robj **prefix, size_t numprefix);
2560void disableTracking(client *c);
2561void trackingRememberKeys(client *c);
2562void trackingInvalidateKey(client *c, robj *keyobj, int bcast);
2563void trackingScheduleKeyInvalidation(uint64_t client_id, robj *keyobj);
2564void trackingHandlePendingKeyInvalidations(void);
2565void trackingInvalidateKeysOnFlush(int async);
2566void freeTrackingRadixTree(rax *rt);
2567void freeTrackingRadixTreeAsync(rax *rt);
2568void trackingLimitUsedSlots(void);
2569uint64_t trackingGetTotalItems(void);
2570uint64_t trackingGetTotalKeys(void);
2571uint64_t trackingGetTotalPrefixes(void);
2572void trackingBroadcastInvalidationMessages(void);
2573int checkPrefixCollisionsOrReply(client *c, robj **prefix, size_t numprefix);
2574
2575/* List data type */
2576void listTypePush(robj *subject, robj *value, int where);
2577robj *listTypePop(robj *subject, int where);
2578unsigned long listTypeLength(const robj *subject);
2579listTypeIterator *listTypeInitIterator(robj *subject, long index, unsigned char direction);
2580void listTypeReleaseIterator(listTypeIterator *li);
2581void listTypeSetIteratorDirection(listTypeIterator *li, unsigned char direction);
2582int listTypeNext(listTypeIterator *li, listTypeEntry *entry);
2583robj *listTypeGet(listTypeEntry *entry);
2584void listTypeInsert(listTypeEntry *entry, robj *value, int where);
2585void listTypeReplace(listTypeEntry *entry, robj *value);
2586int listTypeEqual(listTypeEntry *entry, robj *o);
2587void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry);
2588robj *listTypeDup(robj *o);
2589int listTypeDelRange(robj *o, long start, long stop);
2590void unblockClientWaitingData(client *c);
2591void popGenericCommand(client *c, int where);
2592void listElementsRemoved(client *c, robj *key, int where, robj *o, long count, int *deleted);
2593
2594/* MULTI/EXEC/WATCH... */
2595void unwatchAllKeys(client *c);
2596void initClientMultiState(client *c);
2597void freeClientMultiState(client *c);
2598void queueMultiCommand(client *c, uint64_t cmd_flags);
2599size_t multiStateMemOverhead(client *c);
2600void touchWatchedKey(redisDb *db, robj *key);
2601int isWatchedKeyExpired(client *c);
2602void touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with);
2603void discardTransaction(client *c);
2604void flagTransaction(client *c);
2605void execCommandAbort(client *c, sds error);
2606
2607/* Redis object implementation */
2608void decrRefCount(robj *o);
2609void decrRefCountVoid(void *o);
2610void incrRefCount(robj *o);
2611robj *makeObjectShared(robj *o);
2612void freeStringObject(robj *o);
2613void freeListObject(robj *o);
2614void freeSetObject(robj *o);
2615void freeZsetObject(robj *o);
2616void freeHashObject(robj *o);
2617void dismissObject(robj *o, size_t dump_size);
2618robj *createObject(int type, void *ptr);
2619robj *createStringObject(const char *ptr, size_t len);
2620robj *createRawStringObject(const char *ptr, size_t len);
2621robj *createEmbeddedStringObject(const char *ptr, size_t len);
2622robj *tryCreateRawStringObject(const char *ptr, size_t len);
2623robj *tryCreateStringObject(const char *ptr, size_t len);
2624robj *dupStringObject(const robj *o);
2625int isSdsRepresentableAsLongLong(sds s, long long *llval);
2626int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
2627robj *tryObjectEncoding(robj *o);
2628robj *getDecodedObject(robj *o);
2629size_t stringObjectLen(robj *o);
2630robj *createStringObjectFromLongLong(long long value);
2631robj *createStringObjectFromLongLongForValue(long long value);
2632robj *createStringObjectFromLongDouble(long double value, int humanfriendly);
2633robj *createQuicklistObject(void);
2634robj *createSetObject(void);
2635robj *createIntsetObject(void);
2636robj *createHashObject(void);
2637robj *createZsetObject(void);
2638robj *createZsetListpackObject(void);
2639robj *createStreamObject(void);
2640robj *createModuleObject(moduleType *mt, void *value);
2641int getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);
2642int getPositiveLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg);
2643int getRangeLongFromObjectOrReply(client *c, robj *o, long min, long max, long *target, const char *msg);
2644int checkType(client *c, robj *o, int type);
2645int getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg);
2646int getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg);
2647int getDoubleFromObject(const robj *o, double *target);
2648int getLongLongFromObject(robj *o, long long *target);
2649int getLongDoubleFromObject(robj *o, long double *target);
2650int getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg);
2651int getIntFromObjectOrReply(client *c, robj *o, int *target, const char *msg);
2652char *strEncoding(int encoding);
2653int compareStringObjects(robj *a, robj *b);
2654int collateStringObjects(robj *a, robj *b);
2655int equalStringObjects(robj *a, robj *b);
2656unsigned long long estimateObjectIdleTime(robj *o);
2657void trimStringObjectIfNeeded(robj *o);
2658#define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)
2659
2660/* Synchronous I/O with timeout */
2661ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout);
2662ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout);
2663ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);
2664
2665/* Replication */
2666void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
2667void replicationFeedStreamFromMasterStream(char *buf, size_t buflen);
2668void resetReplicationBuffer(void);
2669void feedReplicationBuffer(char *buf, size_t len);
2670void freeReplicaReferencedReplBuffer(client *replica);
2671void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc);
2672void updateSlavesWaitingBgsave(int bgsaveerr, int type);
2673void replicationCron(void);
2674void replicationStartPendingFork(void);
2675void replicationHandleMasterDisconnection(void);
2676void replicationCacheMaster(client *c);
2677void resizeReplicationBacklog();
2678void replicationSetMaster(char *ip, int port);
2679void replicationUnsetMaster(void);
2680void refreshGoodSlavesCount(void);
2681int checkGoodReplicasStatus(void);
2682void processClientsWaitingReplicas(void);
2683void unblockClientWaitingReplicas(client *c);
2684int replicationCountAcksByOffset(long long offset);
2685void replicationSendNewlineToMaster(void);
2686long long replicationGetSlaveOffset(void);
2687char *replicationGetSlaveName(client *c);
2688long long getPsyncInitialOffset(void);
2689int replicationSetupSlaveForFullResync(client *slave, long long offset);
2690void changeReplicationId(void);
2691void clearReplicationId2(void);
2692void createReplicationBacklog(void);
2693void freeReplicationBacklog(void);
2694void replicationCacheMasterUsingMyself(void);
2695void feedReplicationBacklog(void *ptr, size_t len);
2696void incrementalTrimReplicationBacklog(size_t blocks);
2697int canFeedReplicaReplBuffer(client *replica);
2698void rebaseReplicationBuffer(long long base_repl_offset);
2699void showLatestBacklog(void);
2700void rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
2701void rdbPipeWriteHandlerConnRemoved(struct connection *conn);
2702void clearFailoverState(void);
2703void updateFailoverStatus(void);
2704void abortFailover(const char *err);
2705const char *getFailoverStateString();
2706
2707/* Generic persistence functions */
2708void startLoadingFile(size_t size, char* filename, int rdbflags);
2709void startLoading(size_t size, int rdbflags, int async);
2710void loadingAbsProgress(off_t pos);
2711void loadingIncrProgress(off_t size);
2712void stopLoading(int success);
2713void updateLoadingFileName(char* filename);
2714void startSaving(int rdbflags);
2715void stopSaving(int success);
2716int allPersistenceDisabled(void);
2717
2718#define DISK_ERROR_TYPE_AOF 1 /* Don't accept writes: AOF errors. */
2719#define DISK_ERROR_TYPE_RDB 2 /* Don't accept writes: RDB errors. */
2720#define DISK_ERROR_TYPE_NONE 0 /* No problems, we can accept writes. */
2721int writeCommandsDeniedByDiskError(void);
2722sds writeCommandsGetDiskErrorMessage(int);
2723
2724/* RDB persistence */
2725#include "rdb.h"
2726void killRDBChild(void);
2727int bg_unlink(const char *filename);
2728
2729/* AOF persistence */
2730void flushAppendOnlyFile(int force);
2731void feedAppendOnlyFile(int dictid, robj **argv, int argc);
2732void aofRemoveTempFile(pid_t childpid);
2733int rewriteAppendOnlyFileBackground(void);
2734int loadAppendOnlyFiles(aofManifest *am);
2735void stopAppendOnly(void);
2736int startAppendOnly(void);
2737void backgroundRewriteDoneHandler(int exitcode, int bysignal);
2738ssize_t aofReadDiffFromParent(void);
2739void killAppendOnlyChild(void);
2740void restartAOFAfterSYNC();
2741void aofLoadManifestFromDisk(void);
2742void aofOpenIfNeededOnServerStart(void);
2743void aofManifestFree(aofManifest *am);
2744int aofDelHistoryFiles(void);
2745int aofRewriteLimited(void);
2746
2747/* Child info */
2748void openChildInfoPipe(void);
2749void closeChildInfoPipe(void);
2750void sendChildInfoGeneric(childInfoType info_type, size_t keys, double progress, char *pname);
2751void sendChildCowInfo(childInfoType info_type, char *pname);
2752void sendChildInfo(childInfoType info_type, size_t keys, char *pname);
2753void receiveChildInfo(void);
2754
2755/* Fork helpers */
2756int redisFork(int purpose);
2757int hasActiveChildProcess();
2758void resetChildState();
2759int isMutuallyExclusiveChildType(int type);
2760
2761/* acl.c -- Authentication related prototypes. */
2762extern rax *Users;
2763extern user *DefaultUser;
2764void ACLInit(void);
2765/* Return values for ACLCheckAllPerm(). */
2766#define ACL_OK 0
2767#define ACL_DENIED_CMD 1
2768#define ACL_DENIED_KEY 2
2769#define ACL_DENIED_AUTH 3 /* Only used for ACL LOG entries. */
2770#define ACL_DENIED_CHANNEL 4 /* Only used for pub/sub commands */
2771
2772/* Context values for addACLLogEntry(). */
2773#define ACL_LOG_CTX_TOPLEVEL 0
2774#define ACL_LOG_CTX_LUA 1
2775#define ACL_LOG_CTX_MULTI 2
2776#define ACL_LOG_CTX_MODULE 3
2777
2778/* ACL key permission types */
2779#define ACL_READ_PERMISSION (1<<0)
2780#define ACL_WRITE_PERMISSION (1<<1)
2781#define ACL_ALL_PERMISSION (ACL_READ_PERMISSION|ACL_WRITE_PERMISSION)
2782
2783int ACLCheckUserCredentials(robj *username, robj *password);
2784int ACLAuthenticateUser(client *c, robj *username, robj *password);
2785unsigned long ACLGetCommandID(sds cmdname);
2786void ACLClearCommandID(void);
2787user *ACLGetUserByName(const char *name, size_t namelen);
2788int ACLUserCheckKeyPerm(user *u, const char *key, int keylen, int flags);
2789int ACLUserCheckChannelPerm(user *u, sds channel, int literal);
2790int ACLCheckAllUserCommandPerm(user *u, struct redisCommand *cmd, robj **argv, int argc, int *idxptr);
2791int ACLUserCheckCmdWithUnrestrictedKeyAccess(user *u, struct redisCommand *cmd, robj **argv, int argc, int flags);
2792int ACLCheckAllPerm(client *c, int *idxptr);
2793int ACLSetUser(user *u, const char *op, ssize_t oplen);
2794uint64_t ACLGetCommandCategoryFlagByName(const char *name);
2795int ACLAppendUserForLoading(sds *argv, int argc, int *argc_err);
2796const char *ACLSetUserStringError(void);
2797int ACLLoadConfiguredUsers(void);
2798sds ACLDescribeUser(user *u);
2799void ACLLoadUsersAtStartup(void);
2800void addReplyCommandCategories(client *c, struct redisCommand *cmd);
2801user *ACLCreateUnlinkedUser();
2802void ACLFreeUserAndKillClients(user *u);
2803void addACLLogEntry(client *c, int reason, int context, int argpos, sds username, sds object);
2804const char* getAclErrorMessage(int acl_res);
2805void ACLUpdateDefaultUserPassword(sds password);
2806
2807/* Sorted sets data type */
2808
2809/* Input flags. */
2810#define ZADD_IN_NONE 0
2811#define ZADD_IN_INCR (1<<0) /* Increment the score instead of setting it. */
2812#define ZADD_IN_NX (1<<1) /* Don't touch elements not already existing. */
2813#define ZADD_IN_XX (1<<2) /* Only touch elements already existing. */
2814#define ZADD_IN_GT (1<<3) /* Only update existing when new scores are higher. */
2815#define ZADD_IN_LT (1<<4) /* Only update existing when new scores are lower. */
2816
2817/* Output flags. */
2818#define ZADD_OUT_NOP (1<<0) /* Operation not performed because of conditionals.*/
2819#define ZADD_OUT_NAN (1<<1) /* Only touch elements already existing. */
2820#define ZADD_OUT_ADDED (1<<2) /* The element was new and was added. */
2821#define ZADD_OUT_UPDATED (1<<3) /* The element already existed, score updated. */
2822
2823/* Struct to hold an inclusive/exclusive range spec by score comparison. */
2824typedef struct {
2825 double min, max;
2826 int minex, maxex; /* are min or max exclusive? */
2827} zrangespec;
2828
2829/* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */
2830typedef struct {
2831 sds min, max; /* May be set to shared.(minstring|maxstring) */
2832 int minex, maxex; /* are min or max exclusive? */
2833} zlexrangespec;
2834
2835/* flags for incrCommandFailedCalls */
2836#define ERROR_COMMAND_REJECTED (1<<0) /* Indicate to update the command rejected stats */
2837#define ERROR_COMMAND_FAILED (1<<1) /* Indicate to update the command failed stats */
2838
2839zskiplist *zslCreate(void);
2840void zslFree(zskiplist *zsl);
2841zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele);
2842unsigned char *zzlInsert(unsigned char *zl, sds ele, double score);
2843int zslDelete(zskiplist *zsl, double score, sds ele, zskiplistNode **node);
2844zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range);
2845zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range);
2846double zzlGetScore(unsigned char *sptr);
2847void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
2848void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
2849unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);
2850unsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range);
2851unsigned long zsetLength(const robj *zobj);
2852void zsetConvert(robj *zobj, int encoding);
2853void zsetConvertToListpackIfNeeded(robj *zobj, size_t maxelelen, size_t totelelen);
2854int zsetScore(robj *zobj, sds member, double *score);
2855unsigned long zslGetRank(zskiplist *zsl, double score, sds o);
2856int zsetAdd(robj *zobj, double score, sds ele, int in_flags, int *out_flags, double *newscore);
2857long zsetRank(robj *zobj, sds ele, int reverse);
2858int zsetDel(robj *zobj, sds ele);
2859robj *zsetDup(robj *o);
2860void genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, long count, int use_nested_array, int reply_nil_when_empty, int *deleted);
2861sds lpGetObject(unsigned char *sptr);
2862int zslValueGteMin(double value, zrangespec *spec);
2863int zslValueLteMax(double value, zrangespec *spec);
2864void zslFreeLexRange(zlexrangespec *spec);
2865int zslParseLexRange(robj *min, robj *max, zlexrangespec *spec);
2866unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range);
2867unsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range);
2868zskiplistNode *zslFirstInLexRange(zskiplist *zsl, zlexrangespec *range);
2869zskiplistNode *zslLastInLexRange(zskiplist *zsl, zlexrangespec *range);
2870int zzlLexValueGteMin(unsigned char *p, zlexrangespec *spec);
2871int zzlLexValueLteMax(unsigned char *p, zlexrangespec *spec);
2872int zslLexValueGteMin(sds value, zlexrangespec *spec);
2873int zslLexValueLteMax(sds value, zlexrangespec *spec);
2874
2875/* Core functions */
2876int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *level);
2877size_t freeMemoryGetNotCountedMemory();
2878int overMaxmemoryAfterAlloc(size_t moremem);
2879int processCommand(client *c);
2880int processPendingCommandAndInputBuffer(client *c);
2881void setupSignalHandlers(void);
2882void removeSignalHandlers(void);
2883int createSocketAcceptHandler(socketFds *sfd, aeFileProc *accept_handler);
2884int changeListenPort(int port, socketFds *sfd, aeFileProc *accept_handler);
2885int changeBindAddr(void);
2886struct redisCommand *lookupSubcommand(struct redisCommand *container, sds sub_name);
2887struct redisCommand *lookupCommand(robj **argv, int argc);
2888struct redisCommand *lookupCommandBySdsLogic(dict *commands, sds s);
2889struct redisCommand *lookupCommandBySds(sds s);
2890struct redisCommand *lookupCommandByCStringLogic(dict *commands, const char *s);
2891struct redisCommand *lookupCommandByCString(const char *s);
2892struct redisCommand *lookupCommandOrOriginal(robj **argv, int argc);
2893int commandCheckExistence(client *c, sds *err);
2894int commandCheckArity(client *c, sds *err);
2895void startCommandExecution();
2896int incrCommandStatsOnError(struct redisCommand *cmd, int flags);
2897void call(client *c, int flags);
2898void alsoPropagate(int dbid, robj **argv, int argc, int target);
2899void propagatePendingCommands();
2900void redisOpArrayInit(redisOpArray *oa);
2901void redisOpArrayFree(redisOpArray *oa);
2902void forceCommandPropagation(client *c, int flags);
2903void preventCommandPropagation(client *c);
2904void preventCommandAOF(client *c);
2905void preventCommandReplication(client *c);
2906void slowlogPushCurrentCommand(client *c, struct redisCommand *cmd, ustime_t duration);
2907void updateCommandLatencyHistogram(struct hdr_histogram** latency_histogram, int64_t duration_hist);
2908int prepareForShutdown(int flags);
2909void replyToClientsBlockedOnShutdown(void);
2910int abortShutdown(void);
2911void afterCommand(client *c);
2912int mustObeyClient(client *c);
2913#ifdef __GNUC__
2914void _serverLog(int level, const char *fmt, ...)
2915 __attribute__((format(printf, 2, 3)));
2916#else
2917void _serverLog(int level, const char *fmt, ...);
2918#endif
2919void serverLogRaw(int level, const char *msg);
2920void serverLogFromHandler(int level, const char *msg);
2921void usage(void);
2922void updateDictResizePolicy(void);
2923int htNeedsResize(dict *dict);
2924void populateCommandTable(void);
2925void resetCommandTableStats(dict* commands);
2926void resetErrorTableStats(void);
2927void adjustOpenFilesLimit(void);
2928void incrementErrorCount(const char *fullerr, size_t namelen);
2929void closeListeningSockets(int unlink_unix_socket);
2930void updateCachedTime(int update_daylight_info);
2931void resetServerStats(void);
2932void activeDefragCycle(void);
2933unsigned int getLRUClock(void);
2934unsigned int LRU_CLOCK(void);
2935const char *evictPolicyToString(void);
2936struct redisMemOverhead *getMemoryOverheadData(void);
2937void freeMemoryOverheadData(struct redisMemOverhead *mh);
2938void checkChildrenDone(void);
2939int setOOMScoreAdj(int process_class);
2940void rejectCommandFormat(client *c, const char *fmt, ...);
2941void *activeDefragAlloc(void *ptr);
2942robj *activeDefragStringOb(robj* ob, long *defragged);
2943void dismissSds(sds s);
2944void dismissMemory(void* ptr, size_t size_hint);
2945void dismissMemoryInChild(void);
2946
2947#define RESTART_SERVER_NONE 0
2948#define RESTART_SERVER_GRACEFULLY (1<<0) /* Do proper shutdown. */
2949#define RESTART_SERVER_CONFIG_REWRITE (1<<1) /* CONFIG REWRITE before restart.*/
2950int restartServer(int flags, mstime_t delay);
2951
2952/* Set data type */
2953robj *setTypeCreate(sds value);
2954int setTypeAdd(robj *subject, sds value);
2955int setTypeRemove(robj *subject, sds value);
2956int setTypeIsMember(robj *subject, sds value);
2957setTypeIterator *setTypeInitIterator(robj *subject);
2958void setTypeReleaseIterator(setTypeIterator *si);
2959int setTypeNext(setTypeIterator *si, sds *sdsele, int64_t *llele);
2960sds setTypeNextObject(setTypeIterator *si);
2961int setTypeRandomElement(robj *setobj, sds *sdsele, int64_t *llele);
2962unsigned long setTypeRandomElements(robj *set, unsigned long count, robj *aux_set);
2963unsigned long setTypeSize(const robj *subject);
2964void setTypeConvert(robj *subject, int enc);
2965robj *setTypeDup(robj *o);
2966
2967/* Hash data type */
2968#define HASH_SET_TAKE_FIELD (1<<0)
2969#define HASH_SET_TAKE_VALUE (1<<1)
2970#define HASH_SET_COPY 0
2971
2972void hashTypeConvert(robj *o, int enc);
2973void hashTypeTryConversion(robj *subject, robj **argv, int start, int end);
2974int hashTypeExists(robj *o, sds key);
2975int hashTypeDelete(robj *o, sds key);
2976unsigned long hashTypeLength(const robj *o);
2977hashTypeIterator *hashTypeInitIterator(robj *subject);
2978void hashTypeReleaseIterator(hashTypeIterator *hi);
2979int hashTypeNext(hashTypeIterator *hi);
2980void hashTypeCurrentFromListpack(hashTypeIterator *hi, int what,
2981 unsigned char **vstr,
2982 unsigned int *vlen,
2983 long long *vll);
2984sds hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what);
2985void hashTypeCurrentObject(hashTypeIterator *hi, int what, unsigned char **vstr, unsigned int *vlen, long long *vll);
2986sds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what);
2987robj *hashTypeLookupWriteOrCreate(client *c, robj *key);
2988robj *hashTypeGetValueObject(robj *o, sds field);
2989int hashTypeSet(robj *o, sds field, sds value, int flags);
2990robj *hashTypeDup(robj *o);
2991
2992/* Pub / Sub */
2993int pubsubUnsubscribeAllChannels(client *c, int notify);
2994int pubsubUnsubscribeShardAllChannels(client *c, int notify);
2995void pubsubUnsubscribeShardChannels(robj **channels, unsigned int count);
2996int pubsubUnsubscribeAllPatterns(client *c, int notify);
2997int pubsubPublishMessage(robj *channel, robj *message, int sharded);
2998int pubsubPublishMessageAndPropagateToCluster(robj *channel, robj *message, int sharded);
2999void addReplyPubsubMessage(client *c, robj *channel, robj *msg, robj *message_bulk);
3000int serverPubsubSubscriptionCount();
3001int serverPubsubShardSubscriptionCount();
3002size_t pubsubMemOverhead(client *c);
3003
3004/* Keyspace events notification */
3005void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid);
3006int keyspaceEventsStringToFlags(char *classes);
3007sds keyspaceEventsFlagsToString(int flags);
3008
3009/* Configuration */
3010/* Configuration Flags */
3011#define MODIFIABLE_CONFIG 0 /* This is the implied default for a standard
3012 * config, which is mutable. */
3013#define IMMUTABLE_CONFIG (1ULL<<0) /* Can this value only be set at startup? */
3014#define SENSITIVE_CONFIG (1ULL<<1) /* Does this value contain sensitive information */
3015#define DEBUG_CONFIG (1ULL<<2) /* Values that are useful for debugging. */
3016#define MULTI_ARG_CONFIG (1ULL<<3) /* This config receives multiple arguments. */
3017#define HIDDEN_CONFIG (1ULL<<4) /* This config is hidden in `config get <pattern>` (used for tests/debugging) */
3018#define PROTECTED_CONFIG (1ULL<<5) /* Becomes immutable if enable-protected-configs is enabled. */
3019#define DENY_LOADING_CONFIG (1ULL<<6) /* This config is forbidden during loading. */
3020#define ALIAS_CONFIG (1ULL<<7) /* For configs with multiple names, this flag is set on the alias. */
3021#define MODULE_CONFIG (1ULL<<8) /* This config is a module config */
3022#define VOLATILE_CONFIG (1ULL<<9) /* The config is a reference to the config data and not the config data itself (ex.
3023 * a file name containing more configuration like a tls key). In this case we want
3024 * to apply the configuration change even if the new config value is the same as
3025 * the old. */
3026
3027#define INTEGER_CONFIG 0 /* No flags means a simple integer configuration */
3028#define MEMORY_CONFIG (1<<0) /* Indicates if this value can be loaded as a memory value */
3029#define PERCENT_CONFIG (1<<1) /* Indicates if this value can be loaded as a percent (and stored as a negative int) */
3030#define OCTAL_CONFIG (1<<2) /* This value uses octal representation */
3031
3032/* Enum Configs contain an array of configEnum objects that match a string with an integer. */
3033typedef struct configEnum {
3034 char *name;
3035 int val;
3036} configEnum;
3037
3038/* Type of configuration. */
3039typedef enum {
3040 BOOL_CONFIG,
3041 NUMERIC_CONFIG,
3042 STRING_CONFIG,
3043 SDS_CONFIG,
3044 ENUM_CONFIG,
3045 SPECIAL_CONFIG,
3046} configType;
3047
3048void loadServerConfig(char *filename, char config_from_stdin, char *options);
3049void appendServerSaveParams(time_t seconds, int changes);
3050void resetServerSaveParams(void);
3051struct rewriteConfigState; /* Forward declaration to export API. */
3052void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force);
3053void rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, const char *option);
3054int rewriteConfig(char *path, int force_write);
3055void initConfigValues();
3056void removeConfig(sds name);
3057sds getConfigDebugInfo();
3058int allowProtectedAction(int config, client *c);
3059
3060/* Module Configuration */
3061typedef struct ModuleConfig ModuleConfig;
3062int performModuleConfigSetFromName(sds name, sds value, const char **err);
3063int performModuleConfigSetDefaultFromName(sds name, const char **err);
3064void addModuleBoolConfig(const char *module_name, const char *name, int flags, void *privdata, int default_val);
3065void addModuleStringConfig(const char *module_name, const char *name, int flags, void *privdata, sds default_val);
3066void addModuleEnumConfig(const char *module_name, const char *name, int flags, void *privdata, int default_val, configEnum *enum_vals);
3067void addModuleNumericConfig(const char *module_name, const char *name, int flags, void *privdata, long long default_val, int conf_flags, long long lower, long long upper);
3068void addModuleConfigApply(list *module_configs, ModuleConfig *module_config);
3069int moduleConfigApplyConfig(list *module_configs, const char **err, const char **err_arg_name);
3070int getModuleBoolConfig(ModuleConfig *module_config);
3071int setModuleBoolConfig(ModuleConfig *config, int val, const char **err);
3072sds getModuleStringConfig(ModuleConfig *module_config);
3073int setModuleStringConfig(ModuleConfig *config, sds strval, const char **err);
3074int getModuleEnumConfig(ModuleConfig *module_config);
3075int setModuleEnumConfig(ModuleConfig *config, int val, const char **err);
3076long long getModuleNumericConfig(ModuleConfig *module_config);
3077int setModuleNumericConfig(ModuleConfig *config, long long val, const char **err);
3078
3079/* db.c -- Keyspace access API */
3080int removeExpire(redisDb *db, robj *key);
3081void deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj);
3082void propagateDeletion(redisDb *db, robj *key, int lazy);
3083int keyIsExpired(redisDb *db, robj *key);
3084long long getExpire(redisDb *db, robj *key);
3085void setExpire(client *c, redisDb *db, robj *key, long long when);
3086int checkAlreadyExpired(long long when);
3087robj *lookupKeyRead(redisDb *db, robj *key);
3088robj *lookupKeyWrite(redisDb *db, robj *key);
3089robj *lookupKeyReadOrReply(client *c, robj *key, robj *reply);
3090robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply);
3091robj *lookupKeyReadWithFlags(redisDb *db, robj *key, int flags);
3092robj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags);
3093robj *objectCommandLookup(client *c, robj *key);
3094robj *objectCommandLookupOrReply(client *c, robj *key, robj *reply);
3095int objectSetLRUOrLFU(robj *val, long long lfu_freq, long long lru_idle,
3096 long long lru_clock, int lru_multiplier);
3097#define LOOKUP_NONE 0
3098#define LOOKUP_NOTOUCH (1<<0) /* Don't update LRU. */
3099#define LOOKUP_NONOTIFY (1<<1) /* Don't trigger keyspace event on key misses. */
3100#define LOOKUP_NOSTATS (1<<2) /* Don't update keyspace hits/misses counters. */
3101#define LOOKUP_WRITE (1<<3) /* Delete expired keys even in replicas. */
3102
3103void dbAdd(redisDb *db, robj *key, robj *val);
3104int dbAddRDBLoad(redisDb *db, sds key, robj *val);
3105void dbOverwrite(redisDb *db, robj *key, robj *val);
3106
3107#define SETKEY_KEEPTTL 1
3108#define SETKEY_NO_SIGNAL 2
3109#define SETKEY_ALREADY_EXIST 4
3110#define SETKEY_DOESNT_EXIST 8
3111void setKey(client *c, redisDb *db, robj *key, robj *val, int flags);
3112robj *dbRandomKey(redisDb *db);
3113int dbSyncDelete(redisDb *db, robj *key);
3114int dbDelete(redisDb *db, robj *key);
3115robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o);
3116
3117#define EMPTYDB_NO_FLAGS 0 /* No flags. */
3118#define EMPTYDB_ASYNC (1<<0) /* Reclaim memory in another thread. */
3119#define EMPTYDB_NOFUNCTIONS (1<<1) /* Indicate not to flush the functions. */
3120long long emptyData(int dbnum, int flags, void(callback)(dict*));
3121long long emptyDbStructure(redisDb *dbarray, int dbnum, int async, void(callback)(dict*));
3122void flushAllDataAndResetRDB(int flags);
3123long long dbTotalServerKeyCount();
3124redisDb *initTempDb(void);
3125void discardTempDb(redisDb *tempDb, void(callback)(dict*));
3126
3127
3128int selectDb(client *c, int id);
3129void signalModifiedKey(client *c, redisDb *db, robj *key);
3130void signalFlushedDb(int dbid, int async);
3131void scanGenericCommand(client *c, robj *o, unsigned long cursor);
3132int parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor);
3133int dbAsyncDelete(redisDb *db, robj *key);
3134void emptyDbAsync(redisDb *db);
3135size_t lazyfreeGetPendingObjectsCount(void);
3136size_t lazyfreeGetFreedObjectsCount(void);
3137void lazyfreeResetStats(void);
3138void freeObjAsync(robj *key, robj *obj, int dbid);
3139void freeReplicationBacklogRefMemAsync(list *blocks, rax *index);
3140
3141/* API to get key arguments from commands */
3142#define GET_KEYSPEC_DEFAULT 0
3143#define GET_KEYSPEC_INCLUDE_NOT_KEYS (1<<0) /* Consider 'fake' keys as keys */
3144#define GET_KEYSPEC_RETURN_PARTIAL (1<<1) /* Return all keys that can be found */
3145
3146int getKeysFromCommandWithSpecs(struct redisCommand *cmd, robj **argv, int argc, int search_flags, getKeysResult *result);
3147keyReference *getKeysPrepareResult(getKeysResult *result, int numkeys);
3148int getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3149int doesCommandHaveKeys(struct redisCommand *cmd);
3150int getChannelsFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3151int doesCommandHaveChannelsWithFlags(struct redisCommand *cmd, int flags);
3152void getKeysFreeResult(getKeysResult *result);
3153int sintercardGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
3154int zunionInterDiffGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
3155int zunionInterDiffStoreGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
3156int evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3157int functionGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3158int sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3159int sortROGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3160int migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3161int georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3162int xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3163int lmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3164int blmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3165int zmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3166int bzmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3167int setGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3168int bitfieldGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3169
3170unsigned short crc16(const char *buf, int len);
3171
3172/* Sentinel */
3173void initSentinelConfig(void);
3174void initSentinel(void);
3175void sentinelTimer(void);
3176const char *sentinelHandleConfiguration(char **argv, int argc);
3177void queueSentinelConfig(sds *argv, int argc, int linenum, sds line);
3178void loadSentinelConfigFromQueue(void);
3179void sentinelIsRunning(void);
3180void sentinelCheckConfigFile(void);
3181void sentinelCommand(client *c);
3182void sentinelInfoCommand(client *c);
3183void sentinelPublishCommand(client *c);
3184void sentinelRoleCommand(client *c);
3185
3186/* redis-check-rdb & aof */
3187int redis_check_rdb(char *rdbfilename, FILE *fp);
3188int redis_check_rdb_main(int argc, char **argv, FILE *fp);
3189int redis_check_aof_main(int argc, char **argv);
3190
3191/* Scripting */
3192void scriptingInit(int setup);
3193int ldbRemoveChild(pid_t pid);
3194void ldbKillForkedSessions(void);
3195int ldbPendingChildren(void);
3196sds luaCreateFunction(client *c, robj *body);
3197void luaLdbLineHook(lua_State *lua, lua_Debug *ar);
3198void freeLuaScriptsAsync(dict *lua_scripts);
3199void freeFunctionsAsync(functionsLibCtx *lib_ctx);
3200int ldbIsEnabled();
3201void ldbLog(sds entry);
3202void ldbLogRedisReply(char *reply);
3203void sha1hex(char *digest, char *script, size_t len);
3204unsigned long evalMemory();
3205dict* evalScriptsDict();
3206unsigned long evalScriptsMemory();
3207uint64_t evalGetCommandFlags(client *c, uint64_t orig_flags);
3208uint64_t fcallGetCommandFlags(client *c, uint64_t orig_flags);
3209int isInsideYieldingLongCommand();
3210
3211typedef struct luaScript {
3212 uint64_t flags;
3213 robj *body;
3214} luaScript;
3215
3216/* Blocked clients */
3217void processUnblockedClients(void);
3218void blockClient(client *c, int btype);
3219void unblockClient(client *c);
3220void queueClientForReprocessing(client *c);
3221void replyToBlockedClientTimedOut(client *c);
3222int getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit);
3223void disconnectAllBlockedClients(void);
3224void handleClientsBlockedOnKeys(void);
3225void signalKeyAsReady(redisDb *db, robj *key, int type);
3226void blockForKeys(client *c, int btype, robj **keys, int numkeys, long count, mstime_t timeout, robj *target, struct blockPos *blockpos, streamID *ids);
3227void updateStatsOnUnblock(client *c, long blocked_us, long reply_us, int had_errors);
3228void scanDatabaseForDeletedStreams(redisDb *emptied, redisDb *replaced_with);
3229
3230/* timeout.c -- Blocked clients timeout and connections timeout. */
3231void addClientToTimeoutTable(client *c);
3232void removeClientFromTimeoutTable(client *c);
3233void handleBlockedClientsTimeout(void);
3234int clientsCronHandleTimeout(client *c, mstime_t now_ms);
3235
3236/* expire.c -- Handling of expired keys */
3237void activeExpireCycle(int type);
3238void expireSlaveKeys(void);
3239void rememberSlaveKeyWithExpire(redisDb *db, robj *key);
3240void flushSlaveKeysWithExpireList(void);
3241size_t getSlaveKeyWithExpireCount(void);
3242
3243/* evict.c -- maxmemory handling and LRU eviction. */
3244void evictionPoolAlloc(void);
3245#define LFU_INIT_VAL 5
3246unsigned long LFUGetTimeInMinutes(void);
3247uint8_t LFULogIncr(uint8_t value);
3248unsigned long LFUDecrAndReturn(robj *o);
3249#define EVICT_OK 0
3250#define EVICT_RUNNING 1
3251#define EVICT_FAIL 2
3252int performEvictions(void);
3253void startEvictionTimeProc(void);
3254
3255/* Keys hashing / comparison functions for dict.c hash tables. */
3256uint64_t dictSdsHash(const void *key);
3257uint64_t dictSdsCaseHash(const void *key);
3258int dictSdsKeyCompare(dict *d, const void *key1, const void *key2);
3259int dictSdsKeyCaseCompare(dict *d, const void *key1, const void *key2);
3260void dictSdsDestructor(dict *d, void *val);
3261void *dictSdsDup(dict *d, const void *key);
3262
3263/* Git SHA1 */
3264char *redisGitSHA1(void);
3265char *redisGitDirty(void);
3266uint64_t redisBuildId(void);
3267char *redisBuildIdString(void);
3268
3269/* Commands prototypes */
3270void authCommand(client *c);
3271void pingCommand(client *c);
3272void echoCommand(client *c);
3273void commandCommand(client *c);
3274void commandCountCommand(client *c);
3275void commandListCommand(client *c);
3276void commandInfoCommand(client *c);
3277void commandGetKeysCommand(client *c);
3278void commandGetKeysAndFlagsCommand(client *c);
3279void commandHelpCommand(client *c);
3280void commandDocsCommand(client *c);
3281void setCommand(client *c);
3282void setnxCommand(client *c);
3283void setexCommand(client *c);
3284void psetexCommand(client *c);
3285void getCommand(client *c);
3286void getexCommand(client *c);
3287void getdelCommand(client *c);
3288void delCommand(client *c);
3289void unlinkCommand(client *c);
3290void existsCommand(client *c);
3291void setbitCommand(client *c);
3292void getbitCommand(client *c);
3293void bitfieldCommand(client *c);
3294void bitfieldroCommand(client *c);
3295void setrangeCommand(client *c);
3296void getrangeCommand(client *c);
3297void incrCommand(client *c);
3298void decrCommand(client *c);
3299void incrbyCommand(client *c);
3300void decrbyCommand(client *c);
3301void incrbyfloatCommand(client *c);
3302void selectCommand(client *c);
3303void swapdbCommand(client *c);
3304void randomkeyCommand(client *c);
3305void keysCommand(client *c);
3306void scanCommand(client *c);
3307void dbsizeCommand(client *c);
3308void lastsaveCommand(client *c);
3309void saveCommand(client *c);
3310void bgsaveCommand(client *c);
3311void bgrewriteaofCommand(client *c);
3312void shutdownCommand(client *c);
3313void slowlogCommand(client *c);
3314void moveCommand(client *c);
3315void copyCommand(client *c);
3316void renameCommand(client *c);
3317void renamenxCommand(client *c);
3318void lpushCommand(client *c);
3319void rpushCommand(client *c);
3320void lpushxCommand(client *c);
3321void rpushxCommand(client *c);
3322void linsertCommand(client *c);
3323void lpopCommand(client *c);
3324void rpopCommand(client *c);
3325void lmpopCommand(client *c);
3326void llenCommand(client *c);
3327void lindexCommand(client *c);
3328void lrangeCommand(client *c);
3329void ltrimCommand(client *c);
3330void typeCommand(client *c);
3331void lsetCommand(client *c);
3332void saddCommand(client *c);
3333void sremCommand(client *c);
3334void smoveCommand(client *c);
3335void sismemberCommand(client *c);
3336void smismemberCommand(client *c);
3337void scardCommand(client *c);
3338void spopCommand(client *c);
3339void srandmemberCommand(client *c);
3340void sinterCommand(client *c);
3341void sinterCardCommand(client *c);
3342void sinterstoreCommand(client *c);
3343void sunionCommand(client *c);
3344void sunionstoreCommand(client *c);
3345void sdiffCommand(client *c);
3346void sdiffstoreCommand(client *c);
3347void sscanCommand(client *c);
3348void syncCommand(client *c);
3349void flushdbCommand(client *c);
3350void flushallCommand(client *c);
3351void sortCommand(client *c);
3352void sortroCommand(client *c);
3353void lremCommand(client *c);
3354void lposCommand(client *c);
3355void rpoplpushCommand(client *c);
3356void lmoveCommand(client *c);
3357void infoCommand(client *c);
3358void mgetCommand(client *c);
3359void monitorCommand(client *c);
3360void expireCommand(client *c);
3361void expireatCommand(client *c);
3362void pexpireCommand(client *c);
3363void pexpireatCommand(client *c);
3364void getsetCommand(client *c);
3365void ttlCommand(client *c);
3366void touchCommand(client *c);
3367void pttlCommand(client *c);
3368void expiretimeCommand(client *c);
3369void pexpiretimeCommand(client *c);
3370void persistCommand(client *c);
3371void replicaofCommand(client *c);
3372void roleCommand(client *c);
3373void debugCommand(client *c);
3374void msetCommand(client *c);
3375void msetnxCommand(client *c);
3376void zaddCommand(client *c);
3377void zincrbyCommand(client *c);
3378void zrangeCommand(client *c);
3379void zrangebyscoreCommand(client *c);
3380void zrevrangebyscoreCommand(client *c);
3381void zrangebylexCommand(client *c);
3382void zrevrangebylexCommand(client *c);
3383void zcountCommand(client *c);
3384void zlexcountCommand(client *c);
3385void zrevrangeCommand(client *c);
3386void zcardCommand(client *c);
3387void zremCommand(client *c);
3388void zscoreCommand(client *c);
3389void zmscoreCommand(client *c);
3390void zremrangebyscoreCommand(client *c);
3391void zremrangebylexCommand(client *c);
3392void zpopminCommand(client *c);
3393void zpopmaxCommand(client *c);
3394void zmpopCommand(client *c);
3395void bzpopminCommand(client *c);
3396void bzpopmaxCommand(client *c);
3397void bzmpopCommand(client *c);
3398void zrandmemberCommand(client *c);
3399void multiCommand(client *c);
3400void execCommand(client *c);
3401void discardCommand(client *c);
3402void blpopCommand(client *c);
3403void brpopCommand(client *c);
3404void blmpopCommand(client *c);
3405void brpoplpushCommand(client *c);
3406void blmoveCommand(client *c);
3407void appendCommand(client *c);
3408void strlenCommand(client *c);
3409void zrankCommand(client *c);
3410void zrevrankCommand(client *c);
3411void hsetCommand(client *c);
3412void hsetnxCommand(client *c);
3413void hgetCommand(client *c);
3414void hmgetCommand(client *c);
3415void hdelCommand(client *c);
3416void hlenCommand(client *c);
3417void hstrlenCommand(client *c);
3418void zremrangebyrankCommand(client *c);
3419void zunionstoreCommand(client *c);
3420void zinterstoreCommand(client *c);
3421void zdiffstoreCommand(client *c);
3422void zunionCommand(client *c);
3423void zinterCommand(client *c);
3424void zinterCardCommand(client *c);
3425void zrangestoreCommand(client *c);
3426void zdiffCommand(client *c);
3427void zscanCommand(client *c);
3428void hkeysCommand(client *c);
3429void hvalsCommand(client *c);
3430void hgetallCommand(client *c);
3431void hexistsCommand(client *c);
3432void hscanCommand(client *c);
3433void hrandfieldCommand(client *c);
3434void configSetCommand(client *c);
3435void configGetCommand(client *c);
3436void configResetStatCommand(client *c);
3437void configRewriteCommand(client *c);
3438void configHelpCommand(client *c);
3439void hincrbyCommand(client *c);
3440void hincrbyfloatCommand(client *c);
3441void subscribeCommand(client *c);
3442void unsubscribeCommand(client *c);
3443void psubscribeCommand(client *c);
3444void punsubscribeCommand(client *c);
3445void publishCommand(client *c);
3446void pubsubCommand(client *c);
3447void spublishCommand(client *c);
3448void ssubscribeCommand(client *c);
3449void sunsubscribeCommand(client *c);
3450void watchCommand(client *c);
3451void unwatchCommand(client *c);
3452void clusterCommand(client *c);
3453void restoreCommand(client *c);
3454void migrateCommand(client *c);
3455void askingCommand(client *c);
3456void readonlyCommand(client *c);
3457void readwriteCommand(client *c);
3458int verifyDumpPayload(unsigned char *p, size_t len, uint16_t *rdbver_ptr);
3459void dumpCommand(client *c);
3460void objectCommand(client *c);
3461void memoryCommand(client *c);
3462void clientCommand(client *c);
3463void helloCommand(client *c);
3464void evalCommand(client *c);
3465void evalRoCommand(client *c);
3466void evalShaCommand(client *c);
3467void evalShaRoCommand(client *c);
3468void scriptCommand(client *c);
3469void fcallCommand(client *c);
3470void fcallroCommand(client *c);
3471void functionLoadCommand(client *c);
3472void functionDeleteCommand(client *c);
3473void functionKillCommand(client *c);
3474void functionStatsCommand(client *c);
3475void functionListCommand(client *c);
3476void functionHelpCommand(client *c);
3477void functionFlushCommand(client *c);
3478void functionRestoreCommand(client *c);
3479void functionDumpCommand(client *c);
3480void timeCommand(client *c);
3481void bitopCommand(client *c);
3482void bitcountCommand(client *c);
3483void bitposCommand(client *c);
3484void replconfCommand(client *c);
3485void waitCommand(client *c);
3486void georadiusbymemberCommand(client *c);
3487void georadiusbymemberroCommand(client *c);
3488void georadiusCommand(client *c);
3489void georadiusroCommand(client *c);
3490void geoaddCommand(client *c);
3491void geohashCommand(client *c);
3492void geoposCommand(client *c);
3493void geodistCommand(client *c);
3494void geosearchCommand(client *c);
3495void geosearchstoreCommand(client *c);
3496void pfselftestCommand(client *c);
3497void pfaddCommand(client *c);
3498void pfcountCommand(client *c);
3499void pfmergeCommand(client *c);
3500void pfdebugCommand(client *c);
3501void latencyCommand(client *c);
3502void moduleCommand(client *c);
3503void securityWarningCommand(client *c);
3504void xaddCommand(client *c);
3505void xrangeCommand(client *c);
3506void xrevrangeCommand(client *c);
3507void xlenCommand(client *c);
3508void xreadCommand(client *c);
3509void xgroupCommand(client *c);
3510void xsetidCommand(client *c);
3511void xackCommand(client *c);
3512void xpendingCommand(client *c);
3513void xclaimCommand(client *c);
3514void xautoclaimCommand(client *c);
3515void xinfoCommand(client *c);
3516void xdelCommand(client *c);
3517void xtrimCommand(client *c);
3518void lolwutCommand(client *c);
3519void aclCommand(client *c);
3520void lcsCommand(client *c);
3521void quitCommand(client *c);
3522void resetCommand(client *c);
3523void failoverCommand(client *c);
3524
3525#if defined(__GNUC__)
3526void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
3527void free(void *ptr) __attribute__ ((deprecated));
3528void *malloc(size_t size) __attribute__ ((deprecated));
3529void *realloc(void *ptr, size_t size) __attribute__ ((deprecated));
3530#endif
3531
3532/* Debugging stuff */
3533void _serverAssertWithInfo(const client *c, const robj *o, const char *estr, const char *file, int line);
3534void _serverAssert(const char *estr, const char *file, int line);
3535#ifdef __GNUC__
3536void _serverPanic(const char *file, int line, const char *msg, ...)
3537 __attribute__ ((format (printf, 3, 4)));
3538#else
3539void _serverPanic(const char *file, int line, const char *msg, ...);
3540#endif
3541void serverLogObjectDebugInfo(const robj *o);
3542void sigsegvHandler(int sig, siginfo_t *info, void *secret);
3543const char *getSafeInfoString(const char *s, size_t len, char **tmp);
3544dict *genInfoSectionDict(robj **argv, int argc, char **defaults, int *out_all, int *out_everything);
3545void releaseInfoSectionDict(dict *sec);
3546sds genRedisInfoString(dict *section_dict, int all_sections, int everything);
3547sds genModulesInfoString(sds info);
3548void applyWatchdogPeriod();
3549void watchdogScheduleSignal(int period);
3550void serverLogHexDump(int level, char *descr, void *value, size_t len);
3551int memtest_preserving_test(unsigned long *m, size_t bytes, int passes);
3552void mixDigest(unsigned char *digest, const void *ptr, size_t len);
3553void xorDigest(unsigned char *digest, const void *ptr, size_t len);
3554sds catSubCommandFullname(const char *parent_name, const char *sub_name);
3555void commandAddSubcommand(struct redisCommand *parent, struct redisCommand *subcommand, const char *declared_name);
3556void debugDelay(int usec);
3557void killIOThreads(void);
3558void killThreads(void);
3559void makeThreadKillable(void);
3560void swapMainDbWithTempDb(redisDb *tempDb);
3561
3562/* Use macro for checking log level to avoid evaluating arguments in cases log
3563 * should be ignored due to low level. */
3564#define serverLog(level, ...) do {\
3565 if (((level)&0xff) < server.verbosity) break;\
3566 _serverLog(level, __VA_ARGS__);\
3567 } while(0)
3568
3569/* TLS stuff */
3570void tlsInit(void);
3571void tlsCleanup(void);
3572int tlsConfigure(redisTLSContextConfig *ctx_config);
3573int isTlsConfigured(void);
3574
3575#define redisDebug(fmt, ...) \
3576 printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
3577#define redisDebugMark() \
3578 printf("-- MARK %s:%d --\n", __FILE__, __LINE__)
3579
3580int iAmMaster(void);
3581
3582#define STRINGIFY_(x) #x
3583#define STRINGIFY(x) STRINGIFY_(x)
3584
3585#endif
3586