use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.
the class IgniteAtomicStampedExample method main.
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws Exception If example execution failed.
*/
public static void main(String[] args) throws Exception {
try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
System.out.println();
System.out.println(">>> Atomic stamped example started.");
// Make name of atomic stamped.
String stampedName = UUID.randomUUID().toString();
// Make value of atomic stamped.
String val = UUID.randomUUID().toString();
// Make stamp of atomic stamped.
String stamp = UUID.randomUUID().toString();
// Initialize atomic stamped.
IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
System.out.println("Atomic stamped initial [value=" + stamped.value() + ", stamp=" + stamped.stamp() + ']');
// Make closure for checking atomic stamped.
IgniteRunnable c = new StampedUpdateClosure(stampedName);
// Check atomic stamped on all cluster nodes.
ignite.compute().broadcast(c);
// Make new value of atomic stamped.
String newVal = UUID.randomUUID().toString();
// Make new stamp of atomic stamped.
String newStamp = UUID.randomUUID().toString();
System.out.println("Try to change value and stamp of atomic stamped with wrong expected value and stamp.");
stamped.compareAndSet("WRONG EXPECTED VALUE", newVal, "WRONG EXPECTED STAMP", newStamp);
// Check atomic stamped on all cluster nodes.
// Atomic stamped value and stamp shouldn't be changed.
ignite.compute().run(c);
System.out.println("Try to change value and stamp of atomic stamped with correct value and stamp.");
stamped.compareAndSet(val, newVal, stamp, newStamp);
// Check atomic stamped on all cluster nodes.
// Atomic stamped value and stamp should be changed.
ignite.compute().run(c);
}
System.out.println();
System.out.println("Finished atomic stamped example...");
System.out.println("Check all nodes for output (this node is also part of the cluster).");
}
use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.
the class EventsExample method localListen.
/**
* Listen to events that happen only on local node.
*
* @throws IgniteException If failed.
*/
private static void localListen() throws IgniteException {
System.out.println();
System.out.println(">>> Local event listener example.");
Ignite ignite = Ignition.ignite();
IgnitePredicate<TaskEvent> lsnr = new IgnitePredicate<TaskEvent>() {
@Override
public boolean apply(TaskEvent evt) {
System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');
// Return true to continue listening.
return true;
}
};
// Register event listener for all local task execution events.
ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION);
// Generate task events.
ignite.compute().withName("example-event-task").run(new IgniteRunnable() {
@Override
public void run() {
System.out.println("Executing sample job.");
}
});
// Unsubscribe local task event listener.
ignite.events().stopLocalListen(lsnr);
}
use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.
the class IgniteCacheLockPartitionOnAffinityRunTest method testReleasePartitionJobMasterLeave.
/**
* @throws Exception If failed.
*/
public void testReleasePartitionJobMasterLeave() throws Exception {
final int orgId = primaryKey(grid(0).cache(Organization.class.getSimpleName()));
try {
grid(1).compute().affinityRunAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new IgniteRunnable() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public void run() {
try {
checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
// No-op.
}
}
});
stopGrid(1, true);
Thread.sleep(3000);
awaitPartitionMapExchange();
checkPartitionsReservations(grid(0), orgId, 0);
} finally {
startGrid(1);
awaitPartitionMapExchange();
}
try {
grid(1).compute().affinityCallAsync(Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new IgniteCallable<Object>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public Object call() {
try {
checkPartitionsReservations((IgniteEx) ignite, orgId, 1);
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected exception");
}
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
// No-op.
}
return null;
}
});
stopGrid(1, true);
Thread.sleep(3000);
awaitPartitionMapExchange();
checkPartitionsReservations(grid(0), orgId, 0);
} finally {
startGrid(1);
awaitPartitionMapExchange();
}
}
use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.
the class GridMarshallerAbstractTest method testCompute.
/**
* @throws Exception If failed.
*/
public void testCompute() throws Exception {
IgniteConfiguration cfg = optimize(getConfiguration("g1"));
try (Ignite g1 = G.start(cfg)) {
IgniteCompute compute = compute(grid().cluster().forNode(g1.cluster().localNode()));
compute.run(new IgniteRunnable() {
@Override
public void run() {
// No-op.
}
});
GridMarshallerTestBean inBean = newTestBean(compute);
byte[] buf = marshal(inBean);
GridMarshallerTestBean outBean = unmarshal(buf);
assert inBean.getObjectField() != null;
assert outBean.getObjectField() != null;
assert inBean.getObjectField().getClass().equals(IgniteComputeImpl.class);
assert outBean.getObjectField().getClass().equals(IgniteComputeImpl.class);
assert inBean != outBean;
assert inBean.equals(outBean);
ClusterGroup inPrj = compute.clusterGroup();
ClusterGroup outPrj = ((IgniteCompute) outBean.getObjectField()).clusterGroup();
assert inPrj.getClass().equals(outPrj.getClass());
assert F.eqNotOrdered(inPrj.nodes(), outPrj.nodes());
outBean.checkNullResources();
}
}
use of org.apache.ignite.lang.IgniteRunnable in project ignite by apache.
the class TcpCommunicationSpiDropNodesTest method testTwoNodesEachOther.
/**
* @throws Exception If failed.
*/
public void testTwoNodesEachOther() throws Exception {
pred = new IgniteBiPredicate<ClusterNode, ClusterNode>() {
@Override
public boolean apply(ClusterNode locNode, ClusterNode rmtNode) {
return block && (locNode.order() == 2 || locNode.order() == 4) && (rmtNode.order() == 2 || rmtNode.order() == 4);
}
};
startGrids(NODES_CNT);
final CountDownLatch latch = new CountDownLatch(1);
grid(0).events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event event) {
latch.countDown();
return true;
}
}, EVT_NODE_FAILED);
// Wait for write timeout and closing idle connections.
U.sleep(1000);
block = true;
final CyclicBarrier barrier = new CyclicBarrier(2);
IgniteInternalFuture<Void> fut1 = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
barrier.await();
grid(1).compute().withNoFailover().broadcast(new IgniteRunnable() {
@Override
public void run() {
// No-op.
}
});
return null;
}
});
IgniteInternalFuture<Void> fut2 = GridTestUtils.runAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
barrier.await();
grid(3).compute().withNoFailover().broadcast(new IgniteRunnable() {
@Override
public void run() {
// No-op.
}
});
return null;
}
});
assertTrue(latch.await(5, TimeUnit.SECONDS));
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return grid(2).cluster().nodes().size() == NODES_CNT - 1;
}
}, 5000);
try {
fut1.get();
} catch (IgniteCheckedException e) {
// No-op.
}
try {
fut2.get();
} catch (IgniteCheckedException e) {
// No-op.
}
long failedNodeOrder = 1 + 2 + 3 + 4;
for (ClusterNode node : grid(0).cluster().nodes()) failedNodeOrder -= node.order();
for (int i = 0; i < 10; i++) {
U.sleep(1000);
assertEquals(NODES_CNT - 1, grid(0).cluster().nodes().size());
int liveNodesCnt = 0;
for (int j = 0; j < NODES_CNT; j++) {
IgniteEx ignite;
try {
ignite = grid(j);
log.info("Checking topology for grid(" + j + "): " + ignite.cluster().nodes());
ClusterNode locNode = ignite.localNode();
if (locNode.order() != failedNodeOrder) {
assertEquals(NODES_CNT - 1, ignite.cluster().nodes().size());
for (ClusterNode node : ignite.cluster().nodes()) assertTrue(node.order() != failedNodeOrder);
liveNodesCnt++;
}
} catch (Exception e) {
log.info("Checking topology for grid(" + j + "): no grid in topology.");
}
}
assertEquals(NODES_CNT - 1, liveNodesCnt);
}
}
Aggregations