Search in sources :

Example 1 with State

use of tezc.State in project tezcRaft by tezc.

the class Cluster method handleSnapshotReady.

/**
 * Snapshot ready callback
 *
 * @param success true if snapshotting executed successfully
 * @throws UncheckedIOException on any IO error
 */
public void handleSnapshotReady(boolean success) {
    try {
        if (!success) {
            throw new RaftException("Snapshot failed");
        }
        State state = snapshotWriter.getFirstPendingState();
        state.clearSnapshotInProgress();
        List<CommandRecord> pendings = state.getPendings();
        for (CommandRecord record : pendings) {
            state.commandReceived(record.index, record.term, record.clientId, record.sequence, record.buf);
        }
        snapshotWriter.popFirstPendingState();
        state = snapshotWriter.getFirstPendingState();
        if (state == null) {
            snapshotWriter.postSnapshot();
            snapshotIndex = snapshotWriter.getIndex();
        } else {
            snapshotWorker.addEvent(new TakeSnapshotEvent(worker, this, snapshotWriter));
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : RaftException(tezc.base.exception.RaftException) RaftState(tezc.core.cluster.state.RaftState) State(tezc.State) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TakeSnapshotEvent(tezc.core.worker.clusterworker.TakeSnapshotEvent)

Example 2 with State

use of tezc.State 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)

Example 3 with State

use of tezc.State in project tezcRaft by tezc.

the class Cluster method incrementCommit.

/**
 * Increment commit, apply logs to state machines
 * @param index commit index
 */
private void incrementCommit(long index) {
    if (commit >= index) {
        return;
    }
    for (long i = commit + 1; i <= index; i++) {
        EntryRecord entry = getEntry(i);
        State state = states.get(entry.getStateId());
        if (state != null) {
            state.commandReceived(i, entry.getClientId(), entry.getTerm(), entry.getSequence(), getEntryBuffer(i).backend());
        }
    }
    commit = index;
    // TODO: snapshot might be handled in store creation
    checkSnapshot();
}
Also used : RaftState(tezc.core.cluster.state.RaftState) State(tezc.State)

Aggregations

State (tezc.State)3 RaftState (tezc.core.cluster.state.RaftState)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ByteBuffer (java.nio.ByteBuffer)1 Buffer (tezc.base.common.Buffer)1 RaftException (tezc.base.exception.RaftException)1 TakeSnapshotEvent (tezc.core.worker.clusterworker.TakeSnapshotEvent)1