Skip to content

Commit

Permalink
replication: refactor replicaset_leader()
Browse files Browse the repository at this point in the history
Firstly, rename it to replicaset_find_join_master(). Now, when
there is Raft with a concept of an actual leader, the function
name becomes confusing.

Secondly, do not access ballot member in struct applier in such a
long way - save the ballot pointer on the stack. This is going to
become useful when in one of the next patches the ballot will be
used more.

Part of #5613

(cherry picked from commit 2daec52)
  • Loading branch information
Gerold103 committed Jun 11, 2021
1 parent 7a4c97e commit ad4451f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/box/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,7 @@ box_set_replication_anon(void)
* Wait until the master has registered this
* instance.
*/
struct replica *master = replicaset_leader();
struct replica *master = replicaset_find_join_master();
if (master == NULL || master->applier == NULL ||
master->applier->state != APPLIER_CONNECTED) {
tnt_raise(ClientError, ER_CANNOT_REGISTER);
Expand Down Expand Up @@ -3114,8 +3114,7 @@ bootstrap(const struct tt_uuid *instance_uuid,
*/
box_sync_replication(true);

/* Use the first replica by URI as a bootstrap leader */
struct replica *master = replicaset_leader();
struct replica *master = replicaset_find_join_master();
assert(master == NULL || master->applier != NULL);

if (master != NULL && !tt_uuid_is_equal(&master->uuid, &INSTANCE_UUID)) {
Expand Down
12 changes: 7 additions & 5 deletions src/box/replication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -968,33 +968,35 @@ replicaset_round(bool skip_ro)
struct applier *applier = replica->applier;
if (applier == NULL)
continue;
const struct ballot *ballot = &applier->ballot;
/**
* While bootstrapping a new cluster, read-only
* replicas shouldn't be considered as a leader.
* The only exception if there is no read-write
* replicas since there is still a possibility
* that all replicas exist in cluster table.
*/
if (skip_ro && applier->ballot.is_ro)
if (skip_ro && ballot->is_ro)
continue;
if (leader == NULL) {
leader = replica;
continue;
}
const struct ballot *leader_ballot = &leader->applier->ballot;
/*
* Try to find a replica which has already left
* orphan mode.
*/
if (applier->ballot.is_loading && ! leader->applier->ballot.is_loading)
if (ballot->is_loading && !leader_ballot->is_loading)
continue;
/*
* Choose the replica with the most advanced
* vclock. If there are two or more replicas
* with the same vclock, prefer the one with
* the lowest uuid.
*/
int cmp = vclock_compare_ignore0(&applier->ballot.vclock,
&leader->applier->ballot.vclock);
int cmp = vclock_compare_ignore0(&ballot->vclock,
&leader_ballot->vclock);
if (cmp < 0)
continue;
if (cmp == 0 && tt_uuid_compare(&replica->uuid,
Expand All @@ -1006,7 +1008,7 @@ replicaset_round(bool skip_ro)
}

struct replica *
replicaset_leader(void)
replicaset_find_join_master(void)
{
bool skip_ro = true;
/**
Expand Down
5 changes: 3 additions & 2 deletions src/box/replication.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,11 @@ struct replica *
replica_by_id(uint32_t replica_id);

/**
* Return the replica set leader.
* Find a node in the replicaset on which the instance can try to register to
* join the replicaset.
*/
struct replica *
replicaset_leader(void);
replicaset_find_join_master(void);

struct replica *
replicaset_first(void);
Expand Down

0 comments on commit ad4451f

Please sign in to comment.