Search in sources :

Example 16 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.

the class AffinityServiceTest method testCalculatedAssignmentHappyPath.

@Test
public void testCalculatedAssignmentHappyPath() {
    List<List<ClusterNode>> assignments = AffinityUtils.calculateAssignments(Arrays.asList(new ClusterNode(UUID.randomUUID().toString(), "node0", new NetworkAddress("localhost", 8080)), new ClusterNode(UUID.randomUUID().toString(), "node1", new NetworkAddress("localhost", 8081))), 10, 3);
    assertEquals(10, assignments.size());
    for (List<ClusterNode> partitionAssignment : assignments) {
        assertEquals(2, partitionAssignment.size());
    }
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) NetworkAddress(org.apache.ignite.network.NetworkAddress) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 17 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.

the class ItMetaStorageRaftGroupTest method beforeTest.

/**
 * Run {@code NODES} cluster nodes.
 */
@BeforeEach
public void beforeTest(TestInfo testInfo) {
    List<NetworkAddress> localAddresses = findLocalAddresses(NODE_PORT_BASE, NODE_PORT_BASE + NODES);
    var nodeFinder = new StaticNodeFinder(localAddresses);
    localAddresses.stream().map(addr -> ClusterServiceTestUtils.clusterService(testInfo, addr.port(), nodeFinder, NETWORK_FACTORY)).forEach(clusterService -> {
        clusterService.start();
        cluster.add(clusterService);
    });
    for (ClusterService node : cluster) {
        assertTrue(waitForTopology(node, NODES, 1000));
    }
    LOG.info("Cluster started.");
    executor = new ScheduledThreadPoolExecutor(20, new NamedThreadFactory(Loza.CLIENT_POOL_NAME));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) Path(java.nio.file.Path) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) KeyValueStorage(org.apache.ignite.internal.metastorage.server.KeyValueStorage) Cursor(org.apache.ignite.internal.util.Cursor) Status(org.apache.ignite.raft.jraft.Status) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Collectors(java.util.stream.Collectors) TestUtils(org.apache.ignite.raft.jraft.test.TestUtils) TestInfo(org.junit.jupiter.api.TestInfo) RaftServer(org.apache.ignite.internal.raft.server.RaftServer) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) MetaStorageListener(org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Replicator(org.apache.ignite.raft.jraft.core.Replicator) NotNull(org.jetbrains.annotations.NotNull) RaftGroupServiceImpl(org.apache.ignite.raft.jraft.rpc.impl.RaftGroupServiceImpl) ClusterServiceTestUtils.findLocalAddresses(org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ClusterServiceTestUtils(org.apache.ignite.utils.ClusterServiceTestUtils) Mock(org.mockito.Mock) Loza(org.apache.ignite.internal.raft.Loza) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ArrayList(java.util.ArrayList) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) TestUtils.waitForTopology(org.apache.ignite.raft.jraft.test.TestUtils.waitForTopology) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Iterator(java.util.Iterator) ByteArray(org.apache.ignite.lang.ByteArray) Mockito.when(org.mockito.Mockito.when) WorkDirectory(org.apache.ignite.internal.testframework.WorkDirectory) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Peer(org.apache.ignite.raft.client.Peer) AfterEach(org.junit.jupiter.api.AfterEach) ClusterService(org.apache.ignite.network.ClusterService) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) WorkDirectoryExtension(org.apache.ignite.internal.testframework.WorkDirectoryExtension) ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.

the class ItMetaStorageRaftGroupTest method testRangeNextWorksCorrectlyAfterLeaderChange.

/**
 * Tests that {@link MetaStorageService#range(ByteArray, ByteArray, long)}} next command works correctly
 * after leader changing.
 *
 * @throws Exception If failed.
 */
@Test
public void testRangeNextWorksCorrectlyAfterLeaderChange() throws Exception {
    final AtomicInteger replicatorStartedCounter = new AtomicInteger(0);
    final AtomicInteger replicatorStoppedCounter = new AtomicInteger(0);
    when(mockStorage.range(EXPECTED_RESULT_ENTRY1.key().bytes(), new byte[] { 4 })).thenAnswer(invocation -> {
        List<org.apache.ignite.internal.metastorage.server.Entry> entries = new ArrayList<>(List.of(EXPECTED_SRV_RESULT_ENTRY1, EXPECTED_SRV_RESULT_ENTRY2));
        return new Cursor<org.apache.ignite.internal.metastorage.server.Entry>() {

            private final Iterator<org.apache.ignite.internal.metastorage.server.Entry> it = entries.iterator();

            @Override
            public void close() {
            }

            @NotNull
            @Override
            public Iterator<org.apache.ignite.internal.metastorage.server.Entry> iterator() {
                return it;
            }

            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public org.apache.ignite.internal.metastorage.server.Entry next() {
                return it.next();
            }
        };
    });
    List<Pair<RaftServer, RaftGroupService>> raftServersRaftGroups = prepareJraftMetaStorages(replicatorStartedCounter, replicatorStoppedCounter);
    List<RaftServer> raftServers = raftServersRaftGroups.stream().map(p -> p.key).collect(Collectors.toList());
    NetworkAddress oldLeader = raftServersRaftGroups.get(0).value.leader().address();
    Optional<RaftServer> oldLeaderServer = raftServers.stream().filter(s -> s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
    // Server that will be alive after we stop leader.
    Optional<RaftServer> liveServer = raftServers.stream().filter(s -> !s.clusterService().topologyService().localMember().address().equals(oldLeader)).findFirst();
    if (oldLeaderServer.isEmpty() || liveServer.isEmpty()) {
        fail();
    }
    RaftGroupService raftGroupServiceOfLiveServer = raftServersRaftGroups.stream().filter(p -> p.key.equals(liveServer.get())).findFirst().get().value;
    MetaStorageService metaStorageSvc = new MetaStorageServiceImpl(raftGroupServiceOfLiveServer, "some_node");
    Cursor<Entry> cursor = metaStorageSvc.range(EXPECTED_RESULT_ENTRY1.key(), new ByteArray(new byte[] { 4 }));
    assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
    assertTrue(cursor.hasNext());
    assertEquals(EXPECTED_RESULT_ENTRY1, (cursor.iterator().next()));
    // Ensure that leader has not been changed.
    // In a stable topology unexpected leader election shouldn't happen.
    assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 2, 5_000), replicatorStartedCounter.get() + "");
    // stop leader
    oldLeaderServer.get().stopRaftGroup(METASTORAGE_RAFT_GROUP_NAME);
    oldLeaderServer.get().stop();
    cluster.stream().filter(c -> c.topologyService().localMember().address().equals(oldLeader)).findFirst().get().stop();
    raftGroupServiceOfLiveServer.refreshLeader().get();
    assertNotSame(oldLeader, raftGroupServiceOfLiveServer.leader().address());
    // ensure that leader has been changed only once
    assertTrue(TestUtils.waitForCondition(() -> replicatorStartedCounter.get() == 4, 5_000), replicatorStartedCounter.get() + "");
    assertTrue(TestUtils.waitForCondition(() -> replicatorStoppedCounter.get() == 2, 5_000), replicatorStoppedCounter.get() + "");
    assertTrue(cursor.hasNext());
    assertEquals(EXPECTED_RESULT_ENTRY2, (cursor.iterator().next()));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) IgniteLogger(org.apache.ignite.lang.IgniteLogger) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) JraftServerImpl(org.apache.ignite.internal.raft.server.impl.JraftServerImpl) Path(java.nio.file.Path) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) KeyValueStorage(org.apache.ignite.internal.metastorage.server.KeyValueStorage) Cursor(org.apache.ignite.internal.util.Cursor) Status(org.apache.ignite.raft.jraft.Status) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) Collectors(java.util.stream.Collectors) TestUtils(org.apache.ignite.raft.jraft.test.TestUtils) TestInfo(org.junit.jupiter.api.TestInfo) RaftServer(org.apache.ignite.internal.raft.server.RaftServer) Objects(java.util.Objects) Test(org.junit.jupiter.api.Test) List(java.util.List) StaticNodeFinder(org.apache.ignite.network.StaticNodeFinder) MetaStorageListener(org.apache.ignite.internal.metastorage.server.raft.MetaStorageListener) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Replicator(org.apache.ignite.raft.jraft.core.Replicator) NotNull(org.jetbrains.annotations.NotNull) RaftGroupServiceImpl(org.apache.ignite.raft.jraft.rpc.impl.RaftGroupServiceImpl) ClusterServiceTestUtils.findLocalAddresses(org.apache.ignite.utils.ClusterServiceTestUtils.findLocalAddresses) Assertions.fail(org.junit.jupiter.api.Assertions.fail) ClusterServiceTestUtils(org.apache.ignite.utils.ClusterServiceTestUtils) Mock(org.mockito.Mock) Loza(org.apache.ignite.internal.raft.Loza) NodeOptions(org.apache.ignite.raft.jraft.option.NodeOptions) ArrayList(java.util.ArrayList) TestScaleCubeClusterServiceFactory(org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory) TestUtils.waitForTopology(org.apache.ignite.raft.jraft.test.TestUtils.waitForTopology) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Iterator(java.util.Iterator) ByteArray(org.apache.ignite.lang.ByteArray) Mockito.when(org.mockito.Mockito.when) WorkDirectory(org.apache.ignite.internal.testframework.WorkDirectory) NetworkAddress(org.apache.ignite.network.NetworkAddress) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) PeerId(org.apache.ignite.raft.jraft.entity.PeerId) Peer(org.apache.ignite.raft.client.Peer) AfterEach(org.junit.jupiter.api.AfterEach) ClusterService(org.apache.ignite.network.ClusterService) NamedThreadFactory(org.apache.ignite.internal.thread.NamedThreadFactory) WorkDirectoryExtension(org.apache.ignite.internal.testframework.WorkDirectoryExtension) RaftServer(org.apache.ignite.internal.raft.server.RaftServer) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) ArrayList(java.util.ArrayList) Cursor(org.apache.ignite.internal.util.Cursor) NetworkAddress(org.apache.ignite.network.NetworkAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) ByteArray(org.apache.ignite.lang.ByteArray) Test(org.junit.jupiter.api.Test)

Example 19 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress 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 20 with NetworkAddress

use of org.apache.ignite.network.NetworkAddress in project ignite-3 by apache.

the class ItJraftCounterServerTest method startClient.

/**
 * Starts client.
 *
 * @param groupId Group id.
 * @return The client.
 * @throws Exception If failed.
 */
private RaftGroupService startClient(String groupId) throws Exception {
    var addr = new NetworkAddress(getLocalAddress(), PORT);
    ClusterService clientNode = clusterService(CLIENT_PORT + clients.size(), List.of(addr), true);
    RaftGroupService client = RaftGroupServiceImpl.start(groupId, clientNode, FACTORY, 10_000, List.of(new Peer(addr)), false, 200, executor).get(3, TimeUnit.SECONDS);
    clients.add(client);
    return client;
}
Also used : ClusterService(org.apache.ignite.network.ClusterService) NetworkAddress(org.apache.ignite.network.NetworkAddress) RaftGroupService(org.apache.ignite.raft.client.service.RaftGroupService) Peer(org.apache.ignite.raft.client.Peer)

Aggregations

NetworkAddress (org.apache.ignite.network.NetworkAddress)32 ClusterService (org.apache.ignite.network.ClusterService)18 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)12 StaticNodeFinder (org.apache.ignite.network.StaticNodeFinder)11 Peer (org.apache.ignite.raft.client.Peer)11 Test (org.junit.jupiter.api.Test)11 BeforeEach (org.junit.jupiter.api.BeforeEach)9 ClusterNode (org.apache.ignite.network.ClusterNode)8 List (java.util.List)7 ArrayList (java.util.ArrayList)6 ExecutionException (java.util.concurrent.ExecutionException)6 JraftServerImpl (org.apache.ignite.internal.raft.server.impl.JraftServerImpl)6 TestScaleCubeClusterServiceFactory (org.apache.ignite.network.scalecube.TestScaleCubeClusterServiceFactory)6 Path (java.nio.file.Path)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)5 InternalTableImpl (org.apache.ignite.internal.table.distributed.storage.InternalTableImpl)5 NamedThreadFactory (org.apache.ignite.internal.thread.NamedThreadFactory)5 TxManager (org.apache.ignite.internal.tx.TxManager)5 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)5