summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/server.h
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
commitdcacc00e3750300617ba6e16eb346713f91a783a (patch)
tree38e2d4fb5ed9d119711d4295c6eda4b014af73fd /examples/redis-unstable/src/server.h
parent58dac10aeb8f5a041c46bddbeaf4c7966a99b998 (diff)
downloadcrep-dcacc00e3750300617ba6e16eb346713f91a783a.tar.gz
Remove testing data
Diffstat (limited to 'examples/redis-unstable/src/server.h')
-rw-r--r--examples/redis-unstable/src/server.h4478
1 files changed, 0 insertions, 4478 deletions
diff --git a/examples/redis-unstable/src/server.h b/examples/redis-unstable/src/server.h
deleted file mode 100644
index 96eb286..0000000
--- a/examples/redis-unstable/src/server.h
+++ /dev/null
@@ -1,4478 +0,0 @@
1/*
2 * Copyright (c) 2009-Present, Redis Ltd.
3 * All rights reserved.
4 *
5 * Copyright (c) 2024-present, Valkey contributors.
6 * All rights reserved.
7 *
8 * Licensed under your choice of (a) the Redis Source Available License 2.0
9 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
10 * GNU Affero General Public License v3 (AGPLv3).
11 *
12 * Portions of this file are available under BSD3 terms; see REDISCONTRIBUTIONS for more information.
13 */
14
15#ifndef __REDIS_H
16#define __REDIS_H
17
18#include "fmacros.h"
19#include "config.h"
20#include "solarisfixes.h"
21#include "rio.h"
22#include "atomicvar.h"
23#include "commands.h"
24#include "object.h"
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <stddef.h>
29#include <string.h>
30#include <time.h>
31#include <limits.h>
32#include <unistd.h>
33#include <errno.h>
34#include <inttypes.h>
35#include <pthread.h>
36#include <syslog.h>
37#include <netinet/in.h>
38#include <sys/socket.h>
39#include <lua.h>
40#include <signal.h>
41
42#ifdef HAVE_LIBSYSTEMD
43#include <systemd/sd-daemon.h>
44#endif
45
46typedef long long mstime_t; /* millisecond time type. */
47typedef long long ustime_t; /* microsecond time type. */
48
49#include "ae.h" /* Event driven programming library */
50#include "sds.h" /* Dynamic safe strings */
51#include "entry.h" /* Entry objects (field-value pairs with optional expiration) */
52#include "ebuckets.h" /* expiry data structure */
53#include "dict.h" /* Hash tables */
54#include "kvstore.h" /* Slot-based hash table */
55#include "estore.h" /* Expiration store */
56#include "adlist.h" /* Linked lists */
57#include "zmalloc.h" /* total memory usage aware version of malloc/free */
58#include "anet.h" /* Networking the easy way */
59#include "version.h" /* Version macro */
60#include "util.h" /* Misc functions useful in many places */
61#include "latency.h" /* Latency monitor API */
62#include "sparkline.h" /* ASCII graphs API */
63#include "quicklist.h" /* Lists are encoded as linked lists of
64 N-elements flat arrays */
65#include "rax.h" /* Radix tree */
66#include "connection.h" /* Connection abstraction */
67#include "eventnotifier.h" /* Event notification */
68#include "memory_prefetch.h"
69
70/* Forward declarations needed by redismodule.h and keymeta.h */
71struct redisObject;
72struct RedisModule;
73
74/* This is a structure used to export some meta-information such as dbid to the module. */
75struct RedisModuleKeyOptCtx {
76 struct redisObject *from_key, *to_key; /* Optional name of key processed, NULL when unknown.
77 In most cases, only 'from_key' is valid, but in callbacks
78 such as `copy2`, both 'from_key' and 'to_key' are valid. */
79 int from_dbid, to_dbid; /* The dbid of the key being processed, -1 when unknown.
80 In most cases, only 'from_dbid' is valid, but in callbacks such
81 as `copy2`, 'from_dbid' and 'to_dbid' are both valid. */
82};
83
84#define REDISMODULE_CORE 1
85
86#include "redismodule.h" /* Redis modules API defines. */
87
88/* Following includes allow test functions to be called from Redis main() */
89#include "zipmap.h"
90#include "ziplist.h" /* Compact list data structure */
91#include "sha1.h"
92#include "endianconv.h"
93#include "crc64.h"
94#include "keymeta.h"
95
96struct hdr_histogram;
97
98/* helpers */
99#define numElements(x) (sizeof(x)/sizeof((x)[0]))
100
101/* min/max */
102#undef min
103#undef max
104#define min(a, b) ((a) < (b) ? (a) : (b))
105#define max(a, b) ((a) > (b) ? (a) : (b))
106
107/* Get the pointer of the outer struct from a member address */
108#define redis_member2struct(struct_name, member_name, member_addr) \
109 ((struct_name *)((char*)member_addr - offsetof(struct_name, member_name)))
110
111/* Error codes */
112#define C_OK 0
113#define C_ERR -1
114#define C_RETRY -2
115
116/* Static server configuration */
117#define CONFIG_DEFAULT_HZ 10 /* Time interrupt calls/sec. */
118#define CONFIG_MIN_HZ 1
119#define CONFIG_MAX_HZ 500
120#define MAX_CLIENTS_PER_CLOCK_TICK 200 /* HZ is adapted based on that. */
121#define CRON_DBS_PER_CALL 16
122#define CRON_DICTS_PER_DB 16
123#define NET_MAX_WRITES_PER_EVENT (1024*64)
124#define PROTO_SHARED_SELECT_CMDS 10
125#define OBJ_SHARED_INTEGERS 10000
126#define OBJ_SHARED_BULKHDR_LEN 32
127#define OBJ_SHARED_HDR_STRLEN(_len_) (((_len_) < 10) ? 4 : 5) /* see shared.mbulkhdr etc. */
128#define LOG_MAX_LEN 1024 /* Default maximum length of syslog messages.*/
129#define AOF_REWRITE_ITEMS_PER_CMD 64
130#define AOF_ANNOTATION_LINE_MAX_LEN 1024
131#define CONFIG_RUN_ID_SIZE 40
132#define RDB_EOF_MARK_SIZE 40
133#define CONFIG_REPL_BACKLOG_MIN_SIZE (1024*16) /* 16k */
134#define CONFIG_BGSAVE_RETRY_DELAY 5 /* Wait a few secs before trying again. */
135#define CONFIG_DEFAULT_PID_FILE "/var/run/redis.pid"
136#define CONFIG_DEFAULT_BINDADDR_COUNT 2
137#define CONFIG_DEFAULT_BINDADDR { "*", "-::*" }
138#define NET_HOST_STR_LEN 256 /* Longest valid hostname */
139#define NET_IP_STR_LEN 46 /* INET6_ADDRSTRLEN is 46, but we need to be sure */
140#define NET_ADDR_STR_LEN (NET_IP_STR_LEN+32) /* Must be enough for ip:port */
141#define NET_HOST_PORT_STR_LEN (NET_HOST_STR_LEN+32) /* Must be enough for hostname:port */
142#define CONFIG_BINDADDR_MAX 16
143#define CONFIG_MIN_RESERVED_FDS 32
144#define CONFIG_DEFAULT_PROC_TITLE_TEMPLATE "{title} {listen-addr} {server-mode}"
145#define INCREMENTAL_REHASHING_THRESHOLD_US 1000
146#define CLIENTS_CRON_MIN_ITERATIONS 5
147
148/* Stream IDMP configuration limits */
149#define CONFIG_STREAM_IDMP_MIN_DURATION 1 /* Min IDMP duration in seconds. */
150#define CONFIG_STREAM_IDMP_MAX_DURATION 86400 /* Max IDMP duration in seconds (24 hours). */
151#define CONFIG_STREAM_IDMP_MIN_MAXSIZE 1 /* Min IDMP max entries. */
152#define CONFIG_STREAM_IDMP_MAX_MAXSIZE 10000 /* Max IDMP max entries. */
153
154/* Bucket sizes for client eviction pools. Each bucket stores clients with
155 * memory usage of up to twice the size of the bucket below it. */
156#define CLIENT_MEM_USAGE_BUCKET_MIN_LOG 15 /* Bucket sizes start at up to 32KB (2^15) */
157#define CLIENT_MEM_USAGE_BUCKET_MAX_LOG 33 /* Bucket for largest clients: sizes above 4GB (2^32) */
158#define CLIENT_MEM_USAGE_BUCKETS (1+CLIENT_MEM_USAGE_BUCKET_MAX_LOG-CLIENT_MEM_USAGE_BUCKET_MIN_LOG)
159
160#define ACTIVE_EXPIRE_CYCLE_SLOW 0
161#define ACTIVE_EXPIRE_CYCLE_FAST 1
162
163/* Children process will exit with this status code to signal that the
164 * process terminated without an error: this is useful in order to kill
165 * a saving child (RDB or AOF one), without triggering in the parent the
166 * write protection that is normally turned on on write errors.
167 * Usually children that are terminated with SIGUSR1 will exit with this
168 * special code. */
169#define SERVER_CHILD_NOERROR_RETVAL 255
170
171/* Reading copy-on-write info is sometimes expensive and may slow down child
172 * processes that report it continuously. We measure the cost of obtaining it
173 * and hold back additional reading based on this factor. */
174#define CHILD_COW_DUTY_CYCLE 100
175
176/* Instantaneous metrics tracking. */
177#define STATS_METRIC_SAMPLES 16 /* Number of samples per metric. */
178#define STATS_METRIC_COMMAND 0 /* Number of commands executed. */
179#define STATS_METRIC_NET_INPUT 1 /* Bytes read from network. */
180#define STATS_METRIC_NET_OUTPUT 2 /* Bytes written to network. */
181#define STATS_METRIC_NET_INPUT_REPLICATION 3 /* Bytes read from network during replication. */
182#define STATS_METRIC_NET_OUTPUT_REPLICATION 4 /* Bytes written to network during replication. */
183#define STATS_METRIC_EL_CYCLE 5 /* Number of eventloop cycled. */
184#define STATS_METRIC_EL_DURATION 6 /* Eventloop duration. */
185#define STATS_METRIC_COUNT 7
186
187/* Protocol and I/O related defines */
188#define PROTO_IOBUF_LEN (1024*16) /* Generic I/O buffer size */
189#define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */
190#define PROTO_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */
191#define PROTO_MBULK_BIG_ARG (1024*32)
192#define PROTO_RESIZE_THRESHOLD (1024*32) /* Threshold for determining whether to resize query buffer */
193#define PROTO_REPLY_MIN_BYTES (1024) /* the lower limit on reply buffer size */
194#define REDIS_AUTOSYNC_BYTES (1024*1024*4) /* Sync file every 4MB. */
195
196#define REPLY_BUFFER_DEFAULT_PEAK_RESET_TIME 5000 /* 5 seconds */
197
198/* Reply copy avoidance thresholds */
199#define COPY_AVOID_MIN_IO_THREADS 7 /* Minimum number of IO threads for copy avoidance */
200#define COPY_AVOID_MIN_STRING_SIZE 16384 /* Minimum bulk string size for copy avoidance (no IO threads) */
201#define COPY_AVOID_MIN_STRING_SIZE_THREADED 65536 /* Minimum bulk string size for copy avoidance (with IO threads) */
202
203/* When configuring the server eventloop, we setup it so that the total number
204 * of file descriptors we can handle are server.maxclients + RESERVED_FDS +
205 * a few more to stay safe. Since RESERVED_FDS defaults to 32, we add 96
206 * in order to make sure of not over provisioning more than 128 fds. */
207#define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96)
208
209/* Default lookahead value */
210#define REDIS_DEFAULT_LOOKAHEAD 16
211
212/* OOM Score Adjustment classes. */
213#define CONFIG_OOM_MASTER 0
214#define CONFIG_OOM_REPLICA 1
215#define CONFIG_OOM_BGCHILD 2
216#define CONFIG_OOM_COUNT 3
217
218extern int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT];
219
220/* Hash table parameters */
221#define HASHTABLE_MAX_LOAD_FACTOR 1.618 /* Maximum hash table load factor. */
222
223/* Max number of IO threads */
224#define IO_THREADS_MAX_NUM 128
225
226/* To make IO threads and main thread run in parallel, we will transfer clients
227 * between them if the number of clients in the pending list reaches this value. */
228#define IO_THREAD_MAX_PENDING_CLIENTS 16
229
230/* Main thread id for doing IO work, whatever we enable or disable io thread
231 * the main thread always does IO work, so we can consider that the main thread
232 * is the io thread 0. */
233#define IOTHREAD_MAIN_THREAD_ID 0
234
235/* Command flags. Please check the definition of struct redisCommand in this file
236 * for more information about the meaning of every flag. */
237#define CMD_WRITE (1ULL<<0)
238#define CMD_READONLY (1ULL<<1)
239#define CMD_DENYOOM (1ULL<<2)
240#define CMD_MODULE (1ULL<<3) /* Command exported by module. */
241#define CMD_ADMIN (1ULL<<4)
242#define CMD_PUBSUB (1ULL<<5)
243#define CMD_NOSCRIPT (1ULL<<6)
244#define CMD_BLOCKING (1ULL<<8) /* Has potential to block. */
245#define CMD_LOADING (1ULL<<9)
246#define CMD_STALE (1ULL<<10)
247#define CMD_SKIP_MONITOR (1ULL<<11)
248#define CMD_SKIP_SLOWLOG (1ULL<<12)
249#define CMD_ASKING (1ULL<<13)
250#define CMD_FAST (1ULL<<14)
251#define CMD_NO_AUTH (1ULL<<15)
252#define CMD_MAY_REPLICATE (1ULL<<16)
253#define CMD_SENTINEL (1ULL<<17)
254#define CMD_ONLY_SENTINEL (1ULL<<18)
255#define CMD_NO_MANDATORY_KEYS (1ULL<<19)
256#define CMD_PROTECTED (1ULL<<20)
257#define CMD_MODULE_GETKEYS (1ULL<<21) /* Use the modules getkeys interface. */
258#define CMD_MODULE_NO_CLUSTER (1ULL<<22) /* Deny on Redis Cluster. */
259#define CMD_NO_ASYNC_LOADING (1ULL<<23)
260#define CMD_NO_MULTI (1ULL<<24)
261#define CMD_MOVABLE_KEYS (1ULL<<25) /* The legacy range spec doesn't cover all keys.
262 * Populated by populateCommandLegacyRangeSpec. */
263#define CMD_ALLOW_BUSY ((1ULL<<26))
264#define CMD_MODULE_GETCHANNELS (1ULL<<27) /* Use the modules getchannels interface. */
265#define CMD_TOUCHES_ARBITRARY_KEYS (1ULL<<28)
266#define CMD_INTERNAL (1ULL<<29) /* Internal command. */
267
268/* Command flags that describe ACLs categories. */
269#define ACL_CATEGORY_KEYSPACE (1ULL<<0)
270#define ACL_CATEGORY_READ (1ULL<<1)
271#define ACL_CATEGORY_WRITE (1ULL<<2)
272#define ACL_CATEGORY_SET (1ULL<<3)
273#define ACL_CATEGORY_SORTEDSET (1ULL<<4)
274#define ACL_CATEGORY_LIST (1ULL<<5)
275#define ACL_CATEGORY_HASH (1ULL<<6)
276#define ACL_CATEGORY_STRING (1ULL<<7)
277#define ACL_CATEGORY_BITMAP (1ULL<<8)
278#define ACL_CATEGORY_HYPERLOGLOG (1ULL<<9)
279#define ACL_CATEGORY_GEO (1ULL<<10)
280#define ACL_CATEGORY_STREAM (1ULL<<11)
281#define ACL_CATEGORY_PUBSUB (1ULL<<12)
282#define ACL_CATEGORY_ADMIN (1ULL<<13)
283#define ACL_CATEGORY_FAST (1ULL<<14)
284#define ACL_CATEGORY_SLOW (1ULL<<15)
285#define ACL_CATEGORY_BLOCKING (1ULL<<16)
286#define ACL_CATEGORY_DANGEROUS (1ULL<<17)
287#define ACL_CATEGORY_CONNECTION (1ULL<<18)
288#define ACL_CATEGORY_TRANSACTION (1ULL<<19)
289#define ACL_CATEGORY_SCRIPTING (1ULL<<20)
290
291/* Key-spec flags *
292 * -------------- */
293/* The following refer what the command actually does with the value or metadata
294 * of the key, and not necessarily the user data or how it affects it.
295 * Each key-spec may must have exactly one of these. Any operation that's not
296 * distinctly deletion, overwrite or read-only would be marked as RW. */
297#define CMD_KEY_RO (1ULL<<0) /* Read-Only - Reads the value of the key, but
298 * doesn't necessarily returns it. */
299#define CMD_KEY_RW (1ULL<<1) /* Read-Write - Reads and modifies/deletes
300 * the data stored in the value of the key or
301 * its metadata. */
302#define CMD_KEY_OW (1ULL<<2) /* Overwrite - Overwrites the data stored in
303 * the value of the key. */
304#define CMD_KEY_RM (1ULL<<3) /* Deletes the key without reading it's value. */
305/* The following refer to user data inside the value of the key, not the metadata
306 * like LRU, type, cardinality. It refers to the logical operation on the user's
307 * data (actual input strings / TTL), being used / returned / copied / changed,
308 * It doesn't refer to modification or returning of metadata (like type, count,
309 * presence of data). Any write that's not INSERT or DELETE, would be an UPDATE.
310 * Each key-spec may have one of the writes with or without access, or none: */
311#define CMD_KEY_ACCESS (1ULL<<4) /* Returns, copies or uses the user data from
312 * the value of the key. */
313#define CMD_KEY_UPDATE (1ULL<<5) /* Updates data to the value, new value may
314 * depend on the old value. */
315#define CMD_KEY_INSERT (1ULL<<6) /* Adds data to the value with no chance of
316 * modification or deletion of existing data. */
317#define CMD_KEY_DELETE (1ULL<<7) /* Explicitly deletes some content
318 * from the value of the key. */
319/* Other flags: */
320#define CMD_KEY_NOT_KEY (1ULL<<8) /* A 'fake' key that should be routed
321 * like a key in cluster mode but is
322 * excluded from other key checks. */
323#define CMD_KEY_INCOMPLETE (1ULL<<9) /* Means that the keyspec might not point
324 * out to all keys it should cover */
325#define CMD_KEY_VARIABLE_FLAGS (1ULL<<10) /* Means that some keys might have
326 * different flags depending on arguments */
327#define CMD_KEY_PREFIX (1ULL<<11) /* Given key represents a prefix of a set of keys */
328
329/* Key flags for when access type is unknown */
330#define CMD_KEY_FULL_ACCESS (CMD_KEY_RW | CMD_KEY_ACCESS | CMD_KEY_UPDATE)
331
332/* Key flags for how key is removed */
333#define DB_FLAG_KEY_NONE 0
334#define DB_FLAG_KEY_DELETED (1ULL<<0)
335#define DB_FLAG_KEY_EXPIRED (1ULL<<1)
336#define DB_FLAG_KEY_EVICTED (1ULL<<2)
337#define DB_FLAG_KEY_OVERWRITE (1ULL<<3)
338#define DB_FLAG_NO_UPDATE_KEYSIZES (1ULL<<4) /* Don't update keysizes histograms */
339
340/* Channel flags share the same flag space as the key flags */
341#define CMD_CHANNEL_PATTERN (1ULL<<11) /* The argument is a channel pattern */
342#define CMD_CHANNEL_SUBSCRIBE (1ULL<<12) /* The command subscribes to channels */
343#define CMD_CHANNEL_UNSUBSCRIBE (1ULL<<13) /* The command unsubscribes to channels */
344#define CMD_CHANNEL_PUBLISH (1ULL<<14) /* The command publishes to channels. */
345
346/* AOF states */
347#define AOF_OFF 0 /* AOF is off */
348#define AOF_ON 1 /* AOF is on */
349#define AOF_WAIT_REWRITE 2 /* AOF waits rewrite to start appending */
350
351/* AOF return values for loadAppendOnlyFiles() and loadSingleAppendOnlyFile() */
352#define AOF_OK 0
353#define AOF_NOT_EXIST 1
354#define AOF_EMPTY 2
355#define AOF_OPEN_ERR 3
356#define AOF_FAILED 4
357#define AOF_TRUNCATED 5
358#define AOF_BROKEN_RECOVERED 6
359
360/* RDB return values for rdbLoad. */
361#define RDB_OK 0
362#define RDB_NOT_EXIST 1 /* RDB file doesn't exist. */
363#define RDB_FAILED 2 /* Failed to load the RDB file. */
364
365/* Command doc flags */
366#define CMD_DOC_NONE 0
367#define CMD_DOC_DEPRECATED (1<<0) /* Command is deprecated */
368#define CMD_DOC_SYSCMD (1<<1) /* System (internal) command */
369
370/* Client flags */
371#define CLIENT_SLAVE (1<<0) /* This client is a replica */
372#define CLIENT_MASTER (1<<1) /* This client is a master */
373#define CLIENT_MONITOR (1<<2) /* This client is a slave monitor, see MONITOR */
374#define CLIENT_MULTI (1<<3) /* This client is in a MULTI context */
375#define CLIENT_BLOCKED (1<<4) /* The client is waiting in a blocking operation */
376#define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */
377#define CLIENT_CLOSE_AFTER_REPLY (1<<6) /* Close after writing entire reply. */
378#define CLIENT_UNBLOCKED (1<<7) /* This client was unblocked and is stored in
379 server.unblocked_clients */
380#define CLIENT_SCRIPT (1<<8) /* This is a non connected client used by Lua */
381#define CLIENT_ASKING (1<<9) /* Client issued the ASKING command */
382#define CLIENT_CLOSE_ASAP (1<<10)/* Close this client ASAP */
383#define CLIENT_UNIX_SOCKET (1<<11) /* Client connected via Unix domain socket */
384#define CLIENT_DIRTY_EXEC (1<<12) /* EXEC will fail for errors while queueing */
385#define CLIENT_MASTER_FORCE_REPLY (1<<13) /* Queue replies even if is master */
386#define CLIENT_FORCE_AOF (1<<14) /* Force AOF propagation of current cmd. */
387#define CLIENT_FORCE_REPL (1<<15) /* Force replication of current cmd. */
388#define CLIENT_PRE_PSYNC (1<<16) /* Instance don't understand PSYNC. */
389#define CLIENT_READONLY (1<<17) /* Cluster client is in read-only state. */
390#define CLIENT_PUBSUB (1<<18) /* Client is in Pub/Sub mode. */
391#define CLIENT_PREVENT_AOF_PROP (1<<19) /* Don't propagate to AOF. */
392#define CLIENT_PREVENT_REPL_PROP (1<<20) /* Don't propagate to slaves. */
393#define CLIENT_PREVENT_PROP (CLIENT_PREVENT_AOF_PROP|CLIENT_PREVENT_REPL_PROP)
394#define CLIENT_PENDING_WRITE (1<<21) /* Client has output to send but a write
395 handler is yet not installed. */
396#define CLIENT_REPLY_OFF (1<<22) /* Don't send replies to client. */
397#define CLIENT_REPLY_SKIP_NEXT (1<<23) /* Set CLIENT_REPLY_SKIP for next cmd */
398#define CLIENT_REPLY_SKIP (1<<24) /* Don't send just this reply. */
399#define CLIENT_LUA_DEBUG (1<<25) /* Run EVAL in debug mode. */
400#define CLIENT_LUA_DEBUG_SYNC (1<<26) /* EVAL debugging without fork() */
401#define CLIENT_MODULE (1<<27) /* Non connected client used by some module. */
402#define CLIENT_PROTECTED (1<<28) /* Client should not be freed for now. */
403#define CLIENT_EXECUTING_COMMAND (1<<29) /* Indicates that the client is currently in the process of handling
404 a command. usually this will be marked only during call()
405 however, blocked clients might have this flag kept until they
406 will try to reprocess the command. */
407
408#define CLIENT_PENDING_COMMAND (1<<30) /* Indicates the client has a fully
409 * parsed command ready for execution. */
410#define CLIENT_TRACKING (1ULL<<31) /* Client enabled keys tracking in order to
411 perform client side caching. */
412#define CLIENT_TRACKING_BROKEN_REDIR (1ULL<<32) /* Target client is invalid. */
413#define CLIENT_TRACKING_BCAST (1ULL<<33) /* Tracking in BCAST mode. */
414#define CLIENT_TRACKING_OPTIN (1ULL<<34) /* Tracking in opt-in mode. */
415#define CLIENT_TRACKING_OPTOUT (1ULL<<35) /* Tracking in opt-out mode. */
416#define CLIENT_TRACKING_CACHING (1ULL<<36) /* CACHING yes/no was given,
417 depending on optin/optout mode. */
418#define CLIENT_TRACKING_NOLOOP (1ULL<<37) /* Don't send invalidation messages
419 about writes performed by myself.*/
420#define CLIENT_IN_TO_TABLE (1ULL<<38) /* This client is in the timeout table. */
421#define CLIENT_PROTOCOL_ERROR (1ULL<<39) /* Protocol error chatting with it. */
422#define CLIENT_CLOSE_AFTER_COMMAND (1ULL<<40) /* Close after executing commands
423 * and writing entire reply. */
424#define CLIENT_DENY_BLOCKING (1ULL<<41) /* Indicate that the client should not be blocked.
425 currently, turned on inside MULTI, Lua, RM_Call,
426 and AOF client */
427#define CLIENT_REPL_RDBONLY (1ULL<<42) /* This client is a replica that only wants
428 RDB without replication buffer. */
429#define CLIENT_NO_EVICT (1ULL<<43) /* This client is protected against client
430 memory eviction. */
431#define CLIENT_ALLOW_OOM (1ULL<<44) /* Client used by RM_Call is allowed to fully execute
432 scripts even when in OOM */
433#define CLIENT_NO_TOUCH (1ULL<<45) /* This client will not touch LFU/LRU stats. */
434#define CLIENT_PUSHING (1ULL<<46) /* This client is pushing notifications. */
435#define CLIENT_MODULE_AUTH_HAS_RESULT (1ULL<<47) /* Indicates a client in the middle of module based
436 auth had been authenticated from the Module. */
437#define CLIENT_MODULE_PREVENT_AOF_PROP (1ULL<<48) /* Module client do not want to propagate to AOF */
438#define CLIENT_MODULE_PREVENT_REPL_PROP (1ULL<<49) /* Module client do not want to propagate to replica */
439#define CLIENT_REEXECUTING_COMMAND (1ULL<<50) /* The client is re-executing the command. */
440#define CLIENT_REPL_RDB_CHANNEL (1ULL<<51) /* Client which is used for rdb delivery as part of rdb channel replication */
441#define CLIENT_INTERNAL (1ULL<<52) /* Internal client connection */
442#define CLIENT_ASM_MIGRATING (1ULL<<53) /* Client is migrating RDB/stream data during atomic slot migration. */
443#define CLIENT_ASM_IMPORTING (1ULL<<54) /* Client is importing RDB/stream data during atomic slot migration. */
444
445/* Any flag that does not let optimize FLUSH SYNC to run it in bg as blocking client ASYNC */
446#define CLIENT_AVOID_BLOCKING_ASYNC_FLUSH (CLIENT_DENY_BLOCKING|CLIENT_MULTI|CLIENT_LUA_DEBUG|CLIENT_LUA_DEBUG_SYNC|CLIENT_MODULE)
447
448/* Max deferred objects to be freed by IO thread for each client. */
449#define CLIENT_MAX_DEFERRED_OBJECTS 32
450
451/* Client flags for client IO */
452#define CLIENT_IO_READ_ENABLED (1ULL<<0) /* Client can read from socket. */
453#define CLIENT_IO_WRITE_ENABLED (1ULL<<1) /* Client can write to socket. */
454#define CLIENT_IO_PENDING_COMMAND (1ULL<<2) /* Similar to CLIENT_PENDING_COMMAND. */
455#define CLIENT_IO_REUSABLE_QUERYBUFFER (1ULL<<3) /* The client is using the reusable query buffer. */
456#define CLIENT_IO_CLOSE_ASAP (1ULL<<4) /* Close this client ASAP in IO thread. */
457#define CLIENT_IO_PENDING_CRON (1ULL<<5) /* The client is pending cron job, to be processed in main thread. */
458
459/* Definitions for client read errors. These error codes are used to indicate
460 * various issues that can occur while reading or parsing data from a client. */
461#define CLIENT_READ_TOO_BIG_INLINE_REQUEST 1
462#define CLIENT_READ_UNBALANCED_QUOTES 2
463#define CLIENT_READ_MASTER_USING_INLINE_PROTOCAL 3
464#define CLIENT_READ_TOO_BIG_MBULK_COUNT_STRING 4
465#define CLIENT_READ_TOO_BIG_BUCK_COUNT_STRING 5
466#define CLIENT_READ_EXPECTED_DOLLAR 6
467#define CLIENT_READ_INVALID_BUCK_LENGTH 7
468#define CLIENT_READ_UNAUTH_BUCK_LENGTH 8
469#define CLIENT_READ_INVALID_MULTIBUCK_LENGTH 9
470#define CLIENT_READ_UNAUTH_MBUCK_COUNT 10
471#define CLIENT_READ_CONN_DISCONNECTED 11
472#define CLIENT_READ_CONN_CLOSED 12
473#define CLIENT_READ_REACHED_MAX_QUERYBUF 13
474#define CLIENT_READ_COMMAND_NOT_FOUND 14
475#define CLIENT_READ_BAD_ARITY 15
476#define CLIENT_READ_CROSS_SLOT 16
477
478/* Client block type (btype field in client structure)
479 * if CLIENT_BLOCKED flag is set. */
480typedef enum blocking_type {
481 BLOCKED_NONE, /* Not blocked, no CLIENT_BLOCKED flag set. */
482 BLOCKED_LIST, /* BLPOP & co. */
483 BLOCKED_WAIT, /* WAIT for synchronous replication. */
484 BLOCKED_WAITAOF, /* WAITAOF for AOF file fsync. */
485 BLOCKED_MODULE, /* Blocked by a loadable module. */
486 BLOCKED_STREAM, /* XREAD. */
487 BLOCKED_ZSET, /* BZPOP et al. */
488 BLOCKED_POSTPONE, /* Blocked by processCommand, re-try processing later. */
489 BLOCKED_POSTPONE_TRIM, /* Master client is blocked due to an active trim job. */
490 BLOCKED_SHUTDOWN, /* SHUTDOWN. */
491 BLOCKED_LAZYFREE, /* LAZYFREE */
492 BLOCKED_NUM, /* Number of blocked states. */
493 BLOCKED_END /* End of enumeration */
494} blocking_type;
495
496/* Client request types */
497#define PROTO_REQ_INLINE 1
498#define PROTO_REQ_MULTIBULK 2
499
500/* Client classes for client limits, currently used only for
501 * the max-client-output-buffer limit implementation. */
502#define CLIENT_TYPE_NORMAL 0 /* Normal req-reply clients + MONITORs */
503#define CLIENT_TYPE_SLAVE 1 /* Slaves. */
504#define CLIENT_TYPE_PUBSUB 2 /* Clients subscribed to PubSub channels. */
505#define CLIENT_TYPE_MASTER 3 /* Master. */
506#define CLIENT_TYPE_COUNT 4 /* Total number of client types. */
507#define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output
508 buffer configuration. Just the first
509 three: normal, slave, pubsub. */
510
511/* Slave replication state. Used in server.repl_state for slaves to remember
512 * what to do next. */
513typedef enum {
514 REPL_STATE_NONE = 0, /* No active replication */
515 REPL_STATE_CONNECT, /* Must connect to master */
516 REPL_STATE_CONNECTING, /* Connecting to master */
517 /* --- Handshake states, must be ordered --- */
518 REPL_STATE_RECEIVE_PING_REPLY, /* Wait for PING reply */
519 REPL_STATE_SEND_HANDSHAKE, /* Send handshake sequence to master */
520 REPL_STATE_RECEIVE_AUTH_REPLY, /* Wait for AUTH reply */
521 REPL_STATE_RECEIVE_PORT_REPLY, /* Wait for REPLCONF reply */
522 REPL_STATE_RECEIVE_IP_REPLY, /* Wait for REPLCONF reply */
523 REPL_STATE_RECEIVE_COMP_REPLY, /* Wait for REPLCONF reply */
524 REPL_STATE_RECEIVE_CAPA_REPLY, /* Wait for REPLCONF reply */
525 REPL_STATE_SEND_PSYNC, /* Send PSYNC */
526 REPL_STATE_RECEIVE_PSYNC_REPLY, /* Wait for PSYNC reply */
527 /* --- End of handshake states --- */
528 REPL_STATE_TRANSFER, /* Receiving .rdb from master */
529 REPL_STATE_CONNECTED, /* Connected to master */
530} repl_state;
531
532/* Replica rdb channel replication state. Used in server.repl_rdb_ch_state for
533 * replicas to remember what to do next. */
534typedef enum {
535 REPL_RDB_CH_STATE_NONE = 0, /* No active rdb channel sync */
536 REPL_RDB_CH_SEND_HANDSHAKE, /* Send handshake sequence to master */
537 REPL_RDB_CH_RECEIVE_AUTH_REPLY, /* Wait for AUTH reply */
538 REPL_RDB_CH_RECEIVE_REPLCONF_REPLY, /* Wait for REPLCONF reply */
539 REPL_RDB_CH_RECEIVE_FULLRESYNC, /* Wait for +FULLRESYNC reply */
540 REPL_RDB_CH_RDB_LOADING, /* Loading rdb using rdb channel */
541} repl_rdb_channel_state;
542
543#define REPL_MAIN_CH_NONE (1 << 0)
544#define REPL_MAIN_CH_ACCUMULATE_BUF (1 << 1)
545#define REPL_MAIN_CH_STREAMING_BUF (1 << 2)
546#define REPL_MAIN_CH_CLOSE_ASAP (1 << 3)
547
548/* Replication debug flags for testing. */
549#define REPL_DEBUG_PAUSE_NONE (1 << 0)
550#define REPL_DEBUG_AFTER_FORK (1 << 1)
551#define REPL_DEBUG_BEFORE_RDB_CHANNEL (1 << 2)
552#define REPL_DEBUG_ON_STREAMING_REPL_BUF (1 << 3)
553
554/* The state of an in progress coordinated failover */
555typedef enum {
556 NO_FAILOVER = 0, /* No failover in progress */
557 FAILOVER_WAIT_FOR_SYNC, /* Waiting for target replica to catch up */
558 FAILOVER_IN_PROGRESS /* Waiting for target replica to accept
559 * PSYNC FAILOVER request. */
560} failover_state;
561
562/* State of slaves from the POV of the master. Used in client->replstate.
563 * In SEND_BULK and ONLINE state the slave receives new updates
564 * in its output queue. In the WAIT_BGSAVE states instead the server is waiting
565 * to start the next background saving in order to send updates to it. */
566#define SLAVE_STATE_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */
567#define SLAVE_STATE_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */
568#define SLAVE_STATE_SEND_BULK 8 /* Sending RDB file to slave. */
569#define SLAVE_STATE_ONLINE 9 /* RDB file transmitted, sending just updates. */
570#define SLAVE_STATE_RDB_TRANSMITTED 10 /* RDB file transmitted - This state is used only for
571 * a replica that only wants RDB without replication buffer */
572#define SLAVE_STATE_WAIT_RDB_CHANNEL 11 /* Main channel of replica is connected,
573 * we are waiting rdbchannel connection to start delivery.*/
574#define SLAVE_STATE_SEND_BULK_AND_STREAM 12 /* Main channel of a replica which uses rdb channel replication.
575 * Sending RDB file and replication stream in parallel. */
576
577/* Slave capabilities. */
578#define SLAVE_CAPA_NONE 0
579#define SLAVE_CAPA_EOF (1<<0) /* Can parse the RDB EOF streaming format. */
580#define SLAVE_CAPA_PSYNC2 (1<<1) /* Supports PSYNC2 protocol. */
581#define SLAVE_CAPA_RDB_CHANNEL_REPL (1<<2) /* Supports rdb channel replication during full sync */
582
583/* Slave requirements */
584#define SLAVE_REQ_NONE 0
585#define SLAVE_REQ_RDB_EXCLUDE_DATA (1 << 0) /* Exclude data from RDB */
586#define SLAVE_REQ_RDB_EXCLUDE_FUNCTIONS (1 << 1) /* Exclude functions from RDB */
587#define SLAVE_REQ_SLOTS_SNAPSHOT (1 << 2) /* Only slots snapshot is required */
588#define SLAVE_REQ_RDB_CHANNEL (1 << 3) /* Use rdb channel replication, transfer RDB background */
589#define SLAVE_REQ_RDB_NO_COMPRESS (1 << 4) /* Don't enable RDB compression */
590/* Mask of all bits in the slave requirements bitfield that represent non-standard (filtered) RDB requirements */
591#define SLAVE_REQ_RDB_MASK (SLAVE_REQ_RDB_EXCLUDE_DATA | SLAVE_REQ_RDB_EXCLUDE_FUNCTIONS | SLAVE_REQ_SLOTS_SNAPSHOT)
592
593/* Synchronous read timeout - slave side */
594#define CONFIG_REPL_SYNCIO_TIMEOUT 5
595
596/* The default number of replication backlog blocks to trim per call. */
597#define REPL_BACKLOG_TRIM_BLOCKS_PER_CALL 64
598
599/* In order to quickly find the requested offset for PSYNC requests,
600 * we index some nodes in the replication buffer linked list into a rax. */
601#define REPL_BACKLOG_INDEX_PER_BLOCKS 64
602
603/* List related stuff */
604#define LIST_HEAD 0
605#define LIST_TAIL 1
606#define ZSET_MIN 0
607#define ZSET_MAX 1
608
609/* Sort operations */
610#define SORT_OP_GET 0
611
612/* Log levels */
613#define LL_DEBUG 0
614#define LL_VERBOSE 1
615#define LL_NOTICE 2
616#define LL_WARNING 3
617#define LL_NOTHING 4
618#define LL_RAW (1<<10) /* Modifier to log without timestamp */
619
620/* Supervision options */
621#define SUPERVISED_NONE 0
622#define SUPERVISED_AUTODETECT 1
623#define SUPERVISED_SYSTEMD 2
624#define SUPERVISED_UPSTART 3
625
626/* Anti-warning macro... */
627#define UNUSED(V) ((void) V)
628
629#define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^64 elements */
630#define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
631#define ZSKIPLIST_MAX_SEARCH 10
632
633/* Append only defines */
634#define AOF_FSYNC_NO 0
635#define AOF_FSYNC_ALWAYS 1
636#define AOF_FSYNC_EVERYSEC 2
637
638/* Replication diskless load defines */
639#define REPL_DISKLESS_LOAD_DISABLED 0
640#define REPL_DISKLESS_LOAD_WHEN_DB_EMPTY 1
641#define REPL_DISKLESS_LOAD_SWAPDB 2
642#define REPL_DISKLESS_LOAD_ALWAYS 3
643
644/* TLS Client Authentication */
645#define TLS_CLIENT_AUTH_NO 0
646#define TLS_CLIENT_AUTH_YES 1
647#define TLS_CLIENT_AUTH_OPTIONAL 2
648
649/* TLS Client Certfiicate Authentication */
650#define TLS_CLIENT_FIELD_OFF 0
651#define TLS_CLIENT_FIELD_CN 1
652
653/* Sanitize dump payload */
654#define SANITIZE_DUMP_NO 0
655#define SANITIZE_DUMP_YES 1
656#define SANITIZE_DUMP_CLIENTS 2
657
658/* Enable protected config/command */
659#define PROTECTED_ACTION_ALLOWED_NO 0
660#define PROTECTED_ACTION_ALLOWED_YES 1
661#define PROTECTED_ACTION_ALLOWED_LOCAL 2
662
663/* Sets operations codes */
664#define SET_OP_UNION 0
665#define SET_OP_DIFF 1
666#define SET_OP_INTER 2
667
668/* oom-score-adj defines */
669#define OOM_SCORE_ADJ_NO 0
670#define OOM_SCORE_RELATIVE 1
671#define OOM_SCORE_ADJ_ABSOLUTE 2
672
673/* Redis maxmemory strategies. Instead of using just incremental number
674 * for this defines, we use a set of flags so that testing for certain
675 * properties common to multiple policies is faster. */
676#define MAXMEMORY_FLAG_LRU (1<<0)
677#define MAXMEMORY_FLAG_LFU (1<<1)
678#define MAXMEMORY_FLAG_ALLKEYS (1<<2)
679#define MAXMEMORY_FLAG_LRM (1<<3)
680#define MAXMEMORY_FLAG_NO_SHARED_INTEGERS \
681 (MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_LFU|MAXMEMORY_FLAG_LRM)
682
683#define MAXMEMORY_VOLATILE_LRU ((0<<8)|MAXMEMORY_FLAG_LRU)
684#define MAXMEMORY_VOLATILE_LFU ((1<<8)|MAXMEMORY_FLAG_LFU)
685#define MAXMEMORY_VOLATILE_TTL (2<<8)
686#define MAXMEMORY_VOLATILE_RANDOM (3<<8)
687#define MAXMEMORY_ALLKEYS_LRU ((4<<8)|MAXMEMORY_FLAG_LRU|MAXMEMORY_FLAG_ALLKEYS)
688#define MAXMEMORY_ALLKEYS_LFU ((5<<8)|MAXMEMORY_FLAG_LFU|MAXMEMORY_FLAG_ALLKEYS)
689#define MAXMEMORY_ALLKEYS_RANDOM ((6<<8)|MAXMEMORY_FLAG_ALLKEYS)
690#define MAXMEMORY_NO_EVICTION (7<<8)
691#define MAXMEMORY_VOLATILE_LRM ((8<<8)|MAXMEMORY_FLAG_LRM)
692#define MAXMEMORY_ALLKEYS_LRM ((9<<8)|MAXMEMORY_FLAG_LRM|MAXMEMORY_FLAG_ALLKEYS)
693
694/* Units */
695#define UNIT_SECONDS 0
696#define UNIT_MILLISECONDS 1
697
698/* SHUTDOWN flags */
699#define SHUTDOWN_NOFLAGS 0 /* No flags. */
700#define SHUTDOWN_SAVE 1 /* Force SAVE on SHUTDOWN even if no save
701 points are configured. */
702#define SHUTDOWN_NOSAVE 2 /* Don't SAVE on SHUTDOWN. */
703#define SHUTDOWN_NOW 4 /* Don't wait for replicas to catch up. */
704#define SHUTDOWN_FORCE 8 /* Don't let errors prevent shutdown. */
705
706/* Cluster slot stats flags */
707#define CLUSTER_SLOT_STATS_CPU 1 /* Track CPU usage per slot. */
708#define CLUSTER_SLOT_STATS_NET 2 /* Track network bytes per slot. */
709#define CLUSTER_SLOT_STATS_MEM 4 /* Track memory usage per slot. */
710#define CLUSTER_SLOT_STATS_ALL (CLUSTER_SLOT_STATS_CPU | CLUSTER_SLOT_STATS_NET | CLUSTER_SLOT_STATS_MEM)
711
712/* IO thread pause status */
713#define IO_THREAD_UNPAUSED 0
714#define IO_THREAD_PAUSING 1
715#define IO_THREAD_PAUSED 2
716#define IO_THREAD_RESUMING 3
717
718/* Command call flags, see call() function */
719#define CMD_CALL_NONE 0
720#define CMD_CALL_PROPAGATE_AOF (1<<0)
721#define CMD_CALL_PROPAGATE_REPL (1<<1)
722#define CMD_CALL_FROM_MODULE (1<<2) /* From RM_Call */
723#define CMD_CALL_PROPAGATE (CMD_CALL_PROPAGATE_AOF|CMD_CALL_PROPAGATE_REPL)
724#define CMD_CALL_FULL (CMD_CALL_PROPAGATE)
725
726/* Command propagation flags, see propagateNow() function */
727#define PROPAGATE_NONE 0
728#define PROPAGATE_AOF 1
729#define PROPAGATE_REPL 2
730
731/* Actions pause types */
732#define PAUSE_ACTION_CLIENT_WRITE (1<<0)
733#define PAUSE_ACTION_CLIENT_ALL (1<<1) /* must be bigger than PAUSE_ACTION_CLIENT_WRITE */
734#define PAUSE_ACTION_EXPIRE (1<<2)
735#define PAUSE_ACTION_EVICT (1<<3)
736#define PAUSE_ACTION_REPLICA (1<<4) /* pause replica traffic */
737
738/* common sets of actions to pause/unpause */
739#define PAUSE_ACTIONS_CLIENT_WRITE_SET (PAUSE_ACTION_CLIENT_WRITE|\
740 PAUSE_ACTION_EXPIRE|\
741 PAUSE_ACTION_EVICT|\
742 PAUSE_ACTION_REPLICA)
743#define PAUSE_ACTIONS_CLIENT_ALL_SET (PAUSE_ACTION_CLIENT_ALL|\
744 PAUSE_ACTION_EXPIRE|\
745 PAUSE_ACTION_EVICT|\
746 PAUSE_ACTION_REPLICA)
747
748/* Client pause purposes. Each purpose has its own end time and pause type. */
749typedef enum {
750 PAUSE_BY_CLIENT_COMMAND = 0,
751 PAUSE_DURING_SHUTDOWN,
752 PAUSE_DURING_FAILOVER,
753 PAUSE_DURING_SLOT_HANDOFF,
754 NUM_PAUSE_PURPOSES /* This value is the number of purposes above. */
755} pause_purpose;
756
757typedef struct {
758 uint32_t paused_actions; /* Bitmask of actions */
759 mstime_t end;
760} pause_event;
761
762/* Ways that a clusters endpoint can be described */
763typedef enum {
764 CLUSTER_ENDPOINT_TYPE_IP = 0, /* Show IP address */
765 CLUSTER_ENDPOINT_TYPE_HOSTNAME, /* Show hostname */
766 CLUSTER_ENDPOINT_TYPE_UNKNOWN_ENDPOINT /* Show NULL or empty */
767} cluster_endpoint_type;
768
769/* RDB active child save type. */
770#define RDB_CHILD_TYPE_NONE 0
771#define RDB_CHILD_TYPE_DISK 1 /* RDB is written to disk. */
772#define RDB_CHILD_TYPE_SOCKET 2 /* RDB is written to slave socket. */
773
774/* Keyspace changes notification classes. Every class is associated with a
775 * character for configuration purposes. */
776#define NOTIFY_KEYSPACE (1<<0) /* K */
777#define NOTIFY_KEYEVENT (1<<1) /* E */
778#define NOTIFY_GENERIC (1<<2) /* g */
779#define NOTIFY_STRING (1<<3) /* $ */
780#define NOTIFY_LIST (1<<4) /* l */
781#define NOTIFY_SET (1<<5) /* s */
782#define NOTIFY_HASH (1<<6) /* h */
783#define NOTIFY_ZSET (1<<7) /* z */
784#define NOTIFY_EXPIRED (1<<8) /* x */
785#define NOTIFY_EVICTED (1<<9) /* e */
786#define NOTIFY_STREAM (1<<10) /* t */
787#define NOTIFY_KEY_MISS (1<<11) /* m (Note: This one is excluded from NOTIFY_ALL on purpose) */
788#define NOTIFY_LOADED (1<<12) /* module only key space notification, indicate a key loaded from rdb */
789#define NOTIFY_MODULE (1<<13) /* d, module key space notification */
790#define NOTIFY_NEW (1<<14) /* n, new key notification (Note: excluded from NOTIFY_ALL) */
791#define NOTIFY_OVERWRITTEN (1<<15) /* o, key overwrite notification (Note: excluded from NOTIFY_ALL) */
792#define NOTIFY_TYPE_CHANGED (1<<16) /* c, key type changed notification (Note: excluded from NOTIFY_ALL) */
793#define NOTIFY_KEY_TRIMMED (1<<17) /* module only key space notification, indicates a key trimmed during slot migration */
794#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 */
795
796/* Using the following macro you can run code inside serverCron() with the
797 * specified period, specified in milliseconds.
798 * The actual resolution depends on server.hz. */
799#define run_with_period(_ms_) if (((_ms_) <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz))))
800
801/* We can print the stacktrace, so our assert is defined this way: */
802#define serverAssertWithInfo(_c,_o,_e) (likely(_e)?(void)0 : (_serverAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),redis_unreachable()))
803#define serverAssert(_e) (likely(_e)?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),redis_unreachable()))
804#define serverPanic(...) _serverPanic(__FILE__,__LINE__,__VA_ARGS__),redis_unreachable()
805
806/* The following macros provide assertions that are only executed during test builds and should be used to add
807 * assertions that are too computationally expensive or dangerous to run during normal operations. */
808#ifdef DEBUG_ASSERTIONS
809#define debugServerAssertWithInfo(...) serverAssertWithInfo(__VA_ARGS__)
810#define debugServerAssert(...) serverAssert(__VA_ARGS__)
811#else
812#define debugServerAssertWithInfo(...)
813#define debugServerAssert(...)
814#endif
815
816/* latency histogram per command init settings */
817#define LATENCY_HISTOGRAM_MIN_VALUE 1L /* >= 1 nanosec */
818#define LATENCY_HISTOGRAM_MAX_VALUE 1000000000L /* <= 1 secs */
819#define LATENCY_HISTOGRAM_PRECISION 2 /* Maintain a value precision of 2 significant digits across LATENCY_HISTOGRAM_MIN_VALUE and LATENCY_HISTOGRAM_MAX_VALUE range.
820 * Value quantization within the range will thus be no larger than 1/100th (or 1%) of any value.
821 * The total size per histogram should sit around 40 KiB Bytes. */
822
823/* Busy module flags, see busy_module_yield_flags */
824#define BUSY_MODULE_YIELD_NONE (0)
825#define BUSY_MODULE_YIELD_EVENTS (1<<0)
826#define BUSY_MODULE_YIELD_CLIENTS (1<<1)
827
828/* Key prefetch configs */
829#define PREFETCH_BATCH_MAX_SIZE 128
830
831/*-----------------------------------------------------------------------------
832 * Data types
833 *----------------------------------------------------------------------------*/
834
835/* A redis object, that is a type able to hold a string / list / set */
836
837/* The actual Redis Object */
838#define OBJ_STRING 0 /* String object. */
839#define OBJ_LIST 1 /* List object. */
840#define OBJ_SET 2 /* Set object. */
841#define OBJ_ZSET 3 /* Sorted set object. */
842#define OBJ_HASH 4 /* Hash object. */
843#define OBJ_TYPE_BASIC_MAX 5 /* Max number of basic object types. */
844
845/* The "module" object type is a special one that signals that the object
846 * is one directly managed by a Redis module. In this case the value points
847 * to a moduleValue struct, which contains the object value (which is only
848 * handled by the module itself) and the RedisModuleType struct which lists
849 * function pointers in order to serialize, deserialize, AOF-rewrite and
850 * free the object.
851 *
852 * Inside the RDB file, module types are encoded as OBJ_MODULE followed
853 * by a 64 bit module type ID, which has a 54 bits module-specific signature
854 * in order to dispatch the loading to the right module, plus a 10 bits
855 * encoding version. */
856#define OBJ_MODULE 5 /* Module object. */
857#define OBJ_STREAM 6 /* Stream object. */
858#define OBJ_TYPE_MAX 7 /* Maximum number of object types */
859
860/* Extract encver / signature from a module type ID. */
861#define REDISMODULE_TYPE_ENCVER_BITS 10
862#define REDISMODULE_TYPE_ENCVER_MASK ((1<<REDISMODULE_TYPE_ENCVER_BITS)-1)
863#define REDISMODULE_TYPE_ENCVER(id) ((id) & REDISMODULE_TYPE_ENCVER_MASK)
864#define REDISMODULE_TYPE_SIGN(id) (((id) & ~((uint64_t)REDISMODULE_TYPE_ENCVER_MASK)) >>REDISMODULE_TYPE_ENCVER_BITS)
865
866/* Bit flags for moduleTypeAuxSaveFunc */
867#define REDISMODULE_AUX_BEFORE_RDB (1<<0)
868#define REDISMODULE_AUX_AFTER_RDB (1<<1)
869
870struct RedisModule;
871struct RedisModuleIO;
872struct RedisModuleDigest;
873struct RedisModuleCtx;
874struct moduleLoadQueueEntry;
875struct RedisModuleCommand;
876struct clusterState;
877struct slotRangeArray;
878
879/* Each module type implementation should export a set of methods in order
880 * to serialize and deserialize the value in the RDB file, rewrite the AOF
881 * log, create the digest for "DEBUG DIGEST", and free the value when a key
882 * is deleted. */
883typedef void *(*moduleTypeLoadFunc)(struct RedisModuleIO *io, int encver);
884typedef void (*moduleTypeSaveFunc)(struct RedisModuleIO *io, void *value);
885typedef int (*moduleTypeAuxLoadFunc)(struct RedisModuleIO *rdb, int encver, int when);
886typedef void (*moduleTypeAuxSaveFunc)(struct RedisModuleIO *rdb, int when);
887typedef void (*moduleTypeRewriteFunc)(struct RedisModuleIO *io, struct redisObject *key, void *value);
888typedef void (*moduleTypeDigestFunc)(struct RedisModuleDigest *digest, void *value);
889typedef size_t (*moduleTypeMemUsageFunc)(const void *value);
890typedef void (*moduleTypeFreeFunc)(void *value);
891typedef size_t (*moduleTypeFreeEffortFunc)(struct redisObject *key, const void *value);
892typedef void (*moduleTypeUnlinkFunc)(struct redisObject *key, void *value);
893typedef void *(*moduleTypeCopyFunc)(struct redisObject *fromkey, struct redisObject *tokey, const void *value);
894typedef int (*moduleTypeDefragFunc)(struct RedisModuleDefragCtx *ctx, struct redisObject *key, void **value);
895typedef size_t (*moduleTypeMemUsageFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value, size_t sample_size);
896typedef void (*moduleTypeFreeFunc2)(struct RedisModuleKeyOptCtx *ctx, void *value);
897typedef size_t (*moduleTypeFreeEffortFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value);
898typedef void (*moduleTypeUnlinkFunc2)(struct RedisModuleKeyOptCtx *ctx, void *value);
899typedef void *(*moduleTypeCopyFunc2)(struct RedisModuleKeyOptCtx *ctx, const void *value);
900typedef int (*moduleTypeAuthCallback)(struct RedisModuleCtx *ctx, void *username, void *password, const char **err);
901
902/* Module Entity ID: module type or keymeta. */
903typedef struct ModuleEntityId {
904 struct RedisModule *module;
905 char name[10]; /* 9 bytes name + null term. Charset: A-Z a-z 0-9 _- */
906 uint64_t id; /* Higher 54 bits of type ID + 10 lower bits of encoding ver. */
907} ModuleEntityId;
908
909/* The module type, which is referenced in each value of a given type, defines
910 * the methods and links to the module exporting the type. */
911typedef struct RedisModuleType {
912 ModuleEntityId entity; /* module data type name and ID. */
913 moduleTypeLoadFunc rdb_load;
914 moduleTypeSaveFunc rdb_save;
915 moduleTypeRewriteFunc aof_rewrite;
916 moduleTypeMemUsageFunc mem_usage;
917 moduleTypeDigestFunc digest;
918 moduleTypeFreeFunc free;
919 moduleTypeFreeEffortFunc free_effort;
920 moduleTypeUnlinkFunc unlink;
921 moduleTypeCopyFunc copy;
922 moduleTypeDefragFunc defrag;
923 moduleTypeAuxLoadFunc aux_load;
924 moduleTypeAuxSaveFunc aux_save;
925 moduleTypeMemUsageFunc2 mem_usage2;
926 moduleTypeFreeEffortFunc2 free_effort2;
927 moduleTypeUnlinkFunc2 unlink2;
928 moduleTypeCopyFunc2 copy2;
929 moduleTypeAuxSaveFunc aux_save2;
930 int aux_save_triggers;
931} moduleType;
932
933/* In Redis objects 'robj' structures of type OBJ_MODULE, the value pointer
934 * is set to the following structure, referencing the moduleType structure
935 * in order to work with the value, and at the same time providing a raw
936 * pointer to the value, as created by the module commands operating with
937 * the module type.
938 *
939 * So for example in order to free such a value, it is possible to use
940 * the following code:
941 *
942 * if (robj->type == OBJ_MODULE) {
943 * moduleValue *mt = robj->ptr;
944 * mt->type->free(mt->value);
945 * zfree(mt); // We need to release this in-the-middle struct as well.
946 * }
947 */
948typedef struct moduleValue {
949 moduleType *type;
950 void *value;
951} moduleValue;
952
953/* Describe the state of the module during loading, and the indication which configs were loaded / applied already. */
954typedef enum {
955 MODULE_CONFIGS_DEFAULTS = 0x1, /* The registered defaults were applied. */
956 MODULE_CONFIGS_USER_VALS = 0x2, /* The user provided values were applied. */
957 MODULE_CONFIGS_ALL_APPLIED = 0x3 /* Both of the above applied. */
958} ModuleConfigsApplied;
959
960/* This structure represents a module inside the system. */
961struct RedisModule {
962 void *handle; /* Module dlopen() handle. */
963 char *name; /* Module name. */
964 int ver; /* Module version. We use just progressive integers. */
965 int apiver; /* Module API version as requested during initialization.*/
966 list *types; /* Module data types. */
967 list *usedby; /* List of modules using APIs from this one. */
968 list *using; /* List of modules we use some APIs of. */
969 list *filters; /* List of filters the module has registered. */
970 list *module_configs; /* List of configurations the module has registered */
971 ModuleConfigsApplied configs_initialized; /* Have the module configurations been initialized? */
972 int in_call; /* RM_Call() nesting level */
973 int in_hook; /* Hooks callback nesting level for this module (0 or 1). */
974 int options; /* Module options and capabilities. */
975 int blocked_clients; /* Count of RedisModuleBlockedClient in this module. */
976 RedisModuleInfoFunc info_cb; /* Callback for module to add INFO fields. */
977 RedisModuleDefragFunc defrag_cb; /* Callback for global data defrag. */
978 RedisModuleDefragFunc2 defrag_cb_2; /* Version 2 callback for global data defrag. */
979 RedisModuleDefragFunc defrag_start_cb; /* Callback indicating defrag started. */
980 RedisModuleDefragFunc defrag_end_cb; /* Callback indicating defrag ended. */
981 struct moduleLoadQueueEntry *loadmod; /* Module load arguments for config rewrite. */
982 int num_commands_with_acl_categories; /* Number of commands in this module included in acl categories */
983 int onload; /* Flag to identify if the call is being made from Onload (0 or 1) */
984 size_t num_acl_categories_added; /* Number of acl categories added by this module. */
985};
986typedef struct RedisModule RedisModule;
987
988/* The defrag context, used to manage state during calls to the data type
989 * defrag callback.
990 */
991struct RedisModuleDefragCtx {
992 monotime endtime;
993 unsigned long *cursor;
994 struct redisObject *key; /* Optional name of key processed, NULL when unknown. */
995 int dbid; /* The dbid of the key being processed, -1 when unknown. */
996 long long last_stop_check_hits; /* Number of defrag hits at last check. */
997 long long last_stop_check_misses; /* Number of defrag misses at last check. */
998 int stopping; /* Flag indicating if defrag should stop. */
999};
1000#define INIT_MODULE_DEFRAG_CTX(endtime, cursor, key, dbid) \
1001 ((RedisModuleDefragCtx) { \
1002 (endtime), (cursor), (key), (dbid), \
1003 server.stat_active_defrag_hits, \
1004 server.stat_active_defrag_misses \
1005 })
1006
1007/* This is a wrapper for the 'rio' streams used inside rdb.c in Redis, so that
1008 * the user does not have to take the total count of the written bytes nor
1009 * to care about error conditions. */
1010struct RedisModuleIO {
1011 size_t bytes; /* Bytes read / written so far. */
1012 rio *rio; /* Rio stream. */
1013 ModuleEntityId *entity; /* Module type or keymeta doing the operation. */
1014 int error; /* True if error condition happened. */
1015 struct RedisModuleCtx *ctx; /* Optional context, see RM_GetContextFromIO()*/
1016 struct redisObject *key; /* Optional name of key processed */
1017 int dbid; /* The dbid of the key being processed, -1 when unknown. */
1018 sds pre_flush_buffer; /* A buffer that should be flushed before next write operation
1019 * See rdbSaveSingleModuleAux for more details */
1020};
1021
1022/* Initialize an IO context. Note that the 'ver' field is populated
1023 * inside rdb.c according to the version of the value to load. */
1024static inline void moduleInitIOContext(RedisModuleIO *io, ModuleEntityId *entity,
1025 rio *rioptr, struct redisObject *keyptr, int db)
1026{
1027 io->rio = rioptr;
1028 io->entity = entity;
1029 io->bytes = 0;
1030 io->error = 0;
1031 io->key = keyptr;
1032 io->dbid = db;
1033 io->ctx = NULL;
1034 io->pre_flush_buffer = NULL;
1035}
1036
1037/* This is a structure used to export DEBUG DIGEST capabilities to Redis
1038 * modules. We want to capture both the ordered and unordered elements of
1039 * a data structure, so that a digest can be created in a way that correctly
1040 * reflects the values. See the DEBUG DIGEST command implementation for more
1041 * background. */
1042struct RedisModuleDigest {
1043 unsigned char o[20]; /* Ordered elements. */
1044 unsigned char x[20]; /* Xored elements. */
1045 struct redisObject *key; /* Optional name of key processed */
1046 int dbid; /* The dbid of the key being processed */
1047};
1048
1049/* Just start with a digest composed of all zero bytes. */
1050#define moduleInitDigestContext(mdvar) do { \
1051 memset(mdvar.o,0,sizeof(mdvar.o)); \
1052 memset(mdvar.x,0,sizeof(mdvar.x)); \
1053} while(0)
1054
1055/* Macro to check if the client is in the middle of module based authentication. */
1056#define clientHasModuleAuthInProgress(c) ((c)->module_auth_ctx != NULL)
1057
1058/* The string name for an object's type as listed above
1059 * Native types are checked against the OBJ_STRING, OBJ_LIST, OBJ_* defines,
1060 * and Module types have their registered name returned. */
1061char *getObjectTypeName(robj*);
1062
1063/* Macro used to initialize a Redis object allocated on the stack.
1064 * Note that this macro is taken near the structure definition to make sure
1065 * we'll update it when the structure is changed, to avoid bugs like
1066 * bug #85 introduced exactly in this way. */
1067#define initStaticStringObject(_var,_ptr) do { \
1068 _var.refcount = OBJ_STATIC_REFCOUNT; \
1069 _var.type = OBJ_STRING; \
1070 _var.encoding = OBJ_ENCODING_RAW; \
1071 _var.metabits = 0; \
1072 _var.iskvobj = 0; \
1073 _var.ptr = _ptr; \
1074} while(0)
1075
1076struct evictionPoolEntry; /* Defined in evict.c */
1077
1078/* Encoded buffers contain headers followed by either plain replies or
1079 * by bulk string references */
1080typedef enum {
1081 PLAIN_REPLY = 0, /* plain reply */
1082 BULK_STR_REF /* bulk string references */
1083} payloadType;
1084
1085/* Encoded reply buffers consist of chunks
1086 * Each chunk contains header followed by payload
1087 * The packed attribute is specified because buffer is accessed at arbitrary offsets,
1088 * so no benefit in data structure padding and applying packed saves the space in the buffer */
1089typedef struct __attribute__((__packed__)) payloadHeader {
1090 size_t payload_len; /* payload length in a reply buffer */
1091 uint8_t payload_type; /* one of payloadType */
1092} payloadHeader;
1093static_assert(offsetof(payloadHeader, payload_len) == 0, "payload_len must be at offset 0 to avoid unaligned access");
1094
1095/* To avoid copy of whole string in reply buffer
1096 * we store pointers to object and string itself */
1097typedef struct __attribute__((__packed__)) bulkStrRef {
1098 robj *obj; /* pointer to object used for reference count management */
1099 unsigned int prefix_cnt;
1100 char prefix[LONG_STR_SIZE + 3]; /* $<len>\r\n */
1101 char crlf[2]; /* \r\n */
1102} bulkStrRef;
1103
1104/* This structure is used in order to represent the output buffer of a client,
1105 * which is actually a linked list of blocks like that, that is: client->reply. */
1106typedef struct clientReplyBlock {
1107 size_t size, used;
1108 char buf_encoded;
1109 char buf[];
1110} clientReplyBlock;
1111
1112/* Replication buffer blocks is the list of replBufBlock.
1113 *
1114 * +--------------+ +--------------+ +--------------+
1115 * | refcount = 1 | ... | refcount = 0 | ... | refcount = 2 |
1116 * +--------------+ +--------------+ +--------------+
1117 * | / \
1118 * | / \
1119 * | / \
1120 * Repl Backlog Replica_A Replica_B
1121 *
1122 * Each replica or replication backlog increments only the refcount of the
1123 * 'ref_repl_buf_node' which it points to. So when replica walks to the next
1124 * node, it should first increase the next node's refcount, and when we trim
1125 * the replication buffer nodes, we remove node always from the head node which
1126 * refcount is 0. If the refcount of the head node is not 0, we must stop
1127 * trimming and never iterate the next node.
1128 *
1129 * For replicas in IO threads we don't update the refcount while sending the
1130 * repl data, but only when the client is sent back to main. This avoids data
1131 * races. In order to achieve this, the replicas keep track of following:
1132 * - io_curr_repl_node - the current node we've reached.
1133 * - io_bound_repl_node - the last node in the replication buffer as seen by
1134 * the replica client before it was sent to IO thread
1135 *
1136 * When the client is sent to IO thread for the first time io_curr_repl_node is
1137 * initialized with ref_repl_buf_node.
1138 * When the client is sent back to main it can decrement ref_repl_buf_node's
1139 * refcount and increment it for io_curr_repl_node, since all the nodes
1140 * in-between are already sent and the client doesn't hold reference to them.
1141 *
1142 * `io_bound_repl_node` is needed because IO thread needs to know when to stop
1143 * sending data. If it was reading directly from the replication buffer,
1144 * there will be a data race, because main thread may be writing to it during
1145 * `feedReplicationBuffer`. `io_bound_repl_node` is cached in the client
1146 * together with its used size just before sending the client to IO thread
1147 * in `enqueuePendingClienstToIOThreads`. */
1148
1149/* Similar with 'clientReplyBlock', it is used for shared buffers between
1150 * all replica clients and replication backlog. */
1151typedef struct replBufBlock {
1152 int refcount; /* Number of replicas or repl backlog using. */
1153 long long id; /* The unique incremental number. */
1154 long long repl_offset; /* Start replication offset of the block. */
1155 size_t size; /* Capacity of the buf in bytes */
1156 size_t used; /* Count of written bytes */
1157 char buf[];
1158} replBufBlock;
1159
1160/* Redis database representation. There are multiple databases identified
1161 * by integers from 0 (the default database) up to the max configured
1162 * database. The database number is the 'id' field in the structure. */
1163typedef struct redisDb {
1164 kvstore *keys; /* The keyspace for this DB. As metadata, holds keysizes histogram */
1165 kvstore *expires; /* Timeout of keys with a timeout set */
1166 estore *subexpires; /* Timeout of sub-keys with a timeout set. (Currently only used for hashes) */
1167 dict *blocking_keys; /* Keys with clients waiting for data (BLPOP)*/
1168 dict *blocking_keys_unblock_on_nokey; /* Keys with clients waiting for
1169 * data, and should be unblocked if key is deleted (XREADEDGROUP).
1170 * This is a subset of blocking_keys*/
1171 dict *stream_claim_pending_keys; /* Keys with clients waiting to claim pending entries */
1172 dict *stream_idmp_keys; /* Stream keys with IDMP tracking */
1173 dict *ready_keys; /* Blocked keys that received a PUSH */
1174 dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
1175 int id; /* Database ID */
1176 long long avg_ttl; /* Average TTL, just for stats */
1177 unsigned long expires_cursor; /* Cursor of the active expire cycle. */
1178} redisDb;
1179
1180/* maximum number of bins of keysizes histogram */
1181#define MAX_KEYSIZES_BINS 60
1182#define MAX_KEYSIZES_TYPES 5 /* static_assert at db.c verifies == OBJ_TYPE_BASIC_MAX */
1183typedef int64_t keysizesHist[MAX_KEYSIZES_TYPES][MAX_KEYSIZES_BINS];
1184
1185/* Metadata structure used for kvstores with type `kvstoreExType`, managed outside kvstore */
1186typedef struct {
1187 keysizesHist keysizes_hist;
1188} kvstoreMetadata;
1189
1190/* Like kvstoreMetadata, this one per dict */
1191typedef struct {
1192 kvstoreDictMetaBase base; /* must be first in struct ! */
1193 size_t alloc_size; /* Total memory used (in bytes) by this slot */
1194 uint64_t cpu_usec; /* CPU time (in microseconds) spent on given slot */
1195 uint64_t network_bytes_in; /* Network ingress (in bytes) received for given slot */
1196 uint64_t network_bytes_out; /* Network egress (in bytes) sent for given slot */
1197 keysizesHist keysizes_hist;
1198} kvstoreDictMetadata;
1199
1200/* forward declaration for functions ctx */
1201typedef struct functionsLibCtx functionsLibCtx;
1202
1203/* Holding object that need to be populated during
1204 * rdb loading. On loading end it is possible to decide
1205 * whether not to set those objects on their rightful place.
1206 * For example: dbarray need to be set as main database on
1207 * successful loading and dropped on failure. */
1208typedef struct rdbLoadingCtx {
1209 redisDb* dbarray;
1210 functionsLibCtx* functions_lib_ctx;
1211}rdbLoadingCtx;
1212
1213typedef struct pendingCommand pendingCommand;
1214typedef struct multiState {
1215 pendingCommand **commands; /* Array of pointers to MULTI commands */
1216 int executing_cmd; /* The index of the currently executed transaction
1217 command (index in commands field) */
1218 int count; /* Total number of MULTI commands */
1219 int cmd_flags; /* The accumulated command flags OR-ed together.
1220 So if at least a command has a given flag, it
1221 will be set in this field. */
1222 int cmd_inv_flags; /* Same as cmd_flags, OR-ing the ~flags. so that it
1223 is possible to know if all the commands have a
1224 certain flag. */
1225 size_t argv_len_sums; /* mem used by all commands arguments */
1226 int alloc_count; /* total number of pendingCommand struct memory reserved. */
1227} multiState;
1228
1229/* This structure holds the blocking operation state for a client.
1230 * The fields used depend on client->btype. */
1231typedef struct blockingState {
1232 /* Generic fields. */
1233 blocking_type btype; /* Type of blocking op if CLIENT_BLOCKED. */
1234 mstime_t timeout; /* Blocking operation timeout. If UNIX current time
1235 * is > timeout then the operation timed out. */
1236 int unblock_on_nokey; /* Whether to unblock the client when at least one of the keys
1237 is deleted or does not exist anymore */
1238 /* BLOCKED_LIST, BLOCKED_ZSET and BLOCKED_STREAM or any other Keys related blocking */
1239 dict *keys; /* The keys we are blocked on */
1240
1241 /* BLOCKED_WAIT and BLOCKED_WAITAOF */
1242 int numreplicas; /* Number of replicas we are waiting for ACK. */
1243 int numlocal; /* Indication if WAITAOF is waiting for local fsync. */
1244 long long reploffset; /* Replication offset to reach. */
1245
1246 /* BLOCKED_MODULE */
1247 void *module_blocked_handle; /* RedisModuleBlockedClient structure.
1248 which is opaque for the Redis core, only
1249 handled in module.c. */
1250
1251 void *async_rm_call_handle; /* RedisModuleAsyncRMCallPromise structure.
1252 which is opaque for the Redis core, only
1253 handled in module.c. */
1254
1255 /* BLOCKED_LAZYFREE */
1256 monotime lazyfreeStartTime;
1257} blockingState;
1258
1259/* The following structure represents a node in the server.ready_keys list,
1260 * where we accumulate all the keys that had clients blocked with a blocking
1261 * operation such as B[LR]POP, but received new data in the context of the
1262 * last executed command.
1263 *
1264 * After the execution of every command or script, we iterate over this list to check
1265 * if as a result we should serve data to clients blocked, unblocking them.
1266 * Note that server.ready_keys will not have duplicates as there dictionary
1267 * also called ready_keys in every structure representing a Redis database,
1268 * where we make sure to remember if a given key was already added in the
1269 * server.ready_keys list. */
1270typedef struct readyList {
1271 redisDb *db;
1272 robj *key;
1273} readyList;
1274
1275/* List of pending commands. */
1276typedef struct pendingCommandList {
1277 pendingCommand *head;
1278 pendingCommand *tail;
1279 int len; /* Number of commands in the list */
1280 int ready_len; /* Number of commands that are ready to be processed */
1281} pendingCommandList;
1282
1283/* Pending command pool management structure */
1284#define PENDING_COMMAND_POOL_SIZE 16
1285#define PENDING_COMMAND_POOL_MAX_SIZE 1024
1286typedef struct pendingCommandPool {
1287 pendingCommand **pool; /* Pool array for reusing pendingCommand objects */
1288 int size; /* Current number of objects in pool */
1289 int capacity; /* Current capacity of the pool array */
1290 int min_size; /* Minimum size since last check (indicates peak usage) */
1291} pendingCommandPool;
1292
1293/* This structure represents a Redis user. This is useful for ACLs, the
1294 * user is associated to the connection after the connection is authenticated.
1295 * If there is no associated user, the connection uses the default user. */
1296#define USER_COMMAND_BITS_COUNT 1024 /* The total number of command bits
1297 in the user structure. The last valid
1298 command ID we can set in the user
1299 is USER_COMMAND_BITS_COUNT-1. */
1300#define USER_FLAG_ENABLED (1<<0) /* The user is active. */
1301#define USER_FLAG_DISABLED (1<<1) /* The user is disabled. */
1302#define USER_FLAG_NOPASS (1<<2) /* The user requires no password, any
1303 provided password will work. For the
1304 default user, this also means that
1305 no AUTH is needed, and every
1306 connection is immediately
1307 authenticated. */
1308#define USER_FLAG_SANITIZE_PAYLOAD (1<<3) /* The user require a deep RESTORE
1309 * payload sanitization. */
1310#define USER_FLAG_SANITIZE_PAYLOAD_SKIP (1<<4) /* The user should skip the
1311 * deep sanitization of RESTORE
1312 * payload. */
1313
1314#define SELECTOR_FLAG_ROOT (1<<0) /* This is the root user permission
1315 * selector. */
1316#define SELECTOR_FLAG_ALLKEYS (1<<1) /* The user can mention any key. */
1317#define SELECTOR_FLAG_ALLCOMMANDS (1<<2) /* The user can run all commands. */
1318#define SELECTOR_FLAG_ALLCHANNELS (1<<3) /* The user can mention any Pub/Sub
1319 channel. */
1320
1321typedef struct {
1322 sds name; /* The username as an SDS string. */
1323 redisAtomic uint32_t flags; /* See USER_FLAG_* */
1324 list *passwords; /* A list of SDS valid passwords for this user. */
1325 list *selectors; /* A list of selectors this user validates commands
1326 against. This list will always contain at least
1327 one selector for backwards compatibility. */
1328 robj *acl_string; /* cached string represent of ACLs */
1329} user;
1330
1331/* With multiplexing we need to take per-client state.
1332 * Clients are taken in a linked list. */
1333
1334#define CLIENT_ID_AOF (UINT64_MAX) /* Reserved ID for the AOF client. If you
1335 need more reserved IDs use UINT64_MAX-1,
1336 -2, ... and so forth. */
1337
1338/* Replication backlog is not a separate memory, it just is one consumer of
1339 * the global replication buffer. This structure records the reference of
1340 * replication buffers. Since the replication buffer block list may be very long,
1341 * it would cost much time to search replication offset on partial resync, so
1342 * we use one rax tree to index some blocks every REPL_BACKLOG_INDEX_PER_BLOCKS
1343 * to make searching offset from replication buffer blocks list faster. */
1344typedef struct replBacklog {
1345 listNode *ref_repl_buf_node; /* Referenced node of replication buffer blocks,
1346 * see the definition of replBufBlock. */
1347 size_t unindexed_count; /* The count from last creating index block. */
1348 rax *blocks_index; /* The index of recorded blocks of replication
1349 * buffer for quickly searching replication
1350 * offset on partial resynchronization. */
1351 long long histlen; /* Backlog actual data length */
1352 long long offset; /* Replication "master offset" of first
1353 * byte in the replication backlog buffer.*/
1354} replBacklog;
1355
1356/* Used by replDataBuf during rdb channel replication to accumulate replication
1357 * stream on replica side. */
1358typedef struct replDataBufBlock {
1359 size_t used; /* Used bytes in the buf */
1360 size_t size; /* Size of the buf */
1361 char buf[]; /* Replication data */
1362} replDataBufBlock;
1363
1364/* Linked list of replDataBufBlock structs, holds replication stream during
1365 * rdb channel replication on replica side. */
1366typedef struct replDataBuf {
1367 list *blocks; /* List of replDataBufBlock */
1368 size_t mem_used; /* Total allocated memory */
1369 size_t size; /* Total number of bytes available in all blocks. */
1370 size_t used; /* Total number of bytes actually used in all blocks. */
1371 size_t peak; /* Peak number of bytes stored in all blocks. */
1372 size_t last_num_blocks; /* Used to verify we consume more than we read from
1373 * the master connection while streaming buffer to
1374 * the db. */
1375} replDataBuf;
1376
1377typedef struct {
1378 list *clients;
1379 size_t mem_usage_sum;
1380} clientMemUsageBucket;
1381
1382#define DEFERRED_OBJECT_TYPE_PENDING_COMMAND 1
1383#define DEFERRED_OBJECT_TYPE_ROBJ 2
1384/* Structure to hold objects that need to be freed later by IO threads.
1385 * This allows the main thread to defer memory cleanup operations to
1386 * IO threads to avoid blocking the main event loop. */
1387typedef struct deferredObject {
1388 int type; /* Pointer to the object to be freed */
1389 void *ptr; /* Type of object: DEFERRED_OBJECT_TYPE_* */
1390} deferredObject;
1391
1392#define SHOULD_CLUSTER_COMPATIBILITY_SAMPLE() \
1393 (server.cluster_compatibility_sample_ratio == 100 || \
1394 (double)rand()/RAND_MAX * 100 < server.cluster_compatibility_sample_ratio)
1395
1396#ifdef LOG_REQ_RES
1397/* Structure used to log client's requests and their
1398 * responses (see logreqres.c) */
1399typedef struct {
1400 /* General */
1401 int argv_logged; /* 1 if the command was logged */
1402 /* Vars for log buffer */
1403 unsigned char *buf; /* Buffer holding the data (request and response) */
1404 size_t used;
1405 size_t capacity;
1406 /* Vars for offsets within the client's reply */
1407 struct {
1408 /* General */
1409 int saved; /* 1 if we already saved the offset (first time we call addReply*) */
1410 /* Offset within the static reply buffer */
1411 size_t bufpos;
1412 /* Offset within the reply block list */
1413 struct {
1414 int index;
1415 size_t used;
1416 } last_node;
1417 } offset;
1418} clientReqResInfo;
1419#endif
1420
1421typedef struct client {
1422 uint64_t id; /* Client incremental unique ID. */
1423 uint64_t flags; /* Client flags: CLIENT_* macros. */
1424 connection *conn;
1425 uint8_t tid; /* Thread assigned ID this client is bound to. */
1426 uint8_t running_tid; /* Thread assigned ID this client is running on. */
1427 uint8_t io_flags; /* Accessed by both main and IO threads, but not modified concurrently */
1428 uint8_t read_error; /* Client read error: CLIENT_READ_* macros. */
1429 int resp; /* RESP protocol version. Can be 2 or 3. */
1430 redisDb *db; /* Pointer to currently SELECTed DB. */
1431 robj *name; /* As set by CLIENT SETNAME. */
1432 robj *lib_name; /* The client library name as set by CLIENT SETINFO. */
1433 robj *lib_ver; /* The client library version as set by CLIENT SETINFO. */
1434 sds querybuf; /* Buffer we use to accumulate client queries. */
1435 size_t qb_pos; /* The position we have read in querybuf. */
1436 size_t querybuf_peak; /* Recent (100ms or more) peak of querybuf size. */
1437 int argc; /* Num of arguments of current command. */
1438 robj **argv; /* Arguments of current command. */
1439 int argv_len; /* Size of argv array (may be more than argc) */
1440 int original_argc; /* Num of arguments of original command if arguments were rewritten. */
1441 robj **original_argv; /* Arguments of original command if arguments were rewritten. */
1442 size_t all_argv_len_sum; /* Sum of lengths of objects in all pendingCommand argv lists */
1443 pendingCommandList pending_cmds; /* List of parsed pending commands */
1444 pendingCommand *current_pending_cmd;
1445 deferredObject *deferred_objects; /* Array of deferred objects to free. */
1446 int deferred_objects_num; /* Number of deferred objects to free. */
1447 robj **io_deferred_objects; /* Objects to be freed by main thread, queued by IO thread */
1448 int io_deferred_objects_num; /* Number of objects in io_deferred_objects */
1449 int io_deferred_objects_size; /* Allocated size of io_deferred_objects */
1450 struct redisCommand *cmd, *lastcmd; /* Last command executed. */
1451 struct redisCommand *lookedcmd; /* Command looked up in lookahead. */
1452 struct redisCommand *realcmd; /* The original command that was executed by the client,
1453 Used to update error stats in case the c->cmd was modified
1454 during the command invocation (like on GEOADD for example). */
1455 user *user; /* User associated with this connection. If the
1456 user is set to NULL the connection can do
1457 anything (admin). */
1458 int reqtype; /* Request protocol type: PROTO_REQ_* */
1459 int multibulklen; /* Number of multi bulk arguments left to read. */
1460 long bulklen; /* Length of bulk argument in multi bulk request. */
1461 list *reply; /* List of reply objects to send to the client. */
1462 unsigned long long reply_bytes; /* Tot bytes of objects in reply list. */
1463 list *deferred_reply_errors; /* Used for module thread safe contexts. */
1464 size_t sentlen; /* Amount of bytes already sent in the current
1465 buffer or object being sent. */
1466 time_t ctime; /* Client creation time. */
1467 long duration; /* Current command duration. Used for measuring latency of blocking/non-blocking cmds */
1468 int slot; /* The slot the client is executing against. Set to -1 if no slot is being used */
1469 int cluster_compatibility_check_slot; /* The slot the client is executing against for cluster compatibility check.
1470 * -2 means we don't need to check slot violation, or we already found
1471 * a violation, reported it and don't need to continue checking.
1472 * -1 means we're looking for the slot number and didn't find it yet.
1473 * any positive number means we found a slot and no violation yet. */
1474 dictEntry *cur_script; /* Cached pointer to the dictEntry of the script being executed. */
1475 time_t lastinteraction; /* Time of the last interaction, used for timeout */
1476 time_t io_lastinteraction; /* Time of the last interaction as seen from
1477 * IO thread. When the client is moved to main
1478 * it updates its `lastinteraction` value from
1479 * this. */
1480 time_t obuf_soft_limit_reached_time;
1481 mstime_t io_last_client_cron; /* Timestamp of last invocation of client
1482 * cron if client is running in IO thread */
1483 mstime_t io_last_repl_cron; /* Timestamp of last invocation of replication
1484 * cron if client is running in IO thread. */
1485 int authenticated; /* Needed when the default user requires auth. */
1486 int replstate; /* Replication state if this is a slave. */
1487 int repl_start_cmd_stream_on_ack; /* Install slave write handler on first ACK. */
1488 int repldbfd; /* Replication DB file descriptor. */
1489 off_t repldboff; /* Replication DB file offset. */
1490 off_t repldbsize; /* Replication DB file size. */
1491 sds replpreamble; /* Replication DB preamble. */
1492 long long read_reploff; /* Read replication offset if this is a master. */
1493 long long io_read_reploff; /* Copy of read_reploff but only used when
1494 * master client is in IO thread so we don't
1495 * have contention with IO thread. */
1496 long long reploff; /* Applied replication offset if this is a master. */
1497 long long reploff_next; /* Next value to set for reploff when a command finishes executing */
1498 long long repl_applied; /* Applied replication data count in querybuf, if this is a replica. */
1499 long long repl_ack_off; /* Replication ack offset, if this is a slave. */
1500 long long repl_aof_off; /* Replication AOF fsync ack offset, if this is a slave. */
1501 long long repl_ack_time;/* Replication ack time, if this is a slave. */
1502 long long io_repl_ack_time; /* Replication ack time, if this is a replica in
1503 * IO thread. Keeps track of repl_ack_time while
1504 * replica is in IO thread to avoid data races
1505 * with main. repl_ack_time is updated with this
1506 * value when replica returns to main thread. */
1507 long long repl_last_partial_write; /* The last time the server did a partial write from the RDB child pipe to this replica */
1508 long long psync_initial_offset; /* FULLRESYNC reply offset other slaves
1509 copying this slave output buffer
1510 should use. */
1511 char replid[CONFIG_RUN_ID_SIZE+1]; /* Master replication ID (if master). */
1512 int slave_listening_port; /* As configured with: REPLCONF listening-port */
1513 char *slave_addr; /* Optionally given by REPLCONF ip-address */
1514 int slave_capa; /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */
1515 int slave_req; /* Slave requirements: SLAVE_REQ_* */
1516 uint64_t main_ch_client_id; /* The client id of this replica's main channel */
1517 multiState mstate; /* MULTI/EXEC state */
1518 blockingState bstate; /* blocking state */
1519 long long woff; /* Last write global replication offset. */
1520 list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */
1521 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
1522 dict *pubsub_patterns; /* patterns a client is interested in (PSUBSCRIBE) */
1523 dict *pubsubshard_channels; /* shard level channels a client is interested in (SSUBSCRIBE) */
1524 sds peerid; /* Cached peer ID. */
1525 sds sockname; /* Cached connection target address. */
1526 listNode *client_list_node; /* list node in client list */
1527 listNode *io_thread_client_list_node; /* list node in io thread client list */
1528 listNode *postponed_list_node; /* list node within the postponed list */
1529 void *module_blocked_client; /* Pointer to the RedisModuleBlockedClient associated with this
1530 * client. This is set in case of module authentication before the
1531 * unblocked client is reprocessed to handle reply callbacks. */
1532 void *module_auth_ctx; /* Ongoing / attempted module based auth callback's ctx.
1533 * This is only tracked within the context of the command attempting
1534 * authentication. If not NULL, it means module auth is in progress. */
1535 RedisModuleUserChangedFunc auth_callback; /* Module callback to execute
1536 * when the authenticated user
1537 * changes. */
1538 void *auth_callback_privdata; /* Private data that is passed when the auth
1539 * changed callback is executed. Opaque for
1540 * Redis Core. */
1541 void *auth_module; /* The module that owns the callback, which is used
1542 * to disconnect the client if the module is
1543 * unloaded for cleanup. Opaque for Redis Core.*/
1544
1545 /* If this client is in tracking mode and this field is non zero,
1546 * invalidation messages for keys fetched by this client will be sent to
1547 * the specified client ID. */
1548 uint64_t client_tracking_redirection;
1549 rax *client_tracking_prefixes; /* A dictionary of prefixes we are already
1550 subscribed to in BCAST mode, in the
1551 context of client side caching. */
1552 /* In updateClientMemoryUsage() we track the memory usage of
1553 * each client and add it to the sum of all the clients of a given type,
1554 * however we need to remember what was the old contribution of each
1555 * client, and in which category the client was, in order to remove it
1556 * before adding it the new value. */
1557 size_t last_memory_usage;
1558 int last_memory_type;
1559
1560 listNode *mem_usage_bucket_node;
1561 clientMemUsageBucket *mem_usage_bucket;
1562
1563 listNode *ref_repl_buf_node; /* Referenced node of replication buffer blocks,
1564 * see the definition of replBufBlock. */
1565 size_t ref_block_pos; /* Access position of referenced buffer block,
1566 * i.e. the next offset to send. */
1567 listNode *io_curr_repl_node; /* Current node we are sending repl data from in
1568 * IO thread. */
1569 size_t io_curr_block_pos; /* Current position we are sending repl data from
1570 * in IO thread. */
1571 listNode *io_bound_repl_node;/* Bound node we are sending repl data from in
1572 * IO thread. */
1573 size_t io_bound_block_pos; /* Bound position we are sending repl data from
1574 * in IO thread. */
1575
1576 /* list node in clients_pending_write list */
1577 listNode clients_pending_write_node;
1578 /* list node in clients_with_pending_ref_reply list */
1579 listNode *pending_ref_reply_node;
1580 /* Statistics and metrics */
1581 size_t net_input_bytes_curr_cmd; /* Total network input bytes read for the
1582 * execution of this client's current command. */
1583 size_t net_output_bytes_curr_cmd; /* Total network output bytes sent to this
1584 * client, by the current command. */
1585 /* Response buffer */
1586 size_t buf_peak; /* Peak used size of buffer in last 5 sec interval. */
1587 mstime_t buf_peak_last_reset_time; /* keeps the last time the buffer peak value was reset */
1588 size_t bufpos;
1589 size_t buf_usable_size; /* Usable size of buffer. */
1590 char *buf;
1591 uint8_t buf_encoded; /* True if c->buf content is encoded (e.g. for copy avoidance) */
1592 payloadHeader *last_header; /* Pointer to the last header in a buffer when using copy avoidance */
1593#ifdef LOG_REQ_RES
1594 clientReqResInfo reqres;
1595#endif
1596 unsigned long long net_input_bytes; /* Total network input bytes read from this client. */
1597 unsigned long long net_output_bytes; /* Total network output bytes sent to this client. */
1598 unsigned long long commands_processed; /* Total count of commands this client executed. */
1599 struct asmTask *task; /* Atomic slot migration task */
1600 char *node_id; /* Node ID to connect to for atomic slot migration */
1601
1602 redisAtomic int pending_read; /* Flag indicating an IO thread client residing
1603 * in main thread has received a read event. */
1604} client;
1605
1606typedef struct __attribute__((aligned(CACHE_LINE_SIZE))) {
1607 uint8_t id; /* The unique ID assigned, if IO_THREADS_MAX_NUM is more
1608 * than 256, we should also promote the data type. */
1609 pthread_t tid; /* Pthread ID */
1610 redisAtomic int paused; /* Paused status for the io thread. */
1611 redisAtomic int running; /* Running if true, main thread can send clients directly. */
1612 aeEventLoop *el; /* Main event loop of io thread. */
1613 list *pending_clients; /* List of clients with pending writes. */
1614 list *processing_clients; /* List of clients being processed. */
1615 eventNotifier *pending_clients_notifier; /* Used to wake up the loop when write should be performed. */
1616 pthread_mutex_t pending_clients_mutex; /* Mutex for pending write list */
1617 list *pending_clients_to_main_thread; /* Clients that are waiting to be executed by the main thread. */
1618 list *clients; /* IO thread managed clients. */
1619} IOThread;
1620
1621/* Context for streaming replDataBuf to database */
1622typedef struct replDataBufToDbCtx {
1623 void *privdata; /* Private data of context */
1624 client *client; /* Client to process commands */
1625 size_t applied_offset; /* Offset applied to the database */
1626 int (*should_continue)(void *ctx); /* Check if we should continue */
1627 void (*yield_callback)(void *ctx); /* Yield to the event loop */
1628} replDataBufToDbCtx;
1629
1630/* ACL information */
1631typedef struct aclInfo {
1632 long long user_auth_failures; /* Auth failure counts on user level */
1633 long long invalid_cmd_accesses; /* Invalid command accesses that user doesn't have permission to */
1634 long long invalid_key_accesses; /* Invalid key accesses that user doesn't have permission to */
1635 long long invalid_channel_accesses; /* Invalid channel accesses that user doesn't have permission to */
1636 long long acl_access_denied_tls_cert; /* TLS clients with cert not matching any existing user. */
1637} aclInfo;
1638
1639struct saveparam {
1640 time_t seconds;
1641 int changes;
1642};
1643
1644struct moduleLoadQueueEntry {
1645 sds path;
1646 int argc;
1647 robj **argv;
1648};
1649
1650struct sentinelLoadQueueEntry {
1651 int argc;
1652 sds *argv;
1653 int linenum;
1654 sds line;
1655};
1656
1657struct sentinelConfig {
1658 list *pre_monitor_cfg;
1659 list *monitor_cfg;
1660 list *post_monitor_cfg;
1661};
1662
1663struct sharedObjectsStruct {
1664 robj *ok, *err, *emptybulk, *czero, *cone, *pong, *space,
1665 *queued, *null[4], *nullarray[4], *emptymap[4], *emptyset[4],
1666 *emptyarray, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
1667 *outofrangeerr, *noscripterr, *loadingerr,
1668 *slowevalerr, *slowscripterr, *slowmoduleerr, *bgsaveerr,
1669 *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr,
1670 *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
1671 *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *unlink,
1672 *rpop, *lpop, *lpush, *rpoplpush, *lmove, *blmove, *zpopmin, *zpopmax,
1673 *emptyscan, *multi, *exec, *left, *right, *hset, *srem, *xgroup, *xclaim,
1674 *script, *replconf, *eval, *persist, *set, *pexpireat, *pexpire,
1675 *hdel, *hpexpireat, *hpersist, *hsetex,
1676 *time, *pxat, *absttl, *retrycount, *force, *justid, *entriesread,
1677 *lastid, *ping, *setid, *keepttl, *load, *createconsumer, *fields,
1678 *getack, *special_asterick, *special_equals, *default_username, *redacted,
1679 *ssubscribebulk,*sunsubscribebulk, *smessagebulk,
1680 *select[PROTO_SHARED_SELECT_CMDS],
1681 *integers[OBJ_SHARED_INTEGERS],
1682 *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
1683 *bulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "$<value>\r\n" */
1684 *maphdr[OBJ_SHARED_BULKHDR_LEN], /* "%<value>\r\n" */
1685 *sethdr[OBJ_SHARED_BULKHDR_LEN]; /* "~<value>\r\n" */
1686 sds minstring, maxstring;
1687};
1688
1689/* ZSETs use a specialized version of Skiplists */
1690
1691/* Node info placed in level[0].span since it's unused at level 0 (static assert verified) */
1692typedef struct zskiplistNodeInfo {
1693 uint16_t sdsoffset; /* Offset from node start to sds data (after sds header) */
1694 uint8_t levels; /* Number of levels in this node (1-32) */
1695 uint8_t reserved;
1696} zskiplistNodeInfo;
1697
1698typedef struct zskiplistNode {
1699 double score;
1700 struct zskiplistNode *backward;
1701 struct zskiplistLevel {
1702 struct zskiplistNode *forward;
1703 /* Span is the number of elements between this node and the next node at this level.
1704 * At level 0, span is repurposed to store zskiplistNodeInfo for regular nodes, */
1705 unsigned long span;
1706 } level[];
1707 /* sds ele is embedded after level[] array (assist zslGetNodeElement(node) to access it) */
1708} zskiplistNode;
1709
1710typedef struct zskiplist {
1711 struct zskiplistNode *header, *tail;
1712 unsigned long length;
1713 int level;
1714 size_t alloc_size;
1715} zskiplist;
1716
1717typedef struct zset {
1718 dict *dict;
1719 zskiplist *zsl;
1720} zset;
1721
1722typedef struct clientBufferLimitsConfig {
1723 unsigned long long hard_limit_bytes;
1724 unsigned long long soft_limit_bytes;
1725 time_t soft_limit_seconds;
1726} clientBufferLimitsConfig;
1727
1728extern clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT];
1729
1730/* The redisOp structure defines a Redis Operation, that is an instance of
1731 * a command with an argument vector, database ID, propagation target
1732 * (PROPAGATE_*), and command pointer.
1733 *
1734 * Currently only used to additionally propagate more commands to AOF/Replication
1735 * after the propagation of the executed command. */
1736typedef struct redisOp {
1737 robj **argv;
1738 int argc, dbid, target;
1739} redisOp;
1740
1741/* Defines an array of Redis operations. There is an API to add to this
1742 * structure in an easy way.
1743 *
1744 * int redisOpArrayAppend(redisOpArray *oa, int dbid, robj **argv, int argc, int target);
1745 * void redisOpArrayFree(redisOpArray *oa);
1746 */
1747typedef struct redisOpArray {
1748 redisOp *ops;
1749 int numops;
1750 int capacity;
1751} redisOpArray;
1752
1753/* This structure is returned by the getMemoryOverheadData() function in
1754 * order to return memory overhead information. */
1755struct redisMemOverhead {
1756 size_t peak_allocated;
1757 size_t total_allocated;
1758 size_t startup_allocated;
1759 size_t repl_backlog;
1760 size_t replica_fullsync_buffer;
1761 size_t clients_slaves;
1762 size_t clients_normal;
1763 size_t cluster_links;
1764 size_t aof_buffer;
1765 size_t eval_caches;
1766 size_t functions_caches;
1767 size_t script_vm;
1768 size_t overhead_total;
1769 size_t dataset;
1770 size_t total_keys;
1771 size_t bytes_per_key;
1772 float dataset_perc;
1773 float peak_perc;
1774 float total_frag;
1775 ssize_t total_frag_bytes;
1776 float allocator_frag;
1777 ssize_t allocator_frag_bytes;
1778 float allocator_rss;
1779 ssize_t allocator_rss_bytes;
1780 float rss_extra;
1781 size_t rss_extra_bytes;
1782 size_t num_dbs;
1783 size_t overhead_db_hashtable_lut;
1784 size_t overhead_db_hashtable_rehashing;
1785 unsigned long db_dict_rehashing_count;
1786 size_t asm_import_input_buffer;
1787 size_t asm_migrate_output_buffer;
1788 struct {
1789 size_t dbid;
1790 size_t overhead_ht_main;
1791 size_t overhead_ht_expires;
1792 } *db;
1793};
1794
1795/* Replication error behavior determines the replica behavior
1796 * when it receives an error over the replication stream. In
1797 * either case the error is logged. */
1798typedef enum {
1799 PROPAGATION_ERR_BEHAVIOR_IGNORE = 0,
1800 PROPAGATION_ERR_BEHAVIOR_PANIC,
1801 PROPAGATION_ERR_BEHAVIOR_PANIC_ON_REPLICAS
1802} replicationErrorBehavior;
1803
1804/* This structure can be optionally passed to RDB save/load functions in
1805 * order to implement additional functionalities, by storing and loading
1806 * metadata to the RDB file.
1807 *
1808 * For example, to use select a DB at load time, useful in
1809 * replication in order to make sure that chained slaves (slaves of slaves)
1810 * select the correct DB and are able to accept the stream coming from the
1811 * top-level master. */
1812typedef struct rdbSaveInfo {
1813 /* Used saving and loading. */
1814 int repl_stream_db; /* DB to select in server.master client. */
1815
1816 /* Used only loading. */
1817 int repl_id_is_set; /* True if repl_id field is set. */
1818 char repl_id[CONFIG_RUN_ID_SIZE+1]; /* Replication ID. */
1819 long long repl_offset; /* Replication offset. */
1820} rdbSaveInfo;
1821
1822#define RDB_SAVE_INFO_INIT {-1,0,"0000000000000000000000000000000000000000",-1}
1823
1824struct malloc_stats {
1825 size_t zmalloc_used;
1826 size_t process_rss;
1827 size_t allocator_allocated;
1828 size_t allocator_active;
1829 size_t allocator_resident;
1830 size_t allocator_muzzy;
1831 size_t allocator_frag_smallbins_bytes;
1832 size_t lua_allocator_allocated;
1833 size_t lua_allocator_active;
1834 size_t lua_allocator_resident;
1835 size_t lua_allocator_frag_smallbins_bytes;
1836};
1837
1838/*-----------------------------------------------------------------------------
1839 * TLS Context Configuration
1840 *----------------------------------------------------------------------------*/
1841
1842typedef struct redisTLSContextConfig {
1843 char *cert_file; /* Server side and optionally client side cert file name */
1844 char *key_file; /* Private key filename for cert_file */
1845 char *key_file_pass; /* Optional password for key_file */
1846 char *client_cert_file; /* Certificate to use as a client; if none, use cert_file */
1847 char *client_key_file; /* Private key filename for client_cert_file */
1848 char *client_key_file_pass; /* Optional password for client_key_file */
1849 int client_auth_user; /* Field to be used for automatic TLS authentication based on client TLS certificate */
1850 char *dh_params_file;
1851 char *ca_cert_file;
1852 char *ca_cert_dir;
1853 char *protocols;
1854 char *ciphers;
1855 char *ciphersuites;
1856 int prefer_server_ciphers;
1857 int session_caching;
1858 int session_cache_size;
1859 int session_cache_timeout;
1860} redisTLSContextConfig;
1861
1862/*-----------------------------------------------------------------------------
1863 * AOF manifest definition
1864 *----------------------------------------------------------------------------*/
1865typedef enum {
1866 AOF_FILE_TYPE_BASE = 'b', /* BASE file */
1867 AOF_FILE_TYPE_HIST = 'h', /* HISTORY file */
1868 AOF_FILE_TYPE_INCR = 'i', /* INCR file */
1869} aof_file_type;
1870
1871typedef struct {
1872 sds file_name; /* file name */
1873 long long file_seq; /* file sequence */
1874 aof_file_type file_type; /* file type */
1875 long long start_offset; /* the start replication offset of the file */
1876 long long end_offset; /* the end replication offset of the file */
1877} aofInfo;
1878
1879typedef struct {
1880 aofInfo *base_aof_info; /* BASE file information. NULL if there is no BASE file. */
1881 list *incr_aof_list; /* INCR AOFs list. We may have multiple INCR AOF when rewrite fails. */
1882 list *history_aof_list; /* HISTORY AOF list. When the AOFRW success, The aofInfo contained in
1883 `base_aof_info` and `incr_aof_list` will be moved to this list. We
1884 will delete these AOF files when AOFRW finish. */
1885 long long curr_base_file_seq; /* The sequence number used by the current BASE file. */
1886 long long curr_incr_file_seq; /* The sequence number used by the current INCR file. */
1887 int dirty; /* 1 Indicates that the aofManifest in the memory is inconsistent with
1888 disk, we need to persist it immediately. */
1889} aofManifest;
1890
1891/*-----------------------------------------------------------------------------
1892 * Global server state
1893 *----------------------------------------------------------------------------*/
1894
1895/* AIX defines hz to __hz, we don't use this define and in order to allow
1896 * Redis build on AIX we need to undef it. */
1897#ifdef _AIX
1898#undef hz
1899#endif
1900
1901#define CHILD_TYPE_NONE 0
1902#define CHILD_TYPE_RDB 1
1903#define CHILD_TYPE_AOF 2
1904#define CHILD_TYPE_LDB 3
1905#define CHILD_TYPE_MODULE 4
1906
1907typedef enum childInfoType {
1908 CHILD_INFO_TYPE_CURRENT_INFO,
1909 CHILD_INFO_TYPE_AOF_COW_SIZE,
1910 CHILD_INFO_TYPE_RDB_COW_SIZE,
1911 CHILD_INFO_TYPE_MODULE_COW_SIZE
1912} childInfoType;
1913
1914typedef struct hotkeyStats hotkeyStats;
1915
1916struct redisServer {
1917 /* General */
1918 pid_t pid; /* Main process pid. */
1919 pthread_t main_thread_id; /* Main thread id */
1920 char *configfile; /* Absolute config file path, or NULL */
1921 char *executable; /* Absolute executable file path. */
1922 char **exec_argv; /* Executable argv vector (copy). */
1923 int dynamic_hz; /* Change hz value depending on # of clients. */
1924 int config_hz; /* Configured HZ value. May be different than
1925 the actual 'hz' field value if dynamic-hz
1926 is enabled. */
1927 mode_t umask; /* The umask value of the process on startup */
1928 int hz; /* serverCron() calls frequency in hertz */
1929 int in_fork_child; /* indication that this is a fork child */
1930 redisDb *db;
1931 dict *commands; /* Command table */
1932 dict *orig_commands; /* Command table before command renaming. */
1933 aeEventLoop *el;
1934 rax *errors; /* Errors table */
1935 int errors_enabled; /* If true, errorstats is enabled, and we will add new errors. */
1936 unsigned int lruclock; /* Clock for LRU eviction */
1937 redisAtomic int shutdown_asap; /* Shutdown ordered by signal handler. */
1938 redisAtomic int crashing; /* Server is crashing report. */
1939 mstime_t shutdown_mstime; /* Timestamp to limit graceful shutdown. */
1940 redisAtomic int last_sig_received; /* Indicates the last SIGNAL received, if any (e.g., SIGINT or SIGTERM). */
1941 int shutdown_flags; /* Flags passed to prepareForShutdown(). */
1942 int activerehashing; /* Incremental rehash in serverCron() */
1943 int active_defrag_running; /* Active defragmentation running (holds current scan aggressiveness) */
1944 char *pidfile; /* PID file path */
1945 int arch_bits; /* 32 or 64 depending on sizeof(long) */
1946 int cronloops; /* Number of times the cron function run */
1947 char runid[CONFIG_RUN_ID_SIZE+1]; /* ID always different at every exec. */
1948 int sentinel_mode; /* True if this instance is a Sentinel. */
1949 size_t initial_memory_usage; /* Bytes used after initialization. */
1950 int always_show_logo; /* Show logo even for non-stdout logging. */
1951 int in_exec; /* Are we inside EXEC? */
1952 int busy_module_yield_flags; /* Are we inside a busy module? (triggered by RM_Yield). see BUSY_MODULE_YIELD_ flags. */
1953 const char *busy_module_yield_reply; /* When non-null, we are inside RM_Yield. */
1954 char *ignore_warnings; /* Config: warnings that should be ignored. */
1955 int client_pause_in_transaction; /* Was a client pause executed during this Exec? */
1956 int thp_enabled; /* If true, THP is enabled. */
1957 size_t page_size; /* The page size of OS. */
1958 redisAtomic int running; /* Running if true, IO threads can send clients without notification */
1959 /* Modules */
1960 dict *moduleapi; /* Exported core APIs dictionary for modules. */
1961 dict *sharedapi; /* Like moduleapi but containing the APIs that
1962 modules share with each other. */
1963 dict *module_configs_queue; /* Unmapped configs are queued here, assumed to be module config. Applied after modules are loaded during startup or arguments to loadex. */
1964 list *loadmodule_queue; /* List of modules to load at startup. */
1965 int module_pipe[2]; /* Pipe used to awake the event loop by module threads. */
1966 pid_t child_pid; /* PID of current child */
1967 int child_type; /* Type of current child */
1968 redisAtomic int module_gil_acquring; /* Indicates whether the GIL is being acquiring by the main thread. */
1969 /* Networking */
1970 int port; /* TCP listening port */
1971 int tls_port; /* TLS listening port */
1972 int tcp_backlog; /* TCP listen() backlog */
1973 char *bindaddr[CONFIG_BINDADDR_MAX]; /* Addresses we should bind to */
1974 int bindaddr_count; /* Number of addresses in server.bindaddr[] */
1975 char *bind_source_addr; /* Source address to bind on for outgoing connections */
1976 char *unixsocket; /* UNIX socket path */
1977 unsigned int unixsocketperm; /* UNIX socket permission (see mode_t) */
1978 connListener listeners[CONN_TYPE_MAX]; /* TCP/Unix/TLS even more types */
1979 uint32_t socket_mark_id; /* ID for listen socket marking */
1980 connListener clistener; /* Cluster bus listener */
1981 list *clients; /* List of active clients */
1982 list *clients_to_close; /* Clients to close asynchronously */
1983 list *clients_pending_write; /* There is to write or install handler. */
1984 list *clients_pending_read; /* Client has pending read socket buffers. */
1985 list *clients_with_pending_ref_reply; /* Clients with referenced reply objects. */
1986 list *slaves, *monitors; /* List of slaves and MONITORs */
1987 client *current_client; /* The client that triggered the command execution (External or AOF). */
1988 client *executing_client; /* The client executing the current command (possibly script or module). */
1989
1990#ifdef LOG_REQ_RES
1991 char *req_res_logfile; /* Path of log file for logging all requests and their replies. If NULL, no logging will be performed */
1992 unsigned int client_default_resp;
1993#endif
1994
1995 /* Stuff for client mem eviction */
1996 clientMemUsageBucket* client_mem_usage_buckets;
1997
1998 rax *clients_timeout_table; /* Radix tree for blocked clients timeouts. */
1999 int execution_nesting; /* Execution nesting level.
2000 * e.g. call(), async module stuff (timers, events, etc.),
2001 * cron stuff (active expire, eviction) */
2002 rax *clients_index; /* Active clients dictionary by client ID. */
2003 uint32_t paused_actions; /* Bitmask of actions that are currently paused */
2004 list *postponed_clients; /* List of postponed clients */
2005 pause_event client_pause_per_purpose[NUM_PAUSE_PURPOSES];
2006 char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */
2007 dict *migrate_cached_sockets;/* MIGRATE cached sockets */
2008 redisAtomic uint64_t next_client_id; /* Next client unique ID. Incremental. */
2009 int protected_mode; /* Don't accept external connections. */
2010 int io_threads_num; /* Number of IO threads to use. */
2011 int io_threads_clients_num[IO_THREADS_MAX_NUM]; /* Number of clients assigned to each IO thread. */
2012 int io_threads_do_reads; /* Read and parse from IO threads? */
2013 int io_threads_active; /* Is IO threads currently active? */
2014 pendingCommandPool cmd_pool; /* Shared pool for reusing pendingCommand,
2015 * only when IO threads disabled */
2016 int prefetch_batch_max_size;/* Maximum number of keys to prefetch in a single batch */
2017 long long events_processed_while_blocked; /* processEventsWhileBlocked() */
2018 int enable_protected_configs; /* Enable the modification of protected configs, see PROTECTED_ACTION_ALLOWED_* */
2019 int enable_debug_cmd; /* Enable DEBUG commands, see PROTECTED_ACTION_ALLOWED_* */
2020 int enable_module_cmd; /* Enable MODULE commands, see PROTECTED_ACTION_ALLOWED_* */
2021
2022 /* RDB / AOF loading information */
2023 volatile sig_atomic_t loading; /* We are loading data from disk if true */
2024 volatile sig_atomic_t async_loading; /* We are loading data without blocking the db being served */
2025 off_t loading_total_bytes;
2026 off_t loading_rdb_used_mem;
2027 off_t loading_loaded_bytes;
2028 time_t loading_start_time;
2029 off_t loading_process_events_interval_bytes;
2030 /* Fields used only for stats */
2031 time_t stat_starttime; /* Server start time */
2032 long long stat_numcommands; /* Number of processed commands */
2033 long long stat_numconnections; /* Number of connections received */
2034 long long stat_expiredkeys; /* Number of expired keys */
2035 long long stat_expired_subkeys; /* Number of expired subkeys (Currently only hash-fields) */
2036 double stat_expired_stale_perc; /* Percentage of keys probably expired */
2037 long long stat_expired_time_cap_reached_count; /* Early expire cycle stops.*/
2038 long long stat_expire_cycle_time_used; /* Cumulative microseconds used. */
2039 long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */
2040 long long stat_evictedclients; /* Number of evicted clients */
2041 long long stat_evictedscripts; /* Number of evicted lua scripts. */
2042 long long stat_total_eviction_exceeded_time; /* Total time over the memory limit, unit us */
2043 monotime stat_last_eviction_exceeded_time; /* Timestamp of current eviction start, unit us */
2044 long long stat_keyspace_hits; /* Number of successful lookups of keys */
2045 long long stat_keyspace_misses; /* Number of failed lookups of keys */
2046 long long stat_active_defrag_hits; /* number of allocations moved */
2047 long long stat_active_defrag_misses; /* number of allocations scanned but not moved */
2048 long long stat_active_defrag_key_hits; /* number of keys with moved allocations */
2049 long long stat_active_defrag_key_misses;/* number of keys scanned and not moved */
2050 long long stat_active_defrag_scanned; /* number of dictEntries scanned */
2051 long long stat_total_active_defrag_time; /* Total time memory fragmentation over the limit, unit us */
2052 monotime stat_last_active_defrag_time; /* Timestamp of current active defrag start */
2053 size_t stat_peak_memory; /* Max used memory record */
2054 time_t stat_peak_memory_time; /* Time when stat_peak_memory was recorded */
2055 long long stat_aof_rewrites; /* number of aof file rewrites performed */
2056 long long stat_aofrw_consecutive_failures; /* The number of consecutive failures of aofrw */
2057 long long stat_rdb_saves; /* number of rdb saves performed */
2058 long long stat_rdb_consecutive_failures; /* The number of consecutive failures of rdb saves */
2059 long long stat_fork_time; /* Time needed to perform latest fork() */
2060 double stat_fork_rate; /* Fork rate in GB/sec. */
2061 long long stat_total_forks; /* Total count of fork. */
2062 long long stat_rejected_conn; /* Clients rejected because of maxclients */
2063 long long stat_sync_full; /* Number of full resyncs with slaves. */
2064 long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */
2065 long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */
2066 list *slowlog; /* SLOWLOG list of commands */
2067 long long slowlog_entry_id; /* SLOWLOG current entry ID */
2068 long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
2069 unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */
2070 struct malloc_stats cron_malloc_stats; /* sampled in serverCron(). */
2071 redisAtomic long long stat_net_input_bytes; /* Bytes read from network. */
2072 redisAtomic long long stat_net_output_bytes; /* Bytes written to network. */
2073 redisAtomic long long stat_net_repl_input_bytes; /* Bytes read during replication, added to stat_net_input_bytes in 'info'. */
2074 redisAtomic long long stat_net_repl_output_bytes; /* Bytes written during replication, added to stat_net_output_bytes in 'info'. */
2075 size_t stat_current_cow_peak; /* Peak size of copy on write bytes. */
2076 size_t stat_current_cow_bytes; /* Copy on write bytes while child is active. */
2077 monotime stat_current_cow_updated; /* Last update time of stat_current_cow_bytes */
2078 size_t stat_current_save_keys_processed; /* Processed keys while child is active. */
2079 size_t stat_current_save_keys_total; /* Number of keys when child started. */
2080 size_t stat_rdb_cow_bytes; /* Copy on write bytes during RDB saving. */
2081 size_t stat_aof_cow_bytes; /* Copy on write bytes during AOF rewrite. */
2082 size_t stat_module_cow_bytes; /* Copy on write bytes during module fork. */
2083 double stat_module_progress; /* Module save progress. */
2084 size_t stat_clients_type_memory[CLIENT_TYPE_COUNT];/* Mem usage by type */
2085 size_t stat_cluster_links_memory; /* Mem usage by cluster links */
2086 long long stat_unexpected_error_replies; /* Number of unexpected (aof-loading, replica to master, etc.) error replies */
2087 long long stat_total_error_replies; /* Total number of issued error replies ( command + rejected errors ) */
2088 long long stat_dump_payload_sanitizations; /* Number deep dump payloads integrity validations. */
2089 redisAtomic long long stat_io_reads_processed[IO_THREADS_MAX_NUM]; /* Number of read events processed by IO / Main threads */
2090 redisAtomic long long stat_io_writes_processed[IO_THREADS_MAX_NUM]; /* Number of write events processed by IO / Main threads */
2091 redisAtomic long long stat_client_qbuf_limit_disconnections; /* Total number of clients reached query buf length limit */
2092 long long stat_client_outbuf_limit_disconnections; /* Total number of clients reached output buf length limit */
2093 long long stat_cluster_incompatible_ops; /* Number of operations that are incompatible with cluster mode */
2094 long long stat_total_prefetch_entries; /* Total number of prefetched dict entries */
2095 long long stat_total_prefetch_batches; /* Total number of prefetched batches */
2096 /* The following two are used to track instantaneous metrics, like
2097 * number of operations per second, network traffic. */
2098 struct {
2099 long long last_sample_base; /* The divisor of last sample window */
2100 long long last_sample_value; /* The dividend of last sample window */
2101 long long samples[STATS_METRIC_SAMPLES];
2102 int idx;
2103 } inst_metric[STATS_METRIC_COUNT];
2104 long long stat_reply_buffer_shrinks; /* Total number of output buffer shrinks */
2105 long long stat_reply_buffer_expands; /* Total number of output buffer expands */
2106 monotime el_start;
2107 /* The following two are used to record the max number of commands executed in one eventloop.
2108 * Note that commands in transactions are also counted. */
2109 long long el_cmd_cnt_start;
2110 long long el_cmd_cnt_max;
2111 /* The sum of active-expire, active-defrag and all other tasks done by cron and beforeSleep,
2112 but excluding read, write and AOF, which are counted by other sets of metrics. */
2113 monotime el_cron_duration;
2114 durationStats duration_stats[EL_DURATION_TYPE_NUM];
2115
2116 /* Hotkey tracking */
2117 hotkeyStats *hotkeys;
2118
2119 /* Configuration */
2120 int verbosity; /* Loglevel in redis.conf */
2121 int hide_user_data_from_log; /* In the event of an assertion failure, hide command arguments from the operator */
2122 int maxidletime; /* Client timeout in seconds */
2123 int tcpkeepalive; /* Set SO_KEEPALIVE if non-zero. */
2124 int active_expire_enabled; /* Can be disabled for testing purposes. */
2125 int active_expire_effort; /* From 1 (default) to 10, active effort. */
2126 int allow_access_expired; /* If > 0, allow access to logically expired keys */
2127 int allow_access_trimmed; /* If > 0, allow access to logically trimmed keys */
2128 int active_defrag_enabled;
2129 int sanitize_dump_payload; /* Enables deep sanitization for ziplist and listpack in RDB and RESTORE. */
2130 int skip_checksum_validation; /* Disable checksum validation for RDB and RESTORE payload. */
2131 int jemalloc_bg_thread; /* Enable jemalloc background thread */
2132 int active_defrag_configuration_changed; /* defrag configuration has been changed and need to reconsider
2133 * active_defrag_running in computeDefragCycles. */
2134 size_t active_defrag_ignore_bytes; /* minimum amount of fragmentation waste to start active defrag */
2135 int active_defrag_threshold_lower; /* minimum percentage of fragmentation to start active defrag */
2136 int active_defrag_threshold_upper; /* maximum percentage of fragmentation at which we use maximum effort */
2137 int active_defrag_cycle_min; /* minimal effort for defrag in CPU percentage */
2138 int active_defrag_cycle_max; /* maximal effort for defrag in CPU percentage */
2139 unsigned long active_defrag_max_scan_fields; /* maximum number of fields of set/hash/zset/list to process from within the main dict scan */
2140 size_t client_max_querybuf_len; /* Limit for client query buffer length */
2141 int lookahead; /* how many commands in each client pipeline to decode and prefetch */
2142 int dbnum; /* Total number of configured DBs */
2143 int supervised; /* 1 if supervised, 0 otherwise. */
2144 int supervised_mode; /* See SUPERVISED_* */
2145 int daemonize; /* True if running as a daemon */
2146 int set_proc_title; /* True if change proc title */
2147 char *proc_title_template; /* Process title template format */
2148 clientBufferLimitsConfig client_obuf_limits[CLIENT_TYPE_OBUF_COUNT];
2149 int pause_cron; /* Don't run cron tasks (debug) */
2150 int dict_resizing; /* Whether to allow main dict and expired dict to be resized (debug) */
2151 int latency_tracking_enabled; /* 1 if extended latency tracking is enabled, 0 otherwise. */
2152 double *latency_tracking_info_percentiles; /* Extended latency tracking info output percentile list configuration. */
2153 int latency_tracking_info_percentiles_len;
2154 int memory_tracking_per_slot; /* Account used memory per slot */
2155 unsigned int max_new_tls_conns_per_cycle; /* The maximum number of tls connections that will be accepted during each invocation of the event loop. */
2156 unsigned int max_new_conns_per_cycle; /* The maximum number of tcp connections that will be accepted during each invocation of the event loop. */
2157 int cluster_compatibility_sample_ratio; /* Sampling ratio for cluster mode incompatible commands. */
2158 int lazyexpire_nested_arbitrary_keys; /* If disabled, avoid lazy-expire from commands that touch arbitrary keys (SCAN/RANDOMKEY) within transactions */
2159
2160 /* AOF persistence */
2161 int aof_enabled; /* AOF configuration */
2162 int aof_state; /* AOF_(ON|OFF|WAIT_REWRITE) */
2163 int aof_fsync; /* Kind of fsync() policy */
2164 char *aof_filename; /* Basename of the AOF file and manifest file */
2165 char *aof_dirname; /* Name of the AOF directory */
2166 int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */
2167 int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */
2168 off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */
2169 off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */
2170 off_t aof_current_size; /* AOF current size (Including BASE + INCRs). */
2171 off_t aof_last_incr_size; /* The size of the latest incr AOF. */
2172 off_t aof_last_incr_fsync_offset; /* AOF offset which is already requested to be synced to disk.
2173 * Compare with the aof_last_incr_size. */
2174 int aof_flush_sleep; /* Micros to sleep before flush. (used by tests) */
2175 int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */
2176 sds aof_buf; /* AOF buffer, written before entering the event loop */
2177 int aof_fd; /* File descriptor of currently selected AOF file */
2178 int aof_selected_db; /* Currently selected DB in AOF */
2179 mstime_t aof_flush_postponed_start; /* mstime of postponed AOF flush */
2180 mstime_t aof_last_fsync; /* mstime of last fsync() */
2181 time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */
2182 time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */
2183 time_t aof_cur_timestamp; /* Current record timestamp in AOF */
2184 int aof_timestamp_enabled; /* Enable record timestamp in AOF */
2185 int aof_lastbgrewrite_status; /* C_OK or C_ERR */
2186 unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */
2187 int aof_rewrite_incremental_fsync;/* fsync incrementally while aof rewriting? */
2188 int rdb_save_incremental_fsync; /* fsync incrementally while rdb saving? */
2189 int aof_last_write_status; /* C_OK or C_ERR */
2190 int aof_last_write_errno; /* Valid if aof write/fsync status is ERR */
2191 int aof_load_truncated; /* Don't stop on unexpected AOF EOF. */
2192 off_t aof_load_corrupt_tail_max_size; /* The max size of broken AOF tail than can be ignored. */
2193 int aof_use_rdb_preamble; /* Specify base AOF to use RDB encoding on AOF rewrites. */
2194 redisAtomic int aof_bio_fsync_status; /* Status of AOF fsync in bio job. */
2195 redisAtomic int aof_bio_fsync_errno; /* Errno of AOF fsync in bio job. */
2196 aofManifest *aof_manifest; /* Used to track AOFs. */
2197 int aof_disable_auto_gc; /* If disable automatically deleting HISTORY type AOFs?
2198 default no. (for testings). */
2199
2200 /* RDB persistence */
2201 long long dirty; /* Changes to DB from the last save */
2202 long long dirty_before_bgsave; /* Used to restore dirty on failed BGSAVE */
2203 long long rdb_last_load_keys_expired; /* number of expired keys when loading RDB */
2204 long long rdb_last_load_keys_loaded; /* number of loaded keys when loading RDB */
2205 int bgsave_aborted; /* Set when killing a child, to treat it as aborted even if it succeeds. */
2206 struct saveparam *saveparams; /* Save points array for RDB */
2207 int saveparamslen; /* Number of saving points */
2208 char *rdb_filename; /* Name of RDB file */
2209 int rdb_compression; /* Use compression in RDB? */
2210 int rdb_checksum; /* Use RDB checksum? */
2211 int rdb_del_sync_files; /* Remove RDB files used only for SYNC if
2212 the instance does not use persistence. */
2213 time_t lastsave; /* Unix time of last successful save */
2214 time_t lastbgsave_try; /* Unix time of last attempted bgsave */
2215 time_t rdb_save_time_last; /* Time used by last RDB save run. */
2216 time_t rdb_save_time_start; /* Current RDB save start time. */
2217 int rdb_bgsave_scheduled; /* BGSAVE when possible if true. */
2218 int rdb_child_type; /* Type of save by active child. */
2219 int lastbgsave_status; /* C_OK or C_ERR */
2220 int stop_writes_on_bgsave_err; /* Don't allow writes if can't BGSAVE */
2221 int rdb_pipe_read; /* RDB pipe used to transfer the rdb data */
2222 /* to the parent process in diskless repl. */
2223 int rdb_child_exit_pipe; /* Used by the diskless parent allow child exit. */
2224 connection **rdb_pipe_conns; /* Connections which are currently the */
2225 int rdb_pipe_numconns; /* target of diskless rdb fork child. */
2226 int rdb_pipe_numconns_writing; /* Number of rdb conns with pending writes. */
2227 char *rdb_pipe_buff; /* In diskless replication, this buffer holds data */
2228 int rdb_pipe_bufflen; /* that was read from the rdb pipe. */
2229 int rdb_key_save_delay; /* Delay in microseconds between keys while
2230 * writing aof or rdb. (for testings). negative
2231 * value means fractions of microseconds (on average). */
2232 int key_load_delay; /* Delay in microseconds between keys while
2233 * loading aof or rdb. (for testings). negative
2234 * value means fractions of microseconds (on average). */
2235 /* Pipe and data structures for child -> parent info sharing. */
2236 int child_info_pipe[2]; /* Pipe used to write the child_info_data. */
2237 int child_info_nread; /* Num of bytes of the last read from pipe */
2238 /* Propagation of commands in AOF / replication */
2239 redisOpArray also_propagate; /* Additional command to propagate. */
2240 int replication_allowed; /* Are we allowed to replicate? */
2241 /* Logging */
2242 char *logfile; /* Path of log file */
2243 int syslog_enabled; /* Is syslog enabled? */
2244 char *syslog_ident; /* Syslog ident */
2245 int syslog_facility; /* Syslog facility */
2246 int crashlog_enabled; /* Enable signal handler for crashlog.
2247 * disable for clean core dumps. */
2248 int memcheck_enabled; /* Enable memory check on crash. */
2249 int use_exit_on_panic; /* Use exit() on panic and assert rather than
2250 * abort(). useful for Valgrind. */
2251 /* Shutdown */
2252 int shutdown_timeout; /* Graceful shutdown time limit in seconds. */
2253 int shutdown_on_sigint; /* Shutdown flags configured for SIGINT. */
2254 int shutdown_on_sigterm; /* Shutdown flags configured for SIGTERM. */
2255
2256 /* Replication (master) */
2257 char replid[CONFIG_RUN_ID_SIZE+1]; /* My current replication ID. */
2258 char replid2[CONFIG_RUN_ID_SIZE+1]; /* replid inherited from master*/
2259 long long master_repl_offset; /* My current replication offset */
2260 long long second_replid_offset; /* Accept offsets up to this for replid2. */
2261 redisAtomic long long fsynced_reploff_pending;/* Largest replication offset to
2262 * potentially have been fsynced, applied to
2263 fsynced_reploff only when AOF state is AOF_ON
2264 (not during the initial rewrite) */
2265 long long fsynced_reploff; /* Largest replication offset that has been confirmed to be fsynced */
2266 int slaveseldb; /* Last SELECTed DB in replication output */
2267 int repl_ping_slave_period; /* Master pings the slave every N seconds */
2268 replBacklog *repl_backlog; /* Replication backlog for partial syncs */
2269 long long repl_backlog_size; /* Backlog circular buffer size */
2270 long long repl_full_sync_buffer_limit; /* Accumulated repl data limit during rdb channel replication */
2271 replDataBuf repl_full_sync_buffer; /* Accumulated replication data for rdb channel replication */
2272 time_t repl_backlog_time_limit; /* Time without slaves after the backlog
2273 gets released. */
2274 time_t repl_no_slaves_since; /* We have no slaves since that time.
2275 Only valid if server.slaves len is 0. */
2276 int repl_min_slaves_to_write; /* Min number of slaves to write. */
2277 int repl_min_slaves_max_lag; /* Max lag of <count> slaves to write. */
2278 int repl_good_slaves_count; /* Number of slaves with lag <= max_lag. */
2279 int repl_diskless_sync; /* Master send RDB to slaves sockets directly. */
2280 int repl_diskless_load; /* Slave parse RDB directly from the socket.
2281 * see REPL_DISKLESS_LOAD_* enum */
2282 int repl_diskless_sync_delay; /* Delay to start a diskless repl BGSAVE. */
2283 int repl_diskless_sync_max_replicas;/* Max replicas for diskless repl BGSAVE
2284 * delay (start sooner if they all connect). */
2285 int repl_rdb_channel; /* Config used to determine if the replica should
2286 * use rdb channel replication for full syncs. */
2287 int repl_debug_pause; /* Debug config to force the main process to pause. */
2288 size_t repl_buffer_mem; /* The memory of replication buffer. */
2289 list *repl_buffer_blocks; /* Replication buffers blocks list
2290 * (serving replica clients and repl backlog) */
2291 time_t repl_stream_lastio; /* Unix time of the latest sending replication stream. */
2292 /* Replication (slave) */
2293 char *masteruser; /* AUTH with this user and masterauth with master */
2294 sds masterauth; /* AUTH with this password with master */
2295 char *masterhost; /* Hostname of master */
2296 int masterport; /* Port of master */
2297 int repl_timeout; /* Timeout after N seconds of master idle */
2298 client *master; /* Client that is master for this slave */
2299 client *cached_master; /* Cached master to be reused for PSYNC. */
2300 int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
2301 int repl_state; /* Replication status if the instance is a slave */
2302 int repl_rdb_ch_state; /* State of the replica's rdb channel during rdb channel replication */
2303 int repl_main_ch_state; /* State of the replica's main channel during rdb channel replication */
2304 uint64_t repl_num_master_disconnection; /* Number of master connection was disconnected */
2305 uint64_t repl_main_ch_client_id; /* Main channel client id received in +RDBCHANNELSYNC reply. */
2306 off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
2307 off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
2308 off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
2309 connection *repl_transfer_s; /* Slave -> Master SYNC connection */
2310 connection *repl_rdb_transfer_s; /* Slave -> Master FULL SYNC connection (RDB download) */
2311 int repl_transfer_fd; /* Slave -> Master SYNC temp file descriptor */
2312 char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
2313 time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */
2314 int repl_serve_stale_data; /* Serve stale data when link is down? */
2315 int repl_slave_ro; /* Slave is read only? */
2316 int repl_slave_ignore_maxmemory; /* If true slaves do not evict. */
2317 time_t repl_down_since; /* Unix time at which link with master went down */
2318 time_t repl_up_since; /* Unix time that master link is fully up and healthy */
2319 int repl_disable_tcp_nodelay; /* Disable TCP_NODELAY after SYNC? */
2320 int slave_priority; /* Reported in INFO and used by Sentinel. */
2321 int replica_announced; /* If true, replica is announced by Sentinel */
2322 int slave_announce_port; /* Give the master this listening port. */
2323 char *slave_announce_ip; /* Give the master this ip address. */
2324 int propagation_error_behavior; /* Configures the behavior of the replica
2325 * when it receives an error on the replication stream */
2326 int repl_ignore_disk_write_error; /* Configures whether replicas panic when unable to
2327 * persist writes to AOF. */
2328 /* The following two fields is where we store master PSYNC replid/offset
2329 * while the PSYNC is in progress. At the end we'll copy the fields into
2330 * the server->master client structure. */
2331 char master_replid[CONFIG_RUN_ID_SIZE+1]; /* Master PSYNC runid. */
2332 long long master_initial_offset; /* Master PSYNC offset. */
2333 int repl_slave_lazy_flush; /* Lazy FLUSHALL before loading DB? */
2334 /* Synchronous replication. */
2335 list *clients_waiting_acks; /* Clients waiting in WAIT or WAITAOF. */
2336 int get_ack_from_slaves; /* If true we send REPLCONF GETACK. */
2337 long long repl_current_sync_attempts; /* Number of times in current configuration, the replica attempted to sync since the last success. */
2338 long long repl_total_sync_attempts; /* Number of times in current configuration, the replica attempted to sync to a master */
2339 time_t repl_disconnect_start_time; /* Unix time that master disconnection start */
2340 time_t repl_total_disconnect_time; /* The total cumulative time we've been disconnected as a replica, visible when the link is up too. */
2341 /* Limits */
2342 unsigned int maxclients; /* Max number of simultaneous clients */
2343 unsigned long long maxmemory; /* Max number of memory bytes to use */
2344 ssize_t maxmemory_clients; /* Memory limit for total client buffers */
2345 int maxmemory_policy; /* Policy for key eviction */
2346 int maxmemory_samples; /* Precision of random sampling */
2347 int maxmemory_eviction_tenacity;/* Aggressiveness of eviction processing */
2348 int lfu_log_factor; /* LFU logarithmic counter factor. */
2349 int lfu_decay_time; /* LFU counter decay factor. */
2350 long long proto_max_bulk_len; /* Protocol bulk length maximum size. */
2351 int oom_score_adj_values[CONFIG_OOM_COUNT]; /* Linux oom_score_adj configuration */
2352 int oom_score_adj; /* If true, oom_score_adj is managed */
2353 int disable_thp; /* If true, disable THP by syscall */
2354 /* Blocked clients */
2355 unsigned int blocked_clients; /* # of clients executing a blocking cmd.*/
2356 unsigned int blocked_clients_by_type[BLOCKED_NUM];
2357 list *unblocked_clients; /* list of clients to unblock before next loop */
2358 list *ready_keys; /* List of readyList structures for BLPOP & co */
2359 /* Client side caching. */
2360 unsigned int tracking_clients; /* # of clients with tracking enabled.*/
2361 size_t tracking_table_max_keys; /* Max number of keys in tracking table. */
2362 list *tracking_pending_keys; /* tracking invalidation keys pending to flush */
2363 list *pending_push_messages; /* pending publish or other push messages to flush */
2364 /* Sort parameters - qsort_r() is only available under BSD so we
2365 * have to take this state global, in order to pass it to sortCompare() */
2366 int sort_desc;
2367 int sort_alpha;
2368 int sort_bypattern;
2369 int sort_store;
2370 /* Zip structure config, see redis.conf for more information */
2371 size_t hash_max_listpack_entries;
2372 size_t hash_max_listpack_value;
2373 size_t set_max_intset_entries;
2374 size_t set_max_listpack_entries;
2375 size_t set_max_listpack_value;
2376 size_t zset_max_listpack_entries;
2377 size_t zset_max_listpack_value;
2378 size_t hll_sparse_max_bytes;
2379 size_t stream_node_max_bytes;
2380 long long stream_node_max_entries;
2381 /* Stream IDMP parameters */
2382 long long stream_idmp_duration; /* Default IDMP duration in seconds. */
2383 long long stream_idmp_maxsize; /* Default IDMP max entries. */
2384 /* List parameters */
2385 int list_max_listpack_size;
2386 int list_compress_depth;
2387 /* time cache */
2388 redisAtomic time_t unixtime; /* Unix time sampled every cron cycle. */
2389 time_t timezone; /* Cached timezone. As set by tzset(). */
2390 redisAtomic int daylight_active; /* Currently in daylight saving time. */
2391 mstime_t mstime; /* 'unixtime' in milliseconds. */
2392 ustime_t ustime; /* 'unixtime' in microseconds. */
2393 mstime_t cmd_time_snapshot; /* Time snapshot of the root execution nesting. */
2394 size_t blocking_op_nesting; /* Nesting level of blocking operation, used to reset blocked_last_cron. */
2395 long long blocked_last_cron; /* Indicate the mstime of the last time we did cron jobs from a blocking operation */
2396 /* Pubsub */
2397 kvstore *pubsub_channels; /* Map channels to list of subscribed clients */
2398 dict *pubsub_patterns; /* A dict of pubsub_patterns */
2399 int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an
2400 xor of NOTIFY_... flags. */
2401 kvstore *pubsubshard_channels; /* Map shard channels in every slot to list of subscribed clients */
2402 unsigned int pubsub_clients; /* # of clients in Pub/Sub mode */
2403 unsigned int watching_clients; /* # of clients are wathcing keys */
2404 /* Cluster */
2405 int cluster_enabled; /* Is cluster enabled? */
2406 int cluster_port; /* Set the cluster port for a node. */
2407 mstime_t cluster_node_timeout; /* Cluster node timeout. */
2408 mstime_t cluster_ping_interval; /* A debug configuration for setting how often cluster nodes send ping messages. */
2409 char *cluster_configfile; /* Cluster auto-generated config file name. */
2410 long long asm_handoff_max_lag_bytes; /* Maximum lag in bytes before pausing writes for ASM handoff. */
2411 long long asm_write_pause_timeout; /* Timeout in milliseconds to pause writes during ASM handoff. */
2412 long long asm_sync_buffer_drain_timeout; /* Timeout in milliseconds for sync buffer to drain during ASM. */
2413 int asm_max_archived_tasks; /* Maximum number of archived ASM tasks to keep in memory. */
2414 struct clusterState *cluster; /* State of the cluster */
2415 int cluster_migration_barrier; /* Cluster replicas migration barrier. */
2416 int cluster_allow_replica_migration; /* Automatic replica migrations to orphaned masters and from empty masters */
2417 int cluster_slave_validity_factor; /* Slave max data age for failover. */
2418 int cluster_require_full_coverage; /* If true, put the cluster down if
2419 there is at least an uncovered slot.*/
2420 int cluster_slave_no_failover; /* Prevent slave from starting a failover
2421 if the master is in failure state. */
2422 char *cluster_announce_ip; /* IP address to announce on cluster bus. */
2423 char *cluster_announce_hostname; /* hostname to announce on cluster bus. */
2424 char *cluster_announce_human_nodename; /* Human readable node name assigned to a node. */
2425 int cluster_preferred_endpoint_type; /* Use the announced hostname when available. */
2426 int cluster_announce_port; /* base port to announce on cluster bus. */
2427 int cluster_announce_tls_port; /* TLS port to announce on cluster bus. */
2428 int cluster_announce_bus_port; /* bus port to announce on cluster bus. */
2429 int cluster_module_flags; /* Set of flags that Redis modules are able
2430 to set in order to suppress certain
2431 native Redis Cluster features. Check the
2432 REDISMODULE_CLUSTER_FLAG_*. */
2433 int cluster_module_trim_disablers; /* Number of module requests to disable trimming */
2434 int cluster_allow_reads_when_down; /* Are reads allowed when the cluster
2435 is down? */
2436 int cluster_config_file_lock_fd; /* cluster config fd, will be flocked. */
2437 unsigned long long cluster_link_msg_queue_limit_bytes; /* Memory usage limit on individual link msg queue */
2438 int cluster_drop_packet_filter; /* Debug config that allows tactically
2439 * dropping packets of a specific type */
2440 int cluster_slot_stats_enabled; /* Cluster slot usage statistics tracking enabled. */
2441 /* Scripting */
2442 unsigned int lua_arena; /* eval lua arena used in jemalloc. */
2443 mstime_t busy_reply_threshold; /* Script / module timeout in milliseconds */
2444 int pre_command_oom_state; /* OOM before command (script?) was started */
2445 int script_disable_deny_script; /* Allow running commands marked "noscript" inside a script. */
2446 int lua_enable_deprecated_api; /* Config to enable deprecated api */
2447 /* Lazy free */
2448 int lazyfree_lazy_eviction;
2449 int lazyfree_lazy_expire;
2450 int lazyfree_lazy_server_del;
2451 int lazyfree_lazy_user_del;
2452 int lazyfree_lazy_user_flush;
2453 /* Latency monitor */
2454 long long latency_monitor_threshold;
2455 dict *latency_events;
2456 /* ACLs */
2457 char *acl_filename; /* ACL Users file. NULL if not configured. */
2458 unsigned long acllog_max_len; /* Maximum length of the ACL LOG list. */
2459 sds requirepass; /* Remember the cleartext password set with
2460 the old "requirepass" directive for
2461 backward compatibility with Redis <= 5. */
2462 int acl_pubsub_default; /* Default ACL pub/sub channels flag */
2463 aclInfo acl_info; /* ACL info */
2464 /* Assert & bug reporting */
2465 int watchdog_period; /* Software watchdog period in ms. 0 = off */
2466 /* System hardware info */
2467 size_t system_memory_size; /* Total memory in system as reported by OS */
2468 /* TLS Configuration */
2469 int tls_cluster;
2470 int tls_replication;
2471 int tls_auth_clients;
2472 redisTLSContextConfig tls_ctx_config;
2473 /* cpu affinity */
2474 char *server_cpulist; /* cpu affinity list of redis server main/io thread. */
2475 char *bio_cpulist; /* cpu affinity list of bio thread. */
2476 char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */
2477 char *bgsave_cpulist; /* cpu affinity list of bgsave process. */
2478 /* Sentinel config */
2479 struct sentinelConfig *sentinel_config; /* sentinel config to load at startup time. */
2480 /* Coordinate failover info */
2481 mstime_t failover_end_time; /* Deadline for failover command. */
2482 int force_failover; /* If true then failover will be forced at the
2483 * deadline, otherwise failover is aborted. */
2484 char *target_replica_host; /* Failover target host. If null during a
2485 * failover then any replica can be used. */
2486 int target_replica_port; /* Failover target port */
2487 int failover_state; /* Failover state */
2488 int cluster_allow_pubsubshard_when_down; /* Is pubsubshard allowed when the cluster
2489 is down, doesn't affect pubsub global. */
2490 long reply_buffer_peak_reset_time; /* The amount of time (in milliseconds) to wait between reply buffer peak resets */
2491 int reply_buffer_resizing_enabled; /* Is reply buffer resizing enabled (1 by default) */
2492 int reply_copy_avoidance_enabled; /* Is reply copy avoidance enabled (1 by default) */
2493 /* Local environment */
2494 char *locale_collate;
2495 int dbg_assert_keysizes; /* Assert keysizes histogram after each command */
2496 int dbg_assert_alloc_per_slot; /* Assert per-slot alloc_size after each command */
2497};
2498
2499/* we use 6 so that all getKeyResult fits a cacheline */
2500#define MAX_KEYS_BUFFER 6
2501
2502typedef struct {
2503 int pos; /* The position of the key within the client array */
2504 int flags; /* The flags associated with the key access, see
2505 CMD_KEY_* for more information */
2506} keyReference;
2507
2508/* A result structure for the various getkeys function calls. It lists the
2509 * keys as indices to the provided argv. This functionality is also re-used
2510 * for returning channel information.
2511 */
2512typedef struct {
2513 int numkeys; /* Number of key indices return */
2514 int size; /* Available array size */
2515 keyReference keysbuf[MAX_KEYS_BUFFER]; /* Pre-allocated buffer, to save heap allocations */
2516 keyReference *keys; /* Key indices array, points to keysbuf or heap */
2517} getKeysResult;
2518#define GETKEYS_RESULT_INIT { 0, MAX_KEYS_BUFFER, {{0}}, NULL }
2519
2520/*-----------------------------------------------------------------------------
2521 * Hotkey tracking
2522 *----------------------------------------------------------------------------*/
2523
2524/* Hotkeys tracking metric flags */
2525#define HOTKEYS_TRACK_CPU (1ULL << 0)
2526#define HOTKEYS_TRACK_NET (1ULL << 1)
2527#define HOTKEYS_METRICS_COUNT 2 /* NOTE: update if adding new metric */
2528
2529/* A structure for tracking hotkey statistics by given metrics. */
2530struct hotkeyStats {
2531 struct chkTopK *cpu;
2532 struct chkTopK *net;
2533 mstime_t start; /* Initial time point for wall time tracking */
2534
2535 /* Only keys from selected slots will be tracked. If slots are not
2536 * initialized - all keys are tracked. */
2537 int *slots;
2538 int numslots;
2539
2540 /* Statistics counters. NOTE, time_* members are saved in microseconds for
2541 * accuracy but displayed in milliseconds during HOTKEYS GET */
2542 uint64_t time_sampled_commands_selected_slots; /* microseconds */
2543 uint64_t time_all_commands_selected_slots; /* microseconds */
2544 uint64_t time_all_commands_all_slots; /* microseconds */
2545 uint64_t net_bytes_sampled_commands_selected_slots;
2546 uint64_t net_bytes_all_commands_selected_slots;
2547 uint64_t net_bytes_all_commands_all_slots;
2548
2549 /* rusage stats for CPU time tracking */
2550 struct timeval ru_utime;
2551 struct timeval ru_stime;
2552
2553 int tracking_count; /* Count of top hotkeys we want to track */
2554 int sample_ratio; /* Track a key with probability 1 / sample_ratio */
2555 int active; /* True if tracking is currently active */
2556 mstime_t duration; /* Tracking duration */
2557 uint64_t tracked_metrics; /* Bit flags: HOTKEYS_TRACK_CPU, HOTKEYS_TRACK_NET, etc. */
2558 mstime_t cpu_time; /* Total CPU time spent updating the topk struct in milliseconds */
2559
2560 /* Current command related fields */
2561 getKeysResult keys_result; /* Key results for current command */
2562 client *current_client;
2563 int is_sampled; /* Indicates whether or not keys from cmd are sampled via sample_ratio */
2564 int is_in_selected_slots; /* Indicates whether or not keys from cmd are in selected_slots */
2565};
2566
2567typedef struct hotkeyMetrics {
2568 uint64_t cpu_time_usec;
2569 uint64_t net_bytes;
2570} hotkeyMetrics;
2571
2572/* pendingCommand flags */
2573enum {
2574 PENDING_CMD_FLAG_INCOMPLETE = 1 << 0, /* Command parsing is incomplete, still waiting for more data */
2575 PENDING_CMD_FLAG_PREPROCESSED = 1 << 1, /* This command has passed pre-processing */
2576 PENDING_CMD_KEYS_RESULT_VALID = 1 << 2, /* Command's keys_result is valid and cached */
2577};
2578
2579/* Parser state and parse result of a command from a client's input buffer. */
2580struct pendingCommand {
2581 int argc; /* Num of arguments of current command. */
2582 int argv_len; /* Size of argv array (may be more than argc) */
2583 robj **argv; /* Arguments of current command. */
2584 size_t argv_len_sum; /* Sum of lengths of objects in argv list. */
2585 unsigned long long input_bytes;
2586 struct redisCommand *cmd;
2587 getKeysResult keys_result;
2588 long long reploff; /* c->reploff should be set to this value when the command is processed */
2589 int flags;
2590 int slot; /* The slot the command is executing against. Set to INVALID_CLUSTER_SLOT
2591 * if no slot is being used or if the command has a cross slot error */
2592 uint8_t read_error;
2593
2594 struct pendingCommand *next;
2595 struct pendingCommand *prev;
2596};
2597
2598/* Key specs definitions.
2599 *
2600 * Brief: This is a scheme that tries to describe the location
2601 * of key arguments better than the old [first,last,step] scheme
2602 * which is limited and doesn't fit many commands.
2603 *
2604 * There are two steps:
2605 * 1. begin_search (BS): in which index should we start searching for keys?
2606 * 2. find_keys (FK): relative to the output of BS, how can we will which args are keys?
2607 *
2608 * There are two types of BS:
2609 * 1. index: key args start at a constant index
2610 * 2. keyword: key args start just after a specific keyword
2611 *
2612 * There are two kinds of FK:
2613 * 1. range: keys end at a specific index (or relative to the last argument)
2614 * 2. keynum: there's an arg that contains the number of key args somewhere before the keys themselves
2615 */
2616
2617/* WARNING! Must be synced with generate-command-code.py and RedisModuleKeySpecBeginSearchType */
2618typedef enum {
2619 KSPEC_BS_INVALID = 0, /* Must be 0 */
2620 KSPEC_BS_UNKNOWN,
2621 KSPEC_BS_INDEX,
2622 KSPEC_BS_KEYWORD
2623} kspec_bs_type;
2624
2625/* WARNING! Must be synced with generate-command-code.py and RedisModuleKeySpecFindKeysType */
2626typedef enum {
2627 KSPEC_FK_INVALID = 0, /* Must be 0 */
2628 KSPEC_FK_UNKNOWN,
2629 KSPEC_FK_RANGE,
2630 KSPEC_FK_KEYNUM
2631} kspec_fk_type;
2632
2633/* WARNING! This struct must match RedisModuleCommandKeySpec */
2634typedef struct {
2635 /* Declarative data */
2636 const char *notes;
2637 uint64_t flags;
2638 kspec_bs_type begin_search_type;
2639 union {
2640 struct {
2641 /* The index from which we start the search for keys */
2642 int pos;
2643 } index;
2644 struct {
2645 /* The keyword that indicates the beginning of key args */
2646 const char *keyword;
2647 /* An index in argv from which to start searching.
2648 * Can be negative, which means start search from the end, in reverse
2649 * (Example: -2 means to start in reverse from the penultimate arg) */
2650 int startfrom;
2651 } keyword;
2652 } bs;
2653 kspec_fk_type find_keys_type;
2654 union {
2655 /* NOTE: Indices in this struct are relative to the result of the begin_search step!
2656 * These are: range.lastkey, keynum.keynumidx, keynum.firstkey */
2657 struct {
2658 /* Index of the last key.
2659 * Can be negative, in which case it's not relative. -1 indicating till the last argument,
2660 * -2 one before the last and so on. */
2661 int lastkey;
2662 /* How many args should we skip after finding a key, in order to find the next one. */
2663 int keystep;
2664 /* If lastkey is -1, we use limit to stop the search by a factor. 0 and 1 mean no limit.
2665 * 2 means 1/2 of the remaining args, 3 means 1/3, and so on. */
2666 int limit;
2667 } range;
2668 struct {
2669 /* Index of the argument containing the number of keys to come */
2670 int keynumidx;
2671 /* Index of the fist key (Usually it's just after keynumidx, in
2672 * which case it should be set to keynumidx+1). */
2673 int firstkey;
2674 /* How many args should we skip after finding a key, in order to find the next one. */
2675 int keystep;
2676 } keynum;
2677 } fk;
2678} keySpec;
2679
2680#ifdef LOG_REQ_RES
2681
2682/* Must be synced with generate-command-code.py */
2683typedef enum {
2684 JSON_TYPE_STRING,
2685 JSON_TYPE_INTEGER,
2686 JSON_TYPE_BOOLEAN,
2687 JSON_TYPE_OBJECT,
2688 JSON_TYPE_ARRAY,
2689} jsonType;
2690
2691typedef struct jsonObjectElement {
2692 jsonType type;
2693 const char *key;
2694 union {
2695 const char *string;
2696 long long integer;
2697 int boolean;
2698 struct jsonObject *object;
2699 struct {
2700 struct jsonObject **objects;
2701 int length;
2702 } array;
2703 } value;
2704} jsonObjectElement;
2705
2706typedef struct jsonObject {
2707 struct jsonObjectElement *elements;
2708 int length;
2709} jsonObject;
2710
2711#endif
2712
2713/* WARNING! This struct must match RedisModuleCommandHistoryEntry */
2714typedef struct {
2715 const char *since;
2716 const char *changes;
2717} commandHistory;
2718
2719/* Must be synced with COMMAND_GROUP_STR and generate-command-code.py */
2720typedef enum {
2721 COMMAND_GROUP_GENERIC,
2722 COMMAND_GROUP_STRING,
2723 COMMAND_GROUP_LIST,
2724 COMMAND_GROUP_SET,
2725 COMMAND_GROUP_SORTED_SET,
2726 COMMAND_GROUP_HASH,
2727 COMMAND_GROUP_PUBSUB,
2728 COMMAND_GROUP_TRANSACTIONS,
2729 COMMAND_GROUP_CONNECTION,
2730 COMMAND_GROUP_SERVER,
2731 COMMAND_GROUP_SCRIPTING,
2732 COMMAND_GROUP_HYPERLOGLOG,
2733 COMMAND_GROUP_CLUSTER,
2734 COMMAND_GROUP_SENTINEL,
2735 COMMAND_GROUP_GEO,
2736 COMMAND_GROUP_STREAM,
2737 COMMAND_GROUP_BITMAP,
2738 COMMAND_GROUP_MODULE,
2739} redisCommandGroup;
2740
2741typedef void redisCommandProc(client *c);
2742typedef int redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
2743
2744/* Redis command structure.
2745 *
2746 * Note that the command table is in commands.c and it is auto-generated.
2747 *
2748 * This is the meaning of the flags:
2749 *
2750 * CMD_WRITE: Write command (may modify the key space).
2751 *
2752 * CMD_READONLY: Commands just reading from keys without changing the content.
2753 * Note that commands that don't read from the keyspace such as
2754 * TIME, SELECT, INFO, administrative commands, and connection
2755 * or transaction related commands (multi, exec, discard, ...)
2756 * are not flagged as read-only commands, since they affect the
2757 * server or the connection in other ways.
2758 *
2759 * CMD_DENYOOM: May increase memory usage once called. Don't allow if out
2760 * of memory.
2761 *
2762 * CMD_ADMIN: Administrative command, like SAVE or SHUTDOWN.
2763 *
2764 * CMD_PUBSUB: Pub/Sub related command.
2765 *
2766 * CMD_NOSCRIPT: Command not allowed in scripts.
2767 *
2768 * CMD_BLOCKING: The command has the potential to block the client.
2769 *
2770 * CMD_LOADING: Allow the command while loading the database.
2771 *
2772 * CMD_NO_ASYNC_LOADING: Deny during async loading (when a replica uses diskless
2773 * sync swapdb, and allows access to the old dataset)
2774 *
2775 * CMD_STALE: Allow the command while a slave has stale data but is not
2776 * allowed to serve this data. Normally no command is accepted
2777 * in this condition but just a few.
2778 *
2779 * CMD_SKIP_MONITOR: Do not automatically propagate the command on MONITOR.
2780 *
2781 * CMD_SKIP_SLOWLOG: Do not automatically propagate the command to the slowlog.
2782 *
2783 * CMD_ASKING: Perform an implicit ASKING for this command, so the
2784 * command will be accepted in cluster mode if the slot is marked
2785 * as 'importing'.
2786 *
2787 * CMD_FAST: Fast command: O(1) or O(log(N)) command that should never
2788 * delay its execution as long as the kernel scheduler is giving
2789 * us time. Note that commands that may trigger a DEL as a side
2790 * effect (like SET) are not fast commands.
2791 *
2792 * CMD_NO_AUTH: Command doesn't require authentication
2793 *
2794 * CMD_MAY_REPLICATE: Command may produce replication traffic, but should be
2795 * allowed under circumstances where write commands are disallowed.
2796 * Examples include PUBLISH, which replicates pubsub messages,and
2797 * EVAL, which may execute write commands, which are replicated,
2798 * or may just execute read commands. A command can not be marked
2799 * both CMD_WRITE and CMD_MAY_REPLICATE
2800 *
2801 * CMD_SENTINEL: This command is present in sentinel mode.
2802 *
2803 * CMD_ONLY_SENTINEL: This command is present only when in sentinel mode.
2804 * And should be removed from redis.
2805 *
2806 * CMD_NO_MANDATORY_KEYS: This key arguments for this command are optional.
2807 *
2808 * CMD_NO_MULTI: The command is not allowed inside a transaction
2809 *
2810 * CMD_ALLOW_BUSY: The command can run while another command is running for
2811 * a long time (timedout script, module command that yields)
2812 *
2813 * CMD_TOUCHES_ARBITRARY_KEYS: The command may touch (and cause lazy-expire)
2814 * arbitrary key (i.e not provided in argv)
2815 *
2816 * CMD_INTERNAL: The command may perform operations without performing
2817 * validations such as ACL.
2818 *
2819 * The following additional flags are only used in order to put commands
2820 * in a specific ACL category. Commands can have multiple ACL categories.
2821 * See redis.conf for the exact meaning of each.
2822 *
2823 * @keyspace, @read, @write, @set, @sortedset, @list, @hash, @string, @bitmap,
2824 * @hyperloglog, @stream, @admin, @fast, @slow, @pubsub, @blocking, @dangerous,
2825 * @connection, @transaction, @scripting, @geo.
2826 *
2827 * Note that:
2828 *
2829 * 1) The read-only flag implies the @read ACL category.
2830 * 2) The write flag implies the @write ACL category.
2831 * 3) The fast flag implies the @fast ACL category.
2832 * 4) The admin flag implies the @admin and @dangerous ACL category.
2833 * 5) The pub-sub flag implies the @pubsub ACL category.
2834 * 6) The lack of fast flag implies the @slow ACL category.
2835 * 7) The non obvious "keyspace" category includes the commands
2836 * that interact with keys without having anything to do with
2837 * specific data structures, such as: DEL, RENAME, MOVE, SELECT,
2838 * TYPE, EXPIRE*, PEXPIRE*, TTL, PTTL, ...
2839 */
2840struct redisCommand {
2841 /* Declarative data */
2842 const char *declared_name; /* A string representing the command declared_name.
2843 * It is a const char * for native commands and SDS for module commands. */
2844 const char *summary; /* Summary of the command (optional). */
2845 const char *complexity; /* Complexity description (optional). */
2846 const char *since; /* Debut version of the command (optional). */
2847 int doc_flags; /* Flags for documentation (see CMD_DOC_*). */
2848 const char *replaced_by; /* In case the command is deprecated, this is the successor command. */
2849 const char *deprecated_since; /* In case the command is deprecated, when did it happen? */
2850 redisCommandGroup group; /* Command group */
2851 commandHistory *history; /* History of the command */
2852 int num_history;
2853 const char **tips; /* An array of strings that are meant to be tips for clients/proxies regarding this command */
2854 int num_tips;
2855 redisCommandProc *proc; /* Command implementation */
2856 int arity; /* Number of arguments, it is possible to use -N to say >= N */
2857 uint64_t flags; /* Command flags, see CMD_*. */
2858 uint64_t acl_categories; /* ACl categories, see ACL_CATEGORY_*. */
2859 keySpec *key_specs;
2860 int key_specs_num;
2861 /* Use a function to determine keys arguments in a command line.
2862 * Used for Redis Cluster redirect (may be NULL) */
2863 redisGetKeysProc *getkeys_proc;
2864 int num_args; /* Length of args array. */
2865 /* Array of subcommands (may be NULL) */
2866 struct redisCommand *subcommands;
2867 /* Array of arguments (may be NULL) */
2868 struct redisCommandArg *args;
2869#ifdef LOG_REQ_RES
2870 /* Reply schema */
2871 struct jsonObject *reply_schema;
2872#endif
2873
2874 /* Runtime populated data */
2875 long long microseconds, calls, rejected_calls, failed_calls;
2876 int id; /* Command ID. This is a progressive ID starting from 0 that
2877 is assigned at runtime, and is used in order to check
2878 ACLs. A connection is able to execute a given command if
2879 the user associated to the connection has this command
2880 bit set in the bitmap of allowed commands. */
2881 sds fullname; /* A SDS string representing the command fullname. */
2882 struct hdr_histogram* latency_histogram; /*points to the command latency command histogram (unit of time nanosecond) */
2883 keySpec legacy_range_key_spec; /* The legacy (first,last,step) key spec is
2884 * still maintained (if applicable) so that
2885 * we can still support the reply format of
2886 * COMMAND INFO and COMMAND GETKEYS */
2887 dict *subcommands_dict; /* A dictionary that holds the subcommands, the key is the subcommand sds name
2888 * (not the fullname), and the value is the redisCommand structure pointer. */
2889 struct redisCommand *parent;
2890 struct RedisModuleCommand *module_cmd; /* A pointer to the module command data (NULL if native command) */
2891};
2892
2893struct redisError {
2894 long long count;
2895};
2896
2897struct redisFunctionSym {
2898 char *name;
2899 unsigned long pointer;
2900};
2901
2902typedef struct _redisSortObject {
2903 robj *obj;
2904 union {
2905 double score;
2906 robj *cmpobj;
2907 } u;
2908} redisSortObject;
2909
2910typedef struct _redisSortOperation {
2911 int type;
2912 robj *pattern;
2913} redisSortOperation;
2914
2915/* Structure to hold list iteration abstraction. */
2916typedef struct {
2917 robj *subject;
2918 unsigned char encoding;
2919 unsigned char direction; /* Iteration direction */
2920
2921 unsigned char *lpi; /* listpack iterator */
2922 quicklistIter iter; /* quicklist iterator */
2923} listTypeIterator;
2924
2925/* Structure for an entry while iterating over a list. */
2926typedef struct {
2927 listTypeIterator *li;
2928 unsigned char *lpe; /* Entry in listpack */
2929 quicklistEntry entry; /* Entry in quicklist */
2930} listTypeEntry;
2931
2932/* Structure to hold set iteration abstraction. */
2933typedef struct {
2934 robj *subject;
2935 int encoding;
2936 int ii; /* intset iterator */
2937 dictIterator di;
2938 unsigned char *lpi; /* listpack iterator */
2939} setTypeIterator;
2940
2941/* Structure to hold hash iteration abstraction. Note that iteration over
2942 * hashes involves both fields and values. Because it is possible that
2943 * not both are required, store pointers in the iterator to avoid
2944 * unnecessary memory allocation for fields/values. */
2945typedef struct {
2946 robj *subject;
2947 int encoding;
2948
2949 unsigned char *fptr, *vptr, *tptr;
2950 uint64_t expire_time; /* Only used with OBJ_ENCODING_LISTPACK_EX */
2951
2952 dictIterator di;
2953 dictEntry *de;
2954} hashTypeIterator;
2955
2956#include "stream.h" /* Stream data type header file. */
2957
2958#define OBJ_HASH_KEY 1
2959#define OBJ_HASH_VALUE 2
2960
2961/* Hash-field data type (of t_hash.c) - now using entry directly
2962 * Note: entry* is used directly instead of a typedef for clarity */
2963
2964/*-----------------------------------------------------------------------------
2965 * Extern declarations
2966 *----------------------------------------------------------------------------*/
2967
2968extern struct redisServer server;
2969extern struct sharedObjectsStruct shared;
2970extern dictType objectKeyPointerValueDictType;
2971extern dictType objectKeyHeapPointerValueDictType;
2972extern dictType setDictType;
2973extern dictType BenchmarkDictType;
2974extern dictType zsetDictType;
2975extern dictType dbDictType;
2976extern double R_Zero, R_PosInf, R_NegInf, R_Nan;
2977extern dictType hashDictType;
2978extern dictType entryHashDictType;
2979extern dictType entryHashDictTypeWithHFE;
2980extern dictType stringSetDictType;
2981extern dictType externalStringType;
2982extern dictType sdsHashDictType;
2983extern dictType clientDictType;
2984extern dictType objToDictDictType;
2985extern dictType dbExpiresDictType;
2986extern dictType modulesDictType;
2987extern dictType sdsReplyDictType;
2988extern dictType keylistDictType;
2989extern kvstoreType kvstoreBaseType;
2990extern kvstoreType kvstoreExType;
2991extern dict *modules;
2992
2993extern EbucketsType subexpiresBucketsType; /* global expires */
2994extern EbucketsType hashFieldExpireBucketsType; /* local per hash */
2995
2996/*-----------------------------------------------------------------------------
2997 * Functions prototypes
2998 *----------------------------------------------------------------------------*/
2999
3000/* Command metadata */
3001void populateCommandLegacyRangeSpec(struct redisCommand *c);
3002
3003/* Modules */
3004void moduleInitModulesSystem(void);
3005void moduleInitModulesSystemLast(void);
3006void modulesCron(void);
3007int moduleOnLoad(int (*onload)(void *, void **, int), const char *path, void *handle, void **module_argv, int module_argc, int is_loadex);
3008int moduleLoad(const char *path, void **argv, int argc, int is_loadex);
3009int moduleUnload(sds name, const char **errmsg, int forced_unload);
3010void moduleLoadInternalModules(void);
3011void moduleLoadFromQueue(void);
3012int moduleGetCommandKeysViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3013int moduleGetCommandChannelsViaAPI(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3014moduleType *moduleTypeLookupModuleByID(uint64_t id);
3015moduleType *moduleTypeLookupModuleByName(const char *name);
3016moduleType *moduleTypeLookupModuleByNameIgnoreCase(const char *name);
3017void moduleTypeNameByID(char *name, uint64_t moduleid);
3018const char *moduleTypeModuleName(moduleType *mt);
3019const char *moduleNameFromCommand(struct redisCommand *cmd);
3020void moduleFreeContext(struct RedisModuleCtx *ctx);
3021void moduleCallCommandUnblockedHandler(client *c);
3022int isModuleClientUnblocked(client *c);
3023void unblockClientFromModule(client *c);
3024void moduleHandleBlockedClients(void);
3025void moduleBlockedClientTimedOut(client *c);
3026void modulePipeReadable(aeEventLoop *el, int fd, void *privdata, int mask);
3027size_t moduleCount(void);
3028void moduleAcquireGIL(void);
3029int moduleTryAcquireGIL(void);
3030void moduleReleaseGIL(void);
3031void moduleNotifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);
3032void firePostExecutionUnitJobs(void);
3033void moduleCallCommandFilters(client *c);
3034void modulePostExecutionUnitOperations(void);
3035void ModuleForkDoneHandler(int exitcode, int bysignal);
3036int TerminateModuleForkChild(int child_pid, int wait);
3037ssize_t rdbSaveModulesAux(rio *rdb, int when);
3038int moduleAllDatatypesHandleErrors(void);
3039int moduleAllModulesHandleReplAsyncLoad(void);
3040sds modulesCollectInfo(sds info, dict *sections_dict, int for_crash_report, int sections);
3041void moduleFireServerEvent(uint64_t eid, int subid, void *data);
3042void processModuleLoadingProgressEvent(int is_aof);
3043int moduleTryServeClientBlockedOnKey(client *c, robj *key);
3044void moduleUnblockClient(client *c);
3045int moduleBlockedClientMayTimeout(client *c);
3046int moduleClientIsBlockedOnKeys(client *c);
3047void moduleNotifyUserChanged(client *c);
3048void moduleNotifyKeyUnlink(robj *key, kvobj *kv, int dbid, int flags);
3049size_t moduleGetFreeEffort(robj *key, robj *val, int dbid);
3050size_t moduleGetMemUsage(robj *key, robj *val, size_t sample_size, int dbid);
3051robj *moduleTypeDupOrReply(client *c, robj *fromkey, robj *tokey, int todb, robj *value);
3052int moduleDefragValue(robj *key, robj *obj, int dbid);
3053int moduleLateDefrag(robj *key, robj *value, unsigned long *cursor, monotime endtime, int dbid);
3054void moduleDefragStart(void);
3055void moduleDefragEnd(void);
3056void *moduleGetHandleByName(char *modulename);
3057int moduleIsModuleCommand(void *module_handle, struct redisCommand *cmd);
3058int moduleHasSubscribersForKeyspaceEvent(int type);
3059
3060/* pcmd */
3061void initPendingCommand(pendingCommand *pcmd);
3062void freePendingCommand(client *c, pendingCommand *pcmd);
3063void addPendingCommand(pendingCommandList *queue, pendingCommand *cmd);
3064pendingCommand *popPendingCommandFromHead(pendingCommandList *queue);
3065pendingCommand *popPendingCommandFromTail(pendingCommandList *queue);
3066void shrinkPendingCommandPool(void);
3067
3068/* Utils */
3069long long ustime(void);
3070mstime_t mstime(void);
3071mstime_t commandTimeSnapshot(void);
3072void getRandomHexChars(char *p, size_t len);
3073void getRandomBytes(unsigned char *p, size_t len);
3074uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
3075void exitFromChild(int retcode, int from_signal);
3076long long redisPopcount(void *s, long count);
3077int redisSetProcTitle(char *title);
3078int validateProcTitleTemplate(const char *template);
3079int redisCommunicateSystemd(const char *sd_notify_msg);
3080void redisSetCpuAffinity(const char *cpulist);
3081
3082/* afterErrorReply flags */
3083#define ERR_REPLY_FLAG_NO_STATS_UPDATE (1ULL<<0) /* Indicating that we should not update
3084 error stats after sending error reply */
3085/* networking.c -- Networking and Client related operations */
3086client *createClient(connection *conn);
3087void freeClient(client *c);
3088void freeClientAsync(client *c);
3089void deauthenticateAndCloseClient(client *c);
3090void logInvalidUseAndFreeClientAsync(client *c, const char *fmt, ...);
3091int beforeNextClient(client *c);
3092void clearClientConnectionState(client *c);
3093void resetClient(client *c, int num_pcmds_to_free);
3094void resetClientQbufState(client *c);
3095void freeClientOriginalArgv(client *c);
3096void freeClientArgv(client *c);
3097void freeClientPendingCommands(client *c, int num_pcmds_to_free);
3098void tryDeferFreeClientObject(client *c, int type, void *ptr);
3099void freeClientDeferredObjects(client *c, int free_array);
3100void freeClientIODeferredObjects(client *c, int free_array);
3101void sendReplyToClient(connection *conn);
3102void *addReplyDeferredLen(client *c);
3103void setDeferredArrayLen(client *c, void *node, long length);
3104void setDeferredMapLen(client *c, void *node, long length);
3105void setDeferredSetLen(client *c, void *node, long length);
3106void setDeferredAttributeLen(client *c, void *node, long length);
3107void setDeferredPushLen(client *c, void *node, long length);
3108int isClientReadErrorFatal(client *c);
3109int processInputBuffer(client *c);
3110void acceptCommonHandler(connection *conn, int flags, char *ip);
3111void readQueryFromClient(connection *conn);
3112int prepareClientToWrite(client *c);
3113void addReplyNull(client *c);
3114void addReplyNullArray(client *c);
3115void addReplyBool(client *c, int b);
3116void addReplyVerbatim(client *c, const char *s, size_t len, const char *ext);
3117void addReplyProto(client *c, const char *s, size_t len);
3118void AddReplyFromClient(client *c, client *src);
3119void addReplyBulk(client *c, robj *obj);
3120void addReplyBulkCString(client *c, const char *s);
3121void addReplyBulkCBuffer(client *c, const void *p, size_t len);
3122void addReplyBulkLongLong(client *c, long long ll);
3123void addReply(client *c, robj *obj);
3124void addReplyStatusLength(client *c, const char *s, size_t len);
3125void addReplySds(client *c, sds s);
3126void addReplyBulkSds(client *c, sds s);
3127void setDeferredReplyBulkSds(client *c, void *node, sds s);
3128void addReplyErrorObject(client *c, robj *err);
3129void addReplyOrErrorObject(client *c, robj *reply);
3130void afterErrorReply(client *c, const char *s, size_t len, int flags);
3131void addReplyErrorFormatInternal(client *c, int flags, const char *fmt, va_list ap);
3132void addReplyErrorSdsEx(client *c, sds err, int flags);
3133void addReplyErrorSds(client *c, sds err);
3134void addReplyErrorSdsSafe(client *c, sds err);
3135void addReplyError(client *c, const char *err);
3136void addReplyErrorArity(client *c);
3137void addReplyErrorExpireTime(client *c);
3138void addReplyStatus(client *c, const char *status);
3139void addReplyDouble(client *c, double d);
3140void addReplyBigNum(client *c, const char *num, size_t len);
3141void addReplyHumanLongDouble(client *c, long double d);
3142void addReplyLongLong(client *c, long long ll);
3143void addReplyLongLongFromStr(client *c, robj* str);
3144void addReplyArrayLen(client *c, long length);
3145void addReplyMapLen(client *c, long length);
3146void addReplySetLen(client *c, long length);
3147void addReplyAttributeLen(client *c, long length);
3148void addReplyPushLen(client *c, long length);
3149void addReplyHelp(client *c, const char **help);
3150void addExtendedReplyHelp(client *c, const char **help, const char **extended_help);
3151void addReplySubcommandSyntaxError(client *c);
3152void addReplyLoadedModules(client *c);
3153void copyReplicaOutputBuffer(client *dst, client *src);
3154void addListRangeReply(client *c, robj *o, long start, long end, int reverse);
3155void deferredAfterErrorReply(client *c, list *errors);
3156size_t sdsZmallocSize(sds s);
3157size_t getStringObjectSdsUsedMemory(robj *o);
3158void freeClientReplyValue(void *o);
3159void *dupClientReplyValue(void *o);
3160char *getClientPeerId(client *client);
3161char *getClientSockName(client *client);
3162sds catClientInfoString(sds s, client *client);
3163sds getAllClientsInfoString(int type);
3164int clientSetName(client *c, robj *name, const char **err);
3165void rewriteClientCommandVector(client *c, int argc, ...);
3166void rewriteClientCommandArgument(client *c, int i, robj *newval);
3167void replaceClientCommandVector(client *c, int argc, robj **argv);
3168void redactClientCommandArgument(client *c, int argc);
3169size_t getClientOutputBufferMemoryUsage(client *c);
3170size_t getNormalClientPendingReplyBytes(client *c);
3171size_t getClientMemoryUsage(client *c, size_t *output_buffer_mem_usage);
3172int freeClientsInAsyncFreeQueue(void);
3173int closeClientOnOutputBufferLimitReached(client *c, int async);
3174int getClientType(client *c);
3175int getClientTypeByName(char *name);
3176char *getClientTypeName(int class);
3177void flushSlavesOutputBuffers(void);
3178void disconnectSlaves(void);
3179void evictClients(void);
3180int listenToPort(connListener *fds);
3181void pauseActions(pause_purpose purpose, mstime_t end, uint32_t actions_bitmask);
3182void unpauseActions(pause_purpose purpose);
3183uint32_t isPausedActions(uint32_t action_bitmask);
3184uint32_t isPausedActionsWithUpdate(uint32_t action_bitmask);
3185void updatePausedActions(void);
3186void unblockPostponedClients(void);
3187void processEventsWhileBlocked(void);
3188void whileBlockedCron(void);
3189void blockingOperationStarts(void);
3190void blockingOperationEnds(void);
3191int handleClientsWithPendingWrites(void);
3192int clientHasPendingReplies(client *c);
3193int updateClientMemUsageAndBucket(client *c);
3194void removeClientFromMemUsageBucket(client *c, int allow_eviction);
3195void unlinkClient(client *c);
3196void tryUnlinkClientFromPendingRefReply(client *c, int force);
3197int writeToClient(client *c, int handler_installed);
3198void linkClient(client *c);
3199void protectClient(client *c);
3200void unprotectClient(client *c);
3201client *lookupClientByID(uint64_t id);
3202int authRequired(client *c);
3203void putClientInPendingWriteQueue(client *c);
3204getKeysResult *getClientCachedKeyResult(client *c);
3205/* reply macros */
3206#define ADD_REPLY_BULK_CBUFFER_STRING_CONSTANT(c, str) addReplyBulkCBuffer(c, str, strlen(str))
3207
3208/* iothread.c - the threaded io implementation */
3209void initThreadedIO(void);
3210void killIOThreads(void);
3211void pauseIOThread(int id);
3212void resumeIOThread(int id);
3213void pauseAllIOThreads(void);
3214void resumeAllIOThreads(void);
3215void pauseIOThreadsRange(int start, int end);
3216void resumeIOThreadsRange(int start, int end);
3217int resizeAllIOThreadsEventLoops(size_t newsize);
3218int sendPendingClientsToIOThreads(void);
3219void enqueuePendingClientsToMainThread(client *c, int unbind);
3220void enqueuePendingClienstToIOThreads(client *c);
3221void handleClientReadError(client *c);
3222void unbindClientFromIOThreadEventLoop(client *c);
3223int processClientsOfAllIOThreads(void);
3224int processClientsFromMainThread(IOThread *t);
3225void assignClientToIOThread(client *c);
3226void keepClientInMainThread(client *c);
3227void fetchClientFromIOThread(client *c);
3228int isClientMustHandledByMainThread(client *c);
3229
3230/* logreqres.c - logging of requests and responses */
3231void reqresReset(client *c, int free_buf);
3232void reqresSaveClientReplyOffset(client *c);
3233size_t reqresAppendRequest(client *c);
3234size_t reqresAppendResponse(client *c);
3235
3236#ifdef __GNUC__
3237void addReplyErrorFormatEx(client *c, int flags, const char *fmt, ...)
3238 __attribute__((format(printf, 3, 4)));
3239void addReplyErrorFormat(client *c, const char *fmt, ...)
3240 __attribute__((format(printf, 2, 3)));
3241void addReplyStatusFormat(client *c, const char *fmt, ...)
3242 __attribute__((format(printf, 2, 3)));
3243#else
3244void addReplyErrorFormatEx(client *c, int flags, const char *fmt, ...);
3245void addReplyErrorFormat(client *c, const char *fmt, ...);
3246void addReplyStatusFormat(client *c, const char *fmt, ...);
3247#endif
3248
3249/* Client side caching (tracking mode) */
3250void enableTracking(client *c, uint64_t redirect_to, uint64_t options, robj **prefix, size_t numprefix);
3251void disableTracking(client *c);
3252void trackingRememberKeys(client *tracking, client *executing);
3253void trackingInvalidateKey(client *c, robj *keyobj, int bcast);
3254void trackingScheduleKeyInvalidation(uint64_t client_id, robj *keyobj);
3255void trackingHandlePendingKeyInvalidations(void);
3256void trackingInvalidateKeysOnFlush(int async);
3257void freeTrackingRadixTree(rax *rt);
3258void freeTrackingRadixTreeAsync(rax *rt);
3259void freeErrorsRadixTreeAsync(rax *errors);
3260void trackingLimitUsedSlots(void);
3261uint64_t trackingGetTotalItems(void);
3262uint64_t trackingGetTotalKeys(void);
3263uint64_t trackingGetTotalPrefixes(void);
3264void trackingBroadcastInvalidationMessages(void);
3265int checkPrefixCollisionsOrReply(client *c, robj **prefix, size_t numprefix);
3266
3267/* List data type */
3268void listTypePush(robj *subject, robj *value, int where);
3269robj *listTypePop(robj *subject, int where);
3270unsigned long listTypeLength(const robj *subject);
3271size_t listTypeAllocSize(const robj *o);
3272void listTypeInitIterator(listTypeIterator *li, robj *subject, long index, unsigned char direction);
3273void listTypeResetIterator(listTypeIterator *li);
3274void listTypeSetIteratorDirection(listTypeIterator *li, listTypeEntry *entry, unsigned char direction);
3275int listTypeNext(listTypeIterator *li, listTypeEntry *entry);
3276robj *listTypeGet(listTypeEntry *entry);
3277unsigned char *listTypeGetValue(listTypeEntry *entry, size_t *vlen, long long *lval);
3278void listTypeInsert(listTypeEntry *entry, robj *value, int where);
3279void listTypeReplace(listTypeEntry *entry, robj *value);
3280int listTypeEqual(listTypeEntry *entry, robj *o, size_t object_len,
3281 long long *cached_longval, int *cached_valid);
3282void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry);
3283robj *listTypeDup(robj *o);
3284void listTypeDelRange(robj *o, long start, long stop);
3285void popGenericCommand(client *c, int where);
3286void listElementsRemoved(client *c, robj *key, int where, robj *o, long count, size_t oldsize, int signal, int *deleted);
3287typedef enum {
3288 LIST_CONV_AUTO,
3289 LIST_CONV_GROWING,
3290 LIST_CONV_SHRINKING,
3291} list_conv_type;
3292typedef void (*beforeConvertCB)(void *data);
3293void listTypeTryConversion(robj *o, list_conv_type lct, beforeConvertCB fn, void *data);
3294void listTypeTryConversionAppend(robj *o, robj **argv, int start, int end, beforeConvertCB fn, void *data);
3295
3296/* MULTI/EXEC/WATCH... */
3297void unwatchAllKeys(client *c);
3298void initClientMultiState(client *c);
3299void freeClientMultiState(client *c);
3300void queueMultiCommand(client *c, uint64_t cmd_flags);
3301size_t multiStateMemOverhead(client *c);
3302void touchWatchedKey(redisDb *db, robj *key);
3303int isWatchedKeyExpired(client *c);
3304void touchAllWatchedKeysInDb(redisDb *emptied, redisDb *replaced_with, struct slotRangeArray *slots);
3305void discardTransaction(client *c);
3306void flagTransaction(client *c);
3307void execCommandAbort(client *c, sds error);
3308
3309unsigned char *getObjectReadOnlyString(robj *o, long *len, char *llbuf);
3310
3311unsigned long long estimateObjectIdleTime(robj *o);
3312#define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR)
3313
3314/* Synchronous I/O with timeout */
3315ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout);
3316ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout);
3317ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);
3318
3319/* Replication */
3320void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
3321void replicationFeedStreamFromMasterStream(char *buf, size_t buflen);
3322void resetReplicationBuffer(void);
3323void feedReplicationBuffer(char *buf, size_t len);
3324void freeReplicaReferencedReplBuffer(client *replica);
3325void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc);
3326void updateSlavesWaitingBgsave(int bgsaveerr, int type);
3327void replicationCron(void);
3328void replicationStartPendingFork(void);
3329void replicationHandleMasterDisconnection(void);
3330void replicationCacheMaster(client *c);
3331void resizeReplicationBacklog(void);
3332void replicationSetMaster(char *ip, int port);
3333void replicationUnsetMaster(void);
3334void refreshGoodSlavesCount(void);
3335int checkGoodReplicasStatus(void);
3336void processClientsWaitingReplicas(void);
3337void unblockClientWaitingReplicas(client *c);
3338int replicationCountAcksByOffset(long long offset);
3339int replicationCountAOFAcksByOffset(long long offset);
3340void replicationSendNewlineToMaster(void);
3341long long replicationGetSlaveOffset(void);
3342char *replicationGetSlaveName(client *c);
3343long long getPsyncInitialOffset(void);
3344int replicationSetupSlaveForFullResync(client *slave, long long offset);
3345void changeReplicationId(void);
3346void clearReplicationId2(void);
3347void createReplicationBacklog(void);
3348void freeReplicationBacklog(void);
3349void replicationCacheMasterUsingMyself(void);
3350void feedReplicationBacklog(void *ptr, size_t len);
3351void incrementalTrimReplicationBacklog(size_t blocks);
3352int canFeedReplicaReplBuffer(client *replica);
3353void rebaseReplicationBuffer(long long base_repl_offset);
3354void showLatestBacklog(void);
3355void rdbPipeReadHandler(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
3356void rdbPipeWriteHandlerConnRemoved(struct connection *conn);
3357void clearFailoverState(void);
3358void updateFailoverStatus(void);
3359void abortFailover(const char *err);
3360const char *getFailoverStateString(void);
3361int replicationCheckHasMainChannel(client *slave);
3362unsigned long replicationLogicalReplicaCount(void);
3363void replDataBufInit(replDataBuf *buf);
3364void replDataBufClear(replDataBuf *buf);
3365void replDataBufReadFromConn(connection *conn, replDataBuf *buf, void (*error_handler)(connection *conn));
3366int replDataBufStreamToDb(replDataBuf *buf, replDataBufToDbCtx *ctx);
3367int replicaFromIOThreadHasPendingRead(client *c);
3368void putReplicasInPendingClientsToIOThreads(void);
3369int replicationCronRunMasterClient(void);
3370
3371/* Generic persistence functions */
3372void startLoadingFile(size_t size, char* filename, int rdbflags);
3373void startLoading(size_t size, int rdbflags, int async);
3374void loadingSetFlags(char *filename, size_t size, int async);
3375void loadingFireEvent(int rdbflags);
3376void loadingAbsProgress(off_t pos);
3377void loadingIncrProgress(off_t size);
3378void stopLoading(int success);
3379void updateLoadingFileName(char* filename);
3380void startSaving(int rdbflags);
3381void stopSaving(int success);
3382int allPersistenceDisabled(void);
3383
3384#define DISK_ERROR_TYPE_AOF 1 /* Don't accept writes: AOF errors. */
3385#define DISK_ERROR_TYPE_RDB 2 /* Don't accept writes: RDB errors. */
3386#define DISK_ERROR_TYPE_NONE 0 /* No problems, we can accept writes. */
3387int writeCommandsDeniedByDiskError(void);
3388sds writeCommandsGetDiskErrorMessage(int);
3389
3390/* RDB persistence */
3391#include "rdb.h"
3392void killRDBChild(void);
3393int bg_unlink(const char *filename);
3394
3395/* AOF persistence */
3396void flushAppendOnlyFile(int force);
3397void feedAppendOnlyFile(int dictid, robj **argv, int argc);
3398void aofRemoveTempFile(pid_t childpid);
3399int rewriteAppendOnlyFileBackground(void);
3400int loadAppendOnlyFiles(aofManifest *am);
3401void stopAppendOnly(void);
3402int startAppendOnly(void);
3403void startAppendOnlyWithRetry(void);
3404void applyAppendOnlyConfig(void);
3405void backgroundRewriteDoneHandler(int exitcode, int bysignal);
3406void killAppendOnlyChild(void);
3407void aofLoadManifestFromDisk(void);
3408void aofOpenIfNeededOnServerStart(void);
3409void aofManifestFree(aofManifest *am);
3410int aofDelHistoryFiles(void);
3411int aofRewriteLimited(void);
3412void updateCurIncrAofEndOffset(void);
3413void updateReplOffsetAndResetEndOffset(void);
3414int rewriteObject(rio *r, robj *key, robj *o, int dbid, long long expiretime);
3415
3416/* Child info */
3417void openChildInfoPipe(void);
3418void closeChildInfoPipe(void);
3419void sendChildInfoGeneric(childInfoType info_type, size_t keys, double progress, char *pname);
3420void sendChildCowInfo(childInfoType info_type, char *pname);
3421void sendChildInfo(childInfoType info_type, size_t keys, char *pname);
3422void receiveChildInfo(void);
3423
3424/* Fork helpers */
3425int redisFork(int purpose);
3426int hasActiveChildProcess(void);
3427void resetChildState(void);
3428int isMutuallyExclusiveChildType(int type);
3429
3430/* acl.c -- Authentication related prototypes. */
3431extern rax *Users;
3432extern user *DefaultUser;
3433void ACLInit(void);
3434/* Return values for ACLCheckAllPerm(). */
3435#define ACL_OK 0
3436#define ACL_DENIED_CMD 1
3437#define ACL_DENIED_KEY 2
3438#define ACL_DENIED_AUTH 3 /* Only used for ACL LOG entries. */
3439#define ACL_DENIED_CHANNEL 4 /* Only used for pub/sub commands */
3440#define ACL_INVALID_TLS_CERT_AUTH 5 /* Only used for TLS Auto-authentication */
3441
3442/* Context values for addACLLogEntry(). */
3443#define ACL_LOG_CTX_TOPLEVEL 0
3444#define ACL_LOG_CTX_LUA 1
3445#define ACL_LOG_CTX_MULTI 2
3446#define ACL_LOG_CTX_MODULE 3
3447
3448/* ACL key permission types */
3449#define ACL_READ_PERMISSION (1<<0)
3450#define ACL_WRITE_PERMISSION (1<<1)
3451#define ACL_ALL_PERMISSION (ACL_READ_PERMISSION|ACL_WRITE_PERMISSION)
3452
3453/* Return codes for Authentication functions to indicate the result. */
3454typedef enum {
3455 AUTH_OK = 0,
3456 AUTH_ERR,
3457 AUTH_NOT_HANDLED,
3458 AUTH_BLOCKED
3459} AuthResult;
3460
3461int ACLCheckUserCredentials(robj *username, robj *password);
3462int ACLAuthenticateUser(client *c, robj *username, robj *password, robj **err);
3463int checkModuleAuthentication(client *c, robj *username, robj *password, robj **err);
3464void addAuthErrReply(client *c, robj *err);
3465unsigned long ACLGetCommandID(sds cmdname);
3466void ACLClearCommandID(void);
3467user *ACLGetUserByName(const char *name, size_t namelen);
3468int ACLUserCheckKeyPerm(user *u, const char *key, int keylen, int flags);
3469int ACLUserCheckChannelPerm(user *u, sds channel, int literal);
3470int ACLCheckAllUserCommandPerm(user *u, struct redisCommand *cmd, robj **argv, int argc, getKeysResult *key_result, int *idxptr);
3471int ACLUserCheckCmdWithUnrestrictedKeyAccess(user *u, struct redisCommand *cmd, robj **argv, int argc, int flags);
3472int ACLCheckAllPerm(client *c, int *idxptr);
3473int ACLSetUser(user *u, const char *op, ssize_t oplen);
3474sds ACLStringSetUser(user *u, sds username, sds *argv, int argc);
3475uint64_t ACLGetCommandCategoryFlagByName(const char *name);
3476int ACLAddCommandCategory(const char *name, uint64_t flag);
3477void ACLCleanupCategoriesOnFailure(size_t num_acl_categories_added);
3478int ACLAppendUserForLoading(sds *argv, int argc, int *argc_err);
3479const char *ACLSetUserStringError(void);
3480int ACLLoadConfiguredUsers(void);
3481robj *ACLDescribeUser(user *u);
3482void ACLLoadUsersAtStartup(void);
3483void addReplyCommandCategories(client *c, struct redisCommand *cmd);
3484user *ACLCreateUnlinkedUser(void);
3485void ACLFreeUserAndKillClients(user *u);
3486void addACLLogEntry(client *c, int reason, int context, int argpos, sds username, sds object);
3487sds getAclErrorMessage(int acl_res, user *user, struct redisCommand *cmd, sds errored_val, int verbose);
3488void ACLUpdateDefaultUserPassword(sds password);
3489sds genRedisInfoStringACLStats(sds info);
3490void ACLRecomputeCommandBitsFromCommandRulesAllUsers(void);
3491
3492/* Sorted sets data type */
3493
3494/* Input flags. */
3495#define ZADD_IN_NONE 0
3496#define ZADD_IN_INCR (1<<0) /* Increment the score instead of setting it. */
3497#define ZADD_IN_NX (1<<1) /* Don't touch elements not already existing. */
3498#define ZADD_IN_XX (1<<2) /* Only touch elements already existing. */
3499#define ZADD_IN_GT (1<<3) /* Only update existing when new scores are higher. */
3500#define ZADD_IN_LT (1<<4) /* Only update existing when new scores are lower. */
3501
3502/* Output flags. */
3503#define ZADD_OUT_NOP (1<<0) /* Operation not performed because of conditionals.*/
3504#define ZADD_OUT_NAN (1<<1) /* Only touch elements already existing. */
3505#define ZADD_OUT_ADDED (1<<2) /* The element was new and was added. */
3506#define ZADD_OUT_UPDATED (1<<3) /* The element already existed, score updated. */
3507
3508/* Struct to hold an inclusive/exclusive range spec by score comparison. */
3509typedef struct {
3510 double min, max;
3511 int minex, maxex; /* are min or max exclusive? */
3512} zrangespec;
3513
3514/* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */
3515typedef struct {
3516 sds min, max; /* May be set to shared.(minstring|maxstring) */
3517 int minex, maxex; /* are min or max exclusive? */
3518} zlexrangespec;
3519
3520/* flags for incrCommandFailedCalls */
3521#define ERROR_COMMAND_REJECTED (1<<0) /* Indicate to update the command rejected stats */
3522#define ERROR_COMMAND_FAILED (1<<1) /* Indicate to update the command failed stats */
3523
3524zskiplist *zslCreate(void);
3525void zslFree(zskiplist *zsl);
3526size_t zslAllocSize(const zskiplist *zsl);
3527sds zslGetNodeElement(const zskiplistNode *node);
3528int zslCompareWithNode(double score, sds ele, const zskiplistNode *n);
3529zskiplistNode *zslInsert(zskiplist *zsl, double score, sds ele);
3530unsigned char *zzlInsert(unsigned char *zl, sds ele, double score);
3531zskiplistNode *zslNthInRange(zskiplist *zsl, zrangespec *range, long n, unsigned long *out_rank);
3532double zzlGetScore(unsigned char *sptr);
3533void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
3534void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
3535unsigned char *zzlFirstInRange(unsigned char *zl, zrangespec *range);
3536unsigned char *zzlLastInRange(unsigned char *zl, zrangespec *range);
3537unsigned long zsetLength(const robj *zobj);
3538size_t zsetAllocSize(const robj *o);
3539void zsetConvert(robj *zobj, int encoding);
3540void zsetConvertToListpackIfNeeded(robj *zobj, size_t maxelelen, size_t totelelen);
3541int zsetScore(robj *zobj, sds member, double *score);
3542unsigned long zslGetRank(zskiplist *zsl, double score, sds o);
3543int zsetAdd(robj *zobj, double score, sds ele, int in_flags, int *out_flags, double *newscore);
3544long zsetRank(robj *zobj, sds ele, int reverse, double *score);
3545int zsetDel(robj *zobj, sds ele);
3546robj *zsetDup(robj *o);
3547void genericZpopCommand(client *c, robj **keyv, int keyc, int where, int emitkey, long count, int use_nested_array, int reply_nil_when_empty, int *deleted);
3548sds lpGetObject(unsigned char *sptr);
3549int zslValueGteMin(double value, zrangespec *spec);
3550int zslValueLteMax(double value, zrangespec *spec);
3551void zslFreeLexRange(zlexrangespec *spec);
3552int zslParseLexRange(robj *min, robj *max, zlexrangespec *spec);
3553unsigned char *zzlFirstInLexRange(unsigned char *zl, zlexrangespec *range);
3554unsigned char *zzlLastInLexRange(unsigned char *zl, zlexrangespec *range);
3555zskiplistNode *zslNthInLexRange(zskiplist *zsl, zlexrangespec *range, long n, unsigned long *out_rank);
3556int zzlLexValueGteMin(unsigned char *p, zlexrangespec *spec);
3557int zzlLexValueLteMax(unsigned char *p, zlexrangespec *spec);
3558int zslLexValueGteMin(sds value, zlexrangespec *spec);
3559int zslLexValueLteMax(sds value, zlexrangespec *spec);
3560
3561/* Core functions */
3562int getMaxmemoryState(size_t *total, size_t *logical, size_t *tofree, float *level);
3563void updatePeakMemory(void);
3564size_t freeMemoryGetNotCountedMemory(void);
3565int overMaxmemoryAfterAlloc(size_t moremem);
3566uint64_t getCommandFlags(client *c);
3567void preprocessCommand(client *c, pendingCommand *pcmd);
3568int processCommand(client *c);
3569void commandProcessed(client *c);
3570void prepareForNextCommand(client *c, int update_slot_stats);
3571int processPendingCommandAndInputBuffer(client *c);
3572int processCommandAndResetClient(client *c);
3573int areCommandKeysInSameSlot(client *c, int *hashslot);
3574void setupSignalHandlers(void);
3575int createSocketAcceptHandler(connListener *sfd, aeFileProc *accept_handler);
3576connListener *listenerByType(const char *typename);
3577int changeListener(connListener *listener);
3578void closeListener(connListener *listener);
3579struct redisCommand *lookupSubcommand(struct redisCommand *container, sds sub_name);
3580struct redisCommand *lookupCommand(robj **argv, int argc);
3581struct redisCommand *lookupCommandBySdsLogic(dict *commands, sds s);
3582struct redisCommand *lookupCommandBySds(sds s);
3583struct redisCommand *lookupCommandByCStringLogic(dict *commands, const char *s);
3584struct redisCommand *lookupCommandByCString(const char *s);
3585struct redisCommand *lookupCommandOrOriginal(robj **argv, int argc);
3586int commandCheckExistence(client *c, sds *err);
3587int commandCheckArity(struct redisCommand *cmd, int argc, sds *err);
3588void startCommandExecution(void);
3589int incrCommandStatsOnError(struct redisCommand *cmd, int flags);
3590void call(client *c, int flags);
3591void alsoPropagate(int dbid, robj **argv, int argc, int target);
3592void postExecutionUnitOperations(void);
3593int redisOpArrayAppend(redisOpArray *oa, int dbid, robj **argv, int argc, int target);
3594void redisOpArrayFree(redisOpArray *oa);
3595void forceCommandPropagation(client *c, int flags);
3596void preventCommandPropagation(client *c);
3597void preventCommandAOF(client *c);
3598void preventCommandReplication(client *c);
3599void slowlogPushCurrentCommand(client *c, struct redisCommand *cmd, ustime_t duration);
3600void updateCommandLatencyHistogram(struct hdr_histogram** latency_histogram, int64_t duration_hist);
3601int prepareForShutdown(int flags);
3602void replyToClientsBlockedOnShutdown(void);
3603int abortShutdown(void);
3604void afterCommand(client *c);
3605int mustObeyClient(client *c);
3606#ifdef __GNUC__
3607void _serverLog(int level, const char *fmt, ...)
3608 __attribute__((format(printf, 2, 3)));
3609void serverLogFromHandler(int level, const char *fmt, ...)
3610 __attribute__((format(printf, 2, 3)));
3611#else
3612void serverLogFromHandler(int level, const char *fmt, ...);
3613void _serverLog(int level, const char *fmt, ...);
3614#endif
3615void serverLogRaw(int level, const char *msg);
3616void serverLogRawFromHandler(int level, const char *msg);
3617void usage(void);
3618void updateDictResizePolicy(void);
3619void populateCommandTable(void);
3620void resetCommandTableStats(dict* commands);
3621void resetErrorTableStats(void);
3622void adjustOpenFilesLimit(void);
3623void incrementErrorCount(const char *fullerr, size_t namelen);
3624void closeListeningSockets(int unlink_unix_socket);
3625void updateCachedTime(int update_daylight_info);
3626void enterExecutionUnit(int update_cached_time, long long us);
3627void exitExecutionUnit(void);
3628void resetServerStats(void);
3629void activeDefragCycle(void);
3630void defragWhileBlocked(void);
3631unsigned int getLRUClock(void);
3632unsigned int LRU_CLOCK(void);
3633const char *evictPolicyToString(void);
3634struct redisMemOverhead *getMemoryOverheadData(void);
3635void freeMemoryOverheadData(struct redisMemOverhead *mh);
3636void checkChildrenDone(void);
3637int setOOMScoreAdj(int process_class);
3638void rejectCommandFormat(client *c, const char *fmt, ...);
3639void *activeDefragAlloc(void *ptr);
3640void *activeDefragAllocRaw(size_t size);
3641void activeDefragFreeRaw(void *ptr);
3642robj *activeDefragStringOb(robj* ob);
3643void dismissSds(sds s);
3644void dismissMemory(void* ptr, size_t size_hint);
3645void dismissMemoryInChild(void);
3646int clientsCronRunClient(client *c);
3647
3648#define RESTART_SERVER_NONE 0
3649#define RESTART_SERVER_GRACEFULLY (1<<0) /* Do proper shutdown. */
3650#define RESTART_SERVER_CONFIG_REWRITE (1<<1) /* CONFIG REWRITE before restart.*/
3651int restartServer(int flags, mstime_t delay);
3652int getKeySlot(sds key);
3653int calculateKeySlot(sds key);
3654
3655/* kvstore wrappers */
3656int dbExpand(redisDb *db, uint64_t db_size, int try_expand);
3657int dbExpandExpires(redisDb *db, uint64_t db_size, int try_expand);
3658kvobj *dbFind(redisDb *db, sds key);
3659kvobj *dbFindByLink(redisDb *db, sds key, dictEntryLink *link);
3660kvobj *dbFindExpires(redisDb *db, sds key);
3661unsigned long long dbSize(redisDb *db);
3662unsigned long long dbScan(redisDb *db, unsigned long long cursor, dictScanFunction *scan_cb, void *privdata);
3663
3664/* Set data type */
3665robj *setTypeCreate(sds value, size_t size_hint);
3666int setTypeAdd(robj *subject, sds value);
3667int setTypeAddAux(robj *set, char *str, size_t len, int64_t llval, int str_is_sds);
3668int setTypeRemove(robj *subject, sds value);
3669int setTypeRemoveAux(robj *set, char *str, size_t len, int64_t llval, int str_is_sds);
3670int setTypeIsMember(robj *subject, sds value);
3671int setTypeIsMemberAux(robj *set, char *str, size_t len, int64_t llval, int str_is_sds);
3672void setTypeInitIterator(setTypeIterator *si, robj *subject);
3673void setTypeResetIterator(setTypeIterator *si);
3674int setTypeNext(setTypeIterator *si, char **str, size_t *len, int64_t *llele);
3675sds setTypeNextObject(setTypeIterator *si);
3676int setTypeRandomElement(robj *setobj, char **str, size_t *len, int64_t *llele);
3677unsigned long setTypeSize(const robj *subject);
3678size_t setTypeAllocSize(const robj *o);
3679void setTypeConvert(robj *subject, int enc);
3680int setTypeConvertAndExpand(robj *setobj, int enc, unsigned long cap, int panic);
3681robj *setTypeDup(robj *o);
3682
3683/* Data structure for OBJ_ENCODING_LISTPACK_EX for hash. It contains listpack
3684 * and metadata fields for hash field expiration.*/
3685typedef struct listpackEx {
3686 ExpireMeta meta; /* To be used in order to register the hash in the
3687 global ebuckets subexpires with next, minimum,
3688 hash-field to expire. TTL value might be inaccurate
3689 up-to few seconds due to optimization consideration. */
3690 void *lp; /* listpack that contains 'key-value-ttl' tuples which
3691 are ordered by ttl. */
3692} listpackEx;
3693
3694/* Each dict of hash object that has fields with time-Expiration will have the
3695 * following metadata attached to dict header.
3696 * Note that alloc_size field must be first because hash objects without expre
3697 * already use sizeof(size_t) bytes of metadata for memory accounting. */
3698typedef struct htMetadataEx {
3699 size_t alloc_size; /* Total memory used for keys and values */
3700 ExpireMeta expireMeta; /* embedded ExpireMeta in dict.
3701 To be used in order to register the hash in the
3702 subexpires DB with next minimum hash-field to expire.
3703 TTL value might be inaccurate up-to few seconds due
3704 to optimization consideration. */
3705 ebuckets hfe; /* DS of Hash Fields Expiration, associated to each hash */
3706} htMetadataEx;
3707
3708/* hash metadata helpers */
3709static inline htMetadataEx *htGetMetadataEx(dict *d) {
3710 return (htMetadataEx *)dictMetadata(d);
3711}
3712
3713static inline size_t *htGetMetadataSize(dict *d) {
3714 return (size_t *)dictMetadata(d);
3715}
3716
3717/* Hash data type */
3718#define HASH_SET_TAKE_FIELD (1<<0)
3719#define HASH_SET_TAKE_VALUE (1<<1)
3720#define HASH_SET_COPY 0
3721
3722/* Hash field lazy expiration flags. Used by core hashTypeGetValue() and its callers */
3723#define HFE_LAZY_EXPIRE (0) /* Delete expired field, and if last field also the hash */
3724#define HFE_LAZY_AVOID_FIELD_DEL (1<<0) /* Avoid deleting expired field */
3725#define HFE_LAZY_AVOID_HASH_DEL (1<<1) /* Avoid deleting hash if the field is the last one */
3726#define HFE_LAZY_NO_NOTIFICATION (1<<2) /* Do not send notification, used when multiple fields
3727 * may expire and only one notification is desired. */
3728#define HFE_LAZY_NO_SIGNAL (1<<3) /* Do not send signal, used when multiple fields
3729 * may expire and only one signal is desired. */
3730#define HFE_LAZY_ACCESS_EXPIRED (1<<4) /* Avoid lazy expire and allow access to expired fields */
3731#define HFE_LAZY_NO_UPDATE_KEYSIZES (1<<5) /* If field lazy deleted, avoid updating keysizes histogram */
3732#define HFE_LAZY_NO_UPDATE_ALLOCSIZES (1<<6) /* If field lazy deleted, avoid updating slot allocation sizes */
3733
3734void hashTypeConvert(redisDb *db, robj *o, int enc);
3735void hashTypeTryConversion(redisDb *db, kvobj *kv, robj **argv, int start, int end);
3736int hashTypeExists(redisDb *db, kvobj *kv, sds field, int hfeFlags, int *isHashDeleted);
3737int hashTypeDelete(robj *o, void *key);
3738unsigned long hashTypeLength(const robj *o, int subtractExpiredFields);
3739size_t hashTypeAllocSize(const robj *o);
3740void hashTypeInitIterator(hashTypeIterator *hi, robj *subject);
3741void hashTypeResetIterator(hashTypeIterator *hi);
3742int hashTypeNext(hashTypeIterator *hi, int skipExpiredFields);
3743void hashTypeCurrentFromListpack(hashTypeIterator *hi, int what,
3744 unsigned char **vstr,
3745 unsigned int *vlen,
3746 long long *vll,
3747 uint64_t *expireTime);
3748void hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what, char **str,
3749 size_t *len, uint64_t *expireTime);
3750void hashTypeCurrentObject(hashTypeIterator *hi, int what, unsigned char **vstr,
3751 unsigned int *vlen, long long *vll, uint64_t *expireTime);
3752sds hashTypeCurrentObjectNewSds(hashTypeIterator *hi, int what);
3753Entry *hashTypeCurrentObjectNewEntry(hashTypeIterator *hi, size_t *usable);
3754int hashTypeGetValueObject(redisDb *db, kvobj *kv, sds field, int hfeFlags,
3755 robj **val, uint64_t *expireTime, int *isHashDeleted);
3756int hashTypeSet(redisDb *db, kvobj *kv, sds field, sds value, int flags);
3757robj *hashTypeDup(kvobj *kv, uint64_t *minHashExpire);
3758uint64_t hashTypeActiveExpire(redisDb *db, kvobj *o, uint32_t *quota, int updateSubexpires);
3759void hashTypeFree(robj *o);
3760int hashTypeIsExpired(const robj *o, uint64_t expireAt);
3761unsigned char *hashTypeListpackGetLp(robj *o);
3762uint64_t hashTypeGetMinExpire(robj *o, int accurate);
3763ebuckets *hashTypeGetDictMetaHFE(dict *d);
3764void initDictExpireMetadata(robj *o);
3765struct listpackEx *listpackExCreate(void);
3766void listpackExAddNew(robj *o, char *field, size_t flen,
3767 char *value, size_t vlen, uint64_t expireAt);
3768
3769/* Pub / Sub */
3770int pubsubUnsubscribeAllChannels(client *c, int notify);
3771int pubsubUnsubscribeShardAllChannels(client *c, int notify);
3772void pubsubShardUnsubscribeAllChannelsInSlot(unsigned int slot);
3773int pubsubUnsubscribeAllPatterns(client *c, int notify);
3774int pubsubPublishMessage(robj *channel, robj *message, int sharded);
3775int pubsubPublishMessageAndPropagateToCluster(robj *channel, robj *message, int sharded);
3776void addReplyPubsubMessage(client *c, robj *channel, robj *msg, robj *message_bulk);
3777int serverPubsubSubscriptionCount(void);
3778int serverPubsubShardSubscriptionCount(void);
3779size_t pubsubMemOverhead(client *c);
3780void unmarkClientAsPubSub(client *c);
3781int pubsubTotalSubscriptions(void);
3782dict *getClientPubSubChannels(client *c);
3783dict *getClientPubSubShardChannels(client *c);
3784
3785/* Keyspace events notification */
3786void notifyKeyspaceEvent(int type, const char *event, robj *key, int dbid);
3787int keyspaceEventsStringToFlags(char *classes);
3788sds keyspaceEventsFlagsToString(int flags);
3789
3790/* Configuration */
3791/* Configuration Flags */
3792#define MODIFIABLE_CONFIG 0 /* This is the implied default for a standard
3793 * config, which is mutable. */
3794#define IMMUTABLE_CONFIG (1ULL<<0) /* Can this value only be set at startup? */
3795#define SENSITIVE_CONFIG (1ULL<<1) /* Does this value contain sensitive information */
3796#define DEBUG_CONFIG (1ULL<<2) /* Values that are useful for debugging. */
3797#define MULTI_ARG_CONFIG (1ULL<<3) /* This config receives multiple arguments. */
3798#define HIDDEN_CONFIG (1ULL<<4) /* This config is hidden in `config get <pattern>` (used for tests/debugging) */
3799#define PROTECTED_CONFIG (1ULL<<5) /* Becomes immutable if enable-protected-configs is enabled. */
3800#define DENY_LOADING_CONFIG (1ULL<<6) /* This config is forbidden during loading. */
3801#define ALIAS_CONFIG (1ULL<<7) /* For configs with multiple names, this flag is set on the alias. */
3802#define MODULE_CONFIG (1ULL<<8) /* This config is a module config */
3803#define VOLATILE_CONFIG (1ULL<<9) /* The config is a reference to the config data and not the config data itself (ex.
3804 * a file name containing more configuration like a tls key). In this case we want
3805 * to apply the configuration change even if the new config value is the same as
3806 * the old. */
3807
3808#define INTEGER_CONFIG 0 /* No flags means a simple integer configuration */
3809#define MEMORY_CONFIG (1<<0) /* Indicates if this value can be loaded as a memory value */
3810#define PERCENT_CONFIG (1<<1) /* Indicates if this value can be loaded as a percent (and stored as a negative int) */
3811#define OCTAL_CONFIG (1<<2) /* This value uses octal representation */
3812
3813/* Enum Configs contain an array of configEnum objects that match a string with an integer. */
3814typedef struct configEnum {
3815 char *name;
3816 int val;
3817} configEnum;
3818
3819/* Type of configuration. */
3820typedef enum {
3821 BOOL_CONFIG,
3822 NUMERIC_CONFIG,
3823 STRING_CONFIG,
3824 SDS_CONFIG,
3825 ENUM_CONFIG,
3826 SPECIAL_CONFIG,
3827} configType;
3828
3829void loadServerConfig(char *filename, char config_from_stdin, char *options);
3830void appendServerSaveParams(time_t seconds, int changes);
3831void resetServerSaveParams(void);
3832struct rewriteConfigState; /* Forward declaration to export API. */
3833int rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force);
3834void rewriteConfigMarkAsProcessed(struct rewriteConfigState *state, const char *option);
3835int rewriteConfig(char *path, int force_write);
3836void initConfigValues(void);
3837void removeConfig(sds name);
3838sds getConfigDebugInfo(void);
3839int allowProtectedAction(int config, client *c);
3840void initServerClientMemUsageBuckets(void);
3841void freeServerClientMemUsageBuckets(void);
3842static inline int clusterSlotStatsEnabled(int stat) { return server.cluster_enabled && (server.cluster_slot_stats_enabled & stat); }
3843
3844/* Module Configuration */
3845typedef struct ModuleConfig ModuleConfig;
3846int performModuleConfigSetFromName(sds name, sds value, const char **err);
3847int performModuleConfigSetDefaultFromName(sds name, const char **err);
3848void addModuleBoolConfig(sds name, sds alias, int flags, void *privdata, int default_val);
3849void addModuleStringConfig(sds name, sds alias, int flags, void *privdata, sds default_val);
3850void addModuleEnumConfig(sds name, sds alias, int flags, void *privdata, int default_val, configEnum *enum_vals, int num_enum_vals);
3851void addModuleNumericConfig(sds name, sds alias, int flags, void *privdata, long long default_val, int conf_flags, long long lower, long long upper);
3852void addModuleConfigApply(list *module_configs, ModuleConfig *module_config);
3853int moduleConfigApply(ModuleConfig *module_config, const char **err);
3854int moduleConfigApplyConfig(list *module_configs, const char **err, const char **err_arg_name);
3855int moduleConfigNeedsApply(ModuleConfig *config);
3856int getModuleBoolConfig(ModuleConfig *module_config);
3857int setModuleBoolConfig(ModuleConfig *config, int val, const char **err);
3858sds getModuleStringConfig(ModuleConfig *module_config);
3859int setModuleStringConfig(ModuleConfig *config, sds strval, const char **err);
3860int getModuleEnumConfig(ModuleConfig *module_config);
3861int setModuleEnumConfig(ModuleConfig *config, int val, const char **err);
3862long long getModuleNumericConfig(ModuleConfig *module_config);
3863int setModuleNumericConfig(ModuleConfig *config, long long val, const char **err);
3864
3865/* API for modules to access config values. */
3866dictIterator *moduleGetConfigIterator(void);
3867const char *moduleConfigIteratorNext(dictIterator **iter, sds pattern, int is_glob, configType *typehint);
3868int moduleGetConfigType(sds name, configType *res);
3869int moduleGetBoolConfig(sds name, int *res);
3870int moduleGetStringConfig(sds name, sds *res);
3871int moduleGetEnumConfig(sds name, sds *res);
3872int moduleGetNumericConfig(sds name, long long *res);
3873int moduleSetBoolConfig(client *c, sds name, int val, const char **err);
3874int moduleSetStringConfig(client *c, sds name, const char *val, const char **err);
3875int moduleSetEnumConfig(client *c, sds name, sds *vals, int vals_cnt, const char **err);
3876int moduleSetNumericConfig(client *c, sds name, long long val, const char **err);
3877
3878/* db.c -- Keyspace access API */
3879void updateKeysizesHist(redisDb *db, int didx, uint32_t type, int64_t oldLen, int64_t newLen);
3880void updateSlotAllocSize(redisDb *db, int didx, size_t oldsize, size_t newsize);
3881void dbgAssertKeysizesHist(redisDb *db);
3882void dbgAssertAllocSizePerSlot(redisDb *db);
3883int removeExpire(redisDb *db, robj *key);
3884void deleteExpiredKeyAndPropagate(redisDb *db, robj *keyobj);
3885void deleteEvictedKeyAndPropagate(redisDb *db, robj *keyobj, long long *key_mem_freed);
3886void propagateDeletion(redisDb *db, robj *key, int lazy);
3887int keyIsExpired(redisDb *db, sds key, kvobj *kv);
3888int confAllowsExpireDel(void);
3889long long getExpire(redisDb *db, sds key, kvobj *kv);
3890kvobj *setExpire(client *c, redisDb *db, robj *key, long long when);
3891kvobj *setExpireByLink(client *c, redisDb *db, sds key, long long when, dictEntryLink link);
3892int checkAlreadyExpired(long long when);
3893int parseExtendedExpireArgumentsOrReply(client *c, int *flags);
3894kvobj *lookupKeyRead(redisDb *db, robj *key);
3895kvobj *lookupKeyWrite(redisDb *db, robj *key);
3896kvobj *lookupKeyWriteWithLink(redisDb *db, robj *key, dictEntryLink *link);
3897kvobj *lookupKeyReadOrReply(client *c, robj *key, robj *reply);
3898kvobj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply);
3899kvobj *lookupKeyReadWithFlags(redisDb *db, robj *key, int flags);
3900kvobj *lookupKeyWriteWithFlags(redisDb *db, robj *key, int flags);
3901kvobj *kvobjCommandLookup(client *c, robj *key);
3902kvobj *kvobjCommandLookupOrReply(client *c, robj *key, robj *reply);
3903
3904#define LOOKUP_NONE 0
3905#define LOOKUP_NOTOUCH (1<<0) /* Don't update LRU. */
3906#define LOOKUP_NONOTIFY (1<<1) /* Don't trigger keyspace event on key misses. */
3907#define LOOKUP_NOSTATS (1<<2) /* Don't update keyspace hits/misses counters. */
3908#define LOOKUP_WRITE (1<<3) /* Delete expired keys even in replicas. */
3909#define LOOKUP_NOEXPIRE (1<<4) /* Avoid deleting lazy expired keys. */
3910#define LOOKUP_ACCESS_EXPIRED (1<<5) /* Allow lookup to expired key. */
3911#define LOOKUP_ACCESS_TRIMMED (1<<6) /* Allow lookup to key in slots being trimmed. */
3912#define LOOKUP_NOEFFECTS (LOOKUP_NONOTIFY | LOOKUP_NOSTATS | LOOKUP_NOTOUCH | LOOKUP_NOEXPIRE) /* Avoid any effects from fetching the key */
3913
3914static inline kvobj *dictGetKV(const dictEntry *de) {return (kvobj *) dictGetKey(de);}
3915kvobj *dbAdd(redisDb *db, robj *key, robj **valref);
3916kvobj *dbAddByLink(redisDb *db, robj *key, robj **valref, dictEntryLink *link);
3917kvobj *dbAddInternal(redisDb *db, robj *key, robj **valref, dictEntryLink *link, const KeyMetaSpec *m);
3918kvobj *dbAddRDBLoad(redisDb *db, sds key, robj **valref, const KeyMetaSpec *keyMetaSpec);
3919void dbReplaceValue(redisDb *db, robj *key, kvobj **ioKeyVal, int updateKeySizes);
3920void dbReplaceValueWithLink(redisDb *db, robj *key, robj **val, dictEntryLink link);
3921
3922#define SETKEY_KEEPTTL 1
3923#define SETKEY_NO_SIGNAL 2
3924#define SETKEY_ALREADY_EXIST 4
3925#define SETKEY_DOESNT_EXIST 8
3926
3927void setKey(client *c, redisDb *db, robj *key, robj **ioval, int flags);
3928void setKeyByLink(client *c, redisDb *db, robj *key, robj **valref, int flags, dictEntryLink *link);
3929robj *dbRandomKey(redisDb *db);
3930int dbGenericDelete(redisDb *db, robj *key, int async, int flags);
3931int dbSyncDelete(redisDb *db, robj *key);
3932int dbDelete(redisDb *db, robj *key);
3933int dbDeleteSkipKeysizesUpdate(redisDb *db, robj *key);
3934kvobj *dbUnshareStringValue(redisDb *db, robj *key, kvobj *o);
3935kvobj *dbUnshareStringValueByLink(redisDb *db, robj *key, kvobj *kv, dictEntryLink link);
3936
3937#define FLUSH_TYPE_ALL 0
3938#define FLUSH_TYPE_DB 1
3939#define FLUSH_TYPE_SLOTS 2
3940void replySlotsFlushAndFree(client *c, struct slotRangeArray *slots);
3941int flushCommandCommon(client *c, int type, int flags, struct slotRangeArray *ranges);
3942#define EMPTYDB_NO_FLAGS 0 /* No flags. */
3943#define EMPTYDB_ASYNC (1<<0) /* Reclaim memory in another thread. */
3944#define EMPTYDB_NOFUNCTIONS (1<<1) /* Indicate not to flush the functions. */
3945long long emptyData(int dbnum, int flags, void(callback)(dict*));
3946long long emptyDbStructure(redisDb *dbarray, int dbnum, int async, void(callback)(dict*));
3947void flushAllDataAndResetRDB(int flags);
3948long long dbTotalServerKeyCount(void);
3949redisDb *initTempDb(void);
3950void discardTempDb(redisDb *tempDb);
3951
3952
3953int selectDb(client *c, int id);
3954void keyModified(client *c, redisDb *db, robj *key, robj *val, int signal);
3955void signalFlushedDb(int dbid, int async, struct slotRangeArray *slots);
3956void scanGenericCommand(client *c, robj *o, unsigned long long cursor);
3957int parseScanCursorOrReply(client *c, robj *o, unsigned long long *cursor);
3958int dbAsyncDelete(redisDb *db, robj *key);
3959void emptyDbAsync(redisDb *db);
3960void emptyDbDataAsync(kvstore *keys, kvstore *expires, ebuckets hexpires);
3961size_t lazyfreeGetPendingObjectsCount(void);
3962size_t lazyfreeGetFreedObjectsCount(void);
3963void lazyfreeResetStats(void);
3964void freeObjAsync(robj *key, robj *obj, int dbid);
3965void freeReplicationBacklogRefMemAsync(list *blocks, rax *index);
3966
3967/* API to get key arguments from commands */
3968#define GET_KEYSPEC_DEFAULT 0
3969#define GET_KEYSPEC_INCLUDE_NOT_KEYS (1<<0) /* Consider 'fake' keys as keys */
3970#define GET_KEYSPEC_RETURN_PARTIAL (1<<1) /* Return all keys that can be found */
3971
3972int getKeysFromCommandWithSpecs(struct redisCommand *cmd, robj **argv, int argc, int search_flags, getKeysResult *result);
3973keyReference *getKeysPrepareResult(getKeysResult *result, int numkeys);
3974int getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3975int getSlotFromCommand(struct redisCommand *cmd, robj **argv, int argc);
3976int doesCommandHaveKeys(struct redisCommand *cmd);
3977int getChannelsFromCommand(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3978int doesCommandHaveChannelsWithFlags(struct redisCommand *cmd, int flags);
3979void getKeysFreeResult(getKeysResult *result);
3980int extractKeysAndSlot(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result, int *slot);
3981int sintercardGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
3982int zunionInterDiffGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
3983int zunionInterDiffStoreGetKeys(struct redisCommand *cmd,robj **argv, int argc, getKeysResult *result);
3984int evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3985int functionGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3986int sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3987int sortROGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3988int migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3989int georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3990int xreadGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3991int lmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3992int blmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3993int zmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3994int bzmpopGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3995int setGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3996int delexGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3997int bitfieldGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result);
3998
3999unsigned short crc16(const char *buf, int len);
4000
4001/* Sentinel */
4002void initSentinelConfig(void);
4003void initSentinel(void);
4004void sentinelTimer(void);
4005const char *sentinelHandleConfiguration(char **argv, int argc);
4006void queueSentinelConfig(sds *argv, int argc, int linenum, sds line);
4007void loadSentinelConfigFromQueue(void);
4008void sentinelIsRunning(void);
4009void sentinelCheckConfigFile(void);
4010void sentinelCommand(client *c);
4011void sentinelInfoCommand(client *c);
4012void sentinelPublishCommand(client *c);
4013void sentinelRoleCommand(client *c);
4014
4015/* redis-check-rdb & aof */
4016int redis_check_rdb(char *rdbfilename, FILE *fp);
4017int redis_check_rdb_main(int argc, char **argv, FILE *fp);
4018int redis_check_aof_main(int argc, char **argv);
4019
4020/* Scripting */
4021void scriptingInit(int setup);
4022int ldbRemoveChild(pid_t pid);
4023void ldbKillForkedSessions(void);
4024int ldbPendingChildren(void);
4025void luaLdbLineHook(lua_State *lua, lua_Debug *ar);
4026void freeLuaScriptsSync(dict *lua_scripts, list *lua_scripts_lru_list, lua_State *lua);
4027void freeLuaScriptsAsync(dict *lua_scripts, list *lua_scripts_lru_list, lua_State *lua);
4028void freeFunctionsAsync(functionsLibCtx *functions_lib_ctx, dict *engines);
4029int ldbIsEnabled(void);
4030void ldbLog(sds entry);
4031void ldbLogRedisReply(char *reply);
4032void sha1hex(char *digest, char *script, size_t len);
4033unsigned long evalScriptsMemoryVM(void);
4034dict* evalScriptsDict(void);
4035unsigned long evalScriptsMemoryEngine(void);
4036uint64_t evalGetCommandFlags(client *c, uint64_t orig_flags);
4037uint64_t fcallGetCommandFlags(client *c, uint64_t orig_flags);
4038int isInsideYieldingLongCommand(void);
4039
4040typedef struct luaScript {
4041 uint64_t flags;
4042 robj *body;
4043 listNode *node; /* list node in lua_scripts_lru_list list. */
4044} luaScript;
4045/* Cache of recently used small arguments to avoid malloc calls. */
4046#define LUA_CMD_OBJCACHE_SIZE 32
4047#define LUA_CMD_OBJCACHE_MAX_LEN 64
4048
4049/* Blocked clients API */
4050void processUnblockedClients(void);
4051void initClientBlockingState(client *c);
4052void blockClient(client *c, int btype);
4053void unblockClient(client *c, int queue_for_reprocessing);
4054void unblockClientOnTimeout(client *c);
4055void unblockClientOnError(client *c, const char *err_str);
4056void queueClientForReprocessing(client *c);
4057int blockedClientMayTimeout(client *c);
4058void replyToBlockedClientTimedOut(client *c);
4059int getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit);
4060void disconnectAllBlockedClients(void);
4061void handleClientsBlockedOnKeys(void);
4062void signalKeyAsReady(redisDb *db, robj *key, int type);
4063void blockForKeys(client *c, int btype, robj **keys, int numkeys, mstime_t timeout, int unblock_on_nokey);
4064void blockClientShutdown(client *c);
4065void blockPostponeClient(client *c);
4066void blockPostponeClientWithType(client *c, int btype);
4067void blockForReplication(client *c, mstime_t timeout, long long offset, long numreplicas);
4068void blockForAofFsync(client *c, mstime_t timeout, long long offset, int numlocal, long numreplicas);
4069void signalDeletedKeyAsReady(redisDb *db, robj *key, int type);
4070void updateStatsOnUnblock(client *c, long blocked_us, long reply_us, int had_errors);
4071void scanDatabaseForDeletedKeys(redisDb *emptied, redisDb *replaced_with, struct slotRangeArray *slots);
4072void totalNumberOfStatefulKeys(unsigned long *blocking_keys, unsigned long *bloking_keys_on_nokey, unsigned long *watched_keys);
4073void blockedBeforeSleep(void);
4074
4075/* timeout.c -- Blocked clients timeout and connections timeout. */
4076void addClientToTimeoutTable(client *c);
4077void removeClientFromTimeoutTable(client *c);
4078void handleBlockedClientsTimeout(void);
4079int clientsCronHandleTimeout(client *c, mstime_t now_ms);
4080
4081/* t_stream.c -- Handling of stream data structures */
4082void handleClaimableStreamEntries(void);
4083void handleExpiredIdmpEntries(void);
4084
4085/* expire.c -- Handling of expired keys */
4086void activeExpireCycle(int type);
4087void expireSlaveKeys(void);
4088void rememberSlaveKeyWithExpire(redisDb *db, sds key);
4089void flushSlaveKeysWithExpireList(void);
4090size_t getSlaveKeyWithExpireCount(void);
4091uint64_t activeSubexpires(redisDb *db, int slot, uint32_t maxFieldsToExpire);
4092
4093/* evict.c -- maxmemory handling and LRU eviction. */
4094void evictionPoolAlloc(void);
4095#define LFU_INIT_VAL 5
4096unsigned long LFUGetTimeInMinutes(void);
4097uint8_t LFULogIncr(uint8_t value);
4098unsigned long LFUDecrAndReturn(robj *o);
4099#define EVICT_OK 0
4100#define EVICT_RUNNING 1
4101#define EVICT_FAIL 2
4102int performEvictions(void);
4103void startEvictionTimeProc(void);
4104
4105/* Keys hashing / comparison functions for dict.c hash tables. */
4106uint64_t dictSdsHash(const void *key);
4107uint64_t dictPtrHash(const void *key);
4108uint64_t dictSdsCaseHash(const void *key);
4109size_t dictSdsKeyLen(dict *d, const void *key);
4110int dictSdsKeyCompare(dictCmpCache *cache, const void *key1, const void *key2);
4111int dictSdsKeyCaseCompare(dictCmpCache *cache, const void *key1, const void *key2);
4112void dictSdsDestructor(dict *d, void *val);
4113void dictListDestructor(dict *d, void *val);
4114void *dictSdsDup(dict *d, const void *key);
4115
4116/* Git SHA1 */
4117char *redisGitSHA1(void);
4118char *redisGitDirty(void);
4119uint64_t redisBuildId(void);
4120const char *redisBuildIdRaw(void);
4121char *redisBuildIdString(void);
4122
4123/* XXH3 hash of a string as hex string */
4124sds stringDigest(robj *o);
4125int validateHexDigest(client *c, const sds digest);
4126
4127/* Hotkey tracking */
4128hotkeyStats *hotkeyStatsCreate(int count, int duration, int sample_ratio,
4129 int *slots, int slots_count, uint64_t tracked_metrics);
4130void hotkeyStatsRelease(hotkeyStats *hotkeys);
4131void hotkeyStatsPreCurrentCmd(hotkeyStats *hotkeys, client *c);
4132void hotkeyStatsUpdateCurrentCmd(hotkeyStats *hotkeys, hotkeyMetrics metrics);
4133void hotkeyStatsPostCurrentCmd(hotkeyStats *hotkeys);
4134size_t hotkeysGetMemoryUsage(hotkeyStats *hotkeys);
4135
4136/* Commands prototypes */
4137void authCommand(client *c);
4138void pingCommand(client *c);
4139void echoCommand(client *c);
4140void commandCommand(client *c);
4141void commandCountCommand(client *c);
4142void commandListCommand(client *c);
4143void commandInfoCommand(client *c);
4144void commandGetKeysCommand(client *c);
4145void commandGetKeysAndFlagsCommand(client *c);
4146void commandHelpCommand(client *c);
4147void commandDocsCommand(client *c);
4148void setCommand(client *c);
4149void setnxCommand(client *c);
4150void setexCommand(client *c);
4151void psetexCommand(client *c);
4152void getCommand(client *c);
4153void getexCommand(client *c);
4154void getdelCommand(client *c);
4155void delCommand(client *c);
4156void delexCommand(client *c);
4157void unlinkCommand(client *c);
4158void existsCommand(client *c);
4159void setbitCommand(client *c);
4160void getbitCommand(client *c);
4161void bitfieldCommand(client *c);
4162void bitfieldroCommand(client *c);
4163void setrangeCommand(client *c);
4164void getrangeCommand(client *c);
4165void incrCommand(client *c);
4166void decrCommand(client *c);
4167void incrbyCommand(client *c);
4168void decrbyCommand(client *c);
4169void incrbyfloatCommand(client *c);
4170void selectCommand(client *c);
4171void swapdbCommand(client *c);
4172void randomkeyCommand(client *c);
4173void keysCommand(client *c);
4174void scanCommand(client *c);
4175void dbsizeCommand(client *c);
4176void lastsaveCommand(client *c);
4177void saveCommand(client *c);
4178void bgsaveCommand(client *c);
4179void bgrewriteaofCommand(client *c);
4180void shutdownCommand(client *c);
4181void slowlogCommand(client *c);
4182void moveCommand(client *c);
4183void copyCommand(client *c);
4184void renameCommand(client *c);
4185void renamenxCommand(client *c);
4186void lpushCommand(client *c);
4187void rpushCommand(client *c);
4188void lpushxCommand(client *c);
4189void rpushxCommand(client *c);
4190void linsertCommand(client *c);
4191void lpopCommand(client *c);
4192void rpopCommand(client *c);
4193void lmpopCommand(client *c);
4194void llenCommand(client *c);
4195void lindexCommand(client *c);
4196void lrangeCommand(client *c);
4197void ltrimCommand(client *c);
4198void typeCommand(client *c);
4199void lsetCommand(client *c);
4200void saddCommand(client *c);
4201void sremCommand(client *c);
4202void smoveCommand(client *c);
4203void sismemberCommand(client *c);
4204void smismemberCommand(client *c);
4205void scardCommand(client *c);
4206void spopCommand(client *c);
4207void srandmemberCommand(client *c);
4208void sinterCommand(client *c);
4209void smembersCommand(client *c);
4210void sinterCardCommand(client *c);
4211void sinterstoreCommand(client *c);
4212void sunionCommand(client *c);
4213void sunionstoreCommand(client *c);
4214void sdiffCommand(client *c);
4215void sdiffstoreCommand(client *c);
4216void sscanCommand(client *c);
4217void syncCommand(client *c);
4218void flushdbCommand(client *c);
4219void flushallCommand(client *c);
4220void trimslotsCommand(client *c);
4221void sortCommand(client *c);
4222void sortroCommand(client *c);
4223void lremCommand(client *c);
4224void lposCommand(client *c);
4225void rpoplpushCommand(client *c);
4226void lmoveCommand(client *c);
4227void infoCommand(client *c);
4228void mgetCommand(client *c);
4229void monitorCommand(client *c);
4230void expireCommand(client *c);
4231void expireatCommand(client *c);
4232void pexpireCommand(client *c);
4233void pexpireatCommand(client *c);
4234void getsetCommand(client *c);
4235void ttlCommand(client *c);
4236void touchCommand(client *c);
4237void pttlCommand(client *c);
4238void expiretimeCommand(client *c);
4239void pexpiretimeCommand(client *c);
4240void persistCommand(client *c);
4241void replicaofCommand(client *c);
4242void roleCommand(client *c);
4243void debugCommand(client *c);
4244void msetCommand(client *c);
4245void msetnxCommand(client *c);
4246void msetexCommand(client *c);
4247void zaddCommand(client *c);
4248void zincrbyCommand(client *c);
4249void zrangeCommand(client *c);
4250void zrangebyscoreCommand(client *c);
4251void zrevrangebyscoreCommand(client *c);
4252void zrangebylexCommand(client *c);
4253void zrevrangebylexCommand(client *c);
4254void zcountCommand(client *c);
4255void zlexcountCommand(client *c);
4256void zrevrangeCommand(client *c);
4257void zcardCommand(client *c);
4258void zremCommand(client *c);
4259void zscoreCommand(client *c);
4260void zmscoreCommand(client *c);
4261void zremrangebyscoreCommand(client *c);
4262void zremrangebylexCommand(client *c);
4263void zpopminCommand(client *c);
4264void zpopmaxCommand(client *c);
4265void zmpopCommand(client *c);
4266void bzpopminCommand(client *c);
4267void bzpopmaxCommand(client *c);
4268void bzmpopCommand(client *c);
4269void zrandmemberCommand(client *c);
4270void multiCommand(client *c);
4271void execCommand(client *c);
4272void discardCommand(client *c);
4273void blpopCommand(client *c);
4274void brpopCommand(client *c);
4275void blmpopCommand(client *c);
4276void brpoplpushCommand(client *c);
4277void blmoveCommand(client *c);
4278void appendCommand(client *c);
4279void strlenCommand(client *c);
4280void zrankCommand(client *c);
4281void zrevrankCommand(client *c);
4282void hsetCommand(client *c);
4283void hsetexCommand(client *c);
4284void hpexpireCommand(client *c);
4285void hexpireCommand(client *c);
4286void hpexpireatCommand(client *c);
4287void hexpireatCommand(client *c);
4288void httlCommand(client *c);
4289void hpttlCommand(client *c);
4290void hexpiretimeCommand(client *c);
4291void hpexpiretimeCommand(client *c);
4292void hpersistCommand(client *c);
4293void hsetnxCommand(client *c);
4294void hgetCommand(client *c);
4295void hmgetCommand(client *c);
4296void hgetexCommand(client *c);
4297void hgetdelCommand(client *c);
4298void hdelCommand(client *c);
4299void hlenCommand(client *c);
4300void hstrlenCommand(client *c);
4301void zremrangebyrankCommand(client *c);
4302void zunionstoreCommand(client *c);
4303void zinterstoreCommand(client *c);
4304void zdiffstoreCommand(client *c);
4305void zunionCommand(client *c);
4306void zinterCommand(client *c);
4307void zinterCardCommand(client *c);
4308void zrangestoreCommand(client *c);
4309void zdiffCommand(client *c);
4310void zscanCommand(client *c);
4311void hkeysCommand(client *c);
4312void hvalsCommand(client *c);
4313void hgetallCommand(client *c);
4314void hexistsCommand(client *c);
4315void hscanCommand(client *c);
4316void hrandfieldCommand(client *c);
4317void configSetCommand(client *c);
4318void configGetCommand(client *c);
4319void configResetStatCommand(client *c);
4320void configRewriteCommand(client *c);
4321void configHelpCommand(client *c);
4322int configExists(const sds name);
4323void hincrbyCommand(client *c);
4324void hincrbyfloatCommand(client *c);
4325void subscribeCommand(client *c);
4326void unsubscribeCommand(client *c);
4327void psubscribeCommand(client *c);
4328void punsubscribeCommand(client *c);
4329void publishCommand(client *c);
4330void pubsubCommand(client *c);
4331void spublishCommand(client *c);
4332void ssubscribeCommand(client *c);
4333void sunsubscribeCommand(client *c);
4334void watchCommand(client *c);
4335void unwatchCommand(client *c);
4336void clusterCommand(client *c);
4337void clusterSlotStatsCommand(client *c);
4338void restoreCommand(client *c);
4339void migrateCommand(client *c);
4340void askingCommand(client *c);
4341void readonlyCommand(client *c);
4342void readwriteCommand(client *c);
4343void sflushCommand(client *c);
4344int verifyDumpPayload(unsigned char *p, size_t len, uint16_t *rdbver_ptr);
4345void dumpCommand(client *c);
4346void clientCommand(client *c);
4347void helloCommand(client *c);
4348void clientSetinfoCommand(client *c);
4349void evalCommand(client *c);
4350void evalRoCommand(client *c);
4351void evalShaCommand(client *c);
4352void evalShaRoCommand(client *c);
4353void scriptCommand(client *c);
4354void fcallCommand(client *c);
4355void fcallroCommand(client *c);
4356void functionLoadCommand(client *c);
4357void functionDeleteCommand(client *c);
4358void functionKillCommand(client *c);
4359void functionStatsCommand(client *c);
4360void functionListCommand(client *c);
4361void functionHelpCommand(client *c);
4362void functionFlushCommand(client *c);
4363void functionRestoreCommand(client *c);
4364void functionDumpCommand(client *c);
4365void timeCommand(client *c);
4366void bitopCommand(client *c);
4367void bitcountCommand(client *c);
4368void bitposCommand(client *c);
4369void replconfCommand(client *c);
4370void waitCommand(client *c);
4371void waitaofCommand(client *c);
4372void georadiusbymemberCommand(client *c);
4373void georadiusbymemberroCommand(client *c);
4374void georadiusCommand(client *c);
4375void georadiusroCommand(client *c);
4376void geoaddCommand(client *c);
4377void geohashCommand(client *c);
4378void geoposCommand(client *c);
4379void geodistCommand(client *c);
4380void geosearchCommand(client *c);
4381void geosearchstoreCommand(client *c);
4382void pfselftestCommand(client *c);
4383void pfaddCommand(client *c);
4384void pfcountCommand(client *c);
4385void pfmergeCommand(client *c);
4386void pfdebugCommand(client *c);
4387void latencyCommand(client *c);
4388void moduleCommand(client *c);
4389void securityWarningCommand(client *c);
4390void xaddCommand(client *c);
4391void xrangeCommand(client *c);
4392void xrevrangeCommand(client *c);
4393void xlenCommand(client *c);
4394void xreadCommand(client *c);
4395void xgroupCommand(client *c);
4396void xsetidCommand(client *c);
4397void xackCommand(client *c);
4398void xackdelCommand(client *c);
4399void xpendingCommand(client *c);
4400void xclaimCommand(client *c);
4401void xautoclaimCommand(client *c);
4402void xinfoCommand(client *c);
4403void xcfgsetCommand(client *c);
4404void xdelCommand(client *c);
4405void xdelexCommand(client *c);
4406void xtrimCommand(client *c);
4407void lolwutCommand(client *c);
4408void aclCommand(client *c);
4409void hotkeysCommand(client *c);
4410void lcsCommand(client *c);
4411void quitCommand(client *c);
4412void resetCommand(client *c);
4413void failoverCommand(client *c);
4414void digestCommand(client *c);
4415
4416#if defined(__GNUC__)
4417void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
4418void free(void *ptr) __attribute__ ((deprecated));
4419void *malloc(size_t size) __attribute__ ((deprecated));
4420void *realloc(void *ptr, size_t size) __attribute__ ((deprecated));
4421#endif
4422
4423/* Debugging stuff */
4424void _serverAssertWithInfo(const client *c, const robj *o, const char *estr, const char *file, int line);
4425void _serverAssert(const char *estr, const char *file, int line);
4426#ifdef __GNUC__
4427void _serverPanic(const char *file, int line, const char *msg, ...)
4428 __attribute__ ((format (printf, 3, 4)));
4429#else
4430void _serverPanic(const char *file, int line, const char *msg, ...);
4431#endif
4432void serverLogObjectDebugInfo(const robj *o);
4433void setupDebugSigHandlers(void);
4434void setupSigSegvHandler(void);
4435void removeSigSegvHandlers(void);
4436const char *getSafeInfoString(const char *s, size_t len, char **tmp);
4437dict *genInfoSectionDict(robj **argv, int argc, char **defaults, int *out_all, int *out_everything);
4438void releaseInfoSectionDict(dict *sec);
4439sds genRedisInfoString(dict *section_dict, int all_sections, int everything);
4440sds genModulesInfoString(sds info);
4441void applyWatchdogPeriod(void);
4442void watchdogScheduleSignal(int period);
4443void serverLogHexDump(int level, char *descr, void *value, size_t len);
4444int memtest_preserving_test(unsigned long *m, size_t bytes, int passes);
4445void mixDigest(unsigned char *digest, const void *ptr, size_t len);
4446void xorDigest(unsigned char *digest, const void *ptr, size_t len);
4447sds catSubCommandFullname(const char *parent_name, const char *sub_name);
4448void commandAddSubcommand(struct redisCommand *parent, struct redisCommand *subcommand, const char *declared_name);
4449void debugDelay(int usec);
4450void killThreads(void);
4451void makeThreadKillable(void);
4452void swapMainDbWithTempDb(redisDb *tempDb);
4453sds getVersion(void);
4454void debugPauseProcess(void);
4455
4456/* Log redaction helpers: return "*redacted*" when hide-user-data-from-log is on. */
4457static inline const char *redactLogCstr(const char *s) {
4458 return server.hide_user_data_from_log ? "*redacted*" : (s ? s : "(null)");
4459}
4460
4461/* Use macro for checking log level to avoid evaluating arguments in cases log
4462 * should be ignored due to low level. */
4463#define serverLog(level, ...) do {\
4464 if (((level)&0xff) < server.verbosity) break;\
4465 _serverLog(level, __VA_ARGS__);\
4466 } while(0)
4467
4468#define redisDebug(fmt, ...) \
4469 printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
4470#define redisDebugMark() \
4471 printf("-- MARK %s:%d --\n", __FILE__, __LINE__)
4472
4473int iAmMaster(void);
4474
4475#define STRINGIFY_(x) #x
4476#define STRINGIFY(x) STRINGIFY_(x)
4477
4478#endif