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 376D56F87A; Fri, 16 Apr 2021 19:29:52 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 376D56F87A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618590592; bh=x6bWSWQvzaXU76mPbm97N6egLSAMnEJSKX6L8ntLvCM=; 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=qWZPP6bND3A+JpqAJ3RBD+H9svn1KIxGw8Ftf/ypta3tQ/Z+CV91KP8rXzSIXdl0w uhmWTziGurovCvJALG1Ll8EIwWNEOyttMeq8Mcmk5M4RDJca/p/GJrvO6slNEeTOHJ rRvGR3r20vMbutAHtyVr5fd1mDtc+GBixl2kz75w= Received: from smtp17.mail.ru (smtp17.mail.ru [94.100.176.154]) (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 CC52674B72 for ; Fri, 16 Apr 2021 19:25:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CC52674B72 Received: by smtp17.mail.ru with esmtpa (envelope-from ) id 1lXRI6-00008e-32; Fri, 16 Apr 2021 19:25:58 +0300 To: v.shpilevoy@tarantool.org, gorcunov@gmail.com Date: Fri, 16 Apr 2021 19:25:39 +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: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E74807BAE725B9AE625DE765B0E193B5B7687182A05F5380850400A13BE2332CA35AC54F31D472BB3EBFD8DC4F51A823AC883723B58F18A1C4518 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE75C3A43A35AA60CB6EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063786D6A7D810B9582D8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2BFBEDB291917F49721E80BF1ED63A721CB629EEF1311BF91D2E47CDBA5A96583C09775C1D3CA48CF90BCC82C2C62A6D1117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE7DAFD5FE95A47D84C9FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE78579C924EDBE3338D32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C0ECB66CD4125B61A4CD04E86FAF290E2D7E9C4E3C761E06A71DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3CE9959E2676FD87735872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24209795067102C07E8F7B195E1C978319E0957880793265250138C30FF9A9D12 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBF1196BB3248DD4BD745D731AD8331368ABD757D5A6CD24C9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3442AF01057B6BF997337B0C27118DE976F6D1C4BEEB668C1ED9DED352464323992D58FE7B1B85D9D11D7E09C32AA3244C6C8610217EF59BEEF99A2C374B0D19B897FE24653F78E668927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj3S6P1v0GIqRNbxkopeX5Qg== X-Mailru-Sender: 583F1D7ACE8F49BDD2846D59FC20E9F8DF49C5E8585DB537DAD4B553108B4688A98AFB19BEADDED9424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 08/12] election: 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 19f1528ca..d5a55a30a 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -676,17 +676,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 @@ -1109,7 +1119,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(); @@ -1143,11 +1153,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)