Search in sources :

Example 11 with ClusterNode

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

the class ItScaleCubeNetworkMessagingTest method messageWasSentToAllMembersSuccessfully.

/**
 * Tests sending and receiving messages.
 *
 * @throws Exception in case of errors.
 */
@Test
public void messageWasSentToAllMembersSuccessfully(TestInfo testInfo) throws Exception {
    Map<String, TestMessage> messageStorage = new ConcurrentHashMap<>();
    var messageReceivedLatch = new CountDownLatch(3);
    testCluster = new Cluster(3, testInfo);
    for (ClusterService member : testCluster.members) {
        member.messagingService().addMessageHandler(TestMessageTypes.class, (message, senderAddr, correlationId) -> {
            messageStorage.put(member.localConfiguration().getName(), (TestMessage) message);
            messageReceivedLatch.countDown();
        });
    }
    testCluster.startAwait();
    var testMessage = messageFactory.testMessage().msg("Message from Alice").build();
    ClusterService alice = testCluster.members.get(0);
    for (ClusterNode member : alice.topologyService().allMembers()) {
        alice.messagingService().weakSend(member, testMessage);
    }
    boolean messagesReceived = messageReceivedLatch.await(3, TimeUnit.SECONDS);
    assertTrue(messagesReceived);
    testCluster.members.stream().map(member -> member.localConfiguration().getName()).map(messageStorage::get).forEach(msg -> assertThat(msg.msg(), is(testMessage.msg())));
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) ClusterService(org.apache.ignite.network.ClusterService) TestMessage(org.apache.ignite.network.TestMessage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 12 with ClusterNode

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

the class ItScaleCubeNetworkMessagingTest method testInvokeMessageToSelf.

/**
 * Sends a messages from a node to itself and awaits the response.
 *
 * @throws Exception in case of errors.
 */
@Test
public void testInvokeMessageToSelf(TestInfo testInfo) throws Exception {
    testCluster = new Cluster(1, testInfo);
    testCluster.startAwait();
    ClusterService member = testCluster.members.get(0);
    ClusterNode self = member.topologyService().localMember();
    var requestMessage = messageFactory.testMessage().msg("request").build();
    var responseMessage = messageFactory.testMessage().msg("response").build();
    member.messagingService().addMessageHandler(TestMessageTypes.class, (message, senderAddr, correlationId) -> {
        if (message.equals(requestMessage)) {
            member.messagingService().respond(self, responseMessage, correlationId);
        }
    });
    TestMessage actualResponseMessage = member.messagingService().invoke(self, requestMessage, 1000).thenApply(TestMessage.class::cast).get(3, TimeUnit.SECONDS);
    assertThat(actualResponseMessage.msg(), is(responseMessage.msg()));
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) ClusterService(org.apache.ignite.network.ClusterService) TestMessage(org.apache.ignite.network.TestMessage) Test(org.junit.jupiter.api.Test)

Example 13 with ClusterNode

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

the class RendezvousAffinityFunction method neighbors.

/**
 * Builds neighborhood map for all nodes in snapshot.
 *
 * @param topSnapshot Topology snapshot.
 * @return Neighbors map.
 */
public static Map<String, Collection<ClusterNode>> neighbors(Collection<ClusterNode> topSnapshot) {
    Map<String, Collection<ClusterNode>> macMap = new HashMap<>(topSnapshot.size(), 1.0f);
    // Group by mac addresses.
    for (ClusterNode node : topSnapshot) {
        String macs = String.valueOf(node.hashCode());
        // node.attribute(IgniteNodeAttributes.ATTR_MACS);
        Collection<ClusterNode> nodes = macMap.get(macs);
        if (nodes == null) {
            macMap.put(macs, nodes = new HashSet<>());
        }
        nodes.add(node);
    }
    Map<String, Collection<ClusterNode>> neighbors = new HashMap<>(topSnapshot.size(), 1.0f);
    for (Collection<ClusterNode> group : macMap.values()) {
        for (ClusterNode node : group) {
            neighbors.put(node.id(), group);
        }
    }
    return neighbors;
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) HashMap(java.util.HashMap) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 14 with ClusterNode

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

the class RendezvousAffinityFunction method replicatedAssign.

/**
 * Creates assignment for REPLICATED table.
 *
 * @param nodes       Topology.
 * @param sortedNodes Sorted for specified partitions nodes.
 * @return Assignment.
 */
private static List<ClusterNode> replicatedAssign(List<ClusterNode> nodes, Iterable<ClusterNode> sortedNodes) {
    ClusterNode first = sortedNodes.iterator().next();
    List<ClusterNode> res = new ArrayList<>(nodes.size());
    res.add(first);
    for (ClusterNode n : nodes) {
        if (!n.equals(first)) {
            res.add(n);
        }
    }
    assert res.size() == nodes.size() : "Not enough replicas: " + res.size();
    return res;
}
Also used : ClusterNode(org.apache.ignite.network.ClusterNode) ArrayList(java.util.ArrayList)

Example 15 with ClusterNode

use of org.apache.ignite.network.ClusterNode 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)

Aggregations

ClusterNode (org.apache.ignite.network.ClusterNode)25 NetworkAddress (org.apache.ignite.network.NetworkAddress)11 List (java.util.List)10 ClusterService (org.apache.ignite.network.ClusterService)9 Loza (org.apache.ignite.internal.raft.Loza)8 TopologyService (org.apache.ignite.network.TopologyService)8 RaftGroupService (org.apache.ignite.raft.client.service.RaftGroupService)8 Test (org.junit.jupiter.api.Test)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Int2ObjectOpenHashMap (it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap)6 Map (java.util.Map)6 UUID (java.util.UUID)6 SchemaDescriptor (org.apache.ignite.internal.schema.SchemaDescriptor)6 IgniteLogger (org.apache.ignite.lang.IgniteLogger)6 HashSet (java.util.HashSet)5 Set (java.util.Set)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 Consumer (java.util.function.Consumer)5 Collectors (java.util.stream.Collectors)5