Skip to content

Commit

Permalink
(fix) Forgot to check learner replicator. (sofastack#385)
Browse files Browse the repository at this point in the history
* (fix) Forgot to check learner replicator.

* (feat) Only print log when targetPriority value was changed
  • Loading branch information
killme2008 authored and fengjiachun committed Dec 30, 2019
1 parent 94c5c06 commit b0378d6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -677,8 +677,10 @@ private void checkAndSetConfiguration(final boolean inLock) {
// Update target priority value
final int prevTargetPriority = this.targetPriority;
this.targetPriority = getMaxPriorityOfNodes(this.conf.getConf().getPeers());
LOG.info("Node {} target priority value has changed from: {}, to: {}.", getNodeId(),
prevTargetPriority, this.targetPriority);
if (prevTargetPriority != this.targetPriority) {
LOG.info("Node {} target priority value has changed from: {}, to: {}.", getNodeId(),
prevTargetPriority, this.targetPriority);
}
this.electionTimeoutCounter = 0;
}
} finally {
Expand Down Expand Up @@ -2101,6 +2103,11 @@ private void onCaughtUp(final PeerId peer, final long term, final long version,
}

private void checkDeadNodes(final Configuration conf, final long monotonicNowMs) {
// Check learner replicators at first.
for (PeerId peer : conf.getLearners()) {
checkReplicator(peer);
}
// Ensure quorum nodes alive.
final List<PeerId> peers = conf.listPeers();
final Configuration deadNodes = new Configuration();
if (checkDeadNodes0(peers, monotonicNowMs, true, deadNodes)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public boolean addReplicator(final PeerId peer, final ReplicatorType replicatorT
opts.setPeerId(peer);
final ThreadId rid = Replicator.start(opts, this.raftOptions);
if (rid == null) {
LOG.error("Fail to start replicator to peer={}.", peer);
LOG.error("Fail to start replicator to peer={}, replicatorType={}.", peer, replicatorType);
this.failureReplicators.put(peer, replicatorType);
return false;
}
Expand Down
40 changes: 40 additions & 0 deletions jraft-core/src/test/java/com/alipay/sofa/jraft/core/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,46 @@ public void testResetLearners() throws Exception {
cluster.stopAll();
}

@Test
public void testTripleNodesWithStaticLearners() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);

final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
LinkedHashSet<PeerId> learners = new LinkedHashSet<>();
PeerId learnerPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3);
learners.add(learnerPeer);
cluster.setLearners(learners);

for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint()));
}

// elect leader
cluster.waitLeader();
final Node leader = cluster.getLeader();

assertEquals(3, leader.listPeers().size());
assertEquals(leader.listLearners().size(), 1);
assertTrue(leader.listLearners().contains(learnerPeer));
assertTrue(leader.listAliveLearners().isEmpty());

// start learner after cluster setup.
assertTrue(cluster.start(learnerPeer.getEndpoint()));

Thread.sleep(1000);

assertEquals(3, leader.listPeers().size());
assertEquals(leader.listLearners().size(), 1);
assertEquals(leader.listAliveLearners().size(), 1);

// apply tasks to leader
this.sendTestTaskAndWait(leader);

cluster.ensureSame();
assertEquals(4, cluster.getFsms().size());
cluster.stopAll();
}

@Test
public void testTripleNodesWithLearners() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
Expand Down

0 comments on commit b0378d6

Please sign in to comment.