Search in sources :

Example 16 with Buffer

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);
    }
}
Also used : Buffer(tezc.base.common.Buffer)

Example 17 with Buffer

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;
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 18 with Buffer

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());
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 19 with Buffer

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);
    }
}
Also used : Path(java.nio.file.Path) ByteBuffer(java.nio.ByteBuffer) Buffer(tezc.base.common.Buffer) StandardOpenOption(java.nio.file.StandardOpenOption) FileChannel(java.nio.channels.FileChannel) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 20 with Buffer

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