use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testSetPeer2.
@Test
public void testSetPeer2() 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()));
}
cluster.waitLeader();
// get leader
Node leader = cluster.getLeader();
assertNotNull(leader);
// apply tasks to leader
this.sendTestTaskAndWait(leader);
cluster.ensureSame();
final List<Node> followers = cluster.getFollowers();
assertEquals(2, followers.size());
final PeerId followerPeer1 = followers.get(0).getNodeId().getPeerId();
final Endpoint followerAddr1 = followerPeer1.getEndpoint();
final PeerId followerPeer2 = followers.get(1).getNodeId().getPeerId();
final Endpoint followerAddr2 = followerPeer2.getEndpoint();
LOG.info("Stop and clean follower {}", followerPeer1);
assertTrue(cluster.stop(followerAddr1));
cluster.clean(followerAddr1);
// apply tasks to leader again
this.sendTestTaskAndWait(leader, 10, RaftError.SUCCESS);
// set peer when no quorum die
final Endpoint leaderAddr = leader.getLeaderId().getEndpoint().copy();
LOG.info("Set peers to {}", leaderAddr);
final List<PeerId> newPeers = TestUtils.generatePeers(3);
assertTrue(newPeers.remove(followerPeer1));
LOG.info("Stop and clean follower {}", followerPeer2);
assertTrue(cluster.stop(followerAddr2));
cluster.clean(followerAddr2);
// leader will step-down, become follower
Thread.sleep(2000);
newPeers.clear();
newPeers.add(new PeerId(leaderAddr, 0));
// new peers equal to current conf
assertTrue(leader.resetPeers(new Configuration(peers)).isOk());
// set peer when quorum die
LOG.warn("Set peers to {}", leaderAddr);
assertTrue(leader.resetPeers(new Configuration(newPeers)).isOk());
cluster.waitLeader();
leader = cluster.getLeader();
assertNotNull(leader);
Assert.assertEquals(leaderAddr, leader.getNodeId().getPeerId().getEndpoint());
LOG.info("start follower {}", followerAddr1);
assertTrue(cluster.start(followerAddr1, true, 300));
LOG.info("start follower {}", followerAddr2);
assertTrue(cluster.start(followerAddr2, true, 300));
CountDownLatch latch = new CountDownLatch(1);
LOG.info("Add old follower {}", followerAddr1);
leader.addPeer(followerPeer1, new ExpectClosure(latch));
waitLatch(latch);
latch = new CountDownLatch(1);
LOG.info("Add old follower {}", followerAddr2);
leader.addPeer(followerPeer2, new ExpectClosure(latch));
waitLatch(latch);
newPeers.add(followerPeer1);
newPeers.add(followerPeer2);
cluster.ensureSame();
assertEquals(3, cluster.getFsms().size());
for (final MockStateMachine fsm : cluster.getFsms()) {
assertEquals(20, fsm.getLogs().size());
}
cluster.stopAll();
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method startChangePeersThread.
private Future<?> startChangePeersThread(final ChangeArg arg) {
final Set<RaftError> expectedErrors = new HashSet<>();
expectedErrors.add(RaftError.EBUSY);
expectedErrors.add(RaftError.EPERM);
expectedErrors.add(RaftError.ECATCHUP);
return Utils.runInThread(() -> {
try {
while (!arg.stop) {
arg.c.waitLeader();
final Node leader = arg.c.getLeader();
if (leader == null) {
continue;
}
// select peers in random
final Configuration conf = new Configuration();
if (arg.dontRemoveFirstPeer) {
conf.addPeer(arg.peers.get(0));
}
for (int i = 0; i < arg.peers.size(); i++) {
final boolean select = ThreadLocalRandom.current().nextInt(64) < 32;
if (select && !conf.contains(arg.peers.get(i))) {
conf.addPeer(arg.peers.get(i));
}
}
if (conf.isEmpty()) {
LOG.warn("No peer has been selected");
continue;
}
final SynchronizedClosure done = new SynchronizedClosure();
leader.changePeers(conf, done);
done.await();
assertTrue(done.getStatus().toString(), done.getStatus().isOk() || expectedErrors.contains(done.getStatus().getRaftError()));
}
} catch (final InterruptedException e) {
LOG.error("ChangePeersThread is interrupted", e);
}
});
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class NodeTest method testNoSnapshot.
@Test
public void testNoSnapshot() 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.setRaftMetaUri(this.dataPath + File.separator + "meta");
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);
this.sendTestTaskAndWait(node);
assertEquals(0, fsm.getSaveSnapshotTimes());
// do snapshot but returns error
CountDownLatch latch = new CountDownLatch(1);
node.snapshot(new ExpectClosure(RaftError.EINVAL, "Snapshot is not supported", latch));
waitLatch(latch);
assertEquals(0, fsm.getSaveSnapshotTimes());
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 testSingleNodeWithLearner.
@Test
public void testSingleNodeWithLearner() throws Exception {
final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
final PeerId peer = new PeerId(addr, 0);
final Endpoint learnerAddr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT + 1);
final PeerId learnerPeer = new PeerId(learnerAddr, 0);
NodeManager.getInstance().addAddress(addr);
NodeManager.getInstance().addAddress(learnerAddr);
MockStateMachine learnerFsm = null;
Node learner = null;
RaftGroupService learnerServer = null;
{
// Start learner
final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
learnerFsm = new MockStateMachine(learnerAddr);
nodeOptions.setFsm(learnerFsm);
nodeOptions.setLogUri(this.dataPath + File.separator + "log1");
nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta1");
nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot1");
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
final RpcServer rpcServer = RaftRpcServerFactory.createRaftRpcServer(learnerAddr);
learnerServer = new RaftGroupService("unittest", new PeerId(learnerAddr, 0), nodeOptions, rpcServer);
learner = learnerServer.start();
}
{
// Start leader
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), Collections.singletonList(learnerPeer)));
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()));
}
// wait for entries to be replicated to learner.
Thread.sleep(1000);
node.shutdown();
node.join();
}
{
// assert learner fsm
assertEquals(10, learnerFsm.getLogs().size());
int i = 0;
for (final ByteBuffer data : learnerFsm.getLogs()) {
assertEquals("hello" + i++, new String(data.array()));
}
learnerServer.shutdown();
learnerServer.join();
}
}
use of com.alipay.sofa.jraft.conf.Configuration in project sofa-jraft by sofastack.
the class AtomicRangeGroup method start.
public static AtomicRangeGroup start(StartupConf conf, RpcServer rpcServer) throws IOException {
final NodeOptions nodeOptions = new NodeOptions();
// Set election timeout to 1 second
nodeOptions.setElectionTimeoutMs(1000);
// Close cli service
nodeOptions.setDisableCli(false);
// A snapshot saving would be triggered every 30 seconds
// nodeOptions.setSnapshotIntervalSecs(30);
// Parsing Options
final PeerId serverId = new PeerId();
if (!serverId.parse(conf.getServerAddress())) {
throw new IllegalArgumentException("Fail to parse serverId:" + conf.getServerAddress());
}
final Configuration initConf = new Configuration();
if (!initConf.parse(conf.getConf())) {
throw new IllegalArgumentException("Fail to parse initConf:" + conf.getConf());
}
// Set the initial cluster configuration
nodeOptions.setInitialConf(initConf);
// Startup node
final AtomicRangeGroup node = new AtomicRangeGroup(conf.getDataPath(), conf.getGroupId(), serverId, conf.getMinSlot(), conf.getMaxSlot(), nodeOptions, rpcServer);
LOG.info("Started range node[{}-{}] at port:{}", conf.getMinSlot(), conf.getMaxSlot(), node.getNode().getNodeId().getPeerId().getPort());
return node;
}
Aggregations