Search in sources :

Example 26 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class SegmentedRaftLog method append.

@Override
public List<CompletableFuture<Long>> append(LogEntryProto... entries) {
    checkLogState();
    if (entries == null || entries.length == 0) {
        return Collections.emptyList();
    }
    try (AutoCloseableLock writeLock = writeLock()) {
        Iterator<TermIndex> iter = cache.iterator(entries[0].getIndex());
        int index = 0;
        long truncateIndex = -1;
        for (; iter.hasNext() && index < entries.length; index++) {
            TermIndex storedEntry = iter.next();
            Preconditions.assertTrue(storedEntry.getIndex() == entries[index].getIndex(), "The stored entry's index %s is not consistent with" + " the received entries[%s]'s index %s", storedEntry.getIndex(), index, entries[index].getIndex());
            if (storedEntry.getTerm() != entries[index].getTerm()) {
                // we should truncate from the storedEntry's index
                truncateIndex = storedEntry.getIndex();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("{}: truncate to {}, index={}, ti={}, storedEntry={}, entries={}", server.getId(), truncateIndex, index, ServerProtoUtils.toTermIndex(entries[index]), storedEntry, ServerProtoUtils.toString(entries));
                }
                while (true) {
                    try {
                        final LogEntryProto entry = get(storedEntry.getIndex());
                        server.failClientRequest(entry);
                    } catch (RaftLogIOException e) {
                        LOG.error("Failed to read log " + storedEntry, e);
                    }
                    if (iter.hasNext()) {
                        storedEntry = iter.next();
                    } else {
                        break;
                    }
                }
                break;
            }
        }
        final List<CompletableFuture<Long>> futures;
        if (truncateIndex != -1) {
            futures = new ArrayList<>(entries.length - index + 1);
            futures.add(truncate(truncateIndex));
        } else {
            futures = new ArrayList<>(entries.length - index);
        }
        for (int i = index; i < entries.length; i++) {
            futures.add(appendEntry(entries[i]));
        }
        return futures;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 27 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class SegmentedRaftLog method appendEntry.

@Override
CompletableFuture<Long> appendEntry(LogEntryProto entry) {
    checkLogState();
    if (LOG.isTraceEnabled()) {
        LOG.trace("{}: appendEntry {}", server.getId(), ServerProtoUtils.toLogEntryString(entry));
    }
    try (AutoCloseableLock writeLock = writeLock()) {
        final LogSegment currentOpenSegment = cache.getOpenSegment();
        if (currentOpenSegment == null) {
            cache.addOpenSegment(entry.getIndex());
            fileLogWorker.startLogSegment(entry.getIndex());
        } else if (isSegmentFull(currentOpenSegment, entry)) {
            cache.rollOpenSegment(true);
            fileLogWorker.rollLogSegment(currentOpenSegment);
            checkAndEvictCache();
        } else if (currentOpenSegment.numOfEntries() > 0 && currentOpenSegment.getLastTermIndex().getTerm() != entry.getTerm()) {
            // the term changes
            final long currentTerm = currentOpenSegment.getLastTermIndex().getTerm();
            Preconditions.assertTrue(currentTerm < entry.getTerm(), "open segment's term %s is larger than the new entry's term %s", currentTerm, entry.getTerm());
            cache.rollOpenSegment(true);
            fileLogWorker.rollLogSegment(currentOpenSegment);
            checkAndEvictCache();
        }
        // If the entry has state machine data, then the entry should be inserted
        // to statemachine first and then to the cache. Not following the order
        // will leave a spurious entry in the cache.
        CompletableFuture<Long> writeFuture = fileLogWorker.writeLogEntry(entry).getFuture();
        cache.appendEntry(entry);
        return writeFuture;
    } catch (Throwable throwable) {
        LOG.error(getSelfId() + "exception while appending entry with index:" + entry.getIndex(), throwable);
        throw throwable;
    }
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 28 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class RaftStorageTestUtils method printLog.

static void printLog(RaftLog log, Consumer<String> println) {
    if (log == null) {
        println.accept("log == null");
        return;
    }
    final TermIndex last;
    final long flushed, committed;
    try (AutoCloseableLock readlock = log.readLock()) {
        last = log.getLastEntryTermIndex();
        flushed = log.getLatestFlushedIndex();
        committed = log.getLastCommittedIndex();
    }
    final StringBuilder b = new StringBuilder();
    for (long i = 0; i <= last.getIndex(); i++) {
        b.setLength(0);
        b.append(i == flushed ? 'f' : ' ');
        b.append(i == committed ? 'c' : ' ');
        b.append(String.format("%3d: ", i));
        try {
            final RaftProtos.LogEntryProto entry = log.get(i);
            b.append(entry != null ? entry.getLogEntryBodyCase() : null);
        } catch (RaftLogIOException e) {
            b.append(e);
        }
        println.accept(b.toString());
    }
}
Also used : RaftProtos(org.apache.ratis.shaded.proto.RaftProtos) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 29 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class ArithmeticStateMachine method load.

private long load(SingleFileSnapshotInfo snapshot, boolean reload) throws IOException {
    if (snapshot == null) {
        LOG.warn("The snapshot info is null.");
        return RaftLog.INVALID_LOG_INDEX;
    }
    final File snapshotFile = snapshot.getFile().getPath().toFile();
    if (!snapshotFile.exists()) {
        LOG.warn("The snapshot file {} does not exist for snapshot {}", snapshotFile, snapshot);
        return RaftLog.INVALID_LOG_INDEX;
    }
    final TermIndex last = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);
    try (AutoCloseableLock writeLock = writeLock();
        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(snapshotFile)))) {
        if (reload) {
            reset();
        }
        setLastAppliedTermIndex(last);
        variables.putAll(JavaUtils.cast(in.readObject()));
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
    return last.getIndex();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) File(java.io.File) FileInputStream(java.io.FileInputStream) TermIndex(org.apache.ratis.server.protocol.TermIndex) ObjectInputStream(java.io.ObjectInputStream)

Example 30 with AutoCloseableLock

use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.

the class RaftLogBase method updateCommitIndex.

@Override
public boolean updateCommitIndex(long majorityIndex, long currentTerm, boolean isLeader) {
    try (AutoCloseableLock writeLock = writeLock()) {
        final long oldCommittedIndex = getLastCommittedIndex();
        final long newCommitIndex = Math.min(majorityIndex, getFlushIndex());
        if (oldCommittedIndex < newCommitIndex) {
            if (!isLeader) {
                commitIndex.updateIncreasingly(newCommitIndex, traceIndexChange);
                return true;
            }
            // Only update last committed index for current term. See ยง5.4.2 in paper for details.
            final TermIndex entry = getTermIndex(newCommitIndex);
            if (entry != null && entry.getTerm() == currentTerm) {
                commitIndex.updateIncreasingly(newCommitIndex, traceIndexChange);
                return true;
            }
        }
    }
    return false;
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Aggregations

AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)35 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 IOException (java.io.IOException)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)4 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)4 Timer (com.codahale.metrics.Timer)2 File (java.io.File)2 Expression (org.apache.ratis.examples.arithmetic.expression.Expression)2 RaftLogIOException (org.apache.ratis.server.raftlog.RaftLogIOException)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 RaftPeerRole (org.apache.ratis.proto.RaftProtos.RaftPeerRole)1 Message (org.apache.ratis.protocol.Message)1 StateMachineException (org.apache.ratis.protocol.StateMachineException)1 StateMachineException (org.apache.ratis.protocol.exceptions.StateMachineException)1