use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class SystemViewComputeJobTest method testComputeTask.
/**
*/
@Test
public void testComputeTask() throws Exception {
barrier = new CyclicBarrier(2);
SystemView<ComputeJobView> jobs = server.context().systemView().view(JOBS_VIEW);
client.compute().executeAsync(new ComputeTask<Object, Object>() {
@Override
@NotNull
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable Object arg) throws IgniteException {
return Collections.singletonMap(new ComputeJob() {
@Override
public void cancel() {
// No-op.
}
@Override
public Object execute() throws IgniteException {
try {
barrier.await(TIMEOUT, MILLISECONDS);
barrier.await(TIMEOUT, MILLISECONDS);
} catch (InterruptedException | BrokenBarrierException | TimeoutException e) {
throw new RuntimeException(e);
}
return 1;
}
}, subgrid.get(0));
}
@Override
public ComputeJobResultPolicy result(ComputeJobResult res, List<ComputeJobResult> rcvd) throws IgniteException {
return null;
}
@Nullable
@Override
public Object reduce(List<ComputeJobResult> results) throws IgniteException {
return 1;
}
}, 1);
barrier.await(TIMEOUT, MILLISECONDS);
assertEquals(1, jobs.size());
ComputeJobView t = jobs.iterator().next();
checkJobView(t);
barrier.await(TIMEOUT, MILLISECONDS);
boolean res = waitForCondition(() -> jobs.size() == 0, TIMEOUT);
assertTrue(res);
}
use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class GridP2PTestTask method map.
/**
* {@inheritDoc}
*/
@NotNull
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, Object arg) {
assert subgrid != null;
assert !subgrid.isEmpty();
Integer arg1 = null;
if (arg instanceof GridifyArgument)
arg1 = (Integer) ((GridifyArgument) arg).getMethodParameters()[0];
else if (arg instanceof Integer)
arg1 = (Integer) arg;
else
assert false : "Failed to map task (unknown argument type) [type=" + arg.getClass() + ", val=" + arg + ']';
Map<ComputeJob, ClusterNode> map = new HashMap<>(subgrid.size());
UUID nodeId = ignite != null ? ignite.configuration().getNodeId() : null;
for (ClusterNode node : subgrid) if (!node.id().equals(nodeId))
map.put(new GridP2PTestJob(arg1), node);
return map;
}
use of org.apache.ignite.compute.ComputeJob in project ignite by apache.
the class GarHelloWorldTask method split.
/**
* {@inheritDoc}
*/
@Override
public Collection<? extends ComputeJob> split(int gridSize, String arg) throws IgniteException {
// Create Spring context.
AbstractBeanFactory fac = new XmlBeanFactory(new ClassPathResource("org/apache/ignite/spi/deployment/uri/tasks/gar-spring-bean.xml", getClass().getClassLoader()));
fac.setBeanClassLoader(getClass().getClassLoader());
// Load imported bean from GAR/lib folder.
GarHelloWorldBean bean = (GarHelloWorldBean) fac.getBean("example.bean");
String msg = bean.getMessage(arg);
assert msg != null;
// Split the passed in phrase into multiple words separated by spaces.
List<String> words = Arrays.asList(msg.split(" "));
Collection<ComputeJob> jobs = new ArrayList<>(words.size());
// Use imperative OOP APIs.
for (String word : words) {
// Every job gets its own word as an argument.
jobs.add(new ComputeJobAdapter(word) {
/*
* Simply prints the job's argument.
*/
@Nullable
@Override
public Serializable execute() {
System.out.println(">>>");
System.out.println(">>> Printing '" + argument(0) + "' on this node from grid job.");
System.out.println(">>>");
// This job does not return any result.
return null;
}
});
}
return jobs;
}
Aggregations