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