Search in sources :

Example 6 with GroupMember

use of io.atomix.primitive.partition.GroupMember in project atomix by atomix.

the class PrimaryElectorServiceTest method testEnterSinglePartition.

@Test
public void testEnterSinglePartition() {
    PartitionId partition = new PartitionId("test", 1);
    PrimaryElectorService elector = newService();
    PrimaryTerm term;
    // 1st member to enter should be primary.
    GroupMember m1 = createGroupMember("node1", "group1");
    Session<?> s1 = createSession(m1);
    term = elector.enter(createEnterOp(partition, m1, s1));
    assertEquals(1L, term.term());
    assertEquals(m1, term.primary());
    assertEquals(1, term.candidates().size());
    // 2nd member to enter should be added to candidates.
    GroupMember m2 = createGroupMember("node2", "group1");
    Session<?> s2 = createSession(m2);
    term = elector.enter(createEnterOp(partition, m2, s2));
    assertEquals(1L, term.term());
    assertEquals(m1, term.primary());
    assertEquals(2, term.candidates().size());
    assertEquals(m2, term.candidates().get(1));
}
Also used : GroupMember(io.atomix.primitive.partition.GroupMember) PartitionId(io.atomix.primitive.partition.PartitionId) PrimaryTerm(io.atomix.primitive.partition.PrimaryTerm) Test(org.junit.Test)

Example 7 with GroupMember

use of io.atomix.primitive.partition.GroupMember in project atomix by atomix.

the class PrimaryElectorServiceTest method enter.

private PrimaryTerm enter(String nodeId, String groupId, PrimaryElectorService elector) {
    PartitionId partId = new PartitionId("test", 1);
    GroupMember member = createGroupMember(nodeId, groupId);
    Session session = createSession(member);
    return elector.enter(createEnterOp(partId, member, session));
}
Also used : GroupMember(io.atomix.primitive.partition.GroupMember) PartitionId(io.atomix.primitive.partition.PartitionId) Session(io.atomix.primitive.session.Session)

Example 8 with GroupMember

use of io.atomix.primitive.partition.GroupMember 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)

Example 9 with GroupMember

use of io.atomix.primitive.partition.GroupMember in project atomix by atomix.

the class PrimaryBackupServiceContext method changeRole.

/**
 * Changes the roles.
 */
private void changeRole(PrimaryTerm term) {
    threadContext.execute(() -> {
        if (term.term() > currentTerm) {
            log.debug("Term changed: {}", term);
            currentTerm = term.term();
            primary = term.primary() != null ? term.primary().memberId() : null;
            backups = term.backups(descriptor.backups()).stream().map(GroupMember::memberId).collect(Collectors.toList());
            if (Objects.equals(primary, clusterMembershipService.getLocalMember().id())) {
                if (this.role == null) {
                    this.role = new PrimaryRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.PRIMARY);
                } else if (this.role.role() != Role.PRIMARY) {
                    this.role.close();
                    this.role = new PrimaryRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.PRIMARY);
                }
            } else if (backups.contains(clusterMembershipService.getLocalMember().id())) {
                if (this.role == null) {
                    this.role = new BackupRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.BACKUP);
                } else if (this.role.role() != Role.BACKUP) {
                    this.role.close();
                    this.role = new BackupRole(this);
                    log.debug("{} transitioning to {}", clusterMembershipService.getLocalMember().id(), Role.BACKUP);
                }
            } 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) NoneRole(io.atomix.protocols.backup.roles.NoneRole) PrimaryRole(io.atomix.protocols.backup.roles.PrimaryRole) PrimaryBackupRole(io.atomix.protocols.backup.roles.PrimaryBackupRole) BackupRole(io.atomix.protocols.backup.roles.BackupRole)

Aggregations

GroupMember (io.atomix.primitive.partition.GroupMember)9 PrimaryTerm (io.atomix.primitive.partition.PrimaryTerm)6 PartitionId (io.atomix.primitive.partition.PartitionId)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 MemberId (io.atomix.cluster.MemberId)2 Session (io.atomix.primitive.session.Session)2 FollowerRole (io.atomix.protocols.log.roles.FollowerRole)2 LeaderRole (io.atomix.protocols.log.roles.LeaderRole)2 NoneRole (io.atomix.protocols.log.roles.NoneRole)2 List (java.util.List)2 ClusterMembershipService (io.atomix.cluster.ClusterMembershipService)1 Member (io.atomix.cluster.Member)1 Replication (io.atomix.primitive.Replication)1 ManagedMemberGroupService (io.atomix.primitive.partition.ManagedMemberGroupService)1 MemberGroup (io.atomix.primitive.partition.MemberGroup)1 PrimaryElection (io.atomix.primitive.partition.PrimaryElection)1 PrimaryElectionEvent (io.atomix.primitive.partition.PrimaryElectionEvent)1 PrimaryElectionEventListener (io.atomix.primitive.partition.PrimaryElectionEventListener)1 BackupRole (io.atomix.protocols.backup.roles.BackupRole)1