use of org.apache.ignite.internal.util.typedef.CX1 in project ignite by apache.
the class GridDhtColocatedCache method getAsync.
/**
* {@inheritDoc}
*/
@Override
protected IgniteInternalFuture<V> getAsync(final K key, boolean forcePrimary, boolean skipTx, String taskName, final boolean deserializeBinary, final boolean skipVals, final boolean needVer) {
ctx.checkSecurity(SecurityPermission.CACHE_READ);
GridNearTxLocal tx = checkCurrentTx();
final CacheOperationContext opCtx = ctx.operationContextPerCall();
final boolean recovery = opCtx != null && opCtx.recovery();
final ReadRepairStrategy readRepairStrategy = opCtx != null ? opCtx.readRepairStrategy() : null;
// Get operation bypass Tx in Mvcc mode.
if (!ctx.mvccEnabled() && tx != null && !tx.implicit() && !skipTx) {
return asyncOp(tx, new AsyncOp<V>() {
@Override
public IgniteInternalFuture<V> op(GridNearTxLocal tx, AffinityTopologyVersion readyTopVer) {
IgniteInternalFuture<Map<Object, Object>> fut = tx.getAllAsync(ctx, readyTopVer, Collections.singleton(ctx.toCacheKeyObject(key)), deserializeBinary, skipVals, false, opCtx != null && opCtx.skipStore(), recovery, readRepairStrategy, needVer);
return fut.chain(new CX1<IgniteInternalFuture<Map<Object, Object>>, V>() {
@Override
public V applyx(IgniteInternalFuture<Map<Object, Object>> e) throws IgniteCheckedException {
Map<Object, Object> map = e.get();
assert map.isEmpty() || map.size() == 1 : map.size();
if (skipVals) {
Boolean val = map.isEmpty() ? false : (Boolean) F.firstValue(map);
return (V) (val);
}
return (V) F.firstValue(map);
}
});
}
}, opCtx, /*retry*/
false);
}
MvccSnapshot mvccSnapshot = null;
MvccQueryTracker mvccTracker = null;
if (ctx.mvccEnabled()) {
try {
if (tx != null)
mvccSnapshot = MvccUtils.requestSnapshot(tx);
else {
mvccTracker = MvccUtils.mvccTracker(ctx, null);
mvccSnapshot = mvccTracker.snapshot();
}
assert mvccSnapshot != null;
} catch (IgniteCheckedException ex) {
return new GridFinishedFuture<>(ex);
}
}
AffinityTopologyVersion topVer;
if (tx != null)
topVer = tx.topologyVersion();
else if (mvccTracker != null)
topVer = mvccTracker.topologyVersion();
else
topVer = ctx.affinity().affinityTopologyVersion();
if (readRepairStrategy != null) {
return new GridNearReadRepairCheckOnlyFuture(ctx, Collections.singleton(ctx.toCacheKeyObject(key)), readRepairStrategy, opCtx == null || !opCtx.skipStore(), taskName, deserializeBinary, recovery, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, false, tx).single();
}
GridPartitionedSingleGetFuture fut = new GridPartitionedSingleGetFuture(ctx, ctx.toCacheKeyObject(key), topVer, opCtx == null || !opCtx.skipStore(), forcePrimary, taskName, deserializeBinary, skipVals ? null : expiryPolicy(opCtx != null ? opCtx.expiry() : null), skipVals, needVer, /*keepCacheObjects*/
false, opCtx != null && opCtx.recovery(), null, mvccSnapshot);
fut.init();
if (mvccTracker != null) {
final MvccQueryTracker mvccTracker0 = mvccTracker;
fut.listen(new CI1<IgniteInternalFuture<Object>>() {
@Override
public void apply(IgniteInternalFuture<Object> future) {
if (future.isDone())
mvccTracker0.onDone();
}
});
}
return (IgniteInternalFuture<V>) fut;
}
use of org.apache.ignite.internal.util.typedef.CX1 in project ignite by apache.
the class GridFutureAdapterSelfTest method checkChaining.
/**
* @param exec Executor for chain callback.
* @throws Exception If failed.
*/
@SuppressWarnings("ErrorNotRethrown")
private void checkChaining(ExecutorService exec) throws Exception {
final CX1<IgniteInternalFuture<Object>, Object> passThrough = new CX1<IgniteInternalFuture<Object>, Object>() {
@Override
public Object applyx(IgniteInternalFuture<Object> f) throws IgniteCheckedException {
return f.get();
}
};
GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
IgniteInternalFuture<Object> chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
assertFalse(fut.isDone());
assertFalse(chain.isDone());
try {
chain.get(20);
fail("Expects timeout exception.");
} catch (IgniteFutureTimeoutCheckedException e) {
info("Expected timeout exception: " + e.getMessage());
}
fut.onDone("result");
assertEquals("result", exec == null ? chain.get(1) : chain.get());
// Test exception re-thrown.
fut = new GridFutureAdapter<>();
chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
fut.onDone(new ClusterGroupEmptyCheckedException("test exception"));
try {
chain.get();
fail("Expects failed with exception.");
} catch (ClusterGroupEmptyCheckedException e) {
info("Expected exception: " + e.getMessage());
}
// Test error re-thrown.
fut = new GridFutureAdapter<>();
chain = exec != null ? fut.chain(passThrough, exec) : fut.chain(passThrough);
try {
fut.onDone(new StackOverflowError("test error"));
if (exec == null)
fail("Expects failed with error.");
} catch (StackOverflowError e) {
info("Expected error: " + e.getMessage());
}
try {
chain.get();
fail("Expects failed with error.");
} catch (StackOverflowError e) {
info("Expected error: " + e.getMessage());
}
}
use of org.apache.ignite.internal.util.typedef.CX1 in project ignite by apache.
the class GridCacheAbstractJobExecutionTest method checkTransactions.
/**
* @param concur Concurrency.
* @param isolation Isolation.
* @param jobCnt Job count.
* @throws Exception If fails.
*/
private void checkTransactions(final TransactionConcurrency concur, final TransactionIsolation isolation, final int jobCnt) throws Exception {
info("Grid 0: " + grid(0).localNode().id());
info("Grid 1: " + grid(1).localNode().id());
info("Grid 2: " + grid(2).localNode().id());
info("Grid 3: " + grid(3).localNode().id());
Ignite ignite = grid(0);
Collection<IgniteFuture<?>> futs = new LinkedList<>();
final String key = "TestKey";
info("Primary node for test key: " + grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key));
for (int i = 0; i < jobCnt; i++) {
futs.add(ignite.compute().applyAsync(new CX1<Integer, Void>() {
@IgniteInstanceResource
private Ignite ignite;
@Override
public Void applyx(final Integer i) {
IgniteCache<String, int[]> cache = ignite.cache(DEFAULT_CACHE_NAME);
try (Transaction tx = ignite.transactions().txStart(concur, isolation)) {
int[] arr = cache.get(key);
if (arr == null)
arr = new int[jobCnt];
arr[i] = 1;
cache.put(key, arr);
int c = cntr.getAndIncrement();
if (c % 50 == 0)
X.println("Executing transaction [i=" + i + ", c=" + c + ']');
tx.commit();
}
return null;
}
}, i));
}
for (IgniteFuture<?> fut : futs) // Wait for completion.
fut.get();
for (int i = 0; i < GRID_CNT; i++) {
info("Running iteration: " + i);
for (int g = 0; g < GRID_CNT; g++) {
info("Will check grid: " + g);
info("Value: " + grid(i).cache(DEFAULT_CACHE_NAME).localPeek(key));
}
IgniteCache<String, int[]> c = grid(i).cache(DEFAULT_CACHE_NAME);
// which means that all previous transactions have committed.
try (Transaction tx = grid(i).transactions().txStart(concur, isolation)) {
int[] arr = c.get(key);
assertNotNull(arr);
assertEquals(jobCnt, arr.length);
for (int j : arr) assertEquals(1, j);
tx.commit();
}
}
}
Aggregations