use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.
the class RaftMachineTest method newMembersShouldBeIncludedInHeartbeatMessages.
@Test
public void newMembersShouldBeIncludedInHeartbeatMessages() throws Exception {
// Given
DirectNetworking network = new DirectNetworking();
final MemberId newMember = member(99);
DirectNetworking.Inbound newMemberInbound = network.new Inbound(newMember);
final OutboundMessageCollector messages = new OutboundMessageCollector();
newMemberInbound.registerHandler(new Inbound.MessageHandler<RaftMessages.RaftMessage>() {
@Override
public void handle(RaftMessages.RaftMessage message) {
messages.send(newMember, message);
}
});
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).timeoutService(timeouts).outbound(messages).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
// We make ourselves the leader
timeouts.invokeTimeout(ELECTION);
raft.handle(voteResponse().from(member1).term(1).grant().build());
// When
raft.setTargetMembershipSet(asSet(myself, member1, member2, newMember));
network.processMessages();
timeouts.invokeTimeout(RaftMachine.Timeouts.HEARTBEAT);
network.processMessages();
// Then
assertEquals(RaftMessages.AppendEntries.Request.class, messages.sentTo(newMember).get(0).getClass());
}
use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.
the class DisconnectLeaderScenario method oneIteration.
private long oneIteration(long leaderStabilityMaxTimeMillis) throws InterruptedException, TimeoutException {
List<RaftMachine> rafts = fixture.rafts.stream().map(Fixture.RaftFixture::raftMachine).collect(toList());
MemberId oldLeader = ElectionUtil.waitForLeaderAgreement(rafts, leaderStabilityMaxTimeMillis);
long startTime = System.currentTimeMillis();
fixture.net.disconnect(oldLeader);
MemberId newLeader = ElectionUtil.waitForLeaderAgreement(new FilteringIterable<>(rafts, raft -> !raft.identity().equals(oldLeader)), leaderStabilityMaxTimeMillis);
// this should be guaranteed by the waitForLeaderAgreement call
assert !newLeader.equals(oldLeader);
return System.currentTimeMillis() - startTime;
}
use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.
the class ElectionPerformanceIT method electionPerformance_RapidConditions.
@Test
public void electionPerformance_RapidConditions() throws Throwable {
// given parameters
final long networkLatency = 1L;
final long electionTimeout = 30L;
final long heartbeatInterval = 15L;
final int iterations = 100;
TestNetwork net = new TestNetwork<>((i, o) -> networkLatency);
Set<MemberId> members = asSet(member(0), member(1), member(2));
Fixture fixture = new Fixture(members, net, electionTimeout, heartbeatInterval);
DisconnectLeaderScenario scenario = new DisconnectLeaderScenario(fixture, electionTimeout);
try {
// when running scenario
fixture.boot();
scenario.run(iterations, 10 * electionTimeout);
} finally {
fixture.tearDown();
}
DisconnectLeaderScenario.Result result = scenario.result();
/* These bounds have been experimentally established and should have a very low
* likelihood for false positives without an actual major regression. If this test fails
* then the recommended action is to run the test manually and interpret the results
* to guide further action. Perhaps the power of the test has to be improved, but
* the intention here is not to catch anything but the most major of regressions. */
assertThat(result.nonCollidingAverage, lessThan(2.0 * electionTimeout));
// because of the high number of iterations, it is possible to assert on the collision rate
assertThat(result.collisionRate, lessThan(0.50d));
if (result.collisionCount > 10) {
assertThat(result.collidingAverage, lessThan(5.0 * electionTimeout));
}
// for GC or whatever reason
assertThat(result.timeoutCount, lessThanOrEqualTo(1L));
}
use of org.neo4j.causalclustering.identity.MemberId in project neo4j by neo4j.
the class RaftGroupMembershipTest method shouldRemoveMultipleInstancesFromExistingRaftGroup.
@Test
public void shouldRemoveMultipleInstancesFromExistingRaftGroup() 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 toBeRemoved3 = member(4);
final MemberId[] initialMembers = { leader, stable, toBeRemoved1, toBeRemoved2, toBeRemoved3 };
final MemberId[] finalMembers = { leader, stable };
RaftTestFixture fixture = new RaftTestFixture(net, 2, initialMembers);
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();
// 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.identity.MemberId in project neo4j by neo4j.
the class RaftGroupMembershipTest method shouldRemoveLeaderFromExistingRaftGroupAndActivelyTransferLeadership.
@Test
public void shouldRemoveLeaderFromExistingRaftGroupAndActivelyTransferLeadership() throws Exception {
DirectNetworking net = new DirectNetworking();
// given
final MemberId leader = member(0);
final MemberId stable1 = member(1);
final MemberId stable2 = member(2);
final MemberId[] initialMembers = { leader, stable1, stable2 };
final MemberId[] finalMembers = { stable1, stable2 };
RaftTestFixture fixture = new RaftTestFixture(net, 2, initialMembers);
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(stable1).timeoutService().invokeTimeout(ELECTION);
net.processMessages();
// then
assertThat(fixture.members().withIds(finalMembers), hasCurrentMembers(new RaftTestGroup(finalMembers)));
assertTrue(fixture.members().withId(stable1).raftInstance().isLeader() || fixture.members().withId(stable2).raftInstance().isLeader());
}
Aggregations