From cb82710daf9aa8ba564462ab083e3a4b2a0096fa Mon Sep 17 00:00:00 2001 From: John Sully Date: Sun, 1 Mar 2020 19:17:58 -0500 Subject: [PATCH 01/10] Rename cron command to KEYDB.CRON and add help Former-commit-id: 3eb847e28f6df45528dcebc6761290ff60248e78 --- src/help.h | 8 +++++++- src/server.cpp | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/help.h b/src/help.h index 34f4e77c7..d3870d490 100644 --- a/src/help.h +++ b/src/help.h @@ -1168,7 +1168,13 @@ struct commandHelp { "destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]", "Add multiple sorted sets and store the resulting sorted set in a new key", 4, - "2.0.0" } + "2.0.0" }, + { "KEYDB.CRON", + "name [single/repeat] [optional: start] delay script numkeys [key N] [arg N]", + "Run a specified script after start + delay, optionally repeating every delay interval. The job may be cancelled by deleting the key associated with the job (name parameter)", + 10, + "6.5.2" + } }; #endif diff --git a/src/server.cpp b/src/server.cpp index 837b1c38e..311f2bffa 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1026,7 +1026,7 @@ struct redisCommand redisCommandTable[] = { "read-only fast noprop", 0,NULL,0,0,0,0,0,0}, - {"cron",cronCommand,-5, + {"keydb.cron",cronCommand,-5, "write use-memory", 0,NULL,1,1,1,0,0,0}, }; From aab740a2e35330f2ff6d5deb55b67ce20d070656 Mon Sep 17 00:00:00 2001 From: John Sully Date: Sun, 1 Mar 2020 21:42:01 -0500 Subject: [PATCH 02/10] Update tests to reflect new CRON name (keydb.cron) Former-commit-id: 83f585e30ab9d37408c79b74e2536664325a051f --- tests/unit/cron.tcl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/unit/cron.tcl b/tests/unit/cron.tcl index 554df9328..dddf78255 100644 --- a/tests/unit/cron.tcl +++ b/tests/unit/cron.tcl @@ -1,47 +1,47 @@ start_server {tags {"CRON"}} { - test {cron singleshot past tense} { + test {keydb.cron singleshot past tense} { r flushall - r cron testjob single 0 1 {redis.call("incr", "testkey")} 1 testkey + r keydb.cron testjob single 0 1 {redis.call("incr", "testkey")} 1 testkey after 300 assert_equal 1 [r get testkey] assert_equal 0 [r exists testjob] } - test {cron repeat past tense next exec is in the future} { + test {keydb.cron repeat past tense next exec is in the future} { r flushall - r cron testjob repeat 0 1000000 {redis.call("incr", "testkey")} 1 testkey + r keydb.cron testjob repeat 0 1000000 {redis.call("incr", "testkey")} 1 testkey after 300 assert_equal 1 [r get testkey] assert_equal 1 [r exists testjob] r del testjob } - test {cron repeat works} { + test {keydb.cron repeat works} { r flushall - r cron testjob repeat 0 600 {redis.call("incr","testkey")} + r keydb.cron testjob repeat 0 600 {redis.call("incr","testkey")} after 1000 assert_equal 2 [r get testkey] } - test {cron overwrite works} { + test {keydb.cron overwrite works} { r flushall - r cron testjob single 500 {redis.call("set","testkey","a")} 1 testkey - r cron testjob single 500 {redis.call("set","anotherkey","b")} 1 anotherkey + r keydb.cron testjob single 500 {redis.call("set","testkey","a")} 1 testkey + r keydb.cron testjob single 500 {redis.call("set","anotherkey","b")} 1 anotherkey after 1000 assert_equal 0 [r exists testkey] assert_equal b [r get anotherkey] } - test {cron delete key stops job} { + test {keydb.cron delete key stops job} { r flushall - r cron testjob single 500 {redis.call("set","testkey","a")} + r keydb.cron testjob single 500 {redis.call("set","testkey","a")} r del testjob after 1000 assert_equal 0 [r exists testkey] } - test {cron zero interval rejected} { - catch {r cron testjob single 0 0 {redis.call("incr","testkey")} 1 testkey} e + test {keydb.cron zero interval rejected} { + catch {r keydb.cron testjob single 0 0 {redis.call("incr","testkey")} 1 testkey} e assert_match {ERR*} $e } } From 8366d765c0b28528e0b632b5750bc3ceef16d21d Mon Sep 17 00:00:00 2001 From: John Sully Date: Sun, 1 Mar 2020 22:16:25 -0500 Subject: [PATCH 03/10] Add option to build pro without license checks Former-commit-id: 0713518d2f34559a06db31a9023228b91ce60af3 --- src/Makefile | 5 +++++ src/server.cpp | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/Makefile b/src/Makefile index 45c70f6d6..cef895987 100644 --- a/src/Makefile +++ b/src/Makefile @@ -108,6 +108,11 @@ ifeq ($(USE_JEMALLOC),no) MALLOC=libc endif + +ifeq ($(NO_LICENSE_CHECK),yes) + CXXFLAGS+=-DNO_LICENSE_CHECK=1 +endif + # Override default settings if possible -include .make-settings diff --git a/src/server.cpp b/src/server.cpp index 53a16e54d..82f334fda 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1103,6 +1103,7 @@ void serverLog(int level, const char *fmt, ...) { static void checkTrialTimeout() { +#ifndef NO_LICENSE_CHECK if (cserver.license_key != nullptr && FValidKey(cserver.license_key, strlen(cserver.license_key))) return; time_t curtime = time(NULL); @@ -1118,6 +1119,7 @@ static void checkTrialTimeout() { serverLog(LL_WARNING, "Trial timeout in %ld:%02ld minutes", remaining/60, remaining % 60); } +#endif } /* Log a fixed message without printf-alike capabilities, in a way that is From 0f894365377fc5ab624eb51e0c49f914ecb605d2 Mon Sep 17 00:00:00 2001 From: John Sully Date: Tue, 3 Mar 2020 13:58:48 -0800 Subject: [PATCH 04/10] Update README.md Former-commit-id: c0882864f2fff0a32899e983ad4fe060eb71c1ad --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 70b879d52..d33b7e61d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ ##### Need Help? Check out our extensive [documentation](https://docs.keydb.dev). +##### Have feedback? Take our quick survey: https://www.surveymonkey.com/r/Y9XNS93 + What is KeyDB? -------------- From bd0bfe4d0558fdf645f9887683eb2bf42aa129e8 Mon Sep 17 00:00:00 2001 From: John Sully Date: Wed, 4 Mar 2020 15:14:41 -0500 Subject: [PATCH 05/10] min-clients-per-thread needs to use the new config code Former-commit-id: fed4bd5269a95a24f99d4542ceece09b9ad66d2c --- src/config.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/config.cpp b/src/config.cpp index 8f6cfbad5..de32ae85f 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -540,11 +540,6 @@ void loadServerConfigFromString(char *config) { } else if (!strcasecmp(argv[0],"enable-pro")) { cserver.fUsePro = true; break; - } else if (!strcasecmp(argv[0],"min-clients-per-thread") && argc == 2) { - cserver.thread_min_client_threshold = atoi(argv[1]); - if (cserver.thread_min_client_threshold < 0 || cserver.thread_min_client_threshold > 400) { - err = "min-thread-client must be between 0 and 400"; goto loaderr; - } } else { err = "Bad directive or wrong number of arguments"; goto loaderr; } @@ -2315,6 +2310,7 @@ standardConfig configs[] = { createIntConfig("hz", NULL, MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->config_hz, CONFIG_DEFAULT_HZ, INTEGER_CONFIG, NULL, updateHZ), createIntConfig("min-replicas-to-write", "min-slaves-to-write", MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->repl_min_slaves_to_write, 0, INTEGER_CONFIG, NULL, updateGoodSlaves), createIntConfig("min-replicas-max-lag", "min-slaves-max-lag", MODIFIABLE_CONFIG, 0, INT_MAX, g_pserver->repl_min_slaves_max_lag, 10, INTEGER_CONFIG, NULL, updateGoodSlaves), + createIntConfig("min-clients-per-thread", NULL, MODIFIABLE_CONFIG, 0, 400, cserver.thread_min_client_threshold, 50, INTEGER_CONFIG, NULL, NULL), /* Unsigned int configs */ createUIntConfig("maxclients", NULL, MODIFIABLE_CONFIG, 1, UINT_MAX, g_pserver->maxclients, 10000, INTEGER_CONFIG, NULL, updateMaxclients), From 550413070c7cbe3672504e5aa1c8eb70f03d45d1 Mon Sep 17 00:00:00 2001 From: John Sully Date: Wed, 4 Mar 2020 16:22:55 -0500 Subject: [PATCH 06/10] MOTD refactor Former-commit-id: 6c78ec7c41223b3381e19fdf78478f66b4959dc9 --- src/Makefile | 8 ++-- src/asciilogo.h | 5 +-- src/redis-cli.c | 115 +----------------------------------------------- src/server.cpp | 7 ++- 4 files changed, 14 insertions(+), 121 deletions(-) diff --git a/src/Makefile b/src/Makefile index a0e48831c..9ab3a8a72 100644 --- a/src/Makefile +++ b/src/Makefile @@ -105,7 +105,7 @@ endif FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS) FINAL_CXXFLAGS=$(CXX_STD) $(WARN) $(OPT) $(DEBUG) $(CXXFLAGS) $(REDIS_CFLAGS) FINAL_LDFLAGS=$(LDFLAGS) $(REDIS_LDFLAGS) $(DEBUG) -FINAL_LIBS=-lm +FINAL_LIBS=-lm -lcurl DEBUG=-g -ggdb ifeq ($(uname_S),SunOS) @@ -213,9 +213,9 @@ endif REDIS_SERVER_NAME=keydb-server REDIS_SENTINEL_NAME=keydb-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 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 acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o $(ASM_OBJ) +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 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 acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o motd.o $(ASM_OBJ) REDIS_CLI_NAME=keydb-cli -REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o redis-cli-cpphelper.o zmalloc.o release.o anet.o ae.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o new.o $(ASM_OBJ) +REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o redis-cli-cpphelper.o zmalloc.o release.o anet.o ae.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o new.o motd.o $(ASM_OBJ) REDIS_BENCHMARK_NAME=keydb-benchmark REDIS_BENCHMARK_OBJ=ae.o anet.o redis-benchmark.o adlist.o dict.o zmalloc.o siphash.o redis-benchmark.o storage-lite.o fastlock.o new.o $(ASM_OBJ) REDIS_CHECK_RDB_NAME=keydb-check-rdb @@ -285,7 +285,7 @@ $(REDIS_CHECK_AOF_NAME): $(REDIS_SERVER_NAME) # keydb-cli $(REDIS_CLI_NAME): $(REDIS_CLI_OBJ) - $(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS) -lcurl + $(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o $(FINAL_LIBS) # keydb-benchmark $(REDIS_BENCHMARK_NAME): $(REDIS_BENCHMARK_OBJ) diff --git a/src/asciilogo.h b/src/asciilogo.h index c8b18a0ac..1cbcce142 100644 --- a/src/asciilogo.h +++ b/src/asciilogo.h @@ -36,7 +36,6 @@ const char *ascii_logo = " Port: %d\n" " PID: %ld\n" " \n" -" Like KeyDB? Star us on GitHub! \n" +" %s\n" " \n" -" https://github.com/JohnSully/KeyDB \n" -" \n\n"; +" \n"; diff --git a/src/redis-cli.c b/src/redis-cli.c index 7a1f06341..7332c40e0 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -57,6 +57,7 @@ #include "anet.h" #include "ae.h" #include "storage.h" +#include "motd.h" #include "redis-cli.h" @@ -6536,118 +6537,6 @@ static void intrinsicLatencyMode(void) { } } -/*------------------------------------------------------------------------------ - * Message of the day - *--------------------------------------------------------------------------- */ -#ifdef MOTD -#include - -static const char *szMotdCachePath() -{ - static sds sdsMotdCachePath = NULL; - if (sdsMotdCachePath != NULL) - return sdsMotdCachePath; - - struct passwd *pw = getpwuid(getuid()); - if (pw == NULL) - return ""; - const char *homedir = pw->pw_dir; - sdsMotdCachePath = sdsnew(homedir); - sdsMotdCachePath = sdscat(sdsMotdCachePath, "/.keydb-cli-motd"); - return sdsMotdCachePath; -} -static size_t motd_write_callback(void *ptr, size_t size, size_t nmemb, sds *str) -{ - *str = sdscatlen(*str, ptr, size*nmemb); - return (size*nmemb); -} - -static char *fetchMOTDFromCache() -{ - struct stat attrib; - if (stat(szMotdCachePath(), &attrib) != 0) - return NULL; - time_t t = attrib.st_mtim.tv_sec; - time_t now = time(NULL); - if ((now - t) < 14400) - { - // If our cache was updated no more than 4 hours ago use it instead of fetching the MOTD - FILE *pf = fopen(szMotdCachePath(), "rb"); - if (pf == NULL) - return NULL; - fseek(pf, 0L, SEEK_END); - long cb = ftell(pf); - fseek(pf, 0L, SEEK_SET); // rewind - sds str = sdsnewlen(NULL, cb); - size_t cbRead = fread(str, 1, cb, pf); - fclose(pf); - if ((long)cbRead != cb) - { - sdsfree(str); - return NULL; - } - return str; - } - return NULL; -} - -static void setMOTDCache(const char *sz) -{ - FILE *pf = fopen(szMotdCachePath(), "wb"); - if (pf == NULL) - return; - size_t celem = fwrite(sz, strlen(sz), 1, pf); - (void)celem; // best effort - fclose(pf); -} - -static char *fetchMOTD() -{ - sds str; - CURL *curl; - CURLcode res; - - /* First try and get the string from the cache */ - str = fetchMOTDFromCache(); - if (str != NULL) - return str; - - str = sdsnew(""); - curl = curl_easy_init(); - if(curl) { - curl_easy_setopt(curl, CURLOPT_URL, "http://api.keydb.dev/motd/motd.txt"); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // follow redirects - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2); // take no more than two seconds - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, motd_write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str); - - /* Perform the request, res will get the return code */ - res = curl_easy_perform(curl); - /* Check for errors */ - if(res != CURLE_OK) - { - sdsfree(str); - str = NULL; - } - - /* always cleanup */ - curl_easy_cleanup(curl); - - if (str != NULL) - setMOTDCache(str); - } - return str; -} - -#else - -static char *fetchMOTD() -{ - return NULL; -} - -#endif - /*------------------------------------------------------------------------------ * Program main() *--------------------------------------------------------------------------- */ @@ -6818,7 +6707,7 @@ int main(int argc, char **argv) { if (argc == 0 && !config.eval) { /* Show the message of the day if we are interactive */ if (config.output == OUTPUT_STANDARD) { - char *szMotd = fetchMOTD(); + char *szMotd = fetchMOTD(1 /* cache */); if (szMotd != NULL) { printf("Message of the day:\n %s\n", szMotd); sdsfree(szMotd); diff --git a/src/server.cpp b/src/server.cpp index 311f2bffa..d6fe1ba1f 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -62,6 +62,7 @@ #include #include #include "aelocker.h" +#include "motd.h" int g_fTestMode = false; @@ -4749,14 +4750,18 @@ void redisAsciiArt(void) { mode, g_pserver->port ); } else { + sds motd = fetchMOTD(true); snprintf(buf,1024*16,ascii_logo, KEYDB_REAL_VERSION, redisGitSHA1(), strtol(redisGitDirty(),NULL,10) > 0, (sizeof(long) == 8) ? "64" : "32", mode, g_pserver->port, - (long) getpid() + (long) getpid(), + motd ? motd : "" ); + if (motd) + sdsfree(motd); serverLogRaw(LL_NOTICE|LL_RAW,buf); } zfree(buf); From 01a85ba7fed812d373aad9387a260b53b9371261 Mon Sep 17 00:00:00 2001 From: John Sully Date: Wed, 4 Mar 2020 17:09:12 -0500 Subject: [PATCH 07/10] Add extra logging when reporting errors from masters - especially in rreplay Former-commit-id: 5397f0b03312b8cace07a85333d8f035bdfb8d57 --- src/networking.cpp | 35 +++++++++++++++++++++++++++++++++++ src/replication.cpp | 2 ++ src/server.h | 1 + 3 files changed, 38 insertions(+) diff --git a/src/networking.cpp b/src/networking.cpp index 00322df72..58bd79bcc 100644 --- a/src/networking.cpp +++ b/src/networking.cpp @@ -177,6 +177,7 @@ client *createClient(int fd, int iel) { c->bufposAsync = 0; c->client_tracking_redirection = 0; c->casyncOpsPending = 0; + c->master_error = 0; memset(c->uuid, 0, UUID_BINARY_LEN); listSetFreeMethod(c->pubsub_patterns,decrRefCountVoid); @@ -432,6 +433,34 @@ void addReplyProtoAsync(client *c, const char *s, size_t len) { addReplyProtoCore(c, s, len, true); } +std::string escapeString(sds str) +{ + std::string newstr; + size_t len = sdslen(str); + for (size_t ich = 0; ich < len; ++ich) + { + char ch = str[ich]; + switch (ch) + { + case '\n': + newstr += "\\n"; + break; + + case '\t': + newstr += "\\t"; + break; + + case '\r': + newstr += "\\r"; + break; + + default: + newstr += ch; + } + } + return newstr; +} + /* Low level function called by the addReplyError...() functions. * It emits the protocol for a Redis error, in the form: * @@ -464,6 +493,12 @@ void addReplyErrorLengthCore(client *c, const char *s, size_t len, bool fAsync) serverLog(LL_WARNING,"== CRITICAL == This %s is sending an error " "to its %s: '%s' after processing the command " "'%s'", from, to, s, cmdname); + + if (c->querybuf && sdslen(c->querybuf)) { + std::string str = escapeString(c->querybuf); + serverLog(LL_WARNING, "\tquerybuf: %s", str.c_str()); + } + c->master_error = 1; } } diff --git a/src/replication.cpp b/src/replication.cpp index d7e92d308..2bdc21bf0 100644 --- a/src/replication.cpp +++ b/src/replication.cpp @@ -3482,6 +3482,8 @@ void replicaReplayCommand(client *c) cFake->flags &= ~(CLIENT_MASTER | CLIENT_PREVENT_REPL_PROP); bool fExec = ccmdPrev != serverTL->commandsExecuted; cFake->lock.unlock(); + if (cFake->master_error) + addReplyError(c, "Error in rreplay command, please check logs"); if (fExec || cFake->flags & CLIENT_MULTI) { addReply(c, shared.ok); diff --git a/src/server.h b/src/server.h index 58ea3e620..1e6632576 100644 --- a/src/server.h +++ b/src/server.h @@ -1337,6 +1337,7 @@ typedef struct client { int iel; /* the event loop index we're registered with */ struct fastlock lock; + int master_error; } client; struct saveparam { From 6a8fa9eeb13e5d3f1a1699f44ab9644584ad2f72 Mon Sep 17 00:00:00 2001 From: John Sully Date: Wed, 4 Mar 2020 17:13:18 -0500 Subject: [PATCH 08/10] Add missing files from last checkin Former-commit-id: 60c0d4104de3ff904758c200342e4d132b19501c --- src/motd.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/motd.h | 11 +++++ 2 files changed, 132 insertions(+) create mode 100644 src/motd.cpp create mode 100644 src/motd.h diff --git a/src/motd.cpp b/src/motd.cpp new file mode 100644 index 000000000..e46ba8819 --- /dev/null +++ b/src/motd.cpp @@ -0,0 +1,121 @@ +#include "sds.h" +#include +#include +#include +#include +#include +#include "motd.h" + +/*------------------------------------------------------------------------------ + * Message of the day + *--------------------------------------------------------------------------- */ +#ifdef MOTD +#include + +static const char *szMotdCachePath() +{ + static sds sdsMotdCachePath = NULL; + if (sdsMotdCachePath != NULL) + return sdsMotdCachePath; + + struct passwd *pw = getpwuid(getuid()); + if (pw == NULL) + return ""; + const char *homedir = pw->pw_dir; + sdsMotdCachePath = sdsnew(homedir); + sdsMotdCachePath = sdscat(sdsMotdCachePath, "/.keydb-cli-motd"); + return sdsMotdCachePath; +} +static size_t motd_write_callback(void *ptr, size_t size, size_t nmemb, sds *str) +{ + *str = sdscatlen(*str, ptr, size*nmemb); + return (size*nmemb); +} + +static char *fetchMOTDFromCache() +{ + struct stat attrib; + if (stat(szMotdCachePath(), &attrib) != 0) + return NULL; + time_t t = attrib.st_mtim.tv_sec; + time_t now = time(NULL); + if ((now - t) < 14400) + { + // If our cache was updated no more than 4 hours ago use it instead of fetching the MOTD + FILE *pf = fopen(szMotdCachePath(), "rb"); + if (pf == NULL) + return NULL; + fseek(pf, 0L, SEEK_END); + long cb = ftell(pf); + fseek(pf, 0L, SEEK_SET); // rewind + sds str = sdsnewlen(NULL, cb); + size_t cbRead = fread(str, 1, cb, pf); + fclose(pf); + if ((long)cbRead != cb) + { + sdsfree(str); + return NULL; + } + return str; + } + return NULL; +} + +static void setMOTDCache(const char *sz) +{ + FILE *pf = fopen(szMotdCachePath(), "wb"); + if (pf == NULL) + return; + size_t celem = fwrite(sz, strlen(sz), 1, pf); + (void)celem; // best effort + fclose(pf); +} + +extern "C" char *fetchMOTD(int cache) +{ + sds str; + CURL *curl; + CURLcode res; + + /* First try and get the string from the cache */ + if (cache) { + str = fetchMOTDFromCache(); + if (str != NULL) + return str; + } + + str = sdsnew(""); + curl = curl_easy_init(); + if(curl) { + curl_easy_setopt(curl, CURLOPT_URL, "http://api.keydb.dev/motd/motd.txt"); + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // follow redirects + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2); // take no more than two seconds + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, motd_write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &str); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + /* Check for errors */ + if(res != CURLE_OK) + { + sdsfree(str); + str = NULL; + } + + /* always cleanup */ + curl_easy_cleanup(curl); + + if (str != NULL && cache) + setMOTDCache(str); + } + return str; +} + +#else + +extern "C" char *fetchMOTD(int /* cache */) +{ + return NULL; +} + +#endif \ No newline at end of file diff --git a/src/motd.h b/src/motd.h new file mode 100644 index 000000000..70fa7bb1d --- /dev/null +++ b/src/motd.h @@ -0,0 +1,11 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +char *fetchMOTD(int fCache); + +#ifdef __cplusplus +} +#endif \ No newline at end of file From 7033048f7a4db460cae85819e7411ba2bfd03d1e Mon Sep 17 00:00:00 2001 From: John Sully Date: Wed, 4 Mar 2020 17:22:57 -0500 Subject: [PATCH 09/10] finish merge Former-commit-id: 66063a19f6cdbc87ead8ecbd4887134438ee1ccd --- src/Makefile | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Makefile b/src/Makefile index 27c910264..1b8695cfb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -270,11 +270,7 @@ endif REDIS_SERVER_NAME=keydb-pro-server REDIS_SENTINEL_NAME=keydb-sentinel -<<<<<<< HEAD -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 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 acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o AsyncWorkQueue.o snapshot.o storage/rocksdb.o storage/rocksdbfactory.o storage/teststorageprovider.o keydbutils.o $(ASM_OBJ) -======= -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 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 acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o motd.o $(ASM_OBJ) ->>>>>>> redis_6_merge +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 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 acl.o storage.o rdb-s3.o fastlock.o new.o tracking.o cron.o connection.o tls.o sha256.o AsyncWorkQueue.o snapshot.o storage/rocksdb.o storage/rocksdbfactory.o storage/teststorageprovider.o keydbutils.o motd.o $(ASM_OBJ) REDIS_CLI_NAME=keydb-cli REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o redis-cli-cpphelper.o zmalloc.o release.o anet.o ae.o crc64.o siphash.o crc16.o storage-lite.o fastlock.o new.o motd.o $(ASM_OBJ) REDIS_BENCHMARK_NAME=keydb-benchmark From 1187fb6afcae4d616fe6e54ec965539c53a0a6a5 Mon Sep 17 00:00:00 2001 From: John Sully Date: Wed, 4 Mar 2020 17:23:40 -0500 Subject: [PATCH 10/10] Fix CLANG build break Former-commit-id: 5e63c0955d2861ab0ebe2055d4f2d2e8989ea4f3 --- src/server.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/server.h b/src/server.h index a668cbcf7..73a7d8451 100644 --- a/src/server.h +++ b/src/server.h @@ -55,6 +55,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { #include