Search in sources :

Example 6 with GridTestJob

use of org.apache.ignite.GridTestJob in project ignite by apache.

the class GridWeightedRandomLoadBalancingSpiSelfTest method testSingleNode.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "ObjectEquality" })
public void testSingleNode() throws Exception {
    List<ClusterNode> nodes = Collections.singletonList((ClusterNode) new GridTestNode(UUID.randomUUID()));
    ClusterNode node = getSpi().getBalancedNode(new GridTestTaskSession(), nodes, new GridTestJob());
    assert nodes.contains(node);
    // Verify that same instance is returned every time.
    ClusterNode balancedNode = getSpi().getBalancedNode(new GridTestTaskSession(), nodes, new GridTestJob());
    assert node == balancedNode;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestTaskSession(org.apache.ignite.GridTestTaskSession) GridTestJob(org.apache.ignite.GridTestJob) GridTestNode(org.apache.ignite.testframework.GridTestNode)

Example 7 with GridTestJob

use of org.apache.ignite.GridTestJob in project ignite by apache.

the class GridWeightedRandomLoadBalancingSpiWeightedSelfTest method testWeights.

/**
     * @throws Exception If test failed.
     */
public void testWeights() throws Exception {
    List<ClusterNode> nodes = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        GridTestNode node = new GridTestNode(UUID.randomUUID());
        node.addAttribute(U.spiAttribute(getSpi(), NODE_WEIGHT_ATTR_NAME), i + 1);
        nodes.add(node);
    }
    // Seal it.
    nodes = Collections.unmodifiableList(nodes);
    int[] cnts = new int[10];
    // Invoke load balancer a large number of times, so statistics won't lie.
    for (int i = 0; i < 100000; i++) {
        ClusterNode node = getSpi().getBalancedNode(new GridTestTaskSession(IgniteUuid.randomUuid()), nodes, new GridTestJob());
        int weight = (Integer) node.attribute(U.spiAttribute(getSpi(), NODE_WEIGHT_ATTR_NAME));
        // Increment number of times a node was picked.
        cnts[weight - 1]++;
    }
    for (int i = 0; i < cnts.length - 1; i++) {
        assert cnts[i] < cnts[i + 1] : "Invalid node counts for index [idx=" + i + ", cnts[i]=" + cnts[i] + ", cnts[i+1]=" + cnts[i + 1] + ']';
    }
    info("Node counts: " + Arrays.toString(cnts));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestTaskSession(org.apache.ignite.GridTestTaskSession) ArrayList(java.util.ArrayList) GridTestJob(org.apache.ignite.GridTestJob) GridTestNode(org.apache.ignite.testframework.GridTestNode)

Example 8 with GridTestJob

use of org.apache.ignite.GridTestJob in project ignite by apache.

the class GridRoundRobinLoadBalancingSpiMultipleNodesSelfTest method testMultipleNodes.

/**
     * @throws Exception If test failed.
     */
@SuppressWarnings({ "ObjectEquality" })
public void testMultipleNodes() throws Exception {
    List<ClusterNode> allNodes = (List<ClusterNode>) getSpiContext().nodes();
    ComputeTaskSession ses = new GridTestTaskSession(IgniteUuid.randomUuid());
    // Initialize.
    getSpi().getBalancedNode(ses, allNodes, new GridTestJob());
    List<UUID> orderedNodes = new ArrayList<>(getSpi().getNodeIds(ses));
    // Check the round-robin actually did circle.
    for (int i = 0; i < allNodes.size(); i++) {
        ClusterNode node = getSpi().getBalancedNode(ses, allNodes, new GridTestJob());
        assert orderedNodes.get(i) == node.id();
    }
    // Double-check.
    for (int i = 0; i < allNodes.size(); i++) {
        ClusterNode node = getSpi().getBalancedNode(ses, allNodes, new GridTestJob());
        assert orderedNodes.get(i) == node.id();
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestTaskSession(org.apache.ignite.GridTestTaskSession) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) GridTestJob(org.apache.ignite.GridTestJob) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 9 with GridTestJob

use of org.apache.ignite.GridTestJob in project ignite by apache.

the class GridWeightedRandomLoadBalancingSpiSelfTest method testMultipleNodes.

/**
     * @throws Exception If failed.
     */
public void testMultipleNodes() throws Exception {
    List<ClusterNode> nodes = new ArrayList<>();
    for (int i = 0; i < 10; i++) nodes.add(new GridTestNode(UUID.randomUUID()));
    // Seal it.
    nodes = Collections.unmodifiableList(nodes);
    ClusterNode node = getSpi().getBalancedNode(new GridTestTaskSession(), nodes, new GridTestJob());
    assert node != null;
    assert nodes.contains(node);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestTaskSession(org.apache.ignite.GridTestTaskSession) ArrayList(java.util.ArrayList) GridTestJob(org.apache.ignite.GridTestJob) GridTestNode(org.apache.ignite.testframework.GridTestNode)

Example 10 with GridTestJob

use of org.apache.ignite.GridTestJob in project ignite by apache.

the class GridRoundRobinLoadBalancingNotPerTaskMultithreadedSelfTest method testMultipleTaskSessionsMultithreaded.

/**
     *
     * @throws Exception If failed.
     */
public void testMultipleTaskSessionsMultithreaded() throws Exception {
    final RoundRobinLoadBalancingSpi spi = getSpi();
    final List<ClusterNode> allNodes = (List<ClusterNode>) getSpiContext().nodes();
    GridTestUtils.runMultiThreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            ComputeTaskSession ses = new GridTestTaskSession(IgniteUuid.randomUuid());
            Map<UUID, AtomicInteger> nodeCnts = new HashMap<>();
            for (int i = 1; i <= ITER_CNT; i++) {
                ClusterNode node = spi.getBalancedNode(ses, allNodes, new GridTestJob());
                if (!nodeCnts.containsKey(node.id()))
                    nodeCnts.put(node.id(), new AtomicInteger(1));
                else
                    nodeCnts.get(node.id()).incrementAndGet();
            }
            int predictCnt = ITER_CNT / allNodes.size();
            // Consider +-20% is permissible spread for single node measure.
            int floor = (int) (predictCnt * 0.8);
            double avgSpread = 0;
            for (ClusterNode n : allNodes) {
                int curCnt = nodeCnts.get(n.id()).intValue();
                avgSpread += Math.abs(predictCnt - curCnt);
                String msg = "Node stats [id=" + n.id() + ", cnt=" + curCnt + ", floor=" + floor + ", predictCnt=" + predictCnt + ']';
                info(msg);
                assertTrue(msg, curCnt >= floor);
            }
            avgSpread /= allNodes.size();
            avgSpread = 100.0 * avgSpread / predictCnt;
            info("Average spread for " + allNodes.size() + " nodes is " + avgSpread + " percents");
            // Consider +-10% is permissible average spread for all nodes.
            assertTrue("Average spread is too big: " + avgSpread, avgSpread <= 10);
            return null;
        }
    }, THREAD_CNT, "balancer-test-worker");
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestTaskSession(org.apache.ignite.GridTestTaskSession) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) GridTestJob(org.apache.ignite.GridTestJob) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Aggregations

GridTestJob (org.apache.ignite.GridTestJob)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)15 GridTestTaskSession (org.apache.ignite.GridTestTaskSession)14 GridTestNode (org.apache.ignite.testframework.GridTestNode)9 ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)8 ArrayList (java.util.ArrayList)6 List (java.util.List)5 UUID (java.util.UUID)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IgniteException (org.apache.ignite.IgniteException)1 TaskEvent (org.apache.ignite.events.TaskEvent)1