use of com.alipay.sofa.jraft.conf.Configuration 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.conf.Configuration 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.conf.Configuration 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.conf.Configuration 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.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testChangePeersStepsDownInJointConsensus.
@Test
public void testChangePeersStepsDownInJointConsensus() throws Exception {
final List<PeerId> peers = new ArrayList<>();
final PeerId peer0 = JRaftUtils.getPeerId("127.0.0.1:5006");
final PeerId peer1 = JRaftUtils.getPeerId("127.0.0.1:5007");
final PeerId peer2 = JRaftUtils.getPeerId("127.0.0.1:5008");
final PeerId peer3 = JRaftUtils.getPeerId("127.0.0.1:5009");
// start single cluster
peers.add(peer0);
final TestCluster cluster = new TestCluster("testChangePeersStepsDownInJointConsensus", this.dataPath, peers);
assertTrue(cluster.start(peer0.getEndpoint()));
cluster.waitLeader();
Node leader = cluster.getLeader();
assertNotNull(leader);
this.sendTestTaskAndWait(leader);
// start peer1-3
assertTrue(cluster.start(peer1.getEndpoint()));
assertTrue(cluster.start(peer2.getEndpoint()));
assertTrue(cluster.start(peer3.getEndpoint()));
final Configuration conf = new Configuration();
conf.addPeer(peer0);
conf.addPeer(peer1);
conf.addPeer(peer2);
conf.addPeer(peer3);
// change peers
final SynchronizedClosure done = new SynchronizedClosure();
leader.changePeers(conf, done);
assertTrue(done.await().isOk());
// stop peer3
assertTrue(cluster.stop(peer3.getEndpoint()));
conf.removePeer(peer0);
conf.removePeer(peer1);
// Change peers to [peer2, peer3], which must fail since peer3 is stopped
done.reset();
leader.changePeers(conf, done);
Assert.assertEquals(RaftError.EPERM, done.await().getRaftError());
LOG.info(done.getStatus().toString());
assertFalse(((NodeImpl) leader).getConf().isStable());
leader = cluster.getLeader();
assertNull(leader);
assertTrue(cluster.start(peer3.getEndpoint()));
Thread.sleep(1000);
cluster.waitLeader();
leader = cluster.getLeader();
final List<PeerId> thePeers = leader.listPeers();
assertTrue(thePeers.size() > 0);
assertEquals(conf.getPeerSet(), new HashSet<>(thePeers));
cluster.stopAll();
}
Aggregations