Skip to content

Commit

Permalink
feature: add learner to follower realize (sofastack#598)
Browse files Browse the repository at this point in the history
* learner to follower API
  • Loading branch information
funky-eyes authored Jul 8, 2021
1 parent 511ee01 commit e63ce9b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
13 changes: 13 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 @@ -101,6 +101,19 @@ public interface CliService extends Lifecycle<CliOptions> {
*/
Status removeLearners(final String groupId, final Configuration conf, final List<PeerId> learners);

/**
* Converts the specified learner to follower of |conf|.
* return OK status when success.
*
* @param groupId the raft group id
* @param conf current configuration
* @param learner learner peer
* @return operation status
* @since 1.3.8
*
*/
Status learner2Follower(final String groupId, final Configuration conf, final PeerId learner);

/**
* Update learners set in the replicating group which consists of |conf|.
* return OK status when success.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -373,6 +374,15 @@ public Status removeLearners(final String groupId, final Configuration conf, fin
}
}

@Override
public Status learner2Follower(final String groupId, final Configuration conf, final PeerId learner) {
Status status = removeLearners(groupId, conf, Arrays.asList(learner));
if (status.isOk()) {
status = addPeer(groupId, conf, new PeerId(learner.getIp(), learner.getPort()));
}
return status;
}

@Override
public Status resetLearners(final String groupId, final Configuration conf, final List<PeerId> learners) {
checkLearnersOpParams(groupId, conf, learners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -214,6 +215,18 @@ public void testLearnerServices() throws Exception {
Thread.sleep(1000);
assertEquals(Arrays.asList(learner3), this.cliService.getLearners(this.groupId, this.conf));
assertTrue(this.cliService.getAliveLearners(this.groupId, this.conf).isEmpty());
final PeerId learner4 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + LEARNER_PORT_STEP + 4);
assertTrue(this.cluster.startLearner(learner4));
this.cliService.addLearners(this.groupId, this.conf, Arrays.asList(learner4));
Thread.sleep(1000);
assertTrue(this.cliService.getAliveLearners(this.groupId, this.conf).size() == 1);
assertTrue(this.cliService.learner2Follower(this.groupId, this.conf, learner4).isOk());
Thread.sleep(1000);
List<PeerId> currentLearners = this.cliService.getAliveLearners(this.groupId, this.conf);
assertFalse(currentLearners.contains(learner4));
List<PeerId> currentFollowers = this.cliService.getPeers(this.groupId, this.conf);
assertTrue(currentFollowers.contains(learner4));
this.cluster.stop(learner4.getEndpoint());
}

@Test
Expand Down

0 comments on commit e63ce9b

Please sign in to comment.