Search in sources :

Example 1 with EntryProcessorException

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

the class ClientCacheProxy method invoke.

@Override
public <T> T invoke(K key, EntryProcessor<K, V, T> entryProcessor, Object... arguments) throws EntryProcessorException {
    ensureOpen();
    validateNotNull(key);
    if (entryProcessor == null) {
        throw new NullPointerException("Entry Processor is null");
    }
    final Data keyData = toData(key);
    Data epData = toData(entryProcessor);
    List<Data> argumentsData = null;
    if (arguments != null) {
        argumentsData = new ArrayList<Data>(arguments.length);
        for (int i = 0; i < arguments.length; i++) {
            argumentsData.add(toData(arguments[i]));
        }
    }
    final int completionId = nextCompletionId();
    ClientMessage request = CacheEntryProcessorCodec.encodeRequest(nameWithPrefix, keyData, epData, argumentsData, completionId);
    try {
        final ICompletableFuture<ClientMessage> f = invoke(request, keyData, completionId);
        final ClientMessage response = getSafely(f);
        final Data data = CacheEntryProcessorCodec.decodeResponse(response).response;
        // At client side, we don't know what entry processor does so we ignore it from statistics perspective
        return toObject(data);
    } catch (CacheException ce) {
        throw ce;
    } catch (Exception e) {
        throw new EntryProcessorException(e);
    }
}
Also used : EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) Data(com.hazelcast.nio.serialization.Data) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException)

Example 2 with EntryProcessorException

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

the class CacheProxy method invoke.

@Override
public <T> T invoke(K key, EntryProcessor<K, V, T> entryProcessor, Object... arguments) throws EntryProcessorException {
    ensureOpen();
    validateNotNull(key);
    checkNotNull(entryProcessor, "Entry Processor is null");
    Data keyData = serializationService.toData(key);
    Integer completionId = registerCompletionLatch(1);
    Operation op = operationProvider.createEntryProcessorOperation(keyData, completionId, entryProcessor, arguments);
    try {
        OperationService operationService = getNodeEngine().getOperationService();
        int partitionId = getPartitionId(keyData);
        InternalCompletableFuture<T> future = operationService.invokeOnPartition(getServiceName(), op, partitionId);
        T safely = future.join();
        waitCompletionLatch(completionId);
        return safely;
    } catch (CacheException ce) {
        deregisterCompletionLatch(completionId);
        throw ce;
    } catch (Exception e) {
        deregisterCompletionLatch(completionId);
        throw new EntryProcessorException(e);
    }
}
Also used : EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) Data(com.hazelcast.nio.serialization.Data) CacheListenerRegistrationOperation(com.hazelcast.cache.impl.operation.CacheListenerRegistrationOperation) Operation(com.hazelcast.spi.Operation) OperationService(com.hazelcast.spi.OperationService) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException)

Example 3 with EntryProcessorException

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

the class PlatformCache method convertException.

/** {@inheritDoc} */
@Override
public Exception convertException(Exception e) {
    if (e instanceof CachePartialUpdateException)
        return new PlatformCachePartialUpdateException((CachePartialUpdateCheckedException) e.getCause(), platformCtx, keepBinary);
    if (e instanceof CachePartialUpdateCheckedException)
        return new PlatformCachePartialUpdateException((CachePartialUpdateCheckedException) e, platformCtx, keepBinary);
    if (e.getCause() instanceof EntryProcessorException)
        return (Exception) e.getCause();
    TransactionDeadlockException deadlockException = X.cause(e, TransactionDeadlockException.class);
    if (deadlockException != null)
        return deadlockException;
    TransactionTimeoutException timeoutException = X.cause(e, TransactionTimeoutException.class);
    if (timeoutException != null)
        return timeoutException;
    return super.convertException(e);
}
Also used : TransactionDeadlockException(org.apache.ignite.transactions.TransactionDeadlockException) EntryProcessorException(javax.cache.processor.EntryProcessorException) TransactionTimeoutException(org.apache.ignite.transactions.TransactionTimeoutException) CachePartialUpdateException(org.apache.ignite.cache.CachePartialUpdateException) CachePartialUpdateCheckedException(org.apache.ignite.internal.processors.cache.CachePartialUpdateCheckedException)

Example 4 with EntryProcessorException

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

the class CacheContinuousQueryFailoverAbstractSelfTest method failoverStartStopFilter.

/**
     * @param backups Number of backups.
     * @throws Exception If failed.
     */
private void failoverStartStopFilter(int backups) throws Exception {
    this.backups = backups;
    final int SRV_NODES = 4;
    startGridsMultiThreaded(SRV_NODES);
    client = true;
    Ignite qryClient = startGrid(SRV_NODES);
    client = false;
    IgniteCache<Object, Object> qryClnCache = qryClient.cache(DEFAULT_CACHE_NAME);
    final CacheEventListener2 lsnr = new CacheEventListener2();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    qry.setRemoteFilter(asyncCallback() ? new CacheEventAsyncFilter() : new CacheEventFilter());
    QueryCursor<?> cur = qryClnCache.query(qry);
    CacheEventListener2 dinLsnr = null;
    QueryCursor<?> dinQry = null;
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicReference<CountDownLatch> checkLatch = new AtomicReference<>();
    IgniteInternalFuture<?> restartFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            while (!stop.get() && !err) {
                final int idx = ThreadLocalRandom.current().nextInt(SRV_NODES - 1);
                log.info("Stop node: " + idx);
                awaitPartitionMapExchange();
                Thread.sleep(400);
                stopGrid(idx);
                awaitPartitionMapExchange();
                Thread.sleep(400);
                log.info("Start node: " + idx);
                startGrid(idx);
                Thread.sleep(200);
                CountDownLatch latch = new CountDownLatch(1);
                assertTrue(checkLatch.compareAndSet(null, latch));
                if (!stop.get()) {
                    log.info("Wait for event check.");
                    assertTrue(latch.await(1, MINUTES));
                }
            }
            return null;
        }
    });
    final Map<Integer, Integer> vals = new HashMap<>();
    final Map<Integer, List<T2<Integer, Integer>>> expEvts = new HashMap<>();
    final List<T3<Object, Object, Object>> expEvtsNewLsnr = new ArrayList<>();
    final List<T3<Object, Object, Object>> expEvtsLsnr = new ArrayList<>();
    try {
        long stopTime = System.currentTimeMillis() + 60_000;
        // Start new filter each 5 sec.
        long startFilterTime = System.currentTimeMillis() + 5_000;
        final int PARTS = qryClient.affinity(DEFAULT_CACHE_NAME).partitions();
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
        boolean filtered = false;
        boolean processorPut = false;
        while (System.currentTimeMillis() < stopTime) {
            Integer key = rnd.nextInt(PARTS);
            Integer prevVal = vals.get(key);
            Integer val = vals.get(key);
            if (System.currentTimeMillis() > startFilterTime) {
                // Stop filter and check events.
                if (dinQry != null) {
                    dinQry.close();
                    log.info("Continuous query listener closed. Await events: " + expEvtsNewLsnr.size());
                    checkEvents(expEvtsNewLsnr, dinLsnr, backups == 0);
                }
                dinLsnr = new CacheEventListener2();
                ContinuousQuery<Object, Object> newQry = new ContinuousQuery<>();
                newQry.setLocalListener(dinLsnr);
                newQry.setRemoteFilter(asyncCallback() ? new CacheEventAsyncFilter() : new CacheEventFilter());
                dinQry = qryClnCache.query(newQry);
                log.info("Continuous query listener started.");
                startFilterTime = System.currentTimeMillis() + 5_000;
            }
            if (val == null)
                val = 0;
            else
                val = Math.abs(val) + 1;
            if (filtered)
                val = -val;
            if (processorPut && prevVal != null) {
                qryClnCache.invoke(key, new CacheEntryProcessor<Object, Object, Void>() {

                    @Override
                    public Void process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
                        entry.setValue(arguments[0]);
                        return null;
                    }
                }, val);
            } else
                qryClnCache.put(key, val);
            processorPut = !processorPut;
            vals.put(key, val);
            if (val >= 0) {
                List<T2<Integer, Integer>> keyEvts = expEvts.get(key);
                if (keyEvts == null) {
                    keyEvts = new ArrayList<>();
                    expEvts.put(key, keyEvts);
                }
                keyEvts.add(new T2<>(val, prevVal));
                T3<Object, Object, Object> tupVal = new T3<>((Object) key, (Object) val, (Object) prevVal);
                expEvtsLsnr.add(tupVal);
                if (dinQry != null)
                    expEvtsNewLsnr.add(tupVal);
            }
            filtered = !filtered;
            CountDownLatch latch = checkLatch.get();
            if (latch != null) {
                log.info("Check events.");
                checkLatch.set(null);
                boolean success = false;
                try {
                    if (err)
                        break;
                    checkEvents(expEvtsLsnr, lsnr, backups == 0);
                    success = true;
                    log.info("Events checked.");
                } finally {
                    if (!success)
                        err = true;
                    latch.countDown();
                }
            }
        }
    } finally {
        stop.set(true);
    }
    CountDownLatch latch = checkLatch.get();
    if (latch != null)
        latch.countDown();
    restartFut.get();
    checkEvents(expEvtsLsnr, lsnr, backups == 0);
    lsnr.evts.clear();
    lsnr.vals.clear();
    if (dinQry != null) {
        checkEvents(expEvtsNewLsnr, dinLsnr, backups == 0);
        dinLsnr.evts.clear();
        dinLsnr.vals.clear();
    }
    List<T3<Object, Object, Object>> afterRestEvts = new ArrayList<>();
    for (int i = 0; i < qryClient.affinity(DEFAULT_CACHE_NAME).partitions(); i++) {
        Integer oldVal = (Integer) qryClnCache.get(i);
        qryClnCache.put(i, i);
        afterRestEvts.add(new T3<>((Object) i, (Object) i, (Object) oldVal));
    }
    checkEvents(new ArrayList<>(afterRestEvts), lsnr, false);
    cur.close();
    if (dinQry != null) {
        checkEvents(new ArrayList<>(afterRestEvts), dinLsnr, false);
        dinQry.close();
    }
    assertFalse("Unexpected error during test, see log for details.", err);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) EntryProcessorException(javax.cache.processor.EntryProcessorException) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) T2(org.apache.ignite.internal.util.typedef.T2) T3(org.apache.ignite.internal.util.typedef.T3) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CacheEntryListenerException(javax.cache.event.CacheEntryListenerException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) EntryProcessorException(javax.cache.processor.EntryProcessorException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) CacheException(javax.cache.CacheException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 5 with EntryProcessorException

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

the class CacheContinuousQueryAsyncFilterListenerTest method testNonDeadLockInListener.

/**
     * @param ccfg Cache configuration.
     * @param asyncFltr Async filter.
     * @param asyncLsnr Async listener.
     * @param jcacheApi Use JCache api for registration entry update listener.
     * @throws Exception If failed.
     */
private void testNonDeadLockInListener(CacheConfiguration ccfg, final boolean asyncFltr, 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 (asyncFltr) {
                        assertFalse("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("sys-"));
                        assertTrue("Failed: " + Thread.currentThread().getName(), Thread.currentThread().getName().contains("callback-"));
                    }
                }
            };
            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) {
                    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();
                    }
                }
            };
            QueryCursor qry = null;
            MutableCacheEntryListenerConfiguration<QueryTestKey, QueryTestValue> lsnrCfg = null;
            CacheInvokeListener locLsnr = asyncLsnr ? new CacheInvokeListenerAsync(lsnrClsr) : new CacheInvokeListener(lsnrClsr);
            CacheEntryEventSerializableFilter<QueryTestKey, QueryTestValue> rmtFltr = asyncFltr ? 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;
                        }
                    });
                }
                assertTrue("Failed to waiting event.", U.await(latch, 3, SECONDS));
                assertEquals(cache.get(key), new QueryTestValue(2));
                assertTrue("Failed to waiting event from listener.", 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) EntryProcessorException(javax.cache.processor.EntryProcessorException) 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)

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