use of org.apache.ignite.lang.IgniteCallable in project ignite by apache.
the class GridClosureSerializationTest method testSerializationFailure.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored", "Convert2Lambda" })
public void testSerializationFailure() throws Exception {
final IgniteEx ignite0 = grid(0);
final IgniteEx ignite1 = grid(1);
GridTestUtils.assertThrows(null, new Callable<Object>() {
@Override
public Object call() throws Exception {
ignite1.compute(ignite1.cluster().forNode(ignite0.localNode())).call(new IgniteCallable<Object>() {
@Override
public Object call() throws Exception {
return new CaseClass.CaseClass2();
}
});
return null;
}
}, BinaryObjectException.class, null);
}
use of org.apache.ignite.lang.IgniteCallable in project ignite by apache.
the class GridCacheQueueApiSelfAbstractTest method testAffinityCall.
/**
* @throws Exception If failed.
*/
public void testAffinityCall() throws Exception {
final CollectionConfiguration colCfg = collectionConfiguration();
colCfg.setCollocated(false);
colCfg.setGroupName("testGroup");
colCfg.setCacheMode(CacheMode.PARTITIONED);
try (final IgniteQueue<Integer> queue1 = grid(0).queue("Queue1", 0, colCfg)) {
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
queue1.affinityCall(new IgniteCallable<Object>() {
@Override
public Object call() {
return null;
}
});
return null;
}
}, IgniteException.class, "Failed to execute affinityCall() for non-collocated queue: " + queue1.name() + ". This operation is supported only for collocated queues.");
}
colCfg.setCollocated(true);
try (final IgniteQueue<Integer> queue2 = grid(0).queue("Queue2", 0, colCfg)) {
queue2.add(100);
Integer res = queue2.affinityCall(new IgniteCallable<Integer>() {
@IgniteInstanceResource
private IgniteEx ignite;
@Override
public Integer call() {
assertTrue(ignite.cachex(cctx(queue2).cache().name()).affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Queue2"));
return queue2.take();
}
});
assertEquals(100, res.intValue());
}
}
use of org.apache.ignite.lang.IgniteCallable in project ignite by apache.
the class IgniteTxPessimisticOriginatingNodeFailureAbstractSelfTest method testTxOriginatingNodeFails.
/**
* @param keys Keys to update.
* @param fullFailure Flag indicating whether to simulate rollback state.
* @throws Exception If failed.
*/
protected void testTxOriginatingNodeFails(Collection<Integer> keys, final boolean fullFailure) throws Exception {
assertFalse(keys.isEmpty());
final Collection<IgniteKernal> grids = new ArrayList<>();
ClusterNode txNode = grid(originatingNode()).localNode();
for (int i = 1; i < gridCount(); i++) grids.add((IgniteKernal) grid(i));
failingNodeId = grid(0).localNode().id();
final Map<Integer, String> map = new HashMap<>();
final String initVal = "initialValue";
for (Integer key : keys) {
grid(originatingNode()).cache(DEFAULT_CACHE_NAME).put(key, initVal);
map.put(key, String.valueOf(key));
}
Map<Integer, Collection<ClusterNode>> nodeMap = new HashMap<>();
info("Node being checked: " + grid(1).localNode().id());
for (Integer key : keys) {
Collection<ClusterNode> nodes = new ArrayList<>();
nodes.addAll(grid(1).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key));
nodes.remove(txNode);
nodeMap.put(key, nodes);
}
info("Starting tx [values=" + map + ", topVer=" + ((IgniteKernal) grid(1)).context().discovery().topologyVersion() + ']');
if (fullFailure)
ignoreMessages(ignoreMessageClasses(), F.asList(grid(1).localNode().id()));
final IgniteEx originatingNodeGrid = grid(originatingNode());
GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteCache<Integer, String> cache = originatingNodeGrid.cache(DEFAULT_CACHE_NAME);
assertNotNull(cache);
Transaction tx = originatingNodeGrid.transactions().txStart();
assertEquals(PESSIMISTIC, tx.concurrency());
try {
cache.putAll(map);
info("Before commitAsync");
IgniteFuture<?> fut = tx.commitAsync();
info("Got future for commitAsync().");
fut.get(3, TimeUnit.SECONDS);
} catch (IgniteFutureTimeoutException ignored) {
info("Failed to wait for commit future completion [fullFailure=" + fullFailure + ']');
}
return null;
}
}).get();
info(">>> Stopping originating node " + txNode);
G.stop(grid(originatingNode()).name(), true);
ignoreMessages(Collections.<Class<?>>emptyList(), Collections.<UUID>emptyList());
info(">>> Stopped originating node: " + txNode.id());
boolean txFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
for (IgniteKernal g : grids) {
GridCacheAdapter<?, ?> cache = g.internalCache(DEFAULT_CACHE_NAME);
IgniteTxManager txMgr = cache.isNear() ? ((GridNearCacheAdapter) cache).dht().context().tm() : cache.context().tm();
int txNum = txMgr.idMapSize();
if (txNum != 0)
return false;
}
return true;
}
}, 10000);
assertTrue(txFinished);
info("Transactions finished.");
for (Map.Entry<Integer, Collection<ClusterNode>> e : nodeMap.entrySet()) {
final Integer key = e.getKey();
final String val = map.get(key);
assertFalse(e.getValue().isEmpty());
for (ClusterNode node : e.getValue()) {
final UUID checkNodeId = node.id();
compute(G.ignite(checkNodeId).cluster().forNode(node)).call(new IgniteCallable<Void>() {
/**
*/
@IgniteInstanceResource
private Ignite ignite;
@Override
public Void call() throws Exception {
IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
assertNotNull(cache);
assertEquals("Failed to check entry value on node: " + checkNodeId, fullFailure ? initVal : val, cache.localPeek(key));
return null;
}
});
}
}
for (Map.Entry<Integer, String> e : map.entrySet()) {
for (Ignite g : G.allGrids()) assertEquals(fullFailure ? initVal : e.getValue(), g.cache(DEFAULT_CACHE_NAME).get(e.getKey()));
}
}
use of org.apache.ignite.lang.IgniteCallable in project ignite by apache.
the class ComputeCallableExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws IgniteException If example execution failed.
*/
public static void main(String[] args) throws IgniteException {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Compute callable example started.");
Collection<IgniteCallable<Integer>> calls = new ArrayList<>();
// Iterate through all words in the sentence and create callable jobs.
for (String word : "Count characters using callable".split(" ")) {
calls.add(() -> {
System.out.println();
System.out.println(">>> Printing '" + word + "' on this node from ignite job.");
return word.length();
});
}
// Execute collection of callables on the ignite.
Collection<Integer> res = ignite.compute().call(calls);
int sum = res.stream().mapToInt(i -> i).sum();
System.out.println();
System.out.println(">>> Total number of characters in the phrase is '" + sum + "'.");
System.out.println(">>> Check all nodes for output (this node is also part of the cluster).");
}
}
use of org.apache.ignite.lang.IgniteCallable 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));
}
Aggregations