Search in sources :

Example 1 with LogAppender

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;
}
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 LogAppender

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

Example 3 with LogAppender

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;
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogAppender(org.apache.ratis.server.leader.LogAppender)

Example 4 with LogAppender

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;
        }
    }
}
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 LogAppender

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();
}
Also used : NotLeaderException(org.apache.ratis.protocol.exceptions.NotLeaderException) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) LogAppender(org.apache.ratis.server.leader.LogAppender) TransactionContext(org.apache.ratis.statemachine.TransactionContext) IOException(java.io.IOException)

Aggregations

LogAppender (org.apache.ratis.server.leader.LogAppender)5 FollowerInfo (org.apache.ratis.server.leader.FollowerInfo)3 IOException (java.io.IOException)2 CommitInfoProto (org.apache.ratis.proto.RaftProtos.CommitInfoProto)2 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)2 RaftPeer (org.apache.ratis.protocol.RaftPeer)2 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)2 TermIndex (org.apache.ratis.server.protocol.TermIndex)2 ArrayList (java.util.ArrayList)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