use of org.apache.ignite.lang.IgniteOutClosure in project ignite by apache.
the class GridCacheAdapter method asyncOp.
/**
* @param tx Transaction.
* @param op Cache operation.
* @param opCtx Cache operation context.
* @param <T> Return type.
* @return Future.
*/
@SuppressWarnings("unchecked")
protected <T> IgniteInternalFuture<T> asyncOp(GridNearTxLocal tx, final AsyncOp<T> op, final CacheOperationContext opCtx, final boolean retry) {
IgniteInternalFuture<T> fail = asyncOpAcquire(retry);
if (fail != null)
return fail;
FutureHolder holder = lastFut.get();
holder.lock();
try {
IgniteInternalFuture fut = holder.future();
final GridNearTxLocal tx0 = tx;
final CX1 clo = new CX1<IgniteInternalFuture<T>, T>() {
@Override
public T applyx(IgniteInternalFuture<T> tFut) throws IgniteCheckedException {
try {
return tFut.get();
} catch (IgniteTxTimeoutCheckedException | IgniteTxRollbackCheckedException | NodeStoppingException | IgniteConsistencyViolationException e) {
throw e;
} catch (IgniteCheckedException e1) {
try {
tx0.rollbackNearTxLocalAsync();
} catch (Throwable e2) {
if (e1 != e2)
e1.addSuppressed(e2);
}
throw e1;
} finally {
ctx.shared().txContextReset();
}
}
};
if (fut != null && !fut.isDone()) {
IgniteInternalFuture<T> f = new GridEmbeddedFuture(fut, (IgniteOutClosure<IgniteInternalFuture>) () -> {
GridFutureAdapter resFut = new GridFutureAdapter();
ctx.kernalContext().closure().runLocalSafe((GridPlainRunnable) () -> {
IgniteInternalFuture fut0;
if (ctx.kernalContext().isStopping())
fut0 = new GridFinishedFuture<>(new IgniteCheckedException("Operation has been cancelled (node or cache is stopping)."));
else if (ctx.gate().isStopped())
fut0 = new GridFinishedFuture<>(new CacheStoppedException(ctx.name()));
else {
ctx.operationContextPerCall(opCtx);
ctx.shared().txContextReset();
try {
fut0 = op.op(tx0).chain(clo);
} finally {
// It is necessary to clear tx context in this thread as well.
ctx.shared().txContextReset();
ctx.operationContextPerCall(null);
}
}
fut0.listen((IgniteInClosure<IgniteInternalFuture>) fut01 -> {
try {
resFut.onDone(fut01.get());
} catch (Throwable ex) {
resFut.onDone(ex);
}
});
}, true);
return resFut;
});
saveFuture(holder, f, retry);
return f;
}
/**
* Wait for concurrent tx operation to finish.
* See {@link GridDhtTxLocalAdapter#updateLockFuture(IgniteInternalFuture, IgniteInternalFuture)}
*/
if (!tx0.txState().implicitSingle())
tx0.txState().awaitLastFuture(ctx.shared());
IgniteInternalFuture<T> f;
try {
f = op.op(tx).chain(clo);
} finally {
// It is necessary to clear tx context in this thread as well.
ctx.shared().txContextReset();
}
saveFuture(holder, f, retry);
if (tx.implicit())
ctx.tm().resetContext();
return f;
} finally {
holder.unlock();
}
}
use of org.apache.ignite.lang.IgniteOutClosure in project ignite by apache.
the class IgniteCacheDatabaseSharedManager method freeSpaceProvider.
/**
* Closure that can be used to compute fill factor for provided data region.
*
* @param dataRegCfg Data region configuration.
* @return Closure.
*
* @deprecated use {@link #dataRegionMetricsProvider(DataRegionConfiguration)} instead.
*/
@Deprecated
protected IgniteOutClosure<Long> freeSpaceProvider(final DataRegionConfiguration dataRegCfg) {
final String dataRegName = dataRegCfg.getName();
return new IgniteOutClosure<Long>() {
private CacheFreeList freeList;
@Override
public Long apply() {
if (freeList == null) {
CacheFreeList freeList0 = freeListMap.get(dataRegName);
if (freeList0 == null)
return 0L;
freeList = freeList0;
}
return freeList.freeSpace();
}
};
}
use of org.apache.ignite.lang.IgniteOutClosure in project ignite by apache.
the class CacheContinuousWithTransformerFailoverTest method testServerNodeLeft.
/**
* @throws Exception If failed.
*/
@Test
public void testServerNodeLeft() throws Exception {
startGrids(3);
final int CLIENT_ID = 3;
Ignite clnNode = startClientGrid(CLIENT_ID);
IgniteOutClosure<IgniteCache<Integer, Integer>> cache = new IgniteOutClosure<IgniteCache<Integer, Integer>>() {
int cnt = 0;
@Override
public IgniteCache<Integer, Integer> apply() {
++cnt;
return grid(CLIENT_ID).cache(DEFAULT_CACHE_NAME);
}
};
final CacheEventListener lsnr = new CacheEventListener();
ContinuousQueryWithTransformer<Object, Object, String> qry = new ContinuousQueryWithTransformer<>();
qry.setLocalListener(lsnr);
qry.setRemoteTransformerFactory(FactoryBuilder.factoryOf(new IgniteClosure<CacheEntryEvent<?, ?>, String>() {
@Override
public String apply(CacheEntryEvent<?, ?> evt) {
return "" + evt.getKey() + evt.getValue();
}
}));
QueryCursor<?> cur = clnNode.cache(DEFAULT_CACHE_NAME).query(qry);
boolean first = true;
int keyCnt = 1;
for (int i = 0; i < 10; i++) {
log.info("Start iteration: " + i);
if (first)
first = false;
else {
for (int srv = 0; srv < CLIENT_ID - 1; srv++) startGrid(srv);
}
lsnr.latch = new CountDownLatch(keyCnt);
for (int key = 0; key < keyCnt; key++) cache.apply().put(key, key);
assertTrue("Failed to wait for event. Left events: " + lsnr.latch.getCount(), lsnr.latch.await(10, SECONDS));
for (int srv = 0; srv < CLIENT_ID - 1; srv++) stopGrid(srv);
}
tryClose(cur);
}
Aggregations