Search in sources :

Example 1 with LogOutputStream

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;
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) MD5Hash(org.apache.ratis.io.MD5Hash) IOException(java.io.IOException) File(java.io.File) LogOutputStream(org.apache.ratis.server.storage.LogOutputStream) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 MD5Hash (org.apache.ratis.io.MD5Hash)1 TermIndex (org.apache.ratis.server.protocol.TermIndex)1 LogOutputStream (org.apache.ratis.server.storage.LogOutputStream)1 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)1 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)1