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 A100E6EC5E; Mon, 12 Apr 2021 22:43:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A100E6EC5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618256638; bh=/d4x3pUL9A/JP6pGiYOyl3QqsZ+O3EczSNYVqGxflzQ=; 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=znIps1o/drOAkHXlO+m1yrhWgFdViw7XFkD32S/WLXXvTysrBDmcvus0HYwahZ8vi kPZH2zelF+EFwV28oMPbW9QQyDQF8kp0mYueGC1NCQynyv0gvyn5Z9wQREP6F4ZO0A 84aTz3X6Febj5REZKaf8+6vbYtjlLUwGw/H/gy78= Received: from smtp53.i.mail.ru (smtp53.i.mail.ru [94.100.177.113]) (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 4E2486EC5E for ; Mon, 12 Apr 2021 22:40:31 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4E2486EC5E Received: by smtp53.i.mail.ru with esmtpa (envelope-from ) id 1lW2QA-0008Az-JT; Mon, 12 Apr 2021 22:40:30 +0300 To: v.shpilevoy@tarantool.org, gorcunov@gmail.com Date: Mon, 12 Apr 2021 22:40:20 +0300 Message-Id: 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: 4F1203BC0FB41BD92FFCB8E6708E748094FADAEB10E66ADA4C48BE3C291E66DA182A05F53808504041C2FFBF20F88CBCB68B85E65AD2975864FE5714FCCE949599F4F5761AF3CB45 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76DDA24AE0BFC288EEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637389D8DDD54F43F7A8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B20DB040D45514167620E8FDFA80FC16E60FE69BD46C3DD557D2E47CDBA5A96583C09775C1D3CA48CF53FA85A707D24CADCC7F00164DA146DAFE8445B8C89999729449624AB7ADAF37F6B57BC7E64490611E7FA7ABCAF51C92176DF2183F8FC7C06FD1C55BDD38FC3FD2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE41BF15D38FB6CB3ACFA063A519E5965DD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3DA7BFA4571439BB26E0066C2D8992A16C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B9EB32FE0C26A3A4C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2368A440D3B0F6089093C9A16E5BC824A2A04A2ABAA09D25379311020FFC8D4ADF048BB164F7B76AF066905F23B5E0314 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CB69F9342289A40B3F52AF00CBEE24EED003DE3A5EC1B73AF9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A533D6DF7731C664770EAA7F0DD5778338914655E8258400B2BFD5D622A60F61735BB3D7C567BFFD1D7E09C32AA3244C80618CF9A3283FA2562C8220F4E5031DF165894D92D62706927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojq8JA+pXcDumhEqKqiL/8Og== X-Mailru-Sender: 583F1D7ACE8F49BDD2846D59FC20E9F8AFCB052CDF231E7B5E6410913E3EDBA26DFFAB98D7634357424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 7/9] 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 aae57ec29..b77b0a08d 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)