use of org.apache.ratis.server.leader.LogAppender in project incubator-ratis by apache.
the class LeaderStateImpl method addSenders.
Collection<LogAppender> addSenders(Collection<RaftPeer> newPeers, long nextIndex, boolean attendVote) {
final Timestamp t = Timestamp.currentTime().addTimeMs(-server.getMaxTimeoutMs());
final List<LogAppender> newAppenders = newPeers.stream().map(peer -> {
final FollowerInfo f = new FollowerInfoImpl(server.getMemberId(), peer, t, nextIndex, attendVote);
LogAppender logAppender = server.newLogAppender(this, f);
peerIdFollowerInfoMap.put(peer.getId(), f);
raftServerMetrics.addFollower(peer.getId());
logAppenderMetrics.addFollowerGauges(peer.getId(), f::getNextIndex, f::getMatchIndex, f::getLastRpcTime);
return logAppender;
}).collect(Collectors.toList());
senders.addAll(newAppenders);
return newAppenders;
}
use of org.apache.ratis.server.leader.LogAppender in project incubator-ratis by apache.
the class LeaderStateImpl method updateFollowerCommitInfos.
void updateFollowerCommitInfos(CommitInfoCache cache, List<CommitInfoProto> protos) {
for (LogAppender sender : senders.getSenders()) {
FollowerInfo info = sender.getFollower();
protos.add(cache.update(info.getPeer(), info.getCommitIndex()));
}
}
use of org.apache.ratis.server.leader.LogAppender in project incubator-ratis by apache.
the class LeaderStateImpl method start.
LogEntryProto start() {
// In the beginning of the new term, replicate a conf entry in order
// to finally commit entries in the previous term.
// Also this message can help identify the last committed index and the conf.
final LogEntryProto placeHolder = LogProtoUtils.toLogEntryProto(server.getRaftConf(), server.getState().getCurrentTerm(), raftLog.getNextIndex());
CodeInjectionForTesting.execute(APPEND_PLACEHOLDER, server.getId().toString(), null);
raftLog.append(placeHolder);
processor.start();
senders.forEach(LogAppender::start);
return placeHolder;
}
use of org.apache.ratis.server.leader.LogAppender in project incubator-ratis by apache.
the class LeaderStateImpl method yieldLeaderToHigherPriorityPeer.
private void yieldLeaderToHigherPriorityPeer() {
if (!server.getInfo().isLeader()) {
return;
}
final RaftConfigurationImpl conf = server.getRaftConf();
final RaftPeer leader = conf.getPeer(server.getId());
if (leader == null) {
LOG.error("{} the leader {} is not in the conf {}", this, server.getId(), conf);
return;
}
int leaderPriority = leader.getPriority();
for (LogAppender logAppender : senders.getSenders()) {
final FollowerInfo followerInfo = logAppender.getFollower();
final RaftPeerId followerID = followerInfo.getPeer().getId();
final RaftPeer follower = conf.getPeer(followerID);
if (follower == null) {
LOG.error("{} the follower {} is not in the conf {}", this, server.getId(), conf);
continue;
}
final int followerPriority = follower.getPriority();
if (followerPriority <= leaderPriority) {
continue;
}
final TermIndex leaderLastEntry = server.getState().getLastEntry();
if (leaderLastEntry == null) {
LOG.info("{} send StartLeaderElectionRequest to follower:{} on term:{} because follower's priority:{} " + "is higher than leader's:{} and leader's lastEntry is null", this, followerID, currentTerm, followerPriority, leaderPriority);
sendStartLeaderElectionToHigherPriorityPeer(followerID, null);
return;
}
if (followerInfo.getMatchIndex() >= leaderLastEntry.getIndex()) {
LOG.info("{} send StartLeaderElectionRequest to follower:{} on term:{} because follower's priority:{} " + "is higher than leader's:{} and follower's lastEntry index:{} catch up with leader's:{}", this, followerID, currentTerm, followerPriority, leaderPriority, followerInfo.getMatchIndex(), leaderLastEntry.getIndex());
sendStartLeaderElectionToHigherPriorityPeer(followerID, leaderLastEntry);
return;
}
}
}
use of org.apache.ratis.server.leader.LogAppender in project incubator-ratis by apache.
the class LeaderStateImpl method stop.
void stop() {
this.running = false;
// do not interrupt event processor since it may be in the middle of logSync
senders.forEach(LogAppender::stop);
final NotLeaderException nle = server.generateNotLeaderException();
final Collection<CommitInfoProto> commitInfos = server.getCommitInfos();
try {
final Collection<TransactionContext> transactions = pendingRequests.sendNotLeaderResponses(nle, commitInfos);
server.getStateMachine().leaderEvent().notifyNotLeader(transactions);
watchRequests.failWatches(nle);
} catch (IOException e) {
LOG.warn("{}: Caught exception in sendNotLeaderResponses", this, e);
}
messageStreamRequests.clear();
server.getServerRpc().notifyNotLeader(server.getMemberId().getGroupId());
logAppenderMetrics.unregister();
raftServerMetrics.unregister();
pendingRequests.close();
}
Aggregations