Search in sources :

Example 1 with FollowerInfo

use of org.apache.ratis.server.leader.FollowerInfo 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;
}
Also used : Arrays(java.util.Arrays) TermIndex(org.apache.ratis.server.protocol.TermIndex) LongSupplier(java.util.function.LongSupplier) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) TransactionContext(org.apache.ratis.statemachine.TransactionContext) NotReplicatedException(org.apache.ratis.protocol.exceptions.NotReplicatedException) Map(java.util.Map) JavaUtils(org.apache.ratis.util.JavaUtils) ToLongFunction(java.util.function.ToLongFunction) ReconfigurationTimeoutException(org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException) Timestamp(org.apache.ratis.util.Timestamp) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftPeer(org.apache.ratis.protocol.RaftPeer) Predicate(java.util.function.Predicate) TransferLeadershipRequest(org.apache.ratis.protocol.TransferLeadershipRequest) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AppendEntriesRequestProto(org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto) BlockingQueue(java.util.concurrent.BlockingQueue) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) List(java.util.List) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) LogAppender(org.apache.ratis.server.leader.LogAppender) RaftProperties(org.apache.ratis.conf.RaftProperties) Optional(java.util.Optional) TimeDuration(org.apache.ratis.util.TimeDuration) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) Preconditions(org.apache.ratis.util.Preconditions) RaftLog(org.apache.ratis.server.raftlog.RaftLog) SetConfigurationRequest(org.apache.ratis.protocol.SetConfigurationRequest) CollectionUtils(org.apache.ratis.util.CollectionUtils) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) LogAppenderMetrics(org.apache.ratis.server.metrics.LogAppenderMetrics) ArrayList(java.util.ArrayList) Message(org.apache.ratis.protocol.Message) NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) ReplicationLevel(org.apache.ratis.proto.RaftProtos.ReplicationLevel) FollowerInfo(org.apache.ratis.server.leader.FollowerInfo) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) LOG(org.apache.ratis.server.RaftServer.Division.LOG) Daemon(org.apache.ratis.util.Daemon) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) IOException(java.io.IOException) CodeInjectionForTesting(org.apache.ratis.util.CodeInjectionForTesting) StartLeaderElectionRequestProto(org.apache.ratis.proto.RaftProtos.StartLeaderElectionRequestProto) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) FOLLOWER_GAP_RATIO_MAX_KEY(org.apache.ratis.server.RaftServerConfigKeys.Write.FOLLOWER_GAP_RATIO_MAX_KEY) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) LeaderState(org.apache.ratis.server.leader.LeaderState) Collections(java.util.Collections) StartLeaderElectionReplyProto(org.apache.ratis.proto.RaftProtos.StartLeaderElectionReplyProto) LogAppender(org.apache.ratis.server.leader.LogAppender) Timestamp(org.apache.ratis.util.Timestamp) FollowerInfo(org.apache.ratis.server.leader.FollowerInfo)

Example 2 with FollowerInfo

use of org.apache.ratis.server.leader.FollowerInfo in project incubator-ratis by apache.

the class LeaderStateImpl method getFollowerInfos.

private List<FollowerInfo> getFollowerInfos(List<RaftPeerId> followerIDs) {
    List<FollowerInfo> followerInfos = new ArrayList<>();
    for (int i = 0; i < followerIDs.size(); i++) {
        RaftPeerId id = followerIDs.get(i);
        if (!peerIdFollowerInfoMap.containsKey(id)) {
            throw new IllegalArgumentException("RaftPeerId:" + id + " not in peerIdFollowerInfoMap of leader:" + server.getMemberId());
        }
        followerInfos.add(peerIdFollowerInfoMap.get(id));
    }
    return followerInfos;
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) FollowerInfo(org.apache.ratis.server.leader.FollowerInfo)

Example 3 with FollowerInfo

use of org.apache.ratis.server.leader.FollowerInfo 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()));
    }
}
Also used : LogAppender(org.apache.ratis.server.leader.LogAppender) FollowerInfo(org.apache.ratis.server.leader.FollowerInfo)

Example 4 with FollowerInfo

use of org.apache.ratis.server.leader.FollowerInfo 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;
        }
    }
}
Also used : LogAppender(org.apache.ratis.server.leader.LogAppender) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) FollowerInfo(org.apache.ratis.server.leader.FollowerInfo) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 5 with FollowerInfo

use of org.apache.ratis.server.leader.FollowerInfo in project incubator-ratis by apache.

the class GrpcLogAppender method shouldNotifyToInstallSnapshot.

/**
 * Should the Leader notify the Follower to install the snapshot through
 * its own State Machine.
 * @return the first available log's start term index
 */
private TermIndex shouldNotifyToInstallSnapshot() {
    final FollowerInfo follower = getFollower();
    final long leaderNextIndex = getRaftLog().getNextIndex();
    final boolean isFollowerBootstrapping = getLeaderState().isFollowerBootstrapping(follower);
    final long leaderStartIndex = getRaftLog().getStartIndex();
    final TermIndex firstAvailable = Optional.ofNullable(getRaftLog().getTermIndex(leaderStartIndex)).orElseGet(() -> TermIndex.valueOf(getServer().getInfo().getCurrentTerm(), leaderNextIndex));
    if (isFollowerBootstrapping && !follower.hasAttemptedToInstallSnapshot()) {
        // If the follower is bootstrapping and has not yet installed any snapshot from leader, then the follower should
        // be notified to install a snapshot. Every follower should try to install at least one snapshot during
        // bootstrapping, if available.
        LOG.debug("{}: follower is bootstrapping, notify to install snapshot to {}.", this, firstAvailable);
        return firstAvailable;
    }
    final long followerNextIndex = follower.getNextIndex();
    if (followerNextIndex >= leaderNextIndex) {
        return null;
    }
    if (followerNextIndex < leaderStartIndex) {
        // State Machine.
        return firstAvailable;
    } else if (leaderStartIndex == RaftLog.INVALID_LOG_INDEX) {
        // Leader has no logs to check from, hence return next index.
        return firstAvailable;
    }
    return null;
}
Also used : FollowerInfo(org.apache.ratis.server.leader.FollowerInfo) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Aggregations

FollowerInfo (org.apache.ratis.server.leader.FollowerInfo)6 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)3 LogAppender (org.apache.ratis.server.leader.LogAppender)3 TermIndex (org.apache.ratis.server.protocol.TermIndex)3 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 RaftPeer (org.apache.ratis.protocol.RaftPeer)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 BlockingQueue (java.util.concurrent.BlockingQueue)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1