use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class RaftLogBase method appendMetadataImpl.
private long appendMetadataImpl(long term, long newCommitIndex) {
checkLogState();
if (!shouldAppendMetadata(newCommitIndex)) {
return INVALID_LOG_INDEX;
}
final LogEntryProto entry;
final long nextIndex;
try (AutoCloseableLock writeLock = writeLock()) {
nextIndex = getNextIndex();
entry = LogProtoUtils.toLogEntryProto(newCommitIndex, term, nextIndex);
appendEntry(entry);
}
lastMetadataEntry = entry;
return nextIndex;
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class RaftStorageTestUtils method printLog.
static void printLog(RaftLogBase 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.getFlushIndex();
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 {
b.append(LogProtoUtils.toLogEntryString(log.get(i)));
} 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 MemoryRaftLog method purgeImpl.
@Override
protected CompletableFuture<Long> purgeImpl(long index) {
try (AutoCloseableLock writeLock = writeLock()) {
Preconditions.assertTrue(index >= 0);
entries.purge(Math.toIntExact(index));
}
return CompletableFuture.completedFuture(index);
}
use of org.apache.ratis.util.AutoCloseableLock in project incubator-ratis by apache.
the class SegmentedRaftLog method close.
@Override
public void close() throws IOException {
try (AutoCloseableLock writeLock = writeLock()) {
super.close();
cache.close();
}
fileLogWorker.close();
storage.close();
getRaftLogMetrics().unregister();
}
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();
final LogSegment segment;
final LogRecord record;
try (AutoCloseableLock readLock = readLock()) {
segment = cache.getSegment(index);
if (segment == null) {
return null;
}
record = segment.getLogRecord(index);
if (record == null) {
return null;
}
final LogEntryProto entry = segment.getEntryFromCache(record.getTermIndex());
if (entry != null) {
getRaftLogMetrics().onRaftLogCacheHit();
return entry;
}
}
// the entry is not in the segment's cache. Load the cache without holding the lock.
getRaftLogMetrics().onRaftLogCacheMiss();
checkAndEvictCache();
return segment.loadCache(record);
}
Aggregations