Search in sources :

Example 66 with MemberId

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());
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) FakeClock(org.neo4j.time.FakeClock) Inbound(org.neo4j.causalclustering.messaging.Inbound) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) MemberId(org.neo4j.causalclustering.identity.MemberId) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) Test(org.junit.Test)

Example 67 with MemberId

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;
}
Also used : List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TimeoutException(java.util.concurrent.TimeoutException) MemberId(org.neo4j.causalclustering.identity.MemberId) FilteringIterable(org.neo4j.helpers.collection.FilteringIterable) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) MemberId(org.neo4j.causalclustering.identity.MemberId)

Example 68 with MemberId

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));
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) TestNetwork(org.neo4j.causalclustering.messaging.TestNetwork) Test(org.junit.Test)

Example 69 with MemberId

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());
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) RaftTestFixture(org.neo4j.causalclustering.core.consensus.RaftTestFixture) DirectNetworking(org.neo4j.causalclustering.core.consensus.DirectNetworking) Test(org.junit.Test)

Example 70 with MemberId

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());
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) RaftTestFixture(org.neo4j.causalclustering.core.consensus.RaftTestFixture) DirectNetworking(org.neo4j.causalclustering.core.consensus.DirectNetworking) Test(org.junit.Test)

Aggregations

MemberId (org.neo4j.causalclustering.identity.MemberId)114 Test (org.junit.Test)83 HashMap (java.util.HashMap)26 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)16 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)15 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)13 ReadReplicaTopology (org.neo4j.causalclustering.discovery.ReadReplicaTopology)12 DirectNetworking (org.neo4j.causalclustering.core.consensus.DirectNetworking)10 RaftTestFixture (org.neo4j.causalclustering.core.consensus.RaftTestFixture)10 CoreTopologyService (org.neo4j.causalclustering.discovery.CoreTopologyService)10 Log (org.neo4j.logging.Log)10 ClusterId (org.neo4j.causalclustering.identity.ClusterId)9 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)8 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)8 TopologyService (org.neo4j.causalclustering.discovery.TopologyService)7 ByteBuf (io.netty.buffer.ByteBuf)6 File (java.io.File)6 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)6 HashSet (java.util.HashSet)5