Search in sources :

Example 1 with RaftStorage

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);
        }
    }
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) Message(org.apache.ratis.protocol.Message) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with RaftStorage

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;
        }
    }
}
Also used : SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) RaftStorage(org.apache.ratis.server.storage.RaftStorage) SimpleStateMachineStorage(org.apache.ratis.statemachine.impl.SimpleStateMachineStorage) RaftStorageImpl(org.apache.ratis.server.storage.RaftStorageImpl) DataInputStream(java.io.DataInputStream) CheckpointInputStream(alluxio.master.journal.checkpoint.CheckpointInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with RaftStorage

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;
}
Also used : RaftStorage(org.apache.ratis.server.storage.RaftStorage) SimpleStateMachineStorage(org.apache.ratis.statemachine.impl.SimpleStateMachineStorage) RaftStorageImpl(org.apache.ratis.server.storage.RaftStorageImpl)

Example 4 with RaftStorage

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);
    }
}
Also used : PrintStream(java.io.PrintStream) RaftStorage(org.apache.ratis.server.storage.RaftStorage) FileOutputStream(java.io.FileOutputStream) LogSegmentPath(org.apache.ratis.server.raftlog.segmented.LogSegmentPath) Journal(alluxio.proto.journal.Journal) RaftStorageImpl(org.apache.ratis.server.storage.RaftStorageImpl) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 5 with RaftStorage

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();
    }
}
Also used : SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) RaftStorage(org.apache.ratis.server.storage.RaftStorage) SimpleStateMachineStorage(org.apache.ratis.statemachine.impl.SimpleStateMachineStorage) RaftStorageImpl(org.apache.ratis.server.storage.RaftStorageImpl) IOException(java.io.IOException) File(java.io.File)

Aggregations

RaftStorage (org.apache.ratis.server.storage.RaftStorage)5 RaftStorageImpl (org.apache.ratis.server.storage.RaftStorageImpl)4 IOException (java.io.IOException)3 SimpleStateMachineStorage (org.apache.ratis.statemachine.impl.SimpleStateMachineStorage)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 SingleFileSnapshotInfo (org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo)2 CheckpointInputStream (alluxio.master.journal.checkpoint.CheckpointInputStream)1 Journal (alluxio.proto.journal.Journal)1 BufferedOutputStream (java.io.BufferedOutputStream)1 DataInputStream (java.io.DataInputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Message (org.apache.ratis.protocol.Message)1 LogSegmentPath (org.apache.ratis.server.raftlog.segmented.LogSegmentPath)1 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)1 SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)1