Search in sources :

Example 31 with IgniteCache

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

the class CacheSerializableTransactionsTest method testTxConflictRemoveWithOldValue.

/**
 * @throws Exception If failed.
 */
public void testTxConflictRemoveWithOldValue() throws Exception {
    Ignite ignite0 = ignite(0);
    final IgniteTransactions txs = ignite0.transactions();
    for (CacheConfiguration<Integer, Integer> ccfg : cacheConfigurations()) {
        logCacheInfo(ccfg);
        try {
            IgniteCache<Integer, Integer> cache = ignite0.createCache(ccfg);
            List<Integer> keys = testKeys(cache);
            for (final Integer key : keys) {
                log.info("Test key: " + key);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean rmv = cache.remove(key, 2);
                        assertFalse(rmv);
                        updateKey(cache, key, 1);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 1);
                    assertTrue(rmv);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                cache.remove(key);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 2);
                    assertFalse(rmv);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 2);
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean rmv = cache.remove(key, 2);
                        assertTrue(rmv);
                        updateKey(cache, key, 3);
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, 3, cache.getName());
                try {
                    try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                        boolean rmv = cache.remove(key, 3);
                        assertTrue(rmv);
                        txAsync(cache, OPTIMISTIC, SERIALIZABLE, new IgniteClosure<IgniteCache<Integer, Integer>, Void>() {

                            @Override
                            public Void apply(IgniteCache<Integer, Integer> cache) {
                                cache.remove(key);
                                return null;
                            }
                        });
                        tx.commit();
                    }
                    fail();
                } catch (TransactionOptimisticException e) {
                    log.info("Expected exception: " + e);
                }
                checkValue(key, null, cache.getName());
                cache.put(key, 1);
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 2);
                    assertFalse(rmv);
                    tx.commit();
                }
                checkValue(key, 1, cache.getName());
                try (Transaction tx = txs.txStart(OPTIMISTIC, SERIALIZABLE)) {
                    boolean rmv = cache.remove(key, 1);
                    assertTrue(rmv);
                    tx.commit();
                }
                checkValue(key, null, cache.getName());
            }
        } finally {
            destroyCache(ccfg.getName());
        }
    }
}
Also used : TransactionOptimisticException(org.apache.ignite.transactions.TransactionOptimisticException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite)

Example 32 with IgniteCache

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

the class IgniteWalRecoveryTest method testHugeCheckpointRecord.

/**
 * @throws Exception if failed.
 */
public void testHugeCheckpointRecord() throws Exception {
    try {
        final IgniteEx ignite = startGrid(1);
        ignite.active(true);
        for (int i = 0; i < 50; i++) {
            CacheConfiguration<Object, Object> ccfg = new CacheConfiguration<>("cache-" + i);
            // We can get 'too many open files' with default number of partitions.
            ccfg.setAffinity(new RendezvousAffinityFunction(false, 128));
            IgniteCache<Object, Object> cache = ignite.getOrCreateCache(ccfg);
            cache.put(i, i);
        }
        final long endTime = System.currentTimeMillis() + 30_000;
        IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Random rnd = ThreadLocalRandom.current();
                while (U.currentTimeMillis() < endTime) {
                    IgniteCache<Object, Object> cache = ignite.cache("cache-" + rnd.nextInt(50));
                    cache.put(rnd.nextInt(50_000), rnd.nextInt());
                }
                return null;
            }
        }, 16, "put-thread");
        while (System.currentTimeMillis() < endTime) {
            ignite.context().cache().context().database().wakeupForCheckpoint("test").get();
            U.sleep(500);
        }
        fut.get();
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IgniteEx(org.apache.ignite.internal.IgniteEx) RendezvousAffinityFunction(org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 33 with IgniteCache

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

the class IgniteWalReaderTest method testFillWalWithDifferentTypes.

/**
 * @throws Exception if failed.
 */
public void testFillWalWithDifferentTypes() throws Exception {
    int cntEntries;
    final Map<Object, Object> ctrlMap = new HashMap<>();
    final Map<Object, Object> ctrlMapForBinaryObjects = new HashMap<>();
    final Collection<String> ctrlStringsToSearch = new HashSet<>();
    final Collection<String> ctrlStringsForBinaryObjSearch = new HashSet<>();
    final Ignite ignite0 = startGrid("node0");
    ignite0.active(true);
    final IgniteCache<Object, Object> addlCache = ignite0.getOrCreateCache(CACHE_ADDL_NAME);
    addlCache.put("1", "2");
    addlCache.put(1, 2);
    addlCache.put(1L, 2L);
    addlCache.put(TestEnum.A, "Enum_As_Key");
    addlCache.put("Enum_As_Value", TestEnum.B);
    addlCache.put(TestEnum.C, TestEnum.C);
    addlCache.put("Serializable", new TestSerializable(42));
    addlCache.put(new TestSerializable(42), "Serializable_As_Key");
    addlCache.put("Externalizable", new TestExternalizable(42));
    addlCache.put(new TestExternalizable(42), "Externalizable_As_Key");
    addlCache.put(292, new IndexedObject(292));
    final String search1 = "SomeUnexpectedStringValueAsKeyToSearch";
    ctrlStringsToSearch.add(search1);
    ctrlStringsForBinaryObjSearch.add(search1);
    addlCache.put(search1, "SearchKey");
    String search2 = "SomeTestStringContainerToBePrintedLongLine";
    final TestStringContainerToBePrinted val = new TestStringContainerToBePrinted(search2);
    // will validate original toString() was called
    ctrlStringsToSearch.add(val.toString());
    ctrlStringsForBinaryObjSearch.add(search2);
    addlCache.put("SearchValue", val);
    String search3 = "SomeTestStringContainerToBePrintedLongLine2";
    final TestStringContainerToBePrinted key = new TestStringContainerToBePrinted(search3);
    // will validate original toString() was called
    ctrlStringsToSearch.add(key.toString());
    // validate only string itself
    ctrlStringsForBinaryObjSearch.add(search3);
    addlCache.put(key, "SearchKey");
    cntEntries = addlCache.size();
    for (Cache.Entry<Object, Object> next : addlCache) ctrlMap.put(next.getKey(), next.getValue());
    for (Cache.Entry<Object, Object> next : addlCache) ctrlMapForBinaryObjects.put(next.getKey(), next.getValue());
    final String subfolderName = genDbSubfolderName(ignite0, 0);
    stopGrid("node0");
    final String workDir = U.defaultWorkDirectory();
    final File binaryMeta = U.resolveWorkDirectory(workDir, "binary_meta", false);
    final File binaryMetaWithNodeSubfolder = new File(binaryMeta, subfolderName);
    final File marshallerMapping = U.resolveWorkDirectory(workDir, "marshaller", false);
    final IgniteWalIteratorFactory factory = createWalIteratorFactory(workDir, subfolderName);
    final IgniteBiInClosure<Object, Object> objConsumer = new IgniteBiInClosure<Object, Object>() {

        @Override
        public void apply(Object key, Object val) {
            log.info("K: [" + key + ", " + (key != null ? key.getClass().getName() : "?") + "]" + " V: [" + val + ", " + (val != null ? val.getClass().getName() : "?") + "]");
            boolean rmv = remove(ctrlMap, key, val);
            if (!rmv) {
                String msg = "Unable to remove pair from control map " + "K: [" + key + "] V: [" + val + "]";
                log.error(msg);
            }
            assertFalse(val instanceof BinaryObject);
        }
    };
    final IgniteInClosure<DataRecord> toStrChecker = new IgniteInClosure<DataRecord>() {

        @Override
        public void apply(DataRecord record) {
            String strRepresentation = record.toString();
            for (Iterator<String> iter = ctrlStringsToSearch.iterator(); iter.hasNext(); ) {
                final String next = iter.next();
                if (strRepresentation.contains(next)) {
                    iter.remove();
                    break;
                }
            }
        }
    };
    scanIterateAndCount(factory, workDir, subfolderName, cntEntries, 0, objConsumer, toStrChecker);
    assertTrue(" Control Map is not empty after reading entries: " + ctrlMap, ctrlMap.isEmpty());
    assertTrue(" Control Map for strings in entries is not empty after" + " reading records: " + ctrlStringsToSearch, ctrlStringsToSearch.isEmpty());
    // Validate same WAL log with flag binary objects only
    final IgniteWalIteratorFactory keepBinFactory = new IgniteWalIteratorFactory(log, PAGE_SIZE, binaryMetaWithNodeSubfolder, marshallerMapping, true);
    final IgniteBiInClosure<Object, Object> binObjConsumer = new IgniteBiInClosure<Object, Object>() {

        @Override
        public void apply(Object key, Object val) {
            log.info("K(KeepBinary): [" + key + ", " + (key != null ? key.getClass().getName() : "?") + "]" + " V(KeepBinary): [" + val + ", " + (val != null ? val.getClass().getName() : "?") + "]");
            boolean rmv = remove(ctrlMapForBinaryObjects, key, val);
            if (!rmv) {
                if (key instanceof BinaryObject) {
                    BinaryObject keyBinObj = (BinaryObject) key;
                    String binaryObjTypeName = keyBinObj.type().typeName();
                    if (Objects.equals(TestStringContainerToBePrinted.class.getName(), binaryObjTypeName)) {
                        String data = keyBinObj.field("data");
                        rmv = ctrlMapForBinaryObjects.remove(new TestStringContainerToBePrinted(data)) != null;
                    } else if (Objects.equals(TestSerializable.class.getName(), binaryObjTypeName)) {
                        Integer iVal = keyBinObj.field("iVal");
                        rmv = ctrlMapForBinaryObjects.remove(new TestSerializable(iVal)) != null;
                    } else if (Objects.equals(TestEnum.class.getName(), binaryObjTypeName)) {
                        TestEnum key1 = TestEnum.values()[keyBinObj.enumOrdinal()];
                        rmv = ctrlMapForBinaryObjects.remove(key1) != null;
                    }
                } else if (val instanceof BinaryObject) {
                    // don't compare BO values, just remove by key
                    rmv = ctrlMapForBinaryObjects.remove(key) != null;
                }
            }
            if (!rmv)
                log.error("Unable to remove pair from control map " + "K: [" + key + "] V: [" + val + "]");
            if (val instanceof BinaryObject) {
                BinaryObject binaryObj = (BinaryObject) val;
                String binaryObjTypeName = binaryObj.type().typeName();
                if (Objects.equals(IndexedObject.class.getName(), binaryObjTypeName)) {
                    assertEquals(binaryObj.field("iVal").toString(), binaryObj.field("jVal").toString());
                    byte[] data = binaryObj.field("data");
                    for (byte datum : data) assertTrue(datum >= 'A' && datum <= 'A' + 10);
                }
            }
        }
    };
    final IgniteInClosure<DataRecord> binObjToStrChecker = new IgniteInClosure<DataRecord>() {

        @Override
        public void apply(DataRecord record) {
            String strRepresentation = record.toString();
            for (Iterator<String> iter = ctrlStringsForBinaryObjSearch.iterator(); iter.hasNext(); ) {
                final String next = iter.next();
                if (strRepresentation.contains(next)) {
                    iter.remove();
                    break;
                }
            }
        }
    };
    scanIterateAndCount(keepBinFactory, workDir, subfolderName, cntEntries, 0, binObjConsumer, binObjToStrChecker);
    assertTrue(" Control Map is not empty after reading entries: " + ctrlMapForBinaryObjects, ctrlMapForBinaryObjects.isEmpty());
    assertTrue(" Control Map for strings in entries is not empty after" + " reading records: " + ctrlStringsForBinaryObjSearch, ctrlStringsForBinaryObjSearch.isEmpty());
}
Also used : IgniteWalIteratorFactory(org.apache.ignite.internal.processors.cache.persistence.wal.reader.IgniteWalIteratorFactory) HashMap(java.util.HashMap) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) BinaryObject(org.apache.ignite.binary.BinaryObject) Ignite(org.apache.ignite.Ignite) DataRecord(org.apache.ignite.internal.pagemem.wal.record.DataRecord) HashSet(java.util.HashSet) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) File(java.io.File) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 34 with IgniteCache

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

the class GridCacheSetAbstractSelfTest method assertSetIteratorsCleared.

/**
 * Checks internal iterators maps are cleared.
 */
private void assertSetIteratorsCleared() {
    for (int i = 0; i < gridCount(); i++) {
        IgniteKernal grid = (IgniteKernal) grid(i);
        for (IgniteCache cache : grid.caches()) {
            GridCacheQueryManager queries = grid.internalCache(cache.getName()).context().queries();
            Map map = GridTestUtils.getFieldValue(queries, GridCacheQueryManager.class, "qryIters");
            for (Object obj : map.values()) assertEquals("Iterators not removed for grid " + i, 0, ((Map) obj).size());
        }
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCache(org.apache.ignite.IgniteCache) GridCacheQueryManager(org.apache.ignite.internal.processors.cache.query.GridCacheQueryManager) Map(java.util.Map)

Example 35 with IgniteCache

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

Aggregations

IgniteCache (org.apache.ignite.IgniteCache)403 Ignite (org.apache.ignite.Ignite)233 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)137 Cache (javax.cache.Cache)98 Transaction (org.apache.ignite.transactions.Transaction)87 ArrayList (java.util.ArrayList)69 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)65 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)60 Map (java.util.Map)55 IgniteException (org.apache.ignite.IgniteException)55 List (java.util.List)51 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)48 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)46 HashMap (java.util.HashMap)45 CacheException (javax.cache.CacheException)43 Random (java.util.Random)37 SqlFieldsQuery (org.apache.ignite.cache.query.SqlFieldsQuery)32 CountDownLatch (java.util.concurrent.CountDownLatch)30 ClusterNode (org.apache.ignite.cluster.ClusterNode)30 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)28