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