use of org.apache.ratis.server.storage.RaftStorage in project incubator-ratis by apache.
the class StateMachineUpdater method run.
@Override
public void run() {
final RaftStorage storage = server.getState().getStorage();
while (isRunning()) {
try {
synchronized (this) {
// Thus initially lastAppliedIndex can be greater than lastCommitted.
while (lastAppliedIndex >= raftLog.getLastCommittedIndex()) {
wait();
}
}
final long committedIndex = raftLog.getLastCommittedIndex();
Preconditions.assertTrue(lastAppliedIndex < committedIndex);
if (state == State.RELOAD) {
Preconditions.assertTrue(stateMachine.getLifeCycleState() == LifeCycle.State.PAUSED);
stateMachine.reinitialize(server.getId(), properties, storage);
SnapshotInfo snapshot = stateMachine.getLatestSnapshot();
Preconditions.assertTrue(snapshot != null && snapshot.getIndex() > lastAppliedIndex, "Snapshot: %s, lastAppliedIndex: %s", snapshot, lastAppliedIndex);
lastAppliedIndex = snapshot.getIndex();
lastSnapshotIndex = snapshot.getIndex();
state = State.RUNNING;
}
final MemoizedSupplier<List<CompletableFuture<Message>>> futures = MemoizedSupplier.valueOf(() -> new ArrayList<>());
while (lastAppliedIndex < committedIndex) {
final long nextIndex = lastAppliedIndex + 1;
final LogEntryProto next = raftLog.get(nextIndex);
if (next != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("{}: applying nextIndex={}, nextLog={}", this, nextIndex, ServerProtoUtils.toString(next));
}
final CompletableFuture<Message> f = server.applyLogToStateMachine(next);
if (f != null) {
futures.get().add(f);
}
lastAppliedIndex = nextIndex;
} else {
LOG.debug("{}: logEntry {} is null. There may be snapshot to load. state:{}", this, nextIndex, state);
break;
}
}
// check if need to trigger a snapshot
if (shouldTakeSnapshot(lastAppliedIndex)) {
if (futures.isInitialized()) {
JavaUtils.allOf(futures.get()).get();
}
stateMachine.takeSnapshot();
// TODO purge logs, including log cache. but should keep log for leader's RPCSenders
lastSnapshotIndex = lastAppliedIndex;
}
} catch (InterruptedException e) {
if (!isRunning()) {
LOG.info("{}: the StateMachineUpdater is interrupted and will exit.", this);
} else {
final String s = this + ": the StateMachineUpdater is wrongly interrupted";
ExitUtils.terminate(1, s, e, LOG);
}
} catch (Throwable t) {
final String s = this + ": the StateMachineUpdater hits Throwable";
ExitUtils.terminate(2, s, t, LOG);
}
}
}
use of org.apache.ratis.server.storage.RaftStorage in project alluxio by Alluxio.
the class RaftJournalDumper method readRatisSnapshotFromDir.
private void readRatisSnapshotFromDir() throws IOException {
try (RaftStorage storage = new RaftStorageImpl(getJournalDir(), RaftServerConfigKeys.Log.CorruptionPolicy.getDefault())) {
SimpleStateMachineStorage stateMachineStorage = new SimpleStateMachineStorage();
stateMachineStorage.init(storage);
SingleFileSnapshotInfo currentSnapshot = stateMachineStorage.getLatestSnapshot();
if (currentSnapshot == null) {
LOG.debug("No snapshot found");
return;
}
final File snapshotFile = currentSnapshot.getFile().getPath().toFile();
String checkpointPath = String.format("%s-%s-%s", mCheckpointsDir, currentSnapshot.getIndex(), snapshotFile.lastModified());
try (DataInputStream inputStream = new DataInputStream(new FileInputStream(snapshotFile))) {
LOG.debug("Reading snapshot-Id: {}", inputStream.readLong());
try (CheckpointInputStream checkpointStream = new CheckpointInputStream(inputStream)) {
readCheckpoint(checkpointStream, Paths.get(checkpointPath));
} catch (Exception e) {
LOG.error("Failed to read snapshot from journal.", e);
}
} catch (Exception e) {
LOG.error("Failed to load snapshot {}", snapshotFile, e);
throw e;
}
}
}
use of org.apache.ratis.server.storage.RaftStorage in project alluxio by Alluxio.
the class SnapshotReplicationManagerTest method getSimpleStateMachineStorage.
private SimpleStateMachineStorage getSimpleStateMachineStorage() throws IOException {
RaftStorage rs = new RaftStorageImpl(mFolder.newFolder(CommonUtils.randomAlphaNumString(6)), RaftServerConfigKeys.Log.CorruptionPolicy.getDefault());
SimpleStateMachineStorage snapshotStore = new SimpleStateMachineStorage();
snapshotStore.init(rs);
return snapshotStore;
}
use of org.apache.ratis.server.storage.RaftStorage in project alluxio by Alluxio.
the class RaftJournalDumper method readRatisLogFromDir.
private void readRatisLogFromDir() {
try (PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(mJournalEntryFile)));
RaftStorage storage = new RaftStorageImpl(getJournalDir(), RaftServerConfigKeys.Log.CorruptionPolicy.getDefault())) {
List<LogSegmentPath> paths = LogSegmentPath.getLogSegmentPaths(storage);
for (LogSegmentPath path : paths) {
final int entryCount = LogSegment.readSegmentFile(path.getPath().toFile(), path.getStartEnd(), RaftServerConfigKeys.Log.CorruptionPolicy.EXCEPTION, null, (proto) -> {
if (proto.hasStateMachineLogEntry()) {
try {
Journal.JournalEntry entry = Journal.JournalEntry.parseFrom(proto.getStateMachineLogEntry().getLogData().asReadOnlyByteBuffer());
writeSelected(out, entry);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
LOG.info("Read {} entries from log {}.", entryCount, path.getPath());
}
} catch (Exception e) {
LOG.error("Failed to read logs from journal.", e);
}
}
use of org.apache.ratis.server.storage.RaftStorage in project alluxio by Alluxio.
the class JournalToolTest method getCurrentRatisSnapshotIndex.
private long getCurrentRatisSnapshotIndex(String journalFolder) throws Throwable {
try (RaftStorage storage = new RaftStorageImpl(new File(RaftJournalUtils.getRaftJournalDir(new File(journalFolder)), RaftJournalSystem.RAFT_GROUP_UUID.toString()), RaftServerConfigKeys.Log.CorruptionPolicy.getDefault())) {
SimpleStateMachineStorage stateMachineStorage = new SimpleStateMachineStorage();
stateMachineStorage.init(storage);
SingleFileSnapshotInfo snapshot = stateMachineStorage.getLatestSnapshot();
if (snapshot == null) {
throw new IOException("Failed to find a valid snapshot");
}
return snapshot.getIndex();
}
}
Aggregations