Search in sources :

Example 1 with CachePeekMode

use of org.apache.ignite.cache.CachePeekMode in project ignite by apache.

the class IgfsAbstractSelfTest method testFormat.

/**
 * Ensure that formatting is not propagated to the secondary file system.
 *
 * @throws Exception If failed.
 */
@SuppressWarnings("ConstantConditions")
public void testFormat() throws Exception {
    if (mode == PROXY)
        return;
    final GridCacheAdapter<IgfsBlockKey, byte[]> dataCache = getDataCache(igfs);
    assert dataCache != null;
    int size0 = dataCache.size(new CachePeekMode[] { CachePeekMode.ALL });
    assert size0 == 0 : "Initial data cache size = " + size0;
    if (dual)
        create(igfsSecondary, paths(DIR, SUBDIR, DIR_NEW, SUBDIR_NEW), paths(FILE, FILE_NEW));
    create(igfs, paths(DIR, SUBDIR), paths(FILE));
    try (IgfsOutputStream os = igfs.create(FILE, true)) {
        os.write(new byte[10 * 1024 * 1024]);
    }
    awaitFileClose(igfs, FILE);
    if (dual)
        checkExist(igfsSecondary, DIR, SUBDIR, FILE, DIR_NEW, SUBDIR_NEW, FILE_NEW);
    checkExist(igfs, DIR, SUBDIR, FILE);
    assertEquals(10 * 1024 * 1024, igfs.info(FILE).length());
    assert dataCache.size(new CachePeekMode[] { CachePeekMode.ALL }) > 0;
    igfs.clear();
    // Ensure format is not propagated to the secondary file system.
    if (dual) {
        checkExist(igfsSecondary, DIR, SUBDIR, FILE, DIR_NEW, SUBDIR_NEW, FILE_NEW);
        igfsSecondary.format();
    }
    // Ensure entries deletion in the primary file system.
    checkNotExist(igfs, DIR, SUBDIR, FILE);
    if (!GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            try {
                return dataCache.size(new CachePeekMode[] { CachePeekMode.ALL }) == 0;
            } catch (IgniteCheckedException ice) {
                throw new IgniteException(ice);
            }
        }
    }, 10_000)) {
        Iterable<? extends GridCacheEntryEx> entries = dataCache.allEntries();
        for (GridCacheEntryEx e : entries) {
            X.println("deleted = " + e.deleted());
            X.println("detached = " + e.detached());
            X.println("info = " + e.info());
            X.println("k = " + e.key() + ", v = " + e.valueBytes());
        }
        assert false;
    }
}
Also used : GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) CachePeekMode(org.apache.ignite.cache.CachePeekMode) IgniteException(org.apache.ignite.IgniteException) IgfsOutputStream(org.apache.ignite.igfs.IgfsOutputStream)

Example 2 with CachePeekMode

use of org.apache.ignite.cache.CachePeekMode in project ignite by apache.

the class IgniteCachePeekModesAbstractTest method checkEmpty.

/**
 * Checks size is zero.
 */
private void checkEmpty() {
    for (int i = 0; i < gridCount(); i++) {
        IgniteCache<Integer, String> cache = jcache(i);
        assertEquals(0, cache.localSize());
        assertEquals(0, cache.size());
        for (CachePeekMode peekMode : CachePeekMode.values()) {
            assertEquals(0, cache.localSize(peekMode));
            assertEquals(0, cache.size(peekMode));
        }
    }
    checkPrimarySize(0);
}
Also used : CachePeekMode(org.apache.ignite.cache.CachePeekMode)

Example 3 with CachePeekMode

use of org.apache.ignite.cache.CachePeekMode in project ignite by apache.

the class CacheKeepBinaryIterationTest method doTestLocalEntries.

/**
 * @param ccfg Cache configuration.
 */
private void doTestLocalEntries(final CacheConfiguration<Object, Object> ccfg, boolean keepBinary, boolean primitives) throws Exception {
    IgniteCache<Object, Object> cache = grid(0).createCache(ccfg);
    assertEquals(0, cache.size());
    try {
        for (int i = 0; i < KEYS; i++) if (primitives)
            cache.put(i, i);
        else
            cache.put(new QueryTestKey(i), new QueryTestValue(i));
        for (int i = 0; i < getServerNodeCount(); i++) {
            IgniteCache<Object, Object> cache0 = grid(i).cache(ccfg.getName());
            if (keepBinary)
                cache0 = cache0.withKeepBinary();
            for (CachePeekMode mode : CachePeekMode.values()) {
                int size = 0;
                for (Cache.Entry<Object, Object> e : cache0.localEntries(mode)) {
                    Object key = e.getKey();
                    Object val = e.getValue();
                    if (!primitives) {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == key instanceof BinaryObject);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, keepBinary == val instanceof BinaryObject);
                    } else {
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, key instanceof Integer);
                        assertTrue("Got unexpected object: " + key.getClass() + ", keepBinary: " + keepBinary, val instanceof Integer);
                    }
                    ++size;
                }
                if (mode == CachePeekMode.ALL || mode == CachePeekMode.PRIMARY || mode == CachePeekMode.BACKUP || (mode == CachePeekMode.NEAR && i == 0 && ccfg.getNearConfiguration() != null))
                    assertTrue("Zero result at mode: " + mode, size > 0);
            }
        }
    } finally {
        if (ccfg.getEvictionPolicy() != null) {
            // TODO: IGNITE-3462. Fixes evictionPolicy issues at cache destroy.
            stopAllGrids();
            startGridsMultiThreaded(getServerNodeCount());
        } else
            grid(0).destroyCache(ccfg.getName());
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) CachePeekMode(org.apache.ignite.cache.CachePeekMode) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 4 with CachePeekMode

use of org.apache.ignite.cache.CachePeekMode in project ignite by apache.

the class EvictionPolicyFailureHandlerTest method testCacheMapDoesNotContainsWrongEntityAfterTransaction.

/**
 * We expect that localPeek produces an exception, but the entry evict returns false because the transaction locks
 * this entry. After transaction commit, the entry will be evicted.
 */
@Test
public void testCacheMapDoesNotContainsWrongEntityAfterTransaction() throws Exception {
    LogListener lsnr = LogListener.matches(s -> s.contains("The cache entry cannot be touched")).times(1).build();
    log.registerListener(lsnr);
    IgniteEx node = startGrid(0);
    IgniteEx client = startClientGrid(1);
    GridCacheAdapter<Object, Object> cache = ((IgniteKernal) node).internalCache(DEFAULT_CACHE_NAME);
    cache.put(1, 1);
    CountDownLatch locPeekFinished = new CountDownLatch(1);
    CountDownLatch txStarted = new CountDownLatch(1);
    CountDownLatch txFinished = new CountDownLatch(1);
    GridTestUtils.runAsync(() -> {
        IgniteCache<Object, Object> cache1 = client.cache(DEFAULT_CACHE_NAME);
        IgniteTransactions transactions = client.transactions();
        try (Transaction tx = transactions.txStart(PESSIMISTIC, REPEATABLE_READ)) {
            cache1.put(2.1, 2.4);
            txStarted.countDown();
            locPeekFinished.await();
            tx.commit();
        } catch (Exception ignore) {
        }
        txFinished.countDown();
    }, "tx-thread");
    txStarted.await();
    try {
        cache.localPeek(2.1, new CachePeekMode[] { CachePeekMode.ONHEAP });
    } catch (Exception ignore) {
    }
    locPeekFinished.countDown();
    assertTrue(lsnr.check(10_000));
    txFinished.await();
    assertFalse(cache.map().entrySet(cache.context().cacheId()).stream().anyMatch(e -> new Double(2.1).equals(e.key().value(null, false))));
    assertEquals(ACTIVE, node.cluster().state());
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) LogListener(org.apache.ignite.testframework.LogListener) GridCacheAdapter(org.apache.ignite.internal.processors.cache.GridCacheAdapter) Affinity(org.apache.ignite.cache.affinity.Affinity) Transaction(org.apache.ignite.transactions.Transaction) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteEx(org.apache.ignite.internal.IgniteEx) REPEATABLE_READ(org.apache.ignite.transactions.TransactionIsolation.REPEATABLE_READ) Factory(javax.cache.configuration.Factory) IgniteKernal(org.apache.ignite.internal.IgniteKernal) ACTIVE(org.apache.ignite.cluster.ClusterState.ACTIVE) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) CachePeekMode(org.apache.ignite.cache.CachePeekMode) FailureHandler(org.apache.ignite.failure.FailureHandler) Test(org.junit.Test) IgniteCache(org.apache.ignite.IgniteCache) Serializable(java.io.Serializable) SortedEvictionPolicy(org.apache.ignite.cache.eviction.sorted.SortedEvictionPolicy) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) SortedEvictionPolicyFactory(org.apache.ignite.cache.eviction.sorted.SortedEvictionPolicyFactory) CountDownLatch(java.util.concurrent.CountDownLatch) EvictionPolicy(org.apache.ignite.cache.eviction.EvictionPolicy) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) PESSIMISTIC(org.apache.ignite.transactions.TransactionConcurrency.PESSIMISTIC) IgniteTransactions(org.apache.ignite.IgniteTransactions) EvictableEntry(org.apache.ignite.cache.eviction.EvictableEntry) IgniteKernal(org.apache.ignite.internal.IgniteKernal) LogListener(org.apache.ignite.testframework.LogListener) Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteTransactions(org.apache.ignite.IgniteTransactions) GridAbstractTest(org.apache.ignite.testframework.junits.GridAbstractTest) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with CachePeekMode

use of org.apache.ignite.cache.CachePeekMode in project ignite by apache.

the class GridCacheAdapter method parsePeekModes.

/**
 * @param peekModes Cache peek modes array.
 * @param primary Defines the default behavior if affinity flags are not specified.
 * @return Peek modes flags.
 */
protected static PeekModes parsePeekModes(CachePeekMode[] peekModes, boolean primary) {
    PeekModes modes = new PeekModes();
    if (F.isEmpty(peekModes)) {
        modes.primary = true;
        if (!primary) {
            modes.backup = true;
            modes.near = true;
        }
        modes.heap = true;
        modes.offheap = true;
    } else {
        for (CachePeekMode peekMode : peekModes) {
            A.notNull(peekMode, "peekMode");
            switch(peekMode) {
                case ALL:
                    modes.near = true;
                    modes.primary = true;
                    modes.backup = true;
                    modes.heap = true;
                    modes.offheap = true;
                    break;
                case BACKUP:
                    modes.backup = true;
                    break;
                case PRIMARY:
                    modes.primary = true;
                    break;
                case NEAR:
                    modes.near = true;
                    break;
                case ONHEAP:
                    modes.heap = true;
                    break;
                case OFFHEAP:
                    modes.offheap = true;
                    break;
                default:
                    assert false : peekMode;
            }
        }
    }
    if (!(modes.heap || modes.offheap)) {
        modes.heap = true;
        modes.offheap = true;
    }
    if (!(modes.primary || modes.backup || modes.near)) {
        modes.primary = true;
        if (!primary) {
            modes.backup = true;
            modes.near = true;
        }
    }
    assert modes.heap || modes.offheap;
    assert modes.primary || modes.backup || modes.near;
    return modes;
}
Also used : CachePeekMode(org.apache.ignite.cache.CachePeekMode)

Aggregations

CachePeekMode (org.apache.ignite.cache.CachePeekMode)7 Cache (javax.cache.Cache)2 IgniteCache (org.apache.ignite.IgniteCache)2 IgniteEx (org.apache.ignite.internal.IgniteEx)2 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Factory (javax.cache.configuration.Factory)1 Ignite (org.apache.ignite.Ignite)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 IgniteTransactions (org.apache.ignite.IgniteTransactions)1 BinaryObject (org.apache.ignite.binary.BinaryObject)1 CacheAtomicityMode (org.apache.ignite.cache.CacheAtomicityMode)1 Affinity (org.apache.ignite.cache.affinity.Affinity)1 EvictableEntry (org.apache.ignite.cache.eviction.EvictableEntry)1 EvictionPolicy (org.apache.ignite.cache.eviction.EvictionPolicy)1