Search in sources :

Example 1 with RaftMessage

use of org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage in project neo4j by neo4j.

the class FollowerTest method followerReceivingHeartbeatIndicatingClusterIsAheadShouldElicitAppendResponse.

@Test
public void followerReceivingHeartbeatIndicatingClusterIsAheadShouldElicitAppendResponse() throws Exception {
    // given
    int term = 1;
    int followerAppendIndex = 9;
    RaftLog entryLog = new InMemoryRaftLog();
    entryLog.append(new RaftLogEntry(0, new RaftTestGroup(0)));
    RaftState state = raftState().myself(myself).term(term).build();
    Follower follower = new Follower();
    appendSomeEntriesToLog(state, follower, followerAppendIndex - 1, term, 1);
    AppendEntries.Request heartbeat = appendEntriesRequest().from(member1).leaderTerm(term).prevLogIndex(// leader has appended 2 ahead from this follower
    followerAppendIndex + 2).prevLogTerm(// in the same term
    term).build();
    Outcome outcome = follower.handle(heartbeat, state, log());
    assertEquals(1, outcome.getOutgoingMessages().size());
    RaftMessage outgoing = outcome.getOutgoingMessages().iterator().next().message();
    assertEquals(RaftMessages.Type.APPEND_ENTRIES_RESPONSE, outgoing.type());
    RaftMessages.AppendEntries.Response response = (AppendEntries.Response) outgoing;
    assertFalse(response.success());
}
Also used : RaftTestGroup(org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendEntries(org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) RaftMessage(org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) InMemoryRaftLog(org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog) RaftLog(org.neo4j.causalclustering.core.consensus.log.RaftLog) Test(org.junit.Test)

Example 2 with RaftMessage

use of org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage in project neo4j by neo4j.

the class BatchingMessageHandler method collateAndHandleBatch.

private void collateAndHandleBatch(List<RaftMessages.ClusterIdAwareMessage> batch) {
    RaftMessages.NewEntry.BatchRequest batchRequest = null;
    ClusterId clusterId = batch.get(0).clusterId();
    for (RaftMessages.ClusterIdAwareMessage clusterIdAwareMessage : batch) {
        if (batchRequest != null && !clusterIdAwareMessage.clusterId().equals(clusterId)) {
            handler.handle(new RaftMessages.ClusterIdAwareMessage(clusterId, batchRequest));
            batchRequest = null;
        }
        clusterId = clusterIdAwareMessage.clusterId();
        RaftMessage message = clusterIdAwareMessage.message();
        if (message instanceof RaftMessages.NewEntry.Request) {
            RaftMessages.NewEntry.Request newEntryRequest = (RaftMessages.NewEntry.Request) message;
            if (batchRequest == null) {
                batchRequest = new RaftMessages.NewEntry.BatchRequest(batch.size());
            }
            batchRequest.add(newEntryRequest.content());
        } else {
            handler.handle(clusterIdAwareMessage);
        }
    }
    if (batchRequest != null) {
        handler.handle(new RaftMessages.ClusterIdAwareMessage(clusterId, batchRequest));
    }
}
Also used : ClusterId(org.neo4j.causalclustering.identity.ClusterId) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftMessage(org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage)

Aggregations

RaftMessage (org.neo4j.causalclustering.core.consensus.RaftMessages.RaftMessage)2 Test (org.junit.Test)1 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)1 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)1 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)1 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)1 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)1 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)1 ClusterId (org.neo4j.causalclustering.identity.ClusterId)1