use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class NodeTest method sendTestTaskAndWait.
private void sendTestTaskAndWait(final Node node, final int start, final RaftError err) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(10);
for (int i = start; i < start + 10; i++) {
final ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
final Task task = new Task(data, new ExpectClosure(err, latch));
node.apply(task);
}
waitLatch(latch);
}
use of com.alipay.sofa.jraft.entity.Task 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.Task 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();
}
}
use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class NodeTest method testChangePeersChaosWithoutSnapshot.
@Test
public void testChangePeersChaosWithoutSnapshot() 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, 10000));
}
final ChangeArg arg = new ChangeArg(cluster, peers, false, true);
final Future<?> future = startChangePeersThread(arg);
final int tasks = 5000;
for (int i = 0; i < tasks; ) {
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);
assertTrue(done.await().isOk());
cluster.ensureSame();
assertEquals(10, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertTrue(fsm.getLogs().size() >= tasks);
assertTrue(fsm.getLogs().size() - tasks < 100);
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.entity.Task in project sofa-jraft by sofastack.
the class NodeTest method testTripleNodesV1V2Codec.
@Test
public void testTripleNodesV1V2Codec() throws Exception {
final List<PeerId> peers = TestUtils.generatePeers(3);
final TestCluster cluster = new TestCluster("unittest", this.dataPath, peers);
for (int i = 0; i < peers.size(); i++) {
// Peer3 use codec v1
if (i == 2) {
cluster.setRaftServiceFactory(new V1JRaftServiceFactory());
}
assertTrue(cluster.start(peers.get(i).getEndpoint()));
}
// elect leader
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
// apply tasks to leader
this.sendTestTaskAndWait(leader);
{
final ByteBuffer data = ByteBuffer.wrap("no closure".getBytes());
final Task task = new Task(data, null);
leader.apply(task);
}
{
// task with TaskClosure
final ByteBuffer data = ByteBuffer.wrap("task closure".getBytes());
final Vector<String> cbs = new Vector<>();
final CountDownLatch latch = new CountDownLatch(1);
final Task task = new Task(data, new TaskClosure() {
@Override
public void run(final Status status) {
cbs.add("apply");
latch.countDown();
}
@Override
public void onCommitted() {
cbs.add("commit");
}
});
leader.apply(task);
latch.await();
assertEquals(2, cbs.size());
assertEquals("commit", cbs.get(0));
assertEquals("apply", cbs.get(1));
}
cluster.ensureSame(-1);
assertEquals(2, cluster.getFollowers().size());
// transfer the leader to v1 codec peer
assertTrue(leader.transferLeadershipTo(peers.get(2)).isOk());
cluster.waitLeader();
leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(leader.getLeaderId(), peers.get(2));
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
cluster.stopAll();
// start the cluster with v2 codec, should work
final TestCluster newCluster = new TestCluster("unittest", this.dataPath, peers);
for (int i = 0; i < peers.size(); i++) {
assertTrue(newCluster.start(peers.get(i).getEndpoint()));
}
// elect leader
newCluster.waitLeader();
newCluster.ensureSame();
leader = newCluster.getLeader();
assertNotNull(leader);
// apply new tasks
this.sendTestTaskAndWait(leader);
newCluster.ensureSame();
newCluster.stopAll();
}
Aggregations