Search in sources :

Example 1 with JraftServerImpl

use of org.apache.ignite.internal.raft.server.impl.JraftServerImpl in project ignite-3 by apache.

the class ItJraftCounterServerTest method after.

/**
 * After each.
 */
@AfterEach
@Override
protected void after() throws Exception {
    super.after();
    LOG.info("Start client shutdown");
    Iterator<RaftGroupService> iterClients = clients.iterator();
    while (iterClients.hasNext()) {
        RaftGroupService client = iterClients.next();
        iterClients.remove();
        client.shutdown();
    }
    LOG.info("Start server shutdown servers={}", servers.size());
    Iterator<JraftServerImpl> iterSrv = servers.iterator();
    while (iterSrv.hasNext()) {
        JraftServerImpl server = iterSrv.next();
        iterSrv.remove();
        Set<String> grps = server.startedGroups();
        for (String grp : grps) {
            server.stopRaftGroup(grp);
        }
        server.beforeNodeStop();
        server.stop();
    }
    IgniteUtils.shutdownAndAwaitTermination(executor, 10, TimeUnit.SECONDS);
    TestUtils.assertAllJraftThreadsStopped();
    LOG.info(">>>>>>>>>>>>>>> End test method: {}", testInfo.getTestMethod().orElseThrow().getName());
}
Also used : RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) AfterEach(org.junit.jupiter.api.AfterEach)

Example 2 with JraftServerImpl

use of org.apache.ignite.internal.raft.server.impl.JraftServerImpl 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)

Example 3 with JraftServerImpl

use of org.apache.ignite.internal.raft.server.impl.JraftServerImpl in project ignite-3 by apache.

the class ItJraftCounterServerTest method testCreateSnapshot.

@Test
public void testCreateSnapshot() throws Exception {
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    JraftServerImpl server = servers.get(0);
    long val = applyIncrements(client1, 1, 10);
    assertEquals(sum(10), val);
    client1.snapshot(server.localPeer(COUNTER_GROUP_0)).get();
    long val2 = applyIncrements(client2, 1, 20);
    assertEquals(sum(20), val2);
    client2.snapshot(server.localPeer(COUNTER_GROUP_1)).get();
    Path snapshotDir0 = server.getServerDataPath(COUNTER_GROUP_0).resolve("snapshot");
    assertEquals(1L, countFiles(snapshotDir0));
    Path snapshotDir1 = server.getServerDataPath(COUNTER_GROUP_1).resolve("snapshot");
    assertEquals(1L, countFiles(snapshotDir1));
}
Also used : Path(java.nio.file.Path) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) Test(org.junit.jupiter.api.Test)

Example 4 with JraftServerImpl

use of org.apache.ignite.internal.raft.server.impl.JraftServerImpl in project ignite-3 by apache.

the class ItJraftCounterServerTest method doTestFollowerCatchUp.

/**
 * Do test follower catch up.
 *
 * @param snapshot {@code True} to create snapshot on leader and truncate log.
 * @param cleanDir {@code True} to clean persistent state on follower before restart.
 * @throws Exception If failed.
 */
private void doTestFollowerCatchUp(boolean snapshot, boolean cleanDir) throws Exception {
    startCluster();
    RaftGroupService client1 = clients.get(0);
    RaftGroupService client2 = clients.get(1);
    client1.refreshLeader().get();
    client2.refreshLeader().get();
    Peer leader1 = client1.leader();
    assertNotNull(leader1);
    Peer leader2 = client2.leader();
    assertNotNull(leader2);
    applyIncrements(client1, 0, 10);
    applyIncrements(client2, 0, 20);
    // First snapshot will not truncate logs.
    client1.snapshot(leader1).get();
    client2.snapshot(leader2).get();
    JraftServerImpl toStop = null;
    // Find the follower for both groups.
    for (JraftServerImpl server : servers) {
        Peer peer = server.localPeer(COUNTER_GROUP_0);
        if (!peer.equals(leader1) && !peer.equals(leader2)) {
            toStop = server;
            break;
        }
    }
    Path serverDataPath0 = toStop.getServerDataPath(COUNTER_GROUP_0);
    Path serverDataPath1 = toStop.getServerDataPath(COUNTER_GROUP_1);
    final int stopIdx = servers.indexOf(toStop);
    toStop.stopRaftGroup(COUNTER_GROUP_0);
    toStop.stopRaftGroup(COUNTER_GROUP_1);
    toStop.beforeNodeStop();
    toStop.stop();
    applyIncrements(client1, 11, 20);
    applyIncrements(client2, 21, 30);
    if (snapshot) {
        client1.snapshot(leader1).get();
        client2.snapshot(leader2).get();
    }
    if (cleanDir) {
        IgniteUtils.deleteIfExists(serverDataPath0);
        IgniteUtils.deleteIfExists(serverDataPath1);
    }
    var svc2 = startServer(stopIdx, r -> {
        r.startRaftGroup(COUNTER_GROUP_0, listenerFactory.get(), INITIAL_CONF);
        r.startRaftGroup(COUNTER_GROUP_1, listenerFactory.get(), INITIAL_CONF);
    }, opts -> {
    });
    waitForCondition(() -> validateStateMachine(sum(20), svc2, COUNTER_GROUP_0), 5_000);
    waitForCondition(() -> validateStateMachine(sum(30), svc2, COUNTER_GROUP_1), 5_000);
    svc2.stopRaftGroup(COUNTER_GROUP_0);
    svc2.stopRaftGroup(COUNTER_GROUP_1);
    svc2.beforeNodeStop();
    svc2.stop();
    var svc3 = startServer(stopIdx, r -> {
        r.startRaftGroup(COUNTER_GROUP_0, listenerFactory.get(), INITIAL_CONF);
        r.startRaftGroup(COUNTER_GROUP_1, listenerFactory.get(), INITIAL_CONF);
    }, opts -> {
    });
    waitForCondition(() -> validateStateMachine(sum(20), svc3, COUNTER_GROUP_0), 5_000);
    waitForCondition(() -> validateStateMachine(sum(30), svc3, COUNTER_GROUP_1), 5_000);
}
Also used : Path(java.nio.file.Path) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl)

Example 5 with JraftServerImpl

use of org.apache.ignite.internal.raft.server.impl.JraftServerImpl in project ignite-3 by apache.

the class ItJraftCounterServerTest method startServer.

/**
 * Starts server.
 *
 * @param idx The index.
 * @param clo Init closure.
 * @param cons Node options updater.
 *
 * @return Raft server instance.
 */
private JraftServerImpl startServer(int idx, Consumer<RaftServer> clo, Consumer<NodeOptions> cons) {
    var addr = new NetworkAddress(getLocalAddress(), PORT);
    ClusterService service = clusterService(PORT + idx, List.of(addr), true);
    NodeOptions opts = new NodeOptions();
    cons.accept(opts);
    JraftServerImpl server = new JraftServerImpl(service, dataPath, opts) {

        @Override
        public void stop() {
            servers.remove(this);
            super.stop();
            service.stop();
        }
    };
    server.start();
    clo.accept(server);
    servers.add(server);
    assertTrue(waitForTopology(service, servers.size(), 15_000));
    return server;
}
Also used : ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl)

Aggregations

JraftServerImpl (org.apache.ignite.internal.raft.server.impl.JraftServerImpl)11 Path (java.nio.file.Path)4 ClusterService (org.apache.ignite.network.ClusterService)4 NetworkAddress (org.apache.ignite.network.NetworkAddress)3 Peer (org.apache.ignite.raft.client.Peer)3 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)3 ArrayList (java.util.ArrayList)2 ClusterNode (org.apache.ignite.network.ClusterNode)2 NodeOptions (org.apache.ignite.raft.jraft.option.NodeOptions)2 AfterEach (org.junit.jupiter.api.AfterEach)2 Test (org.junit.jupiter.api.Test)2 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 TimeoutException (java.util.concurrent.TimeoutException)1