use of org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf in project neo4j by neo4j.
the class CoreReplicatedContentMarshalTest method assertMarshalingEquality.
private void assertMarshalingEquality(ByteBuf buffer, ReplicatedContent replicatedTx) throws IOException, EndOfStreamException {
marshal.marshal(replicatedTx, new NetworkFlushableByteBuf(buffer));
assertThat(marshal.unmarshal(new NetworkReadableClosableChannelNetty4(buffer)), equalTo(replicatedTx));
}
use of org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf in project neo4j by neo4j.
the class TxPullResponseEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, TxPullResponse response, ByteBuf out) throws Exception {
NetworkFlushableByteBuf channel = new NetworkFlushableByteBuf(out);
StoreIdMarshal.INSTANCE.marshal(response.storeId(), channel);
new CommittedTransactionSerializer(channel).visit(response.tx());
}
use of org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf in project neo4j by neo4j.
the class RaftContentByteBufferMarshalTest method assertMarshalingEquality.
private void assertMarshalingEquality(CoreReplicatedContentMarshal marshal, ByteBuf buffer, ReplicatedContent replicatedTx) throws IOException, EndOfStreamException {
marshal.marshal(replicatedTx, new NetworkFlushableByteBuf(buffer));
assertThat(marshal.unmarshal(new NetworkReadableClosableChannelNetty4(buffer)), equalTo(replicatedTx));
}
use of org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf in project neo4j by neo4j.
the class RaftContentByteBufferMarshalTest method shouldSerializeTransactionRepresentation.
@Test
public void shouldSerializeTransactionRepresentation() throws Exception {
// given
CoreReplicatedContentMarshal serializer = new CoreReplicatedContentMarshal();
Collection<StorageCommand> commands = new ArrayList<>();
IndexCommand.AddNodeCommand addNodeCommand = new IndexCommand.AddNodeCommand();
addNodeCommand.init(0, 0, 0, 0);
commands.add(addNodeCommand);
byte[] extraHeader = new byte[0];
PhysicalTransactionRepresentation txIn = new PhysicalTransactionRepresentation(commands);
txIn.setHeader(extraHeader, -1, -1, 0, 0, 0, 0);
ReplicatedTransaction in = ReplicatedTransactionFactory.createImmutableReplicatedTransaction(txIn);
// when
ByteBuf buf = Unpooled.buffer();
serializer.marshal(in, new NetworkFlushableByteBuf(buf));
ReplicatedTransaction out = (ReplicatedTransaction) serializer.unmarshal(new NetworkReadableClosableChannelNetty4(buf));
TransactionRepresentation txOut = ReplicatedTransactionFactory.extractTransactionRepresentation(out, extraHeader);
// then
assertEquals(in, out);
assertEquals(txIn, txOut);
}
use of org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf in project neo4j by neo4j.
the class RaftMessageEncoder method encode.
@Override
protected synchronized void encode(ChannelHandlerContext ctx, RaftMessages.ClusterIdAwareMessage decoratedMessage, ByteBuf out) throws Exception {
RaftMessages.RaftMessage message = decoratedMessage.message();
ClusterId clusterId = decoratedMessage.clusterId();
MemberId.Marshal memberMarshal = new MemberId.Marshal();
NetworkFlushableByteBuf channel = new NetworkFlushableByteBuf(out);
ClusterId.Marshal.INSTANCE.marshal(clusterId, channel);
channel.putInt(message.type().ordinal());
memberMarshal.marshal(message.from(), channel);
if (message instanceof RaftMessages.Vote.Request) {
RaftMessages.Vote.Request voteRequest = (RaftMessages.Vote.Request) message;
memberMarshal.marshal(voteRequest.candidate(), channel);
channel.putLong(voteRequest.term());
channel.putLong(voteRequest.lastLogIndex());
channel.putLong(voteRequest.lastLogTerm());
} else if (message instanceof RaftMessages.Vote.Response) {
RaftMessages.Vote.Response voteResponse = (RaftMessages.Vote.Response) message;
channel.putLong(voteResponse.term());
channel.put((byte) (voteResponse.voteGranted() ? 1 : 0));
} else if (message instanceof RaftMessages.AppendEntries.Request) {
RaftMessages.AppendEntries.Request appendRequest = (RaftMessages.AppendEntries.Request) message;
channel.putLong(appendRequest.leaderTerm());
channel.putLong(appendRequest.prevLogIndex());
channel.putLong(appendRequest.prevLogTerm());
channel.putLong(appendRequest.leaderCommit());
channel.putLong(appendRequest.entries().length);
for (RaftLogEntry raftLogEntry : appendRequest.entries()) {
channel.putLong(raftLogEntry.term());
marshal.marshal(raftLogEntry.content(), channel);
}
} else if (message instanceof RaftMessages.AppendEntries.Response) {
RaftMessages.AppendEntries.Response appendResponse = (RaftMessages.AppendEntries.Response) message;
channel.putLong(appendResponse.term());
channel.put((byte) (appendResponse.success() ? 1 : 0));
channel.putLong(appendResponse.matchIndex());
channel.putLong(appendResponse.appendIndex());
} else if (message instanceof RaftMessages.NewEntry.Request) {
RaftMessages.NewEntry.Request newEntryRequest = (RaftMessages.NewEntry.Request) message;
marshal.marshal(newEntryRequest.content(), channel);
} else if (message instanceof RaftMessages.Heartbeat) {
RaftMessages.Heartbeat heartbeat = (RaftMessages.Heartbeat) message;
channel.putLong(heartbeat.leaderTerm());
channel.putLong(heartbeat.commitIndexTerm());
channel.putLong(heartbeat.commitIndex());
} else if (message instanceof RaftMessages.HeartbeatResponse) {
//Heartbeat Response does not have any data attached to it.
} else if (message instanceof RaftMessages.LogCompactionInfo) {
RaftMessages.LogCompactionInfo logCompactionInfo = (RaftMessages.LogCompactionInfo) message;
channel.putLong(logCompactionInfo.leaderTerm());
channel.putLong(logCompactionInfo.prevIndex());
} else {
throw new IllegalArgumentException("Unknown message type: " + message);
}
}
Aggregations