use of tezc.base.common.Buffer in project tezcRaft by tezc.
the class ReqVoteResp method encode.
/**
* Encode message
*/
@Override
public void encode() {
if (!rawReady) {
length = ProtoUtil.byteLen(ReqVoteResp.TYPE) + ProtoUtil.longLen(term) + ProtoUtil.booleanLen(voteGranted);
if (rawMsg == null) {
rawMsg = new Buffer(length + ProtoUtil.varIntLen(length));
}
rawMsg.clear();
rawMsg.putVarInt(length);
rawMsg.put(ReqVoteResp.TYPE);
rawMsg.putLong(term);
rawMsg.putBoolean(voteGranted);
rawMsg.flip();
rawReady = true;
assert (rawMsg.remaining() >= Msg.MIN_MSG_SIZE);
}
}
use of tezc.base.common.Buffer in project tezcRaft by tezc.
the class ListState method onQuery.
@Override
public ByteBuffer onQuery(ByteBuffer buf) {
Buffer buffer = new Buffer(buf);
list.add(buffer.getString());
return null;
}
use of tezc.base.common.Buffer in project tezcRaft by tezc.
the class ListState method onCommand.
@Override
public void onCommand(ByteBuffer buf) {
Buffer buffer = new Buffer(buf);
list.add(buffer.getString());
}
use of tezc.base.common.Buffer in project tezcRaft by tezc.
the class Cluster method writeMeta.
/**
* Write metadata of the cluster to config file.
*/
private void writeMeta() {
Path configPath = Paths.get(this.path + "/" + "cluster" + ".conf");
Path configTmpPath = Paths.get(this.path + "/" + "cluster" + ".conf.tmp");
configBuf.clear();
int len = ProtoUtil.intLen(nodeId) + ProtoUtil.booleanLen(started) + ProtoUtil.longLen(currentTerm) + ProtoUtil.intLen(votedFor) + clusterRecord.encodedLen();
if (len > configBuf.cap()) {
configBuf = new Buffer(ByteBuffer.allocateDirect(len));
}
configBuf.putInt(nodeId);
configBuf.putBoolean(started);
configBuf.putLong(currentTerm);
configBuf.putInt(votedFor);
clusterRecord.encode(configBuf);
configBuf.flip();
try {
Set<StandardOpenOption> options = EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.SYNC, StandardOpenOption.TRUNCATE_EXISTING);
FileChannel tmp = FileChannel.open(configTmpPath, options);
while (configBuf.hasRemaining()) {
tmp.write(configBuf.backend());
}
tmp.close();
Files.deleteIfExists(configPath);
Files.move(configTmpPath, configPath);
} catch (IOException e) {
// Use unchecked exceptions for code clarity
throw new UncheckedIOException(e);
}
}
use of tezc.base.common.Buffer in project tezcRaft by tezc.
the class SnapshotWriter method encodeMeta.
/**
* Encode meta data for this state
* @param state state
*/
private void encodeMeta(State state) {
int len = ProtoUtil.intLen(state.getStateId()) + ProtoUtil.longLen(state.getLastApplied()) + ProtoUtil.longLen(state.getTerm());
Map<Long, Long> latestRequests = state.getLatestRequests();
len += ProtoUtil.intLen(latestRequests.size());
for (Map.Entry<Long, Long> entry : latestRequests.entrySet()) {
len += ProtoUtil.varLongLen(entry.getKey());
len += ProtoUtil.varLongLen(entry.getValue());
}
int headerLen = ProtoUtil.intLen(len);
if (len + headerLen > buf.remaining()) {
buf = new Buffer(ByteBuffer.allocateDirect(len + headerLen));
}
buf.putInt(len);
buf.putInt(state.getStateId());
buf.putLong(state.getTerm());
buf.putLong(state.getLastApplied());
buf.putInt(latestRequests.size());
for (Map.Entry<Long, Long> entry : latestRequests.entrySet()) {
buf.putVarLong(entry.getKey());
buf.putVarLong(entry.getValue());
}
}
Aggregations