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);
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
Aggregations