Search in sources :

Example 1 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class GridMultipleVersionsDeploymentSelfTest method testMultipleVersionsP2PDeploy.

/**
 * @throws Exception If test failed.
 */
@Test
public void testMultipleVersionsP2PDeploy() throws Exception {
    try {
        Ignite g1 = startGrid(1);
        Ignite g2 = startGrid(2);
        final CountDownLatch latch = new CountDownLatch(2);
        g2.events().localListen(new IgnitePredicate<Event>() {

            @Override
            public boolean apply(Event evt) {
                info("Received event: " + evt);
                latch.countDown();
                return true;
            }
        }, EVT_TASK_UNDEPLOYED);
        ClassLoader ldr1 = new GridTestClassLoader(Collections.singletonMap("testResource", "1"), getClass().getClassLoader(), EXCLUDE_CLASSES);
        ClassLoader ldr2 = new GridTestClassLoader(Collections.singletonMap("testResource", "2"), getClass().getClassLoader(), EXCLUDE_CLASSES);
        Class<? extends ComputeTask<?, ?>> taskCls1 = (Class<? extends ComputeTask<?, ?>>) ldr1.loadClass(GridDeploymentTestTask.class.getName());
        Class<? extends ComputeTask<?, ?>> taskCls2 = (Class<? extends ComputeTask<?, ?>>) ldr2.loadClass(GridDeploymentTestTask.class.getName());
        g1.compute().localDeployTask(taskCls1, ldr1);
        // Task will wait for the signal.
        ComputeTaskFuture fut1 = executeAsync(g1.compute(), "GridDeploymentTestTask", null);
        assert checkDeployed(g1, "GridDeploymentTestTask");
        // We should wait here when to be sure that job has been started.
        // Since we loader task/job classes with different class loaders we cannot
        // use any kind of mutex because of the illegal state exception.
        // We have to use timer here. DO NOT CHANGE 2 seconds here.
        Thread.sleep(1000);
        // Deploy new one - this should move first task to the obsolete list.
        g1.compute().localDeployTask(taskCls2, ldr2);
        // Task will wait for the signal.
        ComputeTaskFuture fut2 = executeAsync(g1.compute(), "GridDeploymentTestTask", null);
        boolean deployed = checkDeployed(g1, "GridDeploymentTestTask");
        Object res1 = fut1.get();
        Object res2 = fut2.get();
        g1.compute().undeployTask("GridDeploymentTestTask");
        // New one should be deployed.
        assert deployed;
        // Wait for the execution.
        assert res1.equals(1);
        assert res2.equals(2);
        stopGrid(1);
        assert latch.await(3000, MILLISECONDS);
        assert !checkDeployed(g2, "GridDeploymentTestTask");
    } finally {
        stopGrid(2);
        stopGrid(1);
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) CountDownLatch(java.util.concurrent.CountDownLatch) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) ComputeTaskFuture(org.apache.ignite.compute.ComputeTaskFuture) Event(org.apache.ignite.events.Event) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) Ignite(org.apache.ignite.Ignite) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 2 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class IgniteExplicitImplicitDeploymentSelfTest method execImplicitDeployLocally.

/**
 * @param byCls If {@code true} than executes task by Class.
 * @param byTask If {@code true} than executes task instance.
 * @param byName If {@code true} than executes task by class name.
 * @throws Exception If test failed.
 */
private void execImplicitDeployLocally(boolean byCls, boolean byTask, boolean byName) throws Exception {
    final IgniteEx ignite = startGrids(1);
    final IgniteEx client = startClientGrid(1);
    try {
        // First task class loader.
        ClassLoader ldr1 = new GridTestClassLoader(Collections.singletonMap("testResource", "1"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        // Second task class loader
        ClassLoader ldr2 = new GridTestClassLoader(Collections.singletonMap("testResource", "2"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        // The same name but different classes/ class loaders.
        Class<? extends ComputeTask<String, Integer>> taskCls1 = (Class<? extends ComputeTask<String, Integer>>) ldr1.loadClass(GridDeploymentResourceTestTask.class.getName());
        Class<? extends ComputeTask<String, Integer>> taskCls2 = (Class<? extends ComputeTask<String, Integer>>) ldr2.loadClass(GridDeploymentResourceTestTask.class.getName());
        if (byCls) {
            IgniteInternalFuture f1 = runAsyncByClass(ignite, client, taskCls1, 1);
            IgniteInternalFuture f2 = runAsyncByClass(ignite, client, taskCls2, 2);
            f1.get();
            f2.get();
        }
        if (byTask) {
            final ComputeTask<String, Integer> tc1 = taskCls1.newInstance();
            final ComputeTask<String, Integer> tc2 = taskCls2.newInstance();
            IgniteInternalFuture f1 = runAsyncByInstance(ignite, client, tc1, 1);
            IgniteInternalFuture f2 = runAsyncByInstance(ignite, client, tc2, 2);
            f1.get();
            f2.get();
        }
        if (byName) {
            ignite.compute().localDeployTask(taskCls1, ldr1);
            Integer res1 = ignite.compute().execute(taskCls1.getName(), null);
            assert res1 != null;
            assert res1 == 1 : "Invalid res1: " + res1;
            ignite.compute().localDeployTask(taskCls2, ldr2);
            Integer res2 = ignite.compute().execute(taskCls2.getName(), null);
            assert res2 != null;
            assert res2 == 2 : "Invalid res2: " + res2;
        }
    } finally {
        stopAllGrids();
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader)

Example 3 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class IgniteExplicitImplicitDeploymentSelfTest method execExplicitDeployLocally.

/**
 * @param byCls If {@code true} than executes task by Class.
 * @param byTask If {@code true} than executes task instance.
 * @param byName If {@code true} than executes task by class name.
 * @throws Exception If test failed.
 */
private void execExplicitDeployLocally(boolean byCls, boolean byTask, boolean byName) throws Exception {
    Ignite ignite;
    try {
        ignite = startGrid();
        // Explicit Deployment. Task execution should return 0.
        // Say resource class loader - different to task one.
        ClassLoader ldr1 = new GridTestClassLoader(Collections.singletonMap("testResource", "1"), getClass().getClassLoader());
        // Assume that users task and job were loaded with this class loader
        ClassLoader ldr2 = new GridTestClassLoader(Collections.singletonMap("testResource", "2"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        info("Loader1: " + ldr1);
        info("Loader2: " + ldr2);
        Class<? extends ComputeTask<String, Integer>> taskCls = (Class<? extends ComputeTask<String, Integer>>) ldr2.loadClass(GridDeploymentResourceTestTask.class.getName());
        // Check auto-deploy. It should pick up resource class loader.
        if (byCls) {
            ignite.compute().localDeployTask(taskCls, ldr1);
            Integer res = ignite.compute().execute(taskCls, null);
            assert res != null;
            assert res == 2 : "Invalid response: " + res;
        }
        if (byTask) {
            ignite.compute().localDeployTask(taskCls, ldr1);
            Integer res = ignite.compute().execute(taskCls.newInstance(), null);
            assert res != null;
            assert res == 2 : "Invalid response: " + res;
        }
        if (byName) {
            ignite.compute().localDeployTask(taskCls, ldr1);
            Integer res = ignite.compute().execute(taskCls.getName(), null);
            assert res != null;
            assert res == 1 : "Invalid response: " + res;
        }
    } finally {
        stopAllGrids();
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) Ignite(org.apache.ignite.Ignite)

Example 4 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class IgniteExplicitImplicitDeploymentSelfTest method execImplicitDeployP2P.

/**
 * @param byCls If {@code true} than executes task by Class.
 * @param byTask If {@code true} than executes task instance.
 * @param byName If {@code true} than executes task by class name.
 * @throws Exception If test failed.
 */
private void execImplicitDeployP2P(boolean byCls, boolean byTask, boolean byName) throws Exception {
    try {
        IgniteEx ignite = startGrids(1);
        final IgniteEx client = startClientGrid(1);
        ClassLoader ldr1 = new GridTestClassLoader(Collections.singletonMap("testResource", "1"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        ClassLoader ldr2 = new GridTestClassLoader(Collections.singletonMap("testResource", "2"), getClass().getClassLoader(), GridDeploymentResourceTestTask.class.getName(), GridDeploymentResourceTestJob.class.getName());
        Class<? extends ComputeTask<String, Integer>> taskCls1 = (Class<? extends ComputeTask<String, Integer>>) ldr1.loadClass(GridDeploymentResourceTestTask.class.getName());
        Class<? extends ComputeTask<String, Integer>> taskCls2 = (Class<? extends ComputeTask<String, Integer>>) ldr2.loadClass(GridDeploymentResourceTestTask.class.getName());
        if (byCls) {
            IgniteInternalFuture f1 = runAsyncByClass(ignite, client, taskCls1, 1);
            IgniteInternalFuture f2 = runAsyncByClass(ignite, client, taskCls2, 2);
            f1.get();
            f2.get();
        }
        if (byTask) {
            final ComputeTask<String, Integer> tc1 = taskCls1.newInstance();
            final ComputeTask<String, Integer> tc2 = taskCls2.newInstance();
            IgniteInternalFuture f1 = runAsyncByInstance(ignite, client, tc1, 1);
            IgniteInternalFuture f2 = runAsyncByInstance(ignite, client, tc2, 2);
            f1.get();
            f2.get();
        }
        if (byName) {
            ignite.compute().localDeployTask(taskCls1, ldr1);
            Integer res1 = ignite.compute().execute(taskCls1.getName(), null);
            assert res1 != null;
            assertEquals("Invalid res1: ", 1, (int) res1);
            ignite.compute().localDeployTask(taskCls2, ldr2);
            Integer res2 = ignite.compute().execute(taskCls2.getName(), null);
            assert res2 != null;
            assertEquals("Invalid res2: ", 2, (int) res2);
        }
    } finally {
        stopAllGrids();
    }
}
Also used : ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader)

Example 5 with GridTestClassLoader

use of org.apache.ignite.testframework.GridTestClassLoader in project ignite by apache.

the class GridP2PClassLoadingSelfTest method testClassLoading.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings({ "ConstantConditions" })
@Test
public void testClassLoading() throws Exception {
    ComputeTask<?, ?> task = (ComputeTask<?, ?>) tstClsLdr.loadClass(GridP2PTestTask.class.getName()).newInstance();
    byte[] rawTask = GridTestIoUtils.serializeJdk(task);
    ComputeTask<Object, Integer> res = GridTestIoUtils.deserializeJdk(rawTask, tstClsLdr);
    ClusterNode fakeNode = new TestGridNode();
    List<ClusterNode> nodes = Collections.singletonList(fakeNode);
    ComputeJob p2pJob = res.map(nodes, 1).entrySet().iterator().next().getKey();
    assert p2pJob.getClass().getClassLoader() instanceof GridTestClassLoader : "Class loader = " + res.getClass().getClassLoader();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJob(org.apache.ignite.compute.ComputeJob) ComputeTask(org.apache.ignite.compute.ComputeTask) GridTestClassLoader(org.apache.ignite.testframework.GridTestClassLoader) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test) GridCommonTest(org.apache.ignite.testframework.junits.common.GridCommonTest)

Aggregations

GridTestClassLoader (org.apache.ignite.testframework.GridTestClassLoader)15 ComputeTask (org.apache.ignite.compute.ComputeTask)13 Ignite (org.apache.ignite.Ignite)8 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)5 Test (org.junit.Test)5 GridCommonTest (org.apache.ignite.testframework.junits.common.GridCommonTest)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 ComputeTaskFuture (org.apache.ignite.compute.ComputeTaskFuture)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URLClassLoader (java.net.URLClassLoader)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 ComputeJob (org.apache.ignite.compute.ComputeJob)1 Gridify (org.apache.ignite.compute.gridify.Gridify)1 Event (org.apache.ignite.events.Event)1 GridPlainRunnable (org.apache.ignite.internal.util.lang.GridPlainRunnable)1 GridP2PTestJob (org.apache.ignite.p2p.GridP2PTestJob)1 GridP2PTestTask (org.apache.ignite.p2p.GridP2PTestTask)1