Search in sources :

Example 1 with ReadableChannel

use of org.neo4j.storageengine.api.ReadableChannel 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 2 with ReadableChannel

use of org.neo4j.storageengine.api.ReadableChannel in project neo4j by neo4j.

the class PhysicalLogCommandReadersTest method channelWithRelGroupRecord.

private static ReadableChannel channelWithRelGroupRecord(long id, byte inUse, short type, long next, long firstOut, long firstIn, long firstLoop, long owningNode) throws IOException {
    ReadableChannel channel = mock(ReadableChannel.class);
    when(channel.get()).thenReturn(NeoCommandType.REL_GROUP_COMMAND).thenReturn(inUse);
    when(channel.getLong()).thenReturn(id).thenReturn(next).thenReturn(firstOut).thenReturn(firstIn).thenReturn(firstLoop).thenReturn(owningNode);
    when(channel.getShort()).thenReturn(type);
    return channel;
}
Also used : ReadableChannel(org.neo4j.storageengine.api.ReadableChannel)

Aggregations

ReadableChannel (org.neo4j.storageengine.api.ReadableChannel)2 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1 ReplicatedContent (org.neo4j.causalclustering.core.replication.ReplicatedContent)1 ClusterId (org.neo4j.causalclustering.identity.ClusterId)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 NetworkReadableClosableChannelNetty4 (org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4)1