use of org.apache.ratis.server.storage.LogOutputStream in project incubator-ratis by apache.
the class SimpleStateMachine4Testing method takeSnapshot.
@Override
public long takeSnapshot() {
final TermIndex termIndex = getLastAppliedTermIndex();
if (termIndex.getTerm() <= 0 || termIndex.getIndex() <= 0) {
return RaftServerConstants.INVALID_LOG_INDEX;
}
final long endIndex = termIndex.getIndex();
// TODO: snapshot should be written to a tmp file, then renamed
File snapshotFile = storage.getSnapshotFile(termIndex.getTerm(), termIndex.getIndex());
LOG.debug("Taking a snapshot with t:{}, i:{}, file:{}", termIndex.getTerm(), termIndex.getIndex(), snapshotFile);
try (LogOutputStream out = new LogOutputStream(snapshotFile, false, segmentMaxSize, preallocatedSize, bufferSize)) {
for (final LogEntryProto entry : list) {
if (entry.getIndex() > endIndex) {
break;
} else {
out.write(entry);
}
}
out.flush();
} catch (IOException e) {
LOG.warn("Failed to take snapshot", e);
}
try {
final MD5Hash digest = MD5FileUtil.computeMd5ForFile(snapshotFile);
MD5FileUtil.saveMD5File(snapshotFile, digest);
} catch (IOException e) {
LOG.warn("Hit IOException when computing MD5 for snapshot file " + snapshotFile, e);
}
try {
this.storage.loadLatestSnapshot();
} catch (IOException e) {
LOG.warn("Hit IOException when loading latest snapshot for snapshot file " + snapshotFile, e);
}
// TODO: purge log segments
return endIndex;
}
Aggregations