use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteSourceTask method start.
/**
* Filtering is done remotely. Local listener buffers data for injection into Kafka.
*
* @param props Task properties.
*/
@Override
public void start(Map<String, String> props) {
synchronized (lock) {
// Nothing to do if the task has been already started.
if (!stopped)
return;
cacheName = props.get(IgniteSourceConstants.CACHE_NAME);
igniteCfgFile = props.get(IgniteSourceConstants.CACHE_CFG_PATH);
topics = props.get(IgniteSourceConstants.TOPIC_NAMES).split("\\s*,\\s*");
if (props.containsKey(IgniteSourceConstants.INTL_BUF_SIZE))
evtBufSize = Integer.parseInt(props.get(IgniteSourceConstants.INTL_BUF_SIZE));
if (props.containsKey(IgniteSourceConstants.INTL_BATCH_SIZE))
evtBatchSize = Integer.parseInt(props.get(IgniteSourceConstants.INTL_BATCH_SIZE));
if (props.containsKey(IgniteSourceConstants.CACHE_FILTER_CLASS)) {
String filterCls = props.get(IgniteSourceConstants.CACHE_FILTER_CLASS);
if (filterCls != null && !filterCls.isEmpty()) {
try {
Class<? extends IgnitePredicate<CacheEvent>> clazz = (Class<? extends IgnitePredicate<CacheEvent>>) Class.forName(filterCls);
filter = clazz.newInstance();
} catch (Exception e) {
log.error("Failed to instantiate the provided filter! " + "User-enabled filtering is ignored!", e);
}
}
}
TaskRemoteFilter rmtLsnr = new TaskRemoteFilter(cacheName);
try {
int[] evts = cacheEvents(props.get(IgniteSourceConstants.CACHE_EVENTS));
rmtLsnrId = IgniteGrid.getIgnite().events(IgniteGrid.getIgnite().cluster().forCacheNodes(cacheName)).remoteListen(locLsnr, rmtLsnr, evts);
} catch (Exception e) {
log.error("Failed to register event listener!", e);
throw new ConnectException(e);
} finally {
stopped = false;
}
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteSinkConnectorTest method testSinkPuts.
/**
* Tests the whole data flow from injecting data to Kafka to transferring it to the grid. It reads from two
* specified Kafka topics, because a sink task can read from multiple topics.
*
* @param sinkProps Sink properties.
* @param keyless Tests on Kafka stream with null keys if true.
* @throws Exception Thrown in case of the failure.
*/
private void testSinkPuts(Map<String, String> sinkProps, boolean keyless) throws Exception {
FutureCallback<Herder.Created<ConnectorInfo>> cb = new FutureCallback<>(new Callback<Herder.Created<ConnectorInfo>>() {
@Override
public void onCompletion(Throwable error, Herder.Created<ConnectorInfo> info) {
if (error != null)
throw new RuntimeException("Failed to create a job!");
}
});
herder.putConnectorConfig(sinkProps.get(ConnectorConfig.NAME_CONFIG), sinkProps, false, cb);
cb.get();
final CountDownLatch latch = new CountDownLatch(EVENT_CNT * TOPICS.length);
final IgnitePredicate<Event> putLsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
assert evt != null;
latch.countDown();
return true;
}
};
grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).localListen(putLsnr, EVT_CACHE_OBJECT_PUT);
IgniteCache<String, String> cache = grid.cache(CACHE_NAME);
assertEquals(0, cache.size(CachePeekMode.PRIMARY));
Map<String, String> keyValMap = new HashMap<>(EVENT_CNT * TOPICS.length);
// Produces events for the specified number of topics
for (String topic : TOPICS) keyValMap.putAll(produceStream(topic, keyless));
// Checks all events successfully processed in 10 seconds.
assertTrue(latch.await(10, TimeUnit.SECONDS));
grid.events(grid.cluster().forCacheNodes(CACHE_NAME)).stopLocalListen(putLsnr);
// Checks that each event was processed properly.
for (Map.Entry<String, String> entry : keyValMap.entrySet()) assertEquals(entry.getValue(), cache.get(entry.getKey()));
assertEquals(EVENT_CNT * TOPICS.length, cache.size(CachePeekMode.PRIMARY));
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class IgniteClientReconnectFailoverAbstractTest method reconnectFailover.
/**
* @param c Test closure.
* @throws Exception If failed.
*/
protected final void reconnectFailover(final Callable<Void> c) throws Exception {
final Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
Ignite srv = clientRouter(client);
TestTcpDiscoverySpi srvSpi = spi(srv);
final AtomicBoolean stop = new AtomicBoolean(false);
final IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
int iter = 0;
while (!stop.get()) {
try {
c.call();
} catch (CacheException e) {
checkAndWait(e);
} catch (IgniteClientDisconnectedException e) {
checkAndWait(e);
}
if (++iter % 100 == 0)
log.info("Iteration: " + iter);
if (barrier != null)
barrier.await();
}
return null;
} catch (Throwable e) {
log.error("Unexpected error in operation thread: " + e, e);
stop.set(true);
throw e;
}
}
}, THREADS, "test-operation-thread");
final AtomicReference<CountDownLatch> disconnected = new AtomicReference<>();
final AtomicReference<CountDownLatch> reconnected = new AtomicReference<>();
IgnitePredicate<Event> p = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
if (evt.type() == EVT_CLIENT_NODE_RECONNECTED) {
info("Reconnected: " + evt);
CountDownLatch latch = reconnected.get();
assertNotNull(latch);
assertEquals(1, latch.getCount());
latch.countDown();
} else if (evt.type() == EVT_CLIENT_NODE_DISCONNECTED) {
info("Disconnected: " + evt);
CountDownLatch latch = disconnected.get();
assertNotNull(latch);
assertEquals(1, latch.getCount());
latch.countDown();
}
return true;
}
};
client.events().localListen(p, EVT_CLIENT_NODE_DISCONNECTED, EVT_CLIENT_NODE_RECONNECTED);
try {
long stopTime = System.currentTimeMillis() + TEST_TIME;
String err = null;
while (System.currentTimeMillis() < stopTime && !fut.isDone()) {
U.sleep(500);
CountDownLatch disconnectLatch = new CountDownLatch(1);
CountDownLatch reconnectLatch = new CountDownLatch(1);
disconnected.set(disconnectLatch);
reconnected.set(reconnectLatch);
UUID nodeId = client.cluster().localNode().id();
log.info("Fail client: " + nodeId);
srvSpi.failNode(nodeId, null);
if (!disconnectLatch.await(10_000, MILLISECONDS)) {
err = "Failed to wait for disconnect";
break;
}
if (!reconnectLatch.await(10_000, MILLISECONDS)) {
err = "Failed to wait for reconnect";
break;
}
barrier = new CyclicBarrier(THREADS + 1, new Runnable() {
@Override
public void run() {
barrier = null;
}
});
try {
barrier.await(10, SECONDS);
} catch (TimeoutException ignored) {
err = "Operations hang or fail with unexpected error.";
break;
}
}
if (err != null) {
log.error(err);
U.dumpThreads(log);
CyclicBarrier barrier0 = barrier;
if (barrier0 != null)
barrier0.reset();
stop.set(true);
fut.get();
fail(err);
}
stop.set(true);
fut.get();
} finally {
client.events().stopLocalListen(p);
stop.set(true);
}
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class CacheMetricsForClusterGroupSelfTest method awaitMetricsUpdate.
/**
* Wait for {@link EventType#EVT_NODE_METRICS_UPDATED} event will be receieved.
*/
private void awaitMetricsUpdate() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch((GRID_CNT + 1) * 2);
IgnitePredicate<Event> lsnr = new IgnitePredicate<Event>() {
@Override
public boolean apply(Event ignore) {
latch.countDown();
return true;
}
};
for (int i = 0; i < GRID_CNT; i++) grid(i).events().localListen(lsnr, EVT_NODE_METRICS_UPDATED);
latch.await();
}
use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.
the class GridCacheDhtPreloadMultiThreadedSelfTest method testNodeLeaveBeforePreloadingComplete.
/**
* @throws Exception If failed.
*/
public void testNodeLeaveBeforePreloadingComplete() throws Exception {
try {
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch stopLatch = new CountDownLatch(1);
GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
Ignite g = startGrid("first");
g.events().localListen(new IgnitePredicate<Event>() {
@Override
public boolean apply(Event evt) {
stopLatch.countDown();
return true;
}
}, EventType.EVT_NODE_JOINED);
startLatch.countDown();
stopLatch.await();
G.stop(g.name(), false);
return null;
}
}, 1, "first");
GridTestUtils.runMultiThreaded(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
startLatch.await();
startGrid("second");
return null;
}
}, 1, "second");
} finally {
// Intentionally used this method. See startGrid(String, String).
G.stopAll(false);
}
}
Aggregations