use of org.apache.ignite.raft.jraft.RaftGroupService in project ignite-3 by apache.
the class ItNodeTest method testSingleNodeWithLearner.
@Test
public void testSingleNodeWithLearner() throws Exception {
Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
PeerId peer = new PeerId(addr, 0);
Endpoint learnerAddr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT + 1);
PeerId learnerPeer = new PeerId(learnerAddr, 0);
final int cnt = 10;
MockStateMachine learnerFsm;
RaftGroupService learnerServer;
{
// Start learner
NodeOptions nodeOptions = createNodeOptions();
learnerFsm = new MockStateMachine(learnerAddr);
nodeOptions.setFsm(learnerFsm);
nodeOptions.setLogUri(dataPath + File.separator + "log1");
nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta1");
nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot1");
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
learnerServer = createService("unittest", new PeerId(learnerAddr, 0), nodeOptions);
learnerServer.start();
}
{
// Start leader
NodeOptions nodeOptions = createNodeOptions();
MockStateMachine fsm = new MockStateMachine(addr);
nodeOptions.setFsm(fsm);
nodeOptions.setLogUri(dataPath + File.separator + "log");
nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer), Collections.singletonList(learnerPeer)));
RaftGroupService server = createService("unittest", new PeerId(addr, 0), nodeOptions);
Node node = server.start();
assertEquals(1, node.listPeers().size());
assertTrue(node.listPeers().contains(peer));
assertTrue(waitForCondition(() -> node.isLeader(), 1_000));
sendTestTaskAndWait(node, cnt);
assertEquals(cnt, fsm.getLogs().size());
int i = 0;
for (ByteBuffer data : fsm.getLogs()) assertEquals("hello" + i++, stringFromBytes(data.array()));
// wait for entries to be replicated to learner.
Thread.sleep(1000);
server.shutdown();
}
{
// assert learner fsm
assertEquals(cnt, learnerFsm.getLogs().size());
int i = 0;
for (ByteBuffer data : learnerFsm.getLogs()) assertEquals("hello" + i++, stringFromBytes(data.array()));
learnerServer.shutdown();
}
}
use of org.apache.ignite.raft.jraft.RaftGroupService in project ignite-3 by apache.
the class ItNodeTest method createService.
/**
* @param groupId Group id.
* @param peerId Peer id.
* @param nodeOptions Node options.
* @return Raft group service.
*/
private RaftGroupService createService(String groupId, PeerId peerId, NodeOptions nodeOptions) {
Configuration initialConf = nodeOptions.getInitialConf();
nodeOptions.setStripes(1);
Stream<PeerId> peers = initialConf == null ? Stream.empty() : Stream.concat(initialConf.getPeers().stream(), initialConf.getLearners().stream());
List<NetworkAddress> addressList = peers.map(PeerId::getEndpoint).map(JRaftUtils::addressFromEndpoint).collect(toList());
var nodeManager = new NodeManager();
ClusterService clusterService = ClusterServiceTestUtils.clusterService(testInfo, peerId.getEndpoint().getPort(), new StaticNodeFinder(addressList), new TestScaleCubeClusterServiceFactory());
ExecutorService requestExecutor = JRaftUtils.createRequestExecutor(nodeOptions);
executors.add(requestExecutor);
IgniteRpcServer rpcServer = new TestIgniteRpcServer(clusterService, nodeManager, nodeOptions, requestExecutor);
nodeOptions.setRpcClient(new IgniteRpcClient(clusterService));
clusterService.start();
var service = new RaftGroupService(groupId, peerId, nodeOptions, rpcServer, nodeManager) {
@Override
public synchronized void shutdown() {
rpcServer.shutdown();
super.shutdown();
clusterService.stop();
}
};
services.add(service);
return service;
}
use of org.apache.ignite.raft.jraft.RaftGroupService in project ignite-3 by apache.
the class JraftServerImpl method localPeer.
/**
* {@inheritDoc}
*/
@Override
public Peer localPeer(String groupId) {
RaftGroupService service = groups.get(groupId);
if (service == null) {
return null;
}
PeerId peerId = service.getRaftNode().getNodeId().getPeerId();
return new Peer(addressFromEndpoint(peerId.getEndpoint()), peerId.getPriority());
}
use of org.apache.ignite.raft.jraft.RaftGroupService in project ignite-3 by apache.
the class JraftServerImpl method stopRaftGroup.
/**
* {@inheritDoc}
*/
@Override
public boolean stopRaftGroup(String groupId) {
RaftGroupService svc = groups.remove(groupId);
boolean stopped = svc != null;
if (stopped) {
svc.shutdown();
}
return stopped;
}
use of org.apache.ignite.raft.jraft.RaftGroupService in project ignite-3 by apache.
the class JraftServerImpl method startRaftGroup.
/**
* {@inheritDoc}
*/
@Override
public synchronized boolean startRaftGroup(String groupId, RaftGroupListener lsnr, @Nullable List<Peer> initialConf) {
if (groups.containsKey(groupId)) {
return false;
}
// Thread pools are shared by all raft groups.
NodeOptions nodeOptions = opts.copy();
Path serverDataPath = getServerDataPath(groupId);
try {
Files.createDirectories(serverDataPath);
} catch (IOException e) {
throw new IgniteInternalException(e);
}
nodeOptions.setLogUri(serverDataPath.resolve("logs").toString());
nodeOptions.setRaftMetaUri(serverDataPath.resolve("meta").toString());
nodeOptions.setSnapshotUri(serverDataPath.resolve("snapshot").toString());
nodeOptions.setFsm(new DelegatingStateMachine(lsnr));
if (initialConf != null) {
List<PeerId> mapped = initialConf.stream().map(PeerId::fromPeer).collect(Collectors.toList());
nodeOptions.setInitialConf(new Configuration(mapped, null));
}
IgniteRpcClient client = new IgniteRpcClient(service);
nodeOptions.setRpcClient(client);
NetworkAddress addr = service.topologyService().localMember().address();
var peerId = new PeerId(addr.host(), addr.port(), 0, ElectionPriority.DISABLED);
var server = new RaftGroupService(groupId, peerId, nodeOptions, rpcServer, nodeManager);
server.start();
groups.put(groupId, server);
return true;
}
Aggregations