Search in sources :

Example 76 with IgniteException

use of org.apache.ignite.IgniteException in project ignite by apache.

the class GridCacheAbstractRemoveFailureTest method putAndRemove.

/**
 * @param duration Test duration.
 * @param txConcurrency Transaction concurrency if test explicit transaction.
 * @param txIsolation Transaction isolation if test explicit transaction.
 * @throws Exception If failed.
 */
private void putAndRemove(long duration, final TransactionConcurrency txConcurrency, final TransactionIsolation txIsolation) throws Exception {
    assertEquals(testClientNode(), (boolean) grid(0).configuration().isClientMode());
    grid(0).destroyCache(DEFAULT_CACHE_NAME);
    CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setCacheMode(cacheMode());
    if (cacheMode() == PARTITIONED)
        ccfg.setBackups(1);
    ccfg.setAtomicityMode(atomicityMode());
    ccfg.setNearConfiguration(nearCache());
    final IgniteCache<Integer, Integer> sndCache0 = grid(0).createCache(ccfg);
    final AtomicBoolean stop = new AtomicBoolean();
    final AtomicLong cntr = new AtomicLong();
    final AtomicLong errCntr = new AtomicLong();
    // Expected values in cache.
    final Map<Integer, GridTuple<Integer>> expVals = new ConcurrentHashMap<>();
    final AtomicReference<CyclicBarrier> cmp = new AtomicReference<>();
    IgniteInternalFuture<?> updateFut = GridTestUtils.runAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Thread.currentThread().setName("update-thread");
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            IgniteTransactions txs = sndCache0.unwrap(Ignite.class).transactions();
            while (!stop.get()) {
                for (int i = 0; i < 100; i++) {
                    int key = rnd.nextInt(KEYS_CNT);
                    boolean put = rnd.nextInt(0, 100) > 10;
                    while (true) {
                        try {
                            if (put) {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.put(key, i);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.put(key, i);
                                if (!failed)
                                    expVals.put(key, F.t(i));
                            } else {
                                boolean failed = false;
                                if (txConcurrency != null) {
                                    try (Transaction tx = txs.txStart(txConcurrency, txIsolation)) {
                                        sndCache0.remove(key);
                                        tx.commit();
                                    } catch (CacheException | IgniteException e) {
                                        if (!X.hasCause(e, ClusterTopologyCheckedException.class)) {
                                            log.error("Unexpected error: " + e);
                                            throw e;
                                        }
                                        failed = true;
                                    }
                                } else
                                    sndCache0.remove(key);
                                if (!failed)
                                    expVals.put(key, F.<Integer>t(null));
                            }
                            break;
                        } catch (CacheException e) {
                            if (put)
                                log.error("Put failed [key=" + key + ", val=" + i + ']', e);
                            else
                                log.error("Remove failed [key=" + key + ']', e);
                            errCntr.incrementAndGet();
                        }
                    }
                }
                cntr.addAndGet(100);
                CyclicBarrier barrier = cmp.get();
                if (barrier != null) {
                    log.info("Wait data check.");
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                    log.info("Finished wait data check.");
                }
            }
            return null;
        }
    });
    IgniteInternalFuture killFut = createAndRunConcurrentAction(stop, cmp);
    try {
        long stopTime = duration + U.currentTimeMillis();
        long nextAssert = U.currentTimeMillis() + ASSERT_FREQ;
        while (U.currentTimeMillis() < stopTime) {
            long start = System.nanoTime();
            long ops = cntr.longValue();
            U.sleep(1000);
            long diff = cntr.longValue() - ops;
            double time = (System.nanoTime() - start) / 1_000_000_000d;
            long opsPerSecond = (long) (diff / time);
            log.info("Operations/second: " + opsPerSecond);
            if (U.currentTimeMillis() >= nextAssert) {
                CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {

                    @Override
                    public void run() {
                        try {
                            cmp.set(null);
                            log.info("Checking cache content.");
                            assertCacheContent(expVals);
                            log.info("Finished check cache content.");
                        } catch (Throwable e) {
                            log.error("Unexpected error: " + e, e);
                            throw e;
                        }
                    }
                });
                log.info("Start cache content check.");
                cmp.set(barrier);
                try {
                    barrier.await(60_000, TimeUnit.MILLISECONDS);
                } catch (TimeoutException e) {
                    U.dumpThreads(log);
                    fail("Failed to check cache content: " + e);
                }
                log.info("Cache content check done.");
                nextAssert = System.currentTimeMillis() + ASSERT_FREQ;
            }
        }
    } finally {
        stop.set(true);
    }
    killFut.get();
    updateFut.get();
    log.info("Test finished. Update errors: " + errCntr.get());
}
Also used : CacheException(javax.cache.CacheException) IgniteTransactions(org.apache.ignite.IgniteTransactions) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) TimeoutException(java.util.concurrent.TimeoutException) GridTuple(org.apache.ignite.internal.util.lang.GridTuple) AtomicReference(java.util.concurrent.atomic.AtomicReference) TimeoutException(java.util.concurrent.TimeoutException) CacheException(javax.cache.CacheException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)

Example 77 with IgniteException

use of org.apache.ignite.IgniteException in project ignite by apache.

the class GridListenActorSelfTest method testRespondToRemote.

/**
 * Testing {@link org.apache.ignite.messaging.MessagingListenActor#respond(UUID, Object)} method.
 *
 * @throws Exception If failed.
 */
public void testRespondToRemote() throws Exception {
    startGrid(1);
    try {
        final ClusterNode rmt = grid(1).localNode();
        grid().message().localListen(null, new MessagingListenActor<String>() {

            @Override
            protected void receive(UUID nodeId, String rcvMsg) throws IgniteException {
                System.out.println("Local node received message: '" + rcvMsg + "'");
                respond(rmt.id(), "RESPONSE");
            }
        });
        final AtomicInteger cnt = new AtomicInteger();
        // Response listener
        grid(1).message().localListen(null, new MessagingListenActor<String>() {

            @Override
            public void receive(UUID nodeId, String rcvMsg) {
                if ("RESPONSE".equals(rcvMsg)) {
                    System.out.println("Remote node received message: '" + rcvMsg + "'");
                    cnt.incrementAndGet();
                }
            }
        });
        grid().message().send(null, "REQUEST");
        assert GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return cnt.intValue() == 1;
            }
        }, getTestTimeout());
    } finally {
        stopGrid(1);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) PA(org.apache.ignite.internal.util.typedef.PA) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) UUID(java.util.UUID)

Example 78 with IgniteException

use of org.apache.ignite.IgniteException in project ignite by apache.

the class GridCacheQueueRotativeMultiNodeAbstractTest method testTakeRemoveRotativeNodes.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testTakeRemoveRotativeNodes() throws Exception {
    lthTake = new CountDownLatch(1);
    final String queueName = UUID.randomUUID().toString();
    final IgniteQueue<Integer> queue = grid(0).queue(queueName, QUEUE_CAPACITY, config(true));
    assertTrue(queue.isEmpty());
    Thread th = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                assert grid(1).compute().call(new TakeJob(queueName, config(true)));
            } catch (IgniteException e) {
                error(e.getMessage(), e);
            }
        }
    });
    th.start();
    assert lthTake.await(1, TimeUnit.MINUTES) : "Timeout happened.";
    assertTrue(grid(2).compute().call(new RemoveQueueJob(queueName)));
    GridTestUtils.assertThrows(log, new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            queue.poll();
            return null;
        }
    }, IllegalStateException.class, null);
    info("Queue was removed: " + queue);
    th.join();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteException(org.apache.ignite.IgniteException)

Example 79 with IgniteException

use of org.apache.ignite.IgniteException 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 80 with IgniteException

use of org.apache.ignite.IgniteException 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

IgniteException (org.apache.ignite.IgniteException)498 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)160 Ignite (org.apache.ignite.Ignite)97 ClusterNode (org.apache.ignite.cluster.ClusterNode)54 ArrayList (java.util.ArrayList)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)45 CountDownLatch (java.util.concurrent.CountDownLatch)44 UUID (java.util.UUID)43 IOException (java.io.IOException)39 CacheException (javax.cache.CacheException)35 HashMap (java.util.HashMap)34 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)34 Transaction (org.apache.ignite.transactions.Transaction)34 List (java.util.List)24 CyclicBarrier (java.util.concurrent.CyclicBarrier)21 Map (java.util.Map)20 Collection (java.util.Collection)18 ClusterStartNodeResult (org.apache.ignite.cluster.ClusterStartNodeResult)18 ClusterTopologyCheckedException (org.apache.ignite.internal.cluster.ClusterTopologyCheckedException)18 IgniteClientDisconnectedException (org.apache.ignite.IgniteClientDisconnectedException)17