Search in sources :

Example 16 with RaftCoreState

use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.

the class RaftTestFixture method bootstrap.

public void bootstrap(MemberId[] members) throws RaftMachine.BootstrapException, IOException {
    for (MemberFixture member : members()) {
        member.raftLog().append(new RaftLogEntry(0, new MemberIdSet(asSet(members))));
        member.raftInstance().installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(members))));
        member.raftInstance().startTimers();
    }
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) MemberIdSet(org.neo4j.causalclustering.core.consensus.membership.MemberIdSet) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 17 with RaftCoreState

use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.

the class Fixture method boot.

void boot() throws BootstrapException, TimeoutException, InterruptedException, IOException {
    for (RaftFixture raft : rafts) {
        raft.raftLog().append(new RaftLogEntry(0, new MemberIdSet(asSet(members))));
        raft.raftMachine().installCoreState(new RaftCoreState(new MembershipEntry(0, members)));
        raft.raftMachine.startTimers();
    }
    net.start();
    awaitBootstrapped();
}
Also used : MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) MemberIdSet(org.neo4j.causalclustering.core.consensus.membership.MemberIdSet) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)

Example 18 with RaftCoreState

use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.

the class CoreBootstrapperTest method shouldSetAllCoreState.

@Test
public void shouldSetAllCoreState() throws Exception {
    // given
    int nodeCount = 100;
    FileSystemAbstraction fileSystem = fileSystemRule.get();
    File classicNeo4jStore = RestoreClusterUtils.createClassicNeo4jStore(testDirectory.directory(), fileSystem, nodeCount, Standard.LATEST_NAME);
    PageCache pageCache = pageCacheRule.getPageCache(fileSystem);
    CoreBootstrapper bootstrapper = new CoreBootstrapper(classicNeo4jStore, pageCache, fileSystem, Config.defaults(), NullLogProvider.getInstance());
    // when
    Set<MemberId> membership = asSet(randomMember(), randomMember(), randomMember());
    CoreSnapshot snapshot = bootstrapper.bootstrap(membership);
    // then
    assertEquals(nodeCount, ((IdAllocationState) snapshot.get(CoreStateType.ID_ALLOCATION)).firstUnallocated(IdType.NODE));
    /* Bootstrapped state is created in RAFT land at index -1 and term -1. */
    assertEquals(0, snapshot.prevIndex());
    assertEquals(0, snapshot.prevTerm());
    /* Lock is initially not taken. */
    assertEquals(new ReplicatedLockTokenState(), snapshot.get(CoreStateType.LOCK_TOKEN));
    /* Raft has the bootstrapped set of members initially. */
    assertEquals(membership, ((RaftCoreState) snapshot.get(CoreStateType.RAFT_CORE_STATE)).committed().members());
    /* The session state is initially empty. */
    assertEquals(new GlobalSessionTrackerState(), snapshot.get(CoreStateType.SESSION_TRACKER));
    LastCommittedIndexFinder lastCommittedIndexFinder = new LastCommittedIndexFinder(new ReadOnlyTransactionIdStore(pageCache, classicNeo4jStore), new ReadOnlyTransactionStore(pageCache, fileSystem, classicNeo4jStore, new Monitors()), NullLogProvider.getInstance());
    long lastCommittedIndex = lastCommittedIndexFinder.getLastCommittedIndex();
    assertEquals(-1, lastCommittedIndex);
}
Also used : FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) LastCommittedIndexFinder(org.neo4j.causalclustering.core.state.machines.tx.LastCommittedIndexFinder) ReadOnlyTransactionStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionStore) GlobalSessionTrackerState(org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) ReadOnlyTransactionIdStore(org.neo4j.kernel.impl.transaction.log.ReadOnlyTransactionIdStore) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) Monitors(org.neo4j.kernel.monitoring.Monitors) ReplicatedLockTokenState(org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 19 with RaftCoreState

use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.

the class ElectionTest method candidateShouldLoseElectionAndRemainCandidate.

@Test
public void candidateShouldLoseElectionAndRemainCandidate() throws Exception {
    // Note the etcd implementation seems to diverge from the paper here, since the paper suggests that it should
    // remain as a candidate
    // given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    timeouts.invokeTimeout(RaftMachine.Timeouts.ELECTION);
    // when
    raft.handle(voteResponse().from(member1).term(1).deny().build());
    raft.handle(voteResponse().from(member2).term(1).deny().build());
    // then
    assertEquals(1, raft.term());
    assertEquals(CANDIDATE, raft.currentRole());
    verify(outbound, never()).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
    verify(outbound, never()).send(eq(member2), isA(RaftMessages.AppendEntries.Request.class));
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) TestMessageBuilders.voteRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Example 20 with RaftCoreState

use of org.neo4j.causalclustering.core.state.snapshot.RaftCoreState in project neo4j by neo4j.

the class ElectionTest method candidateShouldWinElectionAndBecomeLeader.

@Test
public void candidateShouldWinElectionAndBecomeLeader() throws Exception {
    // given
    FakeClock fakeClock = Clocks.fakeClock();
    ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
    RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
    raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
    raft.startTimers();
    timeouts.invokeTimeout(RaftMachine.Timeouts.ELECTION);
    // when
    raft.handle(voteResponse().from(member1).term(1).grant().build());
    raft.handle(voteResponse().from(member2).term(1).grant().build());
    // then
    assertEquals(1, raft.term());
    assertEquals(LEADER, raft.currentRole());
    verify(outbound).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
    verify(outbound).send(eq(member2), isA(RaftMessages.AppendEntries.Request.class));
}
Also used : RaftMachine(org.neo4j.causalclustering.core.consensus.RaftMachine) RaftMachineBuilder(org.neo4j.causalclustering.core.consensus.RaftMachineBuilder) MembershipEntry(org.neo4j.causalclustering.core.consensus.membership.MembershipEntry) RaftCoreState(org.neo4j.causalclustering.core.state.snapshot.RaftCoreState) FakeClock(org.neo4j.time.FakeClock) TestMessageBuilders.voteRequest(org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ControlledRenewableTimeoutService(org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService) Test(org.junit.Test)

Aggregations

RaftCoreState (org.neo4j.causalclustering.core.state.snapshot.RaftCoreState)22 MembershipEntry (org.neo4j.causalclustering.core.consensus.membership.MembershipEntry)21 Test (org.junit.Test)19 ControlledRenewableTimeoutService (org.neo4j.causalclustering.core.consensus.schedule.ControlledRenewableTimeoutService)17 FakeClock (org.neo4j.time.FakeClock)16 RaftMachine (org.neo4j.causalclustering.core.consensus.RaftMachine)3 RaftMachineBuilder (org.neo4j.causalclustering.core.consensus.RaftMachineBuilder)3 TestMessageBuilders.voteRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.voteRequest)3 MemberId (org.neo4j.causalclustering.identity.MemberId)3 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)2 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)2 MemberIdSet (org.neo4j.causalclustering.core.consensus.membership.MemberIdSet)2 GlobalSessionTrackerState (org.neo4j.causalclustering.core.replication.session.GlobalSessionTrackerState)2 ReplicatedLockTokenState (org.neo4j.causalclustering.core.state.machines.locks.ReplicatedLockTokenState)2 CoreSnapshot (org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot)2 Monitors (org.neo4j.kernel.monitoring.Monitors)2 File (java.io.File)1 Request (org.neo4j.causalclustering.core.consensus.RaftMessages.NewEntry.Request)1 TestMessageBuilders.appendEntriesRequest (org.neo4j.causalclustering.core.consensus.TestMessageBuilders.appendEntriesRequest)1 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)1