use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.
the class GridContinuousProcessor method addNotification.
/**
* @param nodeId ID of the node that started routine.
* @param routineId Routine ID.
* @param obj Notification object.
* @param orderedTopic Topic for ordered notifications. If {@code null}, non-ordered message will be sent.
* @param sync If {@code true} then waits for event acknowledgment.
* @param msg If {@code true} then sent data is message.
* @throws IgniteCheckedException In case of error.
*/
public void addNotification(UUID nodeId, final UUID routineId, @Nullable Object obj, @Nullable Object orderedTopic, boolean sync, boolean msg) throws IgniteCheckedException {
assert nodeId != null;
assert routineId != null;
assert !msg || (obj instanceof Message || obj instanceof Collection) : obj;
assert !nodeId.equals(ctx.localNodeId());
if (processorStopped)
return;
final RemoteRoutineInfo info = rmtInfos.get(routineId);
if (info != null) {
assert info.interval == 0 || !sync;
if (sync) {
SyncMessageAckFuture fut = new SyncMessageAckFuture(nodeId);
IgniteUuid futId = IgniteUuid.randomUuid();
syncMsgFuts.put(futId, fut);
try {
sendNotification(nodeId, routineId, futId, obj instanceof Collection ? (Collection) obj : F.asList(obj), null, msg, null);
info.hnd.onBatchAcknowledged(routineId, info.add(obj), ctx);
} catch (IgniteCheckedException e) {
syncMsgFuts.remove(futId);
throw e;
}
while (true) {
try {
fut.get(100, TimeUnit.MILLISECONDS);
break;
} catch (IgniteFutureTimeoutCheckedException ignored) {
// in case left/fail event processing failed, hanged or delayed.
if (!ctx.discovery().alive(nodeId)) {
SyncMessageAckFuture fut0 = syncMsgFuts.remove(futId);
if (fut0 != null) {
ClusterTopologyCheckedException err = new ClusterTopologyCheckedException("Node left grid after receiving, but before processing the message [node=" + nodeId + "]");
fut0.onDone(err);
}
break;
}
LT.warn(log, "Failed to wait for ack message. [node=" + nodeId + ", routine=" + routineId + "]");
}
}
assert fut.isDone() : "Future in not finished [fut= " + fut + "]";
} else {
final GridContinuousBatch batch = info.add(obj);
if (batch != null) {
CI1<IgniteException> ackC = new CI1<IgniteException>() {
@Override
public void apply(IgniteException e) {
if (e == null)
info.hnd.onBatchAcknowledged(routineId, batch, ctx);
}
};
sendNotification(nodeId, routineId, null, batch.collect(), orderedTopic, msg, ackC);
}
}
}
}
use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.
the class GridContinuousProcessor method registerHandler.
/**
* @param nodeId Node ID.
* @param routineId Consume ID.
* @param hnd Handler.
* @param bufSize Buffer size.
* @param interval Time interval.
* @param autoUnsubscribe Automatic unsubscribe flag.
* @param loc Local registration flag.
* @return Whether listener was actually registered.
* @throws IgniteCheckedException In case of error.
*/
private boolean registerHandler(final UUID nodeId, final UUID routineId, final GridContinuousHandler hnd, int bufSize, final long interval, boolean autoUnsubscribe, boolean loc) throws IgniteCheckedException {
assert nodeId != null;
assert routineId != null;
assert hnd != null;
assert bufSize > 0;
assert interval >= 0;
final RemoteRoutineInfo info = new RemoteRoutineInfo(nodeId, hnd, bufSize, interval, autoUnsubscribe);
boolean doRegister = loc;
if (!doRegister) {
stopLock.lock();
try {
doRegister = !stopped.remove(routineId) && rmtInfos.putIfAbsent(routineId, info) == null;
} finally {
stopLock.unlock();
}
}
if (doRegister) {
if (log.isDebugEnabled())
log.debug("Register handler: [nodeId=" + nodeId + ", routineId=" + routineId + ", info=" + info + ']');
if (interval > 0) {
IgniteThread checker = new IgniteThread(new GridWorker(ctx.igniteInstanceName(), "continuous-buffer-checker", log) {
@Override
protected void body() {
long interval0 = interval;
while (!isCancelled()) {
try {
U.sleep(interval0);
} catch (IgniteInterruptedCheckedException ignored) {
break;
}
IgniteBiTuple<GridContinuousBatch, Long> t = info.checkInterval();
final GridContinuousBatch batch = t.get1();
if (batch != null && batch.size() > 0) {
try {
Collection<Object> toSnd = batch.collect();
boolean msg = toSnd.iterator().next() instanceof Message;
CI1<IgniteException> ackC = new CI1<IgniteException>() {
@Override
public void apply(IgniteException e) {
if (e == null)
info.hnd.onBatchAcknowledged(routineId, batch, ctx);
}
};
sendNotification(nodeId, routineId, null, toSnd, hnd.orderedTopic(), msg, ackC);
} catch (ClusterTopologyCheckedException ignored) {
if (log.isDebugEnabled())
log.debug("Failed to send notification to node (is node alive?): " + nodeId);
} catch (IgniteCheckedException e) {
U.error(log, "Failed to send notification to node: " + nodeId, e);
}
}
interval0 = t.get2();
}
}
});
checker.setUncaughtExceptionHandler(new OomExceptionHandler(ctx));
bufCheckThreads.put(routineId, checker);
checker.start();
}
GridContinuousHandler.RegisterStatus status = hnd.register(nodeId, routineId, ctx);
if (status == GridContinuousHandler.RegisterStatus.DELAYED) {
info.markDelayedRegister();
return false;
} else
return status == GridContinuousHandler.RegisterStatus.REGISTERED;
}
return false;
}
use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.
the class GridTcpRestNioListener method onMessage.
/**
* {@inheritDoc}
*/
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
if (msg instanceof GridMemcachedMessage)
memcachedLsnr.onMessage(ses, (GridMemcachedMessage) msg);
else if (msg instanceof GridRedisMessage)
redisLsnr.onMessage(ses, (GridRedisMessage) msg);
else if (msg instanceof GridClientPingPacket)
ses.send(msg);
else if (msg instanceof GridClientHandshakeRequest) {
GridClientHandshakeRequest hs = (GridClientHandshakeRequest) msg;
short ver = hs.version();
if (!SUPP_VERS.contains(ver)) {
U.error(log, "Client protocol version is not supported [ses=" + ses + ", ver=" + ver + ", supported=" + SUPP_VERS + ']');
onSessionClosed(ses);
} else {
byte marshId = hs.marshallerId();
if (marshMapLatch.getCount() > 0) {
try {
U.await(marshMapLatch);
} catch (IgniteInterruptedCheckedException e) {
U.error(log, "Marshaller is not initialized.", e);
onSessionClosed(ses);
return;
}
}
GridClientMarshaller marsh = marshMap.get(marshId);
if (marsh == null) {
U.error(log, "Client marshaller ID is invalid. Note that .NET and C++ clients " + "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']');
onSessionClosed(ses);
} else {
ses.addMeta(MARSHALLER.ordinal(), marsh);
ses.send(GridClientHandshakeResponse.OK);
}
}
} else {
final GridRestRequest req = createRestRequest(ses, msg);
if (req != null) {
IgniteInternalFuture<GridRestResponse> taskFut = hnd.handleAsync(req);
if (isInterruptible(msg))
addFutureToSession(ses, taskFut);
taskFut.listen(new CI1<IgniteInternalFuture<GridRestResponse>>() {
@Override
public void apply(IgniteInternalFuture<GridRestResponse> fut) {
removeFutureFromSession(ses, taskFut);
GridClientResponse res = new GridClientResponse();
res.requestId(msg.requestId());
res.clientId(msg.clientId());
try {
GridRestResponse restRes = fut.get();
res.sessionToken(restRes.sessionTokenBytes());
res.successStatus(restRes.getSuccessStatus());
res.errorMessage(restRes.getError());
Object o = restRes.getResponse();
// In case of metrics a little adjustment is needed.
if (o instanceof GridCacheRestMetrics)
o = ((GridCacheRestMetrics) o).map();
res.result(o);
} catch (IgniteCheckedException e) {
U.error(log, "Failed to process client request: " + msg, e);
res.successStatus(GridClientResponse.STATUS_FAILED);
res.errorMessage("Failed to process client request: " + e.getMessage());
}
GridNioFuture<?> sf = ses.send(res);
// Check if send failed.
sf.listen(new CI1<IgniteInternalFuture<?>>() {
@Override
public void apply(IgniteInternalFuture<?> fut) {
try {
fut.get();
} catch (IgniteCheckedException e) {
U.error(log, "Failed to process client request [ses=" + ses + ", msg=" + msg + ']', e);
}
}
});
}
});
} else
U.error(log, "Failed to process client request (unknown packet type) [ses=" + ses + ", msg=" + msg + ']');
}
}
use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.
the class IgniteCacheDynamicStopSelfTest method checkStopStartCacheWithDataLoader.
/**
* @param allowOverwrite Allow overwrite flag for streamer.
* @throws Exception If failed.
*/
public void checkStopStartCacheWithDataLoader(final boolean allowOverwrite) throws Exception {
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setCacheMode(CacheMode.PARTITIONED);
ignite(0).createCache(ccfg);
final AtomicBoolean stop = new AtomicBoolean();
IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
/**
* {@inheritDoc}
*/
@Override
public Object call() throws Exception {
while (!stop.get()) {
try (IgniteDataStreamer<Integer, Integer> str = ignite(0).dataStreamer(DEFAULT_CACHE_NAME)) {
str.allowOverwrite(allowOverwrite);
int i = 0;
while (!stop.get()) {
try {
str.addData(i % 10_000, i).listen(new CI1<IgniteFuture<?>>() {
@Override
public void apply(IgniteFuture<?> f) {
try {
f.get();
} catch (CacheException ignore) {
// This may be debugged.
}
}
});
} catch (IllegalStateException ignored) {
break;
}
if (i > 0 && i % 10000 == 0)
info("Added: " + i);
i++;
}
} catch (IllegalStateException | CacheException ignored) {
// This may be debugged.
}
}
return null;
}
});
try {
Thread.sleep(500);
ignite(0).destroyCache(DEFAULT_CACHE_NAME);
Thread.sleep(500);
ignite(0).createCache(ccfg);
Thread.sleep(1000);
} finally {
stop.set(true);
}
fut.get();
int cnt = 0;
for (Cache.Entry<Object, Object> ignored : ignite(0).cache(DEFAULT_CACHE_NAME)) cnt++;
info(">>> cnt=" + cnt);
ignite(0).destroyCache(DEFAULT_CACHE_NAME);
}
use of org.apache.ignite.internal.util.typedef.CI1 in project ignite by apache.
the class IgniteFutureImplTest method testChainAsync.
/**
* @throws Exception If failed.
*/
@Test
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;
}
/**
* {@inheritDoc}
*/
@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);
}
Aggregations