Search in sources :

Example 6 with AutoCloseableLock

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

the class RaftLogBase method updateSnapshotIndex.

@Override
public void updateSnapshotIndex(long newSnapshotIndex) {
    try (AutoCloseableLock writeLock = writeLock()) {
        final long oldSnapshotIndex = getSnapshotIndex();
        if (oldSnapshotIndex < newSnapshotIndex) {
            snapshotIndex.updateIncreasingly(newSnapshotIndex, infoIndexChange);
        }
        final long oldCommitIndex = getLastCommittedIndex();
        if (oldCommitIndex < newSnapshotIndex) {
            commitIndex.updateIncreasingly(newSnapshotIndex, traceIndexChange);
        }
    }
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 7 with AutoCloseableLock

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

the class SegmentedRaftLogCache method computeTruncateIndices.

TruncateIndices computeTruncateIndices(Consumer<TermIndex> failClientRequest, LogEntryProto... entries) {
    int arrayIndex = 0;
    long truncateIndex = -1;
    try (AutoCloseableLock readLock = closedSegments.readLock()) {
        final Iterator<TermIndex> i = iterator(entries[0].getIndex());
        for (; i.hasNext() && arrayIndex < entries.length; arrayIndex++) {
            final TermIndex storedEntry = i.next();
            Preconditions.assertTrue(storedEntry.getIndex() == entries[arrayIndex].getIndex(), "The stored entry's index %s is not consistent with the received entries[%s]'s index %s", storedEntry.getIndex(), arrayIndex, entries[arrayIndex].getIndex());
            if (storedEntry.getTerm() != entries[arrayIndex].getTerm()) {
                // we should truncate from the storedEntry's arrayIndex
                truncateIndex = storedEntry.getIndex();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("{}: truncate to {}, arrayIndex={}, ti={}, storedEntry={}, entries={}", name, truncateIndex, arrayIndex, TermIndex.valueOf(entries[arrayIndex]), storedEntry, LogProtoUtils.toLogEntriesString(entries));
                }
                // fail all requests starting at truncateIndex
                failClientRequest.accept(storedEntry);
                for (; i.hasNext(); ) {
                    failClientRequest.accept(i.next());
                }
                break;
            }
        }
    }
    return new TruncateIndices(arrayIndex, truncateIndex);
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 8 with AutoCloseableLock

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

the class ArithmeticStateMachine method applyTransaction.

@Override
public CompletableFuture<Message> applyTransaction(TransactionContext trx) {
    final LogEntryProto entry = trx.getLogEntry();
    final AssignmentMessage assignment = new AssignmentMessage(entry.getStateMachineLogEntry().getLogData());
    final long index = entry.getIndex();
    final Double result;
    try (AutoCloseableLock writeLock = writeLock()) {
        result = assignment.evaluate(variables);
        updateLastAppliedTermIndex(entry.getTerm(), index);
    }
    final Expression r = Expression.Utils.double2Expression(result);
    final CompletableFuture<Message> f = CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
    final RaftPeerRole role = trx.getServerRole();
    if (role == RaftPeerRole.LEADER) {
        LOG.info("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
    } else {
        LOG.debug("{}:{}-{}: {} = {}", role, getId(), index, assignment, r);
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("{}-{}: variables={}", getId(), index, variables);
    }
    return f;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Message(org.apache.ratis.protocol.Message) Expression(org.apache.ratis.examples.arithmetic.expression.Expression) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) RaftPeerRole(org.apache.ratis.proto.RaftProtos.RaftPeerRole)

Example 9 with AutoCloseableLock

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

the class ArithmeticStateMachine method query.

@Override
public CompletableFuture<Message> query(Message request) {
    final Expression q = Expression.Utils.bytes2Expression(request.getContent().toByteArray(), 0);
    final Double result;
    try (AutoCloseableLock readLock = readLock()) {
        result = q.evaluate(variables);
    }
    final Expression r = Expression.Utils.double2Expression(result);
    LOG.debug("QUERY: {} = {}", q, r);
    return CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
}
Also used : Expression(org.apache.ratis.examples.arithmetic.expression.Expression) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 10 with AutoCloseableLock

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

the class ArithmeticStateMachine method takeSnapshot.

@Override
public long takeSnapshot() {
    final Map<String, Double> copy;
    final TermIndex last;
    try (AutoCloseableLock readLock = readLock()) {
        copy = new HashMap<>(variables);
        last = getLastAppliedTermIndex();
    }
    final File snapshotFile = storage.getSnapshotFile(last.getTerm(), last.getIndex());
    LOG.info("Taking a snapshot to file {}", snapshotFile);
    try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(snapshotFile)))) {
        out.writeObject(copy);
    } catch (IOException ioe) {
        LOG.warn("Failed to write snapshot file \"" + snapshotFile + "\", last applied index=" + last);
    }
    return last.getIndex();
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) 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