Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl learner for jraft, #8 #312

Merged
merged 12 commits into from
Nov 5, 2019
58 changes: 58 additions & 0 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/CliService.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,45 @@ public interface CliService extends Lifecycle<CliOptions> {
*/
Status resetPeer(final String groupId, final PeerId peer, final Configuration newPeers);

/**
* Add some new learners into the replicating group which consists of |conf|.
* return OK status when success.
*
* @param groupId the raft group id
* @param conf current configuration
* @param learners learner peers to add
* @return operation status
* @since 1.3.0
*
*/
Status addLearners(final String groupId, final Configuration conf, final List<PeerId> learners);

/**
* Remove some learners from the replicating group which consists of |conf|.
* return OK status when success.
*
* @param groupId the raft group id
* @param conf current configuration
* @param learners learner peers to remove
* @return operation status
* @since 1.3.0
*
*/
Status removeLearners(final String groupId, final Configuration conf, final List<PeerId> learners);

/**
* Update learners set in the replicating group which consists of |conf|.
* return OK status when success.
*
* @param groupId the raft group id
* @param conf current configuration
* @param learners learner peers to set
* @return operation status
* @since 1.3.0
*
*/
Status resetLearners(final String groupId, final Configuration conf, final List<PeerId> learners);

/**
* Transfer the leader of the replication group to the target peer
*
Expand Down Expand Up @@ -121,6 +160,25 @@ public interface CliService extends Lifecycle<CliOptions> {
*/
List<PeerId> getAlivePeers(final String groupId, final Configuration conf);

/**
* Ask all learners of the replication group.
*
* @param groupId the raft group id
* @param conf target peers configuration
* @return all learners of the replication group
* @since 1.3.0
*/
List<PeerId> getLearners(final String groupId, final Configuration conf);

/**
* Ask all alive learners of the replication group.
*
* @param groupId the raft group id
* @param conf target peers configuration
* @return all alive learners of the replication group
*/
List<PeerId> getAliveLearners(final String groupId, final Configuration conf);

/**
* Balance the number of leaders.
*
Expand Down
59 changes: 57 additions & 2 deletions jraft-core/src/main/java/com/alipay/sofa/jraft/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,41 @@ public interface Node extends Lifecycle<NodeOptions>, Describer {
List<PeerId> listPeers();

/**
* List all alive peers of this raft group, only leader returns.
* List all alive peers of this raft group, only leader returns.</p>
*
* [NOTE] <strong>list_alive_peers is just a transient data (snapshot)
* and a short-term loss of response by the follower will cause it to
* temporarily not exist in this list.</strong>
*
* @return the alive peer list
*
* @since 1.2.6
*/
List<PeerId> listAlivePeers();

/**
* List all learners of this raft group, only leader returns.</p>
*
* [NOTE] <strong>when listLearners concurrency with {@link #addLearners(List, Closure)}/{@link #removeLearners(List, Closure)}/{@link #resetLearners(List, Closure)},
* maybe return peers is staled. Because {@link #addLearners(List, Closure)}/{@link #removeLearners(List, Closure)}/{@link #resetLearners(List, Closure)}
* immediately modify configuration in memory</strong>
*
* @return the learners set
* @since 1.3.0
*/
List<PeerId> listLearners();

/**
* List all alive learners of this raft group, only leader returns.</p>
*
* [NOTE] <strong>when listAliveLearners concurrency with {@link #addLearners(List, Closure)}/{@link #removeLearners(List, Closure)}/{@link #resetLearners(List, Closure)},
* maybe return peers is staled. Because {@link #addLearners(List, Closure)}/{@link #removeLearners(List, Closure)}/{@link #resetLearners(List, Closure)}
* immediately modify configuration in memory</strong>
*
* @return the alive learners set
* @since 1.3.0
*/
List<PeerId> listAliveLearners();

/**
* Add a new peer to the raft group. done.run() would be invoked after this
* operation finishes, describing the detailed result.
Expand Down Expand Up @@ -182,9 +205,41 @@ public interface Node extends Lifecycle<NodeOptions>, Describer {
* availability.
* Notice that neither consistency nor consensus are guaranteed in this
* case, BE CAREFULE when dealing with this method.
*
* @param newPeers new peers
*/
Status resetPeers(final Configuration newPeers);

/**
* Add some new learners to the raft group. done.run() will be invoked after this
* operation finishes, describing the detailed result.
*
* @param learners learners to add
* @param done callback
* @since 1.3.0
*/
void addLearners(final List<PeerId> learners, final Closure done);

/**
* Remove some learners from the raft group. done.run() will be invoked after this
* operation finishes, describing the detailed result.
*
* @param learners learners to remove
* @param done callback
* @since 1.3.0
*/
void removeLearners(final List<PeerId> learners, final Closure done);

/**
* Reset learners in the raft group. done.run() will be invoked after this
* operation finishes, describing the detailed result.
*
* @param learners learners to set
* @param done callback
* @since 1.3.0
*/
void resetLearners(final List<PeerId> learners, final Closure done);

/**
* Start a snapshot immediately if possible. done.run() would be invoked when
* the snapshot finishes, describing the detailed result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.alipay.sofa.jraft.closure.CatchUpClosure;
import com.alipay.sofa.jraft.conf.ConfigurationEntry;
import com.alipay.sofa.jraft.core.ReplicatorType;
import com.alipay.sofa.jraft.entity.NodeId;
import com.alipay.sofa.jraft.entity.PeerId;
import com.alipay.sofa.jraft.option.ReplicatorGroupOptions;
Expand All @@ -45,6 +46,15 @@ public interface ReplicatorGroup extends Describer {
*/
boolean init(final NodeId nodeId, final ReplicatorGroupOptions opts);

/**
* Adds a replicator for follower({@link ReplicatorType#Follower}).
* @see #addReplicator(PeerId, ReplicatorType)
*
* @param peer target peer
* @return true on success
*/
boolean addReplicator(final PeerId peer);

/**
* Add a replicator attached with |peer|
* will be a notification when the replicator catches up according to the
Expand All @@ -53,10 +63,11 @@ public interface ReplicatorGroup extends Describer {
* immediately, and might call Node#stepDown which might have race with
* the caller, you should deal with this situation.
*
* @param peer target peer
* @param peer target peer
* @param replicatorType replicator type
* @return true on success
*/
boolean addReplicator(final PeerId peer);
boolean addReplicator(final PeerId peer, ReplicatorType replicatorType);

/**
* Send heartbeat to a peer.
Expand Down
Loading