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));
}
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));
}
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);
}
}
}
});
}
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);
}
}
}
});
}
Aggregations