use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class MemoryRaftLog method getEntries.
@Override
public TermIndex[] getEntries(long startIndex, long endIndex) {
checkLogState();
try (AutoCloseableLock readLock = readLock()) {
final int from = (int) startIndex;
if (startIndex >= entries.size()) {
return null;
}
final int to = (int) Math.min(entries.size(), endIndex);
TermIndex[] ti = new TermIndex[to - from];
for (int i = 0; i < ti.length; i++) {
ti[i] = TermIndex.newTermIndex(entries.get(i).getTerm(), entries.get(i).getIndex());
}
return ti;
}
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class RaftLog method updateLastCommitted.
/**
* Update the last committed index.
* @param majorityIndex the index that has achieved majority.
* @param currentTerm the current term.
* @return true if update is applied; otherwise, return false, i.e. no update required.
*/
public boolean updateLastCommitted(long majorityIndex, long currentTerm) {
try (AutoCloseableLock writeLock = writeLock()) {
if (lastCommitted.get() < majorityIndex) {
// Only update last committed index for current term. See ยง5.4.2 in
// paper for details.
final TermIndex entry = getTermIndex(majorityIndex);
if (entry != null && entry.getTerm() == currentTerm) {
LOG.debug("{}: Updating lastCommitted to {}", selfId, majorityIndex);
lastCommitted.set(majorityIndex);
return true;
}
}
}
return false;
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class SegmentedRaftLog method get.
@Override
public LogEntryProto get(long index) throws RaftLogIOException {
checkLogState();
LogSegment segment;
LogRecordWithEntry recordAndEntry;
try (AutoCloseableLock readLock = readLock()) {
segment = cache.getSegment(index);
if (segment == null) {
return null;
}
recordAndEntry = segment.getEntryWithoutLoading(index);
if (recordAndEntry == null) {
return null;
}
if (recordAndEntry.hasEntry()) {
return recordAndEntry.getEntry();
}
}
// the entry is not in the segment's cache. Load the cache without holding
// RaftLog's lock.
checkAndEvictCache();
return segment.loadCache(recordAndEntry.getRecord());
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class MemoryRaftLog method appendImpl.
@Override
public List<CompletableFuture<Long>> appendImpl(LogEntryProto... logEntryProtos) {
checkLogState();
if (logEntryProtos == null || logEntryProtos.length == 0) {
return Collections.emptyList();
}
try (AutoCloseableLock writeLock = writeLock()) {
// Before truncating the entries, we first need to check if some
// entries are duplicated. If the leader sends entry 6, entry 7, then
// entry 6 again, without this check the follower may truncate entry 7
// when receiving entry 6 again. Then before the leader detects this
// truncation in the next appendEntries RPC, leader may think entry 7 has
// been committed but in the system the entry has not been committed to
// the quorum of peers' disks.
boolean toTruncate = false;
int truncateIndex = (int) logEntryProtos[0].getIndex();
int index = 0;
for (; truncateIndex < getNextIndex() && index < logEntryProtos.length; index++, truncateIndex++) {
if (this.entries.get(truncateIndex).getTerm() != logEntryProtos[index].getTerm()) {
toTruncate = true;
break;
}
}
final List<CompletableFuture<Long>> futures;
if (toTruncate) {
futures = new ArrayList<>(logEntryProtos.length - index + 1);
futures.add(truncate(truncateIndex));
} else {
futures = new ArrayList<>(logEntryProtos.length - index);
}
for (int i = index; i < logEntryProtos.length; i++) {
this.entries.add(logEntryProtos[i]);
futures.add(CompletableFuture.completedFuture(logEntryProtos[i].getIndex()));
}
return futures;
}
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class MemoryRaftLog method appendEntryImpl.
@Override
protected CompletableFuture<Long> appendEntryImpl(LogEntryProto entry) {
checkLogState();
try (AutoCloseableLock writeLock = writeLock()) {
validateLogEntry(entry);
entries.add(entry);
}
return CompletableFuture.completedFuture(entry.getIndex());
}
Aggregations