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 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <time.h> 41 #include <limits.h> 42 #include <unistd.h> 43 #include <errno.h> 44 #include <inttypes.h> 45 #include <pthread.h> 46 #include <syslog.h> 47 #include <netinet/in.h> 48 #include <lua.h> 49 #include <signal.h> 50 51 typedef long long mstime_t; /* millisecond time type. */ 52 53 #include "ae.h" /* Event driven programming library */ 54 #include "sds.h" /* Dynamic safe strings */ 55 #include "dict.h" /* Hash tables */ 56 #include "adlist.h" /* Linked lists */ 57 #include "zmalloc.h" /* total memory usage aware version of malloc/free */ 58 #include "anet.h" /* Networking the easy way */ 59 #include "ziplist.h" /* Compact list data structure */ 60 #include "intset.h" /* Compact integer set structure */ 61 #include "version.h" /* Version macro */ 62 #include "util.h" /* Misc functions useful in many places */ 63 #include "latency.h" /* Latency monitor API */ 64 #include "sparkline.h" /* ASCII graphs API */ 65 #include "quicklist.h" 66 67 /* Following includes allow test functions to be called from Redis main() */ 68 #include "zipmap.h" 69 #include "sha1.h" 70 #include "endianconv.h" 71 #include "crc64.h" 72 73 /* Error codes */ 74 #define C_OK 0 75 #define C_ERR -1 76 77 /* Static server configuration */ 78 #define CONFIG_DEFAULT_HZ 10 /* Time interrupt calls/sec. */ 79 #define CONFIG_MIN_HZ 1 80 #define CONFIG_MAX_HZ 500 81 #define CONFIG_DEFAULT_SERVER_PORT 6379 /* TCP port */ 82 #define CONFIG_DEFAULT_TCP_BACKLOG 511 /* TCP listen backlog */ 83 #define CONFIG_DEFAULT_CLIENT_TIMEOUT 0 /* default client timeout: infinite */ 84 #define CONFIG_DEFAULT_DBNUM 16 85 #define CONFIG_MAX_LINE 1024 86 #define CRON_DBS_PER_CALL 16 87 #define NET_MAX_WRITES_PER_EVENT (1024*64) 88 #define PROTO_SHARED_SELECT_CMDS 10 89 #define OBJ_SHARED_INTEGERS 10000 90 #define OBJ_SHARED_BULKHDR_LEN 32 91 #define LOG_MAX_LEN 1024 /* Default maximum length of syslog messages */ 92 #define AOF_REWRITE_PERC 100 93 #define AOF_REWRITE_MIN_SIZE (64*1024*1024) 94 #define AOF_REWRITE_ITEMS_PER_CMD 64 95 #define CONFIG_DEFAULT_SLOWLOG_LOG_SLOWER_THAN 10000 96 #define CONFIG_DEFAULT_SLOWLOG_MAX_LEN 128 97 #define CONFIG_DEFAULT_MAX_CLIENTS 10000 98 #define CONFIG_AUTHPASS_MAX_LEN 512 99 #define CONFIG_DEFAULT_SLAVE_PRIORITY 100 100 #define CONFIG_DEFAULT_REPL_TIMEOUT 60 101 #define CONFIG_DEFAULT_REPL_PING_SLAVE_PERIOD 10 102 #define CONFIG_RUN_ID_SIZE 40 103 #define RDB_EOF_MARK_SIZE 40 104 #define CONFIG_DEFAULT_REPL_BACKLOG_SIZE (1024*1024) /* 1mb */ 105 #define CONFIG_DEFAULT_REPL_BACKLOG_TIME_LIMIT (60*60) /* 1 hour */ 106 #define CONFIG_REPL_BACKLOG_MIN_SIZE (1024*16) /* 16k */ 107 #define CONFIG_BGSAVE_RETRY_DELAY 5 /* Wait a few secs before trying again. */ 108 #define CONFIG_DEFAULT_PID_FILE "/var/run/redis.pid" 109 #define CONFIG_DEFAULT_SYSLOG_IDENT "redis" 110 #define CONFIG_DEFAULT_CLUSTER_CONFIG_FILE "nodes.conf" 111 #define CONFIG_DEFAULT_DAEMONIZE 0 112 #define CONFIG_DEFAULT_UNIX_SOCKET_PERM 0 113 #define CONFIG_DEFAULT_TCP_KEEPALIVE 300 114 #define CONFIG_DEFAULT_PROTECTED_MODE 1 115 #define CONFIG_DEFAULT_LOGFILE "" 116 #define CONFIG_DEFAULT_SYSLOG_ENABLED 0 117 #define CONFIG_DEFAULT_STOP_WRITES_ON_BGSAVE_ERROR 1 118 #define CONFIG_DEFAULT_RDB_COMPRESSION 1 119 #define CONFIG_DEFAULT_RDB_CHECKSUM 1 120 #define CONFIG_DEFAULT_RDB_FILENAME "dump.rdb" 121 #define CONFIG_DEFAULT_REPL_DISKLESS_SYNC 0 122 #define CONFIG_DEFAULT_REPL_DISKLESS_SYNC_DELAY 5 123 #define CONFIG_DEFAULT_SLAVE_SERVE_STALE_DATA 1 124 #define CONFIG_DEFAULT_SLAVE_READ_ONLY 1 125 #define CONFIG_DEFAULT_SLAVE_ANNOUNCE_IP NULL 126 #define CONFIG_DEFAULT_SLAVE_ANNOUNCE_PORT 0 127 #define CONFIG_DEFAULT_REPL_DISABLE_TCP_NODELAY 0 128 #define CONFIG_DEFAULT_MAXMEMORY 0 129 #define CONFIG_DEFAULT_MAXMEMORY_SAMPLES 5 130 #define CONFIG_DEFAULT_AOF_FILENAME "appendonly.aof" 131 #define CONFIG_DEFAULT_AOF_NO_FSYNC_ON_REWRITE 0 132 #define CONFIG_DEFAULT_AOF_LOAD_TRUNCATED 1 133 #define CONFIG_DEFAULT_ACTIVE_REHASHING 1 134 #define CONFIG_DEFAULT_AOF_REWRITE_INCREMENTAL_FSYNC 1 135 #define CONFIG_DEFAULT_MIN_SLAVES_TO_WRITE 0 136 #define CONFIG_DEFAULT_MIN_SLAVES_MAX_LAG 10 137 #define NET_IP_STR_LEN 46 /* INET6_ADDRSTRLEN is 46, but we need to be sure */ 138 #define NET_PEER_ID_LEN (NET_IP_STR_LEN+32) /* Must be enough for ip:port */ 139 #define CONFIG_BINDADDR_MAX 16 140 #define CONFIG_MIN_RESERVED_FDS 32 141 #define CONFIG_DEFAULT_LATENCY_MONITOR_THRESHOLD 0 142 143 #define ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP 20 /* Loopkups per loop. */ 144 #define ACTIVE_EXPIRE_CYCLE_FAST_DURATION 1000 /* Microseconds */ 145 #define ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC 25 /* CPU max % for keys collection */ 146 #define ACTIVE_EXPIRE_CYCLE_SLOW 0 147 #define ACTIVE_EXPIRE_CYCLE_FAST 1 148 149 /* Instantaneous metrics tracking. */ 150 #define STATS_METRIC_SAMPLES 16 /* Number of samples per metric. */ 151 #define STATS_METRIC_COMMAND 0 /* Number of commands executed. */ 152 #define STATS_METRIC_NET_INPUT 1 /* Bytes read to network .*/ 153 #define STATS_METRIC_NET_OUTPUT 2 /* Bytes written to network. */ 154 #define STATS_METRIC_COUNT 3 155 156 /* Protocol and I/O related defines */ 157 #define PROTO_MAX_QUERYBUF_LEN (1024*1024*1024) /* 1GB max query buffer. */ 158 #define PROTO_IOBUF_LEN (1024*16) /* Generic I/O buffer size */ 159 #define PROTO_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */ 160 #define PROTO_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */ 161 #define PROTO_MBULK_BIG_ARG (1024*32) 162 #define LONG_STR_SIZE 21 /* Bytes needed for long -> str + '\0' */ 163 #define AOF_AUTOSYNC_BYTES (1024*1024*32) /* fdatasync every 32MB */ 164 165 /* When configuring the server eventloop, we setup it so that the total number 166 * of file descriptors we can handle are server.maxclients + RESERVED_FDS + 167 * a few more to stay safe. Since RESERVED_FDS defaults to 32, we add 96 168 * in order to make sure of not over provisioning more than 128 fds. */ 169 #define CONFIG_FDSET_INCR (CONFIG_MIN_RESERVED_FDS+96) 170 171 /* Hash table parameters */ 172 #define HASHTABLE_MIN_FILL 10 /* Minimal hash table fill 10% */ 173 174 /* Command flags. Please check the command table defined in the redis.c file 175 * for more information about the meaning of every flag. */ 176 #define CMD_WRITE 1 /* "w" flag */ 177 #define CMD_READONLY 2 /* "r" flag */ 178 #define CMD_DENYOOM 4 /* "m" flag */ 179 #define CMD_NOT_USED_1 8 /* no longer used flag */ 180 #define CMD_ADMIN 16 /* "a" flag */ 181 #define CMD_PUBSUB 32 /* "p" flag */ 182 #define CMD_NOSCRIPT 64 /* "s" flag */ 183 #define CMD_RANDOM 128 /* "R" flag */ 184 #define CMD_SORT_FOR_SCRIPT 256 /* "S" flag */ 185 #define CMD_LOADING 512 /* "l" flag */ 186 #define CMD_STALE 1024 /* "t" flag */ 187 #define CMD_SKIP_MONITOR 2048 /* "M" flag */ 188 #define CMD_ASKING 4096 /* "k" flag */ 189 #define CMD_FAST 8192 /* "F" flag */ 190 191 /* Object types */ 192 #define OBJ_STRING 0 193 #define OBJ_LIST 1 194 #define OBJ_SET 2 195 #define OBJ_ZSET 3 196 #define OBJ_HASH 4 197 198 /* Objects encoding. Some kind of objects like Strings and Hashes can be 199 * internally represented in multiple ways. The 'encoding' field of the object 200 * is set to one of this fields for this object. */ 201 #define OBJ_ENCODING_RAW 0 /* Raw representation */ 202 #define OBJ_ENCODING_INT 1 /* Encoded as integer */ 203 #define OBJ_ENCODING_HT 2 /* Encoded as hash table */ 204 #define OBJ_ENCODING_ZIPMAP 3 /* Encoded as zipmap */ 205 #define OBJ_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */ 206 #define OBJ_ENCODING_ZIPLIST 5 /* Encoded as ziplist */ 207 #define OBJ_ENCODING_INTSET 6 /* Encoded as intset */ 208 #define OBJ_ENCODING_SKIPLIST 7 /* Encoded as skiplist */ 209 #define OBJ_ENCODING_EMBSTR 8 /* Embedded sds string encoding */ 210 #define OBJ_ENCODING_QUICKLIST 9 /* Encoded as linked list of ziplists */ 211 212 /* Defines related to the dump file format. To store 32 bits lengths for short 213 * keys requires a lot of space, so we check the most significant 2 bits of 214 * the first byte to interpreter the length: 215 * 216 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte 217 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte 218 * 10|000000 [32 bit integer] => if it's 10, a full 32 bit len will follow 219 * 11|000000 this means: specially encoded object will follow. The six bits 220 * number specify the kind of object that follows. 221 * See the RDB_ENC_* defines. 222 * 223 * Lengths up to 63 are stored using a single byte, most DB keys, and may 224 * values, will fit inside. */ 225 #define RDB_6BITLEN 0 226 #define RDB_14BITLEN 1 227 #define RDB_32BITLEN 2 228 #define RDB_ENCVAL 3 229 #define RDB_LENERR UINT_MAX 230 231 /* When a length of a string object stored on disk has the first two bits 232 * set, the remaining two bits specify a special encoding for the object 233 * accordingly to the following defines: */ 234 #define RDB_ENC_INT8 0 /* 8 bit signed integer */ 235 #define RDB_ENC_INT16 1 /* 16 bit signed integer */ 236 #define RDB_ENC_INT32 2 /* 32 bit signed integer */ 237 #define RDB_ENC_LZF 3 /* string compressed with FASTLZ */ 238 239 /* AOF states */ 240 #define AOF_OFF 0 /* AOF is off */ 241 #define AOF_ON 1 /* AOF is on */ 242 #define AOF_WAIT_REWRITE 2 /* AOF waits rewrite to start appending */ 243 244 /* Client flags */ 245 #define CLIENT_SLAVE (1<<0) /* This client is a slave server */ 246 #define CLIENT_MASTER (1<<1) /* This client is a master server */ 247 #define CLIENT_MONITOR (1<<2) /* This client is a slave monitor, see MONITOR */ 248 #define CLIENT_MULTI (1<<3) /* This client is in a MULTI context */ 249 #define CLIENT_BLOCKED (1<<4) /* The client is waiting in a blocking operation */ 250 #define CLIENT_DIRTY_CAS (1<<5) /* Watched keys modified. EXEC will fail. */ 251 #define CLIENT_CLOSE_AFTER_REPLY (1<<6) /* Close after writing entire reply. */ 252 #define CLIENT_UNBLOCKED (1<<7) /* This client was unblocked and is stored in 253 server.unblocked_clients */ 254 #define CLIENT_LUA (1<<8) /* This is a non connected client used by Lua */ 255 #define CLIENT_ASKING (1<<9) /* Client issued the ASKING command */ 256 #define CLIENT_CLOSE_ASAP (1<<10)/* Close this client ASAP */ 257 #define CLIENT_UNIX_SOCKET (1<<11) /* Client connected via Unix domain socket */ 258 #define CLIENT_DIRTY_EXEC (1<<12) /* EXEC will fail for errors while queueing */ 259 #define CLIENT_MASTER_FORCE_REPLY (1<<13) /* Queue replies even if is master */ 260 #define CLIENT_FORCE_AOF (1<<14) /* Force AOF propagation of current cmd. */ 261 #define CLIENT_FORCE_REPL (1<<15) /* Force replication of current cmd. */ 262 #define CLIENT_PRE_PSYNC (1<<16) /* Instance don't understand PSYNC. */ 263 #define CLIENT_READONLY (1<<17) /* Cluster client is in read-only state. */ 264 #define CLIENT_PUBSUB (1<<18) /* Client is in Pub/Sub mode. */ 265 #define CLIENT_PREVENT_AOF_PROP (1<<19) /* Don't propagate to AOF. */ 266 #define CLIENT_PREVENT_REPL_PROP (1<<20) /* Don't propagate to slaves. */ 267 #define CLIENT_PREVENT_PROP (CLIENT_PREVENT_AOF_PROP|CLIENT_PREVENT_REPL_PROP) 268 #define CLIENT_PENDING_WRITE (1<<21) /* Client has output to send but a write 269 handler is yet not installed. */ 270 #define CLIENT_REPLY_OFF (1<<22) /* Don't send replies to client. */ 271 #define CLIENT_REPLY_SKIP_NEXT (1<<23) /* Set CLIENT_REPLY_SKIP for next cmd */ 272 #define CLIENT_REPLY_SKIP (1<<24) /* Don't send just this reply. */ 273 #define CLIENT_LUA_DEBUG (1<<25) /* Run EVAL in debug mode. */ 274 #define CLIENT_LUA_DEBUG_SYNC (1<<26) /* EVAL debugging without fork() */ 275 276 /* Client block type (btype field in client structure) 277 * if CLIENT_BLOCKED flag is set. */ 278 #define BLOCKED_NONE 0 /* Not blocked, no CLIENT_BLOCKED flag set. */ 279 #define BLOCKED_LIST 1 /* BLPOP & co. */ 280 #define BLOCKED_WAIT 2 /* WAIT for synchronous replication. */ 281 282 /* Client request types */ 283 #define PROTO_REQ_INLINE 1 284 #define PROTO_REQ_MULTIBULK 2 285 286 /* Client classes for client limits, currently used only for 287 * the max-client-output-buffer limit implementation. */ 288 #define CLIENT_TYPE_NORMAL 0 /* Normal req-reply clients + MONITORs */ 289 #define CLIENT_TYPE_SLAVE 1 /* Slaves. */ 290 #define CLIENT_TYPE_PUBSUB 2 /* Clients subscribed to PubSub channels. */ 291 #define CLIENT_TYPE_MASTER 3 /* Master. */ 292 #define CLIENT_TYPE_OBUF_COUNT 3 /* Number of clients to expose to output 293 buffer configuration. Just the first 294 three: normal, slave, pubsub. */ 295 296 /* Slave replication state. Used in server.repl_state for slaves to remember 297 * what to do next. */ 298 #define REPL_STATE_NONE 0 /* No active replication */ 299 #define REPL_STATE_CONNECT 1 /* Must connect to master */ 300 #define REPL_STATE_CONNECTING 2 /* Connecting to master */ 301 /* --- Handshake states, must be ordered --- */ 302 #define REPL_STATE_RECEIVE_PONG 3 /* Wait for PING reply */ 303 #define REPL_STATE_SEND_AUTH 4 /* Send AUTH to master */ 304 #define REPL_STATE_RECEIVE_AUTH 5 /* Wait for AUTH reply */ 305 #define REPL_STATE_SEND_PORT 6 /* Send REPLCONF listening-port */ 306 #define REPL_STATE_RECEIVE_PORT 7 /* Wait for REPLCONF reply */ 307 #define REPL_STATE_SEND_IP 8 /* Send REPLCONF ip-address */ 308 #define REPL_STATE_RECEIVE_IP 9 /* Wait for REPLCONF reply */ 309 #define REPL_STATE_SEND_CAPA 10 /* Send REPLCONF capa */ 310 #define REPL_STATE_RECEIVE_CAPA 11 /* Wait for REPLCONF reply */ 311 #define REPL_STATE_SEND_PSYNC 12 /* Send PSYNC */ 312 #define REPL_STATE_RECEIVE_PSYNC 13 /* Wait for PSYNC reply */ 313 /* --- End of handshake states --- */ 314 #define REPL_STATE_TRANSFER 14 /* Receiving .rdb from master */ 315 #define REPL_STATE_CONNECTED 15 /* Connected to master */ 316 317 /* State of slaves from the POV of the master. Used in client->replstate. 318 * In SEND_BULK and ONLINE state the slave receives new updates 319 * in its output queue. In the WAIT_BGSAVE states instead the server is waiting 320 * to start the next background saving in order to send updates to it. */ 321 #define SLAVE_STATE_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */ 322 #define SLAVE_STATE_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */ 323 #define SLAVE_STATE_SEND_BULK 8 /* Sending RDB file to slave. */ 324 #define SLAVE_STATE_ONLINE 9 /* RDB file transmitted, sending just updates. */ 325 326 /* Slave capabilities. */ 327 #define SLAVE_CAPA_NONE 0 328 #define SLAVE_CAPA_EOF (1<<0) /* Can parse the RDB EOF streaming format. */ 329 330 /* Synchronous read timeout - slave side */ 331 #define CONFIG_REPL_SYNCIO_TIMEOUT 5 332 333 /* List related stuff */ 334 #define LIST_HEAD 0 335 #define LIST_TAIL 1 336 337 /* Sort operations */ 338 #define SORT_OP_GET 0 339 340 /* Log levels */ 341 #define LL_DEBUG 0 342 #define LL_VERBOSE 1 343 #define LL_NOTICE 2 344 #define LL_WARNING 3 345 #define LL_RAW (1<<10) /* Modifier to log without timestamp */ 346 #define CONFIG_DEFAULT_VERBOSITY LL_NOTICE 347 348 /* Supervision options */ 349 #define SUPERVISED_NONE 0 350 #define SUPERVISED_AUTODETECT 1 351 #define SUPERVISED_SYSTEMD 2 352 #define SUPERVISED_UPSTART 3 353 354 /* Anti-warning macro... */ 355 #define UNUSED(V) ((void) V) 356 357 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */ 358 #define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */ 359 360 /* Append only defines */ 361 #define AOF_FSYNC_NO 0 362 #define AOF_FSYNC_ALWAYS 1 363 #define AOF_FSYNC_EVERYSEC 2 364 #define CONFIG_DEFAULT_AOF_FSYNC AOF_FSYNC_EVERYSEC 365 366 /* Zip structure related defaults */ 367 #define OBJ_HASH_MAX_ZIPLIST_ENTRIES 512 368 #define OBJ_HASH_MAX_ZIPLIST_VALUE 64 369 #define OBJ_SET_MAX_INTSET_ENTRIES 512 370 #define OBJ_ZSET_MAX_ZIPLIST_ENTRIES 128 371 #define OBJ_ZSET_MAX_ZIPLIST_VALUE 64 372 373 /* List defaults */ 374 #define OBJ_LIST_MAX_ZIPLIST_SIZE -2 375 #define OBJ_LIST_COMPRESS_DEPTH 0 376 377 /* HyperLogLog defines */ 378 #define CONFIG_DEFAULT_HLL_SPARSE_MAX_BYTES 3000 379 380 /* Sets operations codes */ 381 #define SET_OP_UNION 0 382 #define SET_OP_DIFF 1 383 #define SET_OP_INTER 2 384 385 /* Redis maxmemory strategies */ 386 #define MAXMEMORY_VOLATILE_LRU 0 387 #define MAXMEMORY_VOLATILE_TTL 1 388 #define MAXMEMORY_VOLATILE_RANDOM 2 389 #define MAXMEMORY_ALLKEYS_LRU 3 390 #define MAXMEMORY_ALLKEYS_RANDOM 4 391 #define MAXMEMORY_NO_EVICTION 5 392 #define CONFIG_DEFAULT_MAXMEMORY_POLICY MAXMEMORY_NO_EVICTION 393 394 /* Scripting */ 395 #define LUA_SCRIPT_TIME_LIMIT 5000 /* milliseconds */ 396 397 /* Units */ 398 #define UNIT_SECONDS 0 399 #define UNIT_MILLISECONDS 1 400 401 /* SHUTDOWN flags */ 402 #define SHUTDOWN_NOFLAGS 0 /* No flags. */ 403 #define SHUTDOWN_SAVE 1 /* Force SAVE on SHUTDOWN even if no save 404 points are configured. */ 405 #define SHUTDOWN_NOSAVE 2 /* Don't SAVE on SHUTDOWN. */ 406 407 /* Command call flags, see call() function */ 408 #define CMD_CALL_NONE 0 409 #define CMD_CALL_SLOWLOG (1<<0) 410 #define CMD_CALL_STATS (1<<1) 411 #define CMD_CALL_PROPAGATE_AOF (1<<2) 412 #define CMD_CALL_PROPAGATE_REPL (1<<3) 413 #define CMD_CALL_PROPAGATE (CMD_CALL_PROPAGATE_AOF|CMD_CALL_PROPAGATE_REPL) 414 #define CMD_CALL_FULL (CMD_CALL_SLOWLOG | CMD_CALL_STATS | CMD_CALL_PROPAGATE) 415 416 /* Command propagation flags, see propagate() function */ 417 #define PROPAGATE_NONE 0 418 #define PROPAGATE_AOF 1 419 #define PROPAGATE_REPL 2 420 421 /* RDB active child save type. */ 422 #define RDB_CHILD_TYPE_NONE 0 423 #define RDB_CHILD_TYPE_DISK 1 /* RDB is written to disk. */ 424 #define RDB_CHILD_TYPE_SOCKET 2 /* RDB is written to slave socket. */ 425 426 /* Keyspace changes notification classes. Every class is associated with a 427 * character for configuration purposes. */ 428 #define NOTIFY_KEYSPACE (1<<0) /* K */ 429 #define NOTIFY_KEYEVENT (1<<1) /* E */ 430 #define NOTIFY_GENERIC (1<<2) /* g */ 431 #define NOTIFY_STRING (1<<3) /* $ */ 432 #define NOTIFY_LIST (1<<4) /* l */ 433 #define NOTIFY_SET (1<<5) /* s */ 434 #define NOTIFY_HASH (1<<6) /* h */ 435 #define NOTIFY_ZSET (1<<7) /* z */ 436 #define NOTIFY_EXPIRED (1<<8) /* x */ 437 #define NOTIFY_EVICTED (1<<9) /* e */ 438 #define NOTIFY_ALL (NOTIFY_GENERIC | NOTIFY_STRING | NOTIFY_LIST | NOTIFY_SET | NOTIFY_HASH | NOTIFY_ZSET | NOTIFY_EXPIRED | NOTIFY_EVICTED) /* A */ 439 440 /* Get the first bind addr or NULL */ 441 #define NET_FIRST_BIND_ADDR (server.bindaddr_count ? server.bindaddr[0] : NULL) 442 443 /* Using the following macro you can run code inside serverCron() with the 444 * specified period, specified in milliseconds. 445 * The actual resolution depends on server.hz. */ 446 #define run_with_period(_ms_) if ((_ms_ <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz)))) 447 448 /* We can print the stacktrace, so our assert is defined this way: */ 449 #define serverAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_serverAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1))) 450 #define serverAssert(_e) ((_e)?(void)0 : (_serverAssert(#_e,__FILE__,__LINE__),_exit(1))) 451 #define serverPanic(_e) _serverPanic(#_e,__FILE__,__LINE__),_exit(1) 452 453 /*----------------------------------------------------------------------------- 454 * Data types 455 *----------------------------------------------------------------------------*/ 456 457 /* A redis object, that is a type able to hold a string / list / set */ 458 459 /* The actual Redis Object */ 460 #define LRU_BITS 24 461 #define LRU_CLOCK_MAX ((1<<LRU_BITS)-1) /* Max value of obj->lru */ 462 #define LRU_CLOCK_RESOLUTION 1000 /* LRU clock resolution in ms */ 463 typedef struct redisObject { 464 unsigned type:4; 465 unsigned encoding:4; 466 unsigned lru:LRU_BITS; /* lru time (relative to server.lruclock) */ 467 int refcount; 468 void *ptr; 469 } robj; 470 471 /* Macro used to obtain the current LRU clock. 472 * If the current resolution is lower than the frequency we refresh the 473 * LRU clock (as it should be in production servers) we return the 474 * precomputed value, otherwise we need to resort to a system call. */ 475 #define LRU_CLOCK() ((1000/server.hz <= LRU_CLOCK_RESOLUTION) ? server.lruclock : getLRUClock()) 476 477 /* Macro used to initialize a Redis object allocated on the stack. 478 * Note that this macro is taken near the structure definition to make sure 479 * we'll update it when the structure is changed, to avoid bugs like 480 * bug #85 introduced exactly in this way. */ 481 #define initStaticStringObject(_var,_ptr) do { \ 482 _var.refcount = 1; \ 483 _var.type = OBJ_STRING; \ 484 _var.encoding = OBJ_ENCODING_RAW; \ 485 _var.ptr = _ptr; \ 486 } while(0) 487 488 /* To improve the quality of the LRU approximation we take a set of keys 489 * that are good candidate for eviction across freeMemoryIfNeeded() calls. 490 * 491 * Entries inside the eviciton pool are taken ordered by idle time, putting 492 * greater idle times to the right (ascending order). 493 * 494 * Empty entries have the key pointer set to NULL. */ 495 #define MAXMEMORY_EVICTION_POOL_SIZE 16 496 struct evictionPoolEntry { 497 unsigned long long idle; /* Object idle time. */ 498 sds key; /* Key name. */ 499 }; 500 501 /* Redis database representation. There are multiple databases identified 502 * by integers from 0 (the default database) up to the max configured 503 * database. The database number is the 'id' field in the structure. */ 504 typedef struct redisDb { 505 dict *dict; /* The keyspace for this DB */ 506 dict *expires; /* Timeout of keys with a timeout set */ 507 dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */ 508 dict *ready_keys; /* Blocked keys that received a PUSH */ 509 dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */ 510 struct evictionPoolEntry *eviction_pool; /* Eviction pool of keys */ 511 int id; /* Database ID */ 512 long long avg_ttl; /* Average TTL, just for stats */ 513 } redisDb; 514 515 /* Client MULTI/EXEC state */ 516 typedef struct multiCmd { 517 robj **argv; 518 int argc; 519 struct redisCommand *cmd; 520 } multiCmd; 521 522 typedef struct multiState { 523 multiCmd *commands; /* Array of MULTI commands */ 524 int count; /* Total number of MULTI commands */ 525 int minreplicas; /* MINREPLICAS for synchronous replication */ 526 time_t minreplicas_timeout; /* MINREPLICAS timeout as unixtime. */ 527 } multiState; 528 529 /* This structure holds the blocking operation state for a client. 530 * The fields used depend on client->btype. */ 531 typedef struct blockingState { 532 /* Generic fields. */ 533 mstime_t timeout; /* Blocking operation timeout. If UNIX current time 534 * is > timeout then the operation timed out. */ 535 536 /* BLOCKED_LIST */ 537 dict *keys; /* The keys we are waiting to terminate a blocking 538 * operation such as BLPOP. Otherwise NULL. */ 539 robj *target; /* The key that should receive the element, 540 * for BRPOPLPUSH. */ 541 542 /* BLOCKED_WAIT */ 543 int numreplicas; /* Number of replicas we are waiting for ACK. */ 544 long long reploffset; /* Replication offset to reach. */ 545 } blockingState; 546 547 /* The following structure represents a node in the server.ready_keys list, 548 * where we accumulate all the keys that had clients blocked with a blocking 549 * operation such as B[LR]POP, but received new data in the context of the 550 * last executed command. 551 * 552 * After the execution of every command or script, we run this list to check 553 * if as a result we should serve data to clients blocked, unblocking them. 554 * Note that server.ready_keys will not have duplicates as there dictionary 555 * also called ready_keys in every structure representing a Redis database, 556 * where we make sure to remember if a given key was already added in the 557 * server.ready_keys list. */ 558 typedef struct readyList { 559 redisDb *db; 560 robj *key; 561 } readyList; 562 563 /* With multiplexing we need to take per-client state. 564 * Clients are taken in a linked list. */ 565 typedef struct client { 566 uint64_t id; /* Client incremental unique ID. */ 567 int fd; /* Client socket. */ 568 redisDb *db; /* Pointer to currently SELECTed DB. */ 569 int dictid; /* ID of the currently SELECTed DB. */ 570 robj *name; /* As set by CLIENT SETNAME. */ 571 sds querybuf; /* Buffer we use to accumulate client queries. */ 572 size_t querybuf_peak; /* Recent (100ms or more) peak of querybuf size. */ 573 int argc; /* Num of arguments of current command. */ 574 robj **argv; /* Arguments of current command. */ 575 struct redisCommand *cmd, *lastcmd; /* Last command executed. */ 576 int reqtype; /* Request protocol type: PROTO_REQ_* */ 577 int multibulklen; /* Number of multi bulk arguments left to read. */ 578 long bulklen; /* Length of bulk argument in multi bulk request. */ 579 list *reply; /* List of reply objects to send to the client. */ 580 unsigned long long reply_bytes; /* Tot bytes of objects in reply list. */ 581 size_t sentlen; /* Amount of bytes already sent in the current 582 buffer or object being sent. */ 583 time_t ctime; /* Client creation time. */ 584 time_t lastinteraction; /* Time of the last interaction, used for timeout */ 585 time_t obuf_soft_limit_reached_time; 586 int flags; /* Client flags: CLIENT_* macros. */ 587 int authenticated; /* When requirepass is non-NULL. */ 588 int replstate; /* Replication state if this is a slave. */ 589 int repl_put_online_on_ack; /* Install slave write handler on ACK. */ 590 int repldbfd; /* Replication DB file descriptor. */ 591 off_t repldboff; /* Replication DB file offset. */ 592 off_t repldbsize; /* Replication DB file size. */ 593 sds replpreamble; /* Replication DB preamble. */ 594 long long reploff; /* Replication offset if this is our master. */ 595 long long repl_ack_off; /* Replication ack offset, if this is a slave. */ 596 long long repl_ack_time;/* Replication ack time, if this is a slave. */ 597 long long psync_initial_offset; /* FULLRESYNC reply offset other slaves 598 copying this slave output buffer 599 should use. */ 600 char replrunid[CONFIG_RUN_ID_SIZE+1]; /* Master run id if is a master. */ 601 int slave_listening_port; /* As configured with: REPLCONF listening-port */ 602 char slave_ip[NET_IP_STR_LEN]; /* Optionally given by REPLCONF ip-address */ 603 int slave_capa; /* Slave capabilities: SLAVE_CAPA_* bitwise OR. */ 604 multiState mstate; /* MULTI/EXEC state */ 605 int btype; /* Type of blocking op if CLIENT_BLOCKED. */ 606 blockingState bpop; /* blocking state */ 607 long long woff; /* Last write global replication offset. */ 608 list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */ 609 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */ 610 list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */ 611 sds peerid; /* Cached peer ID. */ 612 613 /* Response buffer */ 614 int bufpos; 615 char buf[PROTO_REPLY_CHUNK_BYTES]; 616 } client; 617 618 struct saveparam { 619 time_t seconds; 620 int changes; 621 }; 622 623 struct sharedObjectsStruct { 624 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *cnegone, *pong, *space, 625 *colon, *nullbulk, *nullmultibulk, *queued, 626 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr, 627 *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *bgsaveerr, 628 *masterdownerr, *roslaveerr, *execaborterr, *noautherr, *noreplicaserr, 629 *busykeyerr, *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk, 630 *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *rpop, *lpop, 631 *lpush, *emptyscan, *minstring, *maxstring, 632 *select[PROTO_SHARED_SELECT_CMDS], 633 *integers[OBJ_SHARED_INTEGERS], 634 *mbulkhdr[OBJ_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */ 635 *bulkhdr[OBJ_SHARED_BULKHDR_LEN]; /* "$<value>\r\n" */ 636 }; 637 638 /* ZSETs use a specialized version of Skiplists */ 639 typedef struct zskiplistNode { 640 robj *obj; 641 double score; 642 struct zskiplistNode *backward; 643 struct zskiplistLevel { 644 struct zskiplistNode *forward; 645 unsigned int span; 646 } level[]; 647 } zskiplistNode; 648 649 typedef struct zskiplist { 650 struct zskiplistNode *header, *tail; 651 unsigned long length; 652 int level; 653 } zskiplist; 654 655 typedef struct zset { 656 dict *dict; 657 zskiplist *zsl; 658 } zset; 659 660 typedef struct clientBufferLimitsConfig { 661 unsigned long long hard_limit_bytes; 662 unsigned long long soft_limit_bytes; 663 time_t soft_limit_seconds; 664 } clientBufferLimitsConfig; 665 666 extern clientBufferLimitsConfig clientBufferLimitsDefaults[CLIENT_TYPE_OBUF_COUNT]; 667 668 /* The redisOp structure defines a Redis Operation, that is an instance of 669 * a command with an argument vector, database ID, propagation target 670 * (PROPAGATE_*), and command pointer. 671 * 672 * Currently only used to additionally propagate more commands to AOF/Replication 673 * after the propagation of the executed command. */ 674 typedef struct redisOp { 675 robj **argv; 676 int argc, dbid, target; 677 struct redisCommand *cmd; 678 } redisOp; 679 680 /* Defines an array of Redis operations. There is an API to add to this 681 * structure in a easy way. 682 * 683 * redisOpArrayInit(); 684 * redisOpArrayAppend(); 685 * redisOpArrayFree(); 686 */ 687 typedef struct redisOpArray { 688 redisOp *ops; 689 int numops; 690 } redisOpArray; 691 692 /*----------------------------------------------------------------------------- 693 * Global server state 694 *----------------------------------------------------------------------------*/ 695 696 struct clusterState; 697 698 /* AIX defines hz to __hz, we don't use this define and in order to allow 699 * Redis build on AIX we need to undef it. */ 700 #ifdef _AIX 701 #undef hz 702 #endif 703 704 struct redisServer { 705 /* General */ 706 pid_t pid; /* Main process pid. */ 707 char *configfile; /* Absolute config file path, or NULL */ 708 char *executable; /* Absolute executable file path. */ 709 char **exec_argv; /* Executable argv vector (copy). */ 710 int hz; /* serverCron() calls frequency in hertz */ 711 redisDb *db; 712 dict *commands; /* Command table */ 713 dict *orig_commands; /* Command table before command renaming. */ 714 aeEventLoop *el; 715 unsigned lruclock:LRU_BITS; /* Clock for LRU eviction */ 716 int shutdown_asap; /* SHUTDOWN needed ASAP */ 717 int activerehashing; /* Incremental rehash in serverCron() */ 718 char *requirepass; /* Pass for AUTH command, or NULL */ 719 char *pidfile; /* PID file path */ 720 int arch_bits; /* 32 or 64 depending on sizeof(long) */ 721 int cronloops; /* Number of times the cron function run */ 722 char runid[CONFIG_RUN_ID_SIZE+1]; /* ID always different at every exec. */ 723 int sentinel_mode; /* True if this instance is a Sentinel. */ 724 /* Networking */ 725 int port; /* TCP listening port */ 726 int tcp_backlog; /* TCP listen() backlog */ 727 char *bindaddr[CONFIG_BINDADDR_MAX]; /* Addresses we should bind to */ 728 int bindaddr_count; /* Number of addresses in server.bindaddr[] */ 729 char *unixsocket; /* UNIX socket path */ 730 mode_t unixsocketperm; /* UNIX socket permission */ 731 int ipfd[CONFIG_BINDADDR_MAX]; /* TCP socket file descriptors */ 732 int ipfd_count; /* Used slots in ipfd[] */ 733 int sofd; /* Unix socket file descriptor */ 734 int cfd[CONFIG_BINDADDR_MAX];/* Cluster bus listening socket */ 735 int cfd_count; /* Used slots in cfd[] */ 736 list *clients; /* List of active clients */ 737 list *clients_to_close; /* Clients to close asynchronously */ 738 list *clients_pending_write; /* There is to write or install handler. */ 739 list *slaves, *monitors; /* List of slaves and MONITORs */ 740 client *current_client; /* Current client, only used on crash report */ 741 int clients_paused; /* True if clients are currently paused */ 742 mstime_t clients_pause_end_time; /* Time when we undo clients_paused */ 743 char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */ 744 dict *migrate_cached_sockets;/* MIGRATE cached sockets */ 745 uint64_t next_client_id; /* Next client unique ID. Incremental. */ 746 int protected_mode; /* Don't accept external connections. */ 747 /* RDB / AOF loading information */ 748 int loading; /* We are loading data from disk if true */ 749 off_t loading_total_bytes; 750 off_t loading_loaded_bytes; 751 time_t loading_start_time; 752 off_t loading_process_events_interval_bytes; 753 /* Fast pointers to often looked up command */ 754 struct redisCommand *delCommand, *multiCommand, *lpushCommand, *lpopCommand, 755 *rpopCommand, *sremCommand, *execCommand; 756 /* Fields used only for stats */ 757 time_t stat_starttime; /* Server start time */ 758 long long stat_numcommands; /* Number of processed commands */ 759 long long stat_numconnections; /* Number of connections received */ 760 long long stat_expiredkeys; /* Number of expired keys */ 761 long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */ 762 long long stat_keyspace_hits; /* Number of successful lookups of keys */ 763 long long stat_keyspace_misses; /* Number of failed lookups of keys */ 764 size_t stat_peak_memory; /* Max used memory record */ 765 long long stat_fork_time; /* Time needed to perform latest fork() */ 766 double stat_fork_rate; /* Fork rate in GB/sec. */ 767 long long stat_rejected_conn; /* Clients rejected because of maxclients */ 768 long long stat_sync_full; /* Number of full resyncs with slaves. */ 769 long long stat_sync_partial_ok; /* Number of accepted PSYNC requests. */ 770 long long stat_sync_partial_err;/* Number of unaccepted PSYNC requests. */ 771 list *slowlog; /* SLOWLOG list of commands */ 772 long long slowlog_entry_id; /* SLOWLOG current entry ID */ 773 long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */ 774 unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */ 775 size_t resident_set_size; /* RSS sampled in serverCron(). */ 776 long long stat_net_input_bytes; /* Bytes read from network. */ 777 long long stat_net_output_bytes; /* Bytes written to network. */ 778 /* The following two are used to track instantaneous metrics, like 779 * number of operations per second, network traffic. */ 780 struct { 781 long long last_sample_time; /* Timestamp of last sample in ms */ 782 long long last_sample_count;/* Count in last sample */ 783 long long samples[STATS_METRIC_SAMPLES]; 784 int idx; 785 } inst_metric[STATS_METRIC_COUNT]; 786 /* Configuration */ 787 int verbosity; /* Loglevel in redis.conf */ 788 int maxidletime; /* Client timeout in seconds */ 789 int tcpkeepalive; /* Set SO_KEEPALIVE if non-zero. */ 790 int active_expire_enabled; /* Can be disabled for testing purposes. */ 791 size_t client_max_querybuf_len; /* Limit for client query buffer length */ 792 int dbnum; /* Total number of configured DBs */ 793 int supervised; /* 1 if supervised, 0 otherwise. */ 794 int supervised_mode; /* See SUPERVISED_* */ 795 int daemonize; /* True if running as a daemon */ 796 clientBufferLimitsConfig client_obuf_limits[CLIENT_TYPE_OBUF_COUNT]; 797 /* AOF persistence */ 798 int aof_state; /* AOF_(ON|OFF|WAIT_REWRITE) */ 799 int aof_fsync; /* Kind of fsync() policy */ 800 char *aof_filename; /* Name of the AOF file */ 801 int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */ 802 int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */ 803 off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */ 804 off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */ 805 off_t aof_current_size; /* AOF current size. */ 806 int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */ 807 pid_t aof_child_pid; /* PID if rewriting process */ 808 list *aof_rewrite_buf_blocks; /* Hold changes during an AOF rewrite. */ 809 sds aof_buf; /* AOF buffer, written before entering the event loop */ 810 int aof_fd; /* File descriptor of currently selected AOF file */ 811 int aof_selected_db; /* Currently selected DB in AOF */ 812 time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */ 813 time_t aof_last_fsync; /* UNIX time of last fsync() */ 814 time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */ 815 time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */ 816 int aof_lastbgrewrite_status; /* C_OK or C_ERR */ 817 unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */ 818 int aof_rewrite_incremental_fsync;/* fsync incrementally while rewriting? */ 819 int aof_last_write_status; /* C_OK or C_ERR */ 820 int aof_last_write_errno; /* Valid if aof_last_write_status is ERR */ 821 int aof_load_truncated; /* Don't stop on unexpected AOF EOF. */ 822 /* AOF pipes used to communicate between parent and child during rewrite. */ 823 int aof_pipe_write_data_to_child; 824 int aof_pipe_read_data_from_parent; 825 int aof_pipe_write_ack_to_parent; 826 int aof_pipe_read_ack_from_child; 827 int aof_pipe_write_ack_to_child; 828 int aof_pipe_read_ack_from_parent; 829 int aof_stop_sending_diff; /* If true stop sending accumulated diffs 830 to child process. */ 831 sds aof_child_diff; /* AOF diff accumulator child side. */ 832 /* RDB persistence */ 833 long long dirty; /* Changes to DB from the last save */ 834 long long dirty_before_bgsave; /* Used to restore dirty on failed BGSAVE */ 835 pid_t rdb_child_pid; /* PID of RDB saving child */ 836 struct saveparam *saveparams; /* Save points array for RDB */ 837 int saveparamslen; /* Number of saving points */ 838 char *rdb_filename; /* Name of RDB file */ 839 int rdb_compression; /* Use compression in RDB? */ 840 int rdb_checksum; /* Use RDB checksum? */ 841 time_t lastsave; /* Unix time of last successful save */ 842 time_t lastbgsave_try; /* Unix time of last attempted bgsave */ 843 time_t rdb_save_time_last; /* Time used by last RDB save run. */ 844 time_t rdb_save_time_start; /* Current RDB save start time. */ 845 int rdb_bgsave_scheduled; /* BGSAVE when possible if true. */ 846 int rdb_child_type; /* Type of save by active child. */ 847 int lastbgsave_status; /* C_OK or C_ERR */ 848 int stop_writes_on_bgsave_err; /* Don't allow writes if can't BGSAVE */ 849 int rdb_pipe_write_result_to_parent; /* RDB pipes used to return the state */ 850 int rdb_pipe_read_result_from_child; /* of each slave in diskless SYNC. */ 851 /* Propagation of commands in AOF / replication */ 852 redisOpArray also_propagate; /* Additional command to propagate. */ 853 /* Logging */ 854 char *logfile; /* Path of log file */ 855 int syslog_enabled; /* Is syslog enabled? */ 856 char *syslog_ident; /* Syslog ident */ 857 int syslog_facility; /* Syslog facility */ 858 /* Replication (master) */ 859 int slaveseldb; /* Last SELECTed DB in replication output */ 860 long long master_repl_offset; /* Global replication offset */ 861 int repl_ping_slave_period; /* Master pings the slave every N seconds */ 862 char *repl_backlog; /* Replication backlog for partial syncs */ 863 long long repl_backlog_size; /* Backlog circular buffer size */ 864 long long repl_backlog_histlen; /* Backlog actual data length */ 865 long long repl_backlog_idx; /* Backlog circular buffer current offset */ 866 long long repl_backlog_off; /* Replication offset of first byte in the 867 backlog buffer. */ 868 time_t repl_backlog_time_limit; /* Time without slaves after the backlog 869 gets released. */ 870 time_t repl_no_slaves_since; /* We have no slaves since that time. 871 Only valid if server.slaves len is 0. */ 872 int repl_min_slaves_to_write; /* Min number of slaves to write. */ 873 int repl_min_slaves_max_lag; /* Max lag of <count> slaves to write. */ 874 int repl_good_slaves_count; /* Number of slaves with lag <= max_lag. */ 875 int repl_diskless_sync; /* Send RDB to slaves sockets directly. */ 876 int repl_diskless_sync_delay; /* Delay to start a diskless repl BGSAVE. */ 877 /* Replication (slave) */ 878 char *masterauth; /* AUTH with this password with master */ 879 char *masterhost; /* Hostname of master */ 880 int masterport; /* Port of master */ 881 int repl_timeout; /* Timeout after N seconds of master idle */ 882 client *master; /* Client that is master for this slave */ 883 client *cached_master; /* Cached master to be reused for PSYNC. */ 884 int repl_syncio_timeout; /* Timeout for synchronous I/O calls */ 885 int repl_state; /* Replication status if the instance is a slave */ 886 off_t repl_transfer_size; /* Size of RDB to read from master during sync. */ 887 off_t repl_transfer_read; /* Amount of RDB read from master during sync. */ 888 off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */ 889 int repl_transfer_s; /* Slave -> Master SYNC socket */ 890 int repl_transfer_fd; /* Slave -> Master SYNC temp file descriptor */ 891 char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */ 892 time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */ 893 int repl_serve_stale_data; /* Serve stale data when link is down? */ 894 int repl_slave_ro; /* Slave is read only? */ 895 time_t repl_down_since; /* Unix time at which link with master went down */ 896 int repl_disable_tcp_nodelay; /* Disable TCP_NODELAY after SYNC? */ 897 int slave_priority; /* Reported in INFO and used by Sentinel. */ 898 int slave_announce_port; /* Give the master this listening port. */ 899 char *slave_announce_ip; /* Give the master this ip address. */ 900 char repl_master_runid[CONFIG_RUN_ID_SIZE+1]; /* Master run id for PSYNC.*/ 901 long long repl_master_initial_offset; /* Master PSYNC offset. */ 902 /* Replication script cache. */ 903 dict *repl_scriptcache_dict; /* SHA1 all slaves are aware of. */ 904 list *repl_scriptcache_fifo; /* First in, first out LRU eviction. */ 905 unsigned int repl_scriptcache_size; /* Max number of elements. */ 906 /* Synchronous replication. */ 907 list *clients_waiting_acks; /* Clients waiting in WAIT command. */ 908 int get_ack_from_slaves; /* If true we send REPLCONF GETACK. */ 909 /* Limits */ 910 unsigned int maxclients; /* Max number of simultaneous clients */ 911 unsigned long long maxmemory; /* Max number of memory bytes to use */ 912 int maxmemory_policy; /* Policy for key eviction */ 913 int maxmemory_samples; /* Pricision of random sampling */ 914 /* Blocked clients */ 915 unsigned int bpop_blocked_clients; /* Number of clients blocked by lists */ 916 list *unblocked_clients; /* list of clients to unblock before next loop */ 917 list *ready_keys; /* List of readyList structures for BLPOP & co */ 918 /* Sort parameters - qsort_r() is only available under BSD so we 919 * have to take this state global, in order to pass it to sortCompare() */ 920 int sort_desc; 921 int sort_alpha; 922 int sort_bypattern; 923 int sort_store; 924 /* Zip structure config, see redis.conf for more information */ 925 size_t hash_max_ziplist_entries; 926 size_t hash_max_ziplist_value; 927 size_t set_max_intset_entries; 928 size_t zset_max_ziplist_entries; 929 size_t zset_max_ziplist_value; 930 size_t hll_sparse_max_bytes; 931 /* List parameters */ 932 int list_max_ziplist_size; 933 int list_compress_depth; 934 /* time cache */ 935 time_t unixtime; /* Unix time sampled every cron cycle. */ 936 long long mstime; /* Like 'unixtime' but with milliseconds resolution. */ 937 /* Pubsub */ 938 dict *pubsub_channels; /* Map channels to list of subscribed clients */ 939 list *pubsub_patterns; /* A list of pubsub_patterns */ 940 int notify_keyspace_events; /* Events to propagate via Pub/Sub. This is an 941 xor of NOTIFY_... flags. */ 942 /* Cluster */ 943 int cluster_enabled; /* Is cluster enabled? */ 944 mstime_t cluster_node_timeout; /* Cluster node timeout. */ 945 char *cluster_configfile; /* Cluster auto-generated config file name. */ 946 struct clusterState *cluster; /* State of the cluster */ 947 int cluster_migration_barrier; /* Cluster replicas migration barrier. */ 948 int cluster_slave_validity_factor; /* Slave max data age for failover. */ 949 int cluster_require_full_coverage; /* If true, put the cluster down if 950 there is at least an uncovered slot.*/ 951 /* Scripting */ 952 lua_State *lua; /* The Lua interpreter. We use just one for all clients */ 953 client *lua_client; /* The "fake client" to query Redis from Lua */ 954 client *lua_caller; /* The client running EVAL right now, or NULL */ 955 dict *lua_scripts; /* A dictionary of SHA1 -> Lua scripts */ 956 mstime_t lua_time_limit; /* Script timeout in milliseconds */ 957 mstime_t lua_time_start; /* Start time of script, milliseconds time */ 958 int lua_write_dirty; /* True if a write command was called during the 959 execution of the current script. */ 960 int lua_random_dirty; /* True if a random command was called during the 961 execution of the current script. */ 962 int lua_replicate_commands; /* True if we are doing single commands repl. */ 963 int lua_multi_emitted;/* True if we already proagated MULTI. */ 964 int lua_repl; /* Script replication flags for redis.set_repl(). */ 965 int lua_timedout; /* True if we reached the time limit for script 966 execution. */ 967 int lua_kill; /* Kill the script if true. */ 968 int lua_always_replicate_commands; /* Default replication type. */ 969 /* Latency monitor */ 970 long long latency_monitor_threshold; 971 dict *latency_events; 972 /* Assert & bug reporting */ 973 char *assert_failed; 974 char *assert_file; 975 int assert_line; 976 int bug_report_start; /* True if bug report header was already logged. */ 977 int watchdog_period; /* Software watchdog period in ms. 0 = off */ 978 /* System hardware info */ 979 size_t system_memory_size; /* Total memory in system as reported by OS */ 980 }; 981 982 typedef struct pubsubPattern { 983 client *client; 984 robj *pattern; 985 } pubsubPattern; 986 987 typedef void redisCommandProc(client *c); 988 typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); 989 struct redisCommand { 990 char *name; 991 redisCommandProc *proc; 992 int arity; 993 char *sflags; /* Flags as string representation, one char per flag. */ 994 int flags; /* The actual flags, obtained from the 'sflags' field. */ 995 /* Use a function to determine keys arguments in a command line. 996 * Used for Redis Cluster redirect. */ 997 redisGetKeysProc *getkeys_proc; 998 /* What keys should be loaded in background when calling this command? */ 999 int firstkey; /* The first argument that's a key (0 = no keys) */ 1000 int lastkey; /* The last argument that's a key */ 1001 int keystep; /* The step between first and last key */ 1002 long long microseconds, calls; 1003 }; 1004 1005 struct redisFunctionSym { 1006 char *name; 1007 unsigned long pointer; 1008 }; 1009 1010 typedef struct _redisSortObject { 1011 robj *obj; 1012 union { 1013 double score; 1014 robj *cmpobj; 1015 } u; 1016 } redisSortObject; 1017 1018 typedef struct _redisSortOperation { 1019 int type; 1020 robj *pattern; 1021 } redisSortOperation; 1022 1023 /* Structure to hold list iteration abstraction. */ 1024 typedef struct { 1025 robj *subject; 1026 unsigned char encoding; 1027 unsigned char direction; /* Iteration direction */ 1028 quicklistIter *iter; 1029 } listTypeIterator; 1030 1031 /* Structure for an entry while iterating over a list. */ 1032 typedef struct { 1033 listTypeIterator *li; 1034 quicklistEntry entry; /* Entry in quicklist */ 1035 } listTypeEntry; 1036 1037 /* Structure to hold set iteration abstraction. */ 1038 typedef struct { 1039 robj *subject; 1040 int encoding; 1041 int ii; /* intset iterator */ 1042 dictIterator *di; 1043 } setTypeIterator; 1044 1045 /* Structure to hold hash iteration abstraction. Note that iteration over 1046 * hashes involves both fields and values. Because it is possible that 1047 * not both are required, store pointers in the iterator to avoid 1048 * unnecessary memory allocation for fields/values. */ 1049 typedef struct { 1050 robj *subject; 1051 int encoding; 1052 1053 unsigned char *fptr, *vptr; 1054 1055 dictIterator *di; 1056 dictEntry *de; 1057 } hashTypeIterator; 1058 1059 #define OBJ_HASH_KEY 1 1060 #define OBJ_HASH_VALUE 2 1061 1062 /*----------------------------------------------------------------------------- 1063 * Extern declarations 1064 *----------------------------------------------------------------------------*/ 1065 1066 extern struct redisServer server; 1067 extern struct sharedObjectsStruct shared; 1068 extern dictType setDictType; 1069 extern dictType zsetDictType; 1070 extern dictType clusterNodesDictType; 1071 extern dictType clusterNodesBlackListDictType; 1072 extern dictType dbDictType; 1073 extern dictType shaScriptObjectDictType; 1074 extern double R_Zero, R_PosInf, R_NegInf, R_Nan; 1075 extern dictType hashDictType; 1076 extern dictType replScriptCacheDictType; 1077 1078 /*----------------------------------------------------------------------------- 1079 * Functions prototypes 1080 *----------------------------------------------------------------------------*/ 1081 1082 /* Utils */ 1083 long long ustime(void); 1084 long long mstime(void); 1085 void getRandomHexChars(char *p, unsigned int len); 1086 uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l); 1087 void exitFromChild(int retcode); 1088 size_t redisPopcount(void *s, long count); 1089 void redisSetProcTitle(char *title); 1090 1091 /* networking.c -- Networking and Client related operations */ 1092 client *createClient(int fd); 1093 void closeTimedoutClients(void); 1094 void freeClient(client *c); 1095 void freeClientAsync(client *c); 1096 void resetClient(client *c); 1097 void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask); 1098 void *addDeferredMultiBulkLength(client *c); 1099 void setDeferredMultiBulkLength(client *c, void *node, long length); 1100 void processInputBuffer(client *c); 1101 void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask); 1102 void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask); 1103 void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask); 1104 void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask); 1105 void addReplyBulk(client *c, robj *obj); 1106 void addReplyBulkCString(client *c, const char *s); 1107 void addReplyBulkCBuffer(client *c, const void *p, size_t len); 1108 void addReplyBulkLongLong(client *c, long long ll); 1109 void addReply(client *c, robj *obj); 1110 void addReplySds(client *c, sds s); 1111 void addReplyBulkSds(client *c, sds s); 1112 void addReplyError(client *c, const char *err); 1113 void addReplyStatus(client *c, const char *status); 1114 void addReplyDouble(client *c, double d); 1115 void addReplyHumanLongDouble(client *c, long double d); 1116 void addReplyLongLong(client *c, long long ll); 1117 void addReplyMultiBulkLen(client *c, long length); 1118 void copyClientOutputBuffer(client *dst, client *src); 1119 void *dupClientReplyValue(void *o); 1120 void getClientsMaxBuffers(unsigned long *longest_output_list, 1121 unsigned long *biggest_input_buffer); 1122 char *getClientPeerId(client *client); 1123 sds catClientInfoString(sds s, client *client); 1124 sds getAllClientsInfoString(void); 1125 void rewriteClientCommandVector(client *c, int argc, ...); 1126 void rewriteClientCommandArgument(client *c, int i, robj *newval); 1127 void replaceClientCommandVector(client *c, int argc, robj **argv); 1128 unsigned long getClientOutputBufferMemoryUsage(client *c); 1129 void freeClientsInAsyncFreeQueue(void); 1130 void asyncCloseClientOnOutputBufferLimitReached(client *c); 1131 int getClientType(client *c); 1132 int getClientTypeByName(char *name); 1133 char *getClientTypeName(int class); 1134 void flushSlavesOutputBuffers(void); 1135 void disconnectSlaves(void); 1136 int listenToPort(int port, int *fds, int *count); 1137 void pauseClients(mstime_t duration); 1138 int clientsArePaused(void); 1139 int processEventsWhileBlocked(void); 1140 int handleClientsWithPendingWrites(void); 1141 int clientHasPendingReplies(client *c); 1142 void unlinkClient(client *c); 1143 int writeToClient(int fd, client *c, int handler_installed); 1144 1145 #ifdef __GNUC__ 1146 void addReplyErrorFormat(client *c, const char *fmt, ...) 1147 __attribute__((format(printf, 2, 3))); 1148 void addReplyStatusFormat(client *c, const char *fmt, ...) 1149 __attribute__((format(printf, 2, 3))); 1150 #else 1151 void addReplyErrorFormat(client *c, const char *fmt, ...); 1152 void addReplyStatusFormat(client *c, const char *fmt, ...); 1153 #endif 1154 1155 /* List data type */ 1156 void listTypeTryConversion(robj *subject, robj *value); 1157 void listTypePush(robj *subject, robj *value, int where); 1158 robj *listTypePop(robj *subject, int where); 1159 unsigned long listTypeLength(robj *subject); 1160 listTypeIterator *listTypeInitIterator(robj *subject, long index, unsigned char direction); 1161 void listTypeReleaseIterator(listTypeIterator *li); 1162 int listTypeNext(listTypeIterator *li, listTypeEntry *entry); 1163 robj *listTypeGet(listTypeEntry *entry); 1164 void listTypeInsert(listTypeEntry *entry, robj *value, int where); 1165 int listTypeEqual(listTypeEntry *entry, robj *o); 1166 void listTypeDelete(listTypeIterator *iter, listTypeEntry *entry); 1167 void listTypeConvert(robj *subject, int enc); 1168 void unblockClientWaitingData(client *c); 1169 void handleClientsBlockedOnLists(void); 1170 void popGenericCommand(client *c, int where); 1171 void signalListAsReady(redisDb *db, robj *key); 1172 1173 /* MULTI/EXEC/WATCH... */ 1174 void unwatchAllKeys(client *c); 1175 void initClientMultiState(client *c); 1176 void freeClientMultiState(client *c); 1177 void queueMultiCommand(client *c); 1178 void touchWatchedKey(redisDb *db, robj *key); 1179 void touchWatchedKeysOnFlush(int dbid); 1180 void discardTransaction(client *c); 1181 void flagTransaction(client *c); 1182 void execCommandPropagateMulti(client *c); 1183 1184 /* Redis object implementation */ 1185 void decrRefCount(robj *o); 1186 void decrRefCountVoid(void *o); 1187 void incrRefCount(robj *o); 1188 robj *resetRefCount(robj *obj); 1189 void freeStringObject(robj *o); 1190 void freeListObject(robj *o); 1191 void freeSetObject(robj *o); 1192 void freeZsetObject(robj *o); 1193 void freeHashObject(robj *o); 1194 robj *createObject(int type, void *ptr); 1195 robj *createStringObject(const char *ptr, size_t len); 1196 robj *createRawStringObject(const char *ptr, size_t len); 1197 robj *createEmbeddedStringObject(const char *ptr, size_t len); 1198 robj *dupStringObject(robj *o); 1199 int isObjectRepresentableAsLongLong(robj *o, long long *llongval); 1200 robj *tryObjectEncoding(robj *o); 1201 robj *getDecodedObject(robj *o); 1202 size_t stringObjectLen(robj *o); 1203 robj *createStringObjectFromLongLong(long long value); 1204 robj *createStringObjectFromLongDouble(long double value, int humanfriendly); 1205 robj *createQuicklistObject(void); 1206 robj *createZiplistObject(void); 1207 robj *createSetObject(void); 1208 robj *createIntsetObject(void); 1209 robj *createHashObject(void); 1210 robj *createZsetObject(void); 1211 robj *createZsetZiplistObject(void); 1212 int getLongFromObjectOrReply(client *c, robj *o, long *target, const char *msg); 1213 int checkType(client *c, robj *o, int type); 1214 int getLongLongFromObjectOrReply(client *c, robj *o, long long *target, const char *msg); 1215 int getDoubleFromObjectOrReply(client *c, robj *o, double *target, const char *msg); 1216 int getLongLongFromObject(robj *o, long long *target); 1217 int getLongDoubleFromObject(robj *o, long double *target); 1218 int getLongDoubleFromObjectOrReply(client *c, robj *o, long double *target, const char *msg); 1219 char *strEncoding(int encoding); 1220 int compareStringObjects(robj *a, robj *b); 1221 int collateStringObjects(robj *a, robj *b); 1222 int equalStringObjects(robj *a, robj *b); 1223 unsigned long long estimateObjectIdleTime(robj *o); 1224 #define sdsEncodedObject(objptr) (objptr->encoding == OBJ_ENCODING_RAW || objptr->encoding == OBJ_ENCODING_EMBSTR) 1225 1226 /* Synchronous I/O with timeout */ 1227 ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout); 1228 ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout); 1229 ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout); 1230 1231 /* Replication */ 1232 void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc); 1233 void replicationFeedMonitors(client *c, list *monitors, int dictid, robj **argv, int argc); 1234 void updateSlavesWaitingBgsave(int bgsaveerr, int type); 1235 void replicationCron(void); 1236 void replicationHandleMasterDisconnection(void); 1237 void replicationCacheMaster(client *c); 1238 void resizeReplicationBacklog(long long newsize); 1239 void replicationSetMaster(char *ip, int port); 1240 void replicationUnsetMaster(void); 1241 void refreshGoodSlavesCount(void); 1242 void replicationScriptCacheInit(void); 1243 void replicationScriptCacheFlush(void); 1244 void replicationScriptCacheAdd(sds sha1); 1245 int replicationScriptCacheExists(sds sha1); 1246 void processClientsWaitingReplicas(void); 1247 void unblockClientWaitingReplicas(client *c); 1248 int replicationCountAcksByOffset(long long offset); 1249 void replicationSendNewlineToMaster(void); 1250 long long replicationGetSlaveOffset(void); 1251 char *replicationGetSlaveName(client *c); 1252 long long getPsyncInitialOffset(void); 1253 int replicationSetupSlaveForFullResync(client *slave, long long offset); 1254 1255 /* Generic persistence functions */ 1256 void startLoading(FILE *fp); 1257 void loadingProgress(off_t pos); 1258 void stopLoading(void); 1259 1260 /* RDB persistence */ 1261 #include "rdb.h" 1262 1263 /* AOF persistence */ 1264 void flushAppendOnlyFile(int force); 1265 void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc); 1266 void aofRemoveTempFile(pid_t childpid); 1267 int rewriteAppendOnlyFileBackground(void); 1268 int loadAppendOnlyFile(char *filename); 1269 void stopAppendOnly(void); 1270 int startAppendOnly(void); 1271 void backgroundRewriteDoneHandler(int exitcode, int bysignal); 1272 void aofRewriteBufferReset(void); 1273 unsigned long aofRewriteBufferSize(void); 1274 1275 /* Sorted sets data type */ 1276 1277 /* Struct to hold a inclusive/exclusive range spec by score comparison. */ 1278 typedef struct { 1279 double min, max; 1280 int minex, maxex; /* are min or max exclusive? */ 1281 } zrangespec; 1282 1283 /* Struct to hold an inclusive/exclusive range spec by lexicographic comparison. */ 1284 typedef struct { 1285 robj *min, *max; /* May be set to shared.(minstring|maxstring) */ 1286 int minex, maxex; /* are min or max exclusive? */ 1287 } zlexrangespec; 1288 1289 zskiplist *zslCreate(void); 1290 void zslFree(zskiplist *zsl); 1291 zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj); 1292 unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score); 1293 int zslDelete(zskiplist *zsl, double score, robj *obj); 1294 zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec *range); 1295 zskiplistNode *zslLastInRange(zskiplist *zsl, zrangespec *range); 1296 double zzlGetScore(unsigned char *sptr); 1297 void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); 1298 void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr); 1299 unsigned int zsetLength(robj *zobj); 1300 void zsetConvert(robj *zobj, int encoding); 1301 void zsetConvertToZiplistIfNeeded(robj *zobj, size_t maxelelen); 1302 int zsetScore(robj *zobj, robj *member, double *score); 1303 unsigned long zslGetRank(zskiplist *zsl, double score, robj *o); 1304 1305 /* Core functions */ 1306 int freeMemoryIfNeeded(void); 1307 int processCommand(client *c); 1308 void setupSignalHandlers(void); 1309 struct redisCommand *lookupCommand(sds name); 1310 struct redisCommand *lookupCommandByCString(char *s); 1311 struct redisCommand *lookupCommandOrOriginal(sds name); 1312 void call(client *c, int flags); 1313 void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int flags); 1314 void alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int target); 1315 void forceCommandPropagation(client *c, int flags); 1316 void preventCommandPropagation(client *c); 1317 void preventCommandAOF(client *c); 1318 void preventCommandReplication(client *c); 1319 int prepareForShutdown(); 1320 #ifdef __GNUC__ 1321 void serverLog(int level, const char *fmt, ...) 1322 __attribute__((format(printf, 2, 3))); 1323 #else 1324 void serverLog(int level, const char *fmt, ...); 1325 #endif 1326 void serverLogRaw(int level, const char *msg); 1327 void serverLogFromHandler(int level, const char *msg); 1328 void usage(void); 1329 void updateDictResizePolicy(void); 1330 int htNeedsResize(dict *dict); 1331 void populateCommandTable(void); 1332 void resetCommandTableStats(void); 1333 void adjustOpenFilesLimit(void); 1334 void closeListeningSockets(int unlink_unix_socket); 1335 void updateCachedTime(void); 1336 void resetServerStats(void); 1337 unsigned int getLRUClock(void); 1338 const char *evictPolicyToString(void); 1339 1340 #define RESTART_SERVER_NONE 0 1341 #define RESTART_SERVER_GRACEFULLY (1<<0) /* Do proper shutdown. */ 1342 #define RESTART_SERVER_CONFIG_REWRITE (1<<1) /* CONFIG REWRITE before restart.*/ 1343 int restartServer(int flags, mstime_t delay); 1344 1345 /* Set data type */ 1346 robj *setTypeCreate(robj *value); 1347 int setTypeAdd(robj *subject, robj *value); 1348 int setTypeRemove(robj *subject, robj *value); 1349 int setTypeIsMember(robj *subject, robj *value); 1350 setTypeIterator *setTypeInitIterator(robj *subject); 1351 void setTypeReleaseIterator(setTypeIterator *si); 1352 int setTypeNext(setTypeIterator *si, robj **objele, int64_t *llele); 1353 robj *setTypeNextObject(setTypeIterator *si); 1354 int setTypeRandomElement(robj *setobj, robj **objele, int64_t *llele); 1355 unsigned long setTypeRandomElements(robj *set, unsigned long count, robj *aux_set); 1356 unsigned long setTypeSize(robj *subject); 1357 void setTypeConvert(robj *subject, int enc); 1358 1359 /* Hash data type */ 1360 void hashTypeConvert(robj *o, int enc); 1361 void hashTypeTryConversion(robj *subject, robj **argv, int start, int end); 1362 void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2); 1363 robj *hashTypeGetObject(robj *o, robj *key); 1364 int hashTypeExists(robj *o, robj *key); 1365 int hashTypeSet(robj *o, robj *key, robj *value); 1366 int hashTypeDelete(robj *o, robj *key); 1367 unsigned long hashTypeLength(robj *o); 1368 hashTypeIterator *hashTypeInitIterator(robj *subject); 1369 void hashTypeReleaseIterator(hashTypeIterator *hi); 1370 int hashTypeNext(hashTypeIterator *hi); 1371 void hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what, 1372 unsigned char **vstr, 1373 unsigned int *vlen, 1374 long long *vll); 1375 void hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what, robj **dst); 1376 robj *hashTypeCurrentObject(hashTypeIterator *hi, int what); 1377 robj *hashTypeLookupWriteOrCreate(client *c, robj *key); 1378 1379 /* Pub / Sub */ 1380 int pubsubUnsubscribeAllChannels(client *c, int notify); 1381 int pubsubUnsubscribeAllPatterns(client *c, int notify); 1382 void freePubsubPattern(void *p); 1383 int listMatchPubsubPattern(void *a, void *b); 1384 int pubsubPublishMessage(robj *channel, robj *message); 1385 1386 /* Keyspace events notification */ 1387 void notifyKeyspaceEvent(int type, char *event, robj *key, int dbid); 1388 int keyspaceEventsStringToFlags(char *classes); 1389 sds keyspaceEventsFlagsToString(int flags); 1390 1391 /* Configuration */ 1392 void loadServerConfig(char *filename, char *options); 1393 void appendServerSaveParams(time_t seconds, int changes); 1394 void resetServerSaveParams(void); 1395 struct rewriteConfigState; /* Forward declaration to export API. */ 1396 void rewriteConfigRewriteLine(struct rewriteConfigState *state, const char *option, sds line, int force); 1397 int rewriteConfig(char *path); 1398 1399 /* db.c -- Keyspace access API */ 1400 int removeExpire(redisDb *db, robj *key); 1401 void propagateExpire(redisDb *db, robj *key); 1402 int expireIfNeeded(redisDb *db, robj *key); 1403 long long getExpire(redisDb *db, robj *key); 1404 void setExpire(redisDb *db, robj *key, long long when); 1405 robj *lookupKey(redisDb *db, robj *key, int flags); 1406 robj *lookupKeyRead(redisDb *db, robj *key); 1407 robj *lookupKeyWrite(redisDb *db, robj *key); 1408 robj *lookupKeyReadOrReply(client *c, robj *key, robj *reply); 1409 robj *lookupKeyWriteOrReply(client *c, robj *key, robj *reply); 1410 robj *lookupKeyReadWithFlags(redisDb *db, robj *key, int flags); 1411 #define LOOKUP_NONE 0 1412 #define LOOKUP_NOTOUCH (1<<0) 1413 void dbAdd(redisDb *db, robj *key, robj *val); 1414 void dbOverwrite(redisDb *db, robj *key, robj *val); 1415 void setKey(redisDb *db, robj *key, robj *val); 1416 int dbExists(redisDb *db, robj *key); 1417 robj *dbRandomKey(redisDb *db); 1418 int dbDelete(redisDb *db, robj *key); 1419 robj *dbUnshareStringValue(redisDb *db, robj *key, robj *o); 1420 long long emptyDb(void(callback)(void*)); 1421 int selectDb(client *c, int id); 1422 void signalModifiedKey(redisDb *db, robj *key); 1423 void signalFlushedDb(int dbid); 1424 unsigned int getKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count); 1425 unsigned int countKeysInSlot(unsigned int hashslot); 1426 unsigned int delKeysInSlot(unsigned int hashslot); 1427 int verifyClusterConfigWithData(void); 1428 void scanGenericCommand(client *c, robj *o, unsigned long cursor); 1429 int parseScanCursorOrReply(client *c, robj *o, unsigned long *cursor); 1430 1431 /* API to get key arguments from commands */ 1432 int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); 1433 void getKeysFreeResult(int *result); 1434 int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys); 1435 int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); 1436 int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); 1437 int *migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys); 1438 1439 /* Cluster */ 1440 void clusterInit(void); 1441 unsigned short crc16(const char *buf, int len); 1442 unsigned int keyHashSlot(char *key, int keylen); 1443 void clusterCron(void); 1444 void clusterPropagatePublish(robj *channel, robj *message); 1445 void migrateCloseTimedoutSockets(void); 1446 void clusterBeforeSleep(void); 1447 1448 /* Sentinel */ 1449 void initSentinelConfig(void); 1450 void initSentinel(void); 1451 void sentinelTimer(void); 1452 char *sentinelHandleConfiguration(char **argv, int argc); 1453 void sentinelIsRunning(void); 1454 1455 /* redis-check-rdb */ 1456 int redis_check_rdb(char *rdbfilename); 1457 int redis_check_rdb_main(int argc, char **argv); 1458 1459 /* Scripting */ 1460 void scriptingInit(int setup); 1461 int ldbRemoveChild(pid_t pid); 1462 void ldbKillForkedSessions(void); 1463 int ldbPendingChildren(void); 1464 1465 /* Blocked clients */ 1466 void processUnblockedClients(void); 1467 void blockClient(client *c, int btype); 1468 void unblockClient(client *c); 1469 void replyToBlockedClientTimedOut(client *c); 1470 int getTimeoutFromObjectOrReply(client *c, robj *object, mstime_t *timeout, int unit); 1471 void disconnectAllBlockedClients(void); 1472 1473 /* Git SHA1 */ 1474 char *redisGitSHA1(void); 1475 char *redisGitDirty(void); 1476 uint64_t redisBuildId(void); 1477 1478 /* Commands prototypes */ 1479 void authCommand(client *c); 1480 void pingCommand(client *c); 1481 void echoCommand(client *c); 1482 void commandCommand(client *c); 1483 void setCommand(client *c); 1484 void setnxCommand(client *c); 1485 void setexCommand(client *c); 1486 void psetexCommand(client *c); 1487 void getCommand(client *c); 1488 void delCommand(client *c); 1489 void existsCommand(client *c); 1490 void setbitCommand(client *c); 1491 void getbitCommand(client *c); 1492 void bitfieldCommand(client *c); 1493 void setrangeCommand(client *c); 1494 void getrangeCommand(client *c); 1495 void incrCommand(client *c); 1496 void decrCommand(client *c); 1497 void incrbyCommand(client *c); 1498 void decrbyCommand(client *c); 1499 void incrbyfloatCommand(client *c); 1500 void selectCommand(client *c); 1501 void randomkeyCommand(client *c); 1502 void keysCommand(client *c); 1503 void scanCommand(client *c); 1504 void dbsizeCommand(client *c); 1505 void lastsaveCommand(client *c); 1506 void saveCommand(client *c); 1507 void bgsaveCommand(client *c); 1508 void bgrewriteaofCommand(client *c); 1509 void shutdownCommand(client *c); 1510 void moveCommand(client *c); 1511 void renameCommand(client *c); 1512 void renamenxCommand(client *c); 1513 void lpushCommand(client *c); 1514 void rpushCommand(client *c); 1515 void lpushxCommand(client *c); 1516 void rpushxCommand(client *c); 1517 void linsertCommand(client *c); 1518 void lpopCommand(client *c); 1519 void rpopCommand(client *c); 1520 void llenCommand(client *c); 1521 void lindexCommand(client *c); 1522 void lrangeCommand(client *c); 1523 void ltrimCommand(client *c); 1524 void typeCommand(client *c); 1525 void lsetCommand(client *c); 1526 void saddCommand(client *c); 1527 void sremCommand(client *c); 1528 void smoveCommand(client *c); 1529 void sismemberCommand(client *c); 1530 void scardCommand(client *c); 1531 void spopCommand(client *c); 1532 void srandmemberCommand(client *c); 1533 void sinterCommand(client *c); 1534 void sinterstoreCommand(client *c); 1535 void sunionCommand(client *c); 1536 void sunionstoreCommand(client *c); 1537 void sdiffCommand(client *c); 1538 void sdiffstoreCommand(client *c); 1539 void sscanCommand(client *c); 1540 void syncCommand(client *c); 1541 void flushdbCommand(client *c); 1542 void flushallCommand(client *c); 1543 void sortCommand(client *c); 1544 void lremCommand(client *c); 1545 void rpoplpushCommand(client *c); 1546 void infoCommand(client *c); 1547 void mgetCommand(client *c); 1548 void monitorCommand(client *c); 1549 void expireCommand(client *c); 1550 void expireatCommand(client *c); 1551 void pexpireCommand(client *c); 1552 void pexpireatCommand(client *c); 1553 void getsetCommand(client *c); 1554 void ttlCommand(client *c); 1555 void touchCommand(client *c); 1556 void pttlCommand(client *c); 1557 void persistCommand(client *c); 1558 void slaveofCommand(client *c); 1559 void roleCommand(client *c); 1560 void debugCommand(client *c); 1561 void msetCommand(client *c); 1562 void msetnxCommand(client *c); 1563 void zaddCommand(client *c); 1564 void zincrbyCommand(client *c); 1565 void zrangeCommand(client *c); 1566 void zrangebyscoreCommand(client *c); 1567 void zrevrangebyscoreCommand(client *c); 1568 void zrangebylexCommand(client *c); 1569 void zrevrangebylexCommand(client *c); 1570 void zcountCommand(client *c); 1571 void zlexcountCommand(client *c); 1572 void zrevrangeCommand(client *c); 1573 void zcardCommand(client *c); 1574 void zremCommand(client *c); 1575 void zscoreCommand(client *c); 1576 void zremrangebyscoreCommand(client *c); 1577 void zremrangebylexCommand(client *c); 1578 void multiCommand(client *c); 1579 void execCommand(client *c); 1580 void discardCommand(client *c); 1581 void blpopCommand(client *c); 1582 void brpopCommand(client *c); 1583 void brpoplpushCommand(client *c); 1584 void appendCommand(client *c); 1585 void strlenCommand(client *c); 1586 void zrankCommand(client *c); 1587 void zrevrankCommand(client *c); 1588 void hsetCommand(client *c); 1589 void hsetnxCommand(client *c); 1590 void hgetCommand(client *c); 1591 void hmsetCommand(client *c); 1592 void hmgetCommand(client *c); 1593 void hdelCommand(client *c); 1594 void hlenCommand(client *c); 1595 void hstrlenCommand(client *c); 1596 void zremrangebyrankCommand(client *c); 1597 void zunionstoreCommand(client *c); 1598 void zinterstoreCommand(client *c); 1599 void zscanCommand(client *c); 1600 void hkeysCommand(client *c); 1601 void hvalsCommand(client *c); 1602 void hgetallCommand(client *c); 1603 void hexistsCommand(client *c); 1604 void hscanCommand(client *c); 1605 void configCommand(client *c); 1606 void hincrbyCommand(client *c); 1607 void hincrbyfloatCommand(client *c); 1608 void subscribeCommand(client *c); 1609 void unsubscribeCommand(client *c); 1610 void psubscribeCommand(client *c); 1611 void punsubscribeCommand(client *c); 1612 void publishCommand(client *c); 1613 void pubsubCommand(client *c); 1614 void watchCommand(client *c); 1615 void unwatchCommand(client *c); 1616 void clusterCommand(client *c); 1617 void restoreCommand(client *c); 1618 void migrateCommand(client *c); 1619 void askingCommand(client *c); 1620 void readonlyCommand(client *c); 1621 void readwriteCommand(client *c); 1622 void dumpCommand(client *c); 1623 void objectCommand(client *c); 1624 void clientCommand(client *c); 1625 void evalCommand(client *c); 1626 void evalShaCommand(client *c); 1627 void scriptCommand(client *c); 1628 void timeCommand(client *c); 1629 void bitopCommand(client *c); 1630 void bitcountCommand(client *c); 1631 void bitposCommand(client *c); 1632 void replconfCommand(client *c); 1633 void waitCommand(client *c); 1634 void geoencodeCommand(client *c); 1635 void geodecodeCommand(client *c); 1636 void georadiusByMemberCommand(client *c); 1637 void georadiusCommand(client *c); 1638 void geoaddCommand(client *c); 1639 void geohashCommand(client *c); 1640 void geoposCommand(client *c); 1641 void geodistCommand(client *c); 1642 void pfselftestCommand(client *c); 1643 void pfaddCommand(client *c); 1644 void pfcountCommand(client *c); 1645 void pfmergeCommand(client *c); 1646 void pfdebugCommand(client *c); 1647 void latencyCommand(client *c); 1648 1649 #if defined(__GNUC__) 1650 void *calloc(size_t count, size_t size) __attribute__ ((deprecated)); 1651 void free(void *ptr) __attribute__ ((deprecated)); 1652 void *malloc(size_t size) __attribute__ ((deprecated)); 1653 void *realloc(void *ptr, size_t size) __attribute__ ((deprecated)); 1654 #endif 1655 1656 /* Debugging stuff */ 1657 void _serverAssertWithInfo(client *c, robj *o, char *estr, char *file, int line); 1658 void _serverAssert(char *estr, char *file, int line); 1659 void _serverPanic(char *msg, char *file, int line); 1660 void bugReportStart(void); 1661 void serverLogObjectDebugInfo(robj *o); 1662 void sigsegvHandler(int sig, siginfo_t *info, void *secret); 1663 sds genRedisInfoString(char *section); 1664 void enableWatchdog(int period); 1665 void disableWatchdog(void); 1666 void watchdogScheduleSignal(int period); 1667 void serverLogHexDump(int level, char *descr, void *value, size_t len); 1668 int memtest_preserving_test(unsigned long *m, size_t bytes, int passes); 1669 1670 #define redisDebug(fmt, ...) \ 1671 printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__) 1672 #define redisDebugMark() \ 1673 printf("-- MARK %s:%d --\n", __FILE__, __LINE__) 1674 1675 #endif 1676