use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class IgniteCacheThreadLocalTxTest method checkTx.
/**
* @param concurrency Tx concurrency.
* @param isolation Tx isolation.
* @param node Node.
* @param cache Cache.
* @param read {@code True} if read in tx.
* @param write {@code True} if write in tx.
* @param endOp Operation to test.
*/
private void checkTx(TransactionConcurrency concurrency, TransactionIsolation isolation, Ignite node, IgniteCache<Object, Object> cache, boolean read, boolean write, int endOp) {
IgniteTransactions txs = node.transactions();
checkNoTx(node);
Transaction tx = txs.txStart(concurrency, isolation);
assertEquals(tx, txs.tx());
try {
txs.txStart(concurrency, isolation);
fail();
} catch (IllegalStateException expected) {
// No-op.
}
if (read)
cache.get(ThreadLocalRandom.current().nextInt(100_000));
if (write)
cache.put(ThreadLocalRandom.current().nextInt(100_000), 1);
try {
txs.txStart(concurrency, isolation);
fail();
} catch (IllegalStateException expected) {
// No-op.
}
assertEquals(tx, txs.tx());
IgniteFuture fut = null;
switch(endOp) {
case 0:
tx.commit();
break;
case 1:
fut = tx.commitAsync();
break;
case 2:
tx.rollback();
break;
case 3:
fut = tx.rollbackAsync();
break;
case 4:
tx.close();
break;
default:
fail();
}
if (fut != null)
fut.get();
checkNoTx(node);
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class CacheAsyncOperationsFailoverAbstractTest method putAllAsyncFailover.
/**
* @param threads Number of threads.
* @param opsPerThread Number of concurrent async operations per thread.
* @throws Exception If failed.
*/
private void putAllAsyncFailover(final int threads, final int opsPerThread) throws Exception {
log.info("Start test [threads=" + threads + ", opsPerThread=" + opsPerThread + ']');
final AtomicBoolean finished = new AtomicBoolean();
final long endTime = System.currentTimeMillis() + TEST_TIME;
IgniteInternalFuture<Object> restartFut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
Thread.currentThread().setName("restart-thread");
while (!finished.get() && System.currentTimeMillis() < endTime) {
startGrid(NODE_CNT);
U.sleep(500);
stopGrid(NODE_CNT);
}
return null;
}
});
try {
final IgniteCache<TestKey, TestValue> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
GridTestUtils.runMultiThreaded(new Callable<Object>() {
@Override
public Object call() throws Exception {
int iter = 0;
ThreadLocalRandom rnd = ThreadLocalRandom.current();
long time;
long lastInfo = 0;
while ((time = System.currentTimeMillis()) < endTime) {
if (time - lastInfo > 5000)
log.info("Starting operations [iter=" + iter + ']');
List<IgniteFuture<?>> futs = new ArrayList<>(opsPerThread);
for (int i = 0; i < opsPerThread; i++) {
TreeMap<TestKey, TestValue> map = new TreeMap<>();
int keys = rnd.nextInt(1, 50);
for (int k = 0; k < keys; k++) map.put(new TestKey(rnd.nextInt(10_000)), new TestValue(iter));
IgniteFuture<?> fut = cache.putAllAsync(map);
assertNotNull(fut);
futs.add(fut);
}
if (time - lastInfo > 5000) {
log.info("Waiting for futures [iter=" + iter + ']');
lastInfo = time;
}
for (IgniteFuture<?> fut : futs) fut.get();
iter++;
}
return null;
}
}, threads, "update-thread");
finished.set(true);
restartFut.get();
} finally {
finished.set(true);
}
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class IgniteFutureImplTest method testChainAsync.
/**
* @throws Exception If failed.
*/
public void testChainAsync() throws Exception {
GridFutureAdapter<String> fut0 = new GridFutureAdapter<>();
IgniteFuture<String> fut = createFuture(fut0);
C1<IgniteFuture<String>, Integer> chainClos = new C1<IgniteFuture<String>, Integer>() {
@Override
public Integer apply(IgniteFuture<String> fut) {
assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName());
return Integer.valueOf(fut.get());
}
};
IgniteFuture<Integer> chained1 = fut.chainAsync(chainClos, customExec);
assertFalse(chained1.isDone());
final CountDownLatch latch = new CountDownLatch(1);
class TestClosure implements CI1<IgniteFuture<Integer>> {
private final CountDownLatch latch;
private TestClosure(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void apply(IgniteFuture<Integer> fut) {
assertEquals(CUSTOM_THREAD_NAME, Thread.currentThread().getName());
assertEquals(10, (int) fut.get());
latch.countDown();
}
}
chained1.listen(new TestClosure(latch));
fut0.onDone("10");
// Chained future will be completed asynchronously.
chained1.get(100, TimeUnit.MILLISECONDS);
assertTrue(chained1.isDone());
assertEquals(10, (int) chained1.get());
assert latch.await(100, TimeUnit.MILLISECONDS);
assertTrue(fut.isDone());
assertEquals("10", fut.get());
// Test finished future
GridFinishedFuture<String> ffut0 = new GridFinishedFuture<>("10");
CountDownLatch latch1 = new CountDownLatch(1);
IgniteFuture<Integer> chained2 = createFuture(ffut0).chainAsync(chainClos, customExec);
chained2.listen(new TestClosure(latch1));
chained2.get(100, TimeUnit.MILLISECONDS);
assertTrue(chained2.isDone());
assertEquals(10, (int) chained2.get());
assert latch1.await(100, TimeUnit.MILLISECONDS);
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class IgniteFutureImplTest method testChain.
/**
* @throws Exception If failed.
*/
public void testChain() throws Exception {
GridFutureAdapter<String> fut0 = new GridFutureAdapter<>();
IgniteFutureImpl<String> fut = createFuture(fut0);
IgniteFuture<Integer> chained = fut.chain(new C1<IgniteFuture<String>, Integer>() {
@Override
public Integer apply(IgniteFuture<String> fut) {
return Integer.valueOf(fut.get());
}
});
assertFalse(chained.isDone());
U.sleep(100);
final AtomicInteger lsnrCnt = new AtomicInteger();
chained.listen(new CI1<IgniteFuture<Integer>>() {
@Override
public void apply(IgniteFuture<Integer> fut) {
assertEquals(10, (int) fut.get());
lsnrCnt.incrementAndGet();
}
});
fut0.onDone("10");
assertTrue(chained.isDone());
assertEquals(10, (int) chained.get());
assertEquals(1, lsnrCnt.get());
assertTrue(fut.isDone());
assertEquals("10", fut.get());
}
use of org.apache.ignite.lang.IgniteFuture in project ignite by apache.
the class ComputeUtils method affinityCallWithRetries.
/**
* Calls the specified {@code fun} function on all partitions so that is't guaranteed that partitions with the same
* index of all specified caches will be placed on the same node and will not be moved before computation is
* finished. If partitions are placed on different nodes then call will be retried, but not more than {@code
* retries} times with {@code interval} interval specified in milliseconds.
*
* @param ignite Ignite instance.
* @param cacheNames Collection of cache names.
* @param fun Function to be applied on all partitions.
* @param retries Number of retries for the case when one of partitions not found on the node.
* @param interval Interval of retries for the case when one of partitions not found on the node.
* @param <R> Type of a result.
* @return Collection of results.
*/
public static <R> Collection<R> affinityCallWithRetries(Ignite ignite, Collection<String> cacheNames, IgniteFunction<Integer, R> fun, int retries, int interval) {
assert cacheNames.size() > 0;
assert interval >= 0;
String primaryCache = cacheNames.iterator().next();
Affinity<?> affinity = ignite.affinity(primaryCache);
int partitions = affinity.partitions();
BitSet completionFlags = new BitSet(partitions);
Collection<R> results = new ArrayList<>();
for (int t = 0; t <= retries; t++) {
ClusterGroup clusterGrp = ignite.cluster().forDataNodes(primaryCache);
// Sends jobs.
Map<Integer, IgniteFuture<R>> futures = new HashMap<>();
for (int part = 0; part < partitions; part++) if (!completionFlags.get(part)) {
final int currPart = part;
futures.put(currPart, ignite.compute(clusterGrp).affinityCallAsync(cacheNames, currPart, () -> fun.apply(currPart)));
}
// Collects results.
for (int part : futures.keySet()) try {
R res = futures.get(part).get();
results.add(res);
completionFlags.set(part);
} catch (IgniteException ignore) {
}
if (completionFlags.cardinality() == partitions)
return results;
LockSupport.parkNanos(interval * 1_000_000);
}
throw new IllegalStateException();
}
Aggregations