use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridEventConsumeSelfTest method testResources.
/**
* @throws Exception If failed.
*/
public void testResources() throws Exception {
final Collection<UUID> nodeIds = new HashSet<>();
final AtomicInteger cnt = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(GRID_CNT);
UUID consumeId = grid(0).events().remoteListen(new P2<UUID, Event>() {
@IgniteInstanceResource
private Ignite grid;
@Override
public boolean apply(UUID nodeId, Event evt) {
info("Event from " + nodeId + " [" + evt.shortDisplay() + ']');
assertEquals(EVT_JOB_STARTED, evt.type());
assertNotNull(grid);
nodeIds.add(nodeId);
cnt.incrementAndGet();
latch.countDown();
return true;
}
}, new P1<Event>() {
@IgniteInstanceResource
private Ignite grid;
@Override
public boolean apply(Event evt) {
assertNotNull(grid);
return true;
}
}, EVT_JOB_STARTED);
try {
assertNotNull(consumeId);
grid(0).compute().broadcast(F.noop());
assert latch.await(2, SECONDS);
assertEquals(GRID_CNT, nodeIds.size());
assertEquals(GRID_CNT, cnt.get());
} finally {
grid(0).events().stopRemoteListen(consumeId);
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class IgniteExecutorServiceTest method testExecute.
/**
* @throws Exception Thrown in case of test failure.
*/
public void testExecute() throws Exception {
Ignite ignite = G.ignite(getTestIgniteInstanceName());
ExecutorService srvc = createExecutorService(ignite);
srvc.execute(new Runnable() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public void run() {
System.out.println("Test message.");
assert this.ignite != null;
}
});
srvc.execute(new TestRunnable());
srvc.shutdown();
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class IgniteSemaphoreAbstractSelfTest method checkSemaphore.
/**
* @throws Exception If failed.
*/
private void checkSemaphore() throws Exception {
// Test API.
checkAcquire();
checkRelease();
checkFailoverSafe();
// Test main functionality.
IgniteSemaphore semaphore1 = grid(0).semaphore("semaphore", -2, true, true);
assertEquals(-2, semaphore1.availablePermits());
IgniteFuture<Object> fut = grid(0).compute().callAsync(new IgniteCallable<Object>() {
@IgniteInstanceResource
private Ignite ignite;
@LoggerResource
private IgniteLogger log;
@Nullable
@Override
public Object call() throws Exception {
// Test semaphore in multiple threads on each node.
IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
IgniteSemaphore semaphore = ignite.semaphore("semaphore", -2, true, true);
assert semaphore != null && semaphore.availablePermits() == -2;
log.info("Thread is going to wait on semaphore: " + Thread.currentThread().getName());
assert semaphore.tryAcquire(1, 1, MINUTES);
log.info("Thread is again runnable: " + Thread.currentThread().getName());
semaphore.release();
return null;
}
}, 5, "test-thread");
fut.get();
return null;
}
});
Thread.sleep(3000);
semaphore1.release(2);
assert semaphore1.availablePermits() == 0;
semaphore1.release(1);
// Ensure there are no hangs.
fut.get();
// Test operations on removed semaphore.
semaphore1.close();
checkRemovedSemaphore(semaphore1);
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class GridCacheDhtMultiBackupTest method testPut.
/**
* @throws Exception If failed
*/
public void testPut() throws Exception {
try {
Ignite g = G.start("examples/config/example-cache.xml");
if (g.cluster().nodes().size() < 5)
U.warn(log, "Topology is too small for this test. " + "Run with 4 remote nodes or more having large number of backup nodes.");
g.compute().run(new CAX() {
@IgniteInstanceResource
private Ignite g;
@Override
public void applyx() {
X.println("Checking whether cache is empty.");
IgniteCache<SampleKey, SampleValue> cache = g.cache("partitioned");
assert cache.localSize() == 0;
}
});
IgniteCache<SampleKey, SampleValue> cache = g.cache("partitioned");
int cnt = 0;
for (int key = 0; key < 1000; key++) {
SampleKey key1 = new SampleKey(key);
if (!g.cluster().localNode().id().equals(g.affinity("partitioned").mapKeyToNode(key1).id())) {
cache.put(key1, new SampleValue(key));
cnt++;
}
}
X.println(">>> Put count: " + cnt);
} finally {
G.stopAll(false);
}
}
use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.
the class ComputeBroadcastExample method gatherSystemInfo.
/**
* Gather system info from all nodes and print it out.
*
* @param ignite Ignite instance.
* @throws IgniteException if failed.
*/
private static void gatherSystemInfo(Ignite ignite) throws IgniteException {
// Gather system info from all nodes.
Collection<String> res = ignite.compute().broadcast(new IgniteCallable<String>() {
// Automatically inject ignite instance.
@IgniteInstanceResource
private Ignite ignite;
@Override
public String call() {
System.out.println();
System.out.println("Executing task on node: " + ignite.cluster().localNode().id());
return "Node ID: " + ignite.cluster().localNode().id() + "\n" + "OS: " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch") + "\n" + "User: " + System.getProperty("user.name") + "\n" + "JRE: " + System.getProperty("java.runtime.name") + " " + System.getProperty("java.runtime.version");
}
});
// Print result.
System.out.println();
System.out.println("Nodes system information:");
System.out.println();
for (String r : res) {
System.out.println(r);
System.out.println();
}
}
Aggregations