Search in sources :

Example 1 with FollowerRole

use of io.atomix.protocols.log.roles.FollowerRole in project atomix by atomix.

the class DistributedLogServerContext method changeRole.

/**
 * Changes the roles.
 */
private void changeRole(PrimaryTerm term) {
    threadContext.execute(() -> {
        if (term.term() >= currentTerm) {
            log.debug("{} - Term changed: {}", memberId, term);
            currentTerm = term.term();
            leader = term.primary() != null ? term.primary().memberId() : null;
            followers = term.backups(replicationFactor - 1).stream().map(GroupMember::memberId).collect(Collectors.toList());
            if (Objects.equals(leader, clusterMembershipService.getLocalMember().id())) {
                if (this.role == null) {
                    this.role = new LeaderRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.LEADER);
                } else if (this.role.role() != Role.LEADER) {
                    this.role.close();
                    this.role = new LeaderRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.LEADER);
                }
            } else if (followers.contains(clusterMembershipService.getLocalMember().id())) {
                if (this.role == null) {
                    this.role = new FollowerRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.FOLLOWER);
                } else if (this.role.role() != Role.FOLLOWER) {
                    this.role.close();
                    this.role = new FollowerRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.FOLLOWER);
                }
            } else {
                if (this.role == null) {
                    this.role = new NoneRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.NONE);
                } else if (this.role.role() != Role.NONE) {
                    this.role.close();
                    this.role = new NoneRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.NONE);
                }
            }
        }
    });
}
Also used : GroupMember(io.atomix.primitive.partition.GroupMember) FollowerRole(io.atomix.protocols.log.roles.FollowerRole) NoneRole(io.atomix.protocols.log.roles.NoneRole) LeaderRole(io.atomix.protocols.log.roles.LeaderRole)

Aggregations

GroupMember (io.atomix.primitive.partition.GroupMember)1 FollowerRole (io.atomix.protocols.log.roles.FollowerRole)1 LeaderRole (io.atomix.protocols.log.roles.LeaderRole)1 NoneRole (io.atomix.protocols.log.roles.NoneRole)1