Search in sources :

Example 11 with RaftGroupService

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();
    }
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 12 with RaftGroupService

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;
}
Also used : TestIgniteRpcServer(org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) IgniteRpcServer(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcServer) TestIgniteRpcServer(org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer) NodeManager(org.apache.ignite.raft.jraft.NodeManager) ClusterService(org.apache.ignite.network.ClusterService) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutorService(java.util.concurrent.ExecutorService) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 13 with RaftGroupService

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());
}
Also used : RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 14 with RaftGroupService

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;
}
Also used : RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService)

Example 15 with RaftGroupService

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;
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) NetworkAddress(org.apache.ignite.network.NetworkAddress) IgniteInternalException(org.apache.ignite.lang.IgniteInternalException) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) IOException(java.io.IOException) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Aggregations

RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)16 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)13 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)11 Node (org.apache.ignite.raft.jraft.Node)9 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)9 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)9 Test (org.junit.jupiter.api.Test)9 ClusterService (org.apache.ignite.network.ClusterService)4 NetworkAddress (org.apache.ignite.network.NetworkAddress)4 ByteBuffer (java.nio.ByteBuffer)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 BootstrapOptions (org.apache.ignite.raft.jraft.option.BootstrapOptions)3 TestIgniteRpcServer (org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer)3 IgniteRpcClient (org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient)3 File (java.io.File)2 Path (java.nio.file.Path)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)2 TestScaleCubeClusterServiceFactory (org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory)2