Search in sources :

Example 6 with MutableEntry

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

the class GridCacheInterceptorAbstractSelfTest method cacheUpdate.

/**
     * @param grid Grid index.
     * @param rmv If {@code true} then executes remove.
     * @param op Operation type.
     * @param key Key.
     * @param val Value.
     * @param expOld Expected expOld value.
     * @param expRmvRet Expected remove result.
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
private void cacheUpdate(int grid, boolean rmv, Operation op, String key, final Integer val, @Nullable final Integer expOld, @Nullable final Integer expRmvRet) throws Exception {
    IgniteCache<String, Integer> cache = jcache(grid);
    if (rmv) {
        assertNull(val);
        switch(op) {
            case UPDATE:
                {
                    assertEquals(expRmvRet, cache.getAndRemove(key));
                    break;
                }
            case UPDATEX:
                {
                    cache.remove(key);
                    break;
                }
            case TRANSFORM:
                {
                    cache.invoke(key, new EntryProcessor<String, Integer, Void>() {

                        @Override
                        public Void process(MutableEntry<String, Integer> e, Object... args) {
                            Integer old = e.getValue();
                            assertEquals(expOld, old);
                            e.remove();
                            return null;
                        }
                    });
                    break;
                }
            default:
                fail();
        }
    } else {
        switch(op) {
            case UPDATE:
                {
                    assertEquals(expOld, cache.getAndPut(key, val));
                    break;
                }
            case UPDATEX:
                {
                    cache.put(key, val);
                    break;
                }
            case TRANSFORM:
                {
                    cache.invoke(key, new EntryProcessor<String, Integer, Void>() {

                        @Override
                        public Void process(MutableEntry<String, Integer> e, Object... args) {
                            Integer old = e.getValue();
                            assertEquals(expOld, old);
                            e.setValue(val);
                            return null;
                        }
                    });
                    break;
                }
            default:
                fail();
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessor(javax.cache.processor.EntryProcessor) MutableEntry(javax.cache.processor.MutableEntry)

Example 7 with MutableEntry

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

the class CacheContinuousQueryOrderingEventTest method doOrderingTest.

/**
     * @param ccfg Cache configuration.
     * @param async Async filter.
     * @throws Exception If failed.
     */
protected void doOrderingTest(final CacheConfiguration ccfg, final boolean async) throws Exception {
    ignite(0).createCache(ccfg);
    List<QueryCursor<?>> qries = new ArrayList<>();
    try {
        List<BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>>> rcvdEvts = new ArrayList<>(LISTENER_CNT * NODES);
        final AtomicInteger qryCntr = new AtomicInteger(0);
        final int threadCnt = 20;
        for (int idx = 0; idx < NODES; idx++) {
            for (int i = 0; i < LISTENER_CNT; i++) {
                BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue = new ArrayBlockingQueue<>(ITERATION_CNT * threadCnt);
                ContinuousQuery qry = new ContinuousQuery();
                if (async) {
                    qry.setLocalListener(new TestCacheAsyncEventListener(queue, qryCntr));
                    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilterAsync(ccfg.getName())));
                } else {
                    qry.setLocalListener(new TestCacheEventListener(queue, qryCntr));
                    qry.setRemoteFilterFactory(FactoryBuilder.factoryOf(new CacheTestRemoteFilter(ccfg.getName())));
                }
                rcvdEvts.add(queue);
                IgniteCache<Object, Object> cache = grid(idx).cache(ccfg.getName());
                QueryCursor qryCursor = cache.query(qry);
                qries.add(qryCursor);
            }
        }
        IgniteInternalFuture<Long> f = GridTestUtils.runMultiThreadedAsync(new Runnable() {

            @Override
            public void run() {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < ITERATION_CNT; i++) {
                    IgniteCache<QueryTestKey, QueryTestValue> cache = grid(rnd.nextInt(NODES)).cache(ccfg.getName());
                    QueryTestKey key = new QueryTestKey(rnd.nextInt(KEYS));
                    boolean startTx = cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() == TRANSACTIONAL && rnd.nextBoolean();
                    Transaction tx = null;
                    if (startTx)
                        tx = cache.unwrap(Ignite.class).transactions().txStart();
                    try {
                        if ((cache.get(key) == null) || rnd.nextBoolean()) {
                            cache.invoke(key, new CacheEntryProcessor<QueryTestKey, QueryTestValue, Object>() {

                                @Override
                                public Object process(MutableEntry<QueryTestKey, QueryTestValue> entry, Object... arguments) throws EntryProcessorException {
                                    if (entry.exists())
                                        entry.setValue(new QueryTestValue(entry.getValue().val1 + 1));
                                    else
                                        entry.setValue(new QueryTestValue(0));
                                    return null;
                                }
                            });
                        } else {
                            QueryTestValue val;
                            QueryTestValue newVal;
                            do {
                                val = cache.get(key);
                                newVal = val == null ? new QueryTestValue(0) : new QueryTestValue(val.val1 + 1);
                            } while (!cache.replace(key, val, newVal));
                        }
                    } finally {
                        if (tx != null)
                            tx.commit();
                    }
                }
            }
        }, threadCnt, "put-thread");
        f.get(15, TimeUnit.SECONDS);
        GridTestUtils.waitForCondition(new PA() {

            @Override
            public boolean apply() {
                return qryCntr.get() >= ITERATION_CNT * threadCnt * LISTENER_CNT * NODES;
            }
        }, 1000L);
        for (BlockingQueue<CacheEntryEvent<QueryTestKey, QueryTestValue>> queue : rcvdEvts) checkEvents(queue, ITERATION_CNT * threadCnt);
        assertFalse("Ordering invocations of filter broken.", fail);
    } finally {
        for (QueryCursor<?> qry : qries) qry.close();
        ignite(0).destroyCache(ccfg.getName());
    }
}
Also used : ArrayList(java.util.ArrayList) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) QueryCursor(org.apache.ignite.cache.query.QueryCursor) BlockingQueue(java.util.concurrent.BlockingQueue) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) IgniteCache(org.apache.ignite.IgniteCache) PA(org.apache.ignite.internal.util.typedef.PA) Transaction(org.apache.ignite.transactions.Transaction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor)

Example 8 with MutableEntry

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

the class IgniteClientReconnectApiExceptionTest method cacheOperationsTest.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
public void cacheOperationsTest() throws Exception {
    clientMode = true;
    final Ignite client = startGrid(serverCount());
    final IgniteCache<Object, Object> dfltCache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(dfltCache);
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check put and get operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.getAndPut(9999, 9999);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.getAndPut(9999, 9999);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            assertEquals(9999, dfltCache.get(9999));
            return true;
        }
    }), // Check put operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.put(10000, 10000);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            dfltCache.put(10000, 10000);
            return true;
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertTrue((Boolean) o);
            assertEquals(10000, dfltCache.get(10000));
            return true;
        }
    }), // Check get operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.get(10001);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.get(10001);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            return true;
        }
    }), // Check invoke operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.invoke(10000, new CacheEntryProcessor<Object, Object, Object>() {

                    @Override
                    public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
                        assertTrue(entry.exists());
                        return (int) entry.getValue() * 2;
                    }
                });
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.invoke(10000, new CacheEntryProcessor<Object, Object, Object>() {

                @Override
                public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
                    assertTrue(entry.exists());
                    return (int) entry.getValue() * 2;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            assertEquals(20000, (int) o);
            return true;
        }
    }), // Check put async operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.putAsync(10002, 10002).get();
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.putAsync(10002, 10002).get();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            assertEquals(10002, dfltCache.get(10002));
            return true;
        }
    }), // Check transaction.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.transactions();
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.transactions();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteTransactions txs = (IgniteTransactions) o;
            assertNotNull(txs);
            return true;
        }
    }), // Check get cache.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.cache(DEFAULT_CACHE_NAME);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.cache(DEFAULT_CACHE_NAME);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCache<Object, Object> cache0 = (IgniteCache<Object, Object>) o;
            assertNotNull(cache0);
            cache0.put(1, 1);
            assertEquals(1, cache0.get(1));
            return true;
        }
    }), // Check streamer.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.dataStreamer(DEFAULT_CACHE_NAME);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.dataStreamer(DEFAULT_CACHE_NAME);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteDataStreamer<Object, Object> streamer = (IgniteDataStreamer<Object, Object>) o;
            streamer.addData(2, 2);
            streamer.close();
            assertEquals(2, client.cache(DEFAULT_CACHE_NAME).get(2));
            return true;
        }
    }), // Check create cache.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.createCache("test_cache");
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.createCache("test_cache");
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCache<Object, Object> cache = (IgniteCache<Object, Object>) o;
            assertNotNull(cache);
            cache.put(1, 1);
            assertEquals(1, cache.get(1));
            return true;
        }
    })));
    clientMode = false;
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

MutableEntry (javax.cache.processor.MutableEntry)8 EntryProcessorException (javax.cache.processor.EntryProcessorException)5 Ignite (org.apache.ignite.Ignite)5 EntryProcessor (javax.cache.processor.EntryProcessor)4 IgniteCache (org.apache.ignite.IgniteCache)4 CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)4 Transaction (org.apache.ignite.transactions.Transaction)4 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CacheEntryEvent (javax.cache.event.CacheEntryEvent)3 IgniteException (org.apache.ignite.IgniteException)3 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)3 QueryCursor (org.apache.ignite.cache.query.QueryCursor)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)2 IgniteBiInClosure (org.apache.ignite.lang.IgniteBiInClosure)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 BlockingQueue (java.util.concurrent.BlockingQueue)1