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());
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class MemoryRaftLog method truncateImpl.
@Override
protected CompletableFuture<Long> truncateImpl(long index) {
checkLogState();
try (AutoCloseableLock writeLock = writeLock()) {
Preconditions.assertTrue(index >= 0);
entries.truncate(Math.toIntExact(index));
}
return CompletableFuture.completedFuture(index);
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class MemoryRaftLog method getEntries.
@Override
public LogEntryHeader[] getEntries(long startIndex, long endIndex) {
checkLogState();
try (AutoCloseableLock readLock = readLock()) {
if (startIndex >= entries.size()) {
return null;
}
final int from = Math.toIntExact(startIndex);
final int to = Math.toIntExact(Math.min(entries.size(), endIndex));
final LogEntryHeader[] headers = new LogEntryHeader[to - from];
for (int i = 0; i < headers.length; i++) {
headers[i] = entries.getLogEntryHeader(i);
}
return headers;
}
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class MemoryRaftLog method append.
@Override
public List<CompletableFuture<Long>> append(LogEntryProto... entries) {
checkLogState();
try (AutoCloseableLock writeLock = writeLock()) {
if (entries == null || entries.length == 0) {
return Collections.emptyList();
}
// 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.
// TODO add a unit test for this
boolean toTruncate = false;
int truncateIndex = (int) entries[0].getIndex();
int index = 0;
for (; truncateIndex < getNextIndex() && index < entries.length; index++, truncateIndex++) {
if (this.entries.get(truncateIndex).getTerm() != entries[index].getTerm()) {
toTruncate = true;
break;
}
}
final List<CompletableFuture<Long>> futures;
if (toTruncate) {
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++) {
this.entries.add(entries[i]);
futures.add(CompletableFuture.completedFuture(entries[i].getIndex()));
}
return futures;
}
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class MemoryRaftLog method truncate.
@Override
CompletableFuture<Long> truncate(long index) {
checkLogState();
try (AutoCloseableLock writeLock = writeLock()) {
Preconditions.assertTrue(index >= 0);
final int truncateIndex = (int) index;
for (int i = entries.size() - 1; i >= truncateIndex; i--) {
entries.remove(i);
}
}
return CompletableFuture.completedFuture(index);
}
Aggregations