Search in sources :

Example 46 with IgnitePredicate

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

the class GridCacheProcessor method onKernalStart.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public void onKernalStart(boolean activeOnStart) throws IgniteCheckedException {
    ClusterNode locNode = ctx.discovery().localNode();
    try {
        boolean checkConsistency = !ctx.config().isDaemon() && !getBoolean(IGNITE_SKIP_CONFIGURATION_CONSISTENCY_CHECK);
        if (checkConsistency)
            checkConsistency();
        cachesInfo.onKernalStart(checkConsistency);
        boolean currStatus = ctx.state().active();
        // which were received on join.
        if (!ctx.isDaemon() && currStatus && !activeOnStart) {
            List<CacheConfiguration> tmpCacheCfg = new ArrayList<>();
            for (CacheConfiguration conf : ctx.config().getCacheConfiguration()) {
                assert conf.getName() != null;
                for (DynamicCacheDescriptor desc : cacheDescriptors()) {
                    CacheConfiguration c = desc.cacheConfiguration();
                    IgnitePredicate filter = c.getNodeFilter();
                    if (c.getName().equals(conf.getName()) && ((desc.receivedOnDiscovery() && CU.affinityNode(locNode, filter)) || CU.isSystemCache(c.getName()))) {
                        tmpCacheCfg.add(c);
                        break;
                    }
                }
            }
            if (!tmpCacheCfg.isEmpty()) {
                CacheConfiguration[] newCacheCfg = new CacheConfiguration[tmpCacheCfg.size()];
                tmpCacheCfg.toArray(newCacheCfg);
                ctx.config().setCacheConfiguration(newCacheCfg);
            }
            activeOnStart = currStatus;
        }
        if (activeOnStart && !ctx.clientNode() && !ctx.isDaemon())
            sharedCtx.database().lock();
        // Must start database before start first cache.
        sharedCtx.database().onKernalStart(false);
        ctx.query().onCacheKernalStart();
        for (GridCacheSharedManager<?, ?> mgr : sharedCtx.managers()) {
            if (sharedCtx.database() != mgr)
                mgr.onKernalStart(false);
        }
    } finally {
        cacheStartedLatch.countDown();
    }
    // Escape if start active on start false
    if (!activeOnStart)
        return;
    if (!ctx.config().isDaemon())
        ctx.cacheObjects().onUtilityCacheStarted();
    ctx.service().onUtilityCacheStarted();
    final AffinityTopologyVersion startTopVer = new AffinityTopologyVersion(ctx.discovery().localJoinEvent().topologyVersion(), 0);
    final List<IgniteInternalFuture> syncFuts = new ArrayList<>(caches.size());
    sharedCtx.forAllCaches(new CIX1<GridCacheContext>() {

        @Override
        public void applyx(GridCacheContext cctx) throws IgniteCheckedException {
            CacheConfiguration cfg = cctx.config();
            if (cctx.affinityNode() && cfg.getRebalanceMode() == SYNC && startTopVer.equals(cctx.startTopologyVersion())) {
                CacheMode cacheMode = cfg.getCacheMode();
                if (cacheMode == REPLICATED || (cacheMode == PARTITIONED && cfg.getRebalanceDelay() >= 0))
                    // Need to wait outside to avoid a deadlock
                    syncFuts.add(cctx.preloader().syncFuture());
            }
        }
    });
    for (int i = 0, size = syncFuts.size(); i < size; i++) syncFuts.get(i).get();
    assert ctx.config().isDaemon() || caches.containsKey(CU.UTILITY_CACHE_NAME) : "Utility cache should be started";
    if (!ctx.clientNode() && !ctx.isDaemon())
        addRemovedItemsCleanupTask(Long.getLong(IGNITE_CACHE_REMOVED_ENTRIES_TTL, 10_000));
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) CacheMode(org.apache.ignite.cache.CacheMode) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration)

Example 47 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 = new IgnitePredicate<TaskEvent>() {

        @Override
        public boolean apply(TaskEvent evt) {
            System.out.println("Received task event [evt=" + evt.name() + ", taskName=" + evt.taskName() + ']');
            // Return true to continue listening.
            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(new IgniteRunnable() {

        @Override
        public void run() {
            System.out.println("Executing sample job.");
        }
    });
    // Unsubscribe local task event listener.
    ignite.events().stopLocalListen(lsnr);
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) TaskEvent(org.apache.ignite.events.TaskEvent) Ignite(org.apache.ignite.Ignite) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Example 48 with IgnitePredicate

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

the class GridCacheAdapter method sizeLongAsync.

/** {@inheritDoc} */
@Override
public IgniteInternalFuture<Long> sizeLongAsync(final int part, final CachePeekMode[] peekModes) {
    assert peekModes != null;
    final PeekModes modes = parsePeekModes(peekModes, true);
    IgniteClusterEx cluster = ctx.grid().cluster();
    final GridCacheAffinityManager aff = ctx.affinity();
    final AffinityTopologyVersion topVer = aff.affinityTopologyVersion();
    ClusterGroup grp = cluster.forDataNodes(name());
    Collection<ClusterNode> nodes = grp.forPredicate(new IgnitePredicate<ClusterNode>() {

        /** {@inheritDoc} */
        @Override
        public boolean apply(ClusterNode clusterNode) {
            return ((modes.primary && aff.primaryByPartition(clusterNode, part, topVer)) || (modes.backup && aff.backupByPartition(clusterNode, part, topVer)));
        }
    }).nodes();
    if (nodes.isEmpty())
        return new GridFinishedFuture<>(0L);
    ctx.kernalContext().task().setThreadContext(TC_SUBGRID, nodes);
    return ctx.kernalContext().task().execute(new PartitionSizeLongTask(ctx.name(), ctx.affinity().affinityTopologyVersion(), peekModes, part), null);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteClusterEx(org.apache.ignite.internal.cluster.IgniteClusterEx) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ClusterGroup(org.apache.ignite.cluster.ClusterGroup)

Example 49 with IgnitePredicate

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

the class IgniteUtilsSelfTest method testResolveLocalAddresses.

/**
     * @throws Exception If failed.
     */
public void testResolveLocalAddresses() throws Exception {
    InetAddress inetAddress = InetAddress.getByName("0.0.0.0");
    IgniteBiTuple<Collection<String>, Collection<String>> addrs = U.resolveLocalAddresses(inetAddress);
    Collection<String> hostNames = addrs.get2();
    assertFalse(hostNames.contains(null));
    assertFalse(hostNames.contains(""));
    assertFalse(hostNames.contains("127.0.0.1"));
    assertFalse(F.exist(hostNames, new IgnitePredicate<String>() {

        @Override
        public boolean apply(String hostName) {
            return hostName.contains("localhost") || hostName.contains("0:0:0:0:0:0:0:1");
        }
    }));
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) Collection(java.util.Collection) InetAddress(java.net.InetAddress)

Example 50 with IgnitePredicate

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

the class IgniteSqlRoutingTest method runQueryEnsureBroadcast.

private List<List<?>> runQueryEnsureBroadcast(IgniteCache<?, ?> cache, SqlFieldsQuery qry) throws Exception {
    final CountDownLatch execLatch = new CountDownLatch(NODE_COUNT);
    final IgnitePredicate<Event> pred = new IgnitePredicate<Event>() {

        @Override
        public boolean apply(Event evt) {
            assert evt instanceof CacheQueryExecutedEvent;
            CacheQueryExecutedEvent qe = (CacheQueryExecutedEvent) evt;
            assertNotNull(qe.clause());
            execLatch.countDown();
            return true;
        }
    };
    for (int i = 0; i < NODE_COUNT; i++) grid(i).events().localListen(pred, EVT_CACHE_QUERY_EXECUTED);
    List<List<?>> result = cache.query(qry).getAll();
    assertTrue(execLatch.await(5000, MILLISECONDS));
    for (int i = 0; i < NODE_COUNT; i++) grid(i).events().stopLocalListen(pred);
    return result;
}
Also used : IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent) Event(org.apache.ignite.events.Event) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) CacheQueryExecutedEvent(org.apache.ignite.events.CacheQueryExecutedEvent)

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