use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testReadIndexChaos.
@Test
public void testReadIndexChaos() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(3);
cluster = new TestCluster("unittest", dataPath, peers, testInfo);
for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint(), false, 300, true));
// elect leader
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
assertEquals(3, leader.listPeers().size());
CountDownLatch latch = new CountDownLatch(10);
ExecutorService executor = Executors.newFixedThreadPool(10);
executors.add(executor);
for (int i = 0; i < 10; i++) {
executor.submit(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
try {
for (int i = 0; i < 100; i++) {
try {
sendTestTaskAndWait(leader);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
readIndexRandom(cluster);
}
} finally {
latch.countDown();
}
}
private void readIndexRandom(TestCluster cluster) {
CountDownLatch readLatch = new CountDownLatch(1);
byte[] requestContext = TestUtils.getRandomBytes();
cluster.getNodes().get(ThreadLocalRandom.current().nextInt(3)).readIndex(requestContext, new ReadIndexClosure() {
@Override
public void run(Status status, long index, byte[] reqCtx) {
if (status.isOk()) {
assertTrue(status.isOk(), status.toString());
assertTrue(index > 0);
assertArrayEquals(requestContext, reqCtx);
}
readLatch.countDown();
}
});
try {
readLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
}
latch.await();
cluster.ensureSame();
for (MockStateMachine fsm : cluster.getFsms()) assertEquals(10000, fsm.getLogs().size());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testInstallLargeSnapshot.
@Test
public void testInstallLargeSnapshot() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(4);
cluster = new TestCluster("unitest", dataPath, peers.subList(0, 3), testInfo);
for (int i = 0; i < peers.size() - 1; i++) {
PeerId peer = peers.get(i);
boolean started = cluster.start(peer.getEndpoint(), false, 200, false);
assertTrue(started);
}
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
cluster.ensureLeader(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
PeerId newPeer = peers.get(3);
RaftOptions raftOptions = new RaftOptions();
raftOptions.setMaxByteCountPerRpc(128);
boolean started = cluster.start(newPeer.getEndpoint(), false, 300, false, null, raftOptions);
assertTrue(started);
CountDownLatch latch = new CountDownLatch(1);
leader.addPeer(newPeer, status -> {
assertTrue(status.isOk(), status.toString());
latch.countDown();
});
waitLatch(latch);
cluster.ensureSame();
assertEquals(4, cluster.getFsms().size());
for (MockStateMachine fsm : cluster.getFsms()) assertEquals(2000, fsm.getLogs().size());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testChangePeersChaosApplyTasks.
@Test
public void testChangePeersChaosApplyTasks() throws Exception {
// start cluster
List<PeerId> peers = new ArrayList<>();
peers.add(new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT));
cluster = new TestCluster("unittest", dataPath, peers, ELECTION_TIMEOUT_MILLIS, testInfo);
assertTrue(cluster.start(peers.get(0).getEndpoint(), false, 100000));
// start other peers
for (int i = 1; i < 10; i++) {
PeerId peer = new PeerId(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + i);
peers.add(peer);
assertTrue(cluster.start(peer.getEndpoint(), false, 100000));
}
final int threads = 3;
List<ChangeArg> args = new ArrayList<>();
List<Future<?>> futures = new ArrayList<>();
CountDownLatch latch = new CountDownLatch(threads);
ExecutorService executor = Executors.newFixedThreadPool(threads);
executors.add(executor);
for (int t = 0; t < threads; t++) {
ChangeArg arg = new ChangeArg(cluster, peers, false, true);
args.add(arg);
futures.add(startChangePeersThread(arg));
Utils.runInThread(executor, () -> {
try {
for (int i = 0; i < 5000; ) {
cluster.waitLeader();
Node leader = cluster.getLeader();
if (leader == null)
continue;
SynchronizedClosure done = new SynchronizedClosure();
Task task = new Task(ByteBuffer.wrap(("hello" + i).getBytes(UTF_8)), done);
leader.apply(task);
Status status = done.await();
if (status.isOk()) {
if (++i % 100 == 0)
System.out.println("Progress:" + i);
} else
assertEquals(RaftError.EPERM, status.getRaftError());
}
} catch (Exception e) {
LOG.error("Failed to run tasks", e);
} finally {
latch.countDown();
}
});
}
latch.await();
for (ChangeArg arg : args) arg.stop = true;
for (Future<?> future : futures) future.get();
cluster.waitLeader();
SynchronizedClosure done = new SynchronizedClosure();
Node leader = cluster.getLeader();
leader.changePeers(new Configuration(peers), done);
assertTrue(done.await().isOk());
cluster.ensureSame();
assertEquals(10, cluster.getFsms().size());
for (MockStateMachine fsm : cluster.getFsms()) {
int logSize = fsm.getLogs().size();
assertTrue(logSize >= 5000 * threads, "logSize= " + logSize);
assertTrue(logSize - 5000 * threads < 100, "logSize= " + logSize);
}
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testRemoveFollower.
@Test
public void testRemoveFollower() throws Exception {
List<PeerId> peers = TestUtils.generatePeers(3);
cluster = new TestCluster("unittest", dataPath, peers, testInfo);
for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint()));
// elect leader
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
cluster.ensureLeader(leader);
// apply tasks to leader
sendTestTaskAndWait(leader);
cluster.ensureSame();
List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
PeerId followerPeer = followers.get(0).getNodeId().getPeerId();
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);
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 (MockStateMachine fsm : cluster.getFsms()) assertEquals(20, fsm.getLogs().size());
}
use of org.apache.ignite.raft.jraft.Node in project ignite-3 by apache.
the class ItNodeTest method testNoLeaderWithZeroValPriorityElection.
@Test
public void testNoLeaderWithZeroValPriorityElection() throws Exception {
List<Integer> priorities = new ArrayList<>();
priorities.add(0);
priorities.add(0);
priorities.add(0);
List<PeerId> peers = TestUtils.generatePriorityPeers(3, priorities);
cluster = new TestCluster("unittest", dataPath, peers, testInfo);
for (PeerId peer : peers) assertTrue(cluster.start(peer.getEndpoint(), peer.getPriority()));
Thread.sleep(200);
List<Node> followers = cluster.getFollowers();
assertEquals(3, followers.size());
for (Node follower : followers) assertEquals(0, follower.getNodeId().getPeerId().getPriority());
}
Aggregations