Search in sources :

Example 6 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInFilter.

/**
     * @param ccfg Cache configuration.
     * @param asyncFilter Async filter.
     * @param asyncLsnr Async listener.
     * @param jcacheApi Use JCache api for start update listener.
     * @throws Exception If failed.
     */
private void testNonDeadLockInFilter(CacheConfiguration ccfg, final boolean asyncFilter, final boolean asyncLsnr, boolean jcacheApi) throws Exception {
    ignite(0).createCache(ccfg);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    try {
        for (int i = 0; i < ITERATION_CNT; i++) {
            log.info("Start iteration: " + i);
            int nodeIdx = i % NODES;
            final String cacheName = ccfg.getName();
            final IgniteCache cache = grid(nodeIdx).cache(cacheName);
            final QueryTestKey key = NODES - 1 != nodeIdx ? affinityKey(cache) : new QueryTestKey(1);
            final QueryTestValue val0 = new QueryTestValue(1);
            final QueryTestValue newVal = new QueryTestValue(2);
            final CountDownLatch latch = new CountDownLatch(1);
            final CountDownLatch evtFromLsnrLatch = new CountDownLatch(1);
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> fltrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    if (asyncFilter) {
                        assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
                        assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
                    }
                    IgniteCache<Object, Object> cache0 = ignite.cache(cacheName);
                    QueryTestValue val = e.getValue();
                    if (val == null)
                        return;
                    else if (val.equals(newVal)) {
                        evtFromLsnrLatch.countDown();
                        return;
                    } else if (!val.equals(val0))
                        return;
                    Transaction tx = null;
                    try {
                        if (cache0.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL)
                            tx = ignite.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
                        assertEquals(val, val0);
                        cache0.put(key, newVal);
                        if (tx != null)
                            tx.commit();
                        latch.countDown();
                    } catch (Exception exp) {
                        log.error("Failed: ", exp);
                        throw new IgniteException(exp);
                    } finally {
                        if (tx != null)
                            tx.close();
                    }
                }
            };
            IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>> lsnrClsr = new IgniteBiInClosure<Ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue>>() {

                @Override
                public void apply(Ignite ignite, CacheEntryEvent<? extends QueryTestKey, ? extends QueryTestValue> e) {
                    if (asyncLsnr) {
                        assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
                        assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
                    }
                    QueryTestValue val = e.getValue();
                    if (val == null || !val.equals(new QueryTestValue(1)))
                        return;
                    assertEquals(val, val0);
                    latch.countDown();
                }
            };
            QueryCursor qry = null;
            MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
            CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
            CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFilter ? new CacheTestRemoteFilterAsync(fltrClsr) : new CacheTestRemoteFilter(fltrClsr);
            if (jcacheApi) {
                lsnrCfg = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder.factoryOf(locLsnr), FactoryBuilder.factoryOf(rmtFltr), true, false);
                cache.registerCacheEntryListener(lsnrCfg);
            } else {
                ContinuousQuery<QueryTestKey, QueryTestValue> conQry = new ContinuousQuery<>();
                conQry.setLocalListener(locLsnr);
                conQry.setRemoteFilterFactory(FactoryBuilder.factoryOf(rmtFltr));
                qry = cache.query(conQry);
            }
            try {
                if (rnd.nextBoolean())
                    cache.put(key, val0);
                else
                    cache.invoke(key, new CacheEntryProcessor() {

                        @Override
                        public Object process(MutableEntry entry, Object... arguments) throws EntryProcessorException {
                            entry.setValue(val0);
                            return null;
                        }
                    });
                assert U.await(latch, 3, SECONDS) : "Failed to waiting event.";
                assertEquals(cache.get(key), new QueryTestValue(2));
                assertTrue("Failed to waiting event from filter.", U.await(latch, 3, SECONDS));
            } finally {
                if (qry != null)
                    qry.close();
                if (lsnrCfg != null)
                    cache.deregisterCacheEntryListener(lsnrCfg);
            }
            log.info("Iteration finished: " + i);
        }
    } finally {
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) Transaction(org.apache.ignite.transactions.Transaction) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Example 7 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class IgnteCacheClientWriteBehindStoreNonCoalescingTest method updateKeys.

/**
     * Update specified keys in async mode.
     *
     * @param cache Cache to use.
     * @param keys Keys to update.
     * @return IgniteFuture.
     */
private IgniteFuture<?> updateKeys(IgniteCache<Integer, Integer> cache, Set<Integer> keys) {
    IgniteCache asyncCache = cache.withAsync();
    // Using EntryProcessor.invokeAll to increment every value in place.
    asyncCache.invokeAll(keys, new EntryProcessor<Integer, Integer, Object>() {

        @Override
        public Object process(MutableEntry<Integer, Integer> entry, Object... arguments) throws EntryProcessorException {
            entry.setValue(entry.getValue() + 1);
            return null;
        }
    });
    return asyncCache.future();
}
Also used : EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteCache(org.apache.ignite.IgniteCache)

Example 8 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project redisson by redisson.

the class JCache method invoke.

@Override
public <T> T invoke(K key, EntryProcessor<K, V, T> entryProcessor, Object... arguments) throws EntryProcessorException {
    checkNotClosed();
    if (key == null) {
        throw new NullPointerException();
    }
    if (entryProcessor == null) {
        throw new NullPointerException();
    }
    long startTime = currentNanoTime();
    if (containsKey(key)) {
        cacheManager.getStatBean(this).addHits(1);
    } else {
        cacheManager.getStatBean(this).addMisses(1);
    }
    cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
    JMutableEntry<K, V> entry = new JMutableEntry<K, V>(this, key, null, config.isReadThrough());
    try {
        T result = entryProcessor.process(entry, arguments);
        if (entry.getAction() == Action.CREATED || entry.getAction() == Action.UPDATED) {
            put(key, entry.value());
        }
        if (entry.getAction() == Action.DELETED) {
            remove(key);
        }
        return result;
    } catch (EntryProcessorException e) {
        throw e;
    } catch (Exception e) {
        throw new EntryProcessorException(e);
    }
}
Also used : EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheWriterException(javax.cache.integration.CacheWriterException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException)

Example 9 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class DmlStatementsProcessor method splitErrors.

/**
     * Process errors of entry processor - split the keys into duplicated/concurrently modified and those whose
     * processing yielded an exception.
     *
     * @param res Result of {@link GridCacheAdapter#invokeAll)}
     * @return pair [array of duplicated/concurrently modified keys, SQL exception for erroneous keys] (exception is
     * null if all keys are duplicates/concurrently modified ones).
     */
private static PageProcessingErrorResult splitErrors(Map<Object, EntryProcessorResult<Boolean>> res) {
    Set<Object> errKeys = new LinkedHashSet<>(res.keySet());
    SQLException currSqlEx = null;
    SQLException firstSqlEx = null;
    int errors = 0;
    // Let's form a chain of SQL exceptions
    for (Map.Entry<Object, EntryProcessorResult<Boolean>> e : res.entrySet()) {
        try {
            e.getValue().get();
        } catch (EntryProcessorException ex) {
            SQLException next = createJdbcSqlException("Failed to process key '" + e.getKey() + '\'', IgniteQueryErrorCode.ENTRY_PROCESSING);
            next.initCause(ex);
            if (currSqlEx != null)
                currSqlEx.setNextException(next);
            else
                firstSqlEx = next;
            currSqlEx = next;
            errKeys.remove(e.getKey());
            errors++;
        }
    }
    return new PageProcessingErrorResult(errKeys.toArray(), firstSqlEx, errors);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) EntryProcessorException(javax.cache.processor.EntryProcessorException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BinaryObject(org.apache.ignite.binary.BinaryObject) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) GridBoundedConcurrentLinkedHashMap(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashMap)

Example 10 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class PlatformCacheEntryProcessorImpl method process.

/** {@inheritDoc} */
@Override
public Object process(MutableEntry entry, Object... args) throws EntryProcessorException {
    try {
        Ignite ignite = (Ignite) entry.unwrap(Ignite.class);
        PlatformProcessor interopProc;
        try {
            interopProc = PlatformUtils.platformProcessor(ignite);
        } catch (IllegalStateException ex) {
            throw new EntryProcessorException(ex);
        }
        interopProc.awaitStart();
        return execute0(interopProc.context(), entry);
    } catch (IgniteCheckedException e) {
        throw U.convertException(e);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) PlatformProcessor(org.apache.ignite.internal.processors.platform.PlatformProcessor) EntryProcessorException(javax.cache.processor.EntryProcessorException) Ignite(org.apache.ignite.Ignite)

Aggregations

EntryProcessorException (javax.cache.processor.EntryProcessorException)16 MutableEntry (javax.cache.processor.MutableEntry)5 Ignite (org.apache.ignite.Ignite)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CacheException (javax.cache.CacheException)4 IgniteCache (org.apache.ignite.IgniteCache)4 IgniteException (org.apache.ignite.IgniteException)4 Transaction (org.apache.ignite.transactions.Transaction)4 HashMap (java.util.HashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)3 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)3 Data (com.hazelcast.nio.serialization.Data)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 EntryProcessor (javax.cache.processor.EntryProcessor)2 EntryProcessorResult (javax.cache.processor.EntryProcessorResult)2 IgniteTransactions (org.apache.ignite.IgniteTransactions)2