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 250776EC5A; Wed, 14 Jul 2021 21:32:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 250776EC5A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1626287578; bh=qKzkbUvCYx3Id8YTRuvn8qymaFBq8IInsGMiOtvLcIs=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=pwn+9DYoI7t79ozagErZg/GmkxApoOZUPoRDSzBP5eMAL7x5NPMiIe2HN40G59Tk8 cxvTlZvIZ5sgTUpQgdCKR+Rtg6Q1C4I+b6AIq7ITVK45+X5iBx9f66R7IRIl/4QJ4a 5evnDsalk9e/Mce8vyfScEE2cIoa/vBCYGqlIsxg= Received: from smtp58.i.mail.ru (smtp58.i.mail.ru [217.69.128.38]) (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 447B86F3F1 for ; Wed, 14 Jul 2021 21:26:14 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 447B86F3F1 Received: by smtp58.i.mail.ru with esmtpa (envelope-from ) id 1m3jaH-0007Q7-FU; Wed, 14 Jul 2021 21:26:13 +0300 To: v.shpilevoy@tarantool.org, gorcunov@gmail.com Date: Wed, 14 Jul 2021 21:25:42 +0300 Message-Id: <5bd37a93c3939dbb5e07b33be6e47f386d88e884.1626287002.git.sergepetrenko@tarantool.org> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD97BB0EF39AD2B33D5CFD6F66580F08A9E8DA110284E1A7113182A05F5380850402F016B0BE1BFB5C0E1CBA9C09F57F16FBE974263DFA380A64681484BD224D091 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7EB1B9B9F97B01B97EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006371A00CE7BDCE207EB8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8BBA165A348A4F706198E737E294F599E117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC292D688DDAD4E7BC389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC87BD21ED50D08CA4DF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B81DF583BEE9BDE68EFF80C71ABB335746BA297DBC24807EABDAD6C7F3747799A X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CE68746B1F2AB10C6F38861CCBB7FE6DD44DC989CF0760AF19C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF309DFB797F6729CB699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D041FB2E16F174C4153280415DCE66F1217E49ACE606A3F7F6DBEED2555A6AF2B33A0F7C2479483B1D7E09C32AA3244C069D9233BA685DA27E62B0A0D2F7BEA4408A6A02710B7304927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojDdSFIg49M1SKG20KpuIcwA== X-Mailru-Sender: 3B9A0136629DC9125D61937A2360A446DCF67A6ACA5D040D4563D9798DEA01F188A007A287150C38424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 14/16] box: extract promote() settings to a separate method 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: Serge Petrenko via Tarantool-patches Reply-To: Serge Petrenko Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Extract the switch(box_election_mode) { } block to a separate method, box_check_promote_election_mode(), and make it rule out whether the instance may issue a promote(), and how the promote() has to perform. This will simplify the code a bit once demote() is introduced. Part-of #6034 --- src/box/box.cc | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/box/box.cc b/src/box/box.cc index 9130fc322..dcfaab29e 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -1679,6 +1679,34 @@ box_issue_promote(uint32_t prev_leader_id, int64_t promote_lsn) assert(txn_limbo_is_empty(&txn_limbo)); } +/** + * Check whether this instance may run a promote() and set promote parameters + * according to its election mode. + */ +static int +box_check_promote_election_mode(bool *try_wait, bool *run_elections) +{ + switch (box_election_mode) { + case ELECTION_MODE_OFF: + if (try_wait != NULL) + *try_wait = true; + break; + case ELECTION_MODE_VOTER: + assert(box_raft()->state == RAFT_STATE_FOLLOWER); + diag_set(ClientError, ER_UNSUPPORTED, "election_mode='voter'", + "manual elections"); + return -1; + case ELECTION_MODE_MANUAL: + case ELECTION_MODE_CANDIDATE: + if (run_elections != NULL) + *run_elections = box_raft()->state != RAFT_STATE_LEADER; + break; + default: + unreachable(); + } + return 0; +} + int box_promote(void) { @@ -1707,22 +1735,8 @@ box_promote(void) bool run_elections = false; bool try_wait = false; - switch (box_election_mode) { - case ELECTION_MODE_OFF: - try_wait = true; - break; - case ELECTION_MODE_VOTER: - assert(box_raft()->state == RAFT_STATE_FOLLOWER); - diag_set(ClientError, ER_UNSUPPORTED, "election_mode='voter'", - "manual elections"); + if (box_check_promote_election_mode(&try_wait, &run_elections) < 0) return -1; - case ELECTION_MODE_MANUAL: - case ELECTION_MODE_CANDIDATE: - run_elections = box_raft()->state != RAFT_STATE_LEADER; - break; - default: - unreachable(); - } int64_t wait_lsn = -1; -- 2.30.1 (Apple Git-130)