Search in sources :

Example 1 with NodeOptions

use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.

the class ItNodeTest method testBootStrapWithSnapshot.

@Test
public void testBootStrapWithSnapshot() throws Exception {
    Endpoint addr = new Endpoint("127.0.0.1", 5006);
    MockStateMachine fsm = new MockStateMachine(addr);
    for (char ch = 'a'; ch <= 'z'; ch++) fsm.getLogs().add(ByteBuffer.wrap(new byte[] { (byte) ch }));
    BootstrapOptions opts = new BootstrapOptions();
    opts.setServiceFactory(new DefaultJRaftServiceFactory());
    opts.setLastLogIndex(fsm.getLogs().size());
    opts.setRaftMetaUri(dataPath + File.separator + "meta");
    opts.setLogUri(dataPath + File.separator + "log");
    opts.setSnapshotUri(dataPath + File.separator + "snapshot");
    opts.setGroupConf(JRaftUtils.getConfiguration("127.0.0.1:5006"));
    opts.setFsm(fsm);
    NodeOptions nodeOpts = createNodeOptions();
    opts.setNodeOptions(nodeOpts);
    assertTrue(JRaftUtils.bootstrap(opts));
    nodeOpts.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOpts.setLogUri(dataPath + File.separator + "log");
    nodeOpts.setSnapshotUri(dataPath + File.separator + "snapshot");
    nodeOpts.setFsm(fsm);
    RaftGroupService service = createService("test", new PeerId(addr, 0), nodeOpts);
    Node node = service.start();
    assertEquals(26, fsm.getLogs().size());
    for (int i = 0; i < 26; i++) assertEquals('a' + i, fsm.getLogs().get(i).get());
    // Group configuration will be restored from snapshot meta.
    while (!node.isLeader()) Thread.sleep(20);
    sendTestTaskAndWait(node);
    assertEquals(36, fsm.getLogs().size());
}
Also used : Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) BootstrapOptions(org.apache.ignite.raft.jraft.option.BootstrapOptions) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 2 with NodeOptions

use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.

the class ItNodeTest method testNodeTaskOverload.

@Test
public void testNodeTaskOverload() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    PeerId peer = new PeerId(addr, 0);
    NodeOptions nodeOptions = createNodeOptions();
    RaftOptions raftOptions = new RaftOptions();
    raftOptions.setDisruptorBufferSize(2);
    nodeOptions.setRaftOptions(raftOptions);
    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)));
    RaftGroupService service = createService("unittest", new PeerId(addr, 0), nodeOptions);
    Node node = service.start();
    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));
    while (!node.isLeader()) ;
    List<Task> tasks = new ArrayList<>();
    AtomicInteger c = new AtomicInteger(0);
    for (int i = 0; i < 10; i++) {
        ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes(UTF_8));
        int finalI = i;
        Task task = new Task(data, new JoinableClosure(status -> {
            LOG.info("{} i={}", status, finalI);
            if (!status.isOk()) {
                assertTrue(status.getRaftError() == RaftError.EBUSY || status.getRaftError() == RaftError.EPERM);
            }
            c.incrementAndGet();
        }));
        node.apply(task);
        tasks.add(task);
    }
    Task.joinAll(tasks, TimeUnit.SECONDS.toMillis(30));
    assertEquals(10, c.get());
}
Also used : SynchronizedClosure(org.apache.ignite.raft.jraft.closure.SynchronizedClosure) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ExponentialBackoffTimeoutStrategy(org.apache.ignite.raft.jraft.util.ExponentialBackoffTimeoutStrategy) TaskClosure(org.apache.ignite.raft.jraft.closure.TaskClosure) SnapshotThrottle(org.apache.ignite.raft.jraft.storage.SnapshotThrottle) Disabled(org.junit.jupiter.api.Disabled) BooleanSupplier(java.util.function.BooleanSupplier) AfterAll(org.junit.jupiter.api.AfterAll) Future(java.util.concurrent.Future) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) Path(java.nio.file.Path) Collections.synchronizedList(java.util.Collections.synchronizedList) Set(java.util.Set) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) ELECTION_TIMEOUT_MILLIS(org.apache.ignite.raft.jraft.core.TestCluster.ELECTION_TIMEOUT_MILLIS) TestUtils(org.apache.ignite.raft.jraft.test.TestUtils) ExecutorServiceHelper(org.apache.ignite.raft.jraft.util.ExecutorServiceHelper) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RaftError(org.apache.ignite.raft.jraft.error.RaftError) RpcServer(org.apache.ignite.raft.jraft.rpc.RpcServer) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) StateMachine(org.apache.ignite.raft.jraft.StateMachine) RaftException(org.apache.ignite.raft.jraft.error.RaftException) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ArrayList(java.util.ArrayList) Task(org.apache.ignite.raft.jraft.entity.Task) IgniteRpcServer(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcServer) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedHashSet(java.util.LinkedHashSet) NodeManager(org.apache.ignite.raft.jraft.NodeManager) LogIndexOutOfBoundsException(org.apache.ignite.raft.jraft.error.LogIndexOutOfBoundsException) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) LogNotFoundException(org.apache.ignite.raft.jraft.error.LogNotFoundException) DefaultRaftClientService(org.apache.ignite.raft.jraft.rpc.impl.core.DefaultRaftClientService) File(java.io.File) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) WorkDirectory(org.apache.ignite.internal.testframework.WorkDirectory) NetworkAddress(org.apache.ignite.network.NetworkAddress) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) AfterEach(org.junit.jupiter.api.AfterEach) ConsoleReporter(com.codahale.metrics.ConsoleReporter) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) EnumOutter(org.apache.ignite.raft.jraft.entity.EnumOutter) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) TestUtils.sender(org.apache.ignite.raft.jraft.test.TestUtils.sender) IgniteLogger(org.apache.ignite.lang.IgniteLogger) ByteBuffer(java.nio.ByteBuffer) ReadOnlyOption(org.apache.ignite.raft.jraft.option.ReadOnlyOption) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NodeFinder(org.apache.ignite.network.NodeFinder) Node(org.apache.ignite.raft.jraft.Node) JoinableClosure(org.apache.ignite.raft.jraft.closure.JoinableClosure) TestIgniteRpcServer(org.apache.ignite.raft.jraft.rpc.TestIgniteRpcServer) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) Status(org.apache.ignite.raft.jraft.Status) BootstrapOptions(org.apache.ignite.raft.jraft.option.BootstrapOptions) TestInfo(org.junit.jupiter.api.TestInfo) Utils(org.apache.ignite.raft.jraft.util.Utils) Test(org.junit.jupiter.api.Test) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) RpcClientEx(org.apache.ignite.raft.jraft.rpc.RpcClientEx) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) ReadIndexClosure(org.apache.ignite.raft.jraft.closure.ReadIndexClosure) Bits(org.apache.ignite.raft.jraft.util.Bits) ClusterServiceTestUtils(org.apache.ignite.utils.ClusterServiceTestUtils) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) FixedThreadsExecutorGroup(org.apache.ignite.raft.jraft.util.concurrent.FixedThreadsExecutorGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) HashSet(java.util.HashSet) BiPredicate(java.util.function.BiPredicate) Iterator(org.apache.ignite.raft.jraft.Iterator) ExecutorService(java.util.concurrent.ExecutorService) JRaftUtils(org.apache.ignite.raft.jraft.JRaftUtils) UTF_8(java.nio.charset.StandardCharsets.UTF_8) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) IgniteRpcClient(org.apache.ignite.raft.jraft.rpc.impl.IgniteRpcClient) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Collectors.toList(java.util.stream.Collectors.toList) ThroughputSnapshotThrottle(org.apache.ignite.raft.jraft.storage.snapshot.ThroughputSnapshotThrottle) ClusterService(org.apache.ignite.network.ClusterService) WorkDirectoryExtension(org.apache.ignite.internal.testframework.WorkDirectoryExtension) UserLog(org.apache.ignite.raft.jraft.entity.UserLog) Collections(java.util.Collections) RaftOptions(org.apache.ignite.raft.jraft.option.RaftOptions) Task(org.apache.ignite.raft.jraft.entity.Task) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftGroupService(org.apache.ignite.raft.jraft.RaftGroupService) Node(org.apache.ignite.raft.jraft.Node) ArrayList(java.util.ArrayList) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ByteBuffer(java.nio.ByteBuffer) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) JoinableClosure(org.apache.ignite.raft.jraft.closure.JoinableClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 3 with NodeOptions

use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.

the class NodeImpl method join.

@Override
public synchronized void join() throws InterruptedException {
    if (this.shutdownLatch != null) {
        if (this.readOnlyService != null) {
            this.readOnlyService.join();
        }
        if (this.logManager != null) {
            this.logManager.join();
        }
        if (this.snapshotExecutor != null) {
            this.snapshotExecutor.join();
        }
        if (this.wakingCandidate != null) {
            Replicator.join(this.wakingCandidate);
        }
        this.shutdownLatch.await();
        this.applyDisruptor.unsubscribe(groupId);
        this.shutdownLatch = null;
    }
    if (this.fsmCaller != null) {
        this.fsmCaller.join();
    }
    // Stop and reset non shared pools.
    NodeOptions opts = getOptions();
    if (opts.getScheduler() != null && !opts.isSharedPools()) {
        opts.getScheduler().shutdown();
        opts.setScheduler(null);
    }
    if (opts.getElectionTimer() != null && !opts.isSharedPools()) {
        opts.getElectionTimer().stop();
        opts.setElectionTimer(null);
    }
    if (opts.getVoteTimer() != null && !opts.isSharedPools()) {
        opts.getVoteTimer().stop();
        opts.setVoteTimer(null);
    }
    if (opts.getStepDownTimer() != null && !opts.isSharedPools()) {
        opts.getStepDownTimer().stop();
        opts.setStepDownTimer(null);
    }
    if (opts.getSnapshotTimer() != null && !opts.isSharedPools()) {
        opts.getSnapshotTimer().stop();
        opts.setSnapshotTimer(null);
    }
    if (opts.getCommonExecutor() != null && !opts.isSharedPools()) {
        ExecutorServiceHelper.shutdownAndAwaitTermination(opts.getCommonExecutor());
        opts.setCommonExecutor(null);
    }
    if (opts.getStripedExecutor() != null && !opts.isSharedPools()) {
        opts.getStripedExecutor().shutdownGracefully();
        opts.setStripedExecutor(null);
    }
    if (opts.getClientExecutor() != null && !opts.isSharedPools()) {
        ExecutorServiceHelper.shutdownAndAwaitTermination(opts.getClientExecutor());
        opts.setClientExecutor(null);
    }
    if (opts.getfSMCallerExecutorDisruptor() != null && !opts.isSharedPools()) {
        opts.getfSMCallerExecutorDisruptor().shutdown();
        opts.setfSMCallerExecutorDisruptor(null);
    }
    if (opts.getNodeApplyDisruptor() != null && !opts.isSharedPools()) {
        opts.getNodeApplyDisruptor().shutdown();
        opts.setNodeApplyDisruptor(null);
    }
    if (opts.getReadOnlyServiceDisruptor() != null && !opts.isSharedPools()) {
        opts.getReadOnlyServiceDisruptor().shutdown();
        opts.setReadOnlyServiceDisruptor(null);
    }
    if (opts.getLogManagerDisruptor() != null && !opts.isSharedPools()) {
        opts.getLogManagerDisruptor().shutdown();
        opts.setLogManagerDisruptor(null);
    }
}
Also used : NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions)

Example 4 with NodeOptions

use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.

the class ItNodeTest method testAutoSnapshot.

@Test
public void testAutoSnapshot() throws Exception {
    Endpoint addr = new Endpoint(TestUtils.getLocalAddress(), TestUtils.INIT_PORT);
    NodeOptions nodeOptions = createNodeOptions();
    MockStateMachine fsm = new MockStateMachine(addr);
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "meta");
    nodeOptions.setSnapshotIntervalSecs(10);
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(new PeerId(addr, 0))));
    RaftGroupService service = createService("unittest", new PeerId(addr, 0), nodeOptions);
    Node node = service.start();
    // wait node elect self as leader
    Thread.sleep(2000);
    sendTestTaskAndWait(node);
    // wait for auto snapshot
    Thread.sleep(10000);
    // first snapshot will be triggered randomly
    int times = fsm.getSaveSnapshotTimes();
    assertTrue(times >= 1, "snapshotTimes=" + times);
    assertTrue(fsm.getSnapshotIndex() > 0);
}
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) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Test(org.junit.jupiter.api.Test)

Example 5 with NodeOptions

use of org.apache.ignite.raft.jraft.option.NodeOptions in project ignite-3 by apache.

the class ItMetaStorageRaftGroupTest method prepareJraftMetaStorages.

private List<Pair<RaftServer, RaftGroupService>> prepareJraftMetaStorages(AtomicInteger replicatorStartedCounter, AtomicInteger replicatorStoppedCounter) throws InterruptedException, ExecutionException {
    List<Peer> peers = new ArrayList<>();
    cluster.forEach(c -> peers.add(new Peer(c.topologyService().localMember().address())));
    assertTrue(cluster.size() > 1);
    NodeOptions opt1 = new NodeOptions();
    opt1.setReplicationStateListeners(List.of(new UserReplicatorStateListener(replicatorStartedCounter, replicatorStoppedCounter)));
    NodeOptions opt2 = new NodeOptions();
    opt2.setReplicationStateListeners(List.of(new UserReplicatorStateListener(replicatorStartedCounter, replicatorStoppedCounter)));
    NodeOptions opt3 = new NodeOptions();
    opt3.setReplicationStateListeners(List.of(new UserReplicatorStateListener(replicatorStartedCounter, replicatorStoppedCounter)));
    metaStorageRaftSrv1 = new JraftServerImpl(cluster.get(0), dataPath, opt1);
    metaStorageRaftSrv2 = new JraftServerImpl(cluster.get(1), dataPath, opt2);
    metaStorageRaftSrv3 = new JraftServerImpl(cluster.get(2), dataPath, opt3);
    metaStorageRaftSrv1.start();
    metaStorageRaftSrv2.start();
    metaStorageRaftSrv3.start();
    metaStorageRaftSrv1.startRaftGroup(METASTORAGE_RAFT_GROUP_NAME, new MetaStorageListener(mockStorage), peers);
    metaStorageRaftSrv2.startRaftGroup(METASTORAGE_RAFT_GROUP_NAME, new MetaStorageListener(mockStorage), peers);
    metaStorageRaftSrv3.startRaftGroup(METASTORAGE_RAFT_GROUP_NAME, new MetaStorageListener(mockStorage), peers);
    metaStorageRaftGrpSvc1 = RaftGroupServiceImpl.start(METASTORAGE_RAFT_GROUP_NAME, cluster.get(0), FACTORY, 10_000, peers, true, 200, executor).get();
    metaStorageRaftGrpSvc2 = RaftGroupServiceImpl.start(METASTORAGE_RAFT_GROUP_NAME, cluster.get(1), FACTORY, 10_000, peers, true, 200, executor).get();
    metaStorageRaftGrpSvc3 = RaftGroupServiceImpl.start(METASTORAGE_RAFT_GROUP_NAME, cluster.get(2), FACTORY, 10_000, peers, true, 200, executor).get();
    assertTrue(TestUtils.waitForCondition(() -> sameLeaders(metaStorageRaftGrpSvc1, metaStorageRaftGrpSvc2, metaStorageRaftGrpSvc3), 10_000), "Leaders: " + metaStorageRaftGrpSvc1.leader() + " " + metaStorageRaftGrpSvc2.leader() + " " + metaStorageRaftGrpSvc3.leader());
    List<Pair<RaftServer, RaftGroupService>> raftServersRaftGroups = new ArrayList<>();
    raftServersRaftGroups.add(new Pair<>(metaStorageRaftSrv1, metaStorageRaftGrpSvc1));
    raftServersRaftGroups.add(new Pair<>(metaStorageRaftSrv2, metaStorageRaftGrpSvc2));
    raftServersRaftGroups.add(new Pair<>(metaStorageRaftSrv3, metaStorageRaftGrpSvc3));
    return raftServersRaftGroups;
}
Also used : Peer(org.apache.ignite.raft.client.Peer) ArrayList(java.util.ArrayList) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) MetaStorageListener(org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener)

Aggregations

NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)34 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)17 Test (org.junit.jupiter.api.Test)14 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)13 RaftGroupService (org.apache.ignite.raft.jraft.RaftGroupService)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 Node (org.apache.ignite.raft.jraft.Node)10 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)8 RaftOptions (org.apache.ignite.raft.jraft.option.RaftOptions)8 ExecutorService (java.util.concurrent.ExecutorService)5 NodeId (org.apache.ignite.raft.jraft.entity.NodeId)4 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ClusterService (org.apache.ignite.network.ClusterService)3 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 SynchronizedClosure (org.apache.ignite.raft.jraft.closure.SynchronizedClosure)3 BootstrapOptions (org.apache.ignite.raft.jraft.option.BootstrapOptions)3 File (java.io.File)2 Path (java.nio.file.Path)2