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);
}
}
}
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);
}
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;
}
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));
}
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();
}
Aggregations