public SegmentedRaftLog(RaftPeerId selfId, RaftServerImpl server, RaftStorage storage, long lastIndexInSnapshot, RaftProperties properties) { this(selfId, server, server != null? server.getStateMachine(): null, server != null? server::submitUpdateCommitEvent: null, storage, lastIndexInSnapshot, properties); }
public static SimpleStateMachine4Testing get(RaftServerImpl s) { return (SimpleStateMachine4Testing)s.getStateMachine(); }
public static SimpleStateMachine4Testing get(RaftServerImpl s) { return (SimpleStateMachine4Testing)s.getStateMachine(); }
static MetricsStateMachine get(RaftServerImpl s) { return (MetricsStateMachine)s.getStateMachine(); }
public static SMTransactionContext get(RaftServerImpl s) { return (SMTransactionContext)s.getStateMachine(); }
public static SMTransactionContext get(RaftServerImpl s) { return (SMTransactionContext)s.getStateMachine(); }
SnapshotInfo getLatestSnapshot() { return server.getStateMachine().getStateMachineStorage().getLatestSnapshot(); }
SnapshotInfo getLatestSnapshot() { return server.getStateMachine().getStateMachineStorage().getLatestSnapshot(); }
protected void checkSlowness() { if (follower.isSlow()) { server.getStateMachine().notifySlowness(server.getGroup(), server.getRoleInfoProto()); } }
RaftLogWorker(RaftPeerId selfId, RaftServerImpl raftServer, RaftStorage storage, RaftProperties properties) { this.name = selfId + "-" + getClass().getSimpleName(); LOG.info("new {} for {}", name, storage); this.raftServer = raftServer; this.stateMachine = raftServer != null? raftServer.getStateMachine(): null; this.storage = storage; this.segmentMaxSize = RaftServerConfigKeys.Log.segmentSizeMax(properties).getSize(); this.preallocatedSize = RaftServerConfigKeys.Log.preallocatedSize(properties).getSize(); this.bufferSize = RaftServerConfigKeys.Log.writeBufferSize(properties).getSizeInt(); this.forceSyncNum = RaftServerConfigKeys.Log.forceSyncNum(properties); this.workerThread = new Thread(this, name); // Server Id can be null in unit tests Supplier<String> serverId = () -> raftServer == null || raftServer.getId() == null ? "null" : raftServer.getId().toString(); this.logFlushTimer = JavaUtils.memoize(() -> RatisMetricsRegistry.getRegistry() .timer(MetricRegistry.name(RaftLogWorker.class, serverId.get(), "flush-time"))); }
@Override public EntryWithData getEntryWithData(long index) throws RaftLogIOException { final LogEntryProto entry = get(index); if (!ServerProtoUtils.shouldReadStateMachineData(entry)) { return new EntryWithData(entry, null); } try { return new EntryWithData(entry, server.map(s -> s.getStateMachine().readStateMachineData(entry)).orElse(null)); } catch (Throwable e) { final String err = getSelfId() + ": Failed readStateMachineData for " + ServerProtoUtils.toLogEntryString(entry); LOG.error(err, e); throw new RaftLogIOException(err, JavaUtils.unwrapCompletionException(e)); } }
/** * The leader state is stopped. Send NotLeaderException to all the pending * requests since they have not got applied to the state machine yet. */ void sendNotLeaderResponses() throws IOException { LOG.info("{} sends responses before shutting down PendingRequestsHandler", server.getId()); // notify the state machine about stepping down final NotLeaderException nle = server.generateNotLeaderException(); server.getStateMachine().notifyNotLeader(pendingRequests.setNotLeaderException(nle)); if (pendingSetConf != null) { pendingSetConf.setNotLeaderException(nle); } delayedReplies.failReplies(); }
void stop() { this.running = false; // do not interrupt event processor since it may be in the middle of logSync senders.forEach(LogAppender::stopAppender); final NotLeaderException nle = server.generateNotLeaderException(); final Collection<CommitInfoProto> commitInfos = server.getCommitInfos(); try { final Collection<TransactionContext> transactions = pendingRequests.sendNotLeaderResponses(nle, commitInfos); server.getStateMachine().notifyNotLeader(transactions); watchRequests.failWatches(nle); } catch (IOException e) { LOG.warn(server.getId() + ": Caught exception in sendNotLeaderResponses", e); } }
boolean isLogUpToDate(TermIndex candidateLastEntry) { TermIndex local = log.getLastEntryTermIndex(); // need to take into account snapshot SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot(); if (local == null && snapshot == null) { return true; } else if (candidateLastEntry == null) { return false; } if (local == null || (snapshot != null && snapshot.getIndex() > local.getIndex())) { local = snapshot.getTermIndex(); } return local.compareTo(candidateLastEntry) <= 0; }
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()); }
boolean isLogUpToDate(TermIndex candidateLastEntry) { TermIndex local = log.getLastEntryTermIndex(); // need to take into account snapshot SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot(); if (local == null && snapshot == null) { return true; } else if (candidateLastEntry == null) { return false; } if (local == null || (snapshot != null && snapshot.getIndex() > local.getIndex())) { local = snapshot.getTermIndex(); } return local.compareTo(candidateLastEntry) <= 0; }
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()); }
private CompletableFuture<RaftClientReply> staleReadAsync(RaftClientRequest request) { final long minIndex = request.getType().getStaleRead().getMinIndex(); final long commitIndex = state.getLog().getLastCommittedIndex(); LOG.debug("{}: minIndex={}, commitIndex={}", getId(), minIndex, commitIndex); if (commitIndex < minIndex) { final StaleReadException e = new StaleReadException( "Unable to serve stale-read due to server commit index = " + commitIndex + " < min = " + minIndex); return CompletableFuture.completedFuture( new RaftClientReply(request, new StateMachineException(getId(), e), getCommitInfos())); } return processQueryFuture(getStateMachine().queryStale(request.getMessage(), minIndex), request); }
private CompletableFuture<RaftClientReply> staleReadAsync(RaftClientRequest request) { final long minIndex = request.getType().getStaleRead().getMinIndex(); final long commitIndex = state.getLog().getLastCommittedIndex(); LOG.debug("{}: minIndex={}, commitIndex={}", getId(), minIndex, commitIndex); if (commitIndex < minIndex) { final StaleReadException e = new StaleReadException( "Unable to serve stale-read due to server commit index = " + commitIndex + " < min = " + minIndex); return CompletableFuture.completedFuture( new RaftClientReply(request, new StateMachineException(getId(), e), getCommitInfos())); } return processQueryFuture(getStateMachine().queryStale(request.getMessage(), minIndex), request); }
CompletableFuture<Message> applyLogToStateMachine(LogEntryProto next) { final StateMachine stateMachine = getStateMachine(); if (next.getLogEntryBodyCase() == CONFIGURATIONENTRY) { // the reply should have already been set. only need to record // the new conf in the state machine. stateMachine.setRaftConfiguration(toRaftConfiguration(next.getIndex(), next.getConfigurationEntry())); } else if (next.getLogEntryBodyCase() == SMLOGENTRY) { // check whether there is a TransactionContext because we are the leader. TransactionContext trx = getTransactionContext(next.getIndex()); if (trx == null) { trx = new TransactionContextImpl(stateMachine, next); } // Let the StateMachine inject logic for committed transactions in sequential order. trx = stateMachine.applyTransactionSerial(trx); // TODO: This step can be parallelized CompletableFuture<Message> stateMachineFuture = stateMachine.applyTransaction(trx); return replyPendingRequest(next, stateMachineFuture); } return null; }