From ff010fe97025a093f2938dded6be4a0be72460ee Mon Sep 17 00:00:00 2001 From: Georgiy Lebedev <g.lebedev@tarantool.org> Date: Fri, 24 May 2024 12:07:25 +0300 Subject: [PATCH] box: refactor `box_demote` to make it more comprehensible Suggested by Nikita Zheleztsov in the scope of #9855. Needed for #9855 NO_CHANGELOG=<refactoring> NO_DOC=<refactoring> NO_TEST=<refactoring> Co-authored-by: Nikita Zheleztsov <n.zheleztsov@proton.me> --- src/box/box.cc | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/box/box.cc b/src/box/box.cc index 5af1148081..22d957b81c 100644 --- a/src/box/box.cc +++ b/src/box/box.cc @@ -3080,24 +3080,7 @@ box_demote(void) return 0; const struct raft *raft = box_raft(); - if (box_election_mode == ELECTION_MODE_OFF) { - assert(raft->state == RAFT_STATE_FOLLOWER); - if (raft->leader != REPLICA_ID_NIL) { - diag_set(ClientError, ER_NOT_LEADER, raft->leader); - return -1; - } - if (txn_limbo.owner_id == REPLICA_ID_NIL) - return 0; - /* - * If the limbo term is up to date with Raft, then it might have - * a valid owner right now. Demotion would disrupt it. In this - * case the user has to explicitly overthrow the old owner with - * local promote(), or call demote() on the actual owner. - */ - if (txn_limbo.promote_greatest_term == raft->term && - txn_limbo.owner_id != instance_id) - return 0; - } else { + if (box_election_mode != ELECTION_MODE_OFF) { if (txn_limbo_replica_term(&txn_limbo, instance_id) != raft->term) return 0; @@ -3105,11 +3088,27 @@ box_demote(void) return 0; if (raft->state != RAFT_STATE_LEADER) return 0; + return box_trigger_elections(); } - if (box_trigger_elections() != 0) + + assert(raft->state == RAFT_STATE_FOLLOWER); + if (raft->leader != REPLICA_ID_NIL) { + diag_set(ClientError, ER_NOT_LEADER, raft->leader); return -1; - if (box_election_mode != ELECTION_MODE_OFF) + } + if (txn_limbo.owner_id == REPLICA_ID_NIL) return 0; + /* + * If the limbo term is up to date with Raft, then it might have + * a valid owner right now. Demotion would disrupt it. In this + * case the user has to explicitly overthrow the old owner with + * local promote(), or call demote() on the actual owner. + */ + if (txn_limbo.promote_greatest_term == raft->term && + txn_limbo.owner_id != instance_id) + return 0; + if (box_trigger_elections() != 0) + return -1; if (box_try_wait_confirm(2 * replication_synchro_timeout) < 0) return -1; int64_t wait_lsn = box_wait_limbo_acked(replication_synchro_timeout); -- GitLab