From 68a8d0b46d08cf745bb29f1063fb579fa39581d6 Mon Sep 17 00:00:00 2001 From: Wen Hui Date: Wed, 19 Jan 2022 04:57:51 -0500 Subject: [PATCH] Add sentinel config set test case (#10114) --- src/sentinel.c | 19 ++++++--- tests/sentinel/tests/03-runtime-reconf.tcl | 47 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/sentinel.c b/src/sentinel.c index e30981039..25bdb8caf 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -402,7 +402,7 @@ void sentinelStartFailover(sentinelRedisInstance *master); void sentinelDiscardReplyCallback(redisAsyncContext *c, void *reply, void *privdata); int sentinelSendSlaveOf(sentinelRedisInstance *ri, const sentinelAddr *addr); char *sentinelVoteLeader(sentinelRedisInstance *master, uint64_t req_epoch, char *req_runid, uint64_t *leader_epoch); -void sentinelFlushConfig(void); +int sentinelFlushConfig(void); void sentinelGenerateInitialMonitorEvents(void); int sentinelSendPing(sentinelRedisInstance *ri); int sentinelForceHelloUpdateForMaster(sentinelRedisInstance *master); @@ -2243,7 +2243,7 @@ void rewriteConfigSentinelOption(struct rewriteConfigState *state) { * configuration file to make sure changes are committed to disk. * * On failure the function logs a warning on the Redis log. */ -void sentinelFlushConfig(void) { +int sentinelFlushConfig(void) { int fd = -1; int saved_hz = server.hz; int rewrite_status; @@ -2256,11 +2256,13 @@ void sentinelFlushConfig(void) { if ((fd = open(server.configfile,O_RDONLY)) == -1) goto werr; if (fsync(fd) == -1) goto werr; if (close(fd) == EOF) goto werr; - return; + serverLog(LL_NOTICE,"Sentinel new configuration saved on disk"); + return C_OK; werr: serverLog(LL_WARNING,"WARNING: Sentinel was not able to save the new configuration on disk!!!: %s", strerror(errno)); if (fd != -1) close(fd); + return C_ERR; } /* ====================== hiredis connection handling ======================= */ @@ -4316,8 +4318,10 @@ void sentinelSetCommand(client *c) { break; } } - - if (changes) sentinelFlushConfig(); + if (changes && sentinelFlushConfig() == C_ERR) { + addReplyErrorFormat(c,"Failed to save Sentinel new configuration on disk"); + return; + } addReply(c,shared.ok); return; @@ -4325,7 +4329,10 @@ badfmt: /* Bad format errors */ addReplyErrorFormat(c,"Invalid argument '%s' for SENTINEL SET '%s'", (char*)c->argv[badarg]->ptr,option); seterr: - if (changes) sentinelFlushConfig(); + if (changes && sentinelFlushConfig() == C_ERR) { + addReplyErrorFormat(c,"Failed to save Sentinel new configuration on disk"); + return; + } return; } diff --git a/tests/sentinel/tests/03-runtime-reconf.tcl b/tests/sentinel/tests/03-runtime-reconf.tcl index 426596c37..5e3a96039 100644 --- a/tests/sentinel/tests/03-runtime-reconf.tcl +++ b/tests/sentinel/tests/03-runtime-reconf.tcl @@ -1 +1,48 @@ # Test runtime reconfiguration command SENTINEL SET. +source "../tests/includes/init-tests.tcl" +set num_sentinels [llength $::sentinel_instances] + +test "Set parameters in normal case" { + + set info [S 0 SENTINEL master mymaster] + set origin_quorum [dict get $info quorum] + set origin_down_after_milliseconds [dict get $info down-after-milliseconds] + set update_quorum [expr $origin_quorum+1] + set update_down_after_milliseconds [expr $origin_down_after_milliseconds+1000] + + assert_equal [S 0 SENTINEL SET mymaster quorum $update_quorum] "OK" + assert_equal [S 0 SENTINEL SET mymaster down-after-milliseconds $update_down_after_milliseconds] "OK" + + set update_info [S 0 SENTINEL master mymaster] + assert {[dict get $update_info quorum] != $origin_quorum} + assert {[dict get $update_info down-after-milliseconds] != $origin_down_after_milliseconds} + + #restore to origin config parameters + assert_equal [S 0 SENTINEL SET mymaster quorum $origin_quorum] "OK" + assert_equal [S 0 SENTINEL SET mymaster down-after-milliseconds $origin_down_after_milliseconds] "OK" +} + +test "Set parameters in normal case with bad format" { + + set info [S 0 SENTINEL master mymaster] + set origin_down_after_milliseconds [dict get $info down-after-milliseconds] + + assert_error "ERR Invalid argument '-20' for SENTINEL SET 'down-after-milliseconds'*" {S 0 SENTINEL SET mymaster down-after-milliseconds -20} + assert_error "ERR Invalid argument 'abc' for SENTINEL SET 'down-after-milliseconds'*" {S 0 SENTINEL SET mymaster down-after-milliseconds "abc"} + + set current_info [S 0 SENTINEL master mymaster] + assert {[dict get $current_info down-after-milliseconds] == $origin_down_after_milliseconds} +} + +test "Sentinel Set with other error situations" { + + # non-existing script + assert_error "ERR Notification script seems non existing*" {S 0 SENTINEL SET mymaster notification-script test.txt} + + # wrong parameter number + assert_error "ERR wrong number of arguments for 'set' command or subcommand" {S 0 SENTINEL SET mymaster fakeoption} + + # unknown parameter option + assert_error "ERR Unknown option or number of arguments for SENTINEL SET 'fakeoption'" {S 0 SENTINEL SET mymaster fakeoption fakevalue} +} +