From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 2AD956EC5F; Tue, 20 Apr 2021 01:35:45 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2AD956EC5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618871745; bh=HuIBlDbk2G3g35KYzHPq4TK/u/rnNZHBTO7X/5J2/nk=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=PjKoYlZyOzvyclEbLecB59CJIG4Esx22iVIZdRPLGki1llO5TWdEVZ/JDrtGZBHNv 0LvjqaRkodfBjOUxBZJs1KhQfzvjUg6zs4Jibf0fngDMWs49RNftqfcSw5hLrv5mBa eUIgZHVeLAJDfI0FFy9dq9wAxORh88GbWa4kFdDI= Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 60CFB6EC5F for ; Tue, 20 Apr 2021 01:35:43 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 60CFB6EC5F Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1lYcUY-0008O5-GE; Tue, 20 Apr 2021 01:35:43 +0300 To: Serge Petrenko , gorcunov@gmail.com Cc: tarantool-patches@dev.tarantool.org References: <7ff56892-5253-174b-a97b-7800100a58db@tarantool.org> <94422851-f0dd-8995-fbe8-78bbd90680e7@tarantool.org> Message-ID: <9fe79f1b-657c-9437-cad7-637c842b2302@tarantool.org> Date: Tue, 20 Apr 2021 00:35:41 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.9.1 MIME-Version: 1.0 In-Reply-To: <94422851-f0dd-8995-fbe8-78bbd90680e7@tarantool.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480EBD5CA77A668ECB87DA2124B0A8E6609182A05F538085040722F528B57DB88D894859A2D760561E61C8368129C2A7395A8446CF5AAC7B185 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BB0F1327467DA3D4EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C9D01A858C0EE7638638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2E693318F90DE0DB2C7330DAA122D769C8DB33178460FA33B02ED4CEA229C1FA827C277FBC8AE2E8B6BA66BB79834B351A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE709B92020B71E24959FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C397DCCBFEAAA0BC6A117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BB07C9E286C61B7F975ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2AD77751E876CB595E8F7B195E1C9783104391FE68B8D07D077A8C5B82A5066A9 X-C1DE0DAB: 0D63561A33F958A59C1615A8F50337F890D88749463BB0EE038098178AEEC5E1D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A533D6DF7731C664249C41678BBDD64EBBB550385393787897525F4941C7ECA538A1105DC8AB34141D7E09C32AA3244CE905D65D0196657798A9FCC1B7C7C606F522A1CF68F4BE05FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojocJA+pXcDunfu5+DdlG7Yg== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822A2EB5B55DF1C4FD4DE8AE24B2EEEE5423841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v4 09/12] raft: introduce raft_start/stop_candidate X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Good job on the patch! You didn't cover the stop_candidate call with demote flag true in the unit tests. I added my commit on top of this one. See it on the branch and below. Squash if you agree. Otherwise lets discuss. ==================== diff --git a/test/unit/raft.c b/test/unit/raft.c index a718ab3f4..29e48da13 100644 --- a/test/unit/raft.c +++ b/test/unit/raft.c @@ -1270,29 +1270,50 @@ raft_test_too_long_wal_write(void) static void raft_test_start_stop_candidate(void) { - raft_start_test(4); + raft_start_test(8); struct raft_node node; raft_node_create(&node); raft_node_cfg_is_candidate(&node, false); raft_node_cfg_election_quorum(&node, 1); - raft_start_candidate(&node.raft); + raft_node_start_candidate(&node); raft_run_next_event(); is(node.raft.state, RAFT_STATE_LEADER, "became leader after " - "start_candidate"); - raft_stop_candidate(&node.raft, false); + "start candidate"); + + raft_node_stop_candidate(&node); raft_run_for(node.cfg_death_timeout); is(node.raft.state, RAFT_STATE_LEADER, "remain leader after " - "stop_candidate"); + "stop candidate"); + + raft_node_demote_candidate(&node); + is(node.raft.state, RAFT_STATE_FOLLOWER, "demote drops a non-candidate " + "leader to a follower"); + + /* + * Ensure the non-candidate leader is demoted when sees a new term, and + * does not try election again. + */ + raft_node_start_candidate(&node); + raft_run_next_event(); + raft_node_stop_candidate(&node); + is(node.raft.state, RAFT_STATE_LEADER, "non-candidate but still " + "leader"); is(raft_node_send_vote_request(&node, - 3 /* Term. */, + 4 /* Term. */, "{}" /* Vclock. */, 2 /* Source. */ ), 0, "vote request from 2"); is(node.raft.state, RAFT_STATE_FOLLOWER, "demote once new election " - "starts"); + "starts"); + + raft_run_for(node.cfg_election_timeout * 2); + is(node.raft.state, RAFT_STATE_FOLLOWER, "still follower"); + is(node.raft.term, 4, "still the same term"); + + raft_node_destroy(&node); raft_finish_test(); } diff --git a/test/unit/raft.result b/test/unit/raft.result index f9a8f249b..3a3dc5dd2 100644 --- a/test/unit/raft.result +++ b/test/unit/raft.result @@ -221,11 +221,15 @@ ok 12 - subtests ok 13 - subtests *** raft_test_too_long_wal_write: done *** *** raft_test_start_stop_candidate *** - 1..4 - ok 1 - became leader after start_candidate - ok 2 - remain leader after stop_candidate - ok 3 - vote request from 2 - ok 4 - demote once new election starts + 1..8 + ok 1 - became leader after start candidate + ok 2 - remain leader after stop candidate + ok 3 - demote drops a non-candidate leader to a follower + ok 4 - non-candidate but still leader + ok 5 - vote request from 2 + ok 6 - demote once new election starts + ok 7 - still follower + ok 8 - still the same term ok 14 - subtests *** raft_test_start_stop_candidate: done *** *** main_f: done *** diff --git a/test/unit/raft_test_utils.c b/test/unit/raft_test_utils.c index b8735f373..452c05c81 100644 --- a/test/unit/raft_test_utils.c +++ b/test/unit/raft_test_utils.c @@ -387,6 +387,27 @@ raft_node_unblock(struct raft_node *node) } } +void +raft_node_start_candidate(struct raft_node *node) +{ + assert(raft_node_is_started(node)); + raft_start_candidate(&node->raft); +} + +void +raft_node_stop_candidate(struct raft_node *node) +{ + assert(raft_node_is_started(node)); + raft_stop_candidate(&node->raft, false); +} + +void +raft_node_demote_candidate(struct raft_node *node) +{ + assert(raft_node_is_started(node)); + raft_stop_candidate(&node->raft, true); +} + void raft_node_cfg_is_enabled(struct raft_node *node, bool value) { diff --git a/test/unit/raft_test_utils.h b/test/unit/raft_test_utils.h index bc3db0c2a..5f8618716 100644 --- a/test/unit/raft_test_utils.h +++ b/test/unit/raft_test_utils.h @@ -208,6 +208,23 @@ raft_node_block(struct raft_node *node); void raft_node_unblock(struct raft_node *node); +/** + * Make the node candidate, and maybe start election if a leader is not known. + */ +void +raft_node_start_candidate(struct raft_node *node); + +/** + * Make the node non-candidate for next elections, but if it is a leader right + * now, it will stay a leader. + */ +void +raft_node_stop_candidate(struct raft_node *node); + +/** Stop the candidate and remove its leader role if present. */ +void +raft_node_demote_candidate(struct raft_node *node); + /** Configuration methods. */ void