Search in sources :

Example 1 with Buffer

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

the class MapState method onQuery.

@Override
public ByteBuffer onQuery(ByteBuffer buf) {
    Buffer buffer = new Buffer(buf);
    String s1 = buffer.getString();
    String s2 = buffer.getString();
    map.put(s1, s2);
    return null;
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Buffer

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

the class MapState method onCommand.

@Override
public void onCommand(ByteBuffer buf) {
    // System.out.println("command received for " + buf.remaining());
    Buffer buffer = new Buffer(buf);
    /*String s1 = buffer.getString();
        String s2 = buffer.getString();
        map.put(s1, s2);*/
    count++;
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer)

Example 3 with Buffer

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

the class Client method decode.

/**
 * Decode message from raw buffer
 * @param buf buffer holding raw message
 * @return    decoded message as Msg object
 */
public Msg decode(ByteBuffer buf) {
    if (raw == null) {
        header.put(buf);
        if (header.remaining() != 0) {
            return null;
        }
        header.flip();
        raw = new Buffer(header.getVarInt() + header.position());
        header.rewind();
        raw.put(header);
        header.clear();
    }
    raw.put(buf);
    if (raw.remaining() == 0) {
        raw.flip();
        Buffer msg = raw;
        raw = null;
        return Msg.create(msg);
    }
    return null;
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(tezc.base.common.Buffer)

Example 4 with Buffer

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

the class Cluster method readMeta.

/**
 * Read metadata of the cluster
 * @return             true if config is found
 * @throws IOException on any IO error
 */
private boolean readMeta() throws IOException {
    Path configPath = Paths.get(this.path + "/" + "cluster" + ".conf");
    Path configTmpPath = Paths.get(this.path + "/" + "cluster" + ".conf.tmp");
    Path path = null;
    if (Files.exists(configPath)) {
        path = configPath;
    } else if (Files.exists(configTmpPath)) {
        path = configTmpPath;
    } else {
        worker.logInfo("Cannot find a config file");
        return false;
    }
    Set<StandardOpenOption> options = EnumSet.of(StandardOpenOption.READ);
    FileChannel channel = FileChannel.open(path, options);
    if (channel.size() > Integer.MAX_VALUE) {
        channel.close();
        worker.logInfo("Config file size " + "is beyond the limit : " + channel.size());
        return false;
    }
    if (channel.size() > configBuf.cap()) {
        configBuf = new Buffer(ByteBuffer.allocateDirect((int) channel.size()));
    }
    int read = 0;
    configBuf.clear();
    while (configBuf.hasRemaining() && read != -1) {
        read = channel.read(configBuf.backend());
    }
    configBuf.flip();
    nodeId = configBuf.getInt();
    started = configBuf.getBoolean();
    currentTerm = configBuf.getLong();
    votedFor = configBuf.getInt();
    clusterRecord = new ClusterRecord(configBuf);
    channel.close();
    return true;
}
Also used : Path(java.nio.file.Path) ByteBuffer(java.nio.ByteBuffer) Buffer(tezc.base.common.Buffer) ClusterRecord(tezc.core.record.ClusterRecord) StandardOpenOption(java.nio.file.StandardOpenOption) FileChannel(java.nio.channels.FileChannel)

Example 5 with Buffer

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

the class SnapshotReader method readSnapshot.

/**
 * Read snapshot file
 *
 * @throws IOException on any IO error
 */
public void readSnapshot() throws IOException {
    if (Files.deleteIfExists(tmpPath)) {
        worker.logInfo("Found tmp snapshot and deleted it : ", tmpPath);
    }
    if (!Files.exists(path)) {
        return;
    }
    channel = FileChannel.open(path, EnumSet.of(StandardOpenOption.READ));
    channel.lock();
    term = -1;
    index = -1;
    while (true) {
        long pos = channel.position();
        buf.clear();
        buf.limit(Long.BYTES + Integer.BYTES);
        int read = 0;
        while (buf.hasRemaining() && read != -1) {
            read = channel.read(buf.backend());
        }
        buf.flip();
        if (buf.remaining() == 0) {
            break;
        }
        if (buf.remaining() != Long.BYTES + Integer.BYTES) {
            worker.logWarn("Snapshot is corrupt, will be deleted : ", path);
            channel.close();
            Files.delete(path);
            break;
        }
        long totalLen = buf.getLong();
        int metaLen = buf.getInt();
        if (metaLen > buf.cap()) {
            buf = new Buffer(ByteBuffer.allocateDirect(metaLen));
        }
        read = 0;
        buf.clear();
        buf.limit(metaLen);
        while (buf.hasRemaining() && read != -1) {
            read = channel.read(buf.backend());
        }
        buf.flip();
        State state = states.get(buf.getInt());
        if (state != null) {
            long currentTerm = buf.getLong();
            long currentIndex = buf.getLong();
            state.setTerm(currentTerm);
            state.setLastApplied(currentIndex);
            term = Math.max(term, currentTerm);
            index = Math.min(index, currentIndex);
            int count = buf.getInt();
            for (int i = 0; i < count; i++) {
                state.putLatestRequest(buf.getVarLong(), buf.getVarLong());
            }
            state.loadState(this);
        }
        channel.position(pos + totalLen);
    }
    snapshotReady = true;
}
Also used : Buffer(tezc.base.common.Buffer) ByteBuffer(java.nio.ByteBuffer) State(tezc.State)

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