use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testReadIndex.
@Test
public void testReadIndex() throws Exception {
final 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(), false, 300, true));
}
// elect leader
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
// apply tasks to leader
this.sendTestTaskAndWait(leader);
// first call will fail-fast when no connection
if (!assertReadIndex(leader, 11)) {
assertTrue(assertReadIndex(leader, 11));
}
// read from follower
for (final Node follower : cluster.getFollowers()) {
assertNotNull(follower);
assertReadIndex(follower, 11);
}
// read with null request context
final CountDownLatch latch = new CountDownLatch(1);
leader.readIndex(null, new ReadIndexClosure() {
@Override
public void run(final Status status, final long index, final byte[] reqCtx) {
assertNull(reqCtx);
assertTrue(status.isOk());
latch.countDown();
}
});
latch.await();
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testRecoverFollower.
@Test
public void testRecoverFollower() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers);
for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint()));
}
cluster.waitLeader();
final Node leader = cluster.getLeader();
assertNotNull(leader);
Thread.sleep(100);
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint().copy();
assertTrue(cluster.stop(followerAddr));
this.sendTestTaskAndWait(leader);
for (int i = 10; i < 30; i++) {
final ByteBuffer data = ByteBuffer.wrap(("no clusre" + i).getBytes());
final Task task = new Task(data, null);
leader.apply(task);
}
// wait leader to compact logs
Thread.sleep(5000);
// restart follower
assertTrue(cluster.start(followerAddr));
assertTrue(cluster.ensureSame(30));
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(30, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testLeaderTransferBeforeLogIsCompleted.
@Test
public void testLeaderTransferBeforeLogIsCompleted() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers, 300);
for (final PeerId peer : peers) {
assertTrue(cluster.start(peer.getEndpoint(), false, 1));
}
cluster.waitLeader();
Node leader = cluster.getLeader();
assertNotNull(leader);
Thread.sleep(100);
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId targetPeer = followers.get(0).getNodeId().getPeerId().copy();
assertTrue(cluster.stop(targetPeer.getEndpoint()));
this.sendTestTaskAndWait(leader);
LOG.info("Transfer leadership from {} to {}", leader, targetPeer);
assertTrue(leader.transferLeadershipTo(targetPeer).isOk());
final CountDownLatch latch = new CountDownLatch(1);
final Task task = new Task(ByteBuffer.wrap("aaaaa".getBytes()), new ExpectClosure(RaftError.EBUSY, latch));
leader.apply(task);
waitLatch(latch);
assertTrue(cluster.start(targetPeer.getEndpoint()));
Thread.sleep(5000);
cluster.waitLeader();
leader = cluster.getLeader();
Assert.assertEquals(targetPeer, leader.getNodeId().getPeerId());
assertTrue(cluster.ensureSame(5));
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testInstallLargeSnapshotWithThrottle.
@Test
public void testInstallLargeSnapshotWithThrottle() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(4);
final TestCluster cluster = new TestCluster("unitest", this.dataPath, peers.subList(0, 3));
for (int i = 0; i < peers.size() - 1; i++) {
final PeerId peer = peers.get(i);
final boolean started = cluster.start(peer.getEndpoint(), false, 200, false);
assertTrue(started);
}
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
// apply tasks to leader
sendTestTaskAndWait(leader, 0, RaftError.SUCCESS);
cluster.ensureSame();
// apply something more
for (int i = 1; i < 100; i++) {
sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
}
Thread.sleep(1000);
// trigger leader snapshot
triggerLeaderSnapshot(cluster, leader);
// apply something more
for (int i = 100; i < 200; i++) {
sendTestTaskAndWait(leader, i * 10, RaftError.SUCCESS);
}
// trigger leader snapshot
triggerLeaderSnapshot(cluster, leader, 2);
// wait leader to compact logs
Thread.sleep(1000);
// add follower
final PeerId newPeer = peers.get(3);
final SnapshotThrottle snapshotThrottle = new ThroughputSnapshotThrottle(128, 1);
final boolean started = cluster.start(newPeer.getEndpoint(), true, 300, false, snapshotThrottle);
assertTrue(started);
final CountDownLatch latch = new CountDownLatch(1);
leader.addPeer(newPeer, status -> {
assertTrue(status.toString(), status.isOk());
latch.countDown();
});
waitLatch(latch);
cluster.ensureSame();
assertEquals(4, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(2000, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.Node in project sofa-jraft by sofastack.
the class NodeTest method testNodeTaskOverload.
@Test
public void testNodeTaskOverload() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
final PeerId peer = new PeerId(addr, 0);
NodeManager.getInstance().addAddress(addr);
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
final RaftOptions raftOptions = new RaftOptions();
raftOptions.setDisruptorBufferSize(2);
nodeOptions.setRaftOptions(raftOptions);
final MockStateMachine fsm = new MockStateMachine(addr);
nodeOptions.setFsm(fsm);
nodeOptions.setLogUri(this.dataPath + File.separator + "log");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
final Node node = new NodeImpl("unittest", peer);
assertTrue(node.init(nodeOptions));
assertEquals(1, node.listPeers().size());
assertTrue(node.listPeers().contains(peer));
while (!node.isLeader()) {
;
}
final List<Task> tasks = new ArrayList<>();
final AtomicInteger c = new AtomicInteger(0);
for (int i = 0; i < 10; i++) {
final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
final Task task = new Task(data, new JoinableClosure(status -> {
System.out.println(status);
if (!status.isOk()) {
assertTrue(status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
}
c.incrementAndGet();
}));
node.apply(task);
tasks.add(task);
}
try {
Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
assertEquals(10, c.get());
} finally {
node.shutdown();
node.join();
}
}
Aggregations