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