Search in sources :

Example 6 with ReplicatedContent

use of org.neo4j.causalclustering.core.replication.ReplicatedContent in project neo4j by neo4j.

the class RaftMessageDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> list) throws Exception {
    ReadableChannel channel = new NetworkReadableClosableChannelNetty4(buffer);
    ClusterId clusterId = ClusterId.Marshal.INSTANCE.unmarshal(channel);
    int messageTypeWire = channel.getInt();
    RaftMessages.Type[] values = RaftMessages.Type.values();
    RaftMessages.Type messageType = values[messageTypeWire];
    MemberId from = retrieveMember(channel);
    RaftMessages.RaftMessage result;
    if (messageType.equals(VOTE_REQUEST)) {
        MemberId candidate = retrieveMember(channel);
        long term = channel.getLong();
        long lastLogIndex = channel.getLong();
        long lastLogTerm = channel.getLong();
        result = new RaftMessages.Vote.Request(from, term, candidate, lastLogIndex, lastLogTerm);
    } else if (messageType.equals(VOTE_RESPONSE)) {
        long term = channel.getLong();
        boolean voteGranted = channel.get() == 1;
        result = new RaftMessages.Vote.Response(from, term, voteGranted);
    } else if (messageType.equals(APPEND_ENTRIES_REQUEST)) {
        // how many
        long term = channel.getLong();
        long prevLogIndex = channel.getLong();
        long prevLogTerm = channel.getLong();
        long leaderCommit = channel.getLong();
        long count = channel.getLong();
        RaftLogEntry[] entries = new RaftLogEntry[(int) count];
        for (int i = 0; i < count; i++) {
            long entryTerm = channel.getLong();
            final ReplicatedContent content = marshal.unmarshal(channel);
            entries[i] = new RaftLogEntry(entryTerm, content);
        }
        result = new RaftMessages.AppendEntries.Request(from, term, prevLogIndex, prevLogTerm, entries, leaderCommit);
    } else if (messageType.equals(APPEND_ENTRIES_RESPONSE)) {
        long term = channel.getLong();
        boolean success = channel.get() == 1;
        long matchIndex = channel.getLong();
        long appendIndex = channel.getLong();
        result = new RaftMessages.AppendEntries.Response(from, term, success, matchIndex, appendIndex);
    } else if (messageType.equals(NEW_ENTRY_REQUEST)) {
        ReplicatedContent content = marshal.unmarshal(channel);
        result = new RaftMessages.NewEntry.Request(from, content);
    } else if (messageType.equals(HEARTBEAT)) {
        long leaderTerm = channel.getLong();
        long commitIndexTerm = channel.getLong();
        long commitIndex = channel.getLong();
        result = new RaftMessages.Heartbeat(from, leaderTerm, commitIndex, commitIndexTerm);
    } else if (messageType.equals(HEARTBEAT_RESPONSE)) {
        result = new RaftMessages.HeartbeatResponse(from);
    } else if (messageType.equals(LOG_COMPACTION_INFO)) {
        long leaderTerm = channel.getLong();
        long prevIndex = channel.getLong();
        result = new RaftMessages.LogCompactionInfo(from, leaderTerm, prevIndex);
    } else {
        throw new IllegalArgumentException("Unknown message type");
    }
    list.add(new RaftMessages.ClusterIdAwareMessage(clusterId, result));
}
Also used : ReadableChannel(org.neo4j.storageengine.api.ReadableChannel) NetworkReadableClosableChannelNetty4(org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) MemberId(org.neo4j.causalclustering.identity.MemberId) ClusterId(org.neo4j.causalclustering.identity.ClusterId) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent)

Example 7 with ReplicatedContent

use of org.neo4j.causalclustering.core.replication.ReplicatedContent in project neo4j by neo4j.

the class CoreReplicatedContentMarshal method unmarshal0.

@Override
public ReplicatedContent unmarshal0(ReadableChannel channel) throws IOException, EndOfStreamException {
    byte type = channel.get();
    final ReplicatedContent content;
    switch(type) {
        case TX_CONTENT_TYPE:
            content = ReplicatedTransactionSerializer.unmarshal(channel);
            break;
        case RAFT_MEMBER_SET_TYPE:
            content = MemberIdSetSerializer.unmarshal(channel);
            break;
        case ID_RANGE_REQUEST_TYPE:
            content = ReplicatedIdAllocationRequestSerializer.unmarshal(channel);
            break;
        case TOKEN_REQUEST_TYPE:
            content = ReplicatedTokenRequestSerializer.unmarshal(channel);
            break;
        case NEW_LEADER_BARRIER_TYPE:
            content = new NewLeaderBarrier();
            break;
        case LOCK_TOKEN_REQUEST:
            content = ReplicatedLockTokenSerializer.unmarshal(channel);
            break;
        case DISTRIBUTED_OPERATION:
            content = DistributedOperation.deserialize(channel);
            break;
        default:
            throw new IllegalArgumentException(String.format("Unknown content type 0x%x", type));
    }
    return content;
}
Also used : ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)

Example 8 with ReplicatedContent

use of org.neo4j.causalclustering.core.replication.ReplicatedContent in project neo4j by neo4j.

the class ReplayRaftLog method main.

public static void main(String[] args) throws IOException {
    Args arg = Args.parse(args);
    String from = arg.get("from");
    System.out.println("From is " + from);
    String to = arg.get("to");
    System.out.println("to is " + to);
    File logDirectory = new File(from);
    System.out.println("logDirectory = " + logDirectory);
    Config config = Config.embeddedDefaults(stringMap());
    try (DefaultFileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction()) {
        LogProvider logProvider = getInstance();
        CoreLogPruningStrategy pruningStrategy = new CoreLogPruningStrategyFactory(config.get(raft_log_pruning_strategy), logProvider).newInstance();
        SegmentedRaftLog log = new SegmentedRaftLog(fileSystem, logDirectory, config.get(raft_log_rotation_size), new CoreReplicatedContentMarshal(), logProvider, config.get(raft_log_reader_pool_size), Clocks.systemClock(), new OnDemandJobScheduler(), pruningStrategy);
        // Not really, but we need to have a way to pass in the commit index
        long totalCommittedEntries = log.appendIndex();
        for (int i = 0; i <= totalCommittedEntries; i++) {
            ReplicatedContent content = readLogEntry(log, i).content();
            if (content instanceof ReplicatedTransaction) {
                ReplicatedTransaction tx = (ReplicatedTransaction) content;
                ReplicatedTransactionFactory.extractTransactionRepresentation(tx, new byte[0]).accept(element -> {
                    System.out.println(element);
                    return false;
                });
            }
        }
    }
}
Also used : Args(org.neo4j.helpers.Args) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) SegmentedRaftLog(org.neo4j.causalclustering.core.consensus.log.segmented.SegmentedRaftLog) ReplicatedTransaction(org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransaction) Config(org.neo4j.kernel.configuration.Config) CoreLogPruningStrategyFactory(org.neo4j.causalclustering.core.consensus.log.segmented.CoreLogPruningStrategyFactory) CoreLogPruningStrategy(org.neo4j.causalclustering.core.consensus.log.segmented.CoreLogPruningStrategy) OnDemandJobScheduler(org.neo4j.test.OnDemandJobScheduler) LogProvider(org.neo4j.logging.LogProvider) CoreReplicatedContentMarshal(org.neo4j.causalclustering.messaging.CoreReplicatedContentMarshal) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) File(java.io.File)

Example 9 with ReplicatedContent

use of org.neo4j.causalclustering.core.replication.ReplicatedContent in project neo4j by neo4j.

the class CommandApplicationProcessTest method shouldFallbackToLogCursorOnCacheMiss.

@Test
public void shouldFallbackToLogCursorOnCacheMiss() throws Throwable {
    // if the cache does not contain all things to be applied, make sure we fall back to the log
    // should only happen in recovery, otherwise this is probably a bug.
    applicationProcess.start();
    //given cache with missing entry
    ReplicatedContent operation0 = operation(nullTx);
    ReplicatedContent operation1 = operation(nullTx);
    ReplicatedContent operation2 = operation(nullTx);
    inFlightMap.put(0L, new RaftLogEntry(0, operation0));
    inFlightMap.put(2L, new RaftLogEntry(2, operation2));
    raftLog.append(new RaftLogEntry(0, operation0));
    raftLog.append(new RaftLogEntry(1, operation1));
    raftLog.append(new RaftLogEntry(2, operation2));
    //when
    applicationProcess.notifyCommitted(2);
    applier.sync(false);
    //then the cache stops being used after it finds 1 is missing
    verify(inFlightMap, times(1)).get(0L);
    verify(inFlightMap, times(1)).get(1L);
    verify(inFlightMap, times(0)).get(2L);
    // we only look up 1 so let's remove that from the cache
    verify(inFlightMap, times(1)).remove(0L);
    verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(0L), anyCallback());
    verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(1L), anyCallback());
    verify(commandDispatcher, times(1)).dispatch(eq(nullTx), eq(2L), anyCallback());
    verify(raftLog, times(1)).getEntryCursor(1);
}
Also used : CoreReplicatedContent(org.neo4j.causalclustering.core.state.machines.tx.CoreReplicatedContent) ReplicatedContent(org.neo4j.causalclustering.core.replication.ReplicatedContent) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Aggregations

ReplicatedContent (org.neo4j.causalclustering.core.replication.ReplicatedContent)9 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)3 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)3 List (java.util.List)2 Test (org.junit.Test)2 Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)2 CoreReplicatedContentMarshal (org.neo4j.causalclustering.messaging.CoreReplicatedContentMarshal)2 ByteBuf (io.netty.buffer.ByteBuf)1 File (java.io.File)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 ArrayList (java.util.ArrayList)1 NewLeaderBarrier (org.neo4j.causalclustering.core.consensus.NewLeaderBarrier)1 Heartbeat (org.neo4j.causalclustering.core.consensus.RaftMessages.Heartbeat)1 LogCompactionInfo (org.neo4j.causalclustering.core.consensus.RaftMessages.LogCompactionInfo)1 CoreLogPruningStrategy (org.neo4j.causalclustering.core.consensus.log.segmented.CoreLogPruningStrategy)1 CoreLogPruningStrategyFactory (org.neo4j.causalclustering.core.consensus.log.segmented.CoreLogPruningStrategyFactory)1 SegmentedRaftLog (org.neo4j.causalclustering.core.consensus.log.segmented.SegmentedRaftLog)1 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)1 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)1