@Override public LogEntryProto load(LogRecord key) throws IOException { final File file = getSegmentFile(); // note the loading should not exceed the endIndex: it is possible that // the on-disk log file should be truncated but has not been done yet. readSegmentFile(file, startIndex, endIndex, isOpen, entry -> entryCache.put(ServerProtoUtils.toTermIndex(entry), entry)); loadingTimes.incrementAndGet(); return Objects.requireNonNull(entryCache.get(key.getTermIndex())); } }
@Override public LogEntryProto load(LogRecord key) throws IOException { final File file = getSegmentFile(); // note the loading should not exceed the endIndex: it is possible that // the on-disk log file should be truncated but has not been done yet. readSegmentFile(file, startIndex, endIndex, isOpen, entry -> entryCache.put(ServerProtoUtils.toTermIndex(entry), entry)); loadingTimes.incrementAndGet(); return Objects.requireNonNull(entryCache.get(key.getTermIndex())); } }
TermIndex getTermIndex(int i) { return ServerProtoUtils.toTermIndex(get(i)); }
@Override public TermIndex getTermIndex(long index) { checkLogState(); try(AutoCloseableLock readLock = readLock()) { final int i = (int) index; return i >= 0 && i < entries.size() ? ServerProtoUtils.toTermIndex(entries.get(i)) : null; } }
@Override public TermIndex getLastEntryTermIndex() { checkLogState(); try(AutoCloseableLock readLock = readLock()) { final int size = entries.size(); return size == 0 ? null : ServerProtoUtils.toTermIndex(entries.get(size - 1)); } }
TruncateIndices computeTruncateIndices(Consumer<TermIndex> failClientRequest, LogEntryProto... entries) { int arrayIndex = 0; long truncateIndex = -1; try(AutoCloseableLock readLock = closedSegments.readLock()) { final Iterator<TermIndex> i = iterator(entries[0].getIndex()); for(; i.hasNext() && arrayIndex < entries.length; arrayIndex++) { final TermIndex storedEntry = i.next(); Preconditions.assertTrue(storedEntry.getIndex() == entries[arrayIndex].getIndex(), "The stored entry's index %s is not consistent with the received entries[%s]'s index %s", storedEntry.getIndex(), arrayIndex, entries[arrayIndex].getIndex()); if (storedEntry.getTerm() != entries[arrayIndex].getTerm()) { // we should truncate from the storedEntry's arrayIndex truncateIndex = storedEntry.getIndex(); if (LOG.isTraceEnabled()) { LOG.trace("{}: truncate to {}, arrayIndex={}, ti={}, storedEntry={}, entries={}", name, truncateIndex, arrayIndex, ServerProtoUtils.toTermIndex(entries[arrayIndex]), storedEntry, ServerProtoUtils.toString(entries)); } // fail all requests starting at truncateIndex failClientRequest.accept(storedEntry); for(; i.hasNext(); ) { failClientRequest.accept(i.next()); } break; } } } return new TruncateIndices(arrayIndex, truncateIndex); }
LOG.trace("{}: truncate to {}, index={}, ti={}, storedEntry={}, entries={}", server.getId(), truncateIndex, index, ServerProtoUtils.toTermIndex(entries[index]), storedEntry, ServerProtoUtils.toString(entries));
void installSnapshot(InstallSnapshotRequestProto request) throws IOException { // TODO: verify that we need to install the snapshot StateMachine sm = server.getStateMachine(); sm.pause(); // pause the SM to prepare for install snapshot snapshotManager.installSnapshot(sm, request); log.syncWithSnapshot(request.getTermIndex().getIndex()); this.latestInstalledSnapshot = ServerProtoUtils.toTermIndex( request.getTermIndex()); }
void installSnapshot(InstallSnapshotRequestProto request) throws IOException { // TODO: verify that we need to install the snapshot StateMachine sm = server.getStateMachine(); sm.pause(); // pause the SM to prepare for install snapshot snapshotManager.installSnapshot(sm, request); log.syncWithSnapshot(request.getTermIndex().getIndex()); this.latestInstalledSnapshot = ServerProtoUtils.toTermIndex( request.getTermIndex()); }
@Override public RequestVoteReplyProto requestVote(RequestVoteRequestProto r) throws IOException { final RaftRpcRequestProto request = r.getServerRequest(); return requestVote(RaftPeerId.valueOf(request.getRequestorId()), ProtoUtils.toRaftGroupId(request.getRaftGroupId()), r.getCandidateTerm(), ServerProtoUtils.toTermIndex(r.getCandidateLastEntry())); }
@Override public RequestVoteReplyProto requestVote(RequestVoteRequestProto r) throws IOException { final RaftRpcRequestProto request = r.getServerRequest(); return requestVote(RaftPeerId.valueOf(request.getRequestorId()), ProtoUtils.toRaftGroupId(request.getRaftGroupId()), r.getCandidateTerm(), ServerProtoUtils.toTermIndex(r.getCandidateLastEntry())); }
@Override public CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto r) throws IOException { // TODO avoid converting list to array final RaftRpcRequestProto request = r.getServerRequest(); final LogEntryProto[] entries = r.getEntriesList() .toArray(new LogEntryProto[r.getEntriesCount()]); final TermIndex previous = r.hasPreviousLog() ? ServerProtoUtils.toTermIndex(r.getPreviousLog()) : null; return appendEntriesAsync(RaftPeerId.valueOf(request.getRequestorId()), ProtoUtils.toRaftGroupId(request.getRaftGroupId()), r.getLeaderTerm(), previous, r.getLeaderCommit(), request.getCallId(), r.getInitializing(), r.getCommitInfosList(), entries); }
@Override public CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto r) throws IOException { // TODO avoid converting list to array final RaftRpcRequestProto request = r.getServerRequest(); final LogEntryProto[] entries = r.getEntriesList() .toArray(new LogEntryProto[r.getEntriesCount()]); final TermIndex previous = r.hasPreviousLog() ? ServerProtoUtils.toTermIndex(r.getPreviousLog()) : null; final RaftPeerId requestorId = RaftPeerId.valueOf(request.getRequestorId()); preAppendEntriesAsync(requestorId, ProtoUtils.toRaftGroupId(request.getRaftGroupId()), r.getLeaderTerm(), previous, r.getLeaderCommit(), r.getInitializing(), entries); try { return appendEntriesAsync(requestorId, r.getLeaderTerm(), previous, r.getLeaderCommit(), request.getCallId(), r.getInitializing(), r.getCommitInfosList(), entries); } catch(Throwable t) { LOG.error(getId() + ": Failed appendEntriesAsync " + r, t); throw t; } }
final TermIndex lastTermIndex = ServerProtoUtils.toTermIndex( request.getTermIndex()); final long lastIncludedIndex = lastTermIndex.getIndex();
final TermIndex lastTermIndex = ServerProtoUtils.toTermIndex( request.getTermIndex()); final long lastIncludedIndex = lastTermIndex.getIndex();