Search in sources :

Example 21 with IgniteCacheProxy

use of org.apache.ignite.internal.processors.cache.IgniteCacheProxy in project ignite by apache.

the class TxDeadlockDetectionMessageMarshallingTest method testMessageUnmarshallWithoutCacheContext.

/**
 * @throws Exception If failed.
 */
@Test
public void testMessageUnmarshallWithoutCacheContext() throws Exception {
    try {
        Ignite ignite = startGrid(0);
        CacheConfiguration<Integer, Integer> ccfg = new CacheConfiguration<>(DEFAULT_CACHE_NAME);
        IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(ccfg);
        Ignite client = startClientGrid(1);
        final GridCacheSharedContext<Object, Object> clientCtx = ((IgniteKernal) client).context().cache().context();
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicBoolean res = new AtomicBoolean();
        clientCtx.gridIO().addMessageListener(TOPIC, new GridMessageListener() {

            @Override
            public void onMessage(UUID nodeId, Object msg, byte plc) {
                if (msg instanceof TxLocksResponse) {
                    try {
                        ((TxLocksResponse) msg).finishUnmarshal(clientCtx, clientCtx.deploy().globalLoader());
                        res.set(true);
                    } catch (Exception e) {
                        log.error("Message unmarshal failed", e);
                    } finally {
                        latch.countDown();
                    }
                }
            }
        });
        GridCacheContext cctx = ((IgniteCacheProxy) cache).context();
        KeyCacheObject key = cctx.toCacheKeyObject(1);
        TxLocksResponse msg = new TxLocksResponse();
        msg.addKey(cctx.txKey(key));
        msg.prepareMarshal(cctx.shared());
        ((IgniteKernal) ignite).context().cache().context().gridIO().sendToCustomTopic(((IgniteKernal) client).localNode(), TOPIC, msg, GridIoPolicy.PUBLIC_POOL);
        boolean await = latch.await(1, TimeUnit.SECONDS);
        assertTrue(await && res.get());
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Ignite(org.apache.ignite.Ignite) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) UUID(java.util.UUID) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 22 with IgniteCacheProxy

use of org.apache.ignite.internal.processors.cache.IgniteCacheProxy in project ignite by apache.

the class CacheMvccBackupsAbstractTest method allVersions.

/**
 * Retrieves all versions of all keys from cache.
 *
 * @param cache Cache.
 * @return {@link Map} of keys to its versions.
 * @throws IgniteCheckedException If failed.
 */
private Map<KeyCacheObject, List<CacheDataRow>> allVersions(IgniteCache cache) throws IgniteCheckedException {
    IgniteCacheProxy cache0 = (IgniteCacheProxy) cache;
    GridCacheContext cctx = cache0.context();
    assert cctx.mvccEnabled();
    Map<KeyCacheObject, List<CacheDataRow>> vers = new HashMap<>();
    for (Object e : cache) {
        IgniteBiTuple entry = (IgniteBiTuple) e;
        KeyCacheObject key = cctx.toCacheKeyObject(entry.getKey());
        GridCursor<CacheDataRow> cur = cctx.offheap().mvccAllVersionsCursor(cctx, key, null);
        List<CacheDataRow> rows = new ArrayList<>();
        while (cur.next()) {
            CacheDataRow row = cur.get();
            rows.add(row);
        }
        vers.put(key, rows);
    }
    return vers;
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) HashMap(java.util.HashMap) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject)

Example 23 with IgniteCacheProxy

use of org.apache.ignite.internal.processors.cache.IgniteCacheProxy in project ignite by apache.

the class CacheMvccSqlUpdateCountersTest method testUpdateCountersMultithreaded.

/**
 * @throws Exception If failed.
 */
@Test
public void testUpdateCountersMultithreaded() throws Exception {
    final int writers = 4;
    final int readers = 0;
    int parts = 8;
    int keys = 20;
    final Map<Integer, AtomicLong> tracker = new ConcurrentHashMap<>();
    for (int i = 0; i < keys; i++) tracker.put(i, new AtomicLong(1));
    final IgniteInClosure<IgniteCache<Object, Object>> init = new IgniteInClosure<IgniteCache<Object, Object>>() {

        @Override
        public void apply(IgniteCache<Object, Object> cache) {
            final IgniteTransactions txs = cache.unwrap(Ignite.class).transactions();
            try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                SqlFieldsQuery qry = new SqlFieldsQuery("INSERT INTO MvccTestAccount(_key, val, updateCnt) VALUES " + "(?, 0, 1)");
                for (int i = 0; i < keys; i++) {
                    try (FieldsQueryCursor<List<?>> cur = cache.query(qry.setArgs(i))) {
                        assertEquals(1L, cur.iterator().next().get(0));
                    }
                    tx.commit();
                }
            }
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> writer = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
            ThreadLocalRandom rnd = ThreadLocalRandom.current();
            Map<Integer, AtomicLong> acc = new HashMap<>();
            int v = 0;
            while (!stop.get()) {
                int cnt = rnd.nextInt(keys / 3);
                if (cnt == 0)
                    cnt = 2;
                // Generate key set to be changed in tx.
                while (acc.size() < cnt) acc.put(rnd.nextInt(cnt), new AtomicLong());
                TestCache<Integer, Integer> cache = randomCache(caches, rnd);
                boolean success = true;
                try {
                    IgniteTransactions txs = cache.cache.unwrap(Ignite.class).transactions();
                    try (Transaction tx = txs.txStart(PESSIMISTIC, REPEATABLE_READ)) {
                        Map<Integer, MvccTestAccount> allVals = readAllByMode(cache.cache, tracker.keySet(), SQL, ACCOUNT_CODEC);
                        boolean rmv = allVals.size() > keys * 2 / 3;
                        for (Map.Entry<Integer, AtomicLong> e : acc.entrySet()) {
                            int key = e.getKey();
                            AtomicLong accCntr = e.getValue();
                            boolean exists = allVals.containsKey(key);
                            int delta = 0;
                            boolean createdInTx = false;
                            if (rmv && rnd.nextBoolean()) {
                                if (exists)
                                    delta = 1;
                                SqlFieldsQuery qry = new SqlFieldsQuery("DELETE FROM MvccTestAccount WHERE _key=" + key);
                                cache.cache.query(qry).getAll();
                            } else {
                                delta = 1;
                                if (!exists)
                                    createdInTx = true;
                                SqlFieldsQuery qry = new SqlFieldsQuery("MERGE INTO MvccTestAccount " + "(_key, val, updateCnt) VALUES (" + key + ",  " + rnd.nextInt(100) + ", 1)");
                                cache.cache.query(qry).getAll();
                            }
                            if (rnd.nextBoolean()) {
                                if (createdInTx)
                                    // Do not count cases when key created and removed in the same tx.
                                    delta = 0;
                                SqlFieldsQuery qry = new SqlFieldsQuery("DELETE FROM MvccTestAccount WHERE _key=" + key);
                                cache.cache.query(qry).getAll();
                            } else {
                                delta = 1;
                                SqlFieldsQuery qry = new SqlFieldsQuery("MERGE INTO MvccTestAccount " + "(_key, val, updateCnt) VALUES (" + key + ",  " + rnd.nextInt(100) + ", 1)");
                                cache.cache.query(qry).getAll();
                            }
                            accCntr.addAndGet(delta);
                        }
                        tx.commit();
                    }
                } catch (Exception e) {
                    handleTxException(e);
                    success = false;
                    int r = 0;
                    for (Map.Entry<Integer, AtomicLong> en : acc.entrySet()) {
                        if (((IgniteCacheProxy) cache.cache).context().affinity().partition(en.getKey()) == 0)
                            r += en.getValue().intValue();
                    }
                } finally {
                    cache.readUnlock();
                    if (success) {
                        v++;
                        for (Map.Entry<Integer, AtomicLong> e : acc.entrySet()) {
                            int k = e.getKey();
                            long updCntr = e.getValue().get();
                            tracker.get(k).addAndGet(updCntr);
                        }
                        int r = 0;
                        for (Map.Entry<Integer, AtomicLong> en : acc.entrySet()) {
                            if (((IgniteCacheProxy) cache.cache).context().affinity().partition(en.getKey()) == 0)
                                r += en.getValue().intValue();
                        }
                    }
                    acc.clear();
                }
            }
            info("Writer done, updates: " + v);
        }
    };
    GridInClosure3<Integer, List<TestCache>, AtomicBoolean> reader = new GridInClosure3<Integer, List<TestCache>, AtomicBoolean>() {

        @Override
        public void apply(Integer idx, List<TestCache> caches, AtomicBoolean stop) {
        // No-op.
        }
    };
    readWriteTest(null, 4, 1, 2, parts, writers, readers, DFLT_TEST_TIME, new InitIndexing(Integer.class, MvccTestAccount.class), init, writer, reader);
    Map<Integer, AtomicLong> updPerParts = new HashMap<>(parts);
    Affinity aff = grid(1).cachex(DEFAULT_CACHE_NAME).affinity();
    for (Map.Entry<Integer, AtomicLong> e : tracker.entrySet()) {
        int k = e.getKey();
        long updCntr = e.getValue().get();
        int p = aff.partition(k);
        AtomicLong cntr = updPerParts.get(p);
        if (cntr == null) {
            cntr = new AtomicLong();
            updPerParts.putIfAbsent(p, cntr);
        }
        cntr.addAndGet(updCntr);
    }
    for (Map.Entry<Integer, AtomicLong> e : updPerParts.entrySet()) checkUpdateCounters(DEFAULT_CACHE_NAME, e.getKey(), e.getValue().get());
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteCacheProxy(org.apache.ignite.internal.processors.cache.IgniteCacheProxy) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridInClosure3(org.apache.ignite.internal.util.lang.GridInClosure3) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IgniteCache(org.apache.ignite.IgniteCache) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) Transaction(org.apache.ignite.transactions.Transaction) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) Affinity(org.apache.ignite.cache.affinity.Affinity) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

IgniteCacheProxy (org.apache.ignite.internal.processors.cache.IgniteCacheProxy)23 Ignite (org.apache.ignite.Ignite)8 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)7 Test (org.junit.Test)7 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)6 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 IgniteKernal (org.apache.ignite.internal.IgniteKernal)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 List (java.util.List)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IgniteCache (org.apache.ignite.IgniteCache)3 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)3 IgniteBiTuple (org.apache.ignite.lang.IgniteBiTuple)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Map (java.util.Map)2 Callable (java.util.concurrent.Callable)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 Cache (javax.cache.Cache)2