use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class NodeTest method testRemoveLeader.
@Test
public void testRemoveLeader() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint()));
}
// elect leader
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId oldLeader = leader.getNodeId().getPeerId().copy();
final Endpoint oldLeaderAddr = oldLeader.getEndpoint();
// remove old leader
LOG.info("Remove old leader {}", oldLeader);
CountDownLatch latch = new CountDownLatch(1);
leader.removePeer(oldLeader, new ExpectClosure(latch));
waitLatch(latch);
Thread.sleep(100);
// elect new leader
cluster.waitLeader();
leader = cluster.getLeader();
LOG.info("New leader is {}", leader);
assertNotNull(leader);
// apply tasks to new leader
this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
// stop and clean old leader
LOG.info("Stop and clean old leader {}", oldLeader);
assertTrue(cluster.stop(oldLeaderAddr));
cluster.clean(oldLeaderAddr);
// Add and start old leader
LOG.info("Start and add old leader {}", oldLeader);
assertTrue(cluster.start(oldLeaderAddr));
peers = TestUtils.generatePeers(3);
assertTrue(peers.remove(oldLeader));
latch = new CountDownLatch(1);
leader.addPeer(oldLeader, new ExpectClosure(latch));
waitLatch(latch);
followers = cluster.getFollowers();
assertEquals(2, followers.size());
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(20, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class NodeTest method testRemoveFollower.
@Test
public void testRemoveFollower() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint()));
}
// elect leader
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
final Endpoint followerAddr = followerPeer.getEndpoint();
// stop and clean follower
LOG.info("Stop and clean follower {}", followerPeer);
assertTrue(cluster.stop(followerAddr));
cluster.clean(followerAddr);
// remove follower
LOG.info("Remove follower {}", followerPeer);
CountDownLatch latch = new CountDownLatch(1);
leader.removePeer(followerPeer, new ExpectClosure(latch));
waitLatch(latch);
this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
followers = cluster.getFollowers();
assertEquals(1, followers.size());
peers = TestUtils.generatePeers(3);
assertTrue(peers.remove(followerPeer));
// start follower
LOG.info("Start and add follower {}", followerPeer);
assertTrue(cluster.start(followerAddr));
// re-add follower
latch = new CountDownLatch(1);
leader.addPeer(followerPeer, new ExpectClosure(latch));
waitLatch(latch);
followers = cluster.getFollowers();
assertEquals(2, followers.size());
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(20, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class PeerIdTest method testIsPriorityDisabled.
@Test
public void testIsPriorityDisabled() {
final Endpoint endpoint1 = new Endpoint("192.168.1.1", 8081);
final PeerId peer1 = new PeerId(endpoint1, 0);
assertEquals("192.168.1.1:8081", peer1.toString());
assertTrue(peer1.isPriorityDisabled());
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class PeerIdTest method testToStringParseWithIdxAndPriority.
@Test
public void testToStringParseWithIdxAndPriority() {
// 1.String format is, ip:port::priority
final Endpoint endpoint1 = new Endpoint("192.168.1.1", 8081);
final PeerId peer1 = new PeerId(endpoint1, 0, 100);
assertEquals("192.168.1.1:8081::100", peer1.toString());
final PeerId p1 = new PeerId();
final String str1 = "192.168.1.1:8081::100";
assertTrue(p1.parse(str1));
assertEquals(8081, p1.getPort());
assertEquals("192.168.1.1", p1.getIp());
assertEquals(0, p1.getIdx());
assertEquals(100, p1.getPriority());
assertEquals(p1, peer1);
assertEquals(p1.hashCode(), peer1.hashCode());
// 2.String format is, ip:port:idx:priority
final Endpoint endpoint2 = new Endpoint("192.168.1.1", 8081);
final PeerId peer2 = new PeerId(endpoint2, 100, 200);
assertEquals("192.168.1.1:8081:100:200", peer2.toString());
final PeerId p2 = new PeerId();
final String str2 = "192.168.1.1:8081:100:200";
assertTrue(p2.parse(str2));
assertEquals(8081, p2.getPort());
assertEquals("192.168.1.1", p2.getIp());
assertEquals(100, p2.getIdx());
assertEquals(200, p2.getPriority());
assertEquals(p2, peer2);
assertEquals(p2.hashCode(), peer2.hashCode());
}
use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.
the class LocalSnapshotCopierTest method testInterrupt.
@Test
public void testInterrupt() throws Exception {
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
// mock get metadata
final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
this.copier.start();
Thread.sleep(10);
Utils.runInThread(new Runnable() {
@Override
public void run() {
LocalSnapshotCopierTest.this.copier.cancel();
}
});
this.copier.join();
// start timer
final SnapshotReader reader = this.copier.getReader();
assertNull(reader);
Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
Assert.assertEquals("Cancel the copier manually.", this.copier.getErrorMsg());
}
Aggregations