use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testShutdownAndJoinWorkAfterInitFails.
@Test
public void testShutdownAndJoinWorkAfterInitFails() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
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.setSnapshotUri(this.dataPath + File.separator + "snapshot");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
nodeOptions.setSnapshotIntervalSecs(10);
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
assertTrue(node.init(nodeOptions));
Thread.sleep(1000);
this.sendTestTaskAndWait(node);
// save snapshot
final CountDownLatch latch = new CountDownLatch(1);
node.snapshot(new ExpectClosure(latch));
waitLatch(latch);
node.shutdown();
node.join();
}
{
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
final MockStateMachine fsm = new MockFSM1(addr);
nodeOptions.setFsm(fsm);
nodeOptions.setLogUri(this.dataPath + File.separator + "log");
nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
nodeOptions.setSnapshotIntervalSecs(10);
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
assertFalse(node.init(nodeOptions));
node.shutdown();
node.join();
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testAutoSnapshot.
@Test
public void testAutoSnapshot() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
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.setSnapshotUri(this.dataPath + File.separator + "snapshot");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
nodeOptions.setSnapshotIntervalSecs(10);
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
final Node node = new NodeImpl("unittest", new PeerId(addr, 0));
assertTrue(node.init(nodeOptions));
// wait node elect self as leader
Thread.sleep(2000);
sendTestTaskAndWait(node);
// wait for auto snapshot
Thread.sleep(10000);
// first snapshot will be triggered randomly
final int times = fsm.getSaveSnapshotTimes();
assertTrue("snapshotTimes=" + times, times >= 1);
assertTrue(fsm.getSnapshotIndex() > 0);
final CountDownLatch latch = new CountDownLatch(1);
node.shutdown(new ExpectClosure(latch));
node.join();
waitLatch(latch);
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testSetPeer1.
@Test
public void testSetPeer1() throws Exception {
final TestCluster cluster = new TestCluster("testSetPeer1", this.dataPath, new ArrayList<>());
final PeerId bootPeer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
assertTrue(cluster.start(bootPeer.getEndpoint()));
final List<Node> nodes = cluster.getFollowers();
assertEquals(1, nodes.size());
final List<PeerId> peers = new ArrayList<>();
peers.add(bootPeer);
// reset peers from empty
assertTrue(nodes.get(0).resetPeers(new Configuration(peers)).isOk());
cluster.waitLeader();
assertNotNull(cluster.getLeader());
cluster.stopAll();
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testRollbackStateMachineWithReadIndex_Issue317.
/**
* Test rollback stateMachine with readIndex for issue 317:
* https://github.com/sofastack/sofa-jraft/issues/317
*/
@Test
public void testRollbackStateMachineWithReadIndex_Issue317() 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 CountDownLatch applyCompleteLatch = new CountDownLatch(1);
final CountDownLatch applyLatch = new CountDownLatch(1);
final CountDownLatch readIndexLatch = new CountDownLatch(1);
final AtomicInteger currentValue = new AtomicInteger(-1);
final String errorMsg = this.testName.getMethodName();
final StateMachine fsm = new StateMachineAdapter() {
@Override
public void onApply(final Iterator iter) {
// Notify that the #onApply is preparing to go.
readIndexLatch.countDown();
// Wait for submitting a read-index request
try {
applyLatch.await();
} catch (InterruptedException e) {
fail();
}
int i = 0;
while (iter.hasNext()) {
byte[] data = iter.next().array();
int v = Bits.getInt(data, 0);
assertEquals(i++, v);
currentValue.set(v);
}
if (i > 0) {
// rollback
currentValue.set(i - 1);
iter.setErrorAndRollback(1, new Status(-1, errorMsg));
applyCompleteLatch.countDown();
}
}
};
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()) {
;
}
int n = 5;
{
// apply tasks
for (int i = 0; i < n; i++) {
byte[] b = new byte[4];
Bits.putInt(b, 0, i);
node.apply(new Task(ByteBuffer.wrap(b), null));
}
}
final AtomicInteger readIndexSuccesses = new AtomicInteger(0);
{
// Submit a read-index, wait for #onApply
readIndexLatch.await();
final CountDownLatch latch = new CountDownLatch(1);
node.readIndex(null, new ReadIndexClosure() {
@Override
public void run(final Status status, final long index, final byte[] reqCtx) {
try {
if (status.isOk()) {
readIndexSuccesses.incrementAndGet();
} else {
assertTrue("Unexpected status: " + status, status.getErrorMsg().contains(errorMsg) || status.getRaftError() == RaftError.ETIMEDOUT || status.getErrorMsg().contains("Invalid state for readIndex: STATE_ERROR"));
}
} finally {
latch.countDown();
}
}
});
// We have already submit a read-index request,
// notify #onApply can go right now
applyLatch.countDown();
// The state machine is in error state, the node should step down.
while (node.isLeader()) {
Thread.sleep(10);
}
latch.await();
applyCompleteLatch.await();
}
// No read-index request succeed.
assertEquals(0, readIndexSuccesses.get());
assertTrue(n - 1 >= currentValue.get());
node.shutdown();
node.join();
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testChangePeers.
@Test
public void testChangePeers() throws Exception {
final PeerId peer0 = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT);
final TestCluster cluster = new TestCluster("testChangePeers", this.dataPath, Collections.singletonList(peer0));
assertTrue(cluster.start(peer0.getEndpoint()));
cluster.waitLeader();
Node leader = cluster.getLeader();
this.sendTestTaskAndWait(leader);
for (int i = 1; i < 10; i++) {
final PeerId peer = new PeerId(TestUtils.getMyIp(), TestUtils.INIT_PORT + i);
assertTrue(cluster.start(peer.getEndpoint(), true, 300));
}
for (int i = 0; i < 9; i++) {
cluster.waitLeader();
leader = cluster.getLeader();
assertNotNull(leader);
PeerId peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + i);
Assert.assertEquals(peer, leader.getNodeId().getPeerId());
peer = new PeerId(TestUtils.getMyIp(), peer0.getEndpoint().getPort() + i + 1);
final SynchronizedClosure done = new SynchronizedClosure();
leader.changePeers(new Configuration(Collections.singletonList(peer)), done);
assertTrue(done.await().isOk());
}
assertTrue(cluster.ensureSame());
cluster.stopAll();
}
Aggregations