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 D657B6901A; Wed, 14 Apr 2021 17:20:53 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D657B6901A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618410053; bh=jcfPjLdw3Obbwvba40+UT8gj4DVi3gz6HNSaLUni0bk=; 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=idJ7UFsCdOQtdTLbB9xALyGbntr/yEebaBmfE+6IeO+J6kesl2yWVPSXE+ktWKkKJ Wnsk/CmF10VDhS4aDsOV8+2PgBtXMKQunMnhU4+xwQU0ovUdvtY3/JN3Vy6I3N6jpz hD2+qwdpnF5gq6elUz+1kOwiUrWCCh+y+ldKodOg= Received: from smtp41.i.mail.ru (smtp41.i.mail.ru [94.100.177.101]) (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 B71A06901A for ; Wed, 14 Apr 2021 17:17:29 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B71A06901A Received: by smtp41.i.mail.ru with esmtpa (envelope-from ) id 1lWgKe-000456-RZ; Wed, 14 Apr 2021 17:17:29 +0300 To: v.shpilevoy@tarantool.org, gorcunov@gmail.com Date: Wed, 14 Apr 2021 17:17:17 +0300 Message-Id: <3b9a0b2ed73d94bad8455e9c8fc9e82e6ca7f7e2.1618409665.git.sergepetrenko@tarantool.org> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480B1C8842CE613979723F2FB4628545A35182A05F538085040E13AF36EE65859441B6DE6C58DA642C806B1ADF3D58AFEC0A615A0F427F506EF X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE73AE72CEE53A788F4EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378B49D47CE295E66E8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2A6F73FB648E84C242C9639FAAA91068DC5BC91295CC49E52D2E47CDBA5A96583C09775C1D3CA48CF53FA85A707D24CADCC7F00164DA146DAFE8445B8C89999729449624AB7ADAF37F6B57BC7E64490611E7FA7ABCAF51C92176DF2183F8FC7C028451B159A507268D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EEB89ED3C7A62817811AD9CA79AA6DBBDDD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE36D3A1509E11137119735652A29929C6CC4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BC7805D11922B9FF275ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CB69F9342289A40B3DAC3A90C7F8220C91A2D6F89103ACD5E9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34B3611847B8BC2D0BEA3E639D8958D27CDA6B0040BE34F376E2C1E39588211B6C93DC7C88EFEDAF9F1D7E09C32AA3244CC2944CDF8B9AE2997ED0C20C55536A5A63871F383B54D9B3927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojrcJA+pXcDun1csu6ddH2Kw== X-Mailru-Sender: 583F1D7ACE8F49BDD2846D59FC20E9F832D6326595065D5C05B9CB19A16D73A4DCBBBD2E9907A725424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3 07/10] replication: introduce a new election mode: "manual" 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" When an instance is configured in "manual" election mode, it behaves as a voter for most of the time, until `box.ctl.promote()` is called. Once `box.ctl.promote()` is called the instance will behave as a candidate for a full election round, e.g. until the leader is known. If the instance wins the elections, it remains in `leader` state until the next elections. Otherwise the instance returns to `voter` state. Follow-up #5445 Part of #3055 --- src/box/box.cc | 37 +++++++++++++++++--------- src/box/raft.c | 2 ++ src/box/raft.h | 17 ++++++++++++ test/replication/election_basic.result | 4 +-- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/box/box.cc b/src/box/box.cc index f44dd0e54..3729ed997 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -680,17 +680,27 @@ box_check_uri(const char *source, const char *option_name) } } -static const char * +static enum election_mode box_check_election_mode(void) { const char *mode = cfg_gets("election_mode"); - if (mode == NULL || (strcmp(mode, "off") != 0 && - strcmp(mode, "voter") != 0 && strcmp(mode, "candidate") != 0)) { - diag_set(ClientError, ER_CFG, "election_mode", "the value must " - "be a string 'off' or 'voter' or 'candidate'"); - return NULL; - } - return mode; + if (mode == NULL) + goto error; + + if (strcmp(mode, "off") == 0) + return ELECTION_MODE_OFF; + else if (strcmp(mode, "voter") == 0) + return ELECTION_MODE_VOTER; + else if (strcmp(mode, "manual") == 0) + return ELECTION_MODE_MANUAL; + else if (strcmp(mode, "candidate") == 0) + return ELECTION_MODE_CANDIDATE; + +error: + diag_set(ClientError, ER_CFG, "election_mode", + "the value must be one of the following strings: " + "'off', 'voter', 'candidate', 'manual'"); + return ELECTION_MODE_INVALID; } static double @@ -1113,7 +1123,7 @@ box_check_config(void) box_check_uri(cfg_gets("listen"), "listen"); box_check_instance_uuid(&uuid); box_check_replicaset_uuid(&uuid); - if (box_check_election_mode() == NULL) + if (box_check_election_mode() == ELECTION_MODE_INVALID) diag_raise(); if (box_check_election_timeout() < 0) diag_raise(); @@ -1147,11 +1157,12 @@ box_check_config(void) int box_set_election_mode(void) { - const char *mode = box_check_election_mode(); - if (mode == NULL) + enum election_mode mode = box_check_election_mode(); + if (mode == ELECTION_MODE_INVALID) return -1; - raft_cfg_is_candidate(box_raft(), strcmp(mode, "candidate") == 0); - raft_cfg_is_enabled(box_raft(), strcmp(mode, "off") != 0); + box_election_mode = mode; + raft_cfg_is_candidate(box_raft(), mode == ELECTION_MODE_CANDIDATE); + raft_cfg_is_enabled(box_raft(), mode != ELECTION_MODE_OFF); return 0; } diff --git a/src/box/raft.c b/src/box/raft.c index cfd898db0..285dbe4fd 100644 --- a/src/box/raft.c +++ b/src/box/raft.c @@ -44,6 +44,8 @@ struct raft box_raft_global = { .state = 0, }; +enum election_mode box_election_mode = ELECTION_MODE_INVALID; + /** * A trigger executed each time the Raft state machine updates any * of its visible attributes. diff --git a/src/box/raft.h b/src/box/raft.h index 1c59f17e6..15f4e80d9 100644 --- a/src/box/raft.h +++ b/src/box/raft.h @@ -35,8 +35,25 @@ extern "C" { #endif +enum election_mode { + ELECTION_MODE_INVALID = -1, + ELECTION_MODE_OFF = 0, + ELECTION_MODE_VOTER = 1, + ELECTION_MODE_MANUAL = 2, + ELECTION_MODE_CANDIDATE = 3, +}; + struct raft_request; +/** + * box_election_mode - current mode of operation for raft. Some modes correspond + * to RAFT operation modes directly, like CANDIDATE, VOTER and OFF. + * There's a mode which does not map to raft operation mode directly: + * MANUAL. In this mode RAFT usually operates as a voter, but it may become a + * candidate for some period of time when user calls `box.ctl.promote()` + */ +extern enum election_mode box_election_mode; + /** Raft state of this instance. */ static inline struct raft * box_raft(void) diff --git a/test/replication/election_basic.result b/test/replication/election_basic.result index 4d7d33f2b..d5320b3ff 100644 --- a/test/replication/election_basic.result +++ b/test/replication/election_basic.result @@ -22,8 +22,8 @@ box.cfg{election_mode = 100} | ... box.cfg{election_mode = '100'} | --- - | - error: 'Incorrect value for option ''election_mode'': the value must be a string - | ''off'' or ''voter'' or ''candidate''' + | - error: 'Incorrect value for option ''election_mode'': the value must be one of the + | following strings: ''off'', ''voter'', ''candidate'', ''manual''' | ... box.cfg{election_timeout = -1} | --- -- 2.24.3 (Apple Git-128)