Search in sources :

Example 1 with AttributeNodeFilter

use of org.apache.ignite.util.AttributeNodeFilter in project ignite by apache.

the class CacheContinuousQueryManager method executeQuery0.

/**
 * @param locLsnr Local listener.
 * @param clsr Closure to create CacheContinuousQueryHandler.
 * @param bufSize Buffer size.
 * @param timeInterval Time interval.
 * @param autoUnsubscribe Auto unsubscribe flag.
 * @param internal Internal flag.
 * @param notifyExisting Notify existing flag.
 * @param loc Local flag.
 * @param keepBinary Keep binary flag.
 * @param onStart Waiting topology exchange.
 * @return Continuous routine ID.
 * @throws IgniteCheckedException In case of error.
 */
private UUID executeQuery0(CacheEntryUpdatedListener locLsnr, IgniteOutClosure<CacheContinuousQueryHandler> clsr, int bufSize, long timeInterval, boolean autoUnsubscribe, boolean internal, boolean notifyExisting, boolean loc, final boolean keepBinary, boolean onStart) throws IgniteCheckedException {
    cctx.checkSecurity(SecurityPermission.CACHE_READ);
    int taskNameHash = !internal && cctx.kernalContext().security().enabled() ? cctx.kernalContext().job().currentTaskNameHash() : 0;
    boolean skipPrimaryCheck = loc && cctx.config().getCacheMode() == CacheMode.REPLICATED && cctx.affinityNode();
    final CacheContinuousQueryHandler hnd = clsr.apply();
    boolean locOnly = cctx.isLocal() || loc;
    hnd.taskNameHash(taskNameHash);
    hnd.skipPrimaryCheck(skipPrimaryCheck);
    hnd.notifyExisting(notifyExisting);
    hnd.internal(internal);
    hnd.keepBinary(keepBinary);
    hnd.localOnly(locOnly);
    IgnitePredicate<ClusterNode> pred = (loc || cctx.config().getCacheMode() == CacheMode.LOCAL) ? F.nodeForNodeId(cctx.localNodeId()) : new IsAllPredicate<>(cctx.group().nodeFilter(), new AttributeNodeFilter(ATTR_CLIENT_MODE, false));
    assert pred != null : cctx.config();
    UUID id = null;
    try {
        id = cctx.kernalContext().continuous().startRoutine(hnd, locOnly, bufSize, timeInterval, autoUnsubscribe, pred).get();
        if (hnd.isQuery() && cctx.userCache() && !locOnly && !onStart)
            hnd.waitTopologyFuture(cctx.kernalContext());
    } catch (NodeStoppingException e) {
        // Wrap original exception to show the source of continuous query start stacktrace.
        throw new NodeStoppingException(e);
    } catch (IgniteCheckedException e) {
        log.warning("Failed to start continuous query.", e);
        if (id != null)
            cctx.kernalContext().continuous().stopRoutine(id);
        throw new IgniteCheckedException("Failed to start continuous query.", e);
    }
    if (notifyExisting) {
        assert locLsnr != null : "Local listener can't be null if notification for existing entries are enabled";
        final Iterator<CacheDataRow> it = cctx.offheap().cacheIterator(cctx.cacheId(), true, true, AffinityTopologyVersion.NONE, null, null);
        locLsnr.onUpdated(new Iterable<CacheEntryEvent>() {

            @Override
            public Iterator<CacheEntryEvent> iterator() {
                return new Iterator<CacheEntryEvent>() {

                    private CacheContinuousQueryEvent next;

                    {
                        advance();
                    }

                    @Override
                    public boolean hasNext() {
                        return next != null;
                    }

                    @Override
                    public CacheEntryEvent next() {
                        if (!hasNext())
                            throw new NoSuchElementException();
                        CacheEntryEvent next0 = next;
                        advance();
                        return next0;
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }

                    private void advance() {
                        next = null;
                        while (next == null) {
                            if (!it.hasNext())
                                break;
                            CacheDataRow e = it.next();
                            CacheContinuousQueryEntry entry = new CacheContinuousQueryEntry(cctx.cacheId(), CREATED, e.key(), e.value(), null, keepBinary, 0, -1, null, (byte) 0);
                            next = new CacheContinuousQueryEvent<>(cctx.kernalContext().cache().jcache(cctx.name()), cctx, entry);
                            if (!hnd.filter(next))
                                next = null;
                        }
                    }
                };
            }
        });
    }
    return id;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) NodeStoppingException(org.apache.ignite.internal.NodeStoppingException) AttributeNodeFilter(org.apache.ignite.util.AttributeNodeFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Iterator(java.util.Iterator) UUID(java.util.UUID) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with AttributeNodeFilter

use of org.apache.ignite.util.AttributeNodeFilter in project ignite by apache.

the class NodeFilter method attributeNodeFilter.

@Test
void attributeNodeFilter() {
    // tag::add-attribute[]
    IgniteConfiguration cfg = new IgniteConfiguration();
    Map<String, Object> attributes = new HashMap<String, Object>();
    attributes.put("host_myCache", true);
    cfg.setUserAttributes(attributes);
    Ignite ignite = Ignition.start(cfg);
    // end::add-attribute[]
    // tag::attribute-node-filter[]
    CacheConfiguration<Integer, String> cacheCfg = new CacheConfiguration<>("myCache");
    cacheCfg.setNodeFilter(new AttributeNodeFilter("host_myCache", "true"));
    // end::attribute-node-filter[]
    ignite.createCache(cacheCfg);
    ignite.close();
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) HashMap(java.util.HashMap) AttributeNodeFilter(org.apache.ignite.util.AttributeNodeFilter) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.jupiter.api.Test)

Example 3 with AttributeNodeFilter

use of org.apache.ignite.util.AttributeNodeFilter in project ignite by apache.

the class CacheMvccClientTopologyTest method getConfiguration.

/**
 * Configure nodes in client mode (filtered by AttributeNodeFilter, no CacheConfiguration is set)
 * or in ordinary server mode.
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    if (getTestIgniteInstanceIndex(igniteInstanceName) != clientModeIdx) {
        String attrName = "has_cache";
        Object attrVal = Boolean.TRUE;
        CacheConfiguration ccfg = defaultCacheConfiguration().setNearConfiguration(null).setNodeFilter(new AttributeNodeFilter(attrName, attrVal)).setAtomicityMode(TRANSACTIONAL_SNAPSHOT);
        return cfg.setCacheConfiguration(ccfg).setUserAttributes(F.asMap(attrName, attrVal));
    }
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AttributeNodeFilter(org.apache.ignite.util.AttributeNodeFilter) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 4 with AttributeNodeFilter

use of org.apache.ignite.util.AttributeNodeFilter in project ignite by apache.

the class GridCachePartitionedSetWithNodeFilterSelfTest method collectionConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected CollectionConfiguration collectionConfiguration() {
    CollectionConfiguration cfg = super.collectionConfiguration();
    cfg.setNodeFilter(new AttributeNodeFilter(ATTR_IGNITE_INSTANCE_NAME, getTestIgniteInstanceName(0)));
    return cfg;
}
Also used : AttributeNodeFilter(org.apache.ignite.util.AttributeNodeFilter) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration)

Example 5 with AttributeNodeFilter

use of org.apache.ignite.util.AttributeNodeFilter in project ignite by apache.

the class CachePartitionLossWithRestartsTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    cfg.setActiveOnStart(false);
    cfg.setConsistentId(igniteInstanceName);
    if (getTestIgniteInstanceIndex(igniteInstanceName) == clientIdx)
        cfg.setClientMode(true);
    cfg.setIncludeEventTypes(EventType.EVTS_ALL);
    DataStorageConfiguration dsCfg = new DataStorageConfiguration();
    dsCfg.setWalSegmentSize(4 * 1024 * 1024);
    dsCfg.setWalMode(WALMode.LOG_ONLY);
    final int size = 50 * 1024 * 1024;
    DataRegionConfiguration dfltRegCfg = new DataRegionConfiguration();
    dfltRegCfg.setName(DEFAULT_CACHE_NAME).setInitialSize(size).setMaxSize(size).setPersistenceEnabled(dfltRegionPersistence);
    dsCfg.setDefaultDataRegionConfiguration(dfltRegCfg);
    cfg.setDataStorageConfiguration(dsCfg);
    // Do not start cache on non-affinity node.
    CacheConfiguration ccfg = defaultCacheConfiguration().setNearConfiguration(null).setNodeFilter(new AttributeNodeFilter(START_CACHE_ATTR, Boolean.TRUE)).setBackups(0).setAffinity(new RendezvousAffinityFunction(false, PARTS_CNT));
    if (mvccEnabled)
        ccfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL_SNAPSHOT);
    if (startClientCache)
        cfg.setCacheConfiguration(ccfg);
    if (getTestIgniteInstanceIndex(igniteInstanceName) != nonAffIdx) {
        cfg.setUserAttributes(F.asMap(START_CACHE_ATTR, Boolean.TRUE));
        if (!startClientCache)
            cfg.setCacheConfiguration(ccfg);
    }
    return cfg;
}
Also used : DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AttributeNodeFilter(org.apache.ignite.util.AttributeNodeFilter) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Aggregations

AttributeNodeFilter (org.apache.ignite.util.AttributeNodeFilter)6 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)3 CollectionConfiguration (org.apache.ignite.configuration.CollectionConfiguration)2 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 UUID (java.util.UUID)1 CacheEntryEvent (javax.cache.event.CacheEntryEvent)1 Ignite (org.apache.ignite.Ignite)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 RendezvousAffinityFunction (org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction)1 ClusterNode (org.apache.ignite.cluster.ClusterNode)1 DataRegionConfiguration (org.apache.ignite.configuration.DataRegionConfiguration)1 DataStorageConfiguration (org.apache.ignite.configuration.DataStorageConfiguration)1 NodeStoppingException (org.apache.ignite.internal.NodeStoppingException)1 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)1 Test (org.junit.jupiter.api.Test)1