Skip to content

Commit ff010fe

Browse files
CuriousGeorgiySerpentian
authored andcommitted
box: refactor box_demote to make it more comprehensible
Suggested by Nikita Zheleztsov in the scope of tarantool#9855. Needed for tarantool#9855 NO_CHANGELOG=<refactoring> NO_DOC=<refactoring> NO_TEST=<refactoring> Co-authored-by: Nikita Zheleztsov <n.zheleztsov@proton.me>
1 parent d529082 commit ff010fe

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/box/box.cc

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3080,36 +3080,35 @@ box_demote(void)
30803080
return 0;
30813081

30823082
const struct raft *raft = box_raft();
3083-
if (box_election_mode == ELECTION_MODE_OFF) {
3084-
assert(raft->state == RAFT_STATE_FOLLOWER);
3085-
if (raft->leader != REPLICA_ID_NIL) {
3086-
diag_set(ClientError, ER_NOT_LEADER, raft->leader);
3087-
return -1;
3088-
}
3089-
if (txn_limbo.owner_id == REPLICA_ID_NIL)
3090-
return 0;
3091-
/*
3092-
* If the limbo term is up to date with Raft, then it might have
3093-
* a valid owner right now. Demotion would disrupt it. In this
3094-
* case the user has to explicitly overthrow the old owner with
3095-
* local promote(), or call demote() on the actual owner.
3096-
*/
3097-
if (txn_limbo.promote_greatest_term == raft->term &&
3098-
txn_limbo.owner_id != instance_id)
3099-
return 0;
3100-
} else {
3083+
if (box_election_mode != ELECTION_MODE_OFF) {
31013084
if (txn_limbo_replica_term(&txn_limbo, instance_id) !=
31023085
raft->term)
31033086
return 0;
31043087
if (txn_limbo.owner_id != instance_id)
31053088
return 0;
31063089
if (raft->state != RAFT_STATE_LEADER)
31073090
return 0;
3091+
return box_trigger_elections();
31083092
}
3109-
if (box_trigger_elections() != 0)
3093+
3094+
assert(raft->state == RAFT_STATE_FOLLOWER);
3095+
if (raft->leader != REPLICA_ID_NIL) {
3096+
diag_set(ClientError, ER_NOT_LEADER, raft->leader);
31103097
return -1;
3111-
if (box_election_mode != ELECTION_MODE_OFF)
3098+
}
3099+
if (txn_limbo.owner_id == REPLICA_ID_NIL)
31123100
return 0;
3101+
/*
3102+
* If the limbo term is up to date with Raft, then it might have
3103+
* a valid owner right now. Demotion would disrupt it. In this
3104+
* case the user has to explicitly overthrow the old owner with
3105+
* local promote(), or call demote() on the actual owner.
3106+
*/
3107+
if (txn_limbo.promote_greatest_term == raft->term &&
3108+
txn_limbo.owner_id != instance_id)
3109+
return 0;
3110+
if (box_trigger_elections() != 0)
3111+
return -1;
31133112
if (box_try_wait_confirm(2 * replication_synchro_timeout) < 0)
31143113
return -1;
31153114
int64_t wait_lsn = box_wait_limbo_acked(replication_synchro_timeout);

0 commit comments

Comments
 (0)