Search in sources :

Example 36 with IgnitePredicate

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;
        }
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheEvent(org.apache.ignite.events.CacheEvent) IgniteException(org.apache.ignite.IgniteException) ConnectException(org.apache.kafka.connect.errors.ConnectException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 37 with IgnitePredicate

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));
}
Also used : ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) HashMap(java.util.HashMap) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CountDownLatch(java.util.concurrent.CountDownLatch) Event(org.apache.ignite.events.Event) Herder(org.apache.kafka.connect.runtime.Herder) StandaloneHerder(org.apache.kafka.connect.runtime.standalone.StandaloneHerder) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) FutureCallback(org.apache.kafka.connect.util.FutureCallback)

Example 38 with IgnitePredicate

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);
    }
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) TimeoutException(java.util.concurrent.TimeoutException)

Example 39 with IgnitePredicate

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();
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 40 with IgnitePredicate

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);
    }
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Event(org.apache.ignite.events.Event) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)75 Event (org.apache.ignite.events.Event)43 CountDownLatch (java.util.concurrent.CountDownLatch)33 Ignite (org.apache.ignite.Ignite)27 ArrayList (java.util.ArrayList)15 UUID (java.util.UUID)15 ClusterNode (org.apache.ignite.cluster.ClusterNode)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 IgfsEvent (org.apache.ignite.events.IgfsEvent)9 HashMap (java.util.HashMap)8 CacheEvent (org.apache.ignite.events.CacheEvent)7 CacheQueryExecutedEvent (org.apache.ignite.events.CacheQueryExecutedEvent)7 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)7 Map (java.util.Map)6 IgniteException (org.apache.ignite.IgniteException)6 ClusterGroup (org.apache.ignite.cluster.ClusterGroup)6 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 CacheQueryReadEvent (org.apache.ignite.events.CacheQueryReadEvent)6 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)6 Ignition (org.apache.ignite.Ignition)5