Search in sources :

Example 16 with ComputeTaskSession

use of org.apache.ignite.compute.ComputeTaskSession 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)

Example 17 with ComputeTaskSession

use of org.apache.ignite.compute.ComputeTaskSession in project ignite by apache.

the class GridRoundRobinLoadBalancingSpiNotPerTaskSelfTest method testNodeNotInTopology.

/** */
public void testNodeNotInTopology() throws Exception {
    ComputeTaskSession ses = new GridTestTaskSession();
    ClusterNode node = new GridTestNode(UUID.randomUUID());
    List<ClusterNode> notInTop = Arrays.asList(node);
    try {
        getSpi().getBalancedNode(ses, notInTop, new GridTestJob());
    } catch (IgniteException e) {
        assertTrue(e.getMessage().contains("Task topology does not have alive nodes"));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestTaskSession(org.apache.ignite.GridTestTaskSession) IgniteException(org.apache.ignite.IgniteException) GridTestJob(org.apache.ignite.GridTestJob) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) GridTestNode(org.apache.ignite.testframework.GridTestNode)

Example 18 with ComputeTaskSession

use of org.apache.ignite.compute.ComputeTaskSession in project ignite by apache.

the class GridRoundRobinLoadBalancingSpiTopologyChangeSelfTest method testTopologyChange.

/**
     * @throws Exception If failed.
     */
public void testTopologyChange() throws Exception {
    ComputeTaskSession ses = new GridTestTaskSession(IgniteUuid.randomUuid());
    // Warm up.
    List<ClusterNode> allNodes = (List<ClusterNode>) getSpiContext().nodes();
    List<UUID> orderedNodes = getSpi().getNodeIds(ses);
    checkCyclicBalancing(getSpi(), allNodes, orderedNodes, ses);
    // Remove node.
    UUID doomed = orderedNodes.get(0);
    if (getSpiContext().localNode().id().equals(doomed))
        doomed = orderedNodes.get(1);
    getSpiContext().removeNode(doomed);
    assertTrue(allNodes.remove(new GridTestNode(doomed)));
    orderedNodes = getSpi().getNodeIds(ses);
    assertFalse("Balancer uses removed node", orderedNodes.contains(doomed));
    checkCyclicBalancing(getSpi(), allNodes, orderedNodes, ses);
    // Add node.
    ClusterNode newNode = new GridTestNode(UUID.randomUUID());
    getSpiContext().addNode(newNode);
    assertTrue(allNodes.add(newNode));
    // Check that new node was added to balancing.
    boolean foundNewNode = false;
    for (int i = 0; i < allNodes.size(); i++) {
        ClusterNode node = getSpi().getBalancedNode(ses, allNodes, new GridTestJob());
        if (newNode.id().equals(node.id())) {
            foundNewNode = true;
            break;
        }
    }
    assertTrue("Balancer doesn't use added node", foundNewNode);
    orderedNodes = getSpi().getNodeIds(ses);
    checkCyclicBalancing(getSpi(), allNodes, orderedNodes, ses);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) GridTestTaskSession(org.apache.ignite.GridTestTaskSession) List(java.util.List) UUID(java.util.UUID) GridTestJob(org.apache.ignite.GridTestJob) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) GridTestNode(org.apache.ignite.testframework.GridTestNode)

Example 19 with ComputeTaskSession

use of org.apache.ignite.compute.ComputeTaskSession in project ignite by apache.

the class GridTaskExecutionContextSelfTest method testWithName.

/**
     * @throws Exception If failed.
     */
public void testWithName() throws Exception {
    IgniteCallable<String> f = new IgniteCallable<String>() {

        @TaskSessionResource
        private ComputeTaskSession ses;

        @Override
        public String call() {
            return ses.getTaskName();
        }
    };
    Ignite g = grid(0);
    assert "name1".equals(g.compute().withName("name1").call(f));
    assert "name2".equals(g.compute().withName("name2").call(f));
    assert f.getClass().getName().equals(g.compute().call(f));
    assert "name1".equals(g.compute().withName("name1").execute(new TestTask(false), null));
    assert "name2".equals(g.compute().withName("name2").execute(new TestTask(false), null));
    assert TestTask.class.getName().equals(g.compute().execute(new TestTask(false), null));
}
Also used : IgniteCallable(org.apache.ignite.lang.IgniteCallable) Ignite(org.apache.ignite.Ignite) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Aggregations

ComputeTaskSession (org.apache.ignite.compute.ComputeTaskSession)19 ClusterNode (org.apache.ignite.cluster.ClusterNode)12 UUID (java.util.UUID)10 GridTestTaskSession (org.apache.ignite.GridTestTaskSession)10 GridTestJob (org.apache.ignite.GridTestJob)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Ignite (org.apache.ignite.Ignite)5 IgniteException (org.apache.ignite.IgniteException)4 TaskEvent (org.apache.ignite.events.TaskEvent)4 GridTestNode (org.apache.ignite.testframework.GridTestNode)4 HashMap (java.util.HashMap)3 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ComputeJobAdapter (org.apache.ignite.compute.ComputeJobAdapter)2 ComputeJobSibling (org.apache.ignite.compute.ComputeJobSibling)2 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)2 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)2 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)2 TaskSessionResource (org.apache.ignite.resources.TaskSessionResource)2