use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class NodeTest method testSingleNode.
@Test
public void testSingleNode() 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 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()) {
;
}
sendTestTaskAndWait(node);
assertEquals(10, fsm.getLogs().size());
int i = 0;
for (final ByteBuffer data : fsm.getLogs()) {
assertEquals("hello" + i++, new String(data.array()));
}
node.shutdown();
node.join();
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class NodeTest method testJoinNodes.
@Test
public void testJoinNodes() throws Exception {
final PeerId peer0 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
final PeerId peer1 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 1);
final PeerId peer2 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 2);
final PeerId peer3 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + 3);
final ArrayList<PeerId> peers = new ArrayList<>();
peers.add(peer0);
// start single cluster
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
assertTrue(cluster.start(peer0.getEndpoint()));
cluster.waitLeader();
final Node leader = cluster.getLeader();
assertNotNull(leader);
Assert.assertEquals(leader.getNodeId().getPeerId(), peer0);
this.sendTestTaskAndWait(leader);
// start peer1
assertTrue(cluster.start(peer1.getEndpoint(), true, 300));
// add peer1
CountDownLatch latch = new CountDownLatch(1);
peers.add(peer1);
leader.addPeer(peer1, new ExpectClosure(latch));
waitLatch(latch);
cluster.ensureSame(-1);
assertEquals(2, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(10, fsm.getLogs().size());
}
// add peer2 but not start
peers.add(peer2);
latch = new CountDownLatch(1);
leader.addPeer(peer2, new ExpectClosure(RaftError.ECATCHUP, latch));
waitLatch(latch);
// start peer2 after 2 seconds
Thread.sleep(2000);
assertTrue(cluster.start(peer2.getEndpoint(), true, 300));
Thread.sleep(10000);
// re-add peer2
latch = new CountDownLatch(2);
leader.addPeer(peer2, new ExpectClosure(latch));
// concurrent configuration change
leader.addPeer(peer3, new ExpectClosure(RaftError.EBUSY, latch));
waitLatch(latch);
try {
leader.addPeer(peer2, new ExpectClosure(latch));
fail();
} catch (final IllegalArgumentException e) {
assertEquals("Peer already exists in current configuration", e.getMessage());
}
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
assertEquals(2, cluster.getFollowers().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(10, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class NodeTest method testChangePeersChaosWithSnapshot.
@Test
public void testChangePeersChaosWithSnapshot() throws Exception {
// start cluster
final List<PeerId> peers = new ArrayList<>();
peers.add(new PeerId("127.0.0.1", TestUtils.INIT_PORT));
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, 1000);
assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 2));
// start other peers
for (int i = 1; i < 10; i++) {
final PeerId peer = new PeerId("127.0.0.1", TestUtils.INIT_PORT + i);
peers.add(peer);
assertTrue(cluster.start(peer.getEndpoint()));
}
final ChangeArg arg = new ChangeArg(cluster, peers, false, false);
final Future<?> future = startChangePeersThread(arg);
for (int i = 0; i < 5000; ) {
cluster.waitLeader();
final Node leader = cluster.getLeader();
if (leader == null) {
continue;
}
final SynchronizedClosure done = new SynchronizedClosure();
final Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes()), done);
leader.apply(task);
final Status status = done.await();
if (status.isOk()) {
if (++i % 100 == 0) {
System.out.println("Progress:" + i);
}
} else {
assertEquals(RaftError.EPERM, status.getRaftError());
}
}
arg.stop = true;
future.get();
cluster.waitLeader();
final SynchronizedClosure done = new SynchronizedClosure();
final Node leader = cluster.getLeader();
leader.changePeers(new Configuration(peers), done);
final Status st = done.await();
assertTrue(st.getErrorMsg(), st.isOk());
cluster.ensureSame();
assertEquals(10, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertTrue(fsm.getLogs().size() >= 5000);
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class NodeTest method testInstallSnapshotWithThrottle.
@Test
public void testInstallSnapshotWithThrottle() 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(), false, 200, false, new ThroughputSnapshotThrottle(1024, 1)));
}
cluster.waitLeader();
// get leader
final Node leader = cluster.getLeader();
assertNotNull(leader);
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
// stop follower1
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final Endpoint followerAddr = followers.get(0).getNodeId().getPeerId().getEndpoint();
assertTrue(cluster.stop(followerAddr));
cluster.waitLeader();
// apply something more
this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
Thread.sleep(1000);
// trigger leader snapshot
triggerLeaderSnapshot(cluster, leader);
// apply something more
this.sendTestTaskAndWait(leader, 20, RaftError.SUCCESS);
// trigger leader snapshot
triggerLeaderSnapshot(cluster, leader, 2);
// wait leader to compact logs
Thread.sleep(1000);
// restart follower.
cluster.clean(followerAddr);
assertTrue(cluster.start(followerAddr, true, 300, false, new ThroughputSnapshotThrottle(1024, 1)));
Thread.sleep(2000);
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(30, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class NodeTest method testChangePeersChaosApplyTasks.
@Test
public void testChangePeersChaosApplyTasks() throws Exception {
// start cluster
final List<PeerId> peers = new ArrayList<>();
peers.add(new PeerId("127.0.0.1", TestUtils.INIT_PORT));
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers, 1000);
assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 100000));
// start other peers
for (int i = 1; i < 10; i++) {
final PeerId peer = new PeerId("127.0.0.1", TestUtils.INIT_PORT + i);
peers.add(peer);
assertTrue(cluster.start(peer.getEndpoint(), true, 100000));
}
final int threads = 3;
final List<ChangeArg> args = new ArrayList<>();
final List<Future<?>> futures = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(threads);
for (int t = 0; t < threads; t++) {
final ChangeArg arg = new ChangeArg(cluster, peers, false, true);
args.add(arg);
futures.add(startChangePeersThread(arg));
Utils.runInThread(() -> {
try {
for (int i = 0; i < 5000; ) {
cluster.waitLeader();
final Node leader = cluster.getLeader();
if (leader == null) {
continue;
}
final SynchronizedClosure done = new SynchronizedClosure();
final Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes()), done);
leader.apply(task);
final Status status = done.await();
if (status.isOk()) {
if (++i % 100 == 0) {
System.out.println("Progress:" + i);
}
} else {
assertEquals(RaftError.EPERM, status.getRaftError());
}
}
} catch (final Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
});
}
latch.await();
for (final ChangeArg arg : args) {
arg.stop = true;
}
for (final Future<?> future : futures) {
future.get();
}
cluster.waitLeader();
final SynchronizedClosure done = new SynchronizedClosure();
final Node leader = cluster.getLeader();
leader.changePeers(new Configuration(peers), done);
assertTrue(done.await().isOk());
cluster.ensureSame();
assertEquals(10, cluster.getFsms().size());
try {
for (final MockStateMachine fsm : cluster.getFsms()) {
final int logSize = fsm.getLogs().size();
assertTrue("logSize= " + logSize, logSize >= 5000 * threads);
assertTrue("logSize= " + logSize, logSize - 5000 * threads < 100);
}
} finally {
cluster.stopAll();
}
}
Aggregations