From d6436eb7cf690afee69a4a3b9b61bf3241eed645 Mon Sep 17 00:00:00 2001 From: zhenwei pi Date: Sat, 2 May 2020 20:05:39 +0800 Subject: [PATCH 01/13] Support setcpuaffinity on linux/bsd Currently, there are several types of threads/child processes of a redis server. Sometimes we need deeply optimise the performance of redis, so we would like to isolate threads/processes. There were some discussion about cpu affinity cases in the issue: https://github.com/antirez/redis/issues/2863 So implement cpu affinity setting by redis.conf in this patch, then we can config server_cpulist/bio_cpulist/aof_rewrite_cpulist/ bgsave_cpulist by cpu list. Examples of cpulist in redis.conf: server_cpulist 0-7:2 means cpu affinity 0,2,4,6 bio_cpulist 1,3 means cpu affinity 1,3 aof_rewrite_cpulist 8-11 means cpu affinity 8,9,10,11 bgsave_cpulist 1,10-11 means cpu affinity 1,10,11 Test on linux/freebsd, both work fine. Signed-off-by: zhenwei pi --- redis.conf | 15 ++++ src/Makefile | 2 +- src/aof.c | 1 + src/bio.c | 2 + src/config.c | 4 ++ src/config.h | 6 ++ src/networking.c | 1 + src/rdb.c | 2 + src/server.c | 9 +++ src/server.h | 6 ++ src/setcpuaffinity.c | 129 +++++++++++++++++++++++++++++++++++ tests/unit/introspection.tcl | 4 ++ 12 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 src/setcpuaffinity.c diff --git a/redis.conf b/redis.conf index 6cec636bc..9c6171d4c 100644 --- a/redis.conf +++ b/redis.conf @@ -1781,3 +1781,18 @@ rdb-save-incremental-fsync yes # Maximum number of set/hash/zset/list fields that will be processed from # the main dictionary scan # active-defrag-max-scan-fields 1000 + +# Redis server/IO threads, bio threads, aof rewrite child process, and bgsave +# child process cpu affinity list config. syntax of cpu list looks like taskset +# command. serveral examples: +# set redis server/io threads to cpu affinity 0,2,4,6 +# server_cpulist 0-7:2 +# +# set bio threads to cpu affinity 1,3 +# bio_cpulist 1,3 +# +# set aof rewrite child process to cpu affinity 8,9,10,11 +# aof_rewrite_cpulist 8-11 +# +# set bgsave child process to cpu affinity 1,10,11 +# bgsave_cpulist 1,10-11 diff --git a/src/Makefile b/src/Makefile index f9922afce..55f862cfc 100644 --- a/src/Makefile +++ b/src/Makefile @@ -206,7 +206,7 @@ endif REDIS_SERVER_NAME=redis-server REDIS_SENTINEL_NAME=redis-sentinel -REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o gopher.o tracking.o connection.o tls.o sha256.o timeout.o +REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o gopher.o tracking.o connection.o tls.o sha256.o timeout.o setcpuaffinity.o REDIS_CLI_NAME=redis-cli REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o ae.o crcspeed.o crc64.o siphash.o crc16.o REDIS_BENCHMARK_NAME=redis-benchmark diff --git a/src/aof.c b/src/aof.c index 301a40848..02409abe6 100644 --- a/src/aof.c +++ b/src/aof.c @@ -1596,6 +1596,7 @@ int rewriteAppendOnlyFileBackground(void) { /* Child */ redisSetProcTitle("redis-aof-rewrite"); + redisSetCpuAffinity(server.aof_rewrite_cpulist); snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid()); if (rewriteAppendOnlyFile(tmpfile) == C_OK) { sendChildCOWInfo(CHILD_INFO_TYPE_AOF, "AOF rewrite"); diff --git a/src/bio.c b/src/bio.c index 85f681185..69c62fc6f 100644 --- a/src/bio.c +++ b/src/bio.c @@ -166,6 +166,8 @@ void *bioProcessBackgroundJobs(void *arg) { break; } + redisSetCpuAffinity(server.bio_cpulist); + /* Make the thread killable at any time, so that bioKillThreads() * can work reliably. */ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); diff --git a/src/config.c b/src/config.c index e0cbcc281..64854592c 100644 --- a/src/config.c +++ b/src/config.c @@ -2133,6 +2133,10 @@ standardConfig configs[] = { createStringConfig("syslog-ident", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.syslog_ident, "redis", NULL, NULL), createStringConfig("dbfilename", NULL, MODIFIABLE_CONFIG, ALLOW_EMPTY_STRING, server.rdb_filename, "dump.rdb", isValidDBfilename, NULL), createStringConfig("appendfilename", NULL, IMMUTABLE_CONFIG, ALLOW_EMPTY_STRING, server.aof_filename, "appendonly.aof", isValidAOFfilename, NULL), + createStringConfig("server_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.server_cpulist, NULL, NULL, NULL), + createStringConfig("bio_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.bio_cpulist, NULL, NULL, NULL), + createStringConfig("aof_rewrite_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.aof_rewrite_cpulist, NULL, NULL, NULL), + createStringConfig("bgsave_cpulist", NULL, IMMUTABLE_CONFIG, EMPTY_STRING_IS_NULL, server.bgsave_cpulist, NULL, NULL, NULL), /* Enum Configs */ createEnumConfig("supervised", NULL, IMMUTABLE_CONFIG, supervised_mode_enum, server.supervised_mode, SUPERVISED_NONE, NULL, NULL), diff --git a/src/config.h b/src/config.h index 40dc683ce..a322ce354 100644 --- a/src/config.h +++ b/src/config.h @@ -244,4 +244,10 @@ int pthread_setname_np(const char *name); #endif #endif +/* Check if we can use setcpuaffinity(). */ +#if (defined __linux || defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__) +#define USE_SETCPUAFFINITY +void setcpuaffinity(const char *cpulist); +#endif + #endif diff --git a/src/networking.c b/src/networking.c index d62533e3e..fd1159e06 100644 --- a/src/networking.c +++ b/src/networking.c @@ -2872,6 +2872,7 @@ void *IOThreadMain(void *myid) { snprintf(thdname, sizeof(thdname), "io_thd_%ld", id); redis_set_thread_title(thdname); + redisSetCpuAffinity(server.server_cpulist); while(1) { /* Wait for start */ diff --git a/src/rdb.c b/src/rdb.c index c882efcb4..5cec208c5 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -1351,6 +1351,7 @@ int rdbSaveBackground(char *filename, rdbSaveInfo *rsi) { /* Child */ redisSetProcTitle("redis-rdb-bgsave"); + redisSetCpuAffinity(server.bgsave_cpulist); retval = rdbSave(filename,rsi); if (retval == C_OK) { sendChildCOWInfo(CHILD_INFO_TYPE_RDB, "RDB"); @@ -2488,6 +2489,7 @@ int rdbSaveToSlavesSockets(rdbSaveInfo *rsi) { rioInitWithFd(&rdb,server.rdb_pipe_write); redisSetProcTitle("redis-rdb-to-slaves"); + redisSetCpuAffinity(server.bgsave_cpulist); retval = rdbSaveRioWithEOFMark(&rdb,NULL,rsi); if (retval == C_OK && rioFlush(&rdb) == 0) diff --git a/src/server.c b/src/server.c index 659604ef3..1baa044be 100644 --- a/src/server.c +++ b/src/server.c @@ -4850,6 +4850,14 @@ void redisSetProcTitle(char *title) { #endif } +void redisSetCpuAffinity(const char *cpulist) { +#ifdef USE_SETCPUAFFINITY + setcpuaffinity(cpulist); +#else + UNUSED(cpulist); +#endif +} + /* * Check whether systemd or upstart have been used to start redis. */ @@ -5118,6 +5126,7 @@ int main(int argc, char **argv) { serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory); } + redisSetCpuAffinity(server.server_cpulist); aeSetBeforeSleepProc(server.el,beforeSleep); aeSetAfterSleepProc(server.el,afterSleep); aeMain(server.el); diff --git a/src/server.h b/src/server.h index 41d767e13..af435b148 100644 --- a/src/server.h +++ b/src/server.h @@ -1433,6 +1433,11 @@ struct redisServer { int tls_replication; int tls_auth_clients; redisTLSContextConfig tls_ctx_config; + /* cpu affinity */ + char *server_cpulist; /* cpu affinity list of redis server main/io thread. */ + char *bio_cpulist; /* cpu affinity list of bio thread. */ + char *aof_rewrite_cpulist; /* cpu affinity list of aof rewrite process. */ + char *bgsave_cpulist; /* cpu affinity list of bgsave process. */ }; typedef struct pubsubPattern { @@ -1585,6 +1590,7 @@ void exitFromChild(int retcode); size_t redisPopcount(void *s, long count); void redisSetProcTitle(char *title); int redisCommunicateSystemd(const char *sd_notify_msg); +void redisSetCpuAffinity(const char *cpulist); /* networking.c -- Networking and Client related operations */ client *createClient(connection *conn); diff --git a/src/setcpuaffinity.c b/src/setcpuaffinity.c new file mode 100644 index 000000000..1a12795cd --- /dev/null +++ b/src/setcpuaffinity.c @@ -0,0 +1,129 @@ +/* ========================================================================== + * setproctitle.c - Linux/BSD setcpuaffinity. + * -------------------------------------------------------------------------- + * Copyright (C) 2020 zhenwei pi + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN + * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * ========================================================================== + */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#ifdef __linux__ +#include +#endif +#ifdef __FreeBSD__ +#include +#include +#endif +#include "config.h" + +#ifdef USE_SETCPUAFFINITY +static const char *next_token(const char *q, int sep) { + if (q) + q = strchr(q, sep); + if (q) + q++; + + return q; +} + +static int next_num(const char *str, char **end, int *result) { + if (!str || *str == '\0' || !isdigit(*str)) + return -1; + + *result = strtoul(str, end, 10); + if (str == *end) + return -1; + + return 0; +} + +/* set current thread cpu affinity to cpu list, this function works like + * taskset command (actually cpulist parsing logic reference to util-linux). + * example of this function: "0,2,3", "0,2-3", "0-20:2". */ +void setcpuaffinity(const char *cpulist) { + const char *p, *q; + char *end = NULL; +#ifdef __linux__ + cpu_set_t cpuset; +#endif +#ifdef __FreeBSD__ + cpuset_t cpuset; +#endif + + if (!cpulist) + return; + + CPU_ZERO(&cpuset); + + q = cpulist; + while (p = q, q = next_token(q, ','), p) { + int a, b, s; + const char *c1, *c2; + + if (next_num(p, &end, &a) != 0) + return; + + b = a; + s = 1; + p = end; + + c1 = next_token(p, '-'); + c2 = next_token(p, ','); + + if (c1 != NULL && (c2 == NULL || c1 < c2)) { + if (next_num(c1, &end, &b) != 0) + return; + + c1 = end && *end ? next_token(end, ':') : NULL; + if (c1 != NULL && (c2 == NULL || c1 < c2)) { + if (next_num(c1, &end, &s) != 0) + return; + + if (s == 0) + return; + } + } + + if ((a > b)) + return; + + while (a <= b) { + CPU_SET(a, &cpuset); + a += s; + } + } + + if (end && *end) + return; + +#ifdef __linux__ + sched_setaffinity(0, sizeof(cpuset), &cpuset); +#endif +#ifdef __FreeBSD__ + cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset), &cpuset); +#endif +} + +#endif /* USE_SETCPUAFFINITY */ diff --git a/tests/unit/introspection.tcl b/tests/unit/introspection.tcl index cd905084a..b60ca0d48 100644 --- a/tests/unit/introspection.tcl +++ b/tests/unit/introspection.tcl @@ -94,6 +94,10 @@ start_server {tags {"introspection"}} { slaveof bind requirepass + server_cpulist + bio_cpulist + aof_rewrite_cpulist + bgsave_cpulist } set configs {} From 4efb25d9c0193954c9b25c05350aa910e7f9758b Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 4 May 2020 11:05:48 +0200 Subject: [PATCH 02/13] Rework a bit the documentation for CPU pinning. --- redis.conf | 24 +++++++++++++++++------- src/setcpuaffinity.c | 2 +- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/redis.conf b/redis.conf index 9c6171d4c..bed906058 100644 --- a/redis.conf +++ b/redis.conf @@ -1782,17 +1782,27 @@ rdb-save-incremental-fsync yes # the main dictionary scan # active-defrag-max-scan-fields 1000 -# Redis server/IO threads, bio threads, aof rewrite child process, and bgsave -# child process cpu affinity list config. syntax of cpu list looks like taskset -# command. serveral examples: -# set redis server/io threads to cpu affinity 0,2,4,6 +# It is possible to pin different threads and processes of Redis to specific +# CPUs in your system, in order to maximize the performances of the server. +# This is useful both in order to pin different Redis threads in different +# CPUs, but also in order to make sure that multiple Redis instances running +# in the same host will be pinned to different CPUs. +# +# Normally you can do this using the "taskset" command, however it is also +# possible to this via Redis configuration directly, both in Linux and FreeBSD. +# +# You can pin the server/IO threads, bio threads, aof rewrite child process, and +# the bgsave child process. The syntax to specify the cpu list is the same as +# the taskset command: +# +# Set redis server/io threads to cpu affinity 0,2,4,6: # server_cpulist 0-7:2 # -# set bio threads to cpu affinity 1,3 +# Set bio threads to cpu affinity 1,3: # bio_cpulist 1,3 # -# set aof rewrite child process to cpu affinity 8,9,10,11 +# Set aof rewrite child process to cpu affinity 8,9,10,11: # aof_rewrite_cpulist 8-11 # -# set bgsave child process to cpu affinity 1,10,11 +# Set bgsave child process to cpu affinity 1,10,11 # bgsave_cpulist 1,10-11 diff --git a/src/setcpuaffinity.c b/src/setcpuaffinity.c index 1a12795cd..dcae81c71 100644 --- a/src/setcpuaffinity.c +++ b/src/setcpuaffinity.c @@ -1,5 +1,5 @@ /* ========================================================================== - * setproctitle.c - Linux/BSD setcpuaffinity. + * setcpuaffinity.c - Linux/BSD setcpuaffinity. * -------------------------------------------------------------------------- * Copyright (C) 2020 zhenwei pi * From b062fd5235e7a66209a58be05738aff8f68f4ad8 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 4 May 2020 11:08:57 +0200 Subject: [PATCH 03/13] Fix NetBSD build by fixing redis_set_thread_title() support. See #7188. --- src/config.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/config.h b/src/config.h index a322ce354..6025d4e96 100644 --- a/src/config.h +++ b/src/config.h @@ -230,9 +230,12 @@ void setproctitle(const char *fmt, ...); #ifdef __linux__ #define redis_set_thread_title(name) pthread_setname_np(pthread_self(), name) #else -#if (defined __NetBSD__ || defined __FreeBSD__ || defined __OpenBSD__) +#if (defined __FreeBSD__ || defined __OpenBSD__) #include #define redis_set_thread_title(name) pthread_set_name_np(pthread_self(), name) +#elif defined __NetBSD__ +#include +#define redis_set_thread_title(name) pthread_setname_np(pthread_self(), name, NULL) #else #if (defined __APPLE__ && defined(MAC_OS_X_VERSION_10_7)) int pthread_setname_np(const char *name); From a6e55c096d8246a098e4cb9b42394d9bfabc6c01 Mon Sep 17 00:00:00 2001 From: hwware Date: Sat, 2 May 2020 19:20:44 -0400 Subject: [PATCH 04/13] Client Side Caching: Add Tracking Prefix Number Stats in Server Info --- src/server.c | 2 ++ src/server.h | 1 + src/tracking.c | 5 +++++ 3 files changed, 8 insertions(+) diff --git a/src/server.c b/src/server.c index 1baa044be..f2f5c8b3f 100644 --- a/src/server.c +++ b/src/server.c @@ -4281,6 +4281,7 @@ sds genRedisInfoString(const char *section) { "active_defrag_key_misses:%lld\r\n" "tracking_total_keys:%lld\r\n" "tracking_total_items:%lld\r\n" + "tracking_total_prefixes:%lld\r\n" "unexpected_error_replies:%lld\r\n", server.stat_numconnections, server.stat_numcommands, @@ -4311,6 +4312,7 @@ sds genRedisInfoString(const char *section) { server.stat_active_defrag_key_misses, (unsigned long long) trackingGetTotalKeys(), (unsigned long long) trackingGetTotalItems(), + (unsigned long long) trackingGetTotalPrefixes(), server.stat_unexpected_error_replies); } diff --git a/src/server.h b/src/server.h index af435b148..9c2b761c4 100644 --- a/src/server.h +++ b/src/server.h @@ -1695,6 +1695,7 @@ void trackingInvalidateKeysOnFlush(int dbid); void trackingLimitUsedSlots(void); uint64_t trackingGetTotalItems(void); uint64_t trackingGetTotalKeys(void); +uint64_t trackingGetTotalPrefixes(void); void trackingBroadcastInvalidationMessages(void); /* List data type */ diff --git a/src/tracking.c b/src/tracking.c index 48d231627..a995817e2 100644 --- a/src/tracking.c +++ b/src/tracking.c @@ -518,3 +518,8 @@ uint64_t trackingGetTotalKeys(void) { if (TrackingTable == NULL) return 0; return raxSize(TrackingTable); } + +uint64_t trackingGetTotalPrefixes(void) { + if (PrefixTable == NULL) return 0; + return raxSize(PrefixTable); +} From fc7bc3204534b4cf81c05eb14432356de5d1c536 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 5 May 2020 10:09:28 +0200 Subject: [PATCH 05/13] Fix CRC64 initialization outside the Redis server itself. --- src/crc64.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/crc64.c b/src/crc64.c index 4cbc019f6..9c555e98b 100644 --- a/src/crc64.c +++ b/src/crc64.c @@ -29,6 +29,7 @@ #include "crc64.h" #include "crcspeed.h" static uint64_t crc64_table[8][256] = {{0}}; +static int crc64_table_initialized = 0; #define POLY UINT64_C(0xad93d23594c935a9) /******************** BEGIN GENERATED PYCRC FUNCTIONS ********************/ @@ -115,10 +116,12 @@ uint64_t _crc64(uint_fast64_t crc, const void *in_data, const uint64_t len) { /* Initializes the 16KB lookup tables. */ void crc64_init(void) { crcspeed64native_init(_crc64, crc64_table); + crc64_table_initialized = 1; } /* Compute crc64 */ uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l) { + if (!crc64_table_initialized) crc64_init(); return crcspeed64native(crc64_table, crc, (void *) s, l); } From ac316d8cc57cef74c520558b024cb8c0babd7e14 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 5 May 2020 10:20:48 +0200 Subject: [PATCH 06/13] Move CRC64 initialization in main(). --- src/crc64.c | 3 --- src/server.c | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/crc64.c b/src/crc64.c index 9c555e98b..4cbc019f6 100644 --- a/src/crc64.c +++ b/src/crc64.c @@ -29,7 +29,6 @@ #include "crc64.h" #include "crcspeed.h" static uint64_t crc64_table[8][256] = {{0}}; -static int crc64_table_initialized = 0; #define POLY UINT64_C(0xad93d23594c935a9) /******************** BEGIN GENERATED PYCRC FUNCTIONS ********************/ @@ -116,12 +115,10 @@ uint64_t _crc64(uint_fast64_t crc, const void *in_data, const uint64_t len) { /* Initializes the 16KB lookup tables. */ void crc64_init(void) { crcspeed64native_init(_crc64, crc64_table); - crc64_table_initialized = 1; } /* Compute crc64 */ uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l) { - if (!crc64_table_initialized) crc64_init(); return crcspeed64native(crc64_table, crc, (void *) s, l); } diff --git a/src/server.c b/src/server.c index f2f5c8b3f..416dcd747 100644 --- a/src/server.c +++ b/src/server.c @@ -2899,7 +2899,6 @@ void initServer(void) { scriptingInit(1); slowlogInit(); latencyMonitorInit(); - crc64_init(); } /* Some steps in server initialization need to be done last (after modules @@ -4963,6 +4962,7 @@ int main(int argc, char **argv) { zmalloc_set_oom_handler(redisOutOfMemoryHandler); srand(time(NULL)^getpid()); gettimeofday(&tv,NULL); + crc64_init(); uint8_t hashseed[16]; getRandomBytes(hashseed,sizeof(hashseed)); From 897a360d060db7acb6d9898b486a067cd895d232 Mon Sep 17 00:00:00 2001 From: Muhammad Zahalqa Date: Mon, 4 May 2020 12:36:01 +0300 Subject: [PATCH 07/13] Fix compiler warnings on function rev(unsigned long) --- src/dict.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dict.c b/src/dict.c index ac6f8cfde..57619c280 100644 --- a/src/dict.c +++ b/src/dict.c @@ -766,9 +766,9 @@ dictEntry *dictGetFairRandomKey(dict *d) { /* Function to reverse bits. Algorithm from: * http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel */ static unsigned long rev(unsigned long v) { - unsigned long s = 8 * sizeof(v); // bit size; must be power of 2 - unsigned long mask = ~0; - while ((s >>= 1) > 0) { + unsigned long s = CHAR_BIT * sizeof(v); // bit size; must be power of 2 + unsigned long mask = ~0UL; + while ((s >>= 1) > 0UL) { mask ^= (mask << s); v = ((v >> s) & mask) | ((v << s) & ~mask); } From d1af82a8838764469e4536c984a61379f33e7260 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 5 May 2020 10:27:27 +0200 Subject: [PATCH 08/13] Drop not needed part from #7194. --- src/dict.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dict.c b/src/dict.c index 57619c280..13c185253 100644 --- a/src/dict.c +++ b/src/dict.c @@ -768,7 +768,7 @@ dictEntry *dictGetFairRandomKey(dict *d) { static unsigned long rev(unsigned long v) { unsigned long s = CHAR_BIT * sizeof(v); // bit size; must be power of 2 unsigned long mask = ~0UL; - while ((s >>= 1) > 0UL) { + while ((s >>= 1) > 0) { mask ^= (mask << s); v = ((v >> s) & mask) | ((v << s) & ~mask); } From 1e561cfaaf0a867f986569147a5cb9dd2cded1af Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Mon, 4 May 2020 08:09:21 -0700 Subject: [PATCH 09/13] Add --user argument to redis-benchmark.c (ACL) --- src/redis-benchmark.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/redis-benchmark.c b/src/redis-benchmark.c index 2df41580b..77daf981c 100644 --- a/src/redis-benchmark.c +++ b/src/redis-benchmark.c @@ -94,6 +94,7 @@ static struct config { sds dbnumstr; char *tests; char *auth; + const char *user; int precision; int num_threads; struct benchmarkThread **threads; @@ -258,7 +259,10 @@ static redisConfig *getRedisConfig(const char *ip, int port, if(config.auth) { void *authReply = NULL; - redisAppendCommand(c, "AUTH %s", config.auth); + if (config.user == NULL) + redisAppendCommand(c, "AUTH %s", config.auth); + else + redisAppendCommand(c, "AUTH %s %s", config.user, config.auth); if (REDIS_OK != redisGetReply(c, &authReply)) goto fail; if (reply) freeReplyObject(reply); reply = ((redisReply *) authReply); @@ -628,7 +632,12 @@ static client createClient(char *cmd, size_t len, client from, int thread_id) { c->prefix_pending = 0; if (config.auth) { char *buf = NULL; - int len = redisFormatCommand(&buf, "AUTH %s", config.auth); + int len; + if (config.user == NULL) + len = redisFormatCommand(&buf, "AUTH %s", config.auth); + else + len = redisFormatCommand(&buf, "AUTH %s %s", + config.user, config.auth); c->obuf = sdscatlen(c->obuf, buf, len); free(buf); c->prefix_pending++; @@ -1299,6 +1308,9 @@ int parseOptions(int argc, const char **argv) { } else if (!strcmp(argv[i],"-a") ) { if (lastarg) goto invalid; config.auth = strdup(argv[++i]); + } else if (!strcmp(argv[i],"--user")) { + if (lastarg) goto invalid; + config.user = argv[++i]; } else if (!strcmp(argv[i],"-d")) { if (lastarg) goto invalid; config.datasize = atoi(argv[++i]); @@ -1385,6 +1397,7 @@ usage: " -p Server port (default 6379)\n" " -s Server socket (overrides host and port)\n" " -a Password for Redis Auth\n" +" --user Used to send ACL style 'AUTH username pass'. Needs -a.\n" " -c Number of parallel connections (default 50)\n" " -n Total number of requests (default 100000)\n" " -d Data size of SET/GET value in bytes (default 3)\n" From e48c37316e281aaa137624bd32abde2ab47858ea Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 5 May 2020 13:18:53 +0200 Subject: [PATCH 10/13] Test: --dont-clean should do first cleanup. --- tests/test_helper.tcl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_helper.tcl b/tests/test_helper.tcl index 3eee1aeb7..de0a64728 100644 --- a/tests/test_helper.tcl +++ b/tests/test_helper.tcl @@ -216,9 +216,6 @@ proc run_solo {name code} { } proc cleanup {} { - if {$::dont_clean} { - return - } if {!$::quiet} {puts -nonewline "Cleanup: may take some time... "} flush stdout catch {exec rm -rf {*}[glob tests/tmp/redis.conf.*]} @@ -456,11 +453,11 @@ proc the_end {} { foreach failed $::failed_tests { puts "*** $failed" } - cleanup + if {!$::dont_clean} cleanup exit 1 } else { puts "\n[colorstr bold-white {\o/}] [colorstr bold-green {All tests passed without errors!}]\n" - cleanup + if {!$::dont_clean} cleanup exit 0 } } From ec1e106ec5c4ef9fd71d3ab3a6137012120058f4 Mon Sep 17 00:00:00 2001 From: Titouan Christophe Date: Mon, 4 May 2020 14:11:43 +0200 Subject: [PATCH 11/13] make struct user anonymous (only typedefed) This works because this struct is never referenced by its name, but always by its type. This prevents a conflict with struct user from when compiling against uclibc. Signed-off-by: Titouan Christophe --- src/server.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.h b/src/server.h index 9c2b761c4..59cf1370e 100644 --- a/src/server.h +++ b/src/server.h @@ -732,7 +732,7 @@ typedef struct readyList { no AUTH is needed, and every connection is immediately authenticated. */ -typedef struct user { +typedef struct { sds name; /* The username as an SDS string. */ uint64_t flags; /* See USER_FLAG_* */ From 84d9766d6a4bc04a642bfec3b82386483dd452c5 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 5 May 2020 13:40:33 +0200 Subject: [PATCH 12/13] Dump recent backlog on master query generating errors. --- src/networking.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/networking.c b/src/networking.c index fd1159e06..75c0c16b1 100644 --- a/src/networking.c +++ b/src/networking.c @@ -393,6 +393,35 @@ void addReplyErrorLength(client *c, const char *s, size_t len) { serverLog(LL_WARNING,"== CRITICAL == This %s is sending an error " "to its %s: '%s' after processing the command " "'%s'", from, to, s, cmdname); + if (ctype == CLIENT_TYPE_MASTER && server.repl_backlog && + server.repl_backlog_histlen > 0) + { + long long dumplen = 256; + if (server.repl_backlog_histlen < dumplen) + dumplen = server.repl_backlog_histlen; + + /* Identify the first byte to dump. */ + long long idx = + (server.repl_backlog_idx + (server.repl_backlog_size - dumplen)) % + server.repl_backlog_size; + + /* Scan the circular buffer to collect 'dumplen' bytes. */ + sds dump = sdsempty(); + while(dumplen) { + long long thislen = + ((server.repl_backlog_size - idx) < dumplen) ? + (server.repl_backlog_size - idx) : dumplen; + + dump = sdscatrepr(dump,server.repl_backlog+idx,thislen); + dumplen -= thislen; + idx = 0; + } + + /* Finally log such bytes: this is vital debugging info to + * understand what happened. */ + serverLog(LL_WARNING,"Latest backlog is: '%s'", dump); + sdsfree(dump); + } server.stat_unexpected_error_replies++; } } From cb683a84f7a9d530bec629f2b656e7d0842a0f75 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 5 May 2020 15:50:00 +0200 Subject: [PATCH 13/13] Don't propagate spurious MULTI on DEBUG LOADAOF. --- src/multi.c | 5 ++++- src/server.c | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/multi.c b/src/multi.c index cbbd2c513..a331a6240 100644 --- a/src/multi.c +++ b/src/multi.c @@ -172,7 +172,10 @@ void execCommand(client *c) { * This way we'll deliver the MULTI/..../EXEC block as a whole and * both the AOF and the replication link will have the same consistency * and atomicity guarantees. */ - if (!must_propagate && !(c->cmd->flags & (CMD_READONLY|CMD_ADMIN))) { + if (!must_propagate && + !server.loading && + !(c->cmd->flags & (CMD_READONLY|CMD_ADMIN))) + { execCommandPropagateMulti(c); must_propagate = 1; } diff --git a/src/server.c b/src/server.c index 416dcd747..e2b4b6f3d 100644 --- a/src/server.c +++ b/src/server.c @@ -3204,8 +3204,8 @@ void call(client *c, int flags) { server.fixed_time_expire++; - /* Sent the command to clients in MONITOR mode, only if the commands are - * not generated from reading an AOF. */ + /* Send the command to clients in MONITOR mode if applicable. + * Administrative commands are considered too dangerous to be shown. */ if (listLength(server.monitors) && !server.loading && !(c->cmd->flags & (CMD_SKIP_MONITOR|CMD_ADMIN)))