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 55BEF6BD0D; Sun, 11 Apr 2021 20:59:41 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 55BEF6BD0D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618163981; bh=u7uqlWK6VjnPkXEe0JJtWMHnxz7RBwOX6wtYm4n9vew=; 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=vhoisbmRlUg8CBIche8ny/sDfJ25obq02WWHVt4A/vYlWls0IAlhQqcjZN7oLeLvc QUORCBeJ2Jtj02TVNuQUGr5sh06vVzS+oR0Okd3fI9xcJpqUH4kEab09A8/dKgEP2B nyP7LE0FG2/sV3ZNFeJE7lXLo8mzrLJgBSvQtg0M= Received: from smtp42.i.mail.ru (smtp42.i.mail.ru [94.100.177.102]) (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 A1E636BD10 for ; Sun, 11 Apr 2021 20:56:16 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A1E636BD10 Received: by smtp42.i.mail.ru with esmtpa (envelope-from ) id 1lVeJj-0001Ua-KQ; Sun, 11 Apr 2021 20:56:16 +0300 To: v.shpilevoy@tarantool.org, gorcunov@gmail.com Date: Sun, 11 Apr 2021 20:56:02 +0300 Message-Id: <39cdd53dfda9043633612a31f2e7a8dd7177753a.1618163409.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: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480EBD5CA77A668ECB87DA2124B0A8E6609182A05F53808504019416B6D280AACA63C81D1CFCF44AEB19FD0CAC023EF90883E93D949CD5F282B X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE775D79D69C7A464F3EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637835928C62272F24E8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B20B667513CA6FECD2C3681B832ED2F766E695ED7F9A6C896ED2E47CDBA5A96583C09775C1D3CA48CF90D92131081DE748117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE791DAD9F922AA71188941B15DA834481F9449624AB7ADAF37BA3038C0950A5D3613377AFFFEAFD2691661749BA6B97735ECA7B8489B51D0C07B076A6E789B0E97A8DF7F3B2552694A1E7802607F20496D49FD398EE364050F140C956E756FBB7AC3123C4324A5CF10B3661434B16C20AC78D18283394535A9E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B682BBBAF5DF00056E089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24209795067102C07E8F7B195E1C9783191471F51605917AD99F3B6BFDA6AA5C4 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CE44850EFB5864EA2BBF9EA79316FF371CF09E6625C04027F9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346F44602F3B494634C1E7266DE433951B68169714698DBC48C0508659F28F2832250B268E1EEE25581D7E09C32AA3244C393671487BE3FCA15B95863343BBA8633FD9C8CA1B0515E0927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXjz3uqod8pbhdecHydaH+rQ X-Mailru-Sender: 583F1D7ACE8F49BDD2846D59FC20E9F8A3D5B6EFD694386939BDB34C9504183E988FD9DDF10FAD7C424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: [Tarantool-patches] [PATCH 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 9b6323b3f..10e8351b0 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)