@Override public TermIndex getTermIndex(long index) { checkLogState(); try(AutoCloseableLock readLock = readLock()) { LogRecord record = cache.getLogRecord(index); return record != null ? record.getTermIndex() : null; } }
void clear() { if (openSegment != null) { openSegment.clear(); clearOpenSegment(); } closedSegments.clear(); } }
@Override protected void openImpl(long lastIndexInSnapshot, Consumer<LogEntryProto> consumer) throws IOException { loadLogSegments(lastIndexInSnapshot, consumer); File openSegmentFile = null; LogSegment openSegment = cache.getOpenSegment(); if (openSegment != null) { openSegmentFile = storage.getStorageDir() .getOpenLogFile(openSegment.getStartIndex()); } fileLogWorker.start(Math.max(cache.getEndIndex(), lastIndexInSnapshot), openSegmentFile); }
private SegmentFileInfo deleteOpenSegment() { final long oldEnd = openSegment.getEndIndex(); openSegment.clear(); SegmentFileInfo info = new SegmentFileInfo(openSegment.getStartIndex(), oldEnd, true, 0, openSegment.getEndIndex()); clearOpenSegment(); return info; }
private MetaFile writeMetaFile(long term, String votedFor) throws IOException { MetaFile metaFile = new MetaFile(storageDir.getMetaFile()); metaFile.set(term, votedFor); return metaFile; }
private File getSegmentFile() { return isOpen ? storage.getStorageDir().getOpenLogFile(startIndex) : storage.getStorageDir().getClosedLogFile(startIndex, endIndex); }
void rollLogSegment(LogSegment segmentToClose) { LOG.info("Rolling segment:{} index to:{}", name, (segmentToClose.getEndIndex() + 1)); addIOTask(new FinalizeLogSegment(segmentToClose)); addIOTask(new StartLogSegment(segmentToClose.getEndIndex() + 1)); }
void addSegment(LogSegment segment) { validateAdding(segment); if (segment.isOpen()) { setOpenSegment(segment); } else { closedSegments.add(segment); } }
long getStartIndex() { if (closedSegments.isEmpty()) { return openSegment != null ? openSegment.getStartIndex() : RaftServerConstants.INVALID_LOG_INDEX; } else { return closedSegments.get(0).getStartIndex(); } }
void evictCache(long[] followerIndices, long flushedIndex, long lastAppliedIndex) { List<LogSegment> toEvict = evictionPolicy.evict(followerIndices, flushedIndex, lastAppliedIndex, closedSegments, maxCachedSegments); for (LogSegment s : toEvict) { s.evictCache(); } }
@Override public TermIndex getLastEntryTermIndex() { checkLogState(); try(AutoCloseableLock readLock = readLock()) { return entries.getTermIndex(entries.size() - 1); } }
private void preallocateIfNecessary(int size) throws IOException { if (out.position() + size > preallocatedPos) { preallocate(); } }
@Override public String toString() { return super.toString() + ": " + segmentToClose.toDebugString(); } }
/** * The following several methods (startLogSegment, rollLogSegment, * writeLogEntry, and truncate) are only called by SegmentedRaftLog which is * protected by RaftServer's lock. * * Thus all the tasks are created and added sequentially. */ void startLogSegment(long startIndex) { addIOTask(new StartLogSegment(startIndex)); }
@Override public TermIndex getTermIndex(long index) { checkLogState(); try(AutoCloseableLock readLock = readLock()) { LogRecord record = cache.getLogRecord(index); return record != null ? record.getTermIndex() : null; } }
void rollLogSegment(LogSegment segmentToClose) { LOG.info("{}: Rolling segment {} to index:{}", name, segmentToClose.toString(), segmentToClose.getEndIndex()); addIOTask(new FinalizeLogSegment(segmentToClose)); addIOTask(new StartLogSegment(segmentToClose.getEndIndex() + 1)); }
void evictCache(long[] followerIndices, long flushedIndex, long lastAppliedIndex) { List<LogSegment> toEvict = evictionPolicy.evict(followerIndices, flushedIndex, lastAppliedIndex, closedSegments, maxCachedSegments); for (LogSegment s : toEvict) { s.evictCache(); } }
private void preallocateIfNecessary(int size) throws IOException { if (out.position() + size > preallocatedPos) { preallocate(); } }