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());
}
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);
}
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);
}
}
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);
}
}
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);
}
}
Aggregations