use of org.neo4j.causalclustering.core.consensus.DirectNetworking in project neo4j by neo4j.
the class RaftGroupMembershipTest method shouldNotFormGroupWithoutAnyBootstrapping.
@Test
public void shouldNotFormGroupWithoutAnyBootstrapping() throws Exception {
// given
DirectNetworking net = new DirectNetworking();
final MemberId[] ids = { member(0), member(1), member(2) };
RaftTestFixture fixture = new RaftTestFixture(net, 3, ids);
fixture.members().setTargetMembershipSet(new RaftTestGroup(ids).getMembers());
fixture.members().invokeTimeout(ELECTION);
// when
net.processMessages();
// then
assertThat(fixture.members(), hasCurrentMembers(new RaftTestGroup(new int[0])));
assertEquals(0, fixture.members().withRole(LEADER).size());
assertEquals(3, fixture.members().withRole(FOLLOWER).size());
}
use of org.neo4j.causalclustering.core.consensus.DirectNetworking in project neo4j by neo4j.
the class RaftGroupMembershipTest method shouldAddMultipleInstancesToExistingRaftGroup.
@Test
public void shouldAddMultipleInstancesToExistingRaftGroup() throws Exception {
// given
DirectNetworking net = new DirectNetworking();
final MemberId leader = member(0);
final MemberId stable1 = member(1);
final MemberId stable2 = member(2);
final MemberId toBeAdded1 = member(3);
final MemberId toBeAdded2 = member(4);
final MemberId toBeAdded3 = member(5);
final MemberId[] initialMembers = { leader, stable1, stable2 };
final MemberId[] finalMembers = { leader, stable1, stable2, toBeAdded1, toBeAdded2, toBeAdded3 };
RaftTestFixture fixture = new RaftTestFixture(net, 3, finalMembers);
fixture.bootstrap(initialMembers);
fixture.members().withId(leader).timeoutService().invokeTimeout(ELECTION);
net.processMessages();
// when
fixture.members().setTargetMembershipSet(new RaftTestGroup(finalMembers).getMembers());
net.processMessages();
// We need a heartbeat for every member we add. It is necessary to have the new members report their state
// so their membership change can be processed. We can probably do better here.
fixture.members().withId(leader).timeoutService().invokeTimeout(HEARTBEAT);
net.processMessages();
fixture.members().withId(leader).timeoutService().invokeTimeout(HEARTBEAT);
net.processMessages();
fixture.members().withId(leader).timeoutService().invokeTimeout(HEARTBEAT);
net.processMessages();
// then
assertThat(fixture.members().withIds(finalMembers), hasCurrentMembers(new RaftTestGroup(finalMembers)));
assertEquals(1, fixture.members().withRole(LEADER).size());
assertEquals(5, fixture.members().withRole(FOLLOWER).size());
}
use of org.neo4j.causalclustering.core.consensus.DirectNetworking in project neo4j by neo4j.
the class RaftGroupMembershipTest method shouldHandleMixedChangeToExistingRaftGroup.
@Test
public void shouldHandleMixedChangeToExistingRaftGroup() throws Exception {
DirectNetworking net = new DirectNetworking();
// given
final MemberId leader = member(0);
final MemberId stable = member(1);
final MemberId toBeRemoved1 = member(2);
final MemberId toBeRemoved2 = member(3);
final MemberId toBeAdded1 = member(4);
final MemberId toBeAdded2 = member(5);
final MemberId[] everyone = { leader, stable, toBeRemoved1, toBeRemoved2, toBeAdded1, toBeAdded2 };
final MemberId[] initialMembers = { leader, stable, toBeRemoved1, toBeRemoved2 };
final MemberId[] finalMembers = { leader, stable, toBeAdded1, toBeAdded2 };
RaftTestFixture fixture = new RaftTestFixture(net, 3, everyone);
fixture.bootstrap(initialMembers);
fixture.members().withId(leader).timeoutService().invokeTimeout(ELECTION);
net.processMessages();
// when
fixture.members().withId(leader).raftInstance().setTargetMembershipSet(new RaftTestGroup(finalMembers).getMembers());
net.processMessages();
fixture.members().withId(leader).timeoutService().invokeTimeout(HEARTBEAT);
net.processMessages();
fixture.members().withId(leader).timeoutService().invokeTimeout(HEARTBEAT);
net.processMessages();
fixture.members().withId(leader).timeoutService().invokeTimeout(HEARTBEAT);
net.processMessages();
// then
assertThat(fixture.members().withIds(finalMembers), hasCurrentMembers(new RaftTestGroup(finalMembers)));
assertEquals(1, fixture.members().withIds(finalMembers).withRole(LEADER).size());
assertEquals(3, fixture.members().withIds(finalMembers).withRole(FOLLOWER).size());
}
use of org.neo4j.causalclustering.core.consensus.DirectNetworking in project neo4j by neo4j.
the class RaftGroupMembershipTest method shouldRemoveSingleInstanceFromExistingRaftGroup.
@Test
public void shouldRemoveSingleInstanceFromExistingRaftGroup() throws Exception {
DirectNetworking net = new DirectNetworking();
// given
final MemberId leader = member(0);
final MemberId stable = member(1);
final MemberId toBeRemoved = member(2);
final MemberId[] initialMembers = { leader, stable, toBeRemoved };
final MemberId[] finalMembers = { leader, stable };
RaftTestFixture fixture = new RaftTestFixture(net, 2, initialMembers);
fixture.bootstrap(initialMembers);
fixture.members().withId(leader).timeoutService().invokeTimeout(ELECTION);
// when
fixture.members().setTargetMembershipSet(new RaftTestGroup(finalMembers).getMembers());
net.processMessages();
// then
assertThat(fixture.members().withIds(finalMembers), hasCurrentMembers(new RaftTestGroup(finalMembers)));
assertEquals(1, fixture.members().withIds(finalMembers).withRole(LEADER).size());
assertEquals(1, fixture.members().withIds(finalMembers).withRole(FOLLOWER).size());
}
use of org.neo4j.causalclustering.core.consensus.DirectNetworking in project neo4j by neo4j.
the class RaftGroupMembershipTest method shouldElectNewLeaderWhenOldOneAbruptlyLeaves.
@Test
public void shouldElectNewLeaderWhenOldOneAbruptlyLeaves() throws Exception {
DirectNetworking net = new DirectNetworking();
// given
final MemberId leader1 = member(0);
final MemberId leader2 = member(1);
final MemberId stable = member(2);
final MemberId[] initialMembers = { leader1, leader2, stable };
RaftTestFixture fixture = new RaftTestFixture(net, 2, initialMembers);
fixture.bootstrap(initialMembers);
fixture.members().withId(leader1).timeoutService().invokeTimeout(ELECTION);
net.processMessages();
// when
net.disconnect(leader1);
fixture.members().withId(leader2).timeoutService().invokeTimeout(ELECTION);
net.processMessages();
// then
assertTrue(fixture.members().withId(leader2).raftInstance().isLeader());
assertFalse(fixture.members().withId(stable).raftInstance().isLeader());
assertEquals(1, fixture.members().withIds(leader2, stable).withRole(LEADER).size());
assertEquals(1, fixture.members().withIds(leader2, stable).withRole(FOLLOWER).size());
}
Aggregations