Search in sources :

Example 1 with LogRecord

use of org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord in project incubator-ratis by apache.

the class TestSegmentedRaftLogCache method checkCache.

private void checkCache(long start, long end, int segmentSize) {
    Assert.assertEquals(start, cache.getStartIndex());
    Assert.assertEquals(end, cache.getEndIndex());
    for (long index = start; index <= end; index++) {
        final LogSegment segment = cache.getSegment(index);
        final LogRecord record = segment.getLogRecord(index);
        final LogEntryProto entry = segment.getEntryFromCache(record.getTermIndex());
        Assert.assertEquals(index, entry.getIndex());
    }
    long[] offsets = new long[] { start, start + 1, start + (end - start) / 2, end - 1, end };
    for (long offset : offsets) {
        checkCacheEntries(offset, (int) (end - offset + 1), end);
        checkCacheEntries(offset, 1, end);
        checkCacheEntries(offset, 20, end);
        checkCacheEntries(offset, segmentSize, end);
        checkCacheEntries(offset, segmentSize - 1, end);
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogRecord(org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord)

Example 2 with LogRecord

use of org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord 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);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogRecord(org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)2 LogRecord (org.apache.ratis.server.raftlog.segmented.LogSegment.LogRecord)2 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)1