Search in sources :

Example 21 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class RaftState method saveState.

/**
 * Serialize state machine and write to outputstream
 *
 * @param out          outputstream
 * @throws IOException on any IO error
 */
@Override
public void saveState(OutputStream out) throws IOException {
    int len = ProtoUtil.varIntLen(sessions.size());
    for (Session session : sessions.values()) {
        len += session.encodedLen();
    }
    len += record.encodedLen();
    Buffer buf = new Buffer(len + ProtoUtil.varIntLen(len));
    buf.putVarInt(len);
    buf.putVarInt(sessions.size());
    for (Session session : sessions.values()) {
        session.encode(buf);
    }
    record.encode(buf);
    buf.flip();
    out.write(buf.backend().array());
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer) Session(tezc.core.cluster.Session)

Example 22 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class RaftState method onCommand.

/**
 * Command callback for state machine
 * @param buf raw encoded command
 */
@Override
public void onCommand(ByteBuffer buf) {
    Buffer buffer = new Buffer(buf);
    Command command = Command.create(buffer);
    command.handle(this);
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 23 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class AppendReq method encode.

/**
 * Encode message
 */
@Override
public void encode() {
    if (!rawReady) {
        length = ProtoUtil.byteLen(TYPE) + ProtoUtil.varLongLen(term) + ProtoUtil.varLongLen(prevLogIndex) + ProtoUtil.varLongLen(prevLogTerm) + ProtoUtil.varLongLen(leaderCommit);
        if (entryBufs != null) {
            for (Buffer buf : entryBufs) {
                length += buf.remaining();
            }
        }
        if (rawMsg == null) {
            rawMsg = new Buffer(length + ProtoUtil.varIntLen(length));
        }
        rawMsg.clear();
        rawMsg.putVarInt(length);
        rawMsg.put(AppendReq.TYPE);
        rawMsg.putVarLong(term);
        rawMsg.putVarLong(prevLogIndex);
        rawMsg.putVarLong(prevLogTerm);
        rawMsg.putVarLong(leaderCommit);
        rawMsg.flip();
        rawReady = true;
        assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
    }
}
Also used : Buffer(tezc.base.common.Buffer)

Example 24 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class AppendResp method encode.

/**
 * Encode message
 */
@Override
public void encode() {
    if (!rawReady) {
        length = ProtoUtil.byteLen(TYPE) + ProtoUtil.varLongLen(term) + ProtoUtil.varLongLen(index) + ProtoUtil.booleanLen(success);
        if (rawMsg == null) {
            rawMsg = new Buffer(length + ProtoUtil.varIntLen(length));
        }
        rawMsg.clear();
        rawMsg.putVarInt(length);
        rawMsg.put(AppendResp.TYPE);
        rawMsg.putVarLong(index);
        rawMsg.putVarLong(term);
        rawMsg.putBoolean(success);
        rawMsg.flip();
        rawReady = true;
        assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
    }
}
Also used : Buffer(tezc.base.common.Buffer)

Example 25 with Buffer

use of tezc.base.common.Buffer in project tezcRaft by tezc.

the class ClientReq method encode.

/**
 * Encode message
 */
@Override
public void encode() {
    if (!rawReady) {
        int headerLen = ProtoUtil.byteLen(TYPE) + ProtoUtil.booleanLen(readOnly) + ProtoUtil.varIntLen(stateId) + ProtoUtil.varLongLen(sequence) + ProtoUtil.booleanLen(config);
        length = headerLen + data.remaining();
        headerLen += ProtoUtil.varIntLen(length);
        if (rawMsg == null) {
            rawMsg = new Buffer(headerLen);
        }
        rawMsg.clear();
        rawMsg.putVarInt(length);
        rawMsg.put(ClientReq.TYPE);
        rawMsg.putBoolean(readOnly);
        rawMsg.putVarLong(stateId);
        rawMsg.putVarLong(sequence);
        rawMsg.putBoolean(config);
        rawMsg.flip();
        rawReady = true;
        assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
    }
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Buffer (tezc.base.common.Buffer)28 ByteBuffer (java.nio.ByteBuffer)16 FileChannel (java.nio.channels.FileChannel)2 Path (java.nio.file.Path)2 StandardOpenOption (java.nio.file.StandardOpenOption)2 Session (tezc.core.cluster.Session)2 ClusterRecord (tezc.core.record.ClusterRecord)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 State (tezc.State)1