use of org.apache.ignite.resources.LoggerResource in project ignite by apache.
the class GridCachePutAllTask method map.
/** {@inheritDoc} */
@Override
public Map<? extends ComputeJob, ClusterNode> map(List<ClusterNode> subgrid, @Nullable final Collection<Integer> data) {
assert !subgrid.isEmpty();
// Give preference to wanted node. Otherwise, take the first one.
ClusterNode targetNode = F.find(subgrid, subgrid.get(0), new IgnitePredicate<ClusterNode>() {
/** {@inheritDoc} */
@Override
public boolean apply(ClusterNode e) {
return preferredNode.equals(e.id());
}
});
return Collections.singletonMap(new ComputeJobAdapter() {
@LoggerResource
private IgniteLogger log;
@IgniteInstanceResource
private Ignite ignite;
@Override
public Object execute() {
if (DEBUG_DATA)
log.info("Going to put data: " + data);
else
log.info("Going to put data [size=" + data.size() + ']');
IgniteCache<Object, Object> cache = ignite.cache(cacheName);
assert cache != null;
HashMap<Integer, Integer> putMap = U.newLinkedHashMap(TX_BOUND);
Iterator<Integer> it = data.iterator();
int cnt = 0;
final int RETRIES = 5;
while (it.hasNext()) {
Integer val = it.next();
putMap.put(val, val);
if (++cnt == TX_BOUND) {
if (DEBUG_DATA)
log.info("Putting keys to cache: " + putMap.keySet());
else
log.info("Putting keys to cache [size=" + putMap.size() + ']');
for (int i = 0; i < RETRIES; i++) {
try {
cache.putAll(putMap);
break;
} catch (CacheException e) {
if (i < RETRIES - 1)
log.info("Put error, will retry: " + e);
else
throw new IgniteException(e);
}
}
cnt = 0;
putMap = U.newLinkedHashMap(TX_BOUND);
}
}
assert cnt < TX_BOUND;
assert putMap.size() == (data.size() % TX_BOUND) : "putMap.size() = " + putMap.size();
if (DEBUG_DATA)
log.info("Putting keys to cache: " + putMap.keySet());
else
log.info("Putting keys to cache [size=" + putMap.size() + ']');
for (int i = 0; i < RETRIES; i++) {
try {
cache.putAll(putMap);
break;
} catch (CacheException e) {
if (i < RETRIES - 1)
log.info("Put error, will retry: " + e);
else
throw new IgniteException(e);
}
}
if (DEBUG_DATA)
log.info("Finished putting data: " + data);
else
log.info("Finished putting data [size=" + data.size() + ']');
return data;
}
}, targetNode);
}
use of org.apache.ignite.resources.LoggerResource in project ignite by apache.
the class IgniteLockAbstractSelfTest method checkReentrantLock.
/**
* @throws Exception If failed.
*/
private void checkReentrantLock(final boolean fair) throws Exception {
// Test API.
checkLock(fair);
checkFailoverSafe(fair);
// Test main functionality.
IgniteLock lock1 = grid(0).reentrantLock("lock", true, fair, true);
assertFalse(lock1.isLocked());
lock1.lock();
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 reentrant lock in multiple threads on each node.
IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
IgniteLock lock = ignite.reentrantLock("lock", true, fair, true);
assert lock != null;
log.info("Thread is going to wait on reentrant lock: " + Thread.currentThread().getName());
assert lock.tryLock(1, MINUTES);
log.info("Thread is again runnable: " + Thread.currentThread().getName());
lock.unlock();
return null;
}
}, 5, "test-thread");
fut.get();
return null;
}
});
Thread.sleep(3000);
assert lock1.isHeldByCurrentThread();
assert lock1.getHoldCount() == 1;
lock1.lock();
assert lock1.isHeldByCurrentThread();
assert lock1.getHoldCount() == 2;
lock1.unlock();
lock1.unlock();
// Ensure there are no hangs.
fut.get();
// Test operations on removed lock.
lock1.close();
checkRemovedReentrantLock(lock1);
}
use of org.apache.ignite.resources.LoggerResource in project ignite by apache.
the class IgniteCountDownLatchAbstractSelfTest method checkLatch.
/**
* @throws Exception If failed.
*/
private void checkLatch() throws Exception {
// Test API.
checkAutoDelete();
checkAwait();
checkCountDown();
// Test main functionality.
IgniteCountDownLatch latch1 = grid(0).countDownLatch("latch", 2, false, true);
assertEquals(2, latch1.count());
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 latch in multiple threads on each node.
IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
IgniteCountDownLatch latch = ignite.countDownLatch("latch", 2, false, true);
assert latch != null && latch.count() == 2;
log.info("Thread is going to wait on latch: " + Thread.currentThread().getName());
assert latch.await(1, MINUTES);
log.info("Thread is again runnable: " + Thread.currentThread().getName());
return null;
}
}, 5, "test-thread");
fut.get();
return null;
}
});
Thread.sleep(3000);
assert latch1.countDown() == 1;
assert latch1.countDown() == 0;
// Ensure there are no hangs.
fut.get();
// Test operations on removed latch.
latch1.close();
checkRemovedLatch(latch1);
}
use of org.apache.ignite.resources.LoggerResource in project ignite by apache.
the class GridLoggerInjectionSelfTest method testClosureField.
/**
* Test that closure gets right log category injected on all nodes using field injection.
*
* @throws Exception If failed.
*/
public void testClosureField() throws Exception {
Ignite ignite = grid(0);
ignite.compute().call(new IgniteCallable<Object>() {
@LoggerResource(categoryClass = GridLoggerInjectionSelfTest.class)
private IgniteLogger log;
@Override
public Object call() throws Exception {
if (log instanceof GridLoggerProxy) {
Object category = U.field(log, "ctgr");
assertTrue("Logger created for the wrong category.", category.toString().contains(GridLoggerInjectionSelfTest.class.getName()));
} else
fail("This test should be run with proxy logger.");
return null;
}
});
}
Aggregations