Search in sources :

Example 1 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class ComputeFibonacciContinuationExample method main.

/**
     * Executes example.
     *
     * @param args Command line arguments, none required.
     * @throws IgniteException If example execution failed.
     */
public static void main(String[] args) throws IgniteException {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println("Compute Fibonacci continuation example started.");
        long N = 100;
        final UUID exampleNodeId = ignite.cluster().localNode().id();
        // Filter to exclude this node from execution.
        final IgnitePredicate<ClusterNode> nodeFilter = new IgnitePredicate<ClusterNode>() {

            @Override
            public boolean apply(ClusterNode n) {
                // Give preference to remote nodes.
                return ignite.cluster().forRemotes().nodes().isEmpty() || !n.id().equals(exampleNodeId);
            }
        };
        long start = System.currentTimeMillis();
        BigInteger fib = ignite.compute(ignite.cluster().forPredicate(nodeFilter)).apply(new ContinuationFibonacciClosure(nodeFilter), N);
        long duration = System.currentTimeMillis() - start;
        System.out.println();
        System.out.println(">>> Finished executing Fibonacci for '" + N + "' in " + duration + " ms.");
        System.out.println(">>> Fibonacci sequence for input number '" + N + "' is '" + fib + "'.");
        System.out.println(">>> If you re-run this example w/o stopping remote nodes - the performance will");
        System.out.println(">>> increase since intermediate results are pre-cache on remote nodes.");
        System.out.println(">>> You should see prints out every recursive Fibonacci execution on cluster nodes.");
        System.out.println(">>> Check remote nodes for output.");
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) BigInteger(java.math.BigInteger) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID)

Example 2 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class EventsExample method localListen.

/**
     * Listen to events that happen only on local node.
     *
     * @throws IgniteException If failed.
     */
private static void localListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Local event listener example.");
    Ignite ignite = Ignition.ignite();
    IgnitePredicate<TaskEvent> lsnr = evt -> {
        System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');
        return true;
    };
    // Register event listener for all local task execution events.
    ignite.events().localListen(lsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    ignite.compute().withName("example-event-task").run(() -> System.out.println("Executing sample job."));
    // Unsubscribe local task event listener.
    ignite.events().stopLocalListen(lsnr);
}
Also used : EVTS_TASK_EXECUTION(org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION) Ignition(org.apache.ignite.Ignition) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) IgniteException(org.apache.ignite.IgniteException) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) TaskEvent(org.apache.ignite.events.TaskEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite)

Example 3 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class EventsExample method remoteListen.

/**
     * Listen to events coming from all cluster nodes.
     *
     * @throws IgniteException If failed.
     */
private static void remoteListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Remote event listener example.");
    // This optional local callback is called for each event notification
    // that passed remote predicate listener.
    IgniteBiPredicate<UUID, TaskEvent> locLsnr = (nodeId, evt) -> {
        // Remote filter only accepts tasks whose name being with "good-task" prefix.
        assert evt.taskName().startsWith("good-task");
        System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
        // Return true to continue listening.
        return true;
    };
    // Remote filter which only accepts tasks whose name begins with "good-task" prefix.
    IgnitePredicate<TaskEvent> rmtLsnr = evt -> evt.taskName().startsWith("good-task");
    Ignite ignite = Ignition.ignite();
    // Register event listeners on all nodes to listen for task events.
    ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    for (int i = 0; i < 10; i++) {
        ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {

            // Auto-inject task session.
            @TaskSessionResource
            private ComputeTaskSession ses;

            @Override
            public void run() {
                System.out.println("Executing sample job for task: " + ses.getTaskName());
            }
        });
    }
}
Also used : EVTS_TASK_EXECUTION(org.apache.ignite.events.EventType.EVTS_TASK_EXECUTION) Ignition(org.apache.ignite.Ignition) IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) IgniteException(org.apache.ignite.IgniteException) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) UUID(java.util.UUID) Ignite(org.apache.ignite.Ignite) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession) TaskEvent(org.apache.ignite.events.TaskEvent) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) UUID(java.util.UUID) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 4 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class EventsExample method remoteListen.

/**
     * Listen to events coming from all cluster nodes.
     *
     * @throws IgniteException If failed.
     */
private static void remoteListen() throws IgniteException {
    System.out.println();
    System.out.println(">>> Remote event listener example.");
    // This optional local callback is called for each event notification
    // that passed remote predicate listener.
    IgniteBiPredicate<UUID, TaskEvent> locLsnr = new IgniteBiPredicate<UUID, TaskEvent>() {

        @Override
        public boolean apply(UUID nodeId, TaskEvent evt) {
            // Remote filter only accepts tasks whose name being with "good-task" prefix.
            assert evt.taskName().startsWith("good-task");
            System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName());
            // Return true to continue listening.
            return true;
        }
    };
    // Remote filter which only accepts tasks whose name begins with "good-task" prefix.
    IgnitePredicate<TaskEvent> rmtLsnr = new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent evt) {
            return evt.taskName().startsWith("good-task");
        }
    };
    Ignite ignite = Ignition.ignite();
    // Register event listeners on all nodes to listen for task events.
    ignite.events().remoteListen(locLsnr, rmtLsnr, EVTS_TASK_EXECUTION);
    // Generate task events.
    for (int i = 0; i < 10; i++) {
        ignite.compute().withName(i < 5 ? "good-task-" + i : "bad-task-" + i).run(new IgniteRunnable() {

            // Auto-inject task session.
            @TaskSessionResource
            private ComputeTaskSession ses;

            @Override
            public void run() {
                System.out.println("Executing sample job for task: " + ses.getTaskName());
            }
        });
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) TaskSessionResource(org.apache.ignite.resources.TaskSessionResource) UUID(java.util.UUID) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ComputeTaskSession(org.apache.ignite.compute.ComputeTaskSession)

Example 5 with IgnitePredicate

use of org.apache.ignite.lang.IgnitePredicate in project ignite by apache.

the class CacheAbstractJdbcStore method loadCache.

/** {@inheritDoc} */
@Override
public void loadCache(final IgniteBiInClosure<K, V> clo, @Nullable Object... args) throws CacheLoaderException {
    ExecutorService pool = null;
    String cacheName = session().cacheName();
    try {
        pool = Executors.newFixedThreadPool(maxPoolSize, new IgniteThreadFactory(ignite.name(), CACHE_LOADER_THREAD_NAME));
        Collection<Future<?>> futs = new ArrayList<>();
        Map<Object, EntryMapping> mappings = getOrCreateCacheMappings(cacheName);
        if (args != null && args.length > 0) {
            if (args.length % 2 != 0)
                throw new CacheLoaderException("Expected even number of arguments, but found: " + args.length);
            if (log.isDebugEnabled())
                log.debug("Start loading entries from db using user queries from arguments...");
            for (int i = 0; i < args.length; i += 2) {
                final String keyType = args[i].toString();
                if (!F.exist(mappings.values(), new IgnitePredicate<EntryMapping>() {

                    @Override
                    public boolean apply(EntryMapping em) {
                        return em.keyType().equals(keyType);
                    }
                }))
                    throw new CacheLoaderException("Provided key type is not found in store or cache configuration " + "[cache=" + U.maskName(cacheName) + ", key=" + keyType + ']');
                EntryMapping em = entryMapping(cacheName, typeIdForTypeName(kindForName(keyType), keyType));
                Object arg = args[i + 1];
                LoadCacheCustomQueryWorker<K, V> task;
                if (arg instanceof PreparedStatement) {
                    PreparedStatement stmt = (PreparedStatement) arg;
                    if (log.isInfoEnabled())
                        log.info("Started load cache using custom statement [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", stmt=" + stmt + ']');
                    task = new LoadCacheCustomQueryWorker<>(em, stmt, clo);
                } else {
                    String qry = arg.toString();
                    if (log.isInfoEnabled())
                        log.info("Started load cache using custom query [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", query=" + qry + ']');
                    task = new LoadCacheCustomQueryWorker<>(em, qry, clo);
                }
                futs.add(pool.submit(task));
            }
        } else {
            Collection<String> processedKeyTypes = new HashSet<>();
            for (EntryMapping em : mappings.values()) {
                String keyType = em.keyType();
                if (processedKeyTypes.contains(keyType))
                    continue;
                processedKeyTypes.add(keyType);
                if (log.isInfoEnabled())
                    log.info("Started load cache [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
                if (parallelLoadCacheMinThreshold > 0) {
                    Connection conn = null;
                    try {
                        conn = connection();
                        PreparedStatement stmt = conn.prepareStatement(em.loadCacheSelRangeQry);
                        stmt.setInt(1, parallelLoadCacheMinThreshold);
                        ResultSet rs = stmt.executeQuery();
                        if (rs.next()) {
                            if (log.isDebugEnabled())
                                log.debug("Multithread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
                            int keyCnt = em.keyCols.size();
                            Object[] upperBound = new Object[keyCnt];
                            for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
                            futs.add(pool.submit(loadCacheRange(em, clo, null, upperBound, 0)));
                            while (rs.next()) {
                                Object[] lowerBound = upperBound;
                                upperBound = new Object[keyCnt];
                                for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
                                futs.add(pool.submit(loadCacheRange(em, clo, lowerBound, upperBound, 0)));
                            }
                            futs.add(pool.submit(loadCacheRange(em, clo, upperBound, null, 0)));
                            continue;
                        }
                    } catch (SQLException e) {
                        log.warning("Failed to load entries from db in multithreaded mode, will try in single thread " + "[cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']', e);
                    } finally {
                        U.closeQuiet(conn);
                    }
                }
                if (log.isDebugEnabled())
                    log.debug("Single thread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
                futs.add(pool.submit(loadCacheFull(em, clo)));
            }
        }
        for (Future<?> fut : futs) U.get(fut);
        if (log.isInfoEnabled())
            log.info("Finished load cache: " + U.maskName(cacheName));
    } catch (IgniteCheckedException e) {
        throw new CacheLoaderException("Failed to load cache: " + U.maskName(cacheName), e.getCause());
    } finally {
        U.shutdownNow(getClass(), pool, log);
    }
}
Also used : SQLException(java.sql.SQLException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) IgniteThreadFactory(org.apache.ignite.thread.IgniteThreadFactory) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

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