use of org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4 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));
}
use of org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4 in project neo4j by neo4j.
the class ReplicatedTransactionFactory method extractTransactionRepresentation.
public static TransactionRepresentation extractTransactionRepresentation(ReplicatedTransaction transactionCommand, byte[] extraHeader) {
ByteBuf txBuffer = Unpooled.wrappedBuffer(transactionCommand.getTxBytes());
NetworkReadableClosableChannelNetty4 channel = new NetworkReadableClosableChannelNetty4(txBuffer);
try {
return read(channel, extraHeader);
} catch (IOException e) {
// Easier said than done though, we use the LogEntry handling routines which throw IOException
throw new RuntimeException(e);
}
}
use of org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4 in project neo4j by neo4j.
the class RaftMembershipStateTest method shouldMarshalCorrectly.
@Test
public void shouldMarshalCorrectly() throws Exception {
// given
RaftMembershipState.Marshal marshal = new RaftMembershipState.Marshal();
state = new RaftMembershipState(5, new MembershipEntry(7, membersA), new MembershipEntry(8, membersB));
// when
ByteBuf buffer = Unpooled.buffer(1_000);
marshal.marshal(state, new NetworkFlushableChannelNetty4(buffer));
final RaftMembershipState recovered = marshal.unmarshal(new NetworkReadableClosableChannelNetty4(buffer));
// then
assertEquals(state, recovered);
}
Aggregations