Search in sources :

Example 1 with NetworkFlushableByteBuf

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));
}
Also used : NetworkFlushableByteBuf(org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf) NetworkReadableClosableChannelNetty4(org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4)

Example 2 with NetworkFlushableByteBuf

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());
}
Also used : NetworkFlushableByteBuf(org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf) CommittedTransactionSerializer(org.neo4j.com.CommittedTransactionSerializer)

Example 3 with NetworkFlushableByteBuf

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));
}
Also used : NetworkFlushableByteBuf(org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf) NetworkReadableClosableChannelNetty4(org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4)

Example 4 with NetworkFlushableByteBuf

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);
}
Also used : NetworkFlushableByteBuf(org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf) ReplicatedTransaction(org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransaction) StorageCommand(org.neo4j.storageengine.api.StorageCommand) TransactionRepresentation(org.neo4j.kernel.impl.transaction.TransactionRepresentation) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) NetworkFlushableByteBuf(org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf) NetworkReadableClosableChannelNetty4(org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4) CoreReplicatedContentMarshal(org.neo4j.causalclustering.messaging.CoreReplicatedContentMarshal) IndexCommand(org.neo4j.kernel.impl.index.IndexCommand) PhysicalTransactionRepresentation(org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation) Test(org.junit.Test)

Example 5 with NetworkFlushableByteBuf

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);
    }
}
Also used : NetworkFlushableByteBuf(org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf) ClusterId(org.neo4j.causalclustering.identity.ClusterId) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) MemberId(org.neo4j.causalclustering.identity.MemberId)

Aggregations

NetworkFlushableByteBuf (org.neo4j.causalclustering.messaging.NetworkFlushableByteBuf)5 NetworkReadableClosableChannelNetty4 (org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4)3 ByteBuf (io.netty.buffer.ByteBuf)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)1 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)1 ReplicatedTransaction (org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransaction)1 ClusterId (org.neo4j.causalclustering.identity.ClusterId)1 MemberId (org.neo4j.causalclustering.identity.MemberId)1 CoreReplicatedContentMarshal (org.neo4j.causalclustering.messaging.CoreReplicatedContentMarshal)1 CommittedTransactionSerializer (org.neo4j.com.CommittedTransactionSerializer)1 IndexCommand (org.neo4j.kernel.impl.index.IndexCommand)1 TransactionRepresentation (org.neo4j.kernel.impl.transaction.TransactionRepresentation)1 PhysicalTransactionRepresentation (org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation)1 StorageCommand (org.neo4j.storageengine.api.StorageCommand)1